blob: 19ed7c4e7cb909e95b36ea1c0ce53d1a213072c7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker05decb22013-08-15 13:33:48 +020064#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020065/*
66 * Convert max_fragment_length codes to length.
67 * RFC 6066 says:
68 * enum{
69 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
70 * } MaxFragmentLength;
71 * and we add 0 -> extension unused
72 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020073static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020074{
75 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
76 512, /* SSL_MAX_FRAG_LEN_512 */
77 1024, /* SSL_MAX_FRAG_LEN_1024 */
78 2048, /* SSL_MAX_FRAG_LEN_2048 */
79 4096, /* SSL_MAX_FRAG_LEN_4096 */
80};
Paul Bakker05decb22013-08-15 13:33:48 +020081#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020082
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020083static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
84{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020085 ssl_session_free( dst );
86 memcpy( dst, src, sizeof( ssl_session ) );
87
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089 if( src->peer_cert != NULL )
90 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020091 int ret;
92
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020093 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
94 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020095 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
96
Paul Bakkerb6b09562013-09-18 14:17:41 +020097 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098
Paul Bakkerddf26b42013-09-18 13:46:23 +020099 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
100 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101 {
102 polarssl_free( dst->peer_cert );
103 dst->peer_cert = NULL;
104 return( ret );
105 }
106 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200108
Paul Bakkera503a632013-08-14 13:48:06 +0200109#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110 if( src->ticket != NULL )
111 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200112 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
113 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200114 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
115
116 memcpy( dst->ticket, src->ticket, src->ticket_len );
117 }
Paul Bakkera503a632013-08-14 13:48:06 +0200118#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119
120 return( 0 );
121}
122
Paul Bakker05ef8352012-05-08 09:17:57 +0000123#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
124int (*ssl_hw_record_init)(ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200125 const unsigned char *key_enc, const unsigned char *key_dec,
126 size_t keylen,
127 const unsigned char *iv_enc, const unsigned char *iv_dec,
128 size_t ivlen,
129 const unsigned char *mac_enc, const unsigned char *mac_dec,
130 size_t maclen) = NULL;
Paul Bakker07eb38b2012-12-19 14:42:06 +0100131int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000132int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
133int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
134int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
135int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200136#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000137
Paul Bakker5121ce52009-01-03 21:22:43 +0000138/*
139 * Key material generation
140 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200141#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200142static int ssl3_prf( const unsigned char *secret, size_t slen,
143 const char *label,
144 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000145 unsigned char *dstbuf, size_t dlen )
146{
147 size_t i;
148 md5_context md5;
149 sha1_context sha1;
150 unsigned char padding[16];
151 unsigned char sha1sum[20];
152 ((void)label);
153
154 /*
155 * SSLv3:
156 * block =
157 * MD5( secret + SHA1( 'A' + secret + random ) ) +
158 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
159 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
160 * ...
161 */
162 for( i = 0; i < dlen / 16; i++ )
163 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200164 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000165
166 sha1_starts( &sha1 );
167 sha1_update( &sha1, padding, 1 + i );
168 sha1_update( &sha1, secret, slen );
169 sha1_update( &sha1, random, rlen );
170 sha1_finish( &sha1, sha1sum );
171
172 md5_starts( &md5 );
173 md5_update( &md5, secret, slen );
174 md5_update( &md5, sha1sum, 20 );
175 md5_finish( &md5, dstbuf + i * 16 );
176 }
177
178 memset( &md5, 0, sizeof( md5 ) );
179 memset( &sha1, 0, sizeof( sha1 ) );
180
181 memset( padding, 0, sizeof( padding ) );
182 memset( sha1sum, 0, sizeof( sha1sum ) );
183
184 return( 0 );
185}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200186#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000187
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200188#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200189static int tls1_prf( const unsigned char *secret, size_t slen,
190 const char *label,
191 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000192 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
Paul Bakker23986e52011-04-24 08:57:21 +0000194 size_t nb, hs;
195 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200196 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 unsigned char tmp[128];
198 unsigned char h_i[20];
199
200 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000201 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 hs = ( slen + 1 ) / 2;
204 S1 = secret;
205 S2 = secret + slen - hs;
206
207 nb = strlen( label );
208 memcpy( tmp + 20, label, nb );
209 memcpy( tmp + 20 + nb, random, rlen );
210 nb += rlen;
211
212 /*
213 * First compute P_md5(secret,label+random)[0..dlen]
214 */
215 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
216
217 for( i = 0; i < dlen; i += 16 )
218 {
219 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
220 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
221
222 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
223
224 for( j = 0; j < k; j++ )
225 dstbuf[i + j] = h_i[j];
226 }
227
228 /*
229 * XOR out with P_sha1(secret,label+random)[0..dlen]
230 */
231 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
232
233 for( i = 0; i < dlen; i += 20 )
234 {
235 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
236 sha1_hmac( S2, hs, tmp, 20, tmp );
237
238 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
239
240 for( j = 0; j < k; j++ )
241 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
242 }
243
244 memset( tmp, 0, sizeof( tmp ) );
245 memset( h_i, 0, sizeof( h_i ) );
246
247 return( 0 );
248}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200249#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200251#if defined(POLARSSL_SSL_PROTO_TLS1_2)
252#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200253static int tls_prf_sha256( const unsigned char *secret, size_t slen,
254 const char *label,
255 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000256 unsigned char *dstbuf, size_t dlen )
257{
258 size_t nb;
259 size_t i, j, k;
260 unsigned char tmp[128];
261 unsigned char h_i[32];
262
263 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
264 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
265
266 nb = strlen( label );
267 memcpy( tmp + 32, label, nb );
268 memcpy( tmp + 32 + nb, random, rlen );
269 nb += rlen;
270
271 /*
272 * Compute P_<hash>(secret, label + random)[0..dlen]
273 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200274 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000275
276 for( i = 0; i < dlen; i += 32 )
277 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200278 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
279 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
282
283 for( j = 0; j < k; j++ )
284 dstbuf[i + j] = h_i[j];
285 }
286
287 memset( tmp, 0, sizeof( tmp ) );
288 memset( h_i, 0, sizeof( h_i ) );
289
290 return( 0 );
291}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200292#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000293
Paul Bakker9e36f042013-06-30 14:34:05 +0200294#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200295static int tls_prf_sha384( const unsigned char *secret, size_t slen,
296 const char *label,
297 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000298 unsigned char *dstbuf, size_t dlen )
299{
300 size_t nb;
301 size_t i, j, k;
302 unsigned char tmp[128];
303 unsigned char h_i[48];
304
305 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
306 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
307
308 nb = strlen( label );
309 memcpy( tmp + 48, label, nb );
310 memcpy( tmp + 48 + nb, random, rlen );
311 nb += rlen;
312
313 /*
314 * Compute P_<hash>(secret, label + random)[0..dlen]
315 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200316 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000317
318 for( i = 0; i < dlen; i += 48 )
319 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200320 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
321 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
324
325 for( j = 0; j < k; j++ )
326 dstbuf[i + j] = h_i[j];
327 }
328
329 memset( tmp, 0, sizeof( tmp ) );
330 memset( h_i, 0, sizeof( h_i ) );
331
332 return( 0 );
333}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200334#endif /* POLARSSL_SHA512_C */
335#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200337static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338
339#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
340 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200341static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif
Paul Bakker380da532012-04-18 16:10:25 +0000343
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200344#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000345static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000346static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
348
349#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
350static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000351static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1_2)
355#if defined(POLARSSL_SHA256_C)
356static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
357static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000358static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200359#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100360
Paul Bakker9e36f042013-06-30 14:34:05 +0200361#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200362static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100363static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000364static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100365#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200366#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000367
Paul Bakker5121ce52009-01-03 21:22:43 +0000368int ssl_derive_keys( ssl_context *ssl )
369{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200370 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 unsigned char keyblk[256];
373 unsigned char *key1;
374 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100375 unsigned char *mac_enc;
376 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200377 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100378 const cipher_info_t *cipher_info;
379 const md_info_t *md_info;
380
Paul Bakker48916f92012-09-16 19:57:18 +0000381 ssl_session *session = ssl->session_negotiate;
382 ssl_transform *transform = ssl->transform_negotiate;
383 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
386
Paul Bakker68884e32013-01-07 18:20:04 +0100387 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
388 if( cipher_info == NULL )
389 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200390 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100391 transform->ciphersuite_info->cipher ) );
392 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
393 }
394
395 md_info = md_info_from_type( transform->ciphersuite_info->mac );
396 if( md_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->mac ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000405 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200406#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000407 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408 {
Paul Bakker48916f92012-09-16 19:57:18 +0000409 handshake->tls_prf = ssl3_prf;
410 handshake->calc_verify = ssl_calc_verify_ssl;
411 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200413 else
414#endif
415#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
416 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 {
Paul Bakker48916f92012-09-16 19:57:18 +0000418 handshake->tls_prf = tls1_prf;
419 handshake->calc_verify = ssl_calc_verify_tls;
420 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000421 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200422 else
423#endif
424#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200425#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
427 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = tls_prf_sha384;
430 handshake->calc_verify = ssl_calc_verify_tls_sha384;
431 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000433 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434#endif
435#if defined(POLARSSL_SHA256_C)
436 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 {
Paul Bakker48916f92012-09-16 19:57:18 +0000438 handshake->tls_prf = tls_prf_sha256;
439 handshake->calc_verify = ssl_calc_verify_tls_sha256;
440 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000441 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442 else
443#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200444#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200445 {
446 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200448 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000449
450 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 * SSLv3:
452 * master =
453 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
454 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
455 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200456 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200457 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 * master = PRF( premaster, "master secret", randbytes )[0..47]
459 */
Paul Bakker0a597072012-09-25 21:55:46 +0000460 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 {
Paul Bakker48916f92012-09-16 19:57:18 +0000462 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
463 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Paul Bakker48916f92012-09-16 19:57:18 +0000465 handshake->tls_prf( handshake->premaster, handshake->pmslen,
466 "master secret",
467 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Paul Bakker48916f92012-09-16 19:57:18 +0000469 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 }
471 else
472 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
473
474 /*
475 * Swap the client and server random values.
476 */
Paul Bakker48916f92012-09-16 19:57:18 +0000477 memcpy( tmp, handshake->randbytes, 64 );
478 memcpy( handshake->randbytes, tmp + 32, 32 );
479 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 memset( tmp, 0, sizeof( tmp ) );
481
482 /*
483 * SSLv3:
484 * key block =
485 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
486 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
487 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
488 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
489 * ...
490 *
491 * TLSv1:
492 * key block = PRF( master, "key expansion", randbytes )
493 */
Paul Bakker48916f92012-09-16 19:57:18 +0000494 handshake->tls_prf( session->master, 48, "key expansion",
495 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
Paul Bakker48916f92012-09-16 19:57:18 +0000497 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
498 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
499 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
500 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
502
Paul Bakker48916f92012-09-16 19:57:18 +0000503 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505 /*
506 * Determine the appropriate key, IV and MAC length.
507 */
Paul Bakker68884e32013-01-07 18:20:04 +0100508
509 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 {
Paul Bakker68884e32013-01-07 18:20:04 +0100511 transform->keylen = cipher_info->key_length;
512 transform->keylen /= 8;
513 transform->minlen = 1;
514 transform->ivlen = 12;
515 transform->fixed_ivlen = 4;
516 transform->maclen = 0;
517 }
518 else
519 {
520 if( md_info->type != POLARSSL_MD_NONE )
521 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200522 int ret;
523
Paul Bakker61d113b2013-07-04 11:51:43 +0200524 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
525 {
526 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
527 return( ret );
528 }
529
530 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
531 {
532 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
533 return( ret );
534 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Paul Bakker68884e32013-01-07 18:20:04 +0100536 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200537
Paul Bakker1f2bc622013-08-15 13:45:55 +0200538#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200539 /*
540 * If HMAC is to be truncated, we shall keep the leftmost bytes,
541 * (rfc 6066 page 13 or rfc 2104 section 4),
542 * so we only need to adjust the length here.
543 */
544 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
545 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200546#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Paul Bakker68884e32013-01-07 18:20:04 +0100549 transform->keylen = cipher_info->key_length;
550 transform->keylen /= 8;
551 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Paul Bakker68884e32013-01-07 18:20:04 +0100553 transform->minlen = transform->keylen;
554 if( transform->minlen < transform->maclen )
555 {
556 if( cipher_info->mode == POLARSSL_MODE_STREAM )
557 transform->minlen = transform->maclen;
558 else
559 transform->minlen += transform->keylen;
560 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 }
562
563 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000564 transform->keylen, transform->minlen, transform->ivlen,
565 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 /*
568 * Finally setup the cipher contexts, IVs and MAC secrets.
569 */
570 if( ssl->endpoint == SSL_IS_CLIENT )
571 {
Paul Bakker48916f92012-09-16 19:57:18 +0000572 key1 = keyblk + transform->maclen * 2;
573 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Paul Bakker68884e32013-01-07 18:20:04 +0100575 mac_enc = keyblk;
576 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000578 /*
579 * This is not used in TLS v1.1.
580 */
Paul Bakker48916f92012-09-16 19:57:18 +0000581 iv_copy_len = ( transform->fixed_ivlen ) ?
582 transform->fixed_ivlen : transform->ivlen;
583 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
584 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000585 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 }
587 else
588 {
Paul Bakker48916f92012-09-16 19:57:18 +0000589 key1 = keyblk + transform->maclen * 2 + transform->keylen;
590 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Paul Bakker68884e32013-01-07 18:20:04 +0100592 mac_enc = keyblk + transform->maclen;
593 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000595 /*
596 * This is not used in TLS v1.1.
597 */
Paul Bakker48916f92012-09-16 19:57:18 +0000598 iv_copy_len = ( transform->fixed_ivlen ) ?
599 transform->fixed_ivlen : transform->ivlen;
600 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
601 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000602 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 }
604
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200605#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100606 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
607 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100608 if( transform->maclen > sizeof transform->mac_enc )
609 {
610 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
611 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
612 }
613
Paul Bakker68884e32013-01-07 18:20:04 +0100614 memcpy( transform->mac_enc, mac_enc, transform->maclen );
615 memcpy( transform->mac_dec, mac_dec, transform->maclen );
616 }
617 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200618#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200619#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
620 defined(POLARSSL_SSL_PROTO_TLS1_2)
621 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100622 {
623 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
624 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
625 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200626 else
627#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200628 {
629 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200630 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200631 }
Paul Bakker68884e32013-01-07 18:20:04 +0100632
Paul Bakker05ef8352012-05-08 09:17:57 +0000633#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
634 if( ssl_hw_record_init != NULL)
635 {
636 int ret = 0;
637
638 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
639
Paul Bakker07eb38b2012-12-19 14:42:06 +0100640 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
641 transform->iv_enc, transform->iv_dec,
642 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100643 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100644 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000645 {
646 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
647 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
648 }
649 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200650#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000651
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200652 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
653 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200655 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
656 return( ret );
657 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200658
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200659 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
660 cipher_info ) ) != 0 )
661 {
662 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
663 return( ret );
664 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200665
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200666 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
667 cipher_info->key_length,
668 POLARSSL_ENCRYPT ) ) != 0 )
669 {
670 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
671 return( ret );
672 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200673
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200674 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
675 cipher_info->key_length,
676 POLARSSL_DECRYPT ) ) != 0 )
677 {
678 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
679 return( ret );
680 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200681
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200682#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200683 if( cipher_info->mode == POLARSSL_MODE_CBC )
684 {
685 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
686 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200687 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200688 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
689 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200690 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200691
692 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
693 POLARSSL_PADDING_NONE ) ) != 0 )
694 {
695 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
696 return( ret );
697 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200699#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701 memset( keyblk, 0, sizeof( keyblk ) );
702
Paul Bakker2770fbd2012-07-03 13:30:23 +0000703#if defined(POLARSSL_ZLIB_SUPPORT)
704 // Initialize compression
705 //
Paul Bakker48916f92012-09-16 19:57:18 +0000706 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000707 {
Paul Bakker16770332013-10-11 09:59:44 +0200708 if( ssl->compress_buf == NULL )
709 {
710 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
711 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
712 if( ssl->compress_buf == NULL )
713 {
714 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
715 SSL_BUFFER_LEN ) );
716 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
717 }
718 }
719
Paul Bakker2770fbd2012-07-03 13:30:23 +0000720 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
721
Paul Bakker48916f92012-09-16 19:57:18 +0000722 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
723 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000724
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200725 if( deflateInit( &transform->ctx_deflate,
726 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000727 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000728 {
729 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
730 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
731 }
732 }
733#endif /* POLARSSL_ZLIB_SUPPORT */
734
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
736
737 return( 0 );
738}
739
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200740#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000741void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742{
743 md5_context md5;
744 sha1_context sha1;
745 unsigned char pad_1[48];
746 unsigned char pad_2[48];
747
Paul Bakker380da532012-04-18 16:10:25 +0000748 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker48916f92012-09-16 19:57:18 +0000750 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
751 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Paul Bakker380da532012-04-18 16:10:25 +0000753 memset( pad_1, 0x36, 48 );
754 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakker48916f92012-09-16 19:57:18 +0000756 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000757 md5_update( &md5, pad_1, 48 );
758 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Paul Bakker380da532012-04-18 16:10:25 +0000760 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000761 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000762 md5_update( &md5, pad_2, 48 );
763 md5_update( &md5, hash, 16 );
764 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakker48916f92012-09-16 19:57:18 +0000766 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000767 sha1_update( &sha1, pad_1, 40 );
768 sha1_finish( &sha1, hash + 16 );
769
770 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000771 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000772 sha1_update( &sha1, pad_2, 40 );
773 sha1_update( &sha1, hash + 16, 20 );
774 sha1_finish( &sha1, hash + 16 );
775
776 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
777 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
778
779 return;
780}
Paul Bakker9af723c2014-05-01 13:03:14 +0200781#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000782
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000784void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
785{
786 md5_context md5;
787 sha1_context sha1;
788
789 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
790
Paul Bakker48916f92012-09-16 19:57:18 +0000791 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
792 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000793
Paul Bakker48916f92012-09-16 19:57:18 +0000794 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000795 sha1_finish( &sha1, hash + 16 );
796
797 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
798 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
799
800 return;
801}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200802#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000803
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#if defined(POLARSSL_SSL_PROTO_TLS1_2)
805#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000806void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
807{
Paul Bakker9e36f042013-06-30 14:34:05 +0200808 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000809
810 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
811
Paul Bakker9e36f042013-06-30 14:34:05 +0200812 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
813 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000814
815 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
816 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
817
818 return;
819}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000821
Paul Bakker9e36f042013-06-30 14:34:05 +0200822#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000823void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
824{
Paul Bakker9e36f042013-06-30 14:34:05 +0200825 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000826
827 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
828
Paul Bakker9e36f042013-06-30 14:34:05 +0200829 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
830 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Paul Bakkerca4ab492012-04-18 14:23:57 +0000832 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
834
835 return;
836}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200837#endif /* POLARSSL_SHA512_C */
838#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200840#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200841int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
842{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200843 unsigned char *p = ssl->handshake->premaster;
844 unsigned char *end = p + sizeof( ssl->handshake->premaster );
845
846 /*
847 * PMS = struct {
848 * opaque other_secret<0..2^16-1>;
849 * opaque psk<0..2^16-1>;
850 * };
851 * with "other_secret" depending on the particular key exchange
852 */
853#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
854 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
855 {
856 if( end - p < 2 + (int) ssl->psk_len )
857 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
858
859 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
860 *(p++) = (unsigned char)( ssl->psk_len );
861 p += ssl->psk_len;
862 }
863 else
864#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200865#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
866 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
867 {
868 /*
869 * other_secret already set by the ClientKeyExchange message,
870 * and is 48 bytes long
871 */
872 *p++ = 0;
873 *p++ = 48;
874 p += 48;
875 }
876 else
877#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200878#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
879 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
880 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200881 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200882 size_t len = ssl->handshake->dhm_ctx.len;
883
884 if( end - p < 2 + (int) len )
885 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
886
887 *(p++) = (unsigned char)( len >> 8 );
888 *(p++) = (unsigned char)( len );
889 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
890 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
891 {
892 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
893 return( ret );
894 }
895 p += len;
896
897 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
898 }
899 else
900#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
901#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
902 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
903 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200904 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200905 size_t zlen;
906
907 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
908 p + 2, end - (p + 2),
909 ssl->f_rng, ssl->p_rng ) ) != 0 )
910 {
911 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
912 return( ret );
913 }
914
915 *(p++) = (unsigned char)( zlen >> 8 );
916 *(p++) = (unsigned char)( zlen );
917 p += zlen;
918
919 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
920 }
921 else
922#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
923 {
924 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
925 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
926 }
927
928 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100929 if( end - p < 2 + (int) ssl->psk_len )
930 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
931
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
933 *(p++) = (unsigned char)( ssl->psk_len );
934 memcpy( p, ssl->psk, ssl->psk_len );
935 p += ssl->psk_len;
936
937 ssl->handshake->pmslen = p - ssl->handshake->premaster;
938
939 return( 0 );
940}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200941#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200942
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200943#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000944/*
945 * SSLv3.0 MAC functions
946 */
Paul Bakker68884e32013-01-07 18:20:04 +0100947static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
948 unsigned char *buf, size_t len,
949 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000950{
951 unsigned char header[11];
952 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100953 int padlen = 0;
954 int md_size = md_get_size( md_ctx->md_info );
955 int md_type = md_get_type( md_ctx->md_info );
956
957 if( md_type == POLARSSL_MD_MD5 )
958 padlen = 48;
959 else if( md_type == POLARSSL_MD_SHA1 )
960 padlen = 40;
961 else if( md_type == POLARSSL_MD_SHA256 )
962 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100963 else if( md_type == POLARSSL_MD_SHA384 )
964 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
966 memcpy( header, ctr, 8 );
967 header[ 8] = (unsigned char) type;
968 header[ 9] = (unsigned char)( len >> 8 );
969 header[10] = (unsigned char)( len );
970
Paul Bakker68884e32013-01-07 18:20:04 +0100971 memset( padding, 0x36, padlen );
972 md_starts( md_ctx );
973 md_update( md_ctx, secret, md_size );
974 md_update( md_ctx, padding, padlen );
975 md_update( md_ctx, header, 11 );
976 md_update( md_ctx, buf, len );
977 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Paul Bakker68884e32013-01-07 18:20:04 +0100979 memset( padding, 0x5C, padlen );
980 md_starts( md_ctx );
981 md_update( md_ctx, secret, md_size );
982 md_update( md_ctx, padding, padlen );
983 md_update( md_ctx, buf + len, md_size );
984 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000985}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200986#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000987
Paul Bakker5121ce52009-01-03 21:22:43 +0000988/*
989 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200990 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000991static int ssl_encrypt_buf( ssl_context *ssl )
992{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200993 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
996
997 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200998 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001000#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1001 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1002 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1003 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
1004 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001005 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001006#if defined(POLARSSL_SSL_PROTO_SSL3)
1007 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1008 {
1009 ssl_mac( &ssl->transform_out->md_ctx_enc,
1010 ssl->transform_out->mac_enc,
1011 ssl->out_msg, ssl->out_msglen,
1012 ssl->out_ctr, ssl->out_msgtype );
1013 }
1014 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001015#endif
1016#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001017 defined(POLARSSL_SSL_PROTO_TLS1_2)
1018 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1019 {
1020 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1021 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1022 ssl->out_msg, ssl->out_msglen );
1023 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1024 ssl->out_msg + ssl->out_msglen );
1025 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1026 }
1027 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001028#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001029 {
1030 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1031 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1032 }
1033
1034 SSL_DEBUG_BUF( 4, "computed mac",
1035 ssl->out_msg + ssl->out_msglen,
1036 ssl->transform_out->maclen );
1037
1038 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001039 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001040#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001042 /*
1043 * Encrypt
1044 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001045#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001046 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1047 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001049 int ret;
1050 size_t olen = 0;
1051
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1053 "including %d bytes of padding",
1054 ssl->out_msglen, 0 ) );
1055
1056 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1057 ssl->out_msg, ssl->out_msglen );
1058
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001059 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001060 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001061 ssl->transform_out->ivlen,
1062 ssl->out_msg, ssl->out_msglen,
1063 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001064 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001065 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001066 return( ret );
1067 }
1068
1069 if( ssl->out_msglen != olen )
1070 {
1071 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1072 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001073 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 }
Paul Bakker68884e32013-01-07 18:20:04 +01001076 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001077#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001078#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001079 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1080 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001081 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001082 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001083 unsigned char *enc_msg;
1084 unsigned char add_data[13];
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001085 int ret;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001086
Paul Bakkerca4ab492012-04-18 14:23:57 +00001087 memcpy( add_data, ssl->out_ctr, 8 );
1088 add_data[8] = ssl->out_msgtype;
1089 add_data[9] = ssl->major_ver;
1090 add_data[10] = ssl->minor_ver;
1091 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1092 add_data[12] = ssl->out_msglen & 0xFF;
1093
1094 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1095 add_data, 13 );
1096
Paul Bakker68884e32013-01-07 18:20:04 +01001097 /*
1098 * Generate IV
1099 */
1100 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001101 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1102 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001103 if( ret != 0 )
1104 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001105
Paul Bakker68884e32013-01-07 18:20:04 +01001106 memcpy( ssl->out_iv,
1107 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1108 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001109
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001110 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001111 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001112
Paul Bakker68884e32013-01-07 18:20:04 +01001113 /*
1114 * Fix pointer positions and message length with added IV
1115 */
1116 enc_msg = ssl->out_msg;
1117 enc_msglen = ssl->out_msglen;
1118 ssl->out_msglen += ssl->transform_out->ivlen -
1119 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001120
Paul Bakker68884e32013-01-07 18:20:04 +01001121 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1122 "including %d bytes of padding",
1123 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001124
Paul Bakker68884e32013-01-07 18:20:04 +01001125 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1126 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001127
Paul Bakker68884e32013-01-07 18:20:04 +01001128 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001129 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001130 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001131 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1132 ssl->transform_out->iv_enc,
1133 ssl->transform_out->ivlen,
1134 add_data, 13,
1135 enc_msg, enc_msglen,
1136 enc_msg, &olen,
1137 enc_msg + enc_msglen, 16 ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001138 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001139 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001140 return( ret );
1141 }
1142
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001143 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001144 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001145 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1146 enc_msglen, olen ) );
1147 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001148 }
1149
Paul Bakker68884e32013-01-07 18:20:04 +01001150 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001151
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001152 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 else
Paul Bakker68884e32013-01-07 18:20:04 +01001155#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001156#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1157 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001158 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1159 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001160 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001161 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001162 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001163 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001164
Paul Bakker48916f92012-09-16 19:57:18 +00001165 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1166 ssl->transform_out->ivlen;
1167 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 padlen = 0;
1169
1170 for( i = 0; i <= padlen; i++ )
1171 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1172
1173 ssl->out_msglen += padlen + 1;
1174
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001175 enc_msglen = ssl->out_msglen;
1176 enc_msg = ssl->out_msg;
1177
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001178#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001179 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001180 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1181 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001182 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001183 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001184 {
1185 /*
1186 * Generate IV
1187 */
Paul Bakker48916f92012-09-16 19:57:18 +00001188 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1189 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001190 if( ret != 0 )
1191 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001192
Paul Bakker92be97b2013-01-02 17:30:03 +01001193 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001194 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001195
1196 /*
1197 * Fix pointer positions and message length with added IV
1198 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001199 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001200 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001201 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001202 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001203#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001204
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001206 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001207 ssl->out_msglen, ssl->transform_out->ivlen,
1208 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001209
1210 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001211 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001212
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001213 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001214 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001215 ssl->transform_out->ivlen,
1216 enc_msg, enc_msglen,
1217 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001218 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001219 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001220 return( ret );
1221 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001222
Paul Bakkercca5b812013-08-31 17:40:26 +02001223 if( enc_msglen != olen )
1224 {
1225 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1226 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001227 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001228 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001229
1230#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001231 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1232 {
1233 /*
1234 * Save IV in SSL3 and TLS1
1235 */
1236 memcpy( ssl->transform_out->iv_enc,
1237 ssl->transform_out->cipher_ctx_enc.iv,
1238 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001240#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001242 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001243#endif /* POLARSSL_CIPHER_MODE_CBC &&
1244 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001245 {
1246 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1247 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1248 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Paul Bakkerca4ab492012-04-18 14:23:57 +00001250 for( i = 8; i > 0; i-- )
1251 if( ++ssl->out_ctr[i - 1] != 0 )
1252 break;
1253
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001254 /* The loops goes to its end iff the counter is wrapping */
1255 if( i == 0 )
1256 {
1257 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1258 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1259 }
1260
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1262
1263 return( 0 );
1264}
1265
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001266#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001267
Paul Bakker5121ce52009-01-03 21:22:43 +00001268static int ssl_decrypt_buf( ssl_context *ssl )
1269{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001270 size_t i;
1271#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1272 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1273 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1274 size_t padlen = 0, correct = 1;
1275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001276
1277 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1278
Paul Bakker48916f92012-09-16 19:57:18 +00001279 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 {
1281 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001282 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001283 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 }
1285
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001286#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001287 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1288 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001289 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001290 int ret;
1291 size_t olen = 0;
1292
Paul Bakker68884e32013-01-07 18:20:04 +01001293 padlen = 0;
1294
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001295 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001296 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001297 ssl->transform_in->ivlen,
1298 ssl->in_msg, ssl->in_msglen,
1299 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001300 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001301 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001302 return( ret );
1303 }
1304
1305 if( ssl->in_msglen != olen )
1306 {
1307 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001308 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 }
Paul Bakker68884e32013-01-07 18:20:04 +01001311 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001312#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001313#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001314 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1315 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001316 {
1317 unsigned char *dec_msg;
1318 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001319 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001320 unsigned char add_data[13];
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001321 int ret;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001322
Paul Bakker68884e32013-01-07 18:20:04 +01001323 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1324 ssl->transform_in->fixed_ivlen );
1325 dec_msglen -= 16;
1326 dec_msg = ssl->in_msg;
1327 dec_msg_result = ssl->in_msg;
1328 ssl->in_msglen = dec_msglen;
1329
1330 memcpy( add_data, ssl->in_ctr, 8 );
1331 add_data[8] = ssl->in_msgtype;
1332 add_data[9] = ssl->major_ver;
1333 add_data[10] = ssl->minor_ver;
1334 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1335 add_data[12] = ssl->in_msglen & 0xFF;
1336
1337 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1338 add_data, 13 );
1339
1340 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1341 ssl->in_iv,
1342 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1343
1344 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1345 ssl->transform_in->ivlen );
1346 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1347
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001348 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001349 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001350 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001351 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1352 ssl->transform_in->iv_dec,
1353 ssl->transform_in->ivlen,
1354 add_data, 13,
1355 dec_msg, dec_msglen,
1356 dec_msg_result, &olen,
1357 dec_msg + dec_msglen, 16 ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001358 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001359 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1360
1361 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1362 return( POLARSSL_ERR_SSL_INVALID_MAC );
1363
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001364 return( ret );
1365 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001366
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001367 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001368 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001369 SSL_DEBUG_MSG( 1, ( "total decrypted length incorrect %d %d",
1370 dec_msglen, olen ) );
1371 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001372 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001373 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 else
Paul Bakker68884e32013-01-07 18:20:04 +01001375#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001376#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1377 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001378 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1379 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 {
Paul Bakker45829992013-01-03 14:52:21 +01001381 /*
1382 * Decrypt and check the padding
1383 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001384 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001385 unsigned char *dec_msg;
1386 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001387 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001388 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001389 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001390
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 /*
Paul Bakker45829992013-01-03 14:52:21 +01001392 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 */
Paul Bakker48916f92012-09-16 19:57:18 +00001394 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 {
1396 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001397 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001398 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 }
1400
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001401#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001402 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1403 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001404#endif
Paul Bakker45829992013-01-03 14:52:21 +01001405
1406 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1407 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1408 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001409 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1410 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1411 ssl->transform_in->ivlen,
1412 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001413 return( POLARSSL_ERR_SSL_INVALID_MAC );
1414 }
1415
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001416 dec_msglen = ssl->in_msglen;
1417 dec_msg = ssl->in_msg;
1418 dec_msg_result = ssl->in_msg;
1419
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001420#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001421 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001422 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001423 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001424 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001425 {
Paul Bakker48916f92012-09-16 19:57:18 +00001426 dec_msglen -= ssl->transform_in->ivlen;
1427 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001428
Paul Bakker48916f92012-09-16 19:57:18 +00001429 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001430 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001431 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001432#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001433
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001434 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001435 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001436 ssl->transform_in->ivlen,
1437 dec_msg, dec_msglen,
1438 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001439 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001440 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001441 return( ret );
1442 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001443
Paul Bakkercca5b812013-08-31 17:40:26 +02001444 if( dec_msglen != olen )
1445 {
1446 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001447 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001448 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001449
1450#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001451 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1452 {
1453 /*
1454 * Save IV in SSL3 and TLS1
1455 */
1456 memcpy( ssl->transform_in->iv_dec,
1457 ssl->transform_in->cipher_ctx_dec.iv,
1458 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001460#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001461
1462 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001463
1464 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1465 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001466#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001467 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1468 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001469#endif
Paul Bakker45829992013-01-03 14:52:21 +01001470 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001471 correct = 0;
1472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001474#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1476 {
Paul Bakker48916f92012-09-16 19:57:18 +00001477 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001479#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1481 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001482 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001483#endif
Paul Bakker45829992013-01-03 14:52:21 +01001484 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 }
1486 }
1487 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001488#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001489#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1490 defined(POLARSSL_SSL_PROTO_TLS1_2)
1491 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 {
1493 /*
Paul Bakker45829992013-01-03 14:52:21 +01001494 * TLSv1+: always check the padding up to the first failure
1495 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001497 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001498 size_t padding_idx = ssl->in_msglen - padlen - 1;
1499
Paul Bakker956c9e02013-12-19 14:42:28 +01001500 /*
1501 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001502 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001503 *
Paul Bakker61885c72014-04-25 12:59:03 +02001504 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1505 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001506 *
1507 * In both cases we reset padding_idx to a safe value (0) to
1508 * prevent out-of-buffer reads.
1509 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001510 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001511 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1512 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001513
1514 padding_idx *= correct;
1515
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001516 for( i = 1; i <= 256; i++ )
1517 {
1518 real_count &= ( i <= padlen );
1519 pad_count += real_count *
1520 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1521 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001522
1523 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001524
Paul Bakkerd66f0702013-01-31 16:57:45 +01001525#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001526 if( padlen > 0 && correct == 0)
1527 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001528#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001529 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001531 else
1532#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1533 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001534 {
1535 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001536 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001539 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001540#endif /* POLARSSL_CIPHER_MODE_CBC &&
1541 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001542 {
1543 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1544 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1545 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001546
1547 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1548 ssl->in_msg, ssl->in_msglen );
1549
1550 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001551 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001553#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1554 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1555 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1556 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1557 POLARSSL_MODE_GCM )
1558 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001559 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1560
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001561 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001562
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001563 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1564 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001566 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001568#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001569 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1570 {
1571 ssl_mac( &ssl->transform_in->md_ctx_dec,
1572 ssl->transform_in->mac_dec,
1573 ssl->in_msg, ssl->in_msglen,
1574 ssl->in_ctr, ssl->in_msgtype );
1575 }
1576 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001577#endif /* POLARSSL_SSL_PROTO_SSL3 */
1578#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001579 defined(POLARSSL_SSL_PROTO_TLS1_2)
1580 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1581 {
1582 /*
1583 * Process MAC and always update for padlen afterwards to make
1584 * total time independent of padlen
1585 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001586 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001587 *
1588 * Known timing attacks:
1589 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1590 *
1591 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1592 * correctly. (We round down instead of up, so -56 is the correct
1593 * value for our calculations instead of -55)
1594 */
1595 size_t j, extra_run = 0;
1596 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1597 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001598
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001599 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001600
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001601 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1602 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1603 ssl->in_msglen );
1604 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1605 ssl->in_msg + ssl->in_msglen );
1606 for( j = 0; j < extra_run; j++ )
1607 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001608
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001609 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1610 }
1611 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001612#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001613 POLARSSL_SSL_PROTO_TLS1_2 */
1614 {
1615 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1616 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1617 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001619 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1620 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1621 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001623 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001624 ssl->transform_in->maclen ) != 0 )
1625 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001626#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001627 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001628#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001629 correct = 0;
1630 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001632 /*
1633 * Finally check the correct flag
1634 */
1635 if( correct == 0 )
1636 return( POLARSSL_ERR_SSL_INVALID_MAC );
1637 }
1638#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
1640 if( ssl->in_msglen == 0 )
1641 {
1642 ssl->nb_zero++;
1643
1644 /*
1645 * Three or more empty messages may be a DoS attack
1646 * (excessive CPU consumption).
1647 */
1648 if( ssl->nb_zero > 3 )
1649 {
1650 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1651 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001652 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 }
1654 }
1655 else
1656 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001657
Paul Bakker23986e52011-04-24 08:57:21 +00001658 for( i = 8; i > 0; i-- )
1659 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001660 break;
1661
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001662 /* The loops goes to its end iff the counter is wrapping */
1663 if( i == 0 )
1664 {
1665 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1666 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1667 }
1668
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1670
1671 return( 0 );
1672}
1673
Paul Bakker2770fbd2012-07-03 13:30:23 +00001674#if defined(POLARSSL_ZLIB_SUPPORT)
1675/*
1676 * Compression/decompression functions
1677 */
1678static int ssl_compress_buf( ssl_context *ssl )
1679{
1680 int ret;
1681 unsigned char *msg_post = ssl->out_msg;
1682 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001683 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001684
1685 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1686
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001687 if( len_pre == 0 )
1688 return( 0 );
1689
Paul Bakker2770fbd2012-07-03 13:30:23 +00001690 memcpy( msg_pre, ssl->out_msg, len_pre );
1691
1692 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1693 ssl->out_msglen ) );
1694
1695 SSL_DEBUG_BUF( 4, "before compression: output payload",
1696 ssl->out_msg, ssl->out_msglen );
1697
Paul Bakker48916f92012-09-16 19:57:18 +00001698 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1699 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1700 ssl->transform_out->ctx_deflate.next_out = msg_post;
1701 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001702
Paul Bakker48916f92012-09-16 19:57:18 +00001703 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001704 if( ret != Z_OK )
1705 {
1706 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1707 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1708 }
1709
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001710 ssl->out_msglen = SSL_BUFFER_LEN -
1711 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001712
Paul Bakker2770fbd2012-07-03 13:30:23 +00001713 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1714 ssl->out_msglen ) );
1715
1716 SSL_DEBUG_BUF( 4, "after compression: output payload",
1717 ssl->out_msg, ssl->out_msglen );
1718
1719 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1720
1721 return( 0 );
1722}
1723
1724static int ssl_decompress_buf( ssl_context *ssl )
1725{
1726 int ret;
1727 unsigned char *msg_post = ssl->in_msg;
1728 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001729 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001730
1731 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1732
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001733 if( len_pre == 0 )
1734 return( 0 );
1735
Paul Bakker2770fbd2012-07-03 13:30:23 +00001736 memcpy( msg_pre, ssl->in_msg, len_pre );
1737
1738 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1739 ssl->in_msglen ) );
1740
1741 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1742 ssl->in_msg, ssl->in_msglen );
1743
Paul Bakker48916f92012-09-16 19:57:18 +00001744 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1745 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1746 ssl->transform_in->ctx_inflate.next_out = msg_post;
1747 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001748
Paul Bakker48916f92012-09-16 19:57:18 +00001749 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001750 if( ret != Z_OK )
1751 {
1752 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1753 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1754 }
1755
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001756 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1757 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001758
Paul Bakker2770fbd2012-07-03 13:30:23 +00001759 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1760 ssl->in_msglen ) );
1761
1762 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1763 ssl->in_msg, ssl->in_msglen );
1764
1765 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1766
1767 return( 0 );
1768}
1769#endif /* POLARSSL_ZLIB_SUPPORT */
1770
Paul Bakker5121ce52009-01-03 21:22:43 +00001771/*
1772 * Fill the input message buffer
1773 */
Paul Bakker23986e52011-04-24 08:57:21 +00001774int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001775{
Paul Bakker23986e52011-04-24 08:57:21 +00001776 int ret;
1777 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
1779 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1780
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001781 if( nb_want > SSL_BUFFER_LEN - 8 )
1782 {
1783 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1784 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1785 }
1786
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 while( ssl->in_left < nb_want )
1788 {
1789 len = nb_want - ssl->in_left;
1790 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1791
1792 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1793 ssl->in_left, nb_want ) );
1794 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1795
Paul Bakker831a7552011-05-18 13:32:51 +00001796 if( ret == 0 )
1797 return( POLARSSL_ERR_SSL_CONN_EOF );
1798
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 if( ret < 0 )
1800 return( ret );
1801
1802 ssl->in_left += ret;
1803 }
1804
1805 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1806
1807 return( 0 );
1808}
1809
1810/*
1811 * Flush any data not yet written
1812 */
1813int ssl_flush_output( ssl_context *ssl )
1814{
1815 int ret;
1816 unsigned char *buf;
1817
1818 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1819
1820 while( ssl->out_left > 0 )
1821 {
1822 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1823 5 + ssl->out_msglen, ssl->out_left ) );
1824
Paul Bakker5bd42292012-12-19 14:40:42 +01001825 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001827
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1829
1830 if( ret <= 0 )
1831 return( ret );
1832
1833 ssl->out_left -= ret;
1834 }
1835
1836 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1837
1838 return( 0 );
1839}
1840
1841/*
1842 * Record layer functions
1843 */
1844int ssl_write_record( ssl_context *ssl )
1845{
Paul Bakker05ef8352012-05-08 09:17:57 +00001846 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001847 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
1849 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1850
Paul Bakker5121ce52009-01-03 21:22:43 +00001851 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1852 {
1853 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1854 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1855 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1856
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001857 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1858 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 }
1860
Paul Bakker2770fbd2012-07-03 13:30:23 +00001861#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001862 if( ssl->transform_out != NULL &&
1863 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001864 {
1865 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1866 {
1867 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1868 return( ret );
1869 }
1870
1871 len = ssl->out_msglen;
1872 }
1873#endif /*POLARSSL_ZLIB_SUPPORT */
1874
Paul Bakker05ef8352012-05-08 09:17:57 +00001875#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1876 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001878 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001879
Paul Bakker05ef8352012-05-08 09:17:57 +00001880 ret = ssl_hw_record_write( ssl );
1881 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1882 {
1883 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1884 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1885 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001886
1887 if( ret == 0 )
1888 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001889 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001890#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001891 if( !done )
1892 {
1893 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1894 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1895 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1897 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001898
Paul Bakker48916f92012-09-16 19:57:18 +00001899 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001900 {
1901 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1902 {
1903 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1904 return( ret );
1905 }
1906
1907 len = ssl->out_msglen;
1908 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1909 ssl->out_hdr[4] = (unsigned char)( len );
1910 }
1911
1912 ssl->out_left = 5 + ssl->out_msglen;
1913
1914 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1915 "version = [%d:%d], msglen = %d",
1916 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1917 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1918
1919 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001920 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 }
1922
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1924 {
1925 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1926 return( ret );
1927 }
1928
1929 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1930
1931 return( 0 );
1932}
1933
1934int ssl_read_record( ssl_context *ssl )
1935{
Paul Bakker05ef8352012-05-08 09:17:57 +00001936 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001937
1938 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1939
Paul Bakker68884e32013-01-07 18:20:04 +01001940 SSL_DEBUG_BUF( 4, "input record from network",
1941 ssl->in_hdr, 5 + ssl->in_msglen );
1942
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 if( ssl->in_hslen != 0 &&
1944 ssl->in_hslen < ssl->in_msglen )
1945 {
1946 /*
1947 * Get next Handshake message in the current record
1948 */
1949 ssl->in_msglen -= ssl->in_hslen;
1950
Paul Bakker8934a982011-08-05 11:11:53 +00001951 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001952 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001953
1954 ssl->in_hslen = 4;
1955 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1956
1957 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1958 " %d, type = %d, hslen = %d",
1959 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1960
1961 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1962 {
1963 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001964 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 }
1966
1967 if( ssl->in_msglen < ssl->in_hslen )
1968 {
1969 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001970 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971 }
1972
Paul Bakker4224bc02014-04-08 14:36:50 +02001973 if( ssl->state != SSL_HANDSHAKE_OVER )
1974 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001975
1976 return( 0 );
1977 }
1978
1979 ssl->in_hslen = 0;
1980
1981 /*
1982 * Read the record header and validate it
1983 */
1984 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1985 {
1986 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1987 return( ret );
1988 }
1989
1990 ssl->in_msgtype = ssl->in_hdr[0];
1991 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
1992
1993 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
1994 "version = [%d:%d], msglen = %d",
1995 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
1996 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
1997
1998 if( ssl->in_hdr[1] != ssl->major_ver )
1999 {
2000 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002001 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 }
2003
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002004 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 {
2006 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002007 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 }
2009
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002010 /* Sanity check (outer boundaries) */
2011 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2012 {
2013 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2014 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2015 }
2016
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002018 * Make sure the message length is acceptable for the current transform
2019 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 */
Paul Bakker48916f92012-09-16 19:57:18 +00002021 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002023 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 {
2025 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002026 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 }
2028 }
2029 else
2030 {
Paul Bakker48916f92012-09-16 19:57:18 +00002031 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 {
2033 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002034 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 }
2036
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002037#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002039 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 {
2041 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002042 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002046#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2047 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 /*
2049 * TLS encrypted messages can have up to 256 bytes of padding
2050 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002051 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002052 ssl->in_msglen > ssl->transform_in->minlen +
2053 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 {
2055 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002056 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 }
2060
2061 /*
2062 * Read and optionally decrypt the message contents
2063 */
2064 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2065 {
2066 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2067 return( ret );
2068 }
2069
2070 SSL_DEBUG_BUF( 4, "input record from network",
2071 ssl->in_hdr, 5 + ssl->in_msglen );
2072
Paul Bakker05ef8352012-05-08 09:17:57 +00002073#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2074 if( ssl_hw_record_read != NULL)
2075 {
2076 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2077
2078 ret = ssl_hw_record_read( ssl );
2079 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2080 {
2081 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2082 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2083 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002084
2085 if( ret == 0 )
2086 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002087 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002088#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002089 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 {
2091 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2092 {
Paul Bakker40865c82013-01-31 17:13:13 +01002093#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2094 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2095 {
2096 ssl_send_alert_message( ssl,
2097 SSL_ALERT_LEVEL_FATAL,
2098 SSL_ALERT_MSG_BAD_RECORD_MAC );
2099 }
2100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2102 return( ret );
2103 }
2104
2105 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2106 ssl->in_msg, ssl->in_msglen );
2107
2108 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2109 {
2110 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002111 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 }
2113 }
2114
Paul Bakker2770fbd2012-07-03 13:30:23 +00002115#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002116 if( ssl->transform_in != NULL &&
2117 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002118 {
2119 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2120 {
2121 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2122 return( ret );
2123 }
2124
2125 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2126 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2127 }
2128#endif /* POLARSSL_ZLIB_SUPPORT */
2129
Paul Bakker0a925182012-04-16 06:46:41 +00002130 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2131 ssl->in_msgtype != SSL_MSG_ALERT &&
2132 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2133 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2134 {
2135 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2136
Paul Bakker48916f92012-09-16 19:57:18 +00002137 if( ( ret = ssl_send_alert_message( ssl,
2138 SSL_ALERT_LEVEL_FATAL,
2139 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002140 {
2141 return( ret );
2142 }
2143
2144 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2145 }
2146
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2148 {
2149 ssl->in_hslen = 4;
2150 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2151
2152 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2153 " %d, type = %d, hslen = %d",
2154 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2155
2156 /*
2157 * Additional checks to validate the handshake header
2158 */
2159 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2160 {
2161 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002162 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 }
2164
2165 if( ssl->in_msglen < ssl->in_hslen )
2166 {
2167 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002168 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 }
2170
Paul Bakker48916f92012-09-16 19:57:18 +00002171 if( ssl->state != SSL_HANDSHAKE_OVER )
2172 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 }
2174
2175 if( ssl->in_msgtype == SSL_MSG_ALERT )
2176 {
2177 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2178 ssl->in_msg[0], ssl->in_msg[1] ) );
2179
2180 /*
2181 * Ignore non-fatal alerts, except close_notify
2182 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002183 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002185 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2186 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002187 /**
2188 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2189 * error identifier.
2190 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002191 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 }
2193
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002194 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2195 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 {
2197 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002198 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 }
2200 }
2201
2202 ssl->in_left = 0;
2203
2204 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2205
2206 return( 0 );
2207}
2208
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002209int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2210{
2211 int ret;
2212
2213 if( ( ret = ssl_send_alert_message( ssl,
2214 SSL_ALERT_LEVEL_FATAL,
2215 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2216 {
2217 return( ret );
2218 }
2219
2220 return( 0 );
2221}
2222
Paul Bakker0a925182012-04-16 06:46:41 +00002223int ssl_send_alert_message( ssl_context *ssl,
2224 unsigned char level,
2225 unsigned char message )
2226{
2227 int ret;
2228
2229 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2230
2231 ssl->out_msgtype = SSL_MSG_ALERT;
2232 ssl->out_msglen = 2;
2233 ssl->out_msg[0] = level;
2234 ssl->out_msg[1] = message;
2235
2236 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2237 {
2238 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2239 return( ret );
2240 }
2241
2242 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2243
2244 return( 0 );
2245}
2246
Paul Bakker5121ce52009-01-03 21:22:43 +00002247/*
2248 * Handshake functions
2249 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002250#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2251 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2252 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2253 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2254 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2255 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2256 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002257int ssl_write_certificate( ssl_context *ssl )
2258{
Paul Bakkered27a042013-04-18 22:46:23 +02002259 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002260 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
2262 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2263
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002264 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002265 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2266 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002267 {
2268 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2269 ssl->state++;
2270 return( 0 );
2271 }
2272
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002273 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002274 return( ret );
2275}
2276
2277int ssl_parse_certificate( ssl_context *ssl )
2278{
2279 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2280 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2281
2282 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2283
2284 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002285 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2286 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002287 {
2288 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2289 ssl->state++;
2290 return( 0 );
2291 }
2292
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002293 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002294 return( ret );
2295}
2296#else
2297int ssl_write_certificate( ssl_context *ssl )
2298{
2299 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2300 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002301 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002302 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2303
2304 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2305
2306 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002307 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2308 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002309 {
2310 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2311 ssl->state++;
2312 return( 0 );
2313 }
2314
Paul Bakker5121ce52009-01-03 21:22:43 +00002315 if( ssl->endpoint == SSL_IS_CLIENT )
2316 {
2317 if( ssl->client_auth == 0 )
2318 {
2319 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2320 ssl->state++;
2321 return( 0 );
2322 }
2323
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002324#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 /*
2326 * If using SSLv3 and got no cert, send an Alert message
2327 * (otherwise an empty Certificate message will be sent).
2328 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002329 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2331 {
2332 ssl->out_msglen = 2;
2333 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002334 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2335 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
2337 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2338 goto write_msg;
2339 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002340#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 }
2342 else /* SSL_IS_SERVER */
2343 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002344 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 {
2346 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002347 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 }
2349 }
2350
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002351 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
2353 /*
2354 * 0 . 0 handshake type
2355 * 1 . 3 handshake length
2356 * 4 . 6 length of all certs
2357 * 7 . 9 length of cert. 1
2358 * 10 . n-1 peer certificate
2359 * n . n+2 length of cert. 2
2360 * n+3 . ... upper level cert, etc.
2361 */
2362 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002363 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
Paul Bakker29087132010-03-21 21:03:34 +00002365 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 {
2367 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002368 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 {
2370 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2371 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002372 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 }
2374
2375 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2376 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2377 ssl->out_msg[i + 2] = (unsigned char)( n );
2378
2379 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2380 i += n; crt = crt->next;
2381 }
2382
2383 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2384 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2385 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2386
2387 ssl->out_msglen = i;
2388 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2389 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2390
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002391#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002392write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002393#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
2395 ssl->state++;
2396
2397 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2398 {
2399 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2400 return( ret );
2401 }
2402
2403 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2404
Paul Bakkered27a042013-04-18 22:46:23 +02002405 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406}
2407
2408int ssl_parse_certificate( ssl_context *ssl )
2409{
Paul Bakkered27a042013-04-18 22:46:23 +02002410 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002411 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002412 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
2414 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2415
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002416 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002417 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2418 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002419 {
2420 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2421 ssl->state++;
2422 return( 0 );
2423 }
2424
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002426 ( ssl->authmode == SSL_VERIFY_NONE ||
2427 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002429 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2431 ssl->state++;
2432 return( 0 );
2433 }
2434
2435 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2436 {
2437 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2438 return( ret );
2439 }
2440
2441 ssl->state++;
2442
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002443#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 /*
2445 * Check if the client sent an empty certificate
2446 */
2447 if( ssl->endpoint == SSL_IS_SERVER &&
2448 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2449 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002450 if( ssl->in_msglen == 2 &&
2451 ssl->in_msgtype == SSL_MSG_ALERT &&
2452 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2453 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 {
2455 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2456
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002457 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2459 return( 0 );
2460 else
Paul Bakker40e46942009-01-03 21:51:57 +00002461 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 }
2463 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002464#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002466#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2467 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 if( ssl->endpoint == SSL_IS_SERVER &&
2469 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2470 {
2471 if( ssl->in_hslen == 7 &&
2472 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2473 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2474 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2475 {
2476 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2477
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002478 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002480 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 else
2482 return( 0 );
2483 }
2484 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002485#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2486 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
2488 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2489 {
2490 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002491 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 }
2493
2494 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2495 {
2496 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002497 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 }
2499
2500 /*
2501 * Same message structure as in ssl_write_certificate()
2502 */
2503 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2504
2505 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2506 {
2507 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002508 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 }
2510
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002511 /* In case we tried to reuse a session but it failed */
2512 if( ssl->session_negotiate->peer_cert != NULL )
2513 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002514 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002515 polarssl_free( ssl->session_negotiate->peer_cert );
2516 }
2517
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002518 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2519 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 {
2521 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002522 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002523 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 }
2525
Paul Bakkerb6b09562013-09-18 14:17:41 +02002526 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 i = 7;
2529
2530 while( i < ssl->in_hslen )
2531 {
2532 if( ssl->in_msg[i] != 0 )
2533 {
2534 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002535 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 }
2537
2538 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2539 | (unsigned int) ssl->in_msg[i + 2];
2540 i += 3;
2541
2542 if( n < 128 || i + n > ssl->in_hslen )
2543 {
2544 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002545 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 }
2547
Paul Bakkerddf26b42013-09-18 13:46:23 +02002548 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2549 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 if( ret != 0 )
2551 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002552 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 return( ret );
2554 }
2555
2556 i += n;
2557 }
2558
Paul Bakker48916f92012-09-16 19:57:18 +00002559 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002561 /*
2562 * On client, make sure the server cert doesn't change during renego to
2563 * avoid "triple handshake" attack: https://secure-resumption.com/
2564 */
2565 if( ssl->endpoint == SSL_IS_CLIENT &&
2566 ssl->renegotiation == SSL_RENEGOTIATION )
2567 {
2568 if( ssl->session->peer_cert == NULL )
2569 {
2570 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2571 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2572 }
2573
2574 if( ssl->session->peer_cert->raw.len !=
2575 ssl->session_negotiate->peer_cert->raw.len ||
2576 memcmp( ssl->session->peer_cert->raw.p,
2577 ssl->session_negotiate->peer_cert->raw.p,
2578 ssl->session->peer_cert->raw.len ) != 0 )
2579 {
2580 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2581 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2582 }
2583 }
2584
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 if( ssl->authmode != SSL_VERIFY_NONE )
2586 {
2587 if( ssl->ca_chain == NULL )
2588 {
2589 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002590 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 }
2592
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002593 /*
2594 * Main check: verify certificate
2595 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002596 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2597 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2598 &ssl->session_negotiate->verify_result,
2599 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
2601 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002602 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002604 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002605
2606 /*
2607 * Secondary checks: always done, but change 'ret' only if it was 0
2608 */
2609
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002610#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002611 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002612 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002613
2614 /* If certificate uses an EC key, make sure the curve is OK */
2615 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2616 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2617 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002618 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002619 if( ret == 0 )
2620 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002621 }
2622 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002623#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002625 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2626 ciphersuite_info,
2627 ! ssl->endpoint ) != 0 )
2628 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002629 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002630 if( ret == 0 )
2631 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2632 }
2633
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2635 ret = 0;
2636 }
2637
2638 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2639
2640 return( ret );
2641}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002642#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2643 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2644 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2645 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2646 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2647 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2648 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
2650int ssl_write_change_cipher_spec( ssl_context *ssl )
2651{
2652 int ret;
2653
2654 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2655
2656 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2657 ssl->out_msglen = 1;
2658 ssl->out_msg[0] = 1;
2659
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 ssl->state++;
2661
2662 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2663 {
2664 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2665 return( ret );
2666 }
2667
2668 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2669
2670 return( 0 );
2671}
2672
2673int ssl_parse_change_cipher_spec( ssl_context *ssl )
2674{
2675 int ret;
2676
2677 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2678
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2680 {
2681 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2682 return( ret );
2683 }
2684
2685 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2686 {
2687 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002688 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 }
2690
2691 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2692 {
2693 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002694 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 }
2696
2697 ssl->state++;
2698
2699 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2700
2701 return( 0 );
2702}
2703
Paul Bakker41c83d32013-03-20 14:39:14 +01002704void ssl_optimize_checksum( ssl_context *ssl,
2705 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002706{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002707 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002708
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002709#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2710 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002711 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002712 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002713 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002714#endif
2715#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2716#if defined(POLARSSL_SHA512_C)
2717 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2718 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2719 else
2720#endif
2721#if defined(POLARSSL_SHA256_C)
2722 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002723 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002724 else
2725#endif
2726#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2727 /* Should never happen */
2728 return;
Paul Bakker380da532012-04-18 16:10:25 +00002729}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002730
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002731static void ssl_update_checksum_start( ssl_context *ssl,
2732 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002733{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002734#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2735 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002736 md5_update( &ssl->handshake->fin_md5 , buf, len );
2737 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002738#endif
2739#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2740#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002741 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002742#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002743#if defined(POLARSSL_SHA512_C)
2744 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002745#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002746#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002747}
2748
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002749#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2750 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002751static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2752 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002753{
Paul Bakker48916f92012-09-16 19:57:18 +00002754 md5_update( &ssl->handshake->fin_md5 , buf, len );
2755 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002756}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002757#endif
Paul Bakker380da532012-04-18 16:10:25 +00002758
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002759#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2760#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002761static void ssl_update_checksum_sha256( ssl_context *ssl,
2762 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002763{
Paul Bakker9e36f042013-06-30 14:34:05 +02002764 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002765}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002766#endif
Paul Bakker380da532012-04-18 16:10:25 +00002767
Paul Bakker9e36f042013-06-30 14:34:05 +02002768#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002769static void ssl_update_checksum_sha384( ssl_context *ssl,
2770 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002771{
Paul Bakker9e36f042013-06-30 14:34:05 +02002772 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002773}
Paul Bakker769075d2012-11-24 11:26:46 +01002774#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002775#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002776
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002777#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002778static void ssl_calc_finished_ssl(
2779 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002780{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002781 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002782 md5_context md5;
2783 sha1_context sha1;
2784
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 unsigned char padbuf[48];
2786 unsigned char md5sum[16];
2787 unsigned char sha1sum[20];
2788
Paul Bakker48916f92012-09-16 19:57:18 +00002789 ssl_session *session = ssl->session_negotiate;
2790 if( !session )
2791 session = ssl->session;
2792
Paul Bakker1ef83d62012-04-11 12:09:53 +00002793 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2794
Paul Bakker48916f92012-09-16 19:57:18 +00002795 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2796 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
2798 /*
2799 * SSLv3:
2800 * hash =
2801 * MD5( master + pad2 +
2802 * MD5( handshake + sender + master + pad1 ) )
2803 * + SHA1( master + pad2 +
2804 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 */
2806
Paul Bakker90995b52013-06-24 19:20:35 +02002807#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002809 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002810#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
Paul Bakker90995b52013-06-24 19:20:35 +02002812#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002814 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002815#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
Paul Bakker3c2122f2013-06-24 19:03:14 +02002817 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2818 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Paul Bakker1ef83d62012-04-11 12:09:53 +00002820 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
Paul Bakker3c2122f2013-06-24 19:03:14 +02002822 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002823 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002824 md5_update( &md5, padbuf, 48 );
2825 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002826
Paul Bakker3c2122f2013-06-24 19:03:14 +02002827 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002828 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002829 sha1_update( &sha1, padbuf, 40 );
2830 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Paul Bakker1ef83d62012-04-11 12:09:53 +00002832 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Paul Bakker1ef83d62012-04-11 12:09:53 +00002834 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002835 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002836 md5_update( &md5, padbuf, 48 );
2837 md5_update( &md5, md5sum, 16 );
2838 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Paul Bakker1ef83d62012-04-11 12:09:53 +00002840 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002841 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002842 sha1_update( &sha1, padbuf , 40 );
2843 sha1_update( &sha1, sha1sum, 20 );
2844 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Paul Bakker1ef83d62012-04-11 12:09:53 +00002846 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Paul Bakker1ef83d62012-04-11 12:09:53 +00002848 memset( &md5, 0, sizeof( md5_context ) );
2849 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
2851 memset( padbuf, 0, sizeof( padbuf ) );
2852 memset( md5sum, 0, sizeof( md5sum ) );
2853 memset( sha1sum, 0, sizeof( sha1sum ) );
2854
2855 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2856}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002857#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002859#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002860static void ssl_calc_finished_tls(
2861 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002862{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002863 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002864 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002867 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Paul Bakker48916f92012-09-16 19:57:18 +00002869 ssl_session *session = ssl->session_negotiate;
2870 if( !session )
2871 session = ssl->session;
2872
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Paul Bakker48916f92012-09-16 19:57:18 +00002875 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2876 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
Paul Bakker1ef83d62012-04-11 12:09:53 +00002878 /*
2879 * TLSv1:
2880 * hash = PRF( master, finished_label,
2881 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2882 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Paul Bakker90995b52013-06-24 19:20:35 +02002884#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002885 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2886 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002887#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002888
Paul Bakker90995b52013-06-24 19:20:35 +02002889#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002890 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2891 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002892#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002893
2894 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002895 ? "client finished"
2896 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002897
2898 md5_finish( &md5, padbuf );
2899 sha1_finish( &sha1, padbuf + 16 );
2900
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002901 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002902 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002903
2904 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2905
2906 memset( &md5, 0, sizeof( md5_context ) );
2907 memset( &sha1, 0, sizeof( sha1_context ) );
2908
2909 memset( padbuf, 0, sizeof( padbuf ) );
2910
2911 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2912}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002913#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002914
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002915#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2916#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002917static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918 ssl_context *ssl, unsigned char *buf, int from )
2919{
2920 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002921 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002922 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 unsigned char padbuf[32];
2924
Paul Bakker48916f92012-09-16 19:57:18 +00002925 ssl_session *session = ssl->session_negotiate;
2926 if( !session )
2927 session = ssl->session;
2928
Paul Bakker380da532012-04-18 16:10:25 +00002929 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930
Paul Bakker9e36f042013-06-30 14:34:05 +02002931 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002932
2933 /*
2934 * TLSv1.2:
2935 * hash = PRF( master, finished_label,
2936 * Hash( handshake ) )[0.11]
2937 */
2938
Paul Bakker9e36f042013-06-30 14:34:05 +02002939#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002941 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002942#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943
2944 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002945 ? "client finished"
2946 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002947
Paul Bakker9e36f042013-06-30 14:34:05 +02002948 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002950 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002951 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952
2953 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2954
Paul Bakker9e36f042013-06-30 14:34:05 +02002955 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956
2957 memset( padbuf, 0, sizeof( padbuf ) );
2958
2959 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2960}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002961#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962
Paul Bakker9e36f042013-06-30 14:34:05 +02002963#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002964static void ssl_calc_finished_tls_sha384(
2965 ssl_context *ssl, unsigned char *buf, int from )
2966{
2967 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002968 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002969 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002970 unsigned char padbuf[48];
2971
Paul Bakker48916f92012-09-16 19:57:18 +00002972 ssl_session *session = ssl->session_negotiate;
2973 if( !session )
2974 session = ssl->session;
2975
Paul Bakker380da532012-04-18 16:10:25 +00002976 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002977
Paul Bakker9e36f042013-06-30 14:34:05 +02002978 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002979
2980 /*
2981 * TLSv1.2:
2982 * hash = PRF( master, finished_label,
2983 * Hash( handshake ) )[0.11]
2984 */
2985
Paul Bakker9e36f042013-06-30 14:34:05 +02002986#if !defined(POLARSSL_SHA512_ALT)
2987 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2988 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002989#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00002990
2991 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002992 ? "client finished"
2993 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00002994
Paul Bakker9e36f042013-06-30 14:34:05 +02002995 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002996
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002997 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002998 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002999
3000 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3001
Paul Bakker9e36f042013-06-30 14:34:05 +02003002 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003003
3004 memset( padbuf, 0, sizeof( padbuf ) );
3005
3006 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3007}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003008#endif /* POLARSSL_SHA512_C */
3009#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003010
Paul Bakker48916f92012-09-16 19:57:18 +00003011void ssl_handshake_wrapup( ssl_context *ssl )
3012{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003013 int resume = ssl->handshake->resume;
3014
Paul Bakker48916f92012-09-16 19:57:18 +00003015 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3016
3017 /*
3018 * Free our handshake params
3019 */
3020 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003021 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003022 ssl->handshake = NULL;
3023
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003024 if( ssl->renegotiation == SSL_RENEGOTIATION )
3025 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3026
Paul Bakker48916f92012-09-16 19:57:18 +00003027 /*
3028 * Switch in our now active transform context
3029 */
3030 if( ssl->transform )
3031 {
3032 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003033 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003034 }
3035 ssl->transform = ssl->transform_negotiate;
3036 ssl->transform_negotiate = NULL;
3037
Paul Bakker0a597072012-09-25 21:55:46 +00003038 if( ssl->session )
3039 {
3040 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003041 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003042 }
3043 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003044 ssl->session_negotiate = NULL;
3045
Paul Bakker0a597072012-09-25 21:55:46 +00003046 /*
3047 * Add cache entry
3048 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003049 if( ssl->f_set_cache != NULL &&
3050 ssl->session->length != 0 &&
3051 resume == 0 )
3052 {
Paul Bakker0a597072012-09-25 21:55:46 +00003053 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3054 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003055 }
Paul Bakker0a597072012-09-25 21:55:46 +00003056
Paul Bakker48916f92012-09-16 19:57:18 +00003057 ssl->state++;
3058
3059 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3060}
3061
Paul Bakker1ef83d62012-04-11 12:09:53 +00003062int ssl_write_finished( ssl_context *ssl )
3063{
3064 int ret, hash_len;
3065
3066 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3067
Paul Bakker92be97b2013-01-02 17:30:03 +01003068 /*
3069 * Set the out_msg pointer to the correct location based on IV length
3070 */
3071 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3072 {
3073 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3074 ssl->transform_negotiate->fixed_ivlen;
3075 }
3076 else
3077 ssl->out_msg = ssl->out_iv;
3078
Paul Bakker48916f92012-09-16 19:57:18 +00003079 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003080
3081 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3083
Paul Bakker48916f92012-09-16 19:57:18 +00003084 ssl->verify_data_len = hash_len;
3085 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3086
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 ssl->out_msglen = 4 + hash_len;
3088 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3089 ssl->out_msg[0] = SSL_HS_FINISHED;
3090
3091 /*
3092 * In case of session resuming, invert the client and server
3093 * ChangeCipherSpec messages order.
3094 */
Paul Bakker0a597072012-09-25 21:55:46 +00003095 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 {
3097 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003098 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 else
3100 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3101 }
3102 else
3103 ssl->state++;
3104
Paul Bakker48916f92012-09-16 19:57:18 +00003105 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003106 * Switch to our negotiated transform and session parameters for outbound
3107 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003108 */
3109 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3110 ssl->transform_out = ssl->transform_negotiate;
3111 ssl->session_out = ssl->session_negotiate;
3112 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Paul Bakker07eb38b2012-12-19 14:42:06 +01003114#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3115 if( ssl_hw_record_activate != NULL)
3116 {
3117 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3118 {
3119 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3120 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3121 }
3122 }
3123#endif
3124
Paul Bakker5121ce52009-01-03 21:22:43 +00003125 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3126 {
3127 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3128 return( ret );
3129 }
3130
3131 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3132
3133 return( 0 );
3134}
3135
3136int ssl_parse_finished( ssl_context *ssl )
3137{
Paul Bakker23986e52011-04-24 08:57:21 +00003138 int ret;
3139 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 unsigned char buf[36];
3141
3142 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3143
Paul Bakker48916f92012-09-16 19:57:18 +00003144 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Paul Bakker48916f92012-09-16 19:57:18 +00003146 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003147 * Switch to our negotiated transform and session parameters for inbound
3148 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003149 */
3150 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3151 ssl->transform_in = ssl->transform_negotiate;
3152 ssl->session_in = ssl->session_negotiate;
3153 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Paul Bakker92be97b2013-01-02 17:30:03 +01003155 /*
3156 * Set the in_msg pointer to the correct location based on IV length
3157 */
3158 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3159 {
3160 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3161 ssl->transform_negotiate->fixed_ivlen;
3162 }
3163 else
3164 ssl->in_msg = ssl->in_iv;
3165
Paul Bakker07eb38b2012-12-19 14:42:06 +01003166#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3167 if( ssl_hw_record_activate != NULL)
3168 {
3169 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3170 {
3171 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3172 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3173 }
3174 }
3175#endif
3176
Paul Bakker5121ce52009-01-03 21:22:43 +00003177 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3178 {
3179 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3180 return( ret );
3181 }
3182
3183 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3184 {
3185 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003186 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003187 }
3188
Paul Bakker1ef83d62012-04-11 12:09:53 +00003189 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003190 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3191
3192 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3193 ssl->in_hslen != 4 + hash_len )
3194 {
3195 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003196 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003197 }
3198
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003199 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 {
3201 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003202 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 }
3204
Paul Bakker48916f92012-09-16 19:57:18 +00003205 ssl->verify_data_len = hash_len;
3206 memcpy( ssl->peer_verify_data, buf, hash_len );
3207
Paul Bakker0a597072012-09-25 21:55:46 +00003208 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 {
3210 if( ssl->endpoint == SSL_IS_CLIENT )
3211 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3212
3213 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003214 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 }
3216 else
3217 ssl->state++;
3218
3219 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3220
3221 return( 0 );
3222}
3223
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003224static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003225{
3226 if( ssl->transform_negotiate )
3227 ssl_transform_free( ssl->transform_negotiate );
3228 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003229 {
3230 ssl->transform_negotiate =
3231 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003232
3233 if( ssl->transform_negotiate != NULL )
3234 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003235 }
Paul Bakker48916f92012-09-16 19:57:18 +00003236
3237 if( ssl->session_negotiate )
3238 ssl_session_free( ssl->session_negotiate );
3239 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003240 {
3241 ssl->session_negotiate =
3242 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003243
3244 if( ssl->session_negotiate != NULL )
3245 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003246 }
Paul Bakker48916f92012-09-16 19:57:18 +00003247
3248 if( ssl->handshake )
3249 ssl_handshake_free( ssl->handshake );
3250 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003251 {
3252 ssl->handshake = (ssl_handshake_params *)
3253 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003254
3255 if( ssl->handshake != NULL )
3256 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003257 }
Paul Bakker48916f92012-09-16 19:57:18 +00003258
3259 if( ssl->handshake == NULL ||
3260 ssl->transform_negotiate == NULL ||
3261 ssl->session_negotiate == NULL )
3262 {
3263 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3264 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3265 }
3266
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003267#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3268 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003269 md5_starts( &ssl->handshake->fin_md5 );
3270 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003271#endif
3272#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3273#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003274 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003275#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003276#if defined(POLARSSL_SHA512_C)
3277 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003278#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003279#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003280
3281 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003282 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003283
Paul Bakker61d113b2013-07-04 11:51:43 +02003284#if defined(POLARSSL_ECDH_C)
3285 ecdh_init( &ssl->handshake->ecdh_ctx );
3286#endif
3287
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003288#if defined(POLARSSL_X509_CRT_PARSE_C)
3289 ssl->handshake->key_cert = ssl->key_cert;
3290#endif
3291
Paul Bakker48916f92012-09-16 19:57:18 +00003292 return( 0 );
3293}
3294
Paul Bakker5121ce52009-01-03 21:22:43 +00003295/*
3296 * Initialize an SSL context
3297 */
3298int ssl_init( ssl_context *ssl )
3299{
Paul Bakker48916f92012-09-16 19:57:18 +00003300 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 int len = SSL_BUFFER_LEN;
3302
3303 memset( ssl, 0, sizeof( ssl_context ) );
3304
Paul Bakker62f2dee2012-09-28 07:31:51 +00003305 /*
3306 * Sane defaults
3307 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003308 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3309 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3310 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3311 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003312
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003313 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003314
Paul Bakker62f2dee2012-09-28 07:31:51 +00003315#if defined(POLARSSL_DHM_C)
3316 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3317 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3318 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3319 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3320 {
3321 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3322 return( ret );
3323 }
3324#endif
3325
3326 /*
3327 * Prepare base structures
3328 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003329 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003331 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 ssl->in_msg = ssl->in_ctr + 13;
3333
3334 if( ssl->in_ctr == NULL )
3335 {
3336 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003337 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003338 }
3339
Paul Bakker6e339b52013-07-03 13:37:05 +02003340 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003341 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003342 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003343 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003344
3345 if( ssl->out_ctr == NULL )
3346 {
3347 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003348 polarssl_free( ssl->in_ctr );
3349 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003350 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 }
3352
3353 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3354 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3355
Paul Bakker606b4ba2013-08-14 16:52:14 +02003356#if defined(POLARSSL_SSL_SESSION_TICKETS)
3357 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3358#endif
3359
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003360#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003361 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003362#endif
3363
Paul Bakker48916f92012-09-16 19:57:18 +00003364 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3365 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366
3367 return( 0 );
3368}
3369
3370/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003371 * Reset an initialized and used SSL context for re-use while retaining
3372 * all application-set variables, function pointers and data.
3373 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003374int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003375{
Paul Bakker48916f92012-09-16 19:57:18 +00003376 int ret;
3377
Paul Bakker7eb013f2011-10-06 12:37:39 +00003378 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003379 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3380 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3381
3382 ssl->verify_data_len = 0;
3383 memset( ssl->own_verify_data, 0, 36 );
3384 memset( ssl->peer_verify_data, 0, 36 );
3385
Paul Bakker7eb013f2011-10-06 12:37:39 +00003386 ssl->in_offt = NULL;
3387
Paul Bakker92be97b2013-01-02 17:30:03 +01003388 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003389 ssl->in_msgtype = 0;
3390 ssl->in_msglen = 0;
3391 ssl->in_left = 0;
3392
3393 ssl->in_hslen = 0;
3394 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003395 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003396
Paul Bakker92be97b2013-01-02 17:30:03 +01003397 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003398 ssl->out_msgtype = 0;
3399 ssl->out_msglen = 0;
3400 ssl->out_left = 0;
3401
Paul Bakker48916f92012-09-16 19:57:18 +00003402 ssl->transform_in = NULL;
3403 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003404
3405 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3406 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003407
3408#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3409 if( ssl_hw_record_reset != NULL)
3410 {
3411 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003412 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003413 {
3414 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3415 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3416 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003417 }
3418#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003419
Paul Bakker48916f92012-09-16 19:57:18 +00003420 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003421 {
Paul Bakker48916f92012-09-16 19:57:18 +00003422 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003423 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003424 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003425 }
Paul Bakker48916f92012-09-16 19:57:18 +00003426
Paul Bakkerc0463502013-02-14 11:19:38 +01003427 if( ssl->session )
3428 {
3429 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003430 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003431 ssl->session = NULL;
3432 }
3433
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003434#if defined(POLARSSL_SSL_ALPN)
3435 ssl->alpn_chosen = NULL;
3436#endif
3437
Paul Bakker48916f92012-09-16 19:57:18 +00003438 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3439 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003440
3441 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003442}
3443
Paul Bakkera503a632013-08-14 13:48:06 +02003444#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003445/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003446 * Allocate and initialize ticket keys
3447 */
3448static int ssl_ticket_keys_init( ssl_context *ssl )
3449{
3450 int ret;
3451 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003452 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003453
3454 if( ssl->ticket_keys != NULL )
3455 return( 0 );
3456
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003457 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3458 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003459 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3460
3461 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003462 {
3463 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003464 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003465 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003466
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003467 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3468 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3469 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3470 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003471 polarssl_free( tkeys );
3472 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003473 }
3474
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003475 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003476 {
3477 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003478 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003479 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003480
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003481 ssl->ticket_keys = tkeys;
3482
3483 return( 0 );
3484}
Paul Bakkera503a632013-08-14 13:48:06 +02003485#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003486
3487/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003488 * SSL set accessors
3489 */
3490void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3491{
3492 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003493
Paul Bakker606b4ba2013-08-14 16:52:14 +02003494#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003495 if( endpoint == SSL_IS_CLIENT )
3496 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003498}
3499
3500void ssl_set_authmode( ssl_context *ssl, int authmode )
3501{
3502 ssl->authmode = authmode;
3503}
3504
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003505#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003506void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003507 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003508 void *p_vrfy )
3509{
3510 ssl->f_vrfy = f_vrfy;
3511 ssl->p_vrfy = p_vrfy;
3512}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003513#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003514
Paul Bakker5121ce52009-01-03 21:22:43 +00003515void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003516 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003517 void *p_rng )
3518{
3519 ssl->f_rng = f_rng;
3520 ssl->p_rng = p_rng;
3521}
3522
3523void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003524 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003525 void *p_dbg )
3526{
3527 ssl->f_dbg = f_dbg;
3528 ssl->p_dbg = p_dbg;
3529}
3530
3531void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003532 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003533 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003534{
3535 ssl->f_recv = f_recv;
3536 ssl->f_send = f_send;
3537 ssl->p_recv = p_recv;
3538 ssl->p_send = p_send;
3539}
3540
Paul Bakker0a597072012-09-25 21:55:46 +00003541void ssl_set_session_cache( ssl_context *ssl,
3542 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3543 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003544{
Paul Bakker0a597072012-09-25 21:55:46 +00003545 ssl->f_get_cache = f_get_cache;
3546 ssl->p_get_cache = p_get_cache;
3547 ssl->f_set_cache = f_set_cache;
3548 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003549}
3550
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003551int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003552{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003553 int ret;
3554
3555 if( ssl == NULL ||
3556 session == NULL ||
3557 ssl->session_negotiate == NULL ||
3558 ssl->endpoint != SSL_IS_CLIENT )
3559 {
3560 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3561 }
3562
3563 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3564 return( ret );
3565
Paul Bakker0a597072012-09-25 21:55:46 +00003566 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003567
3568 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003569}
3570
Paul Bakkerb68cad62012-08-23 08:34:18 +00003571void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003572{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003573 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3574 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3575 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3576 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3577}
3578
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003579void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3580 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003581 int major, int minor )
3582{
3583 if( major != SSL_MAJOR_VERSION_3 )
3584 return;
3585
3586 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3587 return;
3588
3589 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003590}
3591
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003592#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003593/* Add a new (empty) key_cert entry an return a pointer to it */
3594static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3595{
3596 ssl_key_cert *key_cert, *last;
3597
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003598 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3599 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003600 return( NULL );
3601
3602 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3603
3604 /* Append the new key_cert to the (possibly empty) current list */
3605 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003606 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003607 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003608 if( ssl->handshake != NULL )
3609 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003610 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003611 else
3612 {
3613 last = ssl->key_cert;
3614 while( last->next != NULL )
3615 last = last->next;
3616 last->next = key_cert;
3617 }
3618
3619 return key_cert;
3620}
3621
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003622void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003623 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003624{
3625 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003626 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003627 ssl->peer_cn = peer_cn;
3628}
3629
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003630int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003631 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003632{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003633 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3634
3635 if( key_cert == NULL )
3636 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3637
3638 key_cert->cert = own_cert;
3639 key_cert->key = pk_key;
3640
3641 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003642}
3643
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003644#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003645int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003646 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003647{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003648 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003649 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003650
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003651 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003652 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3653
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003654 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3655 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003656 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003657
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003658 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003659
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003660 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003661 if( ret != 0 )
3662 return( ret );
3663
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003664 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003665 return( ret );
3666
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003667 key_cert->cert = own_cert;
3668 key_cert->key_own_alloc = 1;
3669
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003670 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003671}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003672#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003673
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003674int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003675 void *rsa_key,
3676 rsa_decrypt_func rsa_decrypt,
3677 rsa_sign_func rsa_sign,
3678 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003679{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003680 int ret;
3681 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003682
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003683 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003684 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3685
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003686 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3687 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003688 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003689
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003690 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003691
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003692 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3693 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3694 return( ret );
3695
3696 key_cert->cert = own_cert;
3697 key_cert->key_own_alloc = 1;
3698
3699 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003700}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003701#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003702
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003703#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003704int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3705 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003706{
Paul Bakker6db455e2013-09-18 17:29:31 +02003707 if( psk == NULL || psk_identity == NULL )
3708 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3709
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003710 /*
3711 * The length will be check later anyway, but in case it is obviously
3712 * too large, better abort now. The PMS is as follows:
3713 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3714 */
3715 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3716 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3717
Paul Bakker6db455e2013-09-18 17:29:31 +02003718 if( ssl->psk != NULL )
3719 {
3720 polarssl_free( ssl->psk );
3721 polarssl_free( ssl->psk_identity );
3722 }
3723
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003724 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003725 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003726
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003727 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003728 ssl->psk_identity = (unsigned char *)
3729 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003730
3731 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3732 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3733
3734 memcpy( ssl->psk, psk, ssl->psk_len );
3735 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003736
3737 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003738}
3739
3740void ssl_set_psk_cb( ssl_context *ssl,
3741 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3742 size_t),
3743 void *p_psk )
3744{
3745 ssl->f_psk = f_psk;
3746 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003747}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003748#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003749
Paul Bakker48916f92012-09-16 19:57:18 +00003750#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003751int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003752{
3753 int ret;
3754
Paul Bakker48916f92012-09-16 19:57:18 +00003755 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003756 {
3757 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3758 return( ret );
3759 }
3760
Paul Bakker48916f92012-09-16 19:57:18 +00003761 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003762 {
3763 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3764 return( ret );
3765 }
3766
3767 return( 0 );
3768}
3769
Paul Bakker1b57b062011-01-06 15:48:19 +00003770int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3771{
3772 int ret;
3773
Paul Bakker48916f92012-09-16 19:57:18 +00003774 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003775 {
3776 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3777 return( ret );
3778 }
3779
Paul Bakker48916f92012-09-16 19:57:18 +00003780 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003781 {
3782 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3783 return( ret );
3784 }
3785
3786 return( 0 );
3787}
Paul Bakker48916f92012-09-16 19:57:18 +00003788#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003789
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003790#if defined(POLARSSL_SSL_SET_CURVES)
3791/*
3792 * Set the allowed elliptic curves
3793 */
3794void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3795{
3796 ssl->curve_list = curve_list;
3797}
3798#endif
3799
Paul Bakker0be444a2013-08-27 21:55:01 +02003800#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003801int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003802{
3803 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003804 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003805
3806 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003807
3808 if( ssl->hostname_len + 1 == 0 )
3809 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3810
Paul Bakker6e339b52013-07-03 13:37:05 +02003811 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003812
Paul Bakkerb15b8512012-01-13 13:44:06 +00003813 if( ssl->hostname == NULL )
3814 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3815
Paul Bakker3c2122f2013-06-24 19:03:14 +02003816 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003817 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003818
Paul Bakker40ea7de2009-05-03 10:18:48 +00003819 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003820
3821 return( 0 );
3822}
3823
Paul Bakker5701cdc2012-09-27 21:49:42 +00003824void ssl_set_sni( ssl_context *ssl,
3825 int (*f_sni)(void *, ssl_context *,
3826 const unsigned char *, size_t),
3827 void *p_sni )
3828{
3829 ssl->f_sni = f_sni;
3830 ssl->p_sni = p_sni;
3831}
Paul Bakker0be444a2013-08-27 21:55:01 +02003832#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003833
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003834#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003835int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003836{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003837 size_t cur_len, tot_len;
3838 const char **p;
3839
3840 /*
3841 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3842 * truncated". Check lengths now rather than later.
3843 */
3844 tot_len = 0;
3845 for( p = protos; *p != NULL; p++ )
3846 {
3847 cur_len = strlen( *p );
3848 tot_len += cur_len;
3849
3850 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3851 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3852 }
3853
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003854 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003855
3856 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003857}
3858
3859const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3860{
3861 return ssl->alpn_chosen;
3862}
3863#endif /* POLARSSL_SSL_ALPN */
3864
Paul Bakker490ecc82011-10-06 13:04:09 +00003865void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3866{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003867 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3868 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3869 {
3870 ssl->max_major_ver = major;
3871 ssl->max_minor_ver = minor;
3872 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003873}
3874
Paul Bakker1d29fb52012-09-28 13:28:45 +00003875void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3876{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003877 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3878 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3879 {
3880 ssl->min_major_ver = major;
3881 ssl->min_minor_ver = minor;
3882 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003883}
3884
Paul Bakker05decb22013-08-15 13:33:48 +02003885#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003886int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3887{
Paul Bakker77e257e2013-12-16 15:29:52 +01003888 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003889 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003890 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003891 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003892 }
3893
3894 ssl->mfl_code = mfl_code;
3895
3896 return( 0 );
3897}
Paul Bakker05decb22013-08-15 13:33:48 +02003898#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003899
Paul Bakker1f2bc622013-08-15 13:45:55 +02003900#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003901int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003902{
3903 if( ssl->endpoint != SSL_IS_CLIENT )
3904 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3905
Paul Bakker8c1ede62013-07-19 14:14:37 +02003906 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003907
3908 return( 0 );
3909}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003910#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003911
Paul Bakker48916f92012-09-16 19:57:18 +00003912void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3913{
3914 ssl->disable_renegotiation = renegotiation;
3915}
3916
3917void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3918{
3919 ssl->allow_legacy_renegotiation = allow_legacy;
3920}
3921
Paul Bakkera503a632013-08-14 13:48:06 +02003922#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003923int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3924{
3925 ssl->session_tickets = use_tickets;
3926
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003927 if( ssl->endpoint == SSL_IS_CLIENT )
3928 return( 0 );
3929
3930 if( ssl->f_rng == NULL )
3931 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3932
3933 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003934}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003935
3936void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3937{
3938 ssl->ticket_lifetime = lifetime;
3939}
Paul Bakkera503a632013-08-14 13:48:06 +02003940#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003941
Paul Bakker5121ce52009-01-03 21:22:43 +00003942/*
3943 * SSL get accessors
3944 */
Paul Bakker23986e52011-04-24 08:57:21 +00003945size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003946{
3947 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3948}
3949
Paul Bakkerff60ee62010-03-16 21:09:09 +00003950int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003951{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003952 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003953}
3954
Paul Bakkere3166ce2011-01-27 17:40:50 +00003955const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003956{
Paul Bakker926c8e42013-03-06 10:23:34 +01003957 if( ssl == NULL || ssl->session == NULL )
3958 return NULL;
3959
Paul Bakkere3166ce2011-01-27 17:40:50 +00003960 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003961}
3962
Paul Bakker43ca69c2011-01-15 17:35:19 +00003963const char *ssl_get_version( const ssl_context *ssl )
3964{
3965 switch( ssl->minor_ver )
3966 {
3967 case SSL_MINOR_VERSION_0:
3968 return( "SSLv3.0" );
3969
3970 case SSL_MINOR_VERSION_1:
3971 return( "TLSv1.0" );
3972
3973 case SSL_MINOR_VERSION_2:
3974 return( "TLSv1.1" );
3975
Paul Bakker1ef83d62012-04-11 12:09:53 +00003976 case SSL_MINOR_VERSION_3:
3977 return( "TLSv1.2" );
3978
Paul Bakker43ca69c2011-01-15 17:35:19 +00003979 default:
3980 break;
3981 }
3982 return( "unknown" );
3983}
3984
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003985#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003986const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003987{
3988 if( ssl == NULL || ssl->session == NULL )
3989 return NULL;
3990
3991 return ssl->session->peer_cert;
3992}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003993#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003994
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003995int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3996{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003997 if( ssl == NULL ||
3998 dst == NULL ||
3999 ssl->session == NULL ||
4000 ssl->endpoint != SSL_IS_CLIENT )
4001 {
4002 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4003 }
4004
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004005 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004006}
4007
Paul Bakker5121ce52009-01-03 21:22:43 +00004008/*
Paul Bakker1961b702013-01-25 14:49:24 +01004009 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 */
Paul Bakker1961b702013-01-25 14:49:24 +01004011int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004012{
Paul Bakker40e46942009-01-03 21:51:57 +00004013 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004014
Paul Bakker40e46942009-01-03 21:51:57 +00004015#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004016 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004017 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004018#endif
4019
Paul Bakker40e46942009-01-03 21:51:57 +00004020#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004021 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004022 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004023#endif
4024
Paul Bakker1961b702013-01-25 14:49:24 +01004025 return( ret );
4026}
4027
4028/*
4029 * Perform the SSL handshake
4030 */
4031int ssl_handshake( ssl_context *ssl )
4032{
4033 int ret = 0;
4034
4035 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4036
4037 while( ssl->state != SSL_HANDSHAKE_OVER )
4038 {
4039 ret = ssl_handshake_step( ssl );
4040
4041 if( ret != 0 )
4042 break;
4043 }
4044
Paul Bakker5121ce52009-01-03 21:22:43 +00004045 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4046
4047 return( ret );
4048}
4049
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004050#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004051/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004052 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004053 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004054static int ssl_write_hello_request( ssl_context *ssl )
4055{
4056 int ret;
4057
4058 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4059
4060 ssl->out_msglen = 4;
4061 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4062 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4063
4064 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4065 {
4066 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4067 return( ret );
4068 }
4069
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004070 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4071
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004072 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4073
4074 return( 0 );
4075}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004076#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004077
4078/*
4079 * Actually renegotiate current connection, triggered by either:
4080 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004081 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004082 * - receiving any handshake message on server during ssl_read() after the
4083 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004084 * If the handshake doesn't complete due to waiting for I/O, it will continue
4085 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004086 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004087static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004088{
4089 int ret;
4090
4091 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4092
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004093 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4094 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004095
4096 ssl->state = SSL_HELLO_REQUEST;
4097 ssl->renegotiation = SSL_RENEGOTIATION;
4098
Paul Bakker48916f92012-09-16 19:57:18 +00004099 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4100 {
4101 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4102 return( ret );
4103 }
4104
4105 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4106
4107 return( 0 );
4108}
4109
4110/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004111 * Renegotiate current connection on client,
4112 * or request renegotiation on server
4113 */
4114int ssl_renegotiate( ssl_context *ssl )
4115{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004116 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004117
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004118#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004119 /* On server, just send the request */
4120 if( ssl->endpoint == SSL_IS_SERVER )
4121 {
4122 if( ssl->state != SSL_HANDSHAKE_OVER )
4123 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4124
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004125 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004126 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004127#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004128
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004129#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004130 /*
4131 * On client, either start the renegotiation process or,
4132 * if already in progress, continue the handshake
4133 */
4134 if( ssl->renegotiation != SSL_RENEGOTIATION )
4135 {
4136 if( ssl->state != SSL_HANDSHAKE_OVER )
4137 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4138
4139 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4140 {
4141 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4142 return( ret );
4143 }
4144 }
4145 else
4146 {
4147 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4148 {
4149 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4150 return( ret );
4151 }
4152 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004153#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004154
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004155 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004156}
4157
4158/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004159 * Receive application data decrypted from the SSL layer
4160 */
Paul Bakker23986e52011-04-24 08:57:21 +00004161int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004162{
Paul Bakker23986e52011-04-24 08:57:21 +00004163 int ret;
4164 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004165
4166 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4167
4168 if( ssl->state != SSL_HANDSHAKE_OVER )
4169 {
4170 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4171 {
4172 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4173 return( ret );
4174 }
4175 }
4176
4177 if( ssl->in_offt == NULL )
4178 {
4179 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4180 {
Paul Bakker831a7552011-05-18 13:32:51 +00004181 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4182 return( 0 );
4183
Paul Bakker5121ce52009-01-03 21:22:43 +00004184 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4185 return( ret );
4186 }
4187
4188 if( ssl->in_msglen == 0 &&
4189 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4190 {
4191 /*
4192 * OpenSSL sends empty messages to randomize the IV
4193 */
4194 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4195 {
Paul Bakker831a7552011-05-18 13:32:51 +00004196 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4197 return( 0 );
4198
Paul Bakker5121ce52009-01-03 21:22:43 +00004199 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4200 return( ret );
4201 }
4202 }
4203
Paul Bakker48916f92012-09-16 19:57:18 +00004204 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4205 {
4206 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4207
4208 if( ssl->endpoint == SSL_IS_CLIENT &&
4209 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4210 ssl->in_hslen != 4 ) )
4211 {
4212 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4213 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4214 }
4215
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004216 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4217 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004218 ssl->allow_legacy_renegotiation ==
4219 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004220 {
4221 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4222
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004223#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004224 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004225 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004226 /*
4227 * SSLv3 does not have a "no_renegotiation" alert
4228 */
4229 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4230 return( ret );
4231 }
4232 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004233#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004234#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4235 defined(POLARSSL_SSL_PROTO_TLS1_2)
4236 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004237 {
4238 if( ( ret = ssl_send_alert_message( ssl,
4239 SSL_ALERT_LEVEL_WARNING,
4240 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4241 {
4242 return( ret );
4243 }
Paul Bakker48916f92012-09-16 19:57:18 +00004244 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004245 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004246#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4247 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004248 {
4249 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004250 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004251 }
Paul Bakker48916f92012-09-16 19:57:18 +00004252 }
4253 else
4254 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004255 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004256 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004257 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004258 return( ret );
4259 }
4260
4261 return( POLARSSL_ERR_NET_WANT_READ );
4262 }
4263 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004264 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4265 {
4266 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4267 "but not honored by client" ) );
4268 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4269 }
Paul Bakker48916f92012-09-16 19:57:18 +00004270 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004271 {
4272 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004273 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004274 }
4275
4276 ssl->in_offt = ssl->in_msg;
4277 }
4278
4279 n = ( len < ssl->in_msglen )
4280 ? len : ssl->in_msglen;
4281
4282 memcpy( buf, ssl->in_offt, n );
4283 ssl->in_msglen -= n;
4284
4285 if( ssl->in_msglen == 0 )
4286 /* all bytes consumed */
4287 ssl->in_offt = NULL;
4288 else
4289 /* more data available */
4290 ssl->in_offt += n;
4291
4292 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4293
Paul Bakker23986e52011-04-24 08:57:21 +00004294 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004295}
4296
4297/*
4298 * Send application data to be encrypted by the SSL layer
4299 */
Paul Bakker23986e52011-04-24 08:57:21 +00004300int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004301{
Paul Bakker23986e52011-04-24 08:57:21 +00004302 int ret;
4303 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004304 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004305
4306 SSL_DEBUG_MSG( 2, ( "=> write" ) );
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
Paul Bakker05decb22013-08-15 13:33:48 +02004317#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004318 /*
4319 * Assume mfl_code is correct since it was checked when set
4320 */
4321 max_len = mfl_code_to_length[ssl->mfl_code];
4322
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004323 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004324 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004325 */
4326 if( ssl->session_out != NULL &&
4327 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4328 {
4329 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4330 }
Paul Bakker05decb22013-08-15 13:33:48 +02004331#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004332
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004333 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004334
Paul Bakker5121ce52009-01-03 21:22:43 +00004335 if( ssl->out_left != 0 )
4336 {
4337 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4338 {
4339 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4340 return( ret );
4341 }
4342 }
Paul Bakker887bd502011-06-08 13:10:54 +00004343 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004344 {
Paul Bakker887bd502011-06-08 13:10:54 +00004345 ssl->out_msglen = n;
4346 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4347 memcpy( ssl->out_msg, buf, n );
4348
4349 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4350 {
4351 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4352 return( ret );
4353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004354 }
4355
4356 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4357
Paul Bakker23986e52011-04-24 08:57:21 +00004358 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004359}
4360
4361/*
4362 * Notify the peer that the connection is being closed
4363 */
4364int ssl_close_notify( ssl_context *ssl )
4365{
4366 int ret;
4367
4368 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4369
4370 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4371 {
4372 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4373 return( ret );
4374 }
4375
4376 if( ssl->state == SSL_HANDSHAKE_OVER )
4377 {
Paul Bakker48916f92012-09-16 19:57:18 +00004378 if( ( ret = ssl_send_alert_message( ssl,
4379 SSL_ALERT_LEVEL_WARNING,
4380 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004381 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004382 return( ret );
4383 }
4384 }
4385
4386 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4387
4388 return( ret );
4389}
4390
Paul Bakker48916f92012-09-16 19:57:18 +00004391void ssl_transform_free( ssl_transform *transform )
4392{
4393#if defined(POLARSSL_ZLIB_SUPPORT)
4394 deflateEnd( &transform->ctx_deflate );
4395 inflateEnd( &transform->ctx_inflate );
4396#endif
4397
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004398 cipher_free_ctx( &transform->cipher_ctx_enc );
4399 cipher_free_ctx( &transform->cipher_ctx_dec );
4400
Paul Bakker61d113b2013-07-04 11:51:43 +02004401 md_free_ctx( &transform->md_ctx_enc );
4402 md_free_ctx( &transform->md_ctx_dec );
4403
Paul Bakker48916f92012-09-16 19:57:18 +00004404 memset( transform, 0, sizeof( ssl_transform ) );
4405}
4406
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004407#if defined(POLARSSL_X509_CRT_PARSE_C)
4408static void ssl_key_cert_free( ssl_key_cert *key_cert )
4409{
4410 ssl_key_cert *cur = key_cert, *next;
4411
4412 while( cur != NULL )
4413 {
4414 next = cur->next;
4415
4416 if( cur->key_own_alloc )
4417 {
4418 pk_free( cur->key );
4419 polarssl_free( cur->key );
4420 }
4421 polarssl_free( cur );
4422
4423 cur = next;
4424 }
4425}
4426#endif /* POLARSSL_X509_CRT_PARSE_C */
4427
Paul Bakker48916f92012-09-16 19:57:18 +00004428void ssl_handshake_free( ssl_handshake_params *handshake )
4429{
4430#if defined(POLARSSL_DHM_C)
4431 dhm_free( &handshake->dhm_ctx );
4432#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004433#if defined(POLARSSL_ECDH_C)
4434 ecdh_free( &handshake->ecdh_ctx );
4435#endif
4436
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004437#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004438 /* explicit void pointer cast for buggy MS compiler */
4439 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004440#endif
4441
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004442#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4443 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4444 /*
4445 * Free only the linked list wrapper, not the keys themselves
4446 * since the belong to the SNI callback
4447 */
4448 if( handshake->sni_key_cert != NULL )
4449 {
4450 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4451
4452 while( cur != NULL )
4453 {
4454 next = cur->next;
4455 polarssl_free( cur );
4456 cur = next;
4457 }
4458 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004459#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004460
Paul Bakker48916f92012-09-16 19:57:18 +00004461 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4462}
4463
4464void ssl_session_free( ssl_session *session )
4465{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004466#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004467 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004468 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004469 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004470 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004471 }
Paul Bakkered27a042013-04-18 22:46:23 +02004472#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004473
Paul Bakkera503a632013-08-14 13:48:06 +02004474#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004475 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004476#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004477
Paul Bakker0a597072012-09-25 21:55:46 +00004478 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004479}
4480
Paul Bakker5121ce52009-01-03 21:22:43 +00004481/*
4482 * Free an SSL context
4483 */
4484void ssl_free( ssl_context *ssl )
4485{
4486 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4487
Paul Bakker5121ce52009-01-03 21:22:43 +00004488 if( ssl->out_ctr != NULL )
4489 {
4490 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004491 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004492 }
4493
4494 if( ssl->in_ctr != NULL )
4495 {
4496 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004497 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 }
4499
Paul Bakker16770332013-10-11 09:59:44 +02004500#if defined(POLARSSL_ZLIB_SUPPORT)
4501 if( ssl->compress_buf != NULL )
4502 {
4503 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4504 polarssl_free( ssl->compress_buf );
4505 }
4506#endif
4507
Paul Bakker40e46942009-01-03 21:51:57 +00004508#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004509 mpi_free( &ssl->dhm_P );
4510 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004511#endif
4512
Paul Bakker48916f92012-09-16 19:57:18 +00004513 if( ssl->transform )
4514 {
4515 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004516 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004517 }
4518
4519 if( ssl->handshake )
4520 {
4521 ssl_handshake_free( ssl->handshake );
4522 ssl_transform_free( ssl->transform_negotiate );
4523 ssl_session_free( ssl->session_negotiate );
4524
Paul Bakker6e339b52013-07-03 13:37:05 +02004525 polarssl_free( ssl->handshake );
4526 polarssl_free( ssl->transform_negotiate );
4527 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004528 }
4529
Paul Bakkerc0463502013-02-14 11:19:38 +01004530 if( ssl->session )
4531 {
4532 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004533 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004534 }
4535
Paul Bakkera503a632013-08-14 13:48:06 +02004536#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004537 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004538#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004539
Paul Bakker0be444a2013-08-27 21:55:01 +02004540#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004541 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004542 {
4543 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004544 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004545 ssl->hostname_len = 0;
4546 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004547#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004548
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004549#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004550 if( ssl->psk != NULL )
4551 {
4552 memset( ssl->psk, 0, ssl->psk_len );
4553 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4554 polarssl_free( ssl->psk );
4555 polarssl_free( ssl->psk_identity );
4556 ssl->psk_len = 0;
4557 ssl->psk_identity_len = 0;
4558 }
4559#endif
4560
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004561#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004562 ssl_key_cert_free( ssl->key_cert );
4563#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004564
Paul Bakker05ef8352012-05-08 09:17:57 +00004565#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4566 if( ssl_hw_record_finish != NULL )
4567 {
4568 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4569 ssl_hw_record_finish( ssl );
4570 }
4571#endif
4572
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004574
Paul Bakker86f04f42013-02-14 11:20:09 +01004575 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004576 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004577}
4578
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004579#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004580/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004581 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004582 */
4583unsigned char ssl_sig_from_pk( pk_context *pk )
4584{
4585#if defined(POLARSSL_RSA_C)
4586 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4587 return( SSL_SIG_RSA );
4588#endif
4589#if defined(POLARSSL_ECDSA_C)
4590 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4591 return( SSL_SIG_ECDSA );
4592#endif
4593 return( SSL_SIG_ANON );
4594}
4595
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004596pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4597{
4598 switch( sig )
4599 {
4600#if defined(POLARSSL_RSA_C)
4601 case SSL_SIG_RSA:
4602 return( POLARSSL_PK_RSA );
4603#endif
4604#if defined(POLARSSL_ECDSA_C)
4605 case SSL_SIG_ECDSA:
4606 return( POLARSSL_PK_ECDSA );
4607#endif
4608 default:
4609 return( POLARSSL_PK_NONE );
4610 }
4611}
Paul Bakker9af723c2014-05-01 13:03:14 +02004612#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004613
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004614/*
4615 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4616 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004617md_type_t ssl_md_alg_from_hash( unsigned char hash )
4618{
4619 switch( hash )
4620 {
4621#if defined(POLARSSL_MD5_C)
4622 case SSL_HASH_MD5:
4623 return( POLARSSL_MD_MD5 );
4624#endif
4625#if defined(POLARSSL_SHA1_C)
4626 case SSL_HASH_SHA1:
4627 return( POLARSSL_MD_SHA1 );
4628#endif
4629#if defined(POLARSSL_SHA256_C)
4630 case SSL_HASH_SHA224:
4631 return( POLARSSL_MD_SHA224 );
4632 case SSL_HASH_SHA256:
4633 return( POLARSSL_MD_SHA256 );
4634#endif
4635#if defined(POLARSSL_SHA512_C)
4636 case SSL_HASH_SHA384:
4637 return( POLARSSL_MD_SHA384 );
4638 case SSL_HASH_SHA512:
4639 return( POLARSSL_MD_SHA512 );
4640#endif
4641 default:
4642 return( POLARSSL_MD_NONE );
4643 }
4644}
4645
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004646#if defined(POLARSSL_SSL_SET_CURVES)
4647/*
4648 * Check is a curve proposed by the peer is in our list.
4649 * Return 1 if we're willing to use it, 0 otherwise.
4650 */
4651int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4652{
4653 const ecp_group_id *gid;
4654
4655 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4656 if( *gid == grp_id )
4657 return( 1 );
4658
4659 return( 0 );
4660}
Paul Bakker9af723c2014-05-01 13:03:14 +02004661#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004662
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004663#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004664int ssl_check_cert_usage( const x509_crt *cert,
4665 const ssl_ciphersuite_t *ciphersuite,
4666 int cert_endpoint )
4667{
4668#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4669 int usage = 0;
4670#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004671#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4672 const char *ext_oid;
4673 size_t ext_len;
4674#endif
4675
4676#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4677 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4678 ((void) cert);
4679 ((void) cert_endpoint);
4680#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004681
4682#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4683 if( cert_endpoint == SSL_IS_SERVER )
4684 {
4685 /* Server part of the key exchange */
4686 switch( ciphersuite->key_exchange )
4687 {
4688 case POLARSSL_KEY_EXCHANGE_RSA:
4689 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4690 usage = KU_KEY_ENCIPHERMENT;
4691 break;
4692
4693 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4694 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4695 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4696 usage = KU_DIGITAL_SIGNATURE;
4697 break;
4698
4699 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4700 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4701 usage = KU_KEY_AGREEMENT;
4702 break;
4703
4704 /* Don't use default: we want warnings when adding new values */
4705 case POLARSSL_KEY_EXCHANGE_NONE:
4706 case POLARSSL_KEY_EXCHANGE_PSK:
4707 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4708 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4709 usage = 0;
4710 }
4711 }
4712 else
4713 {
4714 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4715 usage = KU_DIGITAL_SIGNATURE;
4716 }
4717
4718 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4719 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004720#else
4721 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004722#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4723
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004724#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4725 if( cert_endpoint == SSL_IS_SERVER )
4726 {
4727 ext_oid = OID_SERVER_AUTH;
4728 ext_len = OID_SIZE( OID_SERVER_AUTH );
4729 }
4730 else
4731 {
4732 ext_oid = OID_CLIENT_AUTH;
4733 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4734 }
4735
4736 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4737 return( -1 );
4738#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4739
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004740 return( 0 );
4741}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004742#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004743
4744#endif /* POLARSSL_SSL_TLS_C */