blob: e44ffa6ee49c3ce6d9a12ebe75886e70910af4c5 [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
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
70static inline size_t ssl_ep_len( const ssl_context *ssl )
71{
72#if defined(POLARSSL_SSL_PROTO_DTLS)
73 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
74 return( 2 );
75#endif
76 return( 0 );
77}
78
Paul Bakker05decb22013-08-15 13:33:48 +020079#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020080/*
81 * Convert max_fragment_length codes to length.
82 * RFC 6066 says:
83 * enum{
84 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
85 * } MaxFragmentLength;
86 * and we add 0 -> extension unused
87 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020088static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020089{
90 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
91 512, /* SSL_MAX_FRAG_LEN_512 */
92 1024, /* SSL_MAX_FRAG_LEN_1024 */
93 2048, /* SSL_MAX_FRAG_LEN_2048 */
94 4096, /* SSL_MAX_FRAG_LEN_4096 */
95};
Paul Bakker05decb22013-08-15 13:33:48 +020096#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020097
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
99{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 ssl_session_free( dst );
101 memcpy( dst, src, sizeof( ssl_session ) );
102
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200104 if( src->peer_cert != NULL )
105 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200106 int ret;
107
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200108 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
109 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
111
Paul Bakkerb6b09562013-09-18 14:17:41 +0200112 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200114 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
115 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200116 {
117 polarssl_free( dst->peer_cert );
118 dst->peer_cert = NULL;
119 return( ret );
120 }
121 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200123
Paul Bakkera503a632013-08-14 13:48:06 +0200124#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200125 if( src->ticket != NULL )
126 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200127 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
128 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200129 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
130
131 memcpy( dst->ticket, src->ticket, src->ticket_len );
132 }
Paul Bakkera503a632013-08-14 13:48:06 +0200133#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200134
135 return( 0 );
136}
137
Paul Bakker05ef8352012-05-08 09:17:57 +0000138#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200139int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200140 const unsigned char *key_enc, const unsigned char *key_dec,
141 size_t keylen,
142 const unsigned char *iv_enc, const unsigned char *iv_dec,
143 size_t ivlen,
144 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200145 size_t maclen ) = NULL;
146int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
147int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
148int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
149int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
150int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200151#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000152
Paul Bakker5121ce52009-01-03 21:22:43 +0000153/*
154 * Key material generation
155 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200156#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200157static int ssl3_prf( const unsigned char *secret, size_t slen,
158 const char *label,
159 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000160 unsigned char *dstbuf, size_t dlen )
161{
162 size_t i;
163 md5_context md5;
164 sha1_context sha1;
165 unsigned char padding[16];
166 unsigned char sha1sum[20];
167 ((void)label);
168
Paul Bakker5b4af392014-06-26 12:09:34 +0200169 md5_init( &md5 );
170 sha1_init( &sha1 );
171
Paul Bakker5f70b252012-09-13 14:23:06 +0000172 /*
173 * SSLv3:
174 * block =
175 * MD5( secret + SHA1( 'A' + secret + random ) ) +
176 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
177 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
178 * ...
179 */
180 for( i = 0; i < dlen / 16; i++ )
181 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200182 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000183
184 sha1_starts( &sha1 );
185 sha1_update( &sha1, padding, 1 + i );
186 sha1_update( &sha1, secret, slen );
187 sha1_update( &sha1, random, rlen );
188 sha1_finish( &sha1, sha1sum );
189
190 md5_starts( &md5 );
191 md5_update( &md5, secret, slen );
192 md5_update( &md5, sha1sum, 20 );
193 md5_finish( &md5, dstbuf + i * 16 );
194 }
195
Paul Bakker5b4af392014-06-26 12:09:34 +0200196 md5_free( &md5 );
197 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000198
Paul Bakker34617722014-06-13 17:20:13 +0200199 polarssl_zeroize( padding, sizeof( padding ) );
200 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000201
202 return( 0 );
203}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200204#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000205
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200206#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200207static int tls1_prf( const unsigned char *secret, size_t slen,
208 const char *label,
209 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000210 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000211{
Paul Bakker23986e52011-04-24 08:57:21 +0000212 size_t nb, hs;
213 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200214 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 unsigned char tmp[128];
216 unsigned char h_i[20];
217
218 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000219 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 hs = ( slen + 1 ) / 2;
222 S1 = secret;
223 S2 = secret + slen - hs;
224
225 nb = strlen( label );
226 memcpy( tmp + 20, label, nb );
227 memcpy( tmp + 20 + nb, random, rlen );
228 nb += rlen;
229
230 /*
231 * First compute P_md5(secret,label+random)[0..dlen]
232 */
233 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
234
235 for( i = 0; i < dlen; i += 16 )
236 {
237 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
238 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
239
240 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
241
242 for( j = 0; j < k; j++ )
243 dstbuf[i + j] = h_i[j];
244 }
245
246 /*
247 * XOR out with P_sha1(secret,label+random)[0..dlen]
248 */
249 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
250
251 for( i = 0; i < dlen; i += 20 )
252 {
253 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
254 sha1_hmac( S2, hs, tmp, 20, tmp );
255
256 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
257
258 for( j = 0; j < k; j++ )
259 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
260 }
261
Paul Bakker34617722014-06-13 17:20:13 +0200262 polarssl_zeroize( tmp, sizeof( tmp ) );
263 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
265 return( 0 );
266}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200267#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200269#if defined(POLARSSL_SSL_PROTO_TLS1_2)
270#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200271static int tls_prf_sha256( const unsigned char *secret, size_t slen,
272 const char *label,
273 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000274 unsigned char *dstbuf, size_t dlen )
275{
276 size_t nb;
277 size_t i, j, k;
278 unsigned char tmp[128];
279 unsigned char h_i[32];
280
281 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
282 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
283
284 nb = strlen( label );
285 memcpy( tmp + 32, label, nb );
286 memcpy( tmp + 32 + nb, random, rlen );
287 nb += rlen;
288
289 /*
290 * Compute P_<hash>(secret, label + random)[0..dlen]
291 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200292 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000293
294 for( i = 0; i < dlen; i += 32 )
295 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200296 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
297 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000298
299 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
300
301 for( j = 0; j < k; j++ )
302 dstbuf[i + j] = h_i[j];
303 }
304
Paul Bakker34617722014-06-13 17:20:13 +0200305 polarssl_zeroize( tmp, sizeof( tmp ) );
306 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000307
308 return( 0 );
309}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200310#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000311
Paul Bakker9e36f042013-06-30 14:34:05 +0200312#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200313static int tls_prf_sha384( const unsigned char *secret, size_t slen,
314 const char *label,
315 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000316 unsigned char *dstbuf, size_t dlen )
317{
318 size_t nb;
319 size_t i, j, k;
320 unsigned char tmp[128];
321 unsigned char h_i[48];
322
323 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
324 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
325
326 nb = strlen( label );
327 memcpy( tmp + 48, label, nb );
328 memcpy( tmp + 48 + nb, random, rlen );
329 nb += rlen;
330
331 /*
332 * Compute P_<hash>(secret, label + random)[0..dlen]
333 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200334 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000335
336 for( i = 0; i < dlen; i += 48 )
337 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200338 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
339 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000340
341 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
342
343 for( j = 0; j < k; j++ )
344 dstbuf[i + j] = h_i[j];
345 }
346
Paul Bakker34617722014-06-13 17:20:13 +0200347 polarssl_zeroize( tmp, sizeof( tmp ) );
348 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000349
350 return( 0 );
351}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif /* POLARSSL_SHA512_C */
353#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000354
Paul Bakker66d5d072014-06-17 16:39:18 +0200355static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200356
357#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
358 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200359static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
Paul Bakker380da532012-04-18 16:10:25 +0000361
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200362#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200363static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
364static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200365#endif
366
367#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200368static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
369static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200370#endif
371
372#if defined(POLARSSL_SSL_PROTO_TLS1_2)
373#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200374static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
375static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
376static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200377#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100378
Paul Bakker9e36f042013-06-30 14:34:05 +0200379#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200380static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
381static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
382static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100383#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200384#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000385
Paul Bakker5121ce52009-01-03 21:22:43 +0000386int ssl_derive_keys( ssl_context *ssl )
387{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200388 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 unsigned char keyblk[256];
391 unsigned char *key1;
392 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100393 unsigned char *mac_enc;
394 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200395 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100396 const cipher_info_t *cipher_info;
397 const md_info_t *md_info;
398
Paul Bakker48916f92012-09-16 19:57:18 +0000399 ssl_session *session = ssl->session_negotiate;
400 ssl_transform *transform = ssl->transform_negotiate;
401 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
404
Paul Bakker68884e32013-01-07 18:20:04 +0100405 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
406 if( cipher_info == NULL )
407 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200408 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100409 transform->ciphersuite_info->cipher ) );
410 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
411 }
412
413 md_info = md_info_from_type( transform->ciphersuite_info->mac );
414 if( md_info == NULL )
415 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200416 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100417 transform->ciphersuite_info->mac ) );
418 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
419 }
420
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000422 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000423 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200424#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000425 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000426 {
Paul Bakker48916f92012-09-16 19:57:18 +0000427 handshake->tls_prf = ssl3_prf;
428 handshake->calc_verify = ssl_calc_verify_ssl;
429 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000430 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431 else
432#endif
433#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
434 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000435 {
Paul Bakker48916f92012-09-16 19:57:18 +0000436 handshake->tls_prf = tls1_prf;
437 handshake->calc_verify = ssl_calc_verify_tls;
438 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000439 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200440 else
441#endif
442#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200443#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
445 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 {
Paul Bakker48916f92012-09-16 19:57:18 +0000447 handshake->tls_prf = tls_prf_sha384;
448 handshake->calc_verify = ssl_calc_verify_tls_sha384;
449 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000450 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000451 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200452#endif
453#if defined(POLARSSL_SHA256_C)
454 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000455 {
Paul Bakker48916f92012-09-16 19:57:18 +0000456 handshake->tls_prf = tls_prf_sha256;
457 handshake->calc_verify = ssl_calc_verify_tls_sha256;
458 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000459 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200460 else
461#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200462#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200463 {
464 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200465 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200466 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000467
468 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 * SSLv3:
470 * master =
471 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
472 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
473 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200474 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200475 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 * master = PRF( premaster, "master secret", randbytes )[0..47]
477 */
Paul Bakker0a597072012-09-25 21:55:46 +0000478 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 {
Paul Bakker48916f92012-09-16 19:57:18 +0000480 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
481 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
Paul Bakker48916f92012-09-16 19:57:18 +0000483 handshake->tls_prf( handshake->premaster, handshake->pmslen,
484 "master secret",
485 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
Paul Bakker34617722014-06-13 17:20:13 +0200487 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 }
489 else
490 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
491
492 /*
493 * Swap the client and server random values.
494 */
Paul Bakker48916f92012-09-16 19:57:18 +0000495 memcpy( tmp, handshake->randbytes, 64 );
496 memcpy( handshake->randbytes, tmp + 32, 32 );
497 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200498 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 /*
501 * SSLv3:
502 * key block =
503 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
504 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
505 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
506 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
507 * ...
508 *
509 * TLSv1:
510 * key block = PRF( master, "key expansion", randbytes )
511 */
Paul Bakker48916f92012-09-16 19:57:18 +0000512 handshake->tls_prf( session->master, 48, "key expansion",
513 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
Paul Bakker48916f92012-09-16 19:57:18 +0000515 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
516 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
517 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
518 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
520
Paul Bakker34617722014-06-13 17:20:13 +0200521 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523 /*
524 * Determine the appropriate key, IV and MAC length.
525 */
Paul Bakker68884e32013-01-07 18:20:04 +0100526
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200527 transform->keylen = cipher_info->key_length / 8;
528
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200529 if( cipher_info->mode == POLARSSL_MODE_GCM ||
530 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200532 transform->maclen = 0;
533
Paul Bakker68884e32013-01-07 18:20:04 +0100534 transform->ivlen = 12;
535 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200536
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200537 /* Minimum length is expicit IV + tag */
538 transform->minlen = transform->ivlen - transform->fixed_ivlen
539 + ( transform->ciphersuite_info->flags &
540 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100541 }
542 else
543 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200544 int ret;
545
546 /* Initialize HMAC contexts */
547 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
548 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100549 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200550 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
551 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100552 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 /* Get MAC length */
555 transform->maclen = md_get_size( md_info );
556
557#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
558 /*
559 * If HMAC is to be truncated, we shall keep the leftmost bytes,
560 * (rfc 6066 page 13 or rfc 2104 section 4),
561 * so we only need to adjust the length here.
562 */
563 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
564 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
565#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
566
567 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100568 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200570 /* Minimum length */
571 if( cipher_info->mode == POLARSSL_MODE_STREAM )
572 transform->minlen = transform->maclen;
573 else
Paul Bakker68884e32013-01-07 18:20:04 +0100574 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200575 /*
576 * GenericBlockCipher:
577 * first multiple of blocklen greater than maclen
578 * + IV except for SSL3 and TLS 1.0
579 */
580 transform->minlen = transform->maclen
581 + cipher_info->block_size
582 - transform->maclen % cipher_info->block_size;
583
584#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
585 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
586 ssl->minor_ver == SSL_MINOR_VERSION_1 )
587 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100588 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200589#endif
590#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
591 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
592 ssl->minor_ver == SSL_MINOR_VERSION_3 )
593 {
594 transform->minlen += transform->ivlen;
595 }
596 else
597#endif
598 {
599 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
600 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
601 }
Paul Bakker68884e32013-01-07 18:20:04 +0100602 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 }
604
605 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000606 transform->keylen, transform->minlen, transform->ivlen,
607 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
609 /*
610 * Finally setup the cipher contexts, IVs and MAC secrets.
611 */
612 if( ssl->endpoint == SSL_IS_CLIENT )
613 {
Paul Bakker48916f92012-09-16 19:57:18 +0000614 key1 = keyblk + transform->maclen * 2;
615 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Paul Bakker68884e32013-01-07 18:20:04 +0100617 mac_enc = keyblk;
618 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000620 /*
621 * This is not used in TLS v1.1.
622 */
Paul Bakker48916f92012-09-16 19:57:18 +0000623 iv_copy_len = ( transform->fixed_ivlen ) ?
624 transform->fixed_ivlen : transform->ivlen;
625 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
626 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000627 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 }
629 else
630 {
Paul Bakker48916f92012-09-16 19:57:18 +0000631 key1 = keyblk + transform->maclen * 2 + transform->keylen;
632 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Paul Bakker68884e32013-01-07 18:20:04 +0100634 mac_enc = keyblk + transform->maclen;
635 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000637 /*
638 * This is not used in TLS v1.1.
639 */
Paul Bakker48916f92012-09-16 19:57:18 +0000640 iv_copy_len = ( transform->fixed_ivlen ) ?
641 transform->fixed_ivlen : transform->ivlen;
642 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
643 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000644 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 }
646
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200647#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100648 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
649 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100650 if( transform->maclen > sizeof transform->mac_enc )
651 {
652 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200653 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100654 }
655
Paul Bakker68884e32013-01-07 18:20:04 +0100656 memcpy( transform->mac_enc, mac_enc, transform->maclen );
657 memcpy( transform->mac_dec, mac_dec, transform->maclen );
658 }
659 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200660#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200661#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
662 defined(POLARSSL_SSL_PROTO_TLS1_2)
663 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100664 {
665 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
666 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
667 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200668 else
669#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200670 {
671 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200672 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200673 }
Paul Bakker68884e32013-01-07 18:20:04 +0100674
Paul Bakker05ef8352012-05-08 09:17:57 +0000675#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200676 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000677 {
678 int ret = 0;
679
680 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
681
Paul Bakker07eb38b2012-12-19 14:42:06 +0100682 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
683 transform->iv_enc, transform->iv_dec,
684 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100685 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100686 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000687 {
688 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200689 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000690 }
691 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200692#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000693
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200694 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
695 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200697 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
698 return( ret );
699 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200700
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200701 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
702 cipher_info ) ) != 0 )
703 {
704 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
705 return( ret );
706 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200707
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200708 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
709 cipher_info->key_length,
710 POLARSSL_ENCRYPT ) ) != 0 )
711 {
712 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
713 return( ret );
714 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200715
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200716 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
717 cipher_info->key_length,
718 POLARSSL_DECRYPT ) ) != 0 )
719 {
720 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
721 return( ret );
722 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200723
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200724#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200725 if( cipher_info->mode == POLARSSL_MODE_CBC )
726 {
727 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
728 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200729 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200730 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
731 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200732 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200733
734 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
735 POLARSSL_PADDING_NONE ) ) != 0 )
736 {
737 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
738 return( ret );
739 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200741#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Paul Bakker34617722014-06-13 17:20:13 +0200743 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Paul Bakker2770fbd2012-07-03 13:30:23 +0000745#if defined(POLARSSL_ZLIB_SUPPORT)
746 // Initialize compression
747 //
Paul Bakker48916f92012-09-16 19:57:18 +0000748 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000749 {
Paul Bakker16770332013-10-11 09:59:44 +0200750 if( ssl->compress_buf == NULL )
751 {
752 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
753 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
754 if( ssl->compress_buf == NULL )
755 {
756 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
757 SSL_BUFFER_LEN ) );
758 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
759 }
760 }
761
Paul Bakker2770fbd2012-07-03 13:30:23 +0000762 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
763
Paul Bakker48916f92012-09-16 19:57:18 +0000764 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
765 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000766
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200767 if( deflateInit( &transform->ctx_deflate,
768 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000769 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000770 {
771 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
772 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
773 }
774 }
775#endif /* POLARSSL_ZLIB_SUPPORT */
776
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
778
779 return( 0 );
780}
781
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200782#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000783void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000784{
785 md5_context md5;
786 sha1_context sha1;
787 unsigned char pad_1[48];
788 unsigned char pad_2[48];
789
Paul Bakker380da532012-04-18 16:10:25 +0000790 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
Paul Bakker48916f92012-09-16 19:57:18 +0000792 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
793 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Paul Bakker380da532012-04-18 16:10:25 +0000795 memset( pad_1, 0x36, 48 );
796 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Paul Bakker48916f92012-09-16 19:57:18 +0000798 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000799 md5_update( &md5, pad_1, 48 );
800 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
Paul Bakker380da532012-04-18 16:10:25 +0000802 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000803 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000804 md5_update( &md5, pad_2, 48 );
805 md5_update( &md5, hash, 16 );
806 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000807
Paul Bakker48916f92012-09-16 19:57:18 +0000808 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000809 sha1_update( &sha1, pad_1, 40 );
810 sha1_finish( &sha1, hash + 16 );
811
812 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000813 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000814 sha1_update( &sha1, pad_2, 40 );
815 sha1_update( &sha1, hash + 16, 20 );
816 sha1_finish( &sha1, hash + 16 );
817
818 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
819 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
820
Paul Bakker5b4af392014-06-26 12:09:34 +0200821 md5_free( &md5 );
822 sha1_free( &sha1 );
823
Paul Bakker380da532012-04-18 16:10:25 +0000824 return;
825}
Paul Bakker9af723c2014-05-01 13:03:14 +0200826#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000827
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000829void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
830{
831 md5_context md5;
832 sha1_context sha1;
833
834 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
837 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000838
Paul Bakker48916f92012-09-16 19:57:18 +0000839 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000840 sha1_finish( &sha1, hash + 16 );
841
842 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
843 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
844
Paul Bakker5b4af392014-06-26 12:09:34 +0200845 md5_free( &md5 );
846 sha1_free( &sha1 );
847
Paul Bakker380da532012-04-18 16:10:25 +0000848 return;
849}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200850#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000851
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200852#if defined(POLARSSL_SSL_PROTO_TLS1_2)
853#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000854void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
855{
Paul Bakker9e36f042013-06-30 14:34:05 +0200856 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000857
858 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
859
Paul Bakker9e36f042013-06-30 14:34:05 +0200860 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
861 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000862
863 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
864 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
865
Paul Bakker5b4af392014-06-26 12:09:34 +0200866 sha256_free( &sha256 );
867
Paul Bakker380da532012-04-18 16:10:25 +0000868 return;
869}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200870#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000871
Paul Bakker9e36f042013-06-30 14:34:05 +0200872#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000873void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
874{
Paul Bakker9e36f042013-06-30 14:34:05 +0200875 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000876
877 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
878
Paul Bakker9e36f042013-06-30 14:34:05 +0200879 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
880 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
Paul Bakkerca4ab492012-04-18 14:23:57 +0000882 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
884
Paul Bakker5b4af392014-06-26 12:09:34 +0200885 sha512_free( &sha512 );
886
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 return;
888}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200889#endif /* POLARSSL_SHA512_C */
890#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200892#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200893int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
894{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895 unsigned char *p = ssl->handshake->premaster;
896 unsigned char *end = p + sizeof( ssl->handshake->premaster );
897
898 /*
899 * PMS = struct {
900 * opaque other_secret<0..2^16-1>;
901 * opaque psk<0..2^16-1>;
902 * };
903 * with "other_secret" depending on the particular key exchange
904 */
905#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
906 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
907 {
908 if( end - p < 2 + (int) ssl->psk_len )
909 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
910
911 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
912 *(p++) = (unsigned char)( ssl->psk_len );
913 p += ssl->psk_len;
914 }
915 else
916#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200917#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
918 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
919 {
920 /*
921 * other_secret already set by the ClientKeyExchange message,
922 * and is 48 bytes long
923 */
924 *p++ = 0;
925 *p++ = 48;
926 p += 48;
927 }
928 else
929#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200930#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
931 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
932 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200933 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200934 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200935
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200936 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200938 p + 2, &len,
939 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200940 {
941 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
942 return( ret );
943 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200944 *(p++) = (unsigned char)( len >> 8 );
945 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200946 p += len;
947
948 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
949 }
950 else
951#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
952#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
953 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
954 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200955 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200956 size_t zlen;
957
958 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200959 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200960 ssl->f_rng, ssl->p_rng ) ) != 0 )
961 {
962 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
963 return( ret );
964 }
965
966 *(p++) = (unsigned char)( zlen >> 8 );
967 *(p++) = (unsigned char)( zlen );
968 p += zlen;
969
970 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
971 }
972 else
973#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
974 {
975 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200976 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200977 }
978
979 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100980 if( end - p < 2 + (int) ssl->psk_len )
981 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
982
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
984 *(p++) = (unsigned char)( ssl->psk_len );
985 memcpy( p, ssl->psk, ssl->psk_len );
986 p += ssl->psk_len;
987
988 ssl->handshake->pmslen = p - ssl->handshake->premaster;
989
990 return( 0 );
991}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200992#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200993
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200994#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000995/*
996 * SSLv3.0 MAC functions
997 */
Paul Bakker68884e32013-01-07 18:20:04 +0100998static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
999 unsigned char *buf, size_t len,
1000 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001001{
1002 unsigned char header[11];
1003 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001004 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001005 int md_size = md_get_size( md_ctx->md_info );
1006 int md_type = md_get_type( md_ctx->md_info );
1007
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001008 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001009 if( md_type == POLARSSL_MD_MD5 )
1010 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001011 else
Paul Bakker68884e32013-01-07 18:20:04 +01001012 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
1014 memcpy( header, ctr, 8 );
1015 header[ 8] = (unsigned char) type;
1016 header[ 9] = (unsigned char)( len >> 8 );
1017 header[10] = (unsigned char)( len );
1018
Paul Bakker68884e32013-01-07 18:20:04 +01001019 memset( padding, 0x36, padlen );
1020 md_starts( md_ctx );
1021 md_update( md_ctx, secret, md_size );
1022 md_update( md_ctx, padding, padlen );
1023 md_update( md_ctx, header, 11 );
1024 md_update( md_ctx, buf, len );
1025 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Paul Bakker68884e32013-01-07 18:20:04 +01001027 memset( padding, 0x5C, padlen );
1028 md_starts( md_ctx );
1029 md_update( md_ctx, secret, md_size );
1030 md_update( md_ctx, padding, padlen );
1031 md_update( md_ctx, buf + len, md_size );
1032 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001033}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001034#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001035
Paul Bakker5121ce52009-01-03 21:22:43 +00001036/*
1037 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001038 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001039static int ssl_encrypt_buf( ssl_context *ssl )
1040{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001041 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001042 const cipher_mode_t mode = cipher_get_cipher_mode(
1043 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
1045 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1046
1047 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001048 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001049 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001050#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1051 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1052 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001053 if( mode != POLARSSL_MODE_GCM &&
1054 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001056#if defined(POLARSSL_SSL_PROTO_SSL3)
1057 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1058 {
1059 ssl_mac( &ssl->transform_out->md_ctx_enc,
1060 ssl->transform_out->mac_enc,
1061 ssl->out_msg, ssl->out_msglen,
1062 ssl->out_ctr, ssl->out_msgtype );
1063 }
1064 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001065#endif
1066#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001067 defined(POLARSSL_SSL_PROTO_TLS1_2)
1068 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1069 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001070 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1071 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1072 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001073 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1074 ssl->out_msg, ssl->out_msglen );
1075 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1076 ssl->out_msg + ssl->out_msglen );
1077 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1078 }
1079 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001080#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001081 {
1082 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001083 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001084 }
1085
1086 SSL_DEBUG_BUF( 4, "computed mac",
1087 ssl->out_msg + ssl->out_msglen,
1088 ssl->transform_out->maclen );
1089
1090 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001091 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001092#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001094 /*
1095 * Encrypt
1096 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001097#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001098 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001100 int ret;
1101 size_t olen = 0;
1102
Paul Bakker5121ce52009-01-03 21:22:43 +00001103 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1104 "including %d bytes of padding",
1105 ssl->out_msglen, 0 ) );
1106
1107 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1108 ssl->out_msg, ssl->out_msglen );
1109
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001110 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001111 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001112 ssl->transform_out->ivlen,
1113 ssl->out_msg, ssl->out_msglen,
1114 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001115 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001116 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001117 return( ret );
1118 }
1119
1120 if( ssl->out_msglen != olen )
1121 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001122 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001123 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001124 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001125 }
Paul Bakker68884e32013-01-07 18:20:04 +01001126 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001127#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001128#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1129 if( mode == POLARSSL_MODE_GCM ||
1130 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001132 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001133 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001134 unsigned char *enc_msg;
1135 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001136 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1137 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001138
Paul Bakkerca4ab492012-04-18 14:23:57 +00001139 memcpy( add_data, ssl->out_ctr, 8 );
1140 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001141 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1142 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001143 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1144 add_data[12] = ssl->out_msglen & 0xFF;
1145
1146 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1147 add_data, 13 );
1148
Paul Bakker68884e32013-01-07 18:20:04 +01001149 /*
1150 * Generate IV
1151 */
1152 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001153 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1154 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001155 if( ret != 0 )
1156 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001157
Paul Bakker68884e32013-01-07 18:20:04 +01001158 memcpy( ssl->out_iv,
1159 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1160 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001161
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001162 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001163 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001164
Paul Bakker68884e32013-01-07 18:20:04 +01001165 /*
1166 * Fix pointer positions and message length with added IV
1167 */
1168 enc_msg = ssl->out_msg;
1169 enc_msglen = ssl->out_msglen;
1170 ssl->out_msglen += ssl->transform_out->ivlen -
1171 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001172
Paul Bakker68884e32013-01-07 18:20:04 +01001173 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1174 "including %d bytes of padding",
1175 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001176
Paul Bakker68884e32013-01-07 18:20:04 +01001177 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1178 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001179
Paul Bakker68884e32013-01-07 18:20:04 +01001180 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001181 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001182 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001183 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1184 ssl->transform_out->iv_enc,
1185 ssl->transform_out->ivlen,
1186 add_data, 13,
1187 enc_msg, enc_msglen,
1188 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001189 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001190 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001191 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001192 return( ret );
1193 }
1194
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001195 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001196 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001197 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001198 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001199 }
1200
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001201 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001203 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001204 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001206#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001207#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1208 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001209 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001211 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001212 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001213 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001214
Paul Bakker48916f92012-09-16 19:57:18 +00001215 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1216 ssl->transform_out->ivlen;
1217 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 padlen = 0;
1219
1220 for( i = 0; i <= padlen; i++ )
1221 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1222
1223 ssl->out_msglen += padlen + 1;
1224
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001225 enc_msglen = ssl->out_msglen;
1226 enc_msg = ssl->out_msg;
1227
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001228#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001229 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001230 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1231 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001232 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001233 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001234 {
1235 /*
1236 * Generate IV
1237 */
Paul Bakker48916f92012-09-16 19:57:18 +00001238 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1239 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001240 if( ret != 0 )
1241 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242
Paul Bakker92be97b2013-01-02 17:30:03 +01001243 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001244 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001245
1246 /*
1247 * Fix pointer positions and message length with added IV
1248 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001249 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001250 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001251 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001252 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001253#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001254
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001256 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001257 ssl->out_msglen, ssl->transform_out->ivlen,
1258 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
1260 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001261 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001263 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001264 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001265 ssl->transform_out->ivlen,
1266 enc_msg, enc_msglen,
1267 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001268 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001269 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001270 return( ret );
1271 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001272
Paul Bakkercca5b812013-08-31 17:40:26 +02001273 if( enc_msglen != olen )
1274 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001275 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001276 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001277 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001278
1279#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001280 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1281 {
1282 /*
1283 * Save IV in SSL3 and TLS1
1284 */
1285 memcpy( ssl->transform_out->iv_enc,
1286 ssl->transform_out->cipher_ctx_enc.iv,
1287 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001289#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001291 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001292#endif /* POLARSSL_CIPHER_MODE_CBC &&
1293 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001294 {
1295 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001296 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001297 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
1299 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1300
1301 return( 0 );
1302}
1303
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001304#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001305
Paul Bakker5121ce52009-01-03 21:22:43 +00001306static int ssl_decrypt_buf( ssl_context *ssl )
1307{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001308 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001309 const cipher_mode_t mode = cipher_get_cipher_mode(
1310 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001311#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1312 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1313 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1314 size_t padlen = 0, correct = 1;
1315#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
1317 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1318
Paul Bakker48916f92012-09-16 19:57:18 +00001319 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 {
1321 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001322 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001323 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 }
1325
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001326#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001327 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001328 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001329 int ret;
1330 size_t olen = 0;
1331
Paul Bakker68884e32013-01-07 18:20:04 +01001332 padlen = 0;
1333
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001334 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001335 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001336 ssl->transform_in->ivlen,
1337 ssl->in_msg, ssl->in_msglen,
1338 ssl->in_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 Bakkerea6ad3f2013-09-02 14:57:01 +02001341 return( ret );
1342 }
1343
1344 if( ssl->in_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 Bakkerea6ad3f2013-09-02 14:57:01 +02001348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 }
Paul Bakker68884e32013-01-07 18:20:04 +01001350 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001351#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001352#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1353 if( mode == POLARSSL_MODE_GCM ||
1354 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001355 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001356 int ret;
1357 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001358 unsigned char *dec_msg;
1359 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001360 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001361 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1362 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001363 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1364 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001365
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001366 if( ssl->in_msglen < explicit_iv_len + taglen )
1367 {
1368 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1369 "+ taglen (%d)", ssl->in_msglen,
1370 explicit_iv_len, taglen ) );
1371 return( POLARSSL_ERR_SSL_INVALID_MAC );
1372 }
1373 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1374
Paul Bakker68884e32013-01-07 18:20:04 +01001375 dec_msg = ssl->in_msg;
1376 dec_msg_result = ssl->in_msg;
1377 ssl->in_msglen = dec_msglen;
1378
1379 memcpy( add_data, ssl->in_ctr, 8 );
1380 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001381 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1382 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001383 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1384 add_data[12] = ssl->in_msglen & 0xFF;
1385
1386 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1387 add_data, 13 );
1388
1389 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1390 ssl->in_iv,
1391 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1392
1393 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1394 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001395 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001396
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001397 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001398 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001399 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001400 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1401 ssl->transform_in->iv_dec,
1402 ssl->transform_in->ivlen,
1403 add_data, 13,
1404 dec_msg, dec_msglen,
1405 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001406 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001407 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001408 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1409
1410 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1411 return( POLARSSL_ERR_SSL_INVALID_MAC );
1412
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001413 return( ret );
1414 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001415
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001416 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001417 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001418 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001419 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001420 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001423#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001424#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1425 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001426 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 {
Paul Bakker45829992013-01-03 14:52:21 +01001428 /*
1429 * Decrypt and check the padding
1430 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001431 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001432 unsigned char *dec_msg;
1433 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001434 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001435 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001436 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001437
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 /*
Paul Bakker45829992013-01-03 14:52:21 +01001439 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 */
Paul Bakker48916f92012-09-16 19:57:18 +00001441 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 {
1443 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001444 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001445 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 }
1447
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001448#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001449 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1450 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001451#endif
Paul Bakker45829992013-01-03 14:52:21 +01001452
1453 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1454 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1455 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001456 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1457 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1458 ssl->transform_in->ivlen,
1459 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001460 return( POLARSSL_ERR_SSL_INVALID_MAC );
1461 }
1462
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001463 dec_msglen = ssl->in_msglen;
1464 dec_msg = ssl->in_msg;
1465 dec_msg_result = ssl->in_msg;
1466
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001467#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001468 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001469 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001470 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001471 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001472 {
Paul Bakker48916f92012-09-16 19:57:18 +00001473 dec_msglen -= ssl->transform_in->ivlen;
1474 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001475
Paul Bakker48916f92012-09-16 19:57:18 +00001476 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001477 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001478 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001479#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001480
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001481 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001482 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001483 ssl->transform_in->ivlen,
1484 dec_msg, dec_msglen,
1485 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001486 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001487 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001488 return( ret );
1489 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001490
Paul Bakkercca5b812013-08-31 17:40:26 +02001491 if( dec_msglen != olen )
1492 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001493 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001494 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001495 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001496
1497#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001498 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1499 {
1500 /*
1501 * Save IV in SSL3 and TLS1
1502 */
1503 memcpy( ssl->transform_in->iv_dec,
1504 ssl->transform_in->cipher_ctx_dec.iv,
1505 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
1509 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001510
1511 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1512 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001513#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001514 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1515 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001516#endif
Paul Bakker45829992013-01-03 14:52:21 +01001517 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001518 correct = 0;
1519 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001520
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001521#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1523 {
Paul Bakker48916f92012-09-16 19:57:18 +00001524 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001526#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1528 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001529 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001530#endif
Paul Bakker45829992013-01-03 14:52:21 +01001531 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 }
1533 }
1534 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001535#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001536#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1537 defined(POLARSSL_SSL_PROTO_TLS1_2)
1538 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 {
1540 /*
Paul Bakker45829992013-01-03 14:52:21 +01001541 * TLSv1+: always check the padding up to the first failure
1542 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001544 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001545 size_t padding_idx = ssl->in_msglen - padlen - 1;
1546
Paul Bakker956c9e02013-12-19 14:42:28 +01001547 /*
1548 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001549 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001550 *
Paul Bakker61885c72014-04-25 12:59:03 +02001551 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1552 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001553 *
1554 * In both cases we reset padding_idx to a safe value (0) to
1555 * prevent out-of-buffer reads.
1556 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001557 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001558 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1559 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001560
1561 padding_idx *= correct;
1562
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001563 for( i = 1; i <= 256; i++ )
1564 {
1565 real_count &= ( i <= padlen );
1566 pad_count += real_count *
1567 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1568 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001569
1570 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001571
Paul Bakkerd66f0702013-01-31 16:57:45 +01001572#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001573 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001574 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001575#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001576 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001578 else
1579#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1580 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001581 {
1582 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001583 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001584 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001586 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001587#endif /* POLARSSL_CIPHER_MODE_CBC &&
1588 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001589 {
1590 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001591 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001593
1594 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1595 ssl->in_msg, ssl->in_msglen );
1596
1597 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001598 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001600#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1601 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1602 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001603 if( mode != POLARSSL_MODE_GCM &&
1604 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001605 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001606 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1607
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001608 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001610 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1611 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001613 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001615#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001616 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1617 {
1618 ssl_mac( &ssl->transform_in->md_ctx_dec,
1619 ssl->transform_in->mac_dec,
1620 ssl->in_msg, ssl->in_msglen,
1621 ssl->in_ctr, ssl->in_msgtype );
1622 }
1623 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001624#endif /* POLARSSL_SSL_PROTO_SSL3 */
1625#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001626 defined(POLARSSL_SSL_PROTO_TLS1_2)
1627 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1628 {
1629 /*
1630 * Process MAC and always update for padlen afterwards to make
1631 * total time independent of padlen
1632 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001633 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001634 *
1635 * Known timing attacks:
1636 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1637 *
1638 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1639 * correctly. (We round down instead of up, so -56 is the correct
1640 * value for our calculations instead of -55)
1641 */
1642 size_t j, extra_run = 0;
1643 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1644 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001645
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001646 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001647
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001648 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1649 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1650 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001651 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1652 ssl->in_msglen );
1653 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1654 ssl->in_msg + ssl->in_msglen );
1655 for( j = 0; j < extra_run; j++ )
1656 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001657
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001658 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1659 }
1660 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001661#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001662 POLARSSL_SSL_PROTO_TLS1_2 */
1663 {
1664 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001665 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1669 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1670 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001672 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001673 ssl->transform_in->maclen ) != 0 )
1674 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001675#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001677#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678 correct = 0;
1679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001681 /*
1682 * Finally check the correct flag
1683 */
1684 if( correct == 0 )
1685 return( POLARSSL_ERR_SSL_INVALID_MAC );
1686 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001687#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001688
1689 if( ssl->in_msglen == 0 )
1690 {
1691 ssl->nb_zero++;
1692
1693 /*
1694 * Three or more empty messages may be a DoS attack
1695 * (excessive CPU consumption).
1696 */
1697 if( ssl->nb_zero > 3 )
1698 {
1699 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1700 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001701 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 }
1703 }
1704 else
1705 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001706
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001707 /* Input counter not used with DTLS right now,
1708 * but it doesn't hurt to have this part ready */
1709 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1710 if( ++ssl->in_ctr[i - 1] != 0 )
1711 break;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001712
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001713 /* The loop goes to its end iff the counter is wrapping */
1714 if( i == ssl_ep_len( ssl ) )
1715 {
1716 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1717 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001718 }
1719
Paul Bakker5121ce52009-01-03 21:22:43 +00001720 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1721
1722 return( 0 );
1723}
1724
Paul Bakker2770fbd2012-07-03 13:30:23 +00001725#if defined(POLARSSL_ZLIB_SUPPORT)
1726/*
1727 * Compression/decompression functions
1728 */
1729static int ssl_compress_buf( ssl_context *ssl )
1730{
1731 int ret;
1732 unsigned char *msg_post = ssl->out_msg;
1733 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001734 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001735
1736 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1737
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001738 if( len_pre == 0 )
1739 return( 0 );
1740
Paul Bakker2770fbd2012-07-03 13:30:23 +00001741 memcpy( msg_pre, ssl->out_msg, len_pre );
1742
1743 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1744 ssl->out_msglen ) );
1745
1746 SSL_DEBUG_BUF( 4, "before compression: output payload",
1747 ssl->out_msg, ssl->out_msglen );
1748
Paul Bakker48916f92012-09-16 19:57:18 +00001749 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1750 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1751 ssl->transform_out->ctx_deflate.next_out = msg_post;
1752 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001753
Paul Bakker48916f92012-09-16 19:57:18 +00001754 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001755 if( ret != Z_OK )
1756 {
1757 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1758 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1759 }
1760
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001761 ssl->out_msglen = SSL_BUFFER_LEN -
1762 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001763
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1765 ssl->out_msglen ) );
1766
1767 SSL_DEBUG_BUF( 4, "after compression: output payload",
1768 ssl->out_msg, ssl->out_msglen );
1769
1770 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1771
1772 return( 0 );
1773}
1774
1775static int ssl_decompress_buf( ssl_context *ssl )
1776{
1777 int ret;
1778 unsigned char *msg_post = ssl->in_msg;
1779 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001780 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001781
1782 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1783
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001784 if( len_pre == 0 )
1785 return( 0 );
1786
Paul Bakker2770fbd2012-07-03 13:30:23 +00001787 memcpy( msg_pre, ssl->in_msg, len_pre );
1788
1789 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1790 ssl->in_msglen ) );
1791
1792 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1793 ssl->in_msg, ssl->in_msglen );
1794
Paul Bakker48916f92012-09-16 19:57:18 +00001795 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1796 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1797 ssl->transform_in->ctx_inflate.next_out = msg_post;
1798 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799
Paul Bakker48916f92012-09-16 19:57:18 +00001800 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001801 if( ret != Z_OK )
1802 {
1803 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1804 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1805 }
1806
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001807 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1808 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001809
Paul Bakker2770fbd2012-07-03 13:30:23 +00001810 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1811 ssl->in_msglen ) );
1812
1813 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1814 ssl->in_msg, ssl->in_msglen );
1815
1816 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1817
1818 return( 0 );
1819}
1820#endif /* POLARSSL_ZLIB_SUPPORT */
1821
Paul Bakker5121ce52009-01-03 21:22:43 +00001822/*
1823 * Fill the input message buffer
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001824 *
1825 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1826 * available (from this read and/or a previous one). Otherwise, an error code
1827 * is returned (possibly EOF or WANT_READ).
1828 *
1829 * Set ssl->in_left to 0 before calling to start a new record. Apart from
1830 * this, ssl->in_left is an internal variable and should never be read.
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 */
Paul Bakker23986e52011-04-24 08:57:21 +00001832int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001833{
Paul Bakker23986e52011-04-24 08:57:21 +00001834 int ret;
1835 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
1837 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1838
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001839 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001840 {
1841 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1842 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1843 }
1844
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001845#if defined(POLARSSL_SSL_PROTO_DTLS)
1846 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1849 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001850
1851 /*
1852 * With UDP, we must always read a full datagram.
1853 * Just remember how much we read and avoid reading again if we
1854 * already have enough data.
1855 */
1856 if( nb_want <= ssl->in_left)
1857 return( 0 );
1858
1859 /*
1860 * A record can't be split accross datagrams. If we need to read but
1861 * are not at the beginning of a new record, the caller did something
1862 * wrong.
1863 */
1864 if( ssl->in_left != 0 )
1865 {
1866 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1867 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1868 }
1869
1870 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
1871 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr, len );
1872
Paul Bakker5121ce52009-01-03 21:22:43 +00001873 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1874
Paul Bakker831a7552011-05-18 13:32:51 +00001875 if( ret == 0 )
1876 return( POLARSSL_ERR_SSL_CONN_EOF );
1877
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 if( ret < 0 )
1879 return( ret );
1880
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001881 ssl->in_left = ret;
1882 }
1883 else
1884#endif
1885 {
1886 while( ssl->in_left < nb_want )
1887 {
1888 len = nb_want - ssl->in_left;
1889 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1890
1891 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1892 ssl->in_left, nb_want ) );
1893 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1894
1895 if( ret == 0 )
1896 return( POLARSSL_ERR_SSL_CONN_EOF );
1897
1898 if( ret < 0 )
1899 return( ret );
1900
1901 ssl->in_left += ret;
1902 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 }
1904
1905 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1906
1907 return( 0 );
1908}
1909
1910/*
1911 * Flush any data not yet written
1912 */
1913int ssl_flush_output( ssl_context *ssl )
1914{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001915 int ret;
1916 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
1918 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1919
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001920 /* Avoid incrementing counter if data is flushed */
1921 if( ssl->out_left == 0 )
1922 {
1923 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1924 return( 0 );
1925 }
1926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 while( ssl->out_left > 0 )
1928 {
1929 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001930 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001931
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001932 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
1933 ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001935
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1937
1938 if( ret <= 0 )
1939 return( ret );
1940
1941 ssl->out_left -= ret;
1942 }
1943
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001944 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001945 if( ++ssl->out_ctr[i - 1] != 0 )
1946 break;
1947
1948 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001949 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001950 {
1951 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1952 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1953 }
1954
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1956
1957 return( 0 );
1958}
1959
1960/*
1961 * Record layer functions
1962 */
1963int ssl_write_record( ssl_context *ssl )
1964{
Paul Bakker05ef8352012-05-08 09:17:57 +00001965 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001966 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
1968 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1969
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1971 {
1972 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1973 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1974 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1975
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001976 /*
1977 * DTLS has additional fields in the Handshake layer,
1978 * between the length field and the actual payload:
1979 * uint16 message_seq;
1980 * uint24 fragment_offset;
1981 * uint24 fragment_length;
1982 */
1983#if defined(POLARSSL_SSL_PROTO_DTLS)
1984 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1985 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001986 /* Make room for the additional DTLS fields */
1987 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001988 ssl->out_msglen += 8;
1989 len += 8;
1990
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001991 /* Write message_seq and update it */
1992 ssl->out_msg[4] = ( ssl->handshake->msg_seq >> 8 ) & 0xFF;
1993 ssl->out_msg[5] = ( ssl->handshake->msg_seq ) & 0xFF;
1994 ++( ssl->handshake->msg_seq );
1995
1996 /* We don't fragment, so frag_offset = 0 and frag_len = len */
1997 memset( ssl->out_msg + 6, 0x00, 3 );
1998 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001999 }
2000#endif /* POLARSSL_SSL_PROTO_DTLS */
2001
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002002 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2003 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 }
2005
Paul Bakker2770fbd2012-07-03 13:30:23 +00002006#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002007 if( ssl->transform_out != NULL &&
2008 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002009 {
2010 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2011 {
2012 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2013 return( ret );
2014 }
2015
2016 len = ssl->out_msglen;
2017 }
2018#endif /*POLARSSL_ZLIB_SUPPORT */
2019
Paul Bakker05ef8352012-05-08 09:17:57 +00002020#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002021 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002023 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
Paul Bakker05ef8352012-05-08 09:17:57 +00002025 ret = ssl_hw_record_write( ssl );
2026 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2027 {
2028 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002029 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002030 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002031
2032 if( ret == 0 )
2033 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002034 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002035#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002036 if( !done )
2037 {
2038 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002039 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2040 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002041
2042 ssl->out_len[0] = (unsigned char)( len >> 8 );
2043 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002044
Paul Bakker48916f92012-09-16 19:57:18 +00002045 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002046 {
2047 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2048 {
2049 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2050 return( ret );
2051 }
2052
2053 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002054 ssl->out_len[0] = (unsigned char)( len >> 8 );
2055 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002056 }
2057
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002058 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002059
2060 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2061 "version = [%d:%d], msglen = %d",
2062 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002063 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002064
2065 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002066 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 }
2068
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2070 {
2071 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2072 return( ret );
2073 }
2074
2075 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2076
2077 return( 0 );
2078}
2079
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002080static int ssl_prepare_handshake_record( ssl_context *ssl )
2081{
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002082 ssl->in_hslen = ssl->transport == SSL_TRANSPORT_DATAGRAM ? 12 : 4;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002083 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2084
2085 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2086 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002087 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002088
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002089 /* We don't handle handshake messages larger than one record (for now) */
2090 if( ssl->in_msg[1] != 0 ||
2091 ssl->in_msglen < ssl->in_hslen )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002092 {
2093 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
2094 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2095 }
2096
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002097 if( ssl->state != SSL_HANDSHAKE_OVER )
2098 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2099
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002100 /*
2101 * For DTLS, we move data so that is looks like
2102 * TLS handshake format to other functions.
2103 */
2104#if defined(POLARSSL_SSL_PROTO_DTLS)
2105 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2106 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002107 // TODO: DTLS: check message_seq
2108
2109 /* For now we don't support fragmentation, so make sure
2110 * fragment_offset == 0 and fragment_length == length */
2111 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
2112 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
2113 {
2114 SSL_DEBUG_MSG( 1, ( "handshake fragmentation not supported" ) );
2115 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2116 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002117
2118 memmove( ssl->in_msg + 4, ssl->in_msg + 12, ssl->in_hslen - 12 );
2119 ssl->in_hslen -= 8;
2120 }
2121#endif /* POLARSSL_SSL_PROTO_DTLS */
2122
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002123 return( 0 );
2124}
2125
Paul Bakker5121ce52009-01-03 21:22:43 +00002126int ssl_read_record( ssl_context *ssl )
2127{
Paul Bakker05ef8352012-05-08 09:17:57 +00002128 int ret, done = 0;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002129 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
2131 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2132
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002133 /*
2134 * With DTLS, we cheated on in_hslen to make the handshake message look
2135 * like TLS format, restore the truth now
2136 */
2137#if defined(POLARSSL_SSL_PROTO_DTLS)
2138 if( ssl->in_hslen != 0 && ssl->transport == SSL_TRANSPORT_DATAGRAM )
2139 ssl->in_hslen += 8;
2140#endif
2141
2142 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 {
2144 /*
2145 * Get next Handshake message in the current record
2146 */
2147 ssl->in_msglen -= ssl->in_hslen;
2148
Paul Bakker8934a982011-08-05 11:11:53 +00002149 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002150 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002151
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002152 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
2153 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002154
2155 return( 0 );
2156 }
2157
2158 ssl->in_hslen = 0;
2159
2160 /*
2161 * Read the record header and validate it
2162 */
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002163 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 {
2165 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2166 return( ret );
2167 }
2168
2169 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002170 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
2172 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2173 "version = [%d:%d], msglen = %d",
2174 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002175 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002176
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002177 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
2178
2179 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 {
2181 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002182 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 }
2184
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002185 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 {
2187 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002188 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 }
2190
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002191 /* Sanity check (outer boundaries) */
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002192 if( ssl->in_msglen < 1 ||
2193 ssl->in_msglen > SSL_BUFFER_LEN - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002194 {
2195 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2196 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2197 }
2198
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002200 * Make sure the message length is acceptable for the current transform
2201 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 */
Paul Bakker48916f92012-09-16 19:57:18 +00002203 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002205 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 {
2207 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002208 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 }
2210 }
2211 else
2212 {
Paul Bakker48916f92012-09-16 19:57:18 +00002213 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 {
2215 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002216 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 }
2218
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002219#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002221 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 {
2223 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002224 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002226#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002227
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002228#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2229 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 /*
2231 * TLS encrypted messages can have up to 256 bytes of padding
2232 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002233 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002234 ssl->in_msglen > ssl->transform_in->minlen +
2235 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 {
2237 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002238 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002240#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 }
2242
2243 /*
2244 * Read and optionally decrypt the message contents
2245 */
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002246 if( ( ret = ssl_fetch_input( ssl,
2247 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 {
2249 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2250 return( ret );
2251 }
2252
2253 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002254 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
Paul Bakker05ef8352012-05-08 09:17:57 +00002256#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002257 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002258 {
2259 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2260
2261 ret = ssl_hw_record_read( ssl );
2262 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2263 {
2264 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002265 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002266 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002267
2268 if( ret == 0 )
2269 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002270 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002271#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002272 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 {
2274 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2275 {
Paul Bakker40865c82013-01-31 17:13:13 +01002276#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2277 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2278 {
2279 ssl_send_alert_message( ssl,
2280 SSL_ALERT_LEVEL_FATAL,
2281 SSL_ALERT_MSG_BAD_RECORD_MAC );
2282 }
2283#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2285 return( ret );
2286 }
2287
2288 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2289 ssl->in_msg, ssl->in_msglen );
2290
2291 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2292 {
2293 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002294 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 }
2296 }
2297
Paul Bakker2770fbd2012-07-03 13:30:23 +00002298#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002299 if( ssl->transform_in != NULL &&
2300 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002301 {
2302 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2303 {
2304 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2305 return( ret );
2306 }
2307
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002308 // TODO: what's the purpose of these lines? is in_len used?
2309 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2310 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002311 }
2312#endif /* POLARSSL_ZLIB_SUPPORT */
2313
Paul Bakker0a925182012-04-16 06:46:41 +00002314 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2315 ssl->in_msgtype != SSL_MSG_ALERT &&
2316 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2317 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2318 {
2319 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2320
Paul Bakker48916f92012-09-16 19:57:18 +00002321 if( ( ret = ssl_send_alert_message( ssl,
2322 SSL_ALERT_LEVEL_FATAL,
2323 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002324 {
2325 return( ret );
2326 }
2327
2328 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2329 }
2330
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2332 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002333 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
2334 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002335 }
2336
2337 if( ssl->in_msgtype == SSL_MSG_ALERT )
2338 {
2339 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2340 ssl->in_msg[0], ssl->in_msg[1] ) );
2341
2342 /*
2343 * Ignore non-fatal alerts, except close_notify
2344 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002345 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002347 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2348 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002349 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 }
2351
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002352 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2353 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 {
2355 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002356 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 }
2358 }
2359
2360 ssl->in_left = 0;
2361
2362 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2363
2364 return( 0 );
2365}
2366
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002367int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2368{
2369 int ret;
2370
2371 if( ( ret = ssl_send_alert_message( ssl,
2372 SSL_ALERT_LEVEL_FATAL,
2373 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2374 {
2375 return( ret );
2376 }
2377
2378 return( 0 );
2379}
2380
Paul Bakker0a925182012-04-16 06:46:41 +00002381int ssl_send_alert_message( ssl_context *ssl,
2382 unsigned char level,
2383 unsigned char message )
2384{
2385 int ret;
2386
2387 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2388
2389 ssl->out_msgtype = SSL_MSG_ALERT;
2390 ssl->out_msglen = 2;
2391 ssl->out_msg[0] = level;
2392 ssl->out_msg[1] = message;
2393
2394 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2395 {
2396 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2397 return( ret );
2398 }
2399
2400 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2401
2402 return( 0 );
2403}
2404
Paul Bakker5121ce52009-01-03 21:22:43 +00002405/*
2406 * Handshake functions
2407 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002408#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2409 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2410 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2411 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2412 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2413 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2414 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002415int ssl_write_certificate( ssl_context *ssl )
2416{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002417 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002418
2419 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2420
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002421 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002422 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2423 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002424 {
2425 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2426 ssl->state++;
2427 return( 0 );
2428 }
2429
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002430 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2431 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002432}
2433
2434int ssl_parse_certificate( ssl_context *ssl )
2435{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002436 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2437
2438 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2439
2440 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002441 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2442 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002443 {
2444 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2445 ssl->state++;
2446 return( 0 );
2447 }
2448
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002449 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2450 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002451}
2452#else
2453int ssl_write_certificate( ssl_context *ssl )
2454{
2455 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2456 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002457 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002458 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2459
2460 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2461
2462 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002463 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2464 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002465 {
2466 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2467 ssl->state++;
2468 return( 0 );
2469 }
2470
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 if( ssl->endpoint == SSL_IS_CLIENT )
2472 {
2473 if( ssl->client_auth == 0 )
2474 {
2475 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2476 ssl->state++;
2477 return( 0 );
2478 }
2479
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002480#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 /*
2482 * If using SSLv3 and got no cert, send an Alert message
2483 * (otherwise an empty Certificate message will be sent).
2484 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002485 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2487 {
2488 ssl->out_msglen = 2;
2489 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002490 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2491 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
2493 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2494 goto write_msg;
2495 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002496#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 }
2498 else /* SSL_IS_SERVER */
2499 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002500 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 {
2502 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002503 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 }
2505 }
2506
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002507 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
2509 /*
2510 * 0 . 0 handshake type
2511 * 1 . 3 handshake length
2512 * 4 . 6 length of all certs
2513 * 7 . 9 length of cert. 1
2514 * 10 . n-1 peer certificate
2515 * n . n+2 length of cert. 2
2516 * n+3 . ... upper level cert, etc.
2517 */
2518 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002519 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Paul Bakker29087132010-03-21 21:03:34 +00002521 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 {
2523 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002524 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 {
2526 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2527 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002528 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 }
2530
2531 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2532 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2533 ssl->out_msg[i + 2] = (unsigned char)( n );
2534
2535 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2536 i += n; crt = crt->next;
2537 }
2538
2539 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2540 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2541 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2542
2543 ssl->out_msglen = i;
2544 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2545 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2546
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002547#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002548write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002549#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
2551 ssl->state++;
2552
2553 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2554 {
2555 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2556 return( ret );
2557 }
2558
2559 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2560
Paul Bakkered27a042013-04-18 22:46:23 +02002561 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002562}
2563
2564int ssl_parse_certificate( ssl_context *ssl )
2565{
Paul Bakkered27a042013-04-18 22:46:23 +02002566 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002567 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002568 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
2570 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2571
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002572 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002573 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2574 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002575 {
2576 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2577 ssl->state++;
2578 return( 0 );
2579 }
2580
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002582 ( ssl->authmode == SSL_VERIFY_NONE ||
2583 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002585 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2587 ssl->state++;
2588 return( 0 );
2589 }
2590
2591 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2592 {
2593 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2594 return( ret );
2595 }
2596
2597 ssl->state++;
2598
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002599#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 /*
2601 * Check if the client sent an empty certificate
2602 */
2603 if( ssl->endpoint == SSL_IS_SERVER &&
2604 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2605 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002606 if( ssl->in_msglen == 2 &&
2607 ssl->in_msgtype == SSL_MSG_ALERT &&
2608 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2609 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 {
2611 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2612
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002613 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2615 return( 0 );
2616 else
Paul Bakker40e46942009-01-03 21:51:57 +00002617 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002620#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002622#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2623 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 if( ssl->endpoint == SSL_IS_SERVER &&
2625 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2626 {
2627 if( ssl->in_hslen == 7 &&
2628 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2629 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2630 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2631 {
2632 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2633
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002634 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002636 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 else
2638 return( 0 );
2639 }
2640 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002641#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2642 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
2644 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2645 {
2646 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002647 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648 }
2649
2650 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2651 {
2652 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002653 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 }
2655
2656 /*
2657 * Same message structure as in ssl_write_certificate()
2658 */
2659 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2660
2661 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2662 {
2663 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002664 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 }
2666
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002667 /* In case we tried to reuse a session but it failed */
2668 if( ssl->session_negotiate->peer_cert != NULL )
2669 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002670 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002671 polarssl_free( ssl->session_negotiate->peer_cert );
2672 }
2673
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002674 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2675 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 {
2677 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002678 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002679 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 }
2681
Paul Bakkerb6b09562013-09-18 14:17:41 +02002682 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
2684 i = 7;
2685
2686 while( i < ssl->in_hslen )
2687 {
2688 if( ssl->in_msg[i] != 0 )
2689 {
2690 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002691 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 }
2693
2694 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2695 | (unsigned int) ssl->in_msg[i + 2];
2696 i += 3;
2697
2698 if( n < 128 || i + n > ssl->in_hslen )
2699 {
2700 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002701 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 }
2703
Paul Bakkerddf26b42013-09-18 13:46:23 +02002704 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2705 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 if( ret != 0 )
2707 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002708 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 return( ret );
2710 }
2711
2712 i += n;
2713 }
2714
Paul Bakker48916f92012-09-16 19:57:18 +00002715 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002717 /*
2718 * On client, make sure the server cert doesn't change during renego to
2719 * avoid "triple handshake" attack: https://secure-resumption.com/
2720 */
2721 if( ssl->endpoint == SSL_IS_CLIENT &&
2722 ssl->renegotiation == SSL_RENEGOTIATION )
2723 {
2724 if( ssl->session->peer_cert == NULL )
2725 {
2726 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2727 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2728 }
2729
2730 if( ssl->session->peer_cert->raw.len !=
2731 ssl->session_negotiate->peer_cert->raw.len ||
2732 memcmp( ssl->session->peer_cert->raw.p,
2733 ssl->session_negotiate->peer_cert->raw.p,
2734 ssl->session->peer_cert->raw.len ) != 0 )
2735 {
2736 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2737 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2738 }
2739 }
2740
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 if( ssl->authmode != SSL_VERIFY_NONE )
2742 {
2743 if( ssl->ca_chain == NULL )
2744 {
2745 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002746 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 }
2748
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002749 /*
2750 * Main check: verify certificate
2751 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002752 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2753 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2754 &ssl->session_negotiate->verify_result,
2755 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
2757 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002758 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002760 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002761
2762 /*
2763 * Secondary checks: always done, but change 'ret' only if it was 0
2764 */
2765
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002766#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002767 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002768 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002769
2770 /* If certificate uses an EC key, make sure the curve is OK */
2771 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2772 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2773 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002774 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002775 if( ret == 0 )
2776 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002777 }
2778 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002779#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002781 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2782 ciphersuite_info,
2783 ! ssl->endpoint ) != 0 )
2784 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002785 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002786 if( ret == 0 )
2787 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2788 }
2789
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2791 ret = 0;
2792 }
2793
2794 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2795
2796 return( ret );
2797}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002798#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2799 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2800 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2801 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2802 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2803 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2804 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
2806int ssl_write_change_cipher_spec( ssl_context *ssl )
2807{
2808 int ret;
2809
2810 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2811
2812 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2813 ssl->out_msglen = 1;
2814 ssl->out_msg[0] = 1;
2815
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 ssl->state++;
2817
2818 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2819 {
2820 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2821 return( ret );
2822 }
2823
2824 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2825
2826 return( 0 );
2827}
2828
2829int ssl_parse_change_cipher_spec( ssl_context *ssl )
2830{
2831 int ret;
2832
2833 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2834
Paul Bakker5121ce52009-01-03 21:22:43 +00002835 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2836 {
2837 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2838 return( ret );
2839 }
2840
2841 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2842 {
2843 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002844 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 }
2846
2847 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2848 {
2849 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002850 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851 }
2852
2853 ssl->state++;
2854
2855 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2856
2857 return( 0 );
2858}
2859
Paul Bakker41c83d32013-03-20 14:39:14 +01002860void ssl_optimize_checksum( ssl_context *ssl,
2861 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002862{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002863 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002864
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002865#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2866 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002867 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002868 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002869 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002870#endif
2871#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2872#if defined(POLARSSL_SHA512_C)
2873 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2874 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2875 else
2876#endif
2877#if defined(POLARSSL_SHA256_C)
2878 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002879 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002880 else
2881#endif
2882#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002883 {
2884 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002885 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002886 }
Paul Bakker380da532012-04-18 16:10:25 +00002887}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002888
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002889static void ssl_update_checksum_start( ssl_context *ssl,
2890 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002891{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002892#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2893 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002894 md5_update( &ssl->handshake->fin_md5 , buf, len );
2895 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002896#endif
2897#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2898#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002899 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002900#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002901#if defined(POLARSSL_SHA512_C)
2902 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002903#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002904#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002905}
2906
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002907#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2908 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002909static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2910 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002911{
Paul Bakker48916f92012-09-16 19:57:18 +00002912 md5_update( &ssl->handshake->fin_md5 , buf, len );
2913 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002914}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002915#endif
Paul Bakker380da532012-04-18 16:10:25 +00002916
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002917#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2918#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002919static void ssl_update_checksum_sha256( ssl_context *ssl,
2920 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002921{
Paul Bakker9e36f042013-06-30 14:34:05 +02002922 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002923}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002924#endif
Paul Bakker380da532012-04-18 16:10:25 +00002925
Paul Bakker9e36f042013-06-30 14:34:05 +02002926#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002927static void ssl_update_checksum_sha384( ssl_context *ssl,
2928 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002929{
Paul Bakker9e36f042013-06-30 14:34:05 +02002930 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002931}
Paul Bakker769075d2012-11-24 11:26:46 +01002932#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002933#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002934
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002935#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936static void ssl_calc_finished_ssl(
2937 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002938{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002939 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940 md5_context md5;
2941 sha1_context sha1;
2942
Paul Bakker5121ce52009-01-03 21:22:43 +00002943 unsigned char padbuf[48];
2944 unsigned char md5sum[16];
2945 unsigned char sha1sum[20];
2946
Paul Bakker48916f92012-09-16 19:57:18 +00002947 ssl_session *session = ssl->session_negotiate;
2948 if( !session )
2949 session = ssl->session;
2950
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2952
Paul Bakker48916f92012-09-16 19:57:18 +00002953 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2954 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
2956 /*
2957 * SSLv3:
2958 * hash =
2959 * MD5( master + pad2 +
2960 * MD5( handshake + sender + master + pad1 ) )
2961 * + SHA1( master + pad2 +
2962 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002963 */
2964
Paul Bakker90995b52013-06-24 19:20:35 +02002965#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002967 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002968#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Paul Bakker90995b52013-06-24 19:20:35 +02002970#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002971 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002973#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Paul Bakker3c2122f2013-06-24 19:03:14 +02002975 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2976 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002977
Paul Bakker1ef83d62012-04-11 12:09:53 +00002978 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
Paul Bakker3c2122f2013-06-24 19:03:14 +02002980 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002981 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982 md5_update( &md5, padbuf, 48 );
2983 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Paul Bakker3c2122f2013-06-24 19:03:14 +02002985 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002986 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002987 sha1_update( &sha1, padbuf, 40 );
2988 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002989
Paul Bakker1ef83d62012-04-11 12:09:53 +00002990 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002991
Paul Bakker1ef83d62012-04-11 12:09:53 +00002992 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002993 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994 md5_update( &md5, padbuf, 48 );
2995 md5_update( &md5, md5sum, 16 );
2996 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Paul Bakker1ef83d62012-04-11 12:09:53 +00002998 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002999 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003000 sha1_update( &sha1, padbuf , 40 );
3001 sha1_update( &sha1, sha1sum, 20 );
3002 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
Paul Bakker1ef83d62012-04-11 12:09:53 +00003004 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
Paul Bakker5b4af392014-06-26 12:09:34 +02003006 md5_free( &md5 );
3007 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Paul Bakker34617722014-06-13 17:20:13 +02003009 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3010 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3011 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
3013 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3014}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003015#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003017#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003018static void ssl_calc_finished_tls(
3019 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003020{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003022 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003023 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003025 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003026
Paul Bakker48916f92012-09-16 19:57:18 +00003027 ssl_session *session = ssl->session_negotiate;
3028 if( !session )
3029 session = ssl->session;
3030
Paul Bakker1ef83d62012-04-11 12:09:53 +00003031 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Paul Bakker48916f92012-09-16 19:57:18 +00003033 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3034 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Paul Bakker1ef83d62012-04-11 12:09:53 +00003036 /*
3037 * TLSv1:
3038 * hash = PRF( master, finished_label,
3039 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3040 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003041
Paul Bakker90995b52013-06-24 19:20:35 +02003042#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003043 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3044 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003045#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003046
Paul Bakker90995b52013-06-24 19:20:35 +02003047#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3049 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003050#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003051
3052 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003053 ? "client finished"
3054 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003055
3056 md5_finish( &md5, padbuf );
3057 sha1_finish( &sha1, padbuf + 16 );
3058
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003059 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003060 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003061
3062 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3063
Paul Bakker5b4af392014-06-26 12:09:34 +02003064 md5_free( &md5 );
3065 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003066
Paul Bakker34617722014-06-13 17:20:13 +02003067 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068
3069 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3070}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003071#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003073#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3074#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003075static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076 ssl_context *ssl, unsigned char *buf, int from )
3077{
3078 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003079 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003080 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081 unsigned char padbuf[32];
3082
Paul Bakker48916f92012-09-16 19:57:18 +00003083 ssl_session *session = ssl->session_negotiate;
3084 if( !session )
3085 session = ssl->session;
3086
Paul Bakker380da532012-04-18 16:10:25 +00003087 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003088
Paul Bakker9e36f042013-06-30 14:34:05 +02003089 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003090
3091 /*
3092 * TLSv1.2:
3093 * hash = PRF( master, finished_label,
3094 * Hash( handshake ) )[0.11]
3095 */
3096
Paul Bakker9e36f042013-06-30 14:34:05 +02003097#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003099 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003100#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003101
3102 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003103 ? "client finished"
3104 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003105
Paul Bakker9e36f042013-06-30 14:34:05 +02003106 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003107
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003108 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003109 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003110
3111 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3112
Paul Bakker5b4af392014-06-26 12:09:34 +02003113 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003114
Paul Bakker34617722014-06-13 17:20:13 +02003115 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003116
3117 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3118}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003119#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003120
Paul Bakker9e36f042013-06-30 14:34:05 +02003121#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003122static void ssl_calc_finished_tls_sha384(
3123 ssl_context *ssl, unsigned char *buf, int from )
3124{
3125 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003126 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003127 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003128 unsigned char padbuf[48];
3129
Paul Bakker48916f92012-09-16 19:57:18 +00003130 ssl_session *session = ssl->session_negotiate;
3131 if( !session )
3132 session = ssl->session;
3133
Paul Bakker380da532012-04-18 16:10:25 +00003134 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003135
Paul Bakker9e36f042013-06-30 14:34:05 +02003136 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003137
3138 /*
3139 * TLSv1.2:
3140 * hash = PRF( master, finished_label,
3141 * Hash( handshake ) )[0.11]
3142 */
3143
Paul Bakker9e36f042013-06-30 14:34:05 +02003144#if !defined(POLARSSL_SHA512_ALT)
3145 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3146 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003147#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003148
3149 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003150 ? "client finished"
3151 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003152
Paul Bakker9e36f042013-06-30 14:34:05 +02003153 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003154
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003155 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003156 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003157
3158 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3159
Paul Bakker5b4af392014-06-26 12:09:34 +02003160 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003161
Paul Bakker34617722014-06-13 17:20:13 +02003162 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003163
3164 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3165}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003166#endif /* POLARSSL_SHA512_C */
3167#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003168
Paul Bakker48916f92012-09-16 19:57:18 +00003169void ssl_handshake_wrapup( ssl_context *ssl )
3170{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003171 int resume = ssl->handshake->resume;
3172
Paul Bakker48916f92012-09-16 19:57:18 +00003173 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3174
3175 /*
3176 * Free our handshake params
3177 */
3178 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003179 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003180 ssl->handshake = NULL;
3181
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003182 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003183 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003184 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003185 ssl->renego_records_seen = 0;
3186 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003187
Paul Bakker48916f92012-09-16 19:57:18 +00003188 /*
3189 * Switch in our now active transform context
3190 */
3191 if( ssl->transform )
3192 {
3193 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003194 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003195 }
3196 ssl->transform = ssl->transform_negotiate;
3197 ssl->transform_negotiate = NULL;
3198
Paul Bakker0a597072012-09-25 21:55:46 +00003199 if( ssl->session )
3200 {
3201 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003202 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003203 }
3204 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003205 ssl->session_negotiate = NULL;
3206
Paul Bakker0a597072012-09-25 21:55:46 +00003207 /*
3208 * Add cache entry
3209 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003210 if( ssl->f_set_cache != NULL &&
3211 ssl->session->length != 0 &&
3212 resume == 0 )
3213 {
Paul Bakker0a597072012-09-25 21:55:46 +00003214 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3215 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003216 }
Paul Bakker0a597072012-09-25 21:55:46 +00003217
Paul Bakker48916f92012-09-16 19:57:18 +00003218 ssl->state++;
3219
3220 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3221}
3222
Paul Bakker1ef83d62012-04-11 12:09:53 +00003223int ssl_write_finished( ssl_context *ssl )
3224{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003225 int ret, hash_len, i;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003226
3227 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3228
Paul Bakker92be97b2013-01-02 17:30:03 +01003229 /*
3230 * Set the out_msg pointer to the correct location based on IV length
3231 */
3232 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3233 {
3234 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3235 ssl->transform_negotiate->fixed_ivlen;
3236 }
3237 else
3238 ssl->out_msg = ssl->out_iv;
3239
Paul Bakker48916f92012-09-16 19:57:18 +00003240 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003241
3242 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003243 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3244
Paul Bakker48916f92012-09-16 19:57:18 +00003245 ssl->verify_data_len = hash_len;
3246 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3247
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 ssl->out_msglen = 4 + hash_len;
3249 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3250 ssl->out_msg[0] = SSL_HS_FINISHED;
3251
3252 /*
3253 * In case of session resuming, invert the client and server
3254 * ChangeCipherSpec messages order.
3255 */
Paul Bakker0a597072012-09-25 21:55:46 +00003256 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 {
3258 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003259 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 else
3261 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3262 }
3263 else
3264 ssl->state++;
3265
Paul Bakker48916f92012-09-16 19:57:18 +00003266 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003267 * Switch to our negotiated transform and session parameters for outbound
3268 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003269 */
3270 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3271 ssl->transform_out = ssl->transform_negotiate;
3272 ssl->session_out = ssl->session_negotiate;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003273
3274 memset( ssl->out_ctr + ssl_ep_len( ssl ), 0, 8 - ssl_ep_len( ssl ) );
3275 for( i = ssl_ep_len( ssl ); i > 0; i-- )
3276 if( ++ssl->out_ctr[i - 1] != 0 )
3277 break;
3278 // TODO: abort on epoch wrap!
Paul Bakker5121ce52009-01-03 21:22:43 +00003279
Paul Bakker07eb38b2012-12-19 14:42:06 +01003280#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003281 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003282 {
3283 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3284 {
3285 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3286 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3287 }
3288 }
3289#endif
3290
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3292 {
3293 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3294 return( ret );
3295 }
3296
3297 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3298
3299 return( 0 );
3300}
3301
3302int ssl_parse_finished( ssl_context *ssl )
3303{
Paul Bakker23986e52011-04-24 08:57:21 +00003304 int ret;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003305 unsigned int hash_len, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 unsigned char buf[36];
3307
3308 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3309
Paul Bakker48916f92012-09-16 19:57:18 +00003310 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003311
Paul Bakker48916f92012-09-16 19:57:18 +00003312 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003313 * Switch to our negotiated transform and session parameters for inbound
3314 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003315 */
3316 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3317 ssl->transform_in = ssl->transform_negotiate;
3318 ssl->session_in = ssl->session_negotiate;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003319
3320 /* Input counter/epoch not used with DTLS right now,
3321 * but it doesn't hurt to have this part ready */
3322 memset( ssl->in_ctr + ssl_ep_len( ssl ), 0, 8 - ssl_ep_len( ssl ) );
3323 for( i = ssl_ep_len( ssl ); i > 0; i-- )
3324 if( ++ssl->in_ctr[i - 1] != 0 )
3325 break;
3326 // TODO: abort on epoch wrap!
Paul Bakker5121ce52009-01-03 21:22:43 +00003327
Paul Bakker92be97b2013-01-02 17:30:03 +01003328 /*
3329 * Set the in_msg pointer to the correct location based on IV length
3330 */
3331 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3332 {
3333 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3334 ssl->transform_negotiate->fixed_ivlen;
3335 }
3336 else
3337 ssl->in_msg = ssl->in_iv;
3338
Paul Bakker07eb38b2012-12-19 14:42:06 +01003339#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003340 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003341 {
3342 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3343 {
3344 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3345 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3346 }
3347 }
3348#endif
3349
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3351 {
3352 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3353 return( ret );
3354 }
3355
3356 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3357 {
3358 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003359 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 }
3361
Paul Bakker1ef83d62012-04-11 12:09:53 +00003362 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3364
3365 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3366 ssl->in_hslen != 4 + hash_len )
3367 {
3368 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003369 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 }
3371
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003372 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003373 {
3374 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003375 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 }
3377
Paul Bakker48916f92012-09-16 19:57:18 +00003378 ssl->verify_data_len = hash_len;
3379 memcpy( ssl->peer_verify_data, buf, hash_len );
3380
Paul Bakker0a597072012-09-25 21:55:46 +00003381 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003382 {
3383 if( ssl->endpoint == SSL_IS_CLIENT )
3384 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3385
3386 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003387 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 }
3389 else
3390 ssl->state++;
3391
3392 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3393
3394 return( 0 );
3395}
3396
Paul Bakker968afaa2014-07-09 11:09:24 +02003397static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003398{
3399 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3400
3401#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3402 defined(POLARSSL_SSL_PROTO_TLS1_1)
3403 md5_init( &handshake->fin_md5 );
3404 sha1_init( &handshake->fin_sha1 );
3405 md5_starts( &handshake->fin_md5 );
3406 sha1_starts( &handshake->fin_sha1 );
3407#endif
3408#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3409#if defined(POLARSSL_SHA256_C)
3410 sha256_init( &handshake->fin_sha256 );
3411 sha256_starts( &handshake->fin_sha256, 0 );
3412#endif
3413#if defined(POLARSSL_SHA512_C)
3414 sha512_init( &handshake->fin_sha512 );
3415 sha512_starts( &handshake->fin_sha512, 1 );
3416#endif
3417#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3418
3419 handshake->update_checksum = ssl_update_checksum_start;
3420 handshake->sig_alg = SSL_HASH_SHA1;
3421
3422#if defined(POLARSSL_DHM_C)
3423 dhm_init( &handshake->dhm_ctx );
3424#endif
3425#if defined(POLARSSL_ECDH_C)
3426 ecdh_init( &handshake->ecdh_ctx );
3427#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003428}
3429
3430static void ssl_transform_init( ssl_transform *transform )
3431{
3432 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003433
3434 cipher_init( &transform->cipher_ctx_enc );
3435 cipher_init( &transform->cipher_ctx_dec );
3436
3437 md_init( &transform->md_ctx_enc );
3438 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003439}
3440
3441void ssl_session_init( ssl_session *session )
3442{
3443 memset( session, 0, sizeof(ssl_session) );
3444}
3445
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003446static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003447{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003448 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003449 if( ssl->transform_negotiate )
3450 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003451 if( ssl->session_negotiate )
3452 ssl_session_free( ssl->session_negotiate );
3453 if( ssl->handshake )
3454 ssl_handshake_free( ssl->handshake );
3455
3456 /*
3457 * Either the pointers are now NULL or cleared properly and can be freed.
3458 * Now allocate missing structures.
3459 */
3460 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003461 {
3462 ssl->transform_negotiate =
3463 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3464 }
Paul Bakker48916f92012-09-16 19:57:18 +00003465
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003466 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003467 {
3468 ssl->session_negotiate =
3469 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3470 }
Paul Bakker48916f92012-09-16 19:57:18 +00003471
Paul Bakker82788fb2014-10-20 13:59:19 +02003472 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003473 {
3474 ssl->handshake = (ssl_handshake_params *)
3475 polarssl_malloc( sizeof(ssl_handshake_params) );
3476 }
Paul Bakker48916f92012-09-16 19:57:18 +00003477
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003478 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003479 if( ssl->handshake == NULL ||
3480 ssl->transform_negotiate == NULL ||
3481 ssl->session_negotiate == NULL )
3482 {
3483 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003484
3485 polarssl_free( ssl->handshake );
3486 polarssl_free( ssl->transform_negotiate );
3487 polarssl_free( ssl->session_negotiate );
3488
3489 ssl->handshake = NULL;
3490 ssl->transform_negotiate = NULL;
3491 ssl->session_negotiate = NULL;
3492
Paul Bakker48916f92012-09-16 19:57:18 +00003493 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3494 }
3495
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003496 /* Initialize structures */
3497 ssl_session_init( ssl->session_negotiate );
3498 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003499 ssl_handshake_params_init( ssl->handshake );
3500
3501#if defined(POLARSSL_X509_CRT_PARSE_C)
3502 ssl->handshake->key_cert = ssl->key_cert;
3503#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003504
Paul Bakker48916f92012-09-16 19:57:18 +00003505 return( 0 );
3506}
3507
Paul Bakker5121ce52009-01-03 21:22:43 +00003508/*
3509 * Initialize an SSL context
3510 */
3511int ssl_init( ssl_context *ssl )
3512{
Paul Bakker48916f92012-09-16 19:57:18 +00003513 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003514 int len = SSL_BUFFER_LEN;
3515
3516 memset( ssl, 0, sizeof( ssl_context ) );
3517
Paul Bakker62f2dee2012-09-28 07:31:51 +00003518 /*
3519 * Sane defaults
3520 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003521 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3522 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3523 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3524 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003525
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003526 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003527
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003528 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3529
Paul Bakker62f2dee2012-09-28 07:31:51 +00003530#if defined(POLARSSL_DHM_C)
3531 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3532 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3533 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3534 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3535 {
3536 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3537 return( ret );
3538 }
3539#endif
3540
3541 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003542 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003543 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003544 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003545 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003546
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003547 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003548 {
3549 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003550 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003551 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003552 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003553 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003554 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003555 }
3556
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003557 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
3558 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00003559
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003560 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
3561 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
3562
Paul Bakker606b4ba2013-08-14 16:52:14 +02003563#if defined(POLARSSL_SSL_SESSION_TICKETS)
3564 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3565#endif
3566
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003567#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003568 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003569#endif
3570
Paul Bakker48916f92012-09-16 19:57:18 +00003571 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3572 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003573
3574 return( 0 );
3575}
3576
3577/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003578 * Reset an initialized and used SSL context for re-use while retaining
3579 * all application-set variables, function pointers and data.
3580 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003581int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003582{
Paul Bakker48916f92012-09-16 19:57:18 +00003583 int ret;
3584
Paul Bakker7eb013f2011-10-06 12:37:39 +00003585 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003586 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3587 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3588
3589 ssl->verify_data_len = 0;
3590 memset( ssl->own_verify_data, 0, 36 );
3591 memset( ssl->peer_verify_data, 0, 36 );
3592
Paul Bakker7eb013f2011-10-06 12:37:39 +00003593 ssl->in_offt = NULL;
3594
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003595 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003596 ssl->in_msgtype = 0;
3597 ssl->in_msglen = 0;
3598 ssl->in_left = 0;
3599
3600 ssl->in_hslen = 0;
3601 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003602 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003603
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003604 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003605 ssl->out_msgtype = 0;
3606 ssl->out_msglen = 0;
3607 ssl->out_left = 0;
3608
Paul Bakker48916f92012-09-16 19:57:18 +00003609 ssl->transform_in = NULL;
3610 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003611
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003612 ssl->renego_records_seen = 0;
3613
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003614 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
3615 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003616
3617#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003618 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003619 {
3620 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003621 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003622 {
3623 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3624 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3625 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003626 }
3627#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003628
Paul Bakker48916f92012-09-16 19:57:18 +00003629 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003630 {
Paul Bakker48916f92012-09-16 19:57:18 +00003631 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003632 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003633 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003634 }
Paul Bakker48916f92012-09-16 19:57:18 +00003635
Paul Bakkerc0463502013-02-14 11:19:38 +01003636 if( ssl->session )
3637 {
3638 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003639 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003640 ssl->session = NULL;
3641 }
3642
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003643#if defined(POLARSSL_SSL_ALPN)
3644 ssl->alpn_chosen = NULL;
3645#endif
3646
Paul Bakker48916f92012-09-16 19:57:18 +00003647 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3648 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003649
3650 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003651}
3652
Paul Bakkera503a632013-08-14 13:48:06 +02003653#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003654static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3655{
3656 aes_free( &tkeys->enc );
3657 aes_free( &tkeys->dec );
3658
3659 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3660}
3661
Paul Bakker7eb013f2011-10-06 12:37:39 +00003662/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003663 * Allocate and initialize ticket keys
3664 */
3665static int ssl_ticket_keys_init( ssl_context *ssl )
3666{
3667 int ret;
3668 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003669 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003670
3671 if( ssl->ticket_keys != NULL )
3672 return( 0 );
3673
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003674 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3675 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003676 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3677
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003678 aes_init( &tkeys->enc );
3679 aes_init( &tkeys->dec );
3680
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003681 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003682 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003683 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003684 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003685 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003686 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003687
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003688 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3689 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3690 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3691 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003692 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003693 polarssl_free( tkeys );
3694 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003695 }
3696
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003697 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003698 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003699 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003700 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003701 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003702 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003703
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003704 ssl->ticket_keys = tkeys;
3705
3706 return( 0 );
3707}
Paul Bakkera503a632013-08-14 13:48:06 +02003708#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003709
3710/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 * SSL set accessors
3712 */
3713void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3714{
3715 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003716
Paul Bakker606b4ba2013-08-14 16:52:14 +02003717#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003718 if( endpoint == SSL_IS_CLIENT )
3719 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003720#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003721}
3722
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003723int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01003724{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003725#if defined(POLARSSL_SSL_PROTO_DTLS)
3726 if( transport == SSL_TRANSPORT_DATAGRAM )
3727 {
3728 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003729
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003730 ssl->out_hdr = ssl->out_buf;
3731 ssl->out_ctr = ssl->out_buf + 3;
3732 ssl->out_len = ssl->out_buf + 11;
3733 ssl->out_iv = ssl->out_buf + 13;
3734 ssl->out_msg = ssl->out_buf + 13;
3735
3736 ssl->in_hdr = ssl->in_buf;
3737 ssl->in_ctr = ssl->in_buf + 3;
3738 ssl->in_len = ssl->in_buf + 11;
3739 ssl->in_iv = ssl->in_buf + 13;
3740 ssl->in_msg = ssl->in_buf + 13;
3741
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003742 /* DTLS starts with TLS1.1 */
3743 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
3744 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003745
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003746 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
3747 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
3748
3749 return( 0 );
3750 }
3751#endif
3752
3753 if( transport == SSL_TRANSPORT_STREAM )
3754 {
3755 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003756
3757 ssl->out_ctr = ssl->out_buf;
3758 ssl->out_hdr = ssl->out_buf + 8;
3759 ssl->out_len = ssl->out_buf + 11;
3760 ssl->out_iv = ssl->out_buf + 13;
3761 ssl->out_msg = ssl->out_buf + 13;
3762
3763 ssl->in_ctr = ssl->in_buf;
3764 ssl->in_hdr = ssl->in_buf + 8;
3765 ssl->in_len = ssl->in_buf + 11;
3766 ssl->in_iv = ssl->in_buf + 13;
3767 ssl->in_msg = ssl->in_buf + 13;
3768
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003769 return( 0 );
3770 }
3771
3772 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01003773}
3774
Paul Bakker5121ce52009-01-03 21:22:43 +00003775void ssl_set_authmode( ssl_context *ssl, int authmode )
3776{
3777 ssl->authmode = authmode;
3778}
3779
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003780#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003781void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003782 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003783 void *p_vrfy )
3784{
3785 ssl->f_vrfy = f_vrfy;
3786 ssl->p_vrfy = p_vrfy;
3787}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003788#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003789
Paul Bakker5121ce52009-01-03 21:22:43 +00003790void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003791 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003792 void *p_rng )
3793{
3794 ssl->f_rng = f_rng;
3795 ssl->p_rng = p_rng;
3796}
3797
3798void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003799 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003800 void *p_dbg )
3801{
3802 ssl->f_dbg = f_dbg;
3803 ssl->p_dbg = p_dbg;
3804}
3805
3806void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003807 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003808 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003809{
3810 ssl->f_recv = f_recv;
3811 ssl->f_send = f_send;
3812 ssl->p_recv = p_recv;
3813 ssl->p_send = p_send;
3814}
3815
Paul Bakker0a597072012-09-25 21:55:46 +00003816void ssl_set_session_cache( ssl_context *ssl,
3817 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3818 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003819{
Paul Bakker0a597072012-09-25 21:55:46 +00003820 ssl->f_get_cache = f_get_cache;
3821 ssl->p_get_cache = p_get_cache;
3822 ssl->f_set_cache = f_set_cache;
3823 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003824}
3825
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003826int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003827{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003828 int ret;
3829
3830 if( ssl == NULL ||
3831 session == NULL ||
3832 ssl->session_negotiate == NULL ||
3833 ssl->endpoint != SSL_IS_CLIENT )
3834 {
3835 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3836 }
3837
3838 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3839 return( ret );
3840
Paul Bakker0a597072012-09-25 21:55:46 +00003841 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003842
3843 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003844}
3845
Paul Bakkerb68cad62012-08-23 08:34:18 +00003846void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003847{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003848 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3849 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3850 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3851 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3852}
3853
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003854void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3855 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003856 int major, int minor )
3857{
3858 if( major != SSL_MAJOR_VERSION_3 )
3859 return;
3860
3861 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3862 return;
3863
3864 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003865}
3866
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003867#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003868/* Add a new (empty) key_cert entry an return a pointer to it */
3869static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3870{
3871 ssl_key_cert *key_cert, *last;
3872
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003873 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3874 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003875 return( NULL );
3876
3877 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3878
3879 /* Append the new key_cert to the (possibly empty) current list */
3880 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003881 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003882 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003883 if( ssl->handshake != NULL )
3884 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003885 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003886 else
3887 {
3888 last = ssl->key_cert;
3889 while( last->next != NULL )
3890 last = last->next;
3891 last->next = key_cert;
3892 }
3893
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003894 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003895}
3896
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003897void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003898 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003899{
3900 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003901 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003902 ssl->peer_cn = peer_cn;
3903}
3904
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003905int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003906 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003907{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003908 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3909
3910 if( key_cert == NULL )
3911 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3912
3913 key_cert->cert = own_cert;
3914 key_cert->key = pk_key;
3915
3916 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003917}
3918
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003919#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003920int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003921 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003922{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003923 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003924 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003925
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003926 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003927 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3928
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003929 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3930 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003931 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003932
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003933 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003934
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003935 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003936 if( ret != 0 )
3937 return( ret );
3938
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003939 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003940 return( ret );
3941
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003942 key_cert->cert = own_cert;
3943 key_cert->key_own_alloc = 1;
3944
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003945 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003946}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003947#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003948
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003949int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003950 void *rsa_key,
3951 rsa_decrypt_func rsa_decrypt,
3952 rsa_sign_func rsa_sign,
3953 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003954{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003955 int ret;
3956 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003957
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003958 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003959 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3960
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003961 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3962 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003963 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003964
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003965 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003966
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003967 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3968 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3969 return( ret );
3970
3971 key_cert->cert = own_cert;
3972 key_cert->key_own_alloc = 1;
3973
3974 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003975}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003976#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003977
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003978#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003979int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3980 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003981{
Paul Bakker6db455e2013-09-18 17:29:31 +02003982 if( psk == NULL || psk_identity == NULL )
3983 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3984
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003985 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003986 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3987
Paul Bakker6db455e2013-09-18 17:29:31 +02003988 if( ssl->psk != NULL )
3989 {
3990 polarssl_free( ssl->psk );
3991 polarssl_free( ssl->psk_identity );
3992 }
3993
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003994 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003995 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003996
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003997 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003998 ssl->psk_identity = (unsigned char *)
3999 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004000
4001 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4002 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4003
4004 memcpy( ssl->psk, psk, ssl->psk_len );
4005 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004006
4007 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004008}
4009
4010void ssl_set_psk_cb( ssl_context *ssl,
4011 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4012 size_t),
4013 void *p_psk )
4014{
4015 ssl->f_psk = f_psk;
4016 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004017}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004018#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004019
Paul Bakker48916f92012-09-16 19:57:18 +00004020#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004021int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004022{
4023 int ret;
4024
Paul Bakker48916f92012-09-16 19:57:18 +00004025 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004026 {
4027 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4028 return( ret );
4029 }
4030
Paul Bakker48916f92012-09-16 19:57:18 +00004031 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004032 {
4033 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4034 return( ret );
4035 }
4036
4037 return( 0 );
4038}
4039
Paul Bakker1b57b062011-01-06 15:48:19 +00004040int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4041{
4042 int ret;
4043
Paul Bakker66d5d072014-06-17 16:39:18 +02004044 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004045 {
4046 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4047 return( ret );
4048 }
4049
Paul Bakker66d5d072014-06-17 16:39:18 +02004050 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004051 {
4052 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4053 return( ret );
4054 }
4055
4056 return( 0 );
4057}
Paul Bakker48916f92012-09-16 19:57:18 +00004058#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004059
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004060#if defined(POLARSSL_SSL_SET_CURVES)
4061/*
4062 * Set the allowed elliptic curves
4063 */
4064void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4065{
4066 ssl->curve_list = curve_list;
4067}
4068#endif
4069
Paul Bakker0be444a2013-08-27 21:55:01 +02004070#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004071int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004072{
4073 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004074 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004075
4076 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004077
4078 if( ssl->hostname_len + 1 == 0 )
4079 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4080
Paul Bakker6e339b52013-07-03 13:37:05 +02004081 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004082
Paul Bakkerb15b8512012-01-13 13:44:06 +00004083 if( ssl->hostname == NULL )
4084 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4085
Paul Bakker3c2122f2013-06-24 19:03:14 +02004086 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004087 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004088
Paul Bakker40ea7de2009-05-03 10:18:48 +00004089 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004090
4091 return( 0 );
4092}
4093
Paul Bakker5701cdc2012-09-27 21:49:42 +00004094void ssl_set_sni( ssl_context *ssl,
4095 int (*f_sni)(void *, ssl_context *,
4096 const unsigned char *, size_t),
4097 void *p_sni )
4098{
4099 ssl->f_sni = f_sni;
4100 ssl->p_sni = p_sni;
4101}
Paul Bakker0be444a2013-08-27 21:55:01 +02004102#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004103
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004104#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004105int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004106{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004107 size_t cur_len, tot_len;
4108 const char **p;
4109
4110 /*
4111 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4112 * truncated". Check lengths now rather than later.
4113 */
4114 tot_len = 0;
4115 for( p = protos; *p != NULL; p++ )
4116 {
4117 cur_len = strlen( *p );
4118 tot_len += cur_len;
4119
4120 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4121 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4122 }
4123
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004124 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004125
4126 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004127}
4128
4129const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4130{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004131 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004132}
4133#endif /* POLARSSL_SSL_ALPN */
4134
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004135static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00004136{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004137 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
4138 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
4139 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4140 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004141 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004142 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004143 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004144
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004145 return( 0 );
4146}
4147
4148int ssl_set_max_version( ssl_context *ssl, int major, int minor )
4149{
4150 if( ssl_check_version( ssl, major, minor ) != 0 )
4151 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4152
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004153 ssl->max_major_ver = major;
4154 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004155
4156 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00004157}
4158
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004159int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00004160{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004161 if( ssl_check_version( ssl, major, minor ) != 0 )
4162 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004163
4164 ssl->min_major_ver = major;
4165 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004166
4167 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00004168}
4169
Paul Bakker05decb22013-08-15 13:33:48 +02004170#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004171int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4172{
Paul Bakker77e257e2013-12-16 15:29:52 +01004173 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004174 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004175 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004176 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004177 }
4178
4179 ssl->mfl_code = mfl_code;
4180
4181 return( 0 );
4182}
Paul Bakker05decb22013-08-15 13:33:48 +02004183#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004184
Paul Bakker1f2bc622013-08-15 13:45:55 +02004185#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004186int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004187{
4188 if( ssl->endpoint != SSL_IS_CLIENT )
4189 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4190
Paul Bakker8c1ede62013-07-19 14:14:37 +02004191 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004192
4193 return( 0 );
4194}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004195#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004196
Paul Bakker48916f92012-09-16 19:57:18 +00004197void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4198{
4199 ssl->disable_renegotiation = renegotiation;
4200}
4201
4202void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4203{
4204 ssl->allow_legacy_renegotiation = allow_legacy;
4205}
4206
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004207void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4208{
4209 ssl->renego_max_records = max_records;
4210}
4211
Paul Bakkera503a632013-08-14 13:48:06 +02004212#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004213int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4214{
4215 ssl->session_tickets = use_tickets;
4216
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004217 if( ssl->endpoint == SSL_IS_CLIENT )
4218 return( 0 );
4219
4220 if( ssl->f_rng == NULL )
4221 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4222
4223 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004224}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004225
4226void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4227{
4228 ssl->ticket_lifetime = lifetime;
4229}
Paul Bakkera503a632013-08-14 13:48:06 +02004230#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004231
Paul Bakker5121ce52009-01-03 21:22:43 +00004232/*
4233 * SSL get accessors
4234 */
Paul Bakker23986e52011-04-24 08:57:21 +00004235size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004236{
4237 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4238}
4239
Paul Bakkerff60ee62010-03-16 21:09:09 +00004240int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004241{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004242 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004243}
4244
Paul Bakkere3166ce2011-01-27 17:40:50 +00004245const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004246{
Paul Bakker926c8e42013-03-06 10:23:34 +01004247 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004248 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004249
Paul Bakkere3166ce2011-01-27 17:40:50 +00004250 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004251}
4252
Paul Bakker43ca69c2011-01-15 17:35:19 +00004253const char *ssl_get_version( const ssl_context *ssl )
4254{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004255#if defined(POLARSSL_SSL_PROTO_DTLS)
4256 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4257 {
4258 switch( ssl->minor_ver )
4259 {
4260 case SSL_MINOR_VERSION_2:
4261 return( "DTLSv1.0" );
4262
4263 case SSL_MINOR_VERSION_3:
4264 return( "DTLSv1.2" );
4265
4266 default:
4267 return( "unknown (DTLS)" );
4268 }
4269 }
4270#endif
4271
Paul Bakker43ca69c2011-01-15 17:35:19 +00004272 switch( ssl->minor_ver )
4273 {
4274 case SSL_MINOR_VERSION_0:
4275 return( "SSLv3.0" );
4276
4277 case SSL_MINOR_VERSION_1:
4278 return( "TLSv1.0" );
4279
4280 case SSL_MINOR_VERSION_2:
4281 return( "TLSv1.1" );
4282
Paul Bakker1ef83d62012-04-11 12:09:53 +00004283 case SSL_MINOR_VERSION_3:
4284 return( "TLSv1.2" );
4285
Paul Bakker43ca69c2011-01-15 17:35:19 +00004286 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004287 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00004288 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00004289}
4290
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004291#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004292const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004293{
4294 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004295 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004296
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004297 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004298}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004299#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004300
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004301int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4302{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004303 if( ssl == NULL ||
4304 dst == NULL ||
4305 ssl->session == NULL ||
4306 ssl->endpoint != SSL_IS_CLIENT )
4307 {
4308 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4309 }
4310
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004311 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004312}
4313
Paul Bakker5121ce52009-01-03 21:22:43 +00004314/*
Paul Bakker1961b702013-01-25 14:49:24 +01004315 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004316 */
Paul Bakker1961b702013-01-25 14:49:24 +01004317int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004318{
Paul Bakker40e46942009-01-03 21:51:57 +00004319 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004320
Paul Bakker40e46942009-01-03 21:51:57 +00004321#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004323 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004324#endif
4325
Paul Bakker40e46942009-01-03 21:51:57 +00004326#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004327 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004328 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004329#endif
4330
Paul Bakker1961b702013-01-25 14:49:24 +01004331 return( ret );
4332}
4333
4334/*
4335 * Perform the SSL handshake
4336 */
4337int ssl_handshake( ssl_context *ssl )
4338{
4339 int ret = 0;
4340
4341 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4342
4343 while( ssl->state != SSL_HANDSHAKE_OVER )
4344 {
4345 ret = ssl_handshake_step( ssl );
4346
4347 if( ret != 0 )
4348 break;
4349 }
4350
Paul Bakker5121ce52009-01-03 21:22:43 +00004351 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4352
4353 return( ret );
4354}
4355
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004356#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004357/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004358 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004359 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004360static int ssl_write_hello_request( ssl_context *ssl )
4361{
4362 int ret;
4363
4364 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4365
4366 ssl->out_msglen = 4;
4367 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4368 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4369
4370 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4371 {
4372 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4373 return( ret );
4374 }
4375
4376 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4377
4378 return( 0 );
4379}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004380#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004381
4382/*
4383 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004384 * - any side: calling ssl_renegotiate(),
4385 * - client: receiving a HelloRequest during ssl_read(),
4386 * - server: receiving any handshake message on server during ssl_read() after
4387 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004388 * If the handshake doesn't complete due to waiting for I/O, it will continue
4389 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004390 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004391static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004392{
4393 int ret;
4394
4395 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4396
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004397 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4398 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004399
4400 ssl->state = SSL_HELLO_REQUEST;
4401 ssl->renegotiation = SSL_RENEGOTIATION;
4402
Paul Bakker48916f92012-09-16 19:57:18 +00004403 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4404 {
4405 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4406 return( ret );
4407 }
4408
4409 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4410
4411 return( 0 );
4412}
4413
4414/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004415 * Renegotiate current connection on client,
4416 * or request renegotiation on server
4417 */
4418int ssl_renegotiate( ssl_context *ssl )
4419{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004420 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004421
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004422#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004423 /* On server, just send the request */
4424 if( ssl->endpoint == SSL_IS_SERVER )
4425 {
4426 if( ssl->state != SSL_HANDSHAKE_OVER )
4427 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4428
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004429 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4430
4431 /* Did we already try/start sending HelloRequest? */
4432 if( ssl->out_left != 0 )
4433 return( ssl_flush_output( ssl ) );
4434
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004435 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004436 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004437#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004438
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004439#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004440 /*
4441 * On client, either start the renegotiation process or,
4442 * if already in progress, continue the handshake
4443 */
4444 if( ssl->renegotiation != SSL_RENEGOTIATION )
4445 {
4446 if( ssl->state != SSL_HANDSHAKE_OVER )
4447 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4448
4449 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4450 {
4451 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4452 return( ret );
4453 }
4454 }
4455 else
4456 {
4457 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4458 {
4459 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4460 return( ret );
4461 }
4462 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004463#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004464
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004465 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004466}
4467
4468/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004469 * Receive application data decrypted from the SSL layer
4470 */
Paul Bakker23986e52011-04-24 08:57:21 +00004471int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004472{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004473 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004474 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004475
4476 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4477
4478 if( ssl->state != SSL_HANDSHAKE_OVER )
4479 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004480 ret = ssl_handshake( ssl );
4481 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4482 {
4483 record_read = 1;
4484 }
4485 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004486 {
4487 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4488 return( ret );
4489 }
4490 }
4491
4492 if( ssl->in_offt == NULL )
4493 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004494 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004495 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004496 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4497 {
4498 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4499 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004500
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004501 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4502 return( ret );
4503 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 }
4505
4506 if( ssl->in_msglen == 0 &&
4507 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4508 {
4509 /*
4510 * OpenSSL sends empty messages to randomize the IV
4511 */
4512 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4513 {
Paul Bakker831a7552011-05-18 13:32:51 +00004514 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4515 return( 0 );
4516
Paul Bakker5121ce52009-01-03 21:22:43 +00004517 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4518 return( ret );
4519 }
4520 }
4521
Paul Bakker48916f92012-09-16 19:57:18 +00004522 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4523 {
4524 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4525
4526 if( ssl->endpoint == SSL_IS_CLIENT &&
4527 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4528 ssl->in_hslen != 4 ) )
4529 {
4530 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4531 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4532 }
4533
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004534 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4535 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004536 ssl->allow_legacy_renegotiation ==
4537 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004538 {
4539 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4540
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004541#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004542 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004543 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004544 /*
4545 * SSLv3 does not have a "no_renegotiation" alert
4546 */
4547 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4548 return( ret );
4549 }
4550 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004551#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004552#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4553 defined(POLARSSL_SSL_PROTO_TLS1_2)
4554 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004555 {
4556 if( ( ret = ssl_send_alert_message( ssl,
4557 SSL_ALERT_LEVEL_WARNING,
4558 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4559 {
4560 return( ret );
4561 }
Paul Bakker48916f92012-09-16 19:57:18 +00004562 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004563 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004564#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4565 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004566 {
4567 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004568 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004569 }
Paul Bakker48916f92012-09-16 19:57:18 +00004570 }
4571 else
4572 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004573 ret = ssl_start_renegotiation( ssl );
4574 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4575 {
4576 record_read = 1;
4577 }
4578 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004579 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004580 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004581 return( ret );
4582 }
Paul Bakker48916f92012-09-16 19:57:18 +00004583 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004584
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004585 /* If a non-handshake record was read during renego, fallthrough,
4586 * else tell the user they should call ssl_read() again */
4587 if( ! record_read )
4588 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004589 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004590 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4591 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004592 ssl->renego_records_seen++;
4593
4594 if( ssl->renego_max_records >= 0 &&
4595 ssl->renego_records_seen > ssl->renego_max_records )
4596 {
4597 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4598 "but not honored by client" ) );
4599 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4600 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004601 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004602
4603 /* Fatal and closure alerts handled by ssl_read_record() */
4604 if( ssl->in_msgtype == SSL_MSG_ALERT )
4605 {
4606 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4607 return( POLARSSL_ERR_NET_WANT_READ );
4608 }
4609
4610 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004611 {
4612 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004613 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004614 }
4615
4616 ssl->in_offt = ssl->in_msg;
4617 }
4618
4619 n = ( len < ssl->in_msglen )
4620 ? len : ssl->in_msglen;
4621
4622 memcpy( buf, ssl->in_offt, n );
4623 ssl->in_msglen -= n;
4624
4625 if( ssl->in_msglen == 0 )
4626 /* all bytes consumed */
4627 ssl->in_offt = NULL;
4628 else
4629 /* more data available */
4630 ssl->in_offt += n;
4631
4632 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4633
Paul Bakker23986e52011-04-24 08:57:21 +00004634 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004635}
4636
4637/*
4638 * Send application data to be encrypted by the SSL layer
4639 */
Paul Bakker23986e52011-04-24 08:57:21 +00004640int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004641{
Paul Bakker23986e52011-04-24 08:57:21 +00004642 int ret;
4643 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004644 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004645
4646 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4647
4648 if( ssl->state != SSL_HANDSHAKE_OVER )
4649 {
4650 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4651 {
4652 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4653 return( ret );
4654 }
4655 }
4656
Paul Bakker05decb22013-08-15 13:33:48 +02004657#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004658 /*
4659 * Assume mfl_code is correct since it was checked when set
4660 */
4661 max_len = mfl_code_to_length[ssl->mfl_code];
4662
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004663 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004664 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004665 */
4666 if( ssl->session_out != NULL &&
4667 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4668 {
4669 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4670 }
Paul Bakker05decb22013-08-15 13:33:48 +02004671#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004672
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004673 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004674
Paul Bakker5121ce52009-01-03 21:22:43 +00004675 if( ssl->out_left != 0 )
4676 {
4677 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4678 {
4679 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4680 return( ret );
4681 }
4682 }
Paul Bakker887bd502011-06-08 13:10:54 +00004683 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004684 {
Paul Bakker887bd502011-06-08 13:10:54 +00004685 ssl->out_msglen = n;
4686 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4687 memcpy( ssl->out_msg, buf, n );
4688
4689 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4690 {
4691 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4692 return( ret );
4693 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004694 }
4695
4696 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4697
Paul Bakker23986e52011-04-24 08:57:21 +00004698 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004699}
4700
4701/*
4702 * Notify the peer that the connection is being closed
4703 */
4704int ssl_close_notify( ssl_context *ssl )
4705{
4706 int ret;
4707
4708 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4709
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004710 if( ssl->out_left != 0 )
4711 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004712
4713 if( ssl->state == SSL_HANDSHAKE_OVER )
4714 {
Paul Bakker48916f92012-09-16 19:57:18 +00004715 if( ( ret = ssl_send_alert_message( ssl,
4716 SSL_ALERT_LEVEL_WARNING,
4717 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004718 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004719 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 return( ret );
4721 }
4722 }
4723
4724 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4725
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004726 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004727}
4728
Paul Bakker48916f92012-09-16 19:57:18 +00004729void ssl_transform_free( ssl_transform *transform )
4730{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004731 if( transform == NULL )
4732 return;
4733
Paul Bakker48916f92012-09-16 19:57:18 +00004734#if defined(POLARSSL_ZLIB_SUPPORT)
4735 deflateEnd( &transform->ctx_deflate );
4736 inflateEnd( &transform->ctx_inflate );
4737#endif
4738
Paul Bakker84bbeb52014-07-01 14:53:22 +02004739 cipher_free( &transform->cipher_ctx_enc );
4740 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004741
Paul Bakker84bbeb52014-07-01 14:53:22 +02004742 md_free( &transform->md_ctx_enc );
4743 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004744
Paul Bakker34617722014-06-13 17:20:13 +02004745 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004746}
4747
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004748#if defined(POLARSSL_X509_CRT_PARSE_C)
4749static void ssl_key_cert_free( ssl_key_cert *key_cert )
4750{
4751 ssl_key_cert *cur = key_cert, *next;
4752
4753 while( cur != NULL )
4754 {
4755 next = cur->next;
4756
4757 if( cur->key_own_alloc )
4758 {
4759 pk_free( cur->key );
4760 polarssl_free( cur->key );
4761 }
4762 polarssl_free( cur );
4763
4764 cur = next;
4765 }
4766}
4767#endif /* POLARSSL_X509_CRT_PARSE_C */
4768
Paul Bakker48916f92012-09-16 19:57:18 +00004769void ssl_handshake_free( ssl_handshake_params *handshake )
4770{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004771 if( handshake == NULL )
4772 return;
4773
Paul Bakker48916f92012-09-16 19:57:18 +00004774#if defined(POLARSSL_DHM_C)
4775 dhm_free( &handshake->dhm_ctx );
4776#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004777#if defined(POLARSSL_ECDH_C)
4778 ecdh_free( &handshake->ecdh_ctx );
4779#endif
4780
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004781#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004782 /* explicit void pointer cast for buggy MS compiler */
4783 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004784#endif
4785
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004786#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4787 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4788 /*
4789 * Free only the linked list wrapper, not the keys themselves
4790 * since the belong to the SNI callback
4791 */
4792 if( handshake->sni_key_cert != NULL )
4793 {
4794 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4795
4796 while( cur != NULL )
4797 {
4798 next = cur->next;
4799 polarssl_free( cur );
4800 cur = next;
4801 }
4802 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004803#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004804
Paul Bakker34617722014-06-13 17:20:13 +02004805 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004806}
4807
4808void ssl_session_free( ssl_session *session )
4809{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004810 if( session == NULL )
4811 return;
4812
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004813#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004814 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004815 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004816 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004817 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004818 }
Paul Bakkered27a042013-04-18 22:46:23 +02004819#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004820
Paul Bakkera503a632013-08-14 13:48:06 +02004821#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004822 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004823#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004824
Paul Bakker34617722014-06-13 17:20:13 +02004825 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004826}
4827
Paul Bakker5121ce52009-01-03 21:22:43 +00004828/*
4829 * Free an SSL context
4830 */
4831void ssl_free( ssl_context *ssl )
4832{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004833 if( ssl == NULL )
4834 return;
4835
Paul Bakker5121ce52009-01-03 21:22:43 +00004836 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4837
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004838 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004840 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
4841 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004842 }
4843
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004844 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004846 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
4847 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 }
4849
Paul Bakker16770332013-10-11 09:59:44 +02004850#if defined(POLARSSL_ZLIB_SUPPORT)
4851 if( ssl->compress_buf != NULL )
4852 {
Paul Bakker34617722014-06-13 17:20:13 +02004853 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004854 polarssl_free( ssl->compress_buf );
4855 }
4856#endif
4857
Paul Bakker40e46942009-01-03 21:51:57 +00004858#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004859 mpi_free( &ssl->dhm_P );
4860 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004861#endif
4862
Paul Bakker48916f92012-09-16 19:57:18 +00004863 if( ssl->transform )
4864 {
4865 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004866 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004867 }
4868
4869 if( ssl->handshake )
4870 {
4871 ssl_handshake_free( ssl->handshake );
4872 ssl_transform_free( ssl->transform_negotiate );
4873 ssl_session_free( ssl->session_negotiate );
4874
Paul Bakker6e339b52013-07-03 13:37:05 +02004875 polarssl_free( ssl->handshake );
4876 polarssl_free( ssl->transform_negotiate );
4877 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004878 }
4879
Paul Bakkerc0463502013-02-14 11:19:38 +01004880 if( ssl->session )
4881 {
4882 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004883 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004884 }
4885
Paul Bakkera503a632013-08-14 13:48:06 +02004886#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004887 if( ssl->ticket_keys )
4888 {
4889 ssl_ticket_keys_free( ssl->ticket_keys );
4890 polarssl_free( ssl->ticket_keys );
4891 }
Paul Bakkera503a632013-08-14 13:48:06 +02004892#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004893
Paul Bakker0be444a2013-08-27 21:55:01 +02004894#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004895 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004896 {
Paul Bakker34617722014-06-13 17:20:13 +02004897 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004898 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004899 ssl->hostname_len = 0;
4900 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004901#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004902
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004903#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004904 if( ssl->psk != NULL )
4905 {
Paul Bakker34617722014-06-13 17:20:13 +02004906 polarssl_zeroize( ssl->psk, ssl->psk_len );
4907 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004908 polarssl_free( ssl->psk );
4909 polarssl_free( ssl->psk_identity );
4910 ssl->psk_len = 0;
4911 ssl->psk_identity_len = 0;
4912 }
4913#endif
4914
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004915#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004916 ssl_key_cert_free( ssl->key_cert );
4917#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004918
Paul Bakker05ef8352012-05-08 09:17:57 +00004919#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4920 if( ssl_hw_record_finish != NULL )
4921 {
4922 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4923 ssl_hw_record_finish( ssl );
4924 }
4925#endif
4926
Paul Bakker5121ce52009-01-03 21:22:43 +00004927 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004928
Paul Bakker86f04f42013-02-14 11:20:09 +01004929 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004930 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004931}
4932
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004933#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004934/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004935 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004936 */
4937unsigned char ssl_sig_from_pk( pk_context *pk )
4938{
4939#if defined(POLARSSL_RSA_C)
4940 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4941 return( SSL_SIG_RSA );
4942#endif
4943#if defined(POLARSSL_ECDSA_C)
4944 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4945 return( SSL_SIG_ECDSA );
4946#endif
4947 return( SSL_SIG_ANON );
4948}
4949
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004950pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4951{
4952 switch( sig )
4953 {
4954#if defined(POLARSSL_RSA_C)
4955 case SSL_SIG_RSA:
4956 return( POLARSSL_PK_RSA );
4957#endif
4958#if defined(POLARSSL_ECDSA_C)
4959 case SSL_SIG_ECDSA:
4960 return( POLARSSL_PK_ECDSA );
4961#endif
4962 default:
4963 return( POLARSSL_PK_NONE );
4964 }
4965}
Paul Bakker9af723c2014-05-01 13:03:14 +02004966#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004967
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004968/*
4969 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4970 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004971md_type_t ssl_md_alg_from_hash( unsigned char hash )
4972{
4973 switch( hash )
4974 {
4975#if defined(POLARSSL_MD5_C)
4976 case SSL_HASH_MD5:
4977 return( POLARSSL_MD_MD5 );
4978#endif
4979#if defined(POLARSSL_SHA1_C)
4980 case SSL_HASH_SHA1:
4981 return( POLARSSL_MD_SHA1 );
4982#endif
4983#if defined(POLARSSL_SHA256_C)
4984 case SSL_HASH_SHA224:
4985 return( POLARSSL_MD_SHA224 );
4986 case SSL_HASH_SHA256:
4987 return( POLARSSL_MD_SHA256 );
4988#endif
4989#if defined(POLARSSL_SHA512_C)
4990 case SSL_HASH_SHA384:
4991 return( POLARSSL_MD_SHA384 );
4992 case SSL_HASH_SHA512:
4993 return( POLARSSL_MD_SHA512 );
4994#endif
4995 default:
4996 return( POLARSSL_MD_NONE );
4997 }
4998}
4999
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005000#if defined(POLARSSL_SSL_SET_CURVES)
5001/*
5002 * Check is a curve proposed by the peer is in our list.
5003 * Return 1 if we're willing to use it, 0 otherwise.
5004 */
5005int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5006{
5007 const ecp_group_id *gid;
5008
5009 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5010 if( *gid == grp_id )
5011 return( 1 );
5012
5013 return( 0 );
5014}
Paul Bakker9af723c2014-05-01 13:03:14 +02005015#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005016
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005017#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005018int ssl_check_cert_usage( const x509_crt *cert,
5019 const ssl_ciphersuite_t *ciphersuite,
5020 int cert_endpoint )
5021{
5022#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5023 int usage = 0;
5024#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005025#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5026 const char *ext_oid;
5027 size_t ext_len;
5028#endif
5029
5030#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5031 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5032 ((void) cert);
5033 ((void) cert_endpoint);
5034#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005035
5036#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5037 if( cert_endpoint == SSL_IS_SERVER )
5038 {
5039 /* Server part of the key exchange */
5040 switch( ciphersuite->key_exchange )
5041 {
5042 case POLARSSL_KEY_EXCHANGE_RSA:
5043 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5044 usage = KU_KEY_ENCIPHERMENT;
5045 break;
5046
5047 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5048 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5049 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5050 usage = KU_DIGITAL_SIGNATURE;
5051 break;
5052
5053 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5054 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5055 usage = KU_KEY_AGREEMENT;
5056 break;
5057
5058 /* Don't use default: we want warnings when adding new values */
5059 case POLARSSL_KEY_EXCHANGE_NONE:
5060 case POLARSSL_KEY_EXCHANGE_PSK:
5061 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5062 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5063 usage = 0;
5064 }
5065 }
5066 else
5067 {
5068 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5069 usage = KU_DIGITAL_SIGNATURE;
5070 }
5071
5072 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5073 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005074#else
5075 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005076#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5077
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005078#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5079 if( cert_endpoint == SSL_IS_SERVER )
5080 {
5081 ext_oid = OID_SERVER_AUTH;
5082 ext_len = OID_SIZE( OID_SERVER_AUTH );
5083 }
5084 else
5085 {
5086 ext_oid = OID_CLIENT_AUTH;
5087 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5088 }
5089
5090 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5091 return( -1 );
5092#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5093
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005094 return( 0 );
5095}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005096#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005097
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005098/*
5099 * Convert version numbers to/from wire format
5100 * and, for DTLS, to/from TLS equivalent.
5101 *
5102 * For TLS this is the identity.
5103 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
5104 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5105 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5106 */
5107void ssl_write_version( int major, int minor, int transport,
5108 unsigned char ver[2] )
5109{
5110 if( transport == SSL_TRANSPORT_STREAM )
5111 {
5112 ver[0] = (unsigned char) major;
5113 ver[1] = (unsigned char) minor;
5114 }
5115#if defined(POLARSSL_SSL_PROTO_DTLS)
5116 else
5117 {
5118 if( minor == SSL_MINOR_VERSION_2 )
5119 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5120
5121 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5122 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5123 }
5124#endif
5125}
5126
5127void ssl_read_version( int *major, int *minor, int transport,
5128 const unsigned char ver[2] )
5129{
5130 if( transport == SSL_TRANSPORT_STREAM )
5131 {
5132 *major = ver[0];
5133 *minor = ver[1];
5134 }
5135#if defined(POLARSSL_SSL_PROTO_DTLS)
5136 else
5137 {
5138 *major = 255 - ver[0] + 2;
5139 *minor = 255 - ver[1] + 1;
5140
5141 if( *minor == SSL_MINOR_VERSION_1 )
5142 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5143 }
5144#endif
5145}
5146
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005147#endif /* POLARSSL_SSL_TLS_C */