blob: 783b00c9084b35e3a9f3308623ddba99a1275f38 [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 Bakker05decb22013-08-15 13:33:48 +020064#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020065/*
66 * Convert max_fragment_length codes to length.
67 * RFC 6066 says:
68 * enum{
69 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
70 * } MaxFragmentLength;
71 * and we add 0 -> extension unused
72 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020073static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020074{
75 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
76 512, /* SSL_MAX_FRAG_LEN_512 */
77 1024, /* SSL_MAX_FRAG_LEN_1024 */
78 2048, /* SSL_MAX_FRAG_LEN_2048 */
79 4096, /* SSL_MAX_FRAG_LEN_4096 */
80};
Paul Bakker05decb22013-08-15 13:33:48 +020081#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020082
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020083static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
84{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020085 ssl_session_free( dst );
86 memcpy( dst, src, sizeof( ssl_session ) );
87
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089 if( src->peer_cert != NULL )
90 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020091 int ret;
92
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020093 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
94 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020095 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
96
Paul Bakkerb6b09562013-09-18 14:17:41 +020097 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098
Paul Bakkerddf26b42013-09-18 13:46:23 +020099 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
100 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101 {
102 polarssl_free( dst->peer_cert );
103 dst->peer_cert = NULL;
104 return( ret );
105 }
106 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200108
Paul Bakkera503a632013-08-14 13:48:06 +0200109#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110 if( src->ticket != NULL )
111 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200112 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
113 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200114 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
115
116 memcpy( dst->ticket, src->ticket, src->ticket_len );
117 }
Paul Bakkera503a632013-08-14 13:48:06 +0200118#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119
120 return( 0 );
121}
122
Paul Bakker05ef8352012-05-08 09:17:57 +0000123#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
124int (*ssl_hw_record_init)(ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200125 const unsigned char *key_enc, const unsigned char *key_dec,
126 size_t keylen,
127 const unsigned char *iv_enc, const unsigned char *iv_dec,
128 size_t ivlen,
129 const unsigned char *mac_enc, const unsigned char *mac_dec,
130 size_t maclen) = NULL;
Paul Bakker07eb38b2012-12-19 14:42:06 +0100131int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000132int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
133int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
134int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
135int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200136#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000137
Paul Bakker5121ce52009-01-03 21:22:43 +0000138/*
139 * Key material generation
140 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200141#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200142static int ssl3_prf( const unsigned char *secret, size_t slen,
143 const char *label,
144 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000145 unsigned char *dstbuf, size_t dlen )
146{
147 size_t i;
148 md5_context md5;
149 sha1_context sha1;
150 unsigned char padding[16];
151 unsigned char sha1sum[20];
152 ((void)label);
153
154 /*
155 * SSLv3:
156 * block =
157 * MD5( secret + SHA1( 'A' + secret + random ) ) +
158 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
159 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
160 * ...
161 */
162 for( i = 0; i < dlen / 16; i++ )
163 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200164 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000165
166 sha1_starts( &sha1 );
167 sha1_update( &sha1, padding, 1 + i );
168 sha1_update( &sha1, secret, slen );
169 sha1_update( &sha1, random, rlen );
170 sha1_finish( &sha1, sha1sum );
171
172 md5_starts( &md5 );
173 md5_update( &md5, secret, slen );
174 md5_update( &md5, sha1sum, 20 );
175 md5_finish( &md5, dstbuf + i * 16 );
176 }
177
178 memset( &md5, 0, sizeof( md5 ) );
179 memset( &sha1, 0, sizeof( sha1 ) );
180
181 memset( padding, 0, sizeof( padding ) );
182 memset( sha1sum, 0, sizeof( sha1sum ) );
183
184 return( 0 );
185}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200186#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000187
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200188#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200189static int tls1_prf( const unsigned char *secret, size_t slen,
190 const char *label,
191 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000192 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
Paul Bakker23986e52011-04-24 08:57:21 +0000194 size_t nb, hs;
195 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200196 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 unsigned char tmp[128];
198 unsigned char h_i[20];
199
200 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000201 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 hs = ( slen + 1 ) / 2;
204 S1 = secret;
205 S2 = secret + slen - hs;
206
207 nb = strlen( label );
208 memcpy( tmp + 20, label, nb );
209 memcpy( tmp + 20 + nb, random, rlen );
210 nb += rlen;
211
212 /*
213 * First compute P_md5(secret,label+random)[0..dlen]
214 */
215 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
216
217 for( i = 0; i < dlen; i += 16 )
218 {
219 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
220 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
221
222 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
223
224 for( j = 0; j < k; j++ )
225 dstbuf[i + j] = h_i[j];
226 }
227
228 /*
229 * XOR out with P_sha1(secret,label+random)[0..dlen]
230 */
231 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
232
233 for( i = 0; i < dlen; i += 20 )
234 {
235 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
236 sha1_hmac( S2, hs, tmp, 20, tmp );
237
238 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
239
240 for( j = 0; j < k; j++ )
241 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
242 }
243
244 memset( tmp, 0, sizeof( tmp ) );
245 memset( h_i, 0, sizeof( h_i ) );
246
247 return( 0 );
248}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200249#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200251#if defined(POLARSSL_SSL_PROTO_TLS1_2)
252#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200253static int tls_prf_sha256( const unsigned char *secret, size_t slen,
254 const char *label,
255 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000256 unsigned char *dstbuf, size_t dlen )
257{
258 size_t nb;
259 size_t i, j, k;
260 unsigned char tmp[128];
261 unsigned char h_i[32];
262
263 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
264 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
265
266 nb = strlen( label );
267 memcpy( tmp + 32, label, nb );
268 memcpy( tmp + 32 + nb, random, rlen );
269 nb += rlen;
270
271 /*
272 * Compute P_<hash>(secret, label + random)[0..dlen]
273 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200274 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000275
276 for( i = 0; i < dlen; i += 32 )
277 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200278 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
279 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
282
283 for( j = 0; j < k; j++ )
284 dstbuf[i + j] = h_i[j];
285 }
286
287 memset( tmp, 0, sizeof( tmp ) );
288 memset( h_i, 0, sizeof( h_i ) );
289
290 return( 0 );
291}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200292#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000293
Paul Bakker9e36f042013-06-30 14:34:05 +0200294#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200295static int tls_prf_sha384( const unsigned char *secret, size_t slen,
296 const char *label,
297 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000298 unsigned char *dstbuf, size_t dlen )
299{
300 size_t nb;
301 size_t i, j, k;
302 unsigned char tmp[128];
303 unsigned char h_i[48];
304
305 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
306 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
307
308 nb = strlen( label );
309 memcpy( tmp + 48, label, nb );
310 memcpy( tmp + 48 + nb, random, rlen );
311 nb += rlen;
312
313 /*
314 * Compute P_<hash>(secret, label + random)[0..dlen]
315 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200316 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000317
318 for( i = 0; i < dlen; i += 48 )
319 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200320 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
321 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
324
325 for( j = 0; j < k; j++ )
326 dstbuf[i + j] = h_i[j];
327 }
328
329 memset( tmp, 0, sizeof( tmp ) );
330 memset( h_i, 0, sizeof( h_i ) );
331
332 return( 0 );
333}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200334#endif /* POLARSSL_SHA512_C */
335#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200337static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338
339#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
340 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200341static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif
Paul Bakker380da532012-04-18 16:10:25 +0000343
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200344#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000345static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000346static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
348
349#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
350static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000351static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1_2)
355#if defined(POLARSSL_SHA256_C)
356static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
357static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000358static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200359#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100360
Paul Bakker9e36f042013-06-30 14:34:05 +0200361#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200362static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100363static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000364static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100365#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200366#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000367
Paul Bakker5121ce52009-01-03 21:22:43 +0000368int ssl_derive_keys( ssl_context *ssl )
369{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200370 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 unsigned char keyblk[256];
373 unsigned char *key1;
374 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100375 unsigned char *mac_enc;
376 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200377 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100378 const cipher_info_t *cipher_info;
379 const md_info_t *md_info;
380
Paul Bakker48916f92012-09-16 19:57:18 +0000381 ssl_session *session = ssl->session_negotiate;
382 ssl_transform *transform = ssl->transform_negotiate;
383 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
386
Paul Bakker68884e32013-01-07 18:20:04 +0100387 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
388 if( cipher_info == NULL )
389 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200390 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100391 transform->ciphersuite_info->cipher ) );
392 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
393 }
394
395 md_info = md_info_from_type( transform->ciphersuite_info->mac );
396 if( md_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->mac ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000405 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200406#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000407 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408 {
Paul Bakker48916f92012-09-16 19:57:18 +0000409 handshake->tls_prf = ssl3_prf;
410 handshake->calc_verify = ssl_calc_verify_ssl;
411 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200413 else
414#endif
415#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
416 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 {
Paul Bakker48916f92012-09-16 19:57:18 +0000418 handshake->tls_prf = tls1_prf;
419 handshake->calc_verify = ssl_calc_verify_tls;
420 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000421 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200422 else
423#endif
424#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200425#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
427 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = tls_prf_sha384;
430 handshake->calc_verify = ssl_calc_verify_tls_sha384;
431 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000433 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434#endif
435#if defined(POLARSSL_SHA256_C)
436 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 {
Paul Bakker48916f92012-09-16 19:57:18 +0000438 handshake->tls_prf = tls_prf_sha256;
439 handshake->calc_verify = ssl_calc_verify_tls_sha256;
440 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000441 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442 else
443#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200444#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200445 {
446 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200447 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200448 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000449
450 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 * SSLv3:
452 * master =
453 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
454 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
455 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200456 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200457 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 * master = PRF( premaster, "master secret", randbytes )[0..47]
459 */
Paul Bakker0a597072012-09-25 21:55:46 +0000460 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 {
Paul Bakker48916f92012-09-16 19:57:18 +0000462 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
463 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Paul Bakker48916f92012-09-16 19:57:18 +0000465 handshake->tls_prf( handshake->premaster, handshake->pmslen,
466 "master secret",
467 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Paul Bakker48916f92012-09-16 19:57:18 +0000469 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 }
471 else
472 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
473
474 /*
475 * Swap the client and server random values.
476 */
Paul Bakker48916f92012-09-16 19:57:18 +0000477 memcpy( tmp, handshake->randbytes, 64 );
478 memcpy( handshake->randbytes, tmp + 32, 32 );
479 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 memset( tmp, 0, sizeof( tmp ) );
481
482 /*
483 * SSLv3:
484 * key block =
485 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
486 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
487 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
488 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
489 * ...
490 *
491 * TLSv1:
492 * key block = PRF( master, "key expansion", randbytes )
493 */
Paul Bakker48916f92012-09-16 19:57:18 +0000494 handshake->tls_prf( session->master, 48, "key expansion",
495 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
Paul Bakker48916f92012-09-16 19:57:18 +0000497 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
498 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
499 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
500 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
502
Paul Bakker48916f92012-09-16 19:57:18 +0000503 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505 /*
506 * Determine the appropriate key, IV and MAC length.
507 */
Paul Bakker68884e32013-01-07 18:20:04 +0100508
509 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 {
Paul Bakker68884e32013-01-07 18:20:04 +0100511 transform->keylen = cipher_info->key_length;
512 transform->keylen /= 8;
513 transform->minlen = 1;
514 transform->ivlen = 12;
515 transform->fixed_ivlen = 4;
516 transform->maclen = 0;
517 }
518 else
519 {
520 if( md_info->type != POLARSSL_MD_NONE )
521 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200522 int ret;
523
Paul Bakker61d113b2013-07-04 11:51:43 +0200524 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
525 {
526 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
527 return( ret );
528 }
529
530 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
531 {
532 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
533 return( ret );
534 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Paul Bakker68884e32013-01-07 18:20:04 +0100536 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200537
Paul Bakker1f2bc622013-08-15 13:45:55 +0200538#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200539 /*
540 * If HMAC is to be truncated, we shall keep the leftmost bytes,
541 * (rfc 6066 page 13 or rfc 2104 section 4),
542 * so we only need to adjust the length here.
543 */
544 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
545 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200546#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Paul Bakker68884e32013-01-07 18:20:04 +0100549 transform->keylen = cipher_info->key_length;
550 transform->keylen /= 8;
551 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Paul Bakker68884e32013-01-07 18:20:04 +0100553 transform->minlen = transform->keylen;
554 if( transform->minlen < transform->maclen )
555 {
556 if( cipher_info->mode == POLARSSL_MODE_STREAM )
557 transform->minlen = transform->maclen;
558 else
559 transform->minlen += transform->keylen;
560 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 }
562
563 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000564 transform->keylen, transform->minlen, transform->ivlen,
565 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 /*
568 * Finally setup the cipher contexts, IVs and MAC secrets.
569 */
570 if( ssl->endpoint == SSL_IS_CLIENT )
571 {
Paul Bakker48916f92012-09-16 19:57:18 +0000572 key1 = keyblk + transform->maclen * 2;
573 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Paul Bakker68884e32013-01-07 18:20:04 +0100575 mac_enc = keyblk;
576 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000578 /*
579 * This is not used in TLS v1.1.
580 */
Paul Bakker48916f92012-09-16 19:57:18 +0000581 iv_copy_len = ( transform->fixed_ivlen ) ?
582 transform->fixed_ivlen : transform->ivlen;
583 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
584 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000585 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 }
587 else
588 {
Paul Bakker48916f92012-09-16 19:57:18 +0000589 key1 = keyblk + transform->maclen * 2 + transform->keylen;
590 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Paul Bakker68884e32013-01-07 18:20:04 +0100592 mac_enc = keyblk + transform->maclen;
593 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000595 /*
596 * This is not used in TLS v1.1.
597 */
Paul Bakker48916f92012-09-16 19:57:18 +0000598 iv_copy_len = ( transform->fixed_ivlen ) ?
599 transform->fixed_ivlen : transform->ivlen;
600 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
601 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000602 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 }
604
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200605#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100606 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
607 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100608 if( transform->maclen > sizeof transform->mac_enc )
609 {
610 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200611 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100612 }
613
Paul Bakker68884e32013-01-07 18:20:04 +0100614 memcpy( transform->mac_enc, mac_enc, transform->maclen );
615 memcpy( transform->mac_dec, mac_dec, transform->maclen );
616 }
617 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200618#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200619#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
620 defined(POLARSSL_SSL_PROTO_TLS1_2)
621 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100622 {
623 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
624 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
625 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200626 else
627#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200628 {
629 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200630 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200631 }
Paul Bakker68884e32013-01-07 18:20:04 +0100632
Paul Bakker05ef8352012-05-08 09:17:57 +0000633#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
634 if( ssl_hw_record_init != NULL)
635 {
636 int ret = 0;
637
638 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
639
Paul Bakker07eb38b2012-12-19 14:42:06 +0100640 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
641 transform->iv_enc, transform->iv_dec,
642 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100643 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100644 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000645 {
646 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
647 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
648 }
649 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200650#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000651
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200652 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
653 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200655 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
656 return( ret );
657 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200658
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200659 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
660 cipher_info ) ) != 0 )
661 {
662 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
663 return( ret );
664 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200665
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200666 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
667 cipher_info->key_length,
668 POLARSSL_ENCRYPT ) ) != 0 )
669 {
670 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
671 return( ret );
672 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200673
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200674 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
675 cipher_info->key_length,
676 POLARSSL_DECRYPT ) ) != 0 )
677 {
678 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
679 return( ret );
680 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200681
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200682#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200683 if( cipher_info->mode == POLARSSL_MODE_CBC )
684 {
685 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
686 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200687 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200688 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
689 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200690 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200691
692 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
693 POLARSSL_PADDING_NONE ) ) != 0 )
694 {
695 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
696 return( ret );
697 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200699#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701 memset( keyblk, 0, sizeof( keyblk ) );
702
Paul Bakker2770fbd2012-07-03 13:30:23 +0000703#if defined(POLARSSL_ZLIB_SUPPORT)
704 // Initialize compression
705 //
Paul Bakker48916f92012-09-16 19:57:18 +0000706 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000707 {
Paul Bakker16770332013-10-11 09:59:44 +0200708 if( ssl->compress_buf == NULL )
709 {
710 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
711 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
712 if( ssl->compress_buf == NULL )
713 {
714 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
715 SSL_BUFFER_LEN ) );
716 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
717 }
718 }
719
Paul Bakker2770fbd2012-07-03 13:30:23 +0000720 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
721
Paul Bakker48916f92012-09-16 19:57:18 +0000722 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
723 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000724
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200725 if( deflateInit( &transform->ctx_deflate,
726 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000727 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000728 {
729 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
730 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
731 }
732 }
733#endif /* POLARSSL_ZLIB_SUPPORT */
734
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
736
737 return( 0 );
738}
739
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200740#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000741void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742{
743 md5_context md5;
744 sha1_context sha1;
745 unsigned char pad_1[48];
746 unsigned char pad_2[48];
747
Paul Bakker380da532012-04-18 16:10:25 +0000748 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker48916f92012-09-16 19:57:18 +0000750 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
751 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Paul Bakker380da532012-04-18 16:10:25 +0000753 memset( pad_1, 0x36, 48 );
754 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakker48916f92012-09-16 19:57:18 +0000756 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000757 md5_update( &md5, pad_1, 48 );
758 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Paul Bakker380da532012-04-18 16:10:25 +0000760 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000761 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000762 md5_update( &md5, pad_2, 48 );
763 md5_update( &md5, hash, 16 );
764 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakker48916f92012-09-16 19:57:18 +0000766 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000767 sha1_update( &sha1, pad_1, 40 );
768 sha1_finish( &sha1, hash + 16 );
769
770 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000771 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000772 sha1_update( &sha1, pad_2, 40 );
773 sha1_update( &sha1, hash + 16, 20 );
774 sha1_finish( &sha1, hash + 16 );
775
776 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
777 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
778
779 return;
780}
Paul Bakker9af723c2014-05-01 13:03:14 +0200781#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000782
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000784void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
785{
786 md5_context md5;
787 sha1_context sha1;
788
789 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
790
Paul Bakker48916f92012-09-16 19:57:18 +0000791 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
792 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000793
Paul Bakker48916f92012-09-16 19:57:18 +0000794 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000795 sha1_finish( &sha1, hash + 16 );
796
797 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
798 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
799
800 return;
801}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200802#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000803
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#if defined(POLARSSL_SSL_PROTO_TLS1_2)
805#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000806void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
807{
Paul Bakker9e36f042013-06-30 14:34:05 +0200808 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000809
810 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
811
Paul Bakker9e36f042013-06-30 14:34:05 +0200812 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
813 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000814
815 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
816 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
817
818 return;
819}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000821
Paul Bakker9e36f042013-06-30 14:34:05 +0200822#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000823void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
824{
Paul Bakker9e36f042013-06-30 14:34:05 +0200825 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000826
827 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
828
Paul Bakker9e36f042013-06-30 14:34:05 +0200829 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
830 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Paul Bakkerca4ab492012-04-18 14:23:57 +0000832 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
834
835 return;
836}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200837#endif /* POLARSSL_SHA512_C */
838#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200840#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200841int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
842{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200843 unsigned char *p = ssl->handshake->premaster;
844 unsigned char *end = p + sizeof( ssl->handshake->premaster );
845
846 /*
847 * PMS = struct {
848 * opaque other_secret<0..2^16-1>;
849 * opaque psk<0..2^16-1>;
850 * };
851 * with "other_secret" depending on the particular key exchange
852 */
853#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
854 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
855 {
856 if( end - p < 2 + (int) ssl->psk_len )
857 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
858
859 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
860 *(p++) = (unsigned char)( ssl->psk_len );
861 p += ssl->psk_len;
862 }
863 else
864#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200865#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
866 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
867 {
868 /*
869 * other_secret already set by the ClientKeyExchange message,
870 * and is 48 bytes long
871 */
872 *p++ = 0;
873 *p++ = 48;
874 p += 48;
875 }
876 else
877#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200878#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
879 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
880 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200881 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200882 size_t len = ssl->handshake->dhm_ctx.len;
883
884 if( end - p < 2 + (int) len )
885 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
886
887 *(p++) = (unsigned char)( len >> 8 );
888 *(p++) = (unsigned char)( len );
889 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
890 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
891 {
892 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
893 return( ret );
894 }
895 p += len;
896
897 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
898 }
899 else
900#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
901#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
902 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
903 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200904 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200905 size_t zlen;
906
907 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
908 p + 2, end - (p + 2),
909 ssl->f_rng, ssl->p_rng ) ) != 0 )
910 {
911 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
912 return( ret );
913 }
914
915 *(p++) = (unsigned char)( zlen >> 8 );
916 *(p++) = (unsigned char)( zlen );
917 p += zlen;
918
919 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
920 }
921 else
922#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
923 {
924 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200925 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200926 }
927
928 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100929 if( end - p < 2 + (int) ssl->psk_len )
930 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
931
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
933 *(p++) = (unsigned char)( ssl->psk_len );
934 memcpy( p, ssl->psk, ssl->psk_len );
935 p += ssl->psk_len;
936
937 ssl->handshake->pmslen = p - ssl->handshake->premaster;
938
939 return( 0 );
940}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200941#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200942
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200943#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000944/*
945 * SSLv3.0 MAC functions
946 */
Paul Bakker68884e32013-01-07 18:20:04 +0100947static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
948 unsigned char *buf, size_t len,
949 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000950{
951 unsigned char header[11];
952 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100953 int padlen = 0;
954 int md_size = md_get_size( md_ctx->md_info );
955 int md_type = md_get_type( md_ctx->md_info );
956
957 if( md_type == POLARSSL_MD_MD5 )
958 padlen = 48;
959 else if( md_type == POLARSSL_MD_SHA1 )
960 padlen = 40;
961 else if( md_type == POLARSSL_MD_SHA256 )
962 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100963 else if( md_type == POLARSSL_MD_SHA384 )
964 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
966 memcpy( header, ctr, 8 );
967 header[ 8] = (unsigned char) type;
968 header[ 9] = (unsigned char)( len >> 8 );
969 header[10] = (unsigned char)( len );
970
Paul Bakker68884e32013-01-07 18:20:04 +0100971 memset( padding, 0x36, padlen );
972 md_starts( md_ctx );
973 md_update( md_ctx, secret, md_size );
974 md_update( md_ctx, padding, padlen );
975 md_update( md_ctx, header, 11 );
976 md_update( md_ctx, buf, len );
977 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Paul Bakker68884e32013-01-07 18:20:04 +0100979 memset( padding, 0x5C, padlen );
980 md_starts( md_ctx );
981 md_update( md_ctx, secret, md_size );
982 md_update( md_ctx, padding, padlen );
983 md_update( md_ctx, buf + len, md_size );
984 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000985}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200986#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000987
Paul Bakker5121ce52009-01-03 21:22:43 +0000988/*
989 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200990 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000991static int ssl_encrypt_buf( ssl_context *ssl )
992{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200993 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
996
997 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200998 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001000#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1001 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1002 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1003 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
1004 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001005 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001006#if defined(POLARSSL_SSL_PROTO_SSL3)
1007 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1008 {
1009 ssl_mac( &ssl->transform_out->md_ctx_enc,
1010 ssl->transform_out->mac_enc,
1011 ssl->out_msg, ssl->out_msglen,
1012 ssl->out_ctr, ssl->out_msgtype );
1013 }
1014 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001015#endif
1016#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001017 defined(POLARSSL_SSL_PROTO_TLS1_2)
1018 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1019 {
1020 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1021 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1022 ssl->out_msg, ssl->out_msglen );
1023 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1024 ssl->out_msg + ssl->out_msglen );
1025 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1026 }
1027 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001028#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001029 {
1030 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001031 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001032 }
1033
1034 SSL_DEBUG_BUF( 4, "computed mac",
1035 ssl->out_msg + ssl->out_msglen,
1036 ssl->transform_out->maclen );
1037
1038 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001039 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001040#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001042 /*
1043 * Encrypt
1044 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001045#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001046 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1047 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001049 int ret;
1050 size_t olen = 0;
1051
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1053 "including %d bytes of padding",
1054 ssl->out_msglen, 0 ) );
1055
1056 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1057 ssl->out_msg, ssl->out_msglen );
1058
Paul Bakker45125bc2013-09-04 16:47:11 +02001059 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001060 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001061 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1062 return( ret );
1063 }
1064
1065 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1066 ssl->transform_out->iv_enc,
1067 ssl->transform_out->ivlen ) ) != 0 )
1068 {
1069 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001070 return( ret );
1071 }
1072
1073 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1074 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1075 &olen ) ) != 0 )
1076 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001077 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001078 return( ret );
1079 }
1080
1081 if( ssl->out_msglen != olen )
1082 {
1083 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1084 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001085 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001086 }
1087
1088 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001089 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001090 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001091 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001092 return( ret );
1093 }
1094
1095 if( 0 != olen )
1096 {
1097 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1098 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001099 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 }
Paul Bakker68884e32013-01-07 18:20:04 +01001102 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001103#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001104#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001105 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1106 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001107 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001108 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001109 unsigned char *enc_msg;
1110 unsigned char add_data[13];
1111 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1112
Paul Bakkerca4ab492012-04-18 14:23:57 +00001113 memcpy( add_data, ssl->out_ctr, 8 );
1114 add_data[8] = ssl->out_msgtype;
1115 add_data[9] = ssl->major_ver;
1116 add_data[10] = ssl->minor_ver;
1117 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1118 add_data[12] = ssl->out_msglen & 0xFF;
1119
1120 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1121 add_data, 13 );
1122
Paul Bakker68884e32013-01-07 18:20:04 +01001123 /*
1124 * Generate IV
1125 */
1126 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001127 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1128 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001129 if( ret != 0 )
1130 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131
Paul Bakker68884e32013-01-07 18:20:04 +01001132 memcpy( ssl->out_iv,
1133 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1134 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001136 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001137 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001138
Paul Bakker68884e32013-01-07 18:20:04 +01001139 /*
1140 * Fix pointer positions and message length with added IV
1141 */
1142 enc_msg = ssl->out_msg;
1143 enc_msglen = ssl->out_msglen;
1144 ssl->out_msglen += ssl->transform_out->ivlen -
1145 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001146
Paul Bakker68884e32013-01-07 18:20:04 +01001147 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1148 "including %d bytes of padding",
1149 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001150
Paul Bakker68884e32013-01-07 18:20:04 +01001151 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1152 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001153
Paul Bakker68884e32013-01-07 18:20:04 +01001154 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001155 * Encrypt
1156 */
1157 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1158 ssl->transform_out->iv_enc,
1159 ssl->transform_out->ivlen ) ) != 0 ||
1160 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1161 {
Manuel Pégourié-Gonnard8ff17c52014-04-11 17:27:48 +02001162 SSL_DEBUG_RET( 1, "cipher_reset", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001163 return( ret );
1164 }
1165
1166 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1167 add_data, 13 ) ) != 0 )
1168 {
Manuel Pégourié-Gonnard8ff17c52014-04-11 17:27:48 +02001169 SSL_DEBUG_RET( 1, "cipher_update_ad", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001170 return( ret );
1171 }
1172
1173 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1174 enc_msg, enc_msglen,
1175 enc_msg, &olen ) ) != 0 )
1176 {
Manuel Pégourié-Gonnard8ff17c52014-04-11 17:27:48 +02001177 SSL_DEBUG_RET( 1, "cipher_update", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001178 return( ret );
1179 }
1180 totlen = olen;
1181
1182 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1183 enc_msg + olen, &olen ) ) != 0 )
1184 {
Manuel Pégourié-Gonnard8ff17c52014-04-11 17:27:48 +02001185 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001186 return( ret );
1187 }
1188 totlen += olen;
1189
1190 if( totlen != enc_msglen )
1191 {
1192 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001193 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001194 }
1195
1196 /*
1197 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001198 */
1199 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001200
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001201 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1202 enc_msg + enc_msglen, 16 ) ) != 0 )
1203 {
Manuel Pégourié-Gonnard8ff17c52014-04-11 17:27:48 +02001204 SSL_DEBUG_RET( 1, "cipher_write_tag", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001205 return( ret );
1206 }
Paul Bakker68884e32013-01-07 18:20:04 +01001207
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001208 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001209 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 else
Paul Bakker68884e32013-01-07 18:20:04 +01001211#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001212#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1213 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001214 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1215 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001217 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001218 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001219 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220
Paul Bakker48916f92012-09-16 19:57:18 +00001221 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1222 ssl->transform_out->ivlen;
1223 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 padlen = 0;
1225
1226 for( i = 0; i <= padlen; i++ )
1227 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1228
1229 ssl->out_msglen += padlen + 1;
1230
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001231 enc_msglen = ssl->out_msglen;
1232 enc_msg = ssl->out_msg;
1233
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001234#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001236 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1237 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001239 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240 {
1241 /*
1242 * Generate IV
1243 */
Paul Bakker48916f92012-09-16 19:57:18 +00001244 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1245 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001246 if( ret != 0 )
1247 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001248
Paul Bakker92be97b2013-01-02 17:30:03 +01001249 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001250 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001251
1252 /*
1253 * Fix pointer positions and message length with added IV
1254 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001255 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001256 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001257 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001258 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001259#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001260
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001262 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001263 ssl->out_msglen, ssl->transform_out->ivlen,
1264 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265
1266 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001267 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001268
Paul Bakker45125bc2013-09-04 16:47:11 +02001269 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001271 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1272 return( ret );
1273 }
1274
1275 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1276 ssl->transform_out->iv_enc,
1277 ssl->transform_out->ivlen ) ) != 0 )
1278 {
1279 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001280 return( ret );
1281 }
Paul Bakker68884e32013-01-07 18:20:04 +01001282
Paul Bakkercca5b812013-08-31 17:40:26 +02001283 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1284 enc_msg, enc_msglen, enc_msg,
1285 &olen ) ) != 0 )
1286 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001287 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001288 return( ret );
1289 }
Paul Bakker68884e32013-01-07 18:20:04 +01001290
Paul Bakkercca5b812013-08-31 17:40:26 +02001291 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001292
Paul Bakkercca5b812013-08-31 17:40:26 +02001293 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001294 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001295 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001296 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001297 return( ret );
1298 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001299
Paul Bakkercca5b812013-08-31 17:40:26 +02001300 if( enc_msglen != olen )
1301 {
1302 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1303 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001304 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001305 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001306
1307#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001308 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1309 {
1310 /*
1311 * Save IV in SSL3 and TLS1
1312 */
1313 memcpy( ssl->transform_out->iv_enc,
1314 ssl->transform_out->cipher_ctx_enc.iv,
1315 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001319 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001320#endif /* POLARSSL_CIPHER_MODE_CBC &&
1321 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001322 {
1323 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001324 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Paul Bakkerca4ab492012-04-18 14:23:57 +00001327 for( i = 8; i > 0; i-- )
1328 if( ++ssl->out_ctr[i - 1] != 0 )
1329 break;
1330
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001331 /* The loops goes to its end iff the counter is wrapping */
1332 if( i == 0 )
1333 {
1334 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1335 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1336 }
1337
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1339
1340 return( 0 );
1341}
1342
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001343#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001344
Paul Bakker5121ce52009-01-03 21:22:43 +00001345static int ssl_decrypt_buf( ssl_context *ssl )
1346{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001347 size_t i;
1348#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1349 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1350 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1351 size_t padlen = 0, correct = 1;
1352#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
1354 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1355
Paul Bakker48916f92012-09-16 19:57:18 +00001356 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 {
1358 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001359 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001360 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
1362
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001363#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001364 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1365 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001366 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001367 int ret;
1368 size_t olen = 0;
1369
Paul Bakker68884e32013-01-07 18:20:04 +01001370 padlen = 0;
1371
Paul Bakker45125bc2013-09-04 16:47:11 +02001372 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001373 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001374 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1375 return( ret );
1376 }
1377
1378 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1379 ssl->transform_in->iv_dec,
1380 ssl->transform_in->ivlen ) ) != 0 )
1381 {
1382 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001383 return( ret );
1384 }
1385
1386 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1387 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1388 &olen ) ) != 0 )
1389 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001390 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001391 return( ret );
1392 }
1393
1394 if( ssl->in_msglen != olen )
1395 {
1396 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001397 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001398 }
1399
1400 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001401 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001402 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001403 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001404 return( ret );
1405 }
1406
1407 if( 0 != olen )
1408 {
1409 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
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 */
Paul Bakker68884e32013-01-07 18:20:04 +01001415#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001416 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1417 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001418 {
1419 unsigned char *dec_msg;
1420 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001421 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001422 unsigned char add_data[13];
1423 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1424
Paul Bakker68884e32013-01-07 18:20:04 +01001425 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1426 ssl->transform_in->fixed_ivlen );
1427 dec_msglen -= 16;
1428 dec_msg = ssl->in_msg;
1429 dec_msg_result = ssl->in_msg;
1430 ssl->in_msglen = dec_msglen;
1431
1432 memcpy( add_data, ssl->in_ctr, 8 );
1433 add_data[8] = ssl->in_msgtype;
1434 add_data[9] = ssl->major_ver;
1435 add_data[10] = ssl->minor_ver;
1436 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1437 add_data[12] = ssl->in_msglen & 0xFF;
1438
1439 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1440 add_data, 13 );
1441
1442 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1443 ssl->in_iv,
1444 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1445
1446 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1447 ssl->transform_in->ivlen );
1448 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1449
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001450 /*
1451 * Decrypt
1452 */
1453 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1454 ssl->transform_in->iv_dec,
1455 ssl->transform_in->ivlen ) ) != 0 ||
1456 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001457 {
Manuel Pégourié-Gonnard8ff17c52014-04-11 17:27:48 +02001458 SSL_DEBUG_RET( 1, "cipher_reset", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001459 return( ret );
1460 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001461
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001462 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1463 add_data, 13 ) ) != 0 )
1464 {
Manuel Pégourié-Gonnard8ff17c52014-04-11 17:27:48 +02001465 SSL_DEBUG_RET( 1, "cipher_update_ad", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001466 return( ret );
1467 }
1468
1469 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1470 dec_msg, dec_msglen,
1471 dec_msg_result, &olen ) ) != 0 )
1472 {
Manuel Pégourié-Gonnard8ff17c52014-04-11 17:27:48 +02001473 SSL_DEBUG_RET( 1, "cipher_update", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001474 return( ret );
1475 }
1476 totlen = olen;
1477
1478 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1479 dec_msg_result + olen, &olen ) ) != 0 )
1480 {
Manuel Pégourié-Gonnard8ff17c52014-04-11 17:27:48 +02001481 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001482 return( ret );
1483 }
1484 totlen += olen;
1485
1486 if( totlen != dec_msglen )
1487 {
1488 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001489 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001490 }
1491
1492 /*
1493 * Authenticate
1494 */
1495 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1496 dec_msg + dec_msglen, 16 ) ) != 0 )
1497 {
1498 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001499 return( POLARSSL_ERR_SSL_INVALID_MAC );
1500 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001501
Paul Bakkerca4ab492012-04-18 14:23:57 +00001502 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001503 else
Paul Bakker68884e32013-01-07 18:20:04 +01001504#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001505#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1506 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001507 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1508 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 {
Paul Bakker45829992013-01-03 14:52:21 +01001510 /*
1511 * Decrypt and check the padding
1512 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001513 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001514 unsigned char *dec_msg;
1515 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001516 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001517 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001518 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001519
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 /*
Paul Bakker45829992013-01-03 14:52:21 +01001521 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 */
Paul Bakker48916f92012-09-16 19:57:18 +00001523 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 {
1525 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001526 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001527 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 }
1529
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001530#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001531 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1532 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001533#endif
Paul Bakker45829992013-01-03 14:52:21 +01001534
1535 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1536 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1537 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001538 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1539 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1540 ssl->transform_in->ivlen,
1541 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001542 return( POLARSSL_ERR_SSL_INVALID_MAC );
1543 }
1544
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001545 dec_msglen = ssl->in_msglen;
1546 dec_msg = ssl->in_msg;
1547 dec_msg_result = ssl->in_msg;
1548
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001549#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001550 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001551 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001552 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001553 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001554 {
Paul Bakker48916f92012-09-16 19:57:18 +00001555 dec_msglen -= ssl->transform_in->ivlen;
1556 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001557
Paul Bakker48916f92012-09-16 19:57:18 +00001558 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001559 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001560 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001561#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001562
Paul Bakker45125bc2013-09-04 16:47:11 +02001563 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001565 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1566 return( ret );
1567 }
1568
1569 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1570 ssl->transform_in->iv_dec,
1571 ssl->transform_in->ivlen ) ) != 0 )
1572 {
1573 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001574 return( ret );
1575 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001576
Paul Bakkercca5b812013-08-31 17:40:26 +02001577 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1578 dec_msg, dec_msglen, dec_msg_result,
1579 &olen ) ) != 0 )
1580 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001581 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001582 return( ret );
1583 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001584
Paul Bakkercca5b812013-08-31 17:40:26 +02001585 dec_msglen -= olen;
1586 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001587 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001588 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001589 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001590 return( ret );
1591 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001592
Paul Bakkercca5b812013-08-31 17:40:26 +02001593 if( dec_msglen != olen )
1594 {
1595 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001596 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001597 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001598
1599#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001600 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1601 {
1602 /*
1603 * Save IV in SSL3 and TLS1
1604 */
1605 memcpy( ssl->transform_in->iv_dec,
1606 ssl->transform_in->cipher_ctx_dec.iv,
1607 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001609#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
1611 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001612
1613 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1614 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001615#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001616 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1617 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001618#endif
Paul Bakker45829992013-01-03 14:52:21 +01001619 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001620 correct = 0;
1621 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001623#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1625 {
Paul Bakker48916f92012-09-16 19:57:18 +00001626 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001627 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001628#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1630 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001631 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001632#endif
Paul Bakker45829992013-01-03 14:52:21 +01001633 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001634 }
1635 }
1636 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001637#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001638#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1639 defined(POLARSSL_SSL_PROTO_TLS1_2)
1640 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001641 {
1642 /*
Paul Bakker45829992013-01-03 14:52:21 +01001643 * TLSv1+: always check the padding up to the first failure
1644 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001646 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001647 size_t padding_idx = ssl->in_msglen - padlen - 1;
1648
Paul Bakker956c9e02013-12-19 14:42:28 +01001649 /*
1650 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001651 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001652 *
Paul Bakker61885c72014-04-25 12:59:03 +02001653 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1654 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001655 *
1656 * In both cases we reset padding_idx to a safe value (0) to
1657 * prevent out-of-buffer reads.
1658 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001659 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001660 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1661 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001662
1663 padding_idx *= correct;
1664
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001665 for( i = 1; i <= 256; i++ )
1666 {
1667 real_count &= ( i <= padlen );
1668 pad_count += real_count *
1669 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1670 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001671
1672 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001673
Paul Bakkerd66f0702013-01-31 16:57:45 +01001674#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001675 if( padlen > 0 && correct == 0)
1676 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001677#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001678 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001680 else
1681#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1682 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001683 {
1684 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001685 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001686 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001688 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001689#endif /* POLARSSL_CIPHER_MODE_CBC &&
1690 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001691 {
1692 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001693 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
1696 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1697 ssl->in_msg, ssl->in_msglen );
1698
1699 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001700 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001702#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1703 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1704 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1705 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1706 POLARSSL_MODE_GCM )
1707 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001708 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1709
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001710 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001711
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001712 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1713 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001715 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001716
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001717#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001718 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1719 {
1720 ssl_mac( &ssl->transform_in->md_ctx_dec,
1721 ssl->transform_in->mac_dec,
1722 ssl->in_msg, ssl->in_msglen,
1723 ssl->in_ctr, ssl->in_msgtype );
1724 }
1725 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001726#endif /* POLARSSL_SSL_PROTO_SSL3 */
1727#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001728 defined(POLARSSL_SSL_PROTO_TLS1_2)
1729 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1730 {
1731 /*
1732 * Process MAC and always update for padlen afterwards to make
1733 * total time independent of padlen
1734 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001735 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001736 *
1737 * Known timing attacks:
1738 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1739 *
1740 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1741 * correctly. (We round down instead of up, so -56 is the correct
1742 * value for our calculations instead of -55)
1743 */
1744 size_t j, extra_run = 0;
1745 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1746 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001747
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001748 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001749
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001750 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1751 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1752 ssl->in_msglen );
1753 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1754 ssl->in_msg + ssl->in_msglen );
1755 for( j = 0; j < extra_run; j++ )
1756 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001757
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001758 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1759 }
1760 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001761#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001762 POLARSSL_SSL_PROTO_TLS1_2 */
1763 {
1764 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001765 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001766 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001768 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1769 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1770 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001772 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001773 ssl->transform_in->maclen ) != 0 )
1774 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001775#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001776 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001777#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001778 correct = 0;
1779 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001780
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001781 /*
1782 * Finally check the correct flag
1783 */
1784 if( correct == 0 )
1785 return( POLARSSL_ERR_SSL_INVALID_MAC );
1786 }
1787#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
1789 if( ssl->in_msglen == 0 )
1790 {
1791 ssl->nb_zero++;
1792
1793 /*
1794 * Three or more empty messages may be a DoS attack
1795 * (excessive CPU consumption).
1796 */
1797 if( ssl->nb_zero > 3 )
1798 {
1799 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1800 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001801 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001802 }
1803 }
1804 else
1805 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001806
Paul Bakker23986e52011-04-24 08:57:21 +00001807 for( i = 8; i > 0; i-- )
1808 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 break;
1810
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001811 /* The loops goes to its end iff the counter is wrapping */
1812 if( i == 0 )
1813 {
1814 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1815 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1816 }
1817
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1819
1820 return( 0 );
1821}
1822
Paul Bakker2770fbd2012-07-03 13:30:23 +00001823#if defined(POLARSSL_ZLIB_SUPPORT)
1824/*
1825 * Compression/decompression functions
1826 */
1827static int ssl_compress_buf( ssl_context *ssl )
1828{
1829 int ret;
1830 unsigned char *msg_post = ssl->out_msg;
1831 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001832 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001833
1834 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1835
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001836 if( len_pre == 0 )
1837 return( 0 );
1838
Paul Bakker2770fbd2012-07-03 13:30:23 +00001839 memcpy( msg_pre, ssl->out_msg, len_pre );
1840
1841 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1842 ssl->out_msglen ) );
1843
1844 SSL_DEBUG_BUF( 4, "before compression: output payload",
1845 ssl->out_msg, ssl->out_msglen );
1846
Paul Bakker48916f92012-09-16 19:57:18 +00001847 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1848 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1849 ssl->transform_out->ctx_deflate.next_out = msg_post;
1850 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001851
Paul Bakker48916f92012-09-16 19:57:18 +00001852 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001853 if( ret != Z_OK )
1854 {
1855 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1856 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1857 }
1858
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001859 ssl->out_msglen = SSL_BUFFER_LEN -
1860 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001861
Paul Bakker2770fbd2012-07-03 13:30:23 +00001862 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1863 ssl->out_msglen ) );
1864
1865 SSL_DEBUG_BUF( 4, "after compression: output payload",
1866 ssl->out_msg, ssl->out_msglen );
1867
1868 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1869
1870 return( 0 );
1871}
1872
1873static int ssl_decompress_buf( ssl_context *ssl )
1874{
1875 int ret;
1876 unsigned char *msg_post = ssl->in_msg;
1877 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001878 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001879
1880 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1881
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001882 if( len_pre == 0 )
1883 return( 0 );
1884
Paul Bakker2770fbd2012-07-03 13:30:23 +00001885 memcpy( msg_pre, ssl->in_msg, len_pre );
1886
1887 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1888 ssl->in_msglen ) );
1889
1890 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1891 ssl->in_msg, ssl->in_msglen );
1892
Paul Bakker48916f92012-09-16 19:57:18 +00001893 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1894 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1895 ssl->transform_in->ctx_inflate.next_out = msg_post;
1896 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001897
Paul Bakker48916f92012-09-16 19:57:18 +00001898 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001899 if( ret != Z_OK )
1900 {
1901 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1902 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1903 }
1904
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001905 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1906 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001907
Paul Bakker2770fbd2012-07-03 13:30:23 +00001908 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1909 ssl->in_msglen ) );
1910
1911 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1912 ssl->in_msg, ssl->in_msglen );
1913
1914 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1915
1916 return( 0 );
1917}
1918#endif /* POLARSSL_ZLIB_SUPPORT */
1919
Paul Bakker5121ce52009-01-03 21:22:43 +00001920/*
1921 * Fill the input message buffer
1922 */
Paul Bakker23986e52011-04-24 08:57:21 +00001923int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001924{
Paul Bakker23986e52011-04-24 08:57:21 +00001925 int ret;
1926 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
1928 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1929
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001930 if( nb_want > SSL_BUFFER_LEN - 8 )
1931 {
1932 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1933 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1934 }
1935
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 while( ssl->in_left < nb_want )
1937 {
1938 len = nb_want - ssl->in_left;
1939 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1940
1941 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1942 ssl->in_left, nb_want ) );
1943 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1944
Paul Bakker831a7552011-05-18 13:32:51 +00001945 if( ret == 0 )
1946 return( POLARSSL_ERR_SSL_CONN_EOF );
1947
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 if( ret < 0 )
1949 return( ret );
1950
1951 ssl->in_left += ret;
1952 }
1953
1954 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1955
1956 return( 0 );
1957}
1958
1959/*
1960 * Flush any data not yet written
1961 */
1962int ssl_flush_output( ssl_context *ssl )
1963{
1964 int ret;
1965 unsigned char *buf;
1966
1967 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1968
1969 while( ssl->out_left > 0 )
1970 {
1971 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1972 5 + ssl->out_msglen, ssl->out_left ) );
1973
Paul Bakker5bd42292012-12-19 14:40:42 +01001974 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001976
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1978
1979 if( ret <= 0 )
1980 return( ret );
1981
1982 ssl->out_left -= ret;
1983 }
1984
1985 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1986
1987 return( 0 );
1988}
1989
1990/*
1991 * Record layer functions
1992 */
1993int ssl_write_record( ssl_context *ssl )
1994{
Paul Bakker05ef8352012-05-08 09:17:57 +00001995 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001996 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
1998 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1999
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2001 {
2002 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2003 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2004 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2005
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002006 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2007 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 }
2009
Paul Bakker2770fbd2012-07-03 13:30:23 +00002010#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002011 if( ssl->transform_out != NULL &&
2012 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002013 {
2014 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2015 {
2016 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2017 return( ret );
2018 }
2019
2020 len = ssl->out_msglen;
2021 }
2022#endif /*POLARSSL_ZLIB_SUPPORT */
2023
Paul Bakker05ef8352012-05-08 09:17:57 +00002024#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2025 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002027 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002028
Paul Bakker05ef8352012-05-08 09:17:57 +00002029 ret = ssl_hw_record_write( ssl );
2030 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2031 {
2032 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
2033 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2034 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002035
2036 if( ret == 0 )
2037 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002038 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002039#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002040 if( !done )
2041 {
2042 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2043 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2044 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2046 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002047
Paul Bakker48916f92012-09-16 19:57:18 +00002048 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002049 {
2050 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2051 {
2052 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2053 return( ret );
2054 }
2055
2056 len = ssl->out_msglen;
2057 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2058 ssl->out_hdr[4] = (unsigned char)( len );
2059 }
2060
2061 ssl->out_left = 5 + ssl->out_msglen;
2062
2063 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2064 "version = [%d:%d], msglen = %d",
2065 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2066 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2067
2068 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002069 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 }
2071
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2073 {
2074 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2075 return( ret );
2076 }
2077
2078 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2079
2080 return( 0 );
2081}
2082
2083int ssl_read_record( ssl_context *ssl )
2084{
Paul Bakker05ef8352012-05-08 09:17:57 +00002085 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
2087 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2088
Paul Bakker68884e32013-01-07 18:20:04 +01002089 SSL_DEBUG_BUF( 4, "input record from network",
2090 ssl->in_hdr, 5 + ssl->in_msglen );
2091
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 if( ssl->in_hslen != 0 &&
2093 ssl->in_hslen < ssl->in_msglen )
2094 {
2095 /*
2096 * Get next Handshake message in the current record
2097 */
2098 ssl->in_msglen -= ssl->in_hslen;
2099
Paul Bakker8934a982011-08-05 11:11:53 +00002100 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002101 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002102
2103 ssl->in_hslen = 4;
2104 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2105
2106 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2107 " %d, type = %d, hslen = %d",
2108 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2109
2110 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2111 {
2112 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002113 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
2115
2116 if( ssl->in_msglen < ssl->in_hslen )
2117 {
2118 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002119 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 }
2121
Paul Bakker4224bc02014-04-08 14:36:50 +02002122 if( ssl->state != SSL_HANDSHAKE_OVER )
2123 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002124
2125 return( 0 );
2126 }
2127
2128 ssl->in_hslen = 0;
2129
2130 /*
2131 * Read the record header and validate it
2132 */
2133 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2134 {
2135 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2136 return( ret );
2137 }
2138
2139 ssl->in_msgtype = ssl->in_hdr[0];
2140 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2141
2142 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2143 "version = [%d:%d], msglen = %d",
2144 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2145 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2146
2147 if( ssl->in_hdr[1] != ssl->major_ver )
2148 {
2149 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002150 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 }
2152
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002153 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 {
2155 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002156 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 }
2158
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002159 /* Sanity check (outer boundaries) */
2160 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2161 {
2162 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2163 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2164 }
2165
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002167 * Make sure the message length is acceptable for the current transform
2168 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 */
Paul Bakker48916f92012-09-16 19:57:18 +00002170 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002171 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002172 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 {
2174 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002175 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 }
2177 }
2178 else
2179 {
Paul Bakker48916f92012-09-16 19:57:18 +00002180 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 {
2182 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002183 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 }
2185
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002186#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002188 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 {
2190 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002191 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002193#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002194
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002195#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2196 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 /*
2198 * TLS encrypted messages can have up to 256 bytes of padding
2199 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002200 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002201 ssl->in_msglen > ssl->transform_in->minlen +
2202 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 {
2204 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002205 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002207#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 }
2209
2210 /*
2211 * Read and optionally decrypt the message contents
2212 */
2213 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2214 {
2215 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2216 return( ret );
2217 }
2218
2219 SSL_DEBUG_BUF( 4, "input record from network",
2220 ssl->in_hdr, 5 + ssl->in_msglen );
2221
Paul Bakker05ef8352012-05-08 09:17:57 +00002222#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2223 if( ssl_hw_record_read != NULL)
2224 {
2225 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2226
2227 ret = ssl_hw_record_read( ssl );
2228 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2229 {
2230 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2231 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2232 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002233
2234 if( ret == 0 )
2235 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002236 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002237#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002238 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 {
2240 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2241 {
Paul Bakker40865c82013-01-31 17:13:13 +01002242#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2243 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2244 {
2245 ssl_send_alert_message( ssl,
2246 SSL_ALERT_LEVEL_FATAL,
2247 SSL_ALERT_MSG_BAD_RECORD_MAC );
2248 }
2249#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2251 return( ret );
2252 }
2253
2254 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2255 ssl->in_msg, ssl->in_msglen );
2256
2257 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2258 {
2259 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002260 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262 }
2263
Paul Bakker2770fbd2012-07-03 13:30:23 +00002264#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002265 if( ssl->transform_in != NULL &&
2266 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002267 {
2268 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2269 {
2270 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2271 return( ret );
2272 }
2273
2274 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2275 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2276 }
2277#endif /* POLARSSL_ZLIB_SUPPORT */
2278
Paul Bakker0a925182012-04-16 06:46:41 +00002279 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2280 ssl->in_msgtype != SSL_MSG_ALERT &&
2281 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2282 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2283 {
2284 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2285
Paul Bakker48916f92012-09-16 19:57:18 +00002286 if( ( ret = ssl_send_alert_message( ssl,
2287 SSL_ALERT_LEVEL_FATAL,
2288 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002289 {
2290 return( ret );
2291 }
2292
2293 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2294 }
2295
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2297 {
2298 ssl->in_hslen = 4;
2299 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2300
2301 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2302 " %d, type = %d, hslen = %d",
2303 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2304
2305 /*
2306 * Additional checks to validate the handshake header
2307 */
2308 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2309 {
2310 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002311 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 }
2313
2314 if( ssl->in_msglen < ssl->in_hslen )
2315 {
2316 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002317 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 }
2319
Paul Bakker48916f92012-09-16 19:57:18 +00002320 if( ssl->state != SSL_HANDSHAKE_OVER )
2321 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 }
2323
2324 if( ssl->in_msgtype == SSL_MSG_ALERT )
2325 {
2326 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2327 ssl->in_msg[0], ssl->in_msg[1] ) );
2328
2329 /*
2330 * Ignore non-fatal alerts, except close_notify
2331 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002332 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002334 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2335 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002336 /**
2337 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2338 * error identifier.
2339 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002340 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 }
2342
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002343 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2344 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 {
2346 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002347 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 }
2349 }
2350
2351 ssl->in_left = 0;
2352
2353 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2354
2355 return( 0 );
2356}
2357
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002358int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2359{
2360 int ret;
2361
2362 if( ( ret = ssl_send_alert_message( ssl,
2363 SSL_ALERT_LEVEL_FATAL,
2364 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2365 {
2366 return( ret );
2367 }
2368
2369 return( 0 );
2370}
2371
Paul Bakker0a925182012-04-16 06:46:41 +00002372int ssl_send_alert_message( ssl_context *ssl,
2373 unsigned char level,
2374 unsigned char message )
2375{
2376 int ret;
2377
2378 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2379
2380 ssl->out_msgtype = SSL_MSG_ALERT;
2381 ssl->out_msglen = 2;
2382 ssl->out_msg[0] = level;
2383 ssl->out_msg[1] = message;
2384
2385 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2386 {
2387 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2388 return( ret );
2389 }
2390
2391 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2392
2393 return( 0 );
2394}
2395
Paul Bakker5121ce52009-01-03 21:22:43 +00002396/*
2397 * Handshake functions
2398 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002399#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2400 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2401 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2402 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2403 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2404 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2405 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002406int ssl_write_certificate( ssl_context *ssl )
2407{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002408 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
2410 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2411
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002412 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002413 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2414 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002415 {
2416 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2417 ssl->state++;
2418 return( 0 );
2419 }
2420
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002421 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2422 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002423}
2424
2425int ssl_parse_certificate( ssl_context *ssl )
2426{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002427 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2428
2429 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2430
2431 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002432 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2433 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002434 {
2435 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2436 ssl->state++;
2437 return( 0 );
2438 }
2439
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002440 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2441 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002442}
2443#else
2444int ssl_write_certificate( ssl_context *ssl )
2445{
2446 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2447 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002448 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2450
2451 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2452
2453 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002454 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2455 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002456 {
2457 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2458 ssl->state++;
2459 return( 0 );
2460 }
2461
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 if( ssl->endpoint == SSL_IS_CLIENT )
2463 {
2464 if( ssl->client_auth == 0 )
2465 {
2466 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2467 ssl->state++;
2468 return( 0 );
2469 }
2470
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002471#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 /*
2473 * If using SSLv3 and got no cert, send an Alert message
2474 * (otherwise an empty Certificate message will be sent).
2475 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002476 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2478 {
2479 ssl->out_msglen = 2;
2480 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002481 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2482 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
2484 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2485 goto write_msg;
2486 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002487#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 }
2489 else /* SSL_IS_SERVER */
2490 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002491 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 {
2493 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002494 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 }
2496 }
2497
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002498 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
2500 /*
2501 * 0 . 0 handshake type
2502 * 1 . 3 handshake length
2503 * 4 . 6 length of all certs
2504 * 7 . 9 length of cert. 1
2505 * 10 . n-1 peer certificate
2506 * n . n+2 length of cert. 2
2507 * n+3 . ... upper level cert, etc.
2508 */
2509 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002510 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Paul Bakker29087132010-03-21 21:03:34 +00002512 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 {
2514 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002515 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 {
2517 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2518 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002519 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 }
2521
2522 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2523 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2524 ssl->out_msg[i + 2] = (unsigned char)( n );
2525
2526 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2527 i += n; crt = crt->next;
2528 }
2529
2530 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2531 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2532 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2533
2534 ssl->out_msglen = i;
2535 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2536 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2537
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002538#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002539write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002540#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
2542 ssl->state++;
2543
2544 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2545 {
2546 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2547 return( ret );
2548 }
2549
2550 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2551
Paul Bakkered27a042013-04-18 22:46:23 +02002552 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553}
2554
2555int ssl_parse_certificate( ssl_context *ssl )
2556{
Paul Bakkered27a042013-04-18 22:46:23 +02002557 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002558 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002559 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
2561 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2562
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002563 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002564 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2565 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002566 {
2567 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2568 ssl->state++;
2569 return( 0 );
2570 }
2571
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002573 ( ssl->authmode == SSL_VERIFY_NONE ||
2574 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002576 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2578 ssl->state++;
2579 return( 0 );
2580 }
2581
2582 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2583 {
2584 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2585 return( ret );
2586 }
2587
2588 ssl->state++;
2589
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002590#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 /*
2592 * Check if the client sent an empty certificate
2593 */
2594 if( ssl->endpoint == SSL_IS_SERVER &&
2595 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2596 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002597 if( ssl->in_msglen == 2 &&
2598 ssl->in_msgtype == SSL_MSG_ALERT &&
2599 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2600 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 {
2602 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2603
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002604 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2606 return( 0 );
2607 else
Paul Bakker40e46942009-01-03 21:51:57 +00002608 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
2610 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002611#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002613#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2614 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 if( ssl->endpoint == SSL_IS_SERVER &&
2616 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2617 {
2618 if( ssl->in_hslen == 7 &&
2619 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2620 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2621 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2622 {
2623 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2624
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002625 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002627 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 else
2629 return( 0 );
2630 }
2631 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002632#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2633 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
2635 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2636 {
2637 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002638 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 }
2640
2641 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2642 {
2643 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002644 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 }
2646
2647 /*
2648 * Same message structure as in ssl_write_certificate()
2649 */
2650 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2651
2652 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2653 {
2654 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002655 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 }
2657
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002658 /* In case we tried to reuse a session but it failed */
2659 if( ssl->session_negotiate->peer_cert != NULL )
2660 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002661 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002662 polarssl_free( ssl->session_negotiate->peer_cert );
2663 }
2664
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002665 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2666 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 {
2668 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002669 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002670 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
2672
Paul Bakkerb6b09562013-09-18 14:17:41 +02002673 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
2675 i = 7;
2676
2677 while( i < ssl->in_hslen )
2678 {
2679 if( ssl->in_msg[i] != 0 )
2680 {
2681 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002682 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 }
2684
2685 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2686 | (unsigned int) ssl->in_msg[i + 2];
2687 i += 3;
2688
2689 if( n < 128 || i + n > ssl->in_hslen )
2690 {
2691 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002692 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 }
2694
Paul Bakkerddf26b42013-09-18 13:46:23 +02002695 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2696 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 if( ret != 0 )
2698 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002699 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 return( ret );
2701 }
2702
2703 i += n;
2704 }
2705
Paul Bakker48916f92012-09-16 19:57:18 +00002706 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002708 /*
2709 * On client, make sure the server cert doesn't change during renego to
2710 * avoid "triple handshake" attack: https://secure-resumption.com/
2711 */
2712 if( ssl->endpoint == SSL_IS_CLIENT &&
2713 ssl->renegotiation == SSL_RENEGOTIATION )
2714 {
2715 if( ssl->session->peer_cert == NULL )
2716 {
2717 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2718 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2719 }
2720
2721 if( ssl->session->peer_cert->raw.len !=
2722 ssl->session_negotiate->peer_cert->raw.len ||
2723 memcmp( ssl->session->peer_cert->raw.p,
2724 ssl->session_negotiate->peer_cert->raw.p,
2725 ssl->session->peer_cert->raw.len ) != 0 )
2726 {
2727 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2728 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2729 }
2730 }
2731
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 if( ssl->authmode != SSL_VERIFY_NONE )
2733 {
2734 if( ssl->ca_chain == NULL )
2735 {
2736 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002737 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 }
2739
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002740 /*
2741 * Main check: verify certificate
2742 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002743 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2744 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2745 &ssl->session_negotiate->verify_result,
2746 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747
2748 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002749 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002750 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002751 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002752
2753 /*
2754 * Secondary checks: always done, but change 'ret' only if it was 0
2755 */
2756
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002757#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002758 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002759 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002760
2761 /* If certificate uses an EC key, make sure the curve is OK */
2762 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2763 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2764 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002765 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002766 if( ret == 0 )
2767 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002768 }
2769 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002770#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002772 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2773 ciphersuite_info,
2774 ! ssl->endpoint ) != 0 )
2775 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002776 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002777 if( ret == 0 )
2778 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2779 }
2780
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2782 ret = 0;
2783 }
2784
2785 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2786
2787 return( ret );
2788}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002789#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2790 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2791 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2792 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2793 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2794 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2795 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002796
2797int ssl_write_change_cipher_spec( ssl_context *ssl )
2798{
2799 int ret;
2800
2801 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2802
2803 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2804 ssl->out_msglen = 1;
2805 ssl->out_msg[0] = 1;
2806
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 ssl->state++;
2808
2809 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2810 {
2811 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2812 return( ret );
2813 }
2814
2815 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2816
2817 return( 0 );
2818}
2819
2820int ssl_parse_change_cipher_spec( ssl_context *ssl )
2821{
2822 int ret;
2823
2824 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2825
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2827 {
2828 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2829 return( ret );
2830 }
2831
2832 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2833 {
2834 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002835 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 }
2837
2838 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2839 {
2840 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002841 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 }
2843
2844 ssl->state++;
2845
2846 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2847
2848 return( 0 );
2849}
2850
Paul Bakker41c83d32013-03-20 14:39:14 +01002851void ssl_optimize_checksum( ssl_context *ssl,
2852 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002853{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002854 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002855
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002856#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2857 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002858 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002859 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002860 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002861#endif
2862#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2863#if defined(POLARSSL_SHA512_C)
2864 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2865 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2866 else
2867#endif
2868#if defined(POLARSSL_SHA256_C)
2869 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002870 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002871 else
2872#endif
2873#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002874 {
2875 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002876 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002877 }
Paul Bakker380da532012-04-18 16:10:25 +00002878}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002879
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002880static void ssl_update_checksum_start( ssl_context *ssl,
2881 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002882{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002883#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2884 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002885 md5_update( &ssl->handshake->fin_md5 , buf, len );
2886 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002887#endif
2888#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2889#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002890 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002891#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002892#if defined(POLARSSL_SHA512_C)
2893 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002894#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002895#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002896}
2897
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002898#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2899 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002900static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2901 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002902{
Paul Bakker48916f92012-09-16 19:57:18 +00002903 md5_update( &ssl->handshake->fin_md5 , buf, len );
2904 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002905}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002906#endif
Paul Bakker380da532012-04-18 16:10:25 +00002907
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002908#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2909#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002910static void ssl_update_checksum_sha256( ssl_context *ssl,
2911 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002912{
Paul Bakker9e36f042013-06-30 14:34:05 +02002913 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002914}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002915#endif
Paul Bakker380da532012-04-18 16:10:25 +00002916
Paul Bakker9e36f042013-06-30 14:34:05 +02002917#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002918static void ssl_update_checksum_sha384( ssl_context *ssl,
2919 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002920{
Paul Bakker9e36f042013-06-30 14:34:05 +02002921 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002922}
Paul Bakker769075d2012-11-24 11:26:46 +01002923#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002924#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002925
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002926#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002927static void ssl_calc_finished_ssl(
2928 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002929{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002930 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002931 md5_context md5;
2932 sha1_context sha1;
2933
Paul Bakker5121ce52009-01-03 21:22:43 +00002934 unsigned char padbuf[48];
2935 unsigned char md5sum[16];
2936 unsigned char sha1sum[20];
2937
Paul Bakker48916f92012-09-16 19:57:18 +00002938 ssl_session *session = ssl->session_negotiate;
2939 if( !session )
2940 session = ssl->session;
2941
Paul Bakker1ef83d62012-04-11 12:09:53 +00002942 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2943
Paul Bakker48916f92012-09-16 19:57:18 +00002944 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2945 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
2947 /*
2948 * SSLv3:
2949 * hash =
2950 * MD5( master + pad2 +
2951 * MD5( handshake + sender + master + pad1 ) )
2952 * + SHA1( master + pad2 +
2953 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 */
2955
Paul Bakker90995b52013-06-24 19:20:35 +02002956#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002959#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
Paul Bakker90995b52013-06-24 19:20:35 +02002961#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002962 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002964#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Paul Bakker3c2122f2013-06-24 19:03:14 +02002966 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2967 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Paul Bakker3c2122f2013-06-24 19:03:14 +02002971 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002972 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002973 md5_update( &md5, padbuf, 48 );
2974 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Paul Bakker3c2122f2013-06-24 19:03:14 +02002976 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002977 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002978 sha1_update( &sha1, padbuf, 40 );
2979 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
Paul Bakker1ef83d62012-04-11 12:09:53 +00002981 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Paul Bakker1ef83d62012-04-11 12:09:53 +00002983 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002984 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002985 md5_update( &md5, padbuf, 48 );
2986 md5_update( &md5, md5sum, 16 );
2987 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002990 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002991 sha1_update( &sha1, padbuf , 40 );
2992 sha1_update( &sha1, sha1sum, 20 );
2993 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002994
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002996
Paul Bakker1ef83d62012-04-11 12:09:53 +00002997 memset( &md5, 0, sizeof( md5_context ) );
2998 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
3000 memset( padbuf, 0, sizeof( padbuf ) );
3001 memset( md5sum, 0, sizeof( md5sum ) );
3002 memset( sha1sum, 0, sizeof( sha1sum ) );
3003
3004 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3005}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003006#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003007
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003008#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003009static void ssl_calc_finished_tls(
3010 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003011{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003012 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003013 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003014 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003016 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
Paul Bakker48916f92012-09-16 19:57:18 +00003018 ssl_session *session = ssl->session_negotiate;
3019 if( !session )
3020 session = ssl->session;
3021
Paul Bakker1ef83d62012-04-11 12:09:53 +00003022 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Paul Bakker48916f92012-09-16 19:57:18 +00003024 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3025 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003026
Paul Bakker1ef83d62012-04-11 12:09:53 +00003027 /*
3028 * TLSv1:
3029 * hash = PRF( master, finished_label,
3030 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3031 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Paul Bakker90995b52013-06-24 19:20:35 +02003033#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003034 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3035 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003036#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037
Paul Bakker90995b52013-06-24 19:20:35 +02003038#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003039 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3040 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003041#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003042
3043 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003044 ? "client finished"
3045 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003046
3047 md5_finish( &md5, padbuf );
3048 sha1_finish( &sha1, padbuf + 16 );
3049
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003050 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003051 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003052
3053 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3054
3055 memset( &md5, 0, sizeof( md5_context ) );
3056 memset( &sha1, 0, sizeof( sha1_context ) );
3057
3058 memset( padbuf, 0, sizeof( padbuf ) );
3059
3060 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3061}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003062#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003064#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3065#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003066static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003067 ssl_context *ssl, unsigned char *buf, int from )
3068{
3069 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003070 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003071 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072 unsigned char padbuf[32];
3073
Paul Bakker48916f92012-09-16 19:57:18 +00003074 ssl_session *session = ssl->session_negotiate;
3075 if( !session )
3076 session = ssl->session;
3077
Paul Bakker380da532012-04-18 16:10:25 +00003078 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003079
Paul Bakker9e36f042013-06-30 14:34:05 +02003080 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081
3082 /*
3083 * TLSv1.2:
3084 * hash = PRF( master, finished_label,
3085 * Hash( handshake ) )[0.11]
3086 */
3087
Paul Bakker9e36f042013-06-30 14:34:05 +02003088#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003089 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003090 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003091#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003092
3093 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003094 ? "client finished"
3095 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003096
Paul Bakker9e36f042013-06-30 14:34:05 +02003097 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003099 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003100 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003101
3102 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3103
Paul Bakker9e36f042013-06-30 14:34:05 +02003104 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003105
3106 memset( padbuf, 0, sizeof( padbuf ) );
3107
3108 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3109}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003110#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003111
Paul Bakker9e36f042013-06-30 14:34:05 +02003112#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003113static void ssl_calc_finished_tls_sha384(
3114 ssl_context *ssl, unsigned char *buf, int from )
3115{
3116 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003117 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003118 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003119 unsigned char padbuf[48];
3120
Paul Bakker48916f92012-09-16 19:57:18 +00003121 ssl_session *session = ssl->session_negotiate;
3122 if( !session )
3123 session = ssl->session;
3124
Paul Bakker380da532012-04-18 16:10:25 +00003125 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003126
Paul Bakker9e36f042013-06-30 14:34:05 +02003127 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003128
3129 /*
3130 * TLSv1.2:
3131 * hash = PRF( master, finished_label,
3132 * Hash( handshake ) )[0.11]
3133 */
3134
Paul Bakker9e36f042013-06-30 14:34:05 +02003135#if !defined(POLARSSL_SHA512_ALT)
3136 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3137 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003138#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003139
3140 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003141 ? "client finished"
3142 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003143
Paul Bakker9e36f042013-06-30 14:34:05 +02003144 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003145
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003146 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003147 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003148
3149 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3150
Paul Bakker9e36f042013-06-30 14:34:05 +02003151 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003152
3153 memset( padbuf, 0, sizeof( padbuf ) );
3154
3155 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3156}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003157#endif /* POLARSSL_SHA512_C */
3158#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003159
Paul Bakker48916f92012-09-16 19:57:18 +00003160void ssl_handshake_wrapup( ssl_context *ssl )
3161{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003162 int resume = ssl->handshake->resume;
3163
Paul Bakker48916f92012-09-16 19:57:18 +00003164 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3165
3166 /*
3167 * Free our handshake params
3168 */
3169 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003170 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003171 ssl->handshake = NULL;
3172
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003173 if( ssl->renegotiation == SSL_RENEGOTIATION )
3174 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3175
Paul Bakker48916f92012-09-16 19:57:18 +00003176 /*
3177 * Switch in our now active transform context
3178 */
3179 if( ssl->transform )
3180 {
3181 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003182 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003183 }
3184 ssl->transform = ssl->transform_negotiate;
3185 ssl->transform_negotiate = NULL;
3186
Paul Bakker0a597072012-09-25 21:55:46 +00003187 if( ssl->session )
3188 {
3189 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003190 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003191 }
3192 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003193 ssl->session_negotiate = NULL;
3194
Paul Bakker0a597072012-09-25 21:55:46 +00003195 /*
3196 * Add cache entry
3197 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003198 if( ssl->f_set_cache != NULL &&
3199 ssl->session->length != 0 &&
3200 resume == 0 )
3201 {
Paul Bakker0a597072012-09-25 21:55:46 +00003202 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3203 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003204 }
Paul Bakker0a597072012-09-25 21:55:46 +00003205
Paul Bakker48916f92012-09-16 19:57:18 +00003206 ssl->state++;
3207
3208 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3209}
3210
Paul Bakker1ef83d62012-04-11 12:09:53 +00003211int ssl_write_finished( ssl_context *ssl )
3212{
3213 int ret, hash_len;
3214
3215 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3216
Paul Bakker92be97b2013-01-02 17:30:03 +01003217 /*
3218 * Set the out_msg pointer to the correct location based on IV length
3219 */
3220 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3221 {
3222 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3223 ssl->transform_negotiate->fixed_ivlen;
3224 }
3225 else
3226 ssl->out_msg = ssl->out_iv;
3227
Paul Bakker48916f92012-09-16 19:57:18 +00003228 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003229
3230 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3232
Paul Bakker48916f92012-09-16 19:57:18 +00003233 ssl->verify_data_len = hash_len;
3234 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3235
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 ssl->out_msglen = 4 + hash_len;
3237 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3238 ssl->out_msg[0] = SSL_HS_FINISHED;
3239
3240 /*
3241 * In case of session resuming, invert the client and server
3242 * ChangeCipherSpec messages order.
3243 */
Paul Bakker0a597072012-09-25 21:55:46 +00003244 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 {
3246 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003247 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 else
3249 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3250 }
3251 else
3252 ssl->state++;
3253
Paul Bakker48916f92012-09-16 19:57:18 +00003254 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003255 * Switch to our negotiated transform and session parameters for outbound
3256 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003257 */
3258 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3259 ssl->transform_out = ssl->transform_negotiate;
3260 ssl->session_out = ssl->session_negotiate;
3261 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003262
Paul Bakker07eb38b2012-12-19 14:42:06 +01003263#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3264 if( ssl_hw_record_activate != NULL)
3265 {
3266 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3267 {
3268 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3269 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3270 }
3271 }
3272#endif
3273
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3275 {
3276 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3277 return( ret );
3278 }
3279
3280 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3281
3282 return( 0 );
3283}
3284
3285int ssl_parse_finished( ssl_context *ssl )
3286{
Paul Bakker23986e52011-04-24 08:57:21 +00003287 int ret;
3288 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 unsigned char buf[36];
3290
3291 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3292
Paul Bakker48916f92012-09-16 19:57:18 +00003293 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Paul Bakker48916f92012-09-16 19:57:18 +00003295 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003296 * Switch to our negotiated transform and session parameters for inbound
3297 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003298 */
3299 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3300 ssl->transform_in = ssl->transform_negotiate;
3301 ssl->session_in = ssl->session_negotiate;
3302 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003303
Paul Bakker92be97b2013-01-02 17:30:03 +01003304 /*
3305 * Set the in_msg pointer to the correct location based on IV length
3306 */
3307 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3308 {
3309 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3310 ssl->transform_negotiate->fixed_ivlen;
3311 }
3312 else
3313 ssl->in_msg = ssl->in_iv;
3314
Paul Bakker07eb38b2012-12-19 14:42:06 +01003315#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3316 if( ssl_hw_record_activate != NULL)
3317 {
3318 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3319 {
3320 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3321 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3322 }
3323 }
3324#endif
3325
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3327 {
3328 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3329 return( ret );
3330 }
3331
3332 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3333 {
3334 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003335 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 }
3337
Paul Bakker1ef83d62012-04-11 12:09:53 +00003338 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3340
3341 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3342 ssl->in_hslen != 4 + hash_len )
3343 {
3344 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003345 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 }
3347
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003348 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 {
3350 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003351 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 }
3353
Paul Bakker48916f92012-09-16 19:57:18 +00003354 ssl->verify_data_len = hash_len;
3355 memcpy( ssl->peer_verify_data, buf, hash_len );
3356
Paul Bakker0a597072012-09-25 21:55:46 +00003357 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 {
3359 if( ssl->endpoint == SSL_IS_CLIENT )
3360 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3361
3362 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003363 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 }
3365 else
3366 ssl->state++;
3367
3368 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3369
3370 return( 0 );
3371}
3372
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003373static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003374{
3375 if( ssl->transform_negotiate )
3376 ssl_transform_free( ssl->transform_negotiate );
3377 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003378 {
3379 ssl->transform_negotiate =
3380 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003381
3382 if( ssl->transform_negotiate != NULL )
3383 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003384 }
Paul Bakker48916f92012-09-16 19:57:18 +00003385
3386 if( ssl->session_negotiate )
3387 ssl_session_free( ssl->session_negotiate );
3388 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003389 {
3390 ssl->session_negotiate =
3391 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003392
3393 if( ssl->session_negotiate != NULL )
3394 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003395 }
Paul Bakker48916f92012-09-16 19:57:18 +00003396
3397 if( ssl->handshake )
3398 ssl_handshake_free( ssl->handshake );
3399 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003400 {
3401 ssl->handshake = (ssl_handshake_params *)
3402 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003403
3404 if( ssl->handshake != NULL )
3405 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003406 }
Paul Bakker48916f92012-09-16 19:57:18 +00003407
3408 if( ssl->handshake == NULL ||
3409 ssl->transform_negotiate == NULL ||
3410 ssl->session_negotiate == NULL )
3411 {
3412 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3413 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3414 }
3415
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003416#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3417 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003418 md5_starts( &ssl->handshake->fin_md5 );
3419 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003420#endif
3421#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3422#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003423 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003424#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003425#if defined(POLARSSL_SHA512_C)
3426 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003427#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003428#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003429
3430 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003431 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003432
Paul Bakker61d113b2013-07-04 11:51:43 +02003433#if defined(POLARSSL_ECDH_C)
3434 ecdh_init( &ssl->handshake->ecdh_ctx );
3435#endif
3436
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003437#if defined(POLARSSL_X509_CRT_PARSE_C)
3438 ssl->handshake->key_cert = ssl->key_cert;
3439#endif
3440
Paul Bakker48916f92012-09-16 19:57:18 +00003441 return( 0 );
3442}
3443
Paul Bakker5121ce52009-01-03 21:22:43 +00003444/*
3445 * Initialize an SSL context
3446 */
3447int ssl_init( ssl_context *ssl )
3448{
Paul Bakker48916f92012-09-16 19:57:18 +00003449 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 int len = SSL_BUFFER_LEN;
3451
3452 memset( ssl, 0, sizeof( ssl_context ) );
3453
Paul Bakker62f2dee2012-09-28 07:31:51 +00003454 /*
3455 * Sane defaults
3456 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003457 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3458 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3459 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3460 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003461
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003462 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003463
Paul Bakker62f2dee2012-09-28 07:31:51 +00003464#if defined(POLARSSL_DHM_C)
3465 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3466 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3467 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3468 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3469 {
3470 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3471 return( ret );
3472 }
3473#endif
3474
3475 /*
3476 * Prepare base structures
3477 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003478 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003479 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003480 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 ssl->in_msg = ssl->in_ctr + 13;
3482
3483 if( ssl->in_ctr == NULL )
3484 {
3485 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003486 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 }
3488
Paul Bakker6e339b52013-07-03 13:37:05 +02003489 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003490 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003491 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003492 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003493
3494 if( ssl->out_ctr == NULL )
3495 {
3496 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003497 polarssl_free( ssl->in_ctr );
3498 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003499 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003500 }
3501
3502 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3503 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3504
Paul Bakker606b4ba2013-08-14 16:52:14 +02003505#if defined(POLARSSL_SSL_SESSION_TICKETS)
3506 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3507#endif
3508
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003509#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003510 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003511#endif
3512
Paul Bakker48916f92012-09-16 19:57:18 +00003513 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3514 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003515
3516 return( 0 );
3517}
3518
3519/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003520 * Reset an initialized and used SSL context for re-use while retaining
3521 * all application-set variables, function pointers and data.
3522 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003523int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003524{
Paul Bakker48916f92012-09-16 19:57:18 +00003525 int ret;
3526
Paul Bakker7eb013f2011-10-06 12:37:39 +00003527 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003528 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3529 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3530
3531 ssl->verify_data_len = 0;
3532 memset( ssl->own_verify_data, 0, 36 );
3533 memset( ssl->peer_verify_data, 0, 36 );
3534
Paul Bakker7eb013f2011-10-06 12:37:39 +00003535 ssl->in_offt = NULL;
3536
Paul Bakker92be97b2013-01-02 17:30:03 +01003537 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003538 ssl->in_msgtype = 0;
3539 ssl->in_msglen = 0;
3540 ssl->in_left = 0;
3541
3542 ssl->in_hslen = 0;
3543 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003544 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003545
Paul Bakker92be97b2013-01-02 17:30:03 +01003546 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003547 ssl->out_msgtype = 0;
3548 ssl->out_msglen = 0;
3549 ssl->out_left = 0;
3550
Paul Bakker48916f92012-09-16 19:57:18 +00003551 ssl->transform_in = NULL;
3552 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003553
3554 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3555 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003556
3557#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3558 if( ssl_hw_record_reset != NULL)
3559 {
3560 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003561 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003562 {
3563 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3564 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3565 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003566 }
3567#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003568
Paul Bakker48916f92012-09-16 19:57:18 +00003569 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003570 {
Paul Bakker48916f92012-09-16 19:57:18 +00003571 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003572 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003573 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003574 }
Paul Bakker48916f92012-09-16 19:57:18 +00003575
Paul Bakkerc0463502013-02-14 11:19:38 +01003576 if( ssl->session )
3577 {
3578 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003579 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003580 ssl->session = NULL;
3581 }
3582
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003583#if defined(POLARSSL_SSL_ALPN)
3584 ssl->alpn_chosen = NULL;
3585#endif
3586
Paul Bakker48916f92012-09-16 19:57:18 +00003587 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3588 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003589
3590 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003591}
3592
Paul Bakkera503a632013-08-14 13:48:06 +02003593#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003594/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003595 * Allocate and initialize ticket keys
3596 */
3597static int ssl_ticket_keys_init( ssl_context *ssl )
3598{
3599 int ret;
3600 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003601 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003602
3603 if( ssl->ticket_keys != NULL )
3604 return( 0 );
3605
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003606 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3607 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003608 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3609
3610 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003611 {
3612 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003613 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003614 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003615
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003616 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3617 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3618 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3619 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003620 polarssl_free( tkeys );
3621 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003622 }
3623
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003624 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003625 {
3626 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003627 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003628 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003629
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003630 ssl->ticket_keys = tkeys;
3631
3632 return( 0 );
3633}
Paul Bakkera503a632013-08-14 13:48:06 +02003634#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003635
3636/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 * SSL set accessors
3638 */
3639void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3640{
3641 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003642
Paul Bakker606b4ba2013-08-14 16:52:14 +02003643#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003644 if( endpoint == SSL_IS_CLIENT )
3645 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003646#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003647}
3648
3649void ssl_set_authmode( ssl_context *ssl, int authmode )
3650{
3651 ssl->authmode = authmode;
3652}
3653
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003654#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003655void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003656 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003657 void *p_vrfy )
3658{
3659 ssl->f_vrfy = f_vrfy;
3660 ssl->p_vrfy = p_vrfy;
3661}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003662#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003663
Paul Bakker5121ce52009-01-03 21:22:43 +00003664void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003665 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 void *p_rng )
3667{
3668 ssl->f_rng = f_rng;
3669 ssl->p_rng = p_rng;
3670}
3671
3672void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003673 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003674 void *p_dbg )
3675{
3676 ssl->f_dbg = f_dbg;
3677 ssl->p_dbg = p_dbg;
3678}
3679
3680void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003681 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003682 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003683{
3684 ssl->f_recv = f_recv;
3685 ssl->f_send = f_send;
3686 ssl->p_recv = p_recv;
3687 ssl->p_send = p_send;
3688}
3689
Paul Bakker0a597072012-09-25 21:55:46 +00003690void ssl_set_session_cache( ssl_context *ssl,
3691 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3692 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003693{
Paul Bakker0a597072012-09-25 21:55:46 +00003694 ssl->f_get_cache = f_get_cache;
3695 ssl->p_get_cache = p_get_cache;
3696 ssl->f_set_cache = f_set_cache;
3697 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003698}
3699
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003700int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003701{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003702 int ret;
3703
3704 if( ssl == NULL ||
3705 session == NULL ||
3706 ssl->session_negotiate == NULL ||
3707 ssl->endpoint != SSL_IS_CLIENT )
3708 {
3709 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3710 }
3711
3712 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3713 return( ret );
3714
Paul Bakker0a597072012-09-25 21:55:46 +00003715 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003716
3717 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003718}
3719
Paul Bakkerb68cad62012-08-23 08:34:18 +00003720void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003721{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003722 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3723 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3724 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3725 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3726}
3727
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003728void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3729 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003730 int major, int minor )
3731{
3732 if( major != SSL_MAJOR_VERSION_3 )
3733 return;
3734
3735 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3736 return;
3737
3738 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003739}
3740
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003741#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003742/* Add a new (empty) key_cert entry an return a pointer to it */
3743static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3744{
3745 ssl_key_cert *key_cert, *last;
3746
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003747 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3748 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003749 return( NULL );
3750
3751 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3752
3753 /* Append the new key_cert to the (possibly empty) current list */
3754 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003755 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003756 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003757 if( ssl->handshake != NULL )
3758 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003759 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003760 else
3761 {
3762 last = ssl->key_cert;
3763 while( last->next != NULL )
3764 last = last->next;
3765 last->next = key_cert;
3766 }
3767
3768 return key_cert;
3769}
3770
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003771void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003772 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003773{
3774 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003775 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 ssl->peer_cn = peer_cn;
3777}
3778
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003779int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003780 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003781{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003782 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3783
3784 if( key_cert == NULL )
3785 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3786
3787 key_cert->cert = own_cert;
3788 key_cert->key = pk_key;
3789
3790 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003791}
3792
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003793#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003794int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003795 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003796{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003797 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003798 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003799
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003800 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003801 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3802
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003803 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3804 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003805 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003806
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003807 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003808
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003809 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003810 if( ret != 0 )
3811 return( ret );
3812
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003813 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003814 return( ret );
3815
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003816 key_cert->cert = own_cert;
3817 key_cert->key_own_alloc = 1;
3818
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003819 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003820}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003821#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003822
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003823int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003824 void *rsa_key,
3825 rsa_decrypt_func rsa_decrypt,
3826 rsa_sign_func rsa_sign,
3827 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003828{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003829 int ret;
3830 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003831
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003832 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003833 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3834
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003835 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3836 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003837 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003838
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003839 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003840
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003841 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3842 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3843 return( ret );
3844
3845 key_cert->cert = own_cert;
3846 key_cert->key_own_alloc = 1;
3847
3848 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003849}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003850#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003851
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003852#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003853int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3854 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003855{
Paul Bakker6db455e2013-09-18 17:29:31 +02003856 if( psk == NULL || psk_identity == NULL )
3857 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3858
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003859 /*
3860 * The length will be check later anyway, but in case it is obviously
3861 * too large, better abort now. The PMS is as follows:
3862 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3863 */
3864 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3865 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3866
Paul Bakker6db455e2013-09-18 17:29:31 +02003867 if( ssl->psk != NULL )
3868 {
3869 polarssl_free( ssl->psk );
3870 polarssl_free( ssl->psk_identity );
3871 }
3872
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003873 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003874 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003875
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003876 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003877 ssl->psk_identity = (unsigned char *)
3878 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003879
3880 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3881 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3882
3883 memcpy( ssl->psk, psk, ssl->psk_len );
3884 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003885
3886 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003887}
3888
3889void ssl_set_psk_cb( ssl_context *ssl,
3890 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3891 size_t),
3892 void *p_psk )
3893{
3894 ssl->f_psk = f_psk;
3895 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003896}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003897#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003898
Paul Bakker48916f92012-09-16 19:57:18 +00003899#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003900int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003901{
3902 int ret;
3903
Paul Bakker48916f92012-09-16 19:57:18 +00003904 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003905 {
3906 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3907 return( ret );
3908 }
3909
Paul Bakker48916f92012-09-16 19:57:18 +00003910 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003911 {
3912 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3913 return( ret );
3914 }
3915
3916 return( 0 );
3917}
3918
Paul Bakker1b57b062011-01-06 15:48:19 +00003919int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3920{
3921 int ret;
3922
Paul Bakker48916f92012-09-16 19:57:18 +00003923 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003924 {
3925 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3926 return( ret );
3927 }
3928
Paul Bakker48916f92012-09-16 19:57:18 +00003929 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003930 {
3931 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3932 return( ret );
3933 }
3934
3935 return( 0 );
3936}
Paul Bakker48916f92012-09-16 19:57:18 +00003937#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003938
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003939#if defined(POLARSSL_SSL_SET_CURVES)
3940/*
3941 * Set the allowed elliptic curves
3942 */
3943void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3944{
3945 ssl->curve_list = curve_list;
3946}
3947#endif
3948
Paul Bakker0be444a2013-08-27 21:55:01 +02003949#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003950int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003951{
3952 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003953 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003954
3955 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003956
3957 if( ssl->hostname_len + 1 == 0 )
3958 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3959
Paul Bakker6e339b52013-07-03 13:37:05 +02003960 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003961
Paul Bakkerb15b8512012-01-13 13:44:06 +00003962 if( ssl->hostname == NULL )
3963 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3964
Paul Bakker3c2122f2013-06-24 19:03:14 +02003965 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003966 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003967
Paul Bakker40ea7de2009-05-03 10:18:48 +00003968 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003969
3970 return( 0 );
3971}
3972
Paul Bakker5701cdc2012-09-27 21:49:42 +00003973void ssl_set_sni( ssl_context *ssl,
3974 int (*f_sni)(void *, ssl_context *,
3975 const unsigned char *, size_t),
3976 void *p_sni )
3977{
3978 ssl->f_sni = f_sni;
3979 ssl->p_sni = p_sni;
3980}
Paul Bakker0be444a2013-08-27 21:55:01 +02003981#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003982
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003983#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003984int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003985{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003986 size_t cur_len, tot_len;
3987 const char **p;
3988
3989 /*
3990 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3991 * truncated". Check lengths now rather than later.
3992 */
3993 tot_len = 0;
3994 for( p = protos; *p != NULL; p++ )
3995 {
3996 cur_len = strlen( *p );
3997 tot_len += cur_len;
3998
3999 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4000 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4001 }
4002
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004003 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004004
4005 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004006}
4007
4008const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4009{
4010 return ssl->alpn_chosen;
4011}
4012#endif /* POLARSSL_SSL_ALPN */
4013
Paul Bakker490ecc82011-10-06 13:04:09 +00004014void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4015{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004016 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4017 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4018 {
4019 ssl->max_major_ver = major;
4020 ssl->max_minor_ver = minor;
4021 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004022}
4023
Paul Bakker1d29fb52012-09-28 13:28:45 +00004024void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4025{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004026 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4027 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4028 {
4029 ssl->min_major_ver = major;
4030 ssl->min_minor_ver = minor;
4031 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004032}
4033
Paul Bakker05decb22013-08-15 13:33:48 +02004034#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004035int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4036{
Paul Bakker77e257e2013-12-16 15:29:52 +01004037 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004038 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004039 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004040 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004041 }
4042
4043 ssl->mfl_code = mfl_code;
4044
4045 return( 0 );
4046}
Paul Bakker05decb22013-08-15 13:33:48 +02004047#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004048
Paul Bakker1f2bc622013-08-15 13:45:55 +02004049#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004050int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004051{
4052 if( ssl->endpoint != SSL_IS_CLIENT )
4053 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4054
Paul Bakker8c1ede62013-07-19 14:14:37 +02004055 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004056
4057 return( 0 );
4058}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004059#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004060
Paul Bakker48916f92012-09-16 19:57:18 +00004061void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4062{
4063 ssl->disable_renegotiation = renegotiation;
4064}
4065
4066void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4067{
4068 ssl->allow_legacy_renegotiation = allow_legacy;
4069}
4070
Paul Bakkera503a632013-08-14 13:48:06 +02004071#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004072int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4073{
4074 ssl->session_tickets = use_tickets;
4075
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004076 if( ssl->endpoint == SSL_IS_CLIENT )
4077 return( 0 );
4078
4079 if( ssl->f_rng == NULL )
4080 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4081
4082 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004083}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004084
4085void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4086{
4087 ssl->ticket_lifetime = lifetime;
4088}
Paul Bakkera503a632013-08-14 13:48:06 +02004089#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004090
Paul Bakker5121ce52009-01-03 21:22:43 +00004091/*
4092 * SSL get accessors
4093 */
Paul Bakker23986e52011-04-24 08:57:21 +00004094size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004095{
4096 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4097}
4098
Paul Bakkerff60ee62010-03-16 21:09:09 +00004099int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004100{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004101 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004102}
4103
Paul Bakkere3166ce2011-01-27 17:40:50 +00004104const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004105{
Paul Bakker926c8e42013-03-06 10:23:34 +01004106 if( ssl == NULL || ssl->session == NULL )
4107 return NULL;
4108
Paul Bakkere3166ce2011-01-27 17:40:50 +00004109 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004110}
4111
Paul Bakker43ca69c2011-01-15 17:35:19 +00004112const char *ssl_get_version( const ssl_context *ssl )
4113{
4114 switch( ssl->minor_ver )
4115 {
4116 case SSL_MINOR_VERSION_0:
4117 return( "SSLv3.0" );
4118
4119 case SSL_MINOR_VERSION_1:
4120 return( "TLSv1.0" );
4121
4122 case SSL_MINOR_VERSION_2:
4123 return( "TLSv1.1" );
4124
Paul Bakker1ef83d62012-04-11 12:09:53 +00004125 case SSL_MINOR_VERSION_3:
4126 return( "TLSv1.2" );
4127
Paul Bakker43ca69c2011-01-15 17:35:19 +00004128 default:
4129 break;
4130 }
4131 return( "unknown" );
4132}
4133
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004134#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004135const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004136{
4137 if( ssl == NULL || ssl->session == NULL )
4138 return NULL;
4139
4140 return ssl->session->peer_cert;
4141}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004142#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004143
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004144int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4145{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004146 if( ssl == NULL ||
4147 dst == NULL ||
4148 ssl->session == NULL ||
4149 ssl->endpoint != SSL_IS_CLIENT )
4150 {
4151 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4152 }
4153
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004154 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004155}
4156
Paul Bakker5121ce52009-01-03 21:22:43 +00004157/*
Paul Bakker1961b702013-01-25 14:49:24 +01004158 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004159 */
Paul Bakker1961b702013-01-25 14:49:24 +01004160int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004161{
Paul Bakker40e46942009-01-03 21:51:57 +00004162 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004163
Paul Bakker40e46942009-01-03 21:51:57 +00004164#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004165 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004166 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004167#endif
4168
Paul Bakker40e46942009-01-03 21:51:57 +00004169#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004170 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004171 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004172#endif
4173
Paul Bakker1961b702013-01-25 14:49:24 +01004174 return( ret );
4175}
4176
4177/*
4178 * Perform the SSL handshake
4179 */
4180int ssl_handshake( ssl_context *ssl )
4181{
4182 int ret = 0;
4183
4184 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4185
4186 while( ssl->state != SSL_HANDSHAKE_OVER )
4187 {
4188 ret = ssl_handshake_step( ssl );
4189
4190 if( ret != 0 )
4191 break;
4192 }
4193
Paul Bakker5121ce52009-01-03 21:22:43 +00004194 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4195
4196 return( ret );
4197}
4198
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004199#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004200/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004201 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004202 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004203static int ssl_write_hello_request( ssl_context *ssl )
4204{
4205 int ret;
4206
4207 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4208
4209 ssl->out_msglen = 4;
4210 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4211 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4212
4213 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4214 {
4215 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4216 return( ret );
4217 }
4218
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004219 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4220
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004221 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4222
4223 return( 0 );
4224}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004225#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004226
4227/*
4228 * Actually renegotiate current connection, triggered by either:
4229 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004230 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004231 * - receiving any handshake message on server during ssl_read() after the
4232 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004233 * If the handshake doesn't complete due to waiting for I/O, it will continue
4234 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004235 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004236static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004237{
4238 int ret;
4239
4240 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4241
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004242 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4243 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004244
4245 ssl->state = SSL_HELLO_REQUEST;
4246 ssl->renegotiation = SSL_RENEGOTIATION;
4247
Paul Bakker48916f92012-09-16 19:57:18 +00004248 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4249 {
4250 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4251 return( ret );
4252 }
4253
4254 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4255
4256 return( 0 );
4257}
4258
4259/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004260 * Renegotiate current connection on client,
4261 * or request renegotiation on server
4262 */
4263int ssl_renegotiate( ssl_context *ssl )
4264{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004265 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004266
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004267#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004268 /* On server, just send the request */
4269 if( ssl->endpoint == SSL_IS_SERVER )
4270 {
4271 if( ssl->state != SSL_HANDSHAKE_OVER )
4272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4273
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004274 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004275 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004276#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004277
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004278#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004279 /*
4280 * On client, either start the renegotiation process or,
4281 * if already in progress, continue the handshake
4282 */
4283 if( ssl->renegotiation != SSL_RENEGOTIATION )
4284 {
4285 if( ssl->state != SSL_HANDSHAKE_OVER )
4286 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4287
4288 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4289 {
4290 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4291 return( ret );
4292 }
4293 }
4294 else
4295 {
4296 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4297 {
4298 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4299 return( ret );
4300 }
4301 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004302#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004303
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004304 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004305}
4306
4307/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 * Receive application data decrypted from the SSL layer
4309 */
Paul Bakker23986e52011-04-24 08:57:21 +00004310int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311{
Paul Bakker23986e52011-04-24 08:57:21 +00004312 int ret;
4313 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004314
4315 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4316
4317 if( ssl->state != SSL_HANDSHAKE_OVER )
4318 {
4319 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4320 {
4321 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4322 return( ret );
4323 }
4324 }
4325
4326 if( ssl->in_offt == NULL )
4327 {
4328 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4329 {
Paul Bakker831a7552011-05-18 13:32:51 +00004330 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4331 return( 0 );
4332
Paul Bakker5121ce52009-01-03 21:22:43 +00004333 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4334 return( ret );
4335 }
4336
4337 if( ssl->in_msglen == 0 &&
4338 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4339 {
4340 /*
4341 * OpenSSL sends empty messages to randomize the IV
4342 */
4343 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4344 {
Paul Bakker831a7552011-05-18 13:32:51 +00004345 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4346 return( 0 );
4347
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4349 return( ret );
4350 }
4351 }
4352
Paul Bakker48916f92012-09-16 19:57:18 +00004353 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4354 {
4355 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4356
4357 if( ssl->endpoint == SSL_IS_CLIENT &&
4358 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4359 ssl->in_hslen != 4 ) )
4360 {
4361 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4362 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4363 }
4364
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004365 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4366 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004367 ssl->allow_legacy_renegotiation ==
4368 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004369 {
4370 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4371
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004372#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004373 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004374 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004375 /*
4376 * SSLv3 does not have a "no_renegotiation" alert
4377 */
4378 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4379 return( ret );
4380 }
4381 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004382#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004383#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4384 defined(POLARSSL_SSL_PROTO_TLS1_2)
4385 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004386 {
4387 if( ( ret = ssl_send_alert_message( ssl,
4388 SSL_ALERT_LEVEL_WARNING,
4389 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4390 {
4391 return( ret );
4392 }
Paul Bakker48916f92012-09-16 19:57:18 +00004393 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004394 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004395#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4396 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004397 {
4398 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004399 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004400 }
Paul Bakker48916f92012-09-16 19:57:18 +00004401 }
4402 else
4403 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004404 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004405 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004406 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004407 return( ret );
4408 }
4409
4410 return( POLARSSL_ERR_NET_WANT_READ );
4411 }
4412 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004413 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4414 {
4415 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4416 "but not honored by client" ) );
4417 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4418 }
Paul Bakker48916f92012-09-16 19:57:18 +00004419 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004420 {
4421 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004422 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004423 }
4424
4425 ssl->in_offt = ssl->in_msg;
4426 }
4427
4428 n = ( len < ssl->in_msglen )
4429 ? len : ssl->in_msglen;
4430
4431 memcpy( buf, ssl->in_offt, n );
4432 ssl->in_msglen -= n;
4433
4434 if( ssl->in_msglen == 0 )
4435 /* all bytes consumed */
4436 ssl->in_offt = NULL;
4437 else
4438 /* more data available */
4439 ssl->in_offt += n;
4440
4441 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4442
Paul Bakker23986e52011-04-24 08:57:21 +00004443 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004444}
4445
4446/*
4447 * Send application data to be encrypted by the SSL layer
4448 */
Paul Bakker23986e52011-04-24 08:57:21 +00004449int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004450{
Paul Bakker23986e52011-04-24 08:57:21 +00004451 int ret;
4452 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004453 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004454
4455 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4456
4457 if( ssl->state != SSL_HANDSHAKE_OVER )
4458 {
4459 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4460 {
4461 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4462 return( ret );
4463 }
4464 }
4465
Paul Bakker05decb22013-08-15 13:33:48 +02004466#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004467 /*
4468 * Assume mfl_code is correct since it was checked when set
4469 */
4470 max_len = mfl_code_to_length[ssl->mfl_code];
4471
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004472 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004473 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004474 */
4475 if( ssl->session_out != NULL &&
4476 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4477 {
4478 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4479 }
Paul Bakker05decb22013-08-15 13:33:48 +02004480#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004481
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004482 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004483
Paul Bakker5121ce52009-01-03 21:22:43 +00004484 if( ssl->out_left != 0 )
4485 {
4486 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4487 {
4488 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4489 return( ret );
4490 }
4491 }
Paul Bakker887bd502011-06-08 13:10:54 +00004492 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004493 {
Paul Bakker887bd502011-06-08 13:10:54 +00004494 ssl->out_msglen = n;
4495 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4496 memcpy( ssl->out_msg, buf, n );
4497
4498 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4499 {
4500 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4501 return( ret );
4502 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004503 }
4504
4505 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4506
Paul Bakker23986e52011-04-24 08:57:21 +00004507 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004508}
4509
4510/*
4511 * Notify the peer that the connection is being closed
4512 */
4513int ssl_close_notify( ssl_context *ssl )
4514{
4515 int ret;
4516
4517 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4518
4519 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4520 {
4521 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4522 return( ret );
4523 }
4524
4525 if( ssl->state == SSL_HANDSHAKE_OVER )
4526 {
Paul Bakker48916f92012-09-16 19:57:18 +00004527 if( ( ret = ssl_send_alert_message( ssl,
4528 SSL_ALERT_LEVEL_WARNING,
4529 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004530 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004531 return( ret );
4532 }
4533 }
4534
4535 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4536
4537 return( ret );
4538}
4539
Paul Bakker48916f92012-09-16 19:57:18 +00004540void ssl_transform_free( ssl_transform *transform )
4541{
4542#if defined(POLARSSL_ZLIB_SUPPORT)
4543 deflateEnd( &transform->ctx_deflate );
4544 inflateEnd( &transform->ctx_inflate );
4545#endif
4546
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004547 cipher_free_ctx( &transform->cipher_ctx_enc );
4548 cipher_free_ctx( &transform->cipher_ctx_dec );
4549
Paul Bakker61d113b2013-07-04 11:51:43 +02004550 md_free_ctx( &transform->md_ctx_enc );
4551 md_free_ctx( &transform->md_ctx_dec );
4552
Paul Bakker48916f92012-09-16 19:57:18 +00004553 memset( transform, 0, sizeof( ssl_transform ) );
4554}
4555
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004556#if defined(POLARSSL_X509_CRT_PARSE_C)
4557static void ssl_key_cert_free( ssl_key_cert *key_cert )
4558{
4559 ssl_key_cert *cur = key_cert, *next;
4560
4561 while( cur != NULL )
4562 {
4563 next = cur->next;
4564
4565 if( cur->key_own_alloc )
4566 {
4567 pk_free( cur->key );
4568 polarssl_free( cur->key );
4569 }
4570 polarssl_free( cur );
4571
4572 cur = next;
4573 }
4574}
4575#endif /* POLARSSL_X509_CRT_PARSE_C */
4576
Paul Bakker48916f92012-09-16 19:57:18 +00004577void ssl_handshake_free( ssl_handshake_params *handshake )
4578{
4579#if defined(POLARSSL_DHM_C)
4580 dhm_free( &handshake->dhm_ctx );
4581#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004582#if defined(POLARSSL_ECDH_C)
4583 ecdh_free( &handshake->ecdh_ctx );
4584#endif
4585
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004586#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004587 /* explicit void pointer cast for buggy MS compiler */
4588 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004589#endif
4590
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004591#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4592 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4593 /*
4594 * Free only the linked list wrapper, not the keys themselves
4595 * since the belong to the SNI callback
4596 */
4597 if( handshake->sni_key_cert != NULL )
4598 {
4599 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4600
4601 while( cur != NULL )
4602 {
4603 next = cur->next;
4604 polarssl_free( cur );
4605 cur = next;
4606 }
4607 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004608#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004609
Paul Bakker48916f92012-09-16 19:57:18 +00004610 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4611}
4612
4613void ssl_session_free( ssl_session *session )
4614{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004615#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004616 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004617 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004618 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004619 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004620 }
Paul Bakkered27a042013-04-18 22:46:23 +02004621#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004622
Paul Bakkera503a632013-08-14 13:48:06 +02004623#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004624 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004625#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004626
Paul Bakker0a597072012-09-25 21:55:46 +00004627 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004628}
4629
Paul Bakker5121ce52009-01-03 21:22:43 +00004630/*
4631 * Free an SSL context
4632 */
4633void ssl_free( ssl_context *ssl )
4634{
4635 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4636
Paul Bakker5121ce52009-01-03 21:22:43 +00004637 if( ssl->out_ctr != NULL )
4638 {
4639 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004640 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004641 }
4642
4643 if( ssl->in_ctr != NULL )
4644 {
4645 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004646 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004647 }
4648
Paul Bakker16770332013-10-11 09:59:44 +02004649#if defined(POLARSSL_ZLIB_SUPPORT)
4650 if( ssl->compress_buf != NULL )
4651 {
4652 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4653 polarssl_free( ssl->compress_buf );
4654 }
4655#endif
4656
Paul Bakker40e46942009-01-03 21:51:57 +00004657#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004658 mpi_free( &ssl->dhm_P );
4659 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004660#endif
4661
Paul Bakker48916f92012-09-16 19:57:18 +00004662 if( ssl->transform )
4663 {
4664 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004665 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004666 }
4667
4668 if( ssl->handshake )
4669 {
4670 ssl_handshake_free( ssl->handshake );
4671 ssl_transform_free( ssl->transform_negotiate );
4672 ssl_session_free( ssl->session_negotiate );
4673
Paul Bakker6e339b52013-07-03 13:37:05 +02004674 polarssl_free( ssl->handshake );
4675 polarssl_free( ssl->transform_negotiate );
4676 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004677 }
4678
Paul Bakkerc0463502013-02-14 11:19:38 +01004679 if( ssl->session )
4680 {
4681 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004682 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004683 }
4684
Paul Bakkera503a632013-08-14 13:48:06 +02004685#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004686 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004687#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004688
Paul Bakker0be444a2013-08-27 21:55:01 +02004689#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004690 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004691 {
4692 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004693 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004694 ssl->hostname_len = 0;
4695 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004696#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004697
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004698#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004699 if( ssl->psk != NULL )
4700 {
4701 memset( ssl->psk, 0, ssl->psk_len );
4702 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4703 polarssl_free( ssl->psk );
4704 polarssl_free( ssl->psk_identity );
4705 ssl->psk_len = 0;
4706 ssl->psk_identity_len = 0;
4707 }
4708#endif
4709
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004710#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004711 ssl_key_cert_free( ssl->key_cert );
4712#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004713
Paul Bakker05ef8352012-05-08 09:17:57 +00004714#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4715 if( ssl_hw_record_finish != NULL )
4716 {
4717 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4718 ssl_hw_record_finish( ssl );
4719 }
4720#endif
4721
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004723
Paul Bakker86f04f42013-02-14 11:20:09 +01004724 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004725 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004726}
4727
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004728#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004729/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004730 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004731 */
4732unsigned char ssl_sig_from_pk( pk_context *pk )
4733{
4734#if defined(POLARSSL_RSA_C)
4735 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4736 return( SSL_SIG_RSA );
4737#endif
4738#if defined(POLARSSL_ECDSA_C)
4739 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4740 return( SSL_SIG_ECDSA );
4741#endif
4742 return( SSL_SIG_ANON );
4743}
4744
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004745pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4746{
4747 switch( sig )
4748 {
4749#if defined(POLARSSL_RSA_C)
4750 case SSL_SIG_RSA:
4751 return( POLARSSL_PK_RSA );
4752#endif
4753#if defined(POLARSSL_ECDSA_C)
4754 case SSL_SIG_ECDSA:
4755 return( POLARSSL_PK_ECDSA );
4756#endif
4757 default:
4758 return( POLARSSL_PK_NONE );
4759 }
4760}
Paul Bakker9af723c2014-05-01 13:03:14 +02004761#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004762
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004763/*
4764 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4765 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004766md_type_t ssl_md_alg_from_hash( unsigned char hash )
4767{
4768 switch( hash )
4769 {
4770#if defined(POLARSSL_MD5_C)
4771 case SSL_HASH_MD5:
4772 return( POLARSSL_MD_MD5 );
4773#endif
4774#if defined(POLARSSL_SHA1_C)
4775 case SSL_HASH_SHA1:
4776 return( POLARSSL_MD_SHA1 );
4777#endif
4778#if defined(POLARSSL_SHA256_C)
4779 case SSL_HASH_SHA224:
4780 return( POLARSSL_MD_SHA224 );
4781 case SSL_HASH_SHA256:
4782 return( POLARSSL_MD_SHA256 );
4783#endif
4784#if defined(POLARSSL_SHA512_C)
4785 case SSL_HASH_SHA384:
4786 return( POLARSSL_MD_SHA384 );
4787 case SSL_HASH_SHA512:
4788 return( POLARSSL_MD_SHA512 );
4789#endif
4790 default:
4791 return( POLARSSL_MD_NONE );
4792 }
4793}
4794
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004795#if defined(POLARSSL_SSL_SET_CURVES)
4796/*
4797 * Check is a curve proposed by the peer is in our list.
4798 * Return 1 if we're willing to use it, 0 otherwise.
4799 */
4800int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4801{
4802 const ecp_group_id *gid;
4803
4804 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4805 if( *gid == grp_id )
4806 return( 1 );
4807
4808 return( 0 );
4809}
Paul Bakker9af723c2014-05-01 13:03:14 +02004810#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004811
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004812#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004813int ssl_check_cert_usage( const x509_crt *cert,
4814 const ssl_ciphersuite_t *ciphersuite,
4815 int cert_endpoint )
4816{
4817#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4818 int usage = 0;
4819#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004820#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4821 const char *ext_oid;
4822 size_t ext_len;
4823#endif
4824
4825#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4826 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4827 ((void) cert);
4828 ((void) cert_endpoint);
4829#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004830
4831#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4832 if( cert_endpoint == SSL_IS_SERVER )
4833 {
4834 /* Server part of the key exchange */
4835 switch( ciphersuite->key_exchange )
4836 {
4837 case POLARSSL_KEY_EXCHANGE_RSA:
4838 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4839 usage = KU_KEY_ENCIPHERMENT;
4840 break;
4841
4842 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4843 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4844 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4845 usage = KU_DIGITAL_SIGNATURE;
4846 break;
4847
4848 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4849 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4850 usage = KU_KEY_AGREEMENT;
4851 break;
4852
4853 /* Don't use default: we want warnings when adding new values */
4854 case POLARSSL_KEY_EXCHANGE_NONE:
4855 case POLARSSL_KEY_EXCHANGE_PSK:
4856 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4857 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4858 usage = 0;
4859 }
4860 }
4861 else
4862 {
4863 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4864 usage = KU_DIGITAL_SIGNATURE;
4865 }
4866
4867 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4868 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004869#else
4870 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004871#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4872
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004873#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4874 if( cert_endpoint == SSL_IS_SERVER )
4875 {
4876 ext_oid = OID_SERVER_AUTH;
4877 ext_len = OID_SIZE( OID_SERVER_AUTH );
4878 }
4879 else
4880 {
4881 ext_oid = OID_CLIENT_AUTH;
4882 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4883 }
4884
4885 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4886 return( -1 );
4887#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4888
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004889 return( 0 );
4890}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004891#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004892
4893#endif /* POLARSSL_SSL_TLS_C */