blob: 480c5e587057e0483b4a8d6d7de755f0775cee72 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker05decb22013-08-15 13:33:48 +020064#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020065/*
66 * Convert max_fragment_length codes to length.
67 * RFC 6066 says:
68 * enum{
69 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
70 * } MaxFragmentLength;
71 * and we add 0 -> extension unused
72 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020073static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020074{
75 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
76 512, /* SSL_MAX_FRAG_LEN_512 */
77 1024, /* SSL_MAX_FRAG_LEN_1024 */
78 2048, /* SSL_MAX_FRAG_LEN_2048 */
79 4096, /* SSL_MAX_FRAG_LEN_4096 */
80};
Paul Bakker05decb22013-08-15 13:33:48 +020081#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020082
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020083static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
84{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020085 ssl_session_free( dst );
86 memcpy( dst, src, sizeof( ssl_session ) );
87
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089 if( src->peer_cert != NULL )
90 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020091 int ret;
92
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020093 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
94 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020095 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
96
Paul Bakkerb6b09562013-09-18 14:17:41 +020097 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098
Paul Bakkerddf26b42013-09-18 13:46:23 +020099 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
100 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101 {
102 polarssl_free( dst->peer_cert );
103 dst->peer_cert = NULL;
104 return( ret );
105 }
106 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200108
Paul Bakkera503a632013-08-14 13:48:06 +0200109#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110 if( src->ticket != NULL )
111 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200112 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
113 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200114 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
115
116 memcpy( dst->ticket, src->ticket, src->ticket_len );
117 }
Paul Bakkera503a632013-08-14 13:48:06 +0200118#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119
120 return( 0 );
121}
122
Paul Bakker05ef8352012-05-08 09:17:57 +0000123#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
124int (*ssl_hw_record_init)(ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200125 const unsigned char *key_enc, const unsigned char *key_dec,
126 size_t keylen,
127 const unsigned char *iv_enc, const unsigned char *iv_dec,
128 size_t ivlen,
129 const unsigned char *mac_enc, const unsigned char *mac_dec,
130 size_t maclen) = NULL;
Paul Bakker07eb38b2012-12-19 14:42:06 +0100131int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000132int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
133int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
134int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
135int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200136#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000137
Paul Bakker5121ce52009-01-03 21:22:43 +0000138/*
139 * Key material generation
140 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200141#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200142static int ssl3_prf( const unsigned char *secret, size_t slen,
143 const char *label,
144 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000145 unsigned char *dstbuf, size_t dlen )
146{
147 size_t i;
148 md5_context md5;
149 sha1_context sha1;
150 unsigned char padding[16];
151 unsigned char sha1sum[20];
152 ((void)label);
153
154 /*
155 * SSLv3:
156 * block =
157 * MD5( secret + SHA1( 'A' + secret + random ) ) +
158 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
159 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
160 * ...
161 */
162 for( i = 0; i < dlen / 16; i++ )
163 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200164 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000165
166 sha1_starts( &sha1 );
167 sha1_update( &sha1, padding, 1 + i );
168 sha1_update( &sha1, secret, slen );
169 sha1_update( &sha1, random, rlen );
170 sha1_finish( &sha1, sha1sum );
171
172 md5_starts( &md5 );
173 md5_update( &md5, secret, slen );
174 md5_update( &md5, sha1sum, 20 );
175 md5_finish( &md5, dstbuf + i * 16 );
176 }
177
178 memset( &md5, 0, sizeof( md5 ) );
179 memset( &sha1, 0, sizeof( sha1 ) );
180
181 memset( padding, 0, sizeof( padding ) );
182 memset( sha1sum, 0, sizeof( sha1sum ) );
183
184 return( 0 );
185}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200186#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000187
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200188#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200189static int tls1_prf( const unsigned char *secret, size_t slen,
190 const char *label,
191 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000192 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
Paul Bakker23986e52011-04-24 08:57:21 +0000194 size_t nb, hs;
195 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200196 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 unsigned char tmp[128];
198 unsigned char h_i[20];
199
200 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000201 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 hs = ( slen + 1 ) / 2;
204 S1 = secret;
205 S2 = secret + slen - hs;
206
207 nb = strlen( label );
208 memcpy( tmp + 20, label, nb );
209 memcpy( tmp + 20 + nb, random, rlen );
210 nb += rlen;
211
212 /*
213 * First compute P_md5(secret,label+random)[0..dlen]
214 */
215 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
216
217 for( i = 0; i < dlen; i += 16 )
218 {
219 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
220 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
221
222 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
223
224 for( j = 0; j < k; j++ )
225 dstbuf[i + j] = h_i[j];
226 }
227
228 /*
229 * XOR out with P_sha1(secret,label+random)[0..dlen]
230 */
231 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
232
233 for( i = 0; i < dlen; i += 20 )
234 {
235 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
236 sha1_hmac( S2, hs, tmp, 20, tmp );
237
238 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
239
240 for( j = 0; j < k; j++ )
241 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
242 }
243
244 memset( tmp, 0, sizeof( tmp ) );
245 memset( h_i, 0, sizeof( h_i ) );
246
247 return( 0 );
248}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200249#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200251#if defined(POLARSSL_SSL_PROTO_TLS1_2)
252#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200253static int tls_prf_sha256( const unsigned char *secret, size_t slen,
254 const char *label,
255 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000256 unsigned char *dstbuf, size_t dlen )
257{
258 size_t nb;
259 size_t i, j, k;
260 unsigned char tmp[128];
261 unsigned char h_i[32];
262
263 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
264 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
265
266 nb = strlen( label );
267 memcpy( tmp + 32, label, nb );
268 memcpy( tmp + 32 + nb, random, rlen );
269 nb += rlen;
270
271 /*
272 * Compute P_<hash>(secret, label + random)[0..dlen]
273 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200274 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000275
276 for( i = 0; i < dlen; i += 32 )
277 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200278 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
279 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
282
283 for( j = 0; j < k; j++ )
284 dstbuf[i + j] = h_i[j];
285 }
286
287 memset( tmp, 0, sizeof( tmp ) );
288 memset( h_i, 0, sizeof( h_i ) );
289
290 return( 0 );
291}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200292#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000293
Paul Bakker9e36f042013-06-30 14:34:05 +0200294#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200295static int tls_prf_sha384( const unsigned char *secret, size_t slen,
296 const char *label,
297 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000298 unsigned char *dstbuf, size_t dlen )
299{
300 size_t nb;
301 size_t i, j, k;
302 unsigned char tmp[128];
303 unsigned char h_i[48];
304
305 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
306 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
307
308 nb = strlen( label );
309 memcpy( tmp + 48, label, nb );
310 memcpy( tmp + 48 + nb, random, rlen );
311 nb += rlen;
312
313 /*
314 * Compute P_<hash>(secret, label + random)[0..dlen]
315 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200316 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000317
318 for( i = 0; i < dlen; i += 48 )
319 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200320 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
321 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
324
325 for( j = 0; j < k; j++ )
326 dstbuf[i + j] = h_i[j];
327 }
328
329 memset( tmp, 0, sizeof( tmp ) );
330 memset( h_i, 0, sizeof( h_i ) );
331
332 return( 0 );
333}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200334#endif /* POLARSSL_SHA512_C */
335#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200337static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338
339#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
340 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200341static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif
Paul Bakker380da532012-04-18 16:10:25 +0000343
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200344#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000345static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000346static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
348
349#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
350static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000351static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1_2)
355#if defined(POLARSSL_SHA256_C)
356static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
357static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000358static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200359#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100360
Paul Bakker9e36f042013-06-30 14:34:05 +0200361#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200362static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100363static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000364static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100365#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200366#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000367
Paul Bakker5121ce52009-01-03 21:22:43 +0000368int ssl_derive_keys( ssl_context *ssl )
369{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200370 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 unsigned char keyblk[256];
373 unsigned char *key1;
374 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100375 unsigned char *mac_enc;
376 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200377 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100378 const cipher_info_t *cipher_info;
379 const md_info_t *md_info;
380
Paul Bakker48916f92012-09-16 19:57:18 +0000381 ssl_session *session = ssl->session_negotiate;
382 ssl_transform *transform = ssl->transform_negotiate;
383 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
386
Paul Bakker68884e32013-01-07 18:20:04 +0100387 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
388 if( cipher_info == NULL )
389 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200390 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100391 transform->ciphersuite_info->cipher ) );
392 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
393 }
394
395 md_info = md_info_from_type( transform->ciphersuite_info->mac );
396 if( md_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->mac ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000405 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200406#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000407 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408 {
Paul Bakker48916f92012-09-16 19:57:18 +0000409 handshake->tls_prf = ssl3_prf;
410 handshake->calc_verify = ssl_calc_verify_ssl;
411 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200413 else
414#endif
415#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
416 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 {
Paul Bakker48916f92012-09-16 19:57:18 +0000418 handshake->tls_prf = tls1_prf;
419 handshake->calc_verify = ssl_calc_verify_tls;
420 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000421 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200422 else
423#endif
424#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200425#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
427 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = tls_prf_sha384;
430 handshake->calc_verify = ssl_calc_verify_tls_sha384;
431 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000433 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434#endif
435#if defined(POLARSSL_SHA256_C)
436 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 {
Paul Bakker48916f92012-09-16 19:57:18 +0000438 handshake->tls_prf = tls_prf_sha256;
439 handshake->calc_verify = ssl_calc_verify_tls_sha256;
440 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000441 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442 else
443#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200444#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200445 {
446 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200448 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000449
450 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 * SSLv3:
452 * master =
453 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
454 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
455 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200456 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200457 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 * master = PRF( premaster, "master secret", randbytes )[0..47]
459 */
Paul Bakker0a597072012-09-25 21:55:46 +0000460 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 {
Paul Bakker48916f92012-09-16 19:57:18 +0000462 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
463 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Paul Bakker48916f92012-09-16 19:57:18 +0000465 handshake->tls_prf( handshake->premaster, handshake->pmslen,
466 "master secret",
467 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Paul Bakker48916f92012-09-16 19:57:18 +0000469 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 }
471 else
472 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
473
474 /*
475 * Swap the client and server random values.
476 */
Paul Bakker48916f92012-09-16 19:57:18 +0000477 memcpy( tmp, handshake->randbytes, 64 );
478 memcpy( handshake->randbytes, tmp + 32, 32 );
479 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 memset( tmp, 0, sizeof( tmp ) );
481
482 /*
483 * SSLv3:
484 * key block =
485 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
486 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
487 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
488 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
489 * ...
490 *
491 * TLSv1:
492 * key block = PRF( master, "key expansion", randbytes )
493 */
Paul Bakker48916f92012-09-16 19:57:18 +0000494 handshake->tls_prf( session->master, 48, "key expansion",
495 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
Paul Bakker48916f92012-09-16 19:57:18 +0000497 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
498 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
499 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
500 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
502
Paul Bakker48916f92012-09-16 19:57:18 +0000503 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505 /*
506 * Determine the appropriate key, IV and MAC length.
507 */
Paul Bakker68884e32013-01-07 18:20:04 +0100508
509 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 {
Paul Bakker68884e32013-01-07 18:20:04 +0100511 transform->keylen = cipher_info->key_length;
512 transform->keylen /= 8;
513 transform->minlen = 1;
514 transform->ivlen = 12;
515 transform->fixed_ivlen = 4;
516 transform->maclen = 0;
517 }
518 else
519 {
520 if( md_info->type != POLARSSL_MD_NONE )
521 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200522 int ret;
523
Paul Bakker61d113b2013-07-04 11:51:43 +0200524 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
525 {
526 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
527 return( ret );
528 }
529
530 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
531 {
532 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
533 return( ret );
534 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Paul Bakker68884e32013-01-07 18:20:04 +0100536 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200537
Paul Bakker1f2bc622013-08-15 13:45:55 +0200538#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200539 /*
540 * If HMAC is to be truncated, we shall keep the leftmost bytes,
541 * (rfc 6066 page 13 or rfc 2104 section 4),
542 * so we only need to adjust the length here.
543 */
544 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
545 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200546#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Paul Bakker68884e32013-01-07 18:20:04 +0100549 transform->keylen = cipher_info->key_length;
550 transform->keylen /= 8;
551 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Paul Bakker68884e32013-01-07 18:20:04 +0100553 transform->minlen = transform->keylen;
554 if( transform->minlen < transform->maclen )
555 {
556 if( cipher_info->mode == POLARSSL_MODE_STREAM )
557 transform->minlen = transform->maclen;
558 else
559 transform->minlen += transform->keylen;
560 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 }
562
563 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000564 transform->keylen, transform->minlen, transform->ivlen,
565 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 /*
568 * Finally setup the cipher contexts, IVs and MAC secrets.
569 */
570 if( ssl->endpoint == SSL_IS_CLIENT )
571 {
Paul Bakker48916f92012-09-16 19:57:18 +0000572 key1 = keyblk + transform->maclen * 2;
573 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Paul Bakker68884e32013-01-07 18:20:04 +0100575 mac_enc = keyblk;
576 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000578 /*
579 * This is not used in TLS v1.1.
580 */
Paul Bakker48916f92012-09-16 19:57:18 +0000581 iv_copy_len = ( transform->fixed_ivlen ) ?
582 transform->fixed_ivlen : transform->ivlen;
583 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
584 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000585 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 }
587 else
588 {
Paul Bakker48916f92012-09-16 19:57:18 +0000589 key1 = keyblk + transform->maclen * 2 + transform->keylen;
590 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Paul Bakker68884e32013-01-07 18:20:04 +0100592 mac_enc = keyblk + transform->maclen;
593 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000595 /*
596 * This is not used in TLS v1.1.
597 */
Paul Bakker48916f92012-09-16 19:57:18 +0000598 iv_copy_len = ( transform->fixed_ivlen ) ?
599 transform->fixed_ivlen : transform->ivlen;
600 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
601 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000602 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 }
604
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200605#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100606 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
607 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100608 if( transform->maclen > sizeof transform->mac_enc )
609 {
610 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
611 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
612 }
613
Paul Bakker68884e32013-01-07 18:20:04 +0100614 memcpy( transform->mac_enc, mac_enc, transform->maclen );
615 memcpy( transform->mac_dec, mac_dec, transform->maclen );
616 }
617 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200618#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200619#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
620 defined(POLARSSL_SSL_PROTO_TLS1_2)
621 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100622 {
623 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
624 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
625 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200626 else
627#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200628 {
629 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200630 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200631 }
Paul Bakker68884e32013-01-07 18:20:04 +0100632
Paul Bakker05ef8352012-05-08 09:17:57 +0000633#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
634 if( ssl_hw_record_init != NULL)
635 {
636 int ret = 0;
637
638 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
639
Paul Bakker07eb38b2012-12-19 14:42:06 +0100640 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
641 transform->iv_enc, transform->iv_dec,
642 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100643 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100644 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000645 {
646 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
647 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
648 }
649 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200650#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000651
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200652 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
653 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200655 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
656 return( ret );
657 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200658
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200659 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
660 cipher_info ) ) != 0 )
661 {
662 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
663 return( ret );
664 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200665
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200666 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
667 cipher_info->key_length,
668 POLARSSL_ENCRYPT ) ) != 0 )
669 {
670 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
671 return( ret );
672 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200673
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200674 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
675 cipher_info->key_length,
676 POLARSSL_DECRYPT ) ) != 0 )
677 {
678 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
679 return( ret );
680 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200681
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200682#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200683 if( cipher_info->mode == POLARSSL_MODE_CBC )
684 {
685 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
686 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200687 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200688 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
689 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200690 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200691
692 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
693 POLARSSL_PADDING_NONE ) ) != 0 )
694 {
695 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
696 return( ret );
697 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200699#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701 memset( keyblk, 0, sizeof( keyblk ) );
702
Paul Bakker2770fbd2012-07-03 13:30:23 +0000703#if defined(POLARSSL_ZLIB_SUPPORT)
704 // Initialize compression
705 //
Paul Bakker48916f92012-09-16 19:57:18 +0000706 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000707 {
Paul Bakker16770332013-10-11 09:59:44 +0200708 if( ssl->compress_buf == NULL )
709 {
710 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
711 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
712 if( ssl->compress_buf == NULL )
713 {
714 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
715 SSL_BUFFER_LEN ) );
716 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
717 }
718 }
719
Paul Bakker2770fbd2012-07-03 13:30:23 +0000720 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
721
Paul Bakker48916f92012-09-16 19:57:18 +0000722 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
723 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000724
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200725 if( deflateInit( &transform->ctx_deflate,
726 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000727 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000728 {
729 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
730 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
731 }
732 }
733#endif /* POLARSSL_ZLIB_SUPPORT */
734
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
736
737 return( 0 );
738}
739
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200740#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000741void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742{
743 md5_context md5;
744 sha1_context sha1;
745 unsigned char pad_1[48];
746 unsigned char pad_2[48];
747
Paul Bakker380da532012-04-18 16:10:25 +0000748 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker48916f92012-09-16 19:57:18 +0000750 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
751 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Paul Bakker380da532012-04-18 16:10:25 +0000753 memset( pad_1, 0x36, 48 );
754 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakker48916f92012-09-16 19:57:18 +0000756 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000757 md5_update( &md5, pad_1, 48 );
758 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Paul Bakker380da532012-04-18 16:10:25 +0000760 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000761 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000762 md5_update( &md5, pad_2, 48 );
763 md5_update( &md5, hash, 16 );
764 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakker48916f92012-09-16 19:57:18 +0000766 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000767 sha1_update( &sha1, pad_1, 40 );
768 sha1_finish( &sha1, hash + 16 );
769
770 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000771 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000772 sha1_update( &sha1, pad_2, 40 );
773 sha1_update( &sha1, hash + 16, 20 );
774 sha1_finish( &sha1, hash + 16 );
775
776 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
777 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
778
779 return;
780}
Paul Bakker9af723c2014-05-01 13:03:14 +0200781#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000782
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000784void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
785{
786 md5_context md5;
787 sha1_context sha1;
788
789 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
790
Paul Bakker48916f92012-09-16 19:57:18 +0000791 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
792 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000793
Paul Bakker48916f92012-09-16 19:57:18 +0000794 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000795 sha1_finish( &sha1, hash + 16 );
796
797 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
798 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
799
800 return;
801}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200802#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000803
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#if defined(POLARSSL_SSL_PROTO_TLS1_2)
805#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000806void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
807{
Paul Bakker9e36f042013-06-30 14:34:05 +0200808 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000809
810 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
811
Paul Bakker9e36f042013-06-30 14:34:05 +0200812 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
813 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000814
815 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
816 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
817
818 return;
819}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000821
Paul Bakker9e36f042013-06-30 14:34:05 +0200822#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000823void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
824{
Paul Bakker9e36f042013-06-30 14:34:05 +0200825 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000826
827 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
828
Paul Bakker9e36f042013-06-30 14:34:05 +0200829 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
830 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Paul Bakkerca4ab492012-04-18 14:23:57 +0000832 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
834
835 return;
836}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200837#endif /* POLARSSL_SHA512_C */
838#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200840#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200841int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
842{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200843 unsigned char *p = ssl->handshake->premaster;
844 unsigned char *end = p + sizeof( ssl->handshake->premaster );
845
846 /*
847 * PMS = struct {
848 * opaque other_secret<0..2^16-1>;
849 * opaque psk<0..2^16-1>;
850 * };
851 * with "other_secret" depending on the particular key exchange
852 */
853#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
854 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
855 {
856 if( end - p < 2 + (int) ssl->psk_len )
857 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
858
859 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
860 *(p++) = (unsigned char)( ssl->psk_len );
861 p += ssl->psk_len;
862 }
863 else
864#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200865#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
866 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
867 {
868 /*
869 * other_secret already set by the ClientKeyExchange message,
870 * and is 48 bytes long
871 */
872 *p++ = 0;
873 *p++ = 48;
874 p += 48;
875 }
876 else
877#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200878#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
879 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
880 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200881 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200882 size_t len = ssl->handshake->dhm_ctx.len;
883
884 if( end - p < 2 + (int) len )
885 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
886
887 *(p++) = (unsigned char)( len >> 8 );
888 *(p++) = (unsigned char)( len );
889 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
890 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
891 {
892 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
893 return( ret );
894 }
895 p += len;
896
897 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
898 }
899 else
900#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
901#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
902 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
903 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200904 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200905 size_t zlen;
906
907 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
908 p + 2, end - (p + 2),
909 ssl->f_rng, ssl->p_rng ) ) != 0 )
910 {
911 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
912 return( ret );
913 }
914
915 *(p++) = (unsigned char)( zlen >> 8 );
916 *(p++) = (unsigned char)( zlen );
917 p += zlen;
918
919 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
920 }
921 else
922#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
923 {
924 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
925 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
926 }
927
928 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100929 if( end - p < 2 + (int) ssl->psk_len )
930 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
931
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
933 *(p++) = (unsigned char)( ssl->psk_len );
934 memcpy( p, ssl->psk, ssl->psk_len );
935 p += ssl->psk_len;
936
937 ssl->handshake->pmslen = p - ssl->handshake->premaster;
938
939 return( 0 );
940}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200941#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200942
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200943#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000944/*
945 * SSLv3.0 MAC functions
946 */
Paul Bakker68884e32013-01-07 18:20:04 +0100947static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
948 unsigned char *buf, size_t len,
949 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000950{
951 unsigned char header[11];
952 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100953 int padlen = 0;
954 int md_size = md_get_size( md_ctx->md_info );
955 int md_type = md_get_type( md_ctx->md_info );
956
957 if( md_type == POLARSSL_MD_MD5 )
958 padlen = 48;
959 else if( md_type == POLARSSL_MD_SHA1 )
960 padlen = 40;
961 else if( md_type == POLARSSL_MD_SHA256 )
962 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100963 else if( md_type == POLARSSL_MD_SHA384 )
964 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
966 memcpy( header, ctr, 8 );
967 header[ 8] = (unsigned char) type;
968 header[ 9] = (unsigned char)( len >> 8 );
969 header[10] = (unsigned char)( len );
970
Paul Bakker68884e32013-01-07 18:20:04 +0100971 memset( padding, 0x36, padlen );
972 md_starts( md_ctx );
973 md_update( md_ctx, secret, md_size );
974 md_update( md_ctx, padding, padlen );
975 md_update( md_ctx, header, 11 );
976 md_update( md_ctx, buf, len );
977 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Paul Bakker68884e32013-01-07 18:20:04 +0100979 memset( padding, 0x5C, padlen );
980 md_starts( md_ctx );
981 md_update( md_ctx, secret, md_size );
982 md_update( md_ctx, padding, padlen );
983 md_update( md_ctx, buf + len, md_size );
984 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000985}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200986#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000987
Paul Bakker5121ce52009-01-03 21:22:43 +0000988/*
989 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200990 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000991static int ssl_encrypt_buf( ssl_context *ssl )
992{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200993 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
996
997 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200998 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001000#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1001 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1002 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1003 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
1004 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001005 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001006#if defined(POLARSSL_SSL_PROTO_SSL3)
1007 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1008 {
1009 ssl_mac( &ssl->transform_out->md_ctx_enc,
1010 ssl->transform_out->mac_enc,
1011 ssl->out_msg, ssl->out_msglen,
1012 ssl->out_ctr, ssl->out_msgtype );
1013 }
1014 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001015#endif
1016#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001017 defined(POLARSSL_SSL_PROTO_TLS1_2)
1018 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1019 {
1020 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1021 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1022 ssl->out_msg, ssl->out_msglen );
1023 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1024 ssl->out_msg + ssl->out_msglen );
1025 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1026 }
1027 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001028#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001029 {
1030 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1031 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1032 }
1033
1034 SSL_DEBUG_BUF( 4, "computed mac",
1035 ssl->out_msg + ssl->out_msglen,
1036 ssl->transform_out->maclen );
1037
1038 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001039 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001040#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001042 /*
1043 * Encrypt
1044 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001045#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001046 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1047 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001049 int ret;
1050 size_t olen = 0;
1051
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1053 "including %d bytes of padding",
1054 ssl->out_msglen, 0 ) );
1055
1056 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1057 ssl->out_msg, ssl->out_msglen );
1058
Paul Bakker45125bc2013-09-04 16:47:11 +02001059 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001060 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001061 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1062 return( ret );
1063 }
1064
1065 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1066 ssl->transform_out->iv_enc,
1067 ssl->transform_out->ivlen ) ) != 0 )
1068 {
1069 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001070 return( ret );
1071 }
1072
1073 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1074 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1075 &olen ) ) != 0 )
1076 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001077 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001078 return( ret );
1079 }
1080
1081 if( ssl->out_msglen != olen )
1082 {
1083 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1084 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001085 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001086 }
1087
1088 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001089 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001090 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001091 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001092 return( ret );
1093 }
1094
1095 if( 0 != olen )
1096 {
1097 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1098 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001099 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 }
Paul Bakker68884e32013-01-07 18:20:04 +01001102 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001103#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001104#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001105 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1106 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001107 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001108 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001109 unsigned char *enc_msg;
1110 unsigned char add_data[13];
1111 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1112
Paul Bakkerca4ab492012-04-18 14:23:57 +00001113 memcpy( add_data, ssl->out_ctr, 8 );
1114 add_data[8] = ssl->out_msgtype;
1115 add_data[9] = ssl->major_ver;
1116 add_data[10] = ssl->minor_ver;
1117 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1118 add_data[12] = ssl->out_msglen & 0xFF;
1119
1120 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1121 add_data, 13 );
1122
Paul Bakker68884e32013-01-07 18:20:04 +01001123 /*
1124 * Generate IV
1125 */
1126 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001127 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1128 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001129 if( ret != 0 )
1130 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131
Paul Bakker68884e32013-01-07 18:20:04 +01001132 memcpy( ssl->out_iv,
1133 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1134 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001136 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001137 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001138
Paul Bakker68884e32013-01-07 18:20:04 +01001139 /*
1140 * Fix pointer positions and message length with added IV
1141 */
1142 enc_msg = ssl->out_msg;
1143 enc_msglen = ssl->out_msglen;
1144 ssl->out_msglen += ssl->transform_out->ivlen -
1145 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001146
Paul Bakker68884e32013-01-07 18:20:04 +01001147 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1148 "including %d bytes of padding",
1149 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001150
Paul Bakker68884e32013-01-07 18:20:04 +01001151 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1152 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001153
Paul Bakker68884e32013-01-07 18:20:04 +01001154 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001155 * Encrypt
1156 */
1157 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1158 ssl->transform_out->iv_enc,
1159 ssl->transform_out->ivlen ) ) != 0 ||
1160 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1161 {
1162 return( ret );
1163 }
1164
1165 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1166 add_data, 13 ) ) != 0 )
1167 {
1168 return( ret );
1169 }
1170
1171 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1172 enc_msg, enc_msglen,
1173 enc_msg, &olen ) ) != 0 )
1174 {
1175 return( ret );
1176 }
1177 totlen = olen;
1178
1179 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1180 enc_msg + olen, &olen ) ) != 0 )
1181 {
1182 return( ret );
1183 }
1184 totlen += olen;
1185
1186 if( totlen != enc_msglen )
1187 {
1188 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1189 return( -1 );
1190 }
1191
1192 /*
1193 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001194 */
1195 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001196
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001197 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1198 enc_msg + enc_msglen, 16 ) ) != 0 )
1199 {
1200 return( ret );
1201 }
Paul Bakker68884e32013-01-07 18:20:04 +01001202
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001203 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001204 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 else
Paul Bakker68884e32013-01-07 18:20:04 +01001206#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001207#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1208 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001209 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1210 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001212 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001214 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001215
Paul Bakker48916f92012-09-16 19:57:18 +00001216 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1217 ssl->transform_out->ivlen;
1218 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 padlen = 0;
1220
1221 for( i = 0; i <= padlen; i++ )
1222 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1223
1224 ssl->out_msglen += padlen + 1;
1225
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226 enc_msglen = ssl->out_msglen;
1227 enc_msg = ssl->out_msg;
1228
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001229#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001231 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1232 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001234 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235 {
1236 /*
1237 * Generate IV
1238 */
Paul Bakker48916f92012-09-16 19:57:18 +00001239 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1240 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001241 if( ret != 0 )
1242 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243
Paul Bakker92be97b2013-01-02 17:30:03 +01001244 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001245 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001246
1247 /*
1248 * Fix pointer positions and message length with added IV
1249 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001250 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001251 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001252 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001253 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001254#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001255
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001257 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001258 ssl->out_msglen, ssl->transform_out->ivlen,
1259 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
1261 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001262 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Paul Bakker45125bc2013-09-04 16:47:11 +02001264 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001266 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1267 return( ret );
1268 }
1269
1270 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1271 ssl->transform_out->iv_enc,
1272 ssl->transform_out->ivlen ) ) != 0 )
1273 {
1274 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001275 return( ret );
1276 }
Paul Bakker68884e32013-01-07 18:20:04 +01001277
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1279 enc_msg, enc_msglen, enc_msg,
1280 &olen ) ) != 0 )
1281 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001282 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001283 return( ret );
1284 }
Paul Bakker68884e32013-01-07 18:20:04 +01001285
Paul Bakkercca5b812013-08-31 17:40:26 +02001286 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001287
Paul Bakkercca5b812013-08-31 17:40:26 +02001288 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001289 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001290 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001291 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001292 return( ret );
1293 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001294
Paul Bakkercca5b812013-08-31 17:40:26 +02001295 if( enc_msglen != olen )
1296 {
1297 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1298 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001299 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001300 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001301
1302#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001303 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1304 {
1305 /*
1306 * Save IV in SSL3 and TLS1
1307 */
1308 memcpy( ssl->transform_out->iv_enc,
1309 ssl->transform_out->cipher_ctx_enc.iv,
1310 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001312#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001314 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001315#endif /* POLARSSL_CIPHER_MODE_CBC &&
1316 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001317 {
1318 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1319 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Paul Bakkerca4ab492012-04-18 14:23:57 +00001322 for( i = 8; i > 0; i-- )
1323 if( ++ssl->out_ctr[i - 1] != 0 )
1324 break;
1325
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001326 /* The loops goes to its end iff the counter is wrapping */
1327 if( i == 0 )
1328 {
1329 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1330 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1331 }
1332
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1334
1335 return( 0 );
1336}
1337
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001338#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001339
Paul Bakker5121ce52009-01-03 21:22:43 +00001340static int ssl_decrypt_buf( ssl_context *ssl )
1341{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001342 size_t i;
1343#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1344 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1345 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1346 size_t padlen = 0, correct = 1;
1347#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
1349 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1350
Paul Bakker48916f92012-09-16 19:57:18 +00001351 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 {
1353 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001354 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001355 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 }
1357
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001358#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001359 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1360 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001361 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001362 int ret;
1363 size_t olen = 0;
1364
Paul Bakker68884e32013-01-07 18:20:04 +01001365 padlen = 0;
1366
Paul Bakker45125bc2013-09-04 16:47:11 +02001367 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001368 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001369 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1370 return( ret );
1371 }
1372
1373 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1374 ssl->transform_in->iv_dec,
1375 ssl->transform_in->ivlen ) ) != 0 )
1376 {
1377 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001378 return( ret );
1379 }
1380
1381 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1382 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1383 &olen ) ) != 0 )
1384 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001385 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001386 return( ret );
1387 }
1388
1389 if( ssl->in_msglen != olen )
1390 {
1391 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001392 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001393 }
1394
1395 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001396 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001397 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001398 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001399 return( ret );
1400 }
1401
1402 if( 0 != olen )
1403 {
1404 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001405 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 }
Paul Bakker68884e32013-01-07 18:20:04 +01001408 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001409#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001410#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001411 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1412 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001413 {
1414 unsigned char *dec_msg;
1415 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001416 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001417 unsigned char add_data[13];
1418 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1419
Paul Bakker68884e32013-01-07 18:20:04 +01001420 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1421 ssl->transform_in->fixed_ivlen );
1422 dec_msglen -= 16;
1423 dec_msg = ssl->in_msg;
1424 dec_msg_result = ssl->in_msg;
1425 ssl->in_msglen = dec_msglen;
1426
1427 memcpy( add_data, ssl->in_ctr, 8 );
1428 add_data[8] = ssl->in_msgtype;
1429 add_data[9] = ssl->major_ver;
1430 add_data[10] = ssl->minor_ver;
1431 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1432 add_data[12] = ssl->in_msglen & 0xFF;
1433
1434 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1435 add_data, 13 );
1436
1437 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1438 ssl->in_iv,
1439 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1440
1441 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1442 ssl->transform_in->ivlen );
1443 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1444
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001445 /*
1446 * Decrypt
1447 */
1448 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1449 ssl->transform_in->iv_dec,
1450 ssl->transform_in->ivlen ) ) != 0 ||
1451 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001452 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001453 return( ret );
1454 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001455
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001456 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1457 add_data, 13 ) ) != 0 )
1458 {
1459 return( ret );
1460 }
1461
1462 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1463 dec_msg, dec_msglen,
1464 dec_msg_result, &olen ) ) != 0 )
1465 {
1466 return( ret );
1467 }
1468 totlen = olen;
1469
1470 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1471 dec_msg_result + olen, &olen ) ) != 0 )
1472 {
1473 return( ret );
1474 }
1475 totlen += olen;
1476
1477 if( totlen != dec_msglen )
1478 {
1479 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1480 return( -1 );
1481 }
1482
1483 /*
1484 * Authenticate
1485 */
1486 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1487 dec_msg + dec_msglen, 16 ) ) != 0 )
1488 {
1489 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001490 return( POLARSSL_ERR_SSL_INVALID_MAC );
1491 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001492
Paul Bakkerca4ab492012-04-18 14:23:57 +00001493 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 else
Paul Bakker68884e32013-01-07 18:20:04 +01001495#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001496#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1497 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001498 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1499 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 {
Paul Bakker45829992013-01-03 14:52:21 +01001501 /*
1502 * Decrypt and check the padding
1503 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001504 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001505 unsigned char *dec_msg;
1506 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001507 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001508 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001509 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001510
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 /*
Paul Bakker45829992013-01-03 14:52:21 +01001512 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 */
Paul Bakker48916f92012-09-16 19:57:18 +00001514 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 {
1516 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001517 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001518 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001519 }
1520
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001521#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001522 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1523 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001524#endif
Paul Bakker45829992013-01-03 14:52:21 +01001525
1526 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1527 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1528 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001529 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1530 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1531 ssl->transform_in->ivlen,
1532 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001533 return( POLARSSL_ERR_SSL_INVALID_MAC );
1534 }
1535
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001536 dec_msglen = ssl->in_msglen;
1537 dec_msg = ssl->in_msg;
1538 dec_msg_result = ssl->in_msg;
1539
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001540#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001541 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001542 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001543 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001544 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001545 {
Paul Bakker48916f92012-09-16 19:57:18 +00001546 dec_msglen -= ssl->transform_in->ivlen;
1547 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001548
Paul Bakker48916f92012-09-16 19:57:18 +00001549 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001550 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001551 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001552#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001553
Paul Bakker45125bc2013-09-04 16:47:11 +02001554 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001556 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1557 return( ret );
1558 }
1559
1560 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1561 ssl->transform_in->iv_dec,
1562 ssl->transform_in->ivlen ) ) != 0 )
1563 {
1564 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001565 return( ret );
1566 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
Paul Bakkercca5b812013-08-31 17:40:26 +02001568 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1569 dec_msg, dec_msglen, dec_msg_result,
1570 &olen ) ) != 0 )
1571 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001572 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001573 return( ret );
1574 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001575
Paul Bakkercca5b812013-08-31 17:40:26 +02001576 dec_msglen -= olen;
1577 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001578 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001579 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001580 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001581 return( ret );
1582 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001583
Paul Bakkercca5b812013-08-31 17:40:26 +02001584 if( dec_msglen != olen )
1585 {
1586 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001587 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001588 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001589
1590#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001591 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1592 {
1593 /*
1594 * Save IV in SSL3 and TLS1
1595 */
1596 memcpy( ssl->transform_in->iv_dec,
1597 ssl->transform_in->cipher_ctx_dec.iv,
1598 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001600#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
1602 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001603
1604 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1605 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001606#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001607 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1608 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001609#endif
Paul Bakker45829992013-01-03 14:52:21 +01001610 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001611 correct = 0;
1612 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001614#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1616 {
Paul Bakker48916f92012-09-16 19:57:18 +00001617 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001618 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001619#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1621 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001622 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001623#endif
Paul Bakker45829992013-01-03 14:52:21 +01001624 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 }
1626 }
1627 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001628#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001629#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1630 defined(POLARSSL_SSL_PROTO_TLS1_2)
1631 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 {
1633 /*
Paul Bakker45829992013-01-03 14:52:21 +01001634 * TLSv1+: always check the padding up to the first failure
1635 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001636 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001637 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001638 size_t padding_idx = ssl->in_msglen - padlen - 1;
1639
Paul Bakker956c9e02013-12-19 14:42:28 +01001640 /*
1641 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001642 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001643 *
Paul Bakker61885c72014-04-25 12:59:03 +02001644 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1645 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001646 *
1647 * In both cases we reset padding_idx to a safe value (0) to
1648 * prevent out-of-buffer reads.
1649 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001650 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001651 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1652 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001653
1654 padding_idx *= correct;
1655
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001656 for( i = 1; i <= 256; i++ )
1657 {
1658 real_count &= ( i <= padlen );
1659 pad_count += real_count *
1660 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1661 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001662
1663 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001664
Paul Bakkerd66f0702013-01-31 16:57:45 +01001665#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001666 if( padlen > 0 && correct == 0)
1667 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001668#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001669 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001671 else
1672#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1673 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001674 {
1675 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001676 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001679 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001680#endif /* POLARSSL_CIPHER_MODE_CBC &&
1681 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001682 {
1683 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1684 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001686
1687 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1688 ssl->in_msg, ssl->in_msglen );
1689
1690 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001691 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001692 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001693#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1694 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1695 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1696 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1697 POLARSSL_MODE_GCM )
1698 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001699 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1700
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001701 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001703 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1704 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001705
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001706 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001708#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001709 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1710 {
1711 ssl_mac( &ssl->transform_in->md_ctx_dec,
1712 ssl->transform_in->mac_dec,
1713 ssl->in_msg, ssl->in_msglen,
1714 ssl->in_ctr, ssl->in_msgtype );
1715 }
1716 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001717#endif /* POLARSSL_SSL_PROTO_SSL3 */
1718#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001719 defined(POLARSSL_SSL_PROTO_TLS1_2)
1720 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1721 {
1722 /*
1723 * Process MAC and always update for padlen afterwards to make
1724 * total time independent of padlen
1725 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001726 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001727 *
1728 * Known timing attacks:
1729 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1730 *
1731 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1732 * correctly. (We round down instead of up, so -56 is the correct
1733 * value for our calculations instead of -55)
1734 */
1735 size_t j, extra_run = 0;
1736 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1737 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001738
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001739 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001740
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001741 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1742 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1743 ssl->in_msglen );
1744 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1745 ssl->in_msg + ssl->in_msglen );
1746 for( j = 0; j < extra_run; j++ )
1747 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001748
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001749 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1750 }
1751 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001752#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001753 POLARSSL_SSL_PROTO_TLS1_2 */
1754 {
1755 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1756 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1757 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001759 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1760 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1761 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001763 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001764 ssl->transform_in->maclen ) != 0 )
1765 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001766#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001767 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001768#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001769 correct = 0;
1770 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001772 /*
1773 * Finally check the correct flag
1774 */
1775 if( correct == 0 )
1776 return( POLARSSL_ERR_SSL_INVALID_MAC );
1777 }
1778#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
1780 if( ssl->in_msglen == 0 )
1781 {
1782 ssl->nb_zero++;
1783
1784 /*
1785 * Three or more empty messages may be a DoS attack
1786 * (excessive CPU consumption).
1787 */
1788 if( ssl->nb_zero > 3 )
1789 {
1790 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1791 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001792 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 }
1794 }
1795 else
1796 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001797
Paul Bakker23986e52011-04-24 08:57:21 +00001798 for( i = 8; i > 0; i-- )
1799 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 break;
1801
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001802 /* The loops goes to its end iff the counter is wrapping */
1803 if( i == 0 )
1804 {
1805 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1806 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1807 }
1808
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1810
1811 return( 0 );
1812}
1813
Paul Bakker2770fbd2012-07-03 13:30:23 +00001814#if defined(POLARSSL_ZLIB_SUPPORT)
1815/*
1816 * Compression/decompression functions
1817 */
1818static int ssl_compress_buf( ssl_context *ssl )
1819{
1820 int ret;
1821 unsigned char *msg_post = ssl->out_msg;
1822 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001823 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001824
1825 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1826
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001827 if( len_pre == 0 )
1828 return( 0 );
1829
Paul Bakker2770fbd2012-07-03 13:30:23 +00001830 memcpy( msg_pre, ssl->out_msg, len_pre );
1831
1832 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1833 ssl->out_msglen ) );
1834
1835 SSL_DEBUG_BUF( 4, "before compression: output payload",
1836 ssl->out_msg, ssl->out_msglen );
1837
Paul Bakker48916f92012-09-16 19:57:18 +00001838 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1839 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1840 ssl->transform_out->ctx_deflate.next_out = msg_post;
1841 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001842
Paul Bakker48916f92012-09-16 19:57:18 +00001843 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001844 if( ret != Z_OK )
1845 {
1846 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1847 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1848 }
1849
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001850 ssl->out_msglen = SSL_BUFFER_LEN -
1851 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001852
Paul Bakker2770fbd2012-07-03 13:30:23 +00001853 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1854 ssl->out_msglen ) );
1855
1856 SSL_DEBUG_BUF( 4, "after compression: output payload",
1857 ssl->out_msg, ssl->out_msglen );
1858
1859 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1860
1861 return( 0 );
1862}
1863
1864static int ssl_decompress_buf( ssl_context *ssl )
1865{
1866 int ret;
1867 unsigned char *msg_post = ssl->in_msg;
1868 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001869 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001870
1871 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1872
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001873 if( len_pre == 0 )
1874 return( 0 );
1875
Paul Bakker2770fbd2012-07-03 13:30:23 +00001876 memcpy( msg_pre, ssl->in_msg, len_pre );
1877
1878 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1879 ssl->in_msglen ) );
1880
1881 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1882 ssl->in_msg, ssl->in_msglen );
1883
Paul Bakker48916f92012-09-16 19:57:18 +00001884 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1885 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1886 ssl->transform_in->ctx_inflate.next_out = msg_post;
1887 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001888
Paul Bakker48916f92012-09-16 19:57:18 +00001889 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001890 if( ret != Z_OK )
1891 {
1892 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1893 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1894 }
1895
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001896 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1897 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001898
Paul Bakker2770fbd2012-07-03 13:30:23 +00001899 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1900 ssl->in_msglen ) );
1901
1902 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1903 ssl->in_msg, ssl->in_msglen );
1904
1905 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1906
1907 return( 0 );
1908}
1909#endif /* POLARSSL_ZLIB_SUPPORT */
1910
Paul Bakker5121ce52009-01-03 21:22:43 +00001911/*
1912 * Fill the input message buffer
1913 */
Paul Bakker23986e52011-04-24 08:57:21 +00001914int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001915{
Paul Bakker23986e52011-04-24 08:57:21 +00001916 int ret;
1917 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
1919 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1920
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001921 if( nb_want > SSL_BUFFER_LEN - 8 )
1922 {
1923 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1924 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1925 }
1926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 while( ssl->in_left < nb_want )
1928 {
1929 len = nb_want - ssl->in_left;
1930 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1931
1932 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1933 ssl->in_left, nb_want ) );
1934 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1935
Paul Bakker831a7552011-05-18 13:32:51 +00001936 if( ret == 0 )
1937 return( POLARSSL_ERR_SSL_CONN_EOF );
1938
Paul Bakker5121ce52009-01-03 21:22:43 +00001939 if( ret < 0 )
1940 return( ret );
1941
1942 ssl->in_left += ret;
1943 }
1944
1945 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1946
1947 return( 0 );
1948}
1949
1950/*
1951 * Flush any data not yet written
1952 */
1953int ssl_flush_output( ssl_context *ssl )
1954{
1955 int ret;
1956 unsigned char *buf;
1957
1958 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1959
1960 while( ssl->out_left > 0 )
1961 {
1962 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1963 5 + ssl->out_msglen, ssl->out_left ) );
1964
Paul Bakker5bd42292012-12-19 14:40:42 +01001965 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001967
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1969
1970 if( ret <= 0 )
1971 return( ret );
1972
1973 ssl->out_left -= ret;
1974 }
1975
1976 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1977
1978 return( 0 );
1979}
1980
1981/*
1982 * Record layer functions
1983 */
1984int ssl_write_record( ssl_context *ssl )
1985{
Paul Bakker05ef8352012-05-08 09:17:57 +00001986 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001987 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001988
1989 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1990
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1992 {
1993 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1994 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1995 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1996
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001997 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1998 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 }
2000
Paul Bakker2770fbd2012-07-03 13:30:23 +00002001#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002002 if( ssl->transform_out != NULL &&
2003 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002004 {
2005 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2006 {
2007 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2008 return( ret );
2009 }
2010
2011 len = ssl->out_msglen;
2012 }
2013#endif /*POLARSSL_ZLIB_SUPPORT */
2014
Paul Bakker05ef8352012-05-08 09:17:57 +00002015#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2016 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002018 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
Paul Bakker05ef8352012-05-08 09:17:57 +00002020 ret = ssl_hw_record_write( ssl );
2021 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2022 {
2023 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
2024 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2025 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002026
2027 if( ret == 0 )
2028 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002029 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002030#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002031 if( !done )
2032 {
2033 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2034 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2035 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2037 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002038
Paul Bakker48916f92012-09-16 19:57:18 +00002039 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002040 {
2041 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2042 {
2043 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2044 return( ret );
2045 }
2046
2047 len = ssl->out_msglen;
2048 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2049 ssl->out_hdr[4] = (unsigned char)( len );
2050 }
2051
2052 ssl->out_left = 5 + ssl->out_msglen;
2053
2054 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2055 "version = [%d:%d], msglen = %d",
2056 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2057 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2058
2059 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002060 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 }
2062
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2064 {
2065 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2066 return( ret );
2067 }
2068
2069 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2070
2071 return( 0 );
2072}
2073
2074int ssl_read_record( ssl_context *ssl )
2075{
Paul Bakker05ef8352012-05-08 09:17:57 +00002076 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
2078 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2079
Paul Bakker68884e32013-01-07 18:20:04 +01002080 SSL_DEBUG_BUF( 4, "input record from network",
2081 ssl->in_hdr, 5 + ssl->in_msglen );
2082
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 if( ssl->in_hslen != 0 &&
2084 ssl->in_hslen < ssl->in_msglen )
2085 {
2086 /*
2087 * Get next Handshake message in the current record
2088 */
2089 ssl->in_msglen -= ssl->in_hslen;
2090
Paul Bakker8934a982011-08-05 11:11:53 +00002091 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002092 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
2094 ssl->in_hslen = 4;
2095 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2096
2097 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2098 " %d, type = %d, hslen = %d",
2099 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2100
2101 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2102 {
2103 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002104 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
2106
2107 if( ssl->in_msglen < ssl->in_hslen )
2108 {
2109 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002110 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 }
2112
Paul Bakker4224bc02014-04-08 14:36:50 +02002113 if( ssl->state != SSL_HANDSHAKE_OVER )
2114 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
2116 return( 0 );
2117 }
2118
2119 ssl->in_hslen = 0;
2120
2121 /*
2122 * Read the record header and validate it
2123 */
2124 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2125 {
2126 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2127 return( ret );
2128 }
2129
2130 ssl->in_msgtype = ssl->in_hdr[0];
2131 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2132
2133 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2134 "version = [%d:%d], msglen = %d",
2135 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2136 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2137
2138 if( ssl->in_hdr[1] != ssl->major_ver )
2139 {
2140 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002141 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 }
2143
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002144 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 {
2146 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002147 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 }
2149
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002150 /* Sanity check (outer boundaries) */
2151 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2152 {
2153 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2154 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2155 }
2156
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002158 * Make sure the message length is acceptable for the current transform
2159 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 */
Paul Bakker48916f92012-09-16 19:57:18 +00002161 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002163 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 {
2165 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002166 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 }
2168 }
2169 else
2170 {
Paul Bakker48916f92012-09-16 19:57:18 +00002171 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 {
2173 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002174 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 }
2176
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002177#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002179 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 {
2181 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002182 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002184#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002186#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2187 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 /*
2189 * TLS encrypted messages can have up to 256 bytes of padding
2190 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002191 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002192 ssl->in_msglen > ssl->transform_in->minlen +
2193 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 {
2195 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002196 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002198#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 }
2200
2201 /*
2202 * Read and optionally decrypt the message contents
2203 */
2204 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2205 {
2206 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2207 return( ret );
2208 }
2209
2210 SSL_DEBUG_BUF( 4, "input record from network",
2211 ssl->in_hdr, 5 + ssl->in_msglen );
2212
Paul Bakker05ef8352012-05-08 09:17:57 +00002213#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2214 if( ssl_hw_record_read != NULL)
2215 {
2216 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2217
2218 ret = ssl_hw_record_read( ssl );
2219 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2220 {
2221 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2222 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2223 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002224
2225 if( ret == 0 )
2226 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002227 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002228#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002229 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 {
2231 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2232 {
Paul Bakker40865c82013-01-31 17:13:13 +01002233#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2234 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2235 {
2236 ssl_send_alert_message( ssl,
2237 SSL_ALERT_LEVEL_FATAL,
2238 SSL_ALERT_MSG_BAD_RECORD_MAC );
2239 }
2240#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2242 return( ret );
2243 }
2244
2245 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2246 ssl->in_msg, ssl->in_msglen );
2247
2248 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2249 {
2250 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002251 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 }
2253 }
2254
Paul Bakker2770fbd2012-07-03 13:30:23 +00002255#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002256 if( ssl->transform_in != NULL &&
2257 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002258 {
2259 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2260 {
2261 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2262 return( ret );
2263 }
2264
2265 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2266 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2267 }
2268#endif /* POLARSSL_ZLIB_SUPPORT */
2269
Paul Bakker0a925182012-04-16 06:46:41 +00002270 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2271 ssl->in_msgtype != SSL_MSG_ALERT &&
2272 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2273 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2274 {
2275 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2276
Paul Bakker48916f92012-09-16 19:57:18 +00002277 if( ( ret = ssl_send_alert_message( ssl,
2278 SSL_ALERT_LEVEL_FATAL,
2279 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002280 {
2281 return( ret );
2282 }
2283
2284 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2285 }
2286
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2288 {
2289 ssl->in_hslen = 4;
2290 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2291
2292 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2293 " %d, type = %d, hslen = %d",
2294 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2295
2296 /*
2297 * Additional checks to validate the handshake header
2298 */
2299 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2300 {
2301 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002302 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002303 }
2304
2305 if( ssl->in_msglen < ssl->in_hslen )
2306 {
2307 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002308 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 }
2310
Paul Bakker48916f92012-09-16 19:57:18 +00002311 if( ssl->state != SSL_HANDSHAKE_OVER )
2312 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 }
2314
2315 if( ssl->in_msgtype == SSL_MSG_ALERT )
2316 {
2317 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2318 ssl->in_msg[0], ssl->in_msg[1] ) );
2319
2320 /*
2321 * Ignore non-fatal alerts, except close_notify
2322 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002323 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002325 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2326 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002327 /**
2328 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2329 * error identifier.
2330 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002331 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 }
2333
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002334 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2335 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002336 {
2337 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002338 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 }
2340 }
2341
2342 ssl->in_left = 0;
2343
2344 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2345
2346 return( 0 );
2347}
2348
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002349int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2350{
2351 int ret;
2352
2353 if( ( ret = ssl_send_alert_message( ssl,
2354 SSL_ALERT_LEVEL_FATAL,
2355 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2356 {
2357 return( ret );
2358 }
2359
2360 return( 0 );
2361}
2362
Paul Bakker0a925182012-04-16 06:46:41 +00002363int ssl_send_alert_message( ssl_context *ssl,
2364 unsigned char level,
2365 unsigned char message )
2366{
2367 int ret;
2368
2369 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2370
2371 ssl->out_msgtype = SSL_MSG_ALERT;
2372 ssl->out_msglen = 2;
2373 ssl->out_msg[0] = level;
2374 ssl->out_msg[1] = message;
2375
2376 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2377 {
2378 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2379 return( ret );
2380 }
2381
2382 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2383
2384 return( 0 );
2385}
2386
Paul Bakker5121ce52009-01-03 21:22:43 +00002387/*
2388 * Handshake functions
2389 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002390#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2391 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2392 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2393 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2394 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2395 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2396 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002397int ssl_write_certificate( ssl_context *ssl )
2398{
Paul Bakkered27a042013-04-18 22:46:23 +02002399 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002400 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
2402 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2403
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002404 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002405 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2406 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002407 {
2408 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2409 ssl->state++;
2410 return( 0 );
2411 }
2412
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002413 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002414 return( ret );
2415}
2416
2417int ssl_parse_certificate( ssl_context *ssl )
2418{
2419 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2420 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2421
2422 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2423
2424 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002425 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2426 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002427 {
2428 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2429 ssl->state++;
2430 return( 0 );
2431 }
2432
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002433 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002434 return( ret );
2435}
2436#else
2437int ssl_write_certificate( ssl_context *ssl )
2438{
2439 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2440 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002441 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002442 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2443
2444 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2445
2446 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002447 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2448 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449 {
2450 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2451 ssl->state++;
2452 return( 0 );
2453 }
2454
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 if( ssl->endpoint == SSL_IS_CLIENT )
2456 {
2457 if( ssl->client_auth == 0 )
2458 {
2459 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2460 ssl->state++;
2461 return( 0 );
2462 }
2463
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002464#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 /*
2466 * If using SSLv3 and got no cert, send an Alert message
2467 * (otherwise an empty Certificate message will be sent).
2468 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002469 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2471 {
2472 ssl->out_msglen = 2;
2473 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002474 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2475 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
2477 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2478 goto write_msg;
2479 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002480#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 }
2482 else /* SSL_IS_SERVER */
2483 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002484 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 {
2486 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002487 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 }
2489 }
2490
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002491 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
2493 /*
2494 * 0 . 0 handshake type
2495 * 1 . 3 handshake length
2496 * 4 . 6 length of all certs
2497 * 7 . 9 length of cert. 1
2498 * 10 . n-1 peer certificate
2499 * n . n+2 length of cert. 2
2500 * n+3 . ... upper level cert, etc.
2501 */
2502 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002503 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504
Paul Bakker29087132010-03-21 21:03:34 +00002505 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 {
2507 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002508 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 {
2510 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2511 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002512 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 }
2514
2515 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2516 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2517 ssl->out_msg[i + 2] = (unsigned char)( n );
2518
2519 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2520 i += n; crt = crt->next;
2521 }
2522
2523 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2524 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2525 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2526
2527 ssl->out_msglen = i;
2528 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2529 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2530
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002531#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002532write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
2535 ssl->state++;
2536
2537 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2538 {
2539 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2540 return( ret );
2541 }
2542
2543 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2544
Paul Bakkered27a042013-04-18 22:46:23 +02002545 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002546}
2547
2548int ssl_parse_certificate( ssl_context *ssl )
2549{
Paul Bakkered27a042013-04-18 22:46:23 +02002550 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002551 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002552 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
2554 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2555
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002556 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002557 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2558 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002559 {
2560 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2561 ssl->state++;
2562 return( 0 );
2563 }
2564
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002566 ( ssl->authmode == SSL_VERIFY_NONE ||
2567 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002569 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2571 ssl->state++;
2572 return( 0 );
2573 }
2574
2575 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2576 {
2577 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2578 return( ret );
2579 }
2580
2581 ssl->state++;
2582
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002583#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 /*
2585 * Check if the client sent an empty certificate
2586 */
2587 if( ssl->endpoint == SSL_IS_SERVER &&
2588 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2589 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002590 if( ssl->in_msglen == 2 &&
2591 ssl->in_msgtype == SSL_MSG_ALERT &&
2592 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2593 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 {
2595 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2596
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002597 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2599 return( 0 );
2600 else
Paul Bakker40e46942009-01-03 21:51:57 +00002601 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 }
2603 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002604#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002606#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2607 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 if( ssl->endpoint == SSL_IS_SERVER &&
2609 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2610 {
2611 if( ssl->in_hslen == 7 &&
2612 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2613 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2614 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2615 {
2616 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2617
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002618 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002620 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 else
2622 return( 0 );
2623 }
2624 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002625#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2626 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
2628 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2629 {
2630 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002631 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 }
2633
2634 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2635 {
2636 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002637 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 }
2639
2640 /*
2641 * Same message structure as in ssl_write_certificate()
2642 */
2643 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2644
2645 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2646 {
2647 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002648 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 }
2650
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002651 /* In case we tried to reuse a session but it failed */
2652 if( ssl->session_negotiate->peer_cert != NULL )
2653 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002654 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002655 polarssl_free( ssl->session_negotiate->peer_cert );
2656 }
2657
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002658 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2659 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 {
2661 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002662 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002663 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 }
2665
Paul Bakkerb6b09562013-09-18 14:17:41 +02002666 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
2668 i = 7;
2669
2670 while( i < ssl->in_hslen )
2671 {
2672 if( ssl->in_msg[i] != 0 )
2673 {
2674 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002675 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 }
2677
2678 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2679 | (unsigned int) ssl->in_msg[i + 2];
2680 i += 3;
2681
2682 if( n < 128 || i + n > ssl->in_hslen )
2683 {
2684 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002685 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 }
2687
Paul Bakkerddf26b42013-09-18 13:46:23 +02002688 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2689 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 if( ret != 0 )
2691 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002692 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 return( ret );
2694 }
2695
2696 i += n;
2697 }
2698
Paul Bakker48916f92012-09-16 19:57:18 +00002699 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002701 /*
2702 * On client, make sure the server cert doesn't change during renego to
2703 * avoid "triple handshake" attack: https://secure-resumption.com/
2704 */
2705 if( ssl->endpoint == SSL_IS_CLIENT &&
2706 ssl->renegotiation == SSL_RENEGOTIATION )
2707 {
2708 if( ssl->session->peer_cert == NULL )
2709 {
2710 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2711 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2712 }
2713
2714 if( ssl->session->peer_cert->raw.len !=
2715 ssl->session_negotiate->peer_cert->raw.len ||
2716 memcmp( ssl->session->peer_cert->raw.p,
2717 ssl->session_negotiate->peer_cert->raw.p,
2718 ssl->session->peer_cert->raw.len ) != 0 )
2719 {
2720 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2721 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2722 }
2723 }
2724
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 if( ssl->authmode != SSL_VERIFY_NONE )
2726 {
2727 if( ssl->ca_chain == NULL )
2728 {
2729 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002730 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 }
2732
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002733 /*
2734 * Main check: verify certificate
2735 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002736 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2737 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2738 &ssl->session_negotiate->verify_result,
2739 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
2741 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002742 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002744 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002745
2746 /*
2747 * Secondary checks: always done, but change 'ret' only if it was 0
2748 */
2749
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002750#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002751 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002752 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002753
2754 /* If certificate uses an EC key, make sure the curve is OK */
2755 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2756 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2757 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002758 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002759 if( ret == 0 )
2760 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002761 }
2762 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002763#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002764
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002765 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2766 ciphersuite_info,
2767 ! ssl->endpoint ) != 0 )
2768 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002769 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002770 if( ret == 0 )
2771 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2772 }
2773
Paul Bakker5121ce52009-01-03 21:22:43 +00002774 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2775 ret = 0;
2776 }
2777
2778 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2779
2780 return( ret );
2781}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002782#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2783 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2784 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2785 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2786 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2787 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2788 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
2790int ssl_write_change_cipher_spec( ssl_context *ssl )
2791{
2792 int ret;
2793
2794 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2795
2796 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2797 ssl->out_msglen = 1;
2798 ssl->out_msg[0] = 1;
2799
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 ssl->state++;
2801
2802 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2803 {
2804 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2805 return( ret );
2806 }
2807
2808 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2809
2810 return( 0 );
2811}
2812
2813int ssl_parse_change_cipher_spec( ssl_context *ssl )
2814{
2815 int ret;
2816
2817 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2818
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2820 {
2821 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2822 return( ret );
2823 }
2824
2825 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2826 {
2827 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002828 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 }
2830
2831 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2832 {
2833 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002834 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835 }
2836
2837 ssl->state++;
2838
2839 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2840
2841 return( 0 );
2842}
2843
Paul Bakker41c83d32013-03-20 14:39:14 +01002844void ssl_optimize_checksum( ssl_context *ssl,
2845 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002846{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002847 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002848
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002849#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2850 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002851 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002852 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002853 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002854#endif
2855#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2856#if defined(POLARSSL_SHA512_C)
2857 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2858 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2859 else
2860#endif
2861#if defined(POLARSSL_SHA256_C)
2862 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002863 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002864 else
2865#endif
2866#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2867 /* Should never happen */
2868 return;
Paul Bakker380da532012-04-18 16:10:25 +00002869}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002870
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002871static void ssl_update_checksum_start( ssl_context *ssl,
2872 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002873{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002874#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2875 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002876 md5_update( &ssl->handshake->fin_md5 , buf, len );
2877 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002878#endif
2879#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2880#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002881 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002882#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002883#if defined(POLARSSL_SHA512_C)
2884 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002885#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002886#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002887}
2888
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002889#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2890 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002891static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2892 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002893{
Paul Bakker48916f92012-09-16 19:57:18 +00002894 md5_update( &ssl->handshake->fin_md5 , buf, len );
2895 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002896}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002897#endif
Paul Bakker380da532012-04-18 16:10:25 +00002898
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002899#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2900#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002901static void ssl_update_checksum_sha256( ssl_context *ssl,
2902 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002903{
Paul Bakker9e36f042013-06-30 14:34:05 +02002904 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002905}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002906#endif
Paul Bakker380da532012-04-18 16:10:25 +00002907
Paul Bakker9e36f042013-06-30 14:34:05 +02002908#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002909static void ssl_update_checksum_sha384( ssl_context *ssl,
2910 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002911{
Paul Bakker9e36f042013-06-30 14:34:05 +02002912 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002913}
Paul Bakker769075d2012-11-24 11:26:46 +01002914#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002915#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002916
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002917#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918static void ssl_calc_finished_ssl(
2919 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002920{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002921 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922 md5_context md5;
2923 sha1_context sha1;
2924
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 unsigned char padbuf[48];
2926 unsigned char md5sum[16];
2927 unsigned char sha1sum[20];
2928
Paul Bakker48916f92012-09-16 19:57:18 +00002929 ssl_session *session = ssl->session_negotiate;
2930 if( !session )
2931 session = ssl->session;
2932
Paul Bakker1ef83d62012-04-11 12:09:53 +00002933 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2934
Paul Bakker48916f92012-09-16 19:57:18 +00002935 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2936 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
2938 /*
2939 * SSLv3:
2940 * hash =
2941 * MD5( master + pad2 +
2942 * MD5( handshake + sender + master + pad1 ) )
2943 * + SHA1( master + pad2 +
2944 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 */
2946
Paul Bakker90995b52013-06-24 19:20:35 +02002947#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002948 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002950#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Paul Bakker90995b52013-06-24 19:20:35 +02002952#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002955#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
Paul Bakker3c2122f2013-06-24 19:03:14 +02002957 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2958 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Paul Bakker3c2122f2013-06-24 19:03:14 +02002962 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002963 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964 md5_update( &md5, padbuf, 48 );
2965 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Paul Bakker3c2122f2013-06-24 19:03:14 +02002967 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002968 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969 sha1_update( &sha1, padbuf, 40 );
2970 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002975 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976 md5_update( &md5, padbuf, 48 );
2977 md5_update( &md5, md5sum, 16 );
2978 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
Paul Bakker1ef83d62012-04-11 12:09:53 +00002980 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002981 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982 sha1_update( &sha1, padbuf , 40 );
2983 sha1_update( &sha1, sha1sum, 20 );
2984 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
Paul Bakker1ef83d62012-04-11 12:09:53 +00002988 memset( &md5, 0, sizeof( md5_context ) );
2989 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
2991 memset( padbuf, 0, sizeof( padbuf ) );
2992 memset( md5sum, 0, sizeof( md5sum ) );
2993 memset( sha1sum, 0, sizeof( sha1sum ) );
2994
2995 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2996}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002997#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002999#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003000static void ssl_calc_finished_tls(
3001 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003002{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003003 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003004 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003005 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003007 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Paul Bakker48916f92012-09-16 19:57:18 +00003009 ssl_session *session = ssl->session_negotiate;
3010 if( !session )
3011 session = ssl->session;
3012
Paul Bakker1ef83d62012-04-11 12:09:53 +00003013 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Paul Bakker48916f92012-09-16 19:57:18 +00003015 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3016 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
Paul Bakker1ef83d62012-04-11 12:09:53 +00003018 /*
3019 * TLSv1:
3020 * hash = PRF( master, finished_label,
3021 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3022 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Paul Bakker90995b52013-06-24 19:20:35 +02003024#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003025 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3026 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003027#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003028
Paul Bakker90995b52013-06-24 19:20:35 +02003029#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003030 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3031 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003032#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003033
3034 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003035 ? "client finished"
3036 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037
3038 md5_finish( &md5, padbuf );
3039 sha1_finish( &sha1, padbuf + 16 );
3040
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003041 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003042 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003043
3044 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3045
3046 memset( &md5, 0, sizeof( md5_context ) );
3047 memset( &sha1, 0, sizeof( sha1_context ) );
3048
3049 memset( padbuf, 0, sizeof( padbuf ) );
3050
3051 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3052}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003053#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003054
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003055#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3056#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003057static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003058 ssl_context *ssl, unsigned char *buf, int from )
3059{
3060 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003061 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003062 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063 unsigned char padbuf[32];
3064
Paul Bakker48916f92012-09-16 19:57:18 +00003065 ssl_session *session = ssl->session_negotiate;
3066 if( !session )
3067 session = ssl->session;
3068
Paul Bakker380da532012-04-18 16:10:25 +00003069 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003070
Paul Bakker9e36f042013-06-30 14:34:05 +02003071 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072
3073 /*
3074 * TLSv1.2:
3075 * hash = PRF( master, finished_label,
3076 * Hash( handshake ) )[0.11]
3077 */
3078
Paul Bakker9e36f042013-06-30 14:34:05 +02003079#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003080 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003081 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003082#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003083
3084 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003085 ? "client finished"
3086 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003087
Paul Bakker9e36f042013-06-30 14:34:05 +02003088 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003089
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003090 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003091 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003092
3093 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3094
Paul Bakker9e36f042013-06-30 14:34:05 +02003095 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003096
3097 memset( padbuf, 0, sizeof( padbuf ) );
3098
3099 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3100}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003101#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102
Paul Bakker9e36f042013-06-30 14:34:05 +02003103#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003104static void ssl_calc_finished_tls_sha384(
3105 ssl_context *ssl, unsigned char *buf, int from )
3106{
3107 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003108 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003109 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003110 unsigned char padbuf[48];
3111
Paul Bakker48916f92012-09-16 19:57:18 +00003112 ssl_session *session = ssl->session_negotiate;
3113 if( !session )
3114 session = ssl->session;
3115
Paul Bakker380da532012-04-18 16:10:25 +00003116 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003117
Paul Bakker9e36f042013-06-30 14:34:05 +02003118 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003119
3120 /*
3121 * TLSv1.2:
3122 * hash = PRF( master, finished_label,
3123 * Hash( handshake ) )[0.11]
3124 */
3125
Paul Bakker9e36f042013-06-30 14:34:05 +02003126#if !defined(POLARSSL_SHA512_ALT)
3127 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3128 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003129#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003130
3131 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003132 ? "client finished"
3133 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003134
Paul Bakker9e36f042013-06-30 14:34:05 +02003135 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003136
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003137 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003138 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003139
3140 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3141
Paul Bakker9e36f042013-06-30 14:34:05 +02003142 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003143
3144 memset( padbuf, 0, sizeof( padbuf ) );
3145
3146 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3147}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003148#endif /* POLARSSL_SHA512_C */
3149#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003150
Paul Bakker48916f92012-09-16 19:57:18 +00003151void ssl_handshake_wrapup( ssl_context *ssl )
3152{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003153 int resume = ssl->handshake->resume;
3154
Paul Bakker48916f92012-09-16 19:57:18 +00003155 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3156
3157 /*
3158 * Free our handshake params
3159 */
3160 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003161 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003162 ssl->handshake = NULL;
3163
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003164 if( ssl->renegotiation == SSL_RENEGOTIATION )
3165 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3166
Paul Bakker48916f92012-09-16 19:57:18 +00003167 /*
3168 * Switch in our now active transform context
3169 */
3170 if( ssl->transform )
3171 {
3172 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003173 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003174 }
3175 ssl->transform = ssl->transform_negotiate;
3176 ssl->transform_negotiate = NULL;
3177
Paul Bakker0a597072012-09-25 21:55:46 +00003178 if( ssl->session )
3179 {
3180 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003181 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003182 }
3183 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003184 ssl->session_negotiate = NULL;
3185
Paul Bakker0a597072012-09-25 21:55:46 +00003186 /*
3187 * Add cache entry
3188 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003189 if( ssl->f_set_cache != NULL &&
3190 ssl->session->length != 0 &&
3191 resume == 0 )
3192 {
Paul Bakker0a597072012-09-25 21:55:46 +00003193 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3194 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003195 }
Paul Bakker0a597072012-09-25 21:55:46 +00003196
Paul Bakker48916f92012-09-16 19:57:18 +00003197 ssl->state++;
3198
3199 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3200}
3201
Paul Bakker1ef83d62012-04-11 12:09:53 +00003202int ssl_write_finished( ssl_context *ssl )
3203{
3204 int ret, hash_len;
3205
3206 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3207
Paul Bakker92be97b2013-01-02 17:30:03 +01003208 /*
3209 * Set the out_msg pointer to the correct location based on IV length
3210 */
3211 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3212 {
3213 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3214 ssl->transform_negotiate->fixed_ivlen;
3215 }
3216 else
3217 ssl->out_msg = ssl->out_iv;
3218
Paul Bakker48916f92012-09-16 19:57:18 +00003219 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003220
3221 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003222 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3223
Paul Bakker48916f92012-09-16 19:57:18 +00003224 ssl->verify_data_len = hash_len;
3225 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3226
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 ssl->out_msglen = 4 + hash_len;
3228 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3229 ssl->out_msg[0] = SSL_HS_FINISHED;
3230
3231 /*
3232 * In case of session resuming, invert the client and server
3233 * ChangeCipherSpec messages order.
3234 */
Paul Bakker0a597072012-09-25 21:55:46 +00003235 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 {
3237 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003238 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 else
3240 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3241 }
3242 else
3243 ssl->state++;
3244
Paul Bakker48916f92012-09-16 19:57:18 +00003245 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003246 * Switch to our negotiated transform and session parameters for outbound
3247 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003248 */
3249 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3250 ssl->transform_out = ssl->transform_negotiate;
3251 ssl->session_out = ssl->session_negotiate;
3252 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Paul Bakker07eb38b2012-12-19 14:42:06 +01003254#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3255 if( ssl_hw_record_activate != NULL)
3256 {
3257 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3258 {
3259 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3260 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3261 }
3262 }
3263#endif
3264
Paul Bakker5121ce52009-01-03 21:22:43 +00003265 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3266 {
3267 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3268 return( ret );
3269 }
3270
3271 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3272
3273 return( 0 );
3274}
3275
3276int ssl_parse_finished( ssl_context *ssl )
3277{
Paul Bakker23986e52011-04-24 08:57:21 +00003278 int ret;
3279 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 unsigned char buf[36];
3281
3282 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3283
Paul Bakker48916f92012-09-16 19:57:18 +00003284 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285
Paul Bakker48916f92012-09-16 19:57:18 +00003286 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003287 * Switch to our negotiated transform and session parameters for inbound
3288 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003289 */
3290 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3291 ssl->transform_in = ssl->transform_negotiate;
3292 ssl->session_in = ssl->session_negotiate;
3293 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Paul Bakker92be97b2013-01-02 17:30:03 +01003295 /*
3296 * Set the in_msg pointer to the correct location based on IV length
3297 */
3298 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3299 {
3300 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3301 ssl->transform_negotiate->fixed_ivlen;
3302 }
3303 else
3304 ssl->in_msg = ssl->in_iv;
3305
Paul Bakker07eb38b2012-12-19 14:42:06 +01003306#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3307 if( ssl_hw_record_activate != NULL)
3308 {
3309 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3310 {
3311 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3312 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3313 }
3314 }
3315#endif
3316
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3318 {
3319 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3320 return( ret );
3321 }
3322
3323 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3324 {
3325 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003326 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003327 }
3328
Paul Bakker1ef83d62012-04-11 12:09:53 +00003329 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3331
3332 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3333 ssl->in_hslen != 4 + hash_len )
3334 {
3335 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003336 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 }
3338
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003339 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003340 {
3341 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003342 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 }
3344
Paul Bakker48916f92012-09-16 19:57:18 +00003345 ssl->verify_data_len = hash_len;
3346 memcpy( ssl->peer_verify_data, buf, hash_len );
3347
Paul Bakker0a597072012-09-25 21:55:46 +00003348 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 {
3350 if( ssl->endpoint == SSL_IS_CLIENT )
3351 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3352
3353 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003354 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 }
3356 else
3357 ssl->state++;
3358
3359 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3360
3361 return( 0 );
3362}
3363
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003364static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003365{
3366 if( ssl->transform_negotiate )
3367 ssl_transform_free( ssl->transform_negotiate );
3368 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003369 {
3370 ssl->transform_negotiate =
3371 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003372
3373 if( ssl->transform_negotiate != NULL )
3374 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003375 }
Paul Bakker48916f92012-09-16 19:57:18 +00003376
3377 if( ssl->session_negotiate )
3378 ssl_session_free( ssl->session_negotiate );
3379 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003380 {
3381 ssl->session_negotiate =
3382 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003383
3384 if( ssl->session_negotiate != NULL )
3385 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003386 }
Paul Bakker48916f92012-09-16 19:57:18 +00003387
3388 if( ssl->handshake )
3389 ssl_handshake_free( ssl->handshake );
3390 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003391 {
3392 ssl->handshake = (ssl_handshake_params *)
3393 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003394
3395 if( ssl->handshake != NULL )
3396 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003397 }
Paul Bakker48916f92012-09-16 19:57:18 +00003398
3399 if( ssl->handshake == NULL ||
3400 ssl->transform_negotiate == NULL ||
3401 ssl->session_negotiate == NULL )
3402 {
3403 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3404 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3405 }
3406
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003407#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3408 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003409 md5_starts( &ssl->handshake->fin_md5 );
3410 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003411#endif
3412#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3413#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003414 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003415#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003416#if defined(POLARSSL_SHA512_C)
3417 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003418#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003419#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003420
3421 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003422 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003423
Paul Bakker61d113b2013-07-04 11:51:43 +02003424#if defined(POLARSSL_ECDH_C)
3425 ecdh_init( &ssl->handshake->ecdh_ctx );
3426#endif
3427
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003428#if defined(POLARSSL_X509_CRT_PARSE_C)
3429 ssl->handshake->key_cert = ssl->key_cert;
3430#endif
3431
Paul Bakker48916f92012-09-16 19:57:18 +00003432 return( 0 );
3433}
3434
Paul Bakker5121ce52009-01-03 21:22:43 +00003435/*
3436 * Initialize an SSL context
3437 */
3438int ssl_init( ssl_context *ssl )
3439{
Paul Bakker48916f92012-09-16 19:57:18 +00003440 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 int len = SSL_BUFFER_LEN;
3442
3443 memset( ssl, 0, sizeof( ssl_context ) );
3444
Paul Bakker62f2dee2012-09-28 07:31:51 +00003445 /*
3446 * Sane defaults
3447 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003448 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3449 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3450 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3451 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003452
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003453 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003454
Paul Bakker62f2dee2012-09-28 07:31:51 +00003455#if defined(POLARSSL_DHM_C)
3456 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3457 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3458 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3459 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3460 {
3461 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3462 return( ret );
3463 }
3464#endif
3465
3466 /*
3467 * Prepare base structures
3468 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003469 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003471 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 ssl->in_msg = ssl->in_ctr + 13;
3473
3474 if( ssl->in_ctr == NULL )
3475 {
3476 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003477 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003478 }
3479
Paul Bakker6e339b52013-07-03 13:37:05 +02003480 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003482 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003483 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003484
3485 if( ssl->out_ctr == NULL )
3486 {
3487 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003488 polarssl_free( ssl->in_ctr );
3489 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003490 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 }
3492
3493 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3494 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3495
Paul Bakker606b4ba2013-08-14 16:52:14 +02003496#if defined(POLARSSL_SSL_SESSION_TICKETS)
3497 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3498#endif
3499
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003500#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003501 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003502#endif
3503
Paul Bakker48916f92012-09-16 19:57:18 +00003504 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3505 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003506
3507 return( 0 );
3508}
3509
3510/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003511 * Reset an initialized and used SSL context for re-use while retaining
3512 * all application-set variables, function pointers and data.
3513 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003514int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003515{
Paul Bakker48916f92012-09-16 19:57:18 +00003516 int ret;
3517
Paul Bakker7eb013f2011-10-06 12:37:39 +00003518 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003519 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3520 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3521
3522 ssl->verify_data_len = 0;
3523 memset( ssl->own_verify_data, 0, 36 );
3524 memset( ssl->peer_verify_data, 0, 36 );
3525
Paul Bakker7eb013f2011-10-06 12:37:39 +00003526 ssl->in_offt = NULL;
3527
Paul Bakker92be97b2013-01-02 17:30:03 +01003528 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003529 ssl->in_msgtype = 0;
3530 ssl->in_msglen = 0;
3531 ssl->in_left = 0;
3532
3533 ssl->in_hslen = 0;
3534 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003535 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003536
Paul Bakker92be97b2013-01-02 17:30:03 +01003537 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003538 ssl->out_msgtype = 0;
3539 ssl->out_msglen = 0;
3540 ssl->out_left = 0;
3541
Paul Bakker48916f92012-09-16 19:57:18 +00003542 ssl->transform_in = NULL;
3543 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003544
3545 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3546 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003547
3548#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3549 if( ssl_hw_record_reset != NULL)
3550 {
3551 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003552 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003553 {
3554 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3555 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3556 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003557 }
3558#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003559
Paul Bakker48916f92012-09-16 19:57:18 +00003560 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003561 {
Paul Bakker48916f92012-09-16 19:57:18 +00003562 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003563 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003564 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003565 }
Paul Bakker48916f92012-09-16 19:57:18 +00003566
Paul Bakkerc0463502013-02-14 11:19:38 +01003567 if( ssl->session )
3568 {
3569 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003570 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003571 ssl->session = NULL;
3572 }
3573
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003574#if defined(POLARSSL_SSL_ALPN)
3575 ssl->alpn_chosen = NULL;
3576#endif
3577
Paul Bakker48916f92012-09-16 19:57:18 +00003578 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3579 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003580
3581 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003582}
3583
Paul Bakkera503a632013-08-14 13:48:06 +02003584#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003585/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003586 * Allocate and initialize ticket keys
3587 */
3588static int ssl_ticket_keys_init( ssl_context *ssl )
3589{
3590 int ret;
3591 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003592 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003593
3594 if( ssl->ticket_keys != NULL )
3595 return( 0 );
3596
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003597 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3598 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003599 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3600
3601 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003602 {
3603 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003604 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003605 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003606
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003607 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3608 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3609 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3610 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003611 polarssl_free( tkeys );
3612 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003613 }
3614
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003615 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003616 {
3617 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003618 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003619 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003620
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003621 ssl->ticket_keys = tkeys;
3622
3623 return( 0 );
3624}
Paul Bakkera503a632013-08-14 13:48:06 +02003625#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003626
3627/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 * SSL set accessors
3629 */
3630void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3631{
3632 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003633
Paul Bakker606b4ba2013-08-14 16:52:14 +02003634#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003635 if( endpoint == SSL_IS_CLIENT )
3636 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003637#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003638}
3639
3640void ssl_set_authmode( ssl_context *ssl, int authmode )
3641{
3642 ssl->authmode = authmode;
3643}
3644
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003645#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003646void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003647 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003648 void *p_vrfy )
3649{
3650 ssl->f_vrfy = f_vrfy;
3651 ssl->p_vrfy = p_vrfy;
3652}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003653#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003654
Paul Bakker5121ce52009-01-03 21:22:43 +00003655void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003656 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 void *p_rng )
3658{
3659 ssl->f_rng = f_rng;
3660 ssl->p_rng = p_rng;
3661}
3662
3663void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003664 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 void *p_dbg )
3666{
3667 ssl->f_dbg = f_dbg;
3668 ssl->p_dbg = p_dbg;
3669}
3670
3671void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003672 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003673 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003674{
3675 ssl->f_recv = f_recv;
3676 ssl->f_send = f_send;
3677 ssl->p_recv = p_recv;
3678 ssl->p_send = p_send;
3679}
3680
Paul Bakker0a597072012-09-25 21:55:46 +00003681void ssl_set_session_cache( ssl_context *ssl,
3682 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3683 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003684{
Paul Bakker0a597072012-09-25 21:55:46 +00003685 ssl->f_get_cache = f_get_cache;
3686 ssl->p_get_cache = p_get_cache;
3687 ssl->f_set_cache = f_set_cache;
3688 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003689}
3690
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003691int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003692{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003693 int ret;
3694
3695 if( ssl == NULL ||
3696 session == NULL ||
3697 ssl->session_negotiate == NULL ||
3698 ssl->endpoint != SSL_IS_CLIENT )
3699 {
3700 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3701 }
3702
3703 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3704 return( ret );
3705
Paul Bakker0a597072012-09-25 21:55:46 +00003706 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003707
3708 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003709}
3710
Paul Bakkerb68cad62012-08-23 08:34:18 +00003711void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003712{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003713 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3714 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3715 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3716 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3717}
3718
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003719void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3720 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003721 int major, int minor )
3722{
3723 if( major != SSL_MAJOR_VERSION_3 )
3724 return;
3725
3726 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3727 return;
3728
3729 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003730}
3731
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003732#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003733/* Add a new (empty) key_cert entry an return a pointer to it */
3734static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3735{
3736 ssl_key_cert *key_cert, *last;
3737
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003738 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3739 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003740 return( NULL );
3741
3742 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3743
3744 /* Append the new key_cert to the (possibly empty) current list */
3745 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003746 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003747 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003748 if( ssl->handshake != NULL )
3749 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003750 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003751 else
3752 {
3753 last = ssl->key_cert;
3754 while( last->next != NULL )
3755 last = last->next;
3756 last->next = key_cert;
3757 }
3758
3759 return key_cert;
3760}
3761
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003762void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003763 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003764{
3765 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003766 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 ssl->peer_cn = peer_cn;
3768}
3769
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003770int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003771 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003772{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003773 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3774
3775 if( key_cert == NULL )
3776 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3777
3778 key_cert->cert = own_cert;
3779 key_cert->key = pk_key;
3780
3781 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003782}
3783
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003784#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003785int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003786 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003787{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003788 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003789 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003790
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003791 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003792 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3793
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003794 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3795 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003796 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003797
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003798 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003799
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003800 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003801 if( ret != 0 )
3802 return( ret );
3803
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003804 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003805 return( ret );
3806
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003807 key_cert->cert = own_cert;
3808 key_cert->key_own_alloc = 1;
3809
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003810 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003811}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003812#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003813
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003814int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003815 void *rsa_key,
3816 rsa_decrypt_func rsa_decrypt,
3817 rsa_sign_func rsa_sign,
3818 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003819{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003820 int ret;
3821 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003822
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003823 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003824 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3825
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003826 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3827 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003828 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003829
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003830 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003831
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003832 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3833 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3834 return( ret );
3835
3836 key_cert->cert = own_cert;
3837 key_cert->key_own_alloc = 1;
3838
3839 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003840}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003841#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003842
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003843#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003844int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3845 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003846{
Paul Bakker6db455e2013-09-18 17:29:31 +02003847 if( psk == NULL || psk_identity == NULL )
3848 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3849
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003850 /*
3851 * The length will be check later anyway, but in case it is obviously
3852 * too large, better abort now. The PMS is as follows:
3853 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3854 */
3855 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3856 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3857
Paul Bakker6db455e2013-09-18 17:29:31 +02003858 if( ssl->psk != NULL )
3859 {
3860 polarssl_free( ssl->psk );
3861 polarssl_free( ssl->psk_identity );
3862 }
3863
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003864 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003865 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003866
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003867 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003868 ssl->psk_identity = (unsigned char *)
3869 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003870
3871 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3872 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3873
3874 memcpy( ssl->psk, psk, ssl->psk_len );
3875 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003876
3877 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003878}
3879
3880void ssl_set_psk_cb( ssl_context *ssl,
3881 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3882 size_t),
3883 void *p_psk )
3884{
3885 ssl->f_psk = f_psk;
3886 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003887}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003888#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003889
Paul Bakker48916f92012-09-16 19:57:18 +00003890#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003891int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003892{
3893 int ret;
3894
Paul Bakker48916f92012-09-16 19:57:18 +00003895 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003896 {
3897 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3898 return( ret );
3899 }
3900
Paul Bakker48916f92012-09-16 19:57:18 +00003901 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003902 {
3903 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3904 return( ret );
3905 }
3906
3907 return( 0 );
3908}
3909
Paul Bakker1b57b062011-01-06 15:48:19 +00003910int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3911{
3912 int ret;
3913
Paul Bakker48916f92012-09-16 19:57:18 +00003914 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003915 {
3916 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3917 return( ret );
3918 }
3919
Paul Bakker48916f92012-09-16 19:57:18 +00003920 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003921 {
3922 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3923 return( ret );
3924 }
3925
3926 return( 0 );
3927}
Paul Bakker48916f92012-09-16 19:57:18 +00003928#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003929
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003930#if defined(POLARSSL_SSL_SET_CURVES)
3931/*
3932 * Set the allowed elliptic curves
3933 */
3934void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3935{
3936 ssl->curve_list = curve_list;
3937}
3938#endif
3939
Paul Bakker0be444a2013-08-27 21:55:01 +02003940#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003941int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003942{
3943 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003944 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003945
3946 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003947
3948 if( ssl->hostname_len + 1 == 0 )
3949 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3950
Paul Bakker6e339b52013-07-03 13:37:05 +02003951 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003952
Paul Bakkerb15b8512012-01-13 13:44:06 +00003953 if( ssl->hostname == NULL )
3954 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3955
Paul Bakker3c2122f2013-06-24 19:03:14 +02003956 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003957 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003958
Paul Bakker40ea7de2009-05-03 10:18:48 +00003959 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003960
3961 return( 0 );
3962}
3963
Paul Bakker5701cdc2012-09-27 21:49:42 +00003964void ssl_set_sni( ssl_context *ssl,
3965 int (*f_sni)(void *, ssl_context *,
3966 const unsigned char *, size_t),
3967 void *p_sni )
3968{
3969 ssl->f_sni = f_sni;
3970 ssl->p_sni = p_sni;
3971}
Paul Bakker0be444a2013-08-27 21:55:01 +02003972#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003973
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003974#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003975int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003976{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003977 size_t cur_len, tot_len;
3978 const char **p;
3979
3980 /*
3981 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3982 * truncated". Check lengths now rather than later.
3983 */
3984 tot_len = 0;
3985 for( p = protos; *p != NULL; p++ )
3986 {
3987 cur_len = strlen( *p );
3988 tot_len += cur_len;
3989
3990 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3991 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3992 }
3993
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003994 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003995
3996 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003997}
3998
3999const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4000{
4001 return ssl->alpn_chosen;
4002}
4003#endif /* POLARSSL_SSL_ALPN */
4004
Paul Bakker490ecc82011-10-06 13:04:09 +00004005void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4006{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004007 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4008 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4009 {
4010 ssl->max_major_ver = major;
4011 ssl->max_minor_ver = minor;
4012 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004013}
4014
Paul Bakker1d29fb52012-09-28 13:28:45 +00004015void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4016{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004017 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4018 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4019 {
4020 ssl->min_major_ver = major;
4021 ssl->min_minor_ver = minor;
4022 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004023}
4024
Paul Bakker05decb22013-08-15 13:33:48 +02004025#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004026int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4027{
Paul Bakker77e257e2013-12-16 15:29:52 +01004028 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004029 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004030 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004031 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004032 }
4033
4034 ssl->mfl_code = mfl_code;
4035
4036 return( 0 );
4037}
Paul Bakker05decb22013-08-15 13:33:48 +02004038#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004039
Paul Bakker1f2bc622013-08-15 13:45:55 +02004040#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004041int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004042{
4043 if( ssl->endpoint != SSL_IS_CLIENT )
4044 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4045
Paul Bakker8c1ede62013-07-19 14:14:37 +02004046 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004047
4048 return( 0 );
4049}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004050#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004051
Paul Bakker48916f92012-09-16 19:57:18 +00004052void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4053{
4054 ssl->disable_renegotiation = renegotiation;
4055}
4056
4057void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4058{
4059 ssl->allow_legacy_renegotiation = allow_legacy;
4060}
4061
Paul Bakkera503a632013-08-14 13:48:06 +02004062#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004063int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4064{
4065 ssl->session_tickets = use_tickets;
4066
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004067 if( ssl->endpoint == SSL_IS_CLIENT )
4068 return( 0 );
4069
4070 if( ssl->f_rng == NULL )
4071 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4072
4073 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004074}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004075
4076void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4077{
4078 ssl->ticket_lifetime = lifetime;
4079}
Paul Bakkera503a632013-08-14 13:48:06 +02004080#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004081
Paul Bakker5121ce52009-01-03 21:22:43 +00004082/*
4083 * SSL get accessors
4084 */
Paul Bakker23986e52011-04-24 08:57:21 +00004085size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004086{
4087 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4088}
4089
Paul Bakkerff60ee62010-03-16 21:09:09 +00004090int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004091{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004092 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004093}
4094
Paul Bakkere3166ce2011-01-27 17:40:50 +00004095const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004096{
Paul Bakker926c8e42013-03-06 10:23:34 +01004097 if( ssl == NULL || ssl->session == NULL )
4098 return NULL;
4099
Paul Bakkere3166ce2011-01-27 17:40:50 +00004100 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004101}
4102
Paul Bakker43ca69c2011-01-15 17:35:19 +00004103const char *ssl_get_version( const ssl_context *ssl )
4104{
4105 switch( ssl->minor_ver )
4106 {
4107 case SSL_MINOR_VERSION_0:
4108 return( "SSLv3.0" );
4109
4110 case SSL_MINOR_VERSION_1:
4111 return( "TLSv1.0" );
4112
4113 case SSL_MINOR_VERSION_2:
4114 return( "TLSv1.1" );
4115
Paul Bakker1ef83d62012-04-11 12:09:53 +00004116 case SSL_MINOR_VERSION_3:
4117 return( "TLSv1.2" );
4118
Paul Bakker43ca69c2011-01-15 17:35:19 +00004119 default:
4120 break;
4121 }
4122 return( "unknown" );
4123}
4124
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004125#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004126const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004127{
4128 if( ssl == NULL || ssl->session == NULL )
4129 return NULL;
4130
4131 return ssl->session->peer_cert;
4132}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004133#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004134
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004135int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4136{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004137 if( ssl == NULL ||
4138 dst == NULL ||
4139 ssl->session == NULL ||
4140 ssl->endpoint != SSL_IS_CLIENT )
4141 {
4142 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4143 }
4144
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004145 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004146}
4147
Paul Bakker5121ce52009-01-03 21:22:43 +00004148/*
Paul Bakker1961b702013-01-25 14:49:24 +01004149 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004150 */
Paul Bakker1961b702013-01-25 14:49:24 +01004151int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004152{
Paul Bakker40e46942009-01-03 21:51:57 +00004153 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004154
Paul Bakker40e46942009-01-03 21:51:57 +00004155#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004156 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004157 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004158#endif
4159
Paul Bakker40e46942009-01-03 21:51:57 +00004160#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004161 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004162 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004163#endif
4164
Paul Bakker1961b702013-01-25 14:49:24 +01004165 return( ret );
4166}
4167
4168/*
4169 * Perform the SSL handshake
4170 */
4171int ssl_handshake( ssl_context *ssl )
4172{
4173 int ret = 0;
4174
4175 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4176
4177 while( ssl->state != SSL_HANDSHAKE_OVER )
4178 {
4179 ret = ssl_handshake_step( ssl );
4180
4181 if( ret != 0 )
4182 break;
4183 }
4184
Paul Bakker5121ce52009-01-03 21:22:43 +00004185 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4186
4187 return( ret );
4188}
4189
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004190#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004191/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004192 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004193 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004194static int ssl_write_hello_request( ssl_context *ssl )
4195{
4196 int ret;
4197
4198 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4199
4200 ssl->out_msglen = 4;
4201 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4202 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4203
4204 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4205 {
4206 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4207 return( ret );
4208 }
4209
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004210 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4211
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004212 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4213
4214 return( 0 );
4215}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004216#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004217
4218/*
4219 * Actually renegotiate current connection, triggered by either:
4220 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004221 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004222 * - receiving any handshake message on server during ssl_read() after the
4223 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004224 * If the handshake doesn't complete due to waiting for I/O, it will continue
4225 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004226 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004227static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004228{
4229 int ret;
4230
4231 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4232
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004233 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4234 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004235
4236 ssl->state = SSL_HELLO_REQUEST;
4237 ssl->renegotiation = SSL_RENEGOTIATION;
4238
Paul Bakker48916f92012-09-16 19:57:18 +00004239 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4240 {
4241 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4242 return( ret );
4243 }
4244
4245 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4246
4247 return( 0 );
4248}
4249
4250/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004251 * Renegotiate current connection on client,
4252 * or request renegotiation on server
4253 */
4254int ssl_renegotiate( ssl_context *ssl )
4255{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004256 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004257
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004258#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004259 /* On server, just send the request */
4260 if( ssl->endpoint == SSL_IS_SERVER )
4261 {
4262 if( ssl->state != SSL_HANDSHAKE_OVER )
4263 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4264
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004265 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004266 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004267#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004268
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004269#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004270 /*
4271 * On client, either start the renegotiation process or,
4272 * if already in progress, continue the handshake
4273 */
4274 if( ssl->renegotiation != SSL_RENEGOTIATION )
4275 {
4276 if( ssl->state != SSL_HANDSHAKE_OVER )
4277 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4278
4279 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4280 {
4281 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4282 return( ret );
4283 }
4284 }
4285 else
4286 {
4287 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4288 {
4289 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4290 return( ret );
4291 }
4292 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004293#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004294
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004295 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004296}
4297
4298/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004299 * Receive application data decrypted from the SSL layer
4300 */
Paul Bakker23986e52011-04-24 08:57:21 +00004301int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004302{
Paul Bakker23986e52011-04-24 08:57:21 +00004303 int ret;
4304 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004305
4306 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4307
4308 if( ssl->state != SSL_HANDSHAKE_OVER )
4309 {
4310 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4311 {
4312 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4313 return( ret );
4314 }
4315 }
4316
4317 if( ssl->in_offt == NULL )
4318 {
4319 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4320 {
Paul Bakker831a7552011-05-18 13:32:51 +00004321 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4322 return( 0 );
4323
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4325 return( ret );
4326 }
4327
4328 if( ssl->in_msglen == 0 &&
4329 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4330 {
4331 /*
4332 * OpenSSL sends empty messages to randomize the IV
4333 */
4334 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4335 {
Paul Bakker831a7552011-05-18 13:32:51 +00004336 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4337 return( 0 );
4338
Paul Bakker5121ce52009-01-03 21:22:43 +00004339 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4340 return( ret );
4341 }
4342 }
4343
Paul Bakker48916f92012-09-16 19:57:18 +00004344 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4345 {
4346 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4347
4348 if( ssl->endpoint == SSL_IS_CLIENT &&
4349 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4350 ssl->in_hslen != 4 ) )
4351 {
4352 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4353 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4354 }
4355
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004356 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4357 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004358 ssl->allow_legacy_renegotiation ==
4359 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004360 {
4361 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4362
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004363#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004364 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004365 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004366 /*
4367 * SSLv3 does not have a "no_renegotiation" alert
4368 */
4369 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4370 return( ret );
4371 }
4372 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004373#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004374#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4375 defined(POLARSSL_SSL_PROTO_TLS1_2)
4376 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004377 {
4378 if( ( ret = ssl_send_alert_message( ssl,
4379 SSL_ALERT_LEVEL_WARNING,
4380 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4381 {
4382 return( ret );
4383 }
Paul Bakker48916f92012-09-16 19:57:18 +00004384 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004385 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004386#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4387 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004388 {
4389 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004390 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004391 }
Paul Bakker48916f92012-09-16 19:57:18 +00004392 }
4393 else
4394 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004395 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004396 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004397 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004398 return( ret );
4399 }
4400
4401 return( POLARSSL_ERR_NET_WANT_READ );
4402 }
4403 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004404 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4405 {
4406 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4407 "but not honored by client" ) );
4408 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4409 }
Paul Bakker48916f92012-09-16 19:57:18 +00004410 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004411 {
4412 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004413 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004414 }
4415
4416 ssl->in_offt = ssl->in_msg;
4417 }
4418
4419 n = ( len < ssl->in_msglen )
4420 ? len : ssl->in_msglen;
4421
4422 memcpy( buf, ssl->in_offt, n );
4423 ssl->in_msglen -= n;
4424
4425 if( ssl->in_msglen == 0 )
4426 /* all bytes consumed */
4427 ssl->in_offt = NULL;
4428 else
4429 /* more data available */
4430 ssl->in_offt += n;
4431
4432 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4433
Paul Bakker23986e52011-04-24 08:57:21 +00004434 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004435}
4436
4437/*
4438 * Send application data to be encrypted by the SSL layer
4439 */
Paul Bakker23986e52011-04-24 08:57:21 +00004440int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004441{
Paul Bakker23986e52011-04-24 08:57:21 +00004442 int ret;
4443 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004444 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004445
4446 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4447
4448 if( ssl->state != SSL_HANDSHAKE_OVER )
4449 {
4450 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4451 {
4452 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4453 return( ret );
4454 }
4455 }
4456
Paul Bakker05decb22013-08-15 13:33:48 +02004457#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004458 /*
4459 * Assume mfl_code is correct since it was checked when set
4460 */
4461 max_len = mfl_code_to_length[ssl->mfl_code];
4462
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004463 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004464 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004465 */
4466 if( ssl->session_out != NULL &&
4467 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4468 {
4469 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4470 }
Paul Bakker05decb22013-08-15 13:33:48 +02004471#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004472
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004473 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004474
Paul Bakker5121ce52009-01-03 21:22:43 +00004475 if( ssl->out_left != 0 )
4476 {
4477 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4478 {
4479 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4480 return( ret );
4481 }
4482 }
Paul Bakker887bd502011-06-08 13:10:54 +00004483 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004484 {
Paul Bakker887bd502011-06-08 13:10:54 +00004485 ssl->out_msglen = n;
4486 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4487 memcpy( ssl->out_msg, buf, n );
4488
4489 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4490 {
4491 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4492 return( ret );
4493 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 }
4495
4496 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4497
Paul Bakker23986e52011-04-24 08:57:21 +00004498 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004499}
4500
4501/*
4502 * Notify the peer that the connection is being closed
4503 */
4504int ssl_close_notify( ssl_context *ssl )
4505{
4506 int ret;
4507
4508 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4509
4510 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4511 {
4512 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4513 return( ret );
4514 }
4515
4516 if( ssl->state == SSL_HANDSHAKE_OVER )
4517 {
Paul Bakker48916f92012-09-16 19:57:18 +00004518 if( ( ret = ssl_send_alert_message( ssl,
4519 SSL_ALERT_LEVEL_WARNING,
4520 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004521 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004522 return( ret );
4523 }
4524 }
4525
4526 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4527
4528 return( ret );
4529}
4530
Paul Bakker48916f92012-09-16 19:57:18 +00004531void ssl_transform_free( ssl_transform *transform )
4532{
4533#if defined(POLARSSL_ZLIB_SUPPORT)
4534 deflateEnd( &transform->ctx_deflate );
4535 inflateEnd( &transform->ctx_inflate );
4536#endif
4537
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004538 cipher_free_ctx( &transform->cipher_ctx_enc );
4539 cipher_free_ctx( &transform->cipher_ctx_dec );
4540
Paul Bakker61d113b2013-07-04 11:51:43 +02004541 md_free_ctx( &transform->md_ctx_enc );
4542 md_free_ctx( &transform->md_ctx_dec );
4543
Paul Bakker48916f92012-09-16 19:57:18 +00004544 memset( transform, 0, sizeof( ssl_transform ) );
4545}
4546
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004547#if defined(POLARSSL_X509_CRT_PARSE_C)
4548static void ssl_key_cert_free( ssl_key_cert *key_cert )
4549{
4550 ssl_key_cert *cur = key_cert, *next;
4551
4552 while( cur != NULL )
4553 {
4554 next = cur->next;
4555
4556 if( cur->key_own_alloc )
4557 {
4558 pk_free( cur->key );
4559 polarssl_free( cur->key );
4560 }
4561 polarssl_free( cur );
4562
4563 cur = next;
4564 }
4565}
4566#endif /* POLARSSL_X509_CRT_PARSE_C */
4567
Paul Bakker48916f92012-09-16 19:57:18 +00004568void ssl_handshake_free( ssl_handshake_params *handshake )
4569{
4570#if defined(POLARSSL_DHM_C)
4571 dhm_free( &handshake->dhm_ctx );
4572#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004573#if defined(POLARSSL_ECDH_C)
4574 ecdh_free( &handshake->ecdh_ctx );
4575#endif
4576
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004577#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004578 /* explicit void pointer cast for buggy MS compiler */
4579 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004580#endif
4581
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004582#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4583 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4584 /*
4585 * Free only the linked list wrapper, not the keys themselves
4586 * since the belong to the SNI callback
4587 */
4588 if( handshake->sni_key_cert != NULL )
4589 {
4590 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4591
4592 while( cur != NULL )
4593 {
4594 next = cur->next;
4595 polarssl_free( cur );
4596 cur = next;
4597 }
4598 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004599#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004600
Paul Bakker48916f92012-09-16 19:57:18 +00004601 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4602}
4603
4604void ssl_session_free( ssl_session *session )
4605{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004606#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004607 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004608 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004609 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004610 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004611 }
Paul Bakkered27a042013-04-18 22:46:23 +02004612#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004613
Paul Bakkera503a632013-08-14 13:48:06 +02004614#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004615 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004616#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004617
Paul Bakker0a597072012-09-25 21:55:46 +00004618 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004619}
4620
Paul Bakker5121ce52009-01-03 21:22:43 +00004621/*
4622 * Free an SSL context
4623 */
4624void ssl_free( ssl_context *ssl )
4625{
4626 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4627
Paul Bakker5121ce52009-01-03 21:22:43 +00004628 if( ssl->out_ctr != NULL )
4629 {
4630 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004631 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004632 }
4633
4634 if( ssl->in_ctr != NULL )
4635 {
4636 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004637 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004638 }
4639
Paul Bakker16770332013-10-11 09:59:44 +02004640#if defined(POLARSSL_ZLIB_SUPPORT)
4641 if( ssl->compress_buf != NULL )
4642 {
4643 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4644 polarssl_free( ssl->compress_buf );
4645 }
4646#endif
4647
Paul Bakker40e46942009-01-03 21:51:57 +00004648#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004649 mpi_free( &ssl->dhm_P );
4650 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004651#endif
4652
Paul Bakker48916f92012-09-16 19:57:18 +00004653 if( ssl->transform )
4654 {
4655 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004656 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004657 }
4658
4659 if( ssl->handshake )
4660 {
4661 ssl_handshake_free( ssl->handshake );
4662 ssl_transform_free( ssl->transform_negotiate );
4663 ssl_session_free( ssl->session_negotiate );
4664
Paul Bakker6e339b52013-07-03 13:37:05 +02004665 polarssl_free( ssl->handshake );
4666 polarssl_free( ssl->transform_negotiate );
4667 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004668 }
4669
Paul Bakkerc0463502013-02-14 11:19:38 +01004670 if( ssl->session )
4671 {
4672 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004673 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004674 }
4675
Paul Bakkera503a632013-08-14 13:48:06 +02004676#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004677 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004678#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004679
Paul Bakker0be444a2013-08-27 21:55:01 +02004680#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004681 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 {
4683 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004684 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 ssl->hostname_len = 0;
4686 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004687#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004688
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004689#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004690 if( ssl->psk != NULL )
4691 {
4692 memset( ssl->psk, 0, ssl->psk_len );
4693 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4694 polarssl_free( ssl->psk );
4695 polarssl_free( ssl->psk_identity );
4696 ssl->psk_len = 0;
4697 ssl->psk_identity_len = 0;
4698 }
4699#endif
4700
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004701#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004702 ssl_key_cert_free( ssl->key_cert );
4703#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004704
Paul Bakker05ef8352012-05-08 09:17:57 +00004705#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4706 if( ssl_hw_record_finish != NULL )
4707 {
4708 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4709 ssl_hw_record_finish( ssl );
4710 }
4711#endif
4712
Paul Bakker5121ce52009-01-03 21:22:43 +00004713 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004714
Paul Bakker86f04f42013-02-14 11:20:09 +01004715 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004716 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004717}
4718
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004719#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004720/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004721 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004722 */
4723unsigned char ssl_sig_from_pk( pk_context *pk )
4724{
4725#if defined(POLARSSL_RSA_C)
4726 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4727 return( SSL_SIG_RSA );
4728#endif
4729#if defined(POLARSSL_ECDSA_C)
4730 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4731 return( SSL_SIG_ECDSA );
4732#endif
4733 return( SSL_SIG_ANON );
4734}
4735
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004736pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4737{
4738 switch( sig )
4739 {
4740#if defined(POLARSSL_RSA_C)
4741 case SSL_SIG_RSA:
4742 return( POLARSSL_PK_RSA );
4743#endif
4744#if defined(POLARSSL_ECDSA_C)
4745 case SSL_SIG_ECDSA:
4746 return( POLARSSL_PK_ECDSA );
4747#endif
4748 default:
4749 return( POLARSSL_PK_NONE );
4750 }
4751}
Paul Bakker9af723c2014-05-01 13:03:14 +02004752#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004753
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004754/*
4755 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4756 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004757md_type_t ssl_md_alg_from_hash( unsigned char hash )
4758{
4759 switch( hash )
4760 {
4761#if defined(POLARSSL_MD5_C)
4762 case SSL_HASH_MD5:
4763 return( POLARSSL_MD_MD5 );
4764#endif
4765#if defined(POLARSSL_SHA1_C)
4766 case SSL_HASH_SHA1:
4767 return( POLARSSL_MD_SHA1 );
4768#endif
4769#if defined(POLARSSL_SHA256_C)
4770 case SSL_HASH_SHA224:
4771 return( POLARSSL_MD_SHA224 );
4772 case SSL_HASH_SHA256:
4773 return( POLARSSL_MD_SHA256 );
4774#endif
4775#if defined(POLARSSL_SHA512_C)
4776 case SSL_HASH_SHA384:
4777 return( POLARSSL_MD_SHA384 );
4778 case SSL_HASH_SHA512:
4779 return( POLARSSL_MD_SHA512 );
4780#endif
4781 default:
4782 return( POLARSSL_MD_NONE );
4783 }
4784}
4785
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004786#if defined(POLARSSL_SSL_SET_CURVES)
4787/*
4788 * Check is a curve proposed by the peer is in our list.
4789 * Return 1 if we're willing to use it, 0 otherwise.
4790 */
4791int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4792{
4793 const ecp_group_id *gid;
4794
4795 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4796 if( *gid == grp_id )
4797 return( 1 );
4798
4799 return( 0 );
4800}
Paul Bakker9af723c2014-05-01 13:03:14 +02004801#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004802
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004803#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004804int ssl_check_cert_usage( const x509_crt *cert,
4805 const ssl_ciphersuite_t *ciphersuite,
4806 int cert_endpoint )
4807{
4808#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4809 int usage = 0;
4810#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004811#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4812 const char *ext_oid;
4813 size_t ext_len;
4814#endif
4815
4816#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4817 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4818 ((void) cert);
4819 ((void) cert_endpoint);
4820#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004821
4822#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4823 if( cert_endpoint == SSL_IS_SERVER )
4824 {
4825 /* Server part of the key exchange */
4826 switch( ciphersuite->key_exchange )
4827 {
4828 case POLARSSL_KEY_EXCHANGE_RSA:
4829 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4830 usage = KU_KEY_ENCIPHERMENT;
4831 break;
4832
4833 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4834 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4835 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4836 usage = KU_DIGITAL_SIGNATURE;
4837 break;
4838
4839 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4840 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4841 usage = KU_KEY_AGREEMENT;
4842 break;
4843
4844 /* Don't use default: we want warnings when adding new values */
4845 case POLARSSL_KEY_EXCHANGE_NONE:
4846 case POLARSSL_KEY_EXCHANGE_PSK:
4847 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4848 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4849 usage = 0;
4850 }
4851 }
4852 else
4853 {
4854 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4855 usage = KU_DIGITAL_SIGNATURE;
4856 }
4857
4858 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4859 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004860#else
4861 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004862#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4863
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004864#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4865 if( cert_endpoint == SSL_IS_SERVER )
4866 {
4867 ext_oid = OID_SERVER_AUTH;
4868 ext_len = OID_SIZE( OID_SERVER_AUTH );
4869 }
4870 else
4871 {
4872 ext_oid = OID_CLIENT_AUTH;
4873 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4874 }
4875
4876 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4877 return( -1 );
4878#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4879
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004880 return( 0 );
4881}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004882#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004883
4884#endif /* POLARSSL_SSL_TLS_C */