blob: 116bc5cf4c6a61ac7fb0791a9f0637e841ebb840 [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 Bakker91c61bc2014-03-26 14:06:55 +01001636 * 2. padding_idx > SSL_MAX_CONTENT_LEN
Paul Bakker956c9e02013-12-19 14:42:28 +01001637 *
1638 * In both cases we reset padding_idx to a safe value (0) to
1639 * prevent out-of-buffer reads.
1640 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001641 correct &= ( ssl->in_msglen >= padlen + 1 );
1642 correct &= ( padding_idx <= SSL_MAX_CONTENT_LEN );
Paul Bakker956c9e02013-12-19 14:42:28 +01001643
1644 padding_idx *= correct;
1645
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001646 for( i = 1; i <= 256; i++ )
1647 {
1648 real_count &= ( i <= padlen );
1649 pad_count += real_count *
1650 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1651 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001652
1653 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001654
Paul Bakkerd66f0702013-01-31 16:57:45 +01001655#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001656 if( padlen > 0 && correct == 0)
1657 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001658#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001659 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001660 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001661 else
1662#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1663 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001664 {
1665 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001666 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001669 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001670#endif /* POLARSSL_CIPHER_MODE_CBC &&
1671 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001672 {
1673 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1674 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
1677 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1678 ssl->in_msg, ssl->in_msglen );
1679
1680 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001681 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001682 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001683#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1684 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1685 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1686 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1687 POLARSSL_MODE_GCM )
1688 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001689 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1690
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001691 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001693 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1694 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001696 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001698#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001699 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1700 {
1701 ssl_mac( &ssl->transform_in->md_ctx_dec,
1702 ssl->transform_in->mac_dec,
1703 ssl->in_msg, ssl->in_msglen,
1704 ssl->in_ctr, ssl->in_msgtype );
1705 }
1706 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001707#endif /* POLARSSL_SSL_PROTO_SSL3 */
1708#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001709 defined(POLARSSL_SSL_PROTO_TLS1_2)
1710 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1711 {
1712 /*
1713 * Process MAC and always update for padlen afterwards to make
1714 * total time independent of padlen
1715 *
1716 * extra_run compensates MAC check for padlen
1717 *
1718 * Known timing attacks:
1719 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1720 *
1721 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1722 * correctly. (We round down instead of up, so -56 is the correct
1723 * value for our calculations instead of -55)
1724 */
1725 size_t j, extra_run = 0;
1726 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1727 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001728
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001729 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001730
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001731 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1732 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1733 ssl->in_msglen );
1734 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1735 ssl->in_msg + ssl->in_msglen );
1736 for( j = 0; j < extra_run; j++ )
1737 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001738
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001739 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1740 }
1741 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001742#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001743 POLARSSL_SSL_PROTO_TLS1_2 */
1744 {
1745 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1746 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1747 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001749 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1750 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1751 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001752
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001753 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001754 ssl->transform_in->maclen ) != 0 )
1755 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001756#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001757 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001758#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001759 correct = 0;
1760 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001762 /*
1763 * Finally check the correct flag
1764 */
1765 if( correct == 0 )
1766 return( POLARSSL_ERR_SSL_INVALID_MAC );
1767 }
1768#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001769
1770 if( ssl->in_msglen == 0 )
1771 {
1772 ssl->nb_zero++;
1773
1774 /*
1775 * Three or more empty messages may be a DoS attack
1776 * (excessive CPU consumption).
1777 */
1778 if( ssl->nb_zero > 3 )
1779 {
1780 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1781 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001782 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 }
1784 }
1785 else
1786 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001787
Paul Bakker23986e52011-04-24 08:57:21 +00001788 for( i = 8; i > 0; i-- )
1789 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 break;
1791
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001792 /* The loops goes to its end iff the counter is wrapping */
1793 if( i == 0 )
1794 {
1795 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1796 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1797 }
1798
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1800
1801 return( 0 );
1802}
1803
Paul Bakker2770fbd2012-07-03 13:30:23 +00001804#if defined(POLARSSL_ZLIB_SUPPORT)
1805/*
1806 * Compression/decompression functions
1807 */
1808static int ssl_compress_buf( ssl_context *ssl )
1809{
1810 int ret;
1811 unsigned char *msg_post = ssl->out_msg;
1812 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001813 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001814
1815 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1816
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001817 if( len_pre == 0 )
1818 return( 0 );
1819
Paul Bakker2770fbd2012-07-03 13:30:23 +00001820 memcpy( msg_pre, ssl->out_msg, len_pre );
1821
1822 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1823 ssl->out_msglen ) );
1824
1825 SSL_DEBUG_BUF( 4, "before compression: output payload",
1826 ssl->out_msg, ssl->out_msglen );
1827
Paul Bakker48916f92012-09-16 19:57:18 +00001828 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1829 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1830 ssl->transform_out->ctx_deflate.next_out = msg_post;
1831 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001832
Paul Bakker48916f92012-09-16 19:57:18 +00001833 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001834 if( ret != Z_OK )
1835 {
1836 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1837 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1838 }
1839
Paul Bakker48916f92012-09-16 19:57:18 +00001840 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001841
Paul Bakker2770fbd2012-07-03 13:30:23 +00001842 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1843 ssl->out_msglen ) );
1844
1845 SSL_DEBUG_BUF( 4, "after compression: output payload",
1846 ssl->out_msg, ssl->out_msglen );
1847
1848 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1849
1850 return( 0 );
1851}
1852
1853static int ssl_decompress_buf( ssl_context *ssl )
1854{
1855 int ret;
1856 unsigned char *msg_post = ssl->in_msg;
1857 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001858 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001859
1860 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1861
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001862 if( len_pre == 0 )
1863 return( 0 );
1864
Paul Bakker2770fbd2012-07-03 13:30:23 +00001865 memcpy( msg_pre, ssl->in_msg, len_pre );
1866
1867 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1868 ssl->in_msglen ) );
1869
1870 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1871 ssl->in_msg, ssl->in_msglen );
1872
Paul Bakker48916f92012-09-16 19:57:18 +00001873 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1874 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1875 ssl->transform_in->ctx_inflate.next_out = msg_post;
1876 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001877
Paul Bakker48916f92012-09-16 19:57:18 +00001878 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001879 if( ret != Z_OK )
1880 {
1881 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1882 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1883 }
1884
Paul Bakker48916f92012-09-16 19:57:18 +00001885 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001886
Paul Bakker2770fbd2012-07-03 13:30:23 +00001887 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1888 ssl->in_msglen ) );
1889
1890 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1891 ssl->in_msg, ssl->in_msglen );
1892
1893 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1894
1895 return( 0 );
1896}
1897#endif /* POLARSSL_ZLIB_SUPPORT */
1898
Paul Bakker5121ce52009-01-03 21:22:43 +00001899/*
1900 * Fill the input message buffer
1901 */
Paul Bakker23986e52011-04-24 08:57:21 +00001902int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001903{
Paul Bakker23986e52011-04-24 08:57:21 +00001904 int ret;
1905 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
1907 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1908
1909 while( ssl->in_left < nb_want )
1910 {
1911 len = nb_want - ssl->in_left;
1912 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1913
1914 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1915 ssl->in_left, nb_want ) );
1916 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1917
Paul Bakker831a7552011-05-18 13:32:51 +00001918 if( ret == 0 )
1919 return( POLARSSL_ERR_SSL_CONN_EOF );
1920
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 if( ret < 0 )
1922 return( ret );
1923
1924 ssl->in_left += ret;
1925 }
1926
1927 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1928
1929 return( 0 );
1930}
1931
1932/*
1933 * Flush any data not yet written
1934 */
1935int ssl_flush_output( ssl_context *ssl )
1936{
1937 int ret;
1938 unsigned char *buf;
1939
1940 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1941
1942 while( ssl->out_left > 0 )
1943 {
1944 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1945 5 + ssl->out_msglen, ssl->out_left ) );
1946
Paul Bakker5bd42292012-12-19 14:40:42 +01001947 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001949
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1951
1952 if( ret <= 0 )
1953 return( ret );
1954
1955 ssl->out_left -= ret;
1956 }
1957
1958 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1959
1960 return( 0 );
1961}
1962
1963/*
1964 * Record layer functions
1965 */
1966int ssl_write_record( ssl_context *ssl )
1967{
Paul Bakker05ef8352012-05-08 09:17:57 +00001968 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001969 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
1971 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1972
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1974 {
1975 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1976 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1977 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1978
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001979 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1980 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001981 }
1982
Paul Bakker2770fbd2012-07-03 13:30:23 +00001983#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001984 if( ssl->transform_out != NULL &&
1985 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001986 {
1987 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1988 {
1989 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1990 return( ret );
1991 }
1992
1993 len = ssl->out_msglen;
1994 }
1995#endif /*POLARSSL_ZLIB_SUPPORT */
1996
Paul Bakker05ef8352012-05-08 09:17:57 +00001997#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1998 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002000 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
Paul Bakker05ef8352012-05-08 09:17:57 +00002002 ret = ssl_hw_record_write( ssl );
2003 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2004 {
2005 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
2006 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2007 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002008
2009 if( ret == 0 )
2010 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002011 }
2012#endif
2013 if( !done )
2014 {
2015 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2016 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2017 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2019 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002020
Paul Bakker48916f92012-09-16 19:57:18 +00002021 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002022 {
2023 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2024 {
2025 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2026 return( ret );
2027 }
2028
2029 len = ssl->out_msglen;
2030 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2031 ssl->out_hdr[4] = (unsigned char)( len );
2032 }
2033
2034 ssl->out_left = 5 + ssl->out_msglen;
2035
2036 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2037 "version = [%d:%d], msglen = %d",
2038 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2039 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2040
2041 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002042 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 }
2044
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2046 {
2047 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2048 return( ret );
2049 }
2050
2051 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2052
2053 return( 0 );
2054}
2055
2056int ssl_read_record( ssl_context *ssl )
2057{
Paul Bakker05ef8352012-05-08 09:17:57 +00002058 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002059
2060 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2061
Paul Bakker68884e32013-01-07 18:20:04 +01002062 SSL_DEBUG_BUF( 4, "input record from network",
2063 ssl->in_hdr, 5 + ssl->in_msglen );
2064
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 if( ssl->in_hslen != 0 &&
2066 ssl->in_hslen < ssl->in_msglen )
2067 {
2068 /*
2069 * Get next Handshake message in the current record
2070 */
2071 ssl->in_msglen -= ssl->in_hslen;
2072
Paul Bakker8934a982011-08-05 11:11:53 +00002073 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002074 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002075
2076 ssl->in_hslen = 4;
2077 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2078
2079 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2080 " %d, type = %d, hslen = %d",
2081 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2082
2083 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2084 {
2085 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002086 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 }
2088
2089 if( ssl->in_msglen < ssl->in_hslen )
2090 {
2091 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002092 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 }
2094
Paul Bakker4224bc02014-04-08 14:36:50 +02002095 if( ssl->state != SSL_HANDSHAKE_OVER )
2096 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002097
2098 return( 0 );
2099 }
2100
2101 ssl->in_hslen = 0;
2102
2103 /*
2104 * Read the record header and validate it
2105 */
2106 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2107 {
2108 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2109 return( ret );
2110 }
2111
2112 ssl->in_msgtype = ssl->in_hdr[0];
2113 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2114
2115 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2116 "version = [%d:%d], msglen = %d",
2117 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2118 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2119
2120 if( ssl->in_hdr[1] != ssl->major_ver )
2121 {
2122 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002123 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
2125
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002126 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 {
2128 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002129 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 }
2131
2132 /*
2133 * Make sure the message length is acceptable
2134 */
Paul Bakker48916f92012-09-16 19:57:18 +00002135 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 {
2137 if( ssl->in_msglen < 1 ||
2138 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2139 {
2140 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002141 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 }
2143 }
2144 else
2145 {
Paul Bakker48916f92012-09-16 19:57:18 +00002146 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 {
2148 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002149 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 }
2151
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002152#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002154 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 {
2156 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002157 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002159#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002160
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002161#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2162 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 /*
2164 * TLS encrypted messages can have up to 256 bytes of padding
2165 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002166 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002167 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 {
2169 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002170 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002171 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002172#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 }
2174
2175 /*
2176 * Read and optionally decrypt the message contents
2177 */
2178 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2179 {
2180 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2181 return( ret );
2182 }
2183
2184 SSL_DEBUG_BUF( 4, "input record from network",
2185 ssl->in_hdr, 5 + ssl->in_msglen );
2186
Paul Bakker05ef8352012-05-08 09:17:57 +00002187#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2188 if( ssl_hw_record_read != NULL)
2189 {
2190 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2191
2192 ret = ssl_hw_record_read( ssl );
2193 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2194 {
2195 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2196 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2197 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002198
2199 if( ret == 0 )
2200 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002201 }
2202#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002203 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 {
2205 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2206 {
Paul Bakker40865c82013-01-31 17:13:13 +01002207#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2208 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2209 {
2210 ssl_send_alert_message( ssl,
2211 SSL_ALERT_LEVEL_FATAL,
2212 SSL_ALERT_MSG_BAD_RECORD_MAC );
2213 }
2214#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2216 return( ret );
2217 }
2218
2219 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2220 ssl->in_msg, ssl->in_msglen );
2221
2222 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2223 {
2224 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002225 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 }
2227 }
2228
Paul Bakker2770fbd2012-07-03 13:30:23 +00002229#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002230 if( ssl->transform_in != NULL &&
2231 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002232 {
2233 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2234 {
2235 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2236 return( ret );
2237 }
2238
2239 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2240 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2241 }
2242#endif /* POLARSSL_ZLIB_SUPPORT */
2243
Paul Bakker0a925182012-04-16 06:46:41 +00002244 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2245 ssl->in_msgtype != SSL_MSG_ALERT &&
2246 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2247 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2248 {
2249 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2250
Paul Bakker48916f92012-09-16 19:57:18 +00002251 if( ( ret = ssl_send_alert_message( ssl,
2252 SSL_ALERT_LEVEL_FATAL,
2253 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002254 {
2255 return( ret );
2256 }
2257
2258 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2259 }
2260
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2262 {
2263 ssl->in_hslen = 4;
2264 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2265
2266 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2267 " %d, type = %d, hslen = %d",
2268 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2269
2270 /*
2271 * Additional checks to validate the handshake header
2272 */
2273 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2274 {
2275 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002276 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 }
2278
2279 if( ssl->in_msglen < ssl->in_hslen )
2280 {
2281 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002282 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 }
2284
Paul Bakker48916f92012-09-16 19:57:18 +00002285 if( ssl->state != SSL_HANDSHAKE_OVER )
2286 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
2288
2289 if( ssl->in_msgtype == SSL_MSG_ALERT )
2290 {
2291 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2292 ssl->in_msg[0], ssl->in_msg[1] ) );
2293
2294 /*
2295 * Ignore non-fatal alerts, except close_notify
2296 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002297 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002299 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2300 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002301 /**
2302 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2303 * error identifier.
2304 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002305 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 }
2307
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002308 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2309 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002310 {
2311 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002312 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 }
2314 }
2315
2316 ssl->in_left = 0;
2317
2318 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2319
2320 return( 0 );
2321}
2322
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002323int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2324{
2325 int ret;
2326
2327 if( ( ret = ssl_send_alert_message( ssl,
2328 SSL_ALERT_LEVEL_FATAL,
2329 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2330 {
2331 return( ret );
2332 }
2333
2334 return( 0 );
2335}
2336
Paul Bakker0a925182012-04-16 06:46:41 +00002337int ssl_send_alert_message( ssl_context *ssl,
2338 unsigned char level,
2339 unsigned char message )
2340{
2341 int ret;
2342
2343 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2344
2345 ssl->out_msgtype = SSL_MSG_ALERT;
2346 ssl->out_msglen = 2;
2347 ssl->out_msg[0] = level;
2348 ssl->out_msg[1] = message;
2349
2350 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2351 {
2352 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2353 return( ret );
2354 }
2355
2356 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2357
2358 return( 0 );
2359}
2360
Paul Bakker5121ce52009-01-03 21:22:43 +00002361/*
2362 * Handshake functions
2363 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002364#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2365 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2366 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2367 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2368 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2369 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2370 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002371int ssl_write_certificate( ssl_context *ssl )
2372{
Paul Bakkered27a042013-04-18 22:46:23 +02002373 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002374 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
2376 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2377
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002378 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002379 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2380 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002381 {
2382 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2383 ssl->state++;
2384 return( 0 );
2385 }
2386
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002387 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002388 return( ret );
2389}
2390
2391int ssl_parse_certificate( ssl_context *ssl )
2392{
2393 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2394 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2395
2396 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2397
2398 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002399 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2400 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002401 {
2402 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2403 ssl->state++;
2404 return( 0 );
2405 }
2406
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002407 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002408 return( ret );
2409}
2410#else
2411int ssl_write_certificate( ssl_context *ssl )
2412{
2413 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2414 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002415 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002416 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2417
2418 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2419
2420 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002421 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2422 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002423 {
2424 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2425 ssl->state++;
2426 return( 0 );
2427 }
2428
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 if( ssl->endpoint == SSL_IS_CLIENT )
2430 {
2431 if( ssl->client_auth == 0 )
2432 {
2433 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2434 ssl->state++;
2435 return( 0 );
2436 }
2437
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002438#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 /*
2440 * If using SSLv3 and got no cert, send an Alert message
2441 * (otherwise an empty Certificate message will be sent).
2442 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002443 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2445 {
2446 ssl->out_msglen = 2;
2447 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002448 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2449 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
2451 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2452 goto write_msg;
2453 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002454#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 }
2456 else /* SSL_IS_SERVER */
2457 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002458 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 {
2460 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002461 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 }
2463 }
2464
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002465 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002466
2467 /*
2468 * 0 . 0 handshake type
2469 * 1 . 3 handshake length
2470 * 4 . 6 length of all certs
2471 * 7 . 9 length of cert. 1
2472 * 10 . n-1 peer certificate
2473 * n . n+2 length of cert. 2
2474 * n+3 . ... upper level cert, etc.
2475 */
2476 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002477 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Paul Bakker29087132010-03-21 21:03:34 +00002479 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 {
2481 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002482 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 {
2484 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2485 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002486 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 }
2488
2489 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2490 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2491 ssl->out_msg[i + 2] = (unsigned char)( n );
2492
2493 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2494 i += n; crt = crt->next;
2495 }
2496
2497 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2498 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2499 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2500
2501 ssl->out_msglen = i;
2502 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2503 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2504
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002505#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002506write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
2509 ssl->state++;
2510
2511 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2512 {
2513 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2514 return( ret );
2515 }
2516
2517 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2518
Paul Bakkered27a042013-04-18 22:46:23 +02002519 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520}
2521
2522int ssl_parse_certificate( ssl_context *ssl )
2523{
Paul Bakkered27a042013-04-18 22:46:23 +02002524 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002525 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002526 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2529
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002530 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002531 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2532 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002533 {
2534 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2535 ssl->state++;
2536 return( 0 );
2537 }
2538
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002540 ( ssl->authmode == SSL_VERIFY_NONE ||
2541 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002543 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2545 ssl->state++;
2546 return( 0 );
2547 }
2548
2549 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2550 {
2551 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2552 return( ret );
2553 }
2554
2555 ssl->state++;
2556
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002557#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 /*
2559 * Check if the client sent an empty certificate
2560 */
2561 if( ssl->endpoint == SSL_IS_SERVER &&
2562 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2563 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002564 if( ssl->in_msglen == 2 &&
2565 ssl->in_msgtype == SSL_MSG_ALERT &&
2566 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2567 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 {
2569 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2570
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002571 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2573 return( 0 );
2574 else
Paul Bakker40e46942009-01-03 21:51:57 +00002575 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 }
2577 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002578#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002580#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2581 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 if( ssl->endpoint == SSL_IS_SERVER &&
2583 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2584 {
2585 if( ssl->in_hslen == 7 &&
2586 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2587 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2588 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2589 {
2590 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2591
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002592 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002594 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 else
2596 return( 0 );
2597 }
2598 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002599#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2600 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
2602 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2603 {
2604 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002605 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 }
2607
2608 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2609 {
2610 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002611 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 }
2613
2614 /*
2615 * Same message structure as in ssl_write_certificate()
2616 */
2617 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2618
2619 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2620 {
2621 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002622 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 }
2624
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002625 /* In case we tried to reuse a session but it failed */
2626 if( ssl->session_negotiate->peer_cert != NULL )
2627 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002628 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002629 polarssl_free( ssl->session_negotiate->peer_cert );
2630 }
2631
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002632 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2633 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 {
2635 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002636 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002637 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 }
2639
Paul Bakkerb6b09562013-09-18 14:17:41 +02002640 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
2642 i = 7;
2643
2644 while( i < ssl->in_hslen )
2645 {
2646 if( ssl->in_msg[i] != 0 )
2647 {
2648 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002649 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 }
2651
2652 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2653 | (unsigned int) ssl->in_msg[i + 2];
2654 i += 3;
2655
2656 if( n < 128 || i + n > ssl->in_hslen )
2657 {
2658 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002659 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 }
2661
Paul Bakkerddf26b42013-09-18 13:46:23 +02002662 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2663 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 if( ret != 0 )
2665 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002666 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 return( ret );
2668 }
2669
2670 i += n;
2671 }
2672
Paul Bakker48916f92012-09-16 19:57:18 +00002673 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002675 /*
2676 * On client, make sure the server cert doesn't change during renego to
2677 * avoid "triple handshake" attack: https://secure-resumption.com/
2678 */
2679 if( ssl->endpoint == SSL_IS_CLIENT &&
2680 ssl->renegotiation == SSL_RENEGOTIATION )
2681 {
2682 if( ssl->session->peer_cert == NULL )
2683 {
2684 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2685 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2686 }
2687
2688 if( ssl->session->peer_cert->raw.len !=
2689 ssl->session_negotiate->peer_cert->raw.len ||
2690 memcmp( ssl->session->peer_cert->raw.p,
2691 ssl->session_negotiate->peer_cert->raw.p,
2692 ssl->session->peer_cert->raw.len ) != 0 )
2693 {
2694 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2695 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2696 }
2697 }
2698
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 if( ssl->authmode != SSL_VERIFY_NONE )
2700 {
2701 if( ssl->ca_chain == NULL )
2702 {
2703 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002704 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 }
2706
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002707 /*
2708 * Main check: verify certificate
2709 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002710 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2711 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2712 &ssl->session_negotiate->verify_result,
2713 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
2715 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002716 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002718 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002719
2720 /*
2721 * Secondary checks: always done, but change 'ret' only if it was 0
2722 */
2723
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002724#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002725 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002726 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002727
2728 /* If certificate uses an EC key, make sure the curve is OK */
2729 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2730 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2731 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002732 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002733 if( ret == 0 )
2734 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002735 }
2736 }
2737#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002739 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2740 ciphersuite_info,
2741 ! ssl->endpoint ) != 0 )
2742 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002743 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002744 if( ret == 0 )
2745 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2746 }
2747
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2749 ret = 0;
2750 }
2751
2752 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2753
2754 return( ret );
2755}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002756#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2757 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2758 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2759 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2760 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2761 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2762 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
2764int ssl_write_change_cipher_spec( ssl_context *ssl )
2765{
2766 int ret;
2767
2768 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2769
2770 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2771 ssl->out_msglen = 1;
2772 ssl->out_msg[0] = 1;
2773
Paul Bakker5121ce52009-01-03 21:22:43 +00002774 ssl->state++;
2775
2776 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2777 {
2778 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2779 return( ret );
2780 }
2781
2782 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2783
2784 return( 0 );
2785}
2786
2787int ssl_parse_change_cipher_spec( ssl_context *ssl )
2788{
2789 int ret;
2790
2791 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2792
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2794 {
2795 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2796 return( ret );
2797 }
2798
2799 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2800 {
2801 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002802 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 }
2804
2805 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2806 {
2807 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002808 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 }
2810
2811 ssl->state++;
2812
2813 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2814
2815 return( 0 );
2816}
2817
Paul Bakker41c83d32013-03-20 14:39:14 +01002818void ssl_optimize_checksum( ssl_context *ssl,
2819 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002820{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002821 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002822
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002823#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2824 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002825 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002826 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002827 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002828#endif
2829#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2830#if defined(POLARSSL_SHA512_C)
2831 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2832 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2833 else
2834#endif
2835#if defined(POLARSSL_SHA256_C)
2836 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002837 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002838 else
2839#endif
2840#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2841 /* Should never happen */
2842 return;
Paul Bakker380da532012-04-18 16:10:25 +00002843}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002844
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002845static void ssl_update_checksum_start( ssl_context *ssl,
2846 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002847{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002848#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2849 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002850 md5_update( &ssl->handshake->fin_md5 , buf, len );
2851 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002852#endif
2853#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2854#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002855 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002856#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002857#if defined(POLARSSL_SHA512_C)
2858 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002859#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002860#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002861}
2862
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002863#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2864 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002865static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2866 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002867{
Paul Bakker48916f92012-09-16 19:57:18 +00002868 md5_update( &ssl->handshake->fin_md5 , buf, len );
2869 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002870}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002871#endif
Paul Bakker380da532012-04-18 16:10:25 +00002872
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002873#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2874#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002875static void ssl_update_checksum_sha256( ssl_context *ssl,
2876 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002877{
Paul Bakker9e36f042013-06-30 14:34:05 +02002878 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002879}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002880#endif
Paul Bakker380da532012-04-18 16:10:25 +00002881
Paul Bakker9e36f042013-06-30 14:34:05 +02002882#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002883static void ssl_update_checksum_sha384( ssl_context *ssl,
2884 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002885{
Paul Bakker9e36f042013-06-30 14:34:05 +02002886 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002887}
Paul Bakker769075d2012-11-24 11:26:46 +01002888#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002889#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002890
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002891#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002892static void ssl_calc_finished_ssl(
2893 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002894{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002895 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 md5_context md5;
2897 sha1_context sha1;
2898
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 unsigned char padbuf[48];
2900 unsigned char md5sum[16];
2901 unsigned char sha1sum[20];
2902
Paul Bakker48916f92012-09-16 19:57:18 +00002903 ssl_session *session = ssl->session_negotiate;
2904 if( !session )
2905 session = ssl->session;
2906
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2908
Paul Bakker48916f92012-09-16 19:57:18 +00002909 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2910 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
2912 /*
2913 * SSLv3:
2914 * hash =
2915 * MD5( master + pad2 +
2916 * MD5( handshake + sender + master + pad1 ) )
2917 * + SHA1( master + pad2 +
2918 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 */
2920
Paul Bakker90995b52013-06-24 19:20:35 +02002921#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002924#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Paul Bakker90995b52013-06-24 19:20:35 +02002926#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002928 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002929#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Paul Bakker3c2122f2013-06-24 19:03:14 +02002931 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2932 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Paul Bakker1ef83d62012-04-11 12:09:53 +00002934 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Paul Bakker3c2122f2013-06-24 19:03:14 +02002936 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002937 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002938 md5_update( &md5, padbuf, 48 );
2939 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Paul Bakker3c2122f2013-06-24 19:03:14 +02002941 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002942 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943 sha1_update( &sha1, padbuf, 40 );
2944 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
Paul Bakker1ef83d62012-04-11 12:09:53 +00002946 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Paul Bakker1ef83d62012-04-11 12:09:53 +00002948 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002949 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002950 md5_update( &md5, padbuf, 48 );
2951 md5_update( &md5, md5sum, 16 );
2952 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002955 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956 sha1_update( &sha1, padbuf , 40 );
2957 sha1_update( &sha1, sha1sum, 20 );
2958 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962 memset( &md5, 0, sizeof( md5_context ) );
2963 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
2965 memset( padbuf, 0, sizeof( padbuf ) );
2966 memset( md5sum, 0, sizeof( md5sum ) );
2967 memset( sha1sum, 0, sizeof( sha1sum ) );
2968
2969 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2970}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002971#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002973#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974static void ssl_calc_finished_tls(
2975 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002976{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002977 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002978 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002981 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Paul Bakker48916f92012-09-16 19:57:18 +00002983 ssl_session *session = ssl->session_negotiate;
2984 if( !session )
2985 session = ssl->session;
2986
Paul Bakker1ef83d62012-04-11 12:09:53 +00002987 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Paul Bakker48916f92012-09-16 19:57:18 +00002989 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2990 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002991
Paul Bakker1ef83d62012-04-11 12:09:53 +00002992 /*
2993 * TLSv1:
2994 * hash = PRF( master, finished_label,
2995 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2996 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Paul Bakker90995b52013-06-24 19:20:35 +02002998#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002999 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3000 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003001#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003002
Paul Bakker90995b52013-06-24 19:20:35 +02003003#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003004 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3005 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003006#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003007
3008 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003009 ? "client finished"
3010 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003011
3012 md5_finish( &md5, padbuf );
3013 sha1_finish( &sha1, padbuf + 16 );
3014
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003015 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003016 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017
3018 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3019
3020 memset( &md5, 0, sizeof( md5_context ) );
3021 memset( &sha1, 0, sizeof( sha1_context ) );
3022
3023 memset( padbuf, 0, sizeof( padbuf ) );
3024
3025 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3026}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003027#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003028
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003029#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3030#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003031static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 ssl_context *ssl, unsigned char *buf, int from )
3033{
3034 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003035 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003036 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037 unsigned char padbuf[32];
3038
Paul Bakker48916f92012-09-16 19:57:18 +00003039 ssl_session *session = ssl->session_negotiate;
3040 if( !session )
3041 session = ssl->session;
3042
Paul Bakker380da532012-04-18 16:10:25 +00003043 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003044
Paul Bakker9e36f042013-06-30 14:34:05 +02003045 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003046
3047 /*
3048 * TLSv1.2:
3049 * hash = PRF( master, finished_label,
3050 * Hash( handshake ) )[0.11]
3051 */
3052
Paul Bakker9e36f042013-06-30 14:34:05 +02003053#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003054 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003055 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003056#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003057
3058 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003059 ? "client finished"
3060 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003061
Paul Bakker9e36f042013-06-30 14:34:05 +02003062 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003064 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003065 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003066
3067 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3068
Paul Bakker9e36f042013-06-30 14:34:05 +02003069 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003070
3071 memset( padbuf, 0, sizeof( padbuf ) );
3072
3073 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3074}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003075#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076
Paul Bakker9e36f042013-06-30 14:34:05 +02003077#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003078static void ssl_calc_finished_tls_sha384(
3079 ssl_context *ssl, unsigned char *buf, int from )
3080{
3081 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003082 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003083 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003084 unsigned char padbuf[48];
3085
Paul Bakker48916f92012-09-16 19:57:18 +00003086 ssl_session *session = ssl->session_negotiate;
3087 if( !session )
3088 session = ssl->session;
3089
Paul Bakker380da532012-04-18 16:10:25 +00003090 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003091
Paul Bakker9e36f042013-06-30 14:34:05 +02003092 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003093
3094 /*
3095 * TLSv1.2:
3096 * hash = PRF( master, finished_label,
3097 * Hash( handshake ) )[0.11]
3098 */
3099
Paul Bakker9e36f042013-06-30 14:34:05 +02003100#if !defined(POLARSSL_SHA512_ALT)
3101 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3102 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003103#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003104
3105 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003106 ? "client finished"
3107 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003108
Paul Bakker9e36f042013-06-30 14:34:05 +02003109 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003110
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003111 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003112 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003113
3114 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3115
Paul Bakker9e36f042013-06-30 14:34:05 +02003116 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003117
3118 memset( padbuf, 0, sizeof( padbuf ) );
3119
3120 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3121}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003122#endif /* POLARSSL_SHA512_C */
3123#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003124
Paul Bakker48916f92012-09-16 19:57:18 +00003125void ssl_handshake_wrapup( ssl_context *ssl )
3126{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003127 int resume = ssl->handshake->resume;
3128
Paul Bakker48916f92012-09-16 19:57:18 +00003129 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3130
3131 /*
3132 * Free our handshake params
3133 */
3134 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003135 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003136 ssl->handshake = NULL;
3137
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003138 if( ssl->renegotiation == SSL_RENEGOTIATION )
3139 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3140
Paul Bakker48916f92012-09-16 19:57:18 +00003141 /*
3142 * Switch in our now active transform context
3143 */
3144 if( ssl->transform )
3145 {
3146 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003147 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003148 }
3149 ssl->transform = ssl->transform_negotiate;
3150 ssl->transform_negotiate = NULL;
3151
Paul Bakker0a597072012-09-25 21:55:46 +00003152 if( ssl->session )
3153 {
3154 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003155 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003156 }
3157 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003158 ssl->session_negotiate = NULL;
3159
Paul Bakker0a597072012-09-25 21:55:46 +00003160 /*
3161 * Add cache entry
3162 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003163 if( ssl->f_set_cache != NULL &&
3164 ssl->session->length != 0 &&
3165 resume == 0 )
3166 {
Paul Bakker0a597072012-09-25 21:55:46 +00003167 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3168 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003169 }
Paul Bakker0a597072012-09-25 21:55:46 +00003170
Paul Bakker48916f92012-09-16 19:57:18 +00003171 ssl->state++;
3172
3173 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3174}
3175
Paul Bakker1ef83d62012-04-11 12:09:53 +00003176int ssl_write_finished( ssl_context *ssl )
3177{
3178 int ret, hash_len;
3179
3180 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3181
Paul Bakker92be97b2013-01-02 17:30:03 +01003182 /*
3183 * Set the out_msg pointer to the correct location based on IV length
3184 */
3185 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3186 {
3187 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3188 ssl->transform_negotiate->fixed_ivlen;
3189 }
3190 else
3191 ssl->out_msg = ssl->out_iv;
3192
Paul Bakker48916f92012-09-16 19:57:18 +00003193 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003194
3195 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3197
Paul Bakker48916f92012-09-16 19:57:18 +00003198 ssl->verify_data_len = hash_len;
3199 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3200
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 ssl->out_msglen = 4 + hash_len;
3202 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3203 ssl->out_msg[0] = SSL_HS_FINISHED;
3204
3205 /*
3206 * In case of session resuming, invert the client and server
3207 * ChangeCipherSpec messages order.
3208 */
Paul Bakker0a597072012-09-25 21:55:46 +00003209 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 {
3211 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003212 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 else
3214 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3215 }
3216 else
3217 ssl->state++;
3218
Paul Bakker48916f92012-09-16 19:57:18 +00003219 /*
3220 * Switch to our negotiated transform and session parameters for outbound data.
3221 */
3222 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3223 ssl->transform_out = ssl->transform_negotiate;
3224 ssl->session_out = ssl->session_negotiate;
3225 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003226
Paul Bakker07eb38b2012-12-19 14:42:06 +01003227#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3228 if( ssl_hw_record_activate != NULL)
3229 {
3230 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3231 {
3232 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3233 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3234 }
3235 }
3236#endif
3237
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3239 {
3240 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3241 return( ret );
3242 }
3243
3244 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3245
3246 return( 0 );
3247}
3248
3249int ssl_parse_finished( ssl_context *ssl )
3250{
Paul Bakker23986e52011-04-24 08:57:21 +00003251 int ret;
3252 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 unsigned char buf[36];
3254
3255 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3256
Paul Bakker48916f92012-09-16 19:57:18 +00003257 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
Paul Bakker48916f92012-09-16 19:57:18 +00003259 /*
3260 * Switch to our negotiated transform and session parameters for inbound data.
3261 */
3262 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3263 ssl->transform_in = ssl->transform_negotiate;
3264 ssl->session_in = ssl->session_negotiate;
3265 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003266
Paul Bakker92be97b2013-01-02 17:30:03 +01003267 /*
3268 * Set the in_msg pointer to the correct location based on IV length
3269 */
3270 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3271 {
3272 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3273 ssl->transform_negotiate->fixed_ivlen;
3274 }
3275 else
3276 ssl->in_msg = ssl->in_iv;
3277
Paul Bakker07eb38b2012-12-19 14:42:06 +01003278#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3279 if( ssl_hw_record_activate != NULL)
3280 {
3281 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3282 {
3283 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3284 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3285 }
3286 }
3287#endif
3288
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3290 {
3291 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3292 return( ret );
3293 }
3294
3295 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3296 {
3297 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003298 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 }
3300
Paul Bakker1ef83d62012-04-11 12:09:53 +00003301 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3303
3304 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3305 ssl->in_hslen != 4 + hash_len )
3306 {
3307 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003308 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 }
3310
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003311 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 {
3313 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003314 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003315 }
3316
Paul Bakker48916f92012-09-16 19:57:18 +00003317 ssl->verify_data_len = hash_len;
3318 memcpy( ssl->peer_verify_data, buf, hash_len );
3319
Paul Bakker0a597072012-09-25 21:55:46 +00003320 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003321 {
3322 if( ssl->endpoint == SSL_IS_CLIENT )
3323 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3324
3325 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003326 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003327 }
3328 else
3329 ssl->state++;
3330
3331 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3332
3333 return( 0 );
3334}
3335
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003336static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003337{
3338 if( ssl->transform_negotiate )
3339 ssl_transform_free( ssl->transform_negotiate );
3340 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003341 {
3342 ssl->transform_negotiate =
3343 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003344
3345 if( ssl->transform_negotiate != NULL )
3346 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003347 }
Paul Bakker48916f92012-09-16 19:57:18 +00003348
3349 if( ssl->session_negotiate )
3350 ssl_session_free( ssl->session_negotiate );
3351 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003352 {
3353 ssl->session_negotiate =
3354 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003355
3356 if( ssl->session_negotiate != NULL )
3357 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003358 }
Paul Bakker48916f92012-09-16 19:57:18 +00003359
3360 if( ssl->handshake )
3361 ssl_handshake_free( ssl->handshake );
3362 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003363 {
3364 ssl->handshake = (ssl_handshake_params *)
3365 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003366
3367 if( ssl->handshake != NULL )
3368 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003369 }
Paul Bakker48916f92012-09-16 19:57:18 +00003370
3371 if( ssl->handshake == NULL ||
3372 ssl->transform_negotiate == NULL ||
3373 ssl->session_negotiate == NULL )
3374 {
3375 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3376 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3377 }
3378
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003379#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3380 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003381 md5_starts( &ssl->handshake->fin_md5 );
3382 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003383#endif
3384#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3385#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003386 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003387#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003388#if defined(POLARSSL_SHA512_C)
3389 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003390#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003391#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003392
3393 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003394 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003395
Paul Bakker61d113b2013-07-04 11:51:43 +02003396#if defined(POLARSSL_ECDH_C)
3397 ecdh_init( &ssl->handshake->ecdh_ctx );
3398#endif
3399
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003400#if defined(POLARSSL_X509_CRT_PARSE_C)
3401 ssl->handshake->key_cert = ssl->key_cert;
3402#endif
3403
Paul Bakker48916f92012-09-16 19:57:18 +00003404 return( 0 );
3405}
3406
Paul Bakker5121ce52009-01-03 21:22:43 +00003407/*
3408 * Initialize an SSL context
3409 */
3410int ssl_init( ssl_context *ssl )
3411{
Paul Bakker48916f92012-09-16 19:57:18 +00003412 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003413 int len = SSL_BUFFER_LEN;
3414
3415 memset( ssl, 0, sizeof( ssl_context ) );
3416
Paul Bakker62f2dee2012-09-28 07:31:51 +00003417 /*
3418 * Sane defaults
3419 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003420 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3421 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3422 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3423 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003424
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003425 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003426
Paul Bakker62f2dee2012-09-28 07:31:51 +00003427#if defined(POLARSSL_DHM_C)
3428 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3429 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3430 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3431 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3432 {
3433 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3434 return( ret );
3435 }
3436#endif
3437
3438 /*
3439 * Prepare base structures
3440 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003441 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003443 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 ssl->in_msg = ssl->in_ctr + 13;
3445
3446 if( ssl->in_ctr == NULL )
3447 {
3448 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003449 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 }
3451
Paul Bakker6e339b52013-07-03 13:37:05 +02003452 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003454 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003455 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003456
3457 if( ssl->out_ctr == NULL )
3458 {
3459 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003460 polarssl_free( ssl->in_ctr );
3461 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003462 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 }
3464
3465 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3466 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3467
Paul Bakker606b4ba2013-08-14 16:52:14 +02003468#if defined(POLARSSL_SSL_SESSION_TICKETS)
3469 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3470#endif
3471
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003472#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003473 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003474#endif
3475
Paul Bakker48916f92012-09-16 19:57:18 +00003476 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3477 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003478
3479 return( 0 );
3480}
3481
3482/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003483 * Reset an initialized and used SSL context for re-use while retaining
3484 * all application-set variables, function pointers and data.
3485 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003486int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003487{
Paul Bakker48916f92012-09-16 19:57:18 +00003488 int ret;
3489
Paul Bakker7eb013f2011-10-06 12:37:39 +00003490 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003491 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3492 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3493
3494 ssl->verify_data_len = 0;
3495 memset( ssl->own_verify_data, 0, 36 );
3496 memset( ssl->peer_verify_data, 0, 36 );
3497
Paul Bakker7eb013f2011-10-06 12:37:39 +00003498 ssl->in_offt = NULL;
3499
Paul Bakker92be97b2013-01-02 17:30:03 +01003500 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003501 ssl->in_msgtype = 0;
3502 ssl->in_msglen = 0;
3503 ssl->in_left = 0;
3504
3505 ssl->in_hslen = 0;
3506 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003507 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003508
Paul Bakker92be97b2013-01-02 17:30:03 +01003509 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003510 ssl->out_msgtype = 0;
3511 ssl->out_msglen = 0;
3512 ssl->out_left = 0;
3513
Paul Bakker48916f92012-09-16 19:57:18 +00003514 ssl->transform_in = NULL;
3515 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003516
3517 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3518 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003519
3520#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3521 if( ssl_hw_record_reset != NULL)
3522 {
3523 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003524 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003525 {
3526 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3527 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3528 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003529 }
3530#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003531
Paul Bakker48916f92012-09-16 19:57:18 +00003532 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003533 {
Paul Bakker48916f92012-09-16 19:57:18 +00003534 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003535 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003536 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003537 }
Paul Bakker48916f92012-09-16 19:57:18 +00003538
Paul Bakkerc0463502013-02-14 11:19:38 +01003539 if( ssl->session )
3540 {
3541 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003542 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003543 ssl->session = NULL;
3544 }
3545
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003546#if defined(POLARSSL_SSL_ALPN)
3547 ssl->alpn_chosen = NULL;
3548#endif
3549
Paul Bakker48916f92012-09-16 19:57:18 +00003550 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3551 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003552
3553 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003554}
3555
Paul Bakkera503a632013-08-14 13:48:06 +02003556#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003557/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003558 * Allocate and initialize ticket keys
3559 */
3560static int ssl_ticket_keys_init( ssl_context *ssl )
3561{
3562 int ret;
3563 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003564 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003565
3566 if( ssl->ticket_keys != NULL )
3567 return( 0 );
3568
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003569 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3570 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003571 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3572
3573 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003574 {
3575 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003576 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003577 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003578
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003579 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3580 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3581 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3582 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003583 polarssl_free( tkeys );
3584 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003585 }
3586
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003587 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003588 {
3589 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003590 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003591 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003592
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003593 ssl->ticket_keys = tkeys;
3594
3595 return( 0 );
3596}
Paul Bakkera503a632013-08-14 13:48:06 +02003597#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003598
3599/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 * SSL set accessors
3601 */
3602void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3603{
3604 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003605
Paul Bakker606b4ba2013-08-14 16:52:14 +02003606#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003607 if( endpoint == SSL_IS_CLIENT )
3608 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003609#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003610}
3611
3612void ssl_set_authmode( ssl_context *ssl, int authmode )
3613{
3614 ssl->authmode = authmode;
3615}
3616
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003617#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003618void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003619 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003620 void *p_vrfy )
3621{
3622 ssl->f_vrfy = f_vrfy;
3623 ssl->p_vrfy = p_vrfy;
3624}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003625#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003626
Paul Bakker5121ce52009-01-03 21:22:43 +00003627void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003628 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003629 void *p_rng )
3630{
3631 ssl->f_rng = f_rng;
3632 ssl->p_rng = p_rng;
3633}
3634
3635void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003636 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 void *p_dbg )
3638{
3639 ssl->f_dbg = f_dbg;
3640 ssl->p_dbg = p_dbg;
3641}
3642
3643void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003644 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003645 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003646{
3647 ssl->f_recv = f_recv;
3648 ssl->f_send = f_send;
3649 ssl->p_recv = p_recv;
3650 ssl->p_send = p_send;
3651}
3652
Paul Bakker0a597072012-09-25 21:55:46 +00003653void ssl_set_session_cache( ssl_context *ssl,
3654 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3655 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003656{
Paul Bakker0a597072012-09-25 21:55:46 +00003657 ssl->f_get_cache = f_get_cache;
3658 ssl->p_get_cache = p_get_cache;
3659 ssl->f_set_cache = f_set_cache;
3660 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003661}
3662
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003663int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003664{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003665 int ret;
3666
3667 if( ssl == NULL ||
3668 session == NULL ||
3669 ssl->session_negotiate == NULL ||
3670 ssl->endpoint != SSL_IS_CLIENT )
3671 {
3672 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3673 }
3674
3675 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3676 return( ret );
3677
Paul Bakker0a597072012-09-25 21:55:46 +00003678 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003679
3680 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003681}
3682
Paul Bakkerb68cad62012-08-23 08:34:18 +00003683void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003684{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003685 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3686 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3687 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3688 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3689}
3690
3691void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3692 int major, int minor )
3693{
3694 if( major != SSL_MAJOR_VERSION_3 )
3695 return;
3696
3697 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3698 return;
3699
3700 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003701}
3702
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003703#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003704/* Add a new (empty) key_cert entry an return a pointer to it */
3705static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3706{
3707 ssl_key_cert *key_cert, *last;
3708
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003709 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3710 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003711 return( NULL );
3712
3713 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3714
3715 /* Append the new key_cert to the (possibly empty) current list */
3716 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003717 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003718 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003719 if( ssl->handshake != NULL )
3720 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003721 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003722 else
3723 {
3724 last = ssl->key_cert;
3725 while( last->next != NULL )
3726 last = last->next;
3727 last->next = key_cert;
3728 }
3729
3730 return key_cert;
3731}
3732
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003733void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003734 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003735{
3736 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003737 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 ssl->peer_cn = peer_cn;
3739}
3740
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003741int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003742 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003743{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003744 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3745
3746 if( key_cert == NULL )
3747 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3748
3749 key_cert->cert = own_cert;
3750 key_cert->key = pk_key;
3751
3752 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003753}
3754
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003755#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003756int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003757 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003758{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003759 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003760 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003761
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003762 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003763 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3764
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003765 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3766 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003767 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003768
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003769 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003770
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003771 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003772 if( ret != 0 )
3773 return( ret );
3774
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003775 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003776 return( ret );
3777
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003778 key_cert->cert = own_cert;
3779 key_cert->key_own_alloc = 1;
3780
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003781 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003782}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003783#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003784
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003785int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003786 void *rsa_key,
3787 rsa_decrypt_func rsa_decrypt,
3788 rsa_sign_func rsa_sign,
3789 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003790{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003791 int ret;
3792 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003793
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003794 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003795 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3796
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003797 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3798 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003799 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003800
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003801 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003802
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003803 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3804 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3805 return( ret );
3806
3807 key_cert->cert = own_cert;
3808 key_cert->key_own_alloc = 1;
3809
3810 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003811}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003812#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003813
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003814#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003815int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3816 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003817{
Paul Bakker6db455e2013-09-18 17:29:31 +02003818 if( psk == NULL || psk_identity == NULL )
3819 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3820
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003821 /*
3822 * The length will be check later anyway, but in case it is obviously
3823 * too large, better abort now. The PMS is as follows:
3824 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3825 */
3826 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3827 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3828
Paul Bakker6db455e2013-09-18 17:29:31 +02003829 if( ssl->psk != NULL )
3830 {
3831 polarssl_free( ssl->psk );
3832 polarssl_free( ssl->psk_identity );
3833 }
3834
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003835 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003836 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003837
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003838 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3839 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003840
3841 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3842 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3843
3844 memcpy( ssl->psk, psk, ssl->psk_len );
3845 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003846
3847 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003848}
3849
3850void ssl_set_psk_cb( ssl_context *ssl,
3851 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3852 size_t),
3853 void *p_psk )
3854{
3855 ssl->f_psk = f_psk;
3856 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003857}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003858#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003859
Paul Bakker48916f92012-09-16 19:57:18 +00003860#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003861int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003862{
3863 int ret;
3864
Paul Bakker48916f92012-09-16 19:57:18 +00003865 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 {
3867 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3868 return( ret );
3869 }
3870
Paul Bakker48916f92012-09-16 19:57:18 +00003871 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 {
3873 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3874 return( ret );
3875 }
3876
3877 return( 0 );
3878}
3879
Paul Bakker1b57b062011-01-06 15:48:19 +00003880int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3881{
3882 int ret;
3883
Paul Bakker48916f92012-09-16 19:57:18 +00003884 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003885 {
3886 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3887 return( ret );
3888 }
3889
Paul Bakker48916f92012-09-16 19:57:18 +00003890 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003891 {
3892 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3893 return( ret );
3894 }
3895
3896 return( 0 );
3897}
Paul Bakker48916f92012-09-16 19:57:18 +00003898#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003899
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003900#if defined(POLARSSL_SSL_SET_CURVES)
3901/*
3902 * Set the allowed elliptic curves
3903 */
3904void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3905{
3906 ssl->curve_list = curve_list;
3907}
3908#endif
3909
Paul Bakker0be444a2013-08-27 21:55:01 +02003910#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003911int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003912{
3913 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003914 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003915
3916 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003917
3918 if( ssl->hostname_len + 1 == 0 )
3919 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3920
Paul Bakker6e339b52013-07-03 13:37:05 +02003921 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003922
Paul Bakkerb15b8512012-01-13 13:44:06 +00003923 if( ssl->hostname == NULL )
3924 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3925
Paul Bakker3c2122f2013-06-24 19:03:14 +02003926 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003928
Paul Bakker40ea7de2009-05-03 10:18:48 +00003929 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003930
3931 return( 0 );
3932}
3933
Paul Bakker5701cdc2012-09-27 21:49:42 +00003934void ssl_set_sni( ssl_context *ssl,
3935 int (*f_sni)(void *, ssl_context *,
3936 const unsigned char *, size_t),
3937 void *p_sni )
3938{
3939 ssl->f_sni = f_sni;
3940 ssl->p_sni = p_sni;
3941}
Paul Bakker0be444a2013-08-27 21:55:01 +02003942#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003943
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003944#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003945int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003946{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003947 size_t cur_len, tot_len;
3948 const char **p;
3949
3950 /*
3951 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3952 * truncated". Check lengths now rather than later.
3953 */
3954 tot_len = 0;
3955 for( p = protos; *p != NULL; p++ )
3956 {
3957 cur_len = strlen( *p );
3958 tot_len += cur_len;
3959
3960 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3961 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3962 }
3963
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003964 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003965
3966 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003967}
3968
3969const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3970{
3971 return ssl->alpn_chosen;
3972}
3973#endif /* POLARSSL_SSL_ALPN */
3974
Paul Bakker490ecc82011-10-06 13:04:09 +00003975void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3976{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003977 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3978 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3979 {
3980 ssl->max_major_ver = major;
3981 ssl->max_minor_ver = minor;
3982 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003983}
3984
Paul Bakker1d29fb52012-09-28 13:28:45 +00003985void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3986{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003987 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3988 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3989 {
3990 ssl->min_major_ver = major;
3991 ssl->min_minor_ver = minor;
3992 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003993}
3994
Paul Bakker05decb22013-08-15 13:33:48 +02003995#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003996int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3997{
Paul Bakker77e257e2013-12-16 15:29:52 +01003998 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003999 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004000 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004001 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004002 }
4003
4004 ssl->mfl_code = mfl_code;
4005
4006 return( 0 );
4007}
Paul Bakker05decb22013-08-15 13:33:48 +02004008#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004009
Paul Bakker1f2bc622013-08-15 13:45:55 +02004010#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004011int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004012{
4013 if( ssl->endpoint != SSL_IS_CLIENT )
4014 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4015
Paul Bakker8c1ede62013-07-19 14:14:37 +02004016 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004017
4018 return( 0 );
4019}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004020#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004021
Paul Bakker48916f92012-09-16 19:57:18 +00004022void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4023{
4024 ssl->disable_renegotiation = renegotiation;
4025}
4026
4027void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4028{
4029 ssl->allow_legacy_renegotiation = allow_legacy;
4030}
4031
Paul Bakkera503a632013-08-14 13:48:06 +02004032#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004033int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4034{
4035 ssl->session_tickets = use_tickets;
4036
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004037 if( ssl->endpoint == SSL_IS_CLIENT )
4038 return( 0 );
4039
4040 if( ssl->f_rng == NULL )
4041 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4042
4043 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004044}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004045
4046void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4047{
4048 ssl->ticket_lifetime = lifetime;
4049}
Paul Bakkera503a632013-08-14 13:48:06 +02004050#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004051
Paul Bakker5121ce52009-01-03 21:22:43 +00004052/*
4053 * SSL get accessors
4054 */
Paul Bakker23986e52011-04-24 08:57:21 +00004055size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004056{
4057 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4058}
4059
Paul Bakkerff60ee62010-03-16 21:09:09 +00004060int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004061{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004062 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004063}
4064
Paul Bakkere3166ce2011-01-27 17:40:50 +00004065const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004066{
Paul Bakker926c8e42013-03-06 10:23:34 +01004067 if( ssl == NULL || ssl->session == NULL )
4068 return NULL;
4069
Paul Bakkere3166ce2011-01-27 17:40:50 +00004070 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004071}
4072
Paul Bakker43ca69c2011-01-15 17:35:19 +00004073const char *ssl_get_version( const ssl_context *ssl )
4074{
4075 switch( ssl->minor_ver )
4076 {
4077 case SSL_MINOR_VERSION_0:
4078 return( "SSLv3.0" );
4079
4080 case SSL_MINOR_VERSION_1:
4081 return( "TLSv1.0" );
4082
4083 case SSL_MINOR_VERSION_2:
4084 return( "TLSv1.1" );
4085
Paul Bakker1ef83d62012-04-11 12:09:53 +00004086 case SSL_MINOR_VERSION_3:
4087 return( "TLSv1.2" );
4088
Paul Bakker43ca69c2011-01-15 17:35:19 +00004089 default:
4090 break;
4091 }
4092 return( "unknown" );
4093}
4094
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004095#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004096const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004097{
4098 if( ssl == NULL || ssl->session == NULL )
4099 return NULL;
4100
4101 return ssl->session->peer_cert;
4102}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004103#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004104
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004105int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4106{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004107 if( ssl == NULL ||
4108 dst == NULL ||
4109 ssl->session == NULL ||
4110 ssl->endpoint != SSL_IS_CLIENT )
4111 {
4112 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4113 }
4114
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004115 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004116}
4117
Paul Bakker5121ce52009-01-03 21:22:43 +00004118/*
Paul Bakker1961b702013-01-25 14:49:24 +01004119 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004120 */
Paul Bakker1961b702013-01-25 14:49:24 +01004121int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004122{
Paul Bakker40e46942009-01-03 21:51:57 +00004123 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004124
Paul Bakker40e46942009-01-03 21:51:57 +00004125#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004126 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004127 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004128#endif
4129
Paul Bakker40e46942009-01-03 21:51:57 +00004130#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004131 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004132 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004133#endif
4134
Paul Bakker1961b702013-01-25 14:49:24 +01004135 return( ret );
4136}
4137
4138/*
4139 * Perform the SSL handshake
4140 */
4141int ssl_handshake( ssl_context *ssl )
4142{
4143 int ret = 0;
4144
4145 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4146
4147 while( ssl->state != SSL_HANDSHAKE_OVER )
4148 {
4149 ret = ssl_handshake_step( ssl );
4150
4151 if( ret != 0 )
4152 break;
4153 }
4154
Paul Bakker5121ce52009-01-03 21:22:43 +00004155 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4156
4157 return( ret );
4158}
4159
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004160#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004161/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004162 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004163 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004164static int ssl_write_hello_request( ssl_context *ssl )
4165{
4166 int ret;
4167
4168 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4169
4170 ssl->out_msglen = 4;
4171 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4172 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4173
4174 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4175 {
4176 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4177 return( ret );
4178 }
4179
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004180 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4181
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004182 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4183
4184 return( 0 );
4185}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004186#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004187
4188/*
4189 * Actually renegotiate current connection, triggered by either:
4190 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004191 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004192 * - receiving any handshake message on server during ssl_read() after the
4193 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004194 * If the handshake doesn't complete due to waiting for I/O, it will continue
4195 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004196 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004197static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004198{
4199 int ret;
4200
4201 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4202
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004203 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4204 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004205
4206 ssl->state = SSL_HELLO_REQUEST;
4207 ssl->renegotiation = SSL_RENEGOTIATION;
4208
Paul Bakker48916f92012-09-16 19:57:18 +00004209 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4210 {
4211 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4212 return( ret );
4213 }
4214
4215 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4216
4217 return( 0 );
4218}
4219
4220/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004221 * Renegotiate current connection on client,
4222 * or request renegotiation on server
4223 */
4224int ssl_renegotiate( ssl_context *ssl )
4225{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004226 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004227
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004228#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004229 /* On server, just send the request */
4230 if( ssl->endpoint == SSL_IS_SERVER )
4231 {
4232 if( ssl->state != SSL_HANDSHAKE_OVER )
4233 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4234
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004235 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004236 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004237#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004238
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004239#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004240 /*
4241 * On client, either start the renegotiation process or,
4242 * if already in progress, continue the handshake
4243 */
4244 if( ssl->renegotiation != SSL_RENEGOTIATION )
4245 {
4246 if( ssl->state != SSL_HANDSHAKE_OVER )
4247 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4248
4249 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4250 {
4251 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4252 return( ret );
4253 }
4254 }
4255 else
4256 {
4257 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4258 {
4259 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4260 return( ret );
4261 }
4262 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004263#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004264
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004265 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004266}
4267
4268/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004269 * Receive application data decrypted from the SSL layer
4270 */
Paul Bakker23986e52011-04-24 08:57:21 +00004271int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004272{
Paul Bakker23986e52011-04-24 08:57:21 +00004273 int ret;
4274 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004275
4276 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4277
4278 if( ssl->state != SSL_HANDSHAKE_OVER )
4279 {
4280 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4281 {
4282 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4283 return( ret );
4284 }
4285 }
4286
4287 if( ssl->in_offt == NULL )
4288 {
4289 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4290 {
Paul Bakker831a7552011-05-18 13:32:51 +00004291 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4292 return( 0 );
4293
Paul Bakker5121ce52009-01-03 21:22:43 +00004294 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4295 return( ret );
4296 }
4297
4298 if( ssl->in_msglen == 0 &&
4299 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4300 {
4301 /*
4302 * OpenSSL sends empty messages to randomize the IV
4303 */
4304 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4305 {
Paul Bakker831a7552011-05-18 13:32:51 +00004306 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4307 return( 0 );
4308
Paul Bakker5121ce52009-01-03 21:22:43 +00004309 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4310 return( ret );
4311 }
4312 }
4313
Paul Bakker48916f92012-09-16 19:57:18 +00004314 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4315 {
4316 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4317
4318 if( ssl->endpoint == SSL_IS_CLIENT &&
4319 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4320 ssl->in_hslen != 4 ) )
4321 {
4322 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4323 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4324 }
4325
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004326 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4327 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4328 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004329 {
4330 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4331
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004332#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004333 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004334 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004335 /*
4336 * SSLv3 does not have a "no_renegotiation" alert
4337 */
4338 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4339 return( ret );
4340 }
4341 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004342#endif
4343#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4344 defined(POLARSSL_SSL_PROTO_TLS1_2)
4345 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004346 {
4347 if( ( ret = ssl_send_alert_message( ssl,
4348 SSL_ALERT_LEVEL_WARNING,
4349 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4350 {
4351 return( ret );
4352 }
Paul Bakker48916f92012-09-16 19:57:18 +00004353 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004354 else
4355#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004356 {
4357 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004358 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004359 }
Paul Bakker48916f92012-09-16 19:57:18 +00004360 }
4361 else
4362 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004363 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004364 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004365 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004366 return( ret );
4367 }
4368
4369 return( POLARSSL_ERR_NET_WANT_READ );
4370 }
4371 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004372 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4373 {
4374 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4375 "but not honored by client" ) );
4376 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4377 }
Paul Bakker48916f92012-09-16 19:57:18 +00004378 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004379 {
4380 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004381 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004382 }
4383
4384 ssl->in_offt = ssl->in_msg;
4385 }
4386
4387 n = ( len < ssl->in_msglen )
4388 ? len : ssl->in_msglen;
4389
4390 memcpy( buf, ssl->in_offt, n );
4391 ssl->in_msglen -= n;
4392
4393 if( ssl->in_msglen == 0 )
4394 /* all bytes consumed */
4395 ssl->in_offt = NULL;
4396 else
4397 /* more data available */
4398 ssl->in_offt += n;
4399
4400 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4401
Paul Bakker23986e52011-04-24 08:57:21 +00004402 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004403}
4404
4405/*
4406 * Send application data to be encrypted by the SSL layer
4407 */
Paul Bakker23986e52011-04-24 08:57:21 +00004408int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004409{
Paul Bakker23986e52011-04-24 08:57:21 +00004410 int ret;
4411 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004412 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004413
4414 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4415
4416 if( ssl->state != SSL_HANDSHAKE_OVER )
4417 {
4418 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4419 {
4420 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4421 return( ret );
4422 }
4423 }
4424
Paul Bakker05decb22013-08-15 13:33:48 +02004425#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004426 /*
4427 * Assume mfl_code is correct since it was checked when set
4428 */
4429 max_len = mfl_code_to_length[ssl->mfl_code];
4430
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004431 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004432 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004433 */
4434 if( ssl->session_out != NULL &&
4435 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4436 {
4437 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4438 }
Paul Bakker05decb22013-08-15 13:33:48 +02004439#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004440
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004441 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004442
Paul Bakker5121ce52009-01-03 21:22:43 +00004443 if( ssl->out_left != 0 )
4444 {
4445 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4446 {
4447 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4448 return( ret );
4449 }
4450 }
Paul Bakker887bd502011-06-08 13:10:54 +00004451 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004452 {
Paul Bakker887bd502011-06-08 13:10:54 +00004453 ssl->out_msglen = n;
4454 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4455 memcpy( ssl->out_msg, buf, n );
4456
4457 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4458 {
4459 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4460 return( ret );
4461 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004462 }
4463
4464 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4465
Paul Bakker23986e52011-04-24 08:57:21 +00004466 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004467}
4468
4469/*
4470 * Notify the peer that the connection is being closed
4471 */
4472int ssl_close_notify( ssl_context *ssl )
4473{
4474 int ret;
4475
4476 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4477
4478 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4479 {
4480 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4481 return( ret );
4482 }
4483
4484 if( ssl->state == SSL_HANDSHAKE_OVER )
4485 {
Paul Bakker48916f92012-09-16 19:57:18 +00004486 if( ( ret = ssl_send_alert_message( ssl,
4487 SSL_ALERT_LEVEL_WARNING,
4488 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004489 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 return( ret );
4491 }
4492 }
4493
4494 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4495
4496 return( ret );
4497}
4498
Paul Bakker48916f92012-09-16 19:57:18 +00004499void ssl_transform_free( ssl_transform *transform )
4500{
4501#if defined(POLARSSL_ZLIB_SUPPORT)
4502 deflateEnd( &transform->ctx_deflate );
4503 inflateEnd( &transform->ctx_inflate );
4504#endif
4505
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004506 cipher_free_ctx( &transform->cipher_ctx_enc );
4507 cipher_free_ctx( &transform->cipher_ctx_dec );
4508
Paul Bakker61d113b2013-07-04 11:51:43 +02004509 md_free_ctx( &transform->md_ctx_enc );
4510 md_free_ctx( &transform->md_ctx_dec );
4511
Paul Bakker48916f92012-09-16 19:57:18 +00004512 memset( transform, 0, sizeof( ssl_transform ) );
4513}
4514
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004515#if defined(POLARSSL_X509_CRT_PARSE_C)
4516static void ssl_key_cert_free( ssl_key_cert *key_cert )
4517{
4518 ssl_key_cert *cur = key_cert, *next;
4519
4520 while( cur != NULL )
4521 {
4522 next = cur->next;
4523
4524 if( cur->key_own_alloc )
4525 {
4526 pk_free( cur->key );
4527 polarssl_free( cur->key );
4528 }
4529 polarssl_free( cur );
4530
4531 cur = next;
4532 }
4533}
4534#endif /* POLARSSL_X509_CRT_PARSE_C */
4535
Paul Bakker48916f92012-09-16 19:57:18 +00004536void ssl_handshake_free( ssl_handshake_params *handshake )
4537{
4538#if defined(POLARSSL_DHM_C)
4539 dhm_free( &handshake->dhm_ctx );
4540#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004541#if defined(POLARSSL_ECDH_C)
4542 ecdh_free( &handshake->ecdh_ctx );
4543#endif
4544
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004545#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004546 /* explicit void pointer cast for buggy MS compiler */
4547 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004548#endif
4549
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004550#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4551 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4552 /*
4553 * Free only the linked list wrapper, not the keys themselves
4554 * since the belong to the SNI callback
4555 */
4556 if( handshake->sni_key_cert != NULL )
4557 {
4558 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4559
4560 while( cur != NULL )
4561 {
4562 next = cur->next;
4563 polarssl_free( cur );
4564 cur = next;
4565 }
4566 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004567#endif
4568
Paul Bakker48916f92012-09-16 19:57:18 +00004569 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4570}
4571
4572void ssl_session_free( ssl_session *session )
4573{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004574#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004575 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004576 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004577 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004578 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004579 }
Paul Bakkered27a042013-04-18 22:46:23 +02004580#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004581
Paul Bakkera503a632013-08-14 13:48:06 +02004582#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004583 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004584#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004585
Paul Bakker0a597072012-09-25 21:55:46 +00004586 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004587}
4588
Paul Bakker5121ce52009-01-03 21:22:43 +00004589/*
4590 * Free an SSL context
4591 */
4592void ssl_free( ssl_context *ssl )
4593{
4594 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4595
Paul Bakker5121ce52009-01-03 21:22:43 +00004596 if( ssl->out_ctr != NULL )
4597 {
4598 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004599 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 }
4601
4602 if( ssl->in_ctr != NULL )
4603 {
4604 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004605 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 }
4607
Paul Bakker16770332013-10-11 09:59:44 +02004608#if defined(POLARSSL_ZLIB_SUPPORT)
4609 if( ssl->compress_buf != NULL )
4610 {
4611 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4612 polarssl_free( ssl->compress_buf );
4613 }
4614#endif
4615
Paul Bakker40e46942009-01-03 21:51:57 +00004616#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004617 mpi_free( &ssl->dhm_P );
4618 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004619#endif
4620
Paul Bakker48916f92012-09-16 19:57:18 +00004621 if( ssl->transform )
4622 {
4623 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004624 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004625 }
4626
4627 if( ssl->handshake )
4628 {
4629 ssl_handshake_free( ssl->handshake );
4630 ssl_transform_free( ssl->transform_negotiate );
4631 ssl_session_free( ssl->session_negotiate );
4632
Paul Bakker6e339b52013-07-03 13:37:05 +02004633 polarssl_free( ssl->handshake );
4634 polarssl_free( ssl->transform_negotiate );
4635 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004636 }
4637
Paul Bakkerc0463502013-02-14 11:19:38 +01004638 if( ssl->session )
4639 {
4640 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004641 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004642 }
4643
Paul Bakkera503a632013-08-14 13:48:06 +02004644#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004645 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004646#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004647
Paul Bakker0be444a2013-08-27 21:55:01 +02004648#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004649 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004650 {
4651 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004652 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004653 ssl->hostname_len = 0;
4654 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004655#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004656
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004657#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004658 if( ssl->psk != NULL )
4659 {
4660 memset( ssl->psk, 0, ssl->psk_len );
4661 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4662 polarssl_free( ssl->psk );
4663 polarssl_free( ssl->psk_identity );
4664 ssl->psk_len = 0;
4665 ssl->psk_identity_len = 0;
4666 }
4667#endif
4668
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004669#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004670 ssl_key_cert_free( ssl->key_cert );
4671#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004672
Paul Bakker05ef8352012-05-08 09:17:57 +00004673#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4674 if( ssl_hw_record_finish != NULL )
4675 {
4676 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4677 ssl_hw_record_finish( ssl );
4678 }
4679#endif
4680
Paul Bakker5121ce52009-01-03 21:22:43 +00004681 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004682
Paul Bakker86f04f42013-02-14 11:20:09 +01004683 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004684 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004685}
4686
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004687#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004688/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004689 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004690 */
4691unsigned char ssl_sig_from_pk( pk_context *pk )
4692{
4693#if defined(POLARSSL_RSA_C)
4694 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4695 return( SSL_SIG_RSA );
4696#endif
4697#if defined(POLARSSL_ECDSA_C)
4698 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4699 return( SSL_SIG_ECDSA );
4700#endif
4701 return( SSL_SIG_ANON );
4702}
4703
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004704pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4705{
4706 switch( sig )
4707 {
4708#if defined(POLARSSL_RSA_C)
4709 case SSL_SIG_RSA:
4710 return( POLARSSL_PK_RSA );
4711#endif
4712#if defined(POLARSSL_ECDSA_C)
4713 case SSL_SIG_ECDSA:
4714 return( POLARSSL_PK_ECDSA );
4715#endif
4716 default:
4717 return( POLARSSL_PK_NONE );
4718 }
4719}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004720#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004721
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004722/*
4723 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4724 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004725md_type_t ssl_md_alg_from_hash( unsigned char hash )
4726{
4727 switch( hash )
4728 {
4729#if defined(POLARSSL_MD5_C)
4730 case SSL_HASH_MD5:
4731 return( POLARSSL_MD_MD5 );
4732#endif
4733#if defined(POLARSSL_SHA1_C)
4734 case SSL_HASH_SHA1:
4735 return( POLARSSL_MD_SHA1 );
4736#endif
4737#if defined(POLARSSL_SHA256_C)
4738 case SSL_HASH_SHA224:
4739 return( POLARSSL_MD_SHA224 );
4740 case SSL_HASH_SHA256:
4741 return( POLARSSL_MD_SHA256 );
4742#endif
4743#if defined(POLARSSL_SHA512_C)
4744 case SSL_HASH_SHA384:
4745 return( POLARSSL_MD_SHA384 );
4746 case SSL_HASH_SHA512:
4747 return( POLARSSL_MD_SHA512 );
4748#endif
4749 default:
4750 return( POLARSSL_MD_NONE );
4751 }
4752}
4753
Paul Bakker5121ce52009-01-03 21:22:43 +00004754#endif
Gergely Budai987bfb52014-01-19 21:48:42 +01004755
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 */