blob: cbbbd5bd7ccc0da248b1ed30c37dab8a6f680995 [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
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker0be444a2013-08-27 21:55:01 +020038#include "polarssl/debug.h"
39#include "polarssl/ssl.h"
40
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020041#if defined(POLARSSL_X509_CRT_PARSE_C) && \
42 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
43#include "polarssl/oid.h"
44#endif
45
Paul Bakker7dc4c442014-02-01 22:50:26 +010046#if defined(POLARSSL_PLATFORM_C)
47#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020048#else
49#define polarssl_malloc malloc
50#define polarssl_free free
51#endif
52
Paul Bakker5121ce52009-01-03 21:22:43 +000053#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Paul Bakker6edcd412013-10-29 15:22:54 +010055#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
56 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000057#define strcasecmp _stricmp
58#endif
59
Paul Bakker05decb22013-08-15 13:33:48 +020060#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020061/*
62 * Convert max_fragment_length codes to length.
63 * RFC 6066 says:
64 * enum{
65 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
66 * } MaxFragmentLength;
67 * and we add 0 -> extension unused
68 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020069static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070{
71 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
72 512, /* SSL_MAX_FRAG_LEN_512 */
73 1024, /* SSL_MAX_FRAG_LEN_1024 */
74 2048, /* SSL_MAX_FRAG_LEN_2048 */
75 4096, /* SSL_MAX_FRAG_LEN_4096 */
76};
Paul Bakker05decb22013-08-15 13:33:48 +020077#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020078
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020079static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
80{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020081 ssl_session_free( dst );
82 memcpy( dst, src, sizeof( ssl_session ) );
83
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020085 if( src->peer_cert != NULL )
86 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020087 int ret;
88
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020089 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
90 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020091 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
92
Paul Bakkerb6b09562013-09-18 14:17:41 +020093 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094
Paul Bakkerddf26b42013-09-18 13:46:23 +020095 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
96 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020097 {
98 polarssl_free( dst->peer_cert );
99 dst->peer_cert = NULL;
100 return( ret );
101 }
102 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200104
Paul Bakkera503a632013-08-14 13:48:06 +0200105#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 if( src->ticket != NULL )
107 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200108 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
109 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
111
112 memcpy( dst->ticket, src->ticket, src->ticket_len );
113 }
Paul Bakkera503a632013-08-14 13:48:06 +0200114#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115
116 return( 0 );
117}
118
Paul Bakker05ef8352012-05-08 09:17:57 +0000119#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
120int (*ssl_hw_record_init)(ssl_context *ssl,
121 const unsigned char *key_enc, const unsigned char *key_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100122 size_t keylen,
Paul Bakker05ef8352012-05-08 09:17:57 +0000123 const unsigned char *iv_enc, const unsigned char *iv_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100124 size_t ivlen,
125 const unsigned char *mac_enc, const unsigned char *mac_dec,
126 size_t maclen) = NULL;
127int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000128int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
129int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
130int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
131int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
132#endif
133
Paul Bakker5121ce52009-01-03 21:22:43 +0000134/*
135 * Key material generation
136 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200137#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200138static int ssl3_prf( const unsigned char *secret, size_t slen,
139 const char *label,
140 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000141 unsigned char *dstbuf, size_t dlen )
142{
143 size_t i;
144 md5_context md5;
145 sha1_context sha1;
146 unsigned char padding[16];
147 unsigned char sha1sum[20];
148 ((void)label);
149
150 /*
151 * SSLv3:
152 * block =
153 * MD5( secret + SHA1( 'A' + secret + random ) ) +
154 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
155 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
156 * ...
157 */
158 for( i = 0; i < dlen / 16; i++ )
159 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200160 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000161
162 sha1_starts( &sha1 );
163 sha1_update( &sha1, padding, 1 + i );
164 sha1_update( &sha1, secret, slen );
165 sha1_update( &sha1, random, rlen );
166 sha1_finish( &sha1, sha1sum );
167
168 md5_starts( &md5 );
169 md5_update( &md5, secret, slen );
170 md5_update( &md5, sha1sum, 20 );
171 md5_finish( &md5, dstbuf + i * 16 );
172 }
173
174 memset( &md5, 0, sizeof( md5 ) );
175 memset( &sha1, 0, sizeof( sha1 ) );
176
177 memset( padding, 0, sizeof( padding ) );
178 memset( sha1sum, 0, sizeof( sha1sum ) );
179
180 return( 0 );
181}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200182#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000183
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200184#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200185static int tls1_prf( const unsigned char *secret, size_t slen,
186 const char *label,
187 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000188 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000189{
Paul Bakker23986e52011-04-24 08:57:21 +0000190 size_t nb, hs;
191 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200192 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 unsigned char tmp[128];
194 unsigned char h_i[20];
195
196 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000197 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199 hs = ( slen + 1 ) / 2;
200 S1 = secret;
201 S2 = secret + slen - hs;
202
203 nb = strlen( label );
204 memcpy( tmp + 20, label, nb );
205 memcpy( tmp + 20 + nb, random, rlen );
206 nb += rlen;
207
208 /*
209 * First compute P_md5(secret,label+random)[0..dlen]
210 */
211 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
212
213 for( i = 0; i < dlen; i += 16 )
214 {
215 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
216 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
217
218 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
219
220 for( j = 0; j < k; j++ )
221 dstbuf[i + j] = h_i[j];
222 }
223
224 /*
225 * XOR out with P_sha1(secret,label+random)[0..dlen]
226 */
227 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
228
229 for( i = 0; i < dlen; i += 20 )
230 {
231 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
232 sha1_hmac( S2, hs, tmp, 20, tmp );
233
234 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
235
236 for( j = 0; j < k; j++ )
237 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
238 }
239
240 memset( tmp, 0, sizeof( tmp ) );
241 memset( h_i, 0, sizeof( h_i ) );
242
243 return( 0 );
244}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200245#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200247#if defined(POLARSSL_SSL_PROTO_TLS1_2)
248#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200249static int tls_prf_sha256( const unsigned char *secret, size_t slen,
250 const char *label,
251 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000252 unsigned char *dstbuf, size_t dlen )
253{
254 size_t nb;
255 size_t i, j, k;
256 unsigned char tmp[128];
257 unsigned char h_i[32];
258
259 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
260 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
261
262 nb = strlen( label );
263 memcpy( tmp + 32, label, nb );
264 memcpy( tmp + 32 + nb, random, rlen );
265 nb += rlen;
266
267 /*
268 * Compute P_<hash>(secret, label + random)[0..dlen]
269 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200270 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000271
272 for( i = 0; i < dlen; i += 32 )
273 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200274 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
275 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000276
277 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
278
279 for( j = 0; j < k; j++ )
280 dstbuf[i + j] = h_i[j];
281 }
282
283 memset( tmp, 0, sizeof( tmp ) );
284 memset( h_i, 0, sizeof( h_i ) );
285
286 return( 0 );
287}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200288#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000289
Paul Bakker9e36f042013-06-30 14:34:05 +0200290#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200291static int tls_prf_sha384( const unsigned char *secret, size_t slen,
292 const char *label,
293 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000294 unsigned char *dstbuf, size_t dlen )
295{
296 size_t nb;
297 size_t i, j, k;
298 unsigned char tmp[128];
299 unsigned char h_i[48];
300
301 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
302 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
303
304 nb = strlen( label );
305 memcpy( tmp + 48, label, nb );
306 memcpy( tmp + 48 + nb, random, rlen );
307 nb += rlen;
308
309 /*
310 * Compute P_<hash>(secret, label + random)[0..dlen]
311 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200312 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000313
314 for( i = 0; i < dlen; i += 48 )
315 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200316 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
317 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000318
319 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
320
321 for( j = 0; j < k; j++ )
322 dstbuf[i + j] = h_i[j];
323 }
324
325 memset( tmp, 0, sizeof( tmp ) );
326 memset( h_i, 0, sizeof( h_i ) );
327
328 return( 0 );
329}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200330#endif /* POLARSSL_SHA512_C */
331#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000332
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200333static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200334
335#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
336 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200337static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338#endif
Paul Bakker380da532012-04-18 16:10:25 +0000339
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200340#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000341static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343#endif
344
345#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
346static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000347static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200348#endif
349
350#if defined(POLARSSL_SSL_PROTO_TLS1_2)
351#if defined(POLARSSL_SHA256_C)
352static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
353static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000354static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100356
Paul Bakker9e36f042013-06-30 14:34:05 +0200357#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200358static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100359static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000360static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100361#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200362#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +0000363
Paul Bakker5121ce52009-01-03 21:22:43 +0000364int ssl_derive_keys( ssl_context *ssl )
365{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200366 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 unsigned char keyblk[256];
369 unsigned char *key1;
370 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100371 unsigned char *mac_enc;
372 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200373 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100374 const cipher_info_t *cipher_info;
375 const md_info_t *md_info;
376
Paul Bakker48916f92012-09-16 19:57:18 +0000377 ssl_session *session = ssl->session_negotiate;
378 ssl_transform *transform = ssl->transform_negotiate;
379 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
381 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
382
Paul Bakker68884e32013-01-07 18:20:04 +0100383 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
384 if( cipher_info == NULL )
385 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200386 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100387 transform->ciphersuite_info->cipher ) );
388 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
389 }
390
391 md_info = md_info_from_type( transform->ciphersuite_info->mac );
392 if( md_info == NULL )
393 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200394 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100395 transform->ciphersuite_info->mac ) );
396 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
397 }
398
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000400 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000401 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200402#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000403 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404 {
Paul Bakker48916f92012-09-16 19:57:18 +0000405 handshake->tls_prf = ssl3_prf;
406 handshake->calc_verify = ssl_calc_verify_ssl;
407 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200409 else
410#endif
411#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
412 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413 {
Paul Bakker48916f92012-09-16 19:57:18 +0000414 handshake->tls_prf = tls1_prf;
415 handshake->calc_verify = ssl_calc_verify_tls;
416 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418 else
419#endif
420#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200421#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200422 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
423 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000424 {
Paul Bakker48916f92012-09-16 19:57:18 +0000425 handshake->tls_prf = tls_prf_sha384;
426 handshake->calc_verify = ssl_calc_verify_tls_sha384;
427 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000429 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430#endif
431#if defined(POLARSSL_SHA256_C)
432 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000433 {
Paul Bakker48916f92012-09-16 19:57:18 +0000434 handshake->tls_prf = tls_prf_sha256;
435 handshake->calc_verify = ssl_calc_verify_tls_sha256;
436 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200438 else
439#endif
440#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200441 {
442 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200443 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200444 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000445
446 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 * SSLv3:
448 * master =
449 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
450 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
451 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200452 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200453 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 * master = PRF( premaster, "master secret", randbytes )[0..47]
455 */
Paul Bakker0a597072012-09-25 21:55:46 +0000456 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 {
Paul Bakker48916f92012-09-16 19:57:18 +0000458 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
459 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
Paul Bakker48916f92012-09-16 19:57:18 +0000461 handshake->tls_prf( handshake->premaster, handshake->pmslen,
462 "master secret",
463 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Paul Bakker48916f92012-09-16 19:57:18 +0000465 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 }
467 else
468 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
469
470 /*
471 * Swap the client and server random values.
472 */
Paul Bakker48916f92012-09-16 19:57:18 +0000473 memcpy( tmp, handshake->randbytes, 64 );
474 memcpy( handshake->randbytes, tmp + 32, 32 );
475 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 memset( tmp, 0, sizeof( tmp ) );
477
478 /*
479 * SSLv3:
480 * key block =
481 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
482 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
483 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
484 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
485 * ...
486 *
487 * TLSv1:
488 * key block = PRF( master, "key expansion", randbytes )
489 */
Paul Bakker48916f92012-09-16 19:57:18 +0000490 handshake->tls_prf( session->master, 48, "key expansion",
491 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
Paul Bakker48916f92012-09-16 19:57:18 +0000493 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
494 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
495 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
496 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
498
Paul Bakker48916f92012-09-16 19:57:18 +0000499 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501 /*
502 * Determine the appropriate key, IV and MAC length.
503 */
Paul Bakker68884e32013-01-07 18:20:04 +0100504
505 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 {
Paul Bakker68884e32013-01-07 18:20:04 +0100507 transform->keylen = cipher_info->key_length;
508 transform->keylen /= 8;
509 transform->minlen = 1;
510 transform->ivlen = 12;
511 transform->fixed_ivlen = 4;
512 transform->maclen = 0;
513 }
514 else
515 {
516 if( md_info->type != POLARSSL_MD_NONE )
517 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200518 int ret;
519
Paul Bakker61d113b2013-07-04 11:51:43 +0200520 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
521 {
522 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
523 return( ret );
524 }
525
526 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
527 {
528 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
529 return( ret );
530 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Paul Bakker68884e32013-01-07 18:20:04 +0100532 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200533
Paul Bakker1f2bc622013-08-15 13:45:55 +0200534#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200535 /*
536 * If HMAC is to be truncated, we shall keep the leftmost bytes,
537 * (rfc 6066 page 13 or rfc 2104 section 4),
538 * so we only need to adjust the length here.
539 */
540 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
541 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200542#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100543 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Paul Bakker68884e32013-01-07 18:20:04 +0100545 transform->keylen = cipher_info->key_length;
546 transform->keylen /= 8;
547 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Paul Bakker68884e32013-01-07 18:20:04 +0100549 transform->minlen = transform->keylen;
550 if( transform->minlen < transform->maclen )
551 {
552 if( cipher_info->mode == POLARSSL_MODE_STREAM )
553 transform->minlen = transform->maclen;
554 else
555 transform->minlen += transform->keylen;
556 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 }
558
559 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000560 transform->keylen, transform->minlen, transform->ivlen,
561 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563 /*
564 * Finally setup the cipher contexts, IVs and MAC secrets.
565 */
566 if( ssl->endpoint == SSL_IS_CLIENT )
567 {
Paul Bakker48916f92012-09-16 19:57:18 +0000568 key1 = keyblk + transform->maclen * 2;
569 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Paul Bakker68884e32013-01-07 18:20:04 +0100571 mac_enc = keyblk;
572 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000574 /*
575 * This is not used in TLS v1.1.
576 */
Paul Bakker48916f92012-09-16 19:57:18 +0000577 iv_copy_len = ( transform->fixed_ivlen ) ?
578 transform->fixed_ivlen : transform->ivlen;
579 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
580 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000581 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 }
583 else
584 {
Paul Bakker48916f92012-09-16 19:57:18 +0000585 key1 = keyblk + transform->maclen * 2 + transform->keylen;
586 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Paul Bakker68884e32013-01-07 18:20:04 +0100588 mac_enc = keyblk + transform->maclen;
589 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000591 /*
592 * This is not used in TLS v1.1.
593 */
Paul Bakker48916f92012-09-16 19:57:18 +0000594 iv_copy_len = ( transform->fixed_ivlen ) ?
595 transform->fixed_ivlen : transform->ivlen;
596 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
597 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000598 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 }
600
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200601#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100602 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
603 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100604 if( transform->maclen > sizeof transform->mac_enc )
605 {
606 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
607 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
608 }
609
Paul Bakker68884e32013-01-07 18:20:04 +0100610 memcpy( transform->mac_enc, mac_enc, transform->maclen );
611 memcpy( transform->mac_dec, mac_dec, transform->maclen );
612 }
613 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200614#endif
615#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
616 defined(POLARSSL_SSL_PROTO_TLS1_2)
617 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100618 {
619 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
620 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
621 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200622 else
623#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200624 {
625 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200626 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200627 }
Paul Bakker68884e32013-01-07 18:20:04 +0100628
Paul Bakker05ef8352012-05-08 09:17:57 +0000629#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
630 if( ssl_hw_record_init != NULL)
631 {
632 int ret = 0;
633
634 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
635
Paul Bakker07eb38b2012-12-19 14:42:06 +0100636 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
637 transform->iv_enc, transform->iv_dec,
638 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100639 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100640 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000641 {
642 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
643 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
644 }
645 }
646#endif
647
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200648 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
649 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200651 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
652 return( ret );
653 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200654
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200655 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
656 cipher_info ) ) != 0 )
657 {
658 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
659 return( ret );
660 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200661
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200662 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
663 cipher_info->key_length,
664 POLARSSL_ENCRYPT ) ) != 0 )
665 {
666 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
667 return( ret );
668 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200669
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200670 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
671 cipher_info->key_length,
672 POLARSSL_DECRYPT ) ) != 0 )
673 {
674 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
675 return( ret );
676 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200677
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200678#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200679 if( cipher_info->mode == POLARSSL_MODE_CBC )
680 {
681 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
682 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200683 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
685 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200686 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200687
688 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
689 POLARSSL_PADDING_NONE ) ) != 0 )
690 {
691 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
692 return( ret );
693 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200695#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
697 memset( keyblk, 0, sizeof( keyblk ) );
698
Paul Bakker2770fbd2012-07-03 13:30:23 +0000699#if defined(POLARSSL_ZLIB_SUPPORT)
700 // Initialize compression
701 //
Paul Bakker48916f92012-09-16 19:57:18 +0000702 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000703 {
Paul Bakker16770332013-10-11 09:59:44 +0200704 if( ssl->compress_buf == NULL )
705 {
706 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
707 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
708 if( ssl->compress_buf == NULL )
709 {
710 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
711 SSL_BUFFER_LEN ) );
712 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
713 }
714 }
715
Paul Bakker2770fbd2012-07-03 13:30:23 +0000716 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
717
Paul Bakker48916f92012-09-16 19:57:18 +0000718 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
719 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000720
Paul Bakker48916f92012-09-16 19:57:18 +0000721 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
722 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000723 {
724 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
725 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
726 }
727 }
728#endif /* POLARSSL_ZLIB_SUPPORT */
729
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
731
732 return( 0 );
733}
734
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200735#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000736void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000737{
738 md5_context md5;
739 sha1_context sha1;
740 unsigned char pad_1[48];
741 unsigned char pad_2[48];
742
Paul Bakker380da532012-04-18 16:10:25 +0000743 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Paul Bakker48916f92012-09-16 19:57:18 +0000745 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
746 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
Paul Bakker380da532012-04-18 16:10:25 +0000748 memset( pad_1, 0x36, 48 );
749 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Paul Bakker48916f92012-09-16 19:57:18 +0000751 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000752 md5_update( &md5, pad_1, 48 );
753 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Paul Bakker380da532012-04-18 16:10:25 +0000755 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000756 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000757 md5_update( &md5, pad_2, 48 );
758 md5_update( &md5, hash, 16 );
759 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Paul Bakker48916f92012-09-16 19:57:18 +0000761 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000762 sha1_update( &sha1, pad_1, 40 );
763 sha1_finish( &sha1, hash + 16 );
764
765 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000766 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000767 sha1_update( &sha1, pad_2, 40 );
768 sha1_update( &sha1, hash + 16, 20 );
769 sha1_finish( &sha1, hash + 16 );
770
771 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
772 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
773
774 return;
775}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200776#endif
Paul Bakker380da532012-04-18 16:10:25 +0000777
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200778#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000779void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
780{
781 md5_context md5;
782 sha1_context sha1;
783
784 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
785
Paul Bakker48916f92012-09-16 19:57:18 +0000786 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
787 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000788
Paul Bakker48916f92012-09-16 19:57:18 +0000789 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000790 sha1_finish( &sha1, hash + 16 );
791
792 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
793 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
794
795 return;
796}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200797#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000798
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200799#if defined(POLARSSL_SSL_PROTO_TLS1_2)
800#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000801void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
802{
Paul Bakker9e36f042013-06-30 14:34:05 +0200803 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000804
805 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
806
Paul Bakker9e36f042013-06-30 14:34:05 +0200807 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
808 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000809
810 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
811 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
812
813 return;
814}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200815#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000816
Paul Bakker9e36f042013-06-30 14:34:05 +0200817#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000818void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
819{
Paul Bakker9e36f042013-06-30 14:34:05 +0200820 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000821
822 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
823
Paul Bakker9e36f042013-06-30 14:34:05 +0200824 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
825 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000826
Paul Bakkerca4ab492012-04-18 14:23:57 +0000827 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
829
830 return;
831}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200832#endif /* POLARSSL_SHA512_C */
833#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200835#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200836int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
837{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200838 unsigned char *p = ssl->handshake->premaster;
839 unsigned char *end = p + sizeof( ssl->handshake->premaster );
840
841 /*
842 * PMS = struct {
843 * opaque other_secret<0..2^16-1>;
844 * opaque psk<0..2^16-1>;
845 * };
846 * with "other_secret" depending on the particular key exchange
847 */
848#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
849 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
850 {
851 if( end - p < 2 + (int) ssl->psk_len )
852 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
853
854 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
855 *(p++) = (unsigned char)( ssl->psk_len );
856 p += ssl->psk_len;
857 }
858 else
859#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200860#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
861 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
862 {
863 /*
864 * other_secret already set by the ClientKeyExchange message,
865 * and is 48 bytes long
866 */
867 *p++ = 0;
868 *p++ = 48;
869 p += 48;
870 }
871 else
872#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200873#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
874 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
875 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200876 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200877 size_t len = ssl->handshake->dhm_ctx.len;
878
879 if( end - p < 2 + (int) len )
880 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
881
882 *(p++) = (unsigned char)( len >> 8 );
883 *(p++) = (unsigned char)( len );
884 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
885 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
886 {
887 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
888 return( ret );
889 }
890 p += len;
891
892 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
893 }
894 else
895#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
896#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
897 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
898 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200899 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200900 size_t zlen;
901
902 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
903 p + 2, end - (p + 2),
904 ssl->f_rng, ssl->p_rng ) ) != 0 )
905 {
906 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
907 return( ret );
908 }
909
910 *(p++) = (unsigned char)( zlen >> 8 );
911 *(p++) = (unsigned char)( zlen );
912 p += zlen;
913
914 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
915 }
916 else
917#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
918 {
919 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
920 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
921 }
922
923 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100924 if( end - p < 2 + (int) ssl->psk_len )
925 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
926
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200927 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
928 *(p++) = (unsigned char)( ssl->psk_len );
929 memcpy( p, ssl->psk, ssl->psk_len );
930 p += ssl->psk_len;
931
932 ssl->handshake->pmslen = p - ssl->handshake->premaster;
933
934 return( 0 );
935}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200936#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200938#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000939/*
940 * SSLv3.0 MAC functions
941 */
Paul Bakker68884e32013-01-07 18:20:04 +0100942static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
943 unsigned char *buf, size_t len,
944 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000945{
946 unsigned char header[11];
947 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100948 int padlen = 0;
949 int md_size = md_get_size( md_ctx->md_info );
950 int md_type = md_get_type( md_ctx->md_info );
951
952 if( md_type == POLARSSL_MD_MD5 )
953 padlen = 48;
954 else if( md_type == POLARSSL_MD_SHA1 )
955 padlen = 40;
956 else if( md_type == POLARSSL_MD_SHA256 )
957 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100958 else if( md_type == POLARSSL_MD_SHA384 )
959 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
961 memcpy( header, ctr, 8 );
962 header[ 8] = (unsigned char) type;
963 header[ 9] = (unsigned char)( len >> 8 );
964 header[10] = (unsigned char)( len );
965
Paul Bakker68884e32013-01-07 18:20:04 +0100966 memset( padding, 0x36, padlen );
967 md_starts( md_ctx );
968 md_update( md_ctx, secret, md_size );
969 md_update( md_ctx, padding, padlen );
970 md_update( md_ctx, header, 11 );
971 md_update( md_ctx, buf, len );
972 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
Paul Bakker68884e32013-01-07 18:20:04 +0100974 memset( padding, 0x5C, padlen );
975 md_starts( md_ctx );
976 md_update( md_ctx, secret, md_size );
977 md_update( md_ctx, padding, padlen );
978 md_update( md_ctx, buf + len, md_size );
979 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000980}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200981#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000982
Paul Bakker5121ce52009-01-03 21:22:43 +0000983/*
984 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200985 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000986static int ssl_encrypt_buf( ssl_context *ssl )
987{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200988 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
991
992 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200993 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200995#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
996 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
997 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
998 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
999 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001001#if defined(POLARSSL_SSL_PROTO_SSL3)
1002 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1003 {
1004 ssl_mac( &ssl->transform_out->md_ctx_enc,
1005 ssl->transform_out->mac_enc,
1006 ssl->out_msg, ssl->out_msglen,
1007 ssl->out_ctr, ssl->out_msgtype );
1008 }
1009 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001010#endif
1011#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001012 defined(POLARSSL_SSL_PROTO_TLS1_2)
1013 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1014 {
1015 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1016 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1017 ssl->out_msg, ssl->out_msglen );
1018 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1019 ssl->out_msg + ssl->out_msglen );
1020 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1021 }
1022 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001023#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001024 {
1025 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1026 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1027 }
1028
1029 SSL_DEBUG_BUF( 4, "computed mac",
1030 ssl->out_msg + ssl->out_msglen,
1031 ssl->transform_out->maclen );
1032
1033 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001034 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001035#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001037 /*
1038 * Encrypt
1039 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001040#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001041 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1042 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001044 int ret;
1045 size_t olen = 0;
1046
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1048 "including %d bytes of padding",
1049 ssl->out_msglen, 0 ) );
1050
1051 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1052 ssl->out_msg, ssl->out_msglen );
1053
Paul Bakker45125bc2013-09-04 16:47:11 +02001054 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001055 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001056 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1057 return( ret );
1058 }
1059
1060 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1061 ssl->transform_out->iv_enc,
1062 ssl->transform_out->ivlen ) ) != 0 )
1063 {
1064 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001065 return( ret );
1066 }
1067
1068 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1069 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1070 &olen ) ) != 0 )
1071 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001072 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001073 return( ret );
1074 }
1075
1076 if( ssl->out_msglen != olen )
1077 {
1078 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1079 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001080 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001081 }
1082
1083 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001084 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001085 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001086 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001087 return( ret );
1088 }
1089
1090 if( 0 != olen )
1091 {
1092 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1093 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001094 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 }
Paul Bakker68884e32013-01-07 18:20:04 +01001097 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001098#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001099#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001100 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1101 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001102 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001103 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001104 unsigned char *enc_msg;
1105 unsigned char add_data[13];
1106 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1107
Paul Bakkerca4ab492012-04-18 14:23:57 +00001108 memcpy( add_data, ssl->out_ctr, 8 );
1109 add_data[8] = ssl->out_msgtype;
1110 add_data[9] = ssl->major_ver;
1111 add_data[10] = ssl->minor_ver;
1112 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1113 add_data[12] = ssl->out_msglen & 0xFF;
1114
1115 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1116 add_data, 13 );
1117
Paul Bakker68884e32013-01-07 18:20:04 +01001118 /*
1119 * Generate IV
1120 */
1121 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001122 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001123 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1124 if( ret != 0 )
1125 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001126
Paul Bakker68884e32013-01-07 18:20:04 +01001127 memcpy( ssl->out_iv,
1128 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1129 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001130
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001131 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1132 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1133
Paul Bakker68884e32013-01-07 18:20:04 +01001134 /*
1135 * Fix pointer positions and message length with added IV
1136 */
1137 enc_msg = ssl->out_msg;
1138 enc_msglen = ssl->out_msglen;
1139 ssl->out_msglen += ssl->transform_out->ivlen -
1140 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001141
Paul Bakker68884e32013-01-07 18:20:04 +01001142 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1143 "including %d bytes of padding",
1144 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001145
Paul Bakker68884e32013-01-07 18:20:04 +01001146 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1147 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001148
Paul Bakker68884e32013-01-07 18:20:04 +01001149 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001150 * Encrypt
1151 */
1152 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1153 ssl->transform_out->iv_enc,
1154 ssl->transform_out->ivlen ) ) != 0 ||
1155 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1156 {
1157 return( ret );
1158 }
1159
1160 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1161 add_data, 13 ) ) != 0 )
1162 {
1163 return( ret );
1164 }
1165
1166 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1167 enc_msg, enc_msglen,
1168 enc_msg, &olen ) ) != 0 )
1169 {
1170 return( ret );
1171 }
1172 totlen = olen;
1173
1174 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1175 enc_msg + olen, &olen ) ) != 0 )
1176 {
1177 return( ret );
1178 }
1179 totlen += olen;
1180
1181 if( totlen != enc_msglen )
1182 {
1183 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1184 return( -1 );
1185 }
1186
1187 /*
1188 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001189 */
1190 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001191
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001192 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1193 enc_msg + enc_msglen, 16 ) ) != 0 )
1194 {
1195 return( ret );
1196 }
Paul Bakker68884e32013-01-07 18:20:04 +01001197
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001198 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 else
Paul Bakker68884e32013-01-07 18:20:04 +01001201#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001202#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1203 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001204 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1205 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001207 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001208 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001209 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001210
Paul Bakker48916f92012-09-16 19:57:18 +00001211 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1212 ssl->transform_out->ivlen;
1213 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 padlen = 0;
1215
1216 for( i = 0; i <= padlen; i++ )
1217 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1218
1219 ssl->out_msglen += padlen + 1;
1220
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001221 enc_msglen = ssl->out_msglen;
1222 enc_msg = ssl->out_msg;
1223
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001224#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001225 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001226 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1227 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001228 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001229 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230 {
1231 /*
1232 * Generate IV
1233 */
Paul Bakker48916f92012-09-16 19:57:18 +00001234 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1235 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001236 if( ret != 0 )
1237 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238
Paul Bakker92be97b2013-01-02 17:30:03 +01001239 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001240 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001241
1242 /*
1243 * Fix pointer positions and message length with added IV
1244 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001245 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001246 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001247 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001248 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001249#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001250
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001252 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001253 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
1255 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001256 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
Paul Bakker45125bc2013-09-04 16:47:11 +02001258 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001260 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1261 return( ret );
1262 }
1263
1264 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1265 ssl->transform_out->iv_enc,
1266 ssl->transform_out->ivlen ) ) != 0 )
1267 {
1268 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001269 return( ret );
1270 }
Paul Bakker68884e32013-01-07 18:20:04 +01001271
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1273 enc_msg, enc_msglen, enc_msg,
1274 &olen ) ) != 0 )
1275 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001276 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001277 return( ret );
1278 }
Paul Bakker68884e32013-01-07 18:20:04 +01001279
Paul Bakkercca5b812013-08-31 17:40:26 +02001280 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001281
Paul Bakkercca5b812013-08-31 17:40:26 +02001282 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001283 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001284 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001285 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001286 return( ret );
1287 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001288
Paul Bakkercca5b812013-08-31 17:40:26 +02001289 if( enc_msglen != olen )
1290 {
1291 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1292 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001293 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001294 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001295
1296#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001297 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1298 {
1299 /*
1300 * Save IV in SSL3 and TLS1
1301 */
1302 memcpy( ssl->transform_out->iv_enc,
1303 ssl->transform_out->cipher_ctx_enc.iv,
1304 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001306#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001308 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001309#endif /* POLARSSL_CIPHER_MODE_CBC &&
1310 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001311 {
1312 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1313 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1314 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
Paul Bakkerca4ab492012-04-18 14:23:57 +00001316 for( i = 8; i > 0; i-- )
1317 if( ++ssl->out_ctr[i - 1] != 0 )
1318 break;
1319
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001320 /* The loops goes to its end iff the counter is wrapping */
1321 if( i == 0 )
1322 {
1323 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1324 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1325 }
1326
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1328
1329 return( 0 );
1330}
1331
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001332#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001333
Paul Bakker5121ce52009-01-03 21:22:43 +00001334static int ssl_decrypt_buf( ssl_context *ssl )
1335{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001336 size_t i;
1337#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1338 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1339 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1340 size_t padlen = 0, correct = 1;
1341#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
1343 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1344
Paul Bakker48916f92012-09-16 19:57:18 +00001345 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 {
1347 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001348 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001349 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
1351
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001352#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001353 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1354 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001355 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001356 int ret;
1357 size_t olen = 0;
1358
Paul Bakker68884e32013-01-07 18:20:04 +01001359 padlen = 0;
1360
Paul Bakker45125bc2013-09-04 16:47:11 +02001361 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001362 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001363 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1364 return( ret );
1365 }
1366
1367 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1368 ssl->transform_in->iv_dec,
1369 ssl->transform_in->ivlen ) ) != 0 )
1370 {
1371 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001372 return( ret );
1373 }
1374
1375 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1376 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1377 &olen ) ) != 0 )
1378 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001379 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001380 return( ret );
1381 }
1382
1383 if( ssl->in_msglen != olen )
1384 {
1385 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001386 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001387 }
1388
1389 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001390 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001391 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001392 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001393 return( ret );
1394 }
1395
1396 if( 0 != olen )
1397 {
1398 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001399 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001400 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 }
Paul Bakker68884e32013-01-07 18:20:04 +01001402 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001403#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001404#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001405 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1406 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001407 {
1408 unsigned char *dec_msg;
1409 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001410 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001411 unsigned char add_data[13];
1412 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1413
Paul Bakker68884e32013-01-07 18:20:04 +01001414 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1415 ssl->transform_in->fixed_ivlen );
1416 dec_msglen -= 16;
1417 dec_msg = ssl->in_msg;
1418 dec_msg_result = ssl->in_msg;
1419 ssl->in_msglen = dec_msglen;
1420
1421 memcpy( add_data, ssl->in_ctr, 8 );
1422 add_data[8] = ssl->in_msgtype;
1423 add_data[9] = ssl->major_ver;
1424 add_data[10] = ssl->minor_ver;
1425 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1426 add_data[12] = ssl->in_msglen & 0xFF;
1427
1428 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1429 add_data, 13 );
1430
1431 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1432 ssl->in_iv,
1433 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1434
1435 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1436 ssl->transform_in->ivlen );
1437 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1438
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001439 /*
1440 * Decrypt
1441 */
1442 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1443 ssl->transform_in->iv_dec,
1444 ssl->transform_in->ivlen ) ) != 0 ||
1445 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001446 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001447 return( ret );
1448 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001449
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001450 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1451 add_data, 13 ) ) != 0 )
1452 {
1453 return( ret );
1454 }
1455
1456 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1457 dec_msg, dec_msglen,
1458 dec_msg_result, &olen ) ) != 0 )
1459 {
1460 return( ret );
1461 }
1462 totlen = olen;
1463
1464 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1465 dec_msg_result + olen, &olen ) ) != 0 )
1466 {
1467 return( ret );
1468 }
1469 totlen += olen;
1470
1471 if( totlen != dec_msglen )
1472 {
1473 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1474 return( -1 );
1475 }
1476
1477 /*
1478 * Authenticate
1479 */
1480 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1481 dec_msg + dec_msglen, 16 ) ) != 0 )
1482 {
1483 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001484 return( POLARSSL_ERR_SSL_INVALID_MAC );
1485 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001486
Paul Bakkerca4ab492012-04-18 14:23:57 +00001487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 else
Paul Bakker68884e32013-01-07 18:20:04 +01001489#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001490#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1491 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001492 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1493 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 {
Paul Bakker45829992013-01-03 14:52:21 +01001495 /*
1496 * Decrypt and check the padding
1497 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001498 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001499 unsigned char *dec_msg;
1500 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001501 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001502 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001503 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001504
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 /*
Paul Bakker45829992013-01-03 14:52:21 +01001506 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 */
Paul Bakker48916f92012-09-16 19:57:18 +00001508 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 {
1510 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001511 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001512 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 }
1514
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001515#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001516 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1517 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001518#endif
Paul Bakker45829992013-01-03 14:52:21 +01001519
1520 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1521 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1522 {
1523 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1524 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1525 return( POLARSSL_ERR_SSL_INVALID_MAC );
1526 }
1527
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001528 dec_msglen = ssl->in_msglen;
1529 dec_msg = ssl->in_msg;
1530 dec_msg_result = ssl->in_msg;
1531
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001532#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001533 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001534 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001535 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001536 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001537 {
Paul Bakker48916f92012-09-16 19:57:18 +00001538 dec_msglen -= ssl->transform_in->ivlen;
1539 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001540
Paul Bakker48916f92012-09-16 19:57:18 +00001541 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001542 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001543 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001544#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001545
Paul Bakker45125bc2013-09-04 16:47:11 +02001546 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001548 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1549 return( ret );
1550 }
1551
1552 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1553 ssl->transform_in->iv_dec,
1554 ssl->transform_in->ivlen ) ) != 0 )
1555 {
1556 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001557 return( ret );
1558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001559
Paul Bakkercca5b812013-08-31 17:40:26 +02001560 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1561 dec_msg, dec_msglen, dec_msg_result,
1562 &olen ) ) != 0 )
1563 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001564 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001565 return( ret );
1566 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001567
Paul Bakkercca5b812013-08-31 17:40:26 +02001568 dec_msglen -= olen;
1569 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001570 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001571 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001572 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001573 return( ret );
1574 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001575
Paul Bakkercca5b812013-08-31 17:40:26 +02001576 if( dec_msglen != olen )
1577 {
1578 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001579 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001580 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001581
1582#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001583 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1584 {
1585 /*
1586 * Save IV in SSL3 and TLS1
1587 */
1588 memcpy( ssl->transform_in->iv_dec,
1589 ssl->transform_in->cipher_ctx_dec.iv,
1590 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001592#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001593
1594 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001595
1596 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1597 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001598#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001599 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1600 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001601#endif
Paul Bakker45829992013-01-03 14:52:21 +01001602 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001603 correct = 0;
1604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001606#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1608 {
Paul Bakker48916f92012-09-16 19:57:18 +00001609 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001610 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001611#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001612 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1613 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001614 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001615#endif
Paul Bakker45829992013-01-03 14:52:21 +01001616 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 }
1618 }
1619 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001620#endif
1621#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1622 defined(POLARSSL_SSL_PROTO_TLS1_2)
1623 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 {
1625 /*
Paul Bakker45829992013-01-03 14:52:21 +01001626 * TLSv1+: always check the padding up to the first failure
1627 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001629 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001630 size_t padding_idx = ssl->in_msglen - padlen - 1;
1631
Paul Bakker956c9e02013-12-19 14:42:28 +01001632 /*
1633 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001634 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001635 *
Paul Bakker61885c72014-04-25 12:59:03 +02001636 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1637 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001638 *
1639 * In both cases we reset padding_idx to a safe value (0) to
1640 * prevent out-of-buffer reads.
1641 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001642 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001643 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1644 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001645
1646 padding_idx *= correct;
1647
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001648 for( i = 1; i <= 256; i++ )
1649 {
1650 real_count &= ( i <= padlen );
1651 pad_count += real_count *
1652 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1653 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001654
1655 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001656
Paul Bakkerd66f0702013-01-31 16:57:45 +01001657#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001658 if( padlen > 0 && correct == 0)
1659 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001660#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001661 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001663 else
1664#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1665 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001666 {
1667 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001668 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001669 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001671 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001672#endif /* POLARSSL_CIPHER_MODE_CBC &&
1673 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001674 {
1675 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1676 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
1679 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1680 ssl->in_msg, ssl->in_msglen );
1681
1682 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001683 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001684 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001685#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1686 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1687 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1688 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1689 POLARSSL_MODE_GCM )
1690 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001691 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1692
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001693 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001695 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1696 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001698 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001700#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001701 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1702 {
1703 ssl_mac( &ssl->transform_in->md_ctx_dec,
1704 ssl->transform_in->mac_dec,
1705 ssl->in_msg, ssl->in_msglen,
1706 ssl->in_ctr, ssl->in_msgtype );
1707 }
1708 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001709#endif /* POLARSSL_SSL_PROTO_SSL3 */
1710#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001711 defined(POLARSSL_SSL_PROTO_TLS1_2)
1712 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1713 {
1714 /*
1715 * Process MAC and always update for padlen afterwards to make
1716 * total time independent of padlen
1717 *
1718 * extra_run compensates MAC check for padlen
1719 *
1720 * Known timing attacks:
1721 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1722 *
1723 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1724 * correctly. (We round down instead of up, so -56 is the correct
1725 * value for our calculations instead of -55)
1726 */
1727 size_t j, extra_run = 0;
1728 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1729 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001730
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001731 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001732
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001733 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1734 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1735 ssl->in_msglen );
1736 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1737 ssl->in_msg + ssl->in_msglen );
1738 for( j = 0; j < extra_run; j++ )
1739 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001740
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001741 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1742 }
1743 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001744#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001745 POLARSSL_SSL_PROTO_TLS1_2 */
1746 {
1747 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1748 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1749 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001751 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1752 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1753 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001755 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001756 ssl->transform_in->maclen ) != 0 )
1757 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001758#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001759 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001760#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001761 correct = 0;
1762 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001763
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001764 /*
1765 * Finally check the correct flag
1766 */
1767 if( correct == 0 )
1768 return( POLARSSL_ERR_SSL_INVALID_MAC );
1769 }
1770#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
1772 if( ssl->in_msglen == 0 )
1773 {
1774 ssl->nb_zero++;
1775
1776 /*
1777 * Three or more empty messages may be a DoS attack
1778 * (excessive CPU consumption).
1779 */
1780 if( ssl->nb_zero > 3 )
1781 {
1782 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1783 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001784 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 }
1786 }
1787 else
1788 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001789
Paul Bakker23986e52011-04-24 08:57:21 +00001790 for( i = 8; i > 0; i-- )
1791 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001792 break;
1793
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001794 /* The loops goes to its end iff the counter is wrapping */
1795 if( i == 0 )
1796 {
1797 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1798 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1799 }
1800
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1802
1803 return( 0 );
1804}
1805
Paul Bakker2770fbd2012-07-03 13:30:23 +00001806#if defined(POLARSSL_ZLIB_SUPPORT)
1807/*
1808 * Compression/decompression functions
1809 */
1810static int ssl_compress_buf( ssl_context *ssl )
1811{
1812 int ret;
1813 unsigned char *msg_post = ssl->out_msg;
1814 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001815 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001816
1817 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1818
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001819 if( len_pre == 0 )
1820 return( 0 );
1821
Paul Bakker2770fbd2012-07-03 13:30:23 +00001822 memcpy( msg_pre, ssl->out_msg, len_pre );
1823
1824 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1825 ssl->out_msglen ) );
1826
1827 SSL_DEBUG_BUF( 4, "before compression: output payload",
1828 ssl->out_msg, ssl->out_msglen );
1829
Paul Bakker48916f92012-09-16 19:57:18 +00001830 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1831 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1832 ssl->transform_out->ctx_deflate.next_out = msg_post;
1833 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001834
Paul Bakker48916f92012-09-16 19:57:18 +00001835 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001836 if( ret != Z_OK )
1837 {
1838 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1839 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1840 }
1841
Paul Bakker48916f92012-09-16 19:57:18 +00001842 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001843
Paul Bakker2770fbd2012-07-03 13:30:23 +00001844 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1845 ssl->out_msglen ) );
1846
1847 SSL_DEBUG_BUF( 4, "after compression: output payload",
1848 ssl->out_msg, ssl->out_msglen );
1849
1850 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1851
1852 return( 0 );
1853}
1854
1855static int ssl_decompress_buf( ssl_context *ssl )
1856{
1857 int ret;
1858 unsigned char *msg_post = ssl->in_msg;
1859 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001860 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001861
1862 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1863
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001864 if( len_pre == 0 )
1865 return( 0 );
1866
Paul Bakker2770fbd2012-07-03 13:30:23 +00001867 memcpy( msg_pre, ssl->in_msg, len_pre );
1868
1869 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1870 ssl->in_msglen ) );
1871
1872 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1873 ssl->in_msg, ssl->in_msglen );
1874
Paul Bakker48916f92012-09-16 19:57:18 +00001875 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1876 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1877 ssl->transform_in->ctx_inflate.next_out = msg_post;
1878 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001879
Paul Bakker48916f92012-09-16 19:57:18 +00001880 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001881 if( ret != Z_OK )
1882 {
1883 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1884 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1885 }
1886
Paul Bakker48916f92012-09-16 19:57:18 +00001887 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001888
Paul Bakker2770fbd2012-07-03 13:30:23 +00001889 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1890 ssl->in_msglen ) );
1891
1892 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1893 ssl->in_msg, ssl->in_msglen );
1894
1895 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1896
1897 return( 0 );
1898}
1899#endif /* POLARSSL_ZLIB_SUPPORT */
1900
Paul Bakker5121ce52009-01-03 21:22:43 +00001901/*
1902 * Fill the input message buffer
1903 */
Paul Bakker23986e52011-04-24 08:57:21 +00001904int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001905{
Paul Bakker23986e52011-04-24 08:57:21 +00001906 int ret;
1907 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
1909 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1910
1911 while( ssl->in_left < nb_want )
1912 {
1913 len = nb_want - ssl->in_left;
1914 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1915
1916 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1917 ssl->in_left, nb_want ) );
1918 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1919
Paul Bakker831a7552011-05-18 13:32:51 +00001920 if( ret == 0 )
1921 return( POLARSSL_ERR_SSL_CONN_EOF );
1922
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 if( ret < 0 )
1924 return( ret );
1925
1926 ssl->in_left += ret;
1927 }
1928
1929 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1930
1931 return( 0 );
1932}
1933
1934/*
1935 * Flush any data not yet written
1936 */
1937int ssl_flush_output( ssl_context *ssl )
1938{
1939 int ret;
1940 unsigned char *buf;
1941
1942 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1943
1944 while( ssl->out_left > 0 )
1945 {
1946 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1947 5 + ssl->out_msglen, ssl->out_left ) );
1948
Paul Bakker5bd42292012-12-19 14:40:42 +01001949 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001951
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1953
1954 if( ret <= 0 )
1955 return( ret );
1956
1957 ssl->out_left -= ret;
1958 }
1959
1960 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1961
1962 return( 0 );
1963}
1964
1965/*
1966 * Record layer functions
1967 */
1968int ssl_write_record( ssl_context *ssl )
1969{
Paul Bakker05ef8352012-05-08 09:17:57 +00001970 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001971 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
1973 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1974
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1976 {
1977 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1978 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1979 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1980
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001981 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1982 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 }
1984
Paul Bakker2770fbd2012-07-03 13:30:23 +00001985#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001986 if( ssl->transform_out != NULL &&
1987 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001988 {
1989 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1990 {
1991 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1992 return( ret );
1993 }
1994
1995 len = ssl->out_msglen;
1996 }
1997#endif /*POLARSSL_ZLIB_SUPPORT */
1998
Paul Bakker05ef8352012-05-08 09:17:57 +00001999#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2000 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002002 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002003
Paul Bakker05ef8352012-05-08 09:17:57 +00002004 ret = ssl_hw_record_write( ssl );
2005 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2006 {
2007 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
2008 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2009 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002010
2011 if( ret == 0 )
2012 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002013 }
2014#endif
2015 if( !done )
2016 {
2017 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2018 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2019 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2021 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002022
Paul Bakker48916f92012-09-16 19:57:18 +00002023 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002024 {
2025 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2026 {
2027 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2028 return( ret );
2029 }
2030
2031 len = ssl->out_msglen;
2032 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2033 ssl->out_hdr[4] = (unsigned char)( len );
2034 }
2035
2036 ssl->out_left = 5 + ssl->out_msglen;
2037
2038 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2039 "version = [%d:%d], msglen = %d",
2040 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2041 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2042
2043 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002044 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 }
2046
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2048 {
2049 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2050 return( ret );
2051 }
2052
2053 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2054
2055 return( 0 );
2056}
2057
2058int ssl_read_record( ssl_context *ssl )
2059{
Paul Bakker05ef8352012-05-08 09:17:57 +00002060 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002061
2062 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2063
Paul Bakker68884e32013-01-07 18:20:04 +01002064 SSL_DEBUG_BUF( 4, "input record from network",
2065 ssl->in_hdr, 5 + ssl->in_msglen );
2066
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 if( ssl->in_hslen != 0 &&
2068 ssl->in_hslen < ssl->in_msglen )
2069 {
2070 /*
2071 * Get next Handshake message in the current record
2072 */
2073 ssl->in_msglen -= ssl->in_hslen;
2074
Paul Bakker8934a982011-08-05 11:11:53 +00002075 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002076 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
2078 ssl->in_hslen = 4;
2079 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2080
2081 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2082 " %d, type = %d, hslen = %d",
2083 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2084
2085 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2086 {
2087 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002088 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
2090
2091 if( ssl->in_msglen < ssl->in_hslen )
2092 {
2093 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002094 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 }
2096
Paul Bakker4224bc02014-04-08 14:36:50 +02002097 if( ssl->state != SSL_HANDSHAKE_OVER )
2098 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
2100 return( 0 );
2101 }
2102
2103 ssl->in_hslen = 0;
2104
2105 /*
2106 * Read the record header and validate it
2107 */
2108 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2109 {
2110 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2111 return( ret );
2112 }
2113
2114 ssl->in_msgtype = ssl->in_hdr[0];
2115 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2116
2117 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2118 "version = [%d:%d], msglen = %d",
2119 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2120 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2121
2122 if( ssl->in_hdr[1] != ssl->major_ver )
2123 {
2124 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002125 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
2127
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002128 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 {
2130 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002131 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
2133
2134 /*
2135 * Make sure the message length is acceptable
2136 */
Paul Bakker48916f92012-09-16 19:57:18 +00002137 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 {
2139 if( ssl->in_msglen < 1 ||
2140 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2141 {
2142 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002143 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 }
2145 }
2146 else
2147 {
Paul Bakker48916f92012-09-16 19:57:18 +00002148 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 {
2150 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002151 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
2153
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002154#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002156 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 {
2158 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002159 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002161#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002162
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002163#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2164 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 /*
2166 * TLS encrypted messages can have up to 256 bytes of padding
2167 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002168 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002169 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 {
2171 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002172 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002174#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 }
2176
2177 /*
2178 * Read and optionally decrypt the message contents
2179 */
2180 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2181 {
2182 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2183 return( ret );
2184 }
2185
2186 SSL_DEBUG_BUF( 4, "input record from network",
2187 ssl->in_hdr, 5 + ssl->in_msglen );
2188
Paul Bakker05ef8352012-05-08 09:17:57 +00002189#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2190 if( ssl_hw_record_read != NULL)
2191 {
2192 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2193
2194 ret = ssl_hw_record_read( ssl );
2195 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2196 {
2197 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2198 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2199 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002200
2201 if( ret == 0 )
2202 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002203 }
2204#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002205 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 {
2207 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2208 {
Paul Bakker40865c82013-01-31 17:13:13 +01002209#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2210 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2211 {
2212 ssl_send_alert_message( ssl,
2213 SSL_ALERT_LEVEL_FATAL,
2214 SSL_ALERT_MSG_BAD_RECORD_MAC );
2215 }
2216#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2218 return( ret );
2219 }
2220
2221 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2222 ssl->in_msg, ssl->in_msglen );
2223
2224 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2225 {
2226 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002227 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002228 }
2229 }
2230
Paul Bakker2770fbd2012-07-03 13:30:23 +00002231#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002232 if( ssl->transform_in != NULL &&
2233 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002234 {
2235 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2236 {
2237 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2238 return( ret );
2239 }
2240
2241 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2242 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2243 }
2244#endif /* POLARSSL_ZLIB_SUPPORT */
2245
Paul Bakker0a925182012-04-16 06:46:41 +00002246 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2247 ssl->in_msgtype != SSL_MSG_ALERT &&
2248 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2249 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2250 {
2251 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2252
Paul Bakker48916f92012-09-16 19:57:18 +00002253 if( ( ret = ssl_send_alert_message( ssl,
2254 SSL_ALERT_LEVEL_FATAL,
2255 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002256 {
2257 return( ret );
2258 }
2259
2260 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2261 }
2262
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2264 {
2265 ssl->in_hslen = 4;
2266 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2267
2268 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2269 " %d, type = %d, hslen = %d",
2270 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2271
2272 /*
2273 * Additional checks to validate the handshake header
2274 */
2275 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2276 {
2277 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002278 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 }
2280
2281 if( ssl->in_msglen < ssl->in_hslen )
2282 {
2283 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002284 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 }
2286
Paul Bakker48916f92012-09-16 19:57:18 +00002287 if( ssl->state != SSL_HANDSHAKE_OVER )
2288 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
2290
2291 if( ssl->in_msgtype == SSL_MSG_ALERT )
2292 {
2293 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2294 ssl->in_msg[0], ssl->in_msg[1] ) );
2295
2296 /*
2297 * Ignore non-fatal alerts, except close_notify
2298 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002299 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002301 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2302 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002303 /**
2304 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2305 * error identifier.
2306 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002307 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
2309
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002310 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2311 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 {
2313 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002314 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002315 }
2316 }
2317
2318 ssl->in_left = 0;
2319
2320 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2321
2322 return( 0 );
2323}
2324
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002325int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2326{
2327 int ret;
2328
2329 if( ( ret = ssl_send_alert_message( ssl,
2330 SSL_ALERT_LEVEL_FATAL,
2331 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2332 {
2333 return( ret );
2334 }
2335
2336 return( 0 );
2337}
2338
Paul Bakker0a925182012-04-16 06:46:41 +00002339int ssl_send_alert_message( ssl_context *ssl,
2340 unsigned char level,
2341 unsigned char message )
2342{
2343 int ret;
2344
2345 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2346
2347 ssl->out_msgtype = SSL_MSG_ALERT;
2348 ssl->out_msglen = 2;
2349 ssl->out_msg[0] = level;
2350 ssl->out_msg[1] = message;
2351
2352 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2353 {
2354 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2355 return( ret );
2356 }
2357
2358 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2359
2360 return( 0 );
2361}
2362
Paul Bakker5121ce52009-01-03 21:22:43 +00002363/*
2364 * Handshake functions
2365 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002366#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2367 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2368 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2369 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2370 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2371 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2372 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002373int ssl_write_certificate( ssl_context *ssl )
2374{
Paul Bakkered27a042013-04-18 22:46:23 +02002375 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002376 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002377
2378 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2379
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002380 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002381 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2382 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002383 {
2384 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2385 ssl->state++;
2386 return( 0 );
2387 }
2388
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002389 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002390 return( ret );
2391}
2392
2393int ssl_parse_certificate( ssl_context *ssl )
2394{
2395 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2396 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2397
2398 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2399
2400 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002401 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2402 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002403 {
2404 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2405 ssl->state++;
2406 return( 0 );
2407 }
2408
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002409 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002410 return( ret );
2411}
2412#else
2413int ssl_write_certificate( ssl_context *ssl )
2414{
2415 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2416 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002417 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002418 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2419
2420 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2421
2422 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002423 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2424 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002425 {
2426 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2427 ssl->state++;
2428 return( 0 );
2429 }
2430
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 if( ssl->endpoint == SSL_IS_CLIENT )
2432 {
2433 if( ssl->client_auth == 0 )
2434 {
2435 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2436 ssl->state++;
2437 return( 0 );
2438 }
2439
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002440#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 /*
2442 * If using SSLv3 and got no cert, send an Alert message
2443 * (otherwise an empty Certificate message will be sent).
2444 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002445 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2447 {
2448 ssl->out_msglen = 2;
2449 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002450 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2451 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
2453 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2454 goto write_msg;
2455 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002456#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 }
2458 else /* SSL_IS_SERVER */
2459 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002460 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 {
2462 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002463 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 }
2465 }
2466
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002467 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 /*
2470 * 0 . 0 handshake type
2471 * 1 . 3 handshake length
2472 * 4 . 6 length of all certs
2473 * 7 . 9 length of cert. 1
2474 * 10 . n-1 peer certificate
2475 * n . n+2 length of cert. 2
2476 * n+3 . ... upper level cert, etc.
2477 */
2478 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002479 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Paul Bakker29087132010-03-21 21:03:34 +00002481 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 {
2483 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002484 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 {
2486 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2487 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002488 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 }
2490
2491 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2492 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2493 ssl->out_msg[i + 2] = (unsigned char)( n );
2494
2495 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2496 i += n; crt = crt->next;
2497 }
2498
2499 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2500 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2501 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2502
2503 ssl->out_msglen = i;
2504 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2505 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2506
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002507#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002508write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
2511 ssl->state++;
2512
2513 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2514 {
2515 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2516 return( ret );
2517 }
2518
2519 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2520
Paul Bakkered27a042013-04-18 22:46:23 +02002521 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002522}
2523
2524int ssl_parse_certificate( ssl_context *ssl )
2525{
Paul Bakkered27a042013-04-18 22:46:23 +02002526 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002527 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002528 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
2530 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2531
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002532 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002533 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2534 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002535 {
2536 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2537 ssl->state++;
2538 return( 0 );
2539 }
2540
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002542 ( ssl->authmode == SSL_VERIFY_NONE ||
2543 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002545 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2547 ssl->state++;
2548 return( 0 );
2549 }
2550
2551 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2552 {
2553 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2554 return( ret );
2555 }
2556
2557 ssl->state++;
2558
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002559#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 /*
2561 * Check if the client sent an empty certificate
2562 */
2563 if( ssl->endpoint == SSL_IS_SERVER &&
2564 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2565 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002566 if( ssl->in_msglen == 2 &&
2567 ssl->in_msgtype == SSL_MSG_ALERT &&
2568 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2569 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 {
2571 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2572
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002573 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2575 return( 0 );
2576 else
Paul Bakker40e46942009-01-03 21:51:57 +00002577 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 }
2579 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002580#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002582#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2583 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 if( ssl->endpoint == SSL_IS_SERVER &&
2585 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2586 {
2587 if( ssl->in_hslen == 7 &&
2588 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2589 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2590 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2591 {
2592 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2593
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002594 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002596 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 else
2598 return( 0 );
2599 }
2600 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002601#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2602 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
2604 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2605 {
2606 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002607 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 }
2609
2610 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2611 {
2612 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002613 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 }
2615
2616 /*
2617 * Same message structure as in ssl_write_certificate()
2618 */
2619 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2620
2621 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2622 {
2623 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002624 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
2626
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002627 /* In case we tried to reuse a session but it failed */
2628 if( ssl->session_negotiate->peer_cert != NULL )
2629 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002630 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002631 polarssl_free( ssl->session_negotiate->peer_cert );
2632 }
2633
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002634 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2635 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 {
2637 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002638 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002639 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 }
2641
Paul Bakkerb6b09562013-09-18 14:17:41 +02002642 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
2644 i = 7;
2645
2646 while( i < ssl->in_hslen )
2647 {
2648 if( ssl->in_msg[i] != 0 )
2649 {
2650 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002651 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 }
2653
2654 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2655 | (unsigned int) ssl->in_msg[i + 2];
2656 i += 3;
2657
2658 if( n < 128 || i + n > ssl->in_hslen )
2659 {
2660 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002661 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 }
2663
Paul Bakkerddf26b42013-09-18 13:46:23 +02002664 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2665 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 if( ret != 0 )
2667 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002668 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 return( ret );
2670 }
2671
2672 i += n;
2673 }
2674
Paul Bakker48916f92012-09-16 19:57:18 +00002675 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002677 /*
2678 * On client, make sure the server cert doesn't change during renego to
2679 * avoid "triple handshake" attack: https://secure-resumption.com/
2680 */
2681 if( ssl->endpoint == SSL_IS_CLIENT &&
2682 ssl->renegotiation == SSL_RENEGOTIATION )
2683 {
2684 if( ssl->session->peer_cert == NULL )
2685 {
2686 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2687 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2688 }
2689
2690 if( ssl->session->peer_cert->raw.len !=
2691 ssl->session_negotiate->peer_cert->raw.len ||
2692 memcmp( ssl->session->peer_cert->raw.p,
2693 ssl->session_negotiate->peer_cert->raw.p,
2694 ssl->session->peer_cert->raw.len ) != 0 )
2695 {
2696 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2697 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2698 }
2699 }
2700
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 if( ssl->authmode != SSL_VERIFY_NONE )
2702 {
2703 if( ssl->ca_chain == NULL )
2704 {
2705 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002706 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 }
2708
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002709 /*
2710 * Main check: verify certificate
2711 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002712 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2713 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2714 &ssl->session_negotiate->verify_result,
2715 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
2717 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002718 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002720 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002721
2722 /*
2723 * Secondary checks: always done, but change 'ret' only if it was 0
2724 */
2725
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002726#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002727 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002728 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002729
2730 /* If certificate uses an EC key, make sure the curve is OK */
2731 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2732 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2733 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002734 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002735 if( ret == 0 )
2736 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002737 }
2738 }
2739#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002741 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2742 ciphersuite_info,
2743 ! ssl->endpoint ) != 0 )
2744 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002745 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002746 if( ret == 0 )
2747 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2748 }
2749
Paul Bakker5121ce52009-01-03 21:22:43 +00002750 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2751 ret = 0;
2752 }
2753
2754 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2755
2756 return( ret );
2757}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002758#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2759 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2760 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2761 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2762 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2763 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2764 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
2766int ssl_write_change_cipher_spec( ssl_context *ssl )
2767{
2768 int ret;
2769
2770 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2771
2772 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2773 ssl->out_msglen = 1;
2774 ssl->out_msg[0] = 1;
2775
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 ssl->state++;
2777
2778 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2779 {
2780 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2781 return( ret );
2782 }
2783
2784 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2785
2786 return( 0 );
2787}
2788
2789int ssl_parse_change_cipher_spec( ssl_context *ssl )
2790{
2791 int ret;
2792
2793 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2794
Paul Bakker5121ce52009-01-03 21:22:43 +00002795 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2796 {
2797 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2798 return( ret );
2799 }
2800
2801 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2802 {
2803 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002804 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 }
2806
2807 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2808 {
2809 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002810 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 }
2812
2813 ssl->state++;
2814
2815 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2816
2817 return( 0 );
2818}
2819
Paul Bakker41c83d32013-03-20 14:39:14 +01002820void ssl_optimize_checksum( ssl_context *ssl,
2821 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002822{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002823 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002824
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002825#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2826 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002827 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002828 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002829 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002830#endif
2831#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2832#if defined(POLARSSL_SHA512_C)
2833 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2834 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2835 else
2836#endif
2837#if defined(POLARSSL_SHA256_C)
2838 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002839 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002840 else
2841#endif
2842#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2843 /* Should never happen */
2844 return;
Paul Bakker380da532012-04-18 16:10:25 +00002845}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002846
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002847static void ssl_update_checksum_start( ssl_context *ssl,
2848 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002849{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002850#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2851 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002852 md5_update( &ssl->handshake->fin_md5 , buf, len );
2853 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002854#endif
2855#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2856#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002857 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002858#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002859#if defined(POLARSSL_SHA512_C)
2860 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002861#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002862#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002863}
2864
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002865#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2866 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002867static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2868 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002869{
Paul Bakker48916f92012-09-16 19:57:18 +00002870 md5_update( &ssl->handshake->fin_md5 , buf, len );
2871 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002872}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002873#endif
Paul Bakker380da532012-04-18 16:10:25 +00002874
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002875#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2876#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002877static void ssl_update_checksum_sha256( ssl_context *ssl,
2878 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002879{
Paul Bakker9e36f042013-06-30 14:34:05 +02002880 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002881}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002882#endif
Paul Bakker380da532012-04-18 16:10:25 +00002883
Paul Bakker9e36f042013-06-30 14:34:05 +02002884#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002885static void ssl_update_checksum_sha384( ssl_context *ssl,
2886 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002887{
Paul Bakker9e36f042013-06-30 14:34:05 +02002888 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002889}
Paul Bakker769075d2012-11-24 11:26:46 +01002890#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002891#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002892
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002893#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894static void ssl_calc_finished_ssl(
2895 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002896{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002897 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898 md5_context md5;
2899 sha1_context sha1;
2900
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 unsigned char padbuf[48];
2902 unsigned char md5sum[16];
2903 unsigned char sha1sum[20];
2904
Paul Bakker48916f92012-09-16 19:57:18 +00002905 ssl_session *session = ssl->session_negotiate;
2906 if( !session )
2907 session = ssl->session;
2908
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2910
Paul Bakker48916f92012-09-16 19:57:18 +00002911 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2912 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
2914 /*
2915 * SSLv3:
2916 * hash =
2917 * MD5( master + pad2 +
2918 * MD5( handshake + sender + master + pad1 ) )
2919 * + SHA1( master + pad2 +
2920 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002921 */
2922
Paul Bakker90995b52013-06-24 19:20:35 +02002923#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002926#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002927
Paul Bakker90995b52013-06-24 19:20:35 +02002928#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002931#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Paul Bakker3c2122f2013-06-24 19:03:14 +02002933 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2934 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
Paul Bakker3c2122f2013-06-24 19:03:14 +02002938 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002939 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940 md5_update( &md5, padbuf, 48 );
2941 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
Paul Bakker3c2122f2013-06-24 19:03:14 +02002943 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002944 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945 sha1_update( &sha1, padbuf, 40 );
2946 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Paul Bakker1ef83d62012-04-11 12:09:53 +00002948 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Paul Bakker1ef83d62012-04-11 12:09:53 +00002950 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002951 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952 md5_update( &md5, padbuf, 48 );
2953 md5_update( &md5, md5sum, 16 );
2954 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002957 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958 sha1_update( &sha1, padbuf , 40 );
2959 sha1_update( &sha1, sha1sum, 20 );
2960 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964 memset( &md5, 0, sizeof( md5_context ) );
2965 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
2967 memset( padbuf, 0, sizeof( padbuf ) );
2968 memset( md5sum, 0, sizeof( md5sum ) );
2969 memset( sha1sum, 0, sizeof( sha1sum ) );
2970
2971 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2972}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002973#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002975#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976static void ssl_calc_finished_tls(
2977 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002978{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002980 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002981 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002982 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002983 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Paul Bakker48916f92012-09-16 19:57:18 +00002985 ssl_session *session = ssl->session_negotiate;
2986 if( !session )
2987 session = ssl->session;
2988
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
Paul Bakker48916f92012-09-16 19:57:18 +00002991 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2992 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002993
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994 /*
2995 * TLSv1:
2996 * hash = PRF( master, finished_label,
2997 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2998 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
Paul Bakker90995b52013-06-24 19:20:35 +02003000#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003001 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3002 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003003#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003004
Paul Bakker90995b52013-06-24 19:20:35 +02003005#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003006 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3007 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003008#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003009
3010 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003011 ? "client finished"
3012 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003013
3014 md5_finish( &md5, padbuf );
3015 sha1_finish( &sha1, padbuf + 16 );
3016
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003017 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003018 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003019
3020 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3021
3022 memset( &md5, 0, sizeof( md5_context ) );
3023 memset( &sha1, 0, sizeof( sha1_context ) );
3024
3025 memset( padbuf, 0, sizeof( padbuf ) );
3026
3027 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3028}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003029#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003030
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003031#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3032#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003033static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003034 ssl_context *ssl, unsigned char *buf, int from )
3035{
3036 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003037 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003038 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003039 unsigned char padbuf[32];
3040
Paul Bakker48916f92012-09-16 19:57:18 +00003041 ssl_session *session = ssl->session_negotiate;
3042 if( !session )
3043 session = ssl->session;
3044
Paul Bakker380da532012-04-18 16:10:25 +00003045 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003046
Paul Bakker9e36f042013-06-30 14:34:05 +02003047 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048
3049 /*
3050 * TLSv1.2:
3051 * hash = PRF( master, finished_label,
3052 * Hash( handshake ) )[0.11]
3053 */
3054
Paul Bakker9e36f042013-06-30 14:34:05 +02003055#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003056 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003057 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003058#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003059
3060 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003061 ? "client finished"
3062 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063
Paul Bakker9e36f042013-06-30 14:34:05 +02003064 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003065
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003066 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003067 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068
3069 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3070
Paul Bakker9e36f042013-06-30 14:34:05 +02003071 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072
3073 memset( padbuf, 0, sizeof( padbuf ) );
3074
3075 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3076}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003077#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078
Paul Bakker9e36f042013-06-30 14:34:05 +02003079#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003080static void ssl_calc_finished_tls_sha384(
3081 ssl_context *ssl, unsigned char *buf, int from )
3082{
3083 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003084 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003085 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003086 unsigned char padbuf[48];
3087
Paul Bakker48916f92012-09-16 19:57:18 +00003088 ssl_session *session = ssl->session_negotiate;
3089 if( !session )
3090 session = ssl->session;
3091
Paul Bakker380da532012-04-18 16:10:25 +00003092 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003093
Paul Bakker9e36f042013-06-30 14:34:05 +02003094 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003095
3096 /*
3097 * TLSv1.2:
3098 * hash = PRF( master, finished_label,
3099 * Hash( handshake ) )[0.11]
3100 */
3101
Paul Bakker9e36f042013-06-30 14:34:05 +02003102#if !defined(POLARSSL_SHA512_ALT)
3103 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3104 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003105#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003106
3107 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003108 ? "client finished"
3109 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003110
Paul Bakker9e36f042013-06-30 14:34:05 +02003111 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003112
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003113 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003114 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003115
3116 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3117
Paul Bakker9e36f042013-06-30 14:34:05 +02003118 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003119
3120 memset( padbuf, 0, sizeof( padbuf ) );
3121
3122 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3123}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003124#endif /* POLARSSL_SHA512_C */
3125#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003126
Paul Bakker48916f92012-09-16 19:57:18 +00003127void ssl_handshake_wrapup( ssl_context *ssl )
3128{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003129 int resume = ssl->handshake->resume;
3130
Paul Bakker48916f92012-09-16 19:57:18 +00003131 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3132
3133 /*
3134 * Free our handshake params
3135 */
3136 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003137 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003138 ssl->handshake = NULL;
3139
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003140 if( ssl->renegotiation == SSL_RENEGOTIATION )
3141 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3142
Paul Bakker48916f92012-09-16 19:57:18 +00003143 /*
3144 * Switch in our now active transform context
3145 */
3146 if( ssl->transform )
3147 {
3148 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003149 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003150 }
3151 ssl->transform = ssl->transform_negotiate;
3152 ssl->transform_negotiate = NULL;
3153
Paul Bakker0a597072012-09-25 21:55:46 +00003154 if( ssl->session )
3155 {
3156 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003157 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003158 }
3159 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003160 ssl->session_negotiate = NULL;
3161
Paul Bakker0a597072012-09-25 21:55:46 +00003162 /*
3163 * Add cache entry
3164 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003165 if( ssl->f_set_cache != NULL &&
3166 ssl->session->length != 0 &&
3167 resume == 0 )
3168 {
Paul Bakker0a597072012-09-25 21:55:46 +00003169 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3170 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003171 }
Paul Bakker0a597072012-09-25 21:55:46 +00003172
Paul Bakker48916f92012-09-16 19:57:18 +00003173 ssl->state++;
3174
3175 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3176}
3177
Paul Bakker1ef83d62012-04-11 12:09:53 +00003178int ssl_write_finished( ssl_context *ssl )
3179{
3180 int ret, hash_len;
3181
3182 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3183
Paul Bakker92be97b2013-01-02 17:30:03 +01003184 /*
3185 * Set the out_msg pointer to the correct location based on IV length
3186 */
3187 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3188 {
3189 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3190 ssl->transform_negotiate->fixed_ivlen;
3191 }
3192 else
3193 ssl->out_msg = ssl->out_iv;
3194
Paul Bakker48916f92012-09-16 19:57:18 +00003195 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003196
3197 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3199
Paul Bakker48916f92012-09-16 19:57:18 +00003200 ssl->verify_data_len = hash_len;
3201 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3202
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 ssl->out_msglen = 4 + hash_len;
3204 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3205 ssl->out_msg[0] = SSL_HS_FINISHED;
3206
3207 /*
3208 * In case of session resuming, invert the client and server
3209 * ChangeCipherSpec messages order.
3210 */
Paul Bakker0a597072012-09-25 21:55:46 +00003211 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 {
3213 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003214 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 else
3216 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3217 }
3218 else
3219 ssl->state++;
3220
Paul Bakker48916f92012-09-16 19:57:18 +00003221 /*
3222 * Switch to our negotiated transform and session parameters for outbound data.
3223 */
3224 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3225 ssl->transform_out = ssl->transform_negotiate;
3226 ssl->session_out = ssl->session_negotiate;
3227 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Paul Bakker07eb38b2012-12-19 14:42:06 +01003229#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3230 if( ssl_hw_record_activate != NULL)
3231 {
3232 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3233 {
3234 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3235 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3236 }
3237 }
3238#endif
3239
Paul Bakker5121ce52009-01-03 21:22:43 +00003240 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3241 {
3242 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3243 return( ret );
3244 }
3245
3246 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3247
3248 return( 0 );
3249}
3250
3251int ssl_parse_finished( ssl_context *ssl )
3252{
Paul Bakker23986e52011-04-24 08:57:21 +00003253 int ret;
3254 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003255 unsigned char buf[36];
3256
3257 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3258
Paul Bakker48916f92012-09-16 19:57:18 +00003259 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003260
Paul Bakker48916f92012-09-16 19:57:18 +00003261 /*
3262 * Switch to our negotiated transform and session parameters for inbound data.
3263 */
3264 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3265 ssl->transform_in = ssl->transform_negotiate;
3266 ssl->session_in = ssl->session_negotiate;
3267 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003268
Paul Bakker92be97b2013-01-02 17:30:03 +01003269 /*
3270 * Set the in_msg pointer to the correct location based on IV length
3271 */
3272 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3273 {
3274 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3275 ssl->transform_negotiate->fixed_ivlen;
3276 }
3277 else
3278 ssl->in_msg = ssl->in_iv;
3279
Paul Bakker07eb38b2012-12-19 14:42:06 +01003280#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3281 if( ssl_hw_record_activate != NULL)
3282 {
3283 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3284 {
3285 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3286 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3287 }
3288 }
3289#endif
3290
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3292 {
3293 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3294 return( ret );
3295 }
3296
3297 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3298 {
3299 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003300 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 }
3302
Paul Bakker1ef83d62012-04-11 12:09:53 +00003303 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3305
3306 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3307 ssl->in_hslen != 4 + hash_len )
3308 {
3309 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003310 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003311 }
3312
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003313 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 {
3315 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003316 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 }
3318
Paul Bakker48916f92012-09-16 19:57:18 +00003319 ssl->verify_data_len = hash_len;
3320 memcpy( ssl->peer_verify_data, buf, hash_len );
3321
Paul Bakker0a597072012-09-25 21:55:46 +00003322 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 {
3324 if( ssl->endpoint == SSL_IS_CLIENT )
3325 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3326
3327 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003328 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 }
3330 else
3331 ssl->state++;
3332
3333 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3334
3335 return( 0 );
3336}
3337
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003338static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003339{
3340 if( ssl->transform_negotiate )
3341 ssl_transform_free( ssl->transform_negotiate );
3342 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003343 {
3344 ssl->transform_negotiate =
3345 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003346
3347 if( ssl->transform_negotiate != NULL )
3348 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003349 }
Paul Bakker48916f92012-09-16 19:57:18 +00003350
3351 if( ssl->session_negotiate )
3352 ssl_session_free( ssl->session_negotiate );
3353 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003354 {
3355 ssl->session_negotiate =
3356 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003357
3358 if( ssl->session_negotiate != NULL )
3359 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003360 }
Paul Bakker48916f92012-09-16 19:57:18 +00003361
3362 if( ssl->handshake )
3363 ssl_handshake_free( ssl->handshake );
3364 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003365 {
3366 ssl->handshake = (ssl_handshake_params *)
3367 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003368
3369 if( ssl->handshake != NULL )
3370 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003371 }
Paul Bakker48916f92012-09-16 19:57:18 +00003372
3373 if( ssl->handshake == NULL ||
3374 ssl->transform_negotiate == NULL ||
3375 ssl->session_negotiate == NULL )
3376 {
3377 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3378 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3379 }
3380
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003381#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3382 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003383 md5_starts( &ssl->handshake->fin_md5 );
3384 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003385#endif
3386#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3387#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003388 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003389#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003390#if defined(POLARSSL_SHA512_C)
3391 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003392#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003393#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003394
3395 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003396 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003397
Paul Bakker61d113b2013-07-04 11:51:43 +02003398#if defined(POLARSSL_ECDH_C)
3399 ecdh_init( &ssl->handshake->ecdh_ctx );
3400#endif
3401
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003402#if defined(POLARSSL_X509_CRT_PARSE_C)
3403 ssl->handshake->key_cert = ssl->key_cert;
3404#endif
3405
Paul Bakker48916f92012-09-16 19:57:18 +00003406 return( 0 );
3407}
3408
Paul Bakker5121ce52009-01-03 21:22:43 +00003409/*
3410 * Initialize an SSL context
3411 */
3412int ssl_init( ssl_context *ssl )
3413{
Paul Bakker48916f92012-09-16 19:57:18 +00003414 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003415 int len = SSL_BUFFER_LEN;
3416
3417 memset( ssl, 0, sizeof( ssl_context ) );
3418
Paul Bakker62f2dee2012-09-28 07:31:51 +00003419 /*
3420 * Sane defaults
3421 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003422 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3423 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3424 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3425 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003426
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003427 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003428
Paul Bakker62f2dee2012-09-28 07:31:51 +00003429#if defined(POLARSSL_DHM_C)
3430 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3431 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3432 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3433 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3434 {
3435 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3436 return( ret );
3437 }
3438#endif
3439
3440 /*
3441 * Prepare base structures
3442 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003443 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003445 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003446 ssl->in_msg = ssl->in_ctr + 13;
3447
3448 if( ssl->in_ctr == NULL )
3449 {
3450 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003451 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 }
3453
Paul Bakker6e339b52013-07-03 13:37:05 +02003454 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003455 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003456 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003457 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003458
3459 if( ssl->out_ctr == NULL )
3460 {
3461 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003462 polarssl_free( ssl->in_ctr );
3463 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003464 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 }
3466
3467 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3468 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3469
Paul Bakker606b4ba2013-08-14 16:52:14 +02003470#if defined(POLARSSL_SSL_SESSION_TICKETS)
3471 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3472#endif
3473
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003474#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003475 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003476#endif
3477
Paul Bakker48916f92012-09-16 19:57:18 +00003478 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3479 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003480
3481 return( 0 );
3482}
3483
3484/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003485 * Reset an initialized and used SSL context for re-use while retaining
3486 * all application-set variables, function pointers and data.
3487 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003488int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003489{
Paul Bakker48916f92012-09-16 19:57:18 +00003490 int ret;
3491
Paul Bakker7eb013f2011-10-06 12:37:39 +00003492 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003493 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3494 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3495
3496 ssl->verify_data_len = 0;
3497 memset( ssl->own_verify_data, 0, 36 );
3498 memset( ssl->peer_verify_data, 0, 36 );
3499
Paul Bakker7eb013f2011-10-06 12:37:39 +00003500 ssl->in_offt = NULL;
3501
Paul Bakker92be97b2013-01-02 17:30:03 +01003502 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003503 ssl->in_msgtype = 0;
3504 ssl->in_msglen = 0;
3505 ssl->in_left = 0;
3506
3507 ssl->in_hslen = 0;
3508 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003509 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003510
Paul Bakker92be97b2013-01-02 17:30:03 +01003511 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003512 ssl->out_msgtype = 0;
3513 ssl->out_msglen = 0;
3514 ssl->out_left = 0;
3515
Paul Bakker48916f92012-09-16 19:57:18 +00003516 ssl->transform_in = NULL;
3517 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003518
3519 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3520 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003521
3522#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3523 if( ssl_hw_record_reset != NULL)
3524 {
3525 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003526 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003527 {
3528 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3529 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3530 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003531 }
3532#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003533
Paul Bakker48916f92012-09-16 19:57:18 +00003534 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003535 {
Paul Bakker48916f92012-09-16 19:57:18 +00003536 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003537 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003538 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003539 }
Paul Bakker48916f92012-09-16 19:57:18 +00003540
Paul Bakkerc0463502013-02-14 11:19:38 +01003541 if( ssl->session )
3542 {
3543 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003544 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003545 ssl->session = NULL;
3546 }
3547
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003548#if defined(POLARSSL_SSL_ALPN)
3549 ssl->alpn_chosen = NULL;
3550#endif
3551
Paul Bakker48916f92012-09-16 19:57:18 +00003552 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3553 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003554
3555 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003556}
3557
Paul Bakkera503a632013-08-14 13:48:06 +02003558#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003559/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003560 * Allocate and initialize ticket keys
3561 */
3562static int ssl_ticket_keys_init( ssl_context *ssl )
3563{
3564 int ret;
3565 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003566 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003567
3568 if( ssl->ticket_keys != NULL )
3569 return( 0 );
3570
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003571 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3572 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003573 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3574
3575 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003576 {
3577 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003578 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003579 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003580
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003581 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3582 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3583 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3584 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003585 polarssl_free( tkeys );
3586 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003587 }
3588
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003589 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003590 {
3591 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003592 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003593 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003594
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003595 ssl->ticket_keys = tkeys;
3596
3597 return( 0 );
3598}
Paul Bakkera503a632013-08-14 13:48:06 +02003599#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003600
3601/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 * SSL set accessors
3603 */
3604void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3605{
3606 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003607
Paul Bakker606b4ba2013-08-14 16:52:14 +02003608#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003609 if( endpoint == SSL_IS_CLIENT )
3610 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003611#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003612}
3613
3614void ssl_set_authmode( ssl_context *ssl, int authmode )
3615{
3616 ssl->authmode = authmode;
3617}
3618
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003619#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003620void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003621 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003622 void *p_vrfy )
3623{
3624 ssl->f_vrfy = f_vrfy;
3625 ssl->p_vrfy = p_vrfy;
3626}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003627#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003628
Paul Bakker5121ce52009-01-03 21:22:43 +00003629void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003630 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 void *p_rng )
3632{
3633 ssl->f_rng = f_rng;
3634 ssl->p_rng = p_rng;
3635}
3636
3637void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003638 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 void *p_dbg )
3640{
3641 ssl->f_dbg = f_dbg;
3642 ssl->p_dbg = p_dbg;
3643}
3644
3645void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003646 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003647 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003648{
3649 ssl->f_recv = f_recv;
3650 ssl->f_send = f_send;
3651 ssl->p_recv = p_recv;
3652 ssl->p_send = p_send;
3653}
3654
Paul Bakker0a597072012-09-25 21:55:46 +00003655void ssl_set_session_cache( ssl_context *ssl,
3656 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3657 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003658{
Paul Bakker0a597072012-09-25 21:55:46 +00003659 ssl->f_get_cache = f_get_cache;
3660 ssl->p_get_cache = p_get_cache;
3661 ssl->f_set_cache = f_set_cache;
3662 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003663}
3664
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003665int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003666{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003667 int ret;
3668
3669 if( ssl == NULL ||
3670 session == NULL ||
3671 ssl->session_negotiate == NULL ||
3672 ssl->endpoint != SSL_IS_CLIENT )
3673 {
3674 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3675 }
3676
3677 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3678 return( ret );
3679
Paul Bakker0a597072012-09-25 21:55:46 +00003680 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003681
3682 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003683}
3684
Paul Bakkerb68cad62012-08-23 08:34:18 +00003685void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003686{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003687 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3688 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3689 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3690 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3691}
3692
3693void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3694 int major, int minor )
3695{
3696 if( major != SSL_MAJOR_VERSION_3 )
3697 return;
3698
3699 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3700 return;
3701
3702 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003703}
3704
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003705#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003706/* Add a new (empty) key_cert entry an return a pointer to it */
3707static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3708{
3709 ssl_key_cert *key_cert, *last;
3710
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003711 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3712 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003713 return( NULL );
3714
3715 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3716
3717 /* Append the new key_cert to the (possibly empty) current list */
3718 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003719 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003720 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003721 if( ssl->handshake != NULL )
3722 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003723 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003724 else
3725 {
3726 last = ssl->key_cert;
3727 while( last->next != NULL )
3728 last = last->next;
3729 last->next = key_cert;
3730 }
3731
3732 return key_cert;
3733}
3734
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003735void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003736 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003737{
3738 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003739 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003740 ssl->peer_cn = peer_cn;
3741}
3742
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003743int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003744 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003745{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003746 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3747
3748 if( key_cert == NULL )
3749 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3750
3751 key_cert->cert = own_cert;
3752 key_cert->key = pk_key;
3753
3754 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003755}
3756
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003757#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003758int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003759 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003760{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003761 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003762 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003763
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003764 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003765 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3766
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003767 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3768 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003769 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003770
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003771 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003772
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003773 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003774 if( ret != 0 )
3775 return( ret );
3776
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003777 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003778 return( ret );
3779
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003780 key_cert->cert = own_cert;
3781 key_cert->key_own_alloc = 1;
3782
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003783 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003784}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003785#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003786
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003787int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003788 void *rsa_key,
3789 rsa_decrypt_func rsa_decrypt,
3790 rsa_sign_func rsa_sign,
3791 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003792{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003793 int ret;
3794 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003795
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003796 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003797 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3798
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003799 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3800 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003801 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003802
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003803 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003804
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003805 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3806 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3807 return( ret );
3808
3809 key_cert->cert = own_cert;
3810 key_cert->key_own_alloc = 1;
3811
3812 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003813}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003814#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003815
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003816#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003817int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3818 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003819{
Paul Bakker6db455e2013-09-18 17:29:31 +02003820 if( psk == NULL || psk_identity == NULL )
3821 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3822
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003823 /*
3824 * The length will be check later anyway, but in case it is obviously
3825 * too large, better abort now. The PMS is as follows:
3826 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3827 */
3828 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3829 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3830
Paul Bakker6db455e2013-09-18 17:29:31 +02003831 if( ssl->psk != NULL )
3832 {
3833 polarssl_free( ssl->psk );
3834 polarssl_free( ssl->psk_identity );
3835 }
3836
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003837 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003838 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003839
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003840 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3841 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003842
3843 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3844 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3845
3846 memcpy( ssl->psk, psk, ssl->psk_len );
3847 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003848
3849 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003850}
3851
3852void ssl_set_psk_cb( ssl_context *ssl,
3853 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3854 size_t),
3855 void *p_psk )
3856{
3857 ssl->f_psk = f_psk;
3858 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003859}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003860#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003861
Paul Bakker48916f92012-09-16 19:57:18 +00003862#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003863int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003864{
3865 int ret;
3866
Paul Bakker48916f92012-09-16 19:57:18 +00003867 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003868 {
3869 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3870 return( ret );
3871 }
3872
Paul Bakker48916f92012-09-16 19:57:18 +00003873 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003874 {
3875 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3876 return( ret );
3877 }
3878
3879 return( 0 );
3880}
3881
Paul Bakker1b57b062011-01-06 15:48:19 +00003882int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3883{
3884 int ret;
3885
Paul Bakker48916f92012-09-16 19:57:18 +00003886 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003887 {
3888 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3889 return( ret );
3890 }
3891
Paul Bakker48916f92012-09-16 19:57:18 +00003892 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003893 {
3894 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3895 return( ret );
3896 }
3897
3898 return( 0 );
3899}
Paul Bakker48916f92012-09-16 19:57:18 +00003900#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003901
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003902#if defined(POLARSSL_SSL_SET_CURVES)
3903/*
3904 * Set the allowed elliptic curves
3905 */
3906void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3907{
3908 ssl->curve_list = curve_list;
3909}
3910#endif
3911
Paul Bakker0be444a2013-08-27 21:55:01 +02003912#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003913int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003914{
3915 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003916 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003917
3918 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003919
3920 if( ssl->hostname_len + 1 == 0 )
3921 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3922
Paul Bakker6e339b52013-07-03 13:37:05 +02003923 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003924
Paul Bakkerb15b8512012-01-13 13:44:06 +00003925 if( ssl->hostname == NULL )
3926 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3927
Paul Bakker3c2122f2013-06-24 19:03:14 +02003928 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003929 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003930
Paul Bakker40ea7de2009-05-03 10:18:48 +00003931 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003932
3933 return( 0 );
3934}
3935
Paul Bakker5701cdc2012-09-27 21:49:42 +00003936void ssl_set_sni( ssl_context *ssl,
3937 int (*f_sni)(void *, ssl_context *,
3938 const unsigned char *, size_t),
3939 void *p_sni )
3940{
3941 ssl->f_sni = f_sni;
3942 ssl->p_sni = p_sni;
3943}
Paul Bakker0be444a2013-08-27 21:55:01 +02003944#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003945
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003946#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003947int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003948{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003949 size_t cur_len, tot_len;
3950 const char **p;
3951
3952 /*
3953 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3954 * truncated". Check lengths now rather than later.
3955 */
3956 tot_len = 0;
3957 for( p = protos; *p != NULL; p++ )
3958 {
3959 cur_len = strlen( *p );
3960 tot_len += cur_len;
3961
3962 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3963 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3964 }
3965
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003966 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003967
3968 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003969}
3970
3971const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3972{
3973 return ssl->alpn_chosen;
3974}
3975#endif /* POLARSSL_SSL_ALPN */
3976
Paul Bakker490ecc82011-10-06 13:04:09 +00003977void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3978{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003979 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3980 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3981 {
3982 ssl->max_major_ver = major;
3983 ssl->max_minor_ver = minor;
3984 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003985}
3986
Paul Bakker1d29fb52012-09-28 13:28:45 +00003987void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3988{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003989 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3990 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3991 {
3992 ssl->min_major_ver = major;
3993 ssl->min_minor_ver = minor;
3994 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003995}
3996
Paul Bakker05decb22013-08-15 13:33:48 +02003997#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003998int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3999{
Paul Bakker77e257e2013-12-16 15:29:52 +01004000 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004001 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004002 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004003 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004004 }
4005
4006 ssl->mfl_code = mfl_code;
4007
4008 return( 0 );
4009}
Paul Bakker05decb22013-08-15 13:33:48 +02004010#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004011
Paul Bakker1f2bc622013-08-15 13:45:55 +02004012#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004013int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004014{
4015 if( ssl->endpoint != SSL_IS_CLIENT )
4016 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4017
Paul Bakker8c1ede62013-07-19 14:14:37 +02004018 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004019
4020 return( 0 );
4021}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004022#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004023
Paul Bakker48916f92012-09-16 19:57:18 +00004024void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4025{
4026 ssl->disable_renegotiation = renegotiation;
4027}
4028
4029void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4030{
4031 ssl->allow_legacy_renegotiation = allow_legacy;
4032}
4033
Paul Bakkera503a632013-08-14 13:48:06 +02004034#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004035int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4036{
4037 ssl->session_tickets = use_tickets;
4038
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004039 if( ssl->endpoint == SSL_IS_CLIENT )
4040 return( 0 );
4041
4042 if( ssl->f_rng == NULL )
4043 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4044
4045 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004046}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004047
4048void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4049{
4050 ssl->ticket_lifetime = lifetime;
4051}
Paul Bakkera503a632013-08-14 13:48:06 +02004052#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004053
Paul Bakker5121ce52009-01-03 21:22:43 +00004054/*
4055 * SSL get accessors
4056 */
Paul Bakker23986e52011-04-24 08:57:21 +00004057size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004058{
4059 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4060}
4061
Paul Bakkerff60ee62010-03-16 21:09:09 +00004062int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004063{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004064 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004065}
4066
Paul Bakkere3166ce2011-01-27 17:40:50 +00004067const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004068{
Paul Bakker926c8e42013-03-06 10:23:34 +01004069 if( ssl == NULL || ssl->session == NULL )
4070 return NULL;
4071
Paul Bakkere3166ce2011-01-27 17:40:50 +00004072 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004073}
4074
Paul Bakker43ca69c2011-01-15 17:35:19 +00004075const char *ssl_get_version( const ssl_context *ssl )
4076{
4077 switch( ssl->minor_ver )
4078 {
4079 case SSL_MINOR_VERSION_0:
4080 return( "SSLv3.0" );
4081
4082 case SSL_MINOR_VERSION_1:
4083 return( "TLSv1.0" );
4084
4085 case SSL_MINOR_VERSION_2:
4086 return( "TLSv1.1" );
4087
Paul Bakker1ef83d62012-04-11 12:09:53 +00004088 case SSL_MINOR_VERSION_3:
4089 return( "TLSv1.2" );
4090
Paul Bakker43ca69c2011-01-15 17:35:19 +00004091 default:
4092 break;
4093 }
4094 return( "unknown" );
4095}
4096
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004097#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004098const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004099{
4100 if( ssl == NULL || ssl->session == NULL )
4101 return NULL;
4102
4103 return ssl->session->peer_cert;
4104}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004105#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004106
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004107int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4108{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004109 if( ssl == NULL ||
4110 dst == NULL ||
4111 ssl->session == NULL ||
4112 ssl->endpoint != SSL_IS_CLIENT )
4113 {
4114 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4115 }
4116
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004117 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004118}
4119
Paul Bakker5121ce52009-01-03 21:22:43 +00004120/*
Paul Bakker1961b702013-01-25 14:49:24 +01004121 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004122 */
Paul Bakker1961b702013-01-25 14:49:24 +01004123int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004124{
Paul Bakker40e46942009-01-03 21:51:57 +00004125 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004126
Paul Bakker40e46942009-01-03 21:51:57 +00004127#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004128 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004129 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004130#endif
4131
Paul Bakker40e46942009-01-03 21:51:57 +00004132#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004133 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004134 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004135#endif
4136
Paul Bakker1961b702013-01-25 14:49:24 +01004137 return( ret );
4138}
4139
4140/*
4141 * Perform the SSL handshake
4142 */
4143int ssl_handshake( ssl_context *ssl )
4144{
4145 int ret = 0;
4146
4147 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4148
4149 while( ssl->state != SSL_HANDSHAKE_OVER )
4150 {
4151 ret = ssl_handshake_step( ssl );
4152
4153 if( ret != 0 )
4154 break;
4155 }
4156
Paul Bakker5121ce52009-01-03 21:22:43 +00004157 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4158
4159 return( ret );
4160}
4161
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004162#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004163/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004164 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004165 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004166static int ssl_write_hello_request( ssl_context *ssl )
4167{
4168 int ret;
4169
4170 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4171
4172 ssl->out_msglen = 4;
4173 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4174 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4175
4176 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4177 {
4178 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4179 return( ret );
4180 }
4181
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004182 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4183
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004184 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4185
4186 return( 0 );
4187}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004188#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004189
4190/*
4191 * Actually renegotiate current connection, triggered by either:
4192 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004193 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004194 * - receiving any handshake message on server during ssl_read() after the
4195 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004196 * If the handshake doesn't complete due to waiting for I/O, it will continue
4197 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004198 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004199static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004200{
4201 int ret;
4202
4203 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4204
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004205 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4206 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004207
4208 ssl->state = SSL_HELLO_REQUEST;
4209 ssl->renegotiation = SSL_RENEGOTIATION;
4210
Paul Bakker48916f92012-09-16 19:57:18 +00004211 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4212 {
4213 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4214 return( ret );
4215 }
4216
4217 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4218
4219 return( 0 );
4220}
4221
4222/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004223 * Renegotiate current connection on client,
4224 * or request renegotiation on server
4225 */
4226int ssl_renegotiate( ssl_context *ssl )
4227{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004228 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004229
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004230#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004231 /* On server, just send the request */
4232 if( ssl->endpoint == SSL_IS_SERVER )
4233 {
4234 if( ssl->state != SSL_HANDSHAKE_OVER )
4235 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4236
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004237 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004238 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004239#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004240
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004241#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004242 /*
4243 * On client, either start the renegotiation process or,
4244 * if already in progress, continue the handshake
4245 */
4246 if( ssl->renegotiation != SSL_RENEGOTIATION )
4247 {
4248 if( ssl->state != SSL_HANDSHAKE_OVER )
4249 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4250
4251 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4252 {
4253 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4254 return( ret );
4255 }
4256 }
4257 else
4258 {
4259 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4260 {
4261 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4262 return( ret );
4263 }
4264 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004265#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004266
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004267 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004268}
4269
4270/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004271 * Receive application data decrypted from the SSL layer
4272 */
Paul Bakker23986e52011-04-24 08:57:21 +00004273int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004274{
Paul Bakker23986e52011-04-24 08:57:21 +00004275 int ret;
4276 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004277
4278 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4279
4280 if( ssl->state != SSL_HANDSHAKE_OVER )
4281 {
4282 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4283 {
4284 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4285 return( ret );
4286 }
4287 }
4288
4289 if( ssl->in_offt == NULL )
4290 {
4291 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4292 {
Paul Bakker831a7552011-05-18 13:32:51 +00004293 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4294 return( 0 );
4295
Paul Bakker5121ce52009-01-03 21:22:43 +00004296 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4297 return( ret );
4298 }
4299
4300 if( ssl->in_msglen == 0 &&
4301 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4302 {
4303 /*
4304 * OpenSSL sends empty messages to randomize the IV
4305 */
4306 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4307 {
Paul Bakker831a7552011-05-18 13:32:51 +00004308 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4309 return( 0 );
4310
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4312 return( ret );
4313 }
4314 }
4315
Paul Bakker48916f92012-09-16 19:57:18 +00004316 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4317 {
4318 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4319
4320 if( ssl->endpoint == SSL_IS_CLIENT &&
4321 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4322 ssl->in_hslen != 4 ) )
4323 {
4324 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4325 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4326 }
4327
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004328 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4329 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4330 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004331 {
4332 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4333
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004334#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004335 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004336 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004337 /*
4338 * SSLv3 does not have a "no_renegotiation" alert
4339 */
4340 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4341 return( ret );
4342 }
4343 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004344#endif
4345#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4346 defined(POLARSSL_SSL_PROTO_TLS1_2)
4347 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004348 {
4349 if( ( ret = ssl_send_alert_message( ssl,
4350 SSL_ALERT_LEVEL_WARNING,
4351 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4352 {
4353 return( ret );
4354 }
Paul Bakker48916f92012-09-16 19:57:18 +00004355 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004356 else
4357#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004358 {
4359 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004360 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004361 }
Paul Bakker48916f92012-09-16 19:57:18 +00004362 }
4363 else
4364 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004365 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004366 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004367 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004368 return( ret );
4369 }
4370
4371 return( POLARSSL_ERR_NET_WANT_READ );
4372 }
4373 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004374 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4375 {
4376 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4377 "but not honored by client" ) );
4378 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4379 }
Paul Bakker48916f92012-09-16 19:57:18 +00004380 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004381 {
4382 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004383 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004384 }
4385
4386 ssl->in_offt = ssl->in_msg;
4387 }
4388
4389 n = ( len < ssl->in_msglen )
4390 ? len : ssl->in_msglen;
4391
4392 memcpy( buf, ssl->in_offt, n );
4393 ssl->in_msglen -= n;
4394
4395 if( ssl->in_msglen == 0 )
4396 /* all bytes consumed */
4397 ssl->in_offt = NULL;
4398 else
4399 /* more data available */
4400 ssl->in_offt += n;
4401
4402 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4403
Paul Bakker23986e52011-04-24 08:57:21 +00004404 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004405}
4406
4407/*
4408 * Send application data to be encrypted by the SSL layer
4409 */
Paul Bakker23986e52011-04-24 08:57:21 +00004410int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004411{
Paul Bakker23986e52011-04-24 08:57:21 +00004412 int ret;
4413 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004414 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004415
4416 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4417
4418 if( ssl->state != SSL_HANDSHAKE_OVER )
4419 {
4420 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4421 {
4422 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4423 return( ret );
4424 }
4425 }
4426
Paul Bakker05decb22013-08-15 13:33:48 +02004427#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004428 /*
4429 * Assume mfl_code is correct since it was checked when set
4430 */
4431 max_len = mfl_code_to_length[ssl->mfl_code];
4432
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004433 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004434 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004435 */
4436 if( ssl->session_out != NULL &&
4437 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4438 {
4439 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4440 }
Paul Bakker05decb22013-08-15 13:33:48 +02004441#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004442
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004443 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004444
Paul Bakker5121ce52009-01-03 21:22:43 +00004445 if( ssl->out_left != 0 )
4446 {
4447 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4448 {
4449 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4450 return( ret );
4451 }
4452 }
Paul Bakker887bd502011-06-08 13:10:54 +00004453 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004454 {
Paul Bakker887bd502011-06-08 13:10:54 +00004455 ssl->out_msglen = n;
4456 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4457 memcpy( ssl->out_msg, buf, n );
4458
4459 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4460 {
4461 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4462 return( ret );
4463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004464 }
4465
4466 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4467
Paul Bakker23986e52011-04-24 08:57:21 +00004468 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004469}
4470
4471/*
4472 * Notify the peer that the connection is being closed
4473 */
4474int ssl_close_notify( ssl_context *ssl )
4475{
4476 int ret;
4477
4478 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4479
4480 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4481 {
4482 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4483 return( ret );
4484 }
4485
4486 if( ssl->state == SSL_HANDSHAKE_OVER )
4487 {
Paul Bakker48916f92012-09-16 19:57:18 +00004488 if( ( ret = ssl_send_alert_message( ssl,
4489 SSL_ALERT_LEVEL_WARNING,
4490 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004491 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004492 return( ret );
4493 }
4494 }
4495
4496 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4497
4498 return( ret );
4499}
4500
Paul Bakker48916f92012-09-16 19:57:18 +00004501void ssl_transform_free( ssl_transform *transform )
4502{
4503#if defined(POLARSSL_ZLIB_SUPPORT)
4504 deflateEnd( &transform->ctx_deflate );
4505 inflateEnd( &transform->ctx_inflate );
4506#endif
4507
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004508 cipher_free_ctx( &transform->cipher_ctx_enc );
4509 cipher_free_ctx( &transform->cipher_ctx_dec );
4510
Paul Bakker61d113b2013-07-04 11:51:43 +02004511 md_free_ctx( &transform->md_ctx_enc );
4512 md_free_ctx( &transform->md_ctx_dec );
4513
Paul Bakker48916f92012-09-16 19:57:18 +00004514 memset( transform, 0, sizeof( ssl_transform ) );
4515}
4516
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004517#if defined(POLARSSL_X509_CRT_PARSE_C)
4518static void ssl_key_cert_free( ssl_key_cert *key_cert )
4519{
4520 ssl_key_cert *cur = key_cert, *next;
4521
4522 while( cur != NULL )
4523 {
4524 next = cur->next;
4525
4526 if( cur->key_own_alloc )
4527 {
4528 pk_free( cur->key );
4529 polarssl_free( cur->key );
4530 }
4531 polarssl_free( cur );
4532
4533 cur = next;
4534 }
4535}
4536#endif /* POLARSSL_X509_CRT_PARSE_C */
4537
Paul Bakker48916f92012-09-16 19:57:18 +00004538void ssl_handshake_free( ssl_handshake_params *handshake )
4539{
4540#if defined(POLARSSL_DHM_C)
4541 dhm_free( &handshake->dhm_ctx );
4542#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004543#if defined(POLARSSL_ECDH_C)
4544 ecdh_free( &handshake->ecdh_ctx );
4545#endif
4546
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004547#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004548 /* explicit void pointer cast for buggy MS compiler */
4549 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004550#endif
4551
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004552#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4553 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4554 /*
4555 * Free only the linked list wrapper, not the keys themselves
4556 * since the belong to the SNI callback
4557 */
4558 if( handshake->sni_key_cert != NULL )
4559 {
4560 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4561
4562 while( cur != NULL )
4563 {
4564 next = cur->next;
4565 polarssl_free( cur );
4566 cur = next;
4567 }
4568 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004569#endif
4570
Paul Bakker48916f92012-09-16 19:57:18 +00004571 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4572}
4573
4574void ssl_session_free( ssl_session *session )
4575{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004576#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004577 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004578 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004579 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004580 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004581 }
Paul Bakkered27a042013-04-18 22:46:23 +02004582#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004583
Paul Bakkera503a632013-08-14 13:48:06 +02004584#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004585 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004586#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004587
Paul Bakker0a597072012-09-25 21:55:46 +00004588 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004589}
4590
Paul Bakker5121ce52009-01-03 21:22:43 +00004591/*
4592 * Free an SSL context
4593 */
4594void ssl_free( ssl_context *ssl )
4595{
4596 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4597
Paul Bakker5121ce52009-01-03 21:22:43 +00004598 if( ssl->out_ctr != NULL )
4599 {
4600 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004601 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004602 }
4603
4604 if( ssl->in_ctr != NULL )
4605 {
4606 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004607 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004608 }
4609
Paul Bakker16770332013-10-11 09:59:44 +02004610#if defined(POLARSSL_ZLIB_SUPPORT)
4611 if( ssl->compress_buf != NULL )
4612 {
4613 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4614 polarssl_free( ssl->compress_buf );
4615 }
4616#endif
4617
Paul Bakker40e46942009-01-03 21:51:57 +00004618#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004619 mpi_free( &ssl->dhm_P );
4620 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004621#endif
4622
Paul Bakker48916f92012-09-16 19:57:18 +00004623 if( ssl->transform )
4624 {
4625 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004626 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004627 }
4628
4629 if( ssl->handshake )
4630 {
4631 ssl_handshake_free( ssl->handshake );
4632 ssl_transform_free( ssl->transform_negotiate );
4633 ssl_session_free( ssl->session_negotiate );
4634
Paul Bakker6e339b52013-07-03 13:37:05 +02004635 polarssl_free( ssl->handshake );
4636 polarssl_free( ssl->transform_negotiate );
4637 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004638 }
4639
Paul Bakkerc0463502013-02-14 11:19:38 +01004640 if( ssl->session )
4641 {
4642 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004643 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004644 }
4645
Paul Bakkera503a632013-08-14 13:48:06 +02004646#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004647 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004648#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004649
Paul Bakker0be444a2013-08-27 21:55:01 +02004650#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004651 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 {
4653 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004654 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004655 ssl->hostname_len = 0;
4656 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004657#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004658
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004659#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004660 if( ssl->psk != NULL )
4661 {
4662 memset( ssl->psk, 0, ssl->psk_len );
4663 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4664 polarssl_free( ssl->psk );
4665 polarssl_free( ssl->psk_identity );
4666 ssl->psk_len = 0;
4667 ssl->psk_identity_len = 0;
4668 }
4669#endif
4670
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004671#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004672 ssl_key_cert_free( ssl->key_cert );
4673#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004674
Paul Bakker05ef8352012-05-08 09:17:57 +00004675#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4676 if( ssl_hw_record_finish != NULL )
4677 {
4678 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4679 ssl_hw_record_finish( ssl );
4680 }
4681#endif
4682
Paul Bakker5121ce52009-01-03 21:22:43 +00004683 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004684
Paul Bakker86f04f42013-02-14 11:20:09 +01004685 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004686 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004687}
4688
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004689#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004690/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004691 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004692 */
4693unsigned char ssl_sig_from_pk( pk_context *pk )
4694{
4695#if defined(POLARSSL_RSA_C)
4696 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4697 return( SSL_SIG_RSA );
4698#endif
4699#if defined(POLARSSL_ECDSA_C)
4700 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4701 return( SSL_SIG_ECDSA );
4702#endif
4703 return( SSL_SIG_ANON );
4704}
4705
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004706pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4707{
4708 switch( sig )
4709 {
4710#if defined(POLARSSL_RSA_C)
4711 case SSL_SIG_RSA:
4712 return( POLARSSL_PK_RSA );
4713#endif
4714#if defined(POLARSSL_ECDSA_C)
4715 case SSL_SIG_ECDSA:
4716 return( POLARSSL_PK_ECDSA );
4717#endif
4718 default:
4719 return( POLARSSL_PK_NONE );
4720 }
4721}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004722#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004723
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004724/*
4725 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4726 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004727md_type_t ssl_md_alg_from_hash( unsigned char hash )
4728{
4729 switch( hash )
4730 {
4731#if defined(POLARSSL_MD5_C)
4732 case SSL_HASH_MD5:
4733 return( POLARSSL_MD_MD5 );
4734#endif
4735#if defined(POLARSSL_SHA1_C)
4736 case SSL_HASH_SHA1:
4737 return( POLARSSL_MD_SHA1 );
4738#endif
4739#if defined(POLARSSL_SHA256_C)
4740 case SSL_HASH_SHA224:
4741 return( POLARSSL_MD_SHA224 );
4742 case SSL_HASH_SHA256:
4743 return( POLARSSL_MD_SHA256 );
4744#endif
4745#if defined(POLARSSL_SHA512_C)
4746 case SSL_HASH_SHA384:
4747 return( POLARSSL_MD_SHA384 );
4748 case SSL_HASH_SHA512:
4749 return( POLARSSL_MD_SHA512 );
4750#endif
4751 default:
4752 return( POLARSSL_MD_NONE );
4753 }
4754}
4755
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004756#if defined(POLARSSL_SSL_SET_CURVES)
4757/*
4758 * Check is a curve proposed by the peer is in our list.
4759 * Return 1 if we're willing to use it, 0 otherwise.
4760 */
4761int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4762{
4763 const ecp_group_id *gid;
4764
4765 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4766 if( *gid == grp_id )
4767 return( 1 );
4768
4769 return( 0 );
4770}
4771#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004772
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004773#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004774int ssl_check_cert_usage( const x509_crt *cert,
4775 const ssl_ciphersuite_t *ciphersuite,
4776 int cert_endpoint )
4777{
4778#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4779 int usage = 0;
4780#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004781#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4782 const char *ext_oid;
4783 size_t ext_len;
4784#endif
4785
4786#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4787 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4788 ((void) cert);
4789 ((void) cert_endpoint);
4790#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004791
4792#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4793 if( cert_endpoint == SSL_IS_SERVER )
4794 {
4795 /* Server part of the key exchange */
4796 switch( ciphersuite->key_exchange )
4797 {
4798 case POLARSSL_KEY_EXCHANGE_RSA:
4799 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4800 usage = KU_KEY_ENCIPHERMENT;
4801 break;
4802
4803 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4804 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4805 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4806 usage = KU_DIGITAL_SIGNATURE;
4807 break;
4808
4809 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4810 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4811 usage = KU_KEY_AGREEMENT;
4812 break;
4813
4814 /* Don't use default: we want warnings when adding new values */
4815 case POLARSSL_KEY_EXCHANGE_NONE:
4816 case POLARSSL_KEY_EXCHANGE_PSK:
4817 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4818 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4819 usage = 0;
4820 }
4821 }
4822 else
4823 {
4824 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4825 usage = KU_DIGITAL_SIGNATURE;
4826 }
4827
4828 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4829 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004830#else
4831 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004832#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4833
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004834#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4835 if( cert_endpoint == SSL_IS_SERVER )
4836 {
4837 ext_oid = OID_SERVER_AUTH;
4838 ext_len = OID_SIZE( OID_SERVER_AUTH );
4839 }
4840 else
4841 {
4842 ext_oid = OID_CLIENT_AUTH;
4843 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4844 }
4845
4846 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4847 return( -1 );
4848#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4849
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004850 return( 0 );
4851}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004852#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004853
4854#endif /* POLARSSL_SSL_TLS_C */