blob: 9078b1986785e10e0d8374b6feebbb0ba505a049 [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
509 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 {
Paul Bakker68884e32013-01-07 18:20:04 +0100511 transform->keylen = cipher_info->key_length;
512 transform->keylen /= 8;
513 transform->minlen = 1;
514 transform->ivlen = 12;
515 transform->fixed_ivlen = 4;
516 transform->maclen = 0;
517 }
518 else
519 {
520 if( md_info->type != POLARSSL_MD_NONE )
521 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200522 int ret;
523
Paul Bakker61d113b2013-07-04 11:51:43 +0200524 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
525 {
526 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
527 return( ret );
528 }
529
530 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
531 {
532 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
533 return( ret );
534 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Paul Bakker68884e32013-01-07 18:20:04 +0100536 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200537
Paul Bakker1f2bc622013-08-15 13:45:55 +0200538#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200539 /*
540 * If HMAC is to be truncated, we shall keep the leftmost bytes,
541 * (rfc 6066 page 13 or rfc 2104 section 4),
542 * so we only need to adjust the length here.
543 */
544 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
545 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200546#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Paul Bakker68884e32013-01-07 18:20:04 +0100549 transform->keylen = cipher_info->key_length;
550 transform->keylen /= 8;
551 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Paul Bakker68884e32013-01-07 18:20:04 +0100553 transform->minlen = transform->keylen;
554 if( transform->minlen < transform->maclen )
555 {
556 if( cipher_info->mode == POLARSSL_MODE_STREAM )
557 transform->minlen = transform->maclen;
558 else
559 transform->minlen += transform->keylen;
560 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 }
562
563 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000564 transform->keylen, transform->minlen, transform->ivlen,
565 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 /*
568 * Finally setup the cipher contexts, IVs and MAC secrets.
569 */
570 if( ssl->endpoint == SSL_IS_CLIENT )
571 {
Paul Bakker48916f92012-09-16 19:57:18 +0000572 key1 = keyblk + transform->maclen * 2;
573 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Paul Bakker68884e32013-01-07 18:20:04 +0100575 mac_enc = keyblk;
576 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000578 /*
579 * This is not used in TLS v1.1.
580 */
Paul Bakker48916f92012-09-16 19:57:18 +0000581 iv_copy_len = ( transform->fixed_ivlen ) ?
582 transform->fixed_ivlen : transform->ivlen;
583 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
584 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000585 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 }
587 else
588 {
Paul Bakker48916f92012-09-16 19:57:18 +0000589 key1 = keyblk + transform->maclen * 2 + transform->keylen;
590 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Paul Bakker68884e32013-01-07 18:20:04 +0100592 mac_enc = keyblk + transform->maclen;
593 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000595 /*
596 * This is not used in TLS v1.1.
597 */
Paul Bakker48916f92012-09-16 19:57:18 +0000598 iv_copy_len = ( transform->fixed_ivlen ) ?
599 transform->fixed_ivlen : transform->ivlen;
600 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
601 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000602 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 }
604
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200605#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100606 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
607 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100608 if( transform->maclen > sizeof transform->mac_enc )
609 {
610 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
611 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
612 }
613
Paul Bakker68884e32013-01-07 18:20:04 +0100614 memcpy( transform->mac_enc, mac_enc, transform->maclen );
615 memcpy( transform->mac_dec, mac_dec, transform->maclen );
616 }
617 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200618#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200619#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
620 defined(POLARSSL_SSL_PROTO_TLS1_2)
621 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100622 {
623 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
624 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
625 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200626 else
627#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200628 {
629 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200630 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200631 }
Paul Bakker68884e32013-01-07 18:20:04 +0100632
Paul Bakker05ef8352012-05-08 09:17:57 +0000633#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
634 if( ssl_hw_record_init != NULL)
635 {
636 int ret = 0;
637
638 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
639
Paul Bakker07eb38b2012-12-19 14:42:06 +0100640 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
641 transform->iv_enc, transform->iv_dec,
642 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100643 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100644 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000645 {
646 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
647 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
648 }
649 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200650#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000651
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200652 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
653 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200655 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
656 return( ret );
657 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200658
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200659 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
660 cipher_info ) ) != 0 )
661 {
662 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
663 return( ret );
664 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200665
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200666 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
667 cipher_info->key_length,
668 POLARSSL_ENCRYPT ) ) != 0 )
669 {
670 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
671 return( ret );
672 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200673
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200674 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
675 cipher_info->key_length,
676 POLARSSL_DECRYPT ) ) != 0 )
677 {
678 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
679 return( ret );
680 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200681
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200682#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200683 if( cipher_info->mode == POLARSSL_MODE_CBC )
684 {
685 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
686 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200687 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200688 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
689 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200690 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200691
692 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
693 POLARSSL_PADDING_NONE ) ) != 0 )
694 {
695 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
696 return( ret );
697 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200699#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701 memset( keyblk, 0, sizeof( keyblk ) );
702
Paul Bakker2770fbd2012-07-03 13:30:23 +0000703#if defined(POLARSSL_ZLIB_SUPPORT)
704 // Initialize compression
705 //
Paul Bakker48916f92012-09-16 19:57:18 +0000706 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000707 {
Paul Bakker16770332013-10-11 09:59:44 +0200708 if( ssl->compress_buf == NULL )
709 {
710 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
711 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
712 if( ssl->compress_buf == NULL )
713 {
714 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
715 SSL_BUFFER_LEN ) );
716 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
717 }
718 }
719
Paul Bakker2770fbd2012-07-03 13:30:23 +0000720 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
721
Paul Bakker48916f92012-09-16 19:57:18 +0000722 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
723 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000724
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200725 if( deflateInit( &transform->ctx_deflate,
726 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000727 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000728 {
729 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
730 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
731 }
732 }
733#endif /* POLARSSL_ZLIB_SUPPORT */
734
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
736
737 return( 0 );
738}
739
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200740#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000741void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742{
743 md5_context md5;
744 sha1_context sha1;
745 unsigned char pad_1[48];
746 unsigned char pad_2[48];
747
Paul Bakker380da532012-04-18 16:10:25 +0000748 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker48916f92012-09-16 19:57:18 +0000750 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
751 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Paul Bakker380da532012-04-18 16:10:25 +0000753 memset( pad_1, 0x36, 48 );
754 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakker48916f92012-09-16 19:57:18 +0000756 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000757 md5_update( &md5, pad_1, 48 );
758 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Paul Bakker380da532012-04-18 16:10:25 +0000760 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000761 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000762 md5_update( &md5, pad_2, 48 );
763 md5_update( &md5, hash, 16 );
764 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakker48916f92012-09-16 19:57:18 +0000766 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000767 sha1_update( &sha1, pad_1, 40 );
768 sha1_finish( &sha1, hash + 16 );
769
770 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000771 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000772 sha1_update( &sha1, pad_2, 40 );
773 sha1_update( &sha1, hash + 16, 20 );
774 sha1_finish( &sha1, hash + 16 );
775
776 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
777 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
778
779 return;
780}
Paul Bakker9af723c2014-05-01 13:03:14 +0200781#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000782
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000784void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
785{
786 md5_context md5;
787 sha1_context sha1;
788
789 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
790
Paul Bakker48916f92012-09-16 19:57:18 +0000791 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
792 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000793
Paul Bakker48916f92012-09-16 19:57:18 +0000794 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000795 sha1_finish( &sha1, hash + 16 );
796
797 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
798 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
799
800 return;
801}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200802#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000803
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#if defined(POLARSSL_SSL_PROTO_TLS1_2)
805#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000806void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
807{
Paul Bakker9e36f042013-06-30 14:34:05 +0200808 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000809
810 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
811
Paul Bakker9e36f042013-06-30 14:34:05 +0200812 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
813 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000814
815 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
816 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
817
818 return;
819}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000821
Paul Bakker9e36f042013-06-30 14:34:05 +0200822#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000823void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
824{
Paul Bakker9e36f042013-06-30 14:34:05 +0200825 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000826
827 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
828
Paul Bakker9e36f042013-06-30 14:34:05 +0200829 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
830 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Paul Bakkerca4ab492012-04-18 14:23:57 +0000832 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
834
835 return;
836}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200837#endif /* POLARSSL_SHA512_C */
838#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200840#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200841int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
842{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200843 unsigned char *p = ssl->handshake->premaster;
844 unsigned char *end = p + sizeof( ssl->handshake->premaster );
845
846 /*
847 * PMS = struct {
848 * opaque other_secret<0..2^16-1>;
849 * opaque psk<0..2^16-1>;
850 * };
851 * with "other_secret" depending on the particular key exchange
852 */
853#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
854 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
855 {
856 if( end - p < 2 + (int) ssl->psk_len )
857 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
858
859 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
860 *(p++) = (unsigned char)( ssl->psk_len );
861 p += ssl->psk_len;
862 }
863 else
864#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200865#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
866 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
867 {
868 /*
869 * other_secret already set by the ClientKeyExchange message,
870 * and is 48 bytes long
871 */
872 *p++ = 0;
873 *p++ = 48;
874 p += 48;
875 }
876 else
877#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200878#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
879 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
880 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200881 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200882 size_t len = ssl->handshake->dhm_ctx.len;
883
884 if( end - p < 2 + (int) len )
885 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
886
887 *(p++) = (unsigned char)( len >> 8 );
888 *(p++) = (unsigned char)( len );
889 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
890 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
891 {
892 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
893 return( ret );
894 }
895 p += len;
896
897 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
898 }
899 else
900#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
901#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
902 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
903 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200904 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200905 size_t zlen;
906
907 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
908 p + 2, end - (p + 2),
909 ssl->f_rng, ssl->p_rng ) ) != 0 )
910 {
911 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
912 return( ret );
913 }
914
915 *(p++) = (unsigned char)( zlen >> 8 );
916 *(p++) = (unsigned char)( zlen );
917 p += zlen;
918
919 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
920 }
921 else
922#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
923 {
924 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
925 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
926 }
927
928 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100929 if( end - p < 2 + (int) ssl->psk_len )
930 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
931
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
933 *(p++) = (unsigned char)( ssl->psk_len );
934 memcpy( p, ssl->psk, ssl->psk_len );
935 p += ssl->psk_len;
936
937 ssl->handshake->pmslen = p - ssl->handshake->premaster;
938
939 return( 0 );
940}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200941#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200942
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200943#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000944/*
945 * SSLv3.0 MAC functions
946 */
Paul Bakker68884e32013-01-07 18:20:04 +0100947static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
948 unsigned char *buf, size_t len,
949 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000950{
951 unsigned char header[11];
952 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100953 int padlen = 0;
954 int md_size = md_get_size( md_ctx->md_info );
955 int md_type = md_get_type( md_ctx->md_info );
956
957 if( md_type == POLARSSL_MD_MD5 )
958 padlen = 48;
959 else if( md_type == POLARSSL_MD_SHA1 )
960 padlen = 40;
961 else if( md_type == POLARSSL_MD_SHA256 )
962 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100963 else if( md_type == POLARSSL_MD_SHA384 )
964 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
966 memcpy( header, ctr, 8 );
967 header[ 8] = (unsigned char) type;
968 header[ 9] = (unsigned char)( len >> 8 );
969 header[10] = (unsigned char)( len );
970
Paul Bakker68884e32013-01-07 18:20:04 +0100971 memset( padding, 0x36, padlen );
972 md_starts( md_ctx );
973 md_update( md_ctx, secret, md_size );
974 md_update( md_ctx, padding, padlen );
975 md_update( md_ctx, header, 11 );
976 md_update( md_ctx, buf, len );
977 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Paul Bakker68884e32013-01-07 18:20:04 +0100979 memset( padding, 0x5C, padlen );
980 md_starts( md_ctx );
981 md_update( md_ctx, secret, md_size );
982 md_update( md_ctx, padding, padlen );
983 md_update( md_ctx, buf + len, md_size );
984 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000985}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200986#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000987
Paul Bakker5121ce52009-01-03 21:22:43 +0000988/*
989 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200990 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000991static int ssl_encrypt_buf( ssl_context *ssl )
992{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200993 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
996
997 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200998 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001000#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1001 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1002 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1003 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
1004 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001005 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001006#if defined(POLARSSL_SSL_PROTO_SSL3)
1007 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1008 {
1009 ssl_mac( &ssl->transform_out->md_ctx_enc,
1010 ssl->transform_out->mac_enc,
1011 ssl->out_msg, ssl->out_msglen,
1012 ssl->out_ctr, ssl->out_msgtype );
1013 }
1014 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001015#endif
1016#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001017 defined(POLARSSL_SSL_PROTO_TLS1_2)
1018 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1019 {
1020 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1021 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1022 ssl->out_msg, ssl->out_msglen );
1023 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1024 ssl->out_msg + ssl->out_msglen );
1025 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1026 }
1027 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001028#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001029 {
1030 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1031 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1032 }
1033
1034 SSL_DEBUG_BUF( 4, "computed mac",
1035 ssl->out_msg + ssl->out_msglen,
1036 ssl->transform_out->maclen );
1037
1038 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001039 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001040#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001042 /*
1043 * Encrypt
1044 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001045#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001046 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1047 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001049 int ret;
1050 size_t olen = 0;
1051
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1053 "including %d bytes of padding",
1054 ssl->out_msglen, 0 ) );
1055
1056 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1057 ssl->out_msg, ssl->out_msglen );
1058
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001059 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001060 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001061 ssl->transform_out->ivlen,
1062 ssl->out_msg, ssl->out_msglen,
1063 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001064 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001065 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001066 return( ret );
1067 }
1068
1069 if( ssl->out_msglen != olen )
1070 {
1071 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1072 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001073 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 }
Paul Bakker68884e32013-01-07 18:20:04 +01001076 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001077#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001078#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001079 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1080 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001081 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001082 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001083 unsigned char *enc_msg;
1084 unsigned char add_data[13];
1085 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1086
Paul Bakkerca4ab492012-04-18 14:23:57 +00001087 memcpy( add_data, ssl->out_ctr, 8 );
1088 add_data[8] = ssl->out_msgtype;
1089 add_data[9] = ssl->major_ver;
1090 add_data[10] = ssl->minor_ver;
1091 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1092 add_data[12] = ssl->out_msglen & 0xFF;
1093
1094 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1095 add_data, 13 );
1096
Paul Bakker68884e32013-01-07 18:20:04 +01001097 /*
1098 * Generate IV
1099 */
1100 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001101 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1102 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001103 if( ret != 0 )
1104 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001105
Paul Bakker68884e32013-01-07 18:20:04 +01001106 memcpy( ssl->out_iv,
1107 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1108 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001109
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001110 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001111 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001112
Paul Bakker68884e32013-01-07 18:20:04 +01001113 /*
1114 * Fix pointer positions and message length with added IV
1115 */
1116 enc_msg = ssl->out_msg;
1117 enc_msglen = ssl->out_msglen;
1118 ssl->out_msglen += ssl->transform_out->ivlen -
1119 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001120
Paul Bakker68884e32013-01-07 18:20:04 +01001121 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1122 "including %d bytes of padding",
1123 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001124
Paul Bakker68884e32013-01-07 18:20:04 +01001125 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1126 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001127
Paul Bakker68884e32013-01-07 18:20:04 +01001128 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001129 * Encrypt
1130 */
1131 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1132 ssl->transform_out->iv_enc,
1133 ssl->transform_out->ivlen ) ) != 0 ||
1134 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1135 {
1136 return( ret );
1137 }
1138
1139 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1140 add_data, 13 ) ) != 0 )
1141 {
1142 return( ret );
1143 }
1144
1145 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1146 enc_msg, enc_msglen,
1147 enc_msg, &olen ) ) != 0 )
1148 {
1149 return( ret );
1150 }
1151 totlen = olen;
1152
1153 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1154 enc_msg + olen, &olen ) ) != 0 )
1155 {
1156 return( ret );
1157 }
1158 totlen += olen;
1159
1160 if( totlen != enc_msglen )
1161 {
1162 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1163 return( -1 );
1164 }
1165
1166 /*
1167 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001168 */
1169 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001170
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001171 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1172 enc_msg + enc_msglen, 16 ) ) != 0 )
1173 {
1174 return( ret );
1175 }
Paul Bakker68884e32013-01-07 18:20:04 +01001176
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001177 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001178 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 else
Paul Bakker68884e32013-01-07 18:20:04 +01001180#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001181#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1182 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001183 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1184 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001186 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001187 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001188 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001189
Paul Bakker48916f92012-09-16 19:57:18 +00001190 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1191 ssl->transform_out->ivlen;
1192 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 padlen = 0;
1194
1195 for( i = 0; i <= padlen; i++ )
1196 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1197
1198 ssl->out_msglen += padlen + 1;
1199
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001200 enc_msglen = ssl->out_msglen;
1201 enc_msg = ssl->out_msg;
1202
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001203#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001204 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001205 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1206 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001207 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001208 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001209 {
1210 /*
1211 * Generate IV
1212 */
Paul Bakker48916f92012-09-16 19:57:18 +00001213 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1214 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001215 if( ret != 0 )
1216 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001217
Paul Bakker92be97b2013-01-02 17:30:03 +01001218 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001219 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220
1221 /*
1222 * Fix pointer positions and message length with added IV
1223 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001224 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001225 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001226 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001227 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001228#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001229
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001231 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001232 ssl->out_msglen, ssl->transform_out->ivlen,
1233 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
1235 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001236 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001238 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001239 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001240 ssl->transform_out->ivlen,
1241 enc_msg, enc_msglen,
1242 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001243 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001244 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001245 return( ret );
1246 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001247
Paul Bakkercca5b812013-08-31 17:40:26 +02001248 if( enc_msglen != olen )
1249 {
1250 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1251 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001252 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001253 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001254
1255#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001256 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1257 {
1258 /*
1259 * Save IV in SSL3 and TLS1
1260 */
1261 memcpy( ssl->transform_out->iv_enc,
1262 ssl->transform_out->cipher_ctx_enc.iv,
1263 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001265#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001267 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001268#endif /* POLARSSL_CIPHER_MODE_CBC &&
1269 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001270 {
1271 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1272 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1273 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001274
Paul Bakkerca4ab492012-04-18 14:23:57 +00001275 for( i = 8; i > 0; i-- )
1276 if( ++ssl->out_ctr[i - 1] != 0 )
1277 break;
1278
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001279 /* The loops goes to its end iff the counter is wrapping */
1280 if( i == 0 )
1281 {
1282 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1283 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1284 }
1285
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1287
1288 return( 0 );
1289}
1290
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001291#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001292
Paul Bakker5121ce52009-01-03 21:22:43 +00001293static int ssl_decrypt_buf( ssl_context *ssl )
1294{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001295 size_t i;
1296#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1297 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1298 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1299 size_t padlen = 0, correct = 1;
1300#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
1302 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1303
Paul Bakker48916f92012-09-16 19:57:18 +00001304 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 {
1306 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001307 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001308 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 }
1310
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001311#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001312 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1313 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001314 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001315 int ret;
1316 size_t olen = 0;
1317
Paul Bakker68884e32013-01-07 18:20:04 +01001318 padlen = 0;
1319
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001320 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001321 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001322 ssl->transform_in->ivlen,
1323 ssl->in_msg, ssl->in_msglen,
1324 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001325 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001326 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001327 return( ret );
1328 }
1329
1330 if( ssl->in_msglen != olen )
1331 {
1332 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001333 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 }
Paul Bakker68884e32013-01-07 18:20:04 +01001336 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001337#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001338#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001339 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1340 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001341 {
1342 unsigned char *dec_msg;
1343 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001344 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001345 unsigned char add_data[13];
1346 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1347
Paul Bakker68884e32013-01-07 18:20:04 +01001348 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1349 ssl->transform_in->fixed_ivlen );
1350 dec_msglen -= 16;
1351 dec_msg = ssl->in_msg;
1352 dec_msg_result = ssl->in_msg;
1353 ssl->in_msglen = dec_msglen;
1354
1355 memcpy( add_data, ssl->in_ctr, 8 );
1356 add_data[8] = ssl->in_msgtype;
1357 add_data[9] = ssl->major_ver;
1358 add_data[10] = ssl->minor_ver;
1359 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1360 add_data[12] = ssl->in_msglen & 0xFF;
1361
1362 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1363 add_data, 13 );
1364
1365 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1366 ssl->in_iv,
1367 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1368
1369 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1370 ssl->transform_in->ivlen );
1371 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1372
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001373 /*
1374 * Decrypt
1375 */
1376 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1377 ssl->transform_in->iv_dec,
1378 ssl->transform_in->ivlen ) ) != 0 ||
1379 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001380 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001381 return( ret );
1382 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001383
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001384 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1385 add_data, 13 ) ) != 0 )
1386 {
1387 return( ret );
1388 }
1389
1390 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1391 dec_msg, dec_msglen,
1392 dec_msg_result, &olen ) ) != 0 )
1393 {
1394 return( ret );
1395 }
1396 totlen = olen;
1397
1398 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1399 dec_msg_result + olen, &olen ) ) != 0 )
1400 {
1401 return( ret );
1402 }
1403 totlen += olen;
1404
1405 if( totlen != dec_msglen )
1406 {
1407 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1408 return( -1 );
1409 }
1410
1411 /*
1412 * Authenticate
1413 */
1414 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1415 dec_msg + dec_msglen, 16 ) ) != 0 )
1416 {
1417 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001418 return( POLARSSL_ERR_SSL_INVALID_MAC );
1419 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001420
Paul Bakkerca4ab492012-04-18 14:23:57 +00001421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 else
Paul Bakker68884e32013-01-07 18:20:04 +01001423#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001424#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1425 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001426 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1427 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 {
Paul Bakker45829992013-01-03 14:52:21 +01001429 /*
1430 * Decrypt and check the padding
1431 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001432 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001433 unsigned char *dec_msg;
1434 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001435 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001436 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001437 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001438
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 /*
Paul Bakker45829992013-01-03 14:52:21 +01001440 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 */
Paul Bakker48916f92012-09-16 19:57:18 +00001442 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 {
1444 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001445 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001446 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
1448
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001449#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001450 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1451 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001452#endif
Paul Bakker45829992013-01-03 14:52:21 +01001453
1454 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1455 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1456 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001457 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1458 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1459 ssl->transform_in->ivlen,
1460 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001461 return( POLARSSL_ERR_SSL_INVALID_MAC );
1462 }
1463
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001464 dec_msglen = ssl->in_msglen;
1465 dec_msg = ssl->in_msg;
1466 dec_msg_result = ssl->in_msg;
1467
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001468#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001469 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001470 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001471 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001472 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001473 {
Paul Bakker48916f92012-09-16 19:57:18 +00001474 dec_msglen -= ssl->transform_in->ivlen;
1475 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001476
Paul Bakker48916f92012-09-16 19:57:18 +00001477 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001478 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001479 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001480#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001481
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001482 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001483 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001484 ssl->transform_in->ivlen,
1485 dec_msg, dec_msglen,
1486 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001487 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001488 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001489 return( ret );
1490 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001491
Paul Bakkercca5b812013-08-31 17:40:26 +02001492 if( dec_msglen != olen )
1493 {
1494 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001495 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001496 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001497
1498#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001499 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1500 {
1501 /*
1502 * Save IV in SSL3 and TLS1
1503 */
1504 memcpy( ssl->transform_in->iv_dec,
1505 ssl->transform_in->cipher_ctx_dec.iv,
1506 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001508#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
1510 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001511
1512 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1513 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001514#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001515 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1516 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001517#endif
Paul Bakker45829992013-01-03 14:52:21 +01001518 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001519 correct = 0;
1520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001521
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001522#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1524 {
Paul Bakker48916f92012-09-16 19:57:18 +00001525 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001527#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1529 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001530 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001531#endif
Paul Bakker45829992013-01-03 14:52:21 +01001532 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 }
1534 }
1535 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001536#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001537#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1538 defined(POLARSSL_SSL_PROTO_TLS1_2)
1539 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 {
1541 /*
Paul Bakker45829992013-01-03 14:52:21 +01001542 * TLSv1+: always check the padding up to the first failure
1543 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001545 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001546 size_t padding_idx = ssl->in_msglen - padlen - 1;
1547
Paul Bakker956c9e02013-12-19 14:42:28 +01001548 /*
1549 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001550 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001551 *
Paul Bakker61885c72014-04-25 12:59:03 +02001552 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1553 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001554 *
1555 * In both cases we reset padding_idx to a safe value (0) to
1556 * prevent out-of-buffer reads.
1557 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001558 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001559 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1560 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001561
1562 padding_idx *= correct;
1563
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001564 for( i = 1; i <= 256; i++ )
1565 {
1566 real_count &= ( i <= padlen );
1567 pad_count += real_count *
1568 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1569 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001570
1571 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001572
Paul Bakkerd66f0702013-01-31 16:57:45 +01001573#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001574 if( padlen > 0 && correct == 0)
1575 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001576#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001577 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001579 else
1580#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1581 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001582 {
1583 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001584 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001587 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001588#endif /* POLARSSL_CIPHER_MODE_CBC &&
1589 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001590 {
1591 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1592 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
1595 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1596 ssl->in_msg, ssl->in_msglen );
1597
1598 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001599 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001601#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1602 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1603 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1604 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1605 POLARSSL_MODE_GCM )
1606 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001607 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1608
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001609 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001611 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1612 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001614 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001616#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001617 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1618 {
1619 ssl_mac( &ssl->transform_in->md_ctx_dec,
1620 ssl->transform_in->mac_dec,
1621 ssl->in_msg, ssl->in_msglen,
1622 ssl->in_ctr, ssl->in_msgtype );
1623 }
1624 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001625#endif /* POLARSSL_SSL_PROTO_SSL3 */
1626#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001627 defined(POLARSSL_SSL_PROTO_TLS1_2)
1628 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1629 {
1630 /*
1631 * Process MAC and always update for padlen afterwards to make
1632 * total time independent of padlen
1633 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001634 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001635 *
1636 * Known timing attacks:
1637 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1638 *
1639 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1640 * correctly. (We round down instead of up, so -56 is the correct
1641 * value for our calculations instead of -55)
1642 */
1643 size_t j, extra_run = 0;
1644 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1645 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001646
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001647 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001648
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001649 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1650 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1651 ssl->in_msglen );
1652 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1653 ssl->in_msg + ssl->in_msglen );
1654 for( j = 0; j < extra_run; j++ )
1655 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001656
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001657 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1658 }
1659 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001660#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001661 POLARSSL_SSL_PROTO_TLS1_2 */
1662 {
1663 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1664 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001667 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1668 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1669 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001670
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001671 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001672 ssl->transform_in->maclen ) != 0 )
1673 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001674#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001675 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001676#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001677 correct = 0;
1678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001680 /*
1681 * Finally check the correct flag
1682 */
1683 if( correct == 0 )
1684 return( POLARSSL_ERR_SSL_INVALID_MAC );
1685 }
1686#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
1688 if( ssl->in_msglen == 0 )
1689 {
1690 ssl->nb_zero++;
1691
1692 /*
1693 * Three or more empty messages may be a DoS attack
1694 * (excessive CPU consumption).
1695 */
1696 if( ssl->nb_zero > 3 )
1697 {
1698 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1699 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001700 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 }
1702 }
1703 else
1704 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001705
Paul Bakker23986e52011-04-24 08:57:21 +00001706 for( i = 8; i > 0; i-- )
1707 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001708 break;
1709
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001710 /* The loops goes to its end iff the counter is wrapping */
1711 if( i == 0 )
1712 {
1713 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1714 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1715 }
1716
Paul Bakker5121ce52009-01-03 21:22:43 +00001717 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1718
1719 return( 0 );
1720}
1721
Paul Bakker2770fbd2012-07-03 13:30:23 +00001722#if defined(POLARSSL_ZLIB_SUPPORT)
1723/*
1724 * Compression/decompression functions
1725 */
1726static int ssl_compress_buf( ssl_context *ssl )
1727{
1728 int ret;
1729 unsigned char *msg_post = ssl->out_msg;
1730 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001731 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001732
1733 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1734
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001735 if( len_pre == 0 )
1736 return( 0 );
1737
Paul Bakker2770fbd2012-07-03 13:30:23 +00001738 memcpy( msg_pre, ssl->out_msg, len_pre );
1739
1740 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1741 ssl->out_msglen ) );
1742
1743 SSL_DEBUG_BUF( 4, "before compression: output payload",
1744 ssl->out_msg, ssl->out_msglen );
1745
Paul Bakker48916f92012-09-16 19:57:18 +00001746 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1747 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1748 ssl->transform_out->ctx_deflate.next_out = msg_post;
1749 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001750
Paul Bakker48916f92012-09-16 19:57:18 +00001751 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001752 if( ret != Z_OK )
1753 {
1754 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1755 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1756 }
1757
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001758 ssl->out_msglen = SSL_BUFFER_LEN -
1759 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001760
Paul Bakker2770fbd2012-07-03 13:30:23 +00001761 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1762 ssl->out_msglen ) );
1763
1764 SSL_DEBUG_BUF( 4, "after compression: output payload",
1765 ssl->out_msg, ssl->out_msglen );
1766
1767 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1768
1769 return( 0 );
1770}
1771
1772static int ssl_decompress_buf( ssl_context *ssl )
1773{
1774 int ret;
1775 unsigned char *msg_post = ssl->in_msg;
1776 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001777 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001778
1779 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1780
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001781 if( len_pre == 0 )
1782 return( 0 );
1783
Paul Bakker2770fbd2012-07-03 13:30:23 +00001784 memcpy( msg_pre, ssl->in_msg, len_pre );
1785
1786 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1787 ssl->in_msglen ) );
1788
1789 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1790 ssl->in_msg, ssl->in_msglen );
1791
Paul Bakker48916f92012-09-16 19:57:18 +00001792 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1793 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1794 ssl->transform_in->ctx_inflate.next_out = msg_post;
1795 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001796
Paul Bakker48916f92012-09-16 19:57:18 +00001797 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001798 if( ret != Z_OK )
1799 {
1800 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1801 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1802 }
1803
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001804 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1805 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001806
Paul Bakker2770fbd2012-07-03 13:30:23 +00001807 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1808 ssl->in_msglen ) );
1809
1810 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1811 ssl->in_msg, ssl->in_msglen );
1812
1813 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1814
1815 return( 0 );
1816}
1817#endif /* POLARSSL_ZLIB_SUPPORT */
1818
Paul Bakker5121ce52009-01-03 21:22:43 +00001819/*
1820 * Fill the input message buffer
1821 */
Paul Bakker23986e52011-04-24 08:57:21 +00001822int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001823{
Paul Bakker23986e52011-04-24 08:57:21 +00001824 int ret;
1825 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001826
1827 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1828
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001829 if( nb_want > SSL_BUFFER_LEN - 8 )
1830 {
1831 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1832 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1833 }
1834
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 while( ssl->in_left < nb_want )
1836 {
1837 len = nb_want - ssl->in_left;
1838 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1839
1840 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1841 ssl->in_left, nb_want ) );
1842 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1843
Paul Bakker831a7552011-05-18 13:32:51 +00001844 if( ret == 0 )
1845 return( POLARSSL_ERR_SSL_CONN_EOF );
1846
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 if( ret < 0 )
1848 return( ret );
1849
1850 ssl->in_left += ret;
1851 }
1852
1853 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1854
1855 return( 0 );
1856}
1857
1858/*
1859 * Flush any data not yet written
1860 */
1861int ssl_flush_output( ssl_context *ssl )
1862{
1863 int ret;
1864 unsigned char *buf;
1865
1866 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1867
1868 while( ssl->out_left > 0 )
1869 {
1870 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1871 5 + ssl->out_msglen, ssl->out_left ) );
1872
Paul Bakker5bd42292012-12-19 14:40:42 +01001873 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001875
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1877
1878 if( ret <= 0 )
1879 return( ret );
1880
1881 ssl->out_left -= ret;
1882 }
1883
1884 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1885
1886 return( 0 );
1887}
1888
1889/*
1890 * Record layer functions
1891 */
1892int ssl_write_record( ssl_context *ssl )
1893{
Paul Bakker05ef8352012-05-08 09:17:57 +00001894 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001895 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
1897 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1898
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1900 {
1901 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1902 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1903 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1904
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001905 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1906 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 }
1908
Paul Bakker2770fbd2012-07-03 13:30:23 +00001909#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001910 if( ssl->transform_out != NULL &&
1911 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001912 {
1913 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1914 {
1915 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1916 return( ret );
1917 }
1918
1919 len = ssl->out_msglen;
1920 }
1921#endif /*POLARSSL_ZLIB_SUPPORT */
1922
Paul Bakker05ef8352012-05-08 09:17:57 +00001923#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1924 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001926 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Paul Bakker05ef8352012-05-08 09:17:57 +00001928 ret = ssl_hw_record_write( ssl );
1929 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1930 {
1931 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1932 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1933 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001934
1935 if( ret == 0 )
1936 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001937 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001938#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001939 if( !done )
1940 {
1941 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1942 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1943 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1945 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001946
Paul Bakker48916f92012-09-16 19:57:18 +00001947 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001948 {
1949 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1950 {
1951 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1952 return( ret );
1953 }
1954
1955 len = ssl->out_msglen;
1956 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1957 ssl->out_hdr[4] = (unsigned char)( len );
1958 }
1959
1960 ssl->out_left = 5 + ssl->out_msglen;
1961
1962 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1963 "version = [%d:%d], msglen = %d",
1964 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1965 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1966
1967 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001968 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 }
1970
Paul Bakker5121ce52009-01-03 21:22:43 +00001971 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1972 {
1973 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1974 return( ret );
1975 }
1976
1977 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1978
1979 return( 0 );
1980}
1981
1982int ssl_read_record( ssl_context *ssl )
1983{
Paul Bakker05ef8352012-05-08 09:17:57 +00001984 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
1986 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1987
Paul Bakker68884e32013-01-07 18:20:04 +01001988 SSL_DEBUG_BUF( 4, "input record from network",
1989 ssl->in_hdr, 5 + ssl->in_msglen );
1990
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 if( ssl->in_hslen != 0 &&
1992 ssl->in_hslen < ssl->in_msglen )
1993 {
1994 /*
1995 * Get next Handshake message in the current record
1996 */
1997 ssl->in_msglen -= ssl->in_hslen;
1998
Paul Bakker8934a982011-08-05 11:11:53 +00001999 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002000 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
2002 ssl->in_hslen = 4;
2003 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2004
2005 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2006 " %d, type = %d, hslen = %d",
2007 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2008
2009 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2010 {
2011 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002012 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 }
2014
2015 if( ssl->in_msglen < ssl->in_hslen )
2016 {
2017 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002018 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 }
2020
Paul Bakker4224bc02014-04-08 14:36:50 +02002021 if( ssl->state != SSL_HANDSHAKE_OVER )
2022 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002023
2024 return( 0 );
2025 }
2026
2027 ssl->in_hslen = 0;
2028
2029 /*
2030 * Read the record header and validate it
2031 */
2032 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2033 {
2034 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2035 return( ret );
2036 }
2037
2038 ssl->in_msgtype = ssl->in_hdr[0];
2039 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2040
2041 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2042 "version = [%d:%d], msglen = %d",
2043 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2044 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2045
2046 if( ssl->in_hdr[1] != ssl->major_ver )
2047 {
2048 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002049 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 }
2051
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002052 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 {
2054 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002055 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 }
2057
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002058 /* Sanity check (outer boundaries) */
2059 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2060 {
2061 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2062 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2063 }
2064
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002066 * Make sure the message length is acceptable for the current transform
2067 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 */
Paul Bakker48916f92012-09-16 19:57:18 +00002069 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002071 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 {
2073 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002074 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 }
2076 }
2077 else
2078 {
Paul Bakker48916f92012-09-16 19:57:18 +00002079 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 {
2081 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002082 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 }
2084
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002085#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002087 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 {
2089 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002090 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002094#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2095 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 /*
2097 * TLS encrypted messages can have up to 256 bytes of padding
2098 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002099 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002100 ssl->in_msglen > ssl->transform_in->minlen +
2101 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 {
2103 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002104 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002106#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 }
2108
2109 /*
2110 * Read and optionally decrypt the message contents
2111 */
2112 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2113 {
2114 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2115 return( ret );
2116 }
2117
2118 SSL_DEBUG_BUF( 4, "input record from network",
2119 ssl->in_hdr, 5 + ssl->in_msglen );
2120
Paul Bakker05ef8352012-05-08 09:17:57 +00002121#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2122 if( ssl_hw_record_read != NULL)
2123 {
2124 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2125
2126 ret = ssl_hw_record_read( ssl );
2127 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2128 {
2129 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2130 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2131 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002132
2133 if( ret == 0 )
2134 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002135 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002136#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002137 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 {
2139 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2140 {
Paul Bakker40865c82013-01-31 17:13:13 +01002141#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2142 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2143 {
2144 ssl_send_alert_message( ssl,
2145 SSL_ALERT_LEVEL_FATAL,
2146 SSL_ALERT_MSG_BAD_RECORD_MAC );
2147 }
2148#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2150 return( ret );
2151 }
2152
2153 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2154 ssl->in_msg, ssl->in_msglen );
2155
2156 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2157 {
2158 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002159 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 }
2161 }
2162
Paul Bakker2770fbd2012-07-03 13:30:23 +00002163#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002164 if( ssl->transform_in != NULL &&
2165 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002166 {
2167 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2168 {
2169 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2170 return( ret );
2171 }
2172
2173 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2174 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2175 }
2176#endif /* POLARSSL_ZLIB_SUPPORT */
2177
Paul Bakker0a925182012-04-16 06:46:41 +00002178 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2179 ssl->in_msgtype != SSL_MSG_ALERT &&
2180 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2181 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2182 {
2183 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2184
Paul Bakker48916f92012-09-16 19:57:18 +00002185 if( ( ret = ssl_send_alert_message( ssl,
2186 SSL_ALERT_LEVEL_FATAL,
2187 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002188 {
2189 return( ret );
2190 }
2191
2192 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2193 }
2194
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2196 {
2197 ssl->in_hslen = 4;
2198 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2199
2200 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2201 " %d, type = %d, hslen = %d",
2202 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2203
2204 /*
2205 * Additional checks to validate the handshake header
2206 */
2207 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2208 {
2209 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002210 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 }
2212
2213 if( ssl->in_msglen < ssl->in_hslen )
2214 {
2215 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002216 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 }
2218
Paul Bakker48916f92012-09-16 19:57:18 +00002219 if( ssl->state != SSL_HANDSHAKE_OVER )
2220 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 }
2222
2223 if( ssl->in_msgtype == SSL_MSG_ALERT )
2224 {
2225 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2226 ssl->in_msg[0], ssl->in_msg[1] ) );
2227
2228 /*
2229 * Ignore non-fatal alerts, except close_notify
2230 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002231 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002233 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2234 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002235 /**
2236 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2237 * error identifier.
2238 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002239 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 }
2241
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002242 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2243 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 {
2245 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002246 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 }
2248 }
2249
2250 ssl->in_left = 0;
2251
2252 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2253
2254 return( 0 );
2255}
2256
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002257int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2258{
2259 int ret;
2260
2261 if( ( ret = ssl_send_alert_message( ssl,
2262 SSL_ALERT_LEVEL_FATAL,
2263 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2264 {
2265 return( ret );
2266 }
2267
2268 return( 0 );
2269}
2270
Paul Bakker0a925182012-04-16 06:46:41 +00002271int ssl_send_alert_message( ssl_context *ssl,
2272 unsigned char level,
2273 unsigned char message )
2274{
2275 int ret;
2276
2277 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2278
2279 ssl->out_msgtype = SSL_MSG_ALERT;
2280 ssl->out_msglen = 2;
2281 ssl->out_msg[0] = level;
2282 ssl->out_msg[1] = message;
2283
2284 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2285 {
2286 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2287 return( ret );
2288 }
2289
2290 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2291
2292 return( 0 );
2293}
2294
Paul Bakker5121ce52009-01-03 21:22:43 +00002295/*
2296 * Handshake functions
2297 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002298#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2299 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2300 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2301 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2302 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2303 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2304 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002305int ssl_write_certificate( ssl_context *ssl )
2306{
Paul Bakkered27a042013-04-18 22:46:23 +02002307 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002308 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002309
2310 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2311
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002312 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002313 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2314 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002315 {
2316 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2317 ssl->state++;
2318 return( 0 );
2319 }
2320
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002321 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002322 return( ret );
2323}
2324
2325int ssl_parse_certificate( ssl_context *ssl )
2326{
2327 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2328 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2329
2330 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2331
2332 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002333 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2334 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002335 {
2336 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2337 ssl->state++;
2338 return( 0 );
2339 }
2340
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002341 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002342 return( ret );
2343}
2344#else
2345int ssl_write_certificate( ssl_context *ssl )
2346{
2347 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2348 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002349 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2351
2352 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2353
2354 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002355 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2356 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002357 {
2358 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2359 ssl->state++;
2360 return( 0 );
2361 }
2362
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 if( ssl->endpoint == SSL_IS_CLIENT )
2364 {
2365 if( ssl->client_auth == 0 )
2366 {
2367 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2368 ssl->state++;
2369 return( 0 );
2370 }
2371
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002372#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 /*
2374 * If using SSLv3 and got no cert, send an Alert message
2375 * (otherwise an empty Certificate message will be sent).
2376 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002377 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2379 {
2380 ssl->out_msglen = 2;
2381 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002382 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2383 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002384
2385 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2386 goto write_msg;
2387 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002388#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 }
2390 else /* SSL_IS_SERVER */
2391 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002392 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 {
2394 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002395 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 }
2397 }
2398
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002399 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
2401 /*
2402 * 0 . 0 handshake type
2403 * 1 . 3 handshake length
2404 * 4 . 6 length of all certs
2405 * 7 . 9 length of cert. 1
2406 * 10 . n-1 peer certificate
2407 * n . n+2 length of cert. 2
2408 * n+3 . ... upper level cert, etc.
2409 */
2410 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002411 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
Paul Bakker29087132010-03-21 21:03:34 +00002413 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 {
2415 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002416 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 {
2418 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2419 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002420 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 }
2422
2423 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2424 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2425 ssl->out_msg[i + 2] = (unsigned char)( n );
2426
2427 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2428 i += n; crt = crt->next;
2429 }
2430
2431 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2432 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2433 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2434
2435 ssl->out_msglen = i;
2436 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2437 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2438
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002439#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002440write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002441#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
2443 ssl->state++;
2444
2445 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2446 {
2447 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2448 return( ret );
2449 }
2450
2451 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2452
Paul Bakkered27a042013-04-18 22:46:23 +02002453 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002454}
2455
2456int ssl_parse_certificate( ssl_context *ssl )
2457{
Paul Bakkered27a042013-04-18 22:46:23 +02002458 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002459 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002460 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
2462 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2463
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002464 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002465 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2466 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002467 {
2468 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2469 ssl->state++;
2470 return( 0 );
2471 }
2472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002474 ( ssl->authmode == SSL_VERIFY_NONE ||
2475 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002477 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2479 ssl->state++;
2480 return( 0 );
2481 }
2482
2483 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2484 {
2485 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2486 return( ret );
2487 }
2488
2489 ssl->state++;
2490
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002491#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 /*
2493 * Check if the client sent an empty certificate
2494 */
2495 if( ssl->endpoint == SSL_IS_SERVER &&
2496 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2497 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002498 if( ssl->in_msglen == 2 &&
2499 ssl->in_msgtype == SSL_MSG_ALERT &&
2500 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2501 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 {
2503 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2504
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002505 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2507 return( 0 );
2508 else
Paul Bakker40e46942009-01-03 21:51:57 +00002509 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 }
2511 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002512#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002514#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2515 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 if( ssl->endpoint == SSL_IS_SERVER &&
2517 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2518 {
2519 if( ssl->in_hslen == 7 &&
2520 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2521 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2522 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2523 {
2524 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2525
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002526 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002528 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 else
2530 return( 0 );
2531 }
2532 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2534 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
2536 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2537 {
2538 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002539 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 }
2541
2542 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2543 {
2544 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002545 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 }
2547
2548 /*
2549 * Same message structure as in ssl_write_certificate()
2550 */
2551 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2552
2553 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2554 {
2555 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002556 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 }
2558
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002559 /* In case we tried to reuse a session but it failed */
2560 if( ssl->session_negotiate->peer_cert != NULL )
2561 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002562 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002563 polarssl_free( ssl->session_negotiate->peer_cert );
2564 }
2565
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002566 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2567 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 {
2569 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002570 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002571 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 }
2573
Paul Bakkerb6b09562013-09-18 14:17:41 +02002574 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
2576 i = 7;
2577
2578 while( i < ssl->in_hslen )
2579 {
2580 if( ssl->in_msg[i] != 0 )
2581 {
2582 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002583 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 }
2585
2586 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2587 | (unsigned int) ssl->in_msg[i + 2];
2588 i += 3;
2589
2590 if( n < 128 || i + n > ssl->in_hslen )
2591 {
2592 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002593 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 }
2595
Paul Bakkerddf26b42013-09-18 13:46:23 +02002596 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2597 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 if( ret != 0 )
2599 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002600 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 return( ret );
2602 }
2603
2604 i += n;
2605 }
2606
Paul Bakker48916f92012-09-16 19:57:18 +00002607 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002609 /*
2610 * On client, make sure the server cert doesn't change during renego to
2611 * avoid "triple handshake" attack: https://secure-resumption.com/
2612 */
2613 if( ssl->endpoint == SSL_IS_CLIENT &&
2614 ssl->renegotiation == SSL_RENEGOTIATION )
2615 {
2616 if( ssl->session->peer_cert == NULL )
2617 {
2618 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2619 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2620 }
2621
2622 if( ssl->session->peer_cert->raw.len !=
2623 ssl->session_negotiate->peer_cert->raw.len ||
2624 memcmp( ssl->session->peer_cert->raw.p,
2625 ssl->session_negotiate->peer_cert->raw.p,
2626 ssl->session->peer_cert->raw.len ) != 0 )
2627 {
2628 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2629 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2630 }
2631 }
2632
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 if( ssl->authmode != SSL_VERIFY_NONE )
2634 {
2635 if( ssl->ca_chain == NULL )
2636 {
2637 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002638 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 }
2640
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002641 /*
2642 * Main check: verify certificate
2643 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002644 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2645 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2646 &ssl->session_negotiate->verify_result,
2647 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
2649 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002650 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002652 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002653
2654 /*
2655 * Secondary checks: always done, but change 'ret' only if it was 0
2656 */
2657
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002658#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002659 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002660 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002661
2662 /* If certificate uses an EC key, make sure the curve is OK */
2663 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2664 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2665 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002666 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002667 if( ret == 0 )
2668 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002669 }
2670 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002671#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002673 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2674 ciphersuite_info,
2675 ! ssl->endpoint ) != 0 )
2676 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002677 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002678 if( ret == 0 )
2679 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2680 }
2681
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2683 ret = 0;
2684 }
2685
2686 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2687
2688 return( ret );
2689}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002690#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2691 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2692 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2693 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2694 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2695 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2696 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
2698int ssl_write_change_cipher_spec( ssl_context *ssl )
2699{
2700 int ret;
2701
2702 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2703
2704 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2705 ssl->out_msglen = 1;
2706 ssl->out_msg[0] = 1;
2707
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 ssl->state++;
2709
2710 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2711 {
2712 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2713 return( ret );
2714 }
2715
2716 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2717
2718 return( 0 );
2719}
2720
2721int ssl_parse_change_cipher_spec( ssl_context *ssl )
2722{
2723 int ret;
2724
2725 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2726
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2728 {
2729 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2730 return( ret );
2731 }
2732
2733 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2734 {
2735 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002736 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 }
2738
2739 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2740 {
2741 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002742 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 }
2744
2745 ssl->state++;
2746
2747 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2748
2749 return( 0 );
2750}
2751
Paul Bakker41c83d32013-03-20 14:39:14 +01002752void ssl_optimize_checksum( ssl_context *ssl,
2753 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002754{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002755 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002756
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002757#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2758 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002759 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002760 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002761 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002762#endif
2763#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2764#if defined(POLARSSL_SHA512_C)
2765 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2766 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2767 else
2768#endif
2769#if defined(POLARSSL_SHA256_C)
2770 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002771 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002772 else
2773#endif
2774#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2775 /* Should never happen */
2776 return;
Paul Bakker380da532012-04-18 16:10:25 +00002777}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002778
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002779static void ssl_update_checksum_start( ssl_context *ssl,
2780 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002781{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002782#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2783 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002784 md5_update( &ssl->handshake->fin_md5 , buf, len );
2785 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002786#endif
2787#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2788#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002789 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002790#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002791#if defined(POLARSSL_SHA512_C)
2792 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002793#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002795}
2796
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002797#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2798 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002799static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2800 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002801{
Paul Bakker48916f92012-09-16 19:57:18 +00002802 md5_update( &ssl->handshake->fin_md5 , buf, len );
2803 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002804}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002805#endif
Paul Bakker380da532012-04-18 16:10:25 +00002806
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002807#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2808#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002809static void ssl_update_checksum_sha256( ssl_context *ssl,
2810 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002811{
Paul Bakker9e36f042013-06-30 14:34:05 +02002812 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002813}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002814#endif
Paul Bakker380da532012-04-18 16:10:25 +00002815
Paul Bakker9e36f042013-06-30 14:34:05 +02002816#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002817static void ssl_update_checksum_sha384( ssl_context *ssl,
2818 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002819{
Paul Bakker9e36f042013-06-30 14:34:05 +02002820 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002821}
Paul Bakker769075d2012-11-24 11:26:46 +01002822#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002823#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002824
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002825#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002826static void ssl_calc_finished_ssl(
2827 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002828{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002829 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002830 md5_context md5;
2831 sha1_context sha1;
2832
Paul Bakker5121ce52009-01-03 21:22:43 +00002833 unsigned char padbuf[48];
2834 unsigned char md5sum[16];
2835 unsigned char sha1sum[20];
2836
Paul Bakker48916f92012-09-16 19:57:18 +00002837 ssl_session *session = ssl->session_negotiate;
2838 if( !session )
2839 session = ssl->session;
2840
Paul Bakker1ef83d62012-04-11 12:09:53 +00002841 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2842
Paul Bakker48916f92012-09-16 19:57:18 +00002843 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2844 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
2846 /*
2847 * SSLv3:
2848 * hash =
2849 * MD5( master + pad2 +
2850 * MD5( handshake + sender + master + pad1 ) )
2851 * + SHA1( master + pad2 +
2852 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 */
2854
Paul Bakker90995b52013-06-24 19:20:35 +02002855#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002857 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002858#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Paul Bakker90995b52013-06-24 19:20:35 +02002860#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002862 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002863#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Paul Bakker3c2122f2013-06-24 19:03:14 +02002865 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2866 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Paul Bakker1ef83d62012-04-11 12:09:53 +00002868 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002869
Paul Bakker3c2122f2013-06-24 19:03:14 +02002870 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002871 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002872 md5_update( &md5, padbuf, 48 );
2873 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Paul Bakker3c2122f2013-06-24 19:03:14 +02002875 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002876 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002877 sha1_update( &sha1, padbuf, 40 );
2878 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
Paul Bakker1ef83d62012-04-11 12:09:53 +00002880 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Paul Bakker1ef83d62012-04-11 12:09:53 +00002882 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002883 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002884 md5_update( &md5, padbuf, 48 );
2885 md5_update( &md5, md5sum, 16 );
2886 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Paul Bakker1ef83d62012-04-11 12:09:53 +00002888 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002889 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002890 sha1_update( &sha1, padbuf , 40 );
2891 sha1_update( &sha1, sha1sum, 20 );
2892 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002895
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 memset( &md5, 0, sizeof( md5_context ) );
2897 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
2899 memset( padbuf, 0, sizeof( padbuf ) );
2900 memset( md5sum, 0, sizeof( md5sum ) );
2901 memset( sha1sum, 0, sizeof( sha1sum ) );
2902
2903 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2904}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002905#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002907#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002908static void ssl_calc_finished_tls(
2909 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002910{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002912 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002913 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Paul Bakker48916f92012-09-16 19:57:18 +00002917 ssl_session *session = ssl->session_negotiate;
2918 if( !session )
2919 session = ssl->session;
2920
Paul Bakker1ef83d62012-04-11 12:09:53 +00002921 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Paul Bakker48916f92012-09-16 19:57:18 +00002923 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2924 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Paul Bakker1ef83d62012-04-11 12:09:53 +00002926 /*
2927 * TLSv1:
2928 * hash = PRF( master, finished_label,
2929 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2930 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
Paul Bakker90995b52013-06-24 19:20:35 +02002932#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002933 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2934 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002935#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936
Paul Bakker90995b52013-06-24 19:20:35 +02002937#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002938 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2939 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002940#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002941
2942 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002943 ? "client finished"
2944 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945
2946 md5_finish( &md5, padbuf );
2947 sha1_finish( &sha1, padbuf + 16 );
2948
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002949 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002950 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951
2952 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2953
2954 memset( &md5, 0, sizeof( md5_context ) );
2955 memset( &sha1, 0, sizeof( sha1_context ) );
2956
2957 memset( padbuf, 0, sizeof( padbuf ) );
2958
2959 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2960}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002961#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002963#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2964#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002965static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966 ssl_context *ssl, unsigned char *buf, int from )
2967{
2968 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002969 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002970 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002971 unsigned char padbuf[32];
2972
Paul Bakker48916f92012-09-16 19:57:18 +00002973 ssl_session *session = ssl->session_negotiate;
2974 if( !session )
2975 session = ssl->session;
2976
Paul Bakker380da532012-04-18 16:10:25 +00002977 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002978
Paul Bakker9e36f042013-06-30 14:34:05 +02002979 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002980
2981 /*
2982 * TLSv1.2:
2983 * hash = PRF( master, finished_label,
2984 * Hash( handshake ) )[0.11]
2985 */
2986
Paul Bakker9e36f042013-06-30 14:34:05 +02002987#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002988 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002989 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002990#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002991
2992 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002993 ? "client finished"
2994 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995
Paul Bakker9e36f042013-06-30 14:34:05 +02002996 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002997
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002998 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002999 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003000
3001 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3002
Paul Bakker9e36f042013-06-30 14:34:05 +02003003 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003004
3005 memset( padbuf, 0, sizeof( padbuf ) );
3006
3007 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3008}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003009#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003010
Paul Bakker9e36f042013-06-30 14:34:05 +02003011#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003012static void ssl_calc_finished_tls_sha384(
3013 ssl_context *ssl, unsigned char *buf, int from )
3014{
3015 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003016 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003017 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003018 unsigned char padbuf[48];
3019
Paul Bakker48916f92012-09-16 19:57:18 +00003020 ssl_session *session = ssl->session_negotiate;
3021 if( !session )
3022 session = ssl->session;
3023
Paul Bakker380da532012-04-18 16:10:25 +00003024 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003025
Paul Bakker9e36f042013-06-30 14:34:05 +02003026 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003027
3028 /*
3029 * TLSv1.2:
3030 * hash = PRF( master, finished_label,
3031 * Hash( handshake ) )[0.11]
3032 */
3033
Paul Bakker9e36f042013-06-30 14:34:05 +02003034#if !defined(POLARSSL_SHA512_ALT)
3035 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3036 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003037#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003038
3039 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003040 ? "client finished"
3041 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003042
Paul Bakker9e36f042013-06-30 14:34:05 +02003043 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003044
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003045 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003046 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003047
3048 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3049
Paul Bakker9e36f042013-06-30 14:34:05 +02003050 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003051
3052 memset( padbuf, 0, sizeof( padbuf ) );
3053
3054 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3055}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003056#endif /* POLARSSL_SHA512_C */
3057#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003058
Paul Bakker48916f92012-09-16 19:57:18 +00003059void ssl_handshake_wrapup( ssl_context *ssl )
3060{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003061 int resume = ssl->handshake->resume;
3062
Paul Bakker48916f92012-09-16 19:57:18 +00003063 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3064
3065 /*
3066 * Free our handshake params
3067 */
3068 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003069 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003070 ssl->handshake = NULL;
3071
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003072 if( ssl->renegotiation == SSL_RENEGOTIATION )
3073 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3074
Paul Bakker48916f92012-09-16 19:57:18 +00003075 /*
3076 * Switch in our now active transform context
3077 */
3078 if( ssl->transform )
3079 {
3080 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003081 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003082 }
3083 ssl->transform = ssl->transform_negotiate;
3084 ssl->transform_negotiate = NULL;
3085
Paul Bakker0a597072012-09-25 21:55:46 +00003086 if( ssl->session )
3087 {
3088 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003089 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003090 }
3091 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003092 ssl->session_negotiate = NULL;
3093
Paul Bakker0a597072012-09-25 21:55:46 +00003094 /*
3095 * Add cache entry
3096 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003097 if( ssl->f_set_cache != NULL &&
3098 ssl->session->length != 0 &&
3099 resume == 0 )
3100 {
Paul Bakker0a597072012-09-25 21:55:46 +00003101 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3102 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003103 }
Paul Bakker0a597072012-09-25 21:55:46 +00003104
Paul Bakker48916f92012-09-16 19:57:18 +00003105 ssl->state++;
3106
3107 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3108}
3109
Paul Bakker1ef83d62012-04-11 12:09:53 +00003110int ssl_write_finished( ssl_context *ssl )
3111{
3112 int ret, hash_len;
3113
3114 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3115
Paul Bakker92be97b2013-01-02 17:30:03 +01003116 /*
3117 * Set the out_msg pointer to the correct location based on IV length
3118 */
3119 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3120 {
3121 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3122 ssl->transform_negotiate->fixed_ivlen;
3123 }
3124 else
3125 ssl->out_msg = ssl->out_iv;
3126
Paul Bakker48916f92012-09-16 19:57:18 +00003127 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003128
3129 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3131
Paul Bakker48916f92012-09-16 19:57:18 +00003132 ssl->verify_data_len = hash_len;
3133 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3134
Paul Bakker5121ce52009-01-03 21:22:43 +00003135 ssl->out_msglen = 4 + hash_len;
3136 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3137 ssl->out_msg[0] = SSL_HS_FINISHED;
3138
3139 /*
3140 * In case of session resuming, invert the client and server
3141 * ChangeCipherSpec messages order.
3142 */
Paul Bakker0a597072012-09-25 21:55:46 +00003143 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003144 {
3145 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003146 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003147 else
3148 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3149 }
3150 else
3151 ssl->state++;
3152
Paul Bakker48916f92012-09-16 19:57:18 +00003153 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003154 * Switch to our negotiated transform and session parameters for outbound
3155 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003156 */
3157 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3158 ssl->transform_out = ssl->transform_negotiate;
3159 ssl->session_out = ssl->session_negotiate;
3160 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
Paul Bakker07eb38b2012-12-19 14:42:06 +01003162#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3163 if( ssl_hw_record_activate != NULL)
3164 {
3165 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3166 {
3167 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3168 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3169 }
3170 }
3171#endif
3172
Paul Bakker5121ce52009-01-03 21:22:43 +00003173 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3174 {
3175 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3176 return( ret );
3177 }
3178
3179 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3180
3181 return( 0 );
3182}
3183
3184int ssl_parse_finished( ssl_context *ssl )
3185{
Paul Bakker23986e52011-04-24 08:57:21 +00003186 int ret;
3187 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 unsigned char buf[36];
3189
3190 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3191
Paul Bakker48916f92012-09-16 19:57:18 +00003192 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003193
Paul Bakker48916f92012-09-16 19:57:18 +00003194 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003195 * Switch to our negotiated transform and session parameters for inbound
3196 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003197 */
3198 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3199 ssl->transform_in = ssl->transform_negotiate;
3200 ssl->session_in = ssl->session_negotiate;
3201 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003202
Paul Bakker92be97b2013-01-02 17:30:03 +01003203 /*
3204 * Set the in_msg pointer to the correct location based on IV length
3205 */
3206 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3207 {
3208 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3209 ssl->transform_negotiate->fixed_ivlen;
3210 }
3211 else
3212 ssl->in_msg = ssl->in_iv;
3213
Paul Bakker07eb38b2012-12-19 14:42:06 +01003214#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3215 if( ssl_hw_record_activate != NULL)
3216 {
3217 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3218 {
3219 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3220 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3221 }
3222 }
3223#endif
3224
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3226 {
3227 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3228 return( ret );
3229 }
3230
3231 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3232 {
3233 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003234 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 }
3236
Paul Bakker1ef83d62012-04-11 12:09:53 +00003237 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3239
3240 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3241 ssl->in_hslen != 4 + hash_len )
3242 {
3243 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003244 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 }
3246
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003247 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 {
3249 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003250 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 }
3252
Paul Bakker48916f92012-09-16 19:57:18 +00003253 ssl->verify_data_len = hash_len;
3254 memcpy( ssl->peer_verify_data, buf, hash_len );
3255
Paul Bakker0a597072012-09-25 21:55:46 +00003256 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 {
3258 if( ssl->endpoint == SSL_IS_CLIENT )
3259 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3260
3261 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003262 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003263 }
3264 else
3265 ssl->state++;
3266
3267 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3268
3269 return( 0 );
3270}
3271
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003272static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003273{
3274 if( ssl->transform_negotiate )
3275 ssl_transform_free( ssl->transform_negotiate );
3276 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003277 {
3278 ssl->transform_negotiate =
3279 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003280
3281 if( ssl->transform_negotiate != NULL )
3282 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003283 }
Paul Bakker48916f92012-09-16 19:57:18 +00003284
3285 if( ssl->session_negotiate )
3286 ssl_session_free( ssl->session_negotiate );
3287 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003288 {
3289 ssl->session_negotiate =
3290 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003291
3292 if( ssl->session_negotiate != NULL )
3293 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003294 }
Paul Bakker48916f92012-09-16 19:57:18 +00003295
3296 if( ssl->handshake )
3297 ssl_handshake_free( ssl->handshake );
3298 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003299 {
3300 ssl->handshake = (ssl_handshake_params *)
3301 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003302
3303 if( ssl->handshake != NULL )
3304 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003305 }
Paul Bakker48916f92012-09-16 19:57:18 +00003306
3307 if( ssl->handshake == NULL ||
3308 ssl->transform_negotiate == NULL ||
3309 ssl->session_negotiate == NULL )
3310 {
3311 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3312 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3313 }
3314
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003315#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3316 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003317 md5_starts( &ssl->handshake->fin_md5 );
3318 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003319#endif
3320#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3321#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003322 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003323#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003324#if defined(POLARSSL_SHA512_C)
3325 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003326#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003327#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003328
3329 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003330 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003331
Paul Bakker61d113b2013-07-04 11:51:43 +02003332#if defined(POLARSSL_ECDH_C)
3333 ecdh_init( &ssl->handshake->ecdh_ctx );
3334#endif
3335
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003336#if defined(POLARSSL_X509_CRT_PARSE_C)
3337 ssl->handshake->key_cert = ssl->key_cert;
3338#endif
3339
Paul Bakker48916f92012-09-16 19:57:18 +00003340 return( 0 );
3341}
3342
Paul Bakker5121ce52009-01-03 21:22:43 +00003343/*
3344 * Initialize an SSL context
3345 */
3346int ssl_init( ssl_context *ssl )
3347{
Paul Bakker48916f92012-09-16 19:57:18 +00003348 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 int len = SSL_BUFFER_LEN;
3350
3351 memset( ssl, 0, sizeof( ssl_context ) );
3352
Paul Bakker62f2dee2012-09-28 07:31:51 +00003353 /*
3354 * Sane defaults
3355 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003356 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3357 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3358 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3359 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003360
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003361 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003362
Paul Bakker62f2dee2012-09-28 07:31:51 +00003363#if defined(POLARSSL_DHM_C)
3364 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3365 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3366 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3367 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3368 {
3369 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3370 return( ret );
3371 }
3372#endif
3373
3374 /*
3375 * Prepare base structures
3376 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003377 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003378 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003379 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003380 ssl->in_msg = ssl->in_ctr + 13;
3381
3382 if( ssl->in_ctr == NULL )
3383 {
3384 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003385 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 }
3387
Paul Bakker6e339b52013-07-03 13:37:05 +02003388 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003389 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003390 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003391 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003392
3393 if( ssl->out_ctr == NULL )
3394 {
3395 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003396 polarssl_free( ssl->in_ctr );
3397 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003398 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003399 }
3400
3401 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3402 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3403
Paul Bakker606b4ba2013-08-14 16:52:14 +02003404#if defined(POLARSSL_SSL_SESSION_TICKETS)
3405 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3406#endif
3407
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003408#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003409 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003410#endif
3411
Paul Bakker48916f92012-09-16 19:57:18 +00003412 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3413 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003414
3415 return( 0 );
3416}
3417
3418/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003419 * Reset an initialized and used SSL context for re-use while retaining
3420 * all application-set variables, function pointers and data.
3421 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003422int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003423{
Paul Bakker48916f92012-09-16 19:57:18 +00003424 int ret;
3425
Paul Bakker7eb013f2011-10-06 12:37:39 +00003426 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003427 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3428 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3429
3430 ssl->verify_data_len = 0;
3431 memset( ssl->own_verify_data, 0, 36 );
3432 memset( ssl->peer_verify_data, 0, 36 );
3433
Paul Bakker7eb013f2011-10-06 12:37:39 +00003434 ssl->in_offt = NULL;
3435
Paul Bakker92be97b2013-01-02 17:30:03 +01003436 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003437 ssl->in_msgtype = 0;
3438 ssl->in_msglen = 0;
3439 ssl->in_left = 0;
3440
3441 ssl->in_hslen = 0;
3442 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003443 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003444
Paul Bakker92be97b2013-01-02 17:30:03 +01003445 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003446 ssl->out_msgtype = 0;
3447 ssl->out_msglen = 0;
3448 ssl->out_left = 0;
3449
Paul Bakker48916f92012-09-16 19:57:18 +00003450 ssl->transform_in = NULL;
3451 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003452
3453 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3454 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003455
3456#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3457 if( ssl_hw_record_reset != NULL)
3458 {
3459 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003460 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003461 {
3462 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3463 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3464 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003465 }
3466#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003467
Paul Bakker48916f92012-09-16 19:57:18 +00003468 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003469 {
Paul Bakker48916f92012-09-16 19:57:18 +00003470 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003471 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003472 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003473 }
Paul Bakker48916f92012-09-16 19:57:18 +00003474
Paul Bakkerc0463502013-02-14 11:19:38 +01003475 if( ssl->session )
3476 {
3477 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003478 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003479 ssl->session = NULL;
3480 }
3481
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003482#if defined(POLARSSL_SSL_ALPN)
3483 ssl->alpn_chosen = NULL;
3484#endif
3485
Paul Bakker48916f92012-09-16 19:57:18 +00003486 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3487 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003488
3489 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003490}
3491
Paul Bakkera503a632013-08-14 13:48:06 +02003492#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003493/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003494 * Allocate and initialize ticket keys
3495 */
3496static int ssl_ticket_keys_init( ssl_context *ssl )
3497{
3498 int ret;
3499 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003500 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003501
3502 if( ssl->ticket_keys != NULL )
3503 return( 0 );
3504
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003505 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3506 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003507 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3508
3509 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003510 {
3511 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003512 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003513 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003514
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003515 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3516 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3517 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3518 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003519 polarssl_free( tkeys );
3520 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003521 }
3522
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003523 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003524 {
3525 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003526 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003527 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003528
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003529 ssl->ticket_keys = tkeys;
3530
3531 return( 0 );
3532}
Paul Bakkera503a632013-08-14 13:48:06 +02003533#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003534
3535/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003536 * SSL set accessors
3537 */
3538void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3539{
3540 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003541
Paul Bakker606b4ba2013-08-14 16:52:14 +02003542#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003543 if( endpoint == SSL_IS_CLIENT )
3544 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003545#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003546}
3547
3548void ssl_set_authmode( ssl_context *ssl, int authmode )
3549{
3550 ssl->authmode = authmode;
3551}
3552
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003553#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003554void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003555 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003556 void *p_vrfy )
3557{
3558 ssl->f_vrfy = f_vrfy;
3559 ssl->p_vrfy = p_vrfy;
3560}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003561#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003562
Paul Bakker5121ce52009-01-03 21:22:43 +00003563void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003564 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 void *p_rng )
3566{
3567 ssl->f_rng = f_rng;
3568 ssl->p_rng = p_rng;
3569}
3570
3571void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003572 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 void *p_dbg )
3574{
3575 ssl->f_dbg = f_dbg;
3576 ssl->p_dbg = p_dbg;
3577}
3578
3579void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003580 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003581 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003582{
3583 ssl->f_recv = f_recv;
3584 ssl->f_send = f_send;
3585 ssl->p_recv = p_recv;
3586 ssl->p_send = p_send;
3587}
3588
Paul Bakker0a597072012-09-25 21:55:46 +00003589void ssl_set_session_cache( ssl_context *ssl,
3590 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3591 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003592{
Paul Bakker0a597072012-09-25 21:55:46 +00003593 ssl->f_get_cache = f_get_cache;
3594 ssl->p_get_cache = p_get_cache;
3595 ssl->f_set_cache = f_set_cache;
3596 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003597}
3598
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003599int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003600{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003601 int ret;
3602
3603 if( ssl == NULL ||
3604 session == NULL ||
3605 ssl->session_negotiate == NULL ||
3606 ssl->endpoint != SSL_IS_CLIENT )
3607 {
3608 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3609 }
3610
3611 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3612 return( ret );
3613
Paul Bakker0a597072012-09-25 21:55:46 +00003614 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003615
3616 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003617}
3618
Paul Bakkerb68cad62012-08-23 08:34:18 +00003619void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003620{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003621 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3622 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3623 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3624 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3625}
3626
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003627void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3628 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003629 int major, int minor )
3630{
3631 if( major != SSL_MAJOR_VERSION_3 )
3632 return;
3633
3634 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3635 return;
3636
3637 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003638}
3639
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003640#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003641/* Add a new (empty) key_cert entry an return a pointer to it */
3642static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3643{
3644 ssl_key_cert *key_cert, *last;
3645
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003646 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3647 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003648 return( NULL );
3649
3650 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3651
3652 /* Append the new key_cert to the (possibly empty) current list */
3653 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003654 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003655 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003656 if( ssl->handshake != NULL )
3657 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003658 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003659 else
3660 {
3661 last = ssl->key_cert;
3662 while( last->next != NULL )
3663 last = last->next;
3664 last->next = key_cert;
3665 }
3666
3667 return key_cert;
3668}
3669
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003670void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003671 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003672{
3673 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003674 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003675 ssl->peer_cn = peer_cn;
3676}
3677
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003678int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003679 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003680{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003681 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3682
3683 if( key_cert == NULL )
3684 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3685
3686 key_cert->cert = own_cert;
3687 key_cert->key = pk_key;
3688
3689 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003690}
3691
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003692#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003693int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003694 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003695{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003696 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003697 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003698
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003699 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003700 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3701
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003702 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3703 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003704 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003705
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003706 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003707
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003708 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003709 if( ret != 0 )
3710 return( ret );
3711
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003712 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003713 return( ret );
3714
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003715 key_cert->cert = own_cert;
3716 key_cert->key_own_alloc = 1;
3717
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003718 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003719}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003720#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003721
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003722int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003723 void *rsa_key,
3724 rsa_decrypt_func rsa_decrypt,
3725 rsa_sign_func rsa_sign,
3726 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003727{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003728 int ret;
3729 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003730
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003731 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003732 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3733
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003734 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3735 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003736 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003737
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003738 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003739
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003740 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3741 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3742 return( ret );
3743
3744 key_cert->cert = own_cert;
3745 key_cert->key_own_alloc = 1;
3746
3747 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003748}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003749#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003750
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003751#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003752int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3753 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003754{
Paul Bakker6db455e2013-09-18 17:29:31 +02003755 if( psk == NULL || psk_identity == NULL )
3756 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3757
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003758 /*
3759 * The length will be check later anyway, but in case it is obviously
3760 * too large, better abort now. The PMS is as follows:
3761 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3762 */
3763 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3764 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3765
Paul Bakker6db455e2013-09-18 17:29:31 +02003766 if( ssl->psk != NULL )
3767 {
3768 polarssl_free( ssl->psk );
3769 polarssl_free( ssl->psk_identity );
3770 }
3771
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003772 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003773 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003774
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003775 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003776 ssl->psk_identity = (unsigned char *)
3777 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003778
3779 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3780 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3781
3782 memcpy( ssl->psk, psk, ssl->psk_len );
3783 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003784
3785 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003786}
3787
3788void ssl_set_psk_cb( ssl_context *ssl,
3789 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3790 size_t),
3791 void *p_psk )
3792{
3793 ssl->f_psk = f_psk;
3794 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003795}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003796#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003797
Paul Bakker48916f92012-09-16 19:57:18 +00003798#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003799int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003800{
3801 int ret;
3802
Paul Bakker48916f92012-09-16 19:57:18 +00003803 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 {
3805 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3806 return( ret );
3807 }
3808
Paul Bakker48916f92012-09-16 19:57:18 +00003809 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003810 {
3811 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3812 return( ret );
3813 }
3814
3815 return( 0 );
3816}
3817
Paul Bakker1b57b062011-01-06 15:48:19 +00003818int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3819{
3820 int ret;
3821
Paul Bakker48916f92012-09-16 19:57:18 +00003822 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003823 {
3824 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3825 return( ret );
3826 }
3827
Paul Bakker48916f92012-09-16 19:57:18 +00003828 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003829 {
3830 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3831 return( ret );
3832 }
3833
3834 return( 0 );
3835}
Paul Bakker48916f92012-09-16 19:57:18 +00003836#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003837
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003838#if defined(POLARSSL_SSL_SET_CURVES)
3839/*
3840 * Set the allowed elliptic curves
3841 */
3842void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3843{
3844 ssl->curve_list = curve_list;
3845}
3846#endif
3847
Paul Bakker0be444a2013-08-27 21:55:01 +02003848#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003849int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003850{
3851 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003852 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003853
3854 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003855
3856 if( ssl->hostname_len + 1 == 0 )
3857 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3858
Paul Bakker6e339b52013-07-03 13:37:05 +02003859 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003860
Paul Bakkerb15b8512012-01-13 13:44:06 +00003861 if( ssl->hostname == NULL )
3862 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3863
Paul Bakker3c2122f2013-06-24 19:03:14 +02003864 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003866
Paul Bakker40ea7de2009-05-03 10:18:48 +00003867 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003868
3869 return( 0 );
3870}
3871
Paul Bakker5701cdc2012-09-27 21:49:42 +00003872void ssl_set_sni( ssl_context *ssl,
3873 int (*f_sni)(void *, ssl_context *,
3874 const unsigned char *, size_t),
3875 void *p_sni )
3876{
3877 ssl->f_sni = f_sni;
3878 ssl->p_sni = p_sni;
3879}
Paul Bakker0be444a2013-08-27 21:55:01 +02003880#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003881
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003882#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003883int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003884{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003885 size_t cur_len, tot_len;
3886 const char **p;
3887
3888 /*
3889 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3890 * truncated". Check lengths now rather than later.
3891 */
3892 tot_len = 0;
3893 for( p = protos; *p != NULL; p++ )
3894 {
3895 cur_len = strlen( *p );
3896 tot_len += cur_len;
3897
3898 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3899 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3900 }
3901
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003902 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003903
3904 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003905}
3906
3907const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3908{
3909 return ssl->alpn_chosen;
3910}
3911#endif /* POLARSSL_SSL_ALPN */
3912
Paul Bakker490ecc82011-10-06 13:04:09 +00003913void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3914{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003915 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3916 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3917 {
3918 ssl->max_major_ver = major;
3919 ssl->max_minor_ver = minor;
3920 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003921}
3922
Paul Bakker1d29fb52012-09-28 13:28:45 +00003923void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3924{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003925 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3926 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3927 {
3928 ssl->min_major_ver = major;
3929 ssl->min_minor_ver = minor;
3930 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003931}
3932
Paul Bakker05decb22013-08-15 13:33:48 +02003933#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003934int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3935{
Paul Bakker77e257e2013-12-16 15:29:52 +01003936 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003937 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003938 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003939 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003940 }
3941
3942 ssl->mfl_code = mfl_code;
3943
3944 return( 0 );
3945}
Paul Bakker05decb22013-08-15 13:33:48 +02003946#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003947
Paul Bakker1f2bc622013-08-15 13:45:55 +02003948#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003949int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003950{
3951 if( ssl->endpoint != SSL_IS_CLIENT )
3952 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3953
Paul Bakker8c1ede62013-07-19 14:14:37 +02003954 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003955
3956 return( 0 );
3957}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003958#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003959
Paul Bakker48916f92012-09-16 19:57:18 +00003960void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3961{
3962 ssl->disable_renegotiation = renegotiation;
3963}
3964
3965void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3966{
3967 ssl->allow_legacy_renegotiation = allow_legacy;
3968}
3969
Paul Bakkera503a632013-08-14 13:48:06 +02003970#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003971int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3972{
3973 ssl->session_tickets = use_tickets;
3974
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003975 if( ssl->endpoint == SSL_IS_CLIENT )
3976 return( 0 );
3977
3978 if( ssl->f_rng == NULL )
3979 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3980
3981 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003982}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003983
3984void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3985{
3986 ssl->ticket_lifetime = lifetime;
3987}
Paul Bakkera503a632013-08-14 13:48:06 +02003988#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003989
Paul Bakker5121ce52009-01-03 21:22:43 +00003990/*
3991 * SSL get accessors
3992 */
Paul Bakker23986e52011-04-24 08:57:21 +00003993size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003994{
3995 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3996}
3997
Paul Bakkerff60ee62010-03-16 21:09:09 +00003998int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003999{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004000 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004001}
4002
Paul Bakkere3166ce2011-01-27 17:40:50 +00004003const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004004{
Paul Bakker926c8e42013-03-06 10:23:34 +01004005 if( ssl == NULL || ssl->session == NULL )
4006 return NULL;
4007
Paul Bakkere3166ce2011-01-27 17:40:50 +00004008 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004009}
4010
Paul Bakker43ca69c2011-01-15 17:35:19 +00004011const char *ssl_get_version( const ssl_context *ssl )
4012{
4013 switch( ssl->minor_ver )
4014 {
4015 case SSL_MINOR_VERSION_0:
4016 return( "SSLv3.0" );
4017
4018 case SSL_MINOR_VERSION_1:
4019 return( "TLSv1.0" );
4020
4021 case SSL_MINOR_VERSION_2:
4022 return( "TLSv1.1" );
4023
Paul Bakker1ef83d62012-04-11 12:09:53 +00004024 case SSL_MINOR_VERSION_3:
4025 return( "TLSv1.2" );
4026
Paul Bakker43ca69c2011-01-15 17:35:19 +00004027 default:
4028 break;
4029 }
4030 return( "unknown" );
4031}
4032
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004033#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004034const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004035{
4036 if( ssl == NULL || ssl->session == NULL )
4037 return NULL;
4038
4039 return ssl->session->peer_cert;
4040}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004041#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004042
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004043int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4044{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004045 if( ssl == NULL ||
4046 dst == NULL ||
4047 ssl->session == NULL ||
4048 ssl->endpoint != SSL_IS_CLIENT )
4049 {
4050 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4051 }
4052
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004053 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004054}
4055
Paul Bakker5121ce52009-01-03 21:22:43 +00004056/*
Paul Bakker1961b702013-01-25 14:49:24 +01004057 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004058 */
Paul Bakker1961b702013-01-25 14:49:24 +01004059int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004060{
Paul Bakker40e46942009-01-03 21:51:57 +00004061 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004062
Paul Bakker40e46942009-01-03 21:51:57 +00004063#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004064 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004065 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004066#endif
4067
Paul Bakker40e46942009-01-03 21:51:57 +00004068#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004069 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004070 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004071#endif
4072
Paul Bakker1961b702013-01-25 14:49:24 +01004073 return( ret );
4074}
4075
4076/*
4077 * Perform the SSL handshake
4078 */
4079int ssl_handshake( ssl_context *ssl )
4080{
4081 int ret = 0;
4082
4083 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4084
4085 while( ssl->state != SSL_HANDSHAKE_OVER )
4086 {
4087 ret = ssl_handshake_step( ssl );
4088
4089 if( ret != 0 )
4090 break;
4091 }
4092
Paul Bakker5121ce52009-01-03 21:22:43 +00004093 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4094
4095 return( ret );
4096}
4097
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004098#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004099/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004100 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004101 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004102static int ssl_write_hello_request( ssl_context *ssl )
4103{
4104 int ret;
4105
4106 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4107
4108 ssl->out_msglen = 4;
4109 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4110 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4111
4112 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4113 {
4114 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4115 return( ret );
4116 }
4117
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004118 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4119
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004120 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4121
4122 return( 0 );
4123}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004124#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004125
4126/*
4127 * Actually renegotiate current connection, triggered by either:
4128 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004129 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004130 * - receiving any handshake message on server during ssl_read() after the
4131 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004132 * If the handshake doesn't complete due to waiting for I/O, it will continue
4133 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004134 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004135static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004136{
4137 int ret;
4138
4139 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4140
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004141 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4142 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004143
4144 ssl->state = SSL_HELLO_REQUEST;
4145 ssl->renegotiation = SSL_RENEGOTIATION;
4146
Paul Bakker48916f92012-09-16 19:57:18 +00004147 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4148 {
4149 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4150 return( ret );
4151 }
4152
4153 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4154
4155 return( 0 );
4156}
4157
4158/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004159 * Renegotiate current connection on client,
4160 * or request renegotiation on server
4161 */
4162int ssl_renegotiate( ssl_context *ssl )
4163{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004164 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004165
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004166#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004167 /* On server, just send the request */
4168 if( ssl->endpoint == SSL_IS_SERVER )
4169 {
4170 if( ssl->state != SSL_HANDSHAKE_OVER )
4171 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4172
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004173 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004174 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004175#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004176
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004177#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004178 /*
4179 * On client, either start the renegotiation process or,
4180 * if already in progress, continue the handshake
4181 */
4182 if( ssl->renegotiation != SSL_RENEGOTIATION )
4183 {
4184 if( ssl->state != SSL_HANDSHAKE_OVER )
4185 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4186
4187 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4188 {
4189 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4190 return( ret );
4191 }
4192 }
4193 else
4194 {
4195 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4196 {
4197 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4198 return( ret );
4199 }
4200 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004201#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004202
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004203 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004204}
4205
4206/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004207 * Receive application data decrypted from the SSL layer
4208 */
Paul Bakker23986e52011-04-24 08:57:21 +00004209int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004210{
Paul Bakker23986e52011-04-24 08:57:21 +00004211 int ret;
4212 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004213
4214 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4215
4216 if( ssl->state != SSL_HANDSHAKE_OVER )
4217 {
4218 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4219 {
4220 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4221 return( ret );
4222 }
4223 }
4224
4225 if( ssl->in_offt == NULL )
4226 {
4227 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4228 {
Paul Bakker831a7552011-05-18 13:32:51 +00004229 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4230 return( 0 );
4231
Paul Bakker5121ce52009-01-03 21:22:43 +00004232 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4233 return( ret );
4234 }
4235
4236 if( ssl->in_msglen == 0 &&
4237 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4238 {
4239 /*
4240 * OpenSSL sends empty messages to randomize the IV
4241 */
4242 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4243 {
Paul Bakker831a7552011-05-18 13:32:51 +00004244 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4245 return( 0 );
4246
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4248 return( ret );
4249 }
4250 }
4251
Paul Bakker48916f92012-09-16 19:57:18 +00004252 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4253 {
4254 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4255
4256 if( ssl->endpoint == SSL_IS_CLIENT &&
4257 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4258 ssl->in_hslen != 4 ) )
4259 {
4260 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4261 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4262 }
4263
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004264 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4265 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004266 ssl->allow_legacy_renegotiation ==
4267 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004268 {
4269 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4270
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004271#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004272 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004273 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004274 /*
4275 * SSLv3 does not have a "no_renegotiation" alert
4276 */
4277 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4278 return( ret );
4279 }
4280 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004281#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004282#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4283 defined(POLARSSL_SSL_PROTO_TLS1_2)
4284 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004285 {
4286 if( ( ret = ssl_send_alert_message( ssl,
4287 SSL_ALERT_LEVEL_WARNING,
4288 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4289 {
4290 return( ret );
4291 }
Paul Bakker48916f92012-09-16 19:57:18 +00004292 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004293 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004294#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4295 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004296 {
4297 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004298 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004299 }
Paul Bakker48916f92012-09-16 19:57:18 +00004300 }
4301 else
4302 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004303 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004304 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004305 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004306 return( ret );
4307 }
4308
4309 return( POLARSSL_ERR_NET_WANT_READ );
4310 }
4311 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004312 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4313 {
4314 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4315 "but not honored by client" ) );
4316 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4317 }
Paul Bakker48916f92012-09-16 19:57:18 +00004318 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004319 {
4320 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004321 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 }
4323
4324 ssl->in_offt = ssl->in_msg;
4325 }
4326
4327 n = ( len < ssl->in_msglen )
4328 ? len : ssl->in_msglen;
4329
4330 memcpy( buf, ssl->in_offt, n );
4331 ssl->in_msglen -= n;
4332
4333 if( ssl->in_msglen == 0 )
4334 /* all bytes consumed */
4335 ssl->in_offt = NULL;
4336 else
4337 /* more data available */
4338 ssl->in_offt += n;
4339
4340 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4341
Paul Bakker23986e52011-04-24 08:57:21 +00004342 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004343}
4344
4345/*
4346 * Send application data to be encrypted by the SSL layer
4347 */
Paul Bakker23986e52011-04-24 08:57:21 +00004348int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004349{
Paul Bakker23986e52011-04-24 08:57:21 +00004350 int ret;
4351 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004352 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004353
4354 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4355
4356 if( ssl->state != SSL_HANDSHAKE_OVER )
4357 {
4358 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4359 {
4360 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4361 return( ret );
4362 }
4363 }
4364
Paul Bakker05decb22013-08-15 13:33:48 +02004365#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004366 /*
4367 * Assume mfl_code is correct since it was checked when set
4368 */
4369 max_len = mfl_code_to_length[ssl->mfl_code];
4370
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004371 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004372 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004373 */
4374 if( ssl->session_out != NULL &&
4375 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4376 {
4377 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4378 }
Paul Bakker05decb22013-08-15 13:33:48 +02004379#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004380
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004381 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004382
Paul Bakker5121ce52009-01-03 21:22:43 +00004383 if( ssl->out_left != 0 )
4384 {
4385 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4386 {
4387 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4388 return( ret );
4389 }
4390 }
Paul Bakker887bd502011-06-08 13:10:54 +00004391 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004392 {
Paul Bakker887bd502011-06-08 13:10:54 +00004393 ssl->out_msglen = n;
4394 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4395 memcpy( ssl->out_msg, buf, n );
4396
4397 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4398 {
4399 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4400 return( ret );
4401 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004402 }
4403
4404 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4405
Paul Bakker23986e52011-04-24 08:57:21 +00004406 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004407}
4408
4409/*
4410 * Notify the peer that the connection is being closed
4411 */
4412int ssl_close_notify( ssl_context *ssl )
4413{
4414 int ret;
4415
4416 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4417
4418 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4419 {
4420 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4421 return( ret );
4422 }
4423
4424 if( ssl->state == SSL_HANDSHAKE_OVER )
4425 {
Paul Bakker48916f92012-09-16 19:57:18 +00004426 if( ( ret = ssl_send_alert_message( ssl,
4427 SSL_ALERT_LEVEL_WARNING,
4428 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004429 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004430 return( ret );
4431 }
4432 }
4433
4434 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4435
4436 return( ret );
4437}
4438
Paul Bakker48916f92012-09-16 19:57:18 +00004439void ssl_transform_free( ssl_transform *transform )
4440{
4441#if defined(POLARSSL_ZLIB_SUPPORT)
4442 deflateEnd( &transform->ctx_deflate );
4443 inflateEnd( &transform->ctx_inflate );
4444#endif
4445
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004446 cipher_free_ctx( &transform->cipher_ctx_enc );
4447 cipher_free_ctx( &transform->cipher_ctx_dec );
4448
Paul Bakker61d113b2013-07-04 11:51:43 +02004449 md_free_ctx( &transform->md_ctx_enc );
4450 md_free_ctx( &transform->md_ctx_dec );
4451
Paul Bakker48916f92012-09-16 19:57:18 +00004452 memset( transform, 0, sizeof( ssl_transform ) );
4453}
4454
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004455#if defined(POLARSSL_X509_CRT_PARSE_C)
4456static void ssl_key_cert_free( ssl_key_cert *key_cert )
4457{
4458 ssl_key_cert *cur = key_cert, *next;
4459
4460 while( cur != NULL )
4461 {
4462 next = cur->next;
4463
4464 if( cur->key_own_alloc )
4465 {
4466 pk_free( cur->key );
4467 polarssl_free( cur->key );
4468 }
4469 polarssl_free( cur );
4470
4471 cur = next;
4472 }
4473}
4474#endif /* POLARSSL_X509_CRT_PARSE_C */
4475
Paul Bakker48916f92012-09-16 19:57:18 +00004476void ssl_handshake_free( ssl_handshake_params *handshake )
4477{
4478#if defined(POLARSSL_DHM_C)
4479 dhm_free( &handshake->dhm_ctx );
4480#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004481#if defined(POLARSSL_ECDH_C)
4482 ecdh_free( &handshake->ecdh_ctx );
4483#endif
4484
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004485#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004486 /* explicit void pointer cast for buggy MS compiler */
4487 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004488#endif
4489
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004490#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4491 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4492 /*
4493 * Free only the linked list wrapper, not the keys themselves
4494 * since the belong to the SNI callback
4495 */
4496 if( handshake->sni_key_cert != NULL )
4497 {
4498 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4499
4500 while( cur != NULL )
4501 {
4502 next = cur->next;
4503 polarssl_free( cur );
4504 cur = next;
4505 }
4506 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004507#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004508
Paul Bakker48916f92012-09-16 19:57:18 +00004509 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4510}
4511
4512void ssl_session_free( ssl_session *session )
4513{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004514#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004515 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004516 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004517 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004518 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004519 }
Paul Bakkered27a042013-04-18 22:46:23 +02004520#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004521
Paul Bakkera503a632013-08-14 13:48:06 +02004522#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004523 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004524#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004525
Paul Bakker0a597072012-09-25 21:55:46 +00004526 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004527}
4528
Paul Bakker5121ce52009-01-03 21:22:43 +00004529/*
4530 * Free an SSL context
4531 */
4532void ssl_free( ssl_context *ssl )
4533{
4534 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4535
Paul Bakker5121ce52009-01-03 21:22:43 +00004536 if( ssl->out_ctr != NULL )
4537 {
4538 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004539 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004540 }
4541
4542 if( ssl->in_ctr != NULL )
4543 {
4544 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004545 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 }
4547
Paul Bakker16770332013-10-11 09:59:44 +02004548#if defined(POLARSSL_ZLIB_SUPPORT)
4549 if( ssl->compress_buf != NULL )
4550 {
4551 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4552 polarssl_free( ssl->compress_buf );
4553 }
4554#endif
4555
Paul Bakker40e46942009-01-03 21:51:57 +00004556#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004557 mpi_free( &ssl->dhm_P );
4558 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004559#endif
4560
Paul Bakker48916f92012-09-16 19:57:18 +00004561 if( ssl->transform )
4562 {
4563 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004564 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004565 }
4566
4567 if( ssl->handshake )
4568 {
4569 ssl_handshake_free( ssl->handshake );
4570 ssl_transform_free( ssl->transform_negotiate );
4571 ssl_session_free( ssl->session_negotiate );
4572
Paul Bakker6e339b52013-07-03 13:37:05 +02004573 polarssl_free( ssl->handshake );
4574 polarssl_free( ssl->transform_negotiate );
4575 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004576 }
4577
Paul Bakkerc0463502013-02-14 11:19:38 +01004578 if( ssl->session )
4579 {
4580 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004581 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004582 }
4583
Paul Bakkera503a632013-08-14 13:48:06 +02004584#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004585 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004586#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004587
Paul Bakker0be444a2013-08-27 21:55:01 +02004588#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004589 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004590 {
4591 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004592 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 ssl->hostname_len = 0;
4594 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004595#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004596
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004597#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004598 if( ssl->psk != NULL )
4599 {
4600 memset( ssl->psk, 0, ssl->psk_len );
4601 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4602 polarssl_free( ssl->psk );
4603 polarssl_free( ssl->psk_identity );
4604 ssl->psk_len = 0;
4605 ssl->psk_identity_len = 0;
4606 }
4607#endif
4608
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004609#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004610 ssl_key_cert_free( ssl->key_cert );
4611#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004612
Paul Bakker05ef8352012-05-08 09:17:57 +00004613#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4614 if( ssl_hw_record_finish != NULL )
4615 {
4616 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4617 ssl_hw_record_finish( ssl );
4618 }
4619#endif
4620
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004622
Paul Bakker86f04f42013-02-14 11:20:09 +01004623 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004624 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004625}
4626
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004627#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004628/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004629 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004630 */
4631unsigned char ssl_sig_from_pk( pk_context *pk )
4632{
4633#if defined(POLARSSL_RSA_C)
4634 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4635 return( SSL_SIG_RSA );
4636#endif
4637#if defined(POLARSSL_ECDSA_C)
4638 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4639 return( SSL_SIG_ECDSA );
4640#endif
4641 return( SSL_SIG_ANON );
4642}
4643
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004644pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4645{
4646 switch( sig )
4647 {
4648#if defined(POLARSSL_RSA_C)
4649 case SSL_SIG_RSA:
4650 return( POLARSSL_PK_RSA );
4651#endif
4652#if defined(POLARSSL_ECDSA_C)
4653 case SSL_SIG_ECDSA:
4654 return( POLARSSL_PK_ECDSA );
4655#endif
4656 default:
4657 return( POLARSSL_PK_NONE );
4658 }
4659}
Paul Bakker9af723c2014-05-01 13:03:14 +02004660#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004661
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004662/*
4663 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4664 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004665md_type_t ssl_md_alg_from_hash( unsigned char hash )
4666{
4667 switch( hash )
4668 {
4669#if defined(POLARSSL_MD5_C)
4670 case SSL_HASH_MD5:
4671 return( POLARSSL_MD_MD5 );
4672#endif
4673#if defined(POLARSSL_SHA1_C)
4674 case SSL_HASH_SHA1:
4675 return( POLARSSL_MD_SHA1 );
4676#endif
4677#if defined(POLARSSL_SHA256_C)
4678 case SSL_HASH_SHA224:
4679 return( POLARSSL_MD_SHA224 );
4680 case SSL_HASH_SHA256:
4681 return( POLARSSL_MD_SHA256 );
4682#endif
4683#if defined(POLARSSL_SHA512_C)
4684 case SSL_HASH_SHA384:
4685 return( POLARSSL_MD_SHA384 );
4686 case SSL_HASH_SHA512:
4687 return( POLARSSL_MD_SHA512 );
4688#endif
4689 default:
4690 return( POLARSSL_MD_NONE );
4691 }
4692}
4693
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004694#if defined(POLARSSL_SSL_SET_CURVES)
4695/*
4696 * Check is a curve proposed by the peer is in our list.
4697 * Return 1 if we're willing to use it, 0 otherwise.
4698 */
4699int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4700{
4701 const ecp_group_id *gid;
4702
4703 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4704 if( *gid == grp_id )
4705 return( 1 );
4706
4707 return( 0 );
4708}
Paul Bakker9af723c2014-05-01 13:03:14 +02004709#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004710
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004711#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004712int ssl_check_cert_usage( const x509_crt *cert,
4713 const ssl_ciphersuite_t *ciphersuite,
4714 int cert_endpoint )
4715{
4716#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4717 int usage = 0;
4718#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004719#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4720 const char *ext_oid;
4721 size_t ext_len;
4722#endif
4723
4724#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4725 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4726 ((void) cert);
4727 ((void) cert_endpoint);
4728#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004729
4730#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4731 if( cert_endpoint == SSL_IS_SERVER )
4732 {
4733 /* Server part of the key exchange */
4734 switch( ciphersuite->key_exchange )
4735 {
4736 case POLARSSL_KEY_EXCHANGE_RSA:
4737 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4738 usage = KU_KEY_ENCIPHERMENT;
4739 break;
4740
4741 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4742 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4743 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4744 usage = KU_DIGITAL_SIGNATURE;
4745 break;
4746
4747 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4748 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4749 usage = KU_KEY_AGREEMENT;
4750 break;
4751
4752 /* Don't use default: we want warnings when adding new values */
4753 case POLARSSL_KEY_EXCHANGE_NONE:
4754 case POLARSSL_KEY_EXCHANGE_PSK:
4755 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4756 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4757 usage = 0;
4758 }
4759 }
4760 else
4761 {
4762 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4763 usage = KU_DIGITAL_SIGNATURE;
4764 }
4765
4766 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4767 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004768#else
4769 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004770#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4771
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004772#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4773 if( cert_endpoint == SSL_IS_SERVER )
4774 {
4775 ext_oid = OID_SERVER_AUTH;
4776 ext_len = OID_SIZE( OID_SERVER_AUTH );
4777 }
4778 else
4779 {
4780 ext_oid = OID_CLIENT_AUTH;
4781 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4782 }
4783
4784 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4785 return( -1 );
4786#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4787
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004788 return( 0 );
4789}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004790#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004791
4792#endif /* POLARSSL_SSL_TLS_C */