blob: f3e082b05b578f3dcc2d516edbc9ea52647a6e5b [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 Bakker48916f92012-09-16 19:57:18 +0000725 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
726 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000727 {
728 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
729 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
730 }
731 }
732#endif /* POLARSSL_ZLIB_SUPPORT */
733
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
735
736 return( 0 );
737}
738
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200739#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000740void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000741{
742 md5_context md5;
743 sha1_context sha1;
744 unsigned char pad_1[48];
745 unsigned char pad_2[48];
746
Paul Bakker380da532012-04-18 16:10:25 +0000747 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000748
Paul Bakker48916f92012-09-16 19:57:18 +0000749 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
750 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Paul Bakker380da532012-04-18 16:10:25 +0000752 memset( pad_1, 0x36, 48 );
753 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Paul Bakker48916f92012-09-16 19:57:18 +0000755 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000756 md5_update( &md5, pad_1, 48 );
757 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Paul Bakker380da532012-04-18 16:10:25 +0000759 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000760 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000761 md5_update( &md5, pad_2, 48 );
762 md5_update( &md5, hash, 16 );
763 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
Paul Bakker48916f92012-09-16 19:57:18 +0000765 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000766 sha1_update( &sha1, pad_1, 40 );
767 sha1_finish( &sha1, hash + 16 );
768
769 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000770 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000771 sha1_update( &sha1, pad_2, 40 );
772 sha1_update( &sha1, hash + 16, 20 );
773 sha1_finish( &sha1, hash + 16 );
774
775 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
776 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
777
778 return;
779}
Paul Bakker9af723c2014-05-01 13:03:14 +0200780#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000781
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200782#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000783void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
784{
785 md5_context md5;
786 sha1_context sha1;
787
788 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
789
Paul Bakker48916f92012-09-16 19:57:18 +0000790 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
791 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000792
Paul Bakker48916f92012-09-16 19:57:18 +0000793 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000794 sha1_finish( &sha1, hash + 16 );
795
796 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
797 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
798
799 return;
800}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200801#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000802
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200803#if defined(POLARSSL_SSL_PROTO_TLS1_2)
804#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000805void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
806{
Paul Bakker9e36f042013-06-30 14:34:05 +0200807 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000808
809 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
810
Paul Bakker9e36f042013-06-30 14:34:05 +0200811 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
812 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000813
814 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
815 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
816
817 return;
818}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200819#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000820
Paul Bakker9e36f042013-06-30 14:34:05 +0200821#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000822void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
823{
Paul Bakker9e36f042013-06-30 14:34:05 +0200824 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000825
826 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
827
Paul Bakker9e36f042013-06-30 14:34:05 +0200828 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
829 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000830
Paul Bakkerca4ab492012-04-18 14:23:57 +0000831 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
833
834 return;
835}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200836#endif /* POLARSSL_SHA512_C */
837#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200839#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200840int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
841{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200842 unsigned char *p = ssl->handshake->premaster;
843 unsigned char *end = p + sizeof( ssl->handshake->premaster );
844
845 /*
846 * PMS = struct {
847 * opaque other_secret<0..2^16-1>;
848 * opaque psk<0..2^16-1>;
849 * };
850 * with "other_secret" depending on the particular key exchange
851 */
852#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
853 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
854 {
855 if( end - p < 2 + (int) ssl->psk_len )
856 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
857
858 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
859 *(p++) = (unsigned char)( ssl->psk_len );
860 p += ssl->psk_len;
861 }
862 else
863#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200864#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
865 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
866 {
867 /*
868 * other_secret already set by the ClientKeyExchange message,
869 * and is 48 bytes long
870 */
871 *p++ = 0;
872 *p++ = 48;
873 p += 48;
874 }
875 else
876#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200877#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
878 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
879 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200880 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200881 size_t len = ssl->handshake->dhm_ctx.len;
882
883 if( end - p < 2 + (int) len )
884 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
885
886 *(p++) = (unsigned char)( len >> 8 );
887 *(p++) = (unsigned char)( len );
888 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
889 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
890 {
891 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
892 return( ret );
893 }
894 p += len;
895
896 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
897 }
898 else
899#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
900#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
901 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
902 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200903 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200904 size_t zlen;
905
906 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
907 p + 2, end - (p + 2),
908 ssl->f_rng, ssl->p_rng ) ) != 0 )
909 {
910 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
911 return( ret );
912 }
913
914 *(p++) = (unsigned char)( zlen >> 8 );
915 *(p++) = (unsigned char)( zlen );
916 p += zlen;
917
918 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
919 }
920 else
921#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
922 {
923 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
924 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
925 }
926
927 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100928 if( end - p < 2 + (int) ssl->psk_len )
929 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
930
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200931 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
932 *(p++) = (unsigned char)( ssl->psk_len );
933 memcpy( p, ssl->psk, ssl->psk_len );
934 p += ssl->psk_len;
935
936 ssl->handshake->pmslen = p - ssl->handshake->premaster;
937
938 return( 0 );
939}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200940#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200941
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200942#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000943/*
944 * SSLv3.0 MAC functions
945 */
Paul Bakker68884e32013-01-07 18:20:04 +0100946static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
947 unsigned char *buf, size_t len,
948 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000949{
950 unsigned char header[11];
951 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100952 int padlen = 0;
953 int md_size = md_get_size( md_ctx->md_info );
954 int md_type = md_get_type( md_ctx->md_info );
955
956 if( md_type == POLARSSL_MD_MD5 )
957 padlen = 48;
958 else if( md_type == POLARSSL_MD_SHA1 )
959 padlen = 40;
960 else if( md_type == POLARSSL_MD_SHA256 )
961 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100962 else if( md_type == POLARSSL_MD_SHA384 )
963 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
965 memcpy( header, ctr, 8 );
966 header[ 8] = (unsigned char) type;
967 header[ 9] = (unsigned char)( len >> 8 );
968 header[10] = (unsigned char)( len );
969
Paul Bakker68884e32013-01-07 18:20:04 +0100970 memset( padding, 0x36, padlen );
971 md_starts( md_ctx );
972 md_update( md_ctx, secret, md_size );
973 md_update( md_ctx, padding, padlen );
974 md_update( md_ctx, header, 11 );
975 md_update( md_ctx, buf, len );
976 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000977
Paul Bakker68884e32013-01-07 18:20:04 +0100978 memset( padding, 0x5C, padlen );
979 md_starts( md_ctx );
980 md_update( md_ctx, secret, md_size );
981 md_update( md_ctx, padding, padlen );
982 md_update( md_ctx, buf + len, md_size );
983 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000984}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200985#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000986
Paul Bakker5121ce52009-01-03 21:22:43 +0000987/*
988 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200989 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000990static int ssl_encrypt_buf( ssl_context *ssl )
991{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200992 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
994 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
995
996 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200997 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200999#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1000 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1001 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1002 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
1003 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001004 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001005#if defined(POLARSSL_SSL_PROTO_SSL3)
1006 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1007 {
1008 ssl_mac( &ssl->transform_out->md_ctx_enc,
1009 ssl->transform_out->mac_enc,
1010 ssl->out_msg, ssl->out_msglen,
1011 ssl->out_ctr, ssl->out_msgtype );
1012 }
1013 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001014#endif
1015#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001016 defined(POLARSSL_SSL_PROTO_TLS1_2)
1017 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1018 {
1019 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1020 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1021 ssl->out_msg, ssl->out_msglen );
1022 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1023 ssl->out_msg + ssl->out_msglen );
1024 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1025 }
1026 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001027#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001028 {
1029 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1030 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1031 }
1032
1033 SSL_DEBUG_BUF( 4, "computed mac",
1034 ssl->out_msg + ssl->out_msglen,
1035 ssl->transform_out->maclen );
1036
1037 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001038 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001039#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001041 /*
1042 * Encrypt
1043 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001044#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001045 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1046 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001048 int ret;
1049 size_t olen = 0;
1050
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1052 "including %d bytes of padding",
1053 ssl->out_msglen, 0 ) );
1054
1055 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1056 ssl->out_msg, ssl->out_msglen );
1057
Paul Bakker45125bc2013-09-04 16:47:11 +02001058 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001059 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001060 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1061 return( ret );
1062 }
1063
1064 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1065 ssl->transform_out->iv_enc,
1066 ssl->transform_out->ivlen ) ) != 0 )
1067 {
1068 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001069 return( ret );
1070 }
1071
1072 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1073 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1074 &olen ) ) != 0 )
1075 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001076 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001077 return( ret );
1078 }
1079
1080 if( ssl->out_msglen != olen )
1081 {
1082 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1083 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001084 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001085 }
1086
1087 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001088 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001089 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001090 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001091 return( ret );
1092 }
1093
1094 if( 0 != olen )
1095 {
1096 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1097 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001098 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001099 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 }
Paul Bakker68884e32013-01-07 18:20:04 +01001101 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001102#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001103#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001104 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1105 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001106 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001107 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001108 unsigned char *enc_msg;
1109 unsigned char add_data[13];
1110 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1111
Paul Bakkerca4ab492012-04-18 14:23:57 +00001112 memcpy( add_data, ssl->out_ctr, 8 );
1113 add_data[8] = ssl->out_msgtype;
1114 add_data[9] = ssl->major_ver;
1115 add_data[10] = ssl->minor_ver;
1116 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1117 add_data[12] = ssl->out_msglen & 0xFF;
1118
1119 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1120 add_data, 13 );
1121
Paul Bakker68884e32013-01-07 18:20:04 +01001122 /*
1123 * Generate IV
1124 */
1125 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001126 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001127 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1128 if( ret != 0 )
1129 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001130
Paul Bakker68884e32013-01-07 18:20:04 +01001131 memcpy( ssl->out_iv,
1132 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1133 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001134
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001135 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1136 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1137
Paul Bakker68884e32013-01-07 18:20:04 +01001138 /*
1139 * Fix pointer positions and message length with added IV
1140 */
1141 enc_msg = ssl->out_msg;
1142 enc_msglen = ssl->out_msglen;
1143 ssl->out_msglen += ssl->transform_out->ivlen -
1144 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001145
Paul Bakker68884e32013-01-07 18:20:04 +01001146 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1147 "including %d bytes of padding",
1148 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001149
Paul Bakker68884e32013-01-07 18:20:04 +01001150 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1151 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001152
Paul Bakker68884e32013-01-07 18:20:04 +01001153 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001154 * Encrypt
1155 */
1156 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1157 ssl->transform_out->iv_enc,
1158 ssl->transform_out->ivlen ) ) != 0 ||
1159 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1160 {
1161 return( ret );
1162 }
1163
1164 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1165 add_data, 13 ) ) != 0 )
1166 {
1167 return( ret );
1168 }
1169
1170 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1171 enc_msg, enc_msglen,
1172 enc_msg, &olen ) ) != 0 )
1173 {
1174 return( ret );
1175 }
1176 totlen = olen;
1177
1178 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1179 enc_msg + olen, &olen ) ) != 0 )
1180 {
1181 return( ret );
1182 }
1183 totlen += olen;
1184
1185 if( totlen != enc_msglen )
1186 {
1187 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1188 return( -1 );
1189 }
1190
1191 /*
1192 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001193 */
1194 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001195
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001196 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1197 enc_msg + enc_msglen, 16 ) ) != 0 )
1198 {
1199 return( ret );
1200 }
Paul Bakker68884e32013-01-07 18:20:04 +01001201
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001202 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001203 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 else
Paul Bakker68884e32013-01-07 18:20:04 +01001205#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001206#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1207 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001208 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1209 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001211 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001212 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001213 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001214
Paul Bakker48916f92012-09-16 19:57:18 +00001215 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1216 ssl->transform_out->ivlen;
1217 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 padlen = 0;
1219
1220 for( i = 0; i <= padlen; i++ )
1221 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1222
1223 ssl->out_msglen += padlen + 1;
1224
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001225 enc_msglen = ssl->out_msglen;
1226 enc_msg = ssl->out_msg;
1227
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001228#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001229 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001230 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1231 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001232 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001233 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001234 {
1235 /*
1236 * Generate IV
1237 */
Paul Bakker48916f92012-09-16 19:57:18 +00001238 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1239 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001240 if( ret != 0 )
1241 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242
Paul Bakker92be97b2013-01-02 17:30:03 +01001243 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001244 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001245
1246 /*
1247 * Fix pointer positions and message length with added IV
1248 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001249 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001250 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001251 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001252 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001253#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001254
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001256 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001257 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258
1259 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001260 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261
Paul Bakker45125bc2013-09-04 16:47:11 +02001262 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001264 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1265 return( ret );
1266 }
1267
1268 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1269 ssl->transform_out->iv_enc,
1270 ssl->transform_out->ivlen ) ) != 0 )
1271 {
1272 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001273 return( ret );
1274 }
Paul Bakker68884e32013-01-07 18:20:04 +01001275
Paul Bakkercca5b812013-08-31 17:40:26 +02001276 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1277 enc_msg, enc_msglen, enc_msg,
1278 &olen ) ) != 0 )
1279 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001280 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 return( ret );
1282 }
Paul Bakker68884e32013-01-07 18:20:04 +01001283
Paul Bakkercca5b812013-08-31 17:40:26 +02001284 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001285
Paul Bakkercca5b812013-08-31 17:40:26 +02001286 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001287 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001288 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001289 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001290 return( ret );
1291 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001292
Paul Bakkercca5b812013-08-31 17:40:26 +02001293 if( enc_msglen != olen )
1294 {
1295 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1296 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001297 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001298 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001299
1300#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001301 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1302 {
1303 /*
1304 * Save IV in SSL3 and TLS1
1305 */
1306 memcpy( ssl->transform_out->iv_enc,
1307 ssl->transform_out->cipher_ctx_enc.iv,
1308 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001310#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001312 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001313#endif /* POLARSSL_CIPHER_MODE_CBC &&
1314 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001315 {
1316 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1317 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1318 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
Paul Bakkerca4ab492012-04-18 14:23:57 +00001320 for( i = 8; i > 0; i-- )
1321 if( ++ssl->out_ctr[i - 1] != 0 )
1322 break;
1323
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001324 /* The loops goes to its end iff the counter is wrapping */
1325 if( i == 0 )
1326 {
1327 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1328 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1329 }
1330
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1332
1333 return( 0 );
1334}
1335
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001336#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001337
Paul Bakker5121ce52009-01-03 21:22:43 +00001338static int ssl_decrypt_buf( ssl_context *ssl )
1339{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001340 size_t i;
1341#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1342 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1343 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1344 size_t padlen = 0, correct = 1;
1345#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
1347 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1348
Paul Bakker48916f92012-09-16 19:57:18 +00001349 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 {
1351 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001352 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001353 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 }
1355
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001356#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001357 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1358 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001359 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001360 int ret;
1361 size_t olen = 0;
1362
Paul Bakker68884e32013-01-07 18:20:04 +01001363 padlen = 0;
1364
Paul Bakker45125bc2013-09-04 16:47:11 +02001365 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001366 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001367 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1368 return( ret );
1369 }
1370
1371 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1372 ssl->transform_in->iv_dec,
1373 ssl->transform_in->ivlen ) ) != 0 )
1374 {
1375 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001376 return( ret );
1377 }
1378
1379 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1380 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1381 &olen ) ) != 0 )
1382 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001383 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001384 return( ret );
1385 }
1386
1387 if( ssl->in_msglen != olen )
1388 {
1389 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001390 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001391 }
1392
1393 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001394 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001395 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001396 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001397 return( ret );
1398 }
1399
1400 if( 0 != olen )
1401 {
1402 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001403 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 }
Paul Bakker68884e32013-01-07 18:20:04 +01001406 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001407#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001408#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001409 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1410 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001411 {
1412 unsigned char *dec_msg;
1413 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001414 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001415 unsigned char add_data[13];
1416 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1417
Paul Bakker68884e32013-01-07 18:20:04 +01001418 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1419 ssl->transform_in->fixed_ivlen );
1420 dec_msglen -= 16;
1421 dec_msg = ssl->in_msg;
1422 dec_msg_result = ssl->in_msg;
1423 ssl->in_msglen = dec_msglen;
1424
1425 memcpy( add_data, ssl->in_ctr, 8 );
1426 add_data[8] = ssl->in_msgtype;
1427 add_data[9] = ssl->major_ver;
1428 add_data[10] = ssl->minor_ver;
1429 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1430 add_data[12] = ssl->in_msglen & 0xFF;
1431
1432 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1433 add_data, 13 );
1434
1435 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1436 ssl->in_iv,
1437 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1438
1439 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1440 ssl->transform_in->ivlen );
1441 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1442
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001443 /*
1444 * Decrypt
1445 */
1446 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1447 ssl->transform_in->iv_dec,
1448 ssl->transform_in->ivlen ) ) != 0 ||
1449 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001450 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001451 return( ret );
1452 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001453
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001454 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1455 add_data, 13 ) ) != 0 )
1456 {
1457 return( ret );
1458 }
1459
1460 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1461 dec_msg, dec_msglen,
1462 dec_msg_result, &olen ) ) != 0 )
1463 {
1464 return( ret );
1465 }
1466 totlen = olen;
1467
1468 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1469 dec_msg_result + olen, &olen ) ) != 0 )
1470 {
1471 return( ret );
1472 }
1473 totlen += olen;
1474
1475 if( totlen != dec_msglen )
1476 {
1477 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1478 return( -1 );
1479 }
1480
1481 /*
1482 * Authenticate
1483 */
1484 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1485 dec_msg + dec_msglen, 16 ) ) != 0 )
1486 {
1487 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001488 return( POLARSSL_ERR_SSL_INVALID_MAC );
1489 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001490
Paul Bakkerca4ab492012-04-18 14:23:57 +00001491 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 else
Paul Bakker68884e32013-01-07 18:20:04 +01001493#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001494#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1495 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001496 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1497 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 {
Paul Bakker45829992013-01-03 14:52:21 +01001499 /*
1500 * Decrypt and check the padding
1501 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001502 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001503 unsigned char *dec_msg;
1504 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001505 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001506 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001507 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001508
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 /*
Paul Bakker45829992013-01-03 14:52:21 +01001510 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 */
Paul Bakker48916f92012-09-16 19:57:18 +00001512 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 {
1514 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001515 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001516 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001517 }
1518
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001519#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001520 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1521 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001522#endif
Paul Bakker45829992013-01-03 14:52:21 +01001523
1524 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1525 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1526 {
1527 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1528 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1529 return( POLARSSL_ERR_SSL_INVALID_MAC );
1530 }
1531
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001532 dec_msglen = ssl->in_msglen;
1533 dec_msg = ssl->in_msg;
1534 dec_msg_result = ssl->in_msg;
1535
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001536#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001537 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001538 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001539 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001540 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001541 {
Paul Bakker48916f92012-09-16 19:57:18 +00001542 dec_msglen -= ssl->transform_in->ivlen;
1543 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001544
Paul Bakker48916f92012-09-16 19:57:18 +00001545 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001546 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001547 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001548#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001549
Paul Bakker45125bc2013-09-04 16:47:11 +02001550 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001552 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1553 return( ret );
1554 }
1555
1556 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1557 ssl->transform_in->iv_dec,
1558 ssl->transform_in->ivlen ) ) != 0 )
1559 {
1560 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 return( ret );
1562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001563
Paul Bakkercca5b812013-08-31 17:40:26 +02001564 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1565 dec_msg, dec_msglen, dec_msg_result,
1566 &olen ) ) != 0 )
1567 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001568 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001569 return( ret );
1570 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001571
Paul Bakkercca5b812013-08-31 17:40:26 +02001572 dec_msglen -= olen;
1573 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001574 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001575 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001576 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001577 return( ret );
1578 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001579
Paul Bakkercca5b812013-08-31 17:40:26 +02001580 if( dec_msglen != olen )
1581 {
1582 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001583 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001584 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001585
1586#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001587 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1588 {
1589 /*
1590 * Save IV in SSL3 and TLS1
1591 */
1592 memcpy( ssl->transform_in->iv_dec,
1593 ssl->transform_in->cipher_ctx_dec.iv,
1594 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001596#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
1598 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001599
1600 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1601 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001602#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001603 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1604 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001605#endif
Paul Bakker45829992013-01-03 14:52:21 +01001606 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001607 correct = 0;
1608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001610#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1612 {
Paul Bakker48916f92012-09-16 19:57:18 +00001613 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001615#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1617 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001618 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001619#endif
Paul Bakker45829992013-01-03 14:52:21 +01001620 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 }
1622 }
1623 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001624#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001625#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1626 defined(POLARSSL_SSL_PROTO_TLS1_2)
1627 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 {
1629 /*
Paul Bakker45829992013-01-03 14:52:21 +01001630 * TLSv1+: always check the padding up to the first failure
1631 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001633 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001634 size_t padding_idx = ssl->in_msglen - padlen - 1;
1635
Paul Bakker956c9e02013-12-19 14:42:28 +01001636 /*
1637 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001638 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001639 *
Paul Bakker61885c72014-04-25 12:59:03 +02001640 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1641 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001642 *
1643 * In both cases we reset padding_idx to a safe value (0) to
1644 * prevent out-of-buffer reads.
1645 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001646 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001647 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1648 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001649
1650 padding_idx *= correct;
1651
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001652 for( i = 1; i <= 256; i++ )
1653 {
1654 real_count &= ( i <= padlen );
1655 pad_count += real_count *
1656 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1657 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001658
1659 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001660
Paul Bakkerd66f0702013-01-31 16:57:45 +01001661#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001662 if( padlen > 0 && correct == 0)
1663 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001664#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001665 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001667 else
1668#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1669 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001670 {
1671 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001672 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001673 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001675 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001676#endif /* POLARSSL_CIPHER_MODE_CBC &&
1677 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001678 {
1679 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1680 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
1683 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1684 ssl->in_msg, ssl->in_msglen );
1685
1686 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001687 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001688 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001689#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1690 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1691 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1692 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1693 POLARSSL_MODE_GCM )
1694 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001695 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1696
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001697 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001699 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1700 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001701
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001702 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001704#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001705 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1706 {
1707 ssl_mac( &ssl->transform_in->md_ctx_dec,
1708 ssl->transform_in->mac_dec,
1709 ssl->in_msg, ssl->in_msglen,
1710 ssl->in_ctr, ssl->in_msgtype );
1711 }
1712 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001713#endif /* POLARSSL_SSL_PROTO_SSL3 */
1714#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001715 defined(POLARSSL_SSL_PROTO_TLS1_2)
1716 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1717 {
1718 /*
1719 * Process MAC and always update for padlen afterwards to make
1720 * total time independent of padlen
1721 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001722 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001723 *
1724 * Known timing attacks:
1725 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1726 *
1727 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1728 * correctly. (We round down instead of up, so -56 is the correct
1729 * value for our calculations instead of -55)
1730 */
1731 size_t j, extra_run = 0;
1732 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1733 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001734
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001735 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001736
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001737 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1738 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1739 ssl->in_msglen );
1740 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1741 ssl->in_msg + ssl->in_msglen );
1742 for( j = 0; j < extra_run; j++ )
1743 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001744
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001745 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1746 }
1747 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001748#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001749 POLARSSL_SSL_PROTO_TLS1_2 */
1750 {
1751 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1752 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001755 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1756 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1757 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001759 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001760 ssl->transform_in->maclen ) != 0 )
1761 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001762#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001763 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001764#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001765 correct = 0;
1766 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001768 /*
1769 * Finally check the correct flag
1770 */
1771 if( correct == 0 )
1772 return( POLARSSL_ERR_SSL_INVALID_MAC );
1773 }
1774#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001775
1776 if( ssl->in_msglen == 0 )
1777 {
1778 ssl->nb_zero++;
1779
1780 /*
1781 * Three or more empty messages may be a DoS attack
1782 * (excessive CPU consumption).
1783 */
1784 if( ssl->nb_zero > 3 )
1785 {
1786 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1787 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001788 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 }
1790 }
1791 else
1792 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001793
Paul Bakker23986e52011-04-24 08:57:21 +00001794 for( i = 8; i > 0; i-- )
1795 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 break;
1797
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001798 /* The loops goes to its end iff the counter is wrapping */
1799 if( i == 0 )
1800 {
1801 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1802 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1803 }
1804
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1806
1807 return( 0 );
1808}
1809
Paul Bakker2770fbd2012-07-03 13:30:23 +00001810#if defined(POLARSSL_ZLIB_SUPPORT)
1811/*
1812 * Compression/decompression functions
1813 */
1814static int ssl_compress_buf( ssl_context *ssl )
1815{
1816 int ret;
1817 unsigned char *msg_post = ssl->out_msg;
1818 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001819 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001820
1821 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1822
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001823 if( len_pre == 0 )
1824 return( 0 );
1825
Paul Bakker2770fbd2012-07-03 13:30:23 +00001826 memcpy( msg_pre, ssl->out_msg, len_pre );
1827
1828 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1829 ssl->out_msglen ) );
1830
1831 SSL_DEBUG_BUF( 4, "before compression: output payload",
1832 ssl->out_msg, ssl->out_msglen );
1833
Paul Bakker48916f92012-09-16 19:57:18 +00001834 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1835 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1836 ssl->transform_out->ctx_deflate.next_out = msg_post;
1837 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001838
Paul Bakker48916f92012-09-16 19:57:18 +00001839 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001840 if( ret != Z_OK )
1841 {
1842 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1843 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1844 }
1845
Paul Bakker48916f92012-09-16 19:57:18 +00001846 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001847
Paul Bakker2770fbd2012-07-03 13:30:23 +00001848 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1849 ssl->out_msglen ) );
1850
1851 SSL_DEBUG_BUF( 4, "after compression: output payload",
1852 ssl->out_msg, ssl->out_msglen );
1853
1854 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1855
1856 return( 0 );
1857}
1858
1859static int ssl_decompress_buf( ssl_context *ssl )
1860{
1861 int ret;
1862 unsigned char *msg_post = ssl->in_msg;
1863 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001864 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001865
1866 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1867
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001868 if( len_pre == 0 )
1869 return( 0 );
1870
Paul Bakker2770fbd2012-07-03 13:30:23 +00001871 memcpy( msg_pre, ssl->in_msg, len_pre );
1872
1873 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1874 ssl->in_msglen ) );
1875
1876 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1877 ssl->in_msg, ssl->in_msglen );
1878
Paul Bakker48916f92012-09-16 19:57:18 +00001879 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1880 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1881 ssl->transform_in->ctx_inflate.next_out = msg_post;
1882 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001883
Paul Bakker48916f92012-09-16 19:57:18 +00001884 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001885 if( ret != Z_OK )
1886 {
1887 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1888 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1889 }
1890
Paul Bakker48916f92012-09-16 19:57:18 +00001891 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001892
Paul Bakker2770fbd2012-07-03 13:30:23 +00001893 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1894 ssl->in_msglen ) );
1895
1896 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1897 ssl->in_msg, ssl->in_msglen );
1898
1899 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1900
1901 return( 0 );
1902}
1903#endif /* POLARSSL_ZLIB_SUPPORT */
1904
Paul Bakker5121ce52009-01-03 21:22:43 +00001905/*
1906 * Fill the input message buffer
1907 */
Paul Bakker23986e52011-04-24 08:57:21 +00001908int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001909{
Paul Bakker23986e52011-04-24 08:57:21 +00001910 int ret;
1911 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
1913 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1914
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001915 if( nb_want > SSL_BUFFER_LEN - 8 )
1916 {
1917 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1918 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1919 }
1920
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 while( ssl->in_left < nb_want )
1922 {
1923 len = nb_want - ssl->in_left;
1924 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1925
1926 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1927 ssl->in_left, nb_want ) );
1928 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1929
Paul Bakker831a7552011-05-18 13:32:51 +00001930 if( ret == 0 )
1931 return( POLARSSL_ERR_SSL_CONN_EOF );
1932
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 if( ret < 0 )
1934 return( ret );
1935
1936 ssl->in_left += ret;
1937 }
1938
1939 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1940
1941 return( 0 );
1942}
1943
1944/*
1945 * Flush any data not yet written
1946 */
1947int ssl_flush_output( ssl_context *ssl )
1948{
1949 int ret;
1950 unsigned char *buf;
1951
1952 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1953
1954 while( ssl->out_left > 0 )
1955 {
1956 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1957 5 + ssl->out_msglen, ssl->out_left ) );
1958
Paul Bakker5bd42292012-12-19 14:40:42 +01001959 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001961
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1963
1964 if( ret <= 0 )
1965 return( ret );
1966
1967 ssl->out_left -= ret;
1968 }
1969
1970 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1971
1972 return( 0 );
1973}
1974
1975/*
1976 * Record layer functions
1977 */
1978int ssl_write_record( ssl_context *ssl )
1979{
Paul Bakker05ef8352012-05-08 09:17:57 +00001980 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001981 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001982
1983 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1984
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1986 {
1987 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1988 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1989 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1990
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001991 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1992 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001993 }
1994
Paul Bakker2770fbd2012-07-03 13:30:23 +00001995#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001996 if( ssl->transform_out != NULL &&
1997 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001998 {
1999 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2000 {
2001 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2002 return( ret );
2003 }
2004
2005 len = ssl->out_msglen;
2006 }
2007#endif /*POLARSSL_ZLIB_SUPPORT */
2008
Paul Bakker05ef8352012-05-08 09:17:57 +00002009#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2010 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002012 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
Paul Bakker05ef8352012-05-08 09:17:57 +00002014 ret = ssl_hw_record_write( ssl );
2015 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2016 {
2017 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
2018 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2019 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002020
2021 if( ret == 0 )
2022 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002023 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002024#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002025 if( !done )
2026 {
2027 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2028 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2029 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2031 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002032
Paul Bakker48916f92012-09-16 19:57:18 +00002033 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002034 {
2035 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2036 {
2037 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2038 return( ret );
2039 }
2040
2041 len = ssl->out_msglen;
2042 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2043 ssl->out_hdr[4] = (unsigned char)( len );
2044 }
2045
2046 ssl->out_left = 5 + ssl->out_msglen;
2047
2048 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2049 "version = [%d:%d], msglen = %d",
2050 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2051 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2052
2053 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002054 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 }
2056
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2058 {
2059 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2060 return( ret );
2061 }
2062
2063 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2064
2065 return( 0 );
2066}
2067
2068int ssl_read_record( ssl_context *ssl )
2069{
Paul Bakker05ef8352012-05-08 09:17:57 +00002070 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
2072 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2073
Paul Bakker68884e32013-01-07 18:20:04 +01002074 SSL_DEBUG_BUF( 4, "input record from network",
2075 ssl->in_hdr, 5 + ssl->in_msglen );
2076
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 if( ssl->in_hslen != 0 &&
2078 ssl->in_hslen < ssl->in_msglen )
2079 {
2080 /*
2081 * Get next Handshake message in the current record
2082 */
2083 ssl->in_msglen -= ssl->in_hslen;
2084
Paul Bakker8934a982011-08-05 11:11:53 +00002085 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002086 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
2088 ssl->in_hslen = 4;
2089 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2090
2091 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2092 " %d, type = %d, hslen = %d",
2093 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2094
2095 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2096 {
2097 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002098 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 }
2100
2101 if( ssl->in_msglen < ssl->in_hslen )
2102 {
2103 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002104 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
2106
Paul Bakker4224bc02014-04-08 14:36:50 +02002107 if( ssl->state != SSL_HANDSHAKE_OVER )
2108 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002109
2110 return( 0 );
2111 }
2112
2113 ssl->in_hslen = 0;
2114
2115 /*
2116 * Read the record header and validate it
2117 */
2118 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2119 {
2120 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2121 return( ret );
2122 }
2123
2124 ssl->in_msgtype = ssl->in_hdr[0];
2125 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2126
2127 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2128 "version = [%d:%d], msglen = %d",
2129 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2130 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2131
2132 if( ssl->in_hdr[1] != ssl->major_ver )
2133 {
2134 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002135 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 }
2137
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002138 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 {
2140 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002141 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 }
2143
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002144 /* Sanity check (outer boundaries) */
2145 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2146 {
2147 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2148 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2149 }
2150
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002152 * Make sure the message length is acceptable for the current transform
2153 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 */
Paul Bakker48916f92012-09-16 19:57:18 +00002155 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002157 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 {
2159 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002160 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 }
2162 }
2163 else
2164 {
Paul Bakker48916f92012-09-16 19:57:18 +00002165 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 {
2167 SSL_DEBUG_MSG( 1, ( "bad message 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 Bakkerd2f068e2013-08-27 21:19:20 +02002171#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002173 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 {
2175 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002176 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002178#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002179
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002180#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2181 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 /*
2183 * TLS encrypted messages can have up to 256 bytes of padding
2184 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002185 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002186 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 {
2188 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002189 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002191#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 }
2193
2194 /*
2195 * Read and optionally decrypt the message contents
2196 */
2197 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2198 {
2199 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2200 return( ret );
2201 }
2202
2203 SSL_DEBUG_BUF( 4, "input record from network",
2204 ssl->in_hdr, 5 + ssl->in_msglen );
2205
Paul Bakker05ef8352012-05-08 09:17:57 +00002206#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2207 if( ssl_hw_record_read != NULL)
2208 {
2209 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2210
2211 ret = ssl_hw_record_read( ssl );
2212 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2213 {
2214 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2215 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2216 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002217
2218 if( ret == 0 )
2219 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002220 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002221#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002222 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 {
2224 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2225 {
Paul Bakker40865c82013-01-31 17:13:13 +01002226#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2227 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2228 {
2229 ssl_send_alert_message( ssl,
2230 SSL_ALERT_LEVEL_FATAL,
2231 SSL_ALERT_MSG_BAD_RECORD_MAC );
2232 }
2233#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2235 return( ret );
2236 }
2237
2238 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2239 ssl->in_msg, ssl->in_msglen );
2240
2241 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2242 {
2243 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002244 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 }
2246 }
2247
Paul Bakker2770fbd2012-07-03 13:30:23 +00002248#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002249 if( ssl->transform_in != NULL &&
2250 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002251 {
2252 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2253 {
2254 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2255 return( ret );
2256 }
2257
2258 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2259 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2260 }
2261#endif /* POLARSSL_ZLIB_SUPPORT */
2262
Paul Bakker0a925182012-04-16 06:46:41 +00002263 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2264 ssl->in_msgtype != SSL_MSG_ALERT &&
2265 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2266 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2267 {
2268 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2269
Paul Bakker48916f92012-09-16 19:57:18 +00002270 if( ( ret = ssl_send_alert_message( ssl,
2271 SSL_ALERT_LEVEL_FATAL,
2272 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002273 {
2274 return( ret );
2275 }
2276
2277 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2278 }
2279
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2281 {
2282 ssl->in_hslen = 4;
2283 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2284
2285 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2286 " %d, type = %d, hslen = %d",
2287 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2288
2289 /*
2290 * Additional checks to validate the handshake header
2291 */
2292 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2293 {
2294 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002295 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 }
2297
2298 if( ssl->in_msglen < ssl->in_hslen )
2299 {
2300 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002301 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 }
2303
Paul Bakker48916f92012-09-16 19:57:18 +00002304 if( ssl->state != SSL_HANDSHAKE_OVER )
2305 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 }
2307
2308 if( ssl->in_msgtype == SSL_MSG_ALERT )
2309 {
2310 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2311 ssl->in_msg[0], ssl->in_msg[1] ) );
2312
2313 /*
2314 * Ignore non-fatal alerts, except close_notify
2315 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002316 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002318 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2319 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002320 /**
2321 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2322 * error identifier.
2323 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002324 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 }
2326
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002327 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2328 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 {
2330 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002331 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 }
2333 }
2334
2335 ssl->in_left = 0;
2336
2337 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2338
2339 return( 0 );
2340}
2341
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002342int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2343{
2344 int ret;
2345
2346 if( ( ret = ssl_send_alert_message( ssl,
2347 SSL_ALERT_LEVEL_FATAL,
2348 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2349 {
2350 return( ret );
2351 }
2352
2353 return( 0 );
2354}
2355
Paul Bakker0a925182012-04-16 06:46:41 +00002356int ssl_send_alert_message( ssl_context *ssl,
2357 unsigned char level,
2358 unsigned char message )
2359{
2360 int ret;
2361
2362 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2363
2364 ssl->out_msgtype = SSL_MSG_ALERT;
2365 ssl->out_msglen = 2;
2366 ssl->out_msg[0] = level;
2367 ssl->out_msg[1] = message;
2368
2369 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2370 {
2371 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2372 return( ret );
2373 }
2374
2375 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2376
2377 return( 0 );
2378}
2379
Paul Bakker5121ce52009-01-03 21:22:43 +00002380/*
2381 * Handshake functions
2382 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002383#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2384 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2385 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2386 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2387 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2388 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2389 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002390int ssl_write_certificate( ssl_context *ssl )
2391{
Paul Bakkered27a042013-04-18 22:46:23 +02002392 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002393 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
2395 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2396
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002397 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002398 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2399 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002400 {
2401 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2402 ssl->state++;
2403 return( 0 );
2404 }
2405
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002406 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002407 return( ret );
2408}
2409
2410int ssl_parse_certificate( ssl_context *ssl )
2411{
2412 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2413 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2414
2415 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2416
2417 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002418 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2419 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420 {
2421 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2422 ssl->state++;
2423 return( 0 );
2424 }
2425
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002426 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002427 return( ret );
2428}
2429#else
2430int ssl_write_certificate( ssl_context *ssl )
2431{
2432 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2433 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002434 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002435 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2436
2437 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2438
2439 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002440 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2441 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002442 {
2443 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2444 ssl->state++;
2445 return( 0 );
2446 }
2447
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 if( ssl->endpoint == SSL_IS_CLIENT )
2449 {
2450 if( ssl->client_auth == 0 )
2451 {
2452 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2453 ssl->state++;
2454 return( 0 );
2455 }
2456
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002457#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 /*
2459 * If using SSLv3 and got no cert, send an Alert message
2460 * (otherwise an empty Certificate message will be sent).
2461 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002462 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2464 {
2465 ssl->out_msglen = 2;
2466 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002467 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2468 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
2470 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2471 goto write_msg;
2472 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002473#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 }
2475 else /* SSL_IS_SERVER */
2476 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002477 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 {
2479 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002480 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 }
2482 }
2483
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002484 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
2486 /*
2487 * 0 . 0 handshake type
2488 * 1 . 3 handshake length
2489 * 4 . 6 length of all certs
2490 * 7 . 9 length of cert. 1
2491 * 10 . n-1 peer certificate
2492 * n . n+2 length of cert. 2
2493 * n+3 . ... upper level cert, etc.
2494 */
2495 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002496 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Paul Bakker29087132010-03-21 21:03:34 +00002498 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 {
2500 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002501 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 {
2503 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2504 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002505 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 }
2507
2508 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2509 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2510 ssl->out_msg[i + 2] = (unsigned char)( n );
2511
2512 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2513 i += n; crt = crt->next;
2514 }
2515
2516 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2517 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2518 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2519
2520 ssl->out_msglen = i;
2521 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2522 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2523
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002524#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002525write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002526#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 ssl->state++;
2529
2530 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2531 {
2532 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2533 return( ret );
2534 }
2535
2536 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2537
Paul Bakkered27a042013-04-18 22:46:23 +02002538 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539}
2540
2541int ssl_parse_certificate( ssl_context *ssl )
2542{
Paul Bakkered27a042013-04-18 22:46:23 +02002543 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002544 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002545 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
2547 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2548
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002549 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002550 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2551 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002552 {
2553 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2554 ssl->state++;
2555 return( 0 );
2556 }
2557
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002559 ( ssl->authmode == SSL_VERIFY_NONE ||
2560 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002562 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2564 ssl->state++;
2565 return( 0 );
2566 }
2567
2568 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2569 {
2570 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2571 return( ret );
2572 }
2573
2574 ssl->state++;
2575
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002576#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 /*
2578 * Check if the client sent an empty certificate
2579 */
2580 if( ssl->endpoint == SSL_IS_SERVER &&
2581 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2582 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002583 if( ssl->in_msglen == 2 &&
2584 ssl->in_msgtype == SSL_MSG_ALERT &&
2585 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2586 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 {
2588 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2589
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002590 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2592 return( 0 );
2593 else
Paul Bakker40e46942009-01-03 21:51:57 +00002594 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 }
2596 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002597#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002599#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2600 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 if( ssl->endpoint == SSL_IS_SERVER &&
2602 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2603 {
2604 if( ssl->in_hslen == 7 &&
2605 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2606 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2607 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2608 {
2609 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2610
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002611 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002613 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 else
2615 return( 0 );
2616 }
2617 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002618#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2619 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
2621 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2622 {
2623 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002624 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
2626
2627 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2628 {
2629 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002630 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 }
2632
2633 /*
2634 * Same message structure as in ssl_write_certificate()
2635 */
2636 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2637
2638 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2639 {
2640 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002641 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 }
2643
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002644 /* In case we tried to reuse a session but it failed */
2645 if( ssl->session_negotiate->peer_cert != NULL )
2646 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002647 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002648 polarssl_free( ssl->session_negotiate->peer_cert );
2649 }
2650
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002651 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2652 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 {
2654 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002655 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002656 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 }
2658
Paul Bakkerb6b09562013-09-18 14:17:41 +02002659 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
2661 i = 7;
2662
2663 while( i < ssl->in_hslen )
2664 {
2665 if( ssl->in_msg[i] != 0 )
2666 {
2667 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002668 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 }
2670
2671 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2672 | (unsigned int) ssl->in_msg[i + 2];
2673 i += 3;
2674
2675 if( n < 128 || i + n > ssl->in_hslen )
2676 {
2677 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002678 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 }
2680
Paul Bakkerddf26b42013-09-18 13:46:23 +02002681 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2682 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 if( ret != 0 )
2684 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002685 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 return( ret );
2687 }
2688
2689 i += n;
2690 }
2691
Paul Bakker48916f92012-09-16 19:57:18 +00002692 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002694 /*
2695 * On client, make sure the server cert doesn't change during renego to
2696 * avoid "triple handshake" attack: https://secure-resumption.com/
2697 */
2698 if( ssl->endpoint == SSL_IS_CLIENT &&
2699 ssl->renegotiation == SSL_RENEGOTIATION )
2700 {
2701 if( ssl->session->peer_cert == NULL )
2702 {
2703 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2704 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2705 }
2706
2707 if( ssl->session->peer_cert->raw.len !=
2708 ssl->session_negotiate->peer_cert->raw.len ||
2709 memcmp( ssl->session->peer_cert->raw.p,
2710 ssl->session_negotiate->peer_cert->raw.p,
2711 ssl->session->peer_cert->raw.len ) != 0 )
2712 {
2713 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2714 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2715 }
2716 }
2717
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 if( ssl->authmode != SSL_VERIFY_NONE )
2719 {
2720 if( ssl->ca_chain == NULL )
2721 {
2722 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002723 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 }
2725
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002726 /*
2727 * Main check: verify certificate
2728 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002729 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2730 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2731 &ssl->session_negotiate->verify_result,
2732 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
2734 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002735 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002737 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002738
2739 /*
2740 * Secondary checks: always done, but change 'ret' only if it was 0
2741 */
2742
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002743#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002744 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002745 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002746
2747 /* If certificate uses an EC key, make sure the curve is OK */
2748 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2749 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2750 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002751 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002752 if( ret == 0 )
2753 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002754 }
2755 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002756#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002758 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2759 ciphersuite_info,
2760 ! ssl->endpoint ) != 0 )
2761 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002762 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002763 if( ret == 0 )
2764 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2765 }
2766
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2768 ret = 0;
2769 }
2770
2771 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2772
2773 return( ret );
2774}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002775#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2776 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2777 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2778 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2779 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2780 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2781 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002782
2783int ssl_write_change_cipher_spec( ssl_context *ssl )
2784{
2785 int ret;
2786
2787 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2788
2789 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2790 ssl->out_msglen = 1;
2791 ssl->out_msg[0] = 1;
2792
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 ssl->state++;
2794
2795 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2796 {
2797 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2798 return( ret );
2799 }
2800
2801 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2802
2803 return( 0 );
2804}
2805
2806int ssl_parse_change_cipher_spec( ssl_context *ssl )
2807{
2808 int ret;
2809
2810 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2811
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2813 {
2814 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2815 return( ret );
2816 }
2817
2818 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2819 {
2820 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002821 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 }
2823
2824 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2825 {
2826 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002827 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 }
2829
2830 ssl->state++;
2831
2832 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2833
2834 return( 0 );
2835}
2836
Paul Bakker41c83d32013-03-20 14:39:14 +01002837void ssl_optimize_checksum( ssl_context *ssl,
2838 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002839{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002840 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002841
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002842#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2843 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002844 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002845 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002846 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002847#endif
2848#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2849#if defined(POLARSSL_SHA512_C)
2850 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2851 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2852 else
2853#endif
2854#if defined(POLARSSL_SHA256_C)
2855 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002856 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002857 else
2858#endif
2859#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2860 /* Should never happen */
2861 return;
Paul Bakker380da532012-04-18 16:10:25 +00002862}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002863
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002864static void ssl_update_checksum_start( ssl_context *ssl,
2865 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002866{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002867#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2868 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002869 md5_update( &ssl->handshake->fin_md5 , buf, len );
2870 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002871#endif
2872#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2873#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002874 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002875#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002876#if defined(POLARSSL_SHA512_C)
2877 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002878#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002879#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002880}
2881
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002882#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2883 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002884static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2885 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002886{
Paul Bakker48916f92012-09-16 19:57:18 +00002887 md5_update( &ssl->handshake->fin_md5 , buf, len );
2888 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002889}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002890#endif
Paul Bakker380da532012-04-18 16:10:25 +00002891
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002892#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2893#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002894static void ssl_update_checksum_sha256( ssl_context *ssl,
2895 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002896{
Paul Bakker9e36f042013-06-30 14:34:05 +02002897 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002898}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002899#endif
Paul Bakker380da532012-04-18 16:10:25 +00002900
Paul Bakker9e36f042013-06-30 14:34:05 +02002901#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002902static void ssl_update_checksum_sha384( ssl_context *ssl,
2903 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002904{
Paul Bakker9e36f042013-06-30 14:34:05 +02002905 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002906}
Paul Bakker769075d2012-11-24 11:26:46 +01002907#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002908#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002909
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002910#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911static void ssl_calc_finished_ssl(
2912 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002913{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002914 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915 md5_context md5;
2916 sha1_context sha1;
2917
Paul Bakker5121ce52009-01-03 21:22:43 +00002918 unsigned char padbuf[48];
2919 unsigned char md5sum[16];
2920 unsigned char sha1sum[20];
2921
Paul Bakker48916f92012-09-16 19:57:18 +00002922 ssl_session *session = ssl->session_negotiate;
2923 if( !session )
2924 session = ssl->session;
2925
Paul Bakker1ef83d62012-04-11 12:09:53 +00002926 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2927
Paul Bakker48916f92012-09-16 19:57:18 +00002928 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2929 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
2931 /*
2932 * SSLv3:
2933 * hash =
2934 * MD5( master + pad2 +
2935 * MD5( handshake + sender + master + pad1 ) )
2936 * + SHA1( master + pad2 +
2937 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002938 */
2939
Paul Bakker90995b52013-06-24 19:20:35 +02002940#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002942 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002943#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Paul Bakker90995b52013-06-24 19:20:35 +02002945#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002946 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002947 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002948#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Paul Bakker3c2122f2013-06-24 19:03:14 +02002950 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2951 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002952
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
Paul Bakker3c2122f2013-06-24 19:03:14 +02002955 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002956 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002957 md5_update( &md5, padbuf, 48 );
2958 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Paul Bakker3c2122f2013-06-24 19:03:14 +02002960 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002961 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962 sha1_update( &sha1, padbuf, 40 );
2963 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Paul Bakker1ef83d62012-04-11 12:09:53 +00002967 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002968 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969 md5_update( &md5, padbuf, 48 );
2970 md5_update( &md5, md5sum, 16 );
2971 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Paul Bakker1ef83d62012-04-11 12:09:53 +00002973 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002974 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002975 sha1_update( &sha1, padbuf , 40 );
2976 sha1_update( &sha1, sha1sum, 20 );
2977 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
Paul Bakker1ef83d62012-04-11 12:09:53 +00002981 memset( &md5, 0, sizeof( md5_context ) );
2982 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002983
2984 memset( padbuf, 0, sizeof( padbuf ) );
2985 memset( md5sum, 0, sizeof( md5sum ) );
2986 memset( sha1sum, 0, sizeof( sha1sum ) );
2987
2988 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2989}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002990#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002991
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002992#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002993static void ssl_calc_finished_tls(
2994 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002995{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002996 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002997 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002998 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002999 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003000 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
Paul Bakker48916f92012-09-16 19:57:18 +00003002 ssl_session *session = ssl->session_negotiate;
3003 if( !session )
3004 session = ssl->session;
3005
Paul Bakker1ef83d62012-04-11 12:09:53 +00003006 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003007
Paul Bakker48916f92012-09-16 19:57:18 +00003008 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3009 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003010
Paul Bakker1ef83d62012-04-11 12:09:53 +00003011 /*
3012 * TLSv1:
3013 * hash = PRF( master, finished_label,
3014 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3015 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Paul Bakker90995b52013-06-24 19:20:35 +02003017#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003018 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3019 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003020#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021
Paul Bakker90995b52013-06-24 19:20:35 +02003022#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003023 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3024 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003025#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003026
3027 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003028 ? "client finished"
3029 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003030
3031 md5_finish( &md5, padbuf );
3032 sha1_finish( &sha1, padbuf + 16 );
3033
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003034 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003035 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003036
3037 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3038
3039 memset( &md5, 0, sizeof( md5_context ) );
3040 memset( &sha1, 0, sizeof( sha1_context ) );
3041
3042 memset( padbuf, 0, sizeof( padbuf ) );
3043
3044 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3045}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003046#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003047
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003048#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3049#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003050static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003051 ssl_context *ssl, unsigned char *buf, int from )
3052{
3053 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003054 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003055 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003056 unsigned char padbuf[32];
3057
Paul Bakker48916f92012-09-16 19:57:18 +00003058 ssl_session *session = ssl->session_negotiate;
3059 if( !session )
3060 session = ssl->session;
3061
Paul Bakker380da532012-04-18 16:10:25 +00003062 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063
Paul Bakker9e36f042013-06-30 14:34:05 +02003064 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003065
3066 /*
3067 * TLSv1.2:
3068 * hash = PRF( master, finished_label,
3069 * Hash( handshake ) )[0.11]
3070 */
3071
Paul Bakker9e36f042013-06-30 14:34:05 +02003072#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003073 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003074 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003075#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076
3077 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003078 ? "client finished"
3079 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003080
Paul Bakker9e36f042013-06-30 14:34:05 +02003081 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003082
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003083 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003084 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085
3086 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3087
Paul Bakker9e36f042013-06-30 14:34:05 +02003088 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003089
3090 memset( padbuf, 0, sizeof( padbuf ) );
3091
3092 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3093}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003094#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003095
Paul Bakker9e36f042013-06-30 14:34:05 +02003096#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003097static void ssl_calc_finished_tls_sha384(
3098 ssl_context *ssl, unsigned char *buf, int from )
3099{
3100 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003101 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003102 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003103 unsigned char padbuf[48];
3104
Paul Bakker48916f92012-09-16 19:57:18 +00003105 ssl_session *session = ssl->session_negotiate;
3106 if( !session )
3107 session = ssl->session;
3108
Paul Bakker380da532012-04-18 16:10:25 +00003109 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003110
Paul Bakker9e36f042013-06-30 14:34:05 +02003111 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003112
3113 /*
3114 * TLSv1.2:
3115 * hash = PRF( master, finished_label,
3116 * Hash( handshake ) )[0.11]
3117 */
3118
Paul Bakker9e36f042013-06-30 14:34:05 +02003119#if !defined(POLARSSL_SHA512_ALT)
3120 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3121 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003122#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003123
3124 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003125 ? "client finished"
3126 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003127
Paul Bakker9e36f042013-06-30 14:34:05 +02003128 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003129
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003130 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003131 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003132
3133 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3134
Paul Bakker9e36f042013-06-30 14:34:05 +02003135 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003136
3137 memset( padbuf, 0, sizeof( padbuf ) );
3138
3139 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3140}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003141#endif /* POLARSSL_SHA512_C */
3142#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003143
Paul Bakker48916f92012-09-16 19:57:18 +00003144void ssl_handshake_wrapup( ssl_context *ssl )
3145{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003146 int resume = ssl->handshake->resume;
3147
Paul Bakker48916f92012-09-16 19:57:18 +00003148 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3149
3150 /*
3151 * Free our handshake params
3152 */
3153 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003154 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003155 ssl->handshake = NULL;
3156
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003157 if( ssl->renegotiation == SSL_RENEGOTIATION )
3158 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3159
Paul Bakker48916f92012-09-16 19:57:18 +00003160 /*
3161 * Switch in our now active transform context
3162 */
3163 if( ssl->transform )
3164 {
3165 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003166 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003167 }
3168 ssl->transform = ssl->transform_negotiate;
3169 ssl->transform_negotiate = NULL;
3170
Paul Bakker0a597072012-09-25 21:55:46 +00003171 if( ssl->session )
3172 {
3173 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003174 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003175 }
3176 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003177 ssl->session_negotiate = NULL;
3178
Paul Bakker0a597072012-09-25 21:55:46 +00003179 /*
3180 * Add cache entry
3181 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003182 if( ssl->f_set_cache != NULL &&
3183 ssl->session->length != 0 &&
3184 resume == 0 )
3185 {
Paul Bakker0a597072012-09-25 21:55:46 +00003186 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3187 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003188 }
Paul Bakker0a597072012-09-25 21:55:46 +00003189
Paul Bakker48916f92012-09-16 19:57:18 +00003190 ssl->state++;
3191
3192 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3193}
3194
Paul Bakker1ef83d62012-04-11 12:09:53 +00003195int ssl_write_finished( ssl_context *ssl )
3196{
3197 int ret, hash_len;
3198
3199 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3200
Paul Bakker92be97b2013-01-02 17:30:03 +01003201 /*
3202 * Set the out_msg pointer to the correct location based on IV length
3203 */
3204 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3205 {
3206 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3207 ssl->transform_negotiate->fixed_ivlen;
3208 }
3209 else
3210 ssl->out_msg = ssl->out_iv;
3211
Paul Bakker48916f92012-09-16 19:57:18 +00003212 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003213
3214 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3216
Paul Bakker48916f92012-09-16 19:57:18 +00003217 ssl->verify_data_len = hash_len;
3218 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3219
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 ssl->out_msglen = 4 + hash_len;
3221 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3222 ssl->out_msg[0] = SSL_HS_FINISHED;
3223
3224 /*
3225 * In case of session resuming, invert the client and server
3226 * ChangeCipherSpec messages order.
3227 */
Paul Bakker0a597072012-09-25 21:55:46 +00003228 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 {
3230 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003231 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 else
3233 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3234 }
3235 else
3236 ssl->state++;
3237
Paul Bakker48916f92012-09-16 19:57:18 +00003238 /*
3239 * Switch to our negotiated transform and session parameters for outbound data.
3240 */
3241 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3242 ssl->transform_out = ssl->transform_negotiate;
3243 ssl->session_out = ssl->session_negotiate;
3244 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003245
Paul Bakker07eb38b2012-12-19 14:42:06 +01003246#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3247 if( ssl_hw_record_activate != NULL)
3248 {
3249 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3250 {
3251 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3252 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3253 }
3254 }
3255#endif
3256
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3258 {
3259 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3260 return( ret );
3261 }
3262
3263 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3264
3265 return( 0 );
3266}
3267
3268int ssl_parse_finished( ssl_context *ssl )
3269{
Paul Bakker23986e52011-04-24 08:57:21 +00003270 int ret;
3271 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003272 unsigned char buf[36];
3273
3274 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3275
Paul Bakker48916f92012-09-16 19:57:18 +00003276 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277
Paul Bakker48916f92012-09-16 19:57:18 +00003278 /*
3279 * Switch to our negotiated transform and session parameters for inbound data.
3280 */
3281 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3282 ssl->transform_in = ssl->transform_negotiate;
3283 ssl->session_in = ssl->session_negotiate;
3284 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285
Paul Bakker92be97b2013-01-02 17:30:03 +01003286 /*
3287 * Set the in_msg pointer to the correct location based on IV length
3288 */
3289 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3290 {
3291 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3292 ssl->transform_negotiate->fixed_ivlen;
3293 }
3294 else
3295 ssl->in_msg = ssl->in_iv;
3296
Paul Bakker07eb38b2012-12-19 14:42:06 +01003297#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3298 if( ssl_hw_record_activate != NULL)
3299 {
3300 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3301 {
3302 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3303 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3304 }
3305 }
3306#endif
3307
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3309 {
3310 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3311 return( ret );
3312 }
3313
3314 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3315 {
3316 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003317 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 }
3319
Paul Bakker1ef83d62012-04-11 12:09:53 +00003320 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003321 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3322
3323 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3324 ssl->in_hslen != 4 + hash_len )
3325 {
3326 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003327 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 }
3329
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003330 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 {
3332 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003333 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 }
3335
Paul Bakker48916f92012-09-16 19:57:18 +00003336 ssl->verify_data_len = hash_len;
3337 memcpy( ssl->peer_verify_data, buf, hash_len );
3338
Paul Bakker0a597072012-09-25 21:55:46 +00003339 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003340 {
3341 if( ssl->endpoint == SSL_IS_CLIENT )
3342 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3343
3344 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003345 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 }
3347 else
3348 ssl->state++;
3349
3350 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3351
3352 return( 0 );
3353}
3354
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003355static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003356{
3357 if( ssl->transform_negotiate )
3358 ssl_transform_free( ssl->transform_negotiate );
3359 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003360 {
3361 ssl->transform_negotiate =
3362 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003363
3364 if( ssl->transform_negotiate != NULL )
3365 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003366 }
Paul Bakker48916f92012-09-16 19:57:18 +00003367
3368 if( ssl->session_negotiate )
3369 ssl_session_free( ssl->session_negotiate );
3370 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003371 {
3372 ssl->session_negotiate =
3373 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003374
3375 if( ssl->session_negotiate != NULL )
3376 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003377 }
Paul Bakker48916f92012-09-16 19:57:18 +00003378
3379 if( ssl->handshake )
3380 ssl_handshake_free( ssl->handshake );
3381 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003382 {
3383 ssl->handshake = (ssl_handshake_params *)
3384 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003385
3386 if( ssl->handshake != NULL )
3387 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003388 }
Paul Bakker48916f92012-09-16 19:57:18 +00003389
3390 if( ssl->handshake == NULL ||
3391 ssl->transform_negotiate == NULL ||
3392 ssl->session_negotiate == NULL )
3393 {
3394 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3395 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3396 }
3397
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003398#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3399 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003400 md5_starts( &ssl->handshake->fin_md5 );
3401 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003402#endif
3403#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3404#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003405 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003406#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003407#if defined(POLARSSL_SHA512_C)
3408 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003409#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003410#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003411
3412 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003413 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003414
Paul Bakker61d113b2013-07-04 11:51:43 +02003415#if defined(POLARSSL_ECDH_C)
3416 ecdh_init( &ssl->handshake->ecdh_ctx );
3417#endif
3418
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003419#if defined(POLARSSL_X509_CRT_PARSE_C)
3420 ssl->handshake->key_cert = ssl->key_cert;
3421#endif
3422
Paul Bakker48916f92012-09-16 19:57:18 +00003423 return( 0 );
3424}
3425
Paul Bakker5121ce52009-01-03 21:22:43 +00003426/*
3427 * Initialize an SSL context
3428 */
3429int ssl_init( ssl_context *ssl )
3430{
Paul Bakker48916f92012-09-16 19:57:18 +00003431 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 int len = SSL_BUFFER_LEN;
3433
3434 memset( ssl, 0, sizeof( ssl_context ) );
3435
Paul Bakker62f2dee2012-09-28 07:31:51 +00003436 /*
3437 * Sane defaults
3438 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003439 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3440 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3441 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3442 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003443
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003444 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003445
Paul Bakker62f2dee2012-09-28 07:31:51 +00003446#if defined(POLARSSL_DHM_C)
3447 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3448 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3449 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3450 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3451 {
3452 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3453 return( ret );
3454 }
3455#endif
3456
3457 /*
3458 * Prepare base structures
3459 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003460 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003462 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 ssl->in_msg = ssl->in_ctr + 13;
3464
3465 if( ssl->in_ctr == NULL )
3466 {
3467 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003468 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 }
3470
Paul Bakker6e339b52013-07-03 13:37:05 +02003471 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003473 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003474 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003475
3476 if( ssl->out_ctr == NULL )
3477 {
3478 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003479 polarssl_free( ssl->in_ctr );
3480 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003481 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003482 }
3483
3484 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3485 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3486
Paul Bakker606b4ba2013-08-14 16:52:14 +02003487#if defined(POLARSSL_SSL_SESSION_TICKETS)
3488 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3489#endif
3490
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003491#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003492 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003493#endif
3494
Paul Bakker48916f92012-09-16 19:57:18 +00003495 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3496 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003497
3498 return( 0 );
3499}
3500
3501/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003502 * Reset an initialized and used SSL context for re-use while retaining
3503 * all application-set variables, function pointers and data.
3504 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003505int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003506{
Paul Bakker48916f92012-09-16 19:57:18 +00003507 int ret;
3508
Paul Bakker7eb013f2011-10-06 12:37:39 +00003509 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003510 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3511 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3512
3513 ssl->verify_data_len = 0;
3514 memset( ssl->own_verify_data, 0, 36 );
3515 memset( ssl->peer_verify_data, 0, 36 );
3516
Paul Bakker7eb013f2011-10-06 12:37:39 +00003517 ssl->in_offt = NULL;
3518
Paul Bakker92be97b2013-01-02 17:30:03 +01003519 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003520 ssl->in_msgtype = 0;
3521 ssl->in_msglen = 0;
3522 ssl->in_left = 0;
3523
3524 ssl->in_hslen = 0;
3525 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003526 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003527
Paul Bakker92be97b2013-01-02 17:30:03 +01003528 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003529 ssl->out_msgtype = 0;
3530 ssl->out_msglen = 0;
3531 ssl->out_left = 0;
3532
Paul Bakker48916f92012-09-16 19:57:18 +00003533 ssl->transform_in = NULL;
3534 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003535
3536 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3537 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003538
3539#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3540 if( ssl_hw_record_reset != NULL)
3541 {
3542 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003543 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003544 {
3545 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3546 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3547 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003548 }
3549#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003550
Paul Bakker48916f92012-09-16 19:57:18 +00003551 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003552 {
Paul Bakker48916f92012-09-16 19:57:18 +00003553 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003554 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003555 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003556 }
Paul Bakker48916f92012-09-16 19:57:18 +00003557
Paul Bakkerc0463502013-02-14 11:19:38 +01003558 if( ssl->session )
3559 {
3560 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003561 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003562 ssl->session = NULL;
3563 }
3564
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003565#if defined(POLARSSL_SSL_ALPN)
3566 ssl->alpn_chosen = NULL;
3567#endif
3568
Paul Bakker48916f92012-09-16 19:57:18 +00003569 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3570 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003571
3572 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003573}
3574
Paul Bakkera503a632013-08-14 13:48:06 +02003575#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003576/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003577 * Allocate and initialize ticket keys
3578 */
3579static int ssl_ticket_keys_init( ssl_context *ssl )
3580{
3581 int ret;
3582 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003583 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003584
3585 if( ssl->ticket_keys != NULL )
3586 return( 0 );
3587
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003588 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3589 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003590 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3591
3592 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003593 {
3594 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003595 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003596 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003597
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003598 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3599 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3600 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3601 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003602 polarssl_free( tkeys );
3603 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003604 }
3605
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003606 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003607 {
3608 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003609 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003610 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003611
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003612 ssl->ticket_keys = tkeys;
3613
3614 return( 0 );
3615}
Paul Bakkera503a632013-08-14 13:48:06 +02003616#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003617
3618/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003619 * SSL set accessors
3620 */
3621void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3622{
3623 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003624
Paul Bakker606b4ba2013-08-14 16:52:14 +02003625#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003626 if( endpoint == SSL_IS_CLIENT )
3627 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003628#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003629}
3630
3631void ssl_set_authmode( ssl_context *ssl, int authmode )
3632{
3633 ssl->authmode = authmode;
3634}
3635
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003636#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003637void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003638 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003639 void *p_vrfy )
3640{
3641 ssl->f_vrfy = f_vrfy;
3642 ssl->p_vrfy = p_vrfy;
3643}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003644#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003645
Paul Bakker5121ce52009-01-03 21:22:43 +00003646void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003647 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003648 void *p_rng )
3649{
3650 ssl->f_rng = f_rng;
3651 ssl->p_rng = p_rng;
3652}
3653
3654void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003655 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003656 void *p_dbg )
3657{
3658 ssl->f_dbg = f_dbg;
3659 ssl->p_dbg = p_dbg;
3660}
3661
3662void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003663 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003664 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003665{
3666 ssl->f_recv = f_recv;
3667 ssl->f_send = f_send;
3668 ssl->p_recv = p_recv;
3669 ssl->p_send = p_send;
3670}
3671
Paul Bakker0a597072012-09-25 21:55:46 +00003672void ssl_set_session_cache( ssl_context *ssl,
3673 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3674 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003675{
Paul Bakker0a597072012-09-25 21:55:46 +00003676 ssl->f_get_cache = f_get_cache;
3677 ssl->p_get_cache = p_get_cache;
3678 ssl->f_set_cache = f_set_cache;
3679 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003680}
3681
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003682int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003683{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003684 int ret;
3685
3686 if( ssl == NULL ||
3687 session == NULL ||
3688 ssl->session_negotiate == NULL ||
3689 ssl->endpoint != SSL_IS_CLIENT )
3690 {
3691 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3692 }
3693
3694 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3695 return( ret );
3696
Paul Bakker0a597072012-09-25 21:55:46 +00003697 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003698
3699 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003700}
3701
Paul Bakkerb68cad62012-08-23 08:34:18 +00003702void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003703{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003704 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3705 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3706 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3707 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3708}
3709
3710void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3711 int major, int minor )
3712{
3713 if( major != SSL_MAJOR_VERSION_3 )
3714 return;
3715
3716 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3717 return;
3718
3719 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003720}
3721
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003722#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003723/* Add a new (empty) key_cert entry an return a pointer to it */
3724static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3725{
3726 ssl_key_cert *key_cert, *last;
3727
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003728 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3729 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003730 return( NULL );
3731
3732 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3733
3734 /* Append the new key_cert to the (possibly empty) current list */
3735 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003736 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003737 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003738 if( ssl->handshake != NULL )
3739 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003740 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003741 else
3742 {
3743 last = ssl->key_cert;
3744 while( last->next != NULL )
3745 last = last->next;
3746 last->next = key_cert;
3747 }
3748
3749 return key_cert;
3750}
3751
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003752void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003753 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003754{
3755 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003756 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003757 ssl->peer_cn = peer_cn;
3758}
3759
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003760int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003761 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003762{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003763 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3764
3765 if( key_cert == NULL )
3766 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3767
3768 key_cert->cert = own_cert;
3769 key_cert->key = pk_key;
3770
3771 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003772}
3773
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003774#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003775int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003776 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003777{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003778 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003779 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003780
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003781 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003782 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3783
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003784 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3785 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003786 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003787
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003788 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003789
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003790 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003791 if( ret != 0 )
3792 return( ret );
3793
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003794 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003795 return( ret );
3796
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003797 key_cert->cert = own_cert;
3798 key_cert->key_own_alloc = 1;
3799
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003800 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003801}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003802#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003803
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003804int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003805 void *rsa_key,
3806 rsa_decrypt_func rsa_decrypt,
3807 rsa_sign_func rsa_sign,
3808 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003809{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003810 int ret;
3811 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003812
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003813 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003814 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3815
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003816 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3817 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003818 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003819
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003820 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003821
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003822 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3823 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3824 return( ret );
3825
3826 key_cert->cert = own_cert;
3827 key_cert->key_own_alloc = 1;
3828
3829 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003830}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003831#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003832
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003833#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003834int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3835 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003836{
Paul Bakker6db455e2013-09-18 17:29:31 +02003837 if( psk == NULL || psk_identity == NULL )
3838 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3839
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003840 /*
3841 * The length will be check later anyway, but in case it is obviously
3842 * too large, better abort now. The PMS is as follows:
3843 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3844 */
3845 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3846 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3847
Paul Bakker6db455e2013-09-18 17:29:31 +02003848 if( ssl->psk != NULL )
3849 {
3850 polarssl_free( ssl->psk );
3851 polarssl_free( ssl->psk_identity );
3852 }
3853
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003854 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003855 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003856
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003857 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3858 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003859
3860 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3861 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3862
3863 memcpy( ssl->psk, psk, ssl->psk_len );
3864 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003865
3866 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003867}
3868
3869void ssl_set_psk_cb( ssl_context *ssl,
3870 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3871 size_t),
3872 void *p_psk )
3873{
3874 ssl->f_psk = f_psk;
3875 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003876}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003877#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003878
Paul Bakker48916f92012-09-16 19:57:18 +00003879#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003880int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003881{
3882 int ret;
3883
Paul Bakker48916f92012-09-16 19:57:18 +00003884 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003885 {
3886 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3887 return( ret );
3888 }
3889
Paul Bakker48916f92012-09-16 19:57:18 +00003890 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003891 {
3892 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3893 return( ret );
3894 }
3895
3896 return( 0 );
3897}
3898
Paul Bakker1b57b062011-01-06 15:48:19 +00003899int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3900{
3901 int ret;
3902
Paul Bakker48916f92012-09-16 19:57:18 +00003903 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003904 {
3905 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3906 return( ret );
3907 }
3908
Paul Bakker48916f92012-09-16 19:57:18 +00003909 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003910 {
3911 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3912 return( ret );
3913 }
3914
3915 return( 0 );
3916}
Paul Bakker48916f92012-09-16 19:57:18 +00003917#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003918
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003919#if defined(POLARSSL_SSL_SET_CURVES)
3920/*
3921 * Set the allowed elliptic curves
3922 */
3923void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3924{
3925 ssl->curve_list = curve_list;
3926}
3927#endif
3928
Paul Bakker0be444a2013-08-27 21:55:01 +02003929#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003930int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003931{
3932 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003933 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003934
3935 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003936
3937 if( ssl->hostname_len + 1 == 0 )
3938 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3939
Paul Bakker6e339b52013-07-03 13:37:05 +02003940 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003941
Paul Bakkerb15b8512012-01-13 13:44:06 +00003942 if( ssl->hostname == NULL )
3943 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3944
Paul Bakker3c2122f2013-06-24 19:03:14 +02003945 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003946 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003947
Paul Bakker40ea7de2009-05-03 10:18:48 +00003948 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003949
3950 return( 0 );
3951}
3952
Paul Bakker5701cdc2012-09-27 21:49:42 +00003953void ssl_set_sni( ssl_context *ssl,
3954 int (*f_sni)(void *, ssl_context *,
3955 const unsigned char *, size_t),
3956 void *p_sni )
3957{
3958 ssl->f_sni = f_sni;
3959 ssl->p_sni = p_sni;
3960}
Paul Bakker0be444a2013-08-27 21:55:01 +02003961#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003962
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003963#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003964int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003965{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003966 size_t cur_len, tot_len;
3967 const char **p;
3968
3969 /*
3970 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3971 * truncated". Check lengths now rather than later.
3972 */
3973 tot_len = 0;
3974 for( p = protos; *p != NULL; p++ )
3975 {
3976 cur_len = strlen( *p );
3977 tot_len += cur_len;
3978
3979 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3980 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3981 }
3982
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003983 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003984
3985 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003986}
3987
3988const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3989{
3990 return ssl->alpn_chosen;
3991}
3992#endif /* POLARSSL_SSL_ALPN */
3993
Paul Bakker490ecc82011-10-06 13:04:09 +00003994void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3995{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003996 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3997 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3998 {
3999 ssl->max_major_ver = major;
4000 ssl->max_minor_ver = minor;
4001 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004002}
4003
Paul Bakker1d29fb52012-09-28 13:28:45 +00004004void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4005{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004006 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4007 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4008 {
4009 ssl->min_major_ver = major;
4010 ssl->min_minor_ver = minor;
4011 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004012}
4013
Paul Bakker05decb22013-08-15 13:33:48 +02004014#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004015int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4016{
Paul Bakker77e257e2013-12-16 15:29:52 +01004017 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004018 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004019 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004020 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004021 }
4022
4023 ssl->mfl_code = mfl_code;
4024
4025 return( 0 );
4026}
Paul Bakker05decb22013-08-15 13:33:48 +02004027#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004028
Paul Bakker1f2bc622013-08-15 13:45:55 +02004029#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004030int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004031{
4032 if( ssl->endpoint != SSL_IS_CLIENT )
4033 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4034
Paul Bakker8c1ede62013-07-19 14:14:37 +02004035 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004036
4037 return( 0 );
4038}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004039#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004040
Paul Bakker48916f92012-09-16 19:57:18 +00004041void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4042{
4043 ssl->disable_renegotiation = renegotiation;
4044}
4045
4046void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4047{
4048 ssl->allow_legacy_renegotiation = allow_legacy;
4049}
4050
Paul Bakkera503a632013-08-14 13:48:06 +02004051#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004052int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4053{
4054 ssl->session_tickets = use_tickets;
4055
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004056 if( ssl->endpoint == SSL_IS_CLIENT )
4057 return( 0 );
4058
4059 if( ssl->f_rng == NULL )
4060 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4061
4062 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004063}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004064
4065void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4066{
4067 ssl->ticket_lifetime = lifetime;
4068}
Paul Bakkera503a632013-08-14 13:48:06 +02004069#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004070
Paul Bakker5121ce52009-01-03 21:22:43 +00004071/*
4072 * SSL get accessors
4073 */
Paul Bakker23986e52011-04-24 08:57:21 +00004074size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004075{
4076 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4077}
4078
Paul Bakkerff60ee62010-03-16 21:09:09 +00004079int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004080{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004081 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004082}
4083
Paul Bakkere3166ce2011-01-27 17:40:50 +00004084const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004085{
Paul Bakker926c8e42013-03-06 10:23:34 +01004086 if( ssl == NULL || ssl->session == NULL )
4087 return NULL;
4088
Paul Bakkere3166ce2011-01-27 17:40:50 +00004089 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004090}
4091
Paul Bakker43ca69c2011-01-15 17:35:19 +00004092const char *ssl_get_version( const ssl_context *ssl )
4093{
4094 switch( ssl->minor_ver )
4095 {
4096 case SSL_MINOR_VERSION_0:
4097 return( "SSLv3.0" );
4098
4099 case SSL_MINOR_VERSION_1:
4100 return( "TLSv1.0" );
4101
4102 case SSL_MINOR_VERSION_2:
4103 return( "TLSv1.1" );
4104
Paul Bakker1ef83d62012-04-11 12:09:53 +00004105 case SSL_MINOR_VERSION_3:
4106 return( "TLSv1.2" );
4107
Paul Bakker43ca69c2011-01-15 17:35:19 +00004108 default:
4109 break;
4110 }
4111 return( "unknown" );
4112}
4113
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004114#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004115const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004116{
4117 if( ssl == NULL || ssl->session == NULL )
4118 return NULL;
4119
4120 return ssl->session->peer_cert;
4121}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004122#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004123
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004124int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4125{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004126 if( ssl == NULL ||
4127 dst == NULL ||
4128 ssl->session == NULL ||
4129 ssl->endpoint != SSL_IS_CLIENT )
4130 {
4131 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4132 }
4133
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004134 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004135}
4136
Paul Bakker5121ce52009-01-03 21:22:43 +00004137/*
Paul Bakker1961b702013-01-25 14:49:24 +01004138 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004139 */
Paul Bakker1961b702013-01-25 14:49:24 +01004140int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004141{
Paul Bakker40e46942009-01-03 21:51:57 +00004142 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004143
Paul Bakker40e46942009-01-03 21:51:57 +00004144#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004145 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004146 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004147#endif
4148
Paul Bakker40e46942009-01-03 21:51:57 +00004149#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004150 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004151 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004152#endif
4153
Paul Bakker1961b702013-01-25 14:49:24 +01004154 return( ret );
4155}
4156
4157/*
4158 * Perform the SSL handshake
4159 */
4160int ssl_handshake( ssl_context *ssl )
4161{
4162 int ret = 0;
4163
4164 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4165
4166 while( ssl->state != SSL_HANDSHAKE_OVER )
4167 {
4168 ret = ssl_handshake_step( ssl );
4169
4170 if( ret != 0 )
4171 break;
4172 }
4173
Paul Bakker5121ce52009-01-03 21:22:43 +00004174 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4175
4176 return( ret );
4177}
4178
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004179#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004180/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004181 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004182 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004183static int ssl_write_hello_request( ssl_context *ssl )
4184{
4185 int ret;
4186
4187 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4188
4189 ssl->out_msglen = 4;
4190 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4191 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4192
4193 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4194 {
4195 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4196 return( ret );
4197 }
4198
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004199 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4200
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004201 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4202
4203 return( 0 );
4204}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004205#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004206
4207/*
4208 * Actually renegotiate current connection, triggered by either:
4209 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004210 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004211 * - receiving any handshake message on server during ssl_read() after the
4212 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004213 * If the handshake doesn't complete due to waiting for I/O, it will continue
4214 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004215 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004216static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004217{
4218 int ret;
4219
4220 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4221
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004222 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4223 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004224
4225 ssl->state = SSL_HELLO_REQUEST;
4226 ssl->renegotiation = SSL_RENEGOTIATION;
4227
Paul Bakker48916f92012-09-16 19:57:18 +00004228 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4229 {
4230 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4231 return( ret );
4232 }
4233
4234 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4235
4236 return( 0 );
4237}
4238
4239/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004240 * Renegotiate current connection on client,
4241 * or request renegotiation on server
4242 */
4243int ssl_renegotiate( ssl_context *ssl )
4244{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004245 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004246
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004247#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004248 /* On server, just send the request */
4249 if( ssl->endpoint == SSL_IS_SERVER )
4250 {
4251 if( ssl->state != SSL_HANDSHAKE_OVER )
4252 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4253
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004254 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004255 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004256#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004257
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004258#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004259 /*
4260 * On client, either start the renegotiation process or,
4261 * if already in progress, continue the handshake
4262 */
4263 if( ssl->renegotiation != SSL_RENEGOTIATION )
4264 {
4265 if( ssl->state != SSL_HANDSHAKE_OVER )
4266 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4267
4268 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4269 {
4270 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4271 return( ret );
4272 }
4273 }
4274 else
4275 {
4276 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4277 {
4278 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4279 return( ret );
4280 }
4281 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004282#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004283
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004284 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004285}
4286
4287/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004288 * Receive application data decrypted from the SSL layer
4289 */
Paul Bakker23986e52011-04-24 08:57:21 +00004290int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004291{
Paul Bakker23986e52011-04-24 08:57:21 +00004292 int ret;
4293 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004294
4295 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4296
4297 if( ssl->state != SSL_HANDSHAKE_OVER )
4298 {
4299 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4300 {
4301 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4302 return( ret );
4303 }
4304 }
4305
4306 if( ssl->in_offt == NULL )
4307 {
4308 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4309 {
Paul Bakker831a7552011-05-18 13:32:51 +00004310 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4311 return( 0 );
4312
Paul Bakker5121ce52009-01-03 21:22:43 +00004313 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4314 return( ret );
4315 }
4316
4317 if( ssl->in_msglen == 0 &&
4318 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4319 {
4320 /*
4321 * OpenSSL sends empty messages to randomize the IV
4322 */
4323 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4324 {
Paul Bakker831a7552011-05-18 13:32:51 +00004325 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4326 return( 0 );
4327
Paul Bakker5121ce52009-01-03 21:22:43 +00004328 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4329 return( ret );
4330 }
4331 }
4332
Paul Bakker48916f92012-09-16 19:57:18 +00004333 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4334 {
4335 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4336
4337 if( ssl->endpoint == SSL_IS_CLIENT &&
4338 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4339 ssl->in_hslen != 4 ) )
4340 {
4341 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4342 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4343 }
4344
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004345 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4346 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4347 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004348 {
4349 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4350
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004351#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004352 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004353 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004354 /*
4355 * SSLv3 does not have a "no_renegotiation" alert
4356 */
4357 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4358 return( ret );
4359 }
4360 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004361#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004362#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4363 defined(POLARSSL_SSL_PROTO_TLS1_2)
4364 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004365 {
4366 if( ( ret = ssl_send_alert_message( ssl,
4367 SSL_ALERT_LEVEL_WARNING,
4368 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4369 {
4370 return( ret );
4371 }
Paul Bakker48916f92012-09-16 19:57:18 +00004372 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004373 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004374#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4375 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004376 {
4377 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004378 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004379 }
Paul Bakker48916f92012-09-16 19:57:18 +00004380 }
4381 else
4382 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004383 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004384 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004385 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004386 return( ret );
4387 }
4388
4389 return( POLARSSL_ERR_NET_WANT_READ );
4390 }
4391 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004392 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4393 {
4394 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4395 "but not honored by client" ) );
4396 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4397 }
Paul Bakker48916f92012-09-16 19:57:18 +00004398 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004399 {
4400 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004401 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004402 }
4403
4404 ssl->in_offt = ssl->in_msg;
4405 }
4406
4407 n = ( len < ssl->in_msglen )
4408 ? len : ssl->in_msglen;
4409
4410 memcpy( buf, ssl->in_offt, n );
4411 ssl->in_msglen -= n;
4412
4413 if( ssl->in_msglen == 0 )
4414 /* all bytes consumed */
4415 ssl->in_offt = NULL;
4416 else
4417 /* more data available */
4418 ssl->in_offt += n;
4419
4420 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4421
Paul Bakker23986e52011-04-24 08:57:21 +00004422 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004423}
4424
4425/*
4426 * Send application data to be encrypted by the SSL layer
4427 */
Paul Bakker23986e52011-04-24 08:57:21 +00004428int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004429{
Paul Bakker23986e52011-04-24 08:57:21 +00004430 int ret;
4431 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004432 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004433
4434 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4435
4436 if( ssl->state != SSL_HANDSHAKE_OVER )
4437 {
4438 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4439 {
4440 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4441 return( ret );
4442 }
4443 }
4444
Paul Bakker05decb22013-08-15 13:33:48 +02004445#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004446 /*
4447 * Assume mfl_code is correct since it was checked when set
4448 */
4449 max_len = mfl_code_to_length[ssl->mfl_code];
4450
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004451 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004452 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004453 */
4454 if( ssl->session_out != NULL &&
4455 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4456 {
4457 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4458 }
Paul Bakker05decb22013-08-15 13:33:48 +02004459#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004460
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004461 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004462
Paul Bakker5121ce52009-01-03 21:22:43 +00004463 if( ssl->out_left != 0 )
4464 {
4465 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4466 {
4467 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4468 return( ret );
4469 }
4470 }
Paul Bakker887bd502011-06-08 13:10:54 +00004471 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004472 {
Paul Bakker887bd502011-06-08 13:10:54 +00004473 ssl->out_msglen = n;
4474 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4475 memcpy( ssl->out_msg, buf, n );
4476
4477 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4478 {
4479 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4480 return( ret );
4481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004482 }
4483
4484 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4485
Paul Bakker23986e52011-04-24 08:57:21 +00004486 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004487}
4488
4489/*
4490 * Notify the peer that the connection is being closed
4491 */
4492int ssl_close_notify( ssl_context *ssl )
4493{
4494 int ret;
4495
4496 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4497
4498 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4499 {
4500 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4501 return( ret );
4502 }
4503
4504 if( ssl->state == SSL_HANDSHAKE_OVER )
4505 {
Paul Bakker48916f92012-09-16 19:57:18 +00004506 if( ( ret = ssl_send_alert_message( ssl,
4507 SSL_ALERT_LEVEL_WARNING,
4508 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004509 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004510 return( ret );
4511 }
4512 }
4513
4514 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4515
4516 return( ret );
4517}
4518
Paul Bakker48916f92012-09-16 19:57:18 +00004519void ssl_transform_free( ssl_transform *transform )
4520{
4521#if defined(POLARSSL_ZLIB_SUPPORT)
4522 deflateEnd( &transform->ctx_deflate );
4523 inflateEnd( &transform->ctx_inflate );
4524#endif
4525
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004526 cipher_free_ctx( &transform->cipher_ctx_enc );
4527 cipher_free_ctx( &transform->cipher_ctx_dec );
4528
Paul Bakker61d113b2013-07-04 11:51:43 +02004529 md_free_ctx( &transform->md_ctx_enc );
4530 md_free_ctx( &transform->md_ctx_dec );
4531
Paul Bakker48916f92012-09-16 19:57:18 +00004532 memset( transform, 0, sizeof( ssl_transform ) );
4533}
4534
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004535#if defined(POLARSSL_X509_CRT_PARSE_C)
4536static void ssl_key_cert_free( ssl_key_cert *key_cert )
4537{
4538 ssl_key_cert *cur = key_cert, *next;
4539
4540 while( cur != NULL )
4541 {
4542 next = cur->next;
4543
4544 if( cur->key_own_alloc )
4545 {
4546 pk_free( cur->key );
4547 polarssl_free( cur->key );
4548 }
4549 polarssl_free( cur );
4550
4551 cur = next;
4552 }
4553}
4554#endif /* POLARSSL_X509_CRT_PARSE_C */
4555
Paul Bakker48916f92012-09-16 19:57:18 +00004556void ssl_handshake_free( ssl_handshake_params *handshake )
4557{
4558#if defined(POLARSSL_DHM_C)
4559 dhm_free( &handshake->dhm_ctx );
4560#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004561#if defined(POLARSSL_ECDH_C)
4562 ecdh_free( &handshake->ecdh_ctx );
4563#endif
4564
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004565#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004566 /* explicit void pointer cast for buggy MS compiler */
4567 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004568#endif
4569
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004570#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4571 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4572 /*
4573 * Free only the linked list wrapper, not the keys themselves
4574 * since the belong to the SNI callback
4575 */
4576 if( handshake->sni_key_cert != NULL )
4577 {
4578 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4579
4580 while( cur != NULL )
4581 {
4582 next = cur->next;
4583 polarssl_free( cur );
4584 cur = next;
4585 }
4586 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004587#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004588
Paul Bakker48916f92012-09-16 19:57:18 +00004589 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4590}
4591
4592void ssl_session_free( ssl_session *session )
4593{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004594#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004595 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004596 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004597 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004598 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004599 }
Paul Bakkered27a042013-04-18 22:46:23 +02004600#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004601
Paul Bakkera503a632013-08-14 13:48:06 +02004602#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004603 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004604#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004605
Paul Bakker0a597072012-09-25 21:55:46 +00004606 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004607}
4608
Paul Bakker5121ce52009-01-03 21:22:43 +00004609/*
4610 * Free an SSL context
4611 */
4612void ssl_free( ssl_context *ssl )
4613{
4614 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4615
Paul Bakker5121ce52009-01-03 21:22:43 +00004616 if( ssl->out_ctr != NULL )
4617 {
4618 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004619 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004620 }
4621
4622 if( ssl->in_ctr != NULL )
4623 {
4624 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004625 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 }
4627
Paul Bakker16770332013-10-11 09:59:44 +02004628#if defined(POLARSSL_ZLIB_SUPPORT)
4629 if( ssl->compress_buf != NULL )
4630 {
4631 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4632 polarssl_free( ssl->compress_buf );
4633 }
4634#endif
4635
Paul Bakker40e46942009-01-03 21:51:57 +00004636#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004637 mpi_free( &ssl->dhm_P );
4638 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004639#endif
4640
Paul Bakker48916f92012-09-16 19:57:18 +00004641 if( ssl->transform )
4642 {
4643 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004644 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004645 }
4646
4647 if( ssl->handshake )
4648 {
4649 ssl_handshake_free( ssl->handshake );
4650 ssl_transform_free( ssl->transform_negotiate );
4651 ssl_session_free( ssl->session_negotiate );
4652
Paul Bakker6e339b52013-07-03 13:37:05 +02004653 polarssl_free( ssl->handshake );
4654 polarssl_free( ssl->transform_negotiate );
4655 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004656 }
4657
Paul Bakkerc0463502013-02-14 11:19:38 +01004658 if( ssl->session )
4659 {
4660 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004661 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004662 }
4663
Paul Bakkera503a632013-08-14 13:48:06 +02004664#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004665 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004666#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004667
Paul Bakker0be444a2013-08-27 21:55:01 +02004668#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004669 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004670 {
4671 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004672 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004673 ssl->hostname_len = 0;
4674 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004675#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004676
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004677#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004678 if( ssl->psk != NULL )
4679 {
4680 memset( ssl->psk, 0, ssl->psk_len );
4681 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4682 polarssl_free( ssl->psk );
4683 polarssl_free( ssl->psk_identity );
4684 ssl->psk_len = 0;
4685 ssl->psk_identity_len = 0;
4686 }
4687#endif
4688
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004689#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004690 ssl_key_cert_free( ssl->key_cert );
4691#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004692
Paul Bakker05ef8352012-05-08 09:17:57 +00004693#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4694 if( ssl_hw_record_finish != NULL )
4695 {
4696 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4697 ssl_hw_record_finish( ssl );
4698 }
4699#endif
4700
Paul Bakker5121ce52009-01-03 21:22:43 +00004701 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004702
Paul Bakker86f04f42013-02-14 11:20:09 +01004703 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004704 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004705}
4706
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004707#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004708/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004709 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004710 */
4711unsigned char ssl_sig_from_pk( pk_context *pk )
4712{
4713#if defined(POLARSSL_RSA_C)
4714 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4715 return( SSL_SIG_RSA );
4716#endif
4717#if defined(POLARSSL_ECDSA_C)
4718 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4719 return( SSL_SIG_ECDSA );
4720#endif
4721 return( SSL_SIG_ANON );
4722}
4723
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004724pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4725{
4726 switch( sig )
4727 {
4728#if defined(POLARSSL_RSA_C)
4729 case SSL_SIG_RSA:
4730 return( POLARSSL_PK_RSA );
4731#endif
4732#if defined(POLARSSL_ECDSA_C)
4733 case SSL_SIG_ECDSA:
4734 return( POLARSSL_PK_ECDSA );
4735#endif
4736 default:
4737 return( POLARSSL_PK_NONE );
4738 }
4739}
Paul Bakker9af723c2014-05-01 13:03:14 +02004740#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004741
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004742/*
4743 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4744 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004745md_type_t ssl_md_alg_from_hash( unsigned char hash )
4746{
4747 switch( hash )
4748 {
4749#if defined(POLARSSL_MD5_C)
4750 case SSL_HASH_MD5:
4751 return( POLARSSL_MD_MD5 );
4752#endif
4753#if defined(POLARSSL_SHA1_C)
4754 case SSL_HASH_SHA1:
4755 return( POLARSSL_MD_SHA1 );
4756#endif
4757#if defined(POLARSSL_SHA256_C)
4758 case SSL_HASH_SHA224:
4759 return( POLARSSL_MD_SHA224 );
4760 case SSL_HASH_SHA256:
4761 return( POLARSSL_MD_SHA256 );
4762#endif
4763#if defined(POLARSSL_SHA512_C)
4764 case SSL_HASH_SHA384:
4765 return( POLARSSL_MD_SHA384 );
4766 case SSL_HASH_SHA512:
4767 return( POLARSSL_MD_SHA512 );
4768#endif
4769 default:
4770 return( POLARSSL_MD_NONE );
4771 }
4772}
4773
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004774#if defined(POLARSSL_SSL_SET_CURVES)
4775/*
4776 * Check is a curve proposed by the peer is in our list.
4777 * Return 1 if we're willing to use it, 0 otherwise.
4778 */
4779int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4780{
4781 const ecp_group_id *gid;
4782
4783 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4784 if( *gid == grp_id )
4785 return( 1 );
4786
4787 return( 0 );
4788}
Paul Bakker9af723c2014-05-01 13:03:14 +02004789#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004790
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004791#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004792int ssl_check_cert_usage( const x509_crt *cert,
4793 const ssl_ciphersuite_t *ciphersuite,
4794 int cert_endpoint )
4795{
4796#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4797 int usage = 0;
4798#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004799#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4800 const char *ext_oid;
4801 size_t ext_len;
4802#endif
4803
4804#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4805 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4806 ((void) cert);
4807 ((void) cert_endpoint);
4808#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004809
4810#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4811 if( cert_endpoint == SSL_IS_SERVER )
4812 {
4813 /* Server part of the key exchange */
4814 switch( ciphersuite->key_exchange )
4815 {
4816 case POLARSSL_KEY_EXCHANGE_RSA:
4817 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4818 usage = KU_KEY_ENCIPHERMENT;
4819 break;
4820
4821 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4822 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4823 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4824 usage = KU_DIGITAL_SIGNATURE;
4825 break;
4826
4827 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4828 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4829 usage = KU_KEY_AGREEMENT;
4830 break;
4831
4832 /* Don't use default: we want warnings when adding new values */
4833 case POLARSSL_KEY_EXCHANGE_NONE:
4834 case POLARSSL_KEY_EXCHANGE_PSK:
4835 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4836 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4837 usage = 0;
4838 }
4839 }
4840 else
4841 {
4842 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4843 usage = KU_DIGITAL_SIGNATURE;
4844 }
4845
4846 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4847 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004848#else
4849 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004850#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4851
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004852#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4853 if( cert_endpoint == SSL_IS_SERVER )
4854 {
4855 ext_oid = OID_SERVER_AUTH;
4856 ext_len = OID_SIZE( OID_SERVER_AUTH );
4857 }
4858 else
4859 {
4860 ext_oid = OID_CLIENT_AUTH;
4861 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4862 }
4863
4864 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4865 return( -1 );
4866#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4867
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004868 return( 0 );
4869}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004870#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004871
4872#endif /* POLARSSL_SSL_TLS_C */