blob: 7c34d0f26ad96e37fa680e919018dda5abbd3581 [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 );
1894#endif
1895
Paul Bakker5121ce52009-01-03 21:22:43 +00001896/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001897 * Fill the input message buffer by appending data to it.
1898 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001899 *
1900 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1901 * available (from this read and/or a previous one). Otherwise, an error code
1902 * is returned (possibly EOF or WANT_READ).
1903 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001904 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1905 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1906 * since we always read a whole datagram at once.
1907 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001908 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001909 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 */
Paul Bakker23986e52011-04-24 08:57:21 +00001911int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001912{
Paul Bakker23986e52011-04-24 08:57:21 +00001913 int ret;
1914 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
1916 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1917
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001918 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1919 {
1920 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1921 "or ssl_set_bio_timeout()" ) );
1922 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1923 }
1924
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001925 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001926 {
1927 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1928 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1929 }
1930
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001931#if defined(POLARSSL_SSL_PROTO_DTLS)
1932 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001934 uint32_t timeout;
1935
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001936 /*
1937 * The point is, we need to always read a full datagram at once, so we
1938 * sometimes read more then requested, and handle the additional data.
1939 * It could be the rest of the current record (while fetching the
1940 * header) and/or some other records in the same datagram.
1941 */
1942
1943 /*
1944 * Move to the next record in the already read datagram if applicable
1945 */
1946 if( ssl->next_record_offset != 0 )
1947 {
1948 if( ssl->in_left < ssl->next_record_offset )
1949 {
1950 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1951 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1952 }
1953
1954 ssl->in_left -= ssl->next_record_offset;
1955
1956 if( ssl->in_left != 0 )
1957 {
1958 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1959 ssl->next_record_offset ) );
1960 memmove( ssl->in_hdr,
1961 ssl->in_hdr + ssl->next_record_offset,
1962 ssl->in_left );
1963 }
1964
1965 ssl->next_record_offset = 0;
1966 }
1967
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1969 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001970
1971 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001972 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001973 */
1974 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001975 {
1976 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001977 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001978 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001979
1980 /*
1981 * A record can't be split accross datagrams. If we need to read but
1982 * are not at the beginning of a new record, the caller did something
1983 * wrong.
1984 */
1985 if( ssl->in_left != 0 )
1986 {
1987 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1988 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1989 }
1990
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001991 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001992
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001993 /*
1994 * Don't even try to read if time's out already.
1995 * This avoids by-passing the timer when repeatedly receiving messages
1996 * that will end up being dropped.
1997 */
1998 if( ssl_check_timer( ssl ) != 0 )
1999 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002000 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002001 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002002 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2003
2004 if( ssl->state != SSL_HANDSHAKE_OVER )
2005 timeout = ssl->handshake->retransmit_timeout;
2006 else
2007 timeout = ssl->read_timeout;
2008
2009 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2010
2011 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2012 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2013 timeout );
2014 else
2015 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2016
2017 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2018
2019 if( ret == 0 )
2020 return( POLARSSL_ERR_SSL_CONN_EOF );
2021 }
2022
2023 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2024 {
2025 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2026 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002027
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002028 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002029 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002030 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2031 {
2032 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2033 return( POLARSSL_ERR_NET_TIMEOUT );
2034 }
2035
2036 if( ( ret = ssl_resend( ssl ) ) != 0 )
2037 {
2038 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2039 return( ret );
2040 }
2041
2042 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002043 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002044#if defined(POLARSSL_SSL_SRV_C)
2045 else if( ssl->endpoint == SSL_IS_SERVER &&
2046 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
2047 {
2048 if( ( ret = ssl_write_hello_request( ssl ) ) != 0 )
2049 {
2050 SSL_DEBUG_RET( 1, "ssl_write_hello_request", ret );
2051 return( ret );
2052 }
2053
2054 return( POLARSSL_ERR_NET_WANT_READ );
2055 }
2056#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002057 }
2058
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 if( ret < 0 )
2060 return( ret );
2061
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002062 ssl->in_left = ret;
2063 }
2064 else
2065#endif
2066 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002067 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2068 ssl->in_left, nb_want ) );
2069
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002070 while( ssl->in_left < nb_want )
2071 {
2072 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002073 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002074
2075 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2076 ssl->in_left, nb_want ) );
2077 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2078
2079 if( ret == 0 )
2080 return( POLARSSL_ERR_SSL_CONN_EOF );
2081
2082 if( ret < 0 )
2083 return( ret );
2084
2085 ssl->in_left += ret;
2086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 }
2088
2089 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2090
2091 return( 0 );
2092}
2093
2094/*
2095 * Flush any data not yet written
2096 */
2097int ssl_flush_output( ssl_context *ssl )
2098{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002099 int ret;
2100 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
2102 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2103
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002104 if( ssl->f_send == NULL )
2105 {
2106 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2107 "or ssl_set_bio_timeout()" ) );
2108 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2109 }
2110
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002111 /* Avoid incrementing counter if data is flushed */
2112 if( ssl->out_left == 0 )
2113 {
2114 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2115 return( 0 );
2116 }
2117
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 while( ssl->out_left > 0 )
2119 {
2120 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002121 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002123 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2124 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002125 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002126
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2128
2129 if( ret <= 0 )
2130 return( ret );
2131
2132 ssl->out_left -= ret;
2133 }
2134
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002135 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002136 if( ++ssl->out_ctr[i - 1] != 0 )
2137 break;
2138
2139 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002140 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002141 {
2142 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2143 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2144 }
2145
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2147
2148 return( 0 );
2149}
2150
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002151/*
2152 * Functions to handle the DTLS retransmission state machine
2153 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002154#if defined(POLARSSL_SSL_PROTO_DTLS)
2155/*
2156 * Append current handshake message to current outgoing flight
2157 */
2158static int ssl_flight_append( ssl_context *ssl )
2159{
2160 ssl_flight_item *msg;
2161
2162 /* Allocate space for current message */
2163 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2164 {
2165 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2166 sizeof( ssl_flight_item ) ) );
2167 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2168 }
2169
2170 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2171 {
2172 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2173 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2174 }
2175
2176 /* Copy current handshake message with headers */
2177 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2178 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002179 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002180 msg->next = NULL;
2181
2182 /* Append to the current flight */
2183 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002184 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002185 else
2186 {
2187 ssl_flight_item *cur = ssl->handshake->flight;
2188 while( cur->next != NULL )
2189 cur = cur->next;
2190 cur->next = msg;
2191 }
2192
2193 return( 0 );
2194}
2195
2196/*
2197 * Free the current flight of handshake messages
2198 */
2199static void ssl_flight_free( ssl_flight_item *flight )
2200{
2201 ssl_flight_item *cur = flight;
2202 ssl_flight_item *next;
2203
2204 while( cur != NULL )
2205 {
2206 next = cur->next;
2207
2208 polarssl_free( cur->p );
2209 polarssl_free( cur );
2210
2211 cur = next;
2212 }
2213}
2214
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002215#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2216static void ssl_dtls_replay_reset( ssl_context *ssl );
2217#endif
2218
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002219/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002220 * Swap transform_out and out_ctr with the alternative ones
2221 */
2222static void ssl_swap_epochs( ssl_context *ssl )
2223{
2224 ssl_transform *tmp_transform;
2225 unsigned char tmp_out_ctr[8];
2226
2227 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2228 {
2229 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2230 return;
2231 }
2232
2233 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2234
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002235 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002236 tmp_transform = ssl->transform_out;
2237 ssl->transform_out = ssl->handshake->alt_transform_out;
2238 ssl->handshake->alt_transform_out = tmp_transform;
2239
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002240 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002241 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2242 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2243 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002244
2245 /* Adjust to the newly activated transform */
2246 if( ssl->transform_out != NULL &&
2247 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2248 {
2249 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2250 ssl->transform_out->fixed_ivlen;
2251 }
2252 else
2253 ssl->out_msg = ssl->out_iv;
2254
2255#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2256 if( ssl_hw_record_activate != NULL )
2257 {
2258 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2259 {
2260 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2261 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2262 }
2263 }
2264#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002265}
2266
2267/*
2268 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002269 *
2270 * Need to remember the current message in case flush_output returns
2271 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002272 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002273 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002274int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002275{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002276 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002277
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002278 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2279 {
2280 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2281
2282 ssl->handshake->cur_msg = ssl->handshake->flight;
2283 ssl_swap_epochs( ssl );
2284
2285 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2286 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002287
2288 while( ssl->handshake->cur_msg != NULL )
2289 {
2290 int ret;
2291 ssl_flight_item *cur = ssl->handshake->cur_msg;
2292
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002293 /* Swap epochs before sending Finished: we can't do it after
2294 * sending ChangeCipherSpec, in case write returns WANT_READ.
2295 * Must be done before copying, may change out_msg pointer */
2296 if( cur->type == SSL_MSG_HANDSHAKE &&
2297 cur->p[0] == SSL_HS_FINISHED )
2298 {
2299 ssl_swap_epochs( ssl );
2300 }
2301
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002302 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002303 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002304 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002305
2306 ssl->handshake->cur_msg = cur->next;
2307
2308 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2309
2310 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2311 {
2312 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2313 return( ret );
2314 }
2315 }
2316
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002317 if( ssl->state == SSL_HANDSHAKE_OVER )
2318 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2319 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002320 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002321 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002322 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2323 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002324
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002325 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002326
2327 return( 0 );
2328}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002329
2330/*
2331 * To be called when the last message of an incoming flight is received.
2332 */
2333void ssl_recv_flight_completed( ssl_context *ssl )
2334{
2335 /* We won't need to resend that one any more */
2336 ssl_flight_free( ssl->handshake->flight );
2337 ssl->handshake->flight = NULL;
2338 ssl->handshake->cur_msg = NULL;
2339
2340 /* The next incoming flight will start with this msg_seq */
2341 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2342
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002343 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002344 ssl_set_timer( ssl, 0 );
2345
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002346 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2347 ssl->in_msg[0] == SSL_HS_FINISHED )
2348 {
2349 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2350 }
2351 else
2352 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2353}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002354
2355/*
2356 * To be called when the last message of an outgoing flight is send.
2357 */
2358void ssl_send_flight_completed( ssl_context *ssl )
2359{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002360 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002361 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002362
2363 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2364 ssl->in_msg[0] == SSL_HS_FINISHED )
2365 {
2366 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2367 }
2368 else
2369 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2370}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002371#endif /* POLARSSL_SSL_PROTO_DTLS */
2372
Paul Bakker5121ce52009-01-03 21:22:43 +00002373/*
2374 * Record layer functions
2375 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002376
2377/*
2378 * Write current record.
2379 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2380 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002381int ssl_write_record( ssl_context *ssl )
2382{
Paul Bakker05ef8352012-05-08 09:17:57 +00002383 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002384 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
2386 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2387
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002388#if defined(POLARSSL_SSL_PROTO_DTLS)
2389 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2390 ssl->handshake != NULL &&
2391 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2392 {
2393 ; /* Skip special handshake treatment when resending */
2394 }
2395 else
2396#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2398 {
2399 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2400 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2401 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2402
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002403 /*
2404 * DTLS has additional fields in the Handshake layer,
2405 * between the length field and the actual payload:
2406 * uint16 message_seq;
2407 * uint24 fragment_offset;
2408 * uint24 fragment_length;
2409 */
2410#if defined(POLARSSL_SSL_PROTO_DTLS)
2411 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2412 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002413 /* Make room for the additional DTLS fields */
2414 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002415 ssl->out_msglen += 8;
2416 len += 8;
2417
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002418 /* Write message_seq and update it, except for HelloRequest */
2419 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2420 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002421 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2422 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2423 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002424 }
2425 else
2426 {
2427 ssl->out_msg[4] = 0;
2428 ssl->out_msg[5] = 0;
2429 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002430
2431 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2432 memset( ssl->out_msg + 6, 0x00, 3 );
2433 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002434 }
2435#endif /* POLARSSL_SSL_PROTO_DTLS */
2436
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002437 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2438 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 }
2440
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002441 /* Save handshake and CCS messages for resending */
2442#if defined(POLARSSL_SSL_PROTO_DTLS)
2443 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2444 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002445 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002446 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2447 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2448 {
2449 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2450 {
2451 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2452 return( ret );
2453 }
2454 }
2455#endif
2456
Paul Bakker2770fbd2012-07-03 13:30:23 +00002457#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002458 if( ssl->transform_out != NULL &&
2459 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002460 {
2461 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2462 {
2463 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2464 return( ret );
2465 }
2466
2467 len = ssl->out_msglen;
2468 }
2469#endif /*POLARSSL_ZLIB_SUPPORT */
2470
Paul Bakker05ef8352012-05-08 09:17:57 +00002471#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002472 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002474 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002475
Paul Bakker05ef8352012-05-08 09:17:57 +00002476 ret = ssl_hw_record_write( ssl );
2477 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2478 {
2479 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002480 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002481 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002482
2483 if( ret == 0 )
2484 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002485 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002486#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002487 if( !done )
2488 {
2489 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002490 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2491 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002492
2493 ssl->out_len[0] = (unsigned char)( len >> 8 );
2494 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002495
Paul Bakker48916f92012-09-16 19:57:18 +00002496 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002497 {
2498 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2499 {
2500 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2501 return( ret );
2502 }
2503
2504 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002505 ssl->out_len[0] = (unsigned char)( len >> 8 );
2506 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002507 }
2508
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002509 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002510
2511 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2512 "version = [%d:%d], msglen = %d",
2513 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002514 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002515
2516 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002517 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 }
2519
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2521 {
2522 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2523 return( ret );
2524 }
2525
2526 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2527
2528 return( 0 );
2529}
2530
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002531#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002532/*
2533 * Mark bits in bitmask (used for DTLS HS reassembly)
2534 */
2535static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2536{
2537 unsigned int start_bits, end_bits;
2538
2539 start_bits = 8 - ( offset % 8 );
2540 if( start_bits != 8 )
2541 {
2542 size_t first_byte_idx = offset / 8;
2543
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002544 /* Special case */
2545 if( len <= start_bits )
2546 {
2547 for( ; len != 0; len-- )
2548 mask[first_byte_idx] |= 1 << ( start_bits - len );
2549
2550 /* Avoid potential issues with offset or len becoming invalid */
2551 return;
2552 }
2553
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002554 offset += start_bits; /* Now offset % 8 == 0 */
2555 len -= start_bits;
2556
2557 for( ; start_bits != 0; start_bits-- )
2558 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2559 }
2560
2561 end_bits = len % 8;
2562 if( end_bits != 0 )
2563 {
2564 size_t last_byte_idx = ( offset + len ) / 8;
2565
2566 len -= end_bits; /* Now len % 8 == 0 */
2567
2568 for( ; end_bits != 0; end_bits-- )
2569 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2570 }
2571
2572 memset( mask + offset / 8, 0xFF, len / 8 );
2573}
2574
2575/*
2576 * Check that bitmask is full
2577 */
2578static int ssl_bitmask_check( unsigned char *mask, size_t len )
2579{
2580 size_t i;
2581
2582 for( i = 0; i < len / 8; i++ )
2583 if( mask[i] != 0xFF )
2584 return( -1 );
2585
2586 for( i = 0; i < len % 8; i++ )
2587 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2588 return( -1 );
2589
2590 return( 0 );
2591}
2592
2593/*
2594 * Reassemble fragmented DTLS handshake messages.
2595 *
2596 * Use a temporary buffer for reassembly, divided in two parts:
2597 * - the first holds the reassembled message (including handshake header),
2598 * - the second holds a bitmask indicating which parts of the message
2599 * (excluding headers) have been received so far.
2600 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002601static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2602{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002603 unsigned char *msg, *bitmask;
2604 size_t frag_len, frag_off;
2605 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2606
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002607 if( ssl->handshake == NULL )
2608 {
2609 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2610 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2611 }
2612
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002613 /*
2614 * For first fragment, check size and allocate buffer
2615 */
2616 if( ssl->handshake->hs_msg == NULL )
2617 {
2618 size_t alloc_len;
2619
2620 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2621 msg_len ) );
2622
2623 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2624 {
2625 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2626 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2627 }
2628
2629 /* The bitmask needs one bit per byte of message excluding header */
2630 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2631
2632 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2633 if( ssl->handshake->hs_msg == NULL )
2634 {
2635 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2636 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2637 }
2638
2639 memset( ssl->handshake->hs_msg, 0, alloc_len );
2640
2641 /* Prepare final header: copy msg_type, length and message_seq,
2642 * then add standardised fragment_offset and fragment_length */
2643 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2644 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2645 memcpy( ssl->handshake->hs_msg + 9,
2646 ssl->handshake->hs_msg + 1, 3 );
2647 }
2648 else
2649 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002650 /* Make sure msg_type and length are consistent */
2651 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002652 {
2653 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2654 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2655 }
2656 }
2657
2658 msg = ssl->handshake->hs_msg + 12;
2659 bitmask = msg + msg_len;
2660
2661 /*
2662 * Check and copy current fragment
2663 */
2664 frag_off = ( ssl->in_msg[6] << 16 ) |
2665 ( ssl->in_msg[7] << 8 ) |
2666 ssl->in_msg[8];
2667 frag_len = ( ssl->in_msg[9] << 16 ) |
2668 ( ssl->in_msg[10] << 8 ) |
2669 ssl->in_msg[11];
2670
2671 if( frag_off + frag_len > msg_len )
2672 {
2673 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2674 frag_off, frag_len, msg_len ) );
2675 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2676 }
2677
2678 if( frag_len + 12 > ssl->in_msglen )
2679 {
2680 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2681 frag_len, ssl->in_msglen ) );
2682 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2683 }
2684
2685 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2686 frag_off, frag_len ) );
2687
2688 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2689 ssl_bitmask_set( bitmask, frag_off, frag_len );
2690
2691 /*
2692 * Do we have the complete message by now?
2693 * If yes, finalize it, else ask to read the next record.
2694 */
2695 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2696 {
2697 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2698 return( POLARSSL_ERR_NET_WANT_READ );
2699 }
2700
2701 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2702
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002703 if( frag_len + 12 < ssl->in_msglen )
2704 {
2705 /*
2706 * We'got more handshake messages in the same record.
2707 * This case is not handled now because no know implementation does
2708 * that and it's hard to test, so we prefer to fail cleanly for now.
2709 */
2710 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2711 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2712 }
2713
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002714 if( ssl->in_left > ssl->next_record_offset )
2715 {
2716 /*
2717 * We've got more data in the buffer after the current record,
2718 * that we don't want to overwrite. Move it before writing the
2719 * reassembled message, and adjust in_left and next_record_offset.
2720 */
2721 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2722 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2723 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2724
2725 /* First compute and check new lengths */
2726 ssl->next_record_offset = new_remain - ssl->in_hdr;
2727 ssl->in_left = ssl->next_record_offset + remain_len;
2728
2729 if( ssl->in_left > SSL_BUFFER_LEN -
2730 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2731 {
2732 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2733 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2734 }
2735
2736 memmove( new_remain, cur_remain, remain_len );
2737 }
2738
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002739 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2740
2741 polarssl_free( ssl->handshake->hs_msg );
2742 ssl->handshake->hs_msg = NULL;
2743
2744 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2745 ssl->in_msg, ssl->in_hslen );
2746
2747 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002748}
2749#endif /* POLARSSL_SSL_PROTO_DTLS */
2750
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002751static int ssl_prepare_handshake_record( ssl_context *ssl )
2752{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002753 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2754 {
2755 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2756 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002757 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002758 }
2759
2760 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2761 ( ssl->in_msg[1] << 16 ) |
2762 ( ssl->in_msg[2] << 8 ) |
2763 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002764
2765 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2766 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002767 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002768
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002769#if defined(POLARSSL_SSL_PROTO_DTLS)
2770 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002771 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002772 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002773 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002774
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002775 /* ssl->handshake is NULL when receiving ClientHello for renego */
2776 if( ssl->handshake != NULL &&
2777 recv_msg_seq != ssl->handshake->in_msg_seq )
2778 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002779 /* Retransmit only on last message from previous flight, to avoid
2780 * too many retransmissions.
2781 * Besides, No sane server ever retransmits HelloVerifyRequest */
2782 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002783 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002784 {
2785 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2786 "message_seq = %d, start_of_flight = %d",
2787 recv_msg_seq,
2788 ssl->handshake->in_flight_start_seq ) );
2789
2790 if( ( ret = ssl_resend( ssl ) ) != 0 )
2791 {
2792 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2793 return( ret );
2794 }
2795 }
2796 else
2797 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002798 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002799 "message_seq = %d, expected = %d",
2800 recv_msg_seq,
2801 ssl->handshake->in_msg_seq ) );
2802 }
2803
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002804 return( POLARSSL_ERR_NET_WANT_READ );
2805 }
2806 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002807
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002808 /* Reassemble if current message is fragmented or reassembly is
2809 * already in progress */
2810 if( ssl->in_msglen < ssl->in_hslen ||
2811 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2812 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2813 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002814 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002815 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2816
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002817 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2818 {
2819 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2820 return( ret );
2821 }
2822 }
2823 }
2824 else
2825#endif /* POLARSSL_SSL_PROTO_DTLS */
2826 /* With TLS we don't handle fragmentation (for now) */
2827 if( ssl->in_msglen < ssl->in_hslen )
2828 {
2829 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002830 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002831 }
2832
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002833 if( ssl->state != SSL_HANDSHAKE_OVER )
2834 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2835
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002836 /* Handshake message is complete, increment counter */
2837#if defined(POLARSSL_SSL_PROTO_DTLS)
2838 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2839 ssl->handshake != NULL )
2840 {
2841 ssl->handshake->in_msg_seq++;
2842 }
2843#endif
2844
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002845 return( 0 );
2846}
2847
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002848/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002849 * DTLS anti-replay: RFC 6347 4.1.2.6
2850 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002851 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2852 * Bit n is set iff record number in_window_top - n has been seen.
2853 *
2854 * Usually, in_window_top is the last record number seen and the lsb of
2855 * in_window is set. The only exception is the initial state (record number 0
2856 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002857 */
2858#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2859static void ssl_dtls_replay_reset( ssl_context *ssl )
2860{
2861 ssl->in_window_top = 0;
2862 ssl->in_window = 0;
2863}
2864
2865static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2866{
2867 return( ( (uint64_t) buf[0] << 40 ) |
2868 ( (uint64_t) buf[1] << 32 ) |
2869 ( (uint64_t) buf[2] << 24 ) |
2870 ( (uint64_t) buf[3] << 16 ) |
2871 ( (uint64_t) buf[4] << 8 ) |
2872 ( (uint64_t) buf[5] ) );
2873}
2874
2875/*
2876 * Return 0 if sequence number is acceptable, -1 otherwise
2877 */
2878int ssl_dtls_replay_check( ssl_context *ssl )
2879{
2880 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2881 uint64_t bit;
2882
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002883 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2884 return( 0 );
2885
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002886 if( rec_seqnum > ssl->in_window_top )
2887 return( 0 );
2888
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002889 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002890
2891 if( bit >= 64 )
2892 return( -1 );
2893
2894 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2895 return( -1 );
2896
2897 return( 0 );
2898}
2899
2900/*
2901 * Update replay window on new validated record
2902 */
2903void ssl_dtls_replay_update( ssl_context *ssl )
2904{
2905 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2906
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002907 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2908 return;
2909
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002910 if( rec_seqnum > ssl->in_window_top )
2911 {
2912 /* Update window_top and the contents of the window */
2913 uint64_t shift = rec_seqnum - ssl->in_window_top;
2914
2915 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002916 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002917 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002918 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002919 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002920 ssl->in_window |= 1;
2921 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002922
2923 ssl->in_window_top = rec_seqnum;
2924 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002925 else
2926 {
2927 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002928 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002929
2930 if( bit < 64 ) /* Always true, but be extra sure */
2931 ssl->in_window |= (uint64_t) 1 << bit;
2932 }
2933}
2934#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2935
2936/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002937 * ContentType type;
2938 * ProtocolVersion version;
2939 * uint16 epoch; // DTLS only
2940 * uint48 sequence_number; // DTLS only
2941 * uint16 length;
2942 */
2943static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002944{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002945 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002946 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002948 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2949
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002951 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002952 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
2954 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2955 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002956 ssl->in_msgtype,
2957 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002958
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002959 /* Check record type */
2960 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2961 ssl->in_msgtype != SSL_MSG_ALERT &&
2962 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2963 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2964 {
2965 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002966
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002967 if( ( ret = ssl_send_alert_message( ssl,
2968 SSL_ALERT_LEVEL_FATAL,
2969 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2970 {
2971 return( ret );
2972 }
2973
2974 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2975 }
2976
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002977#if defined(POLARSSL_SSL_PROTO_DTLS)
2978 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2979 {
2980 /* Drop unexpected ChangeCipherSpec messages */
2981 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
2982 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
2983 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
2984 {
2985 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
2986 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2987 }
2988
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02002989 /* Drop unexpected ApplicationData records,
2990 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002991 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02002992 ssl->state != SSL_HANDSHAKE_OVER &&
2993 ! ( ssl->renegotiation == SSL_RENEGOTIATION &&
2994 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002995 {
2996 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
2997 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2998 }
2999 }
3000#endif
3001
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003002 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003003 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003004 {
3005 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003006 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 }
3008
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003009 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 {
3011 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003012 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 }
3014
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003015 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003016#if defined(POLARSSL_SSL_PROTO_DTLS)
3017 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3018 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003019 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003020
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003021 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003022 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003023 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003024 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003025 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003026 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003027 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003028
3029#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3030 if( ssl_dtls_replay_check( ssl ) != 0 )
3031 {
3032 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3033 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3034 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003035#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003036 }
3037#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003038
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003039 /* Check length against the size of our buffer */
3040 if( ssl->in_msglen > SSL_BUFFER_LEN
3041 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003042 {
3043 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3044 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3045 }
3046
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003047 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003048 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003049 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003050 if( ssl->in_msglen < 1 ||
3051 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 {
3053 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003054 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003055 }
3056 }
3057 else
3058 {
Paul Bakker48916f92012-09-16 19:57:18 +00003059 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003060 {
3061 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003062 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003063 }
3064
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003065#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003066 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003067 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003068 {
3069 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003070 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003071 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003072#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003073#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3074 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003075 /*
3076 * TLS encrypted messages can have up to 256 bytes of padding
3077 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003079 ssl->in_msglen > ssl->transform_in->minlen +
3080 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003081 {
3082 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003083 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003085#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003086 }
3087
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003088 return( 0 );
3089}
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003091/*
3092 * If applicable, decrypt (and decompress) record content
3093 */
3094static int ssl_prepare_record_content( ssl_context *ssl )
3095{
3096 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003097
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003099 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Paul Bakker05ef8352012-05-08 09:17:57 +00003101#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003102 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003103 {
3104 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3105
3106 ret = ssl_hw_record_read( ssl );
3107 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3108 {
3109 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003110 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003111 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003112
3113 if( ret == 0 )
3114 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003115 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003116#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003117 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003118 {
3119 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3120 {
3121 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3122 return( ret );
3123 }
3124
3125 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3126 ssl->in_msg, ssl->in_msglen );
3127
3128 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3129 {
3130 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003131 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 }
3133 }
3134
Paul Bakker2770fbd2012-07-03 13:30:23 +00003135#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003136 if( ssl->transform_in != NULL &&
3137 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003138 {
3139 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3140 {
3141 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3142 return( ret );
3143 }
3144
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003145 // TODO: what's the purpose of these lines? is in_len used?
3146 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3147 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003148 }
3149#endif /* POLARSSL_ZLIB_SUPPORT */
3150
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003151#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003152 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3153 {
3154 ssl_dtls_replay_update( ssl );
3155 }
3156#endif
3157
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003158 return( 0 );
3159}
3160
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003161static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3162
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003163/*
3164 * Read a record.
3165 *
3166 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3167 * and continue reading until a valid record is found.
3168 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003169int ssl_read_record( ssl_context *ssl )
3170{
3171 int ret;
3172
3173 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3174
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003175 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003176 {
3177 /*
3178 * Get next Handshake message in the current record
3179 */
3180 ssl->in_msglen -= ssl->in_hslen;
3181
3182 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3183 ssl->in_msglen );
3184
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003185 SSL_DEBUG_BUF( 4, "remaining content in record",
3186 ssl->in_msg, ssl->in_msglen );
3187
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003188 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3189 return( ret );
3190
3191 return( 0 );
3192 }
3193
3194 ssl->in_hslen = 0;
3195
3196 /*
3197 * Read the record header and parse it
3198 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003199#if defined(POLARSSL_SSL_PROTO_DTLS)
3200read_record_header:
3201#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003202 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3203 {
3204 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3205 return( ret );
3206 }
3207
3208 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003209 {
3210#if defined(POLARSSL_SSL_PROTO_DTLS)
3211 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3212 {
3213 /* Ignore bad record and get next one; drop the whole datagram
3214 * since current header cannot be trusted to find the next record
3215 * in current datagram */
3216 ssl->next_record_offset = 0;
3217 ssl->in_left = 0;
3218
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003219 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003220 goto read_record_header;
3221 }
3222#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003223 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003224 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003225
3226 /*
3227 * Read and optionally decrypt the message contents
3228 */
3229 if( ( ret = ssl_fetch_input( ssl,
3230 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3231 {
3232 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3233 return( ret );
3234 }
3235
3236 /* Done reading this record, get ready for the next one */
3237#if defined(POLARSSL_SSL_PROTO_DTLS)
3238 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3239 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3240 else
3241#endif
3242 ssl->in_left = 0;
3243
3244 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003245 {
3246#if defined(POLARSSL_SSL_PROTO_DTLS)
3247 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3248 {
3249 /* Silently discard invalid records */
3250 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3251 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3252 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003253#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3254 if( ssl->badmac_limit != 0 &&
3255 ++ssl->badmac_seen >= ssl->badmac_limit )
3256 {
3257 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3258 return( POLARSSL_ERR_SSL_INVALID_MAC );
3259 }
3260#endif
3261
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003262 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003263 goto read_record_header;
3264 }
3265
3266 return( ret );
3267 }
3268 else
3269#endif
3270 {
3271 /* Error out (and send alert) on invalid records */
3272#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3273 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3274 {
3275 ssl_send_alert_message( ssl,
3276 SSL_ALERT_LEVEL_FATAL,
3277 SSL_ALERT_MSG_BAD_RECORD_MAC );
3278 }
3279#endif
3280 return( ret );
3281 }
3282 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003283
3284 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003285 * When we sent the last flight of the handshake, we MUST respond to a
3286 * retransmit of the peer's previous flight with a retransmit. (In
3287 * practice, only the Finished message will make it, other messages
3288 * including CCS use the old transform so they're dropped as invalid.)
3289 *
3290 * If the record we received is not a handshake message, however, it
3291 * means the peer received our last flight so we can clean up
3292 * handshake info.
3293 *
3294 * This check needs to be done before prepare_handshake() due to an edge
3295 * case: if the client immediately requests renegotiation, this
3296 * finishes the current handshake first, avoiding the new ClientHello
3297 * being mistaken for an ancient message in the current handshake.
3298 */
3299#if defined(POLARSSL_SSL_PROTO_DTLS)
3300 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3301 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003302 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003303 {
3304 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3305 ssl->in_msg[0] == SSL_HS_FINISHED )
3306 {
3307 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3308
3309 if( ( ret = ssl_resend( ssl ) ) != 0 )
3310 {
3311 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3312 return( ret );
3313 }
3314
3315 return( POLARSSL_ERR_NET_WANT_READ );
3316 }
3317 else
3318 {
3319 ssl_handshake_wrapup_free_hs_transform( ssl );
3320 }
3321 }
3322#endif
3323
3324 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003325 * Handle particular types of records
3326 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003327 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3328 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003329 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3330 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 }
3332
3333 if( ssl->in_msgtype == SSL_MSG_ALERT )
3334 {
3335 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3336 ssl->in_msg[0], ssl->in_msg[1] ) );
3337
3338 /*
3339 * Ignore non-fatal alerts, except close_notify
3340 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003341 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003343 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3344 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003345 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 }
3347
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003348 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3349 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 {
3351 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003352 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 }
3354 }
3355
Paul Bakker5121ce52009-01-03 21:22:43 +00003356 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3357
3358 return( 0 );
3359}
3360
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003361int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3362{
3363 int ret;
3364
3365 if( ( ret = ssl_send_alert_message( ssl,
3366 SSL_ALERT_LEVEL_FATAL,
3367 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3368 {
3369 return( ret );
3370 }
3371
3372 return( 0 );
3373}
3374
Paul Bakker0a925182012-04-16 06:46:41 +00003375int ssl_send_alert_message( ssl_context *ssl,
3376 unsigned char level,
3377 unsigned char message )
3378{
3379 int ret;
3380
3381 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3382
3383 ssl->out_msgtype = SSL_MSG_ALERT;
3384 ssl->out_msglen = 2;
3385 ssl->out_msg[0] = level;
3386 ssl->out_msg[1] = message;
3387
3388 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3389 {
3390 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3391 return( ret );
3392 }
3393
3394 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3395
3396 return( 0 );
3397}
3398
Paul Bakker5121ce52009-01-03 21:22:43 +00003399/*
3400 * Handshake functions
3401 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003402#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3403 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3404 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3405 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3406 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3407 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3408 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003409int ssl_write_certificate( ssl_context *ssl )
3410{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003411 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003412
3413 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3414
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003415 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003416 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3417 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003418 {
3419 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3420 ssl->state++;
3421 return( 0 );
3422 }
3423
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003424 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3425 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003426}
3427
3428int ssl_parse_certificate( ssl_context *ssl )
3429{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003430 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3431
3432 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3433
3434 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003435 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3436 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003437 {
3438 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3439 ssl->state++;
3440 return( 0 );
3441 }
3442
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003443 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3444 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003445}
3446#else
3447int ssl_write_certificate( ssl_context *ssl )
3448{
3449 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3450 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003451 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003452 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3453
3454 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3455
3456 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003457 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3458 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003459 {
3460 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3461 ssl->state++;
3462 return( 0 );
3463 }
3464
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 if( ssl->endpoint == SSL_IS_CLIENT )
3466 {
3467 if( ssl->client_auth == 0 )
3468 {
3469 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3470 ssl->state++;
3471 return( 0 );
3472 }
3473
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003474#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 /*
3476 * If using SSLv3 and got no cert, send an Alert message
3477 * (otherwise an empty Certificate message will be sent).
3478 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003479 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003480 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3481 {
3482 ssl->out_msglen = 2;
3483 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003484 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3485 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003486
3487 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3488 goto write_msg;
3489 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003490#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 }
3492 else /* SSL_IS_SERVER */
3493 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003494 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003495 {
3496 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003497 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003498 }
3499 }
3500
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003501 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003502
3503 /*
3504 * 0 . 0 handshake type
3505 * 1 . 3 handshake length
3506 * 4 . 6 length of all certs
3507 * 7 . 9 length of cert. 1
3508 * 10 . n-1 peer certificate
3509 * n . n+2 length of cert. 2
3510 * n+3 . ... upper level cert, etc.
3511 */
3512 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003513 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003514
Paul Bakker29087132010-03-21 21:03:34 +00003515 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003516 {
3517 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003518 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003519 {
3520 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3521 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003522 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003523 }
3524
3525 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3526 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3527 ssl->out_msg[i + 2] = (unsigned char)( n );
3528
3529 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3530 i += n; crt = crt->next;
3531 }
3532
3533 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3534 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3535 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3536
3537 ssl->out_msglen = i;
3538 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3539 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3540
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003541#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003542write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003543#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003544
3545 ssl->state++;
3546
3547 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3548 {
3549 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3550 return( ret );
3551 }
3552
3553 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3554
Paul Bakkered27a042013-04-18 22:46:23 +02003555 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003556}
3557
3558int ssl_parse_certificate( ssl_context *ssl )
3559{
Paul Bakkered27a042013-04-18 22:46:23 +02003560 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003561 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003562 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003563
3564 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3565
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003566 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003567 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3568 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003569 {
3570 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3571 ssl->state++;
3572 return( 0 );
3573 }
3574
Paul Bakker5121ce52009-01-03 21:22:43 +00003575 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003576 ( ssl->authmode == SSL_VERIFY_NONE ||
3577 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003579 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3581 ssl->state++;
3582 return( 0 );
3583 }
3584
3585 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3586 {
3587 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3588 return( ret );
3589 }
3590
3591 ssl->state++;
3592
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003593#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 /*
3595 * Check if the client sent an empty certificate
3596 */
3597 if( ssl->endpoint == SSL_IS_SERVER &&
3598 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3599 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003600 if( ssl->in_msglen == 2 &&
3601 ssl->in_msgtype == SSL_MSG_ALERT &&
3602 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3603 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 {
3605 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3606
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003607 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3609 return( 0 );
3610 else
Paul Bakker40e46942009-01-03 21:51:57 +00003611 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003612 }
3613 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003614#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003615
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003616#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3617 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003618 if( ssl->endpoint == SSL_IS_SERVER &&
3619 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3620 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003621 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003622 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3623 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003624 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003625 {
3626 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3627
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003628 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003629 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003630 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 else
3632 return( 0 );
3633 }
3634 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003635#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3636 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003637
3638 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3639 {
3640 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003641 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 }
3643
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003644 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3645 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003646 {
3647 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003648 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 }
3650
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003651 i = ssl_hs_hdr_len( ssl );
3652
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 /*
3654 * Same message structure as in ssl_write_certificate()
3655 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003656 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003657
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003658 if( ssl->in_msg[i] != 0 ||
3659 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 {
3661 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003662 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003663 }
3664
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003665 /* In case we tried to reuse a session but it failed */
3666 if( ssl->session_negotiate->peer_cert != NULL )
3667 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003668 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003669 polarssl_free( ssl->session_negotiate->peer_cert );
3670 }
3671
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003672 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3673 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003674 {
3675 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003676 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003677 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 }
3679
Paul Bakkerb6b09562013-09-18 14:17:41 +02003680 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003682 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003683
3684 while( i < ssl->in_hslen )
3685 {
3686 if( ssl->in_msg[i] != 0 )
3687 {
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
3692 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3693 | (unsigned int) ssl->in_msg[i + 2];
3694 i += 3;
3695
3696 if( n < 128 || i + n > ssl->in_hslen )
3697 {
3698 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003699 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 }
3701
Paul Bakkerddf26b42013-09-18 13:46:23 +02003702 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3703 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003704 if( ret != 0 )
3705 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003706 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 return( ret );
3708 }
3709
3710 i += n;
3711 }
3712
Paul Bakker48916f92012-09-16 19:57:18 +00003713 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003714
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003715 /*
3716 * On client, make sure the server cert doesn't change during renego to
3717 * avoid "triple handshake" attack: https://secure-resumption.com/
3718 */
3719 if( ssl->endpoint == SSL_IS_CLIENT &&
3720 ssl->renegotiation == SSL_RENEGOTIATION )
3721 {
3722 if( ssl->session->peer_cert == NULL )
3723 {
3724 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3725 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3726 }
3727
3728 if( ssl->session->peer_cert->raw.len !=
3729 ssl->session_negotiate->peer_cert->raw.len ||
3730 memcmp( ssl->session->peer_cert->raw.p,
3731 ssl->session_negotiate->peer_cert->raw.p,
3732 ssl->session->peer_cert->raw.len ) != 0 )
3733 {
3734 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3735 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3736 }
3737 }
3738
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 if( ssl->authmode != SSL_VERIFY_NONE )
3740 {
3741 if( ssl->ca_chain == NULL )
3742 {
3743 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003744 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003745 }
3746
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003747 /*
3748 * Main check: verify certificate
3749 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003750 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3751 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3752 &ssl->session_negotiate->verify_result,
3753 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003754
3755 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003756 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003757 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003758 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003759
3760 /*
3761 * Secondary checks: always done, but change 'ret' only if it was 0
3762 */
3763
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003764#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003765 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003766 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003767
3768 /* If certificate uses an EC key, make sure the curve is OK */
3769 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3770 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3771 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003772 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003773 if( ret == 0 )
3774 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003775 }
3776 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003777#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003778
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003779 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3780 ciphersuite_info,
3781 ! ssl->endpoint ) != 0 )
3782 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003783 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003784 if( ret == 0 )
3785 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3786 }
3787
Paul Bakker5121ce52009-01-03 21:22:43 +00003788 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3789 ret = 0;
3790 }
3791
3792 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3793
3794 return( ret );
3795}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003796#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3797 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3798 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3799 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3800 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3801 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3802 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003803
3804int ssl_write_change_cipher_spec( ssl_context *ssl )
3805{
3806 int ret;
3807
3808 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3809
3810 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3811 ssl->out_msglen = 1;
3812 ssl->out_msg[0] = 1;
3813
Paul Bakker5121ce52009-01-03 21:22:43 +00003814 ssl->state++;
3815
3816 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3817 {
3818 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3819 return( ret );
3820 }
3821
3822 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3823
3824 return( 0 );
3825}
3826
3827int ssl_parse_change_cipher_spec( ssl_context *ssl )
3828{
3829 int ret;
3830
3831 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3832
Paul Bakker5121ce52009-01-03 21:22:43 +00003833 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3834 {
3835 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3836 return( ret );
3837 }
3838
3839 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3840 {
3841 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003842 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003843 }
3844
3845 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3846 {
3847 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003848 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003849 }
3850
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003851 /*
3852 * Switch to our negotiated transform and session parameters for inbound
3853 * data.
3854 */
3855 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3856 ssl->transform_in = ssl->transform_negotiate;
3857 ssl->session_in = ssl->session_negotiate;
3858
3859#if defined(POLARSSL_SSL_PROTO_DTLS)
3860 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3861 {
3862#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3863 ssl_dtls_replay_reset( ssl );
3864#endif
3865
3866 /* Increment epoch */
3867 if( ++ssl->in_epoch == 0 )
3868 {
3869 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3870 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3871 }
3872 }
3873 else
3874#endif /* POLARSSL_SSL_PROTO_DTLS */
3875 memset( ssl->in_ctr, 0, 8 );
3876
3877 /*
3878 * Set the in_msg pointer to the correct location based on IV length
3879 */
3880 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3881 {
3882 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3883 ssl->transform_negotiate->fixed_ivlen;
3884 }
3885 else
3886 ssl->in_msg = ssl->in_iv;
3887
3888#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3889 if( ssl_hw_record_activate != NULL )
3890 {
3891 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3892 {
3893 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3894 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3895 }
3896 }
3897#endif
3898
Paul Bakker5121ce52009-01-03 21:22:43 +00003899 ssl->state++;
3900
3901 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3902
3903 return( 0 );
3904}
3905
Paul Bakker41c83d32013-03-20 14:39:14 +01003906void ssl_optimize_checksum( ssl_context *ssl,
3907 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003908{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003909 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003910
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003911#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3912 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003913 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003914 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003915 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003916#endif
3917#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3918#if defined(POLARSSL_SHA512_C)
3919 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3920 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3921 else
3922#endif
3923#if defined(POLARSSL_SHA256_C)
3924 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003925 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003926 else
3927#endif
3928#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003929 {
3930 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003931 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003932 }
Paul Bakker380da532012-04-18 16:10:25 +00003933}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003934
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003935void ssl_reset_checksum( ssl_context *ssl )
3936{
3937#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3938 defined(POLARSSL_SSL_PROTO_TLS1_1)
3939 md5_starts( &ssl->handshake->fin_md5 );
3940 sha1_starts( &ssl->handshake->fin_sha1 );
3941#endif
3942#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3943#if defined(POLARSSL_SHA256_C)
3944 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3945#endif
3946#if defined(POLARSSL_SHA512_C)
3947 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3948#endif
3949#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3950}
3951
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003952static void ssl_update_checksum_start( ssl_context *ssl,
3953 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003954{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003955#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3956 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003957 md5_update( &ssl->handshake->fin_md5 , buf, len );
3958 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003959#endif
3960#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3961#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003962 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003963#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003964#if defined(POLARSSL_SHA512_C)
3965 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003966#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003967#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003968}
3969
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003970#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3971 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003972static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3973 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003974{
Paul Bakker48916f92012-09-16 19:57:18 +00003975 md5_update( &ssl->handshake->fin_md5 , buf, len );
3976 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003977}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003978#endif
Paul Bakker380da532012-04-18 16:10:25 +00003979
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003980#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3981#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003982static void ssl_update_checksum_sha256( ssl_context *ssl,
3983 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003984{
Paul Bakker9e36f042013-06-30 14:34:05 +02003985 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003986}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003987#endif
Paul Bakker380da532012-04-18 16:10:25 +00003988
Paul Bakker9e36f042013-06-30 14:34:05 +02003989#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003990static void ssl_update_checksum_sha384( ssl_context *ssl,
3991 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003992{
Paul Bakker9e36f042013-06-30 14:34:05 +02003993 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003994}
Paul Bakker769075d2012-11-24 11:26:46 +01003995#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003996#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003997
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003998#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003999static void ssl_calc_finished_ssl(
4000 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004001{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004002 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004003 md5_context md5;
4004 sha1_context sha1;
4005
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 unsigned char padbuf[48];
4007 unsigned char md5sum[16];
4008 unsigned char sha1sum[20];
4009
Paul Bakker48916f92012-09-16 19:57:18 +00004010 ssl_session *session = ssl->session_negotiate;
4011 if( !session )
4012 session = ssl->session;
4013
Paul Bakker1ef83d62012-04-11 12:09:53 +00004014 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4015
Paul Bakker48916f92012-09-16 19:57:18 +00004016 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4017 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004018
4019 /*
4020 * SSLv3:
4021 * hash =
4022 * MD5( master + pad2 +
4023 * MD5( handshake + sender + master + pad1 ) )
4024 * + SHA1( master + pad2 +
4025 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004026 */
4027
Paul Bakker90995b52013-06-24 19:20:35 +02004028#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004029 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004030 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
Paul Bakker90995b52013-06-24 19:20:35 +02004033#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004034 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004035 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004036#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004037
Paul Bakker3c2122f2013-06-24 19:03:14 +02004038 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4039 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004040
Paul Bakker1ef83d62012-04-11 12:09:53 +00004041 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004042
Paul Bakker3c2122f2013-06-24 19:03:14 +02004043 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004044 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004045 md5_update( &md5, padbuf, 48 );
4046 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004047
Paul Bakker3c2122f2013-06-24 19:03:14 +02004048 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004049 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004050 sha1_update( &sha1, padbuf, 40 );
4051 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004052
Paul Bakker1ef83d62012-04-11 12:09:53 +00004053 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004054
Paul Bakker1ef83d62012-04-11 12:09:53 +00004055 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004056 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004057 md5_update( &md5, padbuf, 48 );
4058 md5_update( &md5, md5sum, 16 );
4059 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004060
Paul Bakker1ef83d62012-04-11 12:09:53 +00004061 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004062 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004063 sha1_update( &sha1, padbuf , 40 );
4064 sha1_update( &sha1, sha1sum, 20 );
4065 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004066
Paul Bakker1ef83d62012-04-11 12:09:53 +00004067 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004068
Paul Bakker5b4af392014-06-26 12:09:34 +02004069 md5_free( &md5 );
4070 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004071
Paul Bakker34617722014-06-13 17:20:13 +02004072 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4073 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4074 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004075
4076 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4077}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004078#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004079
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004080#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004081static void ssl_calc_finished_tls(
4082 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004083{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004084 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004085 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004086 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004087 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004088 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004089
Paul Bakker48916f92012-09-16 19:57:18 +00004090 ssl_session *session = ssl->session_negotiate;
4091 if( !session )
4092 session = ssl->session;
4093
Paul Bakker1ef83d62012-04-11 12:09:53 +00004094 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004095
Paul Bakker48916f92012-09-16 19:57:18 +00004096 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4097 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004098
Paul Bakker1ef83d62012-04-11 12:09:53 +00004099 /*
4100 * TLSv1:
4101 * hash = PRF( master, finished_label,
4102 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4103 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004104
Paul Bakker90995b52013-06-24 19:20:35 +02004105#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004106 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4107 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004108#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004109
Paul Bakker90995b52013-06-24 19:20:35 +02004110#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004111 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4112 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004113#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004114
4115 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004116 ? "client finished"
4117 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004118
4119 md5_finish( &md5, padbuf );
4120 sha1_finish( &sha1, padbuf + 16 );
4121
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004122 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004123 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004124
4125 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4126
Paul Bakker5b4af392014-06-26 12:09:34 +02004127 md5_free( &md5 );
4128 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004129
Paul Bakker34617722014-06-13 17:20:13 +02004130 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004131
4132 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4133}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004134#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004135
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004136#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4137#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004138static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004139 ssl_context *ssl, unsigned char *buf, int from )
4140{
4141 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004142 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004143 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004144 unsigned char padbuf[32];
4145
Paul Bakker48916f92012-09-16 19:57:18 +00004146 ssl_session *session = ssl->session_negotiate;
4147 if( !session )
4148 session = ssl->session;
4149
Paul Bakker380da532012-04-18 16:10:25 +00004150 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004151
Paul Bakker9e36f042013-06-30 14:34:05 +02004152 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004153
4154 /*
4155 * TLSv1.2:
4156 * hash = PRF( master, finished_label,
4157 * Hash( handshake ) )[0.11]
4158 */
4159
Paul Bakker9e36f042013-06-30 14:34:05 +02004160#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004161 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004162 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004163#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004164
4165 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004166 ? "client finished"
4167 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004168
Paul Bakker9e36f042013-06-30 14:34:05 +02004169 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004170
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004171 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004172 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004173
4174 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4175
Paul Bakker5b4af392014-06-26 12:09:34 +02004176 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004177
Paul Bakker34617722014-06-13 17:20:13 +02004178 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004179
4180 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4181}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004182#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004183
Paul Bakker9e36f042013-06-30 14:34:05 +02004184#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004185static void ssl_calc_finished_tls_sha384(
4186 ssl_context *ssl, unsigned char *buf, int from )
4187{
4188 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004189 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004190 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004191 unsigned char padbuf[48];
4192
Paul Bakker48916f92012-09-16 19:57:18 +00004193 ssl_session *session = ssl->session_negotiate;
4194 if( !session )
4195 session = ssl->session;
4196
Paul Bakker380da532012-04-18 16:10:25 +00004197 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004198
Paul Bakker9e36f042013-06-30 14:34:05 +02004199 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004200
4201 /*
4202 * TLSv1.2:
4203 * hash = PRF( master, finished_label,
4204 * Hash( handshake ) )[0.11]
4205 */
4206
Paul Bakker9e36f042013-06-30 14:34:05 +02004207#if !defined(POLARSSL_SHA512_ALT)
4208 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4209 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004210#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004211
4212 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004213 ? "client finished"
4214 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004215
Paul Bakker9e36f042013-06-30 14:34:05 +02004216 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004217
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004218 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004219 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004220
4221 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4222
Paul Bakker5b4af392014-06-26 12:09:34 +02004223 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004224
Paul Bakker34617722014-06-13 17:20:13 +02004225 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004226
4227 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4228}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004229#endif /* POLARSSL_SHA512_C */
4230#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004231
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004232static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004233{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004234 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004235
4236 /*
4237 * Free our handshake params
4238 */
4239 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004240 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004241 ssl->handshake = NULL;
4242
4243 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004244 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004245 */
4246 if( ssl->transform )
4247 {
4248 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004249 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004250 }
4251 ssl->transform = ssl->transform_negotiate;
4252 ssl->transform_negotiate = NULL;
4253
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004254 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4255}
4256
4257void ssl_handshake_wrapup( ssl_context *ssl )
4258{
4259 int resume = ssl->handshake->resume;
4260
4261 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4262
4263 if( ssl->renegotiation == SSL_RENEGOTIATION )
4264 {
4265 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4266 ssl->renego_records_seen = 0;
4267 }
4268
4269 /*
4270 * Free the previous session and switch in the current one
4271 */
Paul Bakker0a597072012-09-25 21:55:46 +00004272 if( ssl->session )
4273 {
4274 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004275 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004276 }
4277 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004278 ssl->session_negotiate = NULL;
4279
Paul Bakker0a597072012-09-25 21:55:46 +00004280 /*
4281 * Add cache entry
4282 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004283 if( ssl->f_set_cache != NULL &&
4284 ssl->session->length != 0 &&
4285 resume == 0 )
4286 {
Paul Bakker0a597072012-09-25 21:55:46 +00004287 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4288 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004289 }
Paul Bakker0a597072012-09-25 21:55:46 +00004290
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004291#if defined(POLARSSL_SSL_PROTO_DTLS)
4292 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4293 ssl->handshake->flight != NULL )
4294 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004295 /* Cancel handshake timer */
4296 ssl_set_timer( ssl, 0 );
4297
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004298 /* Keep last flight around in case we need to resend it:
4299 * we need the handshake and transform structures for that */
4300 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4301 }
4302 else
4303#endif
4304 ssl_handshake_wrapup_free_hs_transform( ssl );
4305
Paul Bakker48916f92012-09-16 19:57:18 +00004306 ssl->state++;
4307
4308 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4309}
4310
Paul Bakker1ef83d62012-04-11 12:09:53 +00004311int ssl_write_finished( ssl_context *ssl )
4312{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004313 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004314
4315 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4316
Paul Bakker92be97b2013-01-02 17:30:03 +01004317 /*
4318 * Set the out_msg pointer to the correct location based on IV length
4319 */
4320 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4321 {
4322 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4323 ssl->transform_negotiate->fixed_ivlen;
4324 }
4325 else
4326 ssl->out_msg = ssl->out_iv;
4327
Paul Bakker48916f92012-09-16 19:57:18 +00004328 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004329
4330 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4332
Paul Bakker48916f92012-09-16 19:57:18 +00004333 ssl->verify_data_len = hash_len;
4334 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4335
Paul Bakker5121ce52009-01-03 21:22:43 +00004336 ssl->out_msglen = 4 + hash_len;
4337 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4338 ssl->out_msg[0] = SSL_HS_FINISHED;
4339
4340 /*
4341 * In case of session resuming, invert the client and server
4342 * ChangeCipherSpec messages order.
4343 */
Paul Bakker0a597072012-09-25 21:55:46 +00004344 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004345 {
4346 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004347 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 else
4349 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4350 }
4351 else
4352 ssl->state++;
4353
Paul Bakker48916f92012-09-16 19:57:18 +00004354 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004355 * Switch to our negotiated transform and session parameters for outbound
4356 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004357 */
4358 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004359
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004360#if defined(POLARSSL_SSL_PROTO_DTLS)
4361 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4362 {
4363 unsigned char i;
4364
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004365 /* Remember current epoch settings for resending */
4366 ssl->handshake->alt_transform_out = ssl->transform_out;
4367 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4368
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004369 /* Set sequence_number to zero */
4370 memset( ssl->out_ctr + 2, 0, 6 );
4371
4372 /* Increment epoch */
4373 for( i = 2; i > 0; i-- )
4374 if( ++ssl->out_ctr[i - 1] != 0 )
4375 break;
4376
4377 /* The loop goes to its end iff the counter is wrapping */
4378 if( i == 0 )
4379 {
4380 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4381 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4382 }
4383 }
4384 else
4385#endif /* POLARSSL_SSL_PROTO_DTLS */
4386 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004387
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004388 ssl->transform_out = ssl->transform_negotiate;
4389 ssl->session_out = ssl->session_negotiate;
4390
Paul Bakker07eb38b2012-12-19 14:42:06 +01004391#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004392 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004393 {
4394 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4395 {
4396 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4397 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4398 }
4399 }
4400#endif
4401
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004402#if defined(POLARSSL_SSL_PROTO_DTLS)
4403 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4404 ssl_send_flight_completed( ssl );
4405#endif
4406
Paul Bakker5121ce52009-01-03 21:22:43 +00004407 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4408 {
4409 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4410 return( ret );
4411 }
4412
4413 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4414
4415 return( 0 );
4416}
4417
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004418#if defined(POLARSSL_SSL_PROTO_SSL3)
4419#define SSL_MAX_HASH_LEN 36
4420#else
4421#define SSL_MAX_HASH_LEN 12
4422#endif
4423
Paul Bakker5121ce52009-01-03 21:22:43 +00004424int ssl_parse_finished( ssl_context *ssl )
4425{
Paul Bakker23986e52011-04-24 08:57:21 +00004426 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004427 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004428 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004429
4430 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4431
Paul Bakker48916f92012-09-16 19:57:18 +00004432 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004433
Paul Bakker5121ce52009-01-03 21:22:43 +00004434 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4435 {
4436 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4437 return( ret );
4438 }
4439
4440 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4441 {
4442 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004443 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004444 }
4445
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004446 /* There is currently no ciphersuite using another length with TLS 1.2 */
4447#if defined(POLARSSL_SSL_PROTO_SSL3)
4448 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4449 hash_len = 36;
4450 else
4451#endif
4452 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004453
4454 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004455 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004456 {
4457 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004458 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004459 }
4460
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004461 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4462 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004463 {
4464 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004465 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004466 }
4467
Paul Bakker48916f92012-09-16 19:57:18 +00004468 ssl->verify_data_len = hash_len;
4469 memcpy( ssl->peer_verify_data, buf, hash_len );
4470
Paul Bakker0a597072012-09-25 21:55:46 +00004471 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004472 {
4473 if( ssl->endpoint == SSL_IS_CLIENT )
4474 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4475
4476 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004477 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004478 }
4479 else
4480 ssl->state++;
4481
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004482#if defined(POLARSSL_SSL_PROTO_DTLS)
4483 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4484 ssl_recv_flight_completed( ssl );
4485#endif
4486
Paul Bakker5121ce52009-01-03 21:22:43 +00004487 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4488
4489 return( 0 );
4490}
4491
Paul Bakker968afaa2014-07-09 11:09:24 +02004492static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004493{
4494 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4495
4496#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4497 defined(POLARSSL_SSL_PROTO_TLS1_1)
4498 md5_init( &handshake->fin_md5 );
4499 sha1_init( &handshake->fin_sha1 );
4500 md5_starts( &handshake->fin_md5 );
4501 sha1_starts( &handshake->fin_sha1 );
4502#endif
4503#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4504#if defined(POLARSSL_SHA256_C)
4505 sha256_init( &handshake->fin_sha256 );
4506 sha256_starts( &handshake->fin_sha256, 0 );
4507#endif
4508#if defined(POLARSSL_SHA512_C)
4509 sha512_init( &handshake->fin_sha512 );
4510 sha512_starts( &handshake->fin_sha512, 1 );
4511#endif
4512#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4513
4514 handshake->update_checksum = ssl_update_checksum_start;
4515 handshake->sig_alg = SSL_HASH_SHA1;
4516
4517#if defined(POLARSSL_DHM_C)
4518 dhm_init( &handshake->dhm_ctx );
4519#endif
4520#if defined(POLARSSL_ECDH_C)
4521 ecdh_init( &handshake->ecdh_ctx );
4522#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004523}
4524
4525static void ssl_transform_init( ssl_transform *transform )
4526{
4527 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004528
4529 cipher_init( &transform->cipher_ctx_enc );
4530 cipher_init( &transform->cipher_ctx_dec );
4531
4532 md_init( &transform->md_ctx_enc );
4533 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004534}
4535
4536void ssl_session_init( ssl_session *session )
4537{
4538 memset( session, 0, sizeof(ssl_session) );
4539}
4540
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004541static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004542{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004543 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004544 if( ssl->transform_negotiate )
4545 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004546 if( ssl->session_negotiate )
4547 ssl_session_free( ssl->session_negotiate );
4548 if( ssl->handshake )
4549 ssl_handshake_free( ssl->handshake );
4550
4551 /*
4552 * Either the pointers are now NULL or cleared properly and can be freed.
4553 * Now allocate missing structures.
4554 */
4555 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004556 {
4557 ssl->transform_negotiate =
4558 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4559 }
Paul Bakker48916f92012-09-16 19:57:18 +00004560
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004561 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004562 {
4563 ssl->session_negotiate =
4564 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4565 }
Paul Bakker48916f92012-09-16 19:57:18 +00004566
Paul Bakker82788fb2014-10-20 13:59:19 +02004567 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004568 {
4569 ssl->handshake = (ssl_handshake_params *)
4570 polarssl_malloc( sizeof(ssl_handshake_params) );
4571 }
Paul Bakker48916f92012-09-16 19:57:18 +00004572
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004573 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004574 if( ssl->handshake == NULL ||
4575 ssl->transform_negotiate == NULL ||
4576 ssl->session_negotiate == NULL )
4577 {
4578 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004579
4580 polarssl_free( ssl->handshake );
4581 polarssl_free( ssl->transform_negotiate );
4582 polarssl_free( ssl->session_negotiate );
4583
4584 ssl->handshake = NULL;
4585 ssl->transform_negotiate = NULL;
4586 ssl->session_negotiate = NULL;
4587
Paul Bakker48916f92012-09-16 19:57:18 +00004588 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4589 }
4590
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004591 /* Initialize structures */
4592 ssl_session_init( ssl->session_negotiate );
4593 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004594 ssl_handshake_params_init( ssl->handshake );
4595
4596#if defined(POLARSSL_X509_CRT_PARSE_C)
4597 ssl->handshake->key_cert = ssl->key_cert;
4598#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004599
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004600 /*
4601 * We may not know yet if we're using DTLS,
4602 * so always initiliase DTLS-specific fields.
4603 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004604#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004605 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004606
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004607 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004608 if( ssl->endpoint == SSL_IS_CLIENT )
4609 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4610 else
4611 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004612#endif
4613
Paul Bakker48916f92012-09-16 19:57:18 +00004614 return( 0 );
4615}
4616
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004617#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4618/* Dummy cookie callbacks for defaults */
4619static int ssl_cookie_write_dummy( void *ctx,
4620 unsigned char **p, unsigned char *end,
4621 const unsigned char *cli_id, size_t cli_id_len )
4622{
4623 ((void) ctx);
4624 ((void) p);
4625 ((void) end);
4626 ((void) cli_id);
4627 ((void) cli_id_len);
4628
4629 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4630}
4631
4632static int ssl_cookie_check_dummy( void *ctx,
4633 const unsigned char *cookie, size_t cookie_len,
4634 const unsigned char *cli_id, size_t cli_id_len )
4635{
4636 ((void) ctx);
4637 ((void) cookie);
4638 ((void) cookie_len);
4639 ((void) cli_id);
4640 ((void) cli_id_len);
4641
4642 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4643}
4644#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4645
Paul Bakker5121ce52009-01-03 21:22:43 +00004646/*
4647 * Initialize an SSL context
4648 */
4649int ssl_init( ssl_context *ssl )
4650{
Paul Bakker48916f92012-09-16 19:57:18 +00004651 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 int len = SSL_BUFFER_LEN;
4653
4654 memset( ssl, 0, sizeof( ssl_context ) );
4655
Paul Bakker62f2dee2012-09-28 07:31:51 +00004656 /*
4657 * Sane defaults
4658 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004659 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4660 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4661 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4662 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004663
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004664 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004665
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004666 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4667
Paul Bakker62f2dee2012-09-28 07:31:51 +00004668#if defined(POLARSSL_DHM_C)
4669 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4670 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4671 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4672 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4673 {
4674 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4675 return( ret );
4676 }
4677#endif
4678
4679 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004680 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004681 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004682 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004683 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004684
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004685 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004686 {
4687 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004688 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004689 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004690 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004691 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004692 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 }
4694
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004695 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4696 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004697
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004698 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4699 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4700
Paul Bakker606b4ba2013-08-14 16:52:14 +02004701#if defined(POLARSSL_SSL_SESSION_TICKETS)
4702 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4703#endif
4704
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004705#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004706 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004707#endif
4708
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004709#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4710 ssl->f_cookie_write = ssl_cookie_write_dummy;
4711 ssl->f_cookie_check = ssl_cookie_check_dummy;
4712#endif
4713
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004714#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4715 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4716#endif
4717
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004718#if defined(POLARSSL_SSL_PROTO_DTLS)
4719 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4720 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4721#endif
4722
Paul Bakker48916f92012-09-16 19:57:18 +00004723 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4724 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004725
4726 return( 0 );
4727}
4728
4729/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004730 * Reset an initialized and used SSL context for re-use while retaining
4731 * all application-set variables, function pointers and data.
4732 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004733int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004734{
Paul Bakker48916f92012-09-16 19:57:18 +00004735 int ret;
4736
Paul Bakker7eb013f2011-10-06 12:37:39 +00004737 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004738 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4739 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4740
4741 ssl->verify_data_len = 0;
4742 memset( ssl->own_verify_data, 0, 36 );
4743 memset( ssl->peer_verify_data, 0, 36 );
4744
Paul Bakker7eb013f2011-10-06 12:37:39 +00004745 ssl->in_offt = NULL;
4746
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004747 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004748 ssl->in_msgtype = 0;
4749 ssl->in_msglen = 0;
4750 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004751#if defined(POLARSSL_SSL_PROTO_DTLS)
4752 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004753 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004754#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004755#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4756 ssl_dtls_replay_reset( ssl );
4757#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004758
4759 ssl->in_hslen = 0;
4760 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004761 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004762
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004763 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004764 ssl->out_msgtype = 0;
4765 ssl->out_msglen = 0;
4766 ssl->out_left = 0;
4767
Paul Bakker48916f92012-09-16 19:57:18 +00004768 ssl->transform_in = NULL;
4769 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004770
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004771 ssl->renego_records_seen = 0;
4772
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004773 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4774 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004775
4776#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004777 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004778 {
4779 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004780 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004781 {
4782 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4783 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4784 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004785 }
4786#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004787
Paul Bakker48916f92012-09-16 19:57:18 +00004788 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004789 {
Paul Bakker48916f92012-09-16 19:57:18 +00004790 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004791 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004792 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004793 }
Paul Bakker48916f92012-09-16 19:57:18 +00004794
Paul Bakkerc0463502013-02-14 11:19:38 +01004795 if( ssl->session )
4796 {
4797 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004798 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004799 ssl->session = NULL;
4800 }
4801
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004802#if defined(POLARSSL_SSL_ALPN)
4803 ssl->alpn_chosen = NULL;
4804#endif
4805
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004806#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004807 polarssl_free( ssl->cli_id );
4808 ssl->cli_id = NULL;
4809 ssl->cli_id_len = 0;
4810#endif
4811
Paul Bakker48916f92012-09-16 19:57:18 +00004812 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4813 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004814
4815 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004816}
4817
Paul Bakkera503a632013-08-14 13:48:06 +02004818#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004819static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4820{
4821 aes_free( &tkeys->enc );
4822 aes_free( &tkeys->dec );
4823
4824 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4825}
4826
Paul Bakker7eb013f2011-10-06 12:37:39 +00004827/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004828 * Allocate and initialize ticket keys
4829 */
4830static int ssl_ticket_keys_init( ssl_context *ssl )
4831{
4832 int ret;
4833 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004834 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004835
4836 if( ssl->ticket_keys != NULL )
4837 return( 0 );
4838
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004839 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4840 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004841 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4842
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004843 aes_init( &tkeys->enc );
4844 aes_init( &tkeys->dec );
4845
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004846 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004847 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004848 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004849 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004850 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004851 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004852
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004853 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4854 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4855 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4856 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004857 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004858 polarssl_free( tkeys );
4859 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004860 }
4861
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004862 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004863 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004864 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004865 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004866 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004867 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004868
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004869 ssl->ticket_keys = tkeys;
4870
4871 return( 0 );
4872}
Paul Bakkera503a632013-08-14 13:48:06 +02004873#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004874
4875/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004876 * SSL set accessors
4877 */
4878void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4879{
4880 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004881
Paul Bakker606b4ba2013-08-14 16:52:14 +02004882#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004883 if( endpoint == SSL_IS_CLIENT )
4884 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004885#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004886}
4887
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004888int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004889{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004890#if defined(POLARSSL_SSL_PROTO_DTLS)
4891 if( transport == SSL_TRANSPORT_DATAGRAM )
4892 {
4893 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004894
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004895 ssl->out_hdr = ssl->out_buf;
4896 ssl->out_ctr = ssl->out_buf + 3;
4897 ssl->out_len = ssl->out_buf + 11;
4898 ssl->out_iv = ssl->out_buf + 13;
4899 ssl->out_msg = ssl->out_buf + 13;
4900
4901 ssl->in_hdr = ssl->in_buf;
4902 ssl->in_ctr = ssl->in_buf + 3;
4903 ssl->in_len = ssl->in_buf + 11;
4904 ssl->in_iv = ssl->in_buf + 13;
4905 ssl->in_msg = ssl->in_buf + 13;
4906
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004907 /* DTLS starts with TLS1.1 */
4908 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4909 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004910
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004911 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4912 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4913
4914 return( 0 );
4915 }
4916#endif
4917
4918 if( transport == SSL_TRANSPORT_STREAM )
4919 {
4920 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004921
4922 ssl->out_ctr = ssl->out_buf;
4923 ssl->out_hdr = ssl->out_buf + 8;
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_ctr = ssl->in_buf;
4929 ssl->in_hdr = ssl->in_buf + 8;
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 return( 0 );
4935 }
4936
4937 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004938}
4939
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004940#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4941void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4942{
4943 ssl->anti_replay = mode;
4944}
4945#endif
4946
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004947#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
4948void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
4949{
4950 ssl->badmac_limit = limit;
4951}
4952#endif
4953
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004954#if defined(POLARSSL_SSL_PROTO_DTLS)
4955void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4956{
4957 ssl->hs_timeout_min = min;
4958 ssl->hs_timeout_max = max;
4959}
4960#endif
4961
Paul Bakker5121ce52009-01-03 21:22:43 +00004962void ssl_set_authmode( ssl_context *ssl, int authmode )
4963{
4964 ssl->authmode = authmode;
4965}
4966
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004967#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004968void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004969 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004970 void *p_vrfy )
4971{
4972 ssl->f_vrfy = f_vrfy;
4973 ssl->p_vrfy = p_vrfy;
4974}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004975#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004976
Paul Bakker5121ce52009-01-03 21:22:43 +00004977void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004978 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004979 void *p_rng )
4980{
4981 ssl->f_rng = f_rng;
4982 ssl->p_rng = p_rng;
4983}
4984
4985void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004986 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004987 void *p_dbg )
4988{
4989 ssl->f_dbg = f_dbg;
4990 ssl->p_dbg = p_dbg;
4991}
4992
4993void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004994 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004995 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004996{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004997 if( p_recv != p_send )
4998 {
4999 ssl->f_recv = NULL;
5000 ssl->f_send = NULL;
5001 ssl->p_bio = NULL;
5002 return;
5003 }
5004
Paul Bakker5121ce52009-01-03 21:22:43 +00005005 ssl->f_recv = f_recv;
5006 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005007 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005008}
5009
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005010void ssl_set_bio_timeout( ssl_context *ssl,
5011 void *p_bio,
5012 int (*f_send)(void *, const unsigned char *, size_t),
5013 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005014 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5015 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005016{
5017 ssl->p_bio = p_bio;
5018 ssl->f_send = f_send;
5019 ssl->f_recv = f_recv;
5020 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005021 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005022}
5023
Paul Bakker0a597072012-09-25 21:55:46 +00005024void ssl_set_session_cache( ssl_context *ssl,
5025 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5026 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005027{
Paul Bakker0a597072012-09-25 21:55:46 +00005028 ssl->f_get_cache = f_get_cache;
5029 ssl->p_get_cache = p_get_cache;
5030 ssl->f_set_cache = f_set_cache;
5031 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005032}
5033
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005034int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005035{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005036 int ret;
5037
5038 if( ssl == NULL ||
5039 session == NULL ||
5040 ssl->session_negotiate == NULL ||
5041 ssl->endpoint != SSL_IS_CLIENT )
5042 {
5043 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5044 }
5045
5046 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5047 return( ret );
5048
Paul Bakker0a597072012-09-25 21:55:46 +00005049 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005050
5051 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005052}
5053
Paul Bakkerb68cad62012-08-23 08:34:18 +00005054void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005055{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005056 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5057 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5058 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5059 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5060}
5061
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005062void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5063 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005064 int major, int minor )
5065{
5066 if( major != SSL_MAJOR_VERSION_3 )
5067 return;
5068
5069 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5070 return;
5071
5072 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005073}
5074
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005075#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005076/* Add a new (empty) key_cert entry an return a pointer to it */
5077static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5078{
5079 ssl_key_cert *key_cert, *last;
5080
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005081 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5082 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005083 return( NULL );
5084
5085 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5086
5087 /* Append the new key_cert to the (possibly empty) current list */
5088 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005089 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005090 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005091 if( ssl->handshake != NULL )
5092 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005093 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005094 else
5095 {
5096 last = ssl->key_cert;
5097 while( last->next != NULL )
5098 last = last->next;
5099 last->next = key_cert;
5100 }
5101
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005102 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005103}
5104
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005105void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005106 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005107{
5108 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005109 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005110 ssl->peer_cn = peer_cn;
5111}
5112
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005113int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005114 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005115{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005116 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5117
5118 if( key_cert == NULL )
5119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5120
5121 key_cert->cert = own_cert;
5122 key_cert->key = pk_key;
5123
5124 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005125}
5126
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005127#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005128int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005129 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005130{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005131 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005132 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005133
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005134 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005135 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5136
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005137 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5138 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005139 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005140
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005141 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005142
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005143 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005144 if( ret != 0 )
5145 return( ret );
5146
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005147 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005148 return( ret );
5149
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005150 key_cert->cert = own_cert;
5151 key_cert->key_own_alloc = 1;
5152
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005153 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005154}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005155#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005156
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005157int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005158 void *rsa_key,
5159 rsa_decrypt_func rsa_decrypt,
5160 rsa_sign_func rsa_sign,
5161 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005162{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005163 int ret;
5164 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005165
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005166 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005167 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5168
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005169 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5170 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005171 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005172
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005173 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005174
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005175 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5176 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5177 return( ret );
5178
5179 key_cert->cert = own_cert;
5180 key_cert->key_own_alloc = 1;
5181
5182 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005183}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005184#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005185
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005186#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005187int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5188 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005189{
Paul Bakker6db455e2013-09-18 17:29:31 +02005190 if( psk == NULL || psk_identity == NULL )
5191 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5192
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005193 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005194 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5195
Paul Bakker6db455e2013-09-18 17:29:31 +02005196 if( ssl->psk != NULL )
5197 {
5198 polarssl_free( ssl->psk );
5199 polarssl_free( ssl->psk_identity );
5200 }
5201
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005202 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005203 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005204
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005205 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005206 ssl->psk_identity = (unsigned char *)
5207 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005208
5209 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5210 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5211
5212 memcpy( ssl->psk, psk, ssl->psk_len );
5213 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005214
5215 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005216}
5217
5218void ssl_set_psk_cb( ssl_context *ssl,
5219 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5220 size_t),
5221 void *p_psk )
5222{
5223 ssl->f_psk = f_psk;
5224 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005225}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005226#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005227
Paul Bakker48916f92012-09-16 19:57:18 +00005228#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005229int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005230{
5231 int ret;
5232
Paul Bakker48916f92012-09-16 19:57:18 +00005233 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005234 {
5235 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5236 return( ret );
5237 }
5238
Paul Bakker48916f92012-09-16 19:57:18 +00005239 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005240 {
5241 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5242 return( ret );
5243 }
5244
5245 return( 0 );
5246}
5247
Paul Bakker1b57b062011-01-06 15:48:19 +00005248int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5249{
5250 int ret;
5251
Paul Bakker66d5d072014-06-17 16:39:18 +02005252 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005253 {
5254 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5255 return( ret );
5256 }
5257
Paul Bakker66d5d072014-06-17 16:39:18 +02005258 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005259 {
5260 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5261 return( ret );
5262 }
5263
5264 return( 0 );
5265}
Paul Bakker48916f92012-09-16 19:57:18 +00005266#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005267
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005268#if defined(POLARSSL_SSL_SET_CURVES)
5269/*
5270 * Set the allowed elliptic curves
5271 */
5272void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5273{
5274 ssl->curve_list = curve_list;
5275}
5276#endif
5277
Paul Bakker0be444a2013-08-27 21:55:01 +02005278#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005279int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005280{
5281 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005282 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005283
5284 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005285
5286 if( ssl->hostname_len + 1 == 0 )
5287 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5288
Paul Bakker6e339b52013-07-03 13:37:05 +02005289 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005290
Paul Bakkerb15b8512012-01-13 13:44:06 +00005291 if( ssl->hostname == NULL )
5292 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5293
Paul Bakker3c2122f2013-06-24 19:03:14 +02005294 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005295 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005296
Paul Bakker40ea7de2009-05-03 10:18:48 +00005297 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005298
5299 return( 0 );
5300}
5301
Paul Bakker5701cdc2012-09-27 21:49:42 +00005302void ssl_set_sni( ssl_context *ssl,
5303 int (*f_sni)(void *, ssl_context *,
5304 const unsigned char *, size_t),
5305 void *p_sni )
5306{
5307 ssl->f_sni = f_sni;
5308 ssl->p_sni = p_sni;
5309}
Paul Bakker0be444a2013-08-27 21:55:01 +02005310#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005311
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005312#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005313int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005314{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005315 size_t cur_len, tot_len;
5316 const char **p;
5317
5318 /*
5319 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5320 * truncated". Check lengths now rather than later.
5321 */
5322 tot_len = 0;
5323 for( p = protos; *p != NULL; p++ )
5324 {
5325 cur_len = strlen( *p );
5326 tot_len += cur_len;
5327
5328 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5329 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5330 }
5331
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005332 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005333
5334 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005335}
5336
5337const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5338{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005339 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005340}
5341#endif /* POLARSSL_SSL_ALPN */
5342
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005343static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005344{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005345 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005346 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005347 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005348 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005349 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005350
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005351#if defined(POLARSSL_SSL_PROTO_DTLS)
5352 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5353 minor < SSL_MINOR_VERSION_2 )
5354 {
5355 return( -1 );
5356 }
5357#else
5358 ((void) ssl);
5359#endif
5360
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005361 return( 0 );
5362}
5363
5364int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5365{
5366 if( ssl_check_version( ssl, major, minor ) != 0 )
5367 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5368
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005369 ssl->max_major_ver = major;
5370 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005371
5372 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005373}
5374
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005375int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005376{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005377 if( ssl_check_version( ssl, major, minor ) != 0 )
5378 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005379
5380 ssl->min_major_ver = major;
5381 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005382
5383 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005384}
5385
Paul Bakker05decb22013-08-15 13:33:48 +02005386#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005387int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5388{
Paul Bakker77e257e2013-12-16 15:29:52 +01005389 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005390 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005391 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005392 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005393 }
5394
5395 ssl->mfl_code = mfl_code;
5396
5397 return( 0 );
5398}
Paul Bakker05decb22013-08-15 13:33:48 +02005399#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005400
Paul Bakker1f2bc622013-08-15 13:45:55 +02005401#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005402int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005403{
5404 if( ssl->endpoint != SSL_IS_CLIENT )
5405 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5406
Paul Bakker8c1ede62013-07-19 14:14:37 +02005407 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005408
5409 return( 0 );
5410}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005411#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005412
Paul Bakker48916f92012-09-16 19:57:18 +00005413void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5414{
5415 ssl->disable_renegotiation = renegotiation;
5416}
5417
5418void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5419{
5420 ssl->allow_legacy_renegotiation = allow_legacy;
5421}
5422
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005423void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5424{
5425 ssl->renego_max_records = max_records;
5426}
5427
Paul Bakkera503a632013-08-14 13:48:06 +02005428#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005429int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5430{
5431 ssl->session_tickets = use_tickets;
5432
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005433 if( ssl->endpoint == SSL_IS_CLIENT )
5434 return( 0 );
5435
5436 if( ssl->f_rng == NULL )
5437 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5438
5439 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005440}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005441
5442void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5443{
5444 ssl->ticket_lifetime = lifetime;
5445}
Paul Bakkera503a632013-08-14 13:48:06 +02005446#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005447
Paul Bakker5121ce52009-01-03 21:22:43 +00005448/*
5449 * SSL get accessors
5450 */
Paul Bakker23986e52011-04-24 08:57:21 +00005451size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005452{
5453 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5454}
5455
Paul Bakkerff60ee62010-03-16 21:09:09 +00005456int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005457{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005458 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005459}
5460
Paul Bakkere3166ce2011-01-27 17:40:50 +00005461const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005462{
Paul Bakker926c8e42013-03-06 10:23:34 +01005463 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005464 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005465
Paul Bakkere3166ce2011-01-27 17:40:50 +00005466 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005467}
5468
Paul Bakker43ca69c2011-01-15 17:35:19 +00005469const char *ssl_get_version( const ssl_context *ssl )
5470{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005471#if defined(POLARSSL_SSL_PROTO_DTLS)
5472 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5473 {
5474 switch( ssl->minor_ver )
5475 {
5476 case SSL_MINOR_VERSION_2:
5477 return( "DTLSv1.0" );
5478
5479 case SSL_MINOR_VERSION_3:
5480 return( "DTLSv1.2" );
5481
5482 default:
5483 return( "unknown (DTLS)" );
5484 }
5485 }
5486#endif
5487
Paul Bakker43ca69c2011-01-15 17:35:19 +00005488 switch( ssl->minor_ver )
5489 {
5490 case SSL_MINOR_VERSION_0:
5491 return( "SSLv3.0" );
5492
5493 case SSL_MINOR_VERSION_1:
5494 return( "TLSv1.0" );
5495
5496 case SSL_MINOR_VERSION_2:
5497 return( "TLSv1.1" );
5498
Paul Bakker1ef83d62012-04-11 12:09:53 +00005499 case SSL_MINOR_VERSION_3:
5500 return( "TLSv1.2" );
5501
Paul Bakker43ca69c2011-01-15 17:35:19 +00005502 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005503 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005504 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005505}
5506
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005507int ssl_get_record_expansion( const ssl_context *ssl )
5508{
5509 int transform_expansion;
5510 const ssl_transform *transform = ssl->transform_out;
5511
5512#if defined(POLARSSL_ZLIB_SUPPORT)
5513 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5514 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5515#endif
5516
5517 if( transform == NULL )
5518 return( ssl_hdr_len( ssl ) );
5519
5520 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5521 {
5522 case POLARSSL_MODE_GCM:
5523 case POLARSSL_MODE_CCM:
5524 case POLARSSL_MODE_STREAM:
5525 transform_expansion = transform->minlen;
5526 break;
5527
5528 case POLARSSL_MODE_CBC:
5529 transform_expansion = transform->maclen
5530 + cipher_get_block_size( &transform->cipher_ctx_enc );
5531 break;
5532
5533 default:
5534 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5535 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5536 }
5537
5538 return( ssl_hdr_len( ssl ) + transform_expansion );
5539}
5540
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005541#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005542const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005543{
5544 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005545 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005546
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005547 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005548}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005549#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005550
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005551int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5552{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005553 if( ssl == NULL ||
5554 dst == NULL ||
5555 ssl->session == NULL ||
5556 ssl->endpoint != SSL_IS_CLIENT )
5557 {
5558 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5559 }
5560
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005561 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005562}
5563
Paul Bakker5121ce52009-01-03 21:22:43 +00005564/*
Paul Bakker1961b702013-01-25 14:49:24 +01005565 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005566 */
Paul Bakker1961b702013-01-25 14:49:24 +01005567int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005568{
Paul Bakker40e46942009-01-03 21:51:57 +00005569 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005570
Paul Bakker40e46942009-01-03 21:51:57 +00005571#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005572 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005573 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005574#endif
5575
Paul Bakker40e46942009-01-03 21:51:57 +00005576#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005577 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005578 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005579#endif
5580
Paul Bakker1961b702013-01-25 14:49:24 +01005581 return( ret );
5582}
5583
5584/*
5585 * Perform the SSL handshake
5586 */
5587int ssl_handshake( ssl_context *ssl )
5588{
5589 int ret = 0;
5590
5591 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5592
5593 while( ssl->state != SSL_HANDSHAKE_OVER )
5594 {
5595 ret = ssl_handshake_step( ssl );
5596
5597 if( ret != 0 )
5598 break;
5599 }
5600
Paul Bakker5121ce52009-01-03 21:22:43 +00005601 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5602
5603 return( ret );
5604}
5605
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005606#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005607/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005608 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005609 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005610static int ssl_write_hello_request( ssl_context *ssl )
5611{
5612 int ret;
5613
5614 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5615
5616 ssl->out_msglen = 4;
5617 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5618 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5619
5620 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5621 {
5622 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5623 return( ret );
5624 }
5625
5626 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5627
5628 return( 0 );
5629}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005630#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005631
5632/*
5633 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005634 * - any side: calling ssl_renegotiate(),
5635 * - client: receiving a HelloRequest during ssl_read(),
5636 * - server: receiving any handshake message on server during ssl_read() after
5637 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005638 * If the handshake doesn't complete due to waiting for I/O, it will continue
5639 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005640 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005641static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005642{
5643 int ret;
5644
5645 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5646
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005647 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5648 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005649
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005650 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5651 * the ServerHello will have message_seq = 1" */
5652#if defined(POLARSSL_SSL_PROTO_DTLS)
5653 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005654 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5655 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005656 if( ssl->endpoint == SSL_IS_SERVER )
5657 ssl->handshake->out_msg_seq = 1;
5658 else
5659 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005660 }
5661#endif
5662
Paul Bakker48916f92012-09-16 19:57:18 +00005663 ssl->state = SSL_HELLO_REQUEST;
5664 ssl->renegotiation = SSL_RENEGOTIATION;
5665
Paul Bakker48916f92012-09-16 19:57:18 +00005666 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5667 {
5668 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5669 return( ret );
5670 }
5671
5672 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5673
5674 return( 0 );
5675}
5676
5677/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005678 * Renegotiate current connection on client,
5679 * or request renegotiation on server
5680 */
5681int ssl_renegotiate( ssl_context *ssl )
5682{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005683 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005684
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005685#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005686 /* On server, just send the request */
5687 if( ssl->endpoint == SSL_IS_SERVER )
5688 {
5689 if( ssl->state != SSL_HANDSHAKE_OVER )
5690 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5691
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005692 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5693
5694 /* Did we already try/start sending HelloRequest? */
5695 if( ssl->out_left != 0 )
5696 return( ssl_flush_output( ssl ) );
5697
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005698 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005699 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005700#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005701
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005702#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005703 /*
5704 * On client, either start the renegotiation process or,
5705 * if already in progress, continue the handshake
5706 */
5707 if( ssl->renegotiation != SSL_RENEGOTIATION )
5708 {
5709 if( ssl->state != SSL_HANDSHAKE_OVER )
5710 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5711
5712 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5713 {
5714 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5715 return( ret );
5716 }
5717 }
5718 else
5719 {
5720 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5721 {
5722 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5723 return( ret );
5724 }
5725 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005726#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005727
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005728 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005729}
5730
5731/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005732 * Receive application data decrypted from the SSL layer
5733 */
Paul Bakker23986e52011-04-24 08:57:21 +00005734int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005735{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005736 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005737 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005738
5739 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5740
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005741#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005742 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005743 {
5744 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5745 return( ret );
5746
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005747 if( ssl->handshake != NULL &&
5748 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5749 {
5750 if( ( ret = ssl_resend( ssl ) ) != 0 )
5751 return( ret );
5752 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005753 }
5754#endif
5755
Paul Bakker5121ce52009-01-03 21:22:43 +00005756 if( ssl->state != SSL_HANDSHAKE_OVER )
5757 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005758 ret = ssl_handshake( ssl );
5759 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5760 {
5761 record_read = 1;
5762 }
5763 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005764 {
5765 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5766 return( ret );
5767 }
5768 }
5769
5770 if( ssl->in_offt == NULL )
5771 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02005772#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005773 /* Start timer if not already running */
5774 if( ssl->time_limit == 0 )
5775 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005776#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005777
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005778 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005779 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005780 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5781 {
5782 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5783 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005784
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005785 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5786 return( ret );
5787 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005788 }
5789
5790 if( ssl->in_msglen == 0 &&
5791 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5792 {
5793 /*
5794 * OpenSSL sends empty messages to randomize the IV
5795 */
5796 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5797 {
Paul Bakker831a7552011-05-18 13:32:51 +00005798 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5799 return( 0 );
5800
Paul Bakker5121ce52009-01-03 21:22:43 +00005801 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5802 return( ret );
5803 }
5804 }
5805
Paul Bakker48916f92012-09-16 19:57:18 +00005806 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5807 {
5808 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5809
5810 if( ssl->endpoint == SSL_IS_CLIENT &&
5811 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005812 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005813 {
5814 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005815
5816 /* With DTLS, drop the packet (probably from last handshake) */
5817#if defined(POLARSSL_SSL_PROTO_DTLS)
5818 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5819 return( POLARSSL_ERR_NET_WANT_READ );
5820#endif
5821 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5822 }
5823
5824 if( ssl->endpoint == SSL_IS_SERVER &&
5825 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5826 {
5827 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5828
5829 /* With DTLS, drop the packet (probably from last handshake) */
5830#if defined(POLARSSL_SSL_PROTO_DTLS)
5831 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5832 return( POLARSSL_ERR_NET_WANT_READ );
5833#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005834 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5835 }
5836
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005837 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5838 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005839 ssl->allow_legacy_renegotiation ==
5840 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005841 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005842 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005843
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005844#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005845 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005846 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005847 /*
5848 * SSLv3 does not have a "no_renegotiation" alert
5849 */
5850 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5851 return( ret );
5852 }
5853 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005854#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005855#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5856 defined(POLARSSL_SSL_PROTO_TLS1_2)
5857 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005858 {
5859 if( ( ret = ssl_send_alert_message( ssl,
5860 SSL_ALERT_LEVEL_WARNING,
5861 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5862 {
5863 return( ret );
5864 }
Paul Bakker48916f92012-09-16 19:57:18 +00005865 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005866 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005867#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5868 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005869 {
5870 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005871 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005872 }
Paul Bakker48916f92012-09-16 19:57:18 +00005873 }
5874 else
5875 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005876 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005877#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005878 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5879 ssl->endpoint == SSL_IS_CLIENT )
5880 {
5881 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5882 }
5883#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005884 ret = ssl_start_renegotiation( ssl );
5885 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5886 {
5887 record_read = 1;
5888 }
5889 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005890 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005891 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005892 return( ret );
5893 }
Paul Bakker48916f92012-09-16 19:57:18 +00005894 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005895
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005896 /* If a non-handshake record was read during renego, fallthrough,
5897 * else tell the user they should call ssl_read() again */
5898 if( ! record_read )
5899 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005900 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005901 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5902 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005903 ssl->renego_records_seen++;
5904
5905 if( ssl->renego_max_records >= 0 &&
5906 ssl->renego_records_seen > ssl->renego_max_records )
5907 {
5908 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5909 "but not honored by client" ) );
5910 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5911 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005912 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005913
5914 /* Fatal and closure alerts handled by ssl_read_record() */
5915 if( ssl->in_msgtype == SSL_MSG_ALERT )
5916 {
5917 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5918 return( POLARSSL_ERR_NET_WANT_READ );
5919 }
5920
5921 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005922 {
5923 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005924 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005925 }
5926
5927 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005928
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02005929#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005930 /* We're going to return something now, cancel timer,
5931 * except if handshake (renegotiation) is in progress */
5932 if( ssl->state == SSL_HANDSHAKE_OVER )
5933 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005934
5935 /* If we requested renego but received AppData, resend HelloRequest.
5936 * Do it now, after setting in_offt, to avoid taking this branch
5937 * again if ssl_write_hello_request() returns WANT_WRITE */
5938#if defined(POLARSSL_SSL_SRV_C)
5939 if( ssl->endpoint == SSL_IS_SERVER &&
5940 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5941 {
5942 if( ( ret = ssl_write_hello_request( ssl ) ) != 0 )
5943 {
5944 SSL_DEBUG_RET( 1, "ssl_write_hello_request", ret );
5945 return( ret );
5946 }
5947 }
5948#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005949#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005950 }
5951
5952 n = ( len < ssl->in_msglen )
5953 ? len : ssl->in_msglen;
5954
5955 memcpy( buf, ssl->in_offt, n );
5956 ssl->in_msglen -= n;
5957
5958 if( ssl->in_msglen == 0 )
5959 /* all bytes consumed */
5960 ssl->in_offt = NULL;
5961 else
5962 /* more data available */
5963 ssl->in_offt += n;
5964
5965 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5966
Paul Bakker23986e52011-04-24 08:57:21 +00005967 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005968}
5969
5970/*
5971 * Send application data to be encrypted by the SSL layer
5972 */
Paul Bakker23986e52011-04-24 08:57:21 +00005973int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005974{
Paul Bakker23986e52011-04-24 08:57:21 +00005975 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005976#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
5977 unsigned int max_len;
5978#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005979
5980 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5981
5982 if( ssl->state != SSL_HANDSHAKE_OVER )
5983 {
5984 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5985 {
5986 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5987 return( ret );
5988 }
5989 }
5990
Paul Bakker05decb22013-08-15 13:33:48 +02005991#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005992 /*
5993 * Assume mfl_code is correct since it was checked when set
5994 */
5995 max_len = mfl_code_to_length[ssl->mfl_code];
5996
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005997 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005998 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005999 */
6000 if( ssl->session_out != NULL &&
6001 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6002 {
6003 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6004 }
6005
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006006 if( len > max_len )
6007 {
6008#if defined(POLARSSL_SSL_PROTO_DTLS)
6009 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6010 {
6011 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6012 "maximum fragment length: %d > %d",
6013 len, max_len ) );
6014 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6015 }
6016 else
6017#endif
6018 len = max_len;
6019 }
6020#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006021
Paul Bakker5121ce52009-01-03 21:22:43 +00006022 if( ssl->out_left != 0 )
6023 {
6024 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6025 {
6026 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6027 return( ret );
6028 }
6029 }
Paul Bakker887bd502011-06-08 13:10:54 +00006030 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006031 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006032 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006033 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006034 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006035
6036 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6037 {
6038 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6039 return( ret );
6040 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006041 }
6042
6043 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6044
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006045 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006046}
6047
6048/*
6049 * Notify the peer that the connection is being closed
6050 */
6051int ssl_close_notify( ssl_context *ssl )
6052{
6053 int ret;
6054
6055 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6056
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006057 if( ssl->out_left != 0 )
6058 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006059
6060 if( ssl->state == SSL_HANDSHAKE_OVER )
6061 {
Paul Bakker48916f92012-09-16 19:57:18 +00006062 if( ( ret = ssl_send_alert_message( ssl,
6063 SSL_ALERT_LEVEL_WARNING,
6064 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006065 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006066 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006067 return( ret );
6068 }
6069 }
6070
6071 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6072
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006073 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006074}
6075
Paul Bakker48916f92012-09-16 19:57:18 +00006076void ssl_transform_free( ssl_transform *transform )
6077{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006078 if( transform == NULL )
6079 return;
6080
Paul Bakker48916f92012-09-16 19:57:18 +00006081#if defined(POLARSSL_ZLIB_SUPPORT)
6082 deflateEnd( &transform->ctx_deflate );
6083 inflateEnd( &transform->ctx_inflate );
6084#endif
6085
Paul Bakker84bbeb52014-07-01 14:53:22 +02006086 cipher_free( &transform->cipher_ctx_enc );
6087 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006088
Paul Bakker84bbeb52014-07-01 14:53:22 +02006089 md_free( &transform->md_ctx_enc );
6090 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006091
Paul Bakker34617722014-06-13 17:20:13 +02006092 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006093}
6094
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006095#if defined(POLARSSL_X509_CRT_PARSE_C)
6096static void ssl_key_cert_free( ssl_key_cert *key_cert )
6097{
6098 ssl_key_cert *cur = key_cert, *next;
6099
6100 while( cur != NULL )
6101 {
6102 next = cur->next;
6103
6104 if( cur->key_own_alloc )
6105 {
6106 pk_free( cur->key );
6107 polarssl_free( cur->key );
6108 }
6109 polarssl_free( cur );
6110
6111 cur = next;
6112 }
6113}
6114#endif /* POLARSSL_X509_CRT_PARSE_C */
6115
Paul Bakker48916f92012-09-16 19:57:18 +00006116void ssl_handshake_free( ssl_handshake_params *handshake )
6117{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006118 if( handshake == NULL )
6119 return;
6120
Paul Bakker48916f92012-09-16 19:57:18 +00006121#if defined(POLARSSL_DHM_C)
6122 dhm_free( &handshake->dhm_ctx );
6123#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006124#if defined(POLARSSL_ECDH_C)
6125 ecdh_free( &handshake->ecdh_ctx );
6126#endif
6127
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006128#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006129 /* explicit void pointer cast for buggy MS compiler */
6130 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006131#endif
6132
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006133#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6134 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6135 /*
6136 * Free only the linked list wrapper, not the keys themselves
6137 * since the belong to the SNI callback
6138 */
6139 if( handshake->sni_key_cert != NULL )
6140 {
6141 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6142
6143 while( cur != NULL )
6144 {
6145 next = cur->next;
6146 polarssl_free( cur );
6147 cur = next;
6148 }
6149 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006150#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006151
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006152#if defined(POLARSSL_SSL_PROTO_DTLS)
6153 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006154 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006155 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006156#endif
6157
Paul Bakker34617722014-06-13 17:20:13 +02006158 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006159}
6160
6161void ssl_session_free( ssl_session *session )
6162{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006163 if( session == NULL )
6164 return;
6165
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006166#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006167 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006168 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006169 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006170 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006171 }
Paul Bakkered27a042013-04-18 22:46:23 +02006172#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006173
Paul Bakkera503a632013-08-14 13:48:06 +02006174#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006175 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006176#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006177
Paul Bakker34617722014-06-13 17:20:13 +02006178 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006179}
6180
Paul Bakker5121ce52009-01-03 21:22:43 +00006181/*
6182 * Free an SSL context
6183 */
6184void ssl_free( ssl_context *ssl )
6185{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006186 if( ssl == NULL )
6187 return;
6188
Paul Bakker5121ce52009-01-03 21:22:43 +00006189 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6190
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006191 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006192 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006193 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6194 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006195 }
6196
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006197 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006198 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006199 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6200 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 }
6202
Paul Bakker16770332013-10-11 09:59:44 +02006203#if defined(POLARSSL_ZLIB_SUPPORT)
6204 if( ssl->compress_buf != NULL )
6205 {
Paul Bakker34617722014-06-13 17:20:13 +02006206 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006207 polarssl_free( ssl->compress_buf );
6208 }
6209#endif
6210
Paul Bakker40e46942009-01-03 21:51:57 +00006211#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006212 mpi_free( &ssl->dhm_P );
6213 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006214#endif
6215
Paul Bakker48916f92012-09-16 19:57:18 +00006216 if( ssl->transform )
6217 {
6218 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006219 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006220 }
6221
6222 if( ssl->handshake )
6223 {
6224 ssl_handshake_free( ssl->handshake );
6225 ssl_transform_free( ssl->transform_negotiate );
6226 ssl_session_free( ssl->session_negotiate );
6227
Paul Bakker6e339b52013-07-03 13:37:05 +02006228 polarssl_free( ssl->handshake );
6229 polarssl_free( ssl->transform_negotiate );
6230 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006231 }
6232
Paul Bakkerc0463502013-02-14 11:19:38 +01006233 if( ssl->session )
6234 {
6235 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006236 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006237 }
6238
Paul Bakkera503a632013-08-14 13:48:06 +02006239#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006240 if( ssl->ticket_keys )
6241 {
6242 ssl_ticket_keys_free( ssl->ticket_keys );
6243 polarssl_free( ssl->ticket_keys );
6244 }
Paul Bakkera503a632013-08-14 13:48:06 +02006245#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006246
Paul Bakker0be444a2013-08-27 21:55:01 +02006247#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006248 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006249 {
Paul Bakker34617722014-06-13 17:20:13 +02006250 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006251 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006252 ssl->hostname_len = 0;
6253 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006254#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006255
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006256#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006257 if( ssl->psk != NULL )
6258 {
Paul Bakker34617722014-06-13 17:20:13 +02006259 polarssl_zeroize( ssl->psk, ssl->psk_len );
6260 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006261 polarssl_free( ssl->psk );
6262 polarssl_free( ssl->psk_identity );
6263 ssl->psk_len = 0;
6264 ssl->psk_identity_len = 0;
6265 }
6266#endif
6267
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006268#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006269 ssl_key_cert_free( ssl->key_cert );
6270#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006271
Paul Bakker05ef8352012-05-08 09:17:57 +00006272#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6273 if( ssl_hw_record_finish != NULL )
6274 {
6275 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6276 ssl_hw_record_finish( ssl );
6277 }
6278#endif
6279
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006280#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006281 polarssl_free( ssl->cli_id );
6282#endif
6283
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006285
Paul Bakker86f04f42013-02-14 11:20:09 +01006286 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006287 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006288}
6289
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006290#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006291/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006292 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006293 */
6294unsigned char ssl_sig_from_pk( pk_context *pk )
6295{
6296#if defined(POLARSSL_RSA_C)
6297 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6298 return( SSL_SIG_RSA );
6299#endif
6300#if defined(POLARSSL_ECDSA_C)
6301 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6302 return( SSL_SIG_ECDSA );
6303#endif
6304 return( SSL_SIG_ANON );
6305}
6306
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006307pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6308{
6309 switch( sig )
6310 {
6311#if defined(POLARSSL_RSA_C)
6312 case SSL_SIG_RSA:
6313 return( POLARSSL_PK_RSA );
6314#endif
6315#if defined(POLARSSL_ECDSA_C)
6316 case SSL_SIG_ECDSA:
6317 return( POLARSSL_PK_ECDSA );
6318#endif
6319 default:
6320 return( POLARSSL_PK_NONE );
6321 }
6322}
Paul Bakker9af723c2014-05-01 13:03:14 +02006323#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006324
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006325/*
6326 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6327 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006328md_type_t ssl_md_alg_from_hash( unsigned char hash )
6329{
6330 switch( hash )
6331 {
6332#if defined(POLARSSL_MD5_C)
6333 case SSL_HASH_MD5:
6334 return( POLARSSL_MD_MD5 );
6335#endif
6336#if defined(POLARSSL_SHA1_C)
6337 case SSL_HASH_SHA1:
6338 return( POLARSSL_MD_SHA1 );
6339#endif
6340#if defined(POLARSSL_SHA256_C)
6341 case SSL_HASH_SHA224:
6342 return( POLARSSL_MD_SHA224 );
6343 case SSL_HASH_SHA256:
6344 return( POLARSSL_MD_SHA256 );
6345#endif
6346#if defined(POLARSSL_SHA512_C)
6347 case SSL_HASH_SHA384:
6348 return( POLARSSL_MD_SHA384 );
6349 case SSL_HASH_SHA512:
6350 return( POLARSSL_MD_SHA512 );
6351#endif
6352 default:
6353 return( POLARSSL_MD_NONE );
6354 }
6355}
6356
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006357#if defined(POLARSSL_SSL_SET_CURVES)
6358/*
6359 * Check is a curve proposed by the peer is in our list.
6360 * Return 1 if we're willing to use it, 0 otherwise.
6361 */
6362int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6363{
6364 const ecp_group_id *gid;
6365
6366 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6367 if( *gid == grp_id )
6368 return( 1 );
6369
6370 return( 0 );
6371}
Paul Bakker9af723c2014-05-01 13:03:14 +02006372#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006373
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006374#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006375int ssl_check_cert_usage( const x509_crt *cert,
6376 const ssl_ciphersuite_t *ciphersuite,
6377 int cert_endpoint )
6378{
6379#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6380 int usage = 0;
6381#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006382#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6383 const char *ext_oid;
6384 size_t ext_len;
6385#endif
6386
6387#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6388 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6389 ((void) cert);
6390 ((void) cert_endpoint);
6391#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006392
6393#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6394 if( cert_endpoint == SSL_IS_SERVER )
6395 {
6396 /* Server part of the key exchange */
6397 switch( ciphersuite->key_exchange )
6398 {
6399 case POLARSSL_KEY_EXCHANGE_RSA:
6400 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6401 usage = KU_KEY_ENCIPHERMENT;
6402 break;
6403
6404 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6405 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6406 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6407 usage = KU_DIGITAL_SIGNATURE;
6408 break;
6409
6410 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6411 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6412 usage = KU_KEY_AGREEMENT;
6413 break;
6414
6415 /* Don't use default: we want warnings when adding new values */
6416 case POLARSSL_KEY_EXCHANGE_NONE:
6417 case POLARSSL_KEY_EXCHANGE_PSK:
6418 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6419 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6420 usage = 0;
6421 }
6422 }
6423 else
6424 {
6425 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6426 usage = KU_DIGITAL_SIGNATURE;
6427 }
6428
6429 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6430 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006431#else
6432 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006433#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6434
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006435#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6436 if( cert_endpoint == SSL_IS_SERVER )
6437 {
6438 ext_oid = OID_SERVER_AUTH;
6439 ext_len = OID_SIZE( OID_SERVER_AUTH );
6440 }
6441 else
6442 {
6443 ext_oid = OID_CLIENT_AUTH;
6444 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6445 }
6446
6447 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6448 return( -1 );
6449#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6450
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006451 return( 0 );
6452}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006453#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006454
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006455/*
6456 * Convert version numbers to/from wire format
6457 * and, for DTLS, to/from TLS equivalent.
6458 *
6459 * For TLS this is the identity.
6460 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6461 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6462 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6463 */
6464void ssl_write_version( int major, int minor, int transport,
6465 unsigned char ver[2] )
6466{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006467#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006468 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006469 {
6470 if( minor == SSL_MINOR_VERSION_2 )
6471 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6472
6473 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6474 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6475 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006476 else
6477#else
6478 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006479#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006480 {
6481 ver[0] = (unsigned char) major;
6482 ver[1] = (unsigned char) minor;
6483 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006484}
6485
6486void ssl_read_version( int *major, int *minor, int transport,
6487 const unsigned char ver[2] )
6488{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006489#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006490 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006491 {
6492 *major = 255 - ver[0] + 2;
6493 *minor = 255 - ver[1] + 1;
6494
6495 if( *minor == SSL_MINOR_VERSION_1 )
6496 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6497 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006498 else
6499#else
6500 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006501#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006502 {
6503 *major = ver[0];
6504 *minor = ver[1];
6505 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006506}
6507
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006508#endif /* POLARSSL_SSL_TLS_C */