blob: 7967214286f7caba0a87c2607e7c23725067bf7c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker05decb22013-08-15 13:33:48 +020064#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020065/*
66 * Convert max_fragment_length codes to length.
67 * RFC 6066 says:
68 * enum{
69 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
70 * } MaxFragmentLength;
71 * and we add 0 -> extension unused
72 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020073static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020074{
75 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
76 512, /* SSL_MAX_FRAG_LEN_512 */
77 1024, /* SSL_MAX_FRAG_LEN_1024 */
78 2048, /* SSL_MAX_FRAG_LEN_2048 */
79 4096, /* SSL_MAX_FRAG_LEN_4096 */
80};
Paul Bakker05decb22013-08-15 13:33:48 +020081#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020082
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020083static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
84{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020085 ssl_session_free( dst );
86 memcpy( dst, src, sizeof( ssl_session ) );
87
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089 if( src->peer_cert != NULL )
90 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020091 int ret;
92
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020093 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
94 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020095 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
96
Paul Bakkerb6b09562013-09-18 14:17:41 +020097 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098
Paul Bakkerddf26b42013-09-18 13:46:23 +020099 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
100 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101 {
102 polarssl_free( dst->peer_cert );
103 dst->peer_cert = NULL;
104 return( ret );
105 }
106 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200108
Paul Bakkera503a632013-08-14 13:48:06 +0200109#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110 if( src->ticket != NULL )
111 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200112 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
113 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200114 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
115
116 memcpy( dst->ticket, src->ticket, src->ticket_len );
117 }
Paul Bakkera503a632013-08-14 13:48:06 +0200118#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119
120 return( 0 );
121}
122
Paul Bakker05ef8352012-05-08 09:17:57 +0000123#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
124int (*ssl_hw_record_init)(ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200125 const unsigned char *key_enc, const unsigned char *key_dec,
126 size_t keylen,
127 const unsigned char *iv_enc, const unsigned char *iv_dec,
128 size_t ivlen,
129 const unsigned char *mac_enc, const unsigned char *mac_dec,
130 size_t maclen) = NULL;
Paul Bakker07eb38b2012-12-19 14:42:06 +0100131int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000132int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
133int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
134int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
135int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200136#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000137
Paul Bakker5121ce52009-01-03 21:22:43 +0000138/*
139 * Key material generation
140 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200141#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200142static int ssl3_prf( const unsigned char *secret, size_t slen,
143 const char *label,
144 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000145 unsigned char *dstbuf, size_t dlen )
146{
147 size_t i;
148 md5_context md5;
149 sha1_context sha1;
150 unsigned char padding[16];
151 unsigned char sha1sum[20];
152 ((void)label);
153
154 /*
155 * SSLv3:
156 * block =
157 * MD5( secret + SHA1( 'A' + secret + random ) ) +
158 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
159 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
160 * ...
161 */
162 for( i = 0; i < dlen / 16; i++ )
163 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200164 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000165
166 sha1_starts( &sha1 );
167 sha1_update( &sha1, padding, 1 + i );
168 sha1_update( &sha1, secret, slen );
169 sha1_update( &sha1, random, rlen );
170 sha1_finish( &sha1, sha1sum );
171
172 md5_starts( &md5 );
173 md5_update( &md5, secret, slen );
174 md5_update( &md5, sha1sum, 20 );
175 md5_finish( &md5, dstbuf + i * 16 );
176 }
177
178 memset( &md5, 0, sizeof( md5 ) );
179 memset( &sha1, 0, sizeof( sha1 ) );
180
181 memset( padding, 0, sizeof( padding ) );
182 memset( sha1sum, 0, sizeof( sha1sum ) );
183
184 return( 0 );
185}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200186#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000187
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200188#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200189static int tls1_prf( const unsigned char *secret, size_t slen,
190 const char *label,
191 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000192 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
Paul Bakker23986e52011-04-24 08:57:21 +0000194 size_t nb, hs;
195 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200196 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 unsigned char tmp[128];
198 unsigned char h_i[20];
199
200 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000201 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 hs = ( slen + 1 ) / 2;
204 S1 = secret;
205 S2 = secret + slen - hs;
206
207 nb = strlen( label );
208 memcpy( tmp + 20, label, nb );
209 memcpy( tmp + 20 + nb, random, rlen );
210 nb += rlen;
211
212 /*
213 * First compute P_md5(secret,label+random)[0..dlen]
214 */
215 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
216
217 for( i = 0; i < dlen; i += 16 )
218 {
219 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
220 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
221
222 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
223
224 for( j = 0; j < k; j++ )
225 dstbuf[i + j] = h_i[j];
226 }
227
228 /*
229 * XOR out with P_sha1(secret,label+random)[0..dlen]
230 */
231 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
232
233 for( i = 0; i < dlen; i += 20 )
234 {
235 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
236 sha1_hmac( S2, hs, tmp, 20, tmp );
237
238 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
239
240 for( j = 0; j < k; j++ )
241 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
242 }
243
244 memset( tmp, 0, sizeof( tmp ) );
245 memset( h_i, 0, sizeof( h_i ) );
246
247 return( 0 );
248}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200249#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200251#if defined(POLARSSL_SSL_PROTO_TLS1_2)
252#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200253static int tls_prf_sha256( const unsigned char *secret, size_t slen,
254 const char *label,
255 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000256 unsigned char *dstbuf, size_t dlen )
257{
258 size_t nb;
259 size_t i, j, k;
260 unsigned char tmp[128];
261 unsigned char h_i[32];
262
263 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
264 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
265
266 nb = strlen( label );
267 memcpy( tmp + 32, label, nb );
268 memcpy( tmp + 32 + nb, random, rlen );
269 nb += rlen;
270
271 /*
272 * Compute P_<hash>(secret, label + random)[0..dlen]
273 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200274 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000275
276 for( i = 0; i < dlen; i += 32 )
277 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200278 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
279 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
282
283 for( j = 0; j < k; j++ )
284 dstbuf[i + j] = h_i[j];
285 }
286
287 memset( tmp, 0, sizeof( tmp ) );
288 memset( h_i, 0, sizeof( h_i ) );
289
290 return( 0 );
291}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200292#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000293
Paul Bakker9e36f042013-06-30 14:34:05 +0200294#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200295static int tls_prf_sha384( const unsigned char *secret, size_t slen,
296 const char *label,
297 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000298 unsigned char *dstbuf, size_t dlen )
299{
300 size_t nb;
301 size_t i, j, k;
302 unsigned char tmp[128];
303 unsigned char h_i[48];
304
305 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
306 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
307
308 nb = strlen( label );
309 memcpy( tmp + 48, label, nb );
310 memcpy( tmp + 48 + nb, random, rlen );
311 nb += rlen;
312
313 /*
314 * Compute P_<hash>(secret, label + random)[0..dlen]
315 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200316 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000317
318 for( i = 0; i < dlen; i += 48 )
319 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200320 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
321 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
324
325 for( j = 0; j < k; j++ )
326 dstbuf[i + j] = h_i[j];
327 }
328
329 memset( tmp, 0, sizeof( tmp ) );
330 memset( h_i, 0, sizeof( h_i ) );
331
332 return( 0 );
333}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200334#endif /* POLARSSL_SHA512_C */
335#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200337static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338
339#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
340 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200341static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif
Paul Bakker380da532012-04-18 16:10:25 +0000343
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200344#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000345static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000346static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
348
349#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
350static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000351static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1_2)
355#if defined(POLARSSL_SHA256_C)
356static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
357static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000358static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200359#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100360
Paul Bakker9e36f042013-06-30 14:34:05 +0200361#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200362static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100363static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000364static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100365#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200366#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000367
Paul Bakker5121ce52009-01-03 21:22:43 +0000368int ssl_derive_keys( ssl_context *ssl )
369{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200370 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 unsigned char keyblk[256];
373 unsigned char *key1;
374 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100375 unsigned char *mac_enc;
376 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200377 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100378 const cipher_info_t *cipher_info;
379 const md_info_t *md_info;
380
Paul Bakker48916f92012-09-16 19:57:18 +0000381 ssl_session *session = ssl->session_negotiate;
382 ssl_transform *transform = ssl->transform_negotiate;
383 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
386
Paul Bakker68884e32013-01-07 18:20:04 +0100387 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
388 if( cipher_info == NULL )
389 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200390 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100391 transform->ciphersuite_info->cipher ) );
392 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
393 }
394
395 md_info = md_info_from_type( transform->ciphersuite_info->mac );
396 if( md_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->mac ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000405 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200406#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000407 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408 {
Paul Bakker48916f92012-09-16 19:57:18 +0000409 handshake->tls_prf = ssl3_prf;
410 handshake->calc_verify = ssl_calc_verify_ssl;
411 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200413 else
414#endif
415#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
416 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 {
Paul Bakker48916f92012-09-16 19:57:18 +0000418 handshake->tls_prf = tls1_prf;
419 handshake->calc_verify = ssl_calc_verify_tls;
420 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000421 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200422 else
423#endif
424#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200425#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
427 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = tls_prf_sha384;
430 handshake->calc_verify = ssl_calc_verify_tls_sha384;
431 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000433 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434#endif
435#if defined(POLARSSL_SHA256_C)
436 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 {
Paul Bakker48916f92012-09-16 19:57:18 +0000438 handshake->tls_prf = tls_prf_sha256;
439 handshake->calc_verify = ssl_calc_verify_tls_sha256;
440 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000441 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442 else
443#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200444#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200445 {
446 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
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" ) );
612 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
613 }
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" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200631 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
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" ) );
926 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
927 }
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" ) );
1034 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1035 }
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 {
1073 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1074 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001075 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 }
Paul Bakker68884e32013-01-07 18:20:04 +01001078 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001079#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001080#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1081 if( mode == POLARSSL_MODE_GCM ||
1082 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001083 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001084 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001085 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001086 unsigned char *enc_msg;
1087 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001088 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1089 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001090
Paul Bakkerca4ab492012-04-18 14:23:57 +00001091 memcpy( add_data, ssl->out_ctr, 8 );
1092 add_data[8] = ssl->out_msgtype;
1093 add_data[9] = ssl->major_ver;
1094 add_data[10] = ssl->minor_ver;
1095 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1096 add_data[12] = ssl->out_msglen & 0xFF;
1097
1098 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1099 add_data, 13 );
1100
Paul Bakker68884e32013-01-07 18:20:04 +01001101 /*
1102 * Generate IV
1103 */
1104 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001105 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1106 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001107 if( ret != 0 )
1108 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001109
Paul Bakker68884e32013-01-07 18:20:04 +01001110 memcpy( ssl->out_iv,
1111 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1112 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001113
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001114 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001115 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001116
Paul Bakker68884e32013-01-07 18:20:04 +01001117 /*
1118 * Fix pointer positions and message length with added IV
1119 */
1120 enc_msg = ssl->out_msg;
1121 enc_msglen = ssl->out_msglen;
1122 ssl->out_msglen += ssl->transform_out->ivlen -
1123 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001124
Paul Bakker68884e32013-01-07 18:20:04 +01001125 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1126 "including %d bytes of padding",
1127 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001128
Paul Bakker68884e32013-01-07 18:20:04 +01001129 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1130 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131
Paul Bakker68884e32013-01-07 18:20:04 +01001132 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001133 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001134 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001135 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1136 ssl->transform_out->iv_enc,
1137 ssl->transform_out->ivlen,
1138 add_data, 13,
1139 enc_msg, enc_msglen,
1140 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001141 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001142 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001143 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001144 return( ret );
1145 }
1146
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001147 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001148 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001149 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1150 enc_msglen, olen ) );
1151 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001152 }
1153
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001154 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001155
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001156 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001157 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001159#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001160#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1161 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001162 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001164 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001165 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001166 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001167
Paul Bakker48916f92012-09-16 19:57:18 +00001168 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1169 ssl->transform_out->ivlen;
1170 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 padlen = 0;
1172
1173 for( i = 0; i <= padlen; i++ )
1174 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1175
1176 ssl->out_msglen += padlen + 1;
1177
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001178 enc_msglen = ssl->out_msglen;
1179 enc_msg = ssl->out_msg;
1180
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001181#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001182 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001183 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1184 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001185 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001186 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001187 {
1188 /*
1189 * Generate IV
1190 */
Paul Bakker48916f92012-09-16 19:57:18 +00001191 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1192 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001193 if( ret != 0 )
1194 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001195
Paul Bakker92be97b2013-01-02 17:30:03 +01001196 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001197 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001198
1199 /*
1200 * Fix pointer positions and message length with added IV
1201 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001202 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001203 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001204 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001205 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001206#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001207
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001209 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001210 ssl->out_msglen, ssl->transform_out->ivlen,
1211 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001212
1213 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001214 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001216 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001217 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001218 ssl->transform_out->ivlen,
1219 enc_msg, enc_msglen,
1220 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001221 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001222 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001223 return( ret );
1224 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001225
Paul Bakkercca5b812013-08-31 17:40:26 +02001226 if( enc_msglen != olen )
1227 {
1228 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1229 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001230 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001231 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001232
1233#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001234 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1235 {
1236 /*
1237 * Save IV in SSL3 and TLS1
1238 */
1239 memcpy( ssl->transform_out->iv_enc,
1240 ssl->transform_out->cipher_ctx_enc.iv,
1241 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001243#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001245 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001246#endif /* POLARSSL_CIPHER_MODE_CBC &&
1247 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001248 {
1249 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1250 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1251 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Paul Bakkerca4ab492012-04-18 14:23:57 +00001253 for( i = 8; i > 0; i-- )
1254 if( ++ssl->out_ctr[i - 1] != 0 )
1255 break;
1256
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001257 /* The loops goes to its end iff the counter is wrapping */
1258 if( i == 0 )
1259 {
1260 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1261 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1262 }
1263
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1265
1266 return( 0 );
1267}
1268
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001269#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001270
Paul Bakker5121ce52009-01-03 21:22:43 +00001271static int ssl_decrypt_buf( ssl_context *ssl )
1272{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001273 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001274 const cipher_mode_t mode = cipher_get_cipher_mode(
1275 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001276#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1277 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1278 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1279 size_t padlen = 0, correct = 1;
1280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
1282 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1283
Paul Bakker48916f92012-09-16 19:57:18 +00001284 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 {
1286 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001287 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001288 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
1290
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001291#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001292 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001293 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001294 int ret;
1295 size_t olen = 0;
1296
Paul Bakker68884e32013-01-07 18:20:04 +01001297 padlen = 0;
1298
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001299 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001300 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001301 ssl->transform_in->ivlen,
1302 ssl->in_msg, ssl->in_msglen,
1303 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001304 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001305 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001306 return( ret );
1307 }
1308
1309 if( ssl->in_msglen != olen )
1310 {
1311 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001312 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 }
Paul Bakker68884e32013-01-07 18:20:04 +01001315 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001316#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001317#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1318 if( mode == POLARSSL_MODE_GCM ||
1319 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001320 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001321 int ret;
1322 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001323 unsigned char *dec_msg;
1324 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001325 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001326 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1327 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001328
Paul Bakker68884e32013-01-07 18:20:04 +01001329 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1330 ssl->transform_in->fixed_ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001331 dec_msglen -= taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001332 dec_msg = ssl->in_msg;
1333 dec_msg_result = ssl->in_msg;
1334 ssl->in_msglen = dec_msglen;
1335
1336 memcpy( add_data, ssl->in_ctr, 8 );
1337 add_data[8] = ssl->in_msgtype;
1338 add_data[9] = ssl->major_ver;
1339 add_data[10] = ssl->minor_ver;
1340 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1341 add_data[12] = ssl->in_msglen & 0xFF;
1342
1343 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1344 add_data, 13 );
1345
1346 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1347 ssl->in_iv,
1348 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1349
1350 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1351 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001352 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001353
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001354 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001355 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001356 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001357 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1358 ssl->transform_in->iv_dec,
1359 ssl->transform_in->ivlen,
1360 add_data, 13,
1361 dec_msg, dec_msglen,
1362 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001363 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001364 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001365 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1366
1367 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1368 return( POLARSSL_ERR_SSL_INVALID_MAC );
1369
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001370 return( ret );
1371 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001372
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001373 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001374 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001375 SSL_DEBUG_MSG( 1, ( "total decrypted length incorrect %d %d",
1376 dec_msglen, olen ) );
1377 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001378 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001379 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001381#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001382#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1383 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001384 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 {
Paul Bakker45829992013-01-03 14:52:21 +01001386 /*
1387 * Decrypt and check the padding
1388 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001389 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001390 unsigned char *dec_msg;
1391 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001392 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001393 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001394 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001395
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 /*
Paul Bakker45829992013-01-03 14:52:21 +01001397 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 */
Paul Bakker48916f92012-09-16 19:57:18 +00001399 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 {
1401 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001402 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001403 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 }
1405
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001406#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001407 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1408 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001409#endif
Paul Bakker45829992013-01-03 14:52:21 +01001410
1411 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1412 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1413 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001414 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1415 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1416 ssl->transform_in->ivlen,
1417 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001418 return( POLARSSL_ERR_SSL_INVALID_MAC );
1419 }
1420
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001421 dec_msglen = ssl->in_msglen;
1422 dec_msg = ssl->in_msg;
1423 dec_msg_result = ssl->in_msg;
1424
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001425#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001426 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001427 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001428 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001429 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001430 {
Paul Bakker48916f92012-09-16 19:57:18 +00001431 dec_msglen -= ssl->transform_in->ivlen;
1432 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001433
Paul Bakker48916f92012-09-16 19:57:18 +00001434 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001435 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001436 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001437#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001438
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001439 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001440 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001441 ssl->transform_in->ivlen,
1442 dec_msg, dec_msglen,
1443 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001444 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001445 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001446 return( ret );
1447 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001448
Paul Bakkercca5b812013-08-31 17:40:26 +02001449 if( dec_msglen != olen )
1450 {
1451 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001453 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001454
1455#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001456 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1457 {
1458 /*
1459 * Save IV in SSL3 and TLS1
1460 */
1461 memcpy( ssl->transform_in->iv_dec,
1462 ssl->transform_in->cipher_ctx_dec.iv,
1463 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001465#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
1467 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001468
1469 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1470 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001471#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001472 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1473 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001474#endif
Paul Bakker45829992013-01-03 14:52:21 +01001475 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001476 correct = 0;
1477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001478
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001479#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1481 {
Paul Bakker48916f92012-09-16 19:57:18 +00001482 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001484#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1486 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001487 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001488#endif
Paul Bakker45829992013-01-03 14:52:21 +01001489 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 }
1491 }
1492 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001493#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001494#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1495 defined(POLARSSL_SSL_PROTO_TLS1_2)
1496 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 {
1498 /*
Paul Bakker45829992013-01-03 14:52:21 +01001499 * TLSv1+: always check the padding up to the first failure
1500 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001502 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001503 size_t padding_idx = ssl->in_msglen - padlen - 1;
1504
Paul Bakker956c9e02013-12-19 14:42:28 +01001505 /*
1506 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001507 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001508 *
Paul Bakker61885c72014-04-25 12:59:03 +02001509 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1510 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001511 *
1512 * In both cases we reset padding_idx to a safe value (0) to
1513 * prevent out-of-buffer reads.
1514 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001515 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001516 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1517 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001518
1519 padding_idx *= correct;
1520
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001521 for( i = 1; i <= 256; i++ )
1522 {
1523 real_count &= ( i <= padlen );
1524 pad_count += real_count *
1525 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1526 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001527
1528 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001529
Paul Bakkerd66f0702013-01-31 16:57:45 +01001530#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001531 if( padlen > 0 && correct == 0)
1532 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001533#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001534 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001536 else
1537#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1538 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001539 {
1540 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001541 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001544 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001545#endif /* POLARSSL_CIPHER_MODE_CBC &&
1546 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001547 {
1548 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1549 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1550 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001551
1552 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1553 ssl->in_msg, ssl->in_msglen );
1554
1555 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001556 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001558#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1559 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1560 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001561 if( mode != POLARSSL_MODE_GCM &&
1562 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001563 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001564 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1565
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001566 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001568 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1569 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001570
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001571 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001573#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001574 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1575 {
1576 ssl_mac( &ssl->transform_in->md_ctx_dec,
1577 ssl->transform_in->mac_dec,
1578 ssl->in_msg, ssl->in_msglen,
1579 ssl->in_ctr, ssl->in_msgtype );
1580 }
1581 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001582#endif /* POLARSSL_SSL_PROTO_SSL3 */
1583#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001584 defined(POLARSSL_SSL_PROTO_TLS1_2)
1585 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1586 {
1587 /*
1588 * Process MAC and always update for padlen afterwards to make
1589 * total time independent of padlen
1590 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001591 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001592 *
1593 * Known timing attacks:
1594 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1595 *
1596 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1597 * correctly. (We round down instead of up, so -56 is the correct
1598 * value for our calculations instead of -55)
1599 */
1600 size_t j, extra_run = 0;
1601 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1602 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001603
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001604 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001605
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001606 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1607 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1608 ssl->in_msglen );
1609 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1610 ssl->in_msg + ssl->in_msglen );
1611 for( j = 0; j < extra_run; j++ )
1612 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001613
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001614 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1615 }
1616 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001617#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001618 POLARSSL_SSL_PROTO_TLS1_2 */
1619 {
1620 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1621 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1622 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001624 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1625 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1626 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001628 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001629 ssl->transform_in->maclen ) != 0 )
1630 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001631#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001632 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001633#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001634 correct = 0;
1635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001637 /*
1638 * Finally check the correct flag
1639 */
1640 if( correct == 0 )
1641 return( POLARSSL_ERR_SSL_INVALID_MAC );
1642 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001643#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
1645 if( ssl->in_msglen == 0 )
1646 {
1647 ssl->nb_zero++;
1648
1649 /*
1650 * Three or more empty messages may be a DoS attack
1651 * (excessive CPU consumption).
1652 */
1653 if( ssl->nb_zero > 3 )
1654 {
1655 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1656 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001657 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658 }
1659 }
1660 else
1661 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001662
Paul Bakker23986e52011-04-24 08:57:21 +00001663 for( i = 8; i > 0; i-- )
1664 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 break;
1666
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001667 /* The loops goes to its end iff the counter is wrapping */
1668 if( i == 0 )
1669 {
1670 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1671 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1672 }
1673
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1675
1676 return( 0 );
1677}
1678
Paul Bakker2770fbd2012-07-03 13:30:23 +00001679#if defined(POLARSSL_ZLIB_SUPPORT)
1680/*
1681 * Compression/decompression functions
1682 */
1683static int ssl_compress_buf( ssl_context *ssl )
1684{
1685 int ret;
1686 unsigned char *msg_post = ssl->out_msg;
1687 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001688 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001689
1690 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1691
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001692 if( len_pre == 0 )
1693 return( 0 );
1694
Paul Bakker2770fbd2012-07-03 13:30:23 +00001695 memcpy( msg_pre, ssl->out_msg, len_pre );
1696
1697 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1698 ssl->out_msglen ) );
1699
1700 SSL_DEBUG_BUF( 4, "before compression: output payload",
1701 ssl->out_msg, ssl->out_msglen );
1702
Paul Bakker48916f92012-09-16 19:57:18 +00001703 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1704 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1705 ssl->transform_out->ctx_deflate.next_out = msg_post;
1706 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001707
Paul Bakker48916f92012-09-16 19:57:18 +00001708 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001709 if( ret != Z_OK )
1710 {
1711 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1712 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1713 }
1714
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001715 ssl->out_msglen = SSL_BUFFER_LEN -
1716 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001717
Paul Bakker2770fbd2012-07-03 13:30:23 +00001718 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1719 ssl->out_msglen ) );
1720
1721 SSL_DEBUG_BUF( 4, "after compression: output payload",
1722 ssl->out_msg, ssl->out_msglen );
1723
1724 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1725
1726 return( 0 );
1727}
1728
1729static int ssl_decompress_buf( ssl_context *ssl )
1730{
1731 int ret;
1732 unsigned char *msg_post = ssl->in_msg;
1733 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001734 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001735
1736 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1737
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001738 if( len_pre == 0 )
1739 return( 0 );
1740
Paul Bakker2770fbd2012-07-03 13:30:23 +00001741 memcpy( msg_pre, ssl->in_msg, len_pre );
1742
1743 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1744 ssl->in_msglen ) );
1745
1746 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1747 ssl->in_msg, ssl->in_msglen );
1748
Paul Bakker48916f92012-09-16 19:57:18 +00001749 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1750 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1751 ssl->transform_in->ctx_inflate.next_out = msg_post;
1752 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001753
Paul Bakker48916f92012-09-16 19:57:18 +00001754 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001755 if( ret != Z_OK )
1756 {
1757 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1758 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1759 }
1760
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001761 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1762 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001763
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1765 ssl->in_msglen ) );
1766
1767 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1768 ssl->in_msg, ssl->in_msglen );
1769
1770 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1771
1772 return( 0 );
1773}
1774#endif /* POLARSSL_ZLIB_SUPPORT */
1775
Paul Bakker5121ce52009-01-03 21:22:43 +00001776/*
1777 * Fill the input message buffer
1778 */
Paul Bakker23986e52011-04-24 08:57:21 +00001779int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001780{
Paul Bakker23986e52011-04-24 08:57:21 +00001781 int ret;
1782 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
1784 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1785
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001786 if( nb_want > SSL_BUFFER_LEN - 8 )
1787 {
1788 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1789 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1790 }
1791
Paul Bakker5121ce52009-01-03 21:22:43 +00001792 while( ssl->in_left < nb_want )
1793 {
1794 len = nb_want - ssl->in_left;
1795 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1796
1797 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1798 ssl->in_left, nb_want ) );
1799 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1800
Paul Bakker831a7552011-05-18 13:32:51 +00001801 if( ret == 0 )
1802 return( POLARSSL_ERR_SSL_CONN_EOF );
1803
Paul Bakker5121ce52009-01-03 21:22:43 +00001804 if( ret < 0 )
1805 return( ret );
1806
1807 ssl->in_left += ret;
1808 }
1809
1810 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1811
1812 return( 0 );
1813}
1814
1815/*
1816 * Flush any data not yet written
1817 */
1818int ssl_flush_output( ssl_context *ssl )
1819{
1820 int ret;
1821 unsigned char *buf;
1822
1823 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1824
1825 while( ssl->out_left > 0 )
1826 {
1827 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1828 5 + ssl->out_msglen, ssl->out_left ) );
1829
Paul Bakker5bd42292012-12-19 14:40:42 +01001830 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001832
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1834
1835 if( ret <= 0 )
1836 return( ret );
1837
1838 ssl->out_left -= ret;
1839 }
1840
1841 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1842
1843 return( 0 );
1844}
1845
1846/*
1847 * Record layer functions
1848 */
1849int ssl_write_record( ssl_context *ssl )
1850{
Paul Bakker05ef8352012-05-08 09:17:57 +00001851 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001852 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
1854 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1855
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1857 {
1858 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1859 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1860 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1861
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001862 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1863 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 }
1865
Paul Bakker2770fbd2012-07-03 13:30:23 +00001866#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001867 if( ssl->transform_out != NULL &&
1868 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001869 {
1870 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1871 {
1872 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1873 return( ret );
1874 }
1875
1876 len = ssl->out_msglen;
1877 }
1878#endif /*POLARSSL_ZLIB_SUPPORT */
1879
Paul Bakker05ef8352012-05-08 09:17:57 +00001880#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1881 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001883 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Paul Bakker05ef8352012-05-08 09:17:57 +00001885 ret = ssl_hw_record_write( ssl );
1886 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1887 {
1888 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1889 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1890 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001891
1892 if( ret == 0 )
1893 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001894 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001895#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001896 if( !done )
1897 {
1898 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1899 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1900 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1902 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001903
Paul Bakker48916f92012-09-16 19:57:18 +00001904 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001905 {
1906 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1907 {
1908 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1909 return( ret );
1910 }
1911
1912 len = ssl->out_msglen;
1913 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1914 ssl->out_hdr[4] = (unsigned char)( len );
1915 }
1916
1917 ssl->out_left = 5 + ssl->out_msglen;
1918
1919 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1920 "version = [%d:%d], msglen = %d",
1921 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1922 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1923
1924 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001925 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 }
1927
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1929 {
1930 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1931 return( ret );
1932 }
1933
1934 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1935
1936 return( 0 );
1937}
1938
1939int ssl_read_record( ssl_context *ssl )
1940{
Paul Bakker05ef8352012-05-08 09:17:57 +00001941 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
1943 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1944
Paul Bakker68884e32013-01-07 18:20:04 +01001945 SSL_DEBUG_BUF( 4, "input record from network",
1946 ssl->in_hdr, 5 + ssl->in_msglen );
1947
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 if( ssl->in_hslen != 0 &&
1949 ssl->in_hslen < ssl->in_msglen )
1950 {
1951 /*
1952 * Get next Handshake message in the current record
1953 */
1954 ssl->in_msglen -= ssl->in_hslen;
1955
Paul Bakker8934a982011-08-05 11:11:53 +00001956 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001957 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
1959 ssl->in_hslen = 4;
1960 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1961
1962 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1963 " %d, type = %d, hslen = %d",
1964 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1965
1966 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1967 {
1968 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001969 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 }
1971
1972 if( ssl->in_msglen < ssl->in_hslen )
1973 {
1974 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001975 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 }
1977
Paul Bakker4224bc02014-04-08 14:36:50 +02001978 if( ssl->state != SSL_HANDSHAKE_OVER )
1979 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001980
1981 return( 0 );
1982 }
1983
1984 ssl->in_hslen = 0;
1985
1986 /*
1987 * Read the record header and validate it
1988 */
1989 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1990 {
1991 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1992 return( ret );
1993 }
1994
1995 ssl->in_msgtype = ssl->in_hdr[0];
1996 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
1997
1998 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
1999 "version = [%d:%d], msglen = %d",
2000 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2001 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2002
2003 if( ssl->in_hdr[1] != ssl->major_ver )
2004 {
2005 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002006 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 }
2008
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002009 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 {
2011 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002012 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 }
2014
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002015 /* Sanity check (outer boundaries) */
2016 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2017 {
2018 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2019 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2020 }
2021
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002023 * Make sure the message length is acceptable for the current transform
2024 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 */
Paul Bakker48916f92012-09-16 19:57:18 +00002026 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002028 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 {
2030 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002031 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 }
2033 }
2034 else
2035 {
Paul Bakker48916f92012-09-16 19:57:18 +00002036 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 {
2038 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002039 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 }
2041
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002042#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002044 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 {
2046 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002047 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002050
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002051#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2052 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 /*
2054 * TLS encrypted messages can have up to 256 bytes of padding
2055 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002056 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002057 ssl->in_msglen > ssl->transform_in->minlen +
2058 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 {
2060 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002061 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002063#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 }
2065
2066 /*
2067 * Read and optionally decrypt the message contents
2068 */
2069 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2070 {
2071 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2072 return( ret );
2073 }
2074
2075 SSL_DEBUG_BUF( 4, "input record from network",
2076 ssl->in_hdr, 5 + ssl->in_msglen );
2077
Paul Bakker05ef8352012-05-08 09:17:57 +00002078#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2079 if( ssl_hw_record_read != NULL)
2080 {
2081 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2082
2083 ret = ssl_hw_record_read( ssl );
2084 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2085 {
2086 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2087 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2088 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002089
2090 if( ret == 0 )
2091 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002092 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002093#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002094 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 {
2096 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2097 {
Paul Bakker40865c82013-01-31 17:13:13 +01002098#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2099 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2100 {
2101 ssl_send_alert_message( ssl,
2102 SSL_ALERT_LEVEL_FATAL,
2103 SSL_ALERT_MSG_BAD_RECORD_MAC );
2104 }
2105#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2107 return( ret );
2108 }
2109
2110 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2111 ssl->in_msg, ssl->in_msglen );
2112
2113 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2114 {
2115 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002116 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 }
2118 }
2119
Paul Bakker2770fbd2012-07-03 13:30:23 +00002120#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002121 if( ssl->transform_in != NULL &&
2122 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002123 {
2124 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2125 {
2126 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2127 return( ret );
2128 }
2129
2130 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2131 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2132 }
2133#endif /* POLARSSL_ZLIB_SUPPORT */
2134
Paul Bakker0a925182012-04-16 06:46:41 +00002135 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2136 ssl->in_msgtype != SSL_MSG_ALERT &&
2137 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2138 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2139 {
2140 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2141
Paul Bakker48916f92012-09-16 19:57:18 +00002142 if( ( ret = ssl_send_alert_message( ssl,
2143 SSL_ALERT_LEVEL_FATAL,
2144 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002145 {
2146 return( ret );
2147 }
2148
2149 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2150 }
2151
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2153 {
2154 ssl->in_hslen = 4;
2155 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2156
2157 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2158 " %d, type = %d, hslen = %d",
2159 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2160
2161 /*
2162 * Additional checks to validate the handshake header
2163 */
2164 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2165 {
2166 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002167 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 }
2169
2170 if( ssl->in_msglen < ssl->in_hslen )
2171 {
2172 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002173 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 }
2175
Paul Bakker48916f92012-09-16 19:57:18 +00002176 if( ssl->state != SSL_HANDSHAKE_OVER )
2177 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 }
2179
2180 if( ssl->in_msgtype == SSL_MSG_ALERT )
2181 {
2182 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2183 ssl->in_msg[0], ssl->in_msg[1] ) );
2184
2185 /*
2186 * Ignore non-fatal alerts, except close_notify
2187 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002188 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002190 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2191 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002192 /**
2193 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2194 * error identifier.
2195 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002196 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 }
2198
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002199 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2200 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 {
2202 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002203 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 }
2205 }
2206
2207 ssl->in_left = 0;
2208
2209 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2210
2211 return( 0 );
2212}
2213
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002214int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2215{
2216 int ret;
2217
2218 if( ( ret = ssl_send_alert_message( ssl,
2219 SSL_ALERT_LEVEL_FATAL,
2220 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2221 {
2222 return( ret );
2223 }
2224
2225 return( 0 );
2226}
2227
Paul Bakker0a925182012-04-16 06:46:41 +00002228int ssl_send_alert_message( ssl_context *ssl,
2229 unsigned char level,
2230 unsigned char message )
2231{
2232 int ret;
2233
2234 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2235
2236 ssl->out_msgtype = SSL_MSG_ALERT;
2237 ssl->out_msglen = 2;
2238 ssl->out_msg[0] = level;
2239 ssl->out_msg[1] = message;
2240
2241 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2242 {
2243 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2244 return( ret );
2245 }
2246
2247 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2248
2249 return( 0 );
2250}
2251
Paul Bakker5121ce52009-01-03 21:22:43 +00002252/*
2253 * Handshake functions
2254 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002255#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2256 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2257 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2258 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2259 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2260 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2261 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002262int ssl_write_certificate( ssl_context *ssl )
2263{
Paul Bakkered27a042013-04-18 22:46:23 +02002264 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002265 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002266
2267 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2268
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002269 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002270 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2271 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002272 {
2273 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2274 ssl->state++;
2275 return( 0 );
2276 }
2277
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002278 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002279 return( ret );
2280}
2281
2282int ssl_parse_certificate( ssl_context *ssl )
2283{
2284 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2285 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2286
2287 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2288
2289 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002290 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2291 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002292 {
2293 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2294 ssl->state++;
2295 return( 0 );
2296 }
2297
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002298 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002299 return( ret );
2300}
2301#else
2302int ssl_write_certificate( ssl_context *ssl )
2303{
2304 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2305 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002306 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2308
2309 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2310
2311 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002312 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2313 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002314 {
2315 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2316 ssl->state++;
2317 return( 0 );
2318 }
2319
Paul Bakker5121ce52009-01-03 21:22:43 +00002320 if( ssl->endpoint == SSL_IS_CLIENT )
2321 {
2322 if( ssl->client_auth == 0 )
2323 {
2324 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2325 ssl->state++;
2326 return( 0 );
2327 }
2328
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002329#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 /*
2331 * If using SSLv3 and got no cert, send an Alert message
2332 * (otherwise an empty Certificate message will be sent).
2333 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002334 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002335 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2336 {
2337 ssl->out_msglen = 2;
2338 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002339 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2340 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002341
2342 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2343 goto write_msg;
2344 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002345#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 }
2347 else /* SSL_IS_SERVER */
2348 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002349 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 {
2351 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002352 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 }
2354 }
2355
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002356 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357
2358 /*
2359 * 0 . 0 handshake type
2360 * 1 . 3 handshake length
2361 * 4 . 6 length of all certs
2362 * 7 . 9 length of cert. 1
2363 * 10 . n-1 peer certificate
2364 * n . n+2 length of cert. 2
2365 * n+3 . ... upper level cert, etc.
2366 */
2367 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002368 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
Paul Bakker29087132010-03-21 21:03:34 +00002370 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 {
2372 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002373 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 {
2375 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2376 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002377 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 }
2379
2380 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2381 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2382 ssl->out_msg[i + 2] = (unsigned char)( n );
2383
2384 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2385 i += n; crt = crt->next;
2386 }
2387
2388 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2389 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2390 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2391
2392 ssl->out_msglen = i;
2393 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2394 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2395
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002396#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002397write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002398#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
2400 ssl->state++;
2401
2402 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2403 {
2404 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2405 return( ret );
2406 }
2407
2408 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2409
Paul Bakkered27a042013-04-18 22:46:23 +02002410 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002411}
2412
2413int ssl_parse_certificate( ssl_context *ssl )
2414{
Paul Bakkered27a042013-04-18 22:46:23 +02002415 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002416 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002417 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002418
2419 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2420
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002421 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002422 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2423 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002424 {
2425 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2426 ssl->state++;
2427 return( 0 );
2428 }
2429
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002431 ( ssl->authmode == SSL_VERIFY_NONE ||
2432 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002434 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2436 ssl->state++;
2437 return( 0 );
2438 }
2439
2440 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2441 {
2442 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2443 return( ret );
2444 }
2445
2446 ssl->state++;
2447
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002448#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 /*
2450 * Check if the client sent an empty certificate
2451 */
2452 if( ssl->endpoint == SSL_IS_SERVER &&
2453 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2454 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002455 if( ssl->in_msglen == 2 &&
2456 ssl->in_msgtype == SSL_MSG_ALERT &&
2457 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2458 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 {
2460 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2461
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002462 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2464 return( 0 );
2465 else
Paul Bakker40e46942009-01-03 21:51:57 +00002466 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 }
2468 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002469#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002471#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2472 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 if( ssl->endpoint == SSL_IS_SERVER &&
2474 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2475 {
2476 if( ssl->in_hslen == 7 &&
2477 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2478 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2479 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2480 {
2481 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2482
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002483 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002485 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 else
2487 return( 0 );
2488 }
2489 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002490#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2491 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
2493 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2494 {
2495 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002496 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 }
2498
2499 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2500 {
2501 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002502 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 }
2504
2505 /*
2506 * Same message structure as in ssl_write_certificate()
2507 */
2508 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2509
2510 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2511 {
2512 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002513 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 }
2515
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002516 /* In case we tried to reuse a session but it failed */
2517 if( ssl->session_negotiate->peer_cert != NULL )
2518 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002519 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002520 polarssl_free( ssl->session_negotiate->peer_cert );
2521 }
2522
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002523 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2524 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 {
2526 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002527 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002528 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 }
2530
Paul Bakkerb6b09562013-09-18 14:17:41 +02002531 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
2533 i = 7;
2534
2535 while( i < ssl->in_hslen )
2536 {
2537 if( ssl->in_msg[i] != 0 )
2538 {
2539 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002540 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 }
2542
2543 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2544 | (unsigned int) ssl->in_msg[i + 2];
2545 i += 3;
2546
2547 if( n < 128 || i + n > ssl->in_hslen )
2548 {
2549 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002550 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 }
2552
Paul Bakkerddf26b42013-09-18 13:46:23 +02002553 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2554 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 if( ret != 0 )
2556 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002557 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 return( ret );
2559 }
2560
2561 i += n;
2562 }
2563
Paul Bakker48916f92012-09-16 19:57:18 +00002564 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002566 /*
2567 * On client, make sure the server cert doesn't change during renego to
2568 * avoid "triple handshake" attack: https://secure-resumption.com/
2569 */
2570 if( ssl->endpoint == SSL_IS_CLIENT &&
2571 ssl->renegotiation == SSL_RENEGOTIATION )
2572 {
2573 if( ssl->session->peer_cert == NULL )
2574 {
2575 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2576 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2577 }
2578
2579 if( ssl->session->peer_cert->raw.len !=
2580 ssl->session_negotiate->peer_cert->raw.len ||
2581 memcmp( ssl->session->peer_cert->raw.p,
2582 ssl->session_negotiate->peer_cert->raw.p,
2583 ssl->session->peer_cert->raw.len ) != 0 )
2584 {
2585 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2586 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2587 }
2588 }
2589
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 if( ssl->authmode != SSL_VERIFY_NONE )
2591 {
2592 if( ssl->ca_chain == NULL )
2593 {
2594 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002595 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 }
2597
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002598 /*
2599 * Main check: verify certificate
2600 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002601 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2602 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2603 &ssl->session_negotiate->verify_result,
2604 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
2606 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002607 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002609 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002610
2611 /*
2612 * Secondary checks: always done, but change 'ret' only if it was 0
2613 */
2614
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002615#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002616 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002617 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002618
2619 /* If certificate uses an EC key, make sure the curve is OK */
2620 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2621 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2622 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002623 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002624 if( ret == 0 )
2625 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002626 }
2627 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002628#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002630 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2631 ciphersuite_info,
2632 ! ssl->endpoint ) != 0 )
2633 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002634 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002635 if( ret == 0 )
2636 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2637 }
2638
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2640 ret = 0;
2641 }
2642
2643 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2644
2645 return( ret );
2646}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002647#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2648 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2649 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2650 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2651 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2652 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2653 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
2655int ssl_write_change_cipher_spec( ssl_context *ssl )
2656{
2657 int ret;
2658
2659 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2660
2661 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2662 ssl->out_msglen = 1;
2663 ssl->out_msg[0] = 1;
2664
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 ssl->state++;
2666
2667 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2668 {
2669 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2670 return( ret );
2671 }
2672
2673 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2674
2675 return( 0 );
2676}
2677
2678int ssl_parse_change_cipher_spec( ssl_context *ssl )
2679{
2680 int ret;
2681
2682 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2683
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2685 {
2686 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2687 return( ret );
2688 }
2689
2690 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2691 {
2692 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002693 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 }
2695
2696 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2697 {
2698 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002699 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 }
2701
2702 ssl->state++;
2703
2704 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2705
2706 return( 0 );
2707}
2708
Paul Bakker41c83d32013-03-20 14:39:14 +01002709void ssl_optimize_checksum( ssl_context *ssl,
2710 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002711{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002712 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002713
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002714#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2715 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002716 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002717 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002718 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002719#endif
2720#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2721#if defined(POLARSSL_SHA512_C)
2722 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2723 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2724 else
2725#endif
2726#if defined(POLARSSL_SHA256_C)
2727 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002728 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002729 else
2730#endif
2731#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2732 /* Should never happen */
2733 return;
Paul Bakker380da532012-04-18 16:10:25 +00002734}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002735
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002736static void ssl_update_checksum_start( ssl_context *ssl,
2737 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002738{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002739#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2740 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002741 md5_update( &ssl->handshake->fin_md5 , buf, len );
2742 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002743#endif
2744#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2745#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002746 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002747#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002748#if defined(POLARSSL_SHA512_C)
2749 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002750#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002751#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002752}
2753
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002754#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2755 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002756static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2757 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002758{
Paul Bakker48916f92012-09-16 19:57:18 +00002759 md5_update( &ssl->handshake->fin_md5 , buf, len );
2760 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002761}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002762#endif
Paul Bakker380da532012-04-18 16:10:25 +00002763
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002764#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2765#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002766static void ssl_update_checksum_sha256( ssl_context *ssl,
2767 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002768{
Paul Bakker9e36f042013-06-30 14:34:05 +02002769 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002770}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002771#endif
Paul Bakker380da532012-04-18 16:10:25 +00002772
Paul Bakker9e36f042013-06-30 14:34:05 +02002773#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002774static void ssl_update_checksum_sha384( ssl_context *ssl,
2775 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002776{
Paul Bakker9e36f042013-06-30 14:34:05 +02002777 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002778}
Paul Bakker769075d2012-11-24 11:26:46 +01002779#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002780#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002781
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002782#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002783static void ssl_calc_finished_ssl(
2784 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002785{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002786 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002787 md5_context md5;
2788 sha1_context sha1;
2789
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 unsigned char padbuf[48];
2791 unsigned char md5sum[16];
2792 unsigned char sha1sum[20];
2793
Paul Bakker48916f92012-09-16 19:57:18 +00002794 ssl_session *session = ssl->session_negotiate;
2795 if( !session )
2796 session = ssl->session;
2797
Paul Bakker1ef83d62012-04-11 12:09:53 +00002798 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2799
Paul Bakker48916f92012-09-16 19:57:18 +00002800 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2801 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002802
2803 /*
2804 * SSLv3:
2805 * hash =
2806 * MD5( master + pad2 +
2807 * MD5( handshake + sender + master + pad1 ) )
2808 * + SHA1( master + pad2 +
2809 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002810 */
2811
Paul Bakker90995b52013-06-24 19:20:35 +02002812#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002814 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002815#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
Paul Bakker90995b52013-06-24 19:20:35 +02002817#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002819 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002820#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
Paul Bakker3c2122f2013-06-24 19:03:14 +02002822 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2823 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002824
Paul Bakker1ef83d62012-04-11 12:09:53 +00002825 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002826
Paul Bakker3c2122f2013-06-24 19:03:14 +02002827 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002828 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002829 md5_update( &md5, padbuf, 48 );
2830 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Paul Bakker3c2122f2013-06-24 19:03:14 +02002832 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002833 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002834 sha1_update( &sha1, padbuf, 40 );
2835 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Paul Bakker1ef83d62012-04-11 12:09:53 +00002837 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
Paul Bakker1ef83d62012-04-11 12:09:53 +00002839 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002840 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002841 md5_update( &md5, padbuf, 48 );
2842 md5_update( &md5, md5sum, 16 );
2843 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Paul Bakker1ef83d62012-04-11 12:09:53 +00002845 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002846 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002847 sha1_update( &sha1, padbuf , 40 );
2848 sha1_update( &sha1, sha1sum, 20 );
2849 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
Paul Bakker1ef83d62012-04-11 12:09:53 +00002851 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
Paul Bakker1ef83d62012-04-11 12:09:53 +00002853 memset( &md5, 0, sizeof( md5_context ) );
2854 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
2856 memset( padbuf, 0, sizeof( padbuf ) );
2857 memset( md5sum, 0, sizeof( md5sum ) );
2858 memset( sha1sum, 0, sizeof( sha1sum ) );
2859
2860 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2861}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002862#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002864#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865static void ssl_calc_finished_tls(
2866 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002867{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002868 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002869 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002870 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002872 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
Paul Bakker48916f92012-09-16 19:57:18 +00002874 ssl_session *session = ssl->session_negotiate;
2875 if( !session )
2876 session = ssl->session;
2877
Paul Bakker1ef83d62012-04-11 12:09:53 +00002878 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
Paul Bakker48916f92012-09-16 19:57:18 +00002880 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2881 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Paul Bakker1ef83d62012-04-11 12:09:53 +00002883 /*
2884 * TLSv1:
2885 * hash = PRF( master, finished_label,
2886 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2887 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002888
Paul Bakker90995b52013-06-24 19:20:35 +02002889#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002890 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2891 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002892#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002893
Paul Bakker90995b52013-06-24 19:20:35 +02002894#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002895 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2896 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002897#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898
2899 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002900 ? "client finished"
2901 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902
2903 md5_finish( &md5, padbuf );
2904 sha1_finish( &sha1, padbuf + 16 );
2905
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002906 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002907 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002908
2909 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2910
2911 memset( &md5, 0, sizeof( md5_context ) );
2912 memset( &sha1, 0, sizeof( sha1_context ) );
2913
2914 memset( padbuf, 0, sizeof( padbuf ) );
2915
2916 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2917}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002918#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002919
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002920#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2921#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002922static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 ssl_context *ssl, unsigned char *buf, int from )
2924{
2925 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002926 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002927 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002928 unsigned char padbuf[32];
2929
Paul Bakker48916f92012-09-16 19:57:18 +00002930 ssl_session *session = ssl->session_negotiate;
2931 if( !session )
2932 session = ssl->session;
2933
Paul Bakker380da532012-04-18 16:10:25 +00002934 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002935
Paul Bakker9e36f042013-06-30 14:34:05 +02002936 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002937
2938 /*
2939 * TLSv1.2:
2940 * hash = PRF( master, finished_label,
2941 * Hash( handshake ) )[0.11]
2942 */
2943
Paul Bakker9e36f042013-06-30 14:34:05 +02002944#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002946 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002947#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002948
2949 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002950 ? "client finished"
2951 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952
Paul Bakker9e36f042013-06-30 14:34:05 +02002953 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002955 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002956 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002957
2958 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2959
Paul Bakker9e36f042013-06-30 14:34:05 +02002960 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002961
2962 memset( padbuf, 0, sizeof( padbuf ) );
2963
2964 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2965}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002966#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002967
Paul Bakker9e36f042013-06-30 14:34:05 +02002968#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002969static void ssl_calc_finished_tls_sha384(
2970 ssl_context *ssl, unsigned char *buf, int from )
2971{
2972 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002973 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002974 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002975 unsigned char padbuf[48];
2976
Paul Bakker48916f92012-09-16 19:57:18 +00002977 ssl_session *session = ssl->session_negotiate;
2978 if( !session )
2979 session = ssl->session;
2980
Paul Bakker380da532012-04-18 16:10:25 +00002981 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002982
Paul Bakker9e36f042013-06-30 14:34:05 +02002983 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002984
2985 /*
2986 * TLSv1.2:
2987 * hash = PRF( master, finished_label,
2988 * Hash( handshake ) )[0.11]
2989 */
2990
Paul Bakker9e36f042013-06-30 14:34:05 +02002991#if !defined(POLARSSL_SHA512_ALT)
2992 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2993 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002994#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00002995
2996 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002997 ? "client finished"
2998 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00002999
Paul Bakker9e36f042013-06-30 14:34:05 +02003000 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003001
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003002 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003003 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004
3005 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3006
Paul Bakker9e36f042013-06-30 14:34:05 +02003007 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003008
3009 memset( padbuf, 0, sizeof( padbuf ) );
3010
3011 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3012}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003013#endif /* POLARSSL_SHA512_C */
3014#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003015
Paul Bakker48916f92012-09-16 19:57:18 +00003016void ssl_handshake_wrapup( ssl_context *ssl )
3017{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003018 int resume = ssl->handshake->resume;
3019
Paul Bakker48916f92012-09-16 19:57:18 +00003020 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3021
3022 /*
3023 * Free our handshake params
3024 */
3025 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003026 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003027 ssl->handshake = NULL;
3028
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003029 if( ssl->renegotiation == SSL_RENEGOTIATION )
3030 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3031
Paul Bakker48916f92012-09-16 19:57:18 +00003032 /*
3033 * Switch in our now active transform context
3034 */
3035 if( ssl->transform )
3036 {
3037 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003038 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003039 }
3040 ssl->transform = ssl->transform_negotiate;
3041 ssl->transform_negotiate = NULL;
3042
Paul Bakker0a597072012-09-25 21:55:46 +00003043 if( ssl->session )
3044 {
3045 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003046 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003047 }
3048 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003049 ssl->session_negotiate = NULL;
3050
Paul Bakker0a597072012-09-25 21:55:46 +00003051 /*
3052 * Add cache entry
3053 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003054 if( ssl->f_set_cache != NULL &&
3055 ssl->session->length != 0 &&
3056 resume == 0 )
3057 {
Paul Bakker0a597072012-09-25 21:55:46 +00003058 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3059 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003060 }
Paul Bakker0a597072012-09-25 21:55:46 +00003061
Paul Bakker48916f92012-09-16 19:57:18 +00003062 ssl->state++;
3063
3064 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3065}
3066
Paul Bakker1ef83d62012-04-11 12:09:53 +00003067int ssl_write_finished( ssl_context *ssl )
3068{
3069 int ret, hash_len;
3070
3071 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3072
Paul Bakker92be97b2013-01-02 17:30:03 +01003073 /*
3074 * Set the out_msg pointer to the correct location based on IV length
3075 */
3076 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3077 {
3078 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3079 ssl->transform_negotiate->fixed_ivlen;
3080 }
3081 else
3082 ssl->out_msg = ssl->out_iv;
3083
Paul Bakker48916f92012-09-16 19:57:18 +00003084 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085
3086 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3088
Paul Bakker48916f92012-09-16 19:57:18 +00003089 ssl->verify_data_len = hash_len;
3090 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3091
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 ssl->out_msglen = 4 + hash_len;
3093 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3094 ssl->out_msg[0] = SSL_HS_FINISHED;
3095
3096 /*
3097 * In case of session resuming, invert the client and server
3098 * ChangeCipherSpec messages order.
3099 */
Paul Bakker0a597072012-09-25 21:55:46 +00003100 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 {
3102 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003103 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 else
3105 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3106 }
3107 else
3108 ssl->state++;
3109
Paul Bakker48916f92012-09-16 19:57:18 +00003110 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003111 * Switch to our negotiated transform and session parameters for outbound
3112 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003113 */
3114 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3115 ssl->transform_out = ssl->transform_negotiate;
3116 ssl->session_out = ssl->session_negotiate;
3117 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003118
Paul Bakker07eb38b2012-12-19 14:42:06 +01003119#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3120 if( ssl_hw_record_activate != NULL)
3121 {
3122 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3123 {
3124 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3125 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3126 }
3127 }
3128#endif
3129
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3131 {
3132 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3133 return( ret );
3134 }
3135
3136 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3137
3138 return( 0 );
3139}
3140
3141int ssl_parse_finished( ssl_context *ssl )
3142{
Paul Bakker23986e52011-04-24 08:57:21 +00003143 int ret;
3144 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 unsigned char buf[36];
3146
3147 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3148
Paul Bakker48916f92012-09-16 19:57:18 +00003149 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Paul Bakker48916f92012-09-16 19:57:18 +00003151 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003152 * Switch to our negotiated transform and session parameters for inbound
3153 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003154 */
3155 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3156 ssl->transform_in = ssl->transform_negotiate;
3157 ssl->session_in = ssl->session_negotiate;
3158 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
Paul Bakker92be97b2013-01-02 17:30:03 +01003160 /*
3161 * Set the in_msg pointer to the correct location based on IV length
3162 */
3163 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3164 {
3165 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3166 ssl->transform_negotiate->fixed_ivlen;
3167 }
3168 else
3169 ssl->in_msg = ssl->in_iv;
3170
Paul Bakker07eb38b2012-12-19 14:42:06 +01003171#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3172 if( ssl_hw_record_activate != NULL)
3173 {
3174 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3175 {
3176 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3177 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3178 }
3179 }
3180#endif
3181
Paul Bakker5121ce52009-01-03 21:22:43 +00003182 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3183 {
3184 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3185 return( ret );
3186 }
3187
3188 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3189 {
3190 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003191 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003192 }
3193
Paul Bakker1ef83d62012-04-11 12:09:53 +00003194 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3196
3197 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3198 ssl->in_hslen != 4 + hash_len )
3199 {
3200 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003201 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003202 }
3203
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003204 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 {
3206 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003207 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 }
3209
Paul Bakker48916f92012-09-16 19:57:18 +00003210 ssl->verify_data_len = hash_len;
3211 memcpy( ssl->peer_verify_data, buf, hash_len );
3212
Paul Bakker0a597072012-09-25 21:55:46 +00003213 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 {
3215 if( ssl->endpoint == SSL_IS_CLIENT )
3216 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3217
3218 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003219 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 }
3221 else
3222 ssl->state++;
3223
3224 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3225
3226 return( 0 );
3227}
3228
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003229static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003230{
3231 if( ssl->transform_negotiate )
3232 ssl_transform_free( ssl->transform_negotiate );
3233 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003234 {
3235 ssl->transform_negotiate =
3236 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003237
3238 if( ssl->transform_negotiate != NULL )
3239 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003240 }
Paul Bakker48916f92012-09-16 19:57:18 +00003241
3242 if( ssl->session_negotiate )
3243 ssl_session_free( ssl->session_negotiate );
3244 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003245 {
3246 ssl->session_negotiate =
3247 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003248
3249 if( ssl->session_negotiate != NULL )
3250 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003251 }
Paul Bakker48916f92012-09-16 19:57:18 +00003252
3253 if( ssl->handshake )
3254 ssl_handshake_free( ssl->handshake );
3255 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003256 {
3257 ssl->handshake = (ssl_handshake_params *)
3258 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003259
3260 if( ssl->handshake != NULL )
3261 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003262 }
Paul Bakker48916f92012-09-16 19:57:18 +00003263
3264 if( ssl->handshake == NULL ||
3265 ssl->transform_negotiate == NULL ||
3266 ssl->session_negotiate == NULL )
3267 {
3268 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3269 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3270 }
3271
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003272#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3273 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003274 md5_starts( &ssl->handshake->fin_md5 );
3275 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003276#endif
3277#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3278#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003279 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003280#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003281#if defined(POLARSSL_SHA512_C)
3282 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003283#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003284#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003285
3286 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003287 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003288
Paul Bakker61d113b2013-07-04 11:51:43 +02003289#if defined(POLARSSL_ECDH_C)
3290 ecdh_init( &ssl->handshake->ecdh_ctx );
3291#endif
3292
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003293#if defined(POLARSSL_X509_CRT_PARSE_C)
3294 ssl->handshake->key_cert = ssl->key_cert;
3295#endif
3296
Paul Bakker48916f92012-09-16 19:57:18 +00003297 return( 0 );
3298}
3299
Paul Bakker5121ce52009-01-03 21:22:43 +00003300/*
3301 * Initialize an SSL context
3302 */
3303int ssl_init( ssl_context *ssl )
3304{
Paul Bakker48916f92012-09-16 19:57:18 +00003305 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 int len = SSL_BUFFER_LEN;
3307
3308 memset( ssl, 0, sizeof( ssl_context ) );
3309
Paul Bakker62f2dee2012-09-28 07:31:51 +00003310 /*
3311 * Sane defaults
3312 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003313 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3314 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3315 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3316 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003317
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003318 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003319
Paul Bakker62f2dee2012-09-28 07:31:51 +00003320#if defined(POLARSSL_DHM_C)
3321 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3322 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3323 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3324 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3325 {
3326 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3327 return( ret );
3328 }
3329#endif
3330
3331 /*
3332 * Prepare base structures
3333 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003334 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003336 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 ssl->in_msg = ssl->in_ctr + 13;
3338
3339 if( ssl->in_ctr == NULL )
3340 {
3341 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003342 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 }
3344
Paul Bakker6e339b52013-07-03 13:37:05 +02003345 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003347 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003348 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349
3350 if( ssl->out_ctr == NULL )
3351 {
3352 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003353 polarssl_free( ssl->in_ctr );
3354 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003355 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003356 }
3357
3358 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3359 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3360
Paul Bakker606b4ba2013-08-14 16:52:14 +02003361#if defined(POLARSSL_SSL_SESSION_TICKETS)
3362 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3363#endif
3364
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003365#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003366 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003367#endif
3368
Paul Bakker48916f92012-09-16 19:57:18 +00003369 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3370 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
3372 return( 0 );
3373}
3374
3375/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003376 * Reset an initialized and used SSL context for re-use while retaining
3377 * all application-set variables, function pointers and data.
3378 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003379int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003380{
Paul Bakker48916f92012-09-16 19:57:18 +00003381 int ret;
3382
Paul Bakker7eb013f2011-10-06 12:37:39 +00003383 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003384 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3385 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3386
3387 ssl->verify_data_len = 0;
3388 memset( ssl->own_verify_data, 0, 36 );
3389 memset( ssl->peer_verify_data, 0, 36 );
3390
Paul Bakker7eb013f2011-10-06 12:37:39 +00003391 ssl->in_offt = NULL;
3392
Paul Bakker92be97b2013-01-02 17:30:03 +01003393 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003394 ssl->in_msgtype = 0;
3395 ssl->in_msglen = 0;
3396 ssl->in_left = 0;
3397
3398 ssl->in_hslen = 0;
3399 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003400 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003401
Paul Bakker92be97b2013-01-02 17:30:03 +01003402 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003403 ssl->out_msgtype = 0;
3404 ssl->out_msglen = 0;
3405 ssl->out_left = 0;
3406
Paul Bakker48916f92012-09-16 19:57:18 +00003407 ssl->transform_in = NULL;
3408 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003409
3410 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3411 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003412
3413#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3414 if( ssl_hw_record_reset != NULL)
3415 {
3416 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003417 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003418 {
3419 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3420 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3421 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003422 }
3423#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003424
Paul Bakker48916f92012-09-16 19:57:18 +00003425 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003426 {
Paul Bakker48916f92012-09-16 19:57:18 +00003427 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003428 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003429 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003430 }
Paul Bakker48916f92012-09-16 19:57:18 +00003431
Paul Bakkerc0463502013-02-14 11:19:38 +01003432 if( ssl->session )
3433 {
3434 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003435 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003436 ssl->session = NULL;
3437 }
3438
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003439#if defined(POLARSSL_SSL_ALPN)
3440 ssl->alpn_chosen = NULL;
3441#endif
3442
Paul Bakker48916f92012-09-16 19:57:18 +00003443 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3444 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003445
3446 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003447}
3448
Paul Bakkera503a632013-08-14 13:48:06 +02003449#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003450/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003451 * Allocate and initialize ticket keys
3452 */
3453static int ssl_ticket_keys_init( ssl_context *ssl )
3454{
3455 int ret;
3456 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003457 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003458
3459 if( ssl->ticket_keys != NULL )
3460 return( 0 );
3461
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003462 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3463 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003464 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3465
3466 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003467 {
3468 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003469 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003470 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003471
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003472 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3473 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3474 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3475 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003476 polarssl_free( tkeys );
3477 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003478 }
3479
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003480 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003481 {
3482 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003483 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003484 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003485
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003486 ssl->ticket_keys = tkeys;
3487
3488 return( 0 );
3489}
Paul Bakkera503a632013-08-14 13:48:06 +02003490#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003491
3492/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 * SSL set accessors
3494 */
3495void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3496{
3497 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003498
Paul Bakker606b4ba2013-08-14 16:52:14 +02003499#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003500 if( endpoint == SSL_IS_CLIENT )
3501 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003502#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003503}
3504
3505void ssl_set_authmode( ssl_context *ssl, int authmode )
3506{
3507 ssl->authmode = authmode;
3508}
3509
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003510#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003511void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003512 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003513 void *p_vrfy )
3514{
3515 ssl->f_vrfy = f_vrfy;
3516 ssl->p_vrfy = p_vrfy;
3517}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003518#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003519
Paul Bakker5121ce52009-01-03 21:22:43 +00003520void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003521 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003522 void *p_rng )
3523{
3524 ssl->f_rng = f_rng;
3525 ssl->p_rng = p_rng;
3526}
3527
3528void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003529 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 void *p_dbg )
3531{
3532 ssl->f_dbg = f_dbg;
3533 ssl->p_dbg = p_dbg;
3534}
3535
3536void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003537 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003538 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003539{
3540 ssl->f_recv = f_recv;
3541 ssl->f_send = f_send;
3542 ssl->p_recv = p_recv;
3543 ssl->p_send = p_send;
3544}
3545
Paul Bakker0a597072012-09-25 21:55:46 +00003546void ssl_set_session_cache( ssl_context *ssl,
3547 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3548 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003549{
Paul Bakker0a597072012-09-25 21:55:46 +00003550 ssl->f_get_cache = f_get_cache;
3551 ssl->p_get_cache = p_get_cache;
3552 ssl->f_set_cache = f_set_cache;
3553 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003554}
3555
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003556int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003557{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003558 int ret;
3559
3560 if( ssl == NULL ||
3561 session == NULL ||
3562 ssl->session_negotiate == NULL ||
3563 ssl->endpoint != SSL_IS_CLIENT )
3564 {
3565 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3566 }
3567
3568 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3569 return( ret );
3570
Paul Bakker0a597072012-09-25 21:55:46 +00003571 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003572
3573 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003574}
3575
Paul Bakkerb68cad62012-08-23 08:34:18 +00003576void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003577{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003578 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3579 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3580 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3581 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3582}
3583
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003584void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3585 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003586 int major, int minor )
3587{
3588 if( major != SSL_MAJOR_VERSION_3 )
3589 return;
3590
3591 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3592 return;
3593
3594 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003595}
3596
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003597#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003598/* Add a new (empty) key_cert entry an return a pointer to it */
3599static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3600{
3601 ssl_key_cert *key_cert, *last;
3602
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003603 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3604 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003605 return( NULL );
3606
3607 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3608
3609 /* Append the new key_cert to the (possibly empty) current list */
3610 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003611 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003612 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003613 if( ssl->handshake != NULL )
3614 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003615 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003616 else
3617 {
3618 last = ssl->key_cert;
3619 while( last->next != NULL )
3620 last = last->next;
3621 last->next = key_cert;
3622 }
3623
3624 return key_cert;
3625}
3626
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003627void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003628 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003629{
3630 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003631 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003632 ssl->peer_cn = peer_cn;
3633}
3634
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003635int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003636 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003637{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003638 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3639
3640 if( key_cert == NULL )
3641 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3642
3643 key_cert->cert = own_cert;
3644 key_cert->key = pk_key;
3645
3646 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003647}
3648
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003649#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003650int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003651 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003652{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003653 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003654 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003655
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003656 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003657 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3658
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003659 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3660 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003661 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003662
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003663 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003664
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003665 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003666 if( ret != 0 )
3667 return( ret );
3668
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003669 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003670 return( ret );
3671
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003672 key_cert->cert = own_cert;
3673 key_cert->key_own_alloc = 1;
3674
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003675 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003676}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003677#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003678
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003679int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003680 void *rsa_key,
3681 rsa_decrypt_func rsa_decrypt,
3682 rsa_sign_func rsa_sign,
3683 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003684{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003685 int ret;
3686 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003687
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003688 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003689 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3690
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003691 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3692 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003693 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003694
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003695 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003696
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003697 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3698 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3699 return( ret );
3700
3701 key_cert->cert = own_cert;
3702 key_cert->key_own_alloc = 1;
3703
3704 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003705}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003706#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003707
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003708#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003709int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3710 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003711{
Paul Bakker6db455e2013-09-18 17:29:31 +02003712 if( psk == NULL || psk_identity == NULL )
3713 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3714
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003715 /*
3716 * The length will be check later anyway, but in case it is obviously
3717 * too large, better abort now. The PMS is as follows:
3718 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3719 */
3720 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3721 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3722
Paul Bakker6db455e2013-09-18 17:29:31 +02003723 if( ssl->psk != NULL )
3724 {
3725 polarssl_free( ssl->psk );
3726 polarssl_free( ssl->psk_identity );
3727 }
3728
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003729 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003730 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003731
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003732 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003733 ssl->psk_identity = (unsigned char *)
3734 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003735
3736 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3737 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3738
3739 memcpy( ssl->psk, psk, ssl->psk_len );
3740 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003741
3742 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003743}
3744
3745void ssl_set_psk_cb( ssl_context *ssl,
3746 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3747 size_t),
3748 void *p_psk )
3749{
3750 ssl->f_psk = f_psk;
3751 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003752}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003753#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003754
Paul Bakker48916f92012-09-16 19:57:18 +00003755#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003756int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003757{
3758 int ret;
3759
Paul Bakker48916f92012-09-16 19:57:18 +00003760 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003761 {
3762 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3763 return( ret );
3764 }
3765
Paul Bakker48916f92012-09-16 19:57:18 +00003766 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 {
3768 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3769 return( ret );
3770 }
3771
3772 return( 0 );
3773}
3774
Paul Bakker1b57b062011-01-06 15:48:19 +00003775int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3776{
3777 int ret;
3778
Paul Bakker48916f92012-09-16 19:57:18 +00003779 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003780 {
3781 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3782 return( ret );
3783 }
3784
Paul Bakker48916f92012-09-16 19:57:18 +00003785 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003786 {
3787 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3788 return( ret );
3789 }
3790
3791 return( 0 );
3792}
Paul Bakker48916f92012-09-16 19:57:18 +00003793#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003794
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003795#if defined(POLARSSL_SSL_SET_CURVES)
3796/*
3797 * Set the allowed elliptic curves
3798 */
3799void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3800{
3801 ssl->curve_list = curve_list;
3802}
3803#endif
3804
Paul Bakker0be444a2013-08-27 21:55:01 +02003805#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003806int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003807{
3808 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003809 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003810
3811 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003812
3813 if( ssl->hostname_len + 1 == 0 )
3814 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3815
Paul Bakker6e339b52013-07-03 13:37:05 +02003816 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003817
Paul Bakkerb15b8512012-01-13 13:44:06 +00003818 if( ssl->hostname == NULL )
3819 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3820
Paul Bakker3c2122f2013-06-24 19:03:14 +02003821 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003822 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003823
Paul Bakker40ea7de2009-05-03 10:18:48 +00003824 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003825
3826 return( 0 );
3827}
3828
Paul Bakker5701cdc2012-09-27 21:49:42 +00003829void ssl_set_sni( ssl_context *ssl,
3830 int (*f_sni)(void *, ssl_context *,
3831 const unsigned char *, size_t),
3832 void *p_sni )
3833{
3834 ssl->f_sni = f_sni;
3835 ssl->p_sni = p_sni;
3836}
Paul Bakker0be444a2013-08-27 21:55:01 +02003837#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003838
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003839#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003840int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003841{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003842 size_t cur_len, tot_len;
3843 const char **p;
3844
3845 /*
3846 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3847 * truncated". Check lengths now rather than later.
3848 */
3849 tot_len = 0;
3850 for( p = protos; *p != NULL; p++ )
3851 {
3852 cur_len = strlen( *p );
3853 tot_len += cur_len;
3854
3855 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3856 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3857 }
3858
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003859 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003860
3861 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003862}
3863
3864const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3865{
3866 return ssl->alpn_chosen;
3867}
3868#endif /* POLARSSL_SSL_ALPN */
3869
Paul Bakker490ecc82011-10-06 13:04:09 +00003870void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3871{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003872 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3873 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3874 {
3875 ssl->max_major_ver = major;
3876 ssl->max_minor_ver = minor;
3877 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003878}
3879
Paul Bakker1d29fb52012-09-28 13:28:45 +00003880void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3881{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003882 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3883 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3884 {
3885 ssl->min_major_ver = major;
3886 ssl->min_minor_ver = minor;
3887 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003888}
3889
Paul Bakker05decb22013-08-15 13:33:48 +02003890#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003891int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3892{
Paul Bakker77e257e2013-12-16 15:29:52 +01003893 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003894 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003895 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003896 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003897 }
3898
3899 ssl->mfl_code = mfl_code;
3900
3901 return( 0 );
3902}
Paul Bakker05decb22013-08-15 13:33:48 +02003903#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003904
Paul Bakker1f2bc622013-08-15 13:45:55 +02003905#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003906int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003907{
3908 if( ssl->endpoint != SSL_IS_CLIENT )
3909 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3910
Paul Bakker8c1ede62013-07-19 14:14:37 +02003911 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003912
3913 return( 0 );
3914}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003915#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003916
Paul Bakker48916f92012-09-16 19:57:18 +00003917void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3918{
3919 ssl->disable_renegotiation = renegotiation;
3920}
3921
3922void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3923{
3924 ssl->allow_legacy_renegotiation = allow_legacy;
3925}
3926
Paul Bakkera503a632013-08-14 13:48:06 +02003927#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003928int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3929{
3930 ssl->session_tickets = use_tickets;
3931
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003932 if( ssl->endpoint == SSL_IS_CLIENT )
3933 return( 0 );
3934
3935 if( ssl->f_rng == NULL )
3936 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3937
3938 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003939}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003940
3941void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3942{
3943 ssl->ticket_lifetime = lifetime;
3944}
Paul Bakkera503a632013-08-14 13:48:06 +02003945#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003946
Paul Bakker5121ce52009-01-03 21:22:43 +00003947/*
3948 * SSL get accessors
3949 */
Paul Bakker23986e52011-04-24 08:57:21 +00003950size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003951{
3952 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3953}
3954
Paul Bakkerff60ee62010-03-16 21:09:09 +00003955int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003956{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003957 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003958}
3959
Paul Bakkere3166ce2011-01-27 17:40:50 +00003960const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003961{
Paul Bakker926c8e42013-03-06 10:23:34 +01003962 if( ssl == NULL || ssl->session == NULL )
3963 return NULL;
3964
Paul Bakkere3166ce2011-01-27 17:40:50 +00003965 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003966}
3967
Paul Bakker43ca69c2011-01-15 17:35:19 +00003968const char *ssl_get_version( const ssl_context *ssl )
3969{
3970 switch( ssl->minor_ver )
3971 {
3972 case SSL_MINOR_VERSION_0:
3973 return( "SSLv3.0" );
3974
3975 case SSL_MINOR_VERSION_1:
3976 return( "TLSv1.0" );
3977
3978 case SSL_MINOR_VERSION_2:
3979 return( "TLSv1.1" );
3980
Paul Bakker1ef83d62012-04-11 12:09:53 +00003981 case SSL_MINOR_VERSION_3:
3982 return( "TLSv1.2" );
3983
Paul Bakker43ca69c2011-01-15 17:35:19 +00003984 default:
3985 break;
3986 }
3987 return( "unknown" );
3988}
3989
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003990#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003991const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003992{
3993 if( ssl == NULL || ssl->session == NULL )
3994 return NULL;
3995
3996 return ssl->session->peer_cert;
3997}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003998#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003999
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004000int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4001{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004002 if( ssl == NULL ||
4003 dst == NULL ||
4004 ssl->session == NULL ||
4005 ssl->endpoint != SSL_IS_CLIENT )
4006 {
4007 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4008 }
4009
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004010 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004011}
4012
Paul Bakker5121ce52009-01-03 21:22:43 +00004013/*
Paul Bakker1961b702013-01-25 14:49:24 +01004014 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 */
Paul Bakker1961b702013-01-25 14:49:24 +01004016int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004017{
Paul Bakker40e46942009-01-03 21:51:57 +00004018 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004019
Paul Bakker40e46942009-01-03 21:51:57 +00004020#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004021 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004022 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004023#endif
4024
Paul Bakker40e46942009-01-03 21:51:57 +00004025#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004026 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004027 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004028#endif
4029
Paul Bakker1961b702013-01-25 14:49:24 +01004030 return( ret );
4031}
4032
4033/*
4034 * Perform the SSL handshake
4035 */
4036int ssl_handshake( ssl_context *ssl )
4037{
4038 int ret = 0;
4039
4040 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4041
4042 while( ssl->state != SSL_HANDSHAKE_OVER )
4043 {
4044 ret = ssl_handshake_step( ssl );
4045
4046 if( ret != 0 )
4047 break;
4048 }
4049
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4051
4052 return( ret );
4053}
4054
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004055#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004056/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004057 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004058 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004059static int ssl_write_hello_request( ssl_context *ssl )
4060{
4061 int ret;
4062
4063 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4064
4065 ssl->out_msglen = 4;
4066 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4067 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4068
4069 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4070 {
4071 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4072 return( ret );
4073 }
4074
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004075 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4076
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004077 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4078
4079 return( 0 );
4080}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004081#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004082
4083/*
4084 * Actually renegotiate current connection, triggered by either:
4085 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004086 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004087 * - receiving any handshake message on server during ssl_read() after the
4088 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004089 * If the handshake doesn't complete due to waiting for I/O, it will continue
4090 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004091 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004092static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004093{
4094 int ret;
4095
4096 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4097
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004098 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4099 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004100
4101 ssl->state = SSL_HELLO_REQUEST;
4102 ssl->renegotiation = SSL_RENEGOTIATION;
4103
Paul Bakker48916f92012-09-16 19:57:18 +00004104 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4105 {
4106 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4107 return( ret );
4108 }
4109
4110 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4111
4112 return( 0 );
4113}
4114
4115/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004116 * Renegotiate current connection on client,
4117 * or request renegotiation on server
4118 */
4119int ssl_renegotiate( ssl_context *ssl )
4120{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004121 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004122
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004123#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004124 /* On server, just send the request */
4125 if( ssl->endpoint == SSL_IS_SERVER )
4126 {
4127 if( ssl->state != SSL_HANDSHAKE_OVER )
4128 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4129
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004130 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004131 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004132#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004133
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004134#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004135 /*
4136 * On client, either start the renegotiation process or,
4137 * if already in progress, continue the handshake
4138 */
4139 if( ssl->renegotiation != SSL_RENEGOTIATION )
4140 {
4141 if( ssl->state != SSL_HANDSHAKE_OVER )
4142 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4143
4144 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4145 {
4146 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4147 return( ret );
4148 }
4149 }
4150 else
4151 {
4152 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4153 {
4154 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4155 return( ret );
4156 }
4157 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004158#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004159
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004160 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004161}
4162
4163/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004164 * Receive application data decrypted from the SSL layer
4165 */
Paul Bakker23986e52011-04-24 08:57:21 +00004166int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004167{
Paul Bakker23986e52011-04-24 08:57:21 +00004168 int ret;
4169 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004170
4171 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4172
4173 if( ssl->state != SSL_HANDSHAKE_OVER )
4174 {
4175 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4176 {
4177 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4178 return( ret );
4179 }
4180 }
4181
4182 if( ssl->in_offt == NULL )
4183 {
4184 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4185 {
Paul Bakker831a7552011-05-18 13:32:51 +00004186 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4187 return( 0 );
4188
Paul Bakker5121ce52009-01-03 21:22:43 +00004189 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4190 return( ret );
4191 }
4192
4193 if( ssl->in_msglen == 0 &&
4194 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4195 {
4196 /*
4197 * OpenSSL sends empty messages to randomize the IV
4198 */
4199 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4200 {
Paul Bakker831a7552011-05-18 13:32:51 +00004201 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4202 return( 0 );
4203
Paul Bakker5121ce52009-01-03 21:22:43 +00004204 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4205 return( ret );
4206 }
4207 }
4208
Paul Bakker48916f92012-09-16 19:57:18 +00004209 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4210 {
4211 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4212
4213 if( ssl->endpoint == SSL_IS_CLIENT &&
4214 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4215 ssl->in_hslen != 4 ) )
4216 {
4217 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4218 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4219 }
4220
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004221 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4222 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004223 ssl->allow_legacy_renegotiation ==
4224 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004225 {
4226 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4227
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004228#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004229 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004230 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004231 /*
4232 * SSLv3 does not have a "no_renegotiation" alert
4233 */
4234 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4235 return( ret );
4236 }
4237 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004238#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004239#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4240 defined(POLARSSL_SSL_PROTO_TLS1_2)
4241 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004242 {
4243 if( ( ret = ssl_send_alert_message( ssl,
4244 SSL_ALERT_LEVEL_WARNING,
4245 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4246 {
4247 return( ret );
4248 }
Paul Bakker48916f92012-09-16 19:57:18 +00004249 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004250 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004251#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4252 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004253 {
4254 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004255 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004256 }
Paul Bakker48916f92012-09-16 19:57:18 +00004257 }
4258 else
4259 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004260 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004261 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004262 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004263 return( ret );
4264 }
4265
4266 return( POLARSSL_ERR_NET_WANT_READ );
4267 }
4268 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004269 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4270 {
4271 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4272 "but not honored by client" ) );
4273 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4274 }
Paul Bakker48916f92012-09-16 19:57:18 +00004275 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004276 {
4277 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004278 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004279 }
4280
4281 ssl->in_offt = ssl->in_msg;
4282 }
4283
4284 n = ( len < ssl->in_msglen )
4285 ? len : ssl->in_msglen;
4286
4287 memcpy( buf, ssl->in_offt, n );
4288 ssl->in_msglen -= n;
4289
4290 if( ssl->in_msglen == 0 )
4291 /* all bytes consumed */
4292 ssl->in_offt = NULL;
4293 else
4294 /* more data available */
4295 ssl->in_offt += n;
4296
4297 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4298
Paul Bakker23986e52011-04-24 08:57:21 +00004299 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004300}
4301
4302/*
4303 * Send application data to be encrypted by the SSL layer
4304 */
Paul Bakker23986e52011-04-24 08:57:21 +00004305int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004306{
Paul Bakker23986e52011-04-24 08:57:21 +00004307 int ret;
4308 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004309 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004310
4311 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4312
4313 if( ssl->state != SSL_HANDSHAKE_OVER )
4314 {
4315 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4316 {
4317 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4318 return( ret );
4319 }
4320 }
4321
Paul Bakker05decb22013-08-15 13:33:48 +02004322#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004323 /*
4324 * Assume mfl_code is correct since it was checked when set
4325 */
4326 max_len = mfl_code_to_length[ssl->mfl_code];
4327
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004328 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004329 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004330 */
4331 if( ssl->session_out != NULL &&
4332 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4333 {
4334 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4335 }
Paul Bakker05decb22013-08-15 13:33:48 +02004336#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004337
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004338 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004339
Paul Bakker5121ce52009-01-03 21:22:43 +00004340 if( ssl->out_left != 0 )
4341 {
4342 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4343 {
4344 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4345 return( ret );
4346 }
4347 }
Paul Bakker887bd502011-06-08 13:10:54 +00004348 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004349 {
Paul Bakker887bd502011-06-08 13:10:54 +00004350 ssl->out_msglen = n;
4351 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4352 memcpy( ssl->out_msg, buf, n );
4353
4354 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4355 {
4356 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4357 return( ret );
4358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 }
4360
4361 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4362
Paul Bakker23986e52011-04-24 08:57:21 +00004363 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004364}
4365
4366/*
4367 * Notify the peer that the connection is being closed
4368 */
4369int ssl_close_notify( ssl_context *ssl )
4370{
4371 int ret;
4372
4373 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4374
4375 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4376 {
4377 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4378 return( ret );
4379 }
4380
4381 if( ssl->state == SSL_HANDSHAKE_OVER )
4382 {
Paul Bakker48916f92012-09-16 19:57:18 +00004383 if( ( ret = ssl_send_alert_message( ssl,
4384 SSL_ALERT_LEVEL_WARNING,
4385 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004387 return( ret );
4388 }
4389 }
4390
4391 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4392
4393 return( ret );
4394}
4395
Paul Bakker48916f92012-09-16 19:57:18 +00004396void ssl_transform_free( ssl_transform *transform )
4397{
4398#if defined(POLARSSL_ZLIB_SUPPORT)
4399 deflateEnd( &transform->ctx_deflate );
4400 inflateEnd( &transform->ctx_inflate );
4401#endif
4402
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004403 cipher_free_ctx( &transform->cipher_ctx_enc );
4404 cipher_free_ctx( &transform->cipher_ctx_dec );
4405
Paul Bakker61d113b2013-07-04 11:51:43 +02004406 md_free_ctx( &transform->md_ctx_enc );
4407 md_free_ctx( &transform->md_ctx_dec );
4408
Paul Bakker48916f92012-09-16 19:57:18 +00004409 memset( transform, 0, sizeof( ssl_transform ) );
4410}
4411
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004412#if defined(POLARSSL_X509_CRT_PARSE_C)
4413static void ssl_key_cert_free( ssl_key_cert *key_cert )
4414{
4415 ssl_key_cert *cur = key_cert, *next;
4416
4417 while( cur != NULL )
4418 {
4419 next = cur->next;
4420
4421 if( cur->key_own_alloc )
4422 {
4423 pk_free( cur->key );
4424 polarssl_free( cur->key );
4425 }
4426 polarssl_free( cur );
4427
4428 cur = next;
4429 }
4430}
4431#endif /* POLARSSL_X509_CRT_PARSE_C */
4432
Paul Bakker48916f92012-09-16 19:57:18 +00004433void ssl_handshake_free( ssl_handshake_params *handshake )
4434{
4435#if defined(POLARSSL_DHM_C)
4436 dhm_free( &handshake->dhm_ctx );
4437#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004438#if defined(POLARSSL_ECDH_C)
4439 ecdh_free( &handshake->ecdh_ctx );
4440#endif
4441
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004442#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004443 /* explicit void pointer cast for buggy MS compiler */
4444 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004445#endif
4446
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004447#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4448 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4449 /*
4450 * Free only the linked list wrapper, not the keys themselves
4451 * since the belong to the SNI callback
4452 */
4453 if( handshake->sni_key_cert != NULL )
4454 {
4455 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4456
4457 while( cur != NULL )
4458 {
4459 next = cur->next;
4460 polarssl_free( cur );
4461 cur = next;
4462 }
4463 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004464#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004465
Paul Bakker48916f92012-09-16 19:57:18 +00004466 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4467}
4468
4469void ssl_session_free( ssl_session *session )
4470{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004471#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004472 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004473 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004474 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004475 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004476 }
Paul Bakkered27a042013-04-18 22:46:23 +02004477#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004478
Paul Bakkera503a632013-08-14 13:48:06 +02004479#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004480 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004481#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004482
Paul Bakker0a597072012-09-25 21:55:46 +00004483 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004484}
4485
Paul Bakker5121ce52009-01-03 21:22:43 +00004486/*
4487 * Free an SSL context
4488 */
4489void ssl_free( ssl_context *ssl )
4490{
4491 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4492
Paul Bakker5121ce52009-01-03 21:22:43 +00004493 if( ssl->out_ctr != NULL )
4494 {
4495 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004496 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004497 }
4498
4499 if( ssl->in_ctr != NULL )
4500 {
4501 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004502 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004503 }
4504
Paul Bakker16770332013-10-11 09:59:44 +02004505#if defined(POLARSSL_ZLIB_SUPPORT)
4506 if( ssl->compress_buf != NULL )
4507 {
4508 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4509 polarssl_free( ssl->compress_buf );
4510 }
4511#endif
4512
Paul Bakker40e46942009-01-03 21:51:57 +00004513#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004514 mpi_free( &ssl->dhm_P );
4515 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004516#endif
4517
Paul Bakker48916f92012-09-16 19:57:18 +00004518 if( ssl->transform )
4519 {
4520 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004521 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004522 }
4523
4524 if( ssl->handshake )
4525 {
4526 ssl_handshake_free( ssl->handshake );
4527 ssl_transform_free( ssl->transform_negotiate );
4528 ssl_session_free( ssl->session_negotiate );
4529
Paul Bakker6e339b52013-07-03 13:37:05 +02004530 polarssl_free( ssl->handshake );
4531 polarssl_free( ssl->transform_negotiate );
4532 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004533 }
4534
Paul Bakkerc0463502013-02-14 11:19:38 +01004535 if( ssl->session )
4536 {
4537 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004538 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004539 }
4540
Paul Bakkera503a632013-08-14 13:48:06 +02004541#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004542 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004543#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004544
Paul Bakker0be444a2013-08-27 21:55:01 +02004545#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004546 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004547 {
4548 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004549 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004550 ssl->hostname_len = 0;
4551 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004552#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004553
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004554#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004555 if( ssl->psk != NULL )
4556 {
4557 memset( ssl->psk, 0, ssl->psk_len );
4558 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4559 polarssl_free( ssl->psk );
4560 polarssl_free( ssl->psk_identity );
4561 ssl->psk_len = 0;
4562 ssl->psk_identity_len = 0;
4563 }
4564#endif
4565
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004566#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004567 ssl_key_cert_free( ssl->key_cert );
4568#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004569
Paul Bakker05ef8352012-05-08 09:17:57 +00004570#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4571 if( ssl_hw_record_finish != NULL )
4572 {
4573 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4574 ssl_hw_record_finish( ssl );
4575 }
4576#endif
4577
Paul Bakker5121ce52009-01-03 21:22:43 +00004578 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004579
Paul Bakker86f04f42013-02-14 11:20:09 +01004580 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004581 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004582}
4583
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004584#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004585/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004586 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004587 */
4588unsigned char ssl_sig_from_pk( pk_context *pk )
4589{
4590#if defined(POLARSSL_RSA_C)
4591 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4592 return( SSL_SIG_RSA );
4593#endif
4594#if defined(POLARSSL_ECDSA_C)
4595 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4596 return( SSL_SIG_ECDSA );
4597#endif
4598 return( SSL_SIG_ANON );
4599}
4600
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004601pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4602{
4603 switch( sig )
4604 {
4605#if defined(POLARSSL_RSA_C)
4606 case SSL_SIG_RSA:
4607 return( POLARSSL_PK_RSA );
4608#endif
4609#if defined(POLARSSL_ECDSA_C)
4610 case SSL_SIG_ECDSA:
4611 return( POLARSSL_PK_ECDSA );
4612#endif
4613 default:
4614 return( POLARSSL_PK_NONE );
4615 }
4616}
Paul Bakker9af723c2014-05-01 13:03:14 +02004617#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004618
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004619/*
4620 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4621 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004622md_type_t ssl_md_alg_from_hash( unsigned char hash )
4623{
4624 switch( hash )
4625 {
4626#if defined(POLARSSL_MD5_C)
4627 case SSL_HASH_MD5:
4628 return( POLARSSL_MD_MD5 );
4629#endif
4630#if defined(POLARSSL_SHA1_C)
4631 case SSL_HASH_SHA1:
4632 return( POLARSSL_MD_SHA1 );
4633#endif
4634#if defined(POLARSSL_SHA256_C)
4635 case SSL_HASH_SHA224:
4636 return( POLARSSL_MD_SHA224 );
4637 case SSL_HASH_SHA256:
4638 return( POLARSSL_MD_SHA256 );
4639#endif
4640#if defined(POLARSSL_SHA512_C)
4641 case SSL_HASH_SHA384:
4642 return( POLARSSL_MD_SHA384 );
4643 case SSL_HASH_SHA512:
4644 return( POLARSSL_MD_SHA512 );
4645#endif
4646 default:
4647 return( POLARSSL_MD_NONE );
4648 }
4649}
4650
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004651#if defined(POLARSSL_SSL_SET_CURVES)
4652/*
4653 * Check is a curve proposed by the peer is in our list.
4654 * Return 1 if we're willing to use it, 0 otherwise.
4655 */
4656int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4657{
4658 const ecp_group_id *gid;
4659
4660 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4661 if( *gid == grp_id )
4662 return( 1 );
4663
4664 return( 0 );
4665}
Paul Bakker9af723c2014-05-01 13:03:14 +02004666#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004667
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004668#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004669int ssl_check_cert_usage( const x509_crt *cert,
4670 const ssl_ciphersuite_t *ciphersuite,
4671 int cert_endpoint )
4672{
4673#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4674 int usage = 0;
4675#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004676#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4677 const char *ext_oid;
4678 size_t ext_len;
4679#endif
4680
4681#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4682 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4683 ((void) cert);
4684 ((void) cert_endpoint);
4685#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004686
4687#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4688 if( cert_endpoint == SSL_IS_SERVER )
4689 {
4690 /* Server part of the key exchange */
4691 switch( ciphersuite->key_exchange )
4692 {
4693 case POLARSSL_KEY_EXCHANGE_RSA:
4694 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4695 usage = KU_KEY_ENCIPHERMENT;
4696 break;
4697
4698 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4699 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4700 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4701 usage = KU_DIGITAL_SIGNATURE;
4702 break;
4703
4704 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4705 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4706 usage = KU_KEY_AGREEMENT;
4707 break;
4708
4709 /* Don't use default: we want warnings when adding new values */
4710 case POLARSSL_KEY_EXCHANGE_NONE:
4711 case POLARSSL_KEY_EXCHANGE_PSK:
4712 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4713 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4714 usage = 0;
4715 }
4716 }
4717 else
4718 {
4719 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4720 usage = KU_DIGITAL_SIGNATURE;
4721 }
4722
4723 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4724 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004725#else
4726 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004727#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4728
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004729#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4730 if( cert_endpoint == SSL_IS_SERVER )
4731 {
4732 ext_oid = OID_SERVER_AUTH;
4733 ext_len = OID_SIZE( OID_SERVER_AUTH );
4734 }
4735 else
4736 {
4737 ext_oid = OID_CLIENT_AUTH;
4738 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4739 }
4740
4741 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4742 return( -1 );
4743#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4744
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004745 return( 0 );
4746}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004747#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004748
4749#endif /* POLARSSL_SSL_TLS_C */