blob: f42a56058225b99c746d5e70f79b82cc03c9e3c2 [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 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +020082#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083/*
84 * Start a timer.
85 * Passing millisecs = 0 cancels a running timer.
86 * The timer is already running iff time_limit != 0.
87 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020088static void ssl_set_timer( ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089{
90 ssl->time_limit = millisecs;
91 get_timer( &ssl->time_info, 1 );
92}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020097static int ssl_check_timer( ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
99 if( ssl->time_limit != 0 &&
100 get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
101 {
102 return( -1 );
103 }
104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200108/*
109 * Double the retransmit timeout value, within the allowed range,
110 * returning -1 if the maximum value has already been reached.
111 */
112static int ssl_double_retransmit_timeout( ssl_context *ssl )
113{
114 uint32_t new_timeout;
115
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200116 if( ssl->handshake->retransmit_timeout >= ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200117 return( -1 );
118
119 new_timeout = 2 * ssl->handshake->retransmit_timeout;
120
121 /* Avoid arithmetic overflow and range overflow */
122 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200123 new_timeout > ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200124 {
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200125 new_timeout = ssl->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200126 }
127
128 ssl->handshake->retransmit_timeout = new_timeout;
129 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
130 ssl->handshake->retransmit_timeout ) );
131
132 return( 0 );
133}
134
135static void ssl_reset_retransmit_timeout( ssl_context *ssl )
136{
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200137 ssl->handshake->retransmit_timeout = ssl->hs_timeout_min;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200138 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
139 ssl->handshake->retransmit_timeout ) );
140}
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +0200141#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200142
Paul Bakker05decb22013-08-15 13:33:48 +0200143#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200144/*
145 * Convert max_fragment_length codes to length.
146 * RFC 6066 says:
147 * enum{
148 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
149 * } MaxFragmentLength;
150 * and we add 0 -> extension unused
151 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200152static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200153{
154 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
155 512, /* SSL_MAX_FRAG_LEN_512 */
156 1024, /* SSL_MAX_FRAG_LEN_1024 */
157 2048, /* SSL_MAX_FRAG_LEN_2048 */
158 4096, /* SSL_MAX_FRAG_LEN_4096 */
159};
Paul Bakker05decb22013-08-15 13:33:48 +0200160#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200161
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200162static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
163{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200164 ssl_session_free( dst );
165 memcpy( dst, src, sizeof( ssl_session ) );
166
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200168 if( src->peer_cert != NULL )
169 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200170 int ret;
171
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
173 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200174 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
175
Paul Bakkerb6b09562013-09-18 14:17:41 +0200176 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200177
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200178 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
179 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200180 {
181 polarssl_free( dst->peer_cert );
182 dst->peer_cert = NULL;
183 return( ret );
184 }
185 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187
Paul Bakkera503a632013-08-14 13:48:06 +0200188#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200189 if( src->ticket != NULL )
190 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200191 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
192 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200193 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
194
195 memcpy( dst->ticket, src->ticket, src->ticket_len );
196 }
Paul Bakkera503a632013-08-14 13:48:06 +0200197#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200198
199 return( 0 );
200}
201
Paul Bakker05ef8352012-05-08 09:17:57 +0000202#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200203int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200204 const unsigned char *key_enc, const unsigned char *key_dec,
205 size_t keylen,
206 const unsigned char *iv_enc, const unsigned char *iv_dec,
207 size_t ivlen,
208 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200209 size_t maclen ) = NULL;
210int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
211int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
212int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
213int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
214int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200215#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000216
Paul Bakker5121ce52009-01-03 21:22:43 +0000217/*
218 * Key material generation
219 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200220#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200221static int ssl3_prf( const unsigned char *secret, size_t slen,
222 const char *label,
223 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000224 unsigned char *dstbuf, size_t dlen )
225{
226 size_t i;
227 md5_context md5;
228 sha1_context sha1;
229 unsigned char padding[16];
230 unsigned char sha1sum[20];
231 ((void)label);
232
Paul Bakker5b4af392014-06-26 12:09:34 +0200233 md5_init( &md5 );
234 sha1_init( &sha1 );
235
Paul Bakker5f70b252012-09-13 14:23:06 +0000236 /*
237 * SSLv3:
238 * block =
239 * MD5( secret + SHA1( 'A' + secret + random ) ) +
240 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
241 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
242 * ...
243 */
244 for( i = 0; i < dlen / 16; i++ )
245 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200246 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000247
248 sha1_starts( &sha1 );
249 sha1_update( &sha1, padding, 1 + i );
250 sha1_update( &sha1, secret, slen );
251 sha1_update( &sha1, random, rlen );
252 sha1_finish( &sha1, sha1sum );
253
254 md5_starts( &md5 );
255 md5_update( &md5, secret, slen );
256 md5_update( &md5, sha1sum, 20 );
257 md5_finish( &md5, dstbuf + i * 16 );
258 }
259
Paul Bakker5b4af392014-06-26 12:09:34 +0200260 md5_free( &md5 );
261 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000262
Paul Bakker34617722014-06-13 17:20:13 +0200263 polarssl_zeroize( padding, sizeof( padding ) );
264 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000265
266 return( 0 );
267}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200268#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000269
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200270#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200271static int tls1_prf( const unsigned char *secret, size_t slen,
272 const char *label,
273 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000274 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000275{
Paul Bakker23986e52011-04-24 08:57:21 +0000276 size_t nb, hs;
277 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200278 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 unsigned char tmp[128];
280 unsigned char h_i[20];
281
282 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000283 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285 hs = ( slen + 1 ) / 2;
286 S1 = secret;
287 S2 = secret + slen - hs;
288
289 nb = strlen( label );
290 memcpy( tmp + 20, label, nb );
291 memcpy( tmp + 20 + nb, random, rlen );
292 nb += rlen;
293
294 /*
295 * First compute P_md5(secret,label+random)[0..dlen]
296 */
297 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
298
299 for( i = 0; i < dlen; i += 16 )
300 {
301 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
302 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
303
304 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
305
306 for( j = 0; j < k; j++ )
307 dstbuf[i + j] = h_i[j];
308 }
309
310 /*
311 * XOR out with P_sha1(secret,label+random)[0..dlen]
312 */
313 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
314
315 for( i = 0; i < dlen; i += 20 )
316 {
317 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
318 sha1_hmac( S2, hs, tmp, 20, tmp );
319
320 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
321
322 for( j = 0; j < k; j++ )
323 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
324 }
325
Paul Bakker34617722014-06-13 17:20:13 +0200326 polarssl_zeroize( tmp, sizeof( tmp ) );
327 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329 return( 0 );
330}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200331#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200333#if defined(POLARSSL_SSL_PROTO_TLS1_2)
334#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200335static int tls_prf_sha256( const unsigned char *secret, size_t slen,
336 const char *label,
337 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000338 unsigned char *dstbuf, size_t dlen )
339{
340 size_t nb;
341 size_t i, j, k;
342 unsigned char tmp[128];
343 unsigned char h_i[32];
344
345 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
346 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
347
348 nb = strlen( label );
349 memcpy( tmp + 32, label, nb );
350 memcpy( tmp + 32 + nb, random, rlen );
351 nb += rlen;
352
353 /*
354 * Compute P_<hash>(secret, label + random)[0..dlen]
355 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200356 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000357
358 for( i = 0; i < dlen; i += 32 )
359 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200360 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
361 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000362
363 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
364
365 for( j = 0; j < k; j++ )
366 dstbuf[i + j] = h_i[j];
367 }
368
Paul Bakker34617722014-06-13 17:20:13 +0200369 polarssl_zeroize( tmp, sizeof( tmp ) );
370 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000371
372 return( 0 );
373}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200374#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker9e36f042013-06-30 14:34:05 +0200376#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200377static int tls_prf_sha384( const unsigned char *secret, size_t slen,
378 const char *label,
379 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000380 unsigned char *dstbuf, size_t dlen )
381{
382 size_t nb;
383 size_t i, j, k;
384 unsigned char tmp[128];
385 unsigned char h_i[48];
386
387 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
388 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
389
390 nb = strlen( label );
391 memcpy( tmp + 48, label, nb );
392 memcpy( tmp + 48 + nb, random, rlen );
393 nb += rlen;
394
395 /*
396 * Compute P_<hash>(secret, label + random)[0..dlen]
397 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200398 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000399
400 for( i = 0; i < dlen; i += 48 )
401 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200402 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
403 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404
405 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
406
407 for( j = 0; j < k; j++ )
408 dstbuf[i + j] = h_i[j];
409 }
410
Paul Bakker34617722014-06-13 17:20:13 +0200411 polarssl_zeroize( tmp, sizeof( tmp ) );
412 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413
414 return( 0 );
415}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200416#endif /* POLARSSL_SHA512_C */
417#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418
Paul Bakker66d5d072014-06-17 16:39:18 +0200419static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200420
421#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
422 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200423static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200424#endif
Paul Bakker380da532012-04-18 16:10:25 +0000425
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200427static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
428static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200429#endif
430
431#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200432static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
433static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434#endif
435
436#if defined(POLARSSL_SSL_PROTO_TLS1_2)
437#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200438static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
439static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
440static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200441#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100442
Paul Bakker9e36f042013-06-30 14:34:05 +0200443#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200444static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
445static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
446static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100447#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200448#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000449
Paul Bakker5121ce52009-01-03 21:22:43 +0000450int ssl_derive_keys( ssl_context *ssl )
451{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200452 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 unsigned char keyblk[256];
455 unsigned char *key1;
456 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100457 unsigned char *mac_enc;
458 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200459 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100460 const cipher_info_t *cipher_info;
461 const md_info_t *md_info;
462
Paul Bakker48916f92012-09-16 19:57:18 +0000463 ssl_session *session = ssl->session_negotiate;
464 ssl_transform *transform = ssl->transform_negotiate;
465 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
468
Paul Bakker68884e32013-01-07 18:20:04 +0100469 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
470 if( cipher_info == NULL )
471 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200472 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100473 transform->ciphersuite_info->cipher ) );
474 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
475 }
476
477 md_info = md_info_from_type( transform->ciphersuite_info->mac );
478 if( md_info == NULL )
479 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200480 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100481 transform->ciphersuite_info->mac ) );
482 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
483 }
484
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000486 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000487 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200488#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000489 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000490 {
Paul Bakker48916f92012-09-16 19:57:18 +0000491 handshake->tls_prf = ssl3_prf;
492 handshake->calc_verify = ssl_calc_verify_ssl;
493 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000494 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200495 else
496#endif
497#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
498 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000499 {
Paul Bakker48916f92012-09-16 19:57:18 +0000500 handshake->tls_prf = tls1_prf;
501 handshake->calc_verify = ssl_calc_verify_tls;
502 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000503 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200504 else
505#endif
506#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200507#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200508 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
509 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000510 {
Paul Bakker48916f92012-09-16 19:57:18 +0000511 handshake->tls_prf = tls_prf_sha384;
512 handshake->calc_verify = ssl_calc_verify_tls_sha384;
513 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000514 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000515 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200516#endif
517#if defined(POLARSSL_SHA256_C)
518 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000519 {
Paul Bakker48916f92012-09-16 19:57:18 +0000520 handshake->tls_prf = tls_prf_sha256;
521 handshake->calc_verify = ssl_calc_verify_tls_sha256;
522 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000523 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200524 else
525#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200526#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200527 {
528 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200529 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200530 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000531
532 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 * SSLv3:
534 * master =
535 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
536 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
537 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200538 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200539 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 * master = PRF( premaster, "master secret", randbytes )[0..47]
541 */
Paul Bakker0a597072012-09-25 21:55:46 +0000542 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 {
Paul Bakker48916f92012-09-16 19:57:18 +0000544 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
545 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Paul Bakker48916f92012-09-16 19:57:18 +0000547 handshake->tls_prf( handshake->premaster, handshake->pmslen,
548 "master secret",
549 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
Paul Bakker34617722014-06-13 17:20:13 +0200551 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 }
553 else
554 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
555
556 /*
557 * Swap the client and server random values.
558 */
Paul Bakker48916f92012-09-16 19:57:18 +0000559 memcpy( tmp, handshake->randbytes, 64 );
560 memcpy( handshake->randbytes, tmp + 32, 32 );
561 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200562 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 /*
565 * SSLv3:
566 * key block =
567 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
568 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
569 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
570 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
571 * ...
572 *
573 * TLSv1:
574 * key block = PRF( master, "key expansion", randbytes )
575 */
Paul Bakker48916f92012-09-16 19:57:18 +0000576 handshake->tls_prf( session->master, 48, "key expansion",
577 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Paul Bakker48916f92012-09-16 19:57:18 +0000579 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
580 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
581 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
582 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
584
Paul Bakker34617722014-06-13 17:20:13 +0200585 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587 /*
588 * Determine the appropriate key, IV and MAC length.
589 */
Paul Bakker68884e32013-01-07 18:20:04 +0100590
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200591 transform->keylen = cipher_info->key_length / 8;
592
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200593 if( cipher_info->mode == POLARSSL_MODE_GCM ||
594 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200596 transform->maclen = 0;
597
Paul Bakker68884e32013-01-07 18:20:04 +0100598 transform->ivlen = 12;
599 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200600
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200601 /* Minimum length is expicit IV + tag */
602 transform->minlen = transform->ivlen - transform->fixed_ivlen
603 + ( transform->ciphersuite_info->flags &
604 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100605 }
606 else
607 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200608 int ret;
609
610 /* Initialize HMAC contexts */
611 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
612 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100613 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200614 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
615 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100616 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200618 /* Get MAC length */
619 transform->maclen = md_get_size( md_info );
620
621#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
622 /*
623 * If HMAC is to be truncated, we shall keep the leftmost bytes,
624 * (rfc 6066 page 13 or rfc 2104 section 4),
625 * so we only need to adjust the length here.
626 */
627 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
628 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
629#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
630
631 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100632 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200634 /* Minimum length */
635 if( cipher_info->mode == POLARSSL_MODE_STREAM )
636 transform->minlen = transform->maclen;
637 else
Paul Bakker68884e32013-01-07 18:20:04 +0100638 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200639 /*
640 * GenericBlockCipher:
641 * first multiple of blocklen greater than maclen
642 * + IV except for SSL3 and TLS 1.0
643 */
644 transform->minlen = transform->maclen
645 + cipher_info->block_size
646 - transform->maclen % cipher_info->block_size;
647
648#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
649 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
650 ssl->minor_ver == SSL_MINOR_VERSION_1 )
651 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100652 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200653#endif
654#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
655 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
656 ssl->minor_ver == SSL_MINOR_VERSION_3 )
657 {
658 transform->minlen += transform->ivlen;
659 }
660 else
661#endif
662 {
663 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
664 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
665 }
Paul Bakker68884e32013-01-07 18:20:04 +0100666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 }
668
669 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000670 transform->keylen, transform->minlen, transform->ivlen,
671 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 /*
674 * Finally setup the cipher contexts, IVs and MAC secrets.
675 */
676 if( ssl->endpoint == SSL_IS_CLIENT )
677 {
Paul Bakker48916f92012-09-16 19:57:18 +0000678 key1 = keyblk + transform->maclen * 2;
679 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000680
Paul Bakker68884e32013-01-07 18:20:04 +0100681 mac_enc = keyblk;
682 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000684 /*
685 * This is not used in TLS v1.1.
686 */
Paul Bakker48916f92012-09-16 19:57:18 +0000687 iv_copy_len = ( transform->fixed_ivlen ) ?
688 transform->fixed_ivlen : transform->ivlen;
689 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
690 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000691 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 }
693 else
694 {
Paul Bakker48916f92012-09-16 19:57:18 +0000695 key1 = keyblk + transform->maclen * 2 + transform->keylen;
696 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
Paul Bakker68884e32013-01-07 18:20:04 +0100698 mac_enc = keyblk + transform->maclen;
699 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000701 /*
702 * This is not used in TLS v1.1.
703 */
Paul Bakker48916f92012-09-16 19:57:18 +0000704 iv_copy_len = ( transform->fixed_ivlen ) ?
705 transform->fixed_ivlen : transform->ivlen;
706 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
707 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000708 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 }
710
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200711#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100712 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
713 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100714 if( transform->maclen > sizeof transform->mac_enc )
715 {
716 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200717 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100718 }
719
Paul Bakker68884e32013-01-07 18:20:04 +0100720 memcpy( transform->mac_enc, mac_enc, transform->maclen );
721 memcpy( transform->mac_dec, mac_dec, transform->maclen );
722 }
723 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200724#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200725#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
726 defined(POLARSSL_SSL_PROTO_TLS1_2)
727 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100728 {
729 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
730 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
731 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200732 else
733#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200734 {
735 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200736 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200737 }
Paul Bakker68884e32013-01-07 18:20:04 +0100738
Paul Bakker05ef8352012-05-08 09:17:57 +0000739#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200740 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000741 {
742 int ret = 0;
743
744 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
745
Paul Bakker07eb38b2012-12-19 14:42:06 +0100746 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
747 transform->iv_enc, transform->iv_dec,
748 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100749 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100750 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000751 {
752 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200753 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000754 }
755 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200756#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000757
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200758 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
759 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200761 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
762 return( ret );
763 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200764
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200765 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
766 cipher_info ) ) != 0 )
767 {
768 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
769 return( ret );
770 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200772 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
773 cipher_info->key_length,
774 POLARSSL_ENCRYPT ) ) != 0 )
775 {
776 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
777 return( ret );
778 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200779
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200780 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
781 cipher_info->key_length,
782 POLARSSL_DECRYPT ) ) != 0 )
783 {
784 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
785 return( ret );
786 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200787
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200788#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200789 if( cipher_info->mode == POLARSSL_MODE_CBC )
790 {
791 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
792 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200793 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200794 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
795 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200796 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200797
798 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
799 POLARSSL_PADDING_NONE ) ) != 0 )
800 {
801 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
802 return( ret );
803 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000804 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200805#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
Paul Bakker34617722014-06-13 17:20:13 +0200807 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
Paul Bakker2770fbd2012-07-03 13:30:23 +0000809#if defined(POLARSSL_ZLIB_SUPPORT)
810 // Initialize compression
811 //
Paul Bakker48916f92012-09-16 19:57:18 +0000812 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000813 {
Paul Bakker16770332013-10-11 09:59:44 +0200814 if( ssl->compress_buf == NULL )
815 {
816 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
817 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
818 if( ssl->compress_buf == NULL )
819 {
820 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
821 SSL_BUFFER_LEN ) );
822 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
823 }
824 }
825
Paul Bakker2770fbd2012-07-03 13:30:23 +0000826 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
827
Paul Bakker48916f92012-09-16 19:57:18 +0000828 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
829 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000830
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200831 if( deflateInit( &transform->ctx_deflate,
832 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000833 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000834 {
835 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
836 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
837 }
838 }
839#endif /* POLARSSL_ZLIB_SUPPORT */
840
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
842
843 return( 0 );
844}
845
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200846#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000847void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000848{
849 md5_context md5;
850 sha1_context sha1;
851 unsigned char pad_1[48];
852 unsigned char pad_2[48];
853
Paul Bakker380da532012-04-18 16:10:25 +0000854 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Paul Bakker48916f92012-09-16 19:57:18 +0000856 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
857 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Paul Bakker380da532012-04-18 16:10:25 +0000859 memset( pad_1, 0x36, 48 );
860 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000861
Paul Bakker48916f92012-09-16 19:57:18 +0000862 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000863 md5_update( &md5, pad_1, 48 );
864 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000865
Paul Bakker380da532012-04-18 16:10:25 +0000866 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000867 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000868 md5_update( &md5, pad_2, 48 );
869 md5_update( &md5, hash, 16 );
870 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Paul Bakker48916f92012-09-16 19:57:18 +0000872 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000873 sha1_update( &sha1, pad_1, 40 );
874 sha1_finish( &sha1, hash + 16 );
875
876 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000877 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000878 sha1_update( &sha1, pad_2, 40 );
879 sha1_update( &sha1, hash + 16, 20 );
880 sha1_finish( &sha1, hash + 16 );
881
882 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
883 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
884
Paul Bakker5b4af392014-06-26 12:09:34 +0200885 md5_free( &md5 );
886 sha1_free( &sha1 );
887
Paul Bakker380da532012-04-18 16:10:25 +0000888 return;
889}
Paul Bakker9af723c2014-05-01 13:03:14 +0200890#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000891
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200892#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000893void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
894{
895 md5_context md5;
896 sha1_context sha1;
897
898 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
899
Paul Bakker48916f92012-09-16 19:57:18 +0000900 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
901 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000902
Paul Bakker48916f92012-09-16 19:57:18 +0000903 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000904 sha1_finish( &sha1, hash + 16 );
905
906 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
907 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
908
Paul Bakker5b4af392014-06-26 12:09:34 +0200909 md5_free( &md5 );
910 sha1_free( &sha1 );
911
Paul Bakker380da532012-04-18 16:10:25 +0000912 return;
913}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200914#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000915
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#if defined(POLARSSL_SSL_PROTO_TLS1_2)
917#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000918void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
919{
Paul Bakker9e36f042013-06-30 14:34:05 +0200920 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000921
922 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
923
Paul Bakker9e36f042013-06-30 14:34:05 +0200924 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
925 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000926
927 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
928 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
929
Paul Bakker5b4af392014-06-26 12:09:34 +0200930 sha256_free( &sha256 );
931
Paul Bakker380da532012-04-18 16:10:25 +0000932 return;
933}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200934#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000935
Paul Bakker9e36f042013-06-30 14:34:05 +0200936#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000937void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
938{
Paul Bakker9e36f042013-06-30 14:34:05 +0200939 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000940
941 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
942
Paul Bakker9e36f042013-06-30 14:34:05 +0200943 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
944 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Paul Bakkerca4ab492012-04-18 14:23:57 +0000946 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
948
Paul Bakker5b4af392014-06-26 12:09:34 +0200949 sha512_free( &sha512 );
950
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 return;
952}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200953#endif /* POLARSSL_SHA512_C */
954#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200956#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200957int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
958{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200959 unsigned char *p = ssl->handshake->premaster;
960 unsigned char *end = p + sizeof( ssl->handshake->premaster );
961
962 /*
963 * PMS = struct {
964 * opaque other_secret<0..2^16-1>;
965 * opaque psk<0..2^16-1>;
966 * };
967 * with "other_secret" depending on the particular key exchange
968 */
969#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
970 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
971 {
972 if( end - p < 2 + (int) ssl->psk_len )
973 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
974
975 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
976 *(p++) = (unsigned char)( ssl->psk_len );
977 p += ssl->psk_len;
978 }
979 else
980#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200981#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
982 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
983 {
984 /*
985 * other_secret already set by the ClientKeyExchange message,
986 * and is 48 bytes long
987 */
988 *p++ = 0;
989 *p++ = 48;
990 p += 48;
991 }
992 else
993#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200994#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
995 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
996 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200997 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200998 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200999
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001000 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001001 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001002 p + 2, &len,
1003 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 {
1005 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1006 return( ret );
1007 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001008 *(p++) = (unsigned char)( len >> 8 );
1009 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001010 p += len;
1011
1012 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1013 }
1014 else
1015#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1016#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1017 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1018 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001019 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001020 size_t zlen;
1021
1022 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001023 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001024 ssl->f_rng, ssl->p_rng ) ) != 0 )
1025 {
1026 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1027 return( ret );
1028 }
1029
1030 *(p++) = (unsigned char)( zlen >> 8 );
1031 *(p++) = (unsigned char)( zlen );
1032 p += zlen;
1033
1034 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1035 }
1036 else
1037#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1038 {
1039 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001040 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001041 }
1042
1043 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001044 if( end - p < 2 + (int) ssl->psk_len )
1045 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1046
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001047 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1048 *(p++) = (unsigned char)( ssl->psk_len );
1049 memcpy( p, ssl->psk, ssl->psk_len );
1050 p += ssl->psk_len;
1051
1052 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1053
1054 return( 0 );
1055}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001056#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001057
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001058#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001059/*
1060 * SSLv3.0 MAC functions
1061 */
Paul Bakker68884e32013-01-07 18:20:04 +01001062static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1063 unsigned char *buf, size_t len,
1064 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001065{
1066 unsigned char header[11];
1067 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001068 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001069 int md_size = md_get_size( md_ctx->md_info );
1070 int md_type = md_get_type( md_ctx->md_info );
1071
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001072 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001073 if( md_type == POLARSSL_MD_MD5 )
1074 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001075 else
Paul Bakker68884e32013-01-07 18:20:04 +01001076 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
1078 memcpy( header, ctr, 8 );
1079 header[ 8] = (unsigned char) type;
1080 header[ 9] = (unsigned char)( len >> 8 );
1081 header[10] = (unsigned char)( len );
1082
Paul Bakker68884e32013-01-07 18:20:04 +01001083 memset( padding, 0x36, padlen );
1084 md_starts( md_ctx );
1085 md_update( md_ctx, secret, md_size );
1086 md_update( md_ctx, padding, padlen );
1087 md_update( md_ctx, header, 11 );
1088 md_update( md_ctx, buf, len );
1089 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Paul Bakker68884e32013-01-07 18:20:04 +01001091 memset( padding, 0x5C, padlen );
1092 md_starts( md_ctx );
1093 md_update( md_ctx, secret, md_size );
1094 md_update( md_ctx, padding, padlen );
1095 md_update( md_ctx, buf + len, md_size );
1096 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001097}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001098#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001099
Paul Bakker5121ce52009-01-03 21:22:43 +00001100/*
1101 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001102 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001103static int ssl_encrypt_buf( ssl_context *ssl )
1104{
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001105 const cipher_mode_t mode = cipher_get_cipher_mode(
1106 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
1108 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1109
1110 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001111 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001113#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1114 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1115 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001116 if( mode != POLARSSL_MODE_GCM &&
1117 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001119#if defined(POLARSSL_SSL_PROTO_SSL3)
1120 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1121 {
1122 ssl_mac( &ssl->transform_out->md_ctx_enc,
1123 ssl->transform_out->mac_enc,
1124 ssl->out_msg, ssl->out_msglen,
1125 ssl->out_ctr, ssl->out_msgtype );
1126 }
1127 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001128#endif
1129#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001130 defined(POLARSSL_SSL_PROTO_TLS1_2)
1131 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1132 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001133 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1134 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1135 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001136 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1137 ssl->out_msg, ssl->out_msglen );
1138 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1139 ssl->out_msg + ssl->out_msglen );
1140 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1141 }
1142 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001143#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001144 {
1145 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001146 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001147 }
1148
1149 SSL_DEBUG_BUF( 4, "computed mac",
1150 ssl->out_msg + ssl->out_msglen,
1151 ssl->transform_out->maclen );
1152
1153 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001154 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001155#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001157 /*
1158 * Encrypt
1159 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001160#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001161 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001163 int ret;
1164 size_t olen = 0;
1165
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1167 "including %d bytes of padding",
1168 ssl->out_msglen, 0 ) );
1169
1170 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1171 ssl->out_msg, ssl->out_msglen );
1172
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001173 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001174 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001175 ssl->transform_out->ivlen,
1176 ssl->out_msg, ssl->out_msglen,
1177 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001178 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001179 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001180 return( ret );
1181 }
1182
1183 if( ssl->out_msglen != olen )
1184 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001185 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001186 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001190#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001191#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1192 if( mode == POLARSSL_MODE_GCM ||
1193 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001194 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001195 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001196 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197 unsigned char *enc_msg;
1198 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001199 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1200 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 memcpy( add_data, ssl->out_ctr, 8 );
1203 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001204 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1205 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001206 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1207 add_data[12] = ssl->out_msglen & 0xFF;
1208
1209 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1210 add_data, 13 );
1211
Paul Bakker68884e32013-01-07 18:20:04 +01001212 /*
1213 * Generate IV
1214 */
1215 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001216 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1217 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001218 if( ret != 0 )
1219 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001220
Paul Bakker68884e32013-01-07 18:20:04 +01001221 memcpy( ssl->out_iv,
1222 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1223 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001224
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001225 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001226 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001227
Paul Bakker68884e32013-01-07 18:20:04 +01001228 /*
1229 * Fix pointer positions and message length with added IV
1230 */
1231 enc_msg = ssl->out_msg;
1232 enc_msglen = ssl->out_msglen;
1233 ssl->out_msglen += ssl->transform_out->ivlen -
1234 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001235
Paul Bakker68884e32013-01-07 18:20:04 +01001236 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1237 "including %d bytes of padding",
1238 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001239
Paul Bakker68884e32013-01-07 18:20:04 +01001240 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1241 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001242
Paul Bakker68884e32013-01-07 18:20:04 +01001243 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001244 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001245 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001246 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1247 ssl->transform_out->iv_enc,
1248 ssl->transform_out->ivlen,
1249 add_data, 13,
1250 enc_msg, enc_msglen,
1251 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001252 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001253 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001254 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001255 return( ret );
1256 }
1257
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001258 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001259 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001260 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001261 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001262 }
1263
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001264 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001265
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001266 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001269#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001270#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1271 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001272 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001273 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001274 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001275 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001276 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001277
Paul Bakker48916f92012-09-16 19:57:18 +00001278 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1279 ssl->transform_out->ivlen;
1280 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 padlen = 0;
1282
1283 for( i = 0; i <= padlen; i++ )
1284 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1285
1286 ssl->out_msglen += padlen + 1;
1287
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001288 enc_msglen = ssl->out_msglen;
1289 enc_msg = ssl->out_msg;
1290
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001291#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001292 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001293 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1294 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001295 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001296 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001297 {
1298 /*
1299 * Generate IV
1300 */
Paul Bakker48916f92012-09-16 19:57:18 +00001301 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1302 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001303 if( ret != 0 )
1304 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305
Paul Bakker92be97b2013-01-02 17:30:03 +01001306 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001307 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001308
1309 /*
1310 * Fix pointer positions and message length with added IV
1311 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001312 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001313 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001314 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001316#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001317
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001319 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001320 ssl->out_msglen, ssl->transform_out->ivlen,
1321 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
1323 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001324 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001326 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001327 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001328 ssl->transform_out->ivlen,
1329 enc_msg, enc_msglen,
1330 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001331 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001332 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001333 return( ret );
1334 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001335
Paul Bakkercca5b812013-08-31 17:40:26 +02001336 if( enc_msglen != olen )
1337 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001338 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001339 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001340 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001341
1342#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001343 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1344 {
1345 /*
1346 * Save IV in SSL3 and TLS1
1347 */
1348 memcpy( ssl->transform_out->iv_enc,
1349 ssl->transform_out->cipher_ctx_enc.iv,
1350 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001352#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001354 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001355#endif /* POLARSSL_CIPHER_MODE_CBC &&
1356 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001357 {
1358 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001359 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1363
1364 return( 0 );
1365}
1366
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001367#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001368
Paul Bakker5121ce52009-01-03 21:22:43 +00001369static int ssl_decrypt_buf( ssl_context *ssl )
1370{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001371 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001372 const cipher_mode_t mode = cipher_get_cipher_mode(
1373 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001374#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1375 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1376 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1377 size_t padlen = 0, correct = 1;
1378#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
1380 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1381
Paul Bakker48916f92012-09-16 19:57:18 +00001382 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 {
1384 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001385 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001386 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 }
1388
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001389#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001390 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001391 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001392 int ret;
1393 size_t olen = 0;
1394
Paul Bakker68884e32013-01-07 18:20:04 +01001395 padlen = 0;
1396
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001397 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001398 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001399 ssl->transform_in->ivlen,
1400 ssl->in_msg, ssl->in_msglen,
1401 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001402 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001403 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001404 return( ret );
1405 }
1406
1407 if( ssl->in_msglen != olen )
1408 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001409 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001410 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 }
Paul Bakker68884e32013-01-07 18:20:04 +01001413 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001414#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001415#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1416 if( mode == POLARSSL_MODE_GCM ||
1417 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001418 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001419 int ret;
1420 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001421 unsigned char *dec_msg;
1422 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001423 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001424 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1425 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001426 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1427 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001428
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001429 if( ssl->in_msglen < explicit_iv_len + taglen )
1430 {
1431 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1432 "+ taglen (%d)", ssl->in_msglen,
1433 explicit_iv_len, taglen ) );
1434 return( POLARSSL_ERR_SSL_INVALID_MAC );
1435 }
1436 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1437
Paul Bakker68884e32013-01-07 18:20:04 +01001438 dec_msg = ssl->in_msg;
1439 dec_msg_result = ssl->in_msg;
1440 ssl->in_msglen = dec_msglen;
1441
1442 memcpy( add_data, ssl->in_ctr, 8 );
1443 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001444 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1445 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001446 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1447 add_data[12] = ssl->in_msglen & 0xFF;
1448
1449 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1450 add_data, 13 );
1451
1452 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1453 ssl->in_iv,
1454 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1455
1456 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1457 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001458 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001459
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001460 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001461 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001462 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001463 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1464 ssl->transform_in->iv_dec,
1465 ssl->transform_in->ivlen,
1466 add_data, 13,
1467 dec_msg, dec_msglen,
1468 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001469 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001470 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001471 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1472
1473 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1474 return( POLARSSL_ERR_SSL_INVALID_MAC );
1475
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001476 return( ret );
1477 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001478
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001479 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001480 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001481 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001482 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001483 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001486#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001487#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1488 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001489 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 {
Paul Bakker45829992013-01-03 14:52:21 +01001491 /*
1492 * Decrypt and check the padding
1493 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001494 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001495 unsigned char *dec_msg;
1496 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001497 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001498 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001499 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001500
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 /*
Paul Bakker45829992013-01-03 14:52:21 +01001502 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001503 */
Paul Bakker48916f92012-09-16 19:57:18 +00001504 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 {
1506 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001507 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001508 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 }
1510
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001511#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001512 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1513 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001514#endif
Paul Bakker45829992013-01-03 14:52:21 +01001515
1516 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1517 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1518 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001519 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1520 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1521 ssl->transform_in->ivlen,
1522 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001523 return( POLARSSL_ERR_SSL_INVALID_MAC );
1524 }
1525
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001526 dec_msglen = ssl->in_msglen;
1527 dec_msg = ssl->in_msg;
1528 dec_msg_result = ssl->in_msg;
1529
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001530#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001531 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001532 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001533 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001534 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001535 {
Paul Bakker48916f92012-09-16 19:57:18 +00001536 dec_msglen -= ssl->transform_in->ivlen;
1537 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001538
Paul Bakker48916f92012-09-16 19:57:18 +00001539 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001540 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001541 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001542#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001543
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001544 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001545 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001546 ssl->transform_in->ivlen,
1547 dec_msg, dec_msglen,
1548 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001549 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001550 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001551 return( ret );
1552 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001553
Paul Bakkercca5b812013-08-31 17:40:26 +02001554 if( dec_msglen != olen )
1555 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001556 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001557 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001558 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001559
1560#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1562 {
1563 /*
1564 * Save IV in SSL3 and TLS1
1565 */
1566 memcpy( ssl->transform_in->iv_dec,
1567 ssl->transform_in->cipher_ctx_dec.iv,
1568 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
1572 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001573
1574 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1575 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001576#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001577 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1578 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001579#endif
Paul Bakker45829992013-01-03 14:52:21 +01001580 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001581 correct = 0;
1582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001584#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1586 {
Paul Bakker48916f92012-09-16 19:57:18 +00001587 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001589#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1591 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001592 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001593#endif
Paul Bakker45829992013-01-03 14:52:21 +01001594 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 }
1596 }
1597 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001598#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001599#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1600 defined(POLARSSL_SSL_PROTO_TLS1_2)
1601 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 {
1603 /*
Paul Bakker45829992013-01-03 14:52:21 +01001604 * TLSv1+: always check the padding up to the first failure
1605 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001606 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001607 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001608 size_t padding_idx = ssl->in_msglen - padlen - 1;
1609
Paul Bakker956c9e02013-12-19 14:42:28 +01001610 /*
1611 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001612 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001613 *
Paul Bakker61885c72014-04-25 12:59:03 +02001614 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1615 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001616 *
1617 * In both cases we reset padding_idx to a safe value (0) to
1618 * prevent out-of-buffer reads.
1619 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001620 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001621 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1622 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001623
1624 padding_idx *= correct;
1625
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001626 for( i = 1; i <= 256; i++ )
1627 {
1628 real_count &= ( i <= padlen );
1629 pad_count += real_count *
1630 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1631 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001632
1633 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001634
Paul Bakkerd66f0702013-01-31 16:57:45 +01001635#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001636 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001637 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001638#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001639 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001641 else
1642#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1643 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001644 {
1645 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001646 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001647 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001648 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001649 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001650#endif /* POLARSSL_CIPHER_MODE_CBC &&
1651 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001652 {
1653 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001654 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
1657 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1658 ssl->in_msg, ssl->in_msglen );
1659
1660 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001661 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1664 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1665 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001666 if( mode != POLARSSL_MODE_GCM &&
1667 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001669 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1670
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001671 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001673 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1674 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001678#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1680 {
1681 ssl_mac( &ssl->transform_in->md_ctx_dec,
1682 ssl->transform_in->mac_dec,
1683 ssl->in_msg, ssl->in_msglen,
1684 ssl->in_ctr, ssl->in_msgtype );
1685 }
1686 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001687#endif /* POLARSSL_SSL_PROTO_SSL3 */
1688#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001689 defined(POLARSSL_SSL_PROTO_TLS1_2)
1690 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1691 {
1692 /*
1693 * Process MAC and always update for padlen afterwards to make
1694 * total time independent of padlen
1695 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001696 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001697 *
1698 * Known timing attacks:
1699 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1700 *
1701 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1702 * correctly. (We round down instead of up, so -56 is the correct
1703 * value for our calculations instead of -55)
1704 */
1705 size_t j, extra_run = 0;
1706 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1707 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001708
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001709 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001710
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001711 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1712 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1713 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001714 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1715 ssl->in_msglen );
1716 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1717 ssl->in_msg + ssl->in_msglen );
1718 for( j = 0; j < extra_run; j++ )
1719 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001720
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001721 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1722 }
1723 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001724#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 POLARSSL_SSL_PROTO_TLS1_2 */
1726 {
1727 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001728 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001729 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001731 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1732 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1733 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001735 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001736 ssl->transform_in->maclen ) != 0 )
1737 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001738#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001739 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001740#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001741 correct = 0;
1742 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 /*
1745 * Finally check the correct flag
1746 */
1747 if( correct == 0 )
1748 return( POLARSSL_ERR_SSL_INVALID_MAC );
1749 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001750#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
1752 if( ssl->in_msglen == 0 )
1753 {
1754 ssl->nb_zero++;
1755
1756 /*
1757 * Three or more empty messages may be a DoS attack
1758 * (excessive CPU consumption).
1759 */
1760 if( ssl->nb_zero > 3 )
1761 {
1762 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1763 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001764 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 }
1766 }
1767 else
1768 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001769
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001770#if defined(POLARSSL_SSL_PROTO_DTLS)
1771 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001772 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001773 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001774 }
1775 else
1776#endif
1777 {
1778 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1779 if( ++ssl->in_ctr[i - 1] != 0 )
1780 break;
1781
1782 /* The loop goes to its end iff the counter is wrapping */
1783 if( i == ssl_ep_len( ssl ) )
1784 {
1785 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1786 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1787 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001788 }
1789
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1791
1792 return( 0 );
1793}
1794
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795#if defined(POLARSSL_ZLIB_SUPPORT)
1796/*
1797 * Compression/decompression functions
1798 */
1799static int ssl_compress_buf( ssl_context *ssl )
1800{
1801 int ret;
1802 unsigned char *msg_post = ssl->out_msg;
1803 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001804 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001805
1806 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1807
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001808 if( len_pre == 0 )
1809 return( 0 );
1810
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811 memcpy( msg_pre, ssl->out_msg, len_pre );
1812
1813 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1814 ssl->out_msglen ) );
1815
1816 SSL_DEBUG_BUF( 4, "before compression: output payload",
1817 ssl->out_msg, ssl->out_msglen );
1818
Paul Bakker48916f92012-09-16 19:57:18 +00001819 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1820 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1821 ssl->transform_out->ctx_deflate.next_out = msg_post;
1822 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001823
Paul Bakker48916f92012-09-16 19:57:18 +00001824 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001825 if( ret != Z_OK )
1826 {
1827 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1828 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1829 }
1830
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001831 ssl->out_msglen = SSL_BUFFER_LEN -
1832 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001833
Paul Bakker2770fbd2012-07-03 13:30:23 +00001834 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1835 ssl->out_msglen ) );
1836
1837 SSL_DEBUG_BUF( 4, "after compression: output payload",
1838 ssl->out_msg, ssl->out_msglen );
1839
1840 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1841
1842 return( 0 );
1843}
1844
1845static int ssl_decompress_buf( ssl_context *ssl )
1846{
1847 int ret;
1848 unsigned char *msg_post = ssl->in_msg;
1849 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001850 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001851
1852 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1853
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001854 if( len_pre == 0 )
1855 return( 0 );
1856
Paul Bakker2770fbd2012-07-03 13:30:23 +00001857 memcpy( msg_pre, ssl->in_msg, len_pre );
1858
1859 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1860 ssl->in_msglen ) );
1861
1862 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1863 ssl->in_msg, ssl->in_msglen );
1864
Paul Bakker48916f92012-09-16 19:57:18 +00001865 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1866 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1867 ssl->transform_in->ctx_inflate.next_out = msg_post;
1868 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001869
Paul Bakker48916f92012-09-16 19:57:18 +00001870 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001871 if( ret != Z_OK )
1872 {
1873 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1874 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1875 }
1876
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001877 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1878 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001879
Paul Bakker2770fbd2012-07-03 13:30:23 +00001880 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1881 ssl->in_msglen ) );
1882
1883 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1884 ssl->in_msg, ssl->in_msglen );
1885
1886 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1887
1888 return( 0 );
1889}
1890#endif /* POLARSSL_ZLIB_SUPPORT */
1891
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001892#if defined(POLARSSL_SSL_SRV_C)
1893static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02001894
1895#if defined(POLARSSL_SSL_PROTO_DTLS)
1896static int ssl_resend_hello_request( ssl_context *ssl )
1897{
1898 /* If renegotiation is not enforced, retransmit until we would reach max
1899 * timeout if we were using the usual handshake doubling scheme */
1900 if( ssl->renego_max_records < 0 )
1901 {
1902 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
1903 unsigned char doublings = 1;
1904
1905 while( ratio != 0 )
1906 {
1907 ++doublings;
1908 ratio >>= 1;
1909 }
1910
1911 if( ++ssl->renego_records_seen > doublings )
1912 {
1913 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
1914 return( 0 );
1915 }
1916 }
1917
1918 return( ssl_write_hello_request( ssl ) );
1919}
1920#endif
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001921#endif
1922
Paul Bakker5121ce52009-01-03 21:22:43 +00001923/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001924 * Fill the input message buffer by appending data to it.
1925 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001926 *
1927 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1928 * available (from this read and/or a previous one). Otherwise, an error code
1929 * is returned (possibly EOF or WANT_READ).
1930 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001931 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1932 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1933 * since we always read a whole datagram at once.
1934 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001935 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001936 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 */
Paul Bakker23986e52011-04-24 08:57:21 +00001938int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001939{
Paul Bakker23986e52011-04-24 08:57:21 +00001940 int ret;
1941 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
1943 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1944
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001945 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1946 {
1947 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1948 "or ssl_set_bio_timeout()" ) );
1949 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1950 }
1951
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001952 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001953 {
1954 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1955 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1956 }
1957
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001958#if defined(POLARSSL_SSL_PROTO_DTLS)
1959 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001961 uint32_t timeout;
1962
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001963 /*
1964 * The point is, we need to always read a full datagram at once, so we
1965 * sometimes read more then requested, and handle the additional data.
1966 * It could be the rest of the current record (while fetching the
1967 * header) and/or some other records in the same datagram.
1968 */
1969
1970 /*
1971 * Move to the next record in the already read datagram if applicable
1972 */
1973 if( ssl->next_record_offset != 0 )
1974 {
1975 if( ssl->in_left < ssl->next_record_offset )
1976 {
1977 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1978 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1979 }
1980
1981 ssl->in_left -= ssl->next_record_offset;
1982
1983 if( ssl->in_left != 0 )
1984 {
1985 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1986 ssl->next_record_offset ) );
1987 memmove( ssl->in_hdr,
1988 ssl->in_hdr + ssl->next_record_offset,
1989 ssl->in_left );
1990 }
1991
1992 ssl->next_record_offset = 0;
1993 }
1994
Paul Bakker5121ce52009-01-03 21:22:43 +00001995 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1996 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001997
1998 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001999 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002000 */
2001 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002002 {
2003 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002004 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002005 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002006
2007 /*
2008 * A record can't be split accross datagrams. If we need to read but
2009 * are not at the beginning of a new record, the caller did something
2010 * wrong.
2011 */
2012 if( ssl->in_left != 0 )
2013 {
2014 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2015 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2016 }
2017
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002018 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002019
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002020 /*
2021 * Don't even try to read if time's out already.
2022 * This avoids by-passing the timer when repeatedly receiving messages
2023 * that will end up being dropped.
2024 */
2025 if( ssl_check_timer( ssl ) != 0 )
2026 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002027 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002028 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002029 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2030
2031 if( ssl->state != SSL_HANDSHAKE_OVER )
2032 timeout = ssl->handshake->retransmit_timeout;
2033 else
2034 timeout = ssl->read_timeout;
2035
2036 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2037
2038 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2039 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2040 timeout );
2041 else
2042 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2043
2044 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2045
2046 if( ret == 0 )
2047 return( POLARSSL_ERR_SSL_CONN_EOF );
2048 }
2049
2050 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2051 {
2052 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2053 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002054
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002055 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002056 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002057 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2058 {
2059 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2060 return( POLARSSL_ERR_NET_TIMEOUT );
2061 }
2062
2063 if( ( ret = ssl_resend( ssl ) ) != 0 )
2064 {
2065 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2066 return( ret );
2067 }
2068
2069 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002070 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002071#if defined(POLARSSL_SSL_SRV_C)
2072 else if( ssl->endpoint == SSL_IS_SERVER &&
2073 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
2074 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002075 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002076 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002077 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002078 return( ret );
2079 }
2080
2081 return( POLARSSL_ERR_NET_WANT_READ );
2082 }
2083#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002084 }
2085
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 if( ret < 0 )
2087 return( ret );
2088
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002089 ssl->in_left = ret;
2090 }
2091 else
2092#endif
2093 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002094 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2095 ssl->in_left, nb_want ) );
2096
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002097 while( ssl->in_left < nb_want )
2098 {
2099 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002100 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002101
2102 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2103 ssl->in_left, nb_want ) );
2104 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2105
2106 if( ret == 0 )
2107 return( POLARSSL_ERR_SSL_CONN_EOF );
2108
2109 if( ret < 0 )
2110 return( ret );
2111
2112 ssl->in_left += ret;
2113 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
2115
2116 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2117
2118 return( 0 );
2119}
2120
2121/*
2122 * Flush any data not yet written
2123 */
2124int ssl_flush_output( ssl_context *ssl )
2125{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002126 int ret;
2127 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002128
2129 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2130
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002131 if( ssl->f_send == NULL )
2132 {
2133 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2134 "or ssl_set_bio_timeout()" ) );
2135 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2136 }
2137
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002138 /* Avoid incrementing counter if data is flushed */
2139 if( ssl->out_left == 0 )
2140 {
2141 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2142 return( 0 );
2143 }
2144
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 while( ssl->out_left > 0 )
2146 {
2147 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002148 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002149
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002150 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2151 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002152 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002153
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2155
2156 if( ret <= 0 )
2157 return( ret );
2158
2159 ssl->out_left -= ret;
2160 }
2161
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002162 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002163 if( ++ssl->out_ctr[i - 1] != 0 )
2164 break;
2165
2166 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002167 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002168 {
2169 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2170 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2171 }
2172
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2174
2175 return( 0 );
2176}
2177
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002178/*
2179 * Functions to handle the DTLS retransmission state machine
2180 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002181#if defined(POLARSSL_SSL_PROTO_DTLS)
2182/*
2183 * Append current handshake message to current outgoing flight
2184 */
2185static int ssl_flight_append( ssl_context *ssl )
2186{
2187 ssl_flight_item *msg;
2188
2189 /* Allocate space for current message */
2190 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2191 {
2192 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2193 sizeof( ssl_flight_item ) ) );
2194 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2195 }
2196
2197 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2198 {
2199 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2200 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2201 }
2202
2203 /* Copy current handshake message with headers */
2204 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2205 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002206 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002207 msg->next = NULL;
2208
2209 /* Append to the current flight */
2210 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002211 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002212 else
2213 {
2214 ssl_flight_item *cur = ssl->handshake->flight;
2215 while( cur->next != NULL )
2216 cur = cur->next;
2217 cur->next = msg;
2218 }
2219
2220 return( 0 );
2221}
2222
2223/*
2224 * Free the current flight of handshake messages
2225 */
2226static void ssl_flight_free( ssl_flight_item *flight )
2227{
2228 ssl_flight_item *cur = flight;
2229 ssl_flight_item *next;
2230
2231 while( cur != NULL )
2232 {
2233 next = cur->next;
2234
2235 polarssl_free( cur->p );
2236 polarssl_free( cur );
2237
2238 cur = next;
2239 }
2240}
2241
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002242#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2243static void ssl_dtls_replay_reset( ssl_context *ssl );
2244#endif
2245
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002246/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002247 * Swap transform_out and out_ctr with the alternative ones
2248 */
2249static void ssl_swap_epochs( ssl_context *ssl )
2250{
2251 ssl_transform *tmp_transform;
2252 unsigned char tmp_out_ctr[8];
2253
2254 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2255 {
2256 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2257 return;
2258 }
2259
2260 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2261
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002262 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002263 tmp_transform = ssl->transform_out;
2264 ssl->transform_out = ssl->handshake->alt_transform_out;
2265 ssl->handshake->alt_transform_out = tmp_transform;
2266
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002267 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002268 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2269 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2270 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002271
2272 /* Adjust to the newly activated transform */
2273 if( ssl->transform_out != NULL &&
2274 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2275 {
2276 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2277 ssl->transform_out->fixed_ivlen;
2278 }
2279 else
2280 ssl->out_msg = ssl->out_iv;
2281
2282#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2283 if( ssl_hw_record_activate != NULL )
2284 {
2285 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2286 {
2287 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2288 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2289 }
2290 }
2291#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002292}
2293
2294/*
2295 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002296 *
2297 * Need to remember the current message in case flush_output returns
2298 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002299 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002300 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002301int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002302{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002303 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002304
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002305 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2306 {
2307 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2308
2309 ssl->handshake->cur_msg = ssl->handshake->flight;
2310 ssl_swap_epochs( ssl );
2311
2312 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2313 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002314
2315 while( ssl->handshake->cur_msg != NULL )
2316 {
2317 int ret;
2318 ssl_flight_item *cur = ssl->handshake->cur_msg;
2319
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002320 /* Swap epochs before sending Finished: we can't do it after
2321 * sending ChangeCipherSpec, in case write returns WANT_READ.
2322 * Must be done before copying, may change out_msg pointer */
2323 if( cur->type == SSL_MSG_HANDSHAKE &&
2324 cur->p[0] == SSL_HS_FINISHED )
2325 {
2326 ssl_swap_epochs( ssl );
2327 }
2328
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002329 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002330 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002331 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002332
2333 ssl->handshake->cur_msg = cur->next;
2334
2335 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2336
2337 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2338 {
2339 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2340 return( ret );
2341 }
2342 }
2343
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002344 if( ssl->state == SSL_HANDSHAKE_OVER )
2345 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2346 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002347 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002348 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002349 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2350 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002351
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002352 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002353
2354 return( 0 );
2355}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002356
2357/*
2358 * To be called when the last message of an incoming flight is received.
2359 */
2360void ssl_recv_flight_completed( ssl_context *ssl )
2361{
2362 /* We won't need to resend that one any more */
2363 ssl_flight_free( ssl->handshake->flight );
2364 ssl->handshake->flight = NULL;
2365 ssl->handshake->cur_msg = NULL;
2366
2367 /* The next incoming flight will start with this msg_seq */
2368 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2369
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002370 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002371 ssl_set_timer( ssl, 0 );
2372
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002373 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2374 ssl->in_msg[0] == SSL_HS_FINISHED )
2375 {
2376 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2377 }
2378 else
2379 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2380}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002381
2382/*
2383 * To be called when the last message of an outgoing flight is send.
2384 */
2385void ssl_send_flight_completed( ssl_context *ssl )
2386{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002387 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002388 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002389
2390 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2391 ssl->in_msg[0] == SSL_HS_FINISHED )
2392 {
2393 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2394 }
2395 else
2396 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2397}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002398#endif /* POLARSSL_SSL_PROTO_DTLS */
2399
Paul Bakker5121ce52009-01-03 21:22:43 +00002400/*
2401 * Record layer functions
2402 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002403
2404/*
2405 * Write current record.
2406 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2407 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002408int ssl_write_record( ssl_context *ssl )
2409{
Paul Bakker05ef8352012-05-08 09:17:57 +00002410 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002411 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
2413 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2414
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002415#if defined(POLARSSL_SSL_PROTO_DTLS)
2416 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2417 ssl->handshake != NULL &&
2418 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2419 {
2420 ; /* Skip special handshake treatment when resending */
2421 }
2422 else
2423#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2425 {
2426 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2427 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2428 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2429
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002430 /*
2431 * DTLS has additional fields in the Handshake layer,
2432 * between the length field and the actual payload:
2433 * uint16 message_seq;
2434 * uint24 fragment_offset;
2435 * uint24 fragment_length;
2436 */
2437#if defined(POLARSSL_SSL_PROTO_DTLS)
2438 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2439 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002440 /* Make room for the additional DTLS fields */
2441 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002442 ssl->out_msglen += 8;
2443 len += 8;
2444
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002445 /* Write message_seq and update it, except for HelloRequest */
2446 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2447 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002448 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2449 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2450 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002451 }
2452 else
2453 {
2454 ssl->out_msg[4] = 0;
2455 ssl->out_msg[5] = 0;
2456 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002457
2458 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2459 memset( ssl->out_msg + 6, 0x00, 3 );
2460 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002461 }
2462#endif /* POLARSSL_SSL_PROTO_DTLS */
2463
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002464 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2465 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 }
2467
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002468 /* Save handshake and CCS messages for resending */
2469#if defined(POLARSSL_SSL_PROTO_DTLS)
2470 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2471 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002472 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002473 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2474 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2475 {
2476 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2477 {
2478 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2479 return( ret );
2480 }
2481 }
2482#endif
2483
Paul Bakker2770fbd2012-07-03 13:30:23 +00002484#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002485 if( ssl->transform_out != NULL &&
2486 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002487 {
2488 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2489 {
2490 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2491 return( ret );
2492 }
2493
2494 len = ssl->out_msglen;
2495 }
2496#endif /*POLARSSL_ZLIB_SUPPORT */
2497
Paul Bakker05ef8352012-05-08 09:17:57 +00002498#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002499 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002501 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Paul Bakker05ef8352012-05-08 09:17:57 +00002503 ret = ssl_hw_record_write( ssl );
2504 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2505 {
2506 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002507 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002508 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002509
2510 if( ret == 0 )
2511 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002512 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002513#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002514 if( !done )
2515 {
2516 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002517 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2518 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002519
2520 ssl->out_len[0] = (unsigned char)( len >> 8 );
2521 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002522
Paul Bakker48916f92012-09-16 19:57:18 +00002523 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002524 {
2525 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2526 {
2527 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2528 return( ret );
2529 }
2530
2531 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002532 ssl->out_len[0] = (unsigned char)( len >> 8 );
2533 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002534 }
2535
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002536 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002537
2538 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2539 "version = [%d:%d], msglen = %d",
2540 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002541 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002542
2543 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002544 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 }
2546
Paul Bakker5121ce52009-01-03 21:22:43 +00002547 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2548 {
2549 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2550 return( ret );
2551 }
2552
2553 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2554
2555 return( 0 );
2556}
2557
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002558#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002559/*
2560 * Mark bits in bitmask (used for DTLS HS reassembly)
2561 */
2562static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2563{
2564 unsigned int start_bits, end_bits;
2565
2566 start_bits = 8 - ( offset % 8 );
2567 if( start_bits != 8 )
2568 {
2569 size_t first_byte_idx = offset / 8;
2570
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002571 /* Special case */
2572 if( len <= start_bits )
2573 {
2574 for( ; len != 0; len-- )
2575 mask[first_byte_idx] |= 1 << ( start_bits - len );
2576
2577 /* Avoid potential issues with offset or len becoming invalid */
2578 return;
2579 }
2580
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002581 offset += start_bits; /* Now offset % 8 == 0 */
2582 len -= start_bits;
2583
2584 for( ; start_bits != 0; start_bits-- )
2585 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2586 }
2587
2588 end_bits = len % 8;
2589 if( end_bits != 0 )
2590 {
2591 size_t last_byte_idx = ( offset + len ) / 8;
2592
2593 len -= end_bits; /* Now len % 8 == 0 */
2594
2595 for( ; end_bits != 0; end_bits-- )
2596 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2597 }
2598
2599 memset( mask + offset / 8, 0xFF, len / 8 );
2600}
2601
2602/*
2603 * Check that bitmask is full
2604 */
2605static int ssl_bitmask_check( unsigned char *mask, size_t len )
2606{
2607 size_t i;
2608
2609 for( i = 0; i < len / 8; i++ )
2610 if( mask[i] != 0xFF )
2611 return( -1 );
2612
2613 for( i = 0; i < len % 8; i++ )
2614 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2615 return( -1 );
2616
2617 return( 0 );
2618}
2619
2620/*
2621 * Reassemble fragmented DTLS handshake messages.
2622 *
2623 * Use a temporary buffer for reassembly, divided in two parts:
2624 * - the first holds the reassembled message (including handshake header),
2625 * - the second holds a bitmask indicating which parts of the message
2626 * (excluding headers) have been received so far.
2627 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002628static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2629{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002630 unsigned char *msg, *bitmask;
2631 size_t frag_len, frag_off;
2632 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2633
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002634 if( ssl->handshake == NULL )
2635 {
2636 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2637 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2638 }
2639
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002640 /*
2641 * For first fragment, check size and allocate buffer
2642 */
2643 if( ssl->handshake->hs_msg == NULL )
2644 {
2645 size_t alloc_len;
2646
2647 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2648 msg_len ) );
2649
2650 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2651 {
2652 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2653 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2654 }
2655
2656 /* The bitmask needs one bit per byte of message excluding header */
2657 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2658
2659 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2660 if( ssl->handshake->hs_msg == NULL )
2661 {
2662 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2663 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2664 }
2665
2666 memset( ssl->handshake->hs_msg, 0, alloc_len );
2667
2668 /* Prepare final header: copy msg_type, length and message_seq,
2669 * then add standardised fragment_offset and fragment_length */
2670 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2671 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2672 memcpy( ssl->handshake->hs_msg + 9,
2673 ssl->handshake->hs_msg + 1, 3 );
2674 }
2675 else
2676 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002677 /* Make sure msg_type and length are consistent */
2678 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002679 {
2680 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2681 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2682 }
2683 }
2684
2685 msg = ssl->handshake->hs_msg + 12;
2686 bitmask = msg + msg_len;
2687
2688 /*
2689 * Check and copy current fragment
2690 */
2691 frag_off = ( ssl->in_msg[6] << 16 ) |
2692 ( ssl->in_msg[7] << 8 ) |
2693 ssl->in_msg[8];
2694 frag_len = ( ssl->in_msg[9] << 16 ) |
2695 ( ssl->in_msg[10] << 8 ) |
2696 ssl->in_msg[11];
2697
2698 if( frag_off + frag_len > msg_len )
2699 {
2700 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2701 frag_off, frag_len, msg_len ) );
2702 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2703 }
2704
2705 if( frag_len + 12 > ssl->in_msglen )
2706 {
2707 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2708 frag_len, ssl->in_msglen ) );
2709 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2710 }
2711
2712 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2713 frag_off, frag_len ) );
2714
2715 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2716 ssl_bitmask_set( bitmask, frag_off, frag_len );
2717
2718 /*
2719 * Do we have the complete message by now?
2720 * If yes, finalize it, else ask to read the next record.
2721 */
2722 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2723 {
2724 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2725 return( POLARSSL_ERR_NET_WANT_READ );
2726 }
2727
2728 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2729
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002730 if( frag_len + 12 < ssl->in_msglen )
2731 {
2732 /*
2733 * We'got more handshake messages in the same record.
2734 * This case is not handled now because no know implementation does
2735 * that and it's hard to test, so we prefer to fail cleanly for now.
2736 */
2737 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2738 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2739 }
2740
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002741 if( ssl->in_left > ssl->next_record_offset )
2742 {
2743 /*
2744 * We've got more data in the buffer after the current record,
2745 * that we don't want to overwrite. Move it before writing the
2746 * reassembled message, and adjust in_left and next_record_offset.
2747 */
2748 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2749 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2750 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2751
2752 /* First compute and check new lengths */
2753 ssl->next_record_offset = new_remain - ssl->in_hdr;
2754 ssl->in_left = ssl->next_record_offset + remain_len;
2755
2756 if( ssl->in_left > SSL_BUFFER_LEN -
2757 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2758 {
2759 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2760 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2761 }
2762
2763 memmove( new_remain, cur_remain, remain_len );
2764 }
2765
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002766 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2767
2768 polarssl_free( ssl->handshake->hs_msg );
2769 ssl->handshake->hs_msg = NULL;
2770
2771 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2772 ssl->in_msg, ssl->in_hslen );
2773
2774 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002775}
2776#endif /* POLARSSL_SSL_PROTO_DTLS */
2777
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002778static int ssl_prepare_handshake_record( ssl_context *ssl )
2779{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002780 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2781 {
2782 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2783 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002784 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002785 }
2786
2787 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2788 ( ssl->in_msg[1] << 16 ) |
2789 ( ssl->in_msg[2] << 8 ) |
2790 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002791
2792 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2793 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002794 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002795
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002796#if defined(POLARSSL_SSL_PROTO_DTLS)
2797 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002798 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002799 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002800 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002801
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002802 /* ssl->handshake is NULL when receiving ClientHello for renego */
2803 if( ssl->handshake != NULL &&
2804 recv_msg_seq != ssl->handshake->in_msg_seq )
2805 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002806 /* Retransmit only on last message from previous flight, to avoid
2807 * too many retransmissions.
2808 * Besides, No sane server ever retransmits HelloVerifyRequest */
2809 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002810 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002811 {
2812 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2813 "message_seq = %d, start_of_flight = %d",
2814 recv_msg_seq,
2815 ssl->handshake->in_flight_start_seq ) );
2816
2817 if( ( ret = ssl_resend( ssl ) ) != 0 )
2818 {
2819 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2820 return( ret );
2821 }
2822 }
2823 else
2824 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002825 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002826 "message_seq = %d, expected = %d",
2827 recv_msg_seq,
2828 ssl->handshake->in_msg_seq ) );
2829 }
2830
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002831 return( POLARSSL_ERR_NET_WANT_READ );
2832 }
2833 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002834
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002835 /* Reassemble if current message is fragmented or reassembly is
2836 * already in progress */
2837 if( ssl->in_msglen < ssl->in_hslen ||
2838 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2839 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2840 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002841 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002842 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2843
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002844 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2845 {
2846 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2847 return( ret );
2848 }
2849 }
2850 }
2851 else
2852#endif /* POLARSSL_SSL_PROTO_DTLS */
2853 /* With TLS we don't handle fragmentation (for now) */
2854 if( ssl->in_msglen < ssl->in_hslen )
2855 {
2856 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002857 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002858 }
2859
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002860 if( ssl->state != SSL_HANDSHAKE_OVER )
2861 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2862
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002863 /* Handshake message is complete, increment counter */
2864#if defined(POLARSSL_SSL_PROTO_DTLS)
2865 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2866 ssl->handshake != NULL )
2867 {
2868 ssl->handshake->in_msg_seq++;
2869 }
2870#endif
2871
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002872 return( 0 );
2873}
2874
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002875/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002876 * DTLS anti-replay: RFC 6347 4.1.2.6
2877 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002878 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2879 * Bit n is set iff record number in_window_top - n has been seen.
2880 *
2881 * Usually, in_window_top is the last record number seen and the lsb of
2882 * in_window is set. The only exception is the initial state (record number 0
2883 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002884 */
2885#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2886static void ssl_dtls_replay_reset( ssl_context *ssl )
2887{
2888 ssl->in_window_top = 0;
2889 ssl->in_window = 0;
2890}
2891
2892static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2893{
2894 return( ( (uint64_t) buf[0] << 40 ) |
2895 ( (uint64_t) buf[1] << 32 ) |
2896 ( (uint64_t) buf[2] << 24 ) |
2897 ( (uint64_t) buf[3] << 16 ) |
2898 ( (uint64_t) buf[4] << 8 ) |
2899 ( (uint64_t) buf[5] ) );
2900}
2901
2902/*
2903 * Return 0 if sequence number is acceptable, -1 otherwise
2904 */
2905int ssl_dtls_replay_check( ssl_context *ssl )
2906{
2907 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2908 uint64_t bit;
2909
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002910 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2911 return( 0 );
2912
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002913 if( rec_seqnum > ssl->in_window_top )
2914 return( 0 );
2915
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002916 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002917
2918 if( bit >= 64 )
2919 return( -1 );
2920
2921 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2922 return( -1 );
2923
2924 return( 0 );
2925}
2926
2927/*
2928 * Update replay window on new validated record
2929 */
2930void ssl_dtls_replay_update( ssl_context *ssl )
2931{
2932 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2933
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002934 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2935 return;
2936
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002937 if( rec_seqnum > ssl->in_window_top )
2938 {
2939 /* Update window_top and the contents of the window */
2940 uint64_t shift = rec_seqnum - ssl->in_window_top;
2941
2942 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002943 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002944 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002945 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002946 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002947 ssl->in_window |= 1;
2948 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002949
2950 ssl->in_window_top = rec_seqnum;
2951 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002952 else
2953 {
2954 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002955 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002956
2957 if( bit < 64 ) /* Always true, but be extra sure */
2958 ssl->in_window |= (uint64_t) 1 << bit;
2959 }
2960}
2961#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2962
2963/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002964 * ContentType type;
2965 * ProtocolVersion version;
2966 * uint16 epoch; // DTLS only
2967 * uint48 sequence_number; // DTLS only
2968 * uint16 length;
2969 */
2970static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002971{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002972 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002973 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002975 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2976
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002978 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002979 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
2981 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2982 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002983 ssl->in_msgtype,
2984 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002986 /* Check record type */
2987 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2988 ssl->in_msgtype != SSL_MSG_ALERT &&
2989 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2990 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2991 {
2992 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002993
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002994 if( ( ret = ssl_send_alert_message( ssl,
2995 SSL_ALERT_LEVEL_FATAL,
2996 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2997 {
2998 return( ret );
2999 }
3000
3001 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3002 }
3003
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003004#if defined(POLARSSL_SSL_PROTO_DTLS)
3005 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3006 {
3007 /* Drop unexpected ChangeCipherSpec messages */
3008 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3009 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3010 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3011 {
3012 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3013 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3014 }
3015
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003016 /* Drop unexpected ApplicationData records,
3017 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003018 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003019 ssl->state != SSL_HANDSHAKE_OVER &&
3020 ! ( ssl->renegotiation == SSL_RENEGOTIATION &&
3021 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003022 {
3023 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3024 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3025 }
3026 }
3027#endif
3028
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003029 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003030 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 {
3032 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003033 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003034 }
3035
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003036 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 {
3038 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003039 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 }
3041
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003042 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003043#if defined(POLARSSL_SSL_PROTO_DTLS)
3044 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3045 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003046 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003047
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003048 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003049 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003050 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003051 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003052 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003053 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003054 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003055
3056#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3057 if( ssl_dtls_replay_check( ssl ) != 0 )
3058 {
3059 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3060 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3061 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003062#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003063 }
3064#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003065
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003066 /* Check length against the size of our buffer */
3067 if( ssl->in_msglen > SSL_BUFFER_LEN
3068 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003069 {
3070 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3071 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3072 }
3073
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003074 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003075 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003077 if( ssl->in_msglen < 1 ||
3078 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003079 {
3080 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003081 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 }
3083 }
3084 else
3085 {
Paul Bakker48916f92012-09-16 19:57:18 +00003086 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 {
3088 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003089 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 }
3091
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003092#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003094 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 {
3096 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003097 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003099#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003100#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3101 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 /*
3103 * TLS encrypted messages can have up to 256 bytes of padding
3104 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003105 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003106 ssl->in_msglen > ssl->transform_in->minlen +
3107 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003108 {
3109 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003110 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003111 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003112#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 }
3114
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003115 return( 0 );
3116}
Paul Bakker5121ce52009-01-03 21:22:43 +00003117
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003118/*
3119 * If applicable, decrypt (and decompress) record content
3120 */
3121static int ssl_prepare_record_content( ssl_context *ssl )
3122{
3123 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003124
Paul Bakker5121ce52009-01-03 21:22:43 +00003125 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003126 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003127
Paul Bakker05ef8352012-05-08 09:17:57 +00003128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003129 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003130 {
3131 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3132
3133 ret = ssl_hw_record_read( ssl );
3134 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3135 {
3136 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003137 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003138 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003139
3140 if( ret == 0 )
3141 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003142 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003143#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003144 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 {
3146 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3147 {
3148 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3149 return( ret );
3150 }
3151
3152 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3153 ssl->in_msg, ssl->in_msglen );
3154
3155 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3156 {
3157 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003158 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 }
3160 }
3161
Paul Bakker2770fbd2012-07-03 13:30:23 +00003162#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003163 if( ssl->transform_in != NULL &&
3164 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003165 {
3166 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3167 {
3168 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3169 return( ret );
3170 }
3171
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003172 // TODO: what's the purpose of these lines? is in_len used?
3173 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3174 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003175 }
3176#endif /* POLARSSL_ZLIB_SUPPORT */
3177
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003178#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003179 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3180 {
3181 ssl_dtls_replay_update( ssl );
3182 }
3183#endif
3184
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003185 return( 0 );
3186}
3187
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003188static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3189
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003190/*
3191 * Read a record.
3192 *
3193 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3194 * and continue reading until a valid record is found.
3195 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003196int ssl_read_record( ssl_context *ssl )
3197{
3198 int ret;
3199
3200 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3201
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003202 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003203 {
3204 /*
3205 * Get next Handshake message in the current record
3206 */
3207 ssl->in_msglen -= ssl->in_hslen;
3208
3209 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3210 ssl->in_msglen );
3211
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003212 SSL_DEBUG_BUF( 4, "remaining content in record",
3213 ssl->in_msg, ssl->in_msglen );
3214
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003215 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3216 return( ret );
3217
3218 return( 0 );
3219 }
3220
3221 ssl->in_hslen = 0;
3222
3223 /*
3224 * Read the record header and parse it
3225 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003226#if defined(POLARSSL_SSL_PROTO_DTLS)
3227read_record_header:
3228#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003229 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3230 {
3231 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3232 return( ret );
3233 }
3234
3235 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003236 {
3237#if defined(POLARSSL_SSL_PROTO_DTLS)
3238 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3239 {
3240 /* Ignore bad record and get next one; drop the whole datagram
3241 * since current header cannot be trusted to find the next record
3242 * in current datagram */
3243 ssl->next_record_offset = 0;
3244 ssl->in_left = 0;
3245
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003246 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003247 goto read_record_header;
3248 }
3249#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003250 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003251 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003252
3253 /*
3254 * Read and optionally decrypt the message contents
3255 */
3256 if( ( ret = ssl_fetch_input( ssl,
3257 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3258 {
3259 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3260 return( ret );
3261 }
3262
3263 /* Done reading this record, get ready for the next one */
3264#if defined(POLARSSL_SSL_PROTO_DTLS)
3265 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3266 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3267 else
3268#endif
3269 ssl->in_left = 0;
3270
3271 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003272 {
3273#if defined(POLARSSL_SSL_PROTO_DTLS)
3274 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3275 {
3276 /* Silently discard invalid records */
3277 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3278 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3279 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003280#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3281 if( ssl->badmac_limit != 0 &&
3282 ++ssl->badmac_seen >= ssl->badmac_limit )
3283 {
3284 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3285 return( POLARSSL_ERR_SSL_INVALID_MAC );
3286 }
3287#endif
3288
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003289 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003290 goto read_record_header;
3291 }
3292
3293 return( ret );
3294 }
3295 else
3296#endif
3297 {
3298 /* Error out (and send alert) on invalid records */
3299#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3300 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3301 {
3302 ssl_send_alert_message( ssl,
3303 SSL_ALERT_LEVEL_FATAL,
3304 SSL_ALERT_MSG_BAD_RECORD_MAC );
3305 }
3306#endif
3307 return( ret );
3308 }
3309 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003310
3311 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003312 * When we sent the last flight of the handshake, we MUST respond to a
3313 * retransmit of the peer's previous flight with a retransmit. (In
3314 * practice, only the Finished message will make it, other messages
3315 * including CCS use the old transform so they're dropped as invalid.)
3316 *
3317 * If the record we received is not a handshake message, however, it
3318 * means the peer received our last flight so we can clean up
3319 * handshake info.
3320 *
3321 * This check needs to be done before prepare_handshake() due to an edge
3322 * case: if the client immediately requests renegotiation, this
3323 * finishes the current handshake first, avoiding the new ClientHello
3324 * being mistaken for an ancient message in the current handshake.
3325 */
3326#if defined(POLARSSL_SSL_PROTO_DTLS)
3327 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3328 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003329 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003330 {
3331 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3332 ssl->in_msg[0] == SSL_HS_FINISHED )
3333 {
3334 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3335
3336 if( ( ret = ssl_resend( ssl ) ) != 0 )
3337 {
3338 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3339 return( ret );
3340 }
3341
3342 return( POLARSSL_ERR_NET_WANT_READ );
3343 }
3344 else
3345 {
3346 ssl_handshake_wrapup_free_hs_transform( ssl );
3347 }
3348 }
3349#endif
3350
3351 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003352 * Handle particular types of records
3353 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003354 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3355 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003356 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3357 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 }
3359
3360 if( ssl->in_msgtype == SSL_MSG_ALERT )
3361 {
3362 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3363 ssl->in_msg[0], ssl->in_msg[1] ) );
3364
3365 /*
3366 * Ignore non-fatal alerts, except close_notify
3367 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003368 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003370 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3371 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003372 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003373 }
3374
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003375 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3376 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 {
3378 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003379 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003380 }
3381 }
3382
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3384
3385 return( 0 );
3386}
3387
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003388int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3389{
3390 int ret;
3391
3392 if( ( ret = ssl_send_alert_message( ssl,
3393 SSL_ALERT_LEVEL_FATAL,
3394 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3395 {
3396 return( ret );
3397 }
3398
3399 return( 0 );
3400}
3401
Paul Bakker0a925182012-04-16 06:46:41 +00003402int ssl_send_alert_message( ssl_context *ssl,
3403 unsigned char level,
3404 unsigned char message )
3405{
3406 int ret;
3407
3408 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3409
3410 ssl->out_msgtype = SSL_MSG_ALERT;
3411 ssl->out_msglen = 2;
3412 ssl->out_msg[0] = level;
3413 ssl->out_msg[1] = message;
3414
3415 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3416 {
3417 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3418 return( ret );
3419 }
3420
3421 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3422
3423 return( 0 );
3424}
3425
Paul Bakker5121ce52009-01-03 21:22:43 +00003426/*
3427 * Handshake functions
3428 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003429#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3430 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3431 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3432 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3433 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3434 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3435 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003436int ssl_write_certificate( ssl_context *ssl )
3437{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003438 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003439
3440 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3441
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003442 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003443 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3444 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003445 {
3446 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3447 ssl->state++;
3448 return( 0 );
3449 }
3450
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003451 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003453}
3454
3455int ssl_parse_certificate( ssl_context *ssl )
3456{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003457 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3458
3459 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3460
3461 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003462 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3463 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003464 {
3465 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3466 ssl->state++;
3467 return( 0 );
3468 }
3469
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003470 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3471 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003472}
3473#else
3474int ssl_write_certificate( ssl_context *ssl )
3475{
3476 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3477 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003478 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003479 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3480
3481 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3482
3483 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003484 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3485 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003486 {
3487 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3488 ssl->state++;
3489 return( 0 );
3490 }
3491
Paul Bakker5121ce52009-01-03 21:22:43 +00003492 if( ssl->endpoint == SSL_IS_CLIENT )
3493 {
3494 if( ssl->client_auth == 0 )
3495 {
3496 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3497 ssl->state++;
3498 return( 0 );
3499 }
3500
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003501#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 /*
3503 * If using SSLv3 and got no cert, send an Alert message
3504 * (otherwise an empty Certificate message will be sent).
3505 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003506 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003507 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3508 {
3509 ssl->out_msglen = 2;
3510 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003511 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3512 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003513
3514 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3515 goto write_msg;
3516 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003517#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003518 }
3519 else /* SSL_IS_SERVER */
3520 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003521 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003522 {
3523 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003524 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003525 }
3526 }
3527
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003528 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
3530 /*
3531 * 0 . 0 handshake type
3532 * 1 . 3 handshake length
3533 * 4 . 6 length of all certs
3534 * 7 . 9 length of cert. 1
3535 * 10 . n-1 peer certificate
3536 * n . n+2 length of cert. 2
3537 * n+3 . ... upper level cert, etc.
3538 */
3539 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003540 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
Paul Bakker29087132010-03-21 21:03:34 +00003542 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003543 {
3544 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003545 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003546 {
3547 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3548 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003549 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003550 }
3551
3552 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3553 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3554 ssl->out_msg[i + 2] = (unsigned char)( n );
3555
3556 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3557 i += n; crt = crt->next;
3558 }
3559
3560 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3561 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3562 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3563
3564 ssl->out_msglen = i;
3565 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3566 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3567
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003568#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003569write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003571
3572 ssl->state++;
3573
3574 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3575 {
3576 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3577 return( ret );
3578 }
3579
3580 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3581
Paul Bakkered27a042013-04-18 22:46:23 +02003582 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003583}
3584
3585int ssl_parse_certificate( ssl_context *ssl )
3586{
Paul Bakkered27a042013-04-18 22:46:23 +02003587 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003588 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003589 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003590
3591 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3592
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003593 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003594 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3595 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003596 {
3597 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3598 ssl->state++;
3599 return( 0 );
3600 }
3601
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003603 ( ssl->authmode == SSL_VERIFY_NONE ||
3604 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003606 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3608 ssl->state++;
3609 return( 0 );
3610 }
3611
3612 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3613 {
3614 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3615 return( ret );
3616 }
3617
3618 ssl->state++;
3619
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003620#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003621 /*
3622 * Check if the client sent an empty certificate
3623 */
3624 if( ssl->endpoint == SSL_IS_SERVER &&
3625 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3626 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003627 if( ssl->in_msglen == 2 &&
3628 ssl->in_msgtype == SSL_MSG_ALERT &&
3629 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3630 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 {
3632 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3633
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003634 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3636 return( 0 );
3637 else
Paul Bakker40e46942009-01-03 21:51:57 +00003638 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 }
3640 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003641#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003642
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003643#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3644 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 if( ssl->endpoint == SSL_IS_SERVER &&
3646 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3647 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003648 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3650 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003651 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 {
3653 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3654
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003655 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003656 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003657 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 else
3659 return( 0 );
3660 }
3661 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003662#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3663 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003664
3665 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3666 {
3667 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003668 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003669 }
3670
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003671 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3672 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003673 {
3674 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003675 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 }
3677
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003678 i = ssl_hs_hdr_len( ssl );
3679
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 /*
3681 * Same message structure as in ssl_write_certificate()
3682 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003683 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003684
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003685 if( ssl->in_msg[i] != 0 ||
3686 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003687 {
3688 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003689 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003690 }
3691
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003692 /* In case we tried to reuse a session but it failed */
3693 if( ssl->session_negotiate->peer_cert != NULL )
3694 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003695 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003696 polarssl_free( ssl->session_negotiate->peer_cert );
3697 }
3698
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003699 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3700 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003701 {
3702 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003703 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003704 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 }
3706
Paul Bakkerb6b09562013-09-18 14:17:41 +02003707 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003708
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003709 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003710
3711 while( i < ssl->in_hslen )
3712 {
3713 if( ssl->in_msg[i] != 0 )
3714 {
3715 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003716 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 }
3718
3719 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3720 | (unsigned int) ssl->in_msg[i + 2];
3721 i += 3;
3722
3723 if( n < 128 || i + n > ssl->in_hslen )
3724 {
3725 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003726 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003727 }
3728
Paul Bakkerddf26b42013-09-18 13:46:23 +02003729 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3730 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 if( ret != 0 )
3732 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003733 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003734 return( ret );
3735 }
3736
3737 i += n;
3738 }
3739
Paul Bakker48916f92012-09-16 19:57:18 +00003740 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003741
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003742 /*
3743 * On client, make sure the server cert doesn't change during renego to
3744 * avoid "triple handshake" attack: https://secure-resumption.com/
3745 */
3746 if( ssl->endpoint == SSL_IS_CLIENT &&
3747 ssl->renegotiation == SSL_RENEGOTIATION )
3748 {
3749 if( ssl->session->peer_cert == NULL )
3750 {
3751 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3752 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3753 }
3754
3755 if( ssl->session->peer_cert->raw.len !=
3756 ssl->session_negotiate->peer_cert->raw.len ||
3757 memcmp( ssl->session->peer_cert->raw.p,
3758 ssl->session_negotiate->peer_cert->raw.p,
3759 ssl->session->peer_cert->raw.len ) != 0 )
3760 {
3761 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3762 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3763 }
3764 }
3765
Paul Bakker5121ce52009-01-03 21:22:43 +00003766 if( ssl->authmode != SSL_VERIFY_NONE )
3767 {
3768 if( ssl->ca_chain == NULL )
3769 {
3770 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003771 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003772 }
3773
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003774 /*
3775 * Main check: verify certificate
3776 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003777 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3778 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3779 &ssl->session_negotiate->verify_result,
3780 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003781
3782 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003783 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003784 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003785 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003786
3787 /*
3788 * Secondary checks: always done, but change 'ret' only if it was 0
3789 */
3790
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003791#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003792 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003793 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003794
3795 /* If certificate uses an EC key, make sure the curve is OK */
3796 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3797 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3798 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003799 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003800 if( ret == 0 )
3801 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003802 }
3803 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003804#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003805
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003806 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3807 ciphersuite_info,
3808 ! ssl->endpoint ) != 0 )
3809 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003810 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003811 if( ret == 0 )
3812 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3813 }
3814
Paul Bakker5121ce52009-01-03 21:22:43 +00003815 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3816 ret = 0;
3817 }
3818
3819 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3820
3821 return( ret );
3822}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003823#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3824 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3825 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3826 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3827 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3828 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3829 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003830
3831int ssl_write_change_cipher_spec( ssl_context *ssl )
3832{
3833 int ret;
3834
3835 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3836
3837 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3838 ssl->out_msglen = 1;
3839 ssl->out_msg[0] = 1;
3840
Paul Bakker5121ce52009-01-03 21:22:43 +00003841 ssl->state++;
3842
3843 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3844 {
3845 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3846 return( ret );
3847 }
3848
3849 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3850
3851 return( 0 );
3852}
3853
3854int ssl_parse_change_cipher_spec( ssl_context *ssl )
3855{
3856 int ret;
3857
3858 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3859
Paul Bakker5121ce52009-01-03 21:22:43 +00003860 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3861 {
3862 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3863 return( ret );
3864 }
3865
3866 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3867 {
3868 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003869 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 }
3871
3872 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3873 {
3874 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003875 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003876 }
3877
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003878 /*
3879 * Switch to our negotiated transform and session parameters for inbound
3880 * data.
3881 */
3882 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3883 ssl->transform_in = ssl->transform_negotiate;
3884 ssl->session_in = ssl->session_negotiate;
3885
3886#if defined(POLARSSL_SSL_PROTO_DTLS)
3887 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3888 {
3889#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3890 ssl_dtls_replay_reset( ssl );
3891#endif
3892
3893 /* Increment epoch */
3894 if( ++ssl->in_epoch == 0 )
3895 {
3896 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3897 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3898 }
3899 }
3900 else
3901#endif /* POLARSSL_SSL_PROTO_DTLS */
3902 memset( ssl->in_ctr, 0, 8 );
3903
3904 /*
3905 * Set the in_msg pointer to the correct location based on IV length
3906 */
3907 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3908 {
3909 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3910 ssl->transform_negotiate->fixed_ivlen;
3911 }
3912 else
3913 ssl->in_msg = ssl->in_iv;
3914
3915#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3916 if( ssl_hw_record_activate != NULL )
3917 {
3918 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3919 {
3920 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3921 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3922 }
3923 }
3924#endif
3925
Paul Bakker5121ce52009-01-03 21:22:43 +00003926 ssl->state++;
3927
3928 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3929
3930 return( 0 );
3931}
3932
Paul Bakker41c83d32013-03-20 14:39:14 +01003933void ssl_optimize_checksum( ssl_context *ssl,
3934 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003935{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003936 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003937
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003938#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3939 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003940 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003941 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003942 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003943#endif
3944#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3945#if defined(POLARSSL_SHA512_C)
3946 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3947 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3948 else
3949#endif
3950#if defined(POLARSSL_SHA256_C)
3951 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003952 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003953 else
3954#endif
3955#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003956 {
3957 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003958 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003959 }
Paul Bakker380da532012-04-18 16:10:25 +00003960}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003961
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003962void ssl_reset_checksum( ssl_context *ssl )
3963{
3964#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3965 defined(POLARSSL_SSL_PROTO_TLS1_1)
3966 md5_starts( &ssl->handshake->fin_md5 );
3967 sha1_starts( &ssl->handshake->fin_sha1 );
3968#endif
3969#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3970#if defined(POLARSSL_SHA256_C)
3971 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3972#endif
3973#if defined(POLARSSL_SHA512_C)
3974 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3975#endif
3976#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3977}
3978
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003979static void ssl_update_checksum_start( ssl_context *ssl,
3980 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003981{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003982#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3983 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003984 md5_update( &ssl->handshake->fin_md5 , buf, len );
3985 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003986#endif
3987#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3988#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003989 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003990#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003991#if defined(POLARSSL_SHA512_C)
3992 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003993#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003994#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003995}
3996
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003997#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3998 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003999static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4000 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004001{
Paul Bakker48916f92012-09-16 19:57:18 +00004002 md5_update( &ssl->handshake->fin_md5 , buf, len );
4003 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004004}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004005#endif
Paul Bakker380da532012-04-18 16:10:25 +00004006
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004007#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4008#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004009static void ssl_update_checksum_sha256( ssl_context *ssl,
4010 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004011{
Paul Bakker9e36f042013-06-30 14:34:05 +02004012 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004013}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004014#endif
Paul Bakker380da532012-04-18 16:10:25 +00004015
Paul Bakker9e36f042013-06-30 14:34:05 +02004016#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004017static void ssl_update_checksum_sha384( ssl_context *ssl,
4018 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004019{
Paul Bakker9e36f042013-06-30 14:34:05 +02004020 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004021}
Paul Bakker769075d2012-11-24 11:26:46 +01004022#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004023#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004024
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004025#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004026static void ssl_calc_finished_ssl(
4027 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004028{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004029 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004030 md5_context md5;
4031 sha1_context sha1;
4032
Paul Bakker5121ce52009-01-03 21:22:43 +00004033 unsigned char padbuf[48];
4034 unsigned char md5sum[16];
4035 unsigned char sha1sum[20];
4036
Paul Bakker48916f92012-09-16 19:57:18 +00004037 ssl_session *session = ssl->session_negotiate;
4038 if( !session )
4039 session = ssl->session;
4040
Paul Bakker1ef83d62012-04-11 12:09:53 +00004041 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4042
Paul Bakker48916f92012-09-16 19:57:18 +00004043 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4044 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004045
4046 /*
4047 * SSLv3:
4048 * hash =
4049 * MD5( master + pad2 +
4050 * MD5( handshake + sender + master + pad1 ) )
4051 * + SHA1( master + pad2 +
4052 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 */
4054
Paul Bakker90995b52013-06-24 19:20:35 +02004055#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004056 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004057 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004059
Paul Bakker90995b52013-06-24 19:20:35 +02004060#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004061 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004062 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004063#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004064
Paul Bakker3c2122f2013-06-24 19:03:14 +02004065 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4066 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004067
Paul Bakker1ef83d62012-04-11 12:09:53 +00004068 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004069
Paul Bakker3c2122f2013-06-24 19:03:14 +02004070 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004071 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004072 md5_update( &md5, padbuf, 48 );
4073 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004074
Paul Bakker3c2122f2013-06-24 19:03:14 +02004075 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004076 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004077 sha1_update( &sha1, padbuf, 40 );
4078 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004079
Paul Bakker1ef83d62012-04-11 12:09:53 +00004080 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004081
Paul Bakker1ef83d62012-04-11 12:09:53 +00004082 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004083 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004084 md5_update( &md5, padbuf, 48 );
4085 md5_update( &md5, md5sum, 16 );
4086 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004087
Paul Bakker1ef83d62012-04-11 12:09:53 +00004088 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004089 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004090 sha1_update( &sha1, padbuf , 40 );
4091 sha1_update( &sha1, sha1sum, 20 );
4092 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004093
Paul Bakker1ef83d62012-04-11 12:09:53 +00004094 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004095
Paul Bakker5b4af392014-06-26 12:09:34 +02004096 md5_free( &md5 );
4097 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004098
Paul Bakker34617722014-06-13 17:20:13 +02004099 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4100 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4101 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004102
4103 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4104}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004105#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004106
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004107#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004108static void ssl_calc_finished_tls(
4109 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004110{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004111 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004112 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004113 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004115 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004116
Paul Bakker48916f92012-09-16 19:57:18 +00004117 ssl_session *session = ssl->session_negotiate;
4118 if( !session )
4119 session = ssl->session;
4120
Paul Bakker1ef83d62012-04-11 12:09:53 +00004121 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004122
Paul Bakker48916f92012-09-16 19:57:18 +00004123 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4124 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004125
Paul Bakker1ef83d62012-04-11 12:09:53 +00004126 /*
4127 * TLSv1:
4128 * hash = PRF( master, finished_label,
4129 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4130 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004131
Paul Bakker90995b52013-06-24 19:20:35 +02004132#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004133 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4134 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004135#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004136
Paul Bakker90995b52013-06-24 19:20:35 +02004137#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004138 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4139 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004140#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004141
4142 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004143 ? "client finished"
4144 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004145
4146 md5_finish( &md5, padbuf );
4147 sha1_finish( &sha1, padbuf + 16 );
4148
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004149 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004150 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004151
4152 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4153
Paul Bakker5b4af392014-06-26 12:09:34 +02004154 md5_free( &md5 );
4155 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004156
Paul Bakker34617722014-06-13 17:20:13 +02004157 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004158
4159 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4160}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004161#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004162
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004163#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4164#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004165static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004166 ssl_context *ssl, unsigned char *buf, int from )
4167{
4168 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004169 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004170 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004171 unsigned char padbuf[32];
4172
Paul Bakker48916f92012-09-16 19:57:18 +00004173 ssl_session *session = ssl->session_negotiate;
4174 if( !session )
4175 session = ssl->session;
4176
Paul Bakker380da532012-04-18 16:10:25 +00004177 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004178
Paul Bakker9e36f042013-06-30 14:34:05 +02004179 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004180
4181 /*
4182 * TLSv1.2:
4183 * hash = PRF( master, finished_label,
4184 * Hash( handshake ) )[0.11]
4185 */
4186
Paul Bakker9e36f042013-06-30 14:34:05 +02004187#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004188 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004189 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004190#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004191
4192 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004193 ? "client finished"
4194 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004195
Paul Bakker9e36f042013-06-30 14:34:05 +02004196 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004197
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004198 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004199 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004200
4201 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4202
Paul Bakker5b4af392014-06-26 12:09:34 +02004203 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004204
Paul Bakker34617722014-06-13 17:20:13 +02004205 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004206
4207 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4208}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004209#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004210
Paul Bakker9e36f042013-06-30 14:34:05 +02004211#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004212static void ssl_calc_finished_tls_sha384(
4213 ssl_context *ssl, unsigned char *buf, int from )
4214{
4215 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004216 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004217 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004218 unsigned char padbuf[48];
4219
Paul Bakker48916f92012-09-16 19:57:18 +00004220 ssl_session *session = ssl->session_negotiate;
4221 if( !session )
4222 session = ssl->session;
4223
Paul Bakker380da532012-04-18 16:10:25 +00004224 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004225
Paul Bakker9e36f042013-06-30 14:34:05 +02004226 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004227
4228 /*
4229 * TLSv1.2:
4230 * hash = PRF( master, finished_label,
4231 * Hash( handshake ) )[0.11]
4232 */
4233
Paul Bakker9e36f042013-06-30 14:34:05 +02004234#if !defined(POLARSSL_SHA512_ALT)
4235 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4236 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004237#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004238
4239 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004240 ? "client finished"
4241 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004242
Paul Bakker9e36f042013-06-30 14:34:05 +02004243 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004244
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004245 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004246 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004247
4248 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4249
Paul Bakker5b4af392014-06-26 12:09:34 +02004250 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004251
Paul Bakker34617722014-06-13 17:20:13 +02004252 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004253
4254 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4255}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004256#endif /* POLARSSL_SHA512_C */
4257#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004258
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004259static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004260{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004261 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004262
4263 /*
4264 * Free our handshake params
4265 */
4266 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004267 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004268 ssl->handshake = NULL;
4269
4270 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004271 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004272 */
4273 if( ssl->transform )
4274 {
4275 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004276 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004277 }
4278 ssl->transform = ssl->transform_negotiate;
4279 ssl->transform_negotiate = NULL;
4280
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004281 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4282}
4283
4284void ssl_handshake_wrapup( ssl_context *ssl )
4285{
4286 int resume = ssl->handshake->resume;
4287
4288 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4289
4290 if( ssl->renegotiation == SSL_RENEGOTIATION )
4291 {
4292 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4293 ssl->renego_records_seen = 0;
4294 }
4295
4296 /*
4297 * Free the previous session and switch in the current one
4298 */
Paul Bakker0a597072012-09-25 21:55:46 +00004299 if( ssl->session )
4300 {
4301 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004302 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004303 }
4304 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004305 ssl->session_negotiate = NULL;
4306
Paul Bakker0a597072012-09-25 21:55:46 +00004307 /*
4308 * Add cache entry
4309 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004310 if( ssl->f_set_cache != NULL &&
4311 ssl->session->length != 0 &&
4312 resume == 0 )
4313 {
Paul Bakker0a597072012-09-25 21:55:46 +00004314 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4315 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004316 }
Paul Bakker0a597072012-09-25 21:55:46 +00004317
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004318#if defined(POLARSSL_SSL_PROTO_DTLS)
4319 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4320 ssl->handshake->flight != NULL )
4321 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004322 /* Cancel handshake timer */
4323 ssl_set_timer( ssl, 0 );
4324
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004325 /* Keep last flight around in case we need to resend it:
4326 * we need the handshake and transform structures for that */
4327 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4328 }
4329 else
4330#endif
4331 ssl_handshake_wrapup_free_hs_transform( ssl );
4332
Paul Bakker48916f92012-09-16 19:57:18 +00004333 ssl->state++;
4334
4335 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4336}
4337
Paul Bakker1ef83d62012-04-11 12:09:53 +00004338int ssl_write_finished( ssl_context *ssl )
4339{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004340 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004341
4342 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4343
Paul Bakker92be97b2013-01-02 17:30:03 +01004344 /*
4345 * Set the out_msg pointer to the correct location based on IV length
4346 */
4347 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4348 {
4349 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4350 ssl->transform_negotiate->fixed_ivlen;
4351 }
4352 else
4353 ssl->out_msg = ssl->out_iv;
4354
Paul Bakker48916f92012-09-16 19:57:18 +00004355 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004356
4357 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4359
Paul Bakker48916f92012-09-16 19:57:18 +00004360 ssl->verify_data_len = hash_len;
4361 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4362
Paul Bakker5121ce52009-01-03 21:22:43 +00004363 ssl->out_msglen = 4 + hash_len;
4364 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4365 ssl->out_msg[0] = SSL_HS_FINISHED;
4366
4367 /*
4368 * In case of session resuming, invert the client and server
4369 * ChangeCipherSpec messages order.
4370 */
Paul Bakker0a597072012-09-25 21:55:46 +00004371 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004372 {
4373 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004374 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004375 else
4376 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4377 }
4378 else
4379 ssl->state++;
4380
Paul Bakker48916f92012-09-16 19:57:18 +00004381 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004382 * Switch to our negotiated transform and session parameters for outbound
4383 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004384 */
4385 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004386
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004387#if defined(POLARSSL_SSL_PROTO_DTLS)
4388 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4389 {
4390 unsigned char i;
4391
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004392 /* Remember current epoch settings for resending */
4393 ssl->handshake->alt_transform_out = ssl->transform_out;
4394 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4395
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004396 /* Set sequence_number to zero */
4397 memset( ssl->out_ctr + 2, 0, 6 );
4398
4399 /* Increment epoch */
4400 for( i = 2; i > 0; i-- )
4401 if( ++ssl->out_ctr[i - 1] != 0 )
4402 break;
4403
4404 /* The loop goes to its end iff the counter is wrapping */
4405 if( i == 0 )
4406 {
4407 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4408 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4409 }
4410 }
4411 else
4412#endif /* POLARSSL_SSL_PROTO_DTLS */
4413 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004414
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004415 ssl->transform_out = ssl->transform_negotiate;
4416 ssl->session_out = ssl->session_negotiate;
4417
Paul Bakker07eb38b2012-12-19 14:42:06 +01004418#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004419 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004420 {
4421 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4422 {
4423 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4424 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4425 }
4426 }
4427#endif
4428
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004429#if defined(POLARSSL_SSL_PROTO_DTLS)
4430 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4431 ssl_send_flight_completed( ssl );
4432#endif
4433
Paul Bakker5121ce52009-01-03 21:22:43 +00004434 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4435 {
4436 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4437 return( ret );
4438 }
4439
4440 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4441
4442 return( 0 );
4443}
4444
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004445#if defined(POLARSSL_SSL_PROTO_SSL3)
4446#define SSL_MAX_HASH_LEN 36
4447#else
4448#define SSL_MAX_HASH_LEN 12
4449#endif
4450
Paul Bakker5121ce52009-01-03 21:22:43 +00004451int ssl_parse_finished( ssl_context *ssl )
4452{
Paul Bakker23986e52011-04-24 08:57:21 +00004453 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004454 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004455 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004456
4457 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4458
Paul Bakker48916f92012-09-16 19:57:18 +00004459 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004460
Paul Bakker5121ce52009-01-03 21:22:43 +00004461 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4462 {
4463 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4464 return( ret );
4465 }
4466
4467 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4468 {
4469 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004470 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004471 }
4472
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004473 /* There is currently no ciphersuite using another length with TLS 1.2 */
4474#if defined(POLARSSL_SSL_PROTO_SSL3)
4475 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4476 hash_len = 36;
4477 else
4478#endif
4479 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004480
4481 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004482 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004483 {
4484 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004485 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004486 }
4487
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004488 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4489 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 {
4491 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004492 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004493 }
4494
Paul Bakker48916f92012-09-16 19:57:18 +00004495 ssl->verify_data_len = hash_len;
4496 memcpy( ssl->peer_verify_data, buf, hash_len );
4497
Paul Bakker0a597072012-09-25 21:55:46 +00004498 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004499 {
4500 if( ssl->endpoint == SSL_IS_CLIENT )
4501 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4502
4503 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004504 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004505 }
4506 else
4507 ssl->state++;
4508
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004509#if defined(POLARSSL_SSL_PROTO_DTLS)
4510 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4511 ssl_recv_flight_completed( ssl );
4512#endif
4513
Paul Bakker5121ce52009-01-03 21:22:43 +00004514 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4515
4516 return( 0 );
4517}
4518
Paul Bakker968afaa2014-07-09 11:09:24 +02004519static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004520{
4521 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4522
4523#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4524 defined(POLARSSL_SSL_PROTO_TLS1_1)
4525 md5_init( &handshake->fin_md5 );
4526 sha1_init( &handshake->fin_sha1 );
4527 md5_starts( &handshake->fin_md5 );
4528 sha1_starts( &handshake->fin_sha1 );
4529#endif
4530#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4531#if defined(POLARSSL_SHA256_C)
4532 sha256_init( &handshake->fin_sha256 );
4533 sha256_starts( &handshake->fin_sha256, 0 );
4534#endif
4535#if defined(POLARSSL_SHA512_C)
4536 sha512_init( &handshake->fin_sha512 );
4537 sha512_starts( &handshake->fin_sha512, 1 );
4538#endif
4539#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4540
4541 handshake->update_checksum = ssl_update_checksum_start;
4542 handshake->sig_alg = SSL_HASH_SHA1;
4543
4544#if defined(POLARSSL_DHM_C)
4545 dhm_init( &handshake->dhm_ctx );
4546#endif
4547#if defined(POLARSSL_ECDH_C)
4548 ecdh_init( &handshake->ecdh_ctx );
4549#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004550}
4551
4552static void ssl_transform_init( ssl_transform *transform )
4553{
4554 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004555
4556 cipher_init( &transform->cipher_ctx_enc );
4557 cipher_init( &transform->cipher_ctx_dec );
4558
4559 md_init( &transform->md_ctx_enc );
4560 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004561}
4562
4563void ssl_session_init( ssl_session *session )
4564{
4565 memset( session, 0, sizeof(ssl_session) );
4566}
4567
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004568static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004569{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004570 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004571 if( ssl->transform_negotiate )
4572 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004573 if( ssl->session_negotiate )
4574 ssl_session_free( ssl->session_negotiate );
4575 if( ssl->handshake )
4576 ssl_handshake_free( ssl->handshake );
4577
4578 /*
4579 * Either the pointers are now NULL or cleared properly and can be freed.
4580 * Now allocate missing structures.
4581 */
4582 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004583 {
4584 ssl->transform_negotiate =
4585 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4586 }
Paul Bakker48916f92012-09-16 19:57:18 +00004587
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004588 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004589 {
4590 ssl->session_negotiate =
4591 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4592 }
Paul Bakker48916f92012-09-16 19:57:18 +00004593
Paul Bakker82788fb2014-10-20 13:59:19 +02004594 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004595 {
4596 ssl->handshake = (ssl_handshake_params *)
4597 polarssl_malloc( sizeof(ssl_handshake_params) );
4598 }
Paul Bakker48916f92012-09-16 19:57:18 +00004599
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004600 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004601 if( ssl->handshake == NULL ||
4602 ssl->transform_negotiate == NULL ||
4603 ssl->session_negotiate == NULL )
4604 {
4605 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004606
4607 polarssl_free( ssl->handshake );
4608 polarssl_free( ssl->transform_negotiate );
4609 polarssl_free( ssl->session_negotiate );
4610
4611 ssl->handshake = NULL;
4612 ssl->transform_negotiate = NULL;
4613 ssl->session_negotiate = NULL;
4614
Paul Bakker48916f92012-09-16 19:57:18 +00004615 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4616 }
4617
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004618 /* Initialize structures */
4619 ssl_session_init( ssl->session_negotiate );
4620 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004621 ssl_handshake_params_init( ssl->handshake );
4622
4623#if defined(POLARSSL_X509_CRT_PARSE_C)
4624 ssl->handshake->key_cert = ssl->key_cert;
4625#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004626
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004627 /*
4628 * We may not know yet if we're using DTLS,
4629 * so always initiliase DTLS-specific fields.
4630 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004631#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004632 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004633
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004634 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004635 if( ssl->endpoint == SSL_IS_CLIENT )
4636 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4637 else
4638 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004639#endif
4640
Paul Bakker48916f92012-09-16 19:57:18 +00004641 return( 0 );
4642}
4643
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004644#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4645/* Dummy cookie callbacks for defaults */
4646static int ssl_cookie_write_dummy( void *ctx,
4647 unsigned char **p, unsigned char *end,
4648 const unsigned char *cli_id, size_t cli_id_len )
4649{
4650 ((void) ctx);
4651 ((void) p);
4652 ((void) end);
4653 ((void) cli_id);
4654 ((void) cli_id_len);
4655
4656 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4657}
4658
4659static int ssl_cookie_check_dummy( void *ctx,
4660 const unsigned char *cookie, size_t cookie_len,
4661 const unsigned char *cli_id, size_t cli_id_len )
4662{
4663 ((void) ctx);
4664 ((void) cookie);
4665 ((void) cookie_len);
4666 ((void) cli_id);
4667 ((void) cli_id_len);
4668
4669 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4670}
4671#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4672
Paul Bakker5121ce52009-01-03 21:22:43 +00004673/*
4674 * Initialize an SSL context
4675 */
4676int ssl_init( ssl_context *ssl )
4677{
Paul Bakker48916f92012-09-16 19:57:18 +00004678 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004679 int len = SSL_BUFFER_LEN;
4680
4681 memset( ssl, 0, sizeof( ssl_context ) );
4682
Paul Bakker62f2dee2012-09-28 07:31:51 +00004683 /*
4684 * Sane defaults
4685 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004686 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4687 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4688 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4689 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004690
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004691 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004692
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004693 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4694
Paul Bakker62f2dee2012-09-28 07:31:51 +00004695#if defined(POLARSSL_DHM_C)
4696 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4697 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4698 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4699 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4700 {
4701 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4702 return( ret );
4703 }
4704#endif
4705
4706 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004707 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004708 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004709 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004710 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004711
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004712 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004713 {
4714 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004715 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004716 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004717 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004718 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004719 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 }
4721
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004722 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4723 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004724
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004725 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4726 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4727
Paul Bakker606b4ba2013-08-14 16:52:14 +02004728#if defined(POLARSSL_SSL_SESSION_TICKETS)
4729 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4730#endif
4731
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004732#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004733 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004734#endif
4735
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004736#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4737 ssl->f_cookie_write = ssl_cookie_write_dummy;
4738 ssl->f_cookie_check = ssl_cookie_check_dummy;
4739#endif
4740
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004741#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4742 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4743#endif
4744
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004745#if defined(POLARSSL_SSL_PROTO_DTLS)
4746 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4747 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4748#endif
4749
Paul Bakker48916f92012-09-16 19:57:18 +00004750 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4751 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004752
4753 return( 0 );
4754}
4755
4756/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004757 * Reset an initialized and used SSL context for re-use while retaining
4758 * all application-set variables, function pointers and data.
4759 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004760int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004761{
Paul Bakker48916f92012-09-16 19:57:18 +00004762 int ret;
4763
Paul Bakker7eb013f2011-10-06 12:37:39 +00004764 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004765 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4766 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4767
4768 ssl->verify_data_len = 0;
4769 memset( ssl->own_verify_data, 0, 36 );
4770 memset( ssl->peer_verify_data, 0, 36 );
4771
Paul Bakker7eb013f2011-10-06 12:37:39 +00004772 ssl->in_offt = NULL;
4773
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004774 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004775 ssl->in_msgtype = 0;
4776 ssl->in_msglen = 0;
4777 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004778#if defined(POLARSSL_SSL_PROTO_DTLS)
4779 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004780 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004781#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004782#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4783 ssl_dtls_replay_reset( ssl );
4784#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004785
4786 ssl->in_hslen = 0;
4787 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004788 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004789
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004790 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004791 ssl->out_msgtype = 0;
4792 ssl->out_msglen = 0;
4793 ssl->out_left = 0;
4794
Paul Bakker48916f92012-09-16 19:57:18 +00004795 ssl->transform_in = NULL;
4796 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004797
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004798 ssl->renego_records_seen = 0;
4799
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004800 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4801 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004802
4803#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004804 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004805 {
4806 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004807 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004808 {
4809 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4810 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4811 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004812 }
4813#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004814
Paul Bakker48916f92012-09-16 19:57:18 +00004815 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004816 {
Paul Bakker48916f92012-09-16 19:57:18 +00004817 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004818 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004819 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004820 }
Paul Bakker48916f92012-09-16 19:57:18 +00004821
Paul Bakkerc0463502013-02-14 11:19:38 +01004822 if( ssl->session )
4823 {
4824 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004825 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004826 ssl->session = NULL;
4827 }
4828
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004829#if defined(POLARSSL_SSL_ALPN)
4830 ssl->alpn_chosen = NULL;
4831#endif
4832
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004833#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004834 polarssl_free( ssl->cli_id );
4835 ssl->cli_id = NULL;
4836 ssl->cli_id_len = 0;
4837#endif
4838
Paul Bakker48916f92012-09-16 19:57:18 +00004839 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4840 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004841
4842 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004843}
4844
Paul Bakkera503a632013-08-14 13:48:06 +02004845#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004846static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4847{
4848 aes_free( &tkeys->enc );
4849 aes_free( &tkeys->dec );
4850
4851 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4852}
4853
Paul Bakker7eb013f2011-10-06 12:37:39 +00004854/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004855 * Allocate and initialize ticket keys
4856 */
4857static int ssl_ticket_keys_init( ssl_context *ssl )
4858{
4859 int ret;
4860 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004861 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004862
4863 if( ssl->ticket_keys != NULL )
4864 return( 0 );
4865
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004866 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4867 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004868 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4869
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004870 aes_init( &tkeys->enc );
4871 aes_init( &tkeys->dec );
4872
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004873 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004874 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004875 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004876 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004877 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004878 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004879
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004880 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4881 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4882 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4883 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004884 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004885 polarssl_free( tkeys );
4886 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004887 }
4888
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004889 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004890 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004891 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004892 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004893 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004894 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004895
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004896 ssl->ticket_keys = tkeys;
4897
4898 return( 0 );
4899}
Paul Bakkera503a632013-08-14 13:48:06 +02004900#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004901
4902/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 * SSL set accessors
4904 */
4905void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4906{
4907 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004908
Paul Bakker606b4ba2013-08-14 16:52:14 +02004909#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004910 if( endpoint == SSL_IS_CLIENT )
4911 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004912#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004913}
4914
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004915int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004916{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004917#if defined(POLARSSL_SSL_PROTO_DTLS)
4918 if( transport == SSL_TRANSPORT_DATAGRAM )
4919 {
4920 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004921
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004922 ssl->out_hdr = ssl->out_buf;
4923 ssl->out_ctr = ssl->out_buf + 3;
4924 ssl->out_len = ssl->out_buf + 11;
4925 ssl->out_iv = ssl->out_buf + 13;
4926 ssl->out_msg = ssl->out_buf + 13;
4927
4928 ssl->in_hdr = ssl->in_buf;
4929 ssl->in_ctr = ssl->in_buf + 3;
4930 ssl->in_len = ssl->in_buf + 11;
4931 ssl->in_iv = ssl->in_buf + 13;
4932 ssl->in_msg = ssl->in_buf + 13;
4933
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004934 /* DTLS starts with TLS1.1 */
4935 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4936 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004937
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004938 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4939 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4940
4941 return( 0 );
4942 }
4943#endif
4944
4945 if( transport == SSL_TRANSPORT_STREAM )
4946 {
4947 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004948
4949 ssl->out_ctr = ssl->out_buf;
4950 ssl->out_hdr = ssl->out_buf + 8;
4951 ssl->out_len = ssl->out_buf + 11;
4952 ssl->out_iv = ssl->out_buf + 13;
4953 ssl->out_msg = ssl->out_buf + 13;
4954
4955 ssl->in_ctr = ssl->in_buf;
4956 ssl->in_hdr = ssl->in_buf + 8;
4957 ssl->in_len = ssl->in_buf + 11;
4958 ssl->in_iv = ssl->in_buf + 13;
4959 ssl->in_msg = ssl->in_buf + 13;
4960
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004961 return( 0 );
4962 }
4963
4964 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004965}
4966
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004967#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4968void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4969{
4970 ssl->anti_replay = mode;
4971}
4972#endif
4973
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004974#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
4975void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
4976{
4977 ssl->badmac_limit = limit;
4978}
4979#endif
4980
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004981#if defined(POLARSSL_SSL_PROTO_DTLS)
4982void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4983{
4984 ssl->hs_timeout_min = min;
4985 ssl->hs_timeout_max = max;
4986}
4987#endif
4988
Paul Bakker5121ce52009-01-03 21:22:43 +00004989void ssl_set_authmode( ssl_context *ssl, int authmode )
4990{
4991 ssl->authmode = authmode;
4992}
4993
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004994#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004995void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004996 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004997 void *p_vrfy )
4998{
4999 ssl->f_vrfy = f_vrfy;
5000 ssl->p_vrfy = p_vrfy;
5001}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005002#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005003
Paul Bakker5121ce52009-01-03 21:22:43 +00005004void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005005 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005006 void *p_rng )
5007{
5008 ssl->f_rng = f_rng;
5009 ssl->p_rng = p_rng;
5010}
5011
5012void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005013 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005014 void *p_dbg )
5015{
5016 ssl->f_dbg = f_dbg;
5017 ssl->p_dbg = p_dbg;
5018}
5019
5020void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005021 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005022 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005023{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005024 if( p_recv != p_send )
5025 {
5026 ssl->f_recv = NULL;
5027 ssl->f_send = NULL;
5028 ssl->p_bio = NULL;
5029 return;
5030 }
5031
Paul Bakker5121ce52009-01-03 21:22:43 +00005032 ssl->f_recv = f_recv;
5033 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005034 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005035}
5036
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005037void ssl_set_bio_timeout( ssl_context *ssl,
5038 void *p_bio,
5039 int (*f_send)(void *, const unsigned char *, size_t),
5040 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005041 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5042 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005043{
5044 ssl->p_bio = p_bio;
5045 ssl->f_send = f_send;
5046 ssl->f_recv = f_recv;
5047 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005048 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005049}
5050
Paul Bakker0a597072012-09-25 21:55:46 +00005051void ssl_set_session_cache( ssl_context *ssl,
5052 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5053 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005054{
Paul Bakker0a597072012-09-25 21:55:46 +00005055 ssl->f_get_cache = f_get_cache;
5056 ssl->p_get_cache = p_get_cache;
5057 ssl->f_set_cache = f_set_cache;
5058 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005059}
5060
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005061int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005062{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005063 int ret;
5064
5065 if( ssl == NULL ||
5066 session == NULL ||
5067 ssl->session_negotiate == NULL ||
5068 ssl->endpoint != SSL_IS_CLIENT )
5069 {
5070 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5071 }
5072
5073 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5074 return( ret );
5075
Paul Bakker0a597072012-09-25 21:55:46 +00005076 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005077
5078 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005079}
5080
Paul Bakkerb68cad62012-08-23 08:34:18 +00005081void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005082{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005083 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5084 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5085 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5086 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5087}
5088
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005089void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5090 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005091 int major, int minor )
5092{
5093 if( major != SSL_MAJOR_VERSION_3 )
5094 return;
5095
5096 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5097 return;
5098
5099 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005100}
5101
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005102#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005103/* Add a new (empty) key_cert entry an return a pointer to it */
5104static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5105{
5106 ssl_key_cert *key_cert, *last;
5107
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005108 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5109 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005110 return( NULL );
5111
5112 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5113
5114 /* Append the new key_cert to the (possibly empty) current list */
5115 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005116 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005117 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005118 if( ssl->handshake != NULL )
5119 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005120 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005121 else
5122 {
5123 last = ssl->key_cert;
5124 while( last->next != NULL )
5125 last = last->next;
5126 last->next = key_cert;
5127 }
5128
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005129 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005130}
5131
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005132void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005133 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005134{
5135 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005136 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005137 ssl->peer_cn = peer_cn;
5138}
5139
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005140int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005141 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005142{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005143 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5144
5145 if( key_cert == NULL )
5146 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5147
5148 key_cert->cert = own_cert;
5149 key_cert->key = pk_key;
5150
5151 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005152}
5153
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005154#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005155int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005156 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005157{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005158 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005159 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005160
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005161 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005162 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5163
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005164 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5165 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005166 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005167
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005168 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005169
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005170 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005171 if( ret != 0 )
5172 return( ret );
5173
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005174 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005175 return( ret );
5176
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005177 key_cert->cert = own_cert;
5178 key_cert->key_own_alloc = 1;
5179
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005180 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005181}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005182#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005183
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005184int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005185 void *rsa_key,
5186 rsa_decrypt_func rsa_decrypt,
5187 rsa_sign_func rsa_sign,
5188 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005189{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005190 int ret;
5191 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005192
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005193 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005194 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5195
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005196 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5197 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005198 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005199
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005200 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005201
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005202 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5203 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5204 return( ret );
5205
5206 key_cert->cert = own_cert;
5207 key_cert->key_own_alloc = 1;
5208
5209 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005210}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005211#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005212
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005213#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005214int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5215 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005216{
Paul Bakker6db455e2013-09-18 17:29:31 +02005217 if( psk == NULL || psk_identity == NULL )
5218 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5219
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005220 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005221 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5222
Paul Bakker6db455e2013-09-18 17:29:31 +02005223 if( ssl->psk != NULL )
5224 {
5225 polarssl_free( ssl->psk );
5226 polarssl_free( ssl->psk_identity );
5227 }
5228
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005229 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005230 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005231
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005232 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005233 ssl->psk_identity = (unsigned char *)
5234 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005235
5236 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5237 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5238
5239 memcpy( ssl->psk, psk, ssl->psk_len );
5240 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005241
5242 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005243}
5244
5245void ssl_set_psk_cb( ssl_context *ssl,
5246 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5247 size_t),
5248 void *p_psk )
5249{
5250 ssl->f_psk = f_psk;
5251 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005252}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005253#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005254
Paul Bakker48916f92012-09-16 19:57:18 +00005255#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005256int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005257{
5258 int ret;
5259
Paul Bakker48916f92012-09-16 19:57:18 +00005260 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005261 {
5262 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5263 return( ret );
5264 }
5265
Paul Bakker48916f92012-09-16 19:57:18 +00005266 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005267 {
5268 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5269 return( ret );
5270 }
5271
5272 return( 0 );
5273}
5274
Paul Bakker1b57b062011-01-06 15:48:19 +00005275int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5276{
5277 int ret;
5278
Paul Bakker66d5d072014-06-17 16:39:18 +02005279 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005280 {
5281 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5282 return( ret );
5283 }
5284
Paul Bakker66d5d072014-06-17 16:39:18 +02005285 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005286 {
5287 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5288 return( ret );
5289 }
5290
5291 return( 0 );
5292}
Paul Bakker48916f92012-09-16 19:57:18 +00005293#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005294
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005295#if defined(POLARSSL_SSL_SET_CURVES)
5296/*
5297 * Set the allowed elliptic curves
5298 */
5299void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5300{
5301 ssl->curve_list = curve_list;
5302}
5303#endif
5304
Paul Bakker0be444a2013-08-27 21:55:01 +02005305#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005306int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005307{
5308 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005309 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005310
5311 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005312
5313 if( ssl->hostname_len + 1 == 0 )
5314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5315
Paul Bakker6e339b52013-07-03 13:37:05 +02005316 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005317
Paul Bakkerb15b8512012-01-13 13:44:06 +00005318 if( ssl->hostname == NULL )
5319 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5320
Paul Bakker3c2122f2013-06-24 19:03:14 +02005321 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005322 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005323
Paul Bakker40ea7de2009-05-03 10:18:48 +00005324 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005325
5326 return( 0 );
5327}
5328
Paul Bakker5701cdc2012-09-27 21:49:42 +00005329void ssl_set_sni( ssl_context *ssl,
5330 int (*f_sni)(void *, ssl_context *,
5331 const unsigned char *, size_t),
5332 void *p_sni )
5333{
5334 ssl->f_sni = f_sni;
5335 ssl->p_sni = p_sni;
5336}
Paul Bakker0be444a2013-08-27 21:55:01 +02005337#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005338
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005339#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005340int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005341{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005342 size_t cur_len, tot_len;
5343 const char **p;
5344
5345 /*
5346 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5347 * truncated". Check lengths now rather than later.
5348 */
5349 tot_len = 0;
5350 for( p = protos; *p != NULL; p++ )
5351 {
5352 cur_len = strlen( *p );
5353 tot_len += cur_len;
5354
5355 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5356 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5357 }
5358
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005359 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005360
5361 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005362}
5363
5364const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5365{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005366 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005367}
5368#endif /* POLARSSL_SSL_ALPN */
5369
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005370static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005371{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005372 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005373 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005374 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005375 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005376 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005377
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005378#if defined(POLARSSL_SSL_PROTO_DTLS)
5379 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5380 minor < SSL_MINOR_VERSION_2 )
5381 {
5382 return( -1 );
5383 }
5384#else
5385 ((void) ssl);
5386#endif
5387
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005388 return( 0 );
5389}
5390
5391int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5392{
5393 if( ssl_check_version( ssl, major, minor ) != 0 )
5394 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5395
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005396 ssl->max_major_ver = major;
5397 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005398
5399 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005400}
5401
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005402int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005403{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005404 if( ssl_check_version( ssl, major, minor ) != 0 )
5405 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005406
5407 ssl->min_major_ver = major;
5408 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005409
5410 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005411}
5412
Paul Bakker05decb22013-08-15 13:33:48 +02005413#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005414int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5415{
Paul Bakker77e257e2013-12-16 15:29:52 +01005416 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005417 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005418 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005419 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005420 }
5421
5422 ssl->mfl_code = mfl_code;
5423
5424 return( 0 );
5425}
Paul Bakker05decb22013-08-15 13:33:48 +02005426#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005427
Paul Bakker1f2bc622013-08-15 13:45:55 +02005428#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005429int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005430{
5431 if( ssl->endpoint != SSL_IS_CLIENT )
5432 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5433
Paul Bakker8c1ede62013-07-19 14:14:37 +02005434 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005435
5436 return( 0 );
5437}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005438#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005439
Paul Bakker48916f92012-09-16 19:57:18 +00005440void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5441{
5442 ssl->disable_renegotiation = renegotiation;
5443}
5444
5445void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5446{
5447 ssl->allow_legacy_renegotiation = allow_legacy;
5448}
5449
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005450void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5451{
5452 ssl->renego_max_records = max_records;
5453}
5454
Paul Bakkera503a632013-08-14 13:48:06 +02005455#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005456int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5457{
5458 ssl->session_tickets = use_tickets;
5459
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005460 if( ssl->endpoint == SSL_IS_CLIENT )
5461 return( 0 );
5462
5463 if( ssl->f_rng == NULL )
5464 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5465
5466 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005467}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005468
5469void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5470{
5471 ssl->ticket_lifetime = lifetime;
5472}
Paul Bakkera503a632013-08-14 13:48:06 +02005473#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005474
Paul Bakker5121ce52009-01-03 21:22:43 +00005475/*
5476 * SSL get accessors
5477 */
Paul Bakker23986e52011-04-24 08:57:21 +00005478size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005479{
5480 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5481}
5482
Paul Bakkerff60ee62010-03-16 21:09:09 +00005483int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005484{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005485 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005486}
5487
Paul Bakkere3166ce2011-01-27 17:40:50 +00005488const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005489{
Paul Bakker926c8e42013-03-06 10:23:34 +01005490 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005491 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005492
Paul Bakkere3166ce2011-01-27 17:40:50 +00005493 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005494}
5495
Paul Bakker43ca69c2011-01-15 17:35:19 +00005496const char *ssl_get_version( const ssl_context *ssl )
5497{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005498#if defined(POLARSSL_SSL_PROTO_DTLS)
5499 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5500 {
5501 switch( ssl->minor_ver )
5502 {
5503 case SSL_MINOR_VERSION_2:
5504 return( "DTLSv1.0" );
5505
5506 case SSL_MINOR_VERSION_3:
5507 return( "DTLSv1.2" );
5508
5509 default:
5510 return( "unknown (DTLS)" );
5511 }
5512 }
5513#endif
5514
Paul Bakker43ca69c2011-01-15 17:35:19 +00005515 switch( ssl->minor_ver )
5516 {
5517 case SSL_MINOR_VERSION_0:
5518 return( "SSLv3.0" );
5519
5520 case SSL_MINOR_VERSION_1:
5521 return( "TLSv1.0" );
5522
5523 case SSL_MINOR_VERSION_2:
5524 return( "TLSv1.1" );
5525
Paul Bakker1ef83d62012-04-11 12:09:53 +00005526 case SSL_MINOR_VERSION_3:
5527 return( "TLSv1.2" );
5528
Paul Bakker43ca69c2011-01-15 17:35:19 +00005529 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005530 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005531 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005532}
5533
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005534int ssl_get_record_expansion( const ssl_context *ssl )
5535{
5536 int transform_expansion;
5537 const ssl_transform *transform = ssl->transform_out;
5538
5539#if defined(POLARSSL_ZLIB_SUPPORT)
5540 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5541 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5542#endif
5543
5544 if( transform == NULL )
5545 return( ssl_hdr_len( ssl ) );
5546
5547 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5548 {
5549 case POLARSSL_MODE_GCM:
5550 case POLARSSL_MODE_CCM:
5551 case POLARSSL_MODE_STREAM:
5552 transform_expansion = transform->minlen;
5553 break;
5554
5555 case POLARSSL_MODE_CBC:
5556 transform_expansion = transform->maclen
5557 + cipher_get_block_size( &transform->cipher_ctx_enc );
5558 break;
5559
5560 default:
5561 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5562 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5563 }
5564
5565 return( ssl_hdr_len( ssl ) + transform_expansion );
5566}
5567
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005568#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005569const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005570{
5571 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005572 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005573
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005574 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005575}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005576#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005577
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005578int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5579{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005580 if( ssl == NULL ||
5581 dst == NULL ||
5582 ssl->session == NULL ||
5583 ssl->endpoint != SSL_IS_CLIENT )
5584 {
5585 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5586 }
5587
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005588 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005589}
5590
Paul Bakker5121ce52009-01-03 21:22:43 +00005591/*
Paul Bakker1961b702013-01-25 14:49:24 +01005592 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005593 */
Paul Bakker1961b702013-01-25 14:49:24 +01005594int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005595{
Paul Bakker40e46942009-01-03 21:51:57 +00005596 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005597
Paul Bakker40e46942009-01-03 21:51:57 +00005598#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005599 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005600 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005601#endif
5602
Paul Bakker40e46942009-01-03 21:51:57 +00005603#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005604 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005605 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005606#endif
5607
Paul Bakker1961b702013-01-25 14:49:24 +01005608 return( ret );
5609}
5610
5611/*
5612 * Perform the SSL handshake
5613 */
5614int ssl_handshake( ssl_context *ssl )
5615{
5616 int ret = 0;
5617
5618 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5619
5620 while( ssl->state != SSL_HANDSHAKE_OVER )
5621 {
5622 ret = ssl_handshake_step( ssl );
5623
5624 if( ret != 0 )
5625 break;
5626 }
5627
Paul Bakker5121ce52009-01-03 21:22:43 +00005628 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5629
5630 return( ret );
5631}
5632
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005633#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005634/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005635 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005636 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005637static int ssl_write_hello_request( ssl_context *ssl )
5638{
5639 int ret;
5640
5641 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5642
5643 ssl->out_msglen = 4;
5644 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5645 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5646
5647 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5648 {
5649 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5650 return( ret );
5651 }
5652
5653 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5654
5655 return( 0 );
5656}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005657#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005658
5659/*
5660 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005661 * - any side: calling ssl_renegotiate(),
5662 * - client: receiving a HelloRequest during ssl_read(),
5663 * - server: receiving any handshake message on server during ssl_read() after
5664 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005665 * If the handshake doesn't complete due to waiting for I/O, it will continue
5666 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005667 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005668static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005669{
5670 int ret;
5671
5672 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5673
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005674 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5675 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005676
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005677 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5678 * the ServerHello will have message_seq = 1" */
5679#if defined(POLARSSL_SSL_PROTO_DTLS)
5680 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005681 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5682 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005683 if( ssl->endpoint == SSL_IS_SERVER )
5684 ssl->handshake->out_msg_seq = 1;
5685 else
5686 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005687 }
5688#endif
5689
Paul Bakker48916f92012-09-16 19:57:18 +00005690 ssl->state = SSL_HELLO_REQUEST;
5691 ssl->renegotiation = SSL_RENEGOTIATION;
5692
Paul Bakker48916f92012-09-16 19:57:18 +00005693 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5694 {
5695 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5696 return( ret );
5697 }
5698
5699 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5700
5701 return( 0 );
5702}
5703
5704/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005705 * Renegotiate current connection on client,
5706 * or request renegotiation on server
5707 */
5708int ssl_renegotiate( ssl_context *ssl )
5709{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005710 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005711
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005712#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005713 /* On server, just send the request */
5714 if( ssl->endpoint == SSL_IS_SERVER )
5715 {
5716 if( ssl->state != SSL_HANDSHAKE_OVER )
5717 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5718
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005719 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5720
5721 /* Did we already try/start sending HelloRequest? */
5722 if( ssl->out_left != 0 )
5723 return( ssl_flush_output( ssl ) );
5724
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005725 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005726 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005727#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005728
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005729#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005730 /*
5731 * On client, either start the renegotiation process or,
5732 * if already in progress, continue the handshake
5733 */
5734 if( ssl->renegotiation != SSL_RENEGOTIATION )
5735 {
5736 if( ssl->state != SSL_HANDSHAKE_OVER )
5737 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5738
5739 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5740 {
5741 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5742 return( ret );
5743 }
5744 }
5745 else
5746 {
5747 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5748 {
5749 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5750 return( ret );
5751 }
5752 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005753#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005754
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005755 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005756}
5757
5758/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005759 * Receive application data decrypted from the SSL layer
5760 */
Paul Bakker23986e52011-04-24 08:57:21 +00005761int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005762{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005763 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005764 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005765
5766 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5767
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005768#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005769 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005770 {
5771 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5772 return( ret );
5773
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005774 if( ssl->handshake != NULL &&
5775 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5776 {
5777 if( ( ret = ssl_resend( ssl ) ) != 0 )
5778 return( ret );
5779 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005780 }
5781#endif
5782
Paul Bakker5121ce52009-01-03 21:22:43 +00005783 if( ssl->state != SSL_HANDSHAKE_OVER )
5784 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005785 ret = ssl_handshake( ssl );
5786 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5787 {
5788 record_read = 1;
5789 }
5790 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 {
5792 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5793 return( ret );
5794 }
5795 }
5796
5797 if( ssl->in_offt == NULL )
5798 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02005799#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005800 /* Start timer if not already running */
5801 if( ssl->time_limit == 0 )
5802 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005803#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005804
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005805 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005806 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005807 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5808 {
5809 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5810 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005811
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005812 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5813 return( ret );
5814 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005815 }
5816
5817 if( ssl->in_msglen == 0 &&
5818 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5819 {
5820 /*
5821 * OpenSSL sends empty messages to randomize the IV
5822 */
5823 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5824 {
Paul Bakker831a7552011-05-18 13:32:51 +00005825 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5826 return( 0 );
5827
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5829 return( ret );
5830 }
5831 }
5832
Paul Bakker48916f92012-09-16 19:57:18 +00005833 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5834 {
5835 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5836
5837 if( ssl->endpoint == SSL_IS_CLIENT &&
5838 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005839 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005840 {
5841 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005842
5843 /* With DTLS, drop the packet (probably from last handshake) */
5844#if defined(POLARSSL_SSL_PROTO_DTLS)
5845 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5846 return( POLARSSL_ERR_NET_WANT_READ );
5847#endif
5848 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5849 }
5850
5851 if( ssl->endpoint == SSL_IS_SERVER &&
5852 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5853 {
5854 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5855
5856 /* With DTLS, drop the packet (probably from last handshake) */
5857#if defined(POLARSSL_SSL_PROTO_DTLS)
5858 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5859 return( POLARSSL_ERR_NET_WANT_READ );
5860#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005861 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5862 }
5863
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005864 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5865 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005866 ssl->allow_legacy_renegotiation ==
5867 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005868 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005869 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005870
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005871#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005872 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005873 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005874 /*
5875 * SSLv3 does not have a "no_renegotiation" alert
5876 */
5877 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5878 return( ret );
5879 }
5880 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005881#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005882#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5883 defined(POLARSSL_SSL_PROTO_TLS1_2)
5884 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005885 {
5886 if( ( ret = ssl_send_alert_message( ssl,
5887 SSL_ALERT_LEVEL_WARNING,
5888 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5889 {
5890 return( ret );
5891 }
Paul Bakker48916f92012-09-16 19:57:18 +00005892 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005893 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005894#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5895 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005896 {
5897 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005898 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005899 }
Paul Bakker48916f92012-09-16 19:57:18 +00005900 }
5901 else
5902 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005903 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005904#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005905 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5906 ssl->endpoint == SSL_IS_CLIENT )
5907 {
5908 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5909 }
5910#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005911 ret = ssl_start_renegotiation( ssl );
5912 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5913 {
5914 record_read = 1;
5915 }
5916 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005917 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005918 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005919 return( ret );
5920 }
Paul Bakker48916f92012-09-16 19:57:18 +00005921 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005922
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005923 /* If a non-handshake record was read during renego, fallthrough,
5924 * else tell the user they should call ssl_read() again */
5925 if( ! record_read )
5926 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005927 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005928 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5929 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005930
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005931 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005932 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005933 if( ++ssl->renego_records_seen > ssl->renego_max_records )
5934 {
5935 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5936 "but not honored by client" ) );
5937 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5938 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005939 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005940 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005941
5942 /* Fatal and closure alerts handled by ssl_read_record() */
5943 if( ssl->in_msgtype == SSL_MSG_ALERT )
5944 {
5945 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5946 return( POLARSSL_ERR_NET_WANT_READ );
5947 }
5948
5949 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005950 {
5951 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005952 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005953 }
5954
5955 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005956
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02005957#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005958 /* We're going to return something now, cancel timer,
5959 * except if handshake (renegotiation) is in progress */
5960 if( ssl->state == SSL_HANDSHAKE_OVER )
5961 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005962
5963 /* If we requested renego but received AppData, resend HelloRequest.
5964 * Do it now, after setting in_offt, to avoid taking this branch
5965 * again if ssl_write_hello_request() returns WANT_WRITE */
5966#if defined(POLARSSL_SSL_SRV_C)
5967 if( ssl->endpoint == SSL_IS_SERVER &&
5968 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5969 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005970 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005971 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005972 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005973 return( ret );
5974 }
5975 }
5976#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005977#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005978 }
5979
5980 n = ( len < ssl->in_msglen )
5981 ? len : ssl->in_msglen;
5982
5983 memcpy( buf, ssl->in_offt, n );
5984 ssl->in_msglen -= n;
5985
5986 if( ssl->in_msglen == 0 )
5987 /* all bytes consumed */
5988 ssl->in_offt = NULL;
5989 else
5990 /* more data available */
5991 ssl->in_offt += n;
5992
5993 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5994
Paul Bakker23986e52011-04-24 08:57:21 +00005995 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005996}
5997
5998/*
5999 * Send application data to be encrypted by the SSL layer
6000 */
Paul Bakker23986e52011-04-24 08:57:21 +00006001int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006002{
Paul Bakker23986e52011-04-24 08:57:21 +00006003 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006004#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6005 unsigned int max_len;
6006#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006007
6008 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6009
6010 if( ssl->state != SSL_HANDSHAKE_OVER )
6011 {
6012 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6013 {
6014 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6015 return( ret );
6016 }
6017 }
6018
Paul Bakker05decb22013-08-15 13:33:48 +02006019#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006020 /*
6021 * Assume mfl_code is correct since it was checked when set
6022 */
6023 max_len = mfl_code_to_length[ssl->mfl_code];
6024
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006025 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006026 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006027 */
6028 if( ssl->session_out != NULL &&
6029 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6030 {
6031 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6032 }
6033
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006034 if( len > max_len )
6035 {
6036#if defined(POLARSSL_SSL_PROTO_DTLS)
6037 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6038 {
6039 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6040 "maximum fragment length: %d > %d",
6041 len, max_len ) );
6042 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6043 }
6044 else
6045#endif
6046 len = max_len;
6047 }
6048#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006049
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 if( ssl->out_left != 0 )
6051 {
6052 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6053 {
6054 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6055 return( ret );
6056 }
6057 }
Paul Bakker887bd502011-06-08 13:10:54 +00006058 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006059 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006060 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006061 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006062 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006063
6064 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6065 {
6066 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6067 return( ret );
6068 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006069 }
6070
6071 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6072
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006073 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006074}
6075
6076/*
6077 * Notify the peer that the connection is being closed
6078 */
6079int ssl_close_notify( ssl_context *ssl )
6080{
6081 int ret;
6082
6083 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6084
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006085 if( ssl->out_left != 0 )
6086 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006087
6088 if( ssl->state == SSL_HANDSHAKE_OVER )
6089 {
Paul Bakker48916f92012-09-16 19:57:18 +00006090 if( ( ret = ssl_send_alert_message( ssl,
6091 SSL_ALERT_LEVEL_WARNING,
6092 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006093 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006094 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 return( ret );
6096 }
6097 }
6098
6099 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6100
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006101 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006102}
6103
Paul Bakker48916f92012-09-16 19:57:18 +00006104void ssl_transform_free( ssl_transform *transform )
6105{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006106 if( transform == NULL )
6107 return;
6108
Paul Bakker48916f92012-09-16 19:57:18 +00006109#if defined(POLARSSL_ZLIB_SUPPORT)
6110 deflateEnd( &transform->ctx_deflate );
6111 inflateEnd( &transform->ctx_inflate );
6112#endif
6113
Paul Bakker84bbeb52014-07-01 14:53:22 +02006114 cipher_free( &transform->cipher_ctx_enc );
6115 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006116
Paul Bakker84bbeb52014-07-01 14:53:22 +02006117 md_free( &transform->md_ctx_enc );
6118 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006119
Paul Bakker34617722014-06-13 17:20:13 +02006120 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006121}
6122
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006123#if defined(POLARSSL_X509_CRT_PARSE_C)
6124static void ssl_key_cert_free( ssl_key_cert *key_cert )
6125{
6126 ssl_key_cert *cur = key_cert, *next;
6127
6128 while( cur != NULL )
6129 {
6130 next = cur->next;
6131
6132 if( cur->key_own_alloc )
6133 {
6134 pk_free( cur->key );
6135 polarssl_free( cur->key );
6136 }
6137 polarssl_free( cur );
6138
6139 cur = next;
6140 }
6141}
6142#endif /* POLARSSL_X509_CRT_PARSE_C */
6143
Paul Bakker48916f92012-09-16 19:57:18 +00006144void ssl_handshake_free( ssl_handshake_params *handshake )
6145{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006146 if( handshake == NULL )
6147 return;
6148
Paul Bakker48916f92012-09-16 19:57:18 +00006149#if defined(POLARSSL_DHM_C)
6150 dhm_free( &handshake->dhm_ctx );
6151#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006152#if defined(POLARSSL_ECDH_C)
6153 ecdh_free( &handshake->ecdh_ctx );
6154#endif
6155
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006156#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006157 /* explicit void pointer cast for buggy MS compiler */
6158 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006159#endif
6160
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006161#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6162 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6163 /*
6164 * Free only the linked list wrapper, not the keys themselves
6165 * since the belong to the SNI callback
6166 */
6167 if( handshake->sni_key_cert != NULL )
6168 {
6169 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6170
6171 while( cur != NULL )
6172 {
6173 next = cur->next;
6174 polarssl_free( cur );
6175 cur = next;
6176 }
6177 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006178#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006179
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006180#if defined(POLARSSL_SSL_PROTO_DTLS)
6181 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006182 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006183 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006184#endif
6185
Paul Bakker34617722014-06-13 17:20:13 +02006186 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006187}
6188
6189void ssl_session_free( ssl_session *session )
6190{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006191 if( session == NULL )
6192 return;
6193
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006194#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006195 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006196 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006197 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006198 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006199 }
Paul Bakkered27a042013-04-18 22:46:23 +02006200#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006201
Paul Bakkera503a632013-08-14 13:48:06 +02006202#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006203 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006204#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006205
Paul Bakker34617722014-06-13 17:20:13 +02006206 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006207}
6208
Paul Bakker5121ce52009-01-03 21:22:43 +00006209/*
6210 * Free an SSL context
6211 */
6212void ssl_free( ssl_context *ssl )
6213{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006214 if( ssl == NULL )
6215 return;
6216
Paul Bakker5121ce52009-01-03 21:22:43 +00006217 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6218
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006219 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006220 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006221 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6222 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006223 }
6224
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006225 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006226 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006227 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6228 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006229 }
6230
Paul Bakker16770332013-10-11 09:59:44 +02006231#if defined(POLARSSL_ZLIB_SUPPORT)
6232 if( ssl->compress_buf != NULL )
6233 {
Paul Bakker34617722014-06-13 17:20:13 +02006234 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006235 polarssl_free( ssl->compress_buf );
6236 }
6237#endif
6238
Paul Bakker40e46942009-01-03 21:51:57 +00006239#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006240 mpi_free( &ssl->dhm_P );
6241 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006242#endif
6243
Paul Bakker48916f92012-09-16 19:57:18 +00006244 if( ssl->transform )
6245 {
6246 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006247 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006248 }
6249
6250 if( ssl->handshake )
6251 {
6252 ssl_handshake_free( ssl->handshake );
6253 ssl_transform_free( ssl->transform_negotiate );
6254 ssl_session_free( ssl->session_negotiate );
6255
Paul Bakker6e339b52013-07-03 13:37:05 +02006256 polarssl_free( ssl->handshake );
6257 polarssl_free( ssl->transform_negotiate );
6258 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006259 }
6260
Paul Bakkerc0463502013-02-14 11:19:38 +01006261 if( ssl->session )
6262 {
6263 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006264 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006265 }
6266
Paul Bakkera503a632013-08-14 13:48:06 +02006267#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006268 if( ssl->ticket_keys )
6269 {
6270 ssl_ticket_keys_free( ssl->ticket_keys );
6271 polarssl_free( ssl->ticket_keys );
6272 }
Paul Bakkera503a632013-08-14 13:48:06 +02006273#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006274
Paul Bakker0be444a2013-08-27 21:55:01 +02006275#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006276 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006277 {
Paul Bakker34617722014-06-13 17:20:13 +02006278 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006279 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006280 ssl->hostname_len = 0;
6281 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006283
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006284#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006285 if( ssl->psk != NULL )
6286 {
Paul Bakker34617722014-06-13 17:20:13 +02006287 polarssl_zeroize( ssl->psk, ssl->psk_len );
6288 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006289 polarssl_free( ssl->psk );
6290 polarssl_free( ssl->psk_identity );
6291 ssl->psk_len = 0;
6292 ssl->psk_identity_len = 0;
6293 }
6294#endif
6295
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006296#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006297 ssl_key_cert_free( ssl->key_cert );
6298#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006299
Paul Bakker05ef8352012-05-08 09:17:57 +00006300#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6301 if( ssl_hw_record_finish != NULL )
6302 {
6303 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6304 ssl_hw_record_finish( ssl );
6305 }
6306#endif
6307
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006308#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006309 polarssl_free( ssl->cli_id );
6310#endif
6311
Paul Bakker5121ce52009-01-03 21:22:43 +00006312 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006313
Paul Bakker86f04f42013-02-14 11:20:09 +01006314 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006315 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006316}
6317
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006318#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006319/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006320 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006321 */
6322unsigned char ssl_sig_from_pk( pk_context *pk )
6323{
6324#if defined(POLARSSL_RSA_C)
6325 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6326 return( SSL_SIG_RSA );
6327#endif
6328#if defined(POLARSSL_ECDSA_C)
6329 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6330 return( SSL_SIG_ECDSA );
6331#endif
6332 return( SSL_SIG_ANON );
6333}
6334
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006335pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6336{
6337 switch( sig )
6338 {
6339#if defined(POLARSSL_RSA_C)
6340 case SSL_SIG_RSA:
6341 return( POLARSSL_PK_RSA );
6342#endif
6343#if defined(POLARSSL_ECDSA_C)
6344 case SSL_SIG_ECDSA:
6345 return( POLARSSL_PK_ECDSA );
6346#endif
6347 default:
6348 return( POLARSSL_PK_NONE );
6349 }
6350}
Paul Bakker9af723c2014-05-01 13:03:14 +02006351#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006352
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006353/*
6354 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6355 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006356md_type_t ssl_md_alg_from_hash( unsigned char hash )
6357{
6358 switch( hash )
6359 {
6360#if defined(POLARSSL_MD5_C)
6361 case SSL_HASH_MD5:
6362 return( POLARSSL_MD_MD5 );
6363#endif
6364#if defined(POLARSSL_SHA1_C)
6365 case SSL_HASH_SHA1:
6366 return( POLARSSL_MD_SHA1 );
6367#endif
6368#if defined(POLARSSL_SHA256_C)
6369 case SSL_HASH_SHA224:
6370 return( POLARSSL_MD_SHA224 );
6371 case SSL_HASH_SHA256:
6372 return( POLARSSL_MD_SHA256 );
6373#endif
6374#if defined(POLARSSL_SHA512_C)
6375 case SSL_HASH_SHA384:
6376 return( POLARSSL_MD_SHA384 );
6377 case SSL_HASH_SHA512:
6378 return( POLARSSL_MD_SHA512 );
6379#endif
6380 default:
6381 return( POLARSSL_MD_NONE );
6382 }
6383}
6384
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006385#if defined(POLARSSL_SSL_SET_CURVES)
6386/*
6387 * Check is a curve proposed by the peer is in our list.
6388 * Return 1 if we're willing to use it, 0 otherwise.
6389 */
6390int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6391{
6392 const ecp_group_id *gid;
6393
6394 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6395 if( *gid == grp_id )
6396 return( 1 );
6397
6398 return( 0 );
6399}
Paul Bakker9af723c2014-05-01 13:03:14 +02006400#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006401
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006402#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006403int ssl_check_cert_usage( const x509_crt *cert,
6404 const ssl_ciphersuite_t *ciphersuite,
6405 int cert_endpoint )
6406{
6407#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6408 int usage = 0;
6409#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006410#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6411 const char *ext_oid;
6412 size_t ext_len;
6413#endif
6414
6415#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6416 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6417 ((void) cert);
6418 ((void) cert_endpoint);
6419#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006420
6421#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6422 if( cert_endpoint == SSL_IS_SERVER )
6423 {
6424 /* Server part of the key exchange */
6425 switch( ciphersuite->key_exchange )
6426 {
6427 case POLARSSL_KEY_EXCHANGE_RSA:
6428 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6429 usage = KU_KEY_ENCIPHERMENT;
6430 break;
6431
6432 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6433 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6434 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6435 usage = KU_DIGITAL_SIGNATURE;
6436 break;
6437
6438 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6439 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6440 usage = KU_KEY_AGREEMENT;
6441 break;
6442
6443 /* Don't use default: we want warnings when adding new values */
6444 case POLARSSL_KEY_EXCHANGE_NONE:
6445 case POLARSSL_KEY_EXCHANGE_PSK:
6446 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6447 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6448 usage = 0;
6449 }
6450 }
6451 else
6452 {
6453 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6454 usage = KU_DIGITAL_SIGNATURE;
6455 }
6456
6457 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6458 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006459#else
6460 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006461#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6462
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006463#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6464 if( cert_endpoint == SSL_IS_SERVER )
6465 {
6466 ext_oid = OID_SERVER_AUTH;
6467 ext_len = OID_SIZE( OID_SERVER_AUTH );
6468 }
6469 else
6470 {
6471 ext_oid = OID_CLIENT_AUTH;
6472 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6473 }
6474
6475 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6476 return( -1 );
6477#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6478
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006479 return( 0 );
6480}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006481#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006482
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006483/*
6484 * Convert version numbers to/from wire format
6485 * and, for DTLS, to/from TLS equivalent.
6486 *
6487 * For TLS this is the identity.
6488 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6489 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6490 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6491 */
6492void ssl_write_version( int major, int minor, int transport,
6493 unsigned char ver[2] )
6494{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006495#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006496 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006497 {
6498 if( minor == SSL_MINOR_VERSION_2 )
6499 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6500
6501 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6502 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6503 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006504 else
6505#else
6506 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006507#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006508 {
6509 ver[0] = (unsigned char) major;
6510 ver[1] = (unsigned char) minor;
6511 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006512}
6513
6514void ssl_read_version( int *major, int *minor, int transport,
6515 const unsigned char ver[2] )
6516{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006517#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006518 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006519 {
6520 *major = 255 - ver[0] + 2;
6521 *minor = 255 - ver[1] + 1;
6522
6523 if( *minor == SSL_MINOR_VERSION_1 )
6524 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6525 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006526 else
6527#else
6528 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006529#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006530 {
6531 *major = ver[0];
6532 *minor = ver[1];
6533 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006534}
6535
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006536#endif /* POLARSSL_SSL_TLS_C */