blob: 7eec3640bde47d29034a48133be986d05a1859eb [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200475 {
476 unsigned char session_hash[48];
477 size_t hash_len;
478
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200479 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200480
481 ssl->handshake->calc_verify( ssl, session_hash );
482
483#if defined(POLARSSL_SSL_PROTO_TLS1_2)
484 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
485 {
486#if defined(POLARSSL_SHA512_C)
487 if( ssl->transform_negotiate->ciphersuite_info->mac ==
488 POLARSSL_MD_SHA384 )
489 {
490 hash_len = 48;
491 }
492 else
493#endif
494 hash_len = 32;
495 }
496 else
497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498 hash_len = 36;
499
500 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
501
502 handshake->tls_prf( handshake->premaster, handshake->pmslen,
503 "extended master secret",
504 session_hash, hash_len, session->master, 48 );
505
506 }
507 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200508#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf( handshake->premaster, handshake->pmslen,
510 "master secret",
511 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200513
Paul Bakker34617722014-06-13 17:20:13 +0200514 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 }
516 else
517 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
518
519 /*
520 * Swap the client and server random values.
521 */
Paul Bakker48916f92012-09-16 19:57:18 +0000522 memcpy( tmp, handshake->randbytes, 64 );
523 memcpy( handshake->randbytes, tmp + 32, 32 );
524 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200525 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 /*
528 * SSLv3:
529 * key block =
530 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
532 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
533 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
534 * ...
535 *
536 * TLSv1:
537 * key block = PRF( master, "key expansion", randbytes )
538 */
Paul Bakker48916f92012-09-16 19:57:18 +0000539 handshake->tls_prf( session->master, 48, "key expansion",
540 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
543 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
544 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
545 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
547
Paul Bakker34617722014-06-13 17:20:13 +0200548 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
551 * Determine the appropriate key, IV and MAC length.
552 */
Paul Bakker68884e32013-01-07 18:20:04 +0100553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 transform->keylen = cipher_info->key_length / 8;
555
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200556 if( cipher_info->mode == POLARSSL_MODE_GCM ||
557 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200559 transform->maclen = 0;
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 transform->ivlen = 12;
562 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200563
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200564 /* Minimum length is expicit IV + tag */
565 transform->minlen = transform->ivlen - transform->fixed_ivlen
566 + ( transform->ciphersuite_info->flags &
567 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100568 }
569 else
570 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200571 int ret;
572
573 /* Initialize HMAC contexts */
574 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
575 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100576 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
578 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100579 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200581 /* Get MAC length */
582 transform->maclen = md_get_size( md_info );
583
584#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
585 /*
586 * If HMAC is to be truncated, we shall keep the leftmost bytes,
587 * (rfc 6066 page 13 or rfc 2104 section 4),
588 * so we only need to adjust the length here.
589 */
590 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
591 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
592#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
593
594 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100595 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200597 /* Minimum length */
598 if( cipher_info->mode == POLARSSL_MODE_STREAM )
599 transform->minlen = transform->maclen;
600 else
Paul Bakker68884e32013-01-07 18:20:04 +0100601 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200602 /*
603 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100604 * 1. if EtM is in use: one block plus MAC
605 * otherwise: * first multiple of blocklen greater than maclen
606 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200607 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100608#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
609 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
610 {
611 transform->minlen = transform->maclen
612 + cipher_info->block_size;
613 }
614 else
615#endif
616 {
617 transform->minlen = transform->maclen
618 + cipher_info->block_size
619 - transform->maclen % cipher_info->block_size;
620 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200621
622#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
623 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
624 ssl->minor_ver == SSL_MINOR_VERSION_1 )
625 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100626 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200627#endif
628#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
629 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
630 ssl->minor_ver == SSL_MINOR_VERSION_3 )
631 {
632 transform->minlen += transform->ivlen;
633 }
634 else
635#endif
636 {
637 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
638 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
639 }
Paul Bakker68884e32013-01-07 18:20:04 +0100640 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 }
642
643 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000644 transform->keylen, transform->minlen, transform->ivlen,
645 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 /*
648 * Finally setup the cipher contexts, IVs and MAC secrets.
649 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100650#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 if( ssl->endpoint == SSL_IS_CLIENT )
652 {
Paul Bakker48916f92012-09-16 19:57:18 +0000653 key1 = keyblk + transform->maclen * 2;
654 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Paul Bakker68884e32013-01-07 18:20:04 +0100656 mac_enc = keyblk;
657 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000658
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000659 /*
660 * This is not used in TLS v1.1.
661 */
Paul Bakker48916f92012-09-16 19:57:18 +0000662 iv_copy_len = ( transform->fixed_ivlen ) ?
663 transform->fixed_ivlen : transform->ivlen;
664 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
665 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000666 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 }
668 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100669#endif /* POLARSSL_SSL_CLI_C */
670#if defined(POLARSSL_SSL_SRV_C)
671 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 {
Paul Bakker48916f92012-09-16 19:57:18 +0000673 key1 = keyblk + transform->maclen * 2 + transform->keylen;
674 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
Paul Bakker68884e32013-01-07 18:20:04 +0100676 mac_enc = keyblk + transform->maclen;
677 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000678
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000679 /*
680 * This is not used in TLS v1.1.
681 */
Paul Bakker48916f92012-09-16 19:57:18 +0000682 iv_copy_len = ( transform->fixed_ivlen ) ?
683 transform->fixed_ivlen : transform->ivlen;
684 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
685 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000686 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100688 else
689#endif /* POLARSSL_SSL_SRV_C */
690 {
691 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
692 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
693 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200695#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100696 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
697 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100698 if( transform->maclen > sizeof transform->mac_enc )
699 {
700 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200701 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100702 }
703
Paul Bakker68884e32013-01-07 18:20:04 +0100704 memcpy( transform->mac_enc, mac_enc, transform->maclen );
705 memcpy( transform->mac_dec, mac_dec, transform->maclen );
706 }
707 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200708#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200709#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
710 defined(POLARSSL_SSL_PROTO_TLS1_2)
711 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100712 {
713 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
714 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
715 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716 else
717#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200718 {
719 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200720 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200721 }
Paul Bakker68884e32013-01-07 18:20:04 +0100722
Paul Bakker05ef8352012-05-08 09:17:57 +0000723#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200724 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000725 {
726 int ret = 0;
727
728 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
729
Paul Bakker07eb38b2012-12-19 14:42:06 +0100730 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
731 transform->iv_enc, transform->iv_dec,
732 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100733 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100734 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000735 {
736 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200737 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000738 }
739 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200740#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000741
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200742 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
743 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200745 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
746 return( ret );
747 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200748
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200749 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
750 cipher_info ) ) != 0 )
751 {
752 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
753 return( ret );
754 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200755
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200756 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
757 cipher_info->key_length,
758 POLARSSL_ENCRYPT ) ) != 0 )
759 {
760 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
761 return( ret );
762 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200763
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200764 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
765 cipher_info->key_length,
766 POLARSSL_DECRYPT ) ) != 0 )
767 {
768 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
769 return( ret );
770 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200772#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200773 if( cipher_info->mode == POLARSSL_MODE_CBC )
774 {
775 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
776 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200777 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200778 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
779 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200780 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200781
782 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
783 POLARSSL_PADDING_NONE ) ) != 0 )
784 {
785 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
786 return( ret );
787 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000788 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200789#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Paul Bakker34617722014-06-13 17:20:13 +0200791 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
Paul Bakker2770fbd2012-07-03 13:30:23 +0000793#if defined(POLARSSL_ZLIB_SUPPORT)
794 // Initialize compression
795 //
Paul Bakker48916f92012-09-16 19:57:18 +0000796 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000797 {
Paul Bakker16770332013-10-11 09:59:44 +0200798 if( ssl->compress_buf == NULL )
799 {
800 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
801 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
802 if( ssl->compress_buf == NULL )
803 {
804 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
805 SSL_BUFFER_LEN ) );
806 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
807 }
808 }
809
Paul Bakker2770fbd2012-07-03 13:30:23 +0000810 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
811
Paul Bakker48916f92012-09-16 19:57:18 +0000812 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
813 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000814
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200815 if( deflateInit( &transform->ctx_deflate,
816 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000817 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000818 {
819 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
820 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
821 }
822 }
823#endif /* POLARSSL_ZLIB_SUPPORT */
824
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
826
827 return( 0 );
828}
829
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200830#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000831void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000832{
833 md5_context md5;
834 sha1_context sha1;
835 unsigned char pad_1[48];
836 unsigned char pad_2[48];
837
Paul Bakker380da532012-04-18 16:10:25 +0000838 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Paul Bakker48916f92012-09-16 19:57:18 +0000840 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
841 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000842
Paul Bakker380da532012-04-18 16:10:25 +0000843 memset( pad_1, 0x36, 48 );
844 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakker48916f92012-09-16 19:57:18 +0000846 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000847 md5_update( &md5, pad_1, 48 );
848 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000849
Paul Bakker380da532012-04-18 16:10:25 +0000850 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000851 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000852 md5_update( &md5, pad_2, 48 );
853 md5_update( &md5, hash, 16 );
854 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Paul Bakker48916f92012-09-16 19:57:18 +0000856 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000857 sha1_update( &sha1, pad_1, 40 );
858 sha1_finish( &sha1, hash + 16 );
859
860 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000861 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000862 sha1_update( &sha1, pad_2, 40 );
863 sha1_update( &sha1, hash + 16, 20 );
864 sha1_finish( &sha1, hash + 16 );
865
866 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
867 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
868
Paul Bakker5b4af392014-06-26 12:09:34 +0200869 md5_free( &md5 );
870 sha1_free( &sha1 );
871
Paul Bakker380da532012-04-18 16:10:25 +0000872 return;
873}
Paul Bakker9af723c2014-05-01 13:03:14 +0200874#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000875
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200876#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000877void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
878{
879 md5_context md5;
880 sha1_context sha1;
881
882 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
883
Paul Bakker48916f92012-09-16 19:57:18 +0000884 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
885 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000886
Paul Bakker48916f92012-09-16 19:57:18 +0000887 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000888 sha1_finish( &sha1, hash + 16 );
889
890 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
891 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
892
Paul Bakker5b4af392014-06-26 12:09:34 +0200893 md5_free( &md5 );
894 sha1_free( &sha1 );
895
Paul Bakker380da532012-04-18 16:10:25 +0000896 return;
897}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200898#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000899
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200900#if defined(POLARSSL_SSL_PROTO_TLS1_2)
901#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000902void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
903{
Paul Bakker9e36f042013-06-30 14:34:05 +0200904 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000905
906 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
907
Paul Bakker9e36f042013-06-30 14:34:05 +0200908 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
909 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000910
911 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
912 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
913
Paul Bakker5b4af392014-06-26 12:09:34 +0200914 sha256_free( &sha256 );
915
Paul Bakker380da532012-04-18 16:10:25 +0000916 return;
917}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200918#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000919
Paul Bakker9e36f042013-06-30 14:34:05 +0200920#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000921void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
922{
Paul Bakker9e36f042013-06-30 14:34:05 +0200923 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000924
925 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
926
Paul Bakker9e36f042013-06-30 14:34:05 +0200927 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
928 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Paul Bakkerca4ab492012-04-18 14:23:57 +0000930 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
932
Paul Bakker5b4af392014-06-26 12:09:34 +0200933 sha512_free( &sha512 );
934
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 return;
936}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200937#endif /* POLARSSL_SHA512_C */
938#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000939
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200940#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200941int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
942{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200943 unsigned char *p = ssl->handshake->premaster;
944 unsigned char *end = p + sizeof( ssl->handshake->premaster );
945
946 /*
947 * PMS = struct {
948 * opaque other_secret<0..2^16-1>;
949 * opaque psk<0..2^16-1>;
950 * };
951 * with "other_secret" depending on the particular key exchange
952 */
953#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
954 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
955 {
956 if( end - p < 2 + (int) ssl->psk_len )
957 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
958
959 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
960 *(p++) = (unsigned char)( ssl->psk_len );
961 p += ssl->psk_len;
962 }
963 else
964#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200965#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
966 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
967 {
968 /*
969 * other_secret already set by the ClientKeyExchange message,
970 * and is 48 bytes long
971 */
972 *p++ = 0;
973 *p++ = 48;
974 p += 48;
975 }
976 else
977#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200978#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
979 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
980 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200981 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200982 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200984 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200985 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200986 p + 2, &len,
987 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200988 {
989 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
990 return( ret );
991 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200992 *(p++) = (unsigned char)( len >> 8 );
993 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200994 p += len;
995
996 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
997 }
998 else
999#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1000#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1001 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1002 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001003 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 size_t zlen;
1005
1006 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001007 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001008 ssl->f_rng, ssl->p_rng ) ) != 0 )
1009 {
1010 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1011 return( ret );
1012 }
1013
1014 *(p++) = (unsigned char)( zlen >> 8 );
1015 *(p++) = (unsigned char)( zlen );
1016 p += zlen;
1017
1018 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1019 }
1020 else
1021#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1022 {
1023 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001024 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001025 }
1026
1027 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001028 if( end - p < 2 + (int) ssl->psk_len )
1029 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1030
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001031 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1032 *(p++) = (unsigned char)( ssl->psk_len );
1033 memcpy( p, ssl->psk, ssl->psk_len );
1034 p += ssl->psk_len;
1035
1036 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1037
1038 return( 0 );
1039}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001040#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001041
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001042#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001043/*
1044 * SSLv3.0 MAC functions
1045 */
Paul Bakker68884e32013-01-07 18:20:04 +01001046static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1047 unsigned char *buf, size_t len,
1048 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001049{
1050 unsigned char header[11];
1051 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001052 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001053 int md_size = md_get_size( md_ctx->md_info );
1054 int md_type = md_get_type( md_ctx->md_info );
1055
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001056 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001057 if( md_type == POLARSSL_MD_MD5 )
1058 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001059 else
Paul Bakker68884e32013-01-07 18:20:04 +01001060 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001061
1062 memcpy( header, ctr, 8 );
1063 header[ 8] = (unsigned char) type;
1064 header[ 9] = (unsigned char)( len >> 8 );
1065 header[10] = (unsigned char)( len );
1066
Paul Bakker68884e32013-01-07 18:20:04 +01001067 memset( padding, 0x36, padlen );
1068 md_starts( md_ctx );
1069 md_update( md_ctx, secret, md_size );
1070 md_update( md_ctx, padding, padlen );
1071 md_update( md_ctx, header, 11 );
1072 md_update( md_ctx, buf, len );
1073 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Paul Bakker68884e32013-01-07 18:20:04 +01001075 memset( padding, 0x5C, padlen );
1076 md_starts( md_ctx );
1077 md_update( md_ctx, secret, md_size );
1078 md_update( md_ctx, padding, padlen );
1079 md_update( md_ctx, buf + len, md_size );
1080 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001081}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001082#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001083
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001084#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1085 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1086 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1087#define POLARSSL_SOME_MODES_USE_MAC
1088#endif
1089
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001090/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001092 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001093static int ssl_encrypt_buf( ssl_context *ssl )
1094{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001095 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001096 cipher_mode_t mode;
1097 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
1099 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1100
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001101 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1102 {
1103 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1104 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1105 }
1106
1107 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1108
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001109 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1110 ssl->out_msg, ssl->out_msglen );
1111
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001113 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001115#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001116 if( mode == POLARSSL_MODE_STREAM ||
1117 ( mode == POLARSSL_MODE_CBC
1118#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1119 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1120#endif
1121 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001123#if defined(POLARSSL_SSL_PROTO_SSL3)
1124 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1125 {
1126 ssl_mac( &ssl->transform_out->md_ctx_enc,
1127 ssl->transform_out->mac_enc,
1128 ssl->out_msg, ssl->out_msglen,
1129 ssl->out_ctr, ssl->out_msgtype );
1130 }
1131 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001132#endif
1133#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001134 defined(POLARSSL_SSL_PROTO_TLS1_2)
1135 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1136 {
1137 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1138 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1139 ssl->out_msg, ssl->out_msglen );
1140 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1141 ssl->out_msg + ssl->out_msglen );
1142 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1143 }
1144 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001145#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001146 {
1147 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001148 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001149 }
1150
1151 SSL_DEBUG_BUF( 4, "computed mac",
1152 ssl->out_msg + ssl->out_msglen,
1153 ssl->transform_out->maclen );
1154
1155 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001156 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001157 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001158#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001160 /*
1161 * Encrypt
1162 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001163#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001164 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001166 int ret;
1167 size_t olen = 0;
1168
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1170 "including %d bytes of padding",
1171 ssl->out_msglen, 0 ) );
1172
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001173 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001174 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001175 ssl->transform_out->ivlen,
1176 ssl->out_msg, ssl->out_msglen,
1177 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001178 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001179 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001180 return( ret );
1181 }
1182
1183 if( ssl->out_msglen != olen )
1184 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001185 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001186 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001190#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001191#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1192 if( mode == POLARSSL_MODE_GCM ||
1193 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001194 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001195 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001196 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197 unsigned char *enc_msg;
1198 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001199 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1200 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 memcpy( add_data, ssl->out_ctr, 8 );
1203 add_data[8] = ssl->out_msgtype;
1204 add_data[9] = ssl->major_ver;
1205 add_data[10] = ssl->minor_ver;
1206 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1207 add_data[12] = ssl->out_msglen & 0xFF;
1208
1209 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1210 add_data, 13 );
1211
Paul Bakker68884e32013-01-07 18:20:04 +01001212 /*
1213 * Generate IV
1214 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001215#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001216 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001217 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1218 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001219 if( ret != 0 )
1220 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001221
Paul Bakker68884e32013-01-07 18:20:04 +01001222 memcpy( ssl->out_iv,
1223 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1224 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001225#else
1226 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1227 {
1228 /* Reminder if we ever add an AEAD mode with a different size */
1229 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1230 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1231 }
1232
1233 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1234 ssl->out_ctr, 8 );
1235 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1236#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001237
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001238 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001239 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001240
Paul Bakker68884e32013-01-07 18:20:04 +01001241 /*
1242 * Fix pointer positions and message length with added IV
1243 */
1244 enc_msg = ssl->out_msg;
1245 enc_msglen = ssl->out_msglen;
1246 ssl->out_msglen += ssl->transform_out->ivlen -
1247 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001248
Paul Bakker68884e32013-01-07 18:20:04 +01001249 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1250 "including %d bytes of padding",
1251 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001252
Paul Bakker68884e32013-01-07 18:20:04 +01001253 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001254 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001255 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001256 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1257 ssl->transform_out->iv_enc,
1258 ssl->transform_out->ivlen,
1259 add_data, 13,
1260 enc_msg, enc_msglen,
1261 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001262 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001263 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001264 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001265 return( ret );
1266 }
1267
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001268 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001269 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001270 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001271 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001272 }
1273
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001274 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001275 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001276
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001277 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001278 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001280#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001281#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1282 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001283 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001285 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001286 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001287 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001288
Paul Bakker48916f92012-09-16 19:57:18 +00001289 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1290 ssl->transform_out->ivlen;
1291 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 padlen = 0;
1293
1294 for( i = 0; i <= padlen; i++ )
1295 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1296
1297 ssl->out_msglen += padlen + 1;
1298
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001299 enc_msglen = ssl->out_msglen;
1300 enc_msg = ssl->out_msg;
1301
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001302#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001303 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001304 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1305 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001306 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001307 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001308 {
1309 /*
1310 * Generate IV
1311 */
Paul Bakker48916f92012-09-16 19:57:18 +00001312 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1313 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001314 if( ret != 0 )
1315 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001316
Paul Bakker92be97b2013-01-02 17:30:03 +01001317 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001318 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001319
1320 /*
1321 * Fix pointer positions and message length with added IV
1322 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001323 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001324 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001325 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001326 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001327#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001328
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001330 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001331 ssl->out_msglen, ssl->transform_out->ivlen,
1332 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001334 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001335 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001336 ssl->transform_out->ivlen,
1337 enc_msg, enc_msglen,
1338 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001339 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001340 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001341 return( ret );
1342 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001343
Paul Bakkercca5b812013-08-31 17:40:26 +02001344 if( enc_msglen != olen )
1345 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001346 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001347 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001348 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001349
1350#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001351 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1352 {
1353 /*
1354 * Save IV in SSL3 and TLS1
1355 */
1356 memcpy( ssl->transform_out->iv_enc,
1357 ssl->transform_out->cipher_ctx_enc.iv,
1358 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001360#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001361
1362#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001363 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001364 {
1365 /*
1366 * MAC(MAC_write_key, seq_num +
1367 * TLSCipherText.type +
1368 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001369 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001370 * IV + // except for TLS 1.0
1371 * ENC(content + padding + padding_length));
1372 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001373 unsigned char pseudo_hdr[13];
1374
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001375 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1376
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001377 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1378 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001379 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1380 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1381
1382 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001383
1384 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1385 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1386 ssl->out_iv, ssl->out_msglen );
1387 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1388 ssl->out_iv + ssl->out_msglen );
1389 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1390
1391 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001392 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001393 }
1394#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001396 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001397#endif /* POLARSSL_CIPHER_MODE_CBC &&
1398 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001399 {
1400 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001401 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001402 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001404 /* Make extra sure authentication was performed, exactly once */
1405 if( auth_done != 1 )
1406 {
1407 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1408 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1409 }
1410
Paul Bakkerca4ab492012-04-18 14:23:57 +00001411 for( i = 8; i > 0; i-- )
1412 if( ++ssl->out_ctr[i - 1] != 0 )
1413 break;
1414
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001415 /* The loops goes to its end iff the counter is wrapping */
1416 if( i == 0 )
1417 {
1418 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1419 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1420 }
1421
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1423
1424 return( 0 );
1425}
1426
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001427#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001428
Paul Bakker5121ce52009-01-03 21:22:43 +00001429static int ssl_decrypt_buf( ssl_context *ssl )
1430{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001431 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001432 cipher_mode_t mode;
1433 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001434#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001435 size_t padlen = 0, correct = 1;
1436#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
1438 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1439
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001440 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1441 {
1442 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1443 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1444 }
1445
1446 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1447
Paul Bakker48916f92012-09-16 19:57:18 +00001448 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 {
1450 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001451 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001452 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 }
1454
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001455#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001456 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001457 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001458 int ret;
1459 size_t olen = 0;
1460
Paul Bakker68884e32013-01-07 18:20:04 +01001461 padlen = 0;
1462
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001463 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001464 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001465 ssl->transform_in->ivlen,
1466 ssl->in_msg, ssl->in_msglen,
1467 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001468 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001469 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001470 return( ret );
1471 }
1472
1473 if( ssl->in_msglen != olen )
1474 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001475 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001476 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 }
Paul Bakker68884e32013-01-07 18:20:04 +01001479 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001480#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001481#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1482 if( mode == POLARSSL_MODE_GCM ||
1483 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001484 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001485 int ret;
1486 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001487 unsigned char *dec_msg;
1488 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001489 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001490 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1491 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001492 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1493 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001494
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001495 if( ssl->in_msglen < explicit_iv_len + taglen )
1496 {
1497 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1498 "+ taglen (%d)", ssl->in_msglen,
1499 explicit_iv_len, taglen ) );
1500 return( POLARSSL_ERR_SSL_INVALID_MAC );
1501 }
1502 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1503
Paul Bakker68884e32013-01-07 18:20:04 +01001504 dec_msg = ssl->in_msg;
1505 dec_msg_result = ssl->in_msg;
1506 ssl->in_msglen = dec_msglen;
1507
1508 memcpy( add_data, ssl->in_ctr, 8 );
1509 add_data[8] = ssl->in_msgtype;
1510 add_data[9] = ssl->major_ver;
1511 add_data[10] = ssl->minor_ver;
1512 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1513 add_data[12] = ssl->in_msglen & 0xFF;
1514
1515 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1516 add_data, 13 );
1517
1518 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1519 ssl->in_iv,
1520 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1521
1522 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1523 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001524 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001525
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001526 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001527 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001528 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001529 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1530 ssl->transform_in->iv_dec,
1531 ssl->transform_in->ivlen,
1532 add_data, 13,
1533 dec_msg, dec_msglen,
1534 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001535 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001536 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001537 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1538
1539 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1540 return( POLARSSL_ERR_SSL_INVALID_MAC );
1541
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001542 return( ret );
1543 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001544 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001545
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001546 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001547 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001548 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001549 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001550 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001553#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001554#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1555 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001556 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 {
Paul Bakker45829992013-01-03 14:52:21 +01001558 /*
1559 * Decrypt and check the padding
1560 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001561 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001562 unsigned char *dec_msg;
1563 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001564 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001565 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001567
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 /*
Paul Bakker45829992013-01-03 14:52:21 +01001569 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001570 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001571#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001572 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1573 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001574#endif
Paul Bakker45829992013-01-03 14:52:21 +01001575
1576 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1577 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1578 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001579 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1580 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1581 ssl->transform_in->ivlen,
1582 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001583 return( POLARSSL_ERR_SSL_INVALID_MAC );
1584 }
1585
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001586 dec_msglen = ssl->in_msglen;
1587 dec_msg = ssl->in_msg;
1588 dec_msg_result = ssl->in_msg;
1589
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001590 /*
1591 * Authenticate before decrypt if enabled
1592 */
1593#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001594 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001595 {
1596 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001597 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001598
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001599 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1600
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001601 dec_msglen -= ssl->transform_in->maclen;
1602 ssl->in_msglen -= ssl->transform_in->maclen;
1603
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001604 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1605 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1606 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1607 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1608
1609 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1610
1611 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001612 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1613 ssl->in_iv, ssl->in_msglen );
1614 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1615 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1616
1617 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1618 ssl->transform_in->maclen );
1619 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1620 ssl->transform_in->maclen );
1621
1622 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1623 ssl->transform_in->maclen ) != 0 )
1624 {
1625 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1626
1627 return( POLARSSL_ERR_SSL_INVALID_MAC );
1628 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001629 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001630 }
1631#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1632
1633 /*
1634 * Check length sanity
1635 */
1636 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1637 {
1638 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1639 ssl->in_msglen, ssl->transform_in->ivlen ) );
1640 return( POLARSSL_ERR_SSL_INVALID_MAC );
1641 }
1642
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001643#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001645 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001646 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001647 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648 {
Paul Bakker48916f92012-09-16 19:57:18 +00001649 dec_msglen -= ssl->transform_in->ivlen;
1650 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651
Paul Bakker48916f92012-09-16 19:57:18 +00001652 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001653 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001654 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001655#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001656
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001657 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001658 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001659 ssl->transform_in->ivlen,
1660 dec_msg, dec_msglen,
1661 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001662 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001663 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001664 return( ret );
1665 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001666
Paul Bakkercca5b812013-08-31 17:40:26 +02001667 if( dec_msglen != olen )
1668 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001669 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001670 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001671 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001672
1673#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001674 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1675 {
1676 /*
1677 * Save IV in SSL3 and TLS1
1678 */
1679 memcpy( ssl->transform_in->iv_dec,
1680 ssl->transform_in->cipher_ctx_dec.iv,
1681 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001682 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001683#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001686
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001687 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001688 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001689 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001690#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001691 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1692 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001693#endif
Paul Bakker45829992013-01-03 14:52:21 +01001694 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001695 correct = 0;
1696 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001698#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1700 {
Paul Bakker48916f92012-09-16 19:57:18 +00001701 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001703#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1705 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001706 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001707#endif
Paul Bakker45829992013-01-03 14:52:21 +01001708 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 }
1710 }
1711 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001712#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001713#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1714 defined(POLARSSL_SSL_PROTO_TLS1_2)
1715 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 {
1717 /*
Paul Bakker45829992013-01-03 14:52:21 +01001718 * TLSv1+: always check the padding up to the first failure
1719 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001720 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001721 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001722 size_t padding_idx = ssl->in_msglen - padlen - 1;
1723
Paul Bakker956c9e02013-12-19 14:42:28 +01001724 /*
1725 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001726 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001727 *
Paul Bakker61885c72014-04-25 12:59:03 +02001728 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1729 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001730 *
1731 * In both cases we reset padding_idx to a safe value (0) to
1732 * prevent out-of-buffer reads.
1733 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001734 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001735 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1736 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001737
1738 padding_idx *= correct;
1739
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001740 for( i = 1; i <= 256; i++ )
1741 {
1742 real_count &= ( i <= padlen );
1743 pad_count += real_count *
1744 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1745 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001746
1747 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001748
Paul Bakkerd66f0702013-01-31 16:57:45 +01001749#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001750 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001751 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001752#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001753 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001755 else
1756#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1757 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001758 {
1759 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001760 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001761 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001762
1763 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001765 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001766#endif /* POLARSSL_CIPHER_MODE_CBC &&
1767 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001768 {
1769 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001770 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001771 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
1773 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1774 ssl->in_msg, ssl->in_msglen );
1775
1776 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001777 * Authenticate if not done yet.
1778 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001780#if defined(POLARSSL_SOME_MODES_USE_MAC)
1781 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001782 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001783 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1784
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001785 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001787 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1788 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001790 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001792#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001793 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1794 {
1795 ssl_mac( &ssl->transform_in->md_ctx_dec,
1796 ssl->transform_in->mac_dec,
1797 ssl->in_msg, ssl->in_msglen,
1798 ssl->in_ctr, ssl->in_msgtype );
1799 }
1800 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001801#endif /* POLARSSL_SSL_PROTO_SSL3 */
1802#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001803 defined(POLARSSL_SSL_PROTO_TLS1_2)
1804 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1805 {
1806 /*
1807 * Process MAC and always update for padlen afterwards to make
1808 * total time independent of padlen
1809 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001810 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001811 *
1812 * Known timing attacks:
1813 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1814 *
1815 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1816 * correctly. (We round down instead of up, so -56 is the correct
1817 * value for our calculations instead of -55)
1818 */
1819 size_t j, extra_run = 0;
1820 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1821 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001822
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001823 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001824
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001825 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1826 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1827 ssl->in_msglen );
1828 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1829 ssl->in_msg + ssl->in_msglen );
1830 for( j = 0; j < extra_run; j++ )
1831 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001832
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001833 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1834 }
1835 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001836#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001837 POLARSSL_SSL_PROTO_TLS1_2 */
1838 {
1839 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001840 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001841 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001843 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1844 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1845 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001847 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001848 ssl->transform_in->maclen ) != 0 )
1849 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001850#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001851 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001852#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001853 correct = 0;
1854 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001855 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001856
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001857 /*
1858 * Finally check the correct flag
1859 */
1860 if( correct == 0 )
1861 return( POLARSSL_ERR_SSL_INVALID_MAC );
1862 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001863#endif /* POLARSSL_SOME_MODES_USE_MAC */
1864
1865 /* Make extra sure authentication was performed, exactly once */
1866 if( auth_done != 1 )
1867 {
1868 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1869 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001871
1872 if( ssl->in_msglen == 0 )
1873 {
1874 ssl->nb_zero++;
1875
1876 /*
1877 * Three or more empty messages may be a DoS attack
1878 * (excessive CPU consumption).
1879 */
1880 if( ssl->nb_zero > 3 )
1881 {
1882 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1883 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001884 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001885 }
1886 }
1887 else
1888 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001889
Paul Bakker23986e52011-04-24 08:57:21 +00001890 for( i = 8; i > 0; i-- )
1891 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 break;
1893
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001894 /* The loops goes to its end iff the counter is wrapping */
1895 if( i == 0 )
1896 {
1897 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1898 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1899 }
1900
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1902
1903 return( 0 );
1904}
1905
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001906#undef MAC_NONE
1907#undef MAC_PLAINTEXT
1908#undef MAC_CIPHERTEXT
1909
Paul Bakker2770fbd2012-07-03 13:30:23 +00001910#if defined(POLARSSL_ZLIB_SUPPORT)
1911/*
1912 * Compression/decompression functions
1913 */
1914static int ssl_compress_buf( ssl_context *ssl )
1915{
1916 int ret;
1917 unsigned char *msg_post = ssl->out_msg;
1918 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001919 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001920
1921 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1922
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001923 if( len_pre == 0 )
1924 return( 0 );
1925
Paul Bakker2770fbd2012-07-03 13:30:23 +00001926 memcpy( msg_pre, ssl->out_msg, len_pre );
1927
1928 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1929 ssl->out_msglen ) );
1930
1931 SSL_DEBUG_BUF( 4, "before compression: output payload",
1932 ssl->out_msg, ssl->out_msglen );
1933
Paul Bakker48916f92012-09-16 19:57:18 +00001934 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1935 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1936 ssl->transform_out->ctx_deflate.next_out = msg_post;
1937 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001938
Paul Bakker48916f92012-09-16 19:57:18 +00001939 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001940 if( ret != Z_OK )
1941 {
1942 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1943 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1944 }
1945
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001946 ssl->out_msglen = SSL_BUFFER_LEN -
1947 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001948
Paul Bakker2770fbd2012-07-03 13:30:23 +00001949 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1950 ssl->out_msglen ) );
1951
1952 SSL_DEBUG_BUF( 4, "after compression: output payload",
1953 ssl->out_msg, ssl->out_msglen );
1954
1955 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1956
1957 return( 0 );
1958}
1959
1960static int ssl_decompress_buf( ssl_context *ssl )
1961{
1962 int ret;
1963 unsigned char *msg_post = ssl->in_msg;
1964 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001965 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001966
1967 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1968
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001969 if( len_pre == 0 )
1970 return( 0 );
1971
Paul Bakker2770fbd2012-07-03 13:30:23 +00001972 memcpy( msg_pre, ssl->in_msg, len_pre );
1973
1974 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1975 ssl->in_msglen ) );
1976
1977 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1978 ssl->in_msg, ssl->in_msglen );
1979
Paul Bakker48916f92012-09-16 19:57:18 +00001980 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1981 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1982 ssl->transform_in->ctx_inflate.next_out = msg_post;
1983 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001984
Paul Bakker48916f92012-09-16 19:57:18 +00001985 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001986 if( ret != Z_OK )
1987 {
1988 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1989 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1990 }
1991
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001992 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1993 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001994
Paul Bakker2770fbd2012-07-03 13:30:23 +00001995 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1996 ssl->in_msglen ) );
1997
1998 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1999 ssl->in_msg, ssl->in_msglen );
2000
2001 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2002
2003 return( 0 );
2004}
2005#endif /* POLARSSL_ZLIB_SUPPORT */
2006
Paul Bakker5121ce52009-01-03 21:22:43 +00002007/*
2008 * Fill the input message buffer
2009 */
Paul Bakker23986e52011-04-24 08:57:21 +00002010int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002011{
Paul Bakker23986e52011-04-24 08:57:21 +00002012 int ret;
2013 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
2015 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2016
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002017 if( nb_want > SSL_BUFFER_LEN - 8 )
2018 {
2019 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2020 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2021 }
2022
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 while( ssl->in_left < nb_want )
2024 {
2025 len = nb_want - ssl->in_left;
2026 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2027
2028 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2029 ssl->in_left, nb_want ) );
2030 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2031
Paul Bakker831a7552011-05-18 13:32:51 +00002032 if( ret == 0 )
2033 return( POLARSSL_ERR_SSL_CONN_EOF );
2034
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 if( ret < 0 )
2036 return( ret );
2037
2038 ssl->in_left += ret;
2039 }
2040
2041 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2042
2043 return( 0 );
2044}
2045
2046/*
2047 * Flush any data not yet written
2048 */
2049int ssl_flush_output( ssl_context *ssl )
2050{
2051 int ret;
2052 unsigned char *buf;
2053
2054 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2055
2056 while( ssl->out_left > 0 )
2057 {
2058 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2059 5 + ssl->out_msglen, ssl->out_left ) );
2060
Paul Bakker5bd42292012-12-19 14:40:42 +01002061 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002063
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2065
2066 if( ret <= 0 )
2067 return( ret );
2068
2069 ssl->out_left -= ret;
2070 }
2071
2072 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2073
2074 return( 0 );
2075}
2076
2077/*
2078 * Record layer functions
2079 */
2080int ssl_write_record( ssl_context *ssl )
2081{
Paul Bakker05ef8352012-05-08 09:17:57 +00002082 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002083 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
2085 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2086
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2088 {
2089 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2090 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2091 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2092
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002093 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2094 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 }
2096
Paul Bakker2770fbd2012-07-03 13:30:23 +00002097#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002098 if( ssl->transform_out != NULL &&
2099 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002100 {
2101 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2102 {
2103 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2104 return( ret );
2105 }
2106
2107 len = ssl->out_msglen;
2108 }
2109#endif /*POLARSSL_ZLIB_SUPPORT */
2110
Paul Bakker05ef8352012-05-08 09:17:57 +00002111#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002112 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002114 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
Paul Bakker05ef8352012-05-08 09:17:57 +00002116 ret = ssl_hw_record_write( ssl );
2117 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2118 {
2119 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002120 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002121 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002122
2123 if( ret == 0 )
2124 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002125 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002126#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002127 if( !done )
2128 {
2129 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2130 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2131 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2133 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002134
Paul Bakker48916f92012-09-16 19:57:18 +00002135 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002136 {
2137 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2138 {
2139 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2140 return( ret );
2141 }
2142
2143 len = ssl->out_msglen;
2144 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2145 ssl->out_hdr[4] = (unsigned char)( len );
2146 }
2147
2148 ssl->out_left = 5 + ssl->out_msglen;
2149
2150 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2151 "version = [%d:%d], msglen = %d",
2152 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2153 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2154
2155 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002156 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 }
2158
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2160 {
2161 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2162 return( ret );
2163 }
2164
2165 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2166
2167 return( 0 );
2168}
2169
2170int ssl_read_record( ssl_context *ssl )
2171{
Paul Bakker05ef8352012-05-08 09:17:57 +00002172 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002173
2174 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2175
2176 if( ssl->in_hslen != 0 &&
2177 ssl->in_hslen < ssl->in_msglen )
2178 {
2179 /*
2180 * Get next Handshake message in the current record
2181 */
2182 ssl->in_msglen -= ssl->in_hslen;
2183
Paul Bakker8934a982011-08-05 11:11:53 +00002184 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002185 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
2187 ssl->in_hslen = 4;
2188 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2189
2190 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2191 " %d, type = %d, hslen = %d",
2192 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2193
2194 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2195 {
2196 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002197 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
2199
2200 if( ssl->in_msglen < ssl->in_hslen )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002203 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 }
2205
Paul Bakker4224bc02014-04-08 14:36:50 +02002206 if( ssl->state != SSL_HANDSHAKE_OVER )
2207 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002208
2209 return( 0 );
2210 }
2211
2212 ssl->in_hslen = 0;
2213
2214 /*
2215 * Read the record header and validate it
2216 */
2217 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2218 {
2219 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2220 return( ret );
2221 }
2222
2223 ssl->in_msgtype = ssl->in_hdr[0];
2224 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2225
2226 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2227 "version = [%d:%d], msglen = %d",
2228 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2229 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2230
2231 if( ssl->in_hdr[1] != ssl->major_ver )
2232 {
2233 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002234 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
2236
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002237 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 {
2239 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002240 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 }
2242
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002243 /* Sanity check (outer boundaries) */
2244 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2245 {
2246 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2247 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2248 }
2249
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002251 * Make sure the message length is acceptable for the current transform
2252 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 */
Paul Bakker48916f92012-09-16 19:57:18 +00002254 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002256 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 {
2258 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002259 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 }
2261 }
2262 else
2263 {
Paul Bakker48916f92012-09-16 19:57:18 +00002264 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 {
2266 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002267 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 }
2269
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002270#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002272 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 {
2274 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002275 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002279#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2280 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 /*
2282 * TLS encrypted messages can have up to 256 bytes of padding
2283 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002284 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002285 ssl->in_msglen > ssl->transform_in->minlen +
2286 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 {
2288 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002289 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002291#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 }
2293
2294 /*
2295 * Read and optionally decrypt the message contents
2296 */
2297 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2298 {
2299 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2300 return( ret );
2301 }
2302
2303 SSL_DEBUG_BUF( 4, "input record from network",
2304 ssl->in_hdr, 5 + ssl->in_msglen );
2305
Paul Bakker05ef8352012-05-08 09:17:57 +00002306#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002307 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002308 {
2309 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2310
2311 ret = ssl_hw_record_read( ssl );
2312 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2313 {
2314 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002315 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002316 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002317
2318 if( ret == 0 )
2319 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002320 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002321#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002322 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002323 {
2324 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2325 {
Paul Bakker40865c82013-01-31 17:13:13 +01002326#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2327 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2328 {
2329 ssl_send_alert_message( ssl,
2330 SSL_ALERT_LEVEL_FATAL,
2331 SSL_ALERT_MSG_BAD_RECORD_MAC );
2332 }
2333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2335 return( ret );
2336 }
2337
2338 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2339 ssl->in_msg, ssl->in_msglen );
2340
2341 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2342 {
2343 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002344 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 }
2346 }
2347
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002349 if( ssl->transform_in != NULL &&
2350 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002351 {
2352 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2353 {
2354 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2355 return( ret );
2356 }
2357
2358 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2359 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2360 }
2361#endif /* POLARSSL_ZLIB_SUPPORT */
2362
Paul Bakker0a925182012-04-16 06:46:41 +00002363 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2364 ssl->in_msgtype != SSL_MSG_ALERT &&
2365 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2366 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2367 {
2368 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2369
Paul Bakker48916f92012-09-16 19:57:18 +00002370 if( ( ret = ssl_send_alert_message( ssl,
2371 SSL_ALERT_LEVEL_FATAL,
2372 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002373 {
2374 return( ret );
2375 }
2376
2377 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2378 }
2379
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2381 {
2382 ssl->in_hslen = 4;
2383 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2384
2385 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2386 " %d, type = %d, hslen = %d",
2387 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2388
2389 /*
2390 * Additional checks to validate the handshake header
2391 */
2392 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2393 {
2394 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002395 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 }
2397
2398 if( ssl->in_msglen < ssl->in_hslen )
2399 {
2400 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002401 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 }
2403
Paul Bakker48916f92012-09-16 19:57:18 +00002404 if( ssl->state != SSL_HANDSHAKE_OVER )
2405 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 }
2407
2408 if( ssl->in_msgtype == SSL_MSG_ALERT )
2409 {
2410 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2411 ssl->in_msg[0], ssl->in_msg[1] ) );
2412
2413 /*
2414 * Ignore non-fatal alerts, except close_notify
2415 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002416 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002418 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2419 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002420 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 }
2422
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002423 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2424 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 {
2426 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002427 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 }
2429 }
2430
2431 ssl->in_left = 0;
2432
2433 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2434
2435 return( 0 );
2436}
2437
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002438int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2439{
2440 int ret;
2441
2442 if( ( ret = ssl_send_alert_message( ssl,
2443 SSL_ALERT_LEVEL_FATAL,
2444 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2445 {
2446 return( ret );
2447 }
2448
2449 return( 0 );
2450}
2451
Paul Bakker0a925182012-04-16 06:46:41 +00002452int ssl_send_alert_message( ssl_context *ssl,
2453 unsigned char level,
2454 unsigned char message )
2455{
2456 int ret;
2457
2458 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2459
2460 ssl->out_msgtype = SSL_MSG_ALERT;
2461 ssl->out_msglen = 2;
2462 ssl->out_msg[0] = level;
2463 ssl->out_msg[1] = message;
2464
2465 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2466 {
2467 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2468 return( ret );
2469 }
2470
2471 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2472
2473 return( 0 );
2474}
2475
Paul Bakker5121ce52009-01-03 21:22:43 +00002476/*
2477 * Handshake functions
2478 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002479#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2480 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2481 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2482 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2483 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2484 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2485 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002486int ssl_write_certificate( ssl_context *ssl )
2487{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002488 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
2490 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2491
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002492 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002493 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2494 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002495 {
2496 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2497 ssl->state++;
2498 return( 0 );
2499 }
2500
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002501 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2502 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002503}
2504
2505int ssl_parse_certificate( ssl_context *ssl )
2506{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002507 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2508
2509 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2510
2511 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002512 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2513 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002514 {
2515 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2516 ssl->state++;
2517 return( 0 );
2518 }
2519
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002520 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2521 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002522}
2523#else
2524int ssl_write_certificate( ssl_context *ssl )
2525{
2526 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2527 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002528 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002529 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2530
2531 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2532
2533 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002534 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2535 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002536 {
2537 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2538 ssl->state++;
2539 return( 0 );
2540 }
2541
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002542#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 if( ssl->endpoint == SSL_IS_CLIENT )
2544 {
2545 if( ssl->client_auth == 0 )
2546 {
2547 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2548 ssl->state++;
2549 return( 0 );
2550 }
2551
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002552#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 /*
2554 * If using SSLv3 and got no cert, send an Alert message
2555 * (otherwise an empty Certificate message will be sent).
2556 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002557 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2559 {
2560 ssl->out_msglen = 2;
2561 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002562 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2563 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
2565 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2566 goto write_msg;
2567 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002568#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002570#endif /* POLARSSL_SSL_CLI_C */
2571#if defined(POLARSSL_SSL_SRV_C)
2572 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002574 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 {
2576 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002577 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 }
2579 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002580#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002582 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
2584 /*
2585 * 0 . 0 handshake type
2586 * 1 . 3 handshake length
2587 * 4 . 6 length of all certs
2588 * 7 . 9 length of cert. 1
2589 * 10 . n-1 peer certificate
2590 * n . n+2 length of cert. 2
2591 * n+3 . ... upper level cert, etc.
2592 */
2593 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002594 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Paul Bakker29087132010-03-21 21:03:34 +00002596 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
2598 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002599 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 {
2601 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2602 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002603 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605
2606 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2607 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2608 ssl->out_msg[i + 2] = (unsigned char)( n );
2609
2610 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2611 i += n; crt = crt->next;
2612 }
2613
2614 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2615 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2616 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2617
2618 ssl->out_msglen = i;
2619 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2620 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2621
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002622#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002623write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002624#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
2626 ssl->state++;
2627
2628 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2629 {
2630 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2631 return( ret );
2632 }
2633
2634 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2635
Paul Bakkered27a042013-04-18 22:46:23 +02002636 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637}
2638
2639int ssl_parse_certificate( ssl_context *ssl )
2640{
Paul Bakkered27a042013-04-18 22:46:23 +02002641 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002642 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002643 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
2645 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2646
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002647 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002648 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2649 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002650 {
2651 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2652 ssl->state++;
2653 return( 0 );
2654 }
2655
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002656#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002658 ( ssl->authmode == SSL_VERIFY_NONE ||
2659 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002661 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2663 ssl->state++;
2664 return( 0 );
2665 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002666#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
2668 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2669 {
2670 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2671 return( ret );
2672 }
2673
2674 ssl->state++;
2675
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002676#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002677#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 /*
2679 * Check if the client sent an empty certificate
2680 */
2681 if( ssl->endpoint == SSL_IS_SERVER &&
2682 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2683 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002684 if( ssl->in_msglen == 2 &&
2685 ssl->in_msgtype == SSL_MSG_ALERT &&
2686 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2687 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 {
2689 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2690
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002691 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2693 return( 0 );
2694 else
Paul Bakker40e46942009-01-03 21:51:57 +00002695 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 }
2697 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002698#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002700#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2701 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 if( ssl->endpoint == SSL_IS_SERVER &&
2703 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2704 {
2705 if( ssl->in_hslen == 7 &&
2706 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2707 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2708 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2709 {
2710 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2711
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002712 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002714 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 else
2716 return( 0 );
2717 }
2718 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002719#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2720 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002721#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
2723 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2724 {
2725 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002726 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 }
2728
2729 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2730 {
2731 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002732 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
2734
2735 /*
2736 * Same message structure as in ssl_write_certificate()
2737 */
2738 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2739
2740 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2741 {
2742 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002743 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 }
2745
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002746 /* In case we tried to reuse a session but it failed */
2747 if( ssl->session_negotiate->peer_cert != NULL )
2748 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002749 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002750 polarssl_free( ssl->session_negotiate->peer_cert );
2751 }
2752
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002753 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2754 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 {
2756 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002757 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002758 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 }
2760
Paul Bakkerb6b09562013-09-18 14:17:41 +02002761 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
2763 i = 7;
2764
2765 while( i < ssl->in_hslen )
2766 {
2767 if( ssl->in_msg[i] != 0 )
2768 {
2769 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002770 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 }
2772
2773 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2774 | (unsigned int) ssl->in_msg[i + 2];
2775 i += 3;
2776
2777 if( n < 128 || i + n > ssl->in_hslen )
2778 {
2779 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002780 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 }
2782
Paul Bakkerddf26b42013-09-18 13:46:23 +02002783 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2784 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 if( ret != 0 )
2786 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002787 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 return( ret );
2789 }
2790
2791 i += n;
2792 }
2793
Paul Bakker48916f92012-09-16 19:57:18 +00002794 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002796 /*
2797 * On client, make sure the server cert doesn't change during renego to
2798 * avoid "triple handshake" attack: https://secure-resumption.com/
2799 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002800#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002801 if( ssl->endpoint == SSL_IS_CLIENT &&
2802 ssl->renegotiation == SSL_RENEGOTIATION )
2803 {
2804 if( ssl->session->peer_cert == NULL )
2805 {
2806 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2807 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2808 }
2809
2810 if( ssl->session->peer_cert->raw.len !=
2811 ssl->session_negotiate->peer_cert->raw.len ||
2812 memcmp( ssl->session->peer_cert->raw.p,
2813 ssl->session_negotiate->peer_cert->raw.p,
2814 ssl->session->peer_cert->raw.len ) != 0 )
2815 {
2816 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2817 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2818 }
2819 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002820#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002821
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 if( ssl->authmode != SSL_VERIFY_NONE )
2823 {
2824 if( ssl->ca_chain == NULL )
2825 {
2826 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002827 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 }
2829
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002830 /*
2831 * Main check: verify certificate
2832 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002833 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2834 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2835 &ssl->session_negotiate->verify_result,
2836 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
2838 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002839 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002841 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002842
2843 /*
2844 * Secondary checks: always done, but change 'ret' only if it was 0
2845 */
2846
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002847#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002848 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002849 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002850
2851 /* If certificate uses an EC key, make sure the curve is OK */
2852 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2853 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2854 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002855 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002856 if( ret == 0 )
2857 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002858 }
2859 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002860#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002862 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2863 ciphersuite_info,
2864 ! ssl->endpoint ) != 0 )
2865 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002866 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002867 if( ret == 0 )
2868 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2869 }
2870
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2872 ret = 0;
2873 }
2874
2875 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2876
2877 return( ret );
2878}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002879#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2880 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2881 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2882 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2883 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2884 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2885 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
2887int ssl_write_change_cipher_spec( ssl_context *ssl )
2888{
2889 int ret;
2890
2891 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2892
2893 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2894 ssl->out_msglen = 1;
2895 ssl->out_msg[0] = 1;
2896
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 ssl->state++;
2898
2899 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2900 {
2901 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2902 return( ret );
2903 }
2904
2905 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2906
2907 return( 0 );
2908}
2909
2910int ssl_parse_change_cipher_spec( ssl_context *ssl )
2911{
2912 int ret;
2913
2914 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2915
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2917 {
2918 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2919 return( ret );
2920 }
2921
2922 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2923 {
2924 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002925 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 }
2927
2928 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2929 {
2930 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002931 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002932 }
2933
2934 ssl->state++;
2935
2936 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2937
2938 return( 0 );
2939}
2940
Paul Bakker41c83d32013-03-20 14:39:14 +01002941void ssl_optimize_checksum( ssl_context *ssl,
2942 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002943{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002944 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002945
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2947 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002948 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002949 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002950 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002951#endif
2952#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2953#if defined(POLARSSL_SHA512_C)
2954 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2955 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2956 else
2957#endif
2958#if defined(POLARSSL_SHA256_C)
2959 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002960 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002961 else
2962#endif
2963#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002964 {
2965 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002966 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002967 }
Paul Bakker380da532012-04-18 16:10:25 +00002968}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002969
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002970static void ssl_update_checksum_start( ssl_context *ssl,
2971 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002972{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002973#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2974 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002975 md5_update( &ssl->handshake->fin_md5 , buf, len );
2976 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002977#endif
2978#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2979#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002980 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002981#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002982#if defined(POLARSSL_SHA512_C)
2983 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002984#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002985#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002986}
2987
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002988#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2989 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002990static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2991 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002992{
Paul Bakker48916f92012-09-16 19:57:18 +00002993 md5_update( &ssl->handshake->fin_md5 , buf, len );
2994 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002995}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002996#endif
Paul Bakker380da532012-04-18 16:10:25 +00002997
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002998#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2999#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003000static void ssl_update_checksum_sha256( ssl_context *ssl,
3001 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003002{
Paul Bakker9e36f042013-06-30 14:34:05 +02003003 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003004}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003005#endif
Paul Bakker380da532012-04-18 16:10:25 +00003006
Paul Bakker9e36f042013-06-30 14:34:05 +02003007#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003008static void ssl_update_checksum_sha384( ssl_context *ssl,
3009 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003010{
Paul Bakker9e36f042013-06-30 14:34:05 +02003011 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003012}
Paul Bakker769075d2012-11-24 11:26:46 +01003013#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003014#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003015
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003016#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017static void ssl_calc_finished_ssl(
3018 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003019{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003020 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021 md5_context md5;
3022 sha1_context sha1;
3023
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 unsigned char padbuf[48];
3025 unsigned char md5sum[16];
3026 unsigned char sha1sum[20];
3027
Paul Bakker48916f92012-09-16 19:57:18 +00003028 ssl_session *session = ssl->session_negotiate;
3029 if( !session )
3030 session = ssl->session;
3031
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3033
Paul Bakker48916f92012-09-16 19:57:18 +00003034 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3035 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
3037 /*
3038 * SSLv3:
3039 * hash =
3040 * MD5( master + pad2 +
3041 * MD5( handshake + sender + master + pad1 ) )
3042 * + SHA1( master + pad2 +
3043 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 */
3045
Paul Bakker90995b52013-06-24 19:20:35 +02003046#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Paul Bakker90995b52013-06-24 19:20:35 +02003051#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003053 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Paul Bakker3c2122f2013-06-24 19:03:14 +02003056 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3057 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker1ef83d62012-04-11 12:09:53 +00003059 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Paul Bakker3c2122f2013-06-24 19:03:14 +02003061 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003062 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063 md5_update( &md5, padbuf, 48 );
3064 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Paul Bakker3c2122f2013-06-24 19:03:14 +02003066 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003067 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068 sha1_update( &sha1, padbuf, 40 );
3069 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003070
Paul Bakker1ef83d62012-04-11 12:09:53 +00003071 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072
Paul Bakker1ef83d62012-04-11 12:09:53 +00003073 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003074 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 md5_update( &md5, padbuf, 48 );
3076 md5_update( &md5, md5sum, 16 );
3077 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
Paul Bakker1ef83d62012-04-11 12:09:53 +00003079 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003080 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081 sha1_update( &sha1, padbuf , 40 );
3082 sha1_update( &sha1, sha1sum, 20 );
3083 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Paul Bakker5b4af392014-06-26 12:09:34 +02003087 md5_free( &md5 );
3088 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Paul Bakker34617722014-06-13 17:20:13 +02003090 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3091 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3092 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003093
3094 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3095}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003096#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003097
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003098#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003099static void ssl_calc_finished_tls(
3100 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003101{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003103 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003104 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003106 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003107
Paul Bakker48916f92012-09-16 19:57:18 +00003108 ssl_session *session = ssl->session_negotiate;
3109 if( !session )
3110 session = ssl->session;
3111
Paul Bakker1ef83d62012-04-11 12:09:53 +00003112 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Paul Bakker48916f92012-09-16 19:57:18 +00003114 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3115 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003116
Paul Bakker1ef83d62012-04-11 12:09:53 +00003117 /*
3118 * TLSv1:
3119 * hash = PRF( master, finished_label,
3120 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3121 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003122
Paul Bakker90995b52013-06-24 19:20:35 +02003123#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003124 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3125 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003126#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127
Paul Bakker90995b52013-06-24 19:20:35 +02003128#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003129 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3130 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003131#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003132
3133 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003134 ? "client finished"
3135 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003136
3137 md5_finish( &md5, padbuf );
3138 sha1_finish( &sha1, padbuf + 16 );
3139
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003140 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003141 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003142
3143 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3144
Paul Bakker5b4af392014-06-26 12:09:34 +02003145 md5_free( &md5 );
3146 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003147
Paul Bakker34617722014-06-13 17:20:13 +02003148 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003149
3150 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3151}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003152#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003153
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003154#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3155#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003156static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003157 ssl_context *ssl, unsigned char *buf, int from )
3158{
3159 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003160 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003161 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003162 unsigned char padbuf[32];
3163
Paul Bakker48916f92012-09-16 19:57:18 +00003164 ssl_session *session = ssl->session_negotiate;
3165 if( !session )
3166 session = ssl->session;
3167
Paul Bakker380da532012-04-18 16:10:25 +00003168 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169
Paul Bakker9e36f042013-06-30 14:34:05 +02003170 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171
3172 /*
3173 * TLSv1.2:
3174 * hash = PRF( master, finished_label,
3175 * Hash( handshake ) )[0.11]
3176 */
3177
Paul Bakker9e36f042013-06-30 14:34:05 +02003178#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003179 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003180 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003181#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003182
3183 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003184 ? "client finished"
3185 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003186
Paul Bakker9e36f042013-06-30 14:34:05 +02003187 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003188
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003189 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003190 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003191
3192 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3193
Paul Bakker5b4af392014-06-26 12:09:34 +02003194 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003195
Paul Bakker34617722014-06-13 17:20:13 +02003196 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003197
3198 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3199}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003200#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003201
Paul Bakker9e36f042013-06-30 14:34:05 +02003202#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003203static void ssl_calc_finished_tls_sha384(
3204 ssl_context *ssl, unsigned char *buf, int from )
3205{
3206 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003207 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003208 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003209 unsigned char padbuf[48];
3210
Paul Bakker48916f92012-09-16 19:57:18 +00003211 ssl_session *session = ssl->session_negotiate;
3212 if( !session )
3213 session = ssl->session;
3214
Paul Bakker380da532012-04-18 16:10:25 +00003215 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003216
Paul Bakker9e36f042013-06-30 14:34:05 +02003217 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003218
3219 /*
3220 * TLSv1.2:
3221 * hash = PRF( master, finished_label,
3222 * Hash( handshake ) )[0.11]
3223 */
3224
Paul Bakker9e36f042013-06-30 14:34:05 +02003225#if !defined(POLARSSL_SHA512_ALT)
3226 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3227 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003228#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003229
3230 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003231 ? "client finished"
3232 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003233
Paul Bakker9e36f042013-06-30 14:34:05 +02003234 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003235
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003236 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003237 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003238
3239 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3240
Paul Bakker5b4af392014-06-26 12:09:34 +02003241 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003242
Paul Bakker34617722014-06-13 17:20:13 +02003243 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003244
3245 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3246}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003247#endif /* POLARSSL_SHA512_C */
3248#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003249
Paul Bakker48916f92012-09-16 19:57:18 +00003250void ssl_handshake_wrapup( ssl_context *ssl )
3251{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003252 int resume = ssl->handshake->resume;
3253
Paul Bakker48916f92012-09-16 19:57:18 +00003254 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3255
3256 /*
3257 * Free our handshake params
3258 */
3259 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003260 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003261 ssl->handshake = NULL;
3262
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003263 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003264 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003265 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003266 ssl->renego_records_seen = 0;
3267 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003268
Paul Bakker48916f92012-09-16 19:57:18 +00003269 /*
3270 * Switch in our now active transform context
3271 */
3272 if( ssl->transform )
3273 {
3274 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003275 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003276 }
3277 ssl->transform = ssl->transform_negotiate;
3278 ssl->transform_negotiate = NULL;
3279
Paul Bakker0a597072012-09-25 21:55:46 +00003280 if( ssl->session )
3281 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003282#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3283 /* RFC 7366 3.1: keep the EtM state */
3284 ssl->session_negotiate->encrypt_then_mac =
3285 ssl->session->encrypt_then_mac;
3286#endif
3287
Paul Bakker0a597072012-09-25 21:55:46 +00003288 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003289 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003290 }
3291 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003292 ssl->session_negotiate = NULL;
3293
Paul Bakker0a597072012-09-25 21:55:46 +00003294 /*
3295 * Add cache entry
3296 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003297 if( ssl->f_set_cache != NULL &&
3298 ssl->session->length != 0 &&
3299 resume == 0 )
3300 {
Paul Bakker0a597072012-09-25 21:55:46 +00003301 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3302 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003303 }
Paul Bakker0a597072012-09-25 21:55:46 +00003304
Paul Bakker48916f92012-09-16 19:57:18 +00003305 ssl->state++;
3306
3307 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3308}
3309
Paul Bakker1ef83d62012-04-11 12:09:53 +00003310int ssl_write_finished( ssl_context *ssl )
3311{
3312 int ret, hash_len;
3313
3314 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3315
Paul Bakker92be97b2013-01-02 17:30:03 +01003316 /*
3317 * Set the out_msg pointer to the correct location based on IV length
3318 */
3319 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3320 {
3321 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3322 ssl->transform_negotiate->fixed_ivlen;
3323 }
3324 else
3325 ssl->out_msg = ssl->out_iv;
3326
Paul Bakker48916f92012-09-16 19:57:18 +00003327 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003328
3329 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3331
Paul Bakker48916f92012-09-16 19:57:18 +00003332 ssl->verify_data_len = hash_len;
3333 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3334
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 ssl->out_msglen = 4 + hash_len;
3336 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3337 ssl->out_msg[0] = SSL_HS_FINISHED;
3338
3339 /*
3340 * In case of session resuming, invert the client and server
3341 * ChangeCipherSpec messages order.
3342 */
Paul Bakker0a597072012-09-25 21:55:46 +00003343 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003345#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003347 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003348#endif
3349#if defined(POLARSSL_SSL_SRV_C)
3350 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003352#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 }
3354 else
3355 ssl->state++;
3356
Paul Bakker48916f92012-09-16 19:57:18 +00003357 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003358 * Switch to our negotiated transform and session parameters for outbound
3359 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003360 */
3361 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3362 ssl->transform_out = ssl->transform_negotiate;
3363 ssl->session_out = ssl->session_negotiate;
3364 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003365
Paul Bakker07eb38b2012-12-19 14:42:06 +01003366#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003367 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003368 {
3369 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3370 {
3371 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3372 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3373 }
3374 }
3375#endif
3376
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3378 {
3379 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3380 return( ret );
3381 }
3382
3383 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3384
3385 return( 0 );
3386}
3387
3388int ssl_parse_finished( ssl_context *ssl )
3389{
Paul Bakker23986e52011-04-24 08:57:21 +00003390 int ret;
3391 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003392 unsigned char buf[36];
3393
3394 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3395
Paul Bakker48916f92012-09-16 19:57:18 +00003396 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003397
Paul Bakker48916f92012-09-16 19:57:18 +00003398 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003399 * Switch to our negotiated transform and session parameters for inbound
3400 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003401 */
3402 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3403 ssl->transform_in = ssl->transform_negotiate;
3404 ssl->session_in = ssl->session_negotiate;
3405 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003406
Paul Bakker92be97b2013-01-02 17:30:03 +01003407 /*
3408 * Set the in_msg pointer to the correct location based on IV length
3409 */
3410 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3411 {
3412 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3413 ssl->transform_negotiate->fixed_ivlen;
3414 }
3415 else
3416 ssl->in_msg = ssl->in_iv;
3417
Paul Bakker07eb38b2012-12-19 14:42:06 +01003418#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003419 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003420 {
3421 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3422 {
3423 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3424 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3425 }
3426 }
3427#endif
3428
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3430 {
3431 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3432 return( ret );
3433 }
3434
3435 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3436 {
3437 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003438 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003439 }
3440
Paul Bakker1ef83d62012-04-11 12:09:53 +00003441 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3443
3444 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3445 ssl->in_hslen != 4 + hash_len )
3446 {
3447 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003448 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 }
3450
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003451 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 {
3453 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003454 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003455 }
3456
Paul Bakker48916f92012-09-16 19:57:18 +00003457 ssl->verify_data_len = hash_len;
3458 memcpy( ssl->peer_verify_data, buf, hash_len );
3459
Paul Bakker0a597072012-09-25 21:55:46 +00003460 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003462#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 if( ssl->endpoint == SSL_IS_CLIENT )
3464 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003465#endif
3466#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003468 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003469#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 }
3471 else
3472 ssl->state++;
3473
3474 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3475
3476 return( 0 );
3477}
3478
Paul Bakker968afaa2014-07-09 11:09:24 +02003479static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003480{
3481 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3482
3483#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3484 defined(POLARSSL_SSL_PROTO_TLS1_1)
3485 md5_init( &handshake->fin_md5 );
3486 sha1_init( &handshake->fin_sha1 );
3487 md5_starts( &handshake->fin_md5 );
3488 sha1_starts( &handshake->fin_sha1 );
3489#endif
3490#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3491#if defined(POLARSSL_SHA256_C)
3492 sha256_init( &handshake->fin_sha256 );
3493 sha256_starts( &handshake->fin_sha256, 0 );
3494#endif
3495#if defined(POLARSSL_SHA512_C)
3496 sha512_init( &handshake->fin_sha512 );
3497 sha512_starts( &handshake->fin_sha512, 1 );
3498#endif
3499#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3500
3501 handshake->update_checksum = ssl_update_checksum_start;
3502 handshake->sig_alg = SSL_HASH_SHA1;
3503
3504#if defined(POLARSSL_DHM_C)
3505 dhm_init( &handshake->dhm_ctx );
3506#endif
3507#if defined(POLARSSL_ECDH_C)
3508 ecdh_init( &handshake->ecdh_ctx );
3509#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003510}
3511
3512static void ssl_transform_init( ssl_transform *transform )
3513{
3514 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003515
3516 cipher_init( &transform->cipher_ctx_enc );
3517 cipher_init( &transform->cipher_ctx_dec );
3518
3519 md_init( &transform->md_ctx_enc );
3520 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003521}
3522
3523void ssl_session_init( ssl_session *session )
3524{
3525 memset( session, 0, sizeof(ssl_session) );
3526}
3527
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003528static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003529{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003530 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003531 if( ssl->transform_negotiate )
3532 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003533 if( ssl->session_negotiate )
3534 ssl_session_free( ssl->session_negotiate );
3535 if( ssl->handshake )
3536 ssl_handshake_free( ssl->handshake );
3537
3538 /*
3539 * Either the pointers are now NULL or cleared properly and can be freed.
3540 * Now allocate missing structures.
3541 */
3542 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003543 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003544 ssl->transform_negotiate = (ssl_transform *) polarssl_malloc(
3545 sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003546 }
Paul Bakker48916f92012-09-16 19:57:18 +00003547
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003548 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003549 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003550 ssl->session_negotiate = (ssl_session *) polarssl_malloc(
3551 sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003552 }
Paul Bakker48916f92012-09-16 19:57:18 +00003553
Paul Bakker82788fb2014-10-20 13:59:19 +02003554 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003555 {
3556 ssl->handshake = (ssl_handshake_params *)
3557 polarssl_malloc( sizeof(ssl_handshake_params) );
3558 }
Paul Bakker48916f92012-09-16 19:57:18 +00003559
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003560 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003561 if( ssl->handshake == NULL ||
3562 ssl->transform_negotiate == NULL ||
3563 ssl->session_negotiate == NULL )
3564 {
3565 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003566
3567 polarssl_free( ssl->handshake );
3568 polarssl_free( ssl->transform_negotiate );
3569 polarssl_free( ssl->session_negotiate );
3570
3571 ssl->handshake = NULL;
3572 ssl->transform_negotiate = NULL;
3573 ssl->session_negotiate = NULL;
3574
Paul Bakker48916f92012-09-16 19:57:18 +00003575 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3576 }
3577
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003578 /* Initialize structures */
3579 ssl_session_init( ssl->session_negotiate );
3580 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003581 ssl_handshake_params_init( ssl->handshake );
3582
3583#if defined(POLARSSL_X509_CRT_PARSE_C)
3584 ssl->handshake->key_cert = ssl->key_cert;
3585#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003586
Paul Bakker48916f92012-09-16 19:57:18 +00003587 return( 0 );
3588}
3589
Paul Bakker5121ce52009-01-03 21:22:43 +00003590/*
3591 * Initialize an SSL context
3592 */
3593int ssl_init( ssl_context *ssl )
3594{
Paul Bakker48916f92012-09-16 19:57:18 +00003595 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 int len = SSL_BUFFER_LEN;
3597
3598 memset( ssl, 0, sizeof( ssl_context ) );
3599
Paul Bakker62f2dee2012-09-28 07:31:51 +00003600 /*
3601 * Sane defaults
3602 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003603 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3604 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3605 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3606 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003607
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003608 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003609
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003610 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3611
Paul Bakker62f2dee2012-09-28 07:31:51 +00003612#if defined(POLARSSL_DHM_C)
3613 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3614 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3615 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3616 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3617 {
3618 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3619 return( ret );
3620 }
3621#endif
3622
3623 /*
3624 * Prepare base structures
3625 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003626 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003627 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003628 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003629 ssl->in_msg = ssl->in_ctr + 13;
3630
3631 if( ssl->in_ctr == NULL )
3632 {
3633 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003634 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 }
3636
Paul Bakker6e339b52013-07-03 13:37:05 +02003637 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003638 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003639 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003640 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003641
3642 if( ssl->out_ctr == NULL )
3643 {
3644 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003645 polarssl_free( ssl->in_ctr );
3646 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003647 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003648 }
3649
3650 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3651 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3652
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003653#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3654 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3655#endif
3656
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003657#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3658 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3659#endif
3660
Paul Bakker606b4ba2013-08-14 16:52:14 +02003661#if defined(POLARSSL_SSL_SESSION_TICKETS)
3662 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3663#endif
3664
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003665#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003666 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003667#endif
3668
Paul Bakker48916f92012-09-16 19:57:18 +00003669 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3670 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003671
3672 return( 0 );
3673}
3674
3675/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003676 * Reset an initialized and used SSL context for re-use while retaining
3677 * all application-set variables, function pointers and data.
3678 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003679int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003680{
Paul Bakker48916f92012-09-16 19:57:18 +00003681 int ret;
3682
Paul Bakker7eb013f2011-10-06 12:37:39 +00003683 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003684 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3685 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3686
3687 ssl->verify_data_len = 0;
3688 memset( ssl->own_verify_data, 0, 36 );
3689 memset( ssl->peer_verify_data, 0, 36 );
3690
Paul Bakker7eb013f2011-10-06 12:37:39 +00003691 ssl->in_offt = NULL;
3692
Paul Bakker92be97b2013-01-02 17:30:03 +01003693 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003694 ssl->in_msgtype = 0;
3695 ssl->in_msglen = 0;
3696 ssl->in_left = 0;
3697
3698 ssl->in_hslen = 0;
3699 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003700 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003701
Paul Bakker92be97b2013-01-02 17:30:03 +01003702 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003703 ssl->out_msgtype = 0;
3704 ssl->out_msglen = 0;
3705 ssl->out_left = 0;
3706
Paul Bakker48916f92012-09-16 19:57:18 +00003707 ssl->transform_in = NULL;
3708 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003709
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003710 ssl->renego_records_seen = 0;
3711
Paul Bakker7eb013f2011-10-06 12:37:39 +00003712 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3713 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003714
3715#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003716 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003717 {
3718 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003719 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003720 {
3721 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3722 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3723 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003724 }
3725#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003726
Paul Bakker48916f92012-09-16 19:57:18 +00003727 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003728 {
Paul Bakker48916f92012-09-16 19:57:18 +00003729 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003730 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003731 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003732 }
Paul Bakker48916f92012-09-16 19:57:18 +00003733
Paul Bakkerc0463502013-02-14 11:19:38 +01003734 if( ssl->session )
3735 {
3736 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003737 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003738 ssl->session = NULL;
3739 }
3740
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003741#if defined(POLARSSL_SSL_ALPN)
3742 ssl->alpn_chosen = NULL;
3743#endif
3744
Paul Bakker48916f92012-09-16 19:57:18 +00003745 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3746 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003747
3748 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003749}
3750
Paul Bakkera503a632013-08-14 13:48:06 +02003751#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003752static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3753{
3754 aes_free( &tkeys->enc );
3755 aes_free( &tkeys->dec );
3756
3757 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3758}
3759
Paul Bakker7eb013f2011-10-06 12:37:39 +00003760/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003761 * Allocate and initialize ticket keys
3762 */
3763static int ssl_ticket_keys_init( ssl_context *ssl )
3764{
3765 int ret;
3766 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003767 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003768
3769 if( ssl->ticket_keys != NULL )
3770 return( 0 );
3771
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003772 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3773 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003774 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3775
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003776 aes_init( &tkeys->enc );
3777 aes_init( &tkeys->dec );
3778
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003779 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003780 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003781 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003782 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003783 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003784 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003785
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003786 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3787 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3788 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3789 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003790 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003791 polarssl_free( tkeys );
3792 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003793 }
3794
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003795 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003796 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003797 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003798 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003799 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003800 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003801
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003802 ssl->ticket_keys = tkeys;
3803
3804 return( 0 );
3805}
Paul Bakkera503a632013-08-14 13:48:06 +02003806#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003807
3808/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 * SSL set accessors
3810 */
3811void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3812{
3813 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003814
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003815#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3816 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003817 if( endpoint == SSL_IS_CLIENT )
3818 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003819#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003820}
3821
3822void ssl_set_authmode( ssl_context *ssl, int authmode )
3823{
3824 ssl->authmode = authmode;
3825}
3826
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003827#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003828void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003829 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003830 void *p_vrfy )
3831{
3832 ssl->f_vrfy = f_vrfy;
3833 ssl->p_vrfy = p_vrfy;
3834}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003835#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003836
Paul Bakker5121ce52009-01-03 21:22:43 +00003837void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003838 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003839 void *p_rng )
3840{
3841 ssl->f_rng = f_rng;
3842 ssl->p_rng = p_rng;
3843}
3844
3845void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003846 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003847 void *p_dbg )
3848{
3849 ssl->f_dbg = f_dbg;
3850 ssl->p_dbg = p_dbg;
3851}
3852
3853void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003854 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003855 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003856{
3857 ssl->f_recv = f_recv;
3858 ssl->f_send = f_send;
3859 ssl->p_recv = p_recv;
3860 ssl->p_send = p_send;
3861}
3862
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003863#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003864void ssl_set_session_cache( ssl_context *ssl,
3865 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3866 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003867{
Paul Bakker0a597072012-09-25 21:55:46 +00003868 ssl->f_get_cache = f_get_cache;
3869 ssl->p_get_cache = p_get_cache;
3870 ssl->f_set_cache = f_set_cache;
3871 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003872}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003873#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003874
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003875#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003876int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003877{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003878 int ret;
3879
3880 if( ssl == NULL ||
3881 session == NULL ||
3882 ssl->session_negotiate == NULL ||
3883 ssl->endpoint != SSL_IS_CLIENT )
3884 {
3885 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3886 }
3887
3888 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3889 return( ret );
3890
Paul Bakker0a597072012-09-25 21:55:46 +00003891 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003892
3893 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003894}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003895#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003896
Paul Bakkerb68cad62012-08-23 08:34:18 +00003897void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003898{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003899 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3900 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3901 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3902 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3903}
3904
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003905void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3906 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003907 int major, int minor )
3908{
3909 if( major != SSL_MAJOR_VERSION_3 )
3910 return;
3911
3912 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3913 return;
3914
3915 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003916}
3917
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003918#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003919/* Add a new (empty) key_cert entry an return a pointer to it */
3920static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3921{
3922 ssl_key_cert *key_cert, *last;
3923
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003924 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3925 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003926 return( NULL );
3927
3928 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3929
3930 /* Append the new key_cert to the (possibly empty) current list */
3931 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003932 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003933 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003934 if( ssl->handshake != NULL )
3935 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003936 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003937 else
3938 {
3939 last = ssl->key_cert;
3940 while( last->next != NULL )
3941 last = last->next;
3942 last->next = key_cert;
3943 }
3944
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003945 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003946}
3947
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003948void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003949 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003950{
3951 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003952 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 ssl->peer_cn = peer_cn;
3954}
3955
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003956int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003957 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003958{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003959 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3960
3961 if( key_cert == NULL )
3962 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3963
3964 key_cert->cert = own_cert;
3965 key_cert->key = pk_key;
3966
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003967 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003968}
3969
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003970#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003971int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003972 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003973{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003974 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003975 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003976
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003977 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003978 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3979
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003980 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3981 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003982 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003983
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003984 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003985
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003986 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003987 if( ret != 0 )
3988 return( ret );
3989
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003990 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003991 return( ret );
3992
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003993 key_cert->cert = own_cert;
3994 key_cert->key_own_alloc = 1;
3995
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003996 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003997}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003998#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003999
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004000int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004001 void *rsa_key,
4002 rsa_decrypt_func rsa_decrypt,
4003 rsa_sign_func rsa_sign,
4004 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004005{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004006 int ret;
4007 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004008
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004009 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004010 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4011
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004012 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4013 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004014 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004015
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004016 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004017
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004018 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4019 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4020 return( ret );
4021
4022 key_cert->cert = own_cert;
4023 key_cert->key_own_alloc = 1;
4024
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01004025 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00004026}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004027#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004028
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004029#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004030int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4031 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004032{
Paul Bakker6db455e2013-09-18 17:29:31 +02004033 if( psk == NULL || psk_identity == NULL )
4034 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4035
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004036 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004037 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4038
Paul Bakker6db455e2013-09-18 17:29:31 +02004039 if( ssl->psk != NULL )
4040 {
4041 polarssl_free( ssl->psk );
4042 polarssl_free( ssl->psk_identity );
4043 }
4044
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004045 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004046 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004047
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004048 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004049 ssl->psk_identity = (unsigned char *)
4050 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004051
4052 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4053 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4054
4055 memcpy( ssl->psk, psk, ssl->psk_len );
4056 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004057
4058 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004059}
4060
4061void ssl_set_psk_cb( ssl_context *ssl,
4062 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4063 size_t),
4064 void *p_psk )
4065{
4066 ssl->f_psk = f_psk;
4067 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004068}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004069#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004070
Paul Bakker48916f92012-09-16 19:57:18 +00004071#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004072int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004073{
4074 int ret;
4075
Paul Bakker48916f92012-09-16 19:57:18 +00004076 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004077 {
4078 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4079 return( ret );
4080 }
4081
Paul Bakker48916f92012-09-16 19:57:18 +00004082 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004083 {
4084 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4085 return( ret );
4086 }
4087
4088 return( 0 );
4089}
4090
Paul Bakker1b57b062011-01-06 15:48:19 +00004091int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4092{
4093 int ret;
4094
Paul Bakker66d5d072014-06-17 16:39:18 +02004095 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004096 {
4097 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4098 return( ret );
4099 }
4100
Paul Bakker66d5d072014-06-17 16:39:18 +02004101 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004102 {
4103 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4104 return( ret );
4105 }
4106
4107 return( 0 );
4108}
Paul Bakker48916f92012-09-16 19:57:18 +00004109#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004110
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004111#if defined(POLARSSL_SSL_SET_CURVES)
4112/*
4113 * Set the allowed elliptic curves
4114 */
4115void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4116{
4117 ssl->curve_list = curve_list;
4118}
4119#endif
4120
Paul Bakker0be444a2013-08-27 21:55:01 +02004121#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004122int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004123{
4124 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004125 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004126
4127 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004128
4129 if( ssl->hostname_len + 1 == 0 )
4130 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4131
Paul Bakker6e339b52013-07-03 13:37:05 +02004132 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004133
Paul Bakkerb15b8512012-01-13 13:44:06 +00004134 if( ssl->hostname == NULL )
4135 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4136
Paul Bakker3c2122f2013-06-24 19:03:14 +02004137 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004138 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004139
Paul Bakker40ea7de2009-05-03 10:18:48 +00004140 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004141
4142 return( 0 );
4143}
4144
Paul Bakker5701cdc2012-09-27 21:49:42 +00004145void ssl_set_sni( ssl_context *ssl,
4146 int (*f_sni)(void *, ssl_context *,
4147 const unsigned char *, size_t),
4148 void *p_sni )
4149{
4150 ssl->f_sni = f_sni;
4151 ssl->p_sni = p_sni;
4152}
Paul Bakker0be444a2013-08-27 21:55:01 +02004153#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004154
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004155#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004156int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004157{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004158 size_t cur_len, tot_len;
4159 const char **p;
4160
4161 /*
4162 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4163 * truncated". Check lengths now rather than later.
4164 */
4165 tot_len = 0;
4166 for( p = protos; *p != NULL; p++ )
4167 {
4168 cur_len = strlen( *p );
4169 tot_len += cur_len;
4170
4171 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4172 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4173 }
4174
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004175 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004176
4177 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004178}
4179
4180const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4181{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004182 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004183}
4184#endif /* POLARSSL_SSL_ALPN */
4185
Paul Bakker490ecc82011-10-06 13:04:09 +00004186void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4187{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004188 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4189 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4190 {
4191 ssl->max_major_ver = major;
4192 ssl->max_minor_ver = minor;
4193 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004194}
4195
Paul Bakker1d29fb52012-09-28 13:28:45 +00004196void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4197{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004198 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4199 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4200 {
4201 ssl->min_major_ver = major;
4202 ssl->min_minor_ver = minor;
4203 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004204}
4205
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004206#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4207void ssl_set_fallback( ssl_context *ssl, char fallback )
4208{
4209 ssl->fallback = fallback;
4210}
4211#endif
4212
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004213#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4214void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4215{
4216 ssl->encrypt_then_mac = etm;
4217}
4218#endif
4219
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004220#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4221void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4222{
4223 ssl->extended_ms = ems;
4224}
4225#endif
4226
Paul Bakker05decb22013-08-15 13:33:48 +02004227#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004228int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4229{
Paul Bakker77e257e2013-12-16 15:29:52 +01004230 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004231 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004232 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004233 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004234 }
4235
4236 ssl->mfl_code = mfl_code;
4237
4238 return( 0 );
4239}
Paul Bakker05decb22013-08-15 13:33:48 +02004240#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004241
Paul Bakker1f2bc622013-08-15 13:45:55 +02004242#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004243int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004244{
4245 if( ssl->endpoint != SSL_IS_CLIENT )
4246 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4247
Paul Bakker8c1ede62013-07-19 14:14:37 +02004248 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004249
4250 return( 0 );
4251}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004252#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004253
Paul Bakker48916f92012-09-16 19:57:18 +00004254void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4255{
4256 ssl->disable_renegotiation = renegotiation;
4257}
4258
4259void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4260{
4261 ssl->allow_legacy_renegotiation = allow_legacy;
4262}
4263
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004264void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4265{
4266 ssl->renego_max_records = max_records;
4267}
4268
Paul Bakkera503a632013-08-14 13:48:06 +02004269#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004270int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4271{
4272 ssl->session_tickets = use_tickets;
4273
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004274#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004275 if( ssl->endpoint == SSL_IS_CLIENT )
4276 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004277#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004278
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004279 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4280 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004281
4282 if( ssl->f_rng == NULL )
4283 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4284
4285 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004286}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004287
4288void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4289{
4290 ssl->ticket_lifetime = lifetime;
4291}
Paul Bakkera503a632013-08-14 13:48:06 +02004292#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004293
Paul Bakker5121ce52009-01-03 21:22:43 +00004294/*
4295 * SSL get accessors
4296 */
Paul Bakker23986e52011-04-24 08:57:21 +00004297size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004298{
4299 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4300}
4301
Paul Bakkerff60ee62010-03-16 21:09:09 +00004302int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004303{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004304 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004305}
4306
Paul Bakkere3166ce2011-01-27 17:40:50 +00004307const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004308{
Paul Bakker926c8e42013-03-06 10:23:34 +01004309 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004310 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004311
Paul Bakkere3166ce2011-01-27 17:40:50 +00004312 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004313}
4314
Paul Bakker43ca69c2011-01-15 17:35:19 +00004315const char *ssl_get_version( const ssl_context *ssl )
4316{
4317 switch( ssl->minor_ver )
4318 {
4319 case SSL_MINOR_VERSION_0:
4320 return( "SSLv3.0" );
4321
4322 case SSL_MINOR_VERSION_1:
4323 return( "TLSv1.0" );
4324
4325 case SSL_MINOR_VERSION_2:
4326 return( "TLSv1.1" );
4327
Paul Bakker1ef83d62012-04-11 12:09:53 +00004328 case SSL_MINOR_VERSION_3:
4329 return( "TLSv1.2" );
4330
Paul Bakker43ca69c2011-01-15 17:35:19 +00004331 default:
4332 break;
4333 }
4334 return( "unknown" );
4335}
4336
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004337#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004338const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004339{
4340 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004341 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004342
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004343 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004344}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004345#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004346
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004347#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004348int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4349{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004350 if( ssl == NULL ||
4351 dst == NULL ||
4352 ssl->session == NULL ||
4353 ssl->endpoint != SSL_IS_CLIENT )
4354 {
4355 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4356 }
4357
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004358 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004359}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004360#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004361
Paul Bakker5121ce52009-01-03 21:22:43 +00004362/*
Paul Bakker1961b702013-01-25 14:49:24 +01004363 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004364 */
Paul Bakker1961b702013-01-25 14:49:24 +01004365int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004366{
Paul Bakker40e46942009-01-03 21:51:57 +00004367 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004368
Paul Bakker40e46942009-01-03 21:51:57 +00004369#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004371 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004372#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004373#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004374 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004375 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004376#endif
4377
Paul Bakker1961b702013-01-25 14:49:24 +01004378 return( ret );
4379}
4380
4381/*
4382 * Perform the SSL handshake
4383 */
4384int ssl_handshake( ssl_context *ssl )
4385{
4386 int ret = 0;
4387
4388 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4389
4390 while( ssl->state != SSL_HANDSHAKE_OVER )
4391 {
4392 ret = ssl_handshake_step( ssl );
4393
4394 if( ret != 0 )
4395 break;
4396 }
4397
Paul Bakker5121ce52009-01-03 21:22:43 +00004398 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4399
4400 return( ret );
4401}
4402
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004403#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004404/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004405 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004406 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004407static int ssl_write_hello_request( ssl_context *ssl )
4408{
4409 int ret;
4410
4411 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4412
4413 ssl->out_msglen = 4;
4414 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4415 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4416
4417 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4418 {
4419 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4420 return( ret );
4421 }
4422
4423 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4424
4425 return( 0 );
4426}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004427#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004428
4429/*
4430 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004431 * - any side: calling ssl_renegotiate(),
4432 * - client: receiving a HelloRequest during ssl_read(),
4433 * - server: receiving any handshake message on server during ssl_read() after
4434 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004435 * If the handshake doesn't complete due to waiting for I/O, it will continue
4436 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004437 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004438static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004439{
4440 int ret;
4441
4442 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4443
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004444 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4445 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004446
4447 ssl->state = SSL_HELLO_REQUEST;
4448 ssl->renegotiation = SSL_RENEGOTIATION;
4449
Paul Bakker48916f92012-09-16 19:57:18 +00004450 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4451 {
4452 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4453 return( ret );
4454 }
4455
4456 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4457
4458 return( 0 );
4459}
4460
4461/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004462 * Renegotiate current connection on client,
4463 * or request renegotiation on server
4464 */
4465int ssl_renegotiate( ssl_context *ssl )
4466{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004467 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004468
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004469#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004470 /* On server, just send the request */
4471 if( ssl->endpoint == SSL_IS_SERVER )
4472 {
4473 if( ssl->state != SSL_HANDSHAKE_OVER )
4474 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4475
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004476 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4477
4478 /* Did we already try/start sending HelloRequest? */
4479 if( ssl->out_left != 0 )
4480 return( ssl_flush_output( ssl ) );
4481
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004482 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004483 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004484#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004485
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004486#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004487 /*
4488 * On client, either start the renegotiation process or,
4489 * if already in progress, continue the handshake
4490 */
4491 if( ssl->renegotiation != SSL_RENEGOTIATION )
4492 {
4493 if( ssl->state != SSL_HANDSHAKE_OVER )
4494 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4495
4496 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4497 {
4498 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4499 return( ret );
4500 }
4501 }
4502 else
4503 {
4504 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4505 {
4506 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4507 return( ret );
4508 }
4509 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004510#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004511
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004512 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004513}
4514
4515/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004516 * Receive application data decrypted from the SSL layer
4517 */
Paul Bakker23986e52011-04-24 08:57:21 +00004518int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004519{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004520 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004521 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004522
4523 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4524
4525 if( ssl->state != SSL_HANDSHAKE_OVER )
4526 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004527 ret = ssl_handshake( ssl );
4528 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4529 {
4530 record_read = 1;
4531 }
4532 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004533 {
4534 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4535 return( ret );
4536 }
4537 }
4538
4539 if( ssl->in_offt == NULL )
4540 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004541 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004542 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004543 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4544 {
4545 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4546 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004547
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004548 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4549 return( ret );
4550 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004551 }
4552
4553 if( ssl->in_msglen == 0 &&
4554 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4555 {
4556 /*
4557 * OpenSSL sends empty messages to randomize the IV
4558 */
4559 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4560 {
Paul Bakker831a7552011-05-18 13:32:51 +00004561 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4562 return( 0 );
4563
Paul Bakker5121ce52009-01-03 21:22:43 +00004564 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4565 return( ret );
4566 }
4567 }
4568
Paul Bakker48916f92012-09-16 19:57:18 +00004569 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4570 {
4571 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4572
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004573#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004574 if( ssl->endpoint == SSL_IS_CLIENT &&
4575 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4576 ssl->in_hslen != 4 ) )
4577 {
4578 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4579 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4580 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004581#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004582
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004583 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4584 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004585 ssl->allow_legacy_renegotiation ==
4586 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004587 {
4588 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4589
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004590#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004591 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004592 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004593 /*
4594 * SSLv3 does not have a "no_renegotiation" alert
4595 */
4596 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4597 return( ret );
4598 }
4599 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004600#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004601#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4602 defined(POLARSSL_SSL_PROTO_TLS1_2)
4603 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004604 {
4605 if( ( ret = ssl_send_alert_message( ssl,
4606 SSL_ALERT_LEVEL_WARNING,
4607 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4608 {
4609 return( ret );
4610 }
Paul Bakker48916f92012-09-16 19:57:18 +00004611 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004612 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004613#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4614 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004615 {
4616 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004617 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004618 }
Paul Bakker48916f92012-09-16 19:57:18 +00004619 }
4620 else
4621 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004622 ret = ssl_start_renegotiation( ssl );
4623 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4624 {
4625 record_read = 1;
4626 }
4627 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004628 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004629 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004630 return( ret );
4631 }
Paul Bakker48916f92012-09-16 19:57:18 +00004632 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004633
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004634 /* If a non-handshake record was read during renego, fallthrough,
4635 * else tell the user they should call ssl_read() again */
4636 if( ! record_read )
4637 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004638 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004639 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4640 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004641 ssl->renego_records_seen++;
4642
4643 if( ssl->renego_max_records >= 0 &&
4644 ssl->renego_records_seen > ssl->renego_max_records )
4645 {
4646 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4647 "but not honored by client" ) );
4648 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4649 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004650 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004651
4652 /* Fatal and closure alerts handled by ssl_read_record() */
4653 if( ssl->in_msgtype == SSL_MSG_ALERT )
4654 {
4655 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4656 return( POLARSSL_ERR_NET_WANT_READ );
4657 }
4658
4659 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004660 {
4661 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004662 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 }
4664
4665 ssl->in_offt = ssl->in_msg;
4666 }
4667
4668 n = ( len < ssl->in_msglen )
4669 ? len : ssl->in_msglen;
4670
4671 memcpy( buf, ssl->in_offt, n );
4672 ssl->in_msglen -= n;
4673
4674 if( ssl->in_msglen == 0 )
4675 /* all bytes consumed */
4676 ssl->in_offt = NULL;
4677 else
4678 /* more data available */
4679 ssl->in_offt += n;
4680
4681 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4682
Paul Bakker23986e52011-04-24 08:57:21 +00004683 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004684}
4685
4686/*
4687 * Send application data to be encrypted by the SSL layer
4688 */
Paul Bakker23986e52011-04-24 08:57:21 +00004689int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004690{
Paul Bakker23986e52011-04-24 08:57:21 +00004691 int ret;
4692 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004693 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004694
4695 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4696
4697 if( ssl->state != SSL_HANDSHAKE_OVER )
4698 {
4699 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4700 {
4701 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4702 return( ret );
4703 }
4704 }
4705
Paul Bakker05decb22013-08-15 13:33:48 +02004706#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004707 /*
4708 * Assume mfl_code is correct since it was checked when set
4709 */
4710 max_len = mfl_code_to_length[ssl->mfl_code];
4711
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004712 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004713 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004714 */
4715 if( ssl->session_out != NULL &&
4716 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4717 {
4718 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4719 }
Paul Bakker05decb22013-08-15 13:33:48 +02004720#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004721
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004722 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004723
Paul Bakker5121ce52009-01-03 21:22:43 +00004724 if( ssl->out_left != 0 )
4725 {
4726 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4727 {
4728 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4729 return( ret );
4730 }
4731 }
Paul Bakker887bd502011-06-08 13:10:54 +00004732 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004733 {
Paul Bakker887bd502011-06-08 13:10:54 +00004734 ssl->out_msglen = n;
4735 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4736 memcpy( ssl->out_msg, buf, n );
4737
4738 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4739 {
4740 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4741 return( ret );
4742 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004743 }
4744
4745 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4746
Paul Bakker23986e52011-04-24 08:57:21 +00004747 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004748}
4749
4750/*
4751 * Notify the peer that the connection is being closed
4752 */
4753int ssl_close_notify( ssl_context *ssl )
4754{
4755 int ret;
4756
4757 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4758
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004759 if( ssl->out_left != 0 )
4760 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004761
4762 if( ssl->state == SSL_HANDSHAKE_OVER )
4763 {
Paul Bakker48916f92012-09-16 19:57:18 +00004764 if( ( ret = ssl_send_alert_message( ssl,
4765 SSL_ALERT_LEVEL_WARNING,
4766 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004767 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004768 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004769 return( ret );
4770 }
4771 }
4772
4773 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4774
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004775 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004776}
4777
Paul Bakker48916f92012-09-16 19:57:18 +00004778void ssl_transform_free( ssl_transform *transform )
4779{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004780 if( transform == NULL )
4781 return;
4782
Paul Bakker48916f92012-09-16 19:57:18 +00004783#if defined(POLARSSL_ZLIB_SUPPORT)
4784 deflateEnd( &transform->ctx_deflate );
4785 inflateEnd( &transform->ctx_inflate );
4786#endif
4787
Paul Bakker84bbeb52014-07-01 14:53:22 +02004788 cipher_free( &transform->cipher_ctx_enc );
4789 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004790
Paul Bakker84bbeb52014-07-01 14:53:22 +02004791 md_free( &transform->md_ctx_enc );
4792 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004793
Paul Bakker34617722014-06-13 17:20:13 +02004794 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004795}
4796
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004797#if defined(POLARSSL_X509_CRT_PARSE_C)
4798static void ssl_key_cert_free( ssl_key_cert *key_cert )
4799{
4800 ssl_key_cert *cur = key_cert, *next;
4801
4802 while( cur != NULL )
4803 {
4804 next = cur->next;
4805
4806 if( cur->key_own_alloc )
4807 {
4808 pk_free( cur->key );
4809 polarssl_free( cur->key );
4810 }
4811 polarssl_free( cur );
4812
4813 cur = next;
4814 }
4815}
4816#endif /* POLARSSL_X509_CRT_PARSE_C */
4817
Paul Bakker48916f92012-09-16 19:57:18 +00004818void ssl_handshake_free( ssl_handshake_params *handshake )
4819{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004820 if( handshake == NULL )
4821 return;
4822
Paul Bakker48916f92012-09-16 19:57:18 +00004823#if defined(POLARSSL_DHM_C)
4824 dhm_free( &handshake->dhm_ctx );
4825#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004826#if defined(POLARSSL_ECDH_C)
4827 ecdh_free( &handshake->ecdh_ctx );
4828#endif
4829
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004830#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004831 /* explicit void pointer cast for buggy MS compiler */
4832 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004833#endif
4834
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004835#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4836 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4837 /*
4838 * Free only the linked list wrapper, not the keys themselves
4839 * since the belong to the SNI callback
4840 */
4841 if( handshake->sni_key_cert != NULL )
4842 {
4843 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4844
4845 while( cur != NULL )
4846 {
4847 next = cur->next;
4848 polarssl_free( cur );
4849 cur = next;
4850 }
4851 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004852#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004853
Paul Bakker34617722014-06-13 17:20:13 +02004854 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004855}
4856
4857void ssl_session_free( ssl_session *session )
4858{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004859 if( session == NULL )
4860 return;
4861
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004862#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004863 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004864 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004865 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004866 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004867 }
Paul Bakkered27a042013-04-18 22:46:23 +02004868#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004869
Paul Bakkera503a632013-08-14 13:48:06 +02004870#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004871 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004872#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004873
Paul Bakker34617722014-06-13 17:20:13 +02004874 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004875}
4876
Paul Bakker5121ce52009-01-03 21:22:43 +00004877/*
4878 * Free an SSL context
4879 */
4880void ssl_free( ssl_context *ssl )
4881{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004882 if( ssl == NULL )
4883 return;
4884
Paul Bakker5121ce52009-01-03 21:22:43 +00004885 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4886
Paul Bakker5121ce52009-01-03 21:22:43 +00004887 if( ssl->out_ctr != NULL )
4888 {
Paul Bakker34617722014-06-13 17:20:13 +02004889 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004890 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004891 }
4892
4893 if( ssl->in_ctr != NULL )
4894 {
Paul Bakker34617722014-06-13 17:20:13 +02004895 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004896 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004897 }
4898
Paul Bakker16770332013-10-11 09:59:44 +02004899#if defined(POLARSSL_ZLIB_SUPPORT)
4900 if( ssl->compress_buf != NULL )
4901 {
Paul Bakker34617722014-06-13 17:20:13 +02004902 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004903 polarssl_free( ssl->compress_buf );
4904 }
4905#endif
4906
Paul Bakker40e46942009-01-03 21:51:57 +00004907#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004908 mpi_free( &ssl->dhm_P );
4909 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004910#endif
4911
Paul Bakker48916f92012-09-16 19:57:18 +00004912 if( ssl->transform )
4913 {
4914 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004915 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004916 }
4917
4918 if( ssl->handshake )
4919 {
4920 ssl_handshake_free( ssl->handshake );
4921 ssl_transform_free( ssl->transform_negotiate );
4922 ssl_session_free( ssl->session_negotiate );
4923
Paul Bakker6e339b52013-07-03 13:37:05 +02004924 polarssl_free( ssl->handshake );
4925 polarssl_free( ssl->transform_negotiate );
4926 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004927 }
4928
Paul Bakkerc0463502013-02-14 11:19:38 +01004929 if( ssl->session )
4930 {
4931 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004932 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004933 }
4934
Paul Bakkera503a632013-08-14 13:48:06 +02004935#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004936 if( ssl->ticket_keys )
4937 {
4938 ssl_ticket_keys_free( ssl->ticket_keys );
4939 polarssl_free( ssl->ticket_keys );
4940 }
Paul Bakkera503a632013-08-14 13:48:06 +02004941#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004942
Paul Bakker0be444a2013-08-27 21:55:01 +02004943#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004944 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004945 {
Paul Bakker34617722014-06-13 17:20:13 +02004946 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004947 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004948 ssl->hostname_len = 0;
4949 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004950#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004951
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004952#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004953 if( ssl->psk != NULL )
4954 {
Paul Bakker34617722014-06-13 17:20:13 +02004955 polarssl_zeroize( ssl->psk, ssl->psk_len );
4956 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004957 polarssl_free( ssl->psk );
4958 polarssl_free( ssl->psk_identity );
4959 ssl->psk_len = 0;
4960 ssl->psk_identity_len = 0;
4961 }
4962#endif
4963
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004964#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004965 ssl_key_cert_free( ssl->key_cert );
4966#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004967
Paul Bakker05ef8352012-05-08 09:17:57 +00004968#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4969 if( ssl_hw_record_finish != NULL )
4970 {
4971 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4972 ssl_hw_record_finish( ssl );
4973 }
4974#endif
4975
Paul Bakker5121ce52009-01-03 21:22:43 +00004976 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004977
Paul Bakker86f04f42013-02-14 11:20:09 +01004978 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004979 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004980}
4981
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004982#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004983/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004984 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004985 */
4986unsigned char ssl_sig_from_pk( pk_context *pk )
4987{
4988#if defined(POLARSSL_RSA_C)
4989 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4990 return( SSL_SIG_RSA );
4991#endif
4992#if defined(POLARSSL_ECDSA_C)
4993 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4994 return( SSL_SIG_ECDSA );
4995#endif
4996 return( SSL_SIG_ANON );
4997}
4998
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004999pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5000{
5001 switch( sig )
5002 {
5003#if defined(POLARSSL_RSA_C)
5004 case SSL_SIG_RSA:
5005 return( POLARSSL_PK_RSA );
5006#endif
5007#if defined(POLARSSL_ECDSA_C)
5008 case SSL_SIG_ECDSA:
5009 return( POLARSSL_PK_ECDSA );
5010#endif
5011 default:
5012 return( POLARSSL_PK_NONE );
5013 }
5014}
Paul Bakker9af723c2014-05-01 13:03:14 +02005015#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005016
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005017/*
5018 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5019 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005020md_type_t ssl_md_alg_from_hash( unsigned char hash )
5021{
5022 switch( hash )
5023 {
5024#if defined(POLARSSL_MD5_C)
5025 case SSL_HASH_MD5:
5026 return( POLARSSL_MD_MD5 );
5027#endif
5028#if defined(POLARSSL_SHA1_C)
5029 case SSL_HASH_SHA1:
5030 return( POLARSSL_MD_SHA1 );
5031#endif
5032#if defined(POLARSSL_SHA256_C)
5033 case SSL_HASH_SHA224:
5034 return( POLARSSL_MD_SHA224 );
5035 case SSL_HASH_SHA256:
5036 return( POLARSSL_MD_SHA256 );
5037#endif
5038#if defined(POLARSSL_SHA512_C)
5039 case SSL_HASH_SHA384:
5040 return( POLARSSL_MD_SHA384 );
5041 case SSL_HASH_SHA512:
5042 return( POLARSSL_MD_SHA512 );
5043#endif
5044 default:
5045 return( POLARSSL_MD_NONE );
5046 }
5047}
5048
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005049#if defined(POLARSSL_SSL_SET_CURVES)
5050/*
5051 * Check is a curve proposed by the peer is in our list.
5052 * Return 1 if we're willing to use it, 0 otherwise.
5053 */
5054int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5055{
5056 const ecp_group_id *gid;
5057
5058 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5059 if( *gid == grp_id )
5060 return( 1 );
5061
5062 return( 0 );
5063}
Paul Bakker9af723c2014-05-01 13:03:14 +02005064#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005065
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005066#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005067int ssl_check_cert_usage( const x509_crt *cert,
5068 const ssl_ciphersuite_t *ciphersuite,
5069 int cert_endpoint )
5070{
5071#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5072 int usage = 0;
5073#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005074#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5075 const char *ext_oid;
5076 size_t ext_len;
5077#endif
5078
5079#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5080 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5081 ((void) cert);
5082 ((void) cert_endpoint);
5083#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005084
5085#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5086 if( cert_endpoint == SSL_IS_SERVER )
5087 {
5088 /* Server part of the key exchange */
5089 switch( ciphersuite->key_exchange )
5090 {
5091 case POLARSSL_KEY_EXCHANGE_RSA:
5092 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5093 usage = KU_KEY_ENCIPHERMENT;
5094 break;
5095
5096 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5097 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5098 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5099 usage = KU_DIGITAL_SIGNATURE;
5100 break;
5101
5102 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5103 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5104 usage = KU_KEY_AGREEMENT;
5105 break;
5106
5107 /* Don't use default: we want warnings when adding new values */
5108 case POLARSSL_KEY_EXCHANGE_NONE:
5109 case POLARSSL_KEY_EXCHANGE_PSK:
5110 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5111 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5112 usage = 0;
5113 }
5114 }
5115 else
5116 {
5117 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5118 usage = KU_DIGITAL_SIGNATURE;
5119 }
5120
5121 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5122 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005123#else
5124 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005125#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5126
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005127#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5128 if( cert_endpoint == SSL_IS_SERVER )
5129 {
5130 ext_oid = OID_SERVER_AUTH;
5131 ext_len = OID_SIZE( OID_SERVER_AUTH );
5132 }
5133 else
5134 {
5135 ext_oid = OID_CLIENT_AUTH;
5136 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5137 }
5138
5139 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5140 return( -1 );
5141#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5142
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005143 return( 0 );
5144}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005145#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005146
5147#endif /* POLARSSL_SSL_TLS_C */