blob: 2cd0cce1d133120f72a77baca31cd24b791e1735 [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,
Peter Vaskovicc2bbac92014-05-24 13:30:50 +0200100 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
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200509 if( cipher_info->mode == POLARSSL_MODE_GCM ||
510 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 {
Paul Bakker68884e32013-01-07 18:20:04 +0100512 transform->keylen = cipher_info->key_length;
513 transform->keylen /= 8;
514 transform->minlen = 1;
515 transform->ivlen = 12;
516 transform->fixed_ivlen = 4;
517 transform->maclen = 0;
518 }
519 else
520 {
521 if( md_info->type != POLARSSL_MD_NONE )
522 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200523 int ret;
524
Paul Bakker61d113b2013-07-04 11:51:43 +0200525 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
526 {
527 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
528 return( ret );
529 }
530
531 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
532 {
533 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
534 return( ret );
535 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
Paul Bakker68884e32013-01-07 18:20:04 +0100537 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200538
Paul Bakker1f2bc622013-08-15 13:45:55 +0200539#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200540 /*
541 * If HMAC is to be truncated, we shall keep the leftmost bytes,
542 * (rfc 6066 page 13 or rfc 2104 section 4),
543 * so we only need to adjust the length here.
544 */
545 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
546 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200547#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100548 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
Paul Bakker68884e32013-01-07 18:20:04 +0100550 transform->keylen = cipher_info->key_length;
551 transform->keylen /= 8;
552 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Paul Bakker68884e32013-01-07 18:20:04 +0100554 transform->minlen = transform->keylen;
555 if( transform->minlen < transform->maclen )
556 {
557 if( cipher_info->mode == POLARSSL_MODE_STREAM )
558 transform->minlen = transform->maclen;
559 else
560 transform->minlen += transform->keylen;
561 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 }
563
564 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000565 transform->keylen, transform->minlen, transform->ivlen,
566 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568 /*
569 * Finally setup the cipher contexts, IVs and MAC secrets.
570 */
571 if( ssl->endpoint == SSL_IS_CLIENT )
572 {
Paul Bakker48916f92012-09-16 19:57:18 +0000573 key1 = keyblk + transform->maclen * 2;
574 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Paul Bakker68884e32013-01-07 18:20:04 +0100576 mac_enc = keyblk;
577 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000579 /*
580 * This is not used in TLS v1.1.
581 */
Paul Bakker48916f92012-09-16 19:57:18 +0000582 iv_copy_len = ( transform->fixed_ivlen ) ?
583 transform->fixed_ivlen : transform->ivlen;
584 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
585 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000586 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 }
588 else
589 {
Paul Bakker48916f92012-09-16 19:57:18 +0000590 key1 = keyblk + transform->maclen * 2 + transform->keylen;
591 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
Paul Bakker68884e32013-01-07 18:20:04 +0100593 mac_enc = keyblk + transform->maclen;
594 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000596 /*
597 * This is not used in TLS v1.1.
598 */
Paul Bakker48916f92012-09-16 19:57:18 +0000599 iv_copy_len = ( transform->fixed_ivlen ) ?
600 transform->fixed_ivlen : transform->ivlen;
601 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
602 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000603 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 }
605
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200606#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100607 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
608 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100609 if( transform->maclen > sizeof transform->mac_enc )
610 {
611 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200612 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100613 }
614
Paul Bakker68884e32013-01-07 18:20:04 +0100615 memcpy( transform->mac_enc, mac_enc, transform->maclen );
616 memcpy( transform->mac_dec, mac_dec, transform->maclen );
617 }
618 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200619#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200620#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
621 defined(POLARSSL_SSL_PROTO_TLS1_2)
622 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100623 {
624 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
625 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
626 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200627 else
628#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200629 {
630 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200631 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200632 }
Paul Bakker68884e32013-01-07 18:20:04 +0100633
Paul Bakker05ef8352012-05-08 09:17:57 +0000634#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
635 if( ssl_hw_record_init != NULL)
636 {
637 int ret = 0;
638
639 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
640
Paul Bakker07eb38b2012-12-19 14:42:06 +0100641 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
642 transform->iv_enc, transform->iv_dec,
643 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100644 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100645 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000646 {
647 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
648 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
649 }
650 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200651#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000652
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200653 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
654 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200656 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
657 return( ret );
658 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200659
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200660 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
661 cipher_info ) ) != 0 )
662 {
663 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
664 return( ret );
665 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200666
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200667 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
668 cipher_info->key_length,
669 POLARSSL_ENCRYPT ) ) != 0 )
670 {
671 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
672 return( ret );
673 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200674
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200675 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
676 cipher_info->key_length,
677 POLARSSL_DECRYPT ) ) != 0 )
678 {
679 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
680 return( ret );
681 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200682
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200683#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684 if( cipher_info->mode == POLARSSL_MODE_CBC )
685 {
686 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
687 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200688 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200689 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
690 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200691 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200692
693 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
694 POLARSSL_PADDING_NONE ) ) != 0 )
695 {
696 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
697 return( ret );
698 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200700#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000701
702 memset( keyblk, 0, sizeof( keyblk ) );
703
Paul Bakker2770fbd2012-07-03 13:30:23 +0000704#if defined(POLARSSL_ZLIB_SUPPORT)
705 // Initialize compression
706 //
Paul Bakker48916f92012-09-16 19:57:18 +0000707 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000708 {
Paul Bakker16770332013-10-11 09:59:44 +0200709 if( ssl->compress_buf == NULL )
710 {
711 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
712 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
713 if( ssl->compress_buf == NULL )
714 {
715 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
716 SSL_BUFFER_LEN ) );
717 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
718 }
719 }
720
Paul Bakker2770fbd2012-07-03 13:30:23 +0000721 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
722
Paul Bakker48916f92012-09-16 19:57:18 +0000723 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
724 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000725
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200726 if( deflateInit( &transform->ctx_deflate,
727 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000728 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000729 {
730 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
731 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
732 }
733 }
734#endif /* POLARSSL_ZLIB_SUPPORT */
735
Paul Bakker5121ce52009-01-03 21:22:43 +0000736 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
737
738 return( 0 );
739}
740
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200741#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000742void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000743{
744 md5_context md5;
745 sha1_context sha1;
746 unsigned char pad_1[48];
747 unsigned char pad_2[48];
748
Paul Bakker380da532012-04-18 16:10:25 +0000749 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Paul Bakker48916f92012-09-16 19:57:18 +0000751 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
752 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
Paul Bakker380da532012-04-18 16:10:25 +0000754 memset( pad_1, 0x36, 48 );
755 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Paul Bakker48916f92012-09-16 19:57:18 +0000757 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000758 md5_update( &md5, pad_1, 48 );
759 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Paul Bakker380da532012-04-18 16:10:25 +0000761 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000762 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000763 md5_update( &md5, pad_2, 48 );
764 md5_update( &md5, hash, 16 );
765 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
Paul Bakker48916f92012-09-16 19:57:18 +0000767 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000768 sha1_update( &sha1, pad_1, 40 );
769 sha1_finish( &sha1, hash + 16 );
770
771 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000772 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000773 sha1_update( &sha1, pad_2, 40 );
774 sha1_update( &sha1, hash + 16, 20 );
775 sha1_finish( &sha1, hash + 16 );
776
777 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
778 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
779
780 return;
781}
Paul Bakker9af723c2014-05-01 13:03:14 +0200782#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000783
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200784#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000785void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
786{
787 md5_context md5;
788 sha1_context sha1;
789
790 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
791
Paul Bakker48916f92012-09-16 19:57:18 +0000792 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
793 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000794
Paul Bakker48916f92012-09-16 19:57:18 +0000795 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000796 sha1_finish( &sha1, hash + 16 );
797
798 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
799 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
800
801 return;
802}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200803#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000804
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200805#if defined(POLARSSL_SSL_PROTO_TLS1_2)
806#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000807void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
808{
Paul Bakker9e36f042013-06-30 14:34:05 +0200809 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000810
811 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
812
Paul Bakker9e36f042013-06-30 14:34:05 +0200813 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
814 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000815
816 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
817 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
818
819 return;
820}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200821#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000822
Paul Bakker9e36f042013-06-30 14:34:05 +0200823#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000824void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
825{
Paul Bakker9e36f042013-06-30 14:34:05 +0200826 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000827
828 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
829
Paul Bakker9e36f042013-06-30 14:34:05 +0200830 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
831 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
Paul Bakkerca4ab492012-04-18 14:23:57 +0000833 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
835
836 return;
837}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200838#endif /* POLARSSL_SHA512_C */
839#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200841#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200842int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
843{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200844 unsigned char *p = ssl->handshake->premaster;
845 unsigned char *end = p + sizeof( ssl->handshake->premaster );
846
847 /*
848 * PMS = struct {
849 * opaque other_secret<0..2^16-1>;
850 * opaque psk<0..2^16-1>;
851 * };
852 * with "other_secret" depending on the particular key exchange
853 */
854#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
855 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
856 {
857 if( end - p < 2 + (int) ssl->psk_len )
858 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
859
860 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
861 *(p++) = (unsigned char)( ssl->psk_len );
862 p += ssl->psk_len;
863 }
864 else
865#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200866#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
867 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
868 {
869 /*
870 * other_secret already set by the ClientKeyExchange message,
871 * and is 48 bytes long
872 */
873 *p++ = 0;
874 *p++ = 48;
875 p += 48;
876 }
877 else
878#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200879#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
880 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
881 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200882 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200883 size_t len = ssl->handshake->dhm_ctx.len;
884
885 if( end - p < 2 + (int) len )
886 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
887
888 *(p++) = (unsigned char)( len >> 8 );
889 *(p++) = (unsigned char)( len );
890 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
891 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
892 {
893 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
894 return( ret );
895 }
896 p += len;
897
898 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
899 }
900 else
901#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
902#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
903 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
904 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200905 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200906 size_t zlen;
907
908 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
909 p + 2, end - (p + 2),
910 ssl->f_rng, ssl->p_rng ) ) != 0 )
911 {
912 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
913 return( ret );
914 }
915
916 *(p++) = (unsigned char)( zlen >> 8 );
917 *(p++) = (unsigned char)( zlen );
918 p += zlen;
919
920 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
921 }
922 else
923#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
924 {
925 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200926 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200927 }
928
929 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100930 if( end - p < 2 + (int) ssl->psk_len )
931 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
932
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200933 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
934 *(p++) = (unsigned char)( ssl->psk_len );
935 memcpy( p, ssl->psk, ssl->psk_len );
936 p += ssl->psk_len;
937
938 ssl->handshake->pmslen = p - ssl->handshake->premaster;
939
940 return( 0 );
941}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200942#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200943
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200944#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000945/*
946 * SSLv3.0 MAC functions
947 */
Paul Bakker68884e32013-01-07 18:20:04 +0100948static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
949 unsigned char *buf, size_t len,
950 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000951{
952 unsigned char header[11];
953 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100954 int padlen = 0;
955 int md_size = md_get_size( md_ctx->md_info );
956 int md_type = md_get_type( md_ctx->md_info );
957
958 if( md_type == POLARSSL_MD_MD5 )
959 padlen = 48;
960 else if( md_type == POLARSSL_MD_SHA1 )
961 padlen = 40;
962 else if( md_type == POLARSSL_MD_SHA256 )
963 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100964 else if( md_type == POLARSSL_MD_SHA384 )
965 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
967 memcpy( header, ctr, 8 );
968 header[ 8] = (unsigned char) type;
969 header[ 9] = (unsigned char)( len >> 8 );
970 header[10] = (unsigned char)( len );
971
Paul Bakker68884e32013-01-07 18:20:04 +0100972 memset( padding, 0x36, padlen );
973 md_starts( md_ctx );
974 md_update( md_ctx, secret, md_size );
975 md_update( md_ctx, padding, padlen );
976 md_update( md_ctx, header, 11 );
977 md_update( md_ctx, buf, len );
978 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000979
Paul Bakker68884e32013-01-07 18:20:04 +0100980 memset( padding, 0x5C, padlen );
981 md_starts( md_ctx );
982 md_update( md_ctx, secret, md_size );
983 md_update( md_ctx, padding, padlen );
984 md_update( md_ctx, buf + len, md_size );
985 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000986}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200987#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000988
Paul Bakker5121ce52009-01-03 21:22:43 +0000989/*
990 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200991 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000992static int ssl_encrypt_buf( ssl_context *ssl )
993{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200994 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +0200995 const cipher_mode_t mode = cipher_get_cipher_mode(
996 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
998 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
999
1000 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001001 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001003#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1004 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1005 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001006 if( mode != POLARSSL_MODE_GCM &&
1007 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001008 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001009#if defined(POLARSSL_SSL_PROTO_SSL3)
1010 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1011 {
1012 ssl_mac( &ssl->transform_out->md_ctx_enc,
1013 ssl->transform_out->mac_enc,
1014 ssl->out_msg, ssl->out_msglen,
1015 ssl->out_ctr, ssl->out_msgtype );
1016 }
1017 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001018#endif
1019#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001020 defined(POLARSSL_SSL_PROTO_TLS1_2)
1021 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1022 {
1023 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1024 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1025 ssl->out_msg, ssl->out_msglen );
1026 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1027 ssl->out_msg + ssl->out_msglen );
1028 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1029 }
1030 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001031#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001032 {
1033 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001034 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001035 }
1036
1037 SSL_DEBUG_BUF( 4, "computed mac",
1038 ssl->out_msg + ssl->out_msglen,
1039 ssl->transform_out->maclen );
1040
1041 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001042 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001043#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001045 /*
1046 * Encrypt
1047 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001048#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001049 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001051 int ret;
1052 size_t olen = 0;
1053
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1055 "including %d bytes of padding",
1056 ssl->out_msglen, 0 ) );
1057
1058 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1059 ssl->out_msg, ssl->out_msglen );
1060
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001061 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001062 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001063 ssl->transform_out->ivlen,
1064 ssl->out_msg, ssl->out_msglen,
1065 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001066 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001067 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001068 return( ret );
1069 }
1070
1071 if( ssl->out_msglen != olen )
1072 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001073 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001074 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001075 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 }
Paul Bakker68884e32013-01-07 18:20:04 +01001077 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001078#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001079#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1080 if( mode == POLARSSL_MODE_GCM ||
1081 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001082 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001083 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001084 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001085 unsigned char *enc_msg;
1086 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001087 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1088 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001089
Paul Bakkerca4ab492012-04-18 14:23:57 +00001090 memcpy( add_data, ssl->out_ctr, 8 );
1091 add_data[8] = ssl->out_msgtype;
1092 add_data[9] = ssl->major_ver;
1093 add_data[10] = ssl->minor_ver;
1094 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1095 add_data[12] = ssl->out_msglen & 0xFF;
1096
1097 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1098 add_data, 13 );
1099
Paul Bakker68884e32013-01-07 18:20:04 +01001100 /*
1101 * Generate IV
1102 */
1103 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001104 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1105 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001106 if( ret != 0 )
1107 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001108
Paul Bakker68884e32013-01-07 18:20:04 +01001109 memcpy( ssl->out_iv,
1110 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1111 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001112
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001113 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001114 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001115
Paul Bakker68884e32013-01-07 18:20:04 +01001116 /*
1117 * Fix pointer positions and message length with added IV
1118 */
1119 enc_msg = ssl->out_msg;
1120 enc_msglen = ssl->out_msglen;
1121 ssl->out_msglen += ssl->transform_out->ivlen -
1122 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001123
Paul Bakker68884e32013-01-07 18:20:04 +01001124 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1125 "including %d bytes of padding",
1126 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001127
Paul Bakker68884e32013-01-07 18:20:04 +01001128 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1129 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001130
Paul Bakker68884e32013-01-07 18:20:04 +01001131 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001132 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001133 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001134 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1135 ssl->transform_out->iv_enc,
1136 ssl->transform_out->ivlen,
1137 add_data, 13,
1138 enc_msg, enc_msglen,
1139 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001140 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001141 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001142 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001143 return( ret );
1144 }
1145
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001146 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001147 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001148 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001149 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001150 }
1151
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001152 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001153
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001154 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001155 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001157#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001158#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1159 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001160 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001162 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001163 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001164 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001165
Paul Bakker48916f92012-09-16 19:57:18 +00001166 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1167 ssl->transform_out->ivlen;
1168 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 padlen = 0;
1170
1171 for( i = 0; i <= padlen; i++ )
1172 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1173
1174 ssl->out_msglen += padlen + 1;
1175
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001176 enc_msglen = ssl->out_msglen;
1177 enc_msg = ssl->out_msg;
1178
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001179#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001180 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001181 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1182 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001183 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001184 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001185 {
1186 /*
1187 * Generate IV
1188 */
Paul Bakker48916f92012-09-16 19:57:18 +00001189 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1190 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001191 if( ret != 0 )
1192 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001193
Paul Bakker92be97b2013-01-02 17:30:03 +01001194 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001195 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001196
1197 /*
1198 * Fix pointer positions and message length with added IV
1199 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001200 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001201 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001202 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001203 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001204#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001205
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001207 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001208 ssl->out_msglen, ssl->transform_out->ivlen,
1209 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210
1211 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001212 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001214 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001215 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001216 ssl->transform_out->ivlen,
1217 enc_msg, enc_msglen,
1218 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001219 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001220 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001221 return( ret );
1222 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001223
Paul Bakkercca5b812013-08-31 17:40:26 +02001224 if( enc_msglen != olen )
1225 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001226 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001227 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001228 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001229
1230#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001231 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1232 {
1233 /*
1234 * Save IV in SSL3 and TLS1
1235 */
1236 memcpy( ssl->transform_out->iv_enc,
1237 ssl->transform_out->cipher_ctx_enc.iv,
1238 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001240#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001242 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001243#endif /* POLARSSL_CIPHER_MODE_CBC &&
1244 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001245 {
1246 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001247 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001248 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Paul Bakkerca4ab492012-04-18 14:23:57 +00001250 for( i = 8; i > 0; i-- )
1251 if( ++ssl->out_ctr[i - 1] != 0 )
1252 break;
1253
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001254 /* The loops goes to its end iff the counter is wrapping */
1255 if( i == 0 )
1256 {
1257 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1258 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1259 }
1260
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1262
1263 return( 0 );
1264}
1265
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001266#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001267
Paul Bakker5121ce52009-01-03 21:22:43 +00001268static int ssl_decrypt_buf( ssl_context *ssl )
1269{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001270 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001271 const cipher_mode_t mode = cipher_get_cipher_mode(
1272 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001273#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1274 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1275 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1276 size_t padlen = 0, correct = 1;
1277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
1279 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1280
Paul Bakker48916f92012-09-16 19:57:18 +00001281 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 {
1283 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001284 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001285 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 }
1287
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001288#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001289 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001290 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001291 int ret;
1292 size_t olen = 0;
1293
Paul Bakker68884e32013-01-07 18:20:04 +01001294 padlen = 0;
1295
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001296 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001297 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001298 ssl->transform_in->ivlen,
1299 ssl->in_msg, ssl->in_msglen,
1300 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001301 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001302 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001303 return( ret );
1304 }
1305
1306 if( ssl->in_msglen != olen )
1307 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001308 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001309 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 }
Paul Bakker68884e32013-01-07 18:20:04 +01001312 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001313#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001314#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1315 if( mode == POLARSSL_MODE_GCM ||
1316 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001317 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001318 int ret;
1319 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001320 unsigned char *dec_msg;
1321 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001322 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001323 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1324 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001325
Paul Bakker68884e32013-01-07 18:20:04 +01001326 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1327 ssl->transform_in->fixed_ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001328 dec_msglen -= taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001329 dec_msg = ssl->in_msg;
1330 dec_msg_result = ssl->in_msg;
1331 ssl->in_msglen = dec_msglen;
1332
1333 memcpy( add_data, ssl->in_ctr, 8 );
1334 add_data[8] = ssl->in_msgtype;
1335 add_data[9] = ssl->major_ver;
1336 add_data[10] = ssl->minor_ver;
1337 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1338 add_data[12] = ssl->in_msglen & 0xFF;
1339
1340 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1341 add_data, 13 );
1342
1343 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1344 ssl->in_iv,
1345 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1346
1347 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1348 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001349 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001350
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001351 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001352 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001353 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001354 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1355 ssl->transform_in->iv_dec,
1356 ssl->transform_in->ivlen,
1357 add_data, 13,
1358 dec_msg, dec_msglen,
1359 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001360 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001361 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001362 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1363
1364 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1365 return( POLARSSL_ERR_SSL_INVALID_MAC );
1366
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001367 return( ret );
1368 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001369
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001370 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001371 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001372 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001373 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001374 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001377#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001378#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1379 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001380 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 {
Paul Bakker45829992013-01-03 14:52:21 +01001382 /*
1383 * Decrypt and check the padding
1384 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001385 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001386 unsigned char *dec_msg;
1387 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001388 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001389 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001390 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001391
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 /*
Paul Bakker45829992013-01-03 14:52:21 +01001393 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 */
Paul Bakker48916f92012-09-16 19:57:18 +00001395 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 {
1397 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001398 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001399 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 }
1401
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001402#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001403 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1404 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001405#endif
Paul Bakker45829992013-01-03 14:52:21 +01001406
1407 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1408 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1409 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001410 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1411 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1412 ssl->transform_in->ivlen,
1413 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001414 return( POLARSSL_ERR_SSL_INVALID_MAC );
1415 }
1416
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001417 dec_msglen = ssl->in_msglen;
1418 dec_msg = ssl->in_msg;
1419 dec_msg_result = ssl->in_msg;
1420
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001421#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001422 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001423 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001424 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001425 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001426 {
Paul Bakker48916f92012-09-16 19:57:18 +00001427 dec_msglen -= ssl->transform_in->ivlen;
1428 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001429
Paul Bakker48916f92012-09-16 19:57:18 +00001430 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001431 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001433#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001434
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001435 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001436 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001437 ssl->transform_in->ivlen,
1438 dec_msg, dec_msglen,
1439 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001440 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001441 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001442 return( ret );
1443 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001444
Paul Bakkercca5b812013-08-31 17:40:26 +02001445 if( dec_msglen != olen )
1446 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001447 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001448 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001449 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001450
1451#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001452 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1453 {
1454 /*
1455 * Save IV in SSL3 and TLS1
1456 */
1457 memcpy( ssl->transform_in->iv_dec,
1458 ssl->transform_in->cipher_ctx_dec.iv,
1459 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001461#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001462
1463 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001464
1465 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1466 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001467#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001468 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1469 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001470#endif
Paul Bakker45829992013-01-03 14:52:21 +01001471 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001472 correct = 0;
1473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001474
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001475#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1477 {
Paul Bakker48916f92012-09-16 19:57:18 +00001478 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001479 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001480#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1482 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001483 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001484#endif
Paul Bakker45829992013-01-03 14:52:21 +01001485 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 }
1487 }
1488 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001489#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001490#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1491 defined(POLARSSL_SSL_PROTO_TLS1_2)
1492 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001493 {
1494 /*
Paul Bakker45829992013-01-03 14:52:21 +01001495 * TLSv1+: always check the padding up to the first failure
1496 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001498 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001499 size_t padding_idx = ssl->in_msglen - padlen - 1;
1500
Paul Bakker956c9e02013-12-19 14:42:28 +01001501 /*
1502 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001503 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001504 *
Paul Bakker61885c72014-04-25 12:59:03 +02001505 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1506 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001507 *
1508 * In both cases we reset padding_idx to a safe value (0) to
1509 * prevent out-of-buffer reads.
1510 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001511 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001512 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1513 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001514
1515 padding_idx *= correct;
1516
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001517 for( i = 1; i <= 256; i++ )
1518 {
1519 real_count &= ( i <= padlen );
1520 pad_count += real_count *
1521 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1522 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001523
1524 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001525
Paul Bakkerd66f0702013-01-31 16:57:45 +01001526#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001527 if( padlen > 0 && correct == 0)
1528 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001529#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001530 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001532 else
1533#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1534 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001535 {
1536 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001537 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001540 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001541#endif /* POLARSSL_CIPHER_MODE_CBC &&
1542 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001543 {
1544 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001545 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001546 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001547
1548 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1549 ssl->in_msg, ssl->in_msglen );
1550
1551 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001552 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001553 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001554#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1555 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1556 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001557 if( mode != POLARSSL_MODE_GCM &&
1558 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001559 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001560 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1561
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001562 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001563
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001564 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1565 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001566
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001567 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001569#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001570 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1571 {
1572 ssl_mac( &ssl->transform_in->md_ctx_dec,
1573 ssl->transform_in->mac_dec,
1574 ssl->in_msg, ssl->in_msglen,
1575 ssl->in_ctr, ssl->in_msgtype );
1576 }
1577 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001578#endif /* POLARSSL_SSL_PROTO_SSL3 */
1579#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001580 defined(POLARSSL_SSL_PROTO_TLS1_2)
1581 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1582 {
1583 /*
1584 * Process MAC and always update for padlen afterwards to make
1585 * total time independent of padlen
1586 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001587 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001588 *
1589 * Known timing attacks:
1590 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1591 *
1592 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1593 * correctly. (We round down instead of up, so -56 is the correct
1594 * value for our calculations instead of -55)
1595 */
1596 size_t j, extra_run = 0;
1597 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1598 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001599
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001600 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001601
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001602 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1603 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1604 ssl->in_msglen );
1605 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1606 ssl->in_msg + ssl->in_msglen );
1607 for( j = 0; j < extra_run; j++ )
1608 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001609
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001610 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1611 }
1612 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001613#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001614 POLARSSL_SSL_PROTO_TLS1_2 */
1615 {
1616 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001617 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001618 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001619
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001620 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1621 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1622 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001624 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001625 ssl->transform_in->maclen ) != 0 )
1626 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001627#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001628 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001629#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001630 correct = 0;
1631 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001632
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001633 /*
1634 * Finally check the correct flag
1635 */
1636 if( correct == 0 )
1637 return( POLARSSL_ERR_SSL_INVALID_MAC );
1638 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001639#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
1641 if( ssl->in_msglen == 0 )
1642 {
1643 ssl->nb_zero++;
1644
1645 /*
1646 * Three or more empty messages may be a DoS attack
1647 * (excessive CPU consumption).
1648 */
1649 if( ssl->nb_zero > 3 )
1650 {
1651 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1652 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001653 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 }
1655 }
1656 else
1657 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001658
Paul Bakker23986e52011-04-24 08:57:21 +00001659 for( i = 8; i > 0; i-- )
1660 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001661 break;
1662
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001663 /* The loops goes to its end iff the counter is wrapping */
1664 if( i == 0 )
1665 {
1666 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1667 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1668 }
1669
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1671
1672 return( 0 );
1673}
1674
Paul Bakker2770fbd2012-07-03 13:30:23 +00001675#if defined(POLARSSL_ZLIB_SUPPORT)
1676/*
1677 * Compression/decompression functions
1678 */
1679static int ssl_compress_buf( ssl_context *ssl )
1680{
1681 int ret;
1682 unsigned char *msg_post = ssl->out_msg;
1683 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001684 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001685
1686 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1687
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001688 if( len_pre == 0 )
1689 return( 0 );
1690
Paul Bakker2770fbd2012-07-03 13:30:23 +00001691 memcpy( msg_pre, ssl->out_msg, len_pre );
1692
1693 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1694 ssl->out_msglen ) );
1695
1696 SSL_DEBUG_BUF( 4, "before compression: output payload",
1697 ssl->out_msg, ssl->out_msglen );
1698
Paul Bakker48916f92012-09-16 19:57:18 +00001699 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1700 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1701 ssl->transform_out->ctx_deflate.next_out = msg_post;
1702 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001703
Paul Bakker48916f92012-09-16 19:57:18 +00001704 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001705 if( ret != Z_OK )
1706 {
1707 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1708 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1709 }
1710
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001711 ssl->out_msglen = SSL_BUFFER_LEN -
1712 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001713
Paul Bakker2770fbd2012-07-03 13:30:23 +00001714 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1715 ssl->out_msglen ) );
1716
1717 SSL_DEBUG_BUF( 4, "after compression: output payload",
1718 ssl->out_msg, ssl->out_msglen );
1719
1720 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1721
1722 return( 0 );
1723}
1724
1725static int ssl_decompress_buf( ssl_context *ssl )
1726{
1727 int ret;
1728 unsigned char *msg_post = ssl->in_msg;
1729 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001730 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001731
1732 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1733
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001734 if( len_pre == 0 )
1735 return( 0 );
1736
Paul Bakker2770fbd2012-07-03 13:30:23 +00001737 memcpy( msg_pre, ssl->in_msg, len_pre );
1738
1739 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1740 ssl->in_msglen ) );
1741
1742 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1743 ssl->in_msg, ssl->in_msglen );
1744
Paul Bakker48916f92012-09-16 19:57:18 +00001745 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1746 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1747 ssl->transform_in->ctx_inflate.next_out = msg_post;
1748 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749
Paul Bakker48916f92012-09-16 19:57:18 +00001750 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001751 if( ret != Z_OK )
1752 {
1753 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1754 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1755 }
1756
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001757 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1758 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001759
Paul Bakker2770fbd2012-07-03 13:30:23 +00001760 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1761 ssl->in_msglen ) );
1762
1763 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1764 ssl->in_msg, ssl->in_msglen );
1765
1766 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1767
1768 return( 0 );
1769}
1770#endif /* POLARSSL_ZLIB_SUPPORT */
1771
Paul Bakker5121ce52009-01-03 21:22:43 +00001772/*
1773 * Fill the input message buffer
1774 */
Paul Bakker23986e52011-04-24 08:57:21 +00001775int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001776{
Paul Bakker23986e52011-04-24 08:57:21 +00001777 int ret;
1778 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
1780 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1781
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001782 if( nb_want > SSL_BUFFER_LEN - 8 )
1783 {
1784 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1785 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1786 }
1787
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 while( ssl->in_left < nb_want )
1789 {
1790 len = nb_want - ssl->in_left;
1791 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1792
1793 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1794 ssl->in_left, nb_want ) );
1795 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1796
Paul Bakker831a7552011-05-18 13:32:51 +00001797 if( ret == 0 )
1798 return( POLARSSL_ERR_SSL_CONN_EOF );
1799
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 if( ret < 0 )
1801 return( ret );
1802
1803 ssl->in_left += ret;
1804 }
1805
1806 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1807
1808 return( 0 );
1809}
1810
1811/*
1812 * Flush any data not yet written
1813 */
1814int ssl_flush_output( ssl_context *ssl )
1815{
1816 int ret;
1817 unsigned char *buf;
1818
1819 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1820
1821 while( ssl->out_left > 0 )
1822 {
1823 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1824 5 + ssl->out_msglen, ssl->out_left ) );
1825
Paul Bakker5bd42292012-12-19 14:40:42 +01001826 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001827 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001828
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1830
1831 if( ret <= 0 )
1832 return( ret );
1833
1834 ssl->out_left -= ret;
1835 }
1836
1837 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1838
1839 return( 0 );
1840}
1841
1842/*
1843 * Record layer functions
1844 */
1845int ssl_write_record( ssl_context *ssl )
1846{
Paul Bakker05ef8352012-05-08 09:17:57 +00001847 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001848 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001849
1850 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1851
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1853 {
1854 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1855 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1856 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1857
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001858 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1859 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 }
1861
Paul Bakker2770fbd2012-07-03 13:30:23 +00001862#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001863 if( ssl->transform_out != NULL &&
1864 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001865 {
1866 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1867 {
1868 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1869 return( ret );
1870 }
1871
1872 len = ssl->out_msglen;
1873 }
1874#endif /*POLARSSL_ZLIB_SUPPORT */
1875
Paul Bakker05ef8352012-05-08 09:17:57 +00001876#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1877 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001879 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001880
Paul Bakker05ef8352012-05-08 09:17:57 +00001881 ret = ssl_hw_record_write( ssl );
1882 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1883 {
1884 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1885 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1886 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001887
1888 if( ret == 0 )
1889 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001890 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001891#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001892 if( !done )
1893 {
1894 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1895 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1896 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1898 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001899
Paul Bakker48916f92012-09-16 19:57:18 +00001900 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001901 {
1902 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1903 {
1904 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1905 return( ret );
1906 }
1907
1908 len = ssl->out_msglen;
1909 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1910 ssl->out_hdr[4] = (unsigned char)( len );
1911 }
1912
1913 ssl->out_left = 5 + ssl->out_msglen;
1914
1915 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1916 "version = [%d:%d], msglen = %d",
1917 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1918 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1919
1920 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001921 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 }
1923
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1925 {
1926 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1927 return( ret );
1928 }
1929
1930 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1931
1932 return( 0 );
1933}
1934
1935int ssl_read_record( ssl_context *ssl )
1936{
Paul Bakker05ef8352012-05-08 09:17:57 +00001937 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
1939 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1940
Paul Bakker68884e32013-01-07 18:20:04 +01001941 SSL_DEBUG_BUF( 4, "input record from network",
1942 ssl->in_hdr, 5 + ssl->in_msglen );
1943
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 if( ssl->in_hslen != 0 &&
1945 ssl->in_hslen < ssl->in_msglen )
1946 {
1947 /*
1948 * Get next Handshake message in the current record
1949 */
1950 ssl->in_msglen -= ssl->in_hslen;
1951
Paul Bakker8934a982011-08-05 11:11:53 +00001952 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001953 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001954
1955 ssl->in_hslen = 4;
1956 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1957
1958 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1959 " %d, type = %d, hslen = %d",
1960 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1961
1962 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1963 {
1964 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001965 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 }
1967
1968 if( ssl->in_msglen < ssl->in_hslen )
1969 {
1970 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001971 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 }
1973
Paul Bakker4224bc02014-04-08 14:36:50 +02001974 if( ssl->state != SSL_HANDSHAKE_OVER )
1975 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
1977 return( 0 );
1978 }
1979
1980 ssl->in_hslen = 0;
1981
1982 /*
1983 * Read the record header and validate it
1984 */
1985 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1986 {
1987 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1988 return( ret );
1989 }
1990
1991 ssl->in_msgtype = ssl->in_hdr[0];
1992 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
1993
1994 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
1995 "version = [%d:%d], msglen = %d",
1996 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
1997 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
1998
1999 if( ssl->in_hdr[1] != ssl->major_ver )
2000 {
2001 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002002 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 }
2004
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002005 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 {
2007 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002008 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 }
2010
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002011 /* Sanity check (outer boundaries) */
2012 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2013 {
2014 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2015 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2016 }
2017
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002019 * Make sure the message length is acceptable for the current transform
2020 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 */
Paul Bakker48916f92012-09-16 19:57:18 +00002022 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002024 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 {
2026 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002027 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 }
2029 }
2030 else
2031 {
Paul Bakker48916f92012-09-16 19:57:18 +00002032 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 {
2034 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002035 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 }
2037
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002038#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002040 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 {
2042 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002043 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002045#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002046
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002047#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2048 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 /*
2050 * TLS encrypted messages can have up to 256 bytes of padding
2051 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002052 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002053 ssl->in_msglen > ssl->transform_in->minlen +
2054 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 {
2056 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002057 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 }
2061
2062 /*
2063 * Read and optionally decrypt the message contents
2064 */
2065 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2066 {
2067 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2068 return( ret );
2069 }
2070
2071 SSL_DEBUG_BUF( 4, "input record from network",
2072 ssl->in_hdr, 5 + ssl->in_msglen );
2073
Paul Bakker05ef8352012-05-08 09:17:57 +00002074#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2075 if( ssl_hw_record_read != NULL)
2076 {
2077 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2078
2079 ret = ssl_hw_record_read( ssl );
2080 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2081 {
2082 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2083 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2084 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002085
2086 if( ret == 0 )
2087 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002088 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002089#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002090 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 {
2092 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2093 {
Paul Bakker40865c82013-01-31 17:13:13 +01002094#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2095 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2096 {
2097 ssl_send_alert_message( ssl,
2098 SSL_ALERT_LEVEL_FATAL,
2099 SSL_ALERT_MSG_BAD_RECORD_MAC );
2100 }
2101#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2103 return( ret );
2104 }
2105
2106 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2107 ssl->in_msg, ssl->in_msglen );
2108
2109 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2110 {
2111 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002112 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 }
2114 }
2115
Paul Bakker2770fbd2012-07-03 13:30:23 +00002116#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002117 if( ssl->transform_in != NULL &&
2118 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002119 {
2120 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2121 {
2122 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2123 return( ret );
2124 }
2125
2126 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2127 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2128 }
2129#endif /* POLARSSL_ZLIB_SUPPORT */
2130
Paul Bakker0a925182012-04-16 06:46:41 +00002131 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2132 ssl->in_msgtype != SSL_MSG_ALERT &&
2133 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2134 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2135 {
2136 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2137
Paul Bakker48916f92012-09-16 19:57:18 +00002138 if( ( ret = ssl_send_alert_message( ssl,
2139 SSL_ALERT_LEVEL_FATAL,
2140 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002141 {
2142 return( ret );
2143 }
2144
2145 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2146 }
2147
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2149 {
2150 ssl->in_hslen = 4;
2151 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2152
2153 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2154 " %d, type = %d, hslen = %d",
2155 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2156
2157 /*
2158 * Additional checks to validate the handshake header
2159 */
2160 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2161 {
2162 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002163 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 }
2165
2166 if( ssl->in_msglen < ssl->in_hslen )
2167 {
2168 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002169 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 }
2171
Paul Bakker48916f92012-09-16 19:57:18 +00002172 if( ssl->state != SSL_HANDSHAKE_OVER )
2173 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 }
2175
2176 if( ssl->in_msgtype == SSL_MSG_ALERT )
2177 {
2178 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2179 ssl->in_msg[0], ssl->in_msg[1] ) );
2180
2181 /*
2182 * Ignore non-fatal alerts, except close_notify
2183 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002184 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002186 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2187 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002188 /**
2189 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2190 * error identifier.
2191 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002192 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 }
2194
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002195 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2196 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 {
2198 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002199 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 }
2201 }
2202
2203 ssl->in_left = 0;
2204
2205 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2206
2207 return( 0 );
2208}
2209
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002210int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2211{
2212 int ret;
2213
2214 if( ( ret = ssl_send_alert_message( ssl,
2215 SSL_ALERT_LEVEL_FATAL,
2216 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2217 {
2218 return( ret );
2219 }
2220
2221 return( 0 );
2222}
2223
Paul Bakker0a925182012-04-16 06:46:41 +00002224int ssl_send_alert_message( ssl_context *ssl,
2225 unsigned char level,
2226 unsigned char message )
2227{
2228 int ret;
2229
2230 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2231
2232 ssl->out_msgtype = SSL_MSG_ALERT;
2233 ssl->out_msglen = 2;
2234 ssl->out_msg[0] = level;
2235 ssl->out_msg[1] = message;
2236
2237 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2238 {
2239 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2240 return( ret );
2241 }
2242
2243 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2244
2245 return( 0 );
2246}
2247
Paul Bakker5121ce52009-01-03 21:22:43 +00002248/*
2249 * Handshake functions
2250 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002251#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2252 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2253 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2254 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2255 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2256 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2257 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002258int ssl_write_certificate( ssl_context *ssl )
2259{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002260 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
2262 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2263
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002264 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002265 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2266 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002267 {
2268 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2269 ssl->state++;
2270 return( 0 );
2271 }
2272
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002273 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2274 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002275}
2276
2277int ssl_parse_certificate( ssl_context *ssl )
2278{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002279 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2280
2281 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2282
2283 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002284 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2285 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002286 {
2287 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2288 ssl->state++;
2289 return( 0 );
2290 }
2291
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002292 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2293 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002294}
2295#else
2296int ssl_write_certificate( ssl_context *ssl )
2297{
2298 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2299 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002300 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002301 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2302
2303 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2304
2305 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002306 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2307 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002308 {
2309 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2310 ssl->state++;
2311 return( 0 );
2312 }
2313
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 if( ssl->endpoint == SSL_IS_CLIENT )
2315 {
2316 if( ssl->client_auth == 0 )
2317 {
2318 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2319 ssl->state++;
2320 return( 0 );
2321 }
2322
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002323#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 /*
2325 * If using SSLv3 and got no cert, send an Alert message
2326 * (otherwise an empty Certificate message will be sent).
2327 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002328 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2330 {
2331 ssl->out_msglen = 2;
2332 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002333 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2334 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
2336 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2337 goto write_msg;
2338 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002339#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 }
2341 else /* SSL_IS_SERVER */
2342 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002343 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 {
2345 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002346 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 }
2348 }
2349
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002350 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002351
2352 /*
2353 * 0 . 0 handshake type
2354 * 1 . 3 handshake length
2355 * 4 . 6 length of all certs
2356 * 7 . 9 length of cert. 1
2357 * 10 . n-1 peer certificate
2358 * n . n+2 length of cert. 2
2359 * n+3 . ... upper level cert, etc.
2360 */
2361 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002362 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
Paul Bakker29087132010-03-21 21:03:34 +00002364 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 {
2366 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002367 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002368 {
2369 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2370 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002371 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 }
2373
2374 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2375 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2376 ssl->out_msg[i + 2] = (unsigned char)( n );
2377
2378 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2379 i += n; crt = crt->next;
2380 }
2381
2382 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2383 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2384 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2385
2386 ssl->out_msglen = i;
2387 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2388 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2389
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002390#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002391write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002392#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
2394 ssl->state++;
2395
2396 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2397 {
2398 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2399 return( ret );
2400 }
2401
2402 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2403
Paul Bakkered27a042013-04-18 22:46:23 +02002404 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405}
2406
2407int ssl_parse_certificate( ssl_context *ssl )
2408{
Paul Bakkered27a042013-04-18 22:46:23 +02002409 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002410 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002411 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
2413 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2414
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002415 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002416 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2417 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002418 {
2419 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2420 ssl->state++;
2421 return( 0 );
2422 }
2423
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002425 ( ssl->authmode == SSL_VERIFY_NONE ||
2426 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002428 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2430 ssl->state++;
2431 return( 0 );
2432 }
2433
2434 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2435 {
2436 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2437 return( ret );
2438 }
2439
2440 ssl->state++;
2441
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002442#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 /*
2444 * Check if the client sent an empty certificate
2445 */
2446 if( ssl->endpoint == SSL_IS_SERVER &&
2447 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2448 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002449 if( ssl->in_msglen == 2 &&
2450 ssl->in_msgtype == SSL_MSG_ALERT &&
2451 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2452 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 {
2454 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2455
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002456 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2458 return( 0 );
2459 else
Paul Bakker40e46942009-01-03 21:51:57 +00002460 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
2462 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002463#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002465#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2466 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 if( ssl->endpoint == SSL_IS_SERVER &&
2468 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2469 {
2470 if( ssl->in_hslen == 7 &&
2471 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2472 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2473 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2474 {
2475 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2476
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002477 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002479 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 else
2481 return( 0 );
2482 }
2483 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002484#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2485 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
2487 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2488 {
2489 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002490 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 }
2492
2493 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2494 {
2495 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002496 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 }
2498
2499 /*
2500 * Same message structure as in ssl_write_certificate()
2501 */
2502 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2503
2504 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2505 {
2506 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002507 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 }
2509
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002510 /* In case we tried to reuse a session but it failed */
2511 if( ssl->session_negotiate->peer_cert != NULL )
2512 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002513 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002514 polarssl_free( ssl->session_negotiate->peer_cert );
2515 }
2516
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002517 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2518 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 {
2520 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002521 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002522 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 }
2524
Paul Bakkerb6b09562013-09-18 14:17:41 +02002525 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
2527 i = 7;
2528
2529 while( i < ssl->in_hslen )
2530 {
2531 if( ssl->in_msg[i] != 0 )
2532 {
2533 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002534 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 }
2536
2537 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2538 | (unsigned int) ssl->in_msg[i + 2];
2539 i += 3;
2540
2541 if( n < 128 || i + n > ssl->in_hslen )
2542 {
2543 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002544 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 }
2546
Paul Bakkerddf26b42013-09-18 13:46:23 +02002547 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2548 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 if( ret != 0 )
2550 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002551 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 return( ret );
2553 }
2554
2555 i += n;
2556 }
2557
Paul Bakker48916f92012-09-16 19:57:18 +00002558 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002560 /*
2561 * On client, make sure the server cert doesn't change during renego to
2562 * avoid "triple handshake" attack: https://secure-resumption.com/
2563 */
2564 if( ssl->endpoint == SSL_IS_CLIENT &&
2565 ssl->renegotiation == SSL_RENEGOTIATION )
2566 {
2567 if( ssl->session->peer_cert == NULL )
2568 {
2569 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2570 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2571 }
2572
2573 if( ssl->session->peer_cert->raw.len !=
2574 ssl->session_negotiate->peer_cert->raw.len ||
2575 memcmp( ssl->session->peer_cert->raw.p,
2576 ssl->session_negotiate->peer_cert->raw.p,
2577 ssl->session->peer_cert->raw.len ) != 0 )
2578 {
2579 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2580 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2581 }
2582 }
2583
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 if( ssl->authmode != SSL_VERIFY_NONE )
2585 {
2586 if( ssl->ca_chain == NULL )
2587 {
2588 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002589 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 }
2591
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002592 /*
2593 * Main check: verify certificate
2594 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002595 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2596 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2597 &ssl->session_negotiate->verify_result,
2598 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
2600 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002601 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002603 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002604
2605 /*
2606 * Secondary checks: always done, but change 'ret' only if it was 0
2607 */
2608
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002609#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002610 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002611 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002612
2613 /* If certificate uses an EC key, make sure the curve is OK */
2614 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2615 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2616 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002617 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002618 if( ret == 0 )
2619 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002620 }
2621 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002622#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002624 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2625 ciphersuite_info,
2626 ! ssl->endpoint ) != 0 )
2627 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002628 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002629 if( ret == 0 )
2630 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2631 }
2632
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2634 ret = 0;
2635 }
2636
2637 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2638
2639 return( ret );
2640}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002641#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2642 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2643 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2644 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2645 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2646 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2647 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
2649int ssl_write_change_cipher_spec( ssl_context *ssl )
2650{
2651 int ret;
2652
2653 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2654
2655 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2656 ssl->out_msglen = 1;
2657 ssl->out_msg[0] = 1;
2658
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 ssl->state++;
2660
2661 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2662 {
2663 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2664 return( ret );
2665 }
2666
2667 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2668
2669 return( 0 );
2670}
2671
2672int ssl_parse_change_cipher_spec( ssl_context *ssl )
2673{
2674 int ret;
2675
2676 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2677
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2679 {
2680 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2681 return( ret );
2682 }
2683
2684 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2685 {
2686 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002687 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 }
2689
2690 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2691 {
2692 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002693 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 }
2695
2696 ssl->state++;
2697
2698 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2699
2700 return( 0 );
2701}
2702
Paul Bakker41c83d32013-03-20 14:39:14 +01002703void ssl_optimize_checksum( ssl_context *ssl,
2704 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002705{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002706 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002707
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002708#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2709 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002710 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002711 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002712 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002713#endif
2714#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2715#if defined(POLARSSL_SHA512_C)
2716 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2717 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2718 else
2719#endif
2720#if defined(POLARSSL_SHA256_C)
2721 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002722 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002723 else
2724#endif
2725#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002726 {
2727 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002728 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002729 }
Paul Bakker380da532012-04-18 16:10:25 +00002730}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002731
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002732static void ssl_update_checksum_start( ssl_context *ssl,
2733 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002734{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002735#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2736 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002737 md5_update( &ssl->handshake->fin_md5 , buf, len );
2738 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002739#endif
2740#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2741#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002742 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002743#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002744#if defined(POLARSSL_SHA512_C)
2745 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002746#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002747#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002748}
2749
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002750#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2751 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002752static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2753 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002754{
Paul Bakker48916f92012-09-16 19:57:18 +00002755 md5_update( &ssl->handshake->fin_md5 , buf, len );
2756 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002757}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002758#endif
Paul Bakker380da532012-04-18 16:10:25 +00002759
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002760#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2761#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002762static void ssl_update_checksum_sha256( ssl_context *ssl,
2763 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002764{
Paul Bakker9e36f042013-06-30 14:34:05 +02002765 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002766}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002767#endif
Paul Bakker380da532012-04-18 16:10:25 +00002768
Paul Bakker9e36f042013-06-30 14:34:05 +02002769#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002770static void ssl_update_checksum_sha384( ssl_context *ssl,
2771 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002772{
Paul Bakker9e36f042013-06-30 14:34:05 +02002773 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002774}
Paul Bakker769075d2012-11-24 11:26:46 +01002775#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002776#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002777
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002778#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002779static void ssl_calc_finished_ssl(
2780 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002781{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002782 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002783 md5_context md5;
2784 sha1_context sha1;
2785
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 unsigned char padbuf[48];
2787 unsigned char md5sum[16];
2788 unsigned char sha1sum[20];
2789
Paul Bakker48916f92012-09-16 19:57:18 +00002790 ssl_session *session = ssl->session_negotiate;
2791 if( !session )
2792 session = ssl->session;
2793
Paul Bakker1ef83d62012-04-11 12:09:53 +00002794 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2795
Paul Bakker48916f92012-09-16 19:57:18 +00002796 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2797 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
2799 /*
2800 * SSLv3:
2801 * hash =
2802 * MD5( master + pad2 +
2803 * MD5( handshake + sender + master + pad1 ) )
2804 * + SHA1( master + pad2 +
2805 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 */
2807
Paul Bakker90995b52013-06-24 19:20:35 +02002808#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002810 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002811#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Paul Bakker90995b52013-06-24 19:20:35 +02002813#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002815 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002816#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
Paul Bakker3c2122f2013-06-24 19:03:14 +02002818 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2819 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
Paul Bakker1ef83d62012-04-11 12:09:53 +00002821 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
Paul Bakker3c2122f2013-06-24 19:03:14 +02002823 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002824 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002825 md5_update( &md5, padbuf, 48 );
2826 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
Paul Bakker3c2122f2013-06-24 19:03:14 +02002828 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002829 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002830 sha1_update( &sha1, padbuf, 40 );
2831 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Paul Bakker1ef83d62012-04-11 12:09:53 +00002833 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Paul Bakker1ef83d62012-04-11 12:09:53 +00002835 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002836 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002837 md5_update( &md5, padbuf, 48 );
2838 md5_update( &md5, md5sum, 16 );
2839 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Paul Bakker1ef83d62012-04-11 12:09:53 +00002841 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002842 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002843 sha1_update( &sha1, padbuf , 40 );
2844 sha1_update( &sha1, sha1sum, 20 );
2845 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
Paul Bakker1ef83d62012-04-11 12:09:53 +00002847 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
Paul Bakker1ef83d62012-04-11 12:09:53 +00002849 memset( &md5, 0, sizeof( md5_context ) );
2850 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
2852 memset( padbuf, 0, sizeof( padbuf ) );
2853 memset( md5sum, 0, sizeof( md5sum ) );
2854 memset( sha1sum, 0, sizeof( sha1sum ) );
2855
2856 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2857}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002858#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002860#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002861static void ssl_calc_finished_tls(
2862 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002863{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002864 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002865 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002866 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002868 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002869
Paul Bakker48916f92012-09-16 19:57:18 +00002870 ssl_session *session = ssl->session_negotiate;
2871 if( !session )
2872 session = ssl->session;
2873
Paul Bakker1ef83d62012-04-11 12:09:53 +00002874 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
Paul Bakker48916f92012-09-16 19:57:18 +00002876 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2877 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 /*
2880 * TLSv1:
2881 * hash = PRF( master, finished_label,
2882 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2883 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Paul Bakker90995b52013-06-24 19:20:35 +02002885#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002886 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2887 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002888#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002889
Paul Bakker90995b52013-06-24 19:20:35 +02002890#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002891 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2892 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002893#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894
2895 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002896 ? "client finished"
2897 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898
2899 md5_finish( &md5, padbuf );
2900 sha1_finish( &sha1, padbuf + 16 );
2901
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002902 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002903 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002904
2905 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2906
2907 memset( &md5, 0, sizeof( md5_context ) );
2908 memset( &sha1, 0, sizeof( sha1_context ) );
2909
2910 memset( padbuf, 0, sizeof( padbuf ) );
2911
2912 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2913}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002914#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002916#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2917#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002918static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002919 ssl_context *ssl, unsigned char *buf, int from )
2920{
2921 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002922 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002923 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924 unsigned char padbuf[32];
2925
Paul Bakker48916f92012-09-16 19:57:18 +00002926 ssl_session *session = ssl->session_negotiate;
2927 if( !session )
2928 session = ssl->session;
2929
Paul Bakker380da532012-04-18 16:10:25 +00002930 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002931
Paul Bakker9e36f042013-06-30 14:34:05 +02002932 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002933
2934 /*
2935 * TLSv1.2:
2936 * hash = PRF( master, finished_label,
2937 * Hash( handshake ) )[0.11]
2938 */
2939
Paul Bakker9e36f042013-06-30 14:34:05 +02002940#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002941 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002942 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002943#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002944
2945 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002946 ? "client finished"
2947 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002948
Paul Bakker9e36f042013-06-30 14:34:05 +02002949 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002950
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002951 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002952 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953
2954 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2955
Paul Bakker9e36f042013-06-30 14:34:05 +02002956 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002957
2958 memset( padbuf, 0, sizeof( padbuf ) );
2959
2960 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2961}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002962#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963
Paul Bakker9e36f042013-06-30 14:34:05 +02002964#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002965static void ssl_calc_finished_tls_sha384(
2966 ssl_context *ssl, unsigned char *buf, int from )
2967{
2968 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002969 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002970 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002971 unsigned char padbuf[48];
2972
Paul Bakker48916f92012-09-16 19:57:18 +00002973 ssl_session *session = ssl->session_negotiate;
2974 if( !session )
2975 session = ssl->session;
2976
Paul Bakker380da532012-04-18 16:10:25 +00002977 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002978
Paul Bakker9e36f042013-06-30 14:34:05 +02002979 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002980
2981 /*
2982 * TLSv1.2:
2983 * hash = PRF( master, finished_label,
2984 * Hash( handshake ) )[0.11]
2985 */
2986
Paul Bakker9e36f042013-06-30 14:34:05 +02002987#if !defined(POLARSSL_SHA512_ALT)
2988 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2989 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002990#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00002991
2992 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002993 ? "client finished"
2994 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00002995
Paul Bakker9e36f042013-06-30 14:34:05 +02002996 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002997
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002998 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002999 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000
3001 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3002
Paul Bakker9e36f042013-06-30 14:34:05 +02003003 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004
3005 memset( padbuf, 0, sizeof( padbuf ) );
3006
3007 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3008}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003009#endif /* POLARSSL_SHA512_C */
3010#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003011
Paul Bakker48916f92012-09-16 19:57:18 +00003012void ssl_handshake_wrapup( ssl_context *ssl )
3013{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003014 int resume = ssl->handshake->resume;
3015
Paul Bakker48916f92012-09-16 19:57:18 +00003016 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3017
3018 /*
3019 * Free our handshake params
3020 */
3021 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003022 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003023 ssl->handshake = NULL;
3024
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003025 if( ssl->renegotiation == SSL_RENEGOTIATION )
3026 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3027
Paul Bakker48916f92012-09-16 19:57:18 +00003028 /*
3029 * Switch in our now active transform context
3030 */
3031 if( ssl->transform )
3032 {
3033 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003034 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003035 }
3036 ssl->transform = ssl->transform_negotiate;
3037 ssl->transform_negotiate = NULL;
3038
Paul Bakker0a597072012-09-25 21:55:46 +00003039 if( ssl->session )
3040 {
3041 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003042 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003043 }
3044 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003045 ssl->session_negotiate = NULL;
3046
Paul Bakker0a597072012-09-25 21:55:46 +00003047 /*
3048 * Add cache entry
3049 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003050 if( ssl->f_set_cache != NULL &&
3051 ssl->session->length != 0 &&
3052 resume == 0 )
3053 {
Paul Bakker0a597072012-09-25 21:55:46 +00003054 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3055 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003056 }
Paul Bakker0a597072012-09-25 21:55:46 +00003057
Paul Bakker48916f92012-09-16 19:57:18 +00003058 ssl->state++;
3059
3060 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3061}
3062
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063int ssl_write_finished( ssl_context *ssl )
3064{
3065 int ret, hash_len;
3066
3067 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3068
Paul Bakker92be97b2013-01-02 17:30:03 +01003069 /*
3070 * Set the out_msg pointer to the correct location based on IV length
3071 */
3072 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3073 {
3074 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3075 ssl->transform_negotiate->fixed_ivlen;
3076 }
3077 else
3078 ssl->out_msg = ssl->out_iv;
3079
Paul Bakker48916f92012-09-16 19:57:18 +00003080 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081
3082 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3084
Paul Bakker48916f92012-09-16 19:57:18 +00003085 ssl->verify_data_len = hash_len;
3086 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3087
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 ssl->out_msglen = 4 + hash_len;
3089 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3090 ssl->out_msg[0] = SSL_HS_FINISHED;
3091
3092 /*
3093 * In case of session resuming, invert the client and server
3094 * ChangeCipherSpec messages order.
3095 */
Paul Bakker0a597072012-09-25 21:55:46 +00003096 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 {
3098 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003099 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 else
3101 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3102 }
3103 else
3104 ssl->state++;
3105
Paul Bakker48916f92012-09-16 19:57:18 +00003106 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003107 * Switch to our negotiated transform and session parameters for outbound
3108 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003109 */
3110 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3111 ssl->transform_out = ssl->transform_negotiate;
3112 ssl->session_out = ssl->session_negotiate;
3113 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Paul Bakker07eb38b2012-12-19 14:42:06 +01003115#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3116 if( ssl_hw_record_activate != NULL)
3117 {
3118 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3119 {
3120 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3121 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3122 }
3123 }
3124#endif
3125
Paul Bakker5121ce52009-01-03 21:22:43 +00003126 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3127 {
3128 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3129 return( ret );
3130 }
3131
3132 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3133
3134 return( 0 );
3135}
3136
3137int ssl_parse_finished( ssl_context *ssl )
3138{
Paul Bakker23986e52011-04-24 08:57:21 +00003139 int ret;
3140 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003141 unsigned char buf[36];
3142
3143 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3144
Paul Bakker48916f92012-09-16 19:57:18 +00003145 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
Paul Bakker48916f92012-09-16 19:57:18 +00003147 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003148 * Switch to our negotiated transform and session parameters for inbound
3149 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003150 */
3151 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3152 ssl->transform_in = ssl->transform_negotiate;
3153 ssl->session_in = ssl->session_negotiate;
3154 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Paul Bakker92be97b2013-01-02 17:30:03 +01003156 /*
3157 * Set the in_msg pointer to the correct location based on IV length
3158 */
3159 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3160 {
3161 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3162 ssl->transform_negotiate->fixed_ivlen;
3163 }
3164 else
3165 ssl->in_msg = ssl->in_iv;
3166
Paul Bakker07eb38b2012-12-19 14:42:06 +01003167#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3168 if( ssl_hw_record_activate != NULL)
3169 {
3170 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3171 {
3172 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3173 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3174 }
3175 }
3176#endif
3177
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3179 {
3180 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3181 return( ret );
3182 }
3183
3184 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3185 {
3186 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003187 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 }
3189
Paul Bakker1ef83d62012-04-11 12:09:53 +00003190 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3192
3193 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3194 ssl->in_hslen != 4 + hash_len )
3195 {
3196 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003197 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 }
3199
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003200 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 {
3202 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003203 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 }
3205
Paul Bakker48916f92012-09-16 19:57:18 +00003206 ssl->verify_data_len = hash_len;
3207 memcpy( ssl->peer_verify_data, buf, hash_len );
3208
Paul Bakker0a597072012-09-25 21:55:46 +00003209 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 {
3211 if( ssl->endpoint == SSL_IS_CLIENT )
3212 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3213
3214 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003215 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 }
3217 else
3218 ssl->state++;
3219
3220 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3221
3222 return( 0 );
3223}
3224
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003225static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003226{
3227 if( ssl->transform_negotiate )
3228 ssl_transform_free( ssl->transform_negotiate );
3229 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003230 {
3231 ssl->transform_negotiate =
3232 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003233
3234 if( ssl->transform_negotiate != NULL )
3235 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003236 }
Paul Bakker48916f92012-09-16 19:57:18 +00003237
3238 if( ssl->session_negotiate )
3239 ssl_session_free( ssl->session_negotiate );
3240 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003241 {
3242 ssl->session_negotiate =
3243 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003244
3245 if( ssl->session_negotiate != NULL )
3246 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003247 }
Paul Bakker48916f92012-09-16 19:57:18 +00003248
3249 if( ssl->handshake )
3250 ssl_handshake_free( ssl->handshake );
3251 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003252 {
3253 ssl->handshake = (ssl_handshake_params *)
3254 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003255
3256 if( ssl->handshake != NULL )
3257 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003258 }
Paul Bakker48916f92012-09-16 19:57:18 +00003259
3260 if( ssl->handshake == NULL ||
3261 ssl->transform_negotiate == NULL ||
3262 ssl->session_negotiate == NULL )
3263 {
3264 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3265 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3266 }
3267
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003268#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3269 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003270 md5_starts( &ssl->handshake->fin_md5 );
3271 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003272#endif
3273#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3274#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003275 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003276#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003277#if defined(POLARSSL_SHA512_C)
3278 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003279#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003280#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003281
3282 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003283 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003284
Paul Bakker61d113b2013-07-04 11:51:43 +02003285#if defined(POLARSSL_ECDH_C)
3286 ecdh_init( &ssl->handshake->ecdh_ctx );
3287#endif
3288
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003289#if defined(POLARSSL_X509_CRT_PARSE_C)
3290 ssl->handshake->key_cert = ssl->key_cert;
3291#endif
3292
Paul Bakker48916f92012-09-16 19:57:18 +00003293 return( 0 );
3294}
3295
Paul Bakker5121ce52009-01-03 21:22:43 +00003296/*
3297 * Initialize an SSL context
3298 */
3299int ssl_init( ssl_context *ssl )
3300{
Paul Bakker48916f92012-09-16 19:57:18 +00003301 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 int len = SSL_BUFFER_LEN;
3303
3304 memset( ssl, 0, sizeof( ssl_context ) );
3305
Paul Bakker62f2dee2012-09-28 07:31:51 +00003306 /*
3307 * Sane defaults
3308 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003309 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3310 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3311 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3312 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003313
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003314 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003315
Paul Bakker62f2dee2012-09-28 07:31:51 +00003316#if defined(POLARSSL_DHM_C)
3317 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3318 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3319 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3320 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3321 {
3322 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3323 return( ret );
3324 }
3325#endif
3326
3327 /*
3328 * Prepare base structures
3329 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003330 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003332 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 ssl->in_msg = ssl->in_ctr + 13;
3334
3335 if( ssl->in_ctr == NULL )
3336 {
3337 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003338 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 }
3340
Paul Bakker6e339b52013-07-03 13:37:05 +02003341 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003343 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003344 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003345
3346 if( ssl->out_ctr == NULL )
3347 {
3348 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003349 polarssl_free( ssl->in_ctr );
3350 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003351 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 }
3353
3354 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3355 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3356
Paul Bakker606b4ba2013-08-14 16:52:14 +02003357#if defined(POLARSSL_SSL_SESSION_TICKETS)
3358 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3359#endif
3360
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003361#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003362 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003363#endif
3364
Paul Bakker48916f92012-09-16 19:57:18 +00003365 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3366 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
3368 return( 0 );
3369}
3370
3371/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003372 * Reset an initialized and used SSL context for re-use while retaining
3373 * all application-set variables, function pointers and data.
3374 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003375int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003376{
Paul Bakker48916f92012-09-16 19:57:18 +00003377 int ret;
3378
Paul Bakker7eb013f2011-10-06 12:37:39 +00003379 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003380 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3381 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3382
3383 ssl->verify_data_len = 0;
3384 memset( ssl->own_verify_data, 0, 36 );
3385 memset( ssl->peer_verify_data, 0, 36 );
3386
Paul Bakker7eb013f2011-10-06 12:37:39 +00003387 ssl->in_offt = NULL;
3388
Paul Bakker92be97b2013-01-02 17:30:03 +01003389 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003390 ssl->in_msgtype = 0;
3391 ssl->in_msglen = 0;
3392 ssl->in_left = 0;
3393
3394 ssl->in_hslen = 0;
3395 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003396 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003397
Paul Bakker92be97b2013-01-02 17:30:03 +01003398 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003399 ssl->out_msgtype = 0;
3400 ssl->out_msglen = 0;
3401 ssl->out_left = 0;
3402
Paul Bakker48916f92012-09-16 19:57:18 +00003403 ssl->transform_in = NULL;
3404 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003405
3406 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3407 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003408
3409#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3410 if( ssl_hw_record_reset != NULL)
3411 {
3412 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003413 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003414 {
3415 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3416 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3417 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003418 }
3419#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003420
Paul Bakker48916f92012-09-16 19:57:18 +00003421 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003422 {
Paul Bakker48916f92012-09-16 19:57:18 +00003423 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003424 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003425 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003426 }
Paul Bakker48916f92012-09-16 19:57:18 +00003427
Paul Bakkerc0463502013-02-14 11:19:38 +01003428 if( ssl->session )
3429 {
3430 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003431 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003432 ssl->session = NULL;
3433 }
3434
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003435#if defined(POLARSSL_SSL_ALPN)
3436 ssl->alpn_chosen = NULL;
3437#endif
3438
Paul Bakker48916f92012-09-16 19:57:18 +00003439 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3440 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003441
3442 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003443}
3444
Paul Bakkera503a632013-08-14 13:48:06 +02003445#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003446/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003447 * Allocate and initialize ticket keys
3448 */
3449static int ssl_ticket_keys_init( ssl_context *ssl )
3450{
3451 int ret;
3452 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003453 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003454
3455 if( ssl->ticket_keys != NULL )
3456 return( 0 );
3457
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003458 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3459 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003460 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3461
3462 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003463 {
3464 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003465 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003466 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003467
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003468 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3469 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3470 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3471 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003472 polarssl_free( tkeys );
3473 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003474 }
3475
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003476 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003477 {
3478 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003479 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003480 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003481
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003482 ssl->ticket_keys = tkeys;
3483
3484 return( 0 );
3485}
Paul Bakkera503a632013-08-14 13:48:06 +02003486#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003487
3488/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 * SSL set accessors
3490 */
3491void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3492{
3493 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003494
Paul Bakker606b4ba2013-08-14 16:52:14 +02003495#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003496 if( endpoint == SSL_IS_CLIENT )
3497 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003498#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003499}
3500
3501void ssl_set_authmode( ssl_context *ssl, int authmode )
3502{
3503 ssl->authmode = authmode;
3504}
3505
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003506#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003507void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003508 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003509 void *p_vrfy )
3510{
3511 ssl->f_vrfy = f_vrfy;
3512 ssl->p_vrfy = p_vrfy;
3513}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003514#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003515
Paul Bakker5121ce52009-01-03 21:22:43 +00003516void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003517 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003518 void *p_rng )
3519{
3520 ssl->f_rng = f_rng;
3521 ssl->p_rng = p_rng;
3522}
3523
3524void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003525 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003526 void *p_dbg )
3527{
3528 ssl->f_dbg = f_dbg;
3529 ssl->p_dbg = p_dbg;
3530}
3531
3532void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003533 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003534 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003535{
3536 ssl->f_recv = f_recv;
3537 ssl->f_send = f_send;
3538 ssl->p_recv = p_recv;
3539 ssl->p_send = p_send;
3540}
3541
Paul Bakker0a597072012-09-25 21:55:46 +00003542void ssl_set_session_cache( ssl_context *ssl,
3543 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3544 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003545{
Paul Bakker0a597072012-09-25 21:55:46 +00003546 ssl->f_get_cache = f_get_cache;
3547 ssl->p_get_cache = p_get_cache;
3548 ssl->f_set_cache = f_set_cache;
3549 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003550}
3551
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003552int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003553{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003554 int ret;
3555
3556 if( ssl == NULL ||
3557 session == NULL ||
3558 ssl->session_negotiate == NULL ||
3559 ssl->endpoint != SSL_IS_CLIENT )
3560 {
3561 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3562 }
3563
3564 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3565 return( ret );
3566
Paul Bakker0a597072012-09-25 21:55:46 +00003567 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003568
3569 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003570}
3571
Paul Bakkerb68cad62012-08-23 08:34:18 +00003572void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003573{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003574 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3575 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3576 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3577 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3578}
3579
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003580void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3581 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003582 int major, int minor )
3583{
3584 if( major != SSL_MAJOR_VERSION_3 )
3585 return;
3586
3587 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3588 return;
3589
3590 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003591}
3592
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003593#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003594/* Add a new (empty) key_cert entry an return a pointer to it */
3595static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3596{
3597 ssl_key_cert *key_cert, *last;
3598
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003599 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3600 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003601 return( NULL );
3602
3603 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3604
3605 /* Append the new key_cert to the (possibly empty) current list */
3606 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003607 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003608 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003609 if( ssl->handshake != NULL )
3610 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003611 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003612 else
3613 {
3614 last = ssl->key_cert;
3615 while( last->next != NULL )
3616 last = last->next;
3617 last->next = key_cert;
3618 }
3619
3620 return key_cert;
3621}
3622
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003623void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003624 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003625{
3626 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003627 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 ssl->peer_cn = peer_cn;
3629}
3630
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003631int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003632 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003633{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003634 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3635
3636 if( key_cert == NULL )
3637 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3638
3639 key_cert->cert = own_cert;
3640 key_cert->key = pk_key;
3641
3642 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003643}
3644
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003645#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003646int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003647 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003648{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003649 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003650 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003651
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003652 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003653 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3654
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003655 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3656 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003657 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003658
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003659 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003660
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003661 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003662 if( ret != 0 )
3663 return( ret );
3664
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003665 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003666 return( ret );
3667
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003668 key_cert->cert = own_cert;
3669 key_cert->key_own_alloc = 1;
3670
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003671 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003672}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003673#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003674
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003675int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003676 void *rsa_key,
3677 rsa_decrypt_func rsa_decrypt,
3678 rsa_sign_func rsa_sign,
3679 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003680{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003681 int ret;
3682 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003683
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003684 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003685 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3686
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003687 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3688 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003689 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003690
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003691 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003692
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003693 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3694 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3695 return( ret );
3696
3697 key_cert->cert = own_cert;
3698 key_cert->key_own_alloc = 1;
3699
3700 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003701}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003702#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003703
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003704#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003705int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3706 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003707{
Paul Bakker6db455e2013-09-18 17:29:31 +02003708 if( psk == NULL || psk_identity == NULL )
3709 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3710
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003711 /*
3712 * The length will be check later anyway, but in case it is obviously
3713 * too large, better abort now. The PMS is as follows:
3714 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3715 */
3716 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3717 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3718
Paul Bakker6db455e2013-09-18 17:29:31 +02003719 if( ssl->psk != NULL )
3720 {
3721 polarssl_free( ssl->psk );
3722 polarssl_free( ssl->psk_identity );
3723 }
3724
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003725 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003726 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003727
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003728 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003729 ssl->psk_identity = (unsigned char *)
3730 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003731
3732 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3733 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3734
3735 memcpy( ssl->psk, psk, ssl->psk_len );
3736 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003737
3738 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003739}
3740
3741void ssl_set_psk_cb( ssl_context *ssl,
3742 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3743 size_t),
3744 void *p_psk )
3745{
3746 ssl->f_psk = f_psk;
3747 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003748}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003749#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003750
Paul Bakker48916f92012-09-16 19:57:18 +00003751#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003752int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003753{
3754 int ret;
3755
Paul Bakker48916f92012-09-16 19:57:18 +00003756 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003757 {
3758 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3759 return( ret );
3760 }
3761
Paul Bakker48916f92012-09-16 19:57:18 +00003762 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003763 {
3764 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3765 return( ret );
3766 }
3767
3768 return( 0 );
3769}
3770
Paul Bakker1b57b062011-01-06 15:48:19 +00003771int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3772{
3773 int ret;
3774
Paul Bakker48916f92012-09-16 19:57:18 +00003775 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003776 {
3777 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3778 return( ret );
3779 }
3780
Paul Bakker48916f92012-09-16 19:57:18 +00003781 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003782 {
3783 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3784 return( ret );
3785 }
3786
3787 return( 0 );
3788}
Paul Bakker48916f92012-09-16 19:57:18 +00003789#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003790
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003791#if defined(POLARSSL_SSL_SET_CURVES)
3792/*
3793 * Set the allowed elliptic curves
3794 */
3795void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3796{
3797 ssl->curve_list = curve_list;
3798}
3799#endif
3800
Paul Bakker0be444a2013-08-27 21:55:01 +02003801#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003802int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003803{
3804 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003805 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003806
3807 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003808
3809 if( ssl->hostname_len + 1 == 0 )
3810 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3811
Paul Bakker6e339b52013-07-03 13:37:05 +02003812 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003813
Paul Bakkerb15b8512012-01-13 13:44:06 +00003814 if( ssl->hostname == NULL )
3815 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3816
Paul Bakker3c2122f2013-06-24 19:03:14 +02003817 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003818 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003819
Paul Bakker40ea7de2009-05-03 10:18:48 +00003820 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003821
3822 return( 0 );
3823}
3824
Paul Bakker5701cdc2012-09-27 21:49:42 +00003825void ssl_set_sni( ssl_context *ssl,
3826 int (*f_sni)(void *, ssl_context *,
3827 const unsigned char *, size_t),
3828 void *p_sni )
3829{
3830 ssl->f_sni = f_sni;
3831 ssl->p_sni = p_sni;
3832}
Paul Bakker0be444a2013-08-27 21:55:01 +02003833#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003834
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003835#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003836int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003837{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003838 size_t cur_len, tot_len;
3839 const char **p;
3840
3841 /*
3842 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3843 * truncated". Check lengths now rather than later.
3844 */
3845 tot_len = 0;
3846 for( p = protos; *p != NULL; p++ )
3847 {
3848 cur_len = strlen( *p );
3849 tot_len += cur_len;
3850
3851 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3852 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3853 }
3854
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003855 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003856
3857 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003858}
3859
3860const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3861{
3862 return ssl->alpn_chosen;
3863}
3864#endif /* POLARSSL_SSL_ALPN */
3865
Paul Bakker490ecc82011-10-06 13:04:09 +00003866void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3867{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003868 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3869 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3870 {
3871 ssl->max_major_ver = major;
3872 ssl->max_minor_ver = minor;
3873 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003874}
3875
Paul Bakker1d29fb52012-09-28 13:28:45 +00003876void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3877{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003878 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3879 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3880 {
3881 ssl->min_major_ver = major;
3882 ssl->min_minor_ver = minor;
3883 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003884}
3885
Paul Bakker05decb22013-08-15 13:33:48 +02003886#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003887int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3888{
Paul Bakker77e257e2013-12-16 15:29:52 +01003889 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003890 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003891 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003892 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003893 }
3894
3895 ssl->mfl_code = mfl_code;
3896
3897 return( 0 );
3898}
Paul Bakker05decb22013-08-15 13:33:48 +02003899#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003900
Paul Bakker1f2bc622013-08-15 13:45:55 +02003901#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003902int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003903{
3904 if( ssl->endpoint != SSL_IS_CLIENT )
3905 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3906
Paul Bakker8c1ede62013-07-19 14:14:37 +02003907 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003908
3909 return( 0 );
3910}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003911#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003912
Paul Bakker48916f92012-09-16 19:57:18 +00003913void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3914{
3915 ssl->disable_renegotiation = renegotiation;
3916}
3917
3918void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3919{
3920 ssl->allow_legacy_renegotiation = allow_legacy;
3921}
3922
Paul Bakkera503a632013-08-14 13:48:06 +02003923#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003924int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3925{
3926 ssl->session_tickets = use_tickets;
3927
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003928 if( ssl->endpoint == SSL_IS_CLIENT )
3929 return( 0 );
3930
3931 if( ssl->f_rng == NULL )
3932 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3933
3934 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003935}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003936
3937void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3938{
3939 ssl->ticket_lifetime = lifetime;
3940}
Paul Bakkera503a632013-08-14 13:48:06 +02003941#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003942
Paul Bakker5121ce52009-01-03 21:22:43 +00003943/*
3944 * SSL get accessors
3945 */
Paul Bakker23986e52011-04-24 08:57:21 +00003946size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003947{
3948 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3949}
3950
Paul Bakkerff60ee62010-03-16 21:09:09 +00003951int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003952{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003953 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003954}
3955
Paul Bakkere3166ce2011-01-27 17:40:50 +00003956const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003957{
Paul Bakker926c8e42013-03-06 10:23:34 +01003958 if( ssl == NULL || ssl->session == NULL )
3959 return NULL;
3960
Paul Bakkere3166ce2011-01-27 17:40:50 +00003961 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003962}
3963
Paul Bakker43ca69c2011-01-15 17:35:19 +00003964const char *ssl_get_version( const ssl_context *ssl )
3965{
3966 switch( ssl->minor_ver )
3967 {
3968 case SSL_MINOR_VERSION_0:
3969 return( "SSLv3.0" );
3970
3971 case SSL_MINOR_VERSION_1:
3972 return( "TLSv1.0" );
3973
3974 case SSL_MINOR_VERSION_2:
3975 return( "TLSv1.1" );
3976
Paul Bakker1ef83d62012-04-11 12:09:53 +00003977 case SSL_MINOR_VERSION_3:
3978 return( "TLSv1.2" );
3979
Paul Bakker43ca69c2011-01-15 17:35:19 +00003980 default:
3981 break;
3982 }
3983 return( "unknown" );
3984}
3985
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003986#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003987const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003988{
3989 if( ssl == NULL || ssl->session == NULL )
3990 return NULL;
3991
3992 return ssl->session->peer_cert;
3993}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003994#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003995
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003996int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3997{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003998 if( ssl == NULL ||
3999 dst == NULL ||
4000 ssl->session == NULL ||
4001 ssl->endpoint != SSL_IS_CLIENT )
4002 {
4003 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4004 }
4005
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004006 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004007}
4008
Paul Bakker5121ce52009-01-03 21:22:43 +00004009/*
Paul Bakker1961b702013-01-25 14:49:24 +01004010 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004011 */
Paul Bakker1961b702013-01-25 14:49:24 +01004012int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004013{
Paul Bakker40e46942009-01-03 21:51:57 +00004014 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Paul Bakker40e46942009-01-03 21:51:57 +00004016#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004018 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004019#endif
4020
Paul Bakker40e46942009-01-03 21:51:57 +00004021#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004023 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004024#endif
4025
Paul Bakker1961b702013-01-25 14:49:24 +01004026 return( ret );
4027}
4028
4029/*
4030 * Perform the SSL handshake
4031 */
4032int ssl_handshake( ssl_context *ssl )
4033{
4034 int ret = 0;
4035
4036 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4037
4038 while( ssl->state != SSL_HANDSHAKE_OVER )
4039 {
4040 ret = ssl_handshake_step( ssl );
4041
4042 if( ret != 0 )
4043 break;
4044 }
4045
Paul Bakker5121ce52009-01-03 21:22:43 +00004046 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4047
4048 return( ret );
4049}
4050
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004051#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004052/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004053 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004054 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004055static int ssl_write_hello_request( ssl_context *ssl )
4056{
4057 int ret;
4058
4059 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4060
4061 ssl->out_msglen = 4;
4062 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4063 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4064
4065 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4066 {
4067 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4068 return( ret );
4069 }
4070
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004071 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4072
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004073 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4074
4075 return( 0 );
4076}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004077#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004078
4079/*
4080 * Actually renegotiate current connection, triggered by either:
4081 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004082 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004083 * - receiving any handshake message on server during ssl_read() after the
4084 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004085 * If the handshake doesn't complete due to waiting for I/O, it will continue
4086 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004087 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004088static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004089{
4090 int ret;
4091
4092 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4093
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004094 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4095 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004096
4097 ssl->state = SSL_HELLO_REQUEST;
4098 ssl->renegotiation = SSL_RENEGOTIATION;
4099
Paul Bakker48916f92012-09-16 19:57:18 +00004100 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4101 {
4102 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4103 return( ret );
4104 }
4105
4106 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4107
4108 return( 0 );
4109}
4110
4111/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004112 * Renegotiate current connection on client,
4113 * or request renegotiation on server
4114 */
4115int ssl_renegotiate( ssl_context *ssl )
4116{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004117 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004118
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004119#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004120 /* On server, just send the request */
4121 if( ssl->endpoint == SSL_IS_SERVER )
4122 {
4123 if( ssl->state != SSL_HANDSHAKE_OVER )
4124 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4125
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004126 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004127 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004128#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004129
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004130#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004131 /*
4132 * On client, either start the renegotiation process or,
4133 * if already in progress, continue the handshake
4134 */
4135 if( ssl->renegotiation != SSL_RENEGOTIATION )
4136 {
4137 if( ssl->state != SSL_HANDSHAKE_OVER )
4138 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4139
4140 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4141 {
4142 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4143 return( ret );
4144 }
4145 }
4146 else
4147 {
4148 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4149 {
4150 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4151 return( ret );
4152 }
4153 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004154#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004155
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004156 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004157}
4158
4159/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004160 * Receive application data decrypted from the SSL layer
4161 */
Paul Bakker23986e52011-04-24 08:57:21 +00004162int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004163{
Paul Bakker23986e52011-04-24 08:57:21 +00004164 int ret;
4165 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004166
4167 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4168
4169 if( ssl->state != SSL_HANDSHAKE_OVER )
4170 {
4171 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4172 {
4173 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4174 return( ret );
4175 }
4176 }
4177
4178 if( ssl->in_offt == NULL )
4179 {
4180 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4181 {
Paul Bakker831a7552011-05-18 13:32:51 +00004182 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4183 return( 0 );
4184
Paul Bakker5121ce52009-01-03 21:22:43 +00004185 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4186 return( ret );
4187 }
4188
4189 if( ssl->in_msglen == 0 &&
4190 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4191 {
4192 /*
4193 * OpenSSL sends empty messages to randomize the IV
4194 */
4195 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4196 {
Paul Bakker831a7552011-05-18 13:32:51 +00004197 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4198 return( 0 );
4199
Paul Bakker5121ce52009-01-03 21:22:43 +00004200 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4201 return( ret );
4202 }
4203 }
4204
Paul Bakker48916f92012-09-16 19:57:18 +00004205 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4206 {
4207 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4208
4209 if( ssl->endpoint == SSL_IS_CLIENT &&
4210 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4211 ssl->in_hslen != 4 ) )
4212 {
4213 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4214 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4215 }
4216
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004217 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4218 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004219 ssl->allow_legacy_renegotiation ==
4220 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004221 {
4222 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4223
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004224#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004225 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004226 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004227 /*
4228 * SSLv3 does not have a "no_renegotiation" alert
4229 */
4230 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4231 return( ret );
4232 }
4233 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004234#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004235#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4236 defined(POLARSSL_SSL_PROTO_TLS1_2)
4237 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004238 {
4239 if( ( ret = ssl_send_alert_message( ssl,
4240 SSL_ALERT_LEVEL_WARNING,
4241 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4242 {
4243 return( ret );
4244 }
Paul Bakker48916f92012-09-16 19:57:18 +00004245 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004246 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004247#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4248 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004249 {
4250 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004251 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004252 }
Paul Bakker48916f92012-09-16 19:57:18 +00004253 }
4254 else
4255 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004256 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004257 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004258 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004259 return( ret );
4260 }
4261
4262 return( POLARSSL_ERR_NET_WANT_READ );
4263 }
4264 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004265 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4266 {
4267 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4268 "but not honored by client" ) );
4269 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4270 }
Paul Bakker48916f92012-09-16 19:57:18 +00004271 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004272 {
4273 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004274 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004275 }
4276
4277 ssl->in_offt = ssl->in_msg;
4278 }
4279
4280 n = ( len < ssl->in_msglen )
4281 ? len : ssl->in_msglen;
4282
4283 memcpy( buf, ssl->in_offt, n );
4284 ssl->in_msglen -= n;
4285
4286 if( ssl->in_msglen == 0 )
4287 /* all bytes consumed */
4288 ssl->in_offt = NULL;
4289 else
4290 /* more data available */
4291 ssl->in_offt += n;
4292
4293 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4294
Paul Bakker23986e52011-04-24 08:57:21 +00004295 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004296}
4297
4298/*
4299 * Send application data to be encrypted by the SSL layer
4300 */
Paul Bakker23986e52011-04-24 08:57:21 +00004301int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004302{
Paul Bakker23986e52011-04-24 08:57:21 +00004303 int ret;
4304 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004305 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004306
4307 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4308
4309 if( ssl->state != SSL_HANDSHAKE_OVER )
4310 {
4311 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4312 {
4313 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4314 return( ret );
4315 }
4316 }
4317
Paul Bakker05decb22013-08-15 13:33:48 +02004318#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004319 /*
4320 * Assume mfl_code is correct since it was checked when set
4321 */
4322 max_len = mfl_code_to_length[ssl->mfl_code];
4323
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004324 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004325 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004326 */
4327 if( ssl->session_out != NULL &&
4328 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4329 {
4330 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4331 }
Paul Bakker05decb22013-08-15 13:33:48 +02004332#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004333
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004334 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004335
Paul Bakker5121ce52009-01-03 21:22:43 +00004336 if( ssl->out_left != 0 )
4337 {
4338 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4339 {
4340 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4341 return( ret );
4342 }
4343 }
Paul Bakker887bd502011-06-08 13:10:54 +00004344 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004345 {
Paul Bakker887bd502011-06-08 13:10:54 +00004346 ssl->out_msglen = n;
4347 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4348 memcpy( ssl->out_msg, buf, n );
4349
4350 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4351 {
4352 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4353 return( ret );
4354 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 }
4356
4357 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4358
Paul Bakker23986e52011-04-24 08:57:21 +00004359 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004360}
4361
4362/*
4363 * Notify the peer that the connection is being closed
4364 */
4365int ssl_close_notify( ssl_context *ssl )
4366{
4367 int ret;
4368
4369 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4370
4371 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4372 {
4373 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4374 return( ret );
4375 }
4376
4377 if( ssl->state == SSL_HANDSHAKE_OVER )
4378 {
Paul Bakker48916f92012-09-16 19:57:18 +00004379 if( ( ret = ssl_send_alert_message( ssl,
4380 SSL_ALERT_LEVEL_WARNING,
4381 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004382 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004383 return( ret );
4384 }
4385 }
4386
4387 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4388
4389 return( ret );
4390}
4391
Paul Bakker48916f92012-09-16 19:57:18 +00004392void ssl_transform_free( ssl_transform *transform )
4393{
4394#if defined(POLARSSL_ZLIB_SUPPORT)
4395 deflateEnd( &transform->ctx_deflate );
4396 inflateEnd( &transform->ctx_inflate );
4397#endif
4398
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004399 cipher_free_ctx( &transform->cipher_ctx_enc );
4400 cipher_free_ctx( &transform->cipher_ctx_dec );
4401
Paul Bakker61d113b2013-07-04 11:51:43 +02004402 md_free_ctx( &transform->md_ctx_enc );
4403 md_free_ctx( &transform->md_ctx_dec );
4404
Paul Bakker48916f92012-09-16 19:57:18 +00004405 memset( transform, 0, sizeof( ssl_transform ) );
4406}
4407
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004408#if defined(POLARSSL_X509_CRT_PARSE_C)
4409static void ssl_key_cert_free( ssl_key_cert *key_cert )
4410{
4411 ssl_key_cert *cur = key_cert, *next;
4412
4413 while( cur != NULL )
4414 {
4415 next = cur->next;
4416
4417 if( cur->key_own_alloc )
4418 {
4419 pk_free( cur->key );
4420 polarssl_free( cur->key );
4421 }
4422 polarssl_free( cur );
4423
4424 cur = next;
4425 }
4426}
4427#endif /* POLARSSL_X509_CRT_PARSE_C */
4428
Paul Bakker48916f92012-09-16 19:57:18 +00004429void ssl_handshake_free( ssl_handshake_params *handshake )
4430{
4431#if defined(POLARSSL_DHM_C)
4432 dhm_free( &handshake->dhm_ctx );
4433#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004434#if defined(POLARSSL_ECDH_C)
4435 ecdh_free( &handshake->ecdh_ctx );
4436#endif
4437
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004438#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004439 /* explicit void pointer cast for buggy MS compiler */
4440 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004441#endif
4442
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004443#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4444 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4445 /*
4446 * Free only the linked list wrapper, not the keys themselves
4447 * since the belong to the SNI callback
4448 */
4449 if( handshake->sni_key_cert != NULL )
4450 {
4451 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4452
4453 while( cur != NULL )
4454 {
4455 next = cur->next;
4456 polarssl_free( cur );
4457 cur = next;
4458 }
4459 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004460#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004461
Paul Bakker48916f92012-09-16 19:57:18 +00004462 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4463}
4464
4465void ssl_session_free( ssl_session *session )
4466{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004467#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004468 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004469 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004470 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004471 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004472 }
Paul Bakkered27a042013-04-18 22:46:23 +02004473#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004474
Paul Bakkera503a632013-08-14 13:48:06 +02004475#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004476 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004477#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004478
Paul Bakker0a597072012-09-25 21:55:46 +00004479 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004480}
4481
Paul Bakker5121ce52009-01-03 21:22:43 +00004482/*
4483 * Free an SSL context
4484 */
4485void ssl_free( ssl_context *ssl )
4486{
4487 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4488
Paul Bakker5121ce52009-01-03 21:22:43 +00004489 if( ssl->out_ctr != NULL )
4490 {
4491 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004492 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004493 }
4494
4495 if( ssl->in_ctr != NULL )
4496 {
4497 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004498 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004499 }
4500
Paul Bakker16770332013-10-11 09:59:44 +02004501#if defined(POLARSSL_ZLIB_SUPPORT)
4502 if( ssl->compress_buf != NULL )
4503 {
4504 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4505 polarssl_free( ssl->compress_buf );
4506 }
4507#endif
4508
Paul Bakker40e46942009-01-03 21:51:57 +00004509#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004510 mpi_free( &ssl->dhm_P );
4511 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004512#endif
4513
Paul Bakker48916f92012-09-16 19:57:18 +00004514 if( ssl->transform )
4515 {
4516 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004517 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004518 }
4519
4520 if( ssl->handshake )
4521 {
4522 ssl_handshake_free( ssl->handshake );
4523 ssl_transform_free( ssl->transform_negotiate );
4524 ssl_session_free( ssl->session_negotiate );
4525
Paul Bakker6e339b52013-07-03 13:37:05 +02004526 polarssl_free( ssl->handshake );
4527 polarssl_free( ssl->transform_negotiate );
4528 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004529 }
4530
Paul Bakkerc0463502013-02-14 11:19:38 +01004531 if( ssl->session )
4532 {
4533 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004534 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004535 }
4536
Paul Bakkera503a632013-08-14 13:48:06 +02004537#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004538 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004539#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004540
Paul Bakker0be444a2013-08-27 21:55:01 +02004541#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004542 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004543 {
4544 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004545 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 ssl->hostname_len = 0;
4547 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004548#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004549
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004550#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004551 if( ssl->psk != NULL )
4552 {
4553 memset( ssl->psk, 0, ssl->psk_len );
4554 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4555 polarssl_free( ssl->psk );
4556 polarssl_free( ssl->psk_identity );
4557 ssl->psk_len = 0;
4558 ssl->psk_identity_len = 0;
4559 }
4560#endif
4561
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004562#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004563 ssl_key_cert_free( ssl->key_cert );
4564#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004565
Paul Bakker05ef8352012-05-08 09:17:57 +00004566#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4567 if( ssl_hw_record_finish != NULL )
4568 {
4569 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4570 ssl_hw_record_finish( ssl );
4571 }
4572#endif
4573
Paul Bakker5121ce52009-01-03 21:22:43 +00004574 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004575
Paul Bakker86f04f42013-02-14 11:20:09 +01004576 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004577 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004578}
4579
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004580#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004581/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004582 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004583 */
4584unsigned char ssl_sig_from_pk( pk_context *pk )
4585{
4586#if defined(POLARSSL_RSA_C)
4587 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4588 return( SSL_SIG_RSA );
4589#endif
4590#if defined(POLARSSL_ECDSA_C)
4591 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4592 return( SSL_SIG_ECDSA );
4593#endif
4594 return( SSL_SIG_ANON );
4595}
4596
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004597pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4598{
4599 switch( sig )
4600 {
4601#if defined(POLARSSL_RSA_C)
4602 case SSL_SIG_RSA:
4603 return( POLARSSL_PK_RSA );
4604#endif
4605#if defined(POLARSSL_ECDSA_C)
4606 case SSL_SIG_ECDSA:
4607 return( POLARSSL_PK_ECDSA );
4608#endif
4609 default:
4610 return( POLARSSL_PK_NONE );
4611 }
4612}
Paul Bakker9af723c2014-05-01 13:03:14 +02004613#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004614
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004615/*
4616 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4617 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004618md_type_t ssl_md_alg_from_hash( unsigned char hash )
4619{
4620 switch( hash )
4621 {
4622#if defined(POLARSSL_MD5_C)
4623 case SSL_HASH_MD5:
4624 return( POLARSSL_MD_MD5 );
4625#endif
4626#if defined(POLARSSL_SHA1_C)
4627 case SSL_HASH_SHA1:
4628 return( POLARSSL_MD_SHA1 );
4629#endif
4630#if defined(POLARSSL_SHA256_C)
4631 case SSL_HASH_SHA224:
4632 return( POLARSSL_MD_SHA224 );
4633 case SSL_HASH_SHA256:
4634 return( POLARSSL_MD_SHA256 );
4635#endif
4636#if defined(POLARSSL_SHA512_C)
4637 case SSL_HASH_SHA384:
4638 return( POLARSSL_MD_SHA384 );
4639 case SSL_HASH_SHA512:
4640 return( POLARSSL_MD_SHA512 );
4641#endif
4642 default:
4643 return( POLARSSL_MD_NONE );
4644 }
4645}
4646
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004647#if defined(POLARSSL_SSL_SET_CURVES)
4648/*
4649 * Check is a curve proposed by the peer is in our list.
4650 * Return 1 if we're willing to use it, 0 otherwise.
4651 */
4652int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4653{
4654 const ecp_group_id *gid;
4655
4656 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4657 if( *gid == grp_id )
4658 return( 1 );
4659
4660 return( 0 );
4661}
Paul Bakker9af723c2014-05-01 13:03:14 +02004662#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004663
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004664#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004665int ssl_check_cert_usage( const x509_crt *cert,
4666 const ssl_ciphersuite_t *ciphersuite,
4667 int cert_endpoint )
4668{
4669#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4670 int usage = 0;
4671#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004672#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4673 const char *ext_oid;
4674 size_t ext_len;
4675#endif
4676
4677#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4678 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4679 ((void) cert);
4680 ((void) cert_endpoint);
4681#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004682
4683#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4684 if( cert_endpoint == SSL_IS_SERVER )
4685 {
4686 /* Server part of the key exchange */
4687 switch( ciphersuite->key_exchange )
4688 {
4689 case POLARSSL_KEY_EXCHANGE_RSA:
4690 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4691 usage = KU_KEY_ENCIPHERMENT;
4692 break;
4693
4694 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4695 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4696 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4697 usage = KU_DIGITAL_SIGNATURE;
4698 break;
4699
4700 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4701 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4702 usage = KU_KEY_AGREEMENT;
4703 break;
4704
4705 /* Don't use default: we want warnings when adding new values */
4706 case POLARSSL_KEY_EXCHANGE_NONE:
4707 case POLARSSL_KEY_EXCHANGE_PSK:
4708 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4709 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4710 usage = 0;
4711 }
4712 }
4713 else
4714 {
4715 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4716 usage = KU_DIGITAL_SIGNATURE;
4717 }
4718
4719 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4720 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004721#else
4722 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004723#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4724
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004725#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4726 if( cert_endpoint == SSL_IS_SERVER )
4727 {
4728 ext_oid = OID_SERVER_AUTH;
4729 ext_len = OID_SIZE( OID_SERVER_AUTH );
4730 }
4731 else
4732 {
4733 ext_oid = OID_CLIENT_AUTH;
4734 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4735 }
4736
4737 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4738 return( -1 );
4739#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4740
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004741 return( 0 );
4742}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004743#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004744
4745#endif /* POLARSSL_SSL_TLS_C */