blob: 527f7271127503c99757e1bf6926bddeaf189647 [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;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +0200994 const cipher_mode_t mode = cipher_get_cipher_mode(
995 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +0000996
997 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
998
999 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001000 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001002#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1003 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1004 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001005 if( mode != POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001007#if defined(POLARSSL_SSL_PROTO_SSL3)
1008 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1009 {
1010 ssl_mac( &ssl->transform_out->md_ctx_enc,
1011 ssl->transform_out->mac_enc,
1012 ssl->out_msg, ssl->out_msglen,
1013 ssl->out_ctr, ssl->out_msgtype );
1014 }
1015 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001016#endif
1017#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001018 defined(POLARSSL_SSL_PROTO_TLS1_2)
1019 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1020 {
1021 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1022 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1023 ssl->out_msg, ssl->out_msglen );
1024 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1025 ssl->out_msg + ssl->out_msglen );
1026 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1027 }
1028 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001029#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001030 {
1031 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1032 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1033 }
1034
1035 SSL_DEBUG_BUF( 4, "computed mac",
1036 ssl->out_msg + ssl->out_msglen,
1037 ssl->transform_out->maclen );
1038
1039 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001040 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001041#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001043 /*
1044 * Encrypt
1045 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001046#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001047 if( mode == 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é-Gonnard5efd7722014-05-14 12:52:22 +02001079 if( mode == POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001080 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001081 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001082 unsigned char *enc_msg;
1083 unsigned char add_data[13];
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001084 int ret;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001085
Paul Bakkerca4ab492012-04-18 14:23:57 +00001086 memcpy( add_data, ssl->out_ctr, 8 );
1087 add_data[8] = ssl->out_msgtype;
1088 add_data[9] = ssl->major_ver;
1089 add_data[10] = ssl->minor_ver;
1090 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1091 add_data[12] = ssl->out_msglen & 0xFF;
1092
1093 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1094 add_data, 13 );
1095
Paul Bakker68884e32013-01-07 18:20:04 +01001096 /*
1097 * Generate IV
1098 */
1099 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001100 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1101 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001102 if( ret != 0 )
1103 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001104
Paul Bakker68884e32013-01-07 18:20:04 +01001105 memcpy( ssl->out_iv,
1106 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1107 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001108
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001109 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001110 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001111
Paul Bakker68884e32013-01-07 18:20:04 +01001112 /*
1113 * Fix pointer positions and message length with added IV
1114 */
1115 enc_msg = ssl->out_msg;
1116 enc_msglen = ssl->out_msglen;
1117 ssl->out_msglen += ssl->transform_out->ivlen -
1118 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001119
Paul Bakker68884e32013-01-07 18:20:04 +01001120 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1121 "including %d bytes of padding",
1122 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001123
Paul Bakker68884e32013-01-07 18:20:04 +01001124 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1125 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001126
Paul Bakker68884e32013-01-07 18:20:04 +01001127 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001128 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001129 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001130 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1131 ssl->transform_out->iv_enc,
1132 ssl->transform_out->ivlen,
1133 add_data, 13,
1134 enc_msg, enc_msglen,
1135 enc_msg, &olen,
1136 enc_msg + enc_msglen, 16 ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001137 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001138 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001139 return( ret );
1140 }
1141
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001142 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001143 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001144 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1145 enc_msglen, olen ) );
1146 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001147 }
1148
Paul Bakker68884e32013-01-07 18:20:04 +01001149 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001150
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001151 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001152 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 else
Paul Bakker68884e32013-01-07 18:20:04 +01001154#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001155#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1156 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001157 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001159 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001160 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001161 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001162
Paul Bakker48916f92012-09-16 19:57:18 +00001163 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1164 ssl->transform_out->ivlen;
1165 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 padlen = 0;
1167
1168 for( i = 0; i <= padlen; i++ )
1169 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1170
1171 ssl->out_msglen += padlen + 1;
1172
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001173 enc_msglen = ssl->out_msglen;
1174 enc_msg = ssl->out_msg;
1175
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001176#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001177 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001178 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1179 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001180 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001181 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001182 {
1183 /*
1184 * Generate IV
1185 */
Paul Bakker48916f92012-09-16 19:57:18 +00001186 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1187 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001188 if( ret != 0 )
1189 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001190
Paul Bakker92be97b2013-01-02 17:30:03 +01001191 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001192 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001193
1194 /*
1195 * Fix pointer positions and message length with added IV
1196 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001197 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001198 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001199 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001200 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001201#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001202
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001204 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001205 ssl->out_msglen, ssl->transform_out->ivlen,
1206 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
1208 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001209 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001211 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001212 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001213 ssl->transform_out->ivlen,
1214 enc_msg, enc_msglen,
1215 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001216 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001217 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001218 return( ret );
1219 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001220
Paul Bakkercca5b812013-08-31 17:40:26 +02001221 if( enc_msglen != olen )
1222 {
1223 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1224 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001225 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001226 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001227
1228#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001229 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1230 {
1231 /*
1232 * Save IV in SSL3 and TLS1
1233 */
1234 memcpy( ssl->transform_out->iv_enc,
1235 ssl->transform_out->cipher_ctx_enc.iv,
1236 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001238#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001240 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001241#endif /* POLARSSL_CIPHER_MODE_CBC &&
1242 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001243 {
1244 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1245 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1246 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Paul Bakkerca4ab492012-04-18 14:23:57 +00001248 for( i = 8; i > 0; i-- )
1249 if( ++ssl->out_ctr[i - 1] != 0 )
1250 break;
1251
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001252 /* The loops goes to its end iff the counter is wrapping */
1253 if( i == 0 )
1254 {
1255 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1256 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1257 }
1258
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1260
1261 return( 0 );
1262}
1263
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001264#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001265
Paul Bakker5121ce52009-01-03 21:22:43 +00001266static int ssl_decrypt_buf( ssl_context *ssl )
1267{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001268 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001269 const cipher_mode_t mode = cipher_get_cipher_mode(
1270 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001271#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1272 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1273 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1274 size_t padlen = 0, correct = 1;
1275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001276
1277 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1278
Paul Bakker48916f92012-09-16 19:57:18 +00001279 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 {
1281 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001282 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001283 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 }
1285
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001286#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001287 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001288 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001289 int ret;
1290 size_t olen = 0;
1291
Paul Bakker68884e32013-01-07 18:20:04 +01001292 padlen = 0;
1293
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001294 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001295 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001296 ssl->transform_in->ivlen,
1297 ssl->in_msg, ssl->in_msglen,
1298 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001299 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001300 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001301 return( ret );
1302 }
1303
1304 if( ssl->in_msglen != olen )
1305 {
1306 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001307 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001308 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 }
Paul Bakker68884e32013-01-07 18:20:04 +01001310 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001311#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001312#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001313 if( mode == POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001314 {
1315 unsigned char *dec_msg;
1316 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001317 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001318 unsigned char add_data[13];
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001319 int ret;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001320
Paul Bakker68884e32013-01-07 18:20:04 +01001321 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1322 ssl->transform_in->fixed_ivlen );
1323 dec_msglen -= 16;
1324 dec_msg = ssl->in_msg;
1325 dec_msg_result = ssl->in_msg;
1326 ssl->in_msglen = dec_msglen;
1327
1328 memcpy( add_data, ssl->in_ctr, 8 );
1329 add_data[8] = ssl->in_msgtype;
1330 add_data[9] = ssl->major_ver;
1331 add_data[10] = ssl->minor_ver;
1332 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1333 add_data[12] = ssl->in_msglen & 0xFF;
1334
1335 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1336 add_data, 13 );
1337
1338 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1339 ssl->in_iv,
1340 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1341
1342 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1343 ssl->transform_in->ivlen );
1344 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1345
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001346 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001347 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001348 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001349 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1350 ssl->transform_in->iv_dec,
1351 ssl->transform_in->ivlen,
1352 add_data, 13,
1353 dec_msg, dec_msglen,
1354 dec_msg_result, &olen,
1355 dec_msg + dec_msglen, 16 ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001356 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001357 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1358
1359 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1360 return( POLARSSL_ERR_SSL_INVALID_MAC );
1361
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001362 return( ret );
1363 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001364
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001365 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001366 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001367 SSL_DEBUG_MSG( 1, ( "total decrypted length incorrect %d %d",
1368 dec_msglen, olen ) );
1369 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001370 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 else
Paul Bakker68884e32013-01-07 18:20:04 +01001373#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001374#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1375 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001376 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 {
Paul Bakker45829992013-01-03 14:52:21 +01001378 /*
1379 * Decrypt and check the padding
1380 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001381 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001382 unsigned char *dec_msg;
1383 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001384 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001385 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001386 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001387
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 /*
Paul Bakker45829992013-01-03 14:52:21 +01001389 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 */
Paul Bakker48916f92012-09-16 19:57:18 +00001391 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 {
1393 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001394 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001395 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 }
1397
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001398#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001399 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1400 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001401#endif
Paul Bakker45829992013-01-03 14:52:21 +01001402
1403 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1404 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1405 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001406 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1407 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1408 ssl->transform_in->ivlen,
1409 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001410 return( POLARSSL_ERR_SSL_INVALID_MAC );
1411 }
1412
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001413 dec_msglen = ssl->in_msglen;
1414 dec_msg = ssl->in_msg;
1415 dec_msg_result = ssl->in_msg;
1416
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001417#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001418 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001419 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001420 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001421 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001422 {
Paul Bakker48916f92012-09-16 19:57:18 +00001423 dec_msglen -= ssl->transform_in->ivlen;
1424 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001425
Paul Bakker48916f92012-09-16 19:57:18 +00001426 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001427 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001428 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001429#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001430
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001431 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001432 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001433 ssl->transform_in->ivlen,
1434 dec_msg, dec_msglen,
1435 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001436 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001437 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001438 return( ret );
1439 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001440
Paul Bakkercca5b812013-08-31 17:40:26 +02001441 if( dec_msglen != olen )
1442 {
1443 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001444 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001445 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001446
1447#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001448 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1449 {
1450 /*
1451 * Save IV in SSL3 and TLS1
1452 */
1453 memcpy( ssl->transform_in->iv_dec,
1454 ssl->transform_in->cipher_ctx_dec.iv,
1455 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001457#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001458
1459 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001460
1461 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1462 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001463#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001464 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1465 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001466#endif
Paul Bakker45829992013-01-03 14:52:21 +01001467 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001468 correct = 0;
1469 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001470
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001471#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1473 {
Paul Bakker48916f92012-09-16 19:57:18 +00001474 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001476#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1478 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001479 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001480#endif
Paul Bakker45829992013-01-03 14:52:21 +01001481 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 }
1483 }
1484 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001485#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001486#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1487 defined(POLARSSL_SSL_PROTO_TLS1_2)
1488 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 {
1490 /*
Paul Bakker45829992013-01-03 14:52:21 +01001491 * TLSv1+: always check the padding up to the first failure
1492 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001493 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001494 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001495 size_t padding_idx = ssl->in_msglen - padlen - 1;
1496
Paul Bakker956c9e02013-12-19 14:42:28 +01001497 /*
1498 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001499 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001500 *
Paul Bakker61885c72014-04-25 12:59:03 +02001501 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1502 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001503 *
1504 * In both cases we reset padding_idx to a safe value (0) to
1505 * prevent out-of-buffer reads.
1506 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001507 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001508 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1509 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001510
1511 padding_idx *= correct;
1512
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001513 for( i = 1; i <= 256; i++ )
1514 {
1515 real_count &= ( i <= padlen );
1516 pad_count += real_count *
1517 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1518 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001519
1520 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001521
Paul Bakkerd66f0702013-01-31 16:57:45 +01001522#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001523 if( padlen > 0 && correct == 0)
1524 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001525#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001526 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001528 else
1529#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1530 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001531 {
1532 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001533 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001536 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001537#endif /* POLARSSL_CIPHER_MODE_CBC &&
1538 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001539 {
1540 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1541 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
1544 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1545 ssl->in_msg, ssl->in_msglen );
1546
1547 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001548 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001549 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001550#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1551 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1552 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001553 if( mode != POLARSSL_MODE_GCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001554 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001555 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1556
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001557 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001558
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001559 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1560 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001561
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001562 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001563
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001564#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001565 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1566 {
1567 ssl_mac( &ssl->transform_in->md_ctx_dec,
1568 ssl->transform_in->mac_dec,
1569 ssl->in_msg, ssl->in_msglen,
1570 ssl->in_ctr, ssl->in_msgtype );
1571 }
1572 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001573#endif /* POLARSSL_SSL_PROTO_SSL3 */
1574#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001575 defined(POLARSSL_SSL_PROTO_TLS1_2)
1576 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1577 {
1578 /*
1579 * Process MAC and always update for padlen afterwards to make
1580 * total time independent of padlen
1581 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001582 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001583 *
1584 * Known timing attacks:
1585 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1586 *
1587 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1588 * correctly. (We round down instead of up, so -56 is the correct
1589 * value for our calculations instead of -55)
1590 */
1591 size_t j, extra_run = 0;
1592 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1593 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001594
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001595 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001596
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001597 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1598 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1599 ssl->in_msglen );
1600 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1601 ssl->in_msg + ssl->in_msglen );
1602 for( j = 0; j < extra_run; j++ )
1603 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001604
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001605 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1606 }
1607 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001608#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001609 POLARSSL_SSL_PROTO_TLS1_2 */
1610 {
1611 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1612 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1613 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001615 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1616 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1617 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001619 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001620 ssl->transform_in->maclen ) != 0 )
1621 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001622#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001623 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001624#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001625 correct = 0;
1626 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001628 /*
1629 * Finally check the correct flag
1630 */
1631 if( correct == 0 )
1632 return( POLARSSL_ERR_SSL_INVALID_MAC );
1633 }
1634#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
1636 if( ssl->in_msglen == 0 )
1637 {
1638 ssl->nb_zero++;
1639
1640 /*
1641 * Three or more empty messages may be a DoS attack
1642 * (excessive CPU consumption).
1643 */
1644 if( ssl->nb_zero > 3 )
1645 {
1646 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1647 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001648 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 }
1650 }
1651 else
1652 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001653
Paul Bakker23986e52011-04-24 08:57:21 +00001654 for( i = 8; i > 0; i-- )
1655 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 break;
1657
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001658 /* The loops goes to its end iff the counter is wrapping */
1659 if( i == 0 )
1660 {
1661 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1662 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1663 }
1664
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1666
1667 return( 0 );
1668}
1669
Paul Bakker2770fbd2012-07-03 13:30:23 +00001670#if defined(POLARSSL_ZLIB_SUPPORT)
1671/*
1672 * Compression/decompression functions
1673 */
1674static int ssl_compress_buf( ssl_context *ssl )
1675{
1676 int ret;
1677 unsigned char *msg_post = ssl->out_msg;
1678 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001679 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001680
1681 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1682
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001683 if( len_pre == 0 )
1684 return( 0 );
1685
Paul Bakker2770fbd2012-07-03 13:30:23 +00001686 memcpy( msg_pre, ssl->out_msg, len_pre );
1687
1688 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1689 ssl->out_msglen ) );
1690
1691 SSL_DEBUG_BUF( 4, "before compression: output payload",
1692 ssl->out_msg, ssl->out_msglen );
1693
Paul Bakker48916f92012-09-16 19:57:18 +00001694 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1695 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1696 ssl->transform_out->ctx_deflate.next_out = msg_post;
1697 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001698
Paul Bakker48916f92012-09-16 19:57:18 +00001699 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001700 if( ret != Z_OK )
1701 {
1702 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1703 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1704 }
1705
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001706 ssl->out_msglen = SSL_BUFFER_LEN -
1707 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001708
Paul Bakker2770fbd2012-07-03 13:30:23 +00001709 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1710 ssl->out_msglen ) );
1711
1712 SSL_DEBUG_BUF( 4, "after compression: output payload",
1713 ssl->out_msg, ssl->out_msglen );
1714
1715 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1716
1717 return( 0 );
1718}
1719
1720static int ssl_decompress_buf( ssl_context *ssl )
1721{
1722 int ret;
1723 unsigned char *msg_post = ssl->in_msg;
1724 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001725 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001726
1727 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1728
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001729 if( len_pre == 0 )
1730 return( 0 );
1731
Paul Bakker2770fbd2012-07-03 13:30:23 +00001732 memcpy( msg_pre, ssl->in_msg, len_pre );
1733
1734 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1735 ssl->in_msglen ) );
1736
1737 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1738 ssl->in_msg, ssl->in_msglen );
1739
Paul Bakker48916f92012-09-16 19:57:18 +00001740 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1741 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1742 ssl->transform_in->ctx_inflate.next_out = msg_post;
1743 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001744
Paul Bakker48916f92012-09-16 19:57:18 +00001745 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001746 if( ret != Z_OK )
1747 {
1748 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1749 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1750 }
1751
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001752 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1753 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001754
Paul Bakker2770fbd2012-07-03 13:30:23 +00001755 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1756 ssl->in_msglen ) );
1757
1758 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1759 ssl->in_msg, ssl->in_msglen );
1760
1761 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1762
1763 return( 0 );
1764}
1765#endif /* POLARSSL_ZLIB_SUPPORT */
1766
Paul Bakker5121ce52009-01-03 21:22:43 +00001767/*
1768 * Fill the input message buffer
1769 */
Paul Bakker23986e52011-04-24 08:57:21 +00001770int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001771{
Paul Bakker23986e52011-04-24 08:57:21 +00001772 int ret;
1773 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
1775 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1776
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001777 if( nb_want > SSL_BUFFER_LEN - 8 )
1778 {
1779 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1780 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1781 }
1782
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 while( ssl->in_left < nb_want )
1784 {
1785 len = nb_want - ssl->in_left;
1786 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1787
1788 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1789 ssl->in_left, nb_want ) );
1790 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1791
Paul Bakker831a7552011-05-18 13:32:51 +00001792 if( ret == 0 )
1793 return( POLARSSL_ERR_SSL_CONN_EOF );
1794
Paul Bakker5121ce52009-01-03 21:22:43 +00001795 if( ret < 0 )
1796 return( ret );
1797
1798 ssl->in_left += ret;
1799 }
1800
1801 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1802
1803 return( 0 );
1804}
1805
1806/*
1807 * Flush any data not yet written
1808 */
1809int ssl_flush_output( ssl_context *ssl )
1810{
1811 int ret;
1812 unsigned char *buf;
1813
1814 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1815
1816 while( ssl->out_left > 0 )
1817 {
1818 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1819 5 + ssl->out_msglen, ssl->out_left ) );
1820
Paul Bakker5bd42292012-12-19 14:40:42 +01001821 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001822 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001823
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1825
1826 if( ret <= 0 )
1827 return( ret );
1828
1829 ssl->out_left -= ret;
1830 }
1831
1832 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1833
1834 return( 0 );
1835}
1836
1837/*
1838 * Record layer functions
1839 */
1840int ssl_write_record( ssl_context *ssl )
1841{
Paul Bakker05ef8352012-05-08 09:17:57 +00001842 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001843 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001844
1845 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1846
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1848 {
1849 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1850 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1851 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1852
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001853 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1854 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855 }
1856
Paul Bakker2770fbd2012-07-03 13:30:23 +00001857#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001858 if( ssl->transform_out != NULL &&
1859 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001860 {
1861 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1862 {
1863 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1864 return( ret );
1865 }
1866
1867 len = ssl->out_msglen;
1868 }
1869#endif /*POLARSSL_ZLIB_SUPPORT */
1870
Paul Bakker05ef8352012-05-08 09:17:57 +00001871#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1872 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001873 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001874 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Paul Bakker05ef8352012-05-08 09:17:57 +00001876 ret = ssl_hw_record_write( ssl );
1877 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1878 {
1879 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1880 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1881 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001882
1883 if( ret == 0 )
1884 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001885 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001886#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001887 if( !done )
1888 {
1889 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1890 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1891 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1893 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001894
Paul Bakker48916f92012-09-16 19:57:18 +00001895 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001896 {
1897 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1898 {
1899 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1900 return( ret );
1901 }
1902
1903 len = ssl->out_msglen;
1904 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1905 ssl->out_hdr[4] = (unsigned char)( len );
1906 }
1907
1908 ssl->out_left = 5 + ssl->out_msglen;
1909
1910 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1911 "version = [%d:%d], msglen = %d",
1912 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1913 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1914
1915 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001916 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 }
1918
Paul Bakker5121ce52009-01-03 21:22:43 +00001919 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1920 {
1921 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1922 return( ret );
1923 }
1924
1925 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1926
1927 return( 0 );
1928}
1929
1930int ssl_read_record( ssl_context *ssl )
1931{
Paul Bakker05ef8352012-05-08 09:17:57 +00001932 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
1934 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1935
Paul Bakker68884e32013-01-07 18:20:04 +01001936 SSL_DEBUG_BUF( 4, "input record from network",
1937 ssl->in_hdr, 5 + ssl->in_msglen );
1938
Paul Bakker5121ce52009-01-03 21:22:43 +00001939 if( ssl->in_hslen != 0 &&
1940 ssl->in_hslen < ssl->in_msglen )
1941 {
1942 /*
1943 * Get next Handshake message in the current record
1944 */
1945 ssl->in_msglen -= ssl->in_hslen;
1946
Paul Bakker8934a982011-08-05 11:11:53 +00001947 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001948 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
1950 ssl->in_hslen = 4;
1951 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1952
1953 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1954 " %d, type = %d, hslen = %d",
1955 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1956
1957 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1958 {
1959 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001960 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 }
1962
1963 if( ssl->in_msglen < ssl->in_hslen )
1964 {
1965 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001966 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 }
1968
Paul Bakker4224bc02014-04-08 14:36:50 +02001969 if( ssl->state != SSL_HANDSHAKE_OVER )
1970 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
1972 return( 0 );
1973 }
1974
1975 ssl->in_hslen = 0;
1976
1977 /*
1978 * Read the record header and validate it
1979 */
1980 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1981 {
1982 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1983 return( ret );
1984 }
1985
1986 ssl->in_msgtype = ssl->in_hdr[0];
1987 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
1988
1989 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
1990 "version = [%d:%d], msglen = %d",
1991 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
1992 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
1993
1994 if( ssl->in_hdr[1] != ssl->major_ver )
1995 {
1996 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001997 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 }
1999
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 {
2002 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002003 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 }
2005
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002006 /* Sanity check (outer boundaries) */
2007 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2008 {
2009 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2010 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2011 }
2012
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002014 * Make sure the message length is acceptable for the current transform
2015 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 */
Paul Bakker48916f92012-09-16 19:57:18 +00002017 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002019 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 {
2021 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002022 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 }
2024 }
2025 else
2026 {
Paul Bakker48916f92012-09-16 19:57:18 +00002027 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 {
2029 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002030 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 }
2032
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002033#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002035 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 {
2037 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002038 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002041
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002042#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2043 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 /*
2045 * TLS encrypted messages can have up to 256 bytes of padding
2046 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002047 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002048 ssl->in_msglen > ssl->transform_in->minlen +
2049 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 {
2051 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002052 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 }
2056
2057 /*
2058 * Read and optionally decrypt the message contents
2059 */
2060 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2061 {
2062 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2063 return( ret );
2064 }
2065
2066 SSL_DEBUG_BUF( 4, "input record from network",
2067 ssl->in_hdr, 5 + ssl->in_msglen );
2068
Paul Bakker05ef8352012-05-08 09:17:57 +00002069#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2070 if( ssl_hw_record_read != NULL)
2071 {
2072 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2073
2074 ret = ssl_hw_record_read( ssl );
2075 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2076 {
2077 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2078 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2079 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002080
2081 if( ret == 0 )
2082 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002083 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002084#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002085 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 {
2087 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2088 {
Paul Bakker40865c82013-01-31 17:13:13 +01002089#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2090 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2091 {
2092 ssl_send_alert_message( ssl,
2093 SSL_ALERT_LEVEL_FATAL,
2094 SSL_ALERT_MSG_BAD_RECORD_MAC );
2095 }
2096#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2098 return( ret );
2099 }
2100
2101 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2102 ssl->in_msg, ssl->in_msglen );
2103
2104 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2105 {
2106 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002107 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
2109 }
2110
Paul Bakker2770fbd2012-07-03 13:30:23 +00002111#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002112 if( ssl->transform_in != NULL &&
2113 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002114 {
2115 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2116 {
2117 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2118 return( ret );
2119 }
2120
2121 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2122 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2123 }
2124#endif /* POLARSSL_ZLIB_SUPPORT */
2125
Paul Bakker0a925182012-04-16 06:46:41 +00002126 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2127 ssl->in_msgtype != SSL_MSG_ALERT &&
2128 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2129 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2130 {
2131 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2132
Paul Bakker48916f92012-09-16 19:57:18 +00002133 if( ( ret = ssl_send_alert_message( ssl,
2134 SSL_ALERT_LEVEL_FATAL,
2135 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002136 {
2137 return( ret );
2138 }
2139
2140 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2141 }
2142
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2144 {
2145 ssl->in_hslen = 4;
2146 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2147
2148 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2149 " %d, type = %d, hslen = %d",
2150 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2151
2152 /*
2153 * Additional checks to validate the handshake header
2154 */
2155 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2156 {
2157 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002158 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 }
2160
2161 if( ssl->in_msglen < ssl->in_hslen )
2162 {
2163 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002164 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 }
2166
Paul Bakker48916f92012-09-16 19:57:18 +00002167 if( ssl->state != SSL_HANDSHAKE_OVER )
2168 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 }
2170
2171 if( ssl->in_msgtype == SSL_MSG_ALERT )
2172 {
2173 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2174 ssl->in_msg[0], ssl->in_msg[1] ) );
2175
2176 /*
2177 * Ignore non-fatal alerts, except close_notify
2178 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002181 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2182 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002183 /**
2184 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2185 * error identifier.
2186 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002187 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 }
2189
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002190 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2191 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 {
2193 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002194 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 }
2196 }
2197
2198 ssl->in_left = 0;
2199
2200 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2201
2202 return( 0 );
2203}
2204
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002205int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2206{
2207 int ret;
2208
2209 if( ( ret = ssl_send_alert_message( ssl,
2210 SSL_ALERT_LEVEL_FATAL,
2211 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2212 {
2213 return( ret );
2214 }
2215
2216 return( 0 );
2217}
2218
Paul Bakker0a925182012-04-16 06:46:41 +00002219int ssl_send_alert_message( ssl_context *ssl,
2220 unsigned char level,
2221 unsigned char message )
2222{
2223 int ret;
2224
2225 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2226
2227 ssl->out_msgtype = SSL_MSG_ALERT;
2228 ssl->out_msglen = 2;
2229 ssl->out_msg[0] = level;
2230 ssl->out_msg[1] = message;
2231
2232 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2233 {
2234 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2235 return( ret );
2236 }
2237
2238 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2239
2240 return( 0 );
2241}
2242
Paul Bakker5121ce52009-01-03 21:22:43 +00002243/*
2244 * Handshake functions
2245 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002246#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2247 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2248 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2249 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2250 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2251 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2252 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002253int ssl_write_certificate( ssl_context *ssl )
2254{
Paul Bakkered27a042013-04-18 22:46:23 +02002255 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002256 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
2258 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2259
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002260 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002261 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2262 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002263 {
2264 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2265 ssl->state++;
2266 return( 0 );
2267 }
2268
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002269 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002270 return( ret );
2271}
2272
2273int ssl_parse_certificate( ssl_context *ssl )
2274{
2275 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2276 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2277
2278 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2279
2280 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002281 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2282 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002283 {
2284 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2285 ssl->state++;
2286 return( 0 );
2287 }
2288
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002289 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002290 return( ret );
2291}
2292#else
2293int ssl_write_certificate( ssl_context *ssl )
2294{
2295 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2296 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002297 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002298 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2299
2300 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2301
2302 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002303 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2304 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002305 {
2306 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2307 ssl->state++;
2308 return( 0 );
2309 }
2310
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 if( ssl->endpoint == SSL_IS_CLIENT )
2312 {
2313 if( ssl->client_auth == 0 )
2314 {
2315 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2316 ssl->state++;
2317 return( 0 );
2318 }
2319
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002320#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 /*
2322 * If using SSLv3 and got no cert, send an Alert message
2323 * (otherwise an empty Certificate message will be sent).
2324 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002325 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002326 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2327 {
2328 ssl->out_msglen = 2;
2329 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002330 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2331 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002332
2333 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2334 goto write_msg;
2335 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002336#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002337 }
2338 else /* SSL_IS_SERVER */
2339 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002340 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 {
2342 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002343 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 }
2345 }
2346
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002347 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002348
2349 /*
2350 * 0 . 0 handshake type
2351 * 1 . 3 handshake length
2352 * 4 . 6 length of all certs
2353 * 7 . 9 length of cert. 1
2354 * 10 . n-1 peer certificate
2355 * n . n+2 length of cert. 2
2356 * n+3 . ... upper level cert, etc.
2357 */
2358 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002359 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
Paul Bakker29087132010-03-21 21:03:34 +00002361 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002362 {
2363 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002364 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 {
2366 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2367 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002368 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 }
2370
2371 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2372 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2373 ssl->out_msg[i + 2] = (unsigned char)( n );
2374
2375 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2376 i += n; crt = crt->next;
2377 }
2378
2379 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2380 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2381 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2382
2383 ssl->out_msglen = i;
2384 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2385 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2386
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002387#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002388write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002389#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002390
2391 ssl->state++;
2392
2393 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2394 {
2395 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2396 return( ret );
2397 }
2398
2399 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2400
Paul Bakkered27a042013-04-18 22:46:23 +02002401 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402}
2403
2404int ssl_parse_certificate( ssl_context *ssl )
2405{
Paul Bakkered27a042013-04-18 22:46:23 +02002406 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002407 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002408 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
2410 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2411
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002412 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002413 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2414 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002415 {
2416 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2417 ssl->state++;
2418 return( 0 );
2419 }
2420
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002422 ( ssl->authmode == SSL_VERIFY_NONE ||
2423 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002425 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2427 ssl->state++;
2428 return( 0 );
2429 }
2430
2431 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2432 {
2433 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2434 return( ret );
2435 }
2436
2437 ssl->state++;
2438
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002439#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 /*
2441 * Check if the client sent an empty certificate
2442 */
2443 if( ssl->endpoint == SSL_IS_SERVER &&
2444 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2445 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002446 if( ssl->in_msglen == 2 &&
2447 ssl->in_msgtype == SSL_MSG_ALERT &&
2448 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2449 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 {
2451 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2452
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002453 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2455 return( 0 );
2456 else
Paul Bakker40e46942009-01-03 21:51:57 +00002457 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 }
2459 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002460#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002462#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2463 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 if( ssl->endpoint == SSL_IS_SERVER &&
2465 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2466 {
2467 if( ssl->in_hslen == 7 &&
2468 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2469 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2470 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2471 {
2472 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2473
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002474 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002476 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 else
2478 return( 0 );
2479 }
2480 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002481#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2482 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
2484 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2485 {
2486 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002487 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 }
2489
2490 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2491 {
2492 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002493 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 }
2495
2496 /*
2497 * Same message structure as in ssl_write_certificate()
2498 */
2499 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2500
2501 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2502 {
2503 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002504 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 }
2506
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002507 /* In case we tried to reuse a session but it failed */
2508 if( ssl->session_negotiate->peer_cert != NULL )
2509 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002510 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002511 polarssl_free( ssl->session_negotiate->peer_cert );
2512 }
2513
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002514 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2515 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 {
2517 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002518 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002519 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 }
2521
Paul Bakkerb6b09562013-09-18 14:17:41 +02002522 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
2524 i = 7;
2525
2526 while( i < ssl->in_hslen )
2527 {
2528 if( ssl->in_msg[i] != 0 )
2529 {
2530 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002531 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 }
2533
2534 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2535 | (unsigned int) ssl->in_msg[i + 2];
2536 i += 3;
2537
2538 if( n < 128 || i + n > ssl->in_hslen )
2539 {
2540 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002541 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 }
2543
Paul Bakkerddf26b42013-09-18 13:46:23 +02002544 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2545 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 if( ret != 0 )
2547 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002548 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 return( ret );
2550 }
2551
2552 i += n;
2553 }
2554
Paul Bakker48916f92012-09-16 19:57:18 +00002555 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002557 /*
2558 * On client, make sure the server cert doesn't change during renego to
2559 * avoid "triple handshake" attack: https://secure-resumption.com/
2560 */
2561 if( ssl->endpoint == SSL_IS_CLIENT &&
2562 ssl->renegotiation == SSL_RENEGOTIATION )
2563 {
2564 if( ssl->session->peer_cert == NULL )
2565 {
2566 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2567 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2568 }
2569
2570 if( ssl->session->peer_cert->raw.len !=
2571 ssl->session_negotiate->peer_cert->raw.len ||
2572 memcmp( ssl->session->peer_cert->raw.p,
2573 ssl->session_negotiate->peer_cert->raw.p,
2574 ssl->session->peer_cert->raw.len ) != 0 )
2575 {
2576 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2577 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2578 }
2579 }
2580
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 if( ssl->authmode != SSL_VERIFY_NONE )
2582 {
2583 if( ssl->ca_chain == NULL )
2584 {
2585 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002586 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 }
2588
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002589 /*
2590 * Main check: verify certificate
2591 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002592 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2593 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2594 &ssl->session_negotiate->verify_result,
2595 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
2597 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002598 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002600 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002601
2602 /*
2603 * Secondary checks: always done, but change 'ret' only if it was 0
2604 */
2605
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002606#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002607 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002608 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002609
2610 /* If certificate uses an EC key, make sure the curve is OK */
2611 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2612 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2613 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002614 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002615 if( ret == 0 )
2616 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002617 }
2618 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002619#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002621 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2622 ciphersuite_info,
2623 ! ssl->endpoint ) != 0 )
2624 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002625 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002626 if( ret == 0 )
2627 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2628 }
2629
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2631 ret = 0;
2632 }
2633
2634 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2635
2636 return( ret );
2637}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002638#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2639 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2640 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2641 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2642 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2643 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2644 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
2646int ssl_write_change_cipher_spec( ssl_context *ssl )
2647{
2648 int ret;
2649
2650 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2651
2652 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2653 ssl->out_msglen = 1;
2654 ssl->out_msg[0] = 1;
2655
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 ssl->state++;
2657
2658 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2659 {
2660 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2661 return( ret );
2662 }
2663
2664 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2665
2666 return( 0 );
2667}
2668
2669int ssl_parse_change_cipher_spec( ssl_context *ssl )
2670{
2671 int ret;
2672
2673 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2674
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2676 {
2677 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2678 return( ret );
2679 }
2680
2681 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2682 {
2683 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002684 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 }
2686
2687 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2688 {
2689 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002690 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 }
2692
2693 ssl->state++;
2694
2695 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2696
2697 return( 0 );
2698}
2699
Paul Bakker41c83d32013-03-20 14:39:14 +01002700void ssl_optimize_checksum( ssl_context *ssl,
2701 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002702{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002703 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002704
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002705#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2706 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002707 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002708 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002709 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002710#endif
2711#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2712#if defined(POLARSSL_SHA512_C)
2713 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2714 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2715 else
2716#endif
2717#if defined(POLARSSL_SHA256_C)
2718 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002719 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002720 else
2721#endif
2722#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2723 /* Should never happen */
2724 return;
Paul Bakker380da532012-04-18 16:10:25 +00002725}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002726
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002727static void ssl_update_checksum_start( ssl_context *ssl,
2728 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002729{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002730#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2731 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002732 md5_update( &ssl->handshake->fin_md5 , buf, len );
2733 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002734#endif
2735#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2736#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002737 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002738#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002739#if defined(POLARSSL_SHA512_C)
2740 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002741#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002742#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002743}
2744
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002745#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2746 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002747static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2748 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002749{
Paul Bakker48916f92012-09-16 19:57:18 +00002750 md5_update( &ssl->handshake->fin_md5 , buf, len );
2751 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002752}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002753#endif
Paul Bakker380da532012-04-18 16:10:25 +00002754
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002755#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2756#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002757static void ssl_update_checksum_sha256( ssl_context *ssl,
2758 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002759{
Paul Bakker9e36f042013-06-30 14:34:05 +02002760 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002761}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002762#endif
Paul Bakker380da532012-04-18 16:10:25 +00002763
Paul Bakker9e36f042013-06-30 14:34:05 +02002764#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002765static void ssl_update_checksum_sha384( ssl_context *ssl,
2766 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002767{
Paul Bakker9e36f042013-06-30 14:34:05 +02002768 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002769}
Paul Bakker769075d2012-11-24 11:26:46 +01002770#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002771#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002772
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002773#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002774static void ssl_calc_finished_ssl(
2775 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002777 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002778 md5_context md5;
2779 sha1_context sha1;
2780
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 unsigned char padbuf[48];
2782 unsigned char md5sum[16];
2783 unsigned char sha1sum[20];
2784
Paul Bakker48916f92012-09-16 19:57:18 +00002785 ssl_session *session = ssl->session_negotiate;
2786 if( !session )
2787 session = ssl->session;
2788
Paul Bakker1ef83d62012-04-11 12:09:53 +00002789 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2790
Paul Bakker48916f92012-09-16 19:57:18 +00002791 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2792 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
2794 /*
2795 * SSLv3:
2796 * hash =
2797 * MD5( master + pad2 +
2798 * MD5( handshake + sender + master + pad1 ) )
2799 * + SHA1( master + pad2 +
2800 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 */
2802
Paul Bakker90995b52013-06-24 19:20:35 +02002803#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002804 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002805 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002806#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
Paul Bakker90995b52013-06-24 19:20:35 +02002808#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002810 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002811#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Paul Bakker3c2122f2013-06-24 19:03:14 +02002813 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2814 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Paul Bakker1ef83d62012-04-11 12:09:53 +00002816 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
Paul Bakker3c2122f2013-06-24 19:03:14 +02002818 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002819 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002820 md5_update( &md5, padbuf, 48 );
2821 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
Paul Bakker3c2122f2013-06-24 19:03:14 +02002823 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002824 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002825 sha1_update( &sha1, padbuf, 40 );
2826 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
Paul Bakker1ef83d62012-04-11 12:09:53 +00002828 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Paul Bakker1ef83d62012-04-11 12:09:53 +00002830 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002831 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002832 md5_update( &md5, padbuf, 48 );
2833 md5_update( &md5, md5sum, 16 );
2834 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Paul Bakker1ef83d62012-04-11 12:09:53 +00002836 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002837 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002838 sha1_update( &sha1, padbuf , 40 );
2839 sha1_update( &sha1, sha1sum, 20 );
2840 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Paul Bakker1ef83d62012-04-11 12:09:53 +00002842 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Paul Bakker1ef83d62012-04-11 12:09:53 +00002844 memset( &md5, 0, sizeof( md5_context ) );
2845 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
2847 memset( padbuf, 0, sizeof( padbuf ) );
2848 memset( md5sum, 0, sizeof( md5sum ) );
2849 memset( sha1sum, 0, sizeof( sha1sum ) );
2850
2851 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2852}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002853#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002855#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002856static void ssl_calc_finished_tls(
2857 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002858{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002859 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002860 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002861 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002863 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Paul Bakker48916f92012-09-16 19:57:18 +00002865 ssl_session *session = ssl->session_negotiate;
2866 if( !session )
2867 session = ssl->session;
2868
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakker48916f92012-09-16 19:57:18 +00002871 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2872 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
Paul Bakker1ef83d62012-04-11 12:09:53 +00002874 /*
2875 * TLSv1:
2876 * hash = PRF( master, finished_label,
2877 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2878 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
Paul Bakker90995b52013-06-24 19:20:35 +02002880#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002881 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2882 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002883#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002884
Paul Bakker90995b52013-06-24 19:20:35 +02002885#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002886 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2887 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002888#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002889
2890 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002891 ? "client finished"
2892 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002893
2894 md5_finish( &md5, padbuf );
2895 sha1_finish( &sha1, padbuf + 16 );
2896
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002897 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002898 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899
2900 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2901
2902 memset( &md5, 0, sizeof( md5_context ) );
2903 memset( &sha1, 0, sizeof( sha1_context ) );
2904
2905 memset( padbuf, 0, sizeof( padbuf ) );
2906
2907 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2908}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002909#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002910
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002911#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2912#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002913static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002914 ssl_context *ssl, unsigned char *buf, int from )
2915{
2916 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002917 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002918 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002919 unsigned char padbuf[32];
2920
Paul Bakker48916f92012-09-16 19:57:18 +00002921 ssl_session *session = ssl->session_negotiate;
2922 if( !session )
2923 session = ssl->session;
2924
Paul Bakker380da532012-04-18 16:10:25 +00002925 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002926
Paul Bakker9e36f042013-06-30 14:34:05 +02002927 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002928
2929 /*
2930 * TLSv1.2:
2931 * hash = PRF( master, finished_label,
2932 * Hash( handshake ) )[0.11]
2933 */
2934
Paul Bakker9e36f042013-06-30 14:34:05 +02002935#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002937 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002938#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939
2940 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002941 ? "client finished"
2942 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943
Paul Bakker9e36f042013-06-30 14:34:05 +02002944 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002946 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002947 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002948
2949 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2950
Paul Bakker9e36f042013-06-30 14:34:05 +02002951 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952
2953 memset( padbuf, 0, sizeof( padbuf ) );
2954
2955 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2956}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002957#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958
Paul Bakker9e36f042013-06-30 14:34:05 +02002959#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002960static void ssl_calc_finished_tls_sha384(
2961 ssl_context *ssl, unsigned char *buf, int from )
2962{
2963 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002964 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002965 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002966 unsigned char padbuf[48];
2967
Paul Bakker48916f92012-09-16 19:57:18 +00002968 ssl_session *session = ssl->session_negotiate;
2969 if( !session )
2970 session = ssl->session;
2971
Paul Bakker380da532012-04-18 16:10:25 +00002972 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002973
Paul Bakker9e36f042013-06-30 14:34:05 +02002974 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002975
2976 /*
2977 * TLSv1.2:
2978 * hash = PRF( master, finished_label,
2979 * Hash( handshake ) )[0.11]
2980 */
2981
Paul Bakker9e36f042013-06-30 14:34:05 +02002982#if !defined(POLARSSL_SHA512_ALT)
2983 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2984 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002985#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00002986
2987 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002988 ? "client finished"
2989 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00002990
Paul Bakker9e36f042013-06-30 14:34:05 +02002991 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002992
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002993 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002994 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002995
2996 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2997
Paul Bakker9e36f042013-06-30 14:34:05 +02002998 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002999
3000 memset( padbuf, 0, sizeof( padbuf ) );
3001
3002 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3003}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003004#endif /* POLARSSL_SHA512_C */
3005#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003006
Paul Bakker48916f92012-09-16 19:57:18 +00003007void ssl_handshake_wrapup( ssl_context *ssl )
3008{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003009 int resume = ssl->handshake->resume;
3010
Paul Bakker48916f92012-09-16 19:57:18 +00003011 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3012
3013 /*
3014 * Free our handshake params
3015 */
3016 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003017 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003018 ssl->handshake = NULL;
3019
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003020 if( ssl->renegotiation == SSL_RENEGOTIATION )
3021 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3022
Paul Bakker48916f92012-09-16 19:57:18 +00003023 /*
3024 * Switch in our now active transform context
3025 */
3026 if( ssl->transform )
3027 {
3028 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003029 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003030 }
3031 ssl->transform = ssl->transform_negotiate;
3032 ssl->transform_negotiate = NULL;
3033
Paul Bakker0a597072012-09-25 21:55:46 +00003034 if( ssl->session )
3035 {
3036 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003037 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003038 }
3039 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003040 ssl->session_negotiate = NULL;
3041
Paul Bakker0a597072012-09-25 21:55:46 +00003042 /*
3043 * Add cache entry
3044 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003045 if( ssl->f_set_cache != NULL &&
3046 ssl->session->length != 0 &&
3047 resume == 0 )
3048 {
Paul Bakker0a597072012-09-25 21:55:46 +00003049 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3050 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003051 }
Paul Bakker0a597072012-09-25 21:55:46 +00003052
Paul Bakker48916f92012-09-16 19:57:18 +00003053 ssl->state++;
3054
3055 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3056}
3057
Paul Bakker1ef83d62012-04-11 12:09:53 +00003058int ssl_write_finished( ssl_context *ssl )
3059{
3060 int ret, hash_len;
3061
3062 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3063
Paul Bakker92be97b2013-01-02 17:30:03 +01003064 /*
3065 * Set the out_msg pointer to the correct location based on IV length
3066 */
3067 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3068 {
3069 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3070 ssl->transform_negotiate->fixed_ivlen;
3071 }
3072 else
3073 ssl->out_msg = ssl->out_iv;
3074
Paul Bakker48916f92012-09-16 19:57:18 +00003075 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076
3077 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3079
Paul Bakker48916f92012-09-16 19:57:18 +00003080 ssl->verify_data_len = hash_len;
3081 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3082
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 ssl->out_msglen = 4 + hash_len;
3084 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3085 ssl->out_msg[0] = SSL_HS_FINISHED;
3086
3087 /*
3088 * In case of session resuming, invert the client and server
3089 * ChangeCipherSpec messages order.
3090 */
Paul Bakker0a597072012-09-25 21:55:46 +00003091 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 {
3093 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003094 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 else
3096 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3097 }
3098 else
3099 ssl->state++;
3100
Paul Bakker48916f92012-09-16 19:57:18 +00003101 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003102 * Switch to our negotiated transform and session parameters for outbound
3103 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003104 */
3105 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3106 ssl->transform_out = ssl->transform_negotiate;
3107 ssl->session_out = ssl->session_negotiate;
3108 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003109
Paul Bakker07eb38b2012-12-19 14:42:06 +01003110#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3111 if( ssl_hw_record_activate != NULL)
3112 {
3113 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3114 {
3115 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3116 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3117 }
3118 }
3119#endif
3120
Paul Bakker5121ce52009-01-03 21:22:43 +00003121 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3122 {
3123 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3124 return( ret );
3125 }
3126
3127 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3128
3129 return( 0 );
3130}
3131
3132int ssl_parse_finished( ssl_context *ssl )
3133{
Paul Bakker23986e52011-04-24 08:57:21 +00003134 int ret;
3135 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 unsigned char buf[36];
3137
3138 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3139
Paul Bakker48916f92012-09-16 19:57:18 +00003140 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003141
Paul Bakker48916f92012-09-16 19:57:18 +00003142 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003143 * Switch to our negotiated transform and session parameters for inbound
3144 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003145 */
3146 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3147 ssl->transform_in = ssl->transform_negotiate;
3148 ssl->session_in = ssl->session_negotiate;
3149 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Paul Bakker92be97b2013-01-02 17:30:03 +01003151 /*
3152 * Set the in_msg pointer to the correct location based on IV length
3153 */
3154 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3155 {
3156 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3157 ssl->transform_negotiate->fixed_ivlen;
3158 }
3159 else
3160 ssl->in_msg = ssl->in_iv;
3161
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_INBOUND ) ) != 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_read_record( ssl ) ) != 0 )
3174 {
3175 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3176 return( ret );
3177 }
3178
3179 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3180 {
3181 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003182 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003183 }
3184
Paul Bakker1ef83d62012-04-11 12:09:53 +00003185 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003186 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3187
3188 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3189 ssl->in_hslen != 4 + hash_len )
3190 {
3191 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003192 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 }
3194
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003195 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 {
3197 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003198 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 }
3200
Paul Bakker48916f92012-09-16 19:57:18 +00003201 ssl->verify_data_len = hash_len;
3202 memcpy( ssl->peer_verify_data, buf, hash_len );
3203
Paul Bakker0a597072012-09-25 21:55:46 +00003204 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 {
3206 if( ssl->endpoint == SSL_IS_CLIENT )
3207 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3208
3209 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003210 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 }
3212 else
3213 ssl->state++;
3214
3215 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3216
3217 return( 0 );
3218}
3219
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003220static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003221{
3222 if( ssl->transform_negotiate )
3223 ssl_transform_free( ssl->transform_negotiate );
3224 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003225 {
3226 ssl->transform_negotiate =
3227 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003228
3229 if( ssl->transform_negotiate != NULL )
3230 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003231 }
Paul Bakker48916f92012-09-16 19:57:18 +00003232
3233 if( ssl->session_negotiate )
3234 ssl_session_free( ssl->session_negotiate );
3235 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003236 {
3237 ssl->session_negotiate =
3238 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003239
3240 if( ssl->session_negotiate != NULL )
3241 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003242 }
Paul Bakker48916f92012-09-16 19:57:18 +00003243
3244 if( ssl->handshake )
3245 ssl_handshake_free( ssl->handshake );
3246 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003247 {
3248 ssl->handshake = (ssl_handshake_params *)
3249 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003250
3251 if( ssl->handshake != NULL )
3252 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003253 }
Paul Bakker48916f92012-09-16 19:57:18 +00003254
3255 if( ssl->handshake == NULL ||
3256 ssl->transform_negotiate == NULL ||
3257 ssl->session_negotiate == NULL )
3258 {
3259 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3260 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3261 }
3262
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003263#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3264 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003265 md5_starts( &ssl->handshake->fin_md5 );
3266 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003267#endif
3268#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3269#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003270 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003271#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003272#if defined(POLARSSL_SHA512_C)
3273 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003274#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003275#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003276
3277 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003278 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003279
Paul Bakker61d113b2013-07-04 11:51:43 +02003280#if defined(POLARSSL_ECDH_C)
3281 ecdh_init( &ssl->handshake->ecdh_ctx );
3282#endif
3283
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003284#if defined(POLARSSL_X509_CRT_PARSE_C)
3285 ssl->handshake->key_cert = ssl->key_cert;
3286#endif
3287
Paul Bakker48916f92012-09-16 19:57:18 +00003288 return( 0 );
3289}
3290
Paul Bakker5121ce52009-01-03 21:22:43 +00003291/*
3292 * Initialize an SSL context
3293 */
3294int ssl_init( ssl_context *ssl )
3295{
Paul Bakker48916f92012-09-16 19:57:18 +00003296 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 int len = SSL_BUFFER_LEN;
3298
3299 memset( ssl, 0, sizeof( ssl_context ) );
3300
Paul Bakker62f2dee2012-09-28 07:31:51 +00003301 /*
3302 * Sane defaults
3303 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003304 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3305 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3306 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3307 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003308
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003309 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003310
Paul Bakker62f2dee2012-09-28 07:31:51 +00003311#if defined(POLARSSL_DHM_C)
3312 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3313 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3314 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3315 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3316 {
3317 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3318 return( ret );
3319 }
3320#endif
3321
3322 /*
3323 * Prepare base structures
3324 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003325 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003327 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 ssl->in_msg = ssl->in_ctr + 13;
3329
3330 if( ssl->in_ctr == NULL )
3331 {
3332 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003333 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 }
3335
Paul Bakker6e339b52013-07-03 13:37:05 +02003336 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003338 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003339 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003340
3341 if( ssl->out_ctr == NULL )
3342 {
3343 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003344 polarssl_free( ssl->in_ctr );
3345 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003346 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 }
3348
3349 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3350 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3351
Paul Bakker606b4ba2013-08-14 16:52:14 +02003352#if defined(POLARSSL_SSL_SESSION_TICKETS)
3353 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3354#endif
3355
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003356#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003357 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003358#endif
3359
Paul Bakker48916f92012-09-16 19:57:18 +00003360 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3361 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
3363 return( 0 );
3364}
3365
3366/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003367 * Reset an initialized and used SSL context for re-use while retaining
3368 * all application-set variables, function pointers and data.
3369 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003370int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003371{
Paul Bakker48916f92012-09-16 19:57:18 +00003372 int ret;
3373
Paul Bakker7eb013f2011-10-06 12:37:39 +00003374 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003375 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3376 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3377
3378 ssl->verify_data_len = 0;
3379 memset( ssl->own_verify_data, 0, 36 );
3380 memset( ssl->peer_verify_data, 0, 36 );
3381
Paul Bakker7eb013f2011-10-06 12:37:39 +00003382 ssl->in_offt = NULL;
3383
Paul Bakker92be97b2013-01-02 17:30:03 +01003384 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003385 ssl->in_msgtype = 0;
3386 ssl->in_msglen = 0;
3387 ssl->in_left = 0;
3388
3389 ssl->in_hslen = 0;
3390 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003391 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003392
Paul Bakker92be97b2013-01-02 17:30:03 +01003393 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003394 ssl->out_msgtype = 0;
3395 ssl->out_msglen = 0;
3396 ssl->out_left = 0;
3397
Paul Bakker48916f92012-09-16 19:57:18 +00003398 ssl->transform_in = NULL;
3399 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003400
3401 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3402 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003403
3404#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3405 if( ssl_hw_record_reset != NULL)
3406 {
3407 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003408 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003409 {
3410 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3411 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3412 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003413 }
3414#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003415
Paul Bakker48916f92012-09-16 19:57:18 +00003416 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003417 {
Paul Bakker48916f92012-09-16 19:57:18 +00003418 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003419 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003420 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003421 }
Paul Bakker48916f92012-09-16 19:57:18 +00003422
Paul Bakkerc0463502013-02-14 11:19:38 +01003423 if( ssl->session )
3424 {
3425 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003426 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003427 ssl->session = NULL;
3428 }
3429
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003430#if defined(POLARSSL_SSL_ALPN)
3431 ssl->alpn_chosen = NULL;
3432#endif
3433
Paul Bakker48916f92012-09-16 19:57:18 +00003434 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3435 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003436
3437 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003438}
3439
Paul Bakkera503a632013-08-14 13:48:06 +02003440#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003441/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003442 * Allocate and initialize ticket keys
3443 */
3444static int ssl_ticket_keys_init( ssl_context *ssl )
3445{
3446 int ret;
3447 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003448 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003449
3450 if( ssl->ticket_keys != NULL )
3451 return( 0 );
3452
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003453 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3454 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003455 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3456
3457 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003458 {
3459 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003460 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003461 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003462
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003463 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3464 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3465 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3466 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003467 polarssl_free( tkeys );
3468 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003469 }
3470
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003471 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003472 {
3473 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003474 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003475 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003476
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003477 ssl->ticket_keys = tkeys;
3478
3479 return( 0 );
3480}
Paul Bakkera503a632013-08-14 13:48:06 +02003481#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003482
3483/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 * SSL set accessors
3485 */
3486void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3487{
3488 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003489
Paul Bakker606b4ba2013-08-14 16:52:14 +02003490#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003491 if( endpoint == SSL_IS_CLIENT )
3492 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003493#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003494}
3495
3496void ssl_set_authmode( ssl_context *ssl, int authmode )
3497{
3498 ssl->authmode = authmode;
3499}
3500
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003501#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003502void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003503 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003504 void *p_vrfy )
3505{
3506 ssl->f_vrfy = f_vrfy;
3507 ssl->p_vrfy = p_vrfy;
3508}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003509#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003510
Paul Bakker5121ce52009-01-03 21:22:43 +00003511void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003512 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 void *p_rng )
3514{
3515 ssl->f_rng = f_rng;
3516 ssl->p_rng = p_rng;
3517}
3518
3519void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003520 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003521 void *p_dbg )
3522{
3523 ssl->f_dbg = f_dbg;
3524 ssl->p_dbg = p_dbg;
3525}
3526
3527void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003528 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003529 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003530{
3531 ssl->f_recv = f_recv;
3532 ssl->f_send = f_send;
3533 ssl->p_recv = p_recv;
3534 ssl->p_send = p_send;
3535}
3536
Paul Bakker0a597072012-09-25 21:55:46 +00003537void ssl_set_session_cache( ssl_context *ssl,
3538 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3539 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003540{
Paul Bakker0a597072012-09-25 21:55:46 +00003541 ssl->f_get_cache = f_get_cache;
3542 ssl->p_get_cache = p_get_cache;
3543 ssl->f_set_cache = f_set_cache;
3544 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003545}
3546
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003547int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003548{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003549 int ret;
3550
3551 if( ssl == NULL ||
3552 session == NULL ||
3553 ssl->session_negotiate == NULL ||
3554 ssl->endpoint != SSL_IS_CLIENT )
3555 {
3556 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3557 }
3558
3559 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3560 return( ret );
3561
Paul Bakker0a597072012-09-25 21:55:46 +00003562 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003563
3564 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003565}
3566
Paul Bakkerb68cad62012-08-23 08:34:18 +00003567void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003568{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003569 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3570 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3571 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3572 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3573}
3574
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003575void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3576 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003577 int major, int minor )
3578{
3579 if( major != SSL_MAJOR_VERSION_3 )
3580 return;
3581
3582 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3583 return;
3584
3585 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003586}
3587
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003588#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003589/* Add a new (empty) key_cert entry an return a pointer to it */
3590static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3591{
3592 ssl_key_cert *key_cert, *last;
3593
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003594 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3595 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003596 return( NULL );
3597
3598 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3599
3600 /* Append the new key_cert to the (possibly empty) current list */
3601 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003602 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003603 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003604 if( ssl->handshake != NULL )
3605 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003606 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003607 else
3608 {
3609 last = ssl->key_cert;
3610 while( last->next != NULL )
3611 last = last->next;
3612 last->next = key_cert;
3613 }
3614
3615 return key_cert;
3616}
3617
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003618void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003619 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003620{
3621 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003622 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003623 ssl->peer_cn = peer_cn;
3624}
3625
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003626int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003627 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003628{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003629 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3630
3631 if( key_cert == NULL )
3632 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3633
3634 key_cert->cert = own_cert;
3635 key_cert->key = pk_key;
3636
3637 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003638}
3639
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003640#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003641int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003642 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003643{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003644 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003645 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003646
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003647 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003648 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3649
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003650 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3651 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003652 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003653
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003654 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003655
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003656 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003657 if( ret != 0 )
3658 return( ret );
3659
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003660 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003661 return( ret );
3662
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003663 key_cert->cert = own_cert;
3664 key_cert->key_own_alloc = 1;
3665
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003666 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003667}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003668#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003669
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003670int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003671 void *rsa_key,
3672 rsa_decrypt_func rsa_decrypt,
3673 rsa_sign_func rsa_sign,
3674 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003675{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003676 int ret;
3677 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003678
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003679 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003680 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3681
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003682 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3683 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003684 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003685
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003686 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003687
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003688 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3689 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3690 return( ret );
3691
3692 key_cert->cert = own_cert;
3693 key_cert->key_own_alloc = 1;
3694
3695 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003696}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003697#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003698
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003699#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003700int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3701 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003702{
Paul Bakker6db455e2013-09-18 17:29:31 +02003703 if( psk == NULL || psk_identity == NULL )
3704 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3705
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003706 /*
3707 * The length will be check later anyway, but in case it is obviously
3708 * too large, better abort now. The PMS is as follows:
3709 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3710 */
3711 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3712 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3713
Paul Bakker6db455e2013-09-18 17:29:31 +02003714 if( ssl->psk != NULL )
3715 {
3716 polarssl_free( ssl->psk );
3717 polarssl_free( ssl->psk_identity );
3718 }
3719
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003720 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003721 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003722
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003723 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003724 ssl->psk_identity = (unsigned char *)
3725 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003726
3727 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3728 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3729
3730 memcpy( ssl->psk, psk, ssl->psk_len );
3731 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003732
3733 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003734}
3735
3736void ssl_set_psk_cb( ssl_context *ssl,
3737 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3738 size_t),
3739 void *p_psk )
3740{
3741 ssl->f_psk = f_psk;
3742 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003743}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003744#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003745
Paul Bakker48916f92012-09-16 19:57:18 +00003746#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003747int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003748{
3749 int ret;
3750
Paul Bakker48916f92012-09-16 19:57:18 +00003751 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003752 {
3753 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3754 return( ret );
3755 }
3756
Paul Bakker48916f92012-09-16 19:57:18 +00003757 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003758 {
3759 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3760 return( ret );
3761 }
3762
3763 return( 0 );
3764}
3765
Paul Bakker1b57b062011-01-06 15:48:19 +00003766int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3767{
3768 int ret;
3769
Paul Bakker48916f92012-09-16 19:57:18 +00003770 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003771 {
3772 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3773 return( ret );
3774 }
3775
Paul Bakker48916f92012-09-16 19:57:18 +00003776 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003777 {
3778 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3779 return( ret );
3780 }
3781
3782 return( 0 );
3783}
Paul Bakker48916f92012-09-16 19:57:18 +00003784#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003785
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003786#if defined(POLARSSL_SSL_SET_CURVES)
3787/*
3788 * Set the allowed elliptic curves
3789 */
3790void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3791{
3792 ssl->curve_list = curve_list;
3793}
3794#endif
3795
Paul Bakker0be444a2013-08-27 21:55:01 +02003796#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003797int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003798{
3799 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003800 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003801
3802 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003803
3804 if( ssl->hostname_len + 1 == 0 )
3805 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3806
Paul Bakker6e339b52013-07-03 13:37:05 +02003807 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003808
Paul Bakkerb15b8512012-01-13 13:44:06 +00003809 if( ssl->hostname == NULL )
3810 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3811
Paul Bakker3c2122f2013-06-24 19:03:14 +02003812 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003813 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003814
Paul Bakker40ea7de2009-05-03 10:18:48 +00003815 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003816
3817 return( 0 );
3818}
3819
Paul Bakker5701cdc2012-09-27 21:49:42 +00003820void ssl_set_sni( ssl_context *ssl,
3821 int (*f_sni)(void *, ssl_context *,
3822 const unsigned char *, size_t),
3823 void *p_sni )
3824{
3825 ssl->f_sni = f_sni;
3826 ssl->p_sni = p_sni;
3827}
Paul Bakker0be444a2013-08-27 21:55:01 +02003828#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003829
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003830#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003831int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003832{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003833 size_t cur_len, tot_len;
3834 const char **p;
3835
3836 /*
3837 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3838 * truncated". Check lengths now rather than later.
3839 */
3840 tot_len = 0;
3841 for( p = protos; *p != NULL; p++ )
3842 {
3843 cur_len = strlen( *p );
3844 tot_len += cur_len;
3845
3846 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3847 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3848 }
3849
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003850 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003851
3852 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003853}
3854
3855const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3856{
3857 return ssl->alpn_chosen;
3858}
3859#endif /* POLARSSL_SSL_ALPN */
3860
Paul Bakker490ecc82011-10-06 13:04:09 +00003861void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3862{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003863 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3864 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3865 {
3866 ssl->max_major_ver = major;
3867 ssl->max_minor_ver = minor;
3868 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003869}
3870
Paul Bakker1d29fb52012-09-28 13:28:45 +00003871void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3872{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003873 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3874 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3875 {
3876 ssl->min_major_ver = major;
3877 ssl->min_minor_ver = minor;
3878 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003879}
3880
Paul Bakker05decb22013-08-15 13:33:48 +02003881#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003882int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3883{
Paul Bakker77e257e2013-12-16 15:29:52 +01003884 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003885 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003886 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003887 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003888 }
3889
3890 ssl->mfl_code = mfl_code;
3891
3892 return( 0 );
3893}
Paul Bakker05decb22013-08-15 13:33:48 +02003894#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003895
Paul Bakker1f2bc622013-08-15 13:45:55 +02003896#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003897int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003898{
3899 if( ssl->endpoint != SSL_IS_CLIENT )
3900 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3901
Paul Bakker8c1ede62013-07-19 14:14:37 +02003902 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003903
3904 return( 0 );
3905}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003906#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003907
Paul Bakker48916f92012-09-16 19:57:18 +00003908void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3909{
3910 ssl->disable_renegotiation = renegotiation;
3911}
3912
3913void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3914{
3915 ssl->allow_legacy_renegotiation = allow_legacy;
3916}
3917
Paul Bakkera503a632013-08-14 13:48:06 +02003918#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003919int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3920{
3921 ssl->session_tickets = use_tickets;
3922
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003923 if( ssl->endpoint == SSL_IS_CLIENT )
3924 return( 0 );
3925
3926 if( ssl->f_rng == NULL )
3927 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3928
3929 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003930}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003931
3932void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3933{
3934 ssl->ticket_lifetime = lifetime;
3935}
Paul Bakkera503a632013-08-14 13:48:06 +02003936#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003937
Paul Bakker5121ce52009-01-03 21:22:43 +00003938/*
3939 * SSL get accessors
3940 */
Paul Bakker23986e52011-04-24 08:57:21 +00003941size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003942{
3943 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3944}
3945
Paul Bakkerff60ee62010-03-16 21:09:09 +00003946int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003947{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003948 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003949}
3950
Paul Bakkere3166ce2011-01-27 17:40:50 +00003951const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003952{
Paul Bakker926c8e42013-03-06 10:23:34 +01003953 if( ssl == NULL || ssl->session == NULL )
3954 return NULL;
3955
Paul Bakkere3166ce2011-01-27 17:40:50 +00003956 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003957}
3958
Paul Bakker43ca69c2011-01-15 17:35:19 +00003959const char *ssl_get_version( const ssl_context *ssl )
3960{
3961 switch( ssl->minor_ver )
3962 {
3963 case SSL_MINOR_VERSION_0:
3964 return( "SSLv3.0" );
3965
3966 case SSL_MINOR_VERSION_1:
3967 return( "TLSv1.0" );
3968
3969 case SSL_MINOR_VERSION_2:
3970 return( "TLSv1.1" );
3971
Paul Bakker1ef83d62012-04-11 12:09:53 +00003972 case SSL_MINOR_VERSION_3:
3973 return( "TLSv1.2" );
3974
Paul Bakker43ca69c2011-01-15 17:35:19 +00003975 default:
3976 break;
3977 }
3978 return( "unknown" );
3979}
3980
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003981#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003982const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003983{
3984 if( ssl == NULL || ssl->session == NULL )
3985 return NULL;
3986
3987 return ssl->session->peer_cert;
3988}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003989#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003990
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003991int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3992{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003993 if( ssl == NULL ||
3994 dst == NULL ||
3995 ssl->session == NULL ||
3996 ssl->endpoint != SSL_IS_CLIENT )
3997 {
3998 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3999 }
4000
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004001 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004002}
4003
Paul Bakker5121ce52009-01-03 21:22:43 +00004004/*
Paul Bakker1961b702013-01-25 14:49:24 +01004005 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 */
Paul Bakker1961b702013-01-25 14:49:24 +01004007int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004008{
Paul Bakker40e46942009-01-03 21:51:57 +00004009 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
Paul Bakker40e46942009-01-03 21:51:57 +00004011#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004012 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004013 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004014#endif
4015
Paul Bakker40e46942009-01-03 21:51:57 +00004016#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004018 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004019#endif
4020
Paul Bakker1961b702013-01-25 14:49:24 +01004021 return( ret );
4022}
4023
4024/*
4025 * Perform the SSL handshake
4026 */
4027int ssl_handshake( ssl_context *ssl )
4028{
4029 int ret = 0;
4030
4031 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4032
4033 while( ssl->state != SSL_HANDSHAKE_OVER )
4034 {
4035 ret = ssl_handshake_step( ssl );
4036
4037 if( ret != 0 )
4038 break;
4039 }
4040
Paul Bakker5121ce52009-01-03 21:22:43 +00004041 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4042
4043 return( ret );
4044}
4045
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004046#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004047/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004048 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004049 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004050static int ssl_write_hello_request( ssl_context *ssl )
4051{
4052 int ret;
4053
4054 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4055
4056 ssl->out_msglen = 4;
4057 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4058 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4059
4060 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4061 {
4062 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4063 return( ret );
4064 }
4065
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004066 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4067
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004068 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4069
4070 return( 0 );
4071}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004072#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004073
4074/*
4075 * Actually renegotiate current connection, triggered by either:
4076 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004077 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004078 * - receiving any handshake message on server during ssl_read() after the
4079 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004080 * If the handshake doesn't complete due to waiting for I/O, it will continue
4081 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004082 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004083static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004084{
4085 int ret;
4086
4087 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4088
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004089 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4090 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004091
4092 ssl->state = SSL_HELLO_REQUEST;
4093 ssl->renegotiation = SSL_RENEGOTIATION;
4094
Paul Bakker48916f92012-09-16 19:57:18 +00004095 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4096 {
4097 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4098 return( ret );
4099 }
4100
4101 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4102
4103 return( 0 );
4104}
4105
4106/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004107 * Renegotiate current connection on client,
4108 * or request renegotiation on server
4109 */
4110int ssl_renegotiate( ssl_context *ssl )
4111{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004112 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004113
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004114#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004115 /* On server, just send the request */
4116 if( ssl->endpoint == SSL_IS_SERVER )
4117 {
4118 if( ssl->state != SSL_HANDSHAKE_OVER )
4119 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4120
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004121 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004122 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004123#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004124
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004125#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004126 /*
4127 * On client, either start the renegotiation process or,
4128 * if already in progress, continue the handshake
4129 */
4130 if( ssl->renegotiation != SSL_RENEGOTIATION )
4131 {
4132 if( ssl->state != SSL_HANDSHAKE_OVER )
4133 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4134
4135 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4136 {
4137 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4138 return( ret );
4139 }
4140 }
4141 else
4142 {
4143 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4144 {
4145 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4146 return( ret );
4147 }
4148 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004149#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004150
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004151 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004152}
4153
4154/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004155 * Receive application data decrypted from the SSL layer
4156 */
Paul Bakker23986e52011-04-24 08:57:21 +00004157int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004158{
Paul Bakker23986e52011-04-24 08:57:21 +00004159 int ret;
4160 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004161
4162 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4163
4164 if( ssl->state != SSL_HANDSHAKE_OVER )
4165 {
4166 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4167 {
4168 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4169 return( ret );
4170 }
4171 }
4172
4173 if( ssl->in_offt == NULL )
4174 {
4175 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4176 {
Paul Bakker831a7552011-05-18 13:32:51 +00004177 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4178 return( 0 );
4179
Paul Bakker5121ce52009-01-03 21:22:43 +00004180 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4181 return( ret );
4182 }
4183
4184 if( ssl->in_msglen == 0 &&
4185 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4186 {
4187 /*
4188 * OpenSSL sends empty messages to randomize the IV
4189 */
4190 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4191 {
Paul Bakker831a7552011-05-18 13:32:51 +00004192 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4193 return( 0 );
4194
Paul Bakker5121ce52009-01-03 21:22:43 +00004195 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4196 return( ret );
4197 }
4198 }
4199
Paul Bakker48916f92012-09-16 19:57:18 +00004200 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4201 {
4202 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4203
4204 if( ssl->endpoint == SSL_IS_CLIENT &&
4205 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4206 ssl->in_hslen != 4 ) )
4207 {
4208 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4209 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4210 }
4211
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004212 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4213 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004214 ssl->allow_legacy_renegotiation ==
4215 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004216 {
4217 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4218
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004219#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004220 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004221 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004222 /*
4223 * SSLv3 does not have a "no_renegotiation" alert
4224 */
4225 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4226 return( ret );
4227 }
4228 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004229#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004230#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4231 defined(POLARSSL_SSL_PROTO_TLS1_2)
4232 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004233 {
4234 if( ( ret = ssl_send_alert_message( ssl,
4235 SSL_ALERT_LEVEL_WARNING,
4236 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4237 {
4238 return( ret );
4239 }
Paul Bakker48916f92012-09-16 19:57:18 +00004240 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004241 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004242#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4243 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004244 {
4245 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004246 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004247 }
Paul Bakker48916f92012-09-16 19:57:18 +00004248 }
4249 else
4250 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004251 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004252 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004253 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004254 return( ret );
4255 }
4256
4257 return( POLARSSL_ERR_NET_WANT_READ );
4258 }
4259 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004260 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4261 {
4262 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4263 "but not honored by client" ) );
4264 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4265 }
Paul Bakker48916f92012-09-16 19:57:18 +00004266 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 {
4268 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004269 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004270 }
4271
4272 ssl->in_offt = ssl->in_msg;
4273 }
4274
4275 n = ( len < ssl->in_msglen )
4276 ? len : ssl->in_msglen;
4277
4278 memcpy( buf, ssl->in_offt, n );
4279 ssl->in_msglen -= n;
4280
4281 if( ssl->in_msglen == 0 )
4282 /* all bytes consumed */
4283 ssl->in_offt = NULL;
4284 else
4285 /* more data available */
4286 ssl->in_offt += n;
4287
4288 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4289
Paul Bakker23986e52011-04-24 08:57:21 +00004290 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004291}
4292
4293/*
4294 * Send application data to be encrypted by the SSL layer
4295 */
Paul Bakker23986e52011-04-24 08:57:21 +00004296int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004297{
Paul Bakker23986e52011-04-24 08:57:21 +00004298 int ret;
4299 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004300 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004301
4302 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4303
4304 if( ssl->state != SSL_HANDSHAKE_OVER )
4305 {
4306 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4307 {
4308 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4309 return( ret );
4310 }
4311 }
4312
Paul Bakker05decb22013-08-15 13:33:48 +02004313#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004314 /*
4315 * Assume mfl_code is correct since it was checked when set
4316 */
4317 max_len = mfl_code_to_length[ssl->mfl_code];
4318
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004319 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004320 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004321 */
4322 if( ssl->session_out != NULL &&
4323 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4324 {
4325 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4326 }
Paul Bakker05decb22013-08-15 13:33:48 +02004327#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004328
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004329 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004330
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 if( ssl->out_left != 0 )
4332 {
4333 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4334 {
4335 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4336 return( ret );
4337 }
4338 }
Paul Bakker887bd502011-06-08 13:10:54 +00004339 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004340 {
Paul Bakker887bd502011-06-08 13:10:54 +00004341 ssl->out_msglen = n;
4342 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4343 memcpy( ssl->out_msg, buf, n );
4344
4345 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4346 {
4347 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4348 return( ret );
4349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004350 }
4351
4352 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4353
Paul Bakker23986e52011-04-24 08:57:21 +00004354 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004355}
4356
4357/*
4358 * Notify the peer that the connection is being closed
4359 */
4360int ssl_close_notify( ssl_context *ssl )
4361{
4362 int ret;
4363
4364 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4365
4366 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4367 {
4368 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4369 return( ret );
4370 }
4371
4372 if( ssl->state == SSL_HANDSHAKE_OVER )
4373 {
Paul Bakker48916f92012-09-16 19:57:18 +00004374 if( ( ret = ssl_send_alert_message( ssl,
4375 SSL_ALERT_LEVEL_WARNING,
4376 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004377 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004378 return( ret );
4379 }
4380 }
4381
4382 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4383
4384 return( ret );
4385}
4386
Paul Bakker48916f92012-09-16 19:57:18 +00004387void ssl_transform_free( ssl_transform *transform )
4388{
4389#if defined(POLARSSL_ZLIB_SUPPORT)
4390 deflateEnd( &transform->ctx_deflate );
4391 inflateEnd( &transform->ctx_inflate );
4392#endif
4393
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004394 cipher_free_ctx( &transform->cipher_ctx_enc );
4395 cipher_free_ctx( &transform->cipher_ctx_dec );
4396
Paul Bakker61d113b2013-07-04 11:51:43 +02004397 md_free_ctx( &transform->md_ctx_enc );
4398 md_free_ctx( &transform->md_ctx_dec );
4399
Paul Bakker48916f92012-09-16 19:57:18 +00004400 memset( transform, 0, sizeof( ssl_transform ) );
4401}
4402
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004403#if defined(POLARSSL_X509_CRT_PARSE_C)
4404static void ssl_key_cert_free( ssl_key_cert *key_cert )
4405{
4406 ssl_key_cert *cur = key_cert, *next;
4407
4408 while( cur != NULL )
4409 {
4410 next = cur->next;
4411
4412 if( cur->key_own_alloc )
4413 {
4414 pk_free( cur->key );
4415 polarssl_free( cur->key );
4416 }
4417 polarssl_free( cur );
4418
4419 cur = next;
4420 }
4421}
4422#endif /* POLARSSL_X509_CRT_PARSE_C */
4423
Paul Bakker48916f92012-09-16 19:57:18 +00004424void ssl_handshake_free( ssl_handshake_params *handshake )
4425{
4426#if defined(POLARSSL_DHM_C)
4427 dhm_free( &handshake->dhm_ctx );
4428#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004429#if defined(POLARSSL_ECDH_C)
4430 ecdh_free( &handshake->ecdh_ctx );
4431#endif
4432
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004433#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004434 /* explicit void pointer cast for buggy MS compiler */
4435 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004436#endif
4437
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004438#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4439 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4440 /*
4441 * Free only the linked list wrapper, not the keys themselves
4442 * since the belong to the SNI callback
4443 */
4444 if( handshake->sni_key_cert != NULL )
4445 {
4446 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4447
4448 while( cur != NULL )
4449 {
4450 next = cur->next;
4451 polarssl_free( cur );
4452 cur = next;
4453 }
4454 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004455#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004456
Paul Bakker48916f92012-09-16 19:57:18 +00004457 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4458}
4459
4460void ssl_session_free( ssl_session *session )
4461{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004462#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004463 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004464 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004465 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004466 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004467 }
Paul Bakkered27a042013-04-18 22:46:23 +02004468#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004469
Paul Bakkera503a632013-08-14 13:48:06 +02004470#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004471 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004472#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004473
Paul Bakker0a597072012-09-25 21:55:46 +00004474 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004475}
4476
Paul Bakker5121ce52009-01-03 21:22:43 +00004477/*
4478 * Free an SSL context
4479 */
4480void ssl_free( ssl_context *ssl )
4481{
4482 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4483
Paul Bakker5121ce52009-01-03 21:22:43 +00004484 if( ssl->out_ctr != NULL )
4485 {
4486 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004487 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004488 }
4489
4490 if( ssl->in_ctr != NULL )
4491 {
4492 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004493 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 }
4495
Paul Bakker16770332013-10-11 09:59:44 +02004496#if defined(POLARSSL_ZLIB_SUPPORT)
4497 if( ssl->compress_buf != NULL )
4498 {
4499 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4500 polarssl_free( ssl->compress_buf );
4501 }
4502#endif
4503
Paul Bakker40e46942009-01-03 21:51:57 +00004504#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004505 mpi_free( &ssl->dhm_P );
4506 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004507#endif
4508
Paul Bakker48916f92012-09-16 19:57:18 +00004509 if( ssl->transform )
4510 {
4511 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004512 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004513 }
4514
4515 if( ssl->handshake )
4516 {
4517 ssl_handshake_free( ssl->handshake );
4518 ssl_transform_free( ssl->transform_negotiate );
4519 ssl_session_free( ssl->session_negotiate );
4520
Paul Bakker6e339b52013-07-03 13:37:05 +02004521 polarssl_free( ssl->handshake );
4522 polarssl_free( ssl->transform_negotiate );
4523 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004524 }
4525
Paul Bakkerc0463502013-02-14 11:19:38 +01004526 if( ssl->session )
4527 {
4528 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004529 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004530 }
4531
Paul Bakkera503a632013-08-14 13:48:06 +02004532#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004533 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004534#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004535
Paul Bakker0be444a2013-08-27 21:55:01 +02004536#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004537 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004538 {
4539 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004540 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004541 ssl->hostname_len = 0;
4542 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004543#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004544
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004545#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004546 if( ssl->psk != NULL )
4547 {
4548 memset( ssl->psk, 0, ssl->psk_len );
4549 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4550 polarssl_free( ssl->psk );
4551 polarssl_free( ssl->psk_identity );
4552 ssl->psk_len = 0;
4553 ssl->psk_identity_len = 0;
4554 }
4555#endif
4556
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004557#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004558 ssl_key_cert_free( ssl->key_cert );
4559#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004560
Paul Bakker05ef8352012-05-08 09:17:57 +00004561#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4562 if( ssl_hw_record_finish != NULL )
4563 {
4564 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4565 ssl_hw_record_finish( ssl );
4566 }
4567#endif
4568
Paul Bakker5121ce52009-01-03 21:22:43 +00004569 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004570
Paul Bakker86f04f42013-02-14 11:20:09 +01004571 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004572 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004573}
4574
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004575#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004576/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004577 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004578 */
4579unsigned char ssl_sig_from_pk( pk_context *pk )
4580{
4581#if defined(POLARSSL_RSA_C)
4582 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4583 return( SSL_SIG_RSA );
4584#endif
4585#if defined(POLARSSL_ECDSA_C)
4586 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4587 return( SSL_SIG_ECDSA );
4588#endif
4589 return( SSL_SIG_ANON );
4590}
4591
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004592pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4593{
4594 switch( sig )
4595 {
4596#if defined(POLARSSL_RSA_C)
4597 case SSL_SIG_RSA:
4598 return( POLARSSL_PK_RSA );
4599#endif
4600#if defined(POLARSSL_ECDSA_C)
4601 case SSL_SIG_ECDSA:
4602 return( POLARSSL_PK_ECDSA );
4603#endif
4604 default:
4605 return( POLARSSL_PK_NONE );
4606 }
4607}
Paul Bakker9af723c2014-05-01 13:03:14 +02004608#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004609
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004610/*
4611 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4612 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004613md_type_t ssl_md_alg_from_hash( unsigned char hash )
4614{
4615 switch( hash )
4616 {
4617#if defined(POLARSSL_MD5_C)
4618 case SSL_HASH_MD5:
4619 return( POLARSSL_MD_MD5 );
4620#endif
4621#if defined(POLARSSL_SHA1_C)
4622 case SSL_HASH_SHA1:
4623 return( POLARSSL_MD_SHA1 );
4624#endif
4625#if defined(POLARSSL_SHA256_C)
4626 case SSL_HASH_SHA224:
4627 return( POLARSSL_MD_SHA224 );
4628 case SSL_HASH_SHA256:
4629 return( POLARSSL_MD_SHA256 );
4630#endif
4631#if defined(POLARSSL_SHA512_C)
4632 case SSL_HASH_SHA384:
4633 return( POLARSSL_MD_SHA384 );
4634 case SSL_HASH_SHA512:
4635 return( POLARSSL_MD_SHA512 );
4636#endif
4637 default:
4638 return( POLARSSL_MD_NONE );
4639 }
4640}
4641
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004642#if defined(POLARSSL_SSL_SET_CURVES)
4643/*
4644 * Check is a curve proposed by the peer is in our list.
4645 * Return 1 if we're willing to use it, 0 otherwise.
4646 */
4647int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4648{
4649 const ecp_group_id *gid;
4650
4651 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4652 if( *gid == grp_id )
4653 return( 1 );
4654
4655 return( 0 );
4656}
Paul Bakker9af723c2014-05-01 13:03:14 +02004657#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004658
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004659#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004660int ssl_check_cert_usage( const x509_crt *cert,
4661 const ssl_ciphersuite_t *ciphersuite,
4662 int cert_endpoint )
4663{
4664#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4665 int usage = 0;
4666#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004667#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4668 const char *ext_oid;
4669 size_t ext_len;
4670#endif
4671
4672#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4673 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4674 ((void) cert);
4675 ((void) cert_endpoint);
4676#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004677
4678#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4679 if( cert_endpoint == SSL_IS_SERVER )
4680 {
4681 /* Server part of the key exchange */
4682 switch( ciphersuite->key_exchange )
4683 {
4684 case POLARSSL_KEY_EXCHANGE_RSA:
4685 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4686 usage = KU_KEY_ENCIPHERMENT;
4687 break;
4688
4689 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4690 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4691 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4692 usage = KU_DIGITAL_SIGNATURE;
4693 break;
4694
4695 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4696 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4697 usage = KU_KEY_AGREEMENT;
4698 break;
4699
4700 /* Don't use default: we want warnings when adding new values */
4701 case POLARSSL_KEY_EXCHANGE_NONE:
4702 case POLARSSL_KEY_EXCHANGE_PSK:
4703 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4704 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4705 usage = 0;
4706 }
4707 }
4708 else
4709 {
4710 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4711 usage = KU_DIGITAL_SIGNATURE;
4712 }
4713
4714 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4715 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004716#else
4717 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004718#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4719
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004720#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4721 if( cert_endpoint == SSL_IS_SERVER )
4722 {
4723 ext_oid = OID_SERVER_AUTH;
4724 ext_len = OID_SIZE( OID_SERVER_AUTH );
4725 }
4726 else
4727 {
4728 ext_oid = OID_CLIENT_AUTH;
4729 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4730 }
4731
4732 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4733 return( -1 );
4734#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4735
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004736 return( 0 );
4737}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004738#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004739
4740#endif /* POLARSSL_SSL_TLS_C */