blob: e9b102467fe579784949314b0256bdd560cee786 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker0be444a2013-08-27 21:55:01 +020038#include "polarssl/debug.h"
39#include "polarssl/ssl.h"
40
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020041#if defined(POLARSSL_X509_CRT_PARSE_C) && \
42 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
43#include "polarssl/oid.h"
44#endif
45
Paul Bakker7dc4c442014-02-01 22:50:26 +010046#if defined(POLARSSL_PLATFORM_C)
47#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020048#else
49#define polarssl_malloc malloc
50#define polarssl_free free
51#endif
52
Paul Bakker5121ce52009-01-03 21:22:43 +000053#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Paul Bakker6edcd412013-10-29 15:22:54 +010055#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
56 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000057#define strcasecmp _stricmp
58#endif
59
Paul Bakker05decb22013-08-15 13:33:48 +020060#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020061/*
62 * Convert max_fragment_length codes to length.
63 * RFC 6066 says:
64 * enum{
65 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
66 * } MaxFragmentLength;
67 * and we add 0 -> extension unused
68 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020069static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070{
71 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
72 512, /* SSL_MAX_FRAG_LEN_512 */
73 1024, /* SSL_MAX_FRAG_LEN_1024 */
74 2048, /* SSL_MAX_FRAG_LEN_2048 */
75 4096, /* SSL_MAX_FRAG_LEN_4096 */
76};
Paul Bakker05decb22013-08-15 13:33:48 +020077#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020078
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020079static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
80{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020081 ssl_session_free( dst );
82 memcpy( dst, src, sizeof( ssl_session ) );
83
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020085 if( src->peer_cert != NULL )
86 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020087 int ret;
88
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020089 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
90 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020091 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
92
Paul Bakkerb6b09562013-09-18 14:17:41 +020093 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094
Paul Bakkerddf26b42013-09-18 13:46:23 +020095 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
96 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020097 {
98 polarssl_free( dst->peer_cert );
99 dst->peer_cert = NULL;
100 return( ret );
101 }
102 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200104
Paul Bakkera503a632013-08-14 13:48:06 +0200105#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 if( src->ticket != NULL )
107 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200108 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
109 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
111
112 memcpy( dst->ticket, src->ticket, src->ticket_len );
113 }
Paul Bakkera503a632013-08-14 13:48:06 +0200114#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115
116 return( 0 );
117}
118
Paul Bakker05ef8352012-05-08 09:17:57 +0000119#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
120int (*ssl_hw_record_init)(ssl_context *ssl,
121 const unsigned char *key_enc, const unsigned char *key_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100122 size_t keylen,
Paul Bakker05ef8352012-05-08 09:17:57 +0000123 const unsigned char *iv_enc, const unsigned char *iv_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100124 size_t ivlen,
125 const unsigned char *mac_enc, const unsigned char *mac_dec,
126 size_t maclen) = NULL;
127int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000128int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
129int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
130int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
131int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
132#endif
133
Paul Bakker5121ce52009-01-03 21:22:43 +0000134/*
135 * Key material generation
136 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200137#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200138static int ssl3_prf( const unsigned char *secret, size_t slen,
139 const char *label,
140 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000141 unsigned char *dstbuf, size_t dlen )
142{
143 size_t i;
144 md5_context md5;
145 sha1_context sha1;
146 unsigned char padding[16];
147 unsigned char sha1sum[20];
148 ((void)label);
149
150 /*
151 * SSLv3:
152 * block =
153 * MD5( secret + SHA1( 'A' + secret + random ) ) +
154 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
155 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
156 * ...
157 */
158 for( i = 0; i < dlen / 16; i++ )
159 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200160 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000161
162 sha1_starts( &sha1 );
163 sha1_update( &sha1, padding, 1 + i );
164 sha1_update( &sha1, secret, slen );
165 sha1_update( &sha1, random, rlen );
166 sha1_finish( &sha1, sha1sum );
167
168 md5_starts( &md5 );
169 md5_update( &md5, secret, slen );
170 md5_update( &md5, sha1sum, 20 );
171 md5_finish( &md5, dstbuf + i * 16 );
172 }
173
174 memset( &md5, 0, sizeof( md5 ) );
175 memset( &sha1, 0, sizeof( sha1 ) );
176
177 memset( padding, 0, sizeof( padding ) );
178 memset( sha1sum, 0, sizeof( sha1sum ) );
179
180 return( 0 );
181}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200182#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000183
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200184#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200185static int tls1_prf( const unsigned char *secret, size_t slen,
186 const char *label,
187 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000188 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000189{
Paul Bakker23986e52011-04-24 08:57:21 +0000190 size_t nb, hs;
191 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200192 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 unsigned char tmp[128];
194 unsigned char h_i[20];
195
196 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000197 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199 hs = ( slen + 1 ) / 2;
200 S1 = secret;
201 S2 = secret + slen - hs;
202
203 nb = strlen( label );
204 memcpy( tmp + 20, label, nb );
205 memcpy( tmp + 20 + nb, random, rlen );
206 nb += rlen;
207
208 /*
209 * First compute P_md5(secret,label+random)[0..dlen]
210 */
211 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
212
213 for( i = 0; i < dlen; i += 16 )
214 {
215 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
216 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
217
218 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
219
220 for( j = 0; j < k; j++ )
221 dstbuf[i + j] = h_i[j];
222 }
223
224 /*
225 * XOR out with P_sha1(secret,label+random)[0..dlen]
226 */
227 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
228
229 for( i = 0; i < dlen; i += 20 )
230 {
231 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
232 sha1_hmac( S2, hs, tmp, 20, tmp );
233
234 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
235
236 for( j = 0; j < k; j++ )
237 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
238 }
239
240 memset( tmp, 0, sizeof( tmp ) );
241 memset( h_i, 0, sizeof( h_i ) );
242
243 return( 0 );
244}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200245#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200247#if defined(POLARSSL_SSL_PROTO_TLS1_2)
248#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200249static int tls_prf_sha256( const unsigned char *secret, size_t slen,
250 const char *label,
251 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000252 unsigned char *dstbuf, size_t dlen )
253{
254 size_t nb;
255 size_t i, j, k;
256 unsigned char tmp[128];
257 unsigned char h_i[32];
258
259 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
260 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
261
262 nb = strlen( label );
263 memcpy( tmp + 32, label, nb );
264 memcpy( tmp + 32 + nb, random, rlen );
265 nb += rlen;
266
267 /*
268 * Compute P_<hash>(secret, label + random)[0..dlen]
269 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200270 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000271
272 for( i = 0; i < dlen; i += 32 )
273 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200274 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
275 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000276
277 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
278
279 for( j = 0; j < k; j++ )
280 dstbuf[i + j] = h_i[j];
281 }
282
283 memset( tmp, 0, sizeof( tmp ) );
284 memset( h_i, 0, sizeof( h_i ) );
285
286 return( 0 );
287}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200288#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000289
Paul Bakker9e36f042013-06-30 14:34:05 +0200290#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200291static int tls_prf_sha384( const unsigned char *secret, size_t slen,
292 const char *label,
293 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000294 unsigned char *dstbuf, size_t dlen )
295{
296 size_t nb;
297 size_t i, j, k;
298 unsigned char tmp[128];
299 unsigned char h_i[48];
300
301 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
302 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
303
304 nb = strlen( label );
305 memcpy( tmp + 48, label, nb );
306 memcpy( tmp + 48 + nb, random, rlen );
307 nb += rlen;
308
309 /*
310 * Compute P_<hash>(secret, label + random)[0..dlen]
311 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200312 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000313
314 for( i = 0; i < dlen; i += 48 )
315 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200316 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
317 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000318
319 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
320
321 for( j = 0; j < k; j++ )
322 dstbuf[i + j] = h_i[j];
323 }
324
325 memset( tmp, 0, sizeof( tmp ) );
326 memset( h_i, 0, sizeof( h_i ) );
327
328 return( 0 );
329}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200330#endif /* POLARSSL_SHA512_C */
331#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000332
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200333static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200334
335#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
336 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200337static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338#endif
Paul Bakker380da532012-04-18 16:10:25 +0000339
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200340#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000341static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343#endif
344
345#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
346static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000347static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200348#endif
349
350#if defined(POLARSSL_SSL_PROTO_TLS1_2)
351#if defined(POLARSSL_SHA256_C)
352static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
353static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000354static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100356
Paul Bakker9e36f042013-06-30 14:34:05 +0200357#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200358static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100359static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000360static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100361#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200362#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +0000363
Paul Bakker5121ce52009-01-03 21:22:43 +0000364int ssl_derive_keys( ssl_context *ssl )
365{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200366 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 unsigned char keyblk[256];
369 unsigned char *key1;
370 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100371 unsigned char *mac_enc;
372 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200373 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100374 const cipher_info_t *cipher_info;
375 const md_info_t *md_info;
376
Paul Bakker48916f92012-09-16 19:57:18 +0000377 ssl_session *session = ssl->session_negotiate;
378 ssl_transform *transform = ssl->transform_negotiate;
379 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
381 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
382
Paul Bakker68884e32013-01-07 18:20:04 +0100383 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
384 if( cipher_info == NULL )
385 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200386 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100387 transform->ciphersuite_info->cipher ) );
388 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
389 }
390
391 md_info = md_info_from_type( transform->ciphersuite_info->mac );
392 if( md_info == NULL )
393 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200394 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100395 transform->ciphersuite_info->mac ) );
396 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
397 }
398
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000400 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000401 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200402#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000403 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404 {
Paul Bakker48916f92012-09-16 19:57:18 +0000405 handshake->tls_prf = ssl3_prf;
406 handshake->calc_verify = ssl_calc_verify_ssl;
407 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200409 else
410#endif
411#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
412 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413 {
Paul Bakker48916f92012-09-16 19:57:18 +0000414 handshake->tls_prf = tls1_prf;
415 handshake->calc_verify = ssl_calc_verify_tls;
416 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418 else
419#endif
420#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200421#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200422 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
423 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000424 {
Paul Bakker48916f92012-09-16 19:57:18 +0000425 handshake->tls_prf = tls_prf_sha384;
426 handshake->calc_verify = ssl_calc_verify_tls_sha384;
427 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000429 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430#endif
431#if defined(POLARSSL_SHA256_C)
432 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000433 {
Paul Bakker48916f92012-09-16 19:57:18 +0000434 handshake->tls_prf = tls_prf_sha256;
435 handshake->calc_verify = ssl_calc_verify_tls_sha256;
436 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200438 else
439#endif
440#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200441 {
442 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200443 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200444 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000445
446 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 * SSLv3:
448 * master =
449 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
450 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
451 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200452 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200453 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 * master = PRF( premaster, "master secret", randbytes )[0..47]
455 */
Paul Bakker0a597072012-09-25 21:55:46 +0000456 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 {
Paul Bakker48916f92012-09-16 19:57:18 +0000458 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
459 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
Paul Bakker48916f92012-09-16 19:57:18 +0000461 handshake->tls_prf( handshake->premaster, handshake->pmslen,
462 "master secret",
463 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Paul Bakker48916f92012-09-16 19:57:18 +0000465 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 }
467 else
468 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
469
470 /*
471 * Swap the client and server random values.
472 */
Paul Bakker48916f92012-09-16 19:57:18 +0000473 memcpy( tmp, handshake->randbytes, 64 );
474 memcpy( handshake->randbytes, tmp + 32, 32 );
475 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 memset( tmp, 0, sizeof( tmp ) );
477
478 /*
479 * SSLv3:
480 * key block =
481 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
482 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
483 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
484 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
485 * ...
486 *
487 * TLSv1:
488 * key block = PRF( master, "key expansion", randbytes )
489 */
Paul Bakker48916f92012-09-16 19:57:18 +0000490 handshake->tls_prf( session->master, 48, "key expansion",
491 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
Paul Bakker48916f92012-09-16 19:57:18 +0000493 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
494 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
495 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
496 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
498
Paul Bakker48916f92012-09-16 19:57:18 +0000499 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501 /*
502 * Determine the appropriate key, IV and MAC length.
503 */
Paul Bakker68884e32013-01-07 18:20:04 +0100504
505 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 {
Paul Bakker68884e32013-01-07 18:20:04 +0100507 transform->keylen = cipher_info->key_length;
508 transform->keylen /= 8;
509 transform->minlen = 1;
510 transform->ivlen = 12;
511 transform->fixed_ivlen = 4;
512 transform->maclen = 0;
513 }
514 else
515 {
516 if( md_info->type != POLARSSL_MD_NONE )
517 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200518 int ret;
519
Paul Bakker61d113b2013-07-04 11:51:43 +0200520 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
521 {
522 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
523 return( ret );
524 }
525
526 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
527 {
528 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
529 return( ret );
530 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Paul Bakker68884e32013-01-07 18:20:04 +0100532 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200533
Paul Bakker1f2bc622013-08-15 13:45:55 +0200534#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200535 /*
536 * If HMAC is to be truncated, we shall keep the leftmost bytes,
537 * (rfc 6066 page 13 or rfc 2104 section 4),
538 * so we only need to adjust the length here.
539 */
540 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
541 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200542#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100543 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Paul Bakker68884e32013-01-07 18:20:04 +0100545 transform->keylen = cipher_info->key_length;
546 transform->keylen /= 8;
547 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Paul Bakker68884e32013-01-07 18:20:04 +0100549 transform->minlen = transform->keylen;
550 if( transform->minlen < transform->maclen )
551 {
552 if( cipher_info->mode == POLARSSL_MODE_STREAM )
553 transform->minlen = transform->maclen;
554 else
555 transform->minlen += transform->keylen;
556 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 }
558
559 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000560 transform->keylen, transform->minlen, transform->ivlen,
561 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563 /*
564 * Finally setup the cipher contexts, IVs and MAC secrets.
565 */
566 if( ssl->endpoint == SSL_IS_CLIENT )
567 {
Paul Bakker48916f92012-09-16 19:57:18 +0000568 key1 = keyblk + transform->maclen * 2;
569 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Paul Bakker68884e32013-01-07 18:20:04 +0100571 mac_enc = keyblk;
572 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000574 /*
575 * This is not used in TLS v1.1.
576 */
Paul Bakker48916f92012-09-16 19:57:18 +0000577 iv_copy_len = ( transform->fixed_ivlen ) ?
578 transform->fixed_ivlen : transform->ivlen;
579 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
580 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000581 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 }
583 else
584 {
Paul Bakker48916f92012-09-16 19:57:18 +0000585 key1 = keyblk + transform->maclen * 2 + transform->keylen;
586 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Paul Bakker68884e32013-01-07 18:20:04 +0100588 mac_enc = keyblk + transform->maclen;
589 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000591 /*
592 * This is not used in TLS v1.1.
593 */
Paul Bakker48916f92012-09-16 19:57:18 +0000594 iv_copy_len = ( transform->fixed_ivlen ) ?
595 transform->fixed_ivlen : transform->ivlen;
596 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
597 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000598 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 }
600
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200601#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100602 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
603 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100604 if( transform->maclen > sizeof transform->mac_enc )
605 {
606 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
607 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
608 }
609
Paul Bakker68884e32013-01-07 18:20:04 +0100610 memcpy( transform->mac_enc, mac_enc, transform->maclen );
611 memcpy( transform->mac_dec, mac_dec, transform->maclen );
612 }
613 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200614#endif
615#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
616 defined(POLARSSL_SSL_PROTO_TLS1_2)
617 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100618 {
619 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
620 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
621 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200622 else
623#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200624 {
625 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200626 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200627 }
Paul Bakker68884e32013-01-07 18:20:04 +0100628
Paul Bakker05ef8352012-05-08 09:17:57 +0000629#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
630 if( ssl_hw_record_init != NULL)
631 {
632 int ret = 0;
633
634 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
635
Paul Bakker07eb38b2012-12-19 14:42:06 +0100636 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
637 transform->iv_enc, transform->iv_dec,
638 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100639 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100640 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000641 {
642 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
643 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
644 }
645 }
646#endif
647
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200648 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
649 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200651 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
652 return( ret );
653 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200654
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200655 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
656 cipher_info ) ) != 0 )
657 {
658 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
659 return( ret );
660 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200661
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200662 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
663 cipher_info->key_length,
664 POLARSSL_ENCRYPT ) ) != 0 )
665 {
666 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
667 return( ret );
668 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200669
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200670 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
671 cipher_info->key_length,
672 POLARSSL_DECRYPT ) ) != 0 )
673 {
674 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
675 return( ret );
676 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200677
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200678#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200679 if( cipher_info->mode == POLARSSL_MODE_CBC )
680 {
681 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
682 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200683 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
685 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200686 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200687
688 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
689 POLARSSL_PADDING_NONE ) ) != 0 )
690 {
691 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
692 return( ret );
693 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200695#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
697 memset( keyblk, 0, sizeof( keyblk ) );
698
Paul Bakker2770fbd2012-07-03 13:30:23 +0000699#if defined(POLARSSL_ZLIB_SUPPORT)
700 // Initialize compression
701 //
Paul Bakker48916f92012-09-16 19:57:18 +0000702 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000703 {
Paul Bakker16770332013-10-11 09:59:44 +0200704 if( ssl->compress_buf == NULL )
705 {
706 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
707 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
708 if( ssl->compress_buf == NULL )
709 {
710 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
711 SSL_BUFFER_LEN ) );
712 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
713 }
714 }
715
Paul Bakker2770fbd2012-07-03 13:30:23 +0000716 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
717
Paul Bakker48916f92012-09-16 19:57:18 +0000718 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
719 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000720
Paul Bakker48916f92012-09-16 19:57:18 +0000721 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
722 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000723 {
724 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
725 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
726 }
727 }
728#endif /* POLARSSL_ZLIB_SUPPORT */
729
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
731
732 return( 0 );
733}
734
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200735#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000736void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000737{
738 md5_context md5;
739 sha1_context sha1;
740 unsigned char pad_1[48];
741 unsigned char pad_2[48];
742
Paul Bakker380da532012-04-18 16:10:25 +0000743 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Paul Bakker48916f92012-09-16 19:57:18 +0000745 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
746 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
Paul Bakker380da532012-04-18 16:10:25 +0000748 memset( pad_1, 0x36, 48 );
749 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Paul Bakker48916f92012-09-16 19:57:18 +0000751 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000752 md5_update( &md5, pad_1, 48 );
753 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Paul Bakker380da532012-04-18 16:10:25 +0000755 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000756 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000757 md5_update( &md5, pad_2, 48 );
758 md5_update( &md5, hash, 16 );
759 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Paul Bakker48916f92012-09-16 19:57:18 +0000761 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000762 sha1_update( &sha1, pad_1, 40 );
763 sha1_finish( &sha1, hash + 16 );
764
765 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000766 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000767 sha1_update( &sha1, pad_2, 40 );
768 sha1_update( &sha1, hash + 16, 20 );
769 sha1_finish( &sha1, hash + 16 );
770
771 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
772 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
773
774 return;
775}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200776#endif
Paul Bakker380da532012-04-18 16:10:25 +0000777
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200778#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000779void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
780{
781 md5_context md5;
782 sha1_context sha1;
783
784 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
785
Paul Bakker48916f92012-09-16 19:57:18 +0000786 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
787 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000788
Paul Bakker48916f92012-09-16 19:57:18 +0000789 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000790 sha1_finish( &sha1, hash + 16 );
791
792 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
793 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
794
795 return;
796}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200797#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000798
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200799#if defined(POLARSSL_SSL_PROTO_TLS1_2)
800#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000801void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
802{
Paul Bakker9e36f042013-06-30 14:34:05 +0200803 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000804
805 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
806
Paul Bakker9e36f042013-06-30 14:34:05 +0200807 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
808 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000809
810 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
811 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
812
813 return;
814}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200815#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000816
Paul Bakker9e36f042013-06-30 14:34:05 +0200817#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000818void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
819{
Paul Bakker9e36f042013-06-30 14:34:05 +0200820 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000821
822 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
823
Paul Bakker9e36f042013-06-30 14:34:05 +0200824 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
825 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000826
Paul Bakkerca4ab492012-04-18 14:23:57 +0000827 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
829
830 return;
831}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200832#endif /* POLARSSL_SHA512_C */
833#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200835#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200836int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
837{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200838 unsigned char *p = ssl->handshake->premaster;
839 unsigned char *end = p + sizeof( ssl->handshake->premaster );
840
841 /*
842 * PMS = struct {
843 * opaque other_secret<0..2^16-1>;
844 * opaque psk<0..2^16-1>;
845 * };
846 * with "other_secret" depending on the particular key exchange
847 */
848#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
849 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
850 {
851 if( end - p < 2 + (int) ssl->psk_len )
852 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
853
854 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
855 *(p++) = (unsigned char)( ssl->psk_len );
856 p += ssl->psk_len;
857 }
858 else
859#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200860#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
861 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
862 {
863 /*
864 * other_secret already set by the ClientKeyExchange message,
865 * and is 48 bytes long
866 */
867 *p++ = 0;
868 *p++ = 48;
869 p += 48;
870 }
871 else
872#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200873#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
874 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
875 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200876 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200877 size_t len = ssl->handshake->dhm_ctx.len;
878
879 if( end - p < 2 + (int) len )
880 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
881
882 *(p++) = (unsigned char)( len >> 8 );
883 *(p++) = (unsigned char)( len );
884 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
885 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
886 {
887 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
888 return( ret );
889 }
890 p += len;
891
892 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
893 }
894 else
895#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
896#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
897 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
898 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200899 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200900 size_t zlen;
901
902 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
903 p + 2, end - (p + 2),
904 ssl->f_rng, ssl->p_rng ) ) != 0 )
905 {
906 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
907 return( ret );
908 }
909
910 *(p++) = (unsigned char)( zlen >> 8 );
911 *(p++) = (unsigned char)( zlen );
912 p += zlen;
913
914 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
915 }
916 else
917#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
918 {
919 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
920 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
921 }
922
923 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100924 if( end - p < 2 + (int) ssl->psk_len )
925 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
926
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200927 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
928 *(p++) = (unsigned char)( ssl->psk_len );
929 memcpy( p, ssl->psk, ssl->psk_len );
930 p += ssl->psk_len;
931
932 ssl->handshake->pmslen = p - ssl->handshake->premaster;
933
934 return( 0 );
935}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200936#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200938#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000939/*
940 * SSLv3.0 MAC functions
941 */
Paul Bakker68884e32013-01-07 18:20:04 +0100942static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
943 unsigned char *buf, size_t len,
944 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000945{
946 unsigned char header[11];
947 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100948 int padlen = 0;
949 int md_size = md_get_size( md_ctx->md_info );
950 int md_type = md_get_type( md_ctx->md_info );
951
952 if( md_type == POLARSSL_MD_MD5 )
953 padlen = 48;
954 else if( md_type == POLARSSL_MD_SHA1 )
955 padlen = 40;
956 else if( md_type == POLARSSL_MD_SHA256 )
957 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100958 else if( md_type == POLARSSL_MD_SHA384 )
959 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
961 memcpy( header, ctr, 8 );
962 header[ 8] = (unsigned char) type;
963 header[ 9] = (unsigned char)( len >> 8 );
964 header[10] = (unsigned char)( len );
965
Paul Bakker68884e32013-01-07 18:20:04 +0100966 memset( padding, 0x36, padlen );
967 md_starts( md_ctx );
968 md_update( md_ctx, secret, md_size );
969 md_update( md_ctx, padding, padlen );
970 md_update( md_ctx, header, 11 );
971 md_update( md_ctx, buf, len );
972 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
Paul Bakker68884e32013-01-07 18:20:04 +0100974 memset( padding, 0x5C, padlen );
975 md_starts( md_ctx );
976 md_update( md_ctx, secret, md_size );
977 md_update( md_ctx, padding, padlen );
978 md_update( md_ctx, buf + len, md_size );
979 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000980}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200981#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000982
Paul Bakker5121ce52009-01-03 21:22:43 +0000983/*
984 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200985 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000986static int ssl_encrypt_buf( ssl_context *ssl )
987{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200988 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
991
992 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200993 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200995#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
996 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
997 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
998 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
999 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001001#if defined(POLARSSL_SSL_PROTO_SSL3)
1002 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1003 {
1004 ssl_mac( &ssl->transform_out->md_ctx_enc,
1005 ssl->transform_out->mac_enc,
1006 ssl->out_msg, ssl->out_msglen,
1007 ssl->out_ctr, ssl->out_msgtype );
1008 }
1009 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001010#endif
1011#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001012 defined(POLARSSL_SSL_PROTO_TLS1_2)
1013 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1014 {
1015 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1016 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1017 ssl->out_msg, ssl->out_msglen );
1018 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1019 ssl->out_msg + ssl->out_msglen );
1020 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1021 }
1022 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001023#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001024 {
1025 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1026 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1027 }
1028
1029 SSL_DEBUG_BUF( 4, "computed mac",
1030 ssl->out_msg + ssl->out_msglen,
1031 ssl->transform_out->maclen );
1032
1033 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001034 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001035#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001037 /*
1038 * Encrypt
1039 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001040#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001041 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1042 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001044 int ret;
1045 size_t olen = 0;
1046
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1048 "including %d bytes of padding",
1049 ssl->out_msglen, 0 ) );
1050
1051 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1052 ssl->out_msg, ssl->out_msglen );
1053
Paul Bakker45125bc2013-09-04 16:47:11 +02001054 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001055 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001056 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1057 return( ret );
1058 }
1059
1060 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1061 ssl->transform_out->iv_enc,
1062 ssl->transform_out->ivlen ) ) != 0 )
1063 {
1064 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001065 return( ret );
1066 }
1067
1068 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1069 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1070 &olen ) ) != 0 )
1071 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001072 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001073 return( ret );
1074 }
1075
1076 if( ssl->out_msglen != olen )
1077 {
1078 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1079 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001080 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001081 }
1082
1083 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001084 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001085 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001086 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001087 return( ret );
1088 }
1089
1090 if( 0 != olen )
1091 {
1092 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1093 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001094 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 }
Paul Bakker68884e32013-01-07 18:20:04 +01001097 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001098#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001099#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001100 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1101 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001102 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001103 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001104 unsigned char *enc_msg;
1105 unsigned char add_data[13];
1106 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1107
Paul Bakkerca4ab492012-04-18 14:23:57 +00001108 memcpy( add_data, ssl->out_ctr, 8 );
1109 add_data[8] = ssl->out_msgtype;
1110 add_data[9] = ssl->major_ver;
1111 add_data[10] = ssl->minor_ver;
1112 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1113 add_data[12] = ssl->out_msglen & 0xFF;
1114
1115 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1116 add_data, 13 );
1117
Paul Bakker68884e32013-01-07 18:20:04 +01001118 /*
1119 * Generate IV
1120 */
1121 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001122 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001123 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1124 if( ret != 0 )
1125 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001126
Paul Bakker68884e32013-01-07 18:20:04 +01001127 memcpy( ssl->out_iv,
1128 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1129 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001130
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001131 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1132 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1133
Paul Bakker68884e32013-01-07 18:20:04 +01001134 /*
1135 * Fix pointer positions and message length with added IV
1136 */
1137 enc_msg = ssl->out_msg;
1138 enc_msglen = ssl->out_msglen;
1139 ssl->out_msglen += ssl->transform_out->ivlen -
1140 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001141
Paul Bakker68884e32013-01-07 18:20:04 +01001142 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1143 "including %d bytes of padding",
1144 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001145
Paul Bakker68884e32013-01-07 18:20:04 +01001146 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1147 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001148
Paul Bakker68884e32013-01-07 18:20:04 +01001149 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001150 * Encrypt
1151 */
1152 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1153 ssl->transform_out->iv_enc,
1154 ssl->transform_out->ivlen ) ) != 0 ||
1155 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1156 {
1157 return( ret );
1158 }
1159
1160 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1161 add_data, 13 ) ) != 0 )
1162 {
1163 return( ret );
1164 }
1165
1166 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1167 enc_msg, enc_msglen,
1168 enc_msg, &olen ) ) != 0 )
1169 {
1170 return( ret );
1171 }
1172 totlen = olen;
1173
1174 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1175 enc_msg + olen, &olen ) ) != 0 )
1176 {
1177 return( ret );
1178 }
1179 totlen += olen;
1180
1181 if( totlen != enc_msglen )
1182 {
1183 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1184 return( -1 );
1185 }
1186
1187 /*
1188 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001189 */
1190 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001191
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001192 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1193 enc_msg + enc_msglen, 16 ) ) != 0 )
1194 {
1195 return( ret );
1196 }
Paul Bakker68884e32013-01-07 18:20:04 +01001197
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001198 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 else
Paul Bakker68884e32013-01-07 18:20:04 +01001201#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001202#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1203 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001204 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1205 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001207 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001208 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001209 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001210
Paul Bakker48916f92012-09-16 19:57:18 +00001211 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1212 ssl->transform_out->ivlen;
1213 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 padlen = 0;
1215
1216 for( i = 0; i <= padlen; i++ )
1217 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1218
1219 ssl->out_msglen += padlen + 1;
1220
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001221 enc_msglen = ssl->out_msglen;
1222 enc_msg = ssl->out_msg;
1223
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001224#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001225 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001226 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1227 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001228 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001229 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230 {
1231 /*
1232 * Generate IV
1233 */
Paul Bakker48916f92012-09-16 19:57:18 +00001234 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1235 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001236 if( ret != 0 )
1237 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238
Paul Bakker92be97b2013-01-02 17:30:03 +01001239 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001240 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001241
1242 /*
1243 * Fix pointer positions and message length with added IV
1244 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001245 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001246 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001247 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001248 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001249#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001250
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001252 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001253 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
1255 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001256 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
Paul Bakker45125bc2013-09-04 16:47:11 +02001258 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001260 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1261 return( ret );
1262 }
1263
1264 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1265 ssl->transform_out->iv_enc,
1266 ssl->transform_out->ivlen ) ) != 0 )
1267 {
1268 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001269 return( ret );
1270 }
Paul Bakker68884e32013-01-07 18:20:04 +01001271
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1273 enc_msg, enc_msglen, enc_msg,
1274 &olen ) ) != 0 )
1275 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001276 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001277 return( ret );
1278 }
Paul Bakker68884e32013-01-07 18:20:04 +01001279
Paul Bakkercca5b812013-08-31 17:40:26 +02001280 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001281
Paul Bakkercca5b812013-08-31 17:40:26 +02001282 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001283 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001284 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001285 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001286 return( ret );
1287 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001288
Paul Bakkercca5b812013-08-31 17:40:26 +02001289 if( enc_msglen != olen )
1290 {
1291 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1292 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001293 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001294 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001295
1296#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001297 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1298 {
1299 /*
1300 * Save IV in SSL3 and TLS1
1301 */
1302 memcpy( ssl->transform_out->iv_enc,
1303 ssl->transform_out->cipher_ctx_enc.iv,
1304 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001306#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001308 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001309#endif /* POLARSSL_CIPHER_MODE_CBC &&
1310 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001311 {
1312 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1313 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1314 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
Paul Bakkerca4ab492012-04-18 14:23:57 +00001316 for( i = 8; i > 0; i-- )
1317 if( ++ssl->out_ctr[i - 1] != 0 )
1318 break;
1319
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001320 /* The loops goes to its end iff the counter is wrapping */
1321 if( i == 0 )
1322 {
1323 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1324 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1325 }
1326
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1328
1329 return( 0 );
1330}
1331
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001332#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001333
Paul Bakker5121ce52009-01-03 21:22:43 +00001334static int ssl_decrypt_buf( ssl_context *ssl )
1335{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001336 size_t i;
1337#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1338 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1339 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1340 size_t padlen = 0, correct = 1;
1341#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
1343 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1344
Paul Bakker48916f92012-09-16 19:57:18 +00001345 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 {
1347 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001348 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001349 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
1351
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001352#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001353 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1354 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001355 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001356 int ret;
1357 size_t olen = 0;
1358
Paul Bakker68884e32013-01-07 18:20:04 +01001359 padlen = 0;
1360
Paul Bakker45125bc2013-09-04 16:47:11 +02001361 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001362 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001363 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1364 return( ret );
1365 }
1366
1367 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1368 ssl->transform_in->iv_dec,
1369 ssl->transform_in->ivlen ) ) != 0 )
1370 {
1371 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001372 return( ret );
1373 }
1374
1375 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1376 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1377 &olen ) ) != 0 )
1378 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001379 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001380 return( ret );
1381 }
1382
1383 if( ssl->in_msglen != olen )
1384 {
1385 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001386 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001387 }
1388
1389 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001390 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001391 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001392 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001393 return( ret );
1394 }
1395
1396 if( 0 != olen )
1397 {
1398 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001399 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001400 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 }
Paul Bakker68884e32013-01-07 18:20:04 +01001402 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001403#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001404#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001405 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1406 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001407 {
1408 unsigned char *dec_msg;
1409 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001410 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001411 unsigned char add_data[13];
1412 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1413
Paul Bakker68884e32013-01-07 18:20:04 +01001414 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1415 ssl->transform_in->fixed_ivlen );
1416 dec_msglen -= 16;
1417 dec_msg = ssl->in_msg;
1418 dec_msg_result = ssl->in_msg;
1419 ssl->in_msglen = dec_msglen;
1420
1421 memcpy( add_data, ssl->in_ctr, 8 );
1422 add_data[8] = ssl->in_msgtype;
1423 add_data[9] = ssl->major_ver;
1424 add_data[10] = ssl->minor_ver;
1425 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1426 add_data[12] = ssl->in_msglen & 0xFF;
1427
1428 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1429 add_data, 13 );
1430
1431 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1432 ssl->in_iv,
1433 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1434
1435 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1436 ssl->transform_in->ivlen );
1437 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1438
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001439 /*
1440 * Decrypt
1441 */
1442 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1443 ssl->transform_in->iv_dec,
1444 ssl->transform_in->ivlen ) ) != 0 ||
1445 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001446 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001447 return( ret );
1448 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001449
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001450 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1451 add_data, 13 ) ) != 0 )
1452 {
1453 return( ret );
1454 }
1455
1456 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1457 dec_msg, dec_msglen,
1458 dec_msg_result, &olen ) ) != 0 )
1459 {
1460 return( ret );
1461 }
1462 totlen = olen;
1463
1464 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1465 dec_msg_result + olen, &olen ) ) != 0 )
1466 {
1467 return( ret );
1468 }
1469 totlen += olen;
1470
1471 if( totlen != dec_msglen )
1472 {
1473 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1474 return( -1 );
1475 }
1476
1477 /*
1478 * Authenticate
1479 */
1480 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1481 dec_msg + dec_msglen, 16 ) ) != 0 )
1482 {
1483 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001484 return( POLARSSL_ERR_SSL_INVALID_MAC );
1485 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001486
Paul Bakkerca4ab492012-04-18 14:23:57 +00001487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 else
Paul Bakker68884e32013-01-07 18:20:04 +01001489#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001490#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1491 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001492 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1493 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 {
Paul Bakker45829992013-01-03 14:52:21 +01001495 /*
1496 * Decrypt and check the padding
1497 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001498 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001499 unsigned char *dec_msg;
1500 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001501 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001502 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001503 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001504
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 /*
Paul Bakker45829992013-01-03 14:52:21 +01001506 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 */
Paul Bakker48916f92012-09-16 19:57:18 +00001508 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 {
1510 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001511 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001512 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 }
1514
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001515#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001516 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1517 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001518#endif
Paul Bakker45829992013-01-03 14:52:21 +01001519
1520 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1521 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1522 {
1523 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1524 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1525 return( POLARSSL_ERR_SSL_INVALID_MAC );
1526 }
1527
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001528 dec_msglen = ssl->in_msglen;
1529 dec_msg = ssl->in_msg;
1530 dec_msg_result = ssl->in_msg;
1531
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001532#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001533 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001534 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001535 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001536 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001537 {
Paul Bakker48916f92012-09-16 19:57:18 +00001538 dec_msglen -= ssl->transform_in->ivlen;
1539 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001540
Paul Bakker48916f92012-09-16 19:57:18 +00001541 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001542 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001543 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001544#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001545
Paul Bakker45125bc2013-09-04 16:47:11 +02001546 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001548 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1549 return( ret );
1550 }
1551
1552 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1553 ssl->transform_in->iv_dec,
1554 ssl->transform_in->ivlen ) ) != 0 )
1555 {
1556 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001557 return( ret );
1558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001559
Paul Bakkercca5b812013-08-31 17:40:26 +02001560 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1561 dec_msg, dec_msglen, dec_msg_result,
1562 &olen ) ) != 0 )
1563 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001564 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001565 return( ret );
1566 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001567
Paul Bakkercca5b812013-08-31 17:40:26 +02001568 dec_msglen -= olen;
1569 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001570 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001571 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001572 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001573 return( ret );
1574 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001575
Paul Bakkercca5b812013-08-31 17:40:26 +02001576 if( dec_msglen != olen )
1577 {
1578 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001579 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001580 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001581
1582#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001583 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1584 {
1585 /*
1586 * Save IV in SSL3 and TLS1
1587 */
1588 memcpy( ssl->transform_in->iv_dec,
1589 ssl->transform_in->cipher_ctx_dec.iv,
1590 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001592#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001593
1594 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001595
1596 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1597 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001598#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001599 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1600 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001601#endif
Paul Bakker45829992013-01-03 14:52:21 +01001602 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001603 correct = 0;
1604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001606#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1608 {
Paul Bakker48916f92012-09-16 19:57:18 +00001609 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001610 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001611#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001612 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1613 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001614 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001615#endif
Paul Bakker45829992013-01-03 14:52:21 +01001616 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 }
1618 }
1619 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001620#endif
1621#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1622 defined(POLARSSL_SSL_PROTO_TLS1_2)
1623 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 {
1625 /*
Paul Bakker45829992013-01-03 14:52:21 +01001626 * TLSv1+: always check the padding up to the first failure
1627 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001629 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001630 size_t padding_idx = ssl->in_msglen - padlen - 1;
1631
Paul Bakker956c9e02013-12-19 14:42:28 +01001632 /*
1633 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001634 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001635 *
Paul Bakker61885c72014-04-25 12:59:03 +02001636 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1637 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001638 *
1639 * In both cases we reset padding_idx to a safe value (0) to
1640 * prevent out-of-buffer reads.
1641 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001642 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001643 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1644 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001645
1646 padding_idx *= correct;
1647
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001648 for( i = 1; i <= 256; i++ )
1649 {
1650 real_count &= ( i <= padlen );
1651 pad_count += real_count *
1652 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1653 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001654
1655 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001656
Paul Bakkerd66f0702013-01-31 16:57:45 +01001657#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001658 if( padlen > 0 && correct == 0)
1659 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001660#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001661 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001663 else
1664#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1665 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001666 {
1667 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001668 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001669 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001671 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001672#endif /* POLARSSL_CIPHER_MODE_CBC &&
1673 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001674 {
1675 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1676 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
1679 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1680 ssl->in_msg, ssl->in_msglen );
1681
1682 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001683 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001684 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001685#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1686 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1687 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1688 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1689 POLARSSL_MODE_GCM )
1690 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001691 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1692
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001693 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001695 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1696 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001698 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001700#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001701 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1702 {
1703 ssl_mac( &ssl->transform_in->md_ctx_dec,
1704 ssl->transform_in->mac_dec,
1705 ssl->in_msg, ssl->in_msglen,
1706 ssl->in_ctr, ssl->in_msgtype );
1707 }
1708 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001709#endif /* POLARSSL_SSL_PROTO_SSL3 */
1710#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001711 defined(POLARSSL_SSL_PROTO_TLS1_2)
1712 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1713 {
1714 /*
1715 * Process MAC and always update for padlen afterwards to make
1716 * total time independent of padlen
1717 *
1718 * extra_run compensates MAC check for padlen
1719 *
1720 * Known timing attacks:
1721 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1722 *
1723 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1724 * correctly. (We round down instead of up, so -56 is the correct
1725 * value for our calculations instead of -55)
1726 */
1727 size_t j, extra_run = 0;
1728 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1729 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001730
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001731 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001732
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001733 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1734 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1735 ssl->in_msglen );
1736 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1737 ssl->in_msg + ssl->in_msglen );
1738 for( j = 0; j < extra_run; j++ )
1739 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001740
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001741 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1742 }
1743 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001744#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001745 POLARSSL_SSL_PROTO_TLS1_2 */
1746 {
1747 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1748 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1749 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001751 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1752 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1753 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001755 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001756 ssl->transform_in->maclen ) != 0 )
1757 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001758#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001759 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001760#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001761 correct = 0;
1762 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001763
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001764 /*
1765 * Finally check the correct flag
1766 */
1767 if( correct == 0 )
1768 return( POLARSSL_ERR_SSL_INVALID_MAC );
1769 }
1770#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
1772 if( ssl->in_msglen == 0 )
1773 {
1774 ssl->nb_zero++;
1775
1776 /*
1777 * Three or more empty messages may be a DoS attack
1778 * (excessive CPU consumption).
1779 */
1780 if( ssl->nb_zero > 3 )
1781 {
1782 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1783 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001784 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 }
1786 }
1787 else
1788 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001789
Paul Bakker23986e52011-04-24 08:57:21 +00001790 for( i = 8; i > 0; i-- )
1791 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001792 break;
1793
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001794 /* The loops goes to its end iff the counter is wrapping */
1795 if( i == 0 )
1796 {
1797 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1798 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1799 }
1800
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1802
1803 return( 0 );
1804}
1805
Paul Bakker2770fbd2012-07-03 13:30:23 +00001806#if defined(POLARSSL_ZLIB_SUPPORT)
1807/*
1808 * Compression/decompression functions
1809 */
1810static int ssl_compress_buf( ssl_context *ssl )
1811{
1812 int ret;
1813 unsigned char *msg_post = ssl->out_msg;
1814 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001815 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001816
1817 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1818
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001819 if( len_pre == 0 )
1820 return( 0 );
1821
Paul Bakker2770fbd2012-07-03 13:30:23 +00001822 memcpy( msg_pre, ssl->out_msg, len_pre );
1823
1824 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1825 ssl->out_msglen ) );
1826
1827 SSL_DEBUG_BUF( 4, "before compression: output payload",
1828 ssl->out_msg, ssl->out_msglen );
1829
Paul Bakker48916f92012-09-16 19:57:18 +00001830 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1831 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1832 ssl->transform_out->ctx_deflate.next_out = msg_post;
1833 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001834
Paul Bakker48916f92012-09-16 19:57:18 +00001835 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001836 if( ret != Z_OK )
1837 {
1838 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1839 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1840 }
1841
Paul Bakker48916f92012-09-16 19:57:18 +00001842 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001843
Paul Bakker2770fbd2012-07-03 13:30:23 +00001844 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1845 ssl->out_msglen ) );
1846
1847 SSL_DEBUG_BUF( 4, "after compression: output payload",
1848 ssl->out_msg, ssl->out_msglen );
1849
1850 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1851
1852 return( 0 );
1853}
1854
1855static int ssl_decompress_buf( ssl_context *ssl )
1856{
1857 int ret;
1858 unsigned char *msg_post = ssl->in_msg;
1859 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001860 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001861
1862 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1863
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001864 if( len_pre == 0 )
1865 return( 0 );
1866
Paul Bakker2770fbd2012-07-03 13:30:23 +00001867 memcpy( msg_pre, ssl->in_msg, len_pre );
1868
1869 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1870 ssl->in_msglen ) );
1871
1872 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1873 ssl->in_msg, ssl->in_msglen );
1874
Paul Bakker48916f92012-09-16 19:57:18 +00001875 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1876 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1877 ssl->transform_in->ctx_inflate.next_out = msg_post;
1878 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001879
Paul Bakker48916f92012-09-16 19:57:18 +00001880 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001881 if( ret != Z_OK )
1882 {
1883 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1884 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1885 }
1886
Paul Bakker48916f92012-09-16 19:57:18 +00001887 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001888
Paul Bakker2770fbd2012-07-03 13:30:23 +00001889 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1890 ssl->in_msglen ) );
1891
1892 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1893 ssl->in_msg, ssl->in_msglen );
1894
1895 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1896
1897 return( 0 );
1898}
1899#endif /* POLARSSL_ZLIB_SUPPORT */
1900
Paul Bakker5121ce52009-01-03 21:22:43 +00001901/*
1902 * Fill the input message buffer
1903 */
Paul Bakker23986e52011-04-24 08:57:21 +00001904int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001905{
Paul Bakker23986e52011-04-24 08:57:21 +00001906 int ret;
1907 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
1909 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1910
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001911 if( nb_want > SSL_BUFFER_LEN - 8 )
1912 {
1913 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1914 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1915 }
1916
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 while( ssl->in_left < nb_want )
1918 {
1919 len = nb_want - ssl->in_left;
1920 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1921
1922 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1923 ssl->in_left, nb_want ) );
1924 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1925
Paul Bakker831a7552011-05-18 13:32:51 +00001926 if( ret == 0 )
1927 return( POLARSSL_ERR_SSL_CONN_EOF );
1928
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 if( ret < 0 )
1930 return( ret );
1931
1932 ssl->in_left += ret;
1933 }
1934
1935 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1936
1937 return( 0 );
1938}
1939
1940/*
1941 * Flush any data not yet written
1942 */
1943int ssl_flush_output( ssl_context *ssl )
1944{
1945 int ret;
1946 unsigned char *buf;
1947
1948 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1949
1950 while( ssl->out_left > 0 )
1951 {
1952 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1953 5 + ssl->out_msglen, ssl->out_left ) );
1954
Paul Bakker5bd42292012-12-19 14:40:42 +01001955 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001957
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1959
1960 if( ret <= 0 )
1961 return( ret );
1962
1963 ssl->out_left -= ret;
1964 }
1965
1966 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1967
1968 return( 0 );
1969}
1970
1971/*
1972 * Record layer functions
1973 */
1974int ssl_write_record( ssl_context *ssl )
1975{
Paul Bakker05ef8352012-05-08 09:17:57 +00001976 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001977 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
1979 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1980
Paul Bakker5121ce52009-01-03 21:22:43 +00001981 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1982 {
1983 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1984 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1985 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1986
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001987 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1988 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 }
1990
Paul Bakker2770fbd2012-07-03 13:30:23 +00001991#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001992 if( ssl->transform_out != NULL &&
1993 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001994 {
1995 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1996 {
1997 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1998 return( ret );
1999 }
2000
2001 len = ssl->out_msglen;
2002 }
2003#endif /*POLARSSL_ZLIB_SUPPORT */
2004
Paul Bakker05ef8352012-05-08 09:17:57 +00002005#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2006 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002008 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
Paul Bakker05ef8352012-05-08 09:17:57 +00002010 ret = ssl_hw_record_write( ssl );
2011 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2012 {
2013 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
2014 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2015 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002016
2017 if( ret == 0 )
2018 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002019 }
2020#endif
2021 if( !done )
2022 {
2023 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2024 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2025 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2027 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002028
Paul Bakker48916f92012-09-16 19:57:18 +00002029 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002030 {
2031 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2032 {
2033 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2034 return( ret );
2035 }
2036
2037 len = ssl->out_msglen;
2038 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2039 ssl->out_hdr[4] = (unsigned char)( len );
2040 }
2041
2042 ssl->out_left = 5 + ssl->out_msglen;
2043
2044 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2045 "version = [%d:%d], msglen = %d",
2046 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2047 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2048
2049 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002050 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 }
2052
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2054 {
2055 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2056 return( ret );
2057 }
2058
2059 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2060
2061 return( 0 );
2062}
2063
2064int ssl_read_record( ssl_context *ssl )
2065{
Paul Bakker05ef8352012-05-08 09:17:57 +00002066 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002067
2068 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2069
Paul Bakker68884e32013-01-07 18:20:04 +01002070 SSL_DEBUG_BUF( 4, "input record from network",
2071 ssl->in_hdr, 5 + ssl->in_msglen );
2072
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 if( ssl->in_hslen != 0 &&
2074 ssl->in_hslen < ssl->in_msglen )
2075 {
2076 /*
2077 * Get next Handshake message in the current record
2078 */
2079 ssl->in_msglen -= ssl->in_hslen;
2080
Paul Bakker8934a982011-08-05 11:11:53 +00002081 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002082 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
2084 ssl->in_hslen = 4;
2085 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2086
2087 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2088 " %d, type = %d, hslen = %d",
2089 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2090
2091 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2092 {
2093 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002094 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 }
2096
2097 if( ssl->in_msglen < ssl->in_hslen )
2098 {
2099 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002100 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
2102
Paul Bakker4224bc02014-04-08 14:36:50 +02002103 if( ssl->state != SSL_HANDSHAKE_OVER )
2104 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
2106 return( 0 );
2107 }
2108
2109 ssl->in_hslen = 0;
2110
2111 /*
2112 * Read the record header and validate it
2113 */
2114 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2115 {
2116 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2117 return( ret );
2118 }
2119
2120 ssl->in_msgtype = ssl->in_hdr[0];
2121 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2122
2123 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2124 "version = [%d:%d], msglen = %d",
2125 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2126 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2127
2128 if( ssl->in_hdr[1] != ssl->major_ver )
2129 {
2130 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002131 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
2133
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002134 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 {
2136 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002137 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 }
2139
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002140 /* Sanity check (outer boundaries) */
2141 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2142 {
2143 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2144 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2145 }
2146
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002148 * Make sure the message length is acceptable for the current transform
2149 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 */
Paul Bakker48916f92012-09-16 19:57:18 +00002151 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002153 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 {
2155 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002156 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 }
2158 }
2159 else
2160 {
Paul Bakker48916f92012-09-16 19:57:18 +00002161 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 {
2163 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002164 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 }
2166
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002167#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002169 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 {
2171 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002172 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002174#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002176#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2177 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 /*
2179 * TLS encrypted messages can have up to 256 bytes of padding
2180 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002181 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002182 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 {
2184 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002185 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002187#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 }
2189
2190 /*
2191 * Read and optionally decrypt the message contents
2192 */
2193 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2194 {
2195 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2196 return( ret );
2197 }
2198
2199 SSL_DEBUG_BUF( 4, "input record from network",
2200 ssl->in_hdr, 5 + ssl->in_msglen );
2201
Paul Bakker05ef8352012-05-08 09:17:57 +00002202#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2203 if( ssl_hw_record_read != NULL)
2204 {
2205 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2206
2207 ret = ssl_hw_record_read( ssl );
2208 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2209 {
2210 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2211 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2212 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002213
2214 if( ret == 0 )
2215 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002216 }
2217#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002218 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 {
2220 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2221 {
Paul Bakker40865c82013-01-31 17:13:13 +01002222#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2223 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2224 {
2225 ssl_send_alert_message( ssl,
2226 SSL_ALERT_LEVEL_FATAL,
2227 SSL_ALERT_MSG_BAD_RECORD_MAC );
2228 }
2229#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2231 return( ret );
2232 }
2233
2234 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2235 ssl->in_msg, ssl->in_msglen );
2236
2237 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2238 {
2239 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002240 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 }
2242 }
2243
Paul Bakker2770fbd2012-07-03 13:30:23 +00002244#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002245 if( ssl->transform_in != NULL &&
2246 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002247 {
2248 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2249 {
2250 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2251 return( ret );
2252 }
2253
2254 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2255 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2256 }
2257#endif /* POLARSSL_ZLIB_SUPPORT */
2258
Paul Bakker0a925182012-04-16 06:46:41 +00002259 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2260 ssl->in_msgtype != SSL_MSG_ALERT &&
2261 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2262 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2263 {
2264 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2265
Paul Bakker48916f92012-09-16 19:57:18 +00002266 if( ( ret = ssl_send_alert_message( ssl,
2267 SSL_ALERT_LEVEL_FATAL,
2268 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002269 {
2270 return( ret );
2271 }
2272
2273 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2274 }
2275
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2277 {
2278 ssl->in_hslen = 4;
2279 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2280
2281 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2282 " %d, type = %d, hslen = %d",
2283 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2284
2285 /*
2286 * Additional checks to validate the handshake header
2287 */
2288 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2289 {
2290 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002291 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 }
2293
2294 if( ssl->in_msglen < ssl->in_hslen )
2295 {
2296 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002297 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 }
2299
Paul Bakker48916f92012-09-16 19:57:18 +00002300 if( ssl->state != SSL_HANDSHAKE_OVER )
2301 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 }
2303
2304 if( ssl->in_msgtype == SSL_MSG_ALERT )
2305 {
2306 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2307 ssl->in_msg[0], ssl->in_msg[1] ) );
2308
2309 /*
2310 * Ignore non-fatal alerts, except close_notify
2311 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002312 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002314 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2315 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002316 /**
2317 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2318 * error identifier.
2319 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002320 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 }
2322
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002323 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2324 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 {
2326 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002327 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 }
2329 }
2330
2331 ssl->in_left = 0;
2332
2333 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2334
2335 return( 0 );
2336}
2337
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002338int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2339{
2340 int ret;
2341
2342 if( ( ret = ssl_send_alert_message( ssl,
2343 SSL_ALERT_LEVEL_FATAL,
2344 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2345 {
2346 return( ret );
2347 }
2348
2349 return( 0 );
2350}
2351
Paul Bakker0a925182012-04-16 06:46:41 +00002352int ssl_send_alert_message( ssl_context *ssl,
2353 unsigned char level,
2354 unsigned char message )
2355{
2356 int ret;
2357
2358 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2359
2360 ssl->out_msgtype = SSL_MSG_ALERT;
2361 ssl->out_msglen = 2;
2362 ssl->out_msg[0] = level;
2363 ssl->out_msg[1] = message;
2364
2365 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2366 {
2367 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2368 return( ret );
2369 }
2370
2371 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2372
2373 return( 0 );
2374}
2375
Paul Bakker5121ce52009-01-03 21:22:43 +00002376/*
2377 * Handshake functions
2378 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002379#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2380 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2381 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2382 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2383 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2384 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2385 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002386int ssl_write_certificate( ssl_context *ssl )
2387{
Paul Bakkered27a042013-04-18 22:46:23 +02002388 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002389 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002390
2391 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2392
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002393 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002394 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2395 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002396 {
2397 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2398 ssl->state++;
2399 return( 0 );
2400 }
2401
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002402 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002403 return( ret );
2404}
2405
2406int ssl_parse_certificate( ssl_context *ssl )
2407{
2408 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2409 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2410
2411 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2412
2413 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002414 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2415 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002416 {
2417 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2418 ssl->state++;
2419 return( 0 );
2420 }
2421
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002422 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002423 return( ret );
2424}
2425#else
2426int ssl_write_certificate( ssl_context *ssl )
2427{
2428 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2429 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002430 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002431 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2432
2433 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2434
2435 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002436 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2437 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002438 {
2439 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2440 ssl->state++;
2441 return( 0 );
2442 }
2443
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 if( ssl->endpoint == SSL_IS_CLIENT )
2445 {
2446 if( ssl->client_auth == 0 )
2447 {
2448 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2449 ssl->state++;
2450 return( 0 );
2451 }
2452
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002453#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 /*
2455 * If using SSLv3 and got no cert, send an Alert message
2456 * (otherwise an empty Certificate message will be sent).
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 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2460 {
2461 ssl->out_msglen = 2;
2462 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002463 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2464 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
2466 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2467 goto write_msg;
2468 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002469#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 }
2471 else /* SSL_IS_SERVER */
2472 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002473 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 {
2475 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002476 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 }
2478 }
2479
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002480 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
2482 /*
2483 * 0 . 0 handshake type
2484 * 1 . 3 handshake length
2485 * 4 . 6 length of all certs
2486 * 7 . 9 length of cert. 1
2487 * 10 . n-1 peer certificate
2488 * n . n+2 length of cert. 2
2489 * n+3 . ... upper level cert, etc.
2490 */
2491 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002492 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
Paul Bakker29087132010-03-21 21:03:34 +00002494 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 {
2496 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002497 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 {
2499 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2500 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002501 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 }
2503
2504 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2505 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2506 ssl->out_msg[i + 2] = (unsigned char)( n );
2507
2508 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2509 i += n; crt = crt->next;
2510 }
2511
2512 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2513 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2514 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2515
2516 ssl->out_msglen = i;
2517 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2518 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2519
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002520#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002521write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002522#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
2524 ssl->state++;
2525
2526 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2527 {
2528 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2529 return( ret );
2530 }
2531
2532 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2533
Paul Bakkered27a042013-04-18 22:46:23 +02002534 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535}
2536
2537int ssl_parse_certificate( ssl_context *ssl )
2538{
Paul Bakkered27a042013-04-18 22:46:23 +02002539 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002540 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002541 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
2543 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2544
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002545 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002546 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2547 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002548 {
2549 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2550 ssl->state++;
2551 return( 0 );
2552 }
2553
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002555 ( ssl->authmode == SSL_VERIFY_NONE ||
2556 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002558 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2560 ssl->state++;
2561 return( 0 );
2562 }
2563
2564 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2565 {
2566 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2567 return( ret );
2568 }
2569
2570 ssl->state++;
2571
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002572#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 /*
2574 * Check if the client sent an empty certificate
2575 */
2576 if( ssl->endpoint == SSL_IS_SERVER &&
2577 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2578 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002579 if( ssl->in_msglen == 2 &&
2580 ssl->in_msgtype == SSL_MSG_ALERT &&
2581 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2582 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 {
2584 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2585
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002586 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2588 return( 0 );
2589 else
Paul Bakker40e46942009-01-03 21:51:57 +00002590 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 }
2592 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002593#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002595#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2596 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 if( ssl->endpoint == SSL_IS_SERVER &&
2598 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2599 {
2600 if( ssl->in_hslen == 7 &&
2601 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2602 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2603 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2604 {
2605 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2606
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002607 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002609 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 else
2611 return( 0 );
2612 }
2613 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002614#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2615 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
2617 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2618 {
2619 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002620 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 }
2622
2623 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2624 {
2625 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002626 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 }
2628
2629 /*
2630 * Same message structure as in ssl_write_certificate()
2631 */
2632 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2633
2634 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2635 {
2636 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002637 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 }
2639
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002640 /* In case we tried to reuse a session but it failed */
2641 if( ssl->session_negotiate->peer_cert != NULL )
2642 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002643 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002644 polarssl_free( ssl->session_negotiate->peer_cert );
2645 }
2646
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002647 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2648 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 {
2650 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002651 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002652 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 }
2654
Paul Bakkerb6b09562013-09-18 14:17:41 +02002655 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
2657 i = 7;
2658
2659 while( i < ssl->in_hslen )
2660 {
2661 if( ssl->in_msg[i] != 0 )
2662 {
2663 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002664 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 }
2666
2667 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2668 | (unsigned int) ssl->in_msg[i + 2];
2669 i += 3;
2670
2671 if( n < 128 || i + n > ssl->in_hslen )
2672 {
2673 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002674 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 }
2676
Paul Bakkerddf26b42013-09-18 13:46:23 +02002677 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2678 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 if( ret != 0 )
2680 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002681 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 return( ret );
2683 }
2684
2685 i += n;
2686 }
2687
Paul Bakker48916f92012-09-16 19:57:18 +00002688 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002690 /*
2691 * On client, make sure the server cert doesn't change during renego to
2692 * avoid "triple handshake" attack: https://secure-resumption.com/
2693 */
2694 if( ssl->endpoint == SSL_IS_CLIENT &&
2695 ssl->renegotiation == SSL_RENEGOTIATION )
2696 {
2697 if( ssl->session->peer_cert == NULL )
2698 {
2699 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2700 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2701 }
2702
2703 if( ssl->session->peer_cert->raw.len !=
2704 ssl->session_negotiate->peer_cert->raw.len ||
2705 memcmp( ssl->session->peer_cert->raw.p,
2706 ssl->session_negotiate->peer_cert->raw.p,
2707 ssl->session->peer_cert->raw.len ) != 0 )
2708 {
2709 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2710 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2711 }
2712 }
2713
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 if( ssl->authmode != SSL_VERIFY_NONE )
2715 {
2716 if( ssl->ca_chain == NULL )
2717 {
2718 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002719 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002722 /*
2723 * Main check: verify certificate
2724 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002725 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2726 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2727 &ssl->session_negotiate->verify_result,
2728 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
2730 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002731 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002733 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002734
2735 /*
2736 * Secondary checks: always done, but change 'ret' only if it was 0
2737 */
2738
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002739#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002740 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002741 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002742
2743 /* If certificate uses an EC key, make sure the curve is OK */
2744 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2745 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2746 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002747 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002748 if( ret == 0 )
2749 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002750 }
2751 }
2752#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002753
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002754 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2755 ciphersuite_info,
2756 ! ssl->endpoint ) != 0 )
2757 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002758 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002759 if( ret == 0 )
2760 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2761 }
2762
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2764 ret = 0;
2765 }
2766
2767 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2768
2769 return( ret );
2770}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002771#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2772 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2773 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2774 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2775 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2776 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2777 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
2779int ssl_write_change_cipher_spec( ssl_context *ssl )
2780{
2781 int ret;
2782
2783 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2784
2785 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2786 ssl->out_msglen = 1;
2787 ssl->out_msg[0] = 1;
2788
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 ssl->state++;
2790
2791 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2792 {
2793 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2794 return( ret );
2795 }
2796
2797 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2798
2799 return( 0 );
2800}
2801
2802int ssl_parse_change_cipher_spec( ssl_context *ssl )
2803{
2804 int ret;
2805
2806 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2807
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2809 {
2810 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2811 return( ret );
2812 }
2813
2814 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2815 {
2816 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002817 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 }
2819
2820 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2821 {
2822 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002823 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 }
2825
2826 ssl->state++;
2827
2828 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2829
2830 return( 0 );
2831}
2832
Paul Bakker41c83d32013-03-20 14:39:14 +01002833void ssl_optimize_checksum( ssl_context *ssl,
2834 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002835{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002836 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002837
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002838#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2839 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002840 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002841 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002842 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002843#endif
2844#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2845#if defined(POLARSSL_SHA512_C)
2846 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2847 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2848 else
2849#endif
2850#if defined(POLARSSL_SHA256_C)
2851 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002852 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002853 else
2854#endif
2855#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2856 /* Should never happen */
2857 return;
Paul Bakker380da532012-04-18 16:10:25 +00002858}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002859
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002860static void ssl_update_checksum_start( ssl_context *ssl,
2861 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002862{
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 Bakker48916f92012-09-16 19:57:18 +00002865 md5_update( &ssl->handshake->fin_md5 , buf, len );
2866 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002867#endif
2868#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2869#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002870 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002871#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002872#if defined(POLARSSL_SHA512_C)
2873 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002874#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002875#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002876}
2877
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002878#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2879 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002880static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2881 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002882{
Paul Bakker48916f92012-09-16 19:57:18 +00002883 md5_update( &ssl->handshake->fin_md5 , buf, len );
2884 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002885}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002886#endif
Paul Bakker380da532012-04-18 16:10:25 +00002887
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002888#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2889#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002890static void ssl_update_checksum_sha256( ssl_context *ssl,
2891 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002892{
Paul Bakker9e36f042013-06-30 14:34:05 +02002893 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002894}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002895#endif
Paul Bakker380da532012-04-18 16:10:25 +00002896
Paul Bakker9e36f042013-06-30 14:34:05 +02002897#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002898static void ssl_update_checksum_sha384( ssl_context *ssl,
2899 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002900{
Paul Bakker9e36f042013-06-30 14:34:05 +02002901 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002902}
Paul Bakker769075d2012-11-24 11:26:46 +01002903#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002904#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002905
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002906#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907static void ssl_calc_finished_ssl(
2908 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002909{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002910 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911 md5_context md5;
2912 sha1_context sha1;
2913
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 unsigned char padbuf[48];
2915 unsigned char md5sum[16];
2916 unsigned char sha1sum[20];
2917
Paul Bakker48916f92012-09-16 19:57:18 +00002918 ssl_session *session = ssl->session_negotiate;
2919 if( !session )
2920 session = ssl->session;
2921
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2923
Paul Bakker48916f92012-09-16 19:57:18 +00002924 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2925 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
2927 /*
2928 * SSLv3:
2929 * hash =
2930 * MD5( master + pad2 +
2931 * MD5( handshake + sender + master + pad1 ) )
2932 * + SHA1( master + pad2 +
2933 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002934 */
2935
Paul Bakker90995b52013-06-24 19:20:35 +02002936#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002937 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002938 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002939#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Paul Bakker90995b52013-06-24 19:20:35 +02002941#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002944#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
Paul Bakker3c2122f2013-06-24 19:03:14 +02002946 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2947 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
Paul Bakker3c2122f2013-06-24 19:03:14 +02002951 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002952 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953 md5_update( &md5, padbuf, 48 );
2954 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Paul Bakker3c2122f2013-06-24 19:03:14 +02002956 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002957 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958 sha1_update( &sha1, padbuf, 40 );
2959 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
Paul Bakker1ef83d62012-04-11 12:09:53 +00002961 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002964 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965 md5_update( &md5, padbuf, 48 );
2966 md5_update( &md5, md5sum, 16 );
2967 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002970 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002971 sha1_update( &sha1, padbuf , 40 );
2972 sha1_update( &sha1, sha1sum, 20 );
2973 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Paul Bakker1ef83d62012-04-11 12:09:53 +00002975 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002976
Paul Bakker1ef83d62012-04-11 12:09:53 +00002977 memset( &md5, 0, sizeof( md5_context ) );
2978 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
2980 memset( padbuf, 0, sizeof( padbuf ) );
2981 memset( md5sum, 0, sizeof( md5sum ) );
2982 memset( sha1sum, 0, sizeof( sha1sum ) );
2983
2984 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2985}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002986#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002988#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989static void ssl_calc_finished_tls(
2990 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002991{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002992 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002993 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002996 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Paul Bakker48916f92012-09-16 19:57:18 +00002998 ssl_session *session = ssl->session_negotiate;
2999 if( !session )
3000 session = ssl->session;
3001
Paul Bakker1ef83d62012-04-11 12:09:53 +00003002 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
Paul Bakker48916f92012-09-16 19:57:18 +00003004 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3005 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Paul Bakker1ef83d62012-04-11 12:09:53 +00003007 /*
3008 * TLSv1:
3009 * hash = PRF( master, finished_label,
3010 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3011 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
Paul Bakker90995b52013-06-24 19:20:35 +02003013#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003014 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3015 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003016#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017
Paul Bakker90995b52013-06-24 19:20:35 +02003018#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003019 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3020 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003021#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003022
3023 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003024 ? "client finished"
3025 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003026
3027 md5_finish( &md5, padbuf );
3028 sha1_finish( &sha1, padbuf + 16 );
3029
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003030 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003031 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032
3033 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3034
3035 memset( &md5, 0, sizeof( md5_context ) );
3036 memset( &sha1, 0, sizeof( sha1_context ) );
3037
3038 memset( padbuf, 0, sizeof( padbuf ) );
3039
3040 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3041}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003042#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003043
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003044#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3045#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003046static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003047 ssl_context *ssl, unsigned char *buf, int from )
3048{
3049 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003050 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003051 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003052 unsigned char padbuf[32];
3053
Paul Bakker48916f92012-09-16 19:57:18 +00003054 ssl_session *session = ssl->session_negotiate;
3055 if( !session )
3056 session = ssl->session;
3057
Paul Bakker380da532012-04-18 16:10:25 +00003058 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003059
Paul Bakker9e36f042013-06-30 14:34:05 +02003060 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003061
3062 /*
3063 * TLSv1.2:
3064 * hash = PRF( master, finished_label,
3065 * Hash( handshake ) )[0.11]
3066 */
3067
Paul Bakker9e36f042013-06-30 14:34:05 +02003068#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003069 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003070 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003071#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072
3073 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003074 ? "client finished"
3075 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076
Paul Bakker9e36f042013-06-30 14:34:05 +02003077 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003079 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003080 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081
3082 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3083
Paul Bakker9e36f042013-06-30 14:34:05 +02003084 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085
3086 memset( padbuf, 0, sizeof( padbuf ) );
3087
3088 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3089}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003090#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003091
Paul Bakker9e36f042013-06-30 14:34:05 +02003092#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003093static void ssl_calc_finished_tls_sha384(
3094 ssl_context *ssl, unsigned char *buf, int from )
3095{
3096 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003097 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003098 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003099 unsigned char padbuf[48];
3100
Paul Bakker48916f92012-09-16 19:57:18 +00003101 ssl_session *session = ssl->session_negotiate;
3102 if( !session )
3103 session = ssl->session;
3104
Paul Bakker380da532012-04-18 16:10:25 +00003105 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003106
Paul Bakker9e36f042013-06-30 14:34:05 +02003107 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003108
3109 /*
3110 * TLSv1.2:
3111 * hash = PRF( master, finished_label,
3112 * Hash( handshake ) )[0.11]
3113 */
3114
Paul Bakker9e36f042013-06-30 14:34:05 +02003115#if !defined(POLARSSL_SHA512_ALT)
3116 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3117 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003118#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003119
3120 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003121 ? "client finished"
3122 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003123
Paul Bakker9e36f042013-06-30 14:34:05 +02003124 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003125
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003126 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003127 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003128
3129 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3130
Paul Bakker9e36f042013-06-30 14:34:05 +02003131 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003132
3133 memset( padbuf, 0, sizeof( padbuf ) );
3134
3135 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3136}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003137#endif /* POLARSSL_SHA512_C */
3138#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003139
Paul Bakker48916f92012-09-16 19:57:18 +00003140void ssl_handshake_wrapup( ssl_context *ssl )
3141{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003142 int resume = ssl->handshake->resume;
3143
Paul Bakker48916f92012-09-16 19:57:18 +00003144 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3145
3146 /*
3147 * Free our handshake params
3148 */
3149 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003150 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003151 ssl->handshake = NULL;
3152
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003153 if( ssl->renegotiation == SSL_RENEGOTIATION )
3154 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3155
Paul Bakker48916f92012-09-16 19:57:18 +00003156 /*
3157 * Switch in our now active transform context
3158 */
3159 if( ssl->transform )
3160 {
3161 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003162 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003163 }
3164 ssl->transform = ssl->transform_negotiate;
3165 ssl->transform_negotiate = NULL;
3166
Paul Bakker0a597072012-09-25 21:55:46 +00003167 if( ssl->session )
3168 {
3169 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003170 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003171 }
3172 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003173 ssl->session_negotiate = NULL;
3174
Paul Bakker0a597072012-09-25 21:55:46 +00003175 /*
3176 * Add cache entry
3177 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003178 if( ssl->f_set_cache != NULL &&
3179 ssl->session->length != 0 &&
3180 resume == 0 )
3181 {
Paul Bakker0a597072012-09-25 21:55:46 +00003182 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3183 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003184 }
Paul Bakker0a597072012-09-25 21:55:46 +00003185
Paul Bakker48916f92012-09-16 19:57:18 +00003186 ssl->state++;
3187
3188 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3189}
3190
Paul Bakker1ef83d62012-04-11 12:09:53 +00003191int ssl_write_finished( ssl_context *ssl )
3192{
3193 int ret, hash_len;
3194
3195 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3196
Paul Bakker92be97b2013-01-02 17:30:03 +01003197 /*
3198 * Set the out_msg pointer to the correct location based on IV length
3199 */
3200 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3201 {
3202 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3203 ssl->transform_negotiate->fixed_ivlen;
3204 }
3205 else
3206 ssl->out_msg = ssl->out_iv;
3207
Paul Bakker48916f92012-09-16 19:57:18 +00003208 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003209
3210 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3212
Paul Bakker48916f92012-09-16 19:57:18 +00003213 ssl->verify_data_len = hash_len;
3214 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3215
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 ssl->out_msglen = 4 + hash_len;
3217 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3218 ssl->out_msg[0] = SSL_HS_FINISHED;
3219
3220 /*
3221 * In case of session resuming, invert the client and server
3222 * ChangeCipherSpec messages order.
3223 */
Paul Bakker0a597072012-09-25 21:55:46 +00003224 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 {
3226 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003227 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 else
3229 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3230 }
3231 else
3232 ssl->state++;
3233
Paul Bakker48916f92012-09-16 19:57:18 +00003234 /*
3235 * Switch to our negotiated transform and session parameters for outbound data.
3236 */
3237 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3238 ssl->transform_out = ssl->transform_negotiate;
3239 ssl->session_out = ssl->session_negotiate;
3240 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003241
Paul Bakker07eb38b2012-12-19 14:42:06 +01003242#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3243 if( ssl_hw_record_activate != NULL)
3244 {
3245 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3246 {
3247 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3248 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3249 }
3250 }
3251#endif
3252
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3254 {
3255 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3256 return( ret );
3257 }
3258
3259 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3260
3261 return( 0 );
3262}
3263
3264int ssl_parse_finished( ssl_context *ssl )
3265{
Paul Bakker23986e52011-04-24 08:57:21 +00003266 int ret;
3267 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 unsigned char buf[36];
3269
3270 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3271
Paul Bakker48916f92012-09-16 19:57:18 +00003272 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
Paul Bakker48916f92012-09-16 19:57:18 +00003274 /*
3275 * Switch to our negotiated transform and session parameters for inbound data.
3276 */
3277 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3278 ssl->transform_in = ssl->transform_negotiate;
3279 ssl->session_in = ssl->session_negotiate;
3280 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003281
Paul Bakker92be97b2013-01-02 17:30:03 +01003282 /*
3283 * Set the in_msg pointer to the correct location based on IV length
3284 */
3285 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3286 {
3287 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3288 ssl->transform_negotiate->fixed_ivlen;
3289 }
3290 else
3291 ssl->in_msg = ssl->in_iv;
3292
Paul Bakker07eb38b2012-12-19 14:42:06 +01003293#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3294 if( ssl_hw_record_activate != NULL)
3295 {
3296 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3297 {
3298 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3299 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3300 }
3301 }
3302#endif
3303
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3305 {
3306 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3307 return( ret );
3308 }
3309
3310 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3311 {
3312 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003313 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 }
3315
Paul Bakker1ef83d62012-04-11 12:09:53 +00003316 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3318
3319 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3320 ssl->in_hslen != 4 + hash_len )
3321 {
3322 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003323 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003324 }
3325
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003326 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003327 {
3328 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003329 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 }
3331
Paul Bakker48916f92012-09-16 19:57:18 +00003332 ssl->verify_data_len = hash_len;
3333 memcpy( ssl->peer_verify_data, buf, hash_len );
3334
Paul Bakker0a597072012-09-25 21:55:46 +00003335 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 {
3337 if( ssl->endpoint == SSL_IS_CLIENT )
3338 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3339
3340 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003341 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 }
3343 else
3344 ssl->state++;
3345
3346 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3347
3348 return( 0 );
3349}
3350
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003351static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003352{
3353 if( ssl->transform_negotiate )
3354 ssl_transform_free( ssl->transform_negotiate );
3355 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003356 {
3357 ssl->transform_negotiate =
3358 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003359
3360 if( ssl->transform_negotiate != NULL )
3361 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003362 }
Paul Bakker48916f92012-09-16 19:57:18 +00003363
3364 if( ssl->session_negotiate )
3365 ssl_session_free( ssl->session_negotiate );
3366 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003367 {
3368 ssl->session_negotiate =
3369 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003370
3371 if( ssl->session_negotiate != NULL )
3372 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003373 }
Paul Bakker48916f92012-09-16 19:57:18 +00003374
3375 if( ssl->handshake )
3376 ssl_handshake_free( ssl->handshake );
3377 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003378 {
3379 ssl->handshake = (ssl_handshake_params *)
3380 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003381
3382 if( ssl->handshake != NULL )
3383 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003384 }
Paul Bakker48916f92012-09-16 19:57:18 +00003385
3386 if( ssl->handshake == NULL ||
3387 ssl->transform_negotiate == NULL ||
3388 ssl->session_negotiate == NULL )
3389 {
3390 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3391 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3392 }
3393
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003394#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3395 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003396 md5_starts( &ssl->handshake->fin_md5 );
3397 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003398#endif
3399#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3400#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003401 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003402#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003403#if defined(POLARSSL_SHA512_C)
3404 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003405#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003406#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003407
3408 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003409 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003410
Paul Bakker61d113b2013-07-04 11:51:43 +02003411#if defined(POLARSSL_ECDH_C)
3412 ecdh_init( &ssl->handshake->ecdh_ctx );
3413#endif
3414
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003415#if defined(POLARSSL_X509_CRT_PARSE_C)
3416 ssl->handshake->key_cert = ssl->key_cert;
3417#endif
3418
Paul Bakker48916f92012-09-16 19:57:18 +00003419 return( 0 );
3420}
3421
Paul Bakker5121ce52009-01-03 21:22:43 +00003422/*
3423 * Initialize an SSL context
3424 */
3425int ssl_init( ssl_context *ssl )
3426{
Paul Bakker48916f92012-09-16 19:57:18 +00003427 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 int len = SSL_BUFFER_LEN;
3429
3430 memset( ssl, 0, sizeof( ssl_context ) );
3431
Paul Bakker62f2dee2012-09-28 07:31:51 +00003432 /*
3433 * Sane defaults
3434 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003435 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3436 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3437 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3438 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003439
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003440 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003441
Paul Bakker62f2dee2012-09-28 07:31:51 +00003442#if defined(POLARSSL_DHM_C)
3443 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3444 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3445 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3446 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3447 {
3448 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3449 return( ret );
3450 }
3451#endif
3452
3453 /*
3454 * Prepare base structures
3455 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003456 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003458 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 ssl->in_msg = ssl->in_ctr + 13;
3460
3461 if( ssl->in_ctr == NULL )
3462 {
3463 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003464 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 }
3466
Paul Bakker6e339b52013-07-03 13:37:05 +02003467 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003468 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003469 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003470 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003471
3472 if( ssl->out_ctr == NULL )
3473 {
3474 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003475 polarssl_free( ssl->in_ctr );
3476 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003477 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003478 }
3479
3480 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3481 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3482
Paul Bakker606b4ba2013-08-14 16:52:14 +02003483#if defined(POLARSSL_SSL_SESSION_TICKETS)
3484 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3485#endif
3486
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003487#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003488 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003489#endif
3490
Paul Bakker48916f92012-09-16 19:57:18 +00003491 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3492 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003493
3494 return( 0 );
3495}
3496
3497/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003498 * Reset an initialized and used SSL context for re-use while retaining
3499 * all application-set variables, function pointers and data.
3500 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003501int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003502{
Paul Bakker48916f92012-09-16 19:57:18 +00003503 int ret;
3504
Paul Bakker7eb013f2011-10-06 12:37:39 +00003505 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003506 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3507 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3508
3509 ssl->verify_data_len = 0;
3510 memset( ssl->own_verify_data, 0, 36 );
3511 memset( ssl->peer_verify_data, 0, 36 );
3512
Paul Bakker7eb013f2011-10-06 12:37:39 +00003513 ssl->in_offt = NULL;
3514
Paul Bakker92be97b2013-01-02 17:30:03 +01003515 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003516 ssl->in_msgtype = 0;
3517 ssl->in_msglen = 0;
3518 ssl->in_left = 0;
3519
3520 ssl->in_hslen = 0;
3521 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003522 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003523
Paul Bakker92be97b2013-01-02 17:30:03 +01003524 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003525 ssl->out_msgtype = 0;
3526 ssl->out_msglen = 0;
3527 ssl->out_left = 0;
3528
Paul Bakker48916f92012-09-16 19:57:18 +00003529 ssl->transform_in = NULL;
3530 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003531
3532 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3533 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003534
3535#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3536 if( ssl_hw_record_reset != NULL)
3537 {
3538 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003539 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003540 {
3541 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3542 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3543 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003544 }
3545#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003546
Paul Bakker48916f92012-09-16 19:57:18 +00003547 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003548 {
Paul Bakker48916f92012-09-16 19:57:18 +00003549 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003550 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003551 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003552 }
Paul Bakker48916f92012-09-16 19:57:18 +00003553
Paul Bakkerc0463502013-02-14 11:19:38 +01003554 if( ssl->session )
3555 {
3556 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003557 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003558 ssl->session = NULL;
3559 }
3560
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003561#if defined(POLARSSL_SSL_ALPN)
3562 ssl->alpn_chosen = NULL;
3563#endif
3564
Paul Bakker48916f92012-09-16 19:57:18 +00003565 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3566 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003567
3568 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003569}
3570
Paul Bakkera503a632013-08-14 13:48:06 +02003571#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003572/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003573 * Allocate and initialize ticket keys
3574 */
3575static int ssl_ticket_keys_init( ssl_context *ssl )
3576{
3577 int ret;
3578 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003579 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003580
3581 if( ssl->ticket_keys != NULL )
3582 return( 0 );
3583
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003584 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3585 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003586 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3587
3588 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003589 {
3590 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003591 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003592 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003593
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003594 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3595 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3596 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3597 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003598 polarssl_free( tkeys );
3599 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003600 }
3601
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003602 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003603 {
3604 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003605 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003606 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003607
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003608 ssl->ticket_keys = tkeys;
3609
3610 return( 0 );
3611}
Paul Bakkera503a632013-08-14 13:48:06 +02003612#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003613
3614/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003615 * SSL set accessors
3616 */
3617void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3618{
3619 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003620
Paul Bakker606b4ba2013-08-14 16:52:14 +02003621#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003622 if( endpoint == SSL_IS_CLIENT )
3623 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003624#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003625}
3626
3627void ssl_set_authmode( ssl_context *ssl, int authmode )
3628{
3629 ssl->authmode = authmode;
3630}
3631
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003632#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003633void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003634 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003635 void *p_vrfy )
3636{
3637 ssl->f_vrfy = f_vrfy;
3638 ssl->p_vrfy = p_vrfy;
3639}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003640#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003641
Paul Bakker5121ce52009-01-03 21:22:43 +00003642void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003643 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003644 void *p_rng )
3645{
3646 ssl->f_rng = f_rng;
3647 ssl->p_rng = p_rng;
3648}
3649
3650void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003651 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 void *p_dbg )
3653{
3654 ssl->f_dbg = f_dbg;
3655 ssl->p_dbg = p_dbg;
3656}
3657
3658void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003659 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003660 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003661{
3662 ssl->f_recv = f_recv;
3663 ssl->f_send = f_send;
3664 ssl->p_recv = p_recv;
3665 ssl->p_send = p_send;
3666}
3667
Paul Bakker0a597072012-09-25 21:55:46 +00003668void ssl_set_session_cache( ssl_context *ssl,
3669 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3670 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003671{
Paul Bakker0a597072012-09-25 21:55:46 +00003672 ssl->f_get_cache = f_get_cache;
3673 ssl->p_get_cache = p_get_cache;
3674 ssl->f_set_cache = f_set_cache;
3675 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003676}
3677
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003678int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003679{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003680 int ret;
3681
3682 if( ssl == NULL ||
3683 session == NULL ||
3684 ssl->session_negotiate == NULL ||
3685 ssl->endpoint != SSL_IS_CLIENT )
3686 {
3687 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3688 }
3689
3690 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3691 return( ret );
3692
Paul Bakker0a597072012-09-25 21:55:46 +00003693 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003694
3695 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003696}
3697
Paul Bakkerb68cad62012-08-23 08:34:18 +00003698void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003699{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003700 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3701 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3702 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3703 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3704}
3705
3706void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3707 int major, int minor )
3708{
3709 if( major != SSL_MAJOR_VERSION_3 )
3710 return;
3711
3712 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3713 return;
3714
3715 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003716}
3717
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003718#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003719/* Add a new (empty) key_cert entry an return a pointer to it */
3720static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3721{
3722 ssl_key_cert *key_cert, *last;
3723
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003724 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3725 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003726 return( NULL );
3727
3728 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3729
3730 /* Append the new key_cert to the (possibly empty) current list */
3731 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003732 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003733 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003734 if( ssl->handshake != NULL )
3735 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003736 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003737 else
3738 {
3739 last = ssl->key_cert;
3740 while( last->next != NULL )
3741 last = last->next;
3742 last->next = key_cert;
3743 }
3744
3745 return key_cert;
3746}
3747
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003748void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003749 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003750{
3751 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003752 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003753 ssl->peer_cn = peer_cn;
3754}
3755
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003756int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003757 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003758{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003759 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3760
3761 if( key_cert == NULL )
3762 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3763
3764 key_cert->cert = own_cert;
3765 key_cert->key = pk_key;
3766
3767 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003768}
3769
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003770#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003771int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003772 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003773{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003774 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003775 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003776
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003777 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003778 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3779
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003780 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3781 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003782 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003783
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003784 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003785
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003786 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003787 if( ret != 0 )
3788 return( ret );
3789
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003790 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003791 return( ret );
3792
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003793 key_cert->cert = own_cert;
3794 key_cert->key_own_alloc = 1;
3795
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003796 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003797}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003798#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003799
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003800int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003801 void *rsa_key,
3802 rsa_decrypt_func rsa_decrypt,
3803 rsa_sign_func rsa_sign,
3804 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003805{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003806 int ret;
3807 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003808
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003809 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003810 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3811
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003812 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3813 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003814 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003815
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003816 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003817
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003818 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3819 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3820 return( ret );
3821
3822 key_cert->cert = own_cert;
3823 key_cert->key_own_alloc = 1;
3824
3825 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003826}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003827#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003828
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003829#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003830int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3831 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003832{
Paul Bakker6db455e2013-09-18 17:29:31 +02003833 if( psk == NULL || psk_identity == NULL )
3834 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3835
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003836 /*
3837 * The length will be check later anyway, but in case it is obviously
3838 * too large, better abort now. The PMS is as follows:
3839 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3840 */
3841 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3842 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3843
Paul Bakker6db455e2013-09-18 17:29:31 +02003844 if( ssl->psk != NULL )
3845 {
3846 polarssl_free( ssl->psk );
3847 polarssl_free( ssl->psk_identity );
3848 }
3849
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003850 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003851 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003852
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003853 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3854 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003855
3856 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3857 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3858
3859 memcpy( ssl->psk, psk, ssl->psk_len );
3860 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003861
3862 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003863}
3864
3865void ssl_set_psk_cb( ssl_context *ssl,
3866 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3867 size_t),
3868 void *p_psk )
3869{
3870 ssl->f_psk = f_psk;
3871 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003872}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003873#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003874
Paul Bakker48916f92012-09-16 19:57:18 +00003875#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003876int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003877{
3878 int ret;
3879
Paul Bakker48916f92012-09-16 19:57:18 +00003880 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003881 {
3882 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3883 return( ret );
3884 }
3885
Paul Bakker48916f92012-09-16 19:57:18 +00003886 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003887 {
3888 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3889 return( ret );
3890 }
3891
3892 return( 0 );
3893}
3894
Paul Bakker1b57b062011-01-06 15:48:19 +00003895int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3896{
3897 int ret;
3898
Paul Bakker48916f92012-09-16 19:57:18 +00003899 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003900 {
3901 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3902 return( ret );
3903 }
3904
Paul Bakker48916f92012-09-16 19:57:18 +00003905 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003906 {
3907 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3908 return( ret );
3909 }
3910
3911 return( 0 );
3912}
Paul Bakker48916f92012-09-16 19:57:18 +00003913#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003914
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003915#if defined(POLARSSL_SSL_SET_CURVES)
3916/*
3917 * Set the allowed elliptic curves
3918 */
3919void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3920{
3921 ssl->curve_list = curve_list;
3922}
3923#endif
3924
Paul Bakker0be444a2013-08-27 21:55:01 +02003925#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003926int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003927{
3928 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003929 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003930
3931 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003932
3933 if( ssl->hostname_len + 1 == 0 )
3934 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3935
Paul Bakker6e339b52013-07-03 13:37:05 +02003936 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003937
Paul Bakkerb15b8512012-01-13 13:44:06 +00003938 if( ssl->hostname == NULL )
3939 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3940
Paul Bakker3c2122f2013-06-24 19:03:14 +02003941 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003943
Paul Bakker40ea7de2009-05-03 10:18:48 +00003944 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003945
3946 return( 0 );
3947}
3948
Paul Bakker5701cdc2012-09-27 21:49:42 +00003949void ssl_set_sni( ssl_context *ssl,
3950 int (*f_sni)(void *, ssl_context *,
3951 const unsigned char *, size_t),
3952 void *p_sni )
3953{
3954 ssl->f_sni = f_sni;
3955 ssl->p_sni = p_sni;
3956}
Paul Bakker0be444a2013-08-27 21:55:01 +02003957#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003958
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003959#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003960int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003961{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003962 size_t cur_len, tot_len;
3963 const char **p;
3964
3965 /*
3966 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3967 * truncated". Check lengths now rather than later.
3968 */
3969 tot_len = 0;
3970 for( p = protos; *p != NULL; p++ )
3971 {
3972 cur_len = strlen( *p );
3973 tot_len += cur_len;
3974
3975 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3976 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3977 }
3978
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003979 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003980
3981 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003982}
3983
3984const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3985{
3986 return ssl->alpn_chosen;
3987}
3988#endif /* POLARSSL_SSL_ALPN */
3989
Paul Bakker490ecc82011-10-06 13:04:09 +00003990void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3991{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003992 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3993 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3994 {
3995 ssl->max_major_ver = major;
3996 ssl->max_minor_ver = minor;
3997 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003998}
3999
Paul Bakker1d29fb52012-09-28 13:28:45 +00004000void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4001{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004002 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4003 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4004 {
4005 ssl->min_major_ver = major;
4006 ssl->min_minor_ver = minor;
4007 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004008}
4009
Paul Bakker05decb22013-08-15 13:33:48 +02004010#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004011int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4012{
Paul Bakker77e257e2013-12-16 15:29:52 +01004013 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004014 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004015 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004016 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004017 }
4018
4019 ssl->mfl_code = mfl_code;
4020
4021 return( 0 );
4022}
Paul Bakker05decb22013-08-15 13:33:48 +02004023#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004024
Paul Bakker1f2bc622013-08-15 13:45:55 +02004025#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004026int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004027{
4028 if( ssl->endpoint != SSL_IS_CLIENT )
4029 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4030
Paul Bakker8c1ede62013-07-19 14:14:37 +02004031 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004032
4033 return( 0 );
4034}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004035#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004036
Paul Bakker48916f92012-09-16 19:57:18 +00004037void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4038{
4039 ssl->disable_renegotiation = renegotiation;
4040}
4041
4042void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4043{
4044 ssl->allow_legacy_renegotiation = allow_legacy;
4045}
4046
Paul Bakkera503a632013-08-14 13:48:06 +02004047#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004048int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4049{
4050 ssl->session_tickets = use_tickets;
4051
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004052 if( ssl->endpoint == SSL_IS_CLIENT )
4053 return( 0 );
4054
4055 if( ssl->f_rng == NULL )
4056 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4057
4058 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004059}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004060
4061void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4062{
4063 ssl->ticket_lifetime = lifetime;
4064}
Paul Bakkera503a632013-08-14 13:48:06 +02004065#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004066
Paul Bakker5121ce52009-01-03 21:22:43 +00004067/*
4068 * SSL get accessors
4069 */
Paul Bakker23986e52011-04-24 08:57:21 +00004070size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004071{
4072 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4073}
4074
Paul Bakkerff60ee62010-03-16 21:09:09 +00004075int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004076{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004077 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004078}
4079
Paul Bakkere3166ce2011-01-27 17:40:50 +00004080const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004081{
Paul Bakker926c8e42013-03-06 10:23:34 +01004082 if( ssl == NULL || ssl->session == NULL )
4083 return NULL;
4084
Paul Bakkere3166ce2011-01-27 17:40:50 +00004085 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004086}
4087
Paul Bakker43ca69c2011-01-15 17:35:19 +00004088const char *ssl_get_version( const ssl_context *ssl )
4089{
4090 switch( ssl->minor_ver )
4091 {
4092 case SSL_MINOR_VERSION_0:
4093 return( "SSLv3.0" );
4094
4095 case SSL_MINOR_VERSION_1:
4096 return( "TLSv1.0" );
4097
4098 case SSL_MINOR_VERSION_2:
4099 return( "TLSv1.1" );
4100
Paul Bakker1ef83d62012-04-11 12:09:53 +00004101 case SSL_MINOR_VERSION_3:
4102 return( "TLSv1.2" );
4103
Paul Bakker43ca69c2011-01-15 17:35:19 +00004104 default:
4105 break;
4106 }
4107 return( "unknown" );
4108}
4109
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004110#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004111const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004112{
4113 if( ssl == NULL || ssl->session == NULL )
4114 return NULL;
4115
4116 return ssl->session->peer_cert;
4117}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004118#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004119
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004120int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4121{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004122 if( ssl == NULL ||
4123 dst == NULL ||
4124 ssl->session == NULL ||
4125 ssl->endpoint != SSL_IS_CLIENT )
4126 {
4127 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4128 }
4129
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004130 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004131}
4132
Paul Bakker5121ce52009-01-03 21:22:43 +00004133/*
Paul Bakker1961b702013-01-25 14:49:24 +01004134 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004135 */
Paul Bakker1961b702013-01-25 14:49:24 +01004136int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004137{
Paul Bakker40e46942009-01-03 21:51:57 +00004138 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004139
Paul Bakker40e46942009-01-03 21:51:57 +00004140#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004141 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004142 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004143#endif
4144
Paul Bakker40e46942009-01-03 21:51:57 +00004145#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004146 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004147 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004148#endif
4149
Paul Bakker1961b702013-01-25 14:49:24 +01004150 return( ret );
4151}
4152
4153/*
4154 * Perform the SSL handshake
4155 */
4156int ssl_handshake( ssl_context *ssl )
4157{
4158 int ret = 0;
4159
4160 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4161
4162 while( ssl->state != SSL_HANDSHAKE_OVER )
4163 {
4164 ret = ssl_handshake_step( ssl );
4165
4166 if( ret != 0 )
4167 break;
4168 }
4169
Paul Bakker5121ce52009-01-03 21:22:43 +00004170 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4171
4172 return( ret );
4173}
4174
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004175#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004176/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004177 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004178 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004179static int ssl_write_hello_request( ssl_context *ssl )
4180{
4181 int ret;
4182
4183 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4184
4185 ssl->out_msglen = 4;
4186 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4187 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4188
4189 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4190 {
4191 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4192 return( ret );
4193 }
4194
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004195 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4196
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004197 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4198
4199 return( 0 );
4200}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004201#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004202
4203/*
4204 * Actually renegotiate current connection, triggered by either:
4205 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004206 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004207 * - receiving any handshake message on server during ssl_read() after the
4208 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004209 * If the handshake doesn't complete due to waiting for I/O, it will continue
4210 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004211 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004212static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004213{
4214 int ret;
4215
4216 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4217
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004218 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4219 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004220
4221 ssl->state = SSL_HELLO_REQUEST;
4222 ssl->renegotiation = SSL_RENEGOTIATION;
4223
Paul Bakker48916f92012-09-16 19:57:18 +00004224 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4225 {
4226 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4227 return( ret );
4228 }
4229
4230 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4231
4232 return( 0 );
4233}
4234
4235/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004236 * Renegotiate current connection on client,
4237 * or request renegotiation on server
4238 */
4239int ssl_renegotiate( ssl_context *ssl )
4240{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004241 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004242
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004243#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004244 /* On server, just send the request */
4245 if( ssl->endpoint == SSL_IS_SERVER )
4246 {
4247 if( ssl->state != SSL_HANDSHAKE_OVER )
4248 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4249
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004250 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004251 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004252#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004253
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004254#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004255 /*
4256 * On client, either start the renegotiation process or,
4257 * if already in progress, continue the handshake
4258 */
4259 if( ssl->renegotiation != SSL_RENEGOTIATION )
4260 {
4261 if( ssl->state != SSL_HANDSHAKE_OVER )
4262 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4263
4264 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4265 {
4266 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4267 return( ret );
4268 }
4269 }
4270 else
4271 {
4272 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4273 {
4274 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4275 return( ret );
4276 }
4277 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004278#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004279
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004280 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004281}
4282
4283/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004284 * Receive application data decrypted from the SSL layer
4285 */
Paul Bakker23986e52011-04-24 08:57:21 +00004286int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004287{
Paul Bakker23986e52011-04-24 08:57:21 +00004288 int ret;
4289 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004290
4291 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4292
4293 if( ssl->state != SSL_HANDSHAKE_OVER )
4294 {
4295 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4296 {
4297 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4298 return( ret );
4299 }
4300 }
4301
4302 if( ssl->in_offt == NULL )
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 if( ssl->in_msglen == 0 &&
4314 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4315 {
4316 /*
4317 * OpenSSL sends empty messages to randomize the IV
4318 */
4319 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4320 {
Paul Bakker831a7552011-05-18 13:32:51 +00004321 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4322 return( 0 );
4323
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4325 return( ret );
4326 }
4327 }
4328
Paul Bakker48916f92012-09-16 19:57:18 +00004329 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4330 {
4331 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4332
4333 if( ssl->endpoint == SSL_IS_CLIENT &&
4334 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4335 ssl->in_hslen != 4 ) )
4336 {
4337 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4338 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4339 }
4340
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004341 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4342 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4343 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004344 {
4345 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4346
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004347#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004348 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004349 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004350 /*
4351 * SSLv3 does not have a "no_renegotiation" alert
4352 */
4353 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4354 return( ret );
4355 }
4356 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004357#endif
4358#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4359 defined(POLARSSL_SSL_PROTO_TLS1_2)
4360 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004361 {
4362 if( ( ret = ssl_send_alert_message( ssl,
4363 SSL_ALERT_LEVEL_WARNING,
4364 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4365 {
4366 return( ret );
4367 }
Paul Bakker48916f92012-09-16 19:57:18 +00004368 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004369 else
4370#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004371 {
4372 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004373 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004374 }
Paul Bakker48916f92012-09-16 19:57:18 +00004375 }
4376 else
4377 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004378 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004379 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004380 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004381 return( ret );
4382 }
4383
4384 return( POLARSSL_ERR_NET_WANT_READ );
4385 }
4386 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004387 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4388 {
4389 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4390 "but not honored by client" ) );
4391 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4392 }
Paul Bakker48916f92012-09-16 19:57:18 +00004393 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004394 {
4395 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004396 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004397 }
4398
4399 ssl->in_offt = ssl->in_msg;
4400 }
4401
4402 n = ( len < ssl->in_msglen )
4403 ? len : ssl->in_msglen;
4404
4405 memcpy( buf, ssl->in_offt, n );
4406 ssl->in_msglen -= n;
4407
4408 if( ssl->in_msglen == 0 )
4409 /* all bytes consumed */
4410 ssl->in_offt = NULL;
4411 else
4412 /* more data available */
4413 ssl->in_offt += n;
4414
4415 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4416
Paul Bakker23986e52011-04-24 08:57:21 +00004417 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004418}
4419
4420/*
4421 * Send application data to be encrypted by the SSL layer
4422 */
Paul Bakker23986e52011-04-24 08:57:21 +00004423int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004424{
Paul Bakker23986e52011-04-24 08:57:21 +00004425 int ret;
4426 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004427 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004428
4429 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4430
4431 if( ssl->state != SSL_HANDSHAKE_OVER )
4432 {
4433 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4434 {
4435 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4436 return( ret );
4437 }
4438 }
4439
Paul Bakker05decb22013-08-15 13:33:48 +02004440#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004441 /*
4442 * Assume mfl_code is correct since it was checked when set
4443 */
4444 max_len = mfl_code_to_length[ssl->mfl_code];
4445
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004446 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004447 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004448 */
4449 if( ssl->session_out != NULL &&
4450 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4451 {
4452 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4453 }
Paul Bakker05decb22013-08-15 13:33:48 +02004454#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004455
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004456 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004457
Paul Bakker5121ce52009-01-03 21:22:43 +00004458 if( ssl->out_left != 0 )
4459 {
4460 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4461 {
4462 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4463 return( ret );
4464 }
4465 }
Paul Bakker887bd502011-06-08 13:10:54 +00004466 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004467 {
Paul Bakker887bd502011-06-08 13:10:54 +00004468 ssl->out_msglen = n;
4469 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4470 memcpy( ssl->out_msg, buf, n );
4471
4472 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4473 {
4474 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4475 return( ret );
4476 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004477 }
4478
4479 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4480
Paul Bakker23986e52011-04-24 08:57:21 +00004481 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004482}
4483
4484/*
4485 * Notify the peer that the connection is being closed
4486 */
4487int ssl_close_notify( ssl_context *ssl )
4488{
4489 int ret;
4490
4491 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4492
4493 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4494 {
4495 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4496 return( ret );
4497 }
4498
4499 if( ssl->state == SSL_HANDSHAKE_OVER )
4500 {
Paul Bakker48916f92012-09-16 19:57:18 +00004501 if( ( ret = ssl_send_alert_message( ssl,
4502 SSL_ALERT_LEVEL_WARNING,
4503 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004505 return( ret );
4506 }
4507 }
4508
4509 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4510
4511 return( ret );
4512}
4513
Paul Bakker48916f92012-09-16 19:57:18 +00004514void ssl_transform_free( ssl_transform *transform )
4515{
4516#if defined(POLARSSL_ZLIB_SUPPORT)
4517 deflateEnd( &transform->ctx_deflate );
4518 inflateEnd( &transform->ctx_inflate );
4519#endif
4520
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004521 cipher_free_ctx( &transform->cipher_ctx_enc );
4522 cipher_free_ctx( &transform->cipher_ctx_dec );
4523
Paul Bakker61d113b2013-07-04 11:51:43 +02004524 md_free_ctx( &transform->md_ctx_enc );
4525 md_free_ctx( &transform->md_ctx_dec );
4526
Paul Bakker48916f92012-09-16 19:57:18 +00004527 memset( transform, 0, sizeof( ssl_transform ) );
4528}
4529
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004530#if defined(POLARSSL_X509_CRT_PARSE_C)
4531static void ssl_key_cert_free( ssl_key_cert *key_cert )
4532{
4533 ssl_key_cert *cur = key_cert, *next;
4534
4535 while( cur != NULL )
4536 {
4537 next = cur->next;
4538
4539 if( cur->key_own_alloc )
4540 {
4541 pk_free( cur->key );
4542 polarssl_free( cur->key );
4543 }
4544 polarssl_free( cur );
4545
4546 cur = next;
4547 }
4548}
4549#endif /* POLARSSL_X509_CRT_PARSE_C */
4550
Paul Bakker48916f92012-09-16 19:57:18 +00004551void ssl_handshake_free( ssl_handshake_params *handshake )
4552{
4553#if defined(POLARSSL_DHM_C)
4554 dhm_free( &handshake->dhm_ctx );
4555#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004556#if defined(POLARSSL_ECDH_C)
4557 ecdh_free( &handshake->ecdh_ctx );
4558#endif
4559
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004560#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004561 /* explicit void pointer cast for buggy MS compiler */
4562 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004563#endif
4564
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004565#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4566 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4567 /*
4568 * Free only the linked list wrapper, not the keys themselves
4569 * since the belong to the SNI callback
4570 */
4571 if( handshake->sni_key_cert != NULL )
4572 {
4573 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4574
4575 while( cur != NULL )
4576 {
4577 next = cur->next;
4578 polarssl_free( cur );
4579 cur = next;
4580 }
4581 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004582#endif
4583
Paul Bakker48916f92012-09-16 19:57:18 +00004584 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4585}
4586
4587void ssl_session_free( ssl_session *session )
4588{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004589#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004590 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004591 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004592 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004593 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004594 }
Paul Bakkered27a042013-04-18 22:46:23 +02004595#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004596
Paul Bakkera503a632013-08-14 13:48:06 +02004597#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004598 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004599#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004600
Paul Bakker0a597072012-09-25 21:55:46 +00004601 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004602}
4603
Paul Bakker5121ce52009-01-03 21:22:43 +00004604/*
4605 * Free an SSL context
4606 */
4607void ssl_free( ssl_context *ssl )
4608{
4609 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4610
Paul Bakker5121ce52009-01-03 21:22:43 +00004611 if( ssl->out_ctr != NULL )
4612 {
4613 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004614 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004615 }
4616
4617 if( ssl->in_ctr != NULL )
4618 {
4619 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004620 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 }
4622
Paul Bakker16770332013-10-11 09:59:44 +02004623#if defined(POLARSSL_ZLIB_SUPPORT)
4624 if( ssl->compress_buf != NULL )
4625 {
4626 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4627 polarssl_free( ssl->compress_buf );
4628 }
4629#endif
4630
Paul Bakker40e46942009-01-03 21:51:57 +00004631#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004632 mpi_free( &ssl->dhm_P );
4633 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004634#endif
4635
Paul Bakker48916f92012-09-16 19:57:18 +00004636 if( ssl->transform )
4637 {
4638 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004639 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004640 }
4641
4642 if( ssl->handshake )
4643 {
4644 ssl_handshake_free( ssl->handshake );
4645 ssl_transform_free( ssl->transform_negotiate );
4646 ssl_session_free( ssl->session_negotiate );
4647
Paul Bakker6e339b52013-07-03 13:37:05 +02004648 polarssl_free( ssl->handshake );
4649 polarssl_free( ssl->transform_negotiate );
4650 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004651 }
4652
Paul Bakkerc0463502013-02-14 11:19:38 +01004653 if( ssl->session )
4654 {
4655 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004656 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004657 }
4658
Paul Bakkera503a632013-08-14 13:48:06 +02004659#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004660 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004661#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004662
Paul Bakker0be444a2013-08-27 21:55:01 +02004663#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004664 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 {
4666 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004667 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004668 ssl->hostname_len = 0;
4669 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004670#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004671
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004672#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004673 if( ssl->psk != NULL )
4674 {
4675 memset( ssl->psk, 0, ssl->psk_len );
4676 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4677 polarssl_free( ssl->psk );
4678 polarssl_free( ssl->psk_identity );
4679 ssl->psk_len = 0;
4680 ssl->psk_identity_len = 0;
4681 }
4682#endif
4683
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004684#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004685 ssl_key_cert_free( ssl->key_cert );
4686#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004687
Paul Bakker05ef8352012-05-08 09:17:57 +00004688#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4689 if( ssl_hw_record_finish != NULL )
4690 {
4691 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4692 ssl_hw_record_finish( ssl );
4693 }
4694#endif
4695
Paul Bakker5121ce52009-01-03 21:22:43 +00004696 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004697
Paul Bakker86f04f42013-02-14 11:20:09 +01004698 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004699 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004700}
4701
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004702#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004703/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004704 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004705 */
4706unsigned char ssl_sig_from_pk( pk_context *pk )
4707{
4708#if defined(POLARSSL_RSA_C)
4709 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4710 return( SSL_SIG_RSA );
4711#endif
4712#if defined(POLARSSL_ECDSA_C)
4713 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4714 return( SSL_SIG_ECDSA );
4715#endif
4716 return( SSL_SIG_ANON );
4717}
4718
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004719pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4720{
4721 switch( sig )
4722 {
4723#if defined(POLARSSL_RSA_C)
4724 case SSL_SIG_RSA:
4725 return( POLARSSL_PK_RSA );
4726#endif
4727#if defined(POLARSSL_ECDSA_C)
4728 case SSL_SIG_ECDSA:
4729 return( POLARSSL_PK_ECDSA );
4730#endif
4731 default:
4732 return( POLARSSL_PK_NONE );
4733 }
4734}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004735#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004736
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004737/*
4738 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4739 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004740md_type_t ssl_md_alg_from_hash( unsigned char hash )
4741{
4742 switch( hash )
4743 {
4744#if defined(POLARSSL_MD5_C)
4745 case SSL_HASH_MD5:
4746 return( POLARSSL_MD_MD5 );
4747#endif
4748#if defined(POLARSSL_SHA1_C)
4749 case SSL_HASH_SHA1:
4750 return( POLARSSL_MD_SHA1 );
4751#endif
4752#if defined(POLARSSL_SHA256_C)
4753 case SSL_HASH_SHA224:
4754 return( POLARSSL_MD_SHA224 );
4755 case SSL_HASH_SHA256:
4756 return( POLARSSL_MD_SHA256 );
4757#endif
4758#if defined(POLARSSL_SHA512_C)
4759 case SSL_HASH_SHA384:
4760 return( POLARSSL_MD_SHA384 );
4761 case SSL_HASH_SHA512:
4762 return( POLARSSL_MD_SHA512 );
4763#endif
4764 default:
4765 return( POLARSSL_MD_NONE );
4766 }
4767}
4768
Paul Bakker5121ce52009-01-03 21:22:43 +00004769#endif
Gergely Budai987bfb52014-01-19 21:48:42 +01004770
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004771#if defined(POLARSSL_SSL_SET_CURVES)
4772/*
4773 * Check is a curve proposed by the peer is in our list.
4774 * Return 1 if we're willing to use it, 0 otherwise.
4775 */
4776int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4777{
4778 const ecp_group_id *gid;
4779
4780 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4781 if( *gid == grp_id )
4782 return( 1 );
4783
4784 return( 0 );
4785}
4786#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004787
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004788#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004789int ssl_check_cert_usage( const x509_crt *cert,
4790 const ssl_ciphersuite_t *ciphersuite,
4791 int cert_endpoint )
4792{
4793#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4794 int usage = 0;
4795#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004796#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4797 const char *ext_oid;
4798 size_t ext_len;
4799#endif
4800
4801#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4802 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4803 ((void) cert);
4804 ((void) cert_endpoint);
4805#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004806
4807#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4808 if( cert_endpoint == SSL_IS_SERVER )
4809 {
4810 /* Server part of the key exchange */
4811 switch( ciphersuite->key_exchange )
4812 {
4813 case POLARSSL_KEY_EXCHANGE_RSA:
4814 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4815 usage = KU_KEY_ENCIPHERMENT;
4816 break;
4817
4818 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4819 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4820 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4821 usage = KU_DIGITAL_SIGNATURE;
4822 break;
4823
4824 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4825 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4826 usage = KU_KEY_AGREEMENT;
4827 break;
4828
4829 /* Don't use default: we want warnings when adding new values */
4830 case POLARSSL_KEY_EXCHANGE_NONE:
4831 case POLARSSL_KEY_EXCHANGE_PSK:
4832 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4833 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4834 usage = 0;
4835 }
4836 }
4837 else
4838 {
4839 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4840 usage = KU_DIGITAL_SIGNATURE;
4841 }
4842
4843 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4844 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004845#else
4846 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004847#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4848
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004849#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4850 if( cert_endpoint == SSL_IS_SERVER )
4851 {
4852 ext_oid = OID_SERVER_AUTH;
4853 ext_len = OID_SIZE( OID_SERVER_AUTH );
4854 }
4855 else
4856 {
4857 ext_oid = OID_CLIENT_AUTH;
4858 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4859 }
4860
4861 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4862 return( -1 );
4863#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4864
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004865 return( 0 );
4866}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004867#endif /* POLARSSL_X509_CRT_PARSE_C */