blob: 6e01494b67e35d53809f3707e7284ac8d0cce4dc [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
Paul Bakker7dc4c442014-02-01 22:50:26 +010041#if defined(POLARSSL_PLATFORM_C)
42#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020043#else
44#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakker6edcd412013-10-29 15:22:54 +010050#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
51 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000052#define strcasecmp _stricmp
53#endif
54
Paul Bakker05decb22013-08-15 13:33:48 +020055#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020056/*
57 * Convert max_fragment_length codes to length.
58 * RFC 6066 says:
59 * enum{
60 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
61 * } MaxFragmentLength;
62 * and we add 0 -> extension unused
63 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020064static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020065{
66 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
67 512, /* SSL_MAX_FRAG_LEN_512 */
68 1024, /* SSL_MAX_FRAG_LEN_1024 */
69 2048, /* SSL_MAX_FRAG_LEN_2048 */
70 4096, /* SSL_MAX_FRAG_LEN_4096 */
71};
Paul Bakker05decb22013-08-15 13:33:48 +020072#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020073
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020074static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
75{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020076 ssl_session_free( dst );
77 memcpy( dst, src, sizeof( ssl_session ) );
78
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020080 if( src->peer_cert != NULL )
81 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020082 int ret;
83
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020084 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
85 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020086 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
87
Paul Bakkerb6b09562013-09-18 14:17:41 +020088 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089
Paul Bakkerddf26b42013-09-18 13:46:23 +020090 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
91 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020092 {
93 polarssl_free( dst->peer_cert );
94 dst->peer_cert = NULL;
95 return( ret );
96 }
97 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020099
Paul Bakkera503a632013-08-14 13:48:06 +0200100#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101 if( src->ticket != NULL )
102 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200103 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
104 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200105 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
106
107 memcpy( dst->ticket, src->ticket, src->ticket_len );
108 }
Paul Bakkera503a632013-08-14 13:48:06 +0200109#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110
111 return( 0 );
112}
113
Paul Bakker05ef8352012-05-08 09:17:57 +0000114#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
115int (*ssl_hw_record_init)(ssl_context *ssl,
116 const unsigned char *key_enc, const unsigned char *key_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100117 size_t keylen,
Paul Bakker05ef8352012-05-08 09:17:57 +0000118 const unsigned char *iv_enc, const unsigned char *iv_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100119 size_t ivlen,
120 const unsigned char *mac_enc, const unsigned char *mac_dec,
121 size_t maclen) = NULL;
122int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000123int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
124int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
125int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
126int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
127#endif
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129/*
130 * Key material generation
131 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200132#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200133static int ssl3_prf( const unsigned char *secret, size_t slen,
134 const char *label,
135 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000136 unsigned char *dstbuf, size_t dlen )
137{
138 size_t i;
139 md5_context md5;
140 sha1_context sha1;
141 unsigned char padding[16];
142 unsigned char sha1sum[20];
143 ((void)label);
144
145 /*
146 * SSLv3:
147 * block =
148 * MD5( secret + SHA1( 'A' + secret + random ) ) +
149 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
150 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
151 * ...
152 */
153 for( i = 0; i < dlen / 16; i++ )
154 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200155 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000156
157 sha1_starts( &sha1 );
158 sha1_update( &sha1, padding, 1 + i );
159 sha1_update( &sha1, secret, slen );
160 sha1_update( &sha1, random, rlen );
161 sha1_finish( &sha1, sha1sum );
162
163 md5_starts( &md5 );
164 md5_update( &md5, secret, slen );
165 md5_update( &md5, sha1sum, 20 );
166 md5_finish( &md5, dstbuf + i * 16 );
167 }
168
169 memset( &md5, 0, sizeof( md5 ) );
170 memset( &sha1, 0, sizeof( sha1 ) );
171
172 memset( padding, 0, sizeof( padding ) );
173 memset( sha1sum, 0, sizeof( sha1sum ) );
174
175 return( 0 );
176}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200177#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000178
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200179#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200180static int tls1_prf( const unsigned char *secret, size_t slen,
181 const char *label,
182 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000183 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184{
Paul Bakker23986e52011-04-24 08:57:21 +0000185 size_t nb, hs;
186 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200187 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 unsigned char tmp[128];
189 unsigned char h_i[20];
190
191 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000192 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 hs = ( slen + 1 ) / 2;
195 S1 = secret;
196 S2 = secret + slen - hs;
197
198 nb = strlen( label );
199 memcpy( tmp + 20, label, nb );
200 memcpy( tmp + 20 + nb, random, rlen );
201 nb += rlen;
202
203 /*
204 * First compute P_md5(secret,label+random)[0..dlen]
205 */
206 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
207
208 for( i = 0; i < dlen; i += 16 )
209 {
210 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
211 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
212
213 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
214
215 for( j = 0; j < k; j++ )
216 dstbuf[i + j] = h_i[j];
217 }
218
219 /*
220 * XOR out with P_sha1(secret,label+random)[0..dlen]
221 */
222 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
223
224 for( i = 0; i < dlen; i += 20 )
225 {
226 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
227 sha1_hmac( S2, hs, tmp, 20, tmp );
228
229 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
230
231 for( j = 0; j < k; j++ )
232 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
233 }
234
235 memset( tmp, 0, sizeof( tmp ) );
236 memset( h_i, 0, sizeof( h_i ) );
237
238 return( 0 );
239}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200240#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200242#if defined(POLARSSL_SSL_PROTO_TLS1_2)
243#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200244static int tls_prf_sha256( const unsigned char *secret, size_t slen,
245 const char *label,
246 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000247 unsigned char *dstbuf, size_t dlen )
248{
249 size_t nb;
250 size_t i, j, k;
251 unsigned char tmp[128];
252 unsigned char h_i[32];
253
254 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
255 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
256
257 nb = strlen( label );
258 memcpy( tmp + 32, label, nb );
259 memcpy( tmp + 32 + nb, random, rlen );
260 nb += rlen;
261
262 /*
263 * Compute P_<hash>(secret, label + random)[0..dlen]
264 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200265 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000266
267 for( i = 0; i < dlen; i += 32 )
268 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200269 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
270 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000271
272 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
273
274 for( j = 0; j < k; j++ )
275 dstbuf[i + j] = h_i[j];
276 }
277
278 memset( tmp, 0, sizeof( tmp ) );
279 memset( h_i, 0, sizeof( h_i ) );
280
281 return( 0 );
282}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200283#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000284
Paul Bakker9e36f042013-06-30 14:34:05 +0200285#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200286static int tls_prf_sha384( const unsigned char *secret, size_t slen,
287 const char *label,
288 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000289 unsigned char *dstbuf, size_t dlen )
290{
291 size_t nb;
292 size_t i, j, k;
293 unsigned char tmp[128];
294 unsigned char h_i[48];
295
296 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
297 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
298
299 nb = strlen( label );
300 memcpy( tmp + 48, label, nb );
301 memcpy( tmp + 48 + nb, random, rlen );
302 nb += rlen;
303
304 /*
305 * Compute P_<hash>(secret, label + random)[0..dlen]
306 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200307 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000308
309 for( i = 0; i < dlen; i += 48 )
310 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200311 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
312 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000313
314 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
315
316 for( j = 0; j < k; j++ )
317 dstbuf[i + j] = h_i[j];
318 }
319
320 memset( tmp, 0, sizeof( tmp ) );
321 memset( h_i, 0, sizeof( h_i ) );
322
323 return( 0 );
324}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200325#endif /* POLARSSL_SHA512_C */
326#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200328static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200329
330#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
331 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200332static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200333#endif
Paul Bakker380da532012-04-18 16:10:25 +0000334
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200335#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000336static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338#endif
339
340#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
341static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343#endif
344
345#if defined(POLARSSL_SSL_PROTO_TLS1_2)
346#if defined(POLARSSL_SHA256_C)
347static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
348static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000349static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100351
Paul Bakker9e36f042013-06-30 14:34:05 +0200352#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200353static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100354static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000355static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100356#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +0000358
Paul Bakker5121ce52009-01-03 21:22:43 +0000359int ssl_derive_keys( ssl_context *ssl )
360{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200361 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 unsigned char keyblk[256];
364 unsigned char *key1;
365 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100366 unsigned char *mac_enc;
367 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200368 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100369 const cipher_info_t *cipher_info;
370 const md_info_t *md_info;
371
Paul Bakker48916f92012-09-16 19:57:18 +0000372 ssl_session *session = ssl->session_negotiate;
373 ssl_transform *transform = ssl->transform_negotiate;
374 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
377
Paul Bakker68884e32013-01-07 18:20:04 +0100378 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
379 if( cipher_info == NULL )
380 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200381 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100382 transform->ciphersuite_info->cipher ) );
383 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
384 }
385
386 md_info = md_info_from_type( transform->ciphersuite_info->mac );
387 if( md_info == NULL )
388 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200389 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100390 transform->ciphersuite_info->mac ) );
391 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
392 }
393
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000395 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000396 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200397#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000398 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000399 {
Paul Bakker48916f92012-09-16 19:57:18 +0000400 handshake->tls_prf = ssl3_prf;
401 handshake->calc_verify = ssl_calc_verify_ssl;
402 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000403 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200404 else
405#endif
406#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
407 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408 {
Paul Bakker48916f92012-09-16 19:57:18 +0000409 handshake->tls_prf = tls1_prf;
410 handshake->calc_verify = ssl_calc_verify_tls;
411 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200413 else
414#endif
415#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200416#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200417 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
418 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000419 {
Paul Bakker48916f92012-09-16 19:57:18 +0000420 handshake->tls_prf = tls_prf_sha384;
421 handshake->calc_verify = ssl_calc_verify_tls_sha384;
422 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000423 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000424 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200425#endif
426#if defined(POLARSSL_SHA256_C)
427 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = tls_prf_sha256;
430 handshake->calc_verify = ssl_calc_verify_tls_sha256;
431 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200433 else
434#endif
435#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200436 {
437 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200438 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200439 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000440
441 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 * SSLv3:
443 * master =
444 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
445 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
446 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200447 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200448 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 * master = PRF( premaster, "master secret", randbytes )[0..47]
450 */
Paul Bakker0a597072012-09-25 21:55:46 +0000451 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 {
Paul Bakker48916f92012-09-16 19:57:18 +0000453 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
454 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
Paul Bakker48916f92012-09-16 19:57:18 +0000456 handshake->tls_prf( handshake->premaster, handshake->pmslen,
457 "master secret",
458 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Paul Bakker48916f92012-09-16 19:57:18 +0000460 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 }
462 else
463 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
464
465 /*
466 * Swap the client and server random values.
467 */
Paul Bakker48916f92012-09-16 19:57:18 +0000468 memcpy( tmp, handshake->randbytes, 64 );
469 memcpy( handshake->randbytes, tmp + 32, 32 );
470 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 memset( tmp, 0, sizeof( tmp ) );
472
473 /*
474 * SSLv3:
475 * key block =
476 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
477 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
478 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
479 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
480 * ...
481 *
482 * TLSv1:
483 * key block = PRF( master, "key expansion", randbytes )
484 */
Paul Bakker48916f92012-09-16 19:57:18 +0000485 handshake->tls_prf( session->master, 48, "key expansion",
486 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
Paul Bakker48916f92012-09-16 19:57:18 +0000488 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
489 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
490 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
491 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
493
Paul Bakker48916f92012-09-16 19:57:18 +0000494 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 /*
497 * Determine the appropriate key, IV and MAC length.
498 */
Paul Bakker68884e32013-01-07 18:20:04 +0100499
500 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 {
Paul Bakker68884e32013-01-07 18:20:04 +0100502 transform->keylen = cipher_info->key_length;
503 transform->keylen /= 8;
504 transform->minlen = 1;
505 transform->ivlen = 12;
506 transform->fixed_ivlen = 4;
507 transform->maclen = 0;
508 }
509 else
510 {
511 if( md_info->type != POLARSSL_MD_NONE )
512 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200513 int ret;
514
Paul Bakker61d113b2013-07-04 11:51:43 +0200515 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
516 {
517 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
518 return( ret );
519 }
520
521 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
522 {
523 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
524 return( ret );
525 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
Paul Bakker68884e32013-01-07 18:20:04 +0100527 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200528
Paul Bakker1f2bc622013-08-15 13:45:55 +0200529#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200530 /*
531 * If HMAC is to be truncated, we shall keep the leftmost bytes,
532 * (rfc 6066 page 13 or rfc 2104 section 4),
533 * so we only need to adjust the length here.
534 */
535 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
536 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200537#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100538 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker68884e32013-01-07 18:20:04 +0100540 transform->keylen = cipher_info->key_length;
541 transform->keylen /= 8;
542 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Paul Bakker68884e32013-01-07 18:20:04 +0100544 transform->minlen = transform->keylen;
545 if( transform->minlen < transform->maclen )
546 {
547 if( cipher_info->mode == POLARSSL_MODE_STREAM )
548 transform->minlen = transform->maclen;
549 else
550 transform->minlen += transform->keylen;
551 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 }
553
554 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000555 transform->keylen, transform->minlen, transform->ivlen,
556 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 /*
559 * Finally setup the cipher contexts, IVs and MAC secrets.
560 */
561 if( ssl->endpoint == SSL_IS_CLIENT )
562 {
Paul Bakker48916f92012-09-16 19:57:18 +0000563 key1 = keyblk + transform->maclen * 2;
564 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Paul Bakker68884e32013-01-07 18:20:04 +0100566 mac_enc = keyblk;
567 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000569 /*
570 * This is not used in TLS v1.1.
571 */
Paul Bakker48916f92012-09-16 19:57:18 +0000572 iv_copy_len = ( transform->fixed_ivlen ) ?
573 transform->fixed_ivlen : transform->ivlen;
574 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
575 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000576 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 }
578 else
579 {
Paul Bakker48916f92012-09-16 19:57:18 +0000580 key1 = keyblk + transform->maclen * 2 + transform->keylen;
581 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Paul Bakker68884e32013-01-07 18:20:04 +0100583 mac_enc = keyblk + transform->maclen;
584 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000586 /*
587 * This is not used in TLS v1.1.
588 */
Paul Bakker48916f92012-09-16 19:57:18 +0000589 iv_copy_len = ( transform->fixed_ivlen ) ?
590 transform->fixed_ivlen : transform->ivlen;
591 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
592 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000593 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 }
595
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200596#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100597 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
598 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100599 if( transform->maclen > sizeof transform->mac_enc )
600 {
601 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
602 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
603 }
604
Paul Bakker68884e32013-01-07 18:20:04 +0100605 memcpy( transform->mac_enc, mac_enc, transform->maclen );
606 memcpy( transform->mac_dec, mac_dec, transform->maclen );
607 }
608 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200609#endif
610#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
611 defined(POLARSSL_SSL_PROTO_TLS1_2)
612 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100613 {
614 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
615 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
616 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200617 else
618#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200619 {
620 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200621 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200622 }
Paul Bakker68884e32013-01-07 18:20:04 +0100623
Paul Bakker05ef8352012-05-08 09:17:57 +0000624#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
625 if( ssl_hw_record_init != NULL)
626 {
627 int ret = 0;
628
629 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
630
Paul Bakker07eb38b2012-12-19 14:42:06 +0100631 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
632 transform->iv_enc, transform->iv_dec,
633 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100634 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100635 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000636 {
637 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
638 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
639 }
640 }
641#endif
642
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200643 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
644 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200646 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
647 return( ret );
648 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200649
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200650 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
651 cipher_info ) ) != 0 )
652 {
653 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
654 return( ret );
655 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200656
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200657 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
658 cipher_info->key_length,
659 POLARSSL_ENCRYPT ) ) != 0 )
660 {
661 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
662 return( ret );
663 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200664
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200665 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
666 cipher_info->key_length,
667 POLARSSL_DECRYPT ) ) != 0 )
668 {
669 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
670 return( ret );
671 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200672
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200673#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200674 if( cipher_info->mode == POLARSSL_MODE_CBC )
675 {
676 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
677 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200678 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200679 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
680 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200681 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200682
683 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
684 POLARSSL_PADDING_NONE ) ) != 0 )
685 {
686 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
687 return( ret );
688 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200690#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
692 memset( keyblk, 0, sizeof( keyblk ) );
693
Paul Bakker2770fbd2012-07-03 13:30:23 +0000694#if defined(POLARSSL_ZLIB_SUPPORT)
695 // Initialize compression
696 //
Paul Bakker48916f92012-09-16 19:57:18 +0000697 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000698 {
Paul Bakker16770332013-10-11 09:59:44 +0200699 if( ssl->compress_buf == NULL )
700 {
701 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
702 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
703 if( ssl->compress_buf == NULL )
704 {
705 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
706 SSL_BUFFER_LEN ) );
707 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
708 }
709 }
710
Paul Bakker2770fbd2012-07-03 13:30:23 +0000711 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
712
Paul Bakker48916f92012-09-16 19:57:18 +0000713 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
714 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000715
Paul Bakker48916f92012-09-16 19:57:18 +0000716 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
717 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000718 {
719 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
720 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
721 }
722 }
723#endif /* POLARSSL_ZLIB_SUPPORT */
724
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
726
727 return( 0 );
728}
729
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200730#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000731void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000732{
733 md5_context md5;
734 sha1_context sha1;
735 unsigned char pad_1[48];
736 unsigned char pad_2[48];
737
Paul Bakker380da532012-04-18 16:10:25 +0000738 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Paul Bakker48916f92012-09-16 19:57:18 +0000740 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
741 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Paul Bakker380da532012-04-18 16:10:25 +0000743 memset( pad_1, 0x36, 48 );
744 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Paul Bakker48916f92012-09-16 19:57:18 +0000746 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000747 md5_update( &md5, pad_1, 48 );
748 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker380da532012-04-18 16:10:25 +0000750 md5_starts( &md5 );
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_2, 48 );
753 md5_update( &md5, hash, 16 );
754 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakker48916f92012-09-16 19:57:18 +0000756 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000757 sha1_update( &sha1, pad_1, 40 );
758 sha1_finish( &sha1, hash + 16 );
759
760 sha1_starts( &sha1 );
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_2, 40 );
763 sha1_update( &sha1, hash + 16, 20 );
764 sha1_finish( &sha1, hash + 16 );
765
766 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
767 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
768
769 return;
770}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200771#endif
Paul Bakker380da532012-04-18 16:10:25 +0000772
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000774void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
775{
776 md5_context md5;
777 sha1_context sha1;
778
779 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
780
Paul Bakker48916f92012-09-16 19:57:18 +0000781 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
782 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000783
Paul Bakker48916f92012-09-16 19:57:18 +0000784 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000785 sha1_finish( &sha1, hash + 16 );
786
787 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
788 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
789
790 return;
791}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200792#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000793
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200794#if defined(POLARSSL_SSL_PROTO_TLS1_2)
795#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000796void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
797{
Paul Bakker9e36f042013-06-30 14:34:05 +0200798 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000799
800 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
801
Paul Bakker9e36f042013-06-30 14:34:05 +0200802 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
803 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000804
805 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
806 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
807
808 return;
809}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000811
Paul Bakker9e36f042013-06-30 14:34:05 +0200812#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000813void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
814{
Paul Bakker9e36f042013-06-30 14:34:05 +0200815 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000816
817 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
818
Paul Bakker9e36f042013-06-30 14:34:05 +0200819 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
820 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821
Paul Bakkerca4ab492012-04-18 14:23:57 +0000822 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
824
825 return;
826}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200827#endif /* POLARSSL_SHA512_C */
828#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200830#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200831int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
832{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200833 unsigned char *p = ssl->handshake->premaster;
834 unsigned char *end = p + sizeof( ssl->handshake->premaster );
835
836 /*
837 * PMS = struct {
838 * opaque other_secret<0..2^16-1>;
839 * opaque psk<0..2^16-1>;
840 * };
841 * with "other_secret" depending on the particular key exchange
842 */
843#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
844 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
845 {
846 if( end - p < 2 + (int) ssl->psk_len )
847 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
848
849 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
850 *(p++) = (unsigned char)( ssl->psk_len );
851 p += ssl->psk_len;
852 }
853 else
854#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200855#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
856 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
857 {
858 /*
859 * other_secret already set by the ClientKeyExchange message,
860 * and is 48 bytes long
861 */
862 *p++ = 0;
863 *p++ = 48;
864 p += 48;
865 }
866 else
867#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200868#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
869 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
870 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200871 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200872 size_t len = ssl->handshake->dhm_ctx.len;
873
874 if( end - p < 2 + (int) len )
875 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
876
877 *(p++) = (unsigned char)( len >> 8 );
878 *(p++) = (unsigned char)( len );
879 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
880 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
881 {
882 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
883 return( ret );
884 }
885 p += len;
886
887 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
888 }
889 else
890#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
891#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
892 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
893 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200894 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895 size_t zlen;
896
897 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
898 p + 2, end - (p + 2),
899 ssl->f_rng, ssl->p_rng ) ) != 0 )
900 {
901 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
902 return( ret );
903 }
904
905 *(p++) = (unsigned char)( zlen >> 8 );
906 *(p++) = (unsigned char)( zlen );
907 p += zlen;
908
909 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
910 }
911 else
912#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
913 {
914 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
915 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
916 }
917
918 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100919 if( end - p < 2 + (int) ssl->psk_len )
920 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
921
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200922 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
923 *(p++) = (unsigned char)( ssl->psk_len );
924 memcpy( p, ssl->psk, ssl->psk_len );
925 p += ssl->psk_len;
926
927 ssl->handshake->pmslen = p - ssl->handshake->premaster;
928
929 return( 0 );
930}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200931#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200933#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000934/*
935 * SSLv3.0 MAC functions
936 */
Paul Bakker68884e32013-01-07 18:20:04 +0100937static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
938 unsigned char *buf, size_t len,
939 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000940{
941 unsigned char header[11];
942 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100943 int padlen = 0;
944 int md_size = md_get_size( md_ctx->md_info );
945 int md_type = md_get_type( md_ctx->md_info );
946
947 if( md_type == POLARSSL_MD_MD5 )
948 padlen = 48;
949 else if( md_type == POLARSSL_MD_SHA1 )
950 padlen = 40;
951 else if( md_type == POLARSSL_MD_SHA256 )
952 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100953 else if( md_type == POLARSSL_MD_SHA384 )
954 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
956 memcpy( header, ctr, 8 );
957 header[ 8] = (unsigned char) type;
958 header[ 9] = (unsigned char)( len >> 8 );
959 header[10] = (unsigned char)( len );
960
Paul Bakker68884e32013-01-07 18:20:04 +0100961 memset( padding, 0x36, padlen );
962 md_starts( md_ctx );
963 md_update( md_ctx, secret, md_size );
964 md_update( md_ctx, padding, padlen );
965 md_update( md_ctx, header, 11 );
966 md_update( md_ctx, buf, len );
967 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
Paul Bakker68884e32013-01-07 18:20:04 +0100969 memset( padding, 0x5C, padlen );
970 md_starts( md_ctx );
971 md_update( md_ctx, secret, md_size );
972 md_update( md_ctx, padding, padlen );
973 md_update( md_ctx, buf + len, md_size );
974 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000975}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200976#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000977
Paul Bakker5121ce52009-01-03 21:22:43 +0000978/*
979 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200980 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000981static int ssl_encrypt_buf( ssl_context *ssl )
982{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200983 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
985 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
986
987 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200988 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200990#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
991 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
992 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
993 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
994 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200996#if defined(POLARSSL_SSL_PROTO_SSL3)
997 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
998 {
999 ssl_mac( &ssl->transform_out->md_ctx_enc,
1000 ssl->transform_out->mac_enc,
1001 ssl->out_msg, ssl->out_msglen,
1002 ssl->out_ctr, ssl->out_msgtype );
1003 }
1004 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001005#endif
1006#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001007 defined(POLARSSL_SSL_PROTO_TLS1_2)
1008 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1009 {
1010 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1011 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1012 ssl->out_msg, ssl->out_msglen );
1013 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1014 ssl->out_msg + ssl->out_msglen );
1015 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1016 }
1017 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001018#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001019 {
1020 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1021 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1022 }
1023
1024 SSL_DEBUG_BUF( 4, "computed mac",
1025 ssl->out_msg + ssl->out_msglen,
1026 ssl->transform_out->maclen );
1027
1028 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001029 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001030#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001032 /*
1033 * Encrypt
1034 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001035#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001036 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1037 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001039 int ret;
1040 size_t olen = 0;
1041
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1043 "including %d bytes of padding",
1044 ssl->out_msglen, 0 ) );
1045
1046 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1047 ssl->out_msg, ssl->out_msglen );
1048
Paul Bakker45125bc2013-09-04 16:47:11 +02001049 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001050 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001051 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1052 return( ret );
1053 }
1054
1055 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1056 ssl->transform_out->iv_enc,
1057 ssl->transform_out->ivlen ) ) != 0 )
1058 {
1059 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001060 return( ret );
1061 }
1062
1063 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1064 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1065 &olen ) ) != 0 )
1066 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001067 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001068 return( ret );
1069 }
1070
1071 if( ssl->out_msglen != olen )
1072 {
1073 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1074 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001075 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001076 }
1077
1078 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001079 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001080 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001081 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001082 return( ret );
1083 }
1084
1085 if( 0 != olen )
1086 {
1087 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1088 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001089 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001090 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 }
Paul Bakker68884e32013-01-07 18:20:04 +01001092 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001093#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001094#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001095 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1096 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001097 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001098 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001099 unsigned char *enc_msg;
1100 unsigned char add_data[13];
1101 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1102
Paul Bakkerca4ab492012-04-18 14:23:57 +00001103 memcpy( add_data, ssl->out_ctr, 8 );
1104 add_data[8] = ssl->out_msgtype;
1105 add_data[9] = ssl->major_ver;
1106 add_data[10] = ssl->minor_ver;
1107 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1108 add_data[12] = ssl->out_msglen & 0xFF;
1109
1110 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1111 add_data, 13 );
1112
Paul Bakker68884e32013-01-07 18:20:04 +01001113 /*
1114 * Generate IV
1115 */
1116 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001117 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001118 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1119 if( ret != 0 )
1120 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001121
Paul Bakker68884e32013-01-07 18:20:04 +01001122 memcpy( ssl->out_iv,
1123 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1124 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001125
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001126 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1127 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1128
Paul Bakker68884e32013-01-07 18:20:04 +01001129 /*
1130 * Fix pointer positions and message length with added IV
1131 */
1132 enc_msg = ssl->out_msg;
1133 enc_msglen = ssl->out_msglen;
1134 ssl->out_msglen += ssl->transform_out->ivlen -
1135 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001136
Paul Bakker68884e32013-01-07 18:20:04 +01001137 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1138 "including %d bytes of padding",
1139 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140
Paul Bakker68884e32013-01-07 18:20:04 +01001141 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1142 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001143
Paul Bakker68884e32013-01-07 18:20:04 +01001144 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001145 * Encrypt
1146 */
1147 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1148 ssl->transform_out->iv_enc,
1149 ssl->transform_out->ivlen ) ) != 0 ||
1150 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1151 {
1152 return( ret );
1153 }
1154
1155 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1156 add_data, 13 ) ) != 0 )
1157 {
1158 return( ret );
1159 }
1160
1161 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1162 enc_msg, enc_msglen,
1163 enc_msg, &olen ) ) != 0 )
1164 {
1165 return( ret );
1166 }
1167 totlen = olen;
1168
1169 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1170 enc_msg + olen, &olen ) ) != 0 )
1171 {
1172 return( ret );
1173 }
1174 totlen += olen;
1175
1176 if( totlen != enc_msglen )
1177 {
1178 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1179 return( -1 );
1180 }
1181
1182 /*
1183 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001184 */
1185 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001186
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001187 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1188 enc_msg + enc_msglen, 16 ) ) != 0 )
1189 {
1190 return( ret );
1191 }
Paul Bakker68884e32013-01-07 18:20:04 +01001192
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001193 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001194 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 else
Paul Bakker68884e32013-01-07 18:20:04 +01001196#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001197#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1198 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001199 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1200 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001202 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001203 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001204 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001205
Paul Bakker48916f92012-09-16 19:57:18 +00001206 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1207 ssl->transform_out->ivlen;
1208 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 padlen = 0;
1210
1211 for( i = 0; i <= padlen; i++ )
1212 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1213
1214 ssl->out_msglen += padlen + 1;
1215
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001216 enc_msglen = ssl->out_msglen;
1217 enc_msg = ssl->out_msg;
1218
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001219#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001221 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1222 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001223 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001224 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001225 {
1226 /*
1227 * Generate IV
1228 */
Paul Bakker48916f92012-09-16 19:57:18 +00001229 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1230 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001231 if( ret != 0 )
1232 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233
Paul Bakker92be97b2013-01-02 17:30:03 +01001234 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001235 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236
1237 /*
1238 * Fix pointer positions and message length with added IV
1239 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001240 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001241 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001242 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001244#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001245
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001247 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001248 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
1250 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001251 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Paul Bakker45125bc2013-09-04 16:47:11 +02001253 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001255 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1256 return( ret );
1257 }
1258
1259 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1260 ssl->transform_out->iv_enc,
1261 ssl->transform_out->ivlen ) ) != 0 )
1262 {
1263 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001264 return( ret );
1265 }
Paul Bakker68884e32013-01-07 18:20:04 +01001266
Paul Bakkercca5b812013-08-31 17:40:26 +02001267 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1268 enc_msg, enc_msglen, enc_msg,
1269 &olen ) ) != 0 )
1270 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001271 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 return( ret );
1273 }
Paul Bakker68884e32013-01-07 18:20:04 +01001274
Paul Bakkercca5b812013-08-31 17:40:26 +02001275 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001276
Paul Bakkercca5b812013-08-31 17:40:26 +02001277 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001278 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001279 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001280 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 return( ret );
1282 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001283
Paul Bakkercca5b812013-08-31 17:40:26 +02001284 if( enc_msglen != olen )
1285 {
1286 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1287 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001288 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001289 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001290
1291#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001292 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1293 {
1294 /*
1295 * Save IV in SSL3 and TLS1
1296 */
1297 memcpy( ssl->transform_out->iv_enc,
1298 ssl->transform_out->cipher_ctx_enc.iv,
1299 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001301#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001303 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001304#endif /* POLARSSL_CIPHER_MODE_CBC &&
1305 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001306 {
1307 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1308 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
Paul Bakkerca4ab492012-04-18 14:23:57 +00001311 for( i = 8; i > 0; i-- )
1312 if( ++ssl->out_ctr[i - 1] != 0 )
1313 break;
1314
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001315 /* The loops goes to its end iff the counter is wrapping */
1316 if( i == 0 )
1317 {
1318 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1319 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1320 }
1321
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1323
1324 return( 0 );
1325}
1326
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001327#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001328
Paul Bakker5121ce52009-01-03 21:22:43 +00001329static int ssl_decrypt_buf( ssl_context *ssl )
1330{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001331 size_t i;
1332#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1333 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1334 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1335 size_t padlen = 0, correct = 1;
1336#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
1338 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1339
Paul Bakker48916f92012-09-16 19:57:18 +00001340 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 {
1342 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001343 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001344 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
1346
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001347#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001348 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1349 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001350 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001351 int ret;
1352 size_t olen = 0;
1353
Paul Bakker68884e32013-01-07 18:20:04 +01001354 padlen = 0;
1355
Paul Bakker45125bc2013-09-04 16:47:11 +02001356 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001357 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001358 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1359 return( ret );
1360 }
1361
1362 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1363 ssl->transform_in->iv_dec,
1364 ssl->transform_in->ivlen ) ) != 0 )
1365 {
1366 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001367 return( ret );
1368 }
1369
1370 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1371 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1372 &olen ) ) != 0 )
1373 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001374 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001375 return( ret );
1376 }
1377
1378 if( ssl->in_msglen != olen )
1379 {
1380 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001381 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001382 }
1383
1384 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001385 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001386 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001387 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001388 return( ret );
1389 }
1390
1391 if( 0 != olen )
1392 {
1393 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001394 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 }
Paul Bakker68884e32013-01-07 18:20:04 +01001397 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001398#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001399#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001400 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1401 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001402 {
1403 unsigned char *dec_msg;
1404 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001405 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001406 unsigned char add_data[13];
1407 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1408
Paul Bakker68884e32013-01-07 18:20:04 +01001409 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1410 ssl->transform_in->fixed_ivlen );
1411 dec_msglen -= 16;
1412 dec_msg = ssl->in_msg;
1413 dec_msg_result = ssl->in_msg;
1414 ssl->in_msglen = dec_msglen;
1415
1416 memcpy( add_data, ssl->in_ctr, 8 );
1417 add_data[8] = ssl->in_msgtype;
1418 add_data[9] = ssl->major_ver;
1419 add_data[10] = ssl->minor_ver;
1420 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1421 add_data[12] = ssl->in_msglen & 0xFF;
1422
1423 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1424 add_data, 13 );
1425
1426 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1427 ssl->in_iv,
1428 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1429
1430 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1431 ssl->transform_in->ivlen );
1432 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1433
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001434 /*
1435 * Decrypt
1436 */
1437 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1438 ssl->transform_in->iv_dec,
1439 ssl->transform_in->ivlen ) ) != 0 ||
1440 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001441 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001442 return( ret );
1443 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001444
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001445 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1446 add_data, 13 ) ) != 0 )
1447 {
1448 return( ret );
1449 }
1450
1451 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1452 dec_msg, dec_msglen,
1453 dec_msg_result, &olen ) ) != 0 )
1454 {
1455 return( ret );
1456 }
1457 totlen = olen;
1458
1459 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1460 dec_msg_result + olen, &olen ) ) != 0 )
1461 {
1462 return( ret );
1463 }
1464 totlen += olen;
1465
1466 if( totlen != dec_msglen )
1467 {
1468 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1469 return( -1 );
1470 }
1471
1472 /*
1473 * Authenticate
1474 */
1475 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1476 dec_msg + dec_msglen, 16 ) ) != 0 )
1477 {
1478 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001479 return( POLARSSL_ERR_SSL_INVALID_MAC );
1480 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001481
Paul Bakkerca4ab492012-04-18 14:23:57 +00001482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 else
Paul Bakker68884e32013-01-07 18:20:04 +01001484#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001485#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1486 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001487 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1488 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 {
Paul Bakker45829992013-01-03 14:52:21 +01001490 /*
1491 * Decrypt and check the padding
1492 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001493 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001494 unsigned char *dec_msg;
1495 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001496 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001497 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001498 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001499
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 /*
Paul Bakker45829992013-01-03 14:52:21 +01001501 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 */
Paul Bakker48916f92012-09-16 19:57:18 +00001503 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 {
1505 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001506 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001507 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 }
1509
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001510#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001511 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1512 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001513#endif
Paul Bakker45829992013-01-03 14:52:21 +01001514
1515 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1516 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1517 {
1518 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1519 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1520 return( POLARSSL_ERR_SSL_INVALID_MAC );
1521 }
1522
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001523 dec_msglen = ssl->in_msglen;
1524 dec_msg = ssl->in_msg;
1525 dec_msg_result = ssl->in_msg;
1526
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001527#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001528 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001529 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001530 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001531 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001532 {
Paul Bakker48916f92012-09-16 19:57:18 +00001533 dec_msglen -= ssl->transform_in->ivlen;
1534 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001535
Paul Bakker48916f92012-09-16 19:57:18 +00001536 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001537 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001538 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001539#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001540
Paul Bakker45125bc2013-09-04 16:47:11 +02001541 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001543 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1544 return( ret );
1545 }
1546
1547 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1548 ssl->transform_in->iv_dec,
1549 ssl->transform_in->ivlen ) ) != 0 )
1550 {
1551 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001552 return( ret );
1553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001554
Paul Bakkercca5b812013-08-31 17:40:26 +02001555 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1556 dec_msg, dec_msglen, dec_msg_result,
1557 &olen ) ) != 0 )
1558 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001559 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001560 return( ret );
1561 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001562
Paul Bakkercca5b812013-08-31 17:40:26 +02001563 dec_msglen -= olen;
1564 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001565 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001566 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001567 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001568 return( ret );
1569 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001570
Paul Bakkercca5b812013-08-31 17:40:26 +02001571 if( dec_msglen != olen )
1572 {
1573 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001574 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001575 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001576
1577#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001578 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1579 {
1580 /*
1581 * Save IV in SSL3 and TLS1
1582 */
1583 memcpy( ssl->transform_in->iv_dec,
1584 ssl->transform_in->cipher_ctx_dec.iv,
1585 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001587#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
1589 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001590
1591 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1592 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001593#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001594 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1595 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001596#endif
Paul Bakker45829992013-01-03 14:52:21 +01001597 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001598 correct = 0;
1599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001601#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1603 {
Paul Bakker48916f92012-09-16 19:57:18 +00001604 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001606#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1608 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001609 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001610#endif
Paul Bakker45829992013-01-03 14:52:21 +01001611 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001612 }
1613 }
1614 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001615#endif
1616#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1617 defined(POLARSSL_SSL_PROTO_TLS1_2)
1618 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 {
1620 /*
Paul Bakker45829992013-01-03 14:52:21 +01001621 * TLSv1+: always check the padding up to the first failure
1622 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001623 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001624 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001625 size_t padding_idx = ssl->in_msglen - padlen - 1;
1626
Paul Bakker956c9e02013-12-19 14:42:28 +01001627 /*
1628 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001629 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001630 *
Paul Bakker91c61bc2014-03-26 14:06:55 +01001631 * 2. padding_idx > SSL_MAX_CONTENT_LEN
Paul Bakker956c9e02013-12-19 14:42:28 +01001632 *
1633 * In both cases we reset padding_idx to a safe value (0) to
1634 * prevent out-of-buffer reads.
1635 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001636 correct &= ( ssl->in_msglen >= padlen + 1 );
1637 correct &= ( padding_idx <= SSL_MAX_CONTENT_LEN );
Paul Bakker956c9e02013-12-19 14:42:28 +01001638
1639 padding_idx *= correct;
1640
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001641 for( i = 1; i <= 256; i++ )
1642 {
1643 real_count &= ( i <= padlen );
1644 pad_count += real_count *
1645 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1646 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001647
1648 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001649
Paul Bakkerd66f0702013-01-31 16:57:45 +01001650#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001651 if( padlen > 0 && correct == 0)
1652 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001653#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001654 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001656 else
1657#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1658 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001659 {
1660 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001661 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001664 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001665#endif /* POLARSSL_CIPHER_MODE_CBC &&
1666 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001667 {
1668 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1669 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
1672 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1673 ssl->in_msg, ssl->in_msglen );
1674
1675 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1679 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1680 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1681 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1682 POLARSSL_MODE_GCM )
1683 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001684 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1685
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001686 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001688 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1689 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001691 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001693#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001694 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1695 {
1696 ssl_mac( &ssl->transform_in->md_ctx_dec,
1697 ssl->transform_in->mac_dec,
1698 ssl->in_msg, ssl->in_msglen,
1699 ssl->in_ctr, ssl->in_msgtype );
1700 }
1701 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001702#endif /* POLARSSL_SSL_PROTO_SSL3 */
1703#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001704 defined(POLARSSL_SSL_PROTO_TLS1_2)
1705 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1706 {
1707 /*
1708 * Process MAC and always update for padlen afterwards to make
1709 * total time independent of padlen
1710 *
1711 * extra_run compensates MAC check for padlen
1712 *
1713 * Known timing attacks:
1714 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1715 *
1716 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1717 * correctly. (We round down instead of up, so -56 is the correct
1718 * value for our calculations instead of -55)
1719 */
1720 size_t j, extra_run = 0;
1721 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1722 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001723
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001724 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001725
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001726 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1727 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1728 ssl->in_msglen );
1729 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1730 ssl->in_msg + ssl->in_msglen );
1731 for( j = 0; j < extra_run; j++ )
1732 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001733
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001734 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1735 }
1736 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001737#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001738 POLARSSL_SSL_PROTO_TLS1_2 */
1739 {
1740 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1741 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1742 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1745 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1746 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001748 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001749 ssl->transform_in->maclen ) != 0 )
1750 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001751#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001752 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001753#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001754 correct = 0;
1755 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001756
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001757 /*
1758 * Finally check the correct flag
1759 */
1760 if( correct == 0 )
1761 return( POLARSSL_ERR_SSL_INVALID_MAC );
1762 }
1763#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001764
1765 if( ssl->in_msglen == 0 )
1766 {
1767 ssl->nb_zero++;
1768
1769 /*
1770 * Three or more empty messages may be a DoS attack
1771 * (excessive CPU consumption).
1772 */
1773 if( ssl->nb_zero > 3 )
1774 {
1775 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1776 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001777 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 }
1779 }
1780 else
1781 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001782
Paul Bakker23986e52011-04-24 08:57:21 +00001783 for( i = 8; i > 0; i-- )
1784 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 break;
1786
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001787 /* The loops goes to its end iff the counter is wrapping */
1788 if( i == 0 )
1789 {
1790 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1791 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1792 }
1793
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1795
1796 return( 0 );
1797}
1798
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799#if defined(POLARSSL_ZLIB_SUPPORT)
1800/*
1801 * Compression/decompression functions
1802 */
1803static int ssl_compress_buf( ssl_context *ssl )
1804{
1805 int ret;
1806 unsigned char *msg_post = ssl->out_msg;
1807 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001808 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001809
1810 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1811
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001812 if( len_pre == 0 )
1813 return( 0 );
1814
Paul Bakker2770fbd2012-07-03 13:30:23 +00001815 memcpy( msg_pre, ssl->out_msg, len_pre );
1816
1817 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1818 ssl->out_msglen ) );
1819
1820 SSL_DEBUG_BUF( 4, "before compression: output payload",
1821 ssl->out_msg, ssl->out_msglen );
1822
Paul Bakker48916f92012-09-16 19:57:18 +00001823 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1824 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1825 ssl->transform_out->ctx_deflate.next_out = msg_post;
1826 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001827
Paul Bakker48916f92012-09-16 19:57:18 +00001828 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001829 if( ret != Z_OK )
1830 {
1831 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1832 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1833 }
1834
Paul Bakker48916f92012-09-16 19:57:18 +00001835 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001836
Paul Bakker2770fbd2012-07-03 13:30:23 +00001837 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1838 ssl->out_msglen ) );
1839
1840 SSL_DEBUG_BUF( 4, "after compression: output payload",
1841 ssl->out_msg, ssl->out_msglen );
1842
1843 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1844
1845 return( 0 );
1846}
1847
1848static int ssl_decompress_buf( ssl_context *ssl )
1849{
1850 int ret;
1851 unsigned char *msg_post = ssl->in_msg;
1852 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001853 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001854
1855 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1856
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001857 if( len_pre == 0 )
1858 return( 0 );
1859
Paul Bakker2770fbd2012-07-03 13:30:23 +00001860 memcpy( msg_pre, ssl->in_msg, len_pre );
1861
1862 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1863 ssl->in_msglen ) );
1864
1865 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1866 ssl->in_msg, ssl->in_msglen );
1867
Paul Bakker48916f92012-09-16 19:57:18 +00001868 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1869 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1870 ssl->transform_in->ctx_inflate.next_out = msg_post;
1871 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001872
Paul Bakker48916f92012-09-16 19:57:18 +00001873 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001874 if( ret != Z_OK )
1875 {
1876 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1877 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1878 }
1879
Paul Bakker48916f92012-09-16 19:57:18 +00001880 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001881
Paul Bakker2770fbd2012-07-03 13:30:23 +00001882 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1883 ssl->in_msglen ) );
1884
1885 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1886 ssl->in_msg, ssl->in_msglen );
1887
1888 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1889
1890 return( 0 );
1891}
1892#endif /* POLARSSL_ZLIB_SUPPORT */
1893
Paul Bakker5121ce52009-01-03 21:22:43 +00001894/*
1895 * Fill the input message buffer
1896 */
Paul Bakker23986e52011-04-24 08:57:21 +00001897int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001898{
Paul Bakker23986e52011-04-24 08:57:21 +00001899 int ret;
1900 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001901
1902 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1903
1904 while( ssl->in_left < nb_want )
1905 {
1906 len = nb_want - ssl->in_left;
1907 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1908
1909 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1910 ssl->in_left, nb_want ) );
1911 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1912
Paul Bakker831a7552011-05-18 13:32:51 +00001913 if( ret == 0 )
1914 return( POLARSSL_ERR_SSL_CONN_EOF );
1915
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 if( ret < 0 )
1917 return( ret );
1918
1919 ssl->in_left += ret;
1920 }
1921
1922 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1923
1924 return( 0 );
1925}
1926
1927/*
1928 * Flush any data not yet written
1929 */
1930int ssl_flush_output( ssl_context *ssl )
1931{
1932 int ret;
1933 unsigned char *buf;
1934
1935 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1936
1937 while( ssl->out_left > 0 )
1938 {
1939 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1940 5 + ssl->out_msglen, ssl->out_left ) );
1941
Paul Bakker5bd42292012-12-19 14:40:42 +01001942 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001944
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1946
1947 if( ret <= 0 )
1948 return( ret );
1949
1950 ssl->out_left -= ret;
1951 }
1952
1953 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1954
1955 return( 0 );
1956}
1957
1958/*
1959 * Record layer functions
1960 */
1961int ssl_write_record( ssl_context *ssl )
1962{
Paul Bakker05ef8352012-05-08 09:17:57 +00001963 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001964 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
1966 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1967
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1969 {
1970 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1971 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1972 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1973
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001974 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1975 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 }
1977
Paul Bakker2770fbd2012-07-03 13:30:23 +00001978#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001979 if( ssl->transform_out != NULL &&
1980 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001981 {
1982 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1983 {
1984 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1985 return( ret );
1986 }
1987
1988 len = ssl->out_msglen;
1989 }
1990#endif /*POLARSSL_ZLIB_SUPPORT */
1991
Paul Bakker05ef8352012-05-08 09:17:57 +00001992#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1993 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001995 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001996
Paul Bakker05ef8352012-05-08 09:17:57 +00001997 ret = ssl_hw_record_write( ssl );
1998 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1999 {
2000 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
2001 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2002 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002003
2004 if( ret == 0 )
2005 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002006 }
2007#endif
2008 if( !done )
2009 {
2010 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2011 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2012 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2014 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002015
Paul Bakker48916f92012-09-16 19:57:18 +00002016 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002017 {
2018 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2019 {
2020 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2021 return( ret );
2022 }
2023
2024 len = ssl->out_msglen;
2025 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2026 ssl->out_hdr[4] = (unsigned char)( len );
2027 }
2028
2029 ssl->out_left = 5 + ssl->out_msglen;
2030
2031 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2032 "version = [%d:%d], msglen = %d",
2033 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2034 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2035
2036 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002037 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 }
2039
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2041 {
2042 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2043 return( ret );
2044 }
2045
2046 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2047
2048 return( 0 );
2049}
2050
2051int ssl_read_record( ssl_context *ssl )
2052{
Paul Bakker05ef8352012-05-08 09:17:57 +00002053 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002054
2055 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2056
Paul Bakker68884e32013-01-07 18:20:04 +01002057 SSL_DEBUG_BUF( 4, "input record from network",
2058 ssl->in_hdr, 5 + ssl->in_msglen );
2059
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 if( ssl->in_hslen != 0 &&
2061 ssl->in_hslen < ssl->in_msglen )
2062 {
2063 /*
2064 * Get next Handshake message in the current record
2065 */
2066 ssl->in_msglen -= ssl->in_hslen;
2067
Paul Bakker8934a982011-08-05 11:11:53 +00002068 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002069 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
2071 ssl->in_hslen = 4;
2072 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2073
2074 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2075 " %d, type = %d, hslen = %d",
2076 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2077
2078 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2079 {
2080 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002081 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 }
2083
2084 if( ssl->in_msglen < ssl->in_hslen )
2085 {
2086 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002087 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 }
2089
Paul Bakker4224bc02014-04-08 14:36:50 +02002090 if( ssl->state != SSL_HANDSHAKE_OVER )
2091 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092
2093 return( 0 );
2094 }
2095
2096 ssl->in_hslen = 0;
2097
2098 /*
2099 * Read the record header and validate it
2100 */
2101 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2102 {
2103 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2104 return( ret );
2105 }
2106
2107 ssl->in_msgtype = ssl->in_hdr[0];
2108 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2109
2110 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2111 "version = [%d:%d], msglen = %d",
2112 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2113 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2114
2115 if( ssl->in_hdr[1] != ssl->major_ver )
2116 {
2117 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002118 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 }
2120
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002121 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 {
2123 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002124 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 }
2126
2127 /*
2128 * Make sure the message length is acceptable
2129 */
Paul Bakker48916f92012-09-16 19:57:18 +00002130 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 {
2132 if( ssl->in_msglen < 1 ||
2133 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2134 {
2135 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002136 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 }
2138 }
2139 else
2140 {
Paul Bakker48916f92012-09-16 19:57:18 +00002141 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 {
2143 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002144 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
2146
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002147#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002149 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 {
2151 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002152 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002154#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002156#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2157 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 /*
2159 * TLS encrypted messages can have up to 256 bytes of padding
2160 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002161 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002162 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 {
2164 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002165 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002167#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 }
2169
2170 /*
2171 * Read and optionally decrypt the message contents
2172 */
2173 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2174 {
2175 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2176 return( ret );
2177 }
2178
2179 SSL_DEBUG_BUF( 4, "input record from network",
2180 ssl->in_hdr, 5 + ssl->in_msglen );
2181
Paul Bakker05ef8352012-05-08 09:17:57 +00002182#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2183 if( ssl_hw_record_read != NULL)
2184 {
2185 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2186
2187 ret = ssl_hw_record_read( ssl );
2188 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2189 {
2190 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2191 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2192 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002193
2194 if( ret == 0 )
2195 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002196 }
2197#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002198 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 {
2200 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2201 {
Paul Bakker40865c82013-01-31 17:13:13 +01002202#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2203 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2204 {
2205 ssl_send_alert_message( ssl,
2206 SSL_ALERT_LEVEL_FATAL,
2207 SSL_ALERT_MSG_BAD_RECORD_MAC );
2208 }
2209#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2211 return( ret );
2212 }
2213
2214 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2215 ssl->in_msg, ssl->in_msglen );
2216
2217 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2218 {
2219 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002220 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 }
2222 }
2223
Paul Bakker2770fbd2012-07-03 13:30:23 +00002224#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002225 if( ssl->transform_in != NULL &&
2226 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002227 {
2228 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2229 {
2230 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2231 return( ret );
2232 }
2233
2234 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2235 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2236 }
2237#endif /* POLARSSL_ZLIB_SUPPORT */
2238
Paul Bakker0a925182012-04-16 06:46:41 +00002239 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2240 ssl->in_msgtype != SSL_MSG_ALERT &&
2241 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2242 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2243 {
2244 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2245
Paul Bakker48916f92012-09-16 19:57:18 +00002246 if( ( ret = ssl_send_alert_message( ssl,
2247 SSL_ALERT_LEVEL_FATAL,
2248 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002249 {
2250 return( ret );
2251 }
2252
2253 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2254 }
2255
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2257 {
2258 ssl->in_hslen = 4;
2259 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2260
2261 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2262 " %d, type = %d, hslen = %d",
2263 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2264
2265 /*
2266 * Additional checks to validate the handshake header
2267 */
2268 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2269 {
2270 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002271 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 }
2273
2274 if( ssl->in_msglen < ssl->in_hslen )
2275 {
2276 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002277 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 }
2279
Paul Bakker48916f92012-09-16 19:57:18 +00002280 if( ssl->state != SSL_HANDSHAKE_OVER )
2281 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 }
2283
2284 if( ssl->in_msgtype == SSL_MSG_ALERT )
2285 {
2286 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2287 ssl->in_msg[0], ssl->in_msg[1] ) );
2288
2289 /*
2290 * Ignore non-fatal alerts, except close_notify
2291 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002292 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002294 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2295 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002296 /**
2297 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2298 * error identifier.
2299 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002300 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 }
2302
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002303 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2304 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 {
2306 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002307 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
2309 }
2310
2311 ssl->in_left = 0;
2312
2313 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2314
2315 return( 0 );
2316}
2317
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002318int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2319{
2320 int ret;
2321
2322 if( ( ret = ssl_send_alert_message( ssl,
2323 SSL_ALERT_LEVEL_FATAL,
2324 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2325 {
2326 return( ret );
2327 }
2328
2329 return( 0 );
2330}
2331
Paul Bakker0a925182012-04-16 06:46:41 +00002332int ssl_send_alert_message( ssl_context *ssl,
2333 unsigned char level,
2334 unsigned char message )
2335{
2336 int ret;
2337
2338 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2339
2340 ssl->out_msgtype = SSL_MSG_ALERT;
2341 ssl->out_msglen = 2;
2342 ssl->out_msg[0] = level;
2343 ssl->out_msg[1] = message;
2344
2345 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2346 {
2347 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2348 return( ret );
2349 }
2350
2351 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2352
2353 return( 0 );
2354}
2355
Paul Bakker5121ce52009-01-03 21:22:43 +00002356/*
2357 * Handshake functions
2358 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002359#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2360 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2361 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2362 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2363 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2364 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2365 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002366int ssl_write_certificate( ssl_context *ssl )
2367{
Paul Bakkered27a042013-04-18 22:46:23 +02002368 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002369 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002370
2371 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2372
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002374 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2375 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002376 {
2377 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2378 ssl->state++;
2379 return( 0 );
2380 }
2381
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002382 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002383 return( ret );
2384}
2385
2386int ssl_parse_certificate( ssl_context *ssl )
2387{
2388 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2389 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2390
2391 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2392
2393 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 Bakker48f7a5d2013-04-19 14:30:58 +02002396 {
2397 SSL_DEBUG_MSG( 2, ( "<= skip parse 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#else
2406int ssl_write_certificate( ssl_context *ssl )
2407{
2408 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2409 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002410 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002411 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2412
2413 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2414
2415 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002416 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2417 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002418 {
2419 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2420 ssl->state++;
2421 return( 0 );
2422 }
2423
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 if( ssl->endpoint == SSL_IS_CLIENT )
2425 {
2426 if( ssl->client_auth == 0 )
2427 {
2428 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2429 ssl->state++;
2430 return( 0 );
2431 }
2432
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002433#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 /*
2435 * If using SSLv3 and got no cert, send an Alert message
2436 * (otherwise an empty Certificate message will be sent).
2437 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002438 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2440 {
2441 ssl->out_msglen = 2;
2442 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002443 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2444 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
2446 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2447 goto write_msg;
2448 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002449#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 }
2451 else /* SSL_IS_SERVER */
2452 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002453 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 {
2455 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002456 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 }
2458 }
2459
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002460 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
2462 /*
2463 * 0 . 0 handshake type
2464 * 1 . 3 handshake length
2465 * 4 . 6 length of all certs
2466 * 7 . 9 length of cert. 1
2467 * 10 . n-1 peer certificate
2468 * n . n+2 length of cert. 2
2469 * n+3 . ... upper level cert, etc.
2470 */
2471 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002472 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Paul Bakker29087132010-03-21 21:03:34 +00002474 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 {
2476 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002477 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 {
2479 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2480 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002481 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 }
2483
2484 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2485 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2486 ssl->out_msg[i + 2] = (unsigned char)( n );
2487
2488 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2489 i += n; crt = crt->next;
2490 }
2491
2492 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2493 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2494 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2495
2496 ssl->out_msglen = i;
2497 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2498 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2499
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002500#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002501write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002502#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
2504 ssl->state++;
2505
2506 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2507 {
2508 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2509 return( ret );
2510 }
2511
2512 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2513
Paul Bakkered27a042013-04-18 22:46:23 +02002514 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515}
2516
2517int ssl_parse_certificate( ssl_context *ssl )
2518{
Paul Bakkered27a042013-04-18 22:46:23 +02002519 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002520 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002521 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
2523 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2524
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002525 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002526 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2527 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002528 {
2529 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2530 ssl->state++;
2531 return( 0 );
2532 }
2533
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002535 ( ssl->authmode == SSL_VERIFY_NONE ||
2536 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002538 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2540 ssl->state++;
2541 return( 0 );
2542 }
2543
2544 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2545 {
2546 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2547 return( ret );
2548 }
2549
2550 ssl->state++;
2551
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002552#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 /*
2554 * Check if the client sent an empty certificate
2555 */
2556 if( ssl->endpoint == SSL_IS_SERVER &&
2557 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2558 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002559 if( ssl->in_msglen == 2 &&
2560 ssl->in_msgtype == SSL_MSG_ALERT &&
2561 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2562 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 {
2564 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2565
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002566 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2568 return( 0 );
2569 else
Paul Bakker40e46942009-01-03 21:51:57 +00002570 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 }
2572 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002573#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002575#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2576 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 if( ssl->endpoint == SSL_IS_SERVER &&
2578 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2579 {
2580 if( ssl->in_hslen == 7 &&
2581 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2582 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2583 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2584 {
2585 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2586
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002587 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002589 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 else
2591 return( 0 );
2592 }
2593 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002594#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2595 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
2597 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2598 {
2599 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002600 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
2602
2603 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2604 {
2605 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002606 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607 }
2608
2609 /*
2610 * Same message structure as in ssl_write_certificate()
2611 */
2612 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2613
2614 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2615 {
2616 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002617 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002620 /* In case we tried to reuse a session but it failed */
2621 if( ssl->session_negotiate->peer_cert != NULL )
2622 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002623 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002624 polarssl_free( ssl->session_negotiate->peer_cert );
2625 }
2626
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002627 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2628 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 {
2630 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002631 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002632 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 }
2634
Paul Bakkerb6b09562013-09-18 14:17:41 +02002635 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
2637 i = 7;
2638
2639 while( i < ssl->in_hslen )
2640 {
2641 if( ssl->in_msg[i] != 0 )
2642 {
2643 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002644 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 }
2646
2647 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2648 | (unsigned int) ssl->in_msg[i + 2];
2649 i += 3;
2650
2651 if( n < 128 || i + n > ssl->in_hslen )
2652 {
2653 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002654 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
Paul Bakkerddf26b42013-09-18 13:46:23 +02002657 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2658 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 if( ret != 0 )
2660 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002661 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 return( ret );
2663 }
2664
2665 i += n;
2666 }
2667
Paul Bakker48916f92012-09-16 19:57:18 +00002668 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002670 /*
2671 * On client, make sure the server cert doesn't change during renego to
2672 * avoid "triple handshake" attack: https://secure-resumption.com/
2673 */
2674 if( ssl->endpoint == SSL_IS_CLIENT &&
2675 ssl->renegotiation == SSL_RENEGOTIATION )
2676 {
2677 if( ssl->session->peer_cert == NULL )
2678 {
2679 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2680 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2681 }
2682
2683 if( ssl->session->peer_cert->raw.len !=
2684 ssl->session_negotiate->peer_cert->raw.len ||
2685 memcmp( ssl->session->peer_cert->raw.p,
2686 ssl->session_negotiate->peer_cert->raw.p,
2687 ssl->session->peer_cert->raw.len ) != 0 )
2688 {
2689 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2690 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2691 }
2692 }
2693
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 if( ssl->authmode != SSL_VERIFY_NONE )
2695 {
2696 if( ssl->ca_chain == NULL )
2697 {
2698 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002699 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 }
2701
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002702 /*
2703 * Main check: verify certificate
2704 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002705 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2706 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2707 &ssl->session_negotiate->verify_result,
2708 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
2710 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002711 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002713 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002714
2715 /*
2716 * Secondary checks: always done, but change 'ret' only if it was 0
2717 */
2718
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002719#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002720 {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002721 const pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002722
2723 /* If certificate uses an EC key, make sure the curve is OK */
2724 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2725 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2726 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002727 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002728 if( ret == 0 )
2729 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002730 }
2731 }
2732#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002734 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2735 ciphersuite_info,
2736 ! ssl->endpoint ) != 0 )
2737 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002738 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002739 if( ret == 0 )
2740 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2741 }
2742
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2744 ret = 0;
2745 }
2746
2747 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2748
2749 return( ret );
2750}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002751#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2752 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2753 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2754 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2755 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2756 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2757 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
2759int ssl_write_change_cipher_spec( ssl_context *ssl )
2760{
2761 int ret;
2762
2763 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2764
2765 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2766 ssl->out_msglen = 1;
2767 ssl->out_msg[0] = 1;
2768
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 ssl->state++;
2770
2771 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2772 {
2773 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2774 return( ret );
2775 }
2776
2777 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2778
2779 return( 0 );
2780}
2781
2782int ssl_parse_change_cipher_spec( ssl_context *ssl )
2783{
2784 int ret;
2785
2786 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2787
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2789 {
2790 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2791 return( ret );
2792 }
2793
2794 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2795 {
2796 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002797 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 }
2799
2800 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2801 {
2802 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002803 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002804 }
2805
2806 ssl->state++;
2807
2808 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2809
2810 return( 0 );
2811}
2812
Paul Bakker41c83d32013-03-20 14:39:14 +01002813void ssl_optimize_checksum( ssl_context *ssl,
2814 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002815{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002816 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002817
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002818#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2819 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002820 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002821 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002822 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002823#endif
2824#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2825#if defined(POLARSSL_SHA512_C)
2826 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2827 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2828 else
2829#endif
2830#if defined(POLARSSL_SHA256_C)
2831 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002832 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002833 else
2834#endif
2835#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2836 /* Should never happen */
2837 return;
Paul Bakker380da532012-04-18 16:10:25 +00002838}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002839
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002840static void ssl_update_checksum_start( ssl_context *ssl,
2841 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002842{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002843#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2844 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002845 md5_update( &ssl->handshake->fin_md5 , buf, len );
2846 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002847#endif
2848#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2849#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002850 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002851#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002852#if defined(POLARSSL_SHA512_C)
2853 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002854#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002855#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002856}
2857
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002858#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2859 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002860static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2861 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002862{
Paul Bakker48916f92012-09-16 19:57:18 +00002863 md5_update( &ssl->handshake->fin_md5 , buf, len );
2864 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002865}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002866#endif
Paul Bakker380da532012-04-18 16:10:25 +00002867
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002868#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2869#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002870static void ssl_update_checksum_sha256( ssl_context *ssl,
2871 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002872{
Paul Bakker9e36f042013-06-30 14:34:05 +02002873 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002874}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002875#endif
Paul Bakker380da532012-04-18 16:10:25 +00002876
Paul Bakker9e36f042013-06-30 14:34:05 +02002877#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002878static void ssl_update_checksum_sha384( ssl_context *ssl,
2879 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002880{
Paul Bakker9e36f042013-06-30 14:34:05 +02002881 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002882}
Paul Bakker769075d2012-11-24 11:26:46 +01002883#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002884#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002885
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002886#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002887static void ssl_calc_finished_ssl(
2888 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002889{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002890 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002891 md5_context md5;
2892 sha1_context sha1;
2893
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 unsigned char padbuf[48];
2895 unsigned char md5sum[16];
2896 unsigned char sha1sum[20];
2897
Paul Bakker48916f92012-09-16 19:57:18 +00002898 ssl_session *session = ssl->session_negotiate;
2899 if( !session )
2900 session = ssl->session;
2901
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2903
Paul Bakker48916f92012-09-16 19:57:18 +00002904 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2905 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
2907 /*
2908 * SSLv3:
2909 * hash =
2910 * MD5( master + pad2 +
2911 * MD5( handshake + sender + master + pad1 ) )
2912 * + SHA1( master + pad2 +
2913 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 */
2915
Paul Bakker90995b52013-06-24 19:20:35 +02002916#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002919#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Paul Bakker90995b52013-06-24 19:20:35 +02002921#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002924#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Paul Bakker3c2122f2013-06-24 19:03:14 +02002926 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2927 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Paul Bakker1ef83d62012-04-11 12:09:53 +00002929 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Paul Bakker3c2122f2013-06-24 19:03:14 +02002931 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002932 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002933 md5_update( &md5, padbuf, 48 );
2934 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Paul Bakker3c2122f2013-06-24 19:03:14 +02002936 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002937 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002938 sha1_update( &sha1, padbuf, 40 );
2939 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Paul Bakker1ef83d62012-04-11 12:09:53 +00002941 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002944 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945 md5_update( &md5, padbuf, 48 );
2946 md5_update( &md5, md5sum, 16 );
2947 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002950 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951 sha1_update( &sha1, padbuf , 40 );
2952 sha1_update( &sha1, sha1sum, 20 );
2953 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
Paul Bakker1ef83d62012-04-11 12:09:53 +00002957 memset( &md5, 0, sizeof( md5_context ) );
2958 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
2960 memset( padbuf, 0, sizeof( padbuf ) );
2961 memset( md5sum, 0, sizeof( md5sum ) );
2962 memset( sha1sum, 0, sizeof( sha1sum ) );
2963
2964 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2965}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002966#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002968#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969static void ssl_calc_finished_tls(
2970 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002971{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002973 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002975 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002977
Paul Bakker48916f92012-09-16 19:57:18 +00002978 ssl_session *session = ssl->session_negotiate;
2979 if( !session )
2980 session = ssl->session;
2981
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002983
Paul Bakker48916f92012-09-16 19:57:18 +00002984 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2985 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Paul Bakker1ef83d62012-04-11 12:09:53 +00002987 /*
2988 * TLSv1:
2989 * hash = PRF( master, finished_label,
2990 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2991 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002992
Paul Bakker90995b52013-06-24 19:20:35 +02002993#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2995 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002996#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002997
Paul Bakker90995b52013-06-24 19:20:35 +02002998#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002999 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3000 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003001#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003002
3003 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003004 ? "client finished"
3005 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003006
3007 md5_finish( &md5, padbuf );
3008 sha1_finish( &sha1, padbuf + 16 );
3009
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003010 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003011 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003012
3013 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3014
3015 memset( &md5, 0, sizeof( md5_context ) );
3016 memset( &sha1, 0, sizeof( sha1_context ) );
3017
3018 memset( padbuf, 0, sizeof( padbuf ) );
3019
3020 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3021}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003022#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003023
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003024#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3025#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003026static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003027 ssl_context *ssl, unsigned char *buf, int from )
3028{
3029 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003030 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003031 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 unsigned char padbuf[32];
3033
Paul Bakker48916f92012-09-16 19:57:18 +00003034 ssl_session *session = ssl->session_negotiate;
3035 if( !session )
3036 session = ssl->session;
3037
Paul Bakker380da532012-04-18 16:10:25 +00003038 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003039
Paul Bakker9e36f042013-06-30 14:34:05 +02003040 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003041
3042 /*
3043 * TLSv1.2:
3044 * hash = PRF( master, finished_label,
3045 * Hash( handshake ) )[0.11]
3046 */
3047
Paul Bakker9e36f042013-06-30 14:34:05 +02003048#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003049 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003050 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003051#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003052
3053 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003054 ? "client finished"
3055 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003056
Paul Bakker9e36f042013-06-30 14:34:05 +02003057 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003058
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003059 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003060 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003061
3062 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3063
Paul Bakker9e36f042013-06-30 14:34:05 +02003064 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003065
3066 memset( padbuf, 0, sizeof( padbuf ) );
3067
3068 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3069}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003070#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003071
Paul Bakker9e36f042013-06-30 14:34:05 +02003072#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003073static void ssl_calc_finished_tls_sha384(
3074 ssl_context *ssl, unsigned char *buf, int from )
3075{
3076 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003077 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003078 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003079 unsigned char padbuf[48];
3080
Paul Bakker48916f92012-09-16 19:57:18 +00003081 ssl_session *session = ssl->session_negotiate;
3082 if( !session )
3083 session = ssl->session;
3084
Paul Bakker380da532012-04-18 16:10:25 +00003085 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003086
Paul Bakker9e36f042013-06-30 14:34:05 +02003087 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003088
3089 /*
3090 * TLSv1.2:
3091 * hash = PRF( master, finished_label,
3092 * Hash( handshake ) )[0.11]
3093 */
3094
Paul Bakker9e36f042013-06-30 14:34:05 +02003095#if !defined(POLARSSL_SHA512_ALT)
3096 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3097 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003098#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003099
3100 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003101 ? "client finished"
3102 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003103
Paul Bakker9e36f042013-06-30 14:34:05 +02003104 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003105
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003106 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003107 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003108
3109 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3110
Paul Bakker9e36f042013-06-30 14:34:05 +02003111 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003112
3113 memset( padbuf, 0, sizeof( padbuf ) );
3114
3115 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3116}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003117#endif /* POLARSSL_SHA512_C */
3118#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003119
Paul Bakker48916f92012-09-16 19:57:18 +00003120void ssl_handshake_wrapup( ssl_context *ssl )
3121{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003122 int resume = ssl->handshake->resume;
3123
Paul Bakker48916f92012-09-16 19:57:18 +00003124 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3125
3126 /*
3127 * Free our handshake params
3128 */
3129 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003130 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003131 ssl->handshake = NULL;
3132
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003133 if( ssl->renegotiation == SSL_RENEGOTIATION )
3134 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3135
Paul Bakker48916f92012-09-16 19:57:18 +00003136 /*
3137 * Switch in our now active transform context
3138 */
3139 if( ssl->transform )
3140 {
3141 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003142 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003143 }
3144 ssl->transform = ssl->transform_negotiate;
3145 ssl->transform_negotiate = NULL;
3146
Paul Bakker0a597072012-09-25 21:55:46 +00003147 if( ssl->session )
3148 {
3149 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003150 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003151 }
3152 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003153 ssl->session_negotiate = NULL;
3154
Paul Bakker0a597072012-09-25 21:55:46 +00003155 /*
3156 * Add cache entry
3157 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003158 if( ssl->f_set_cache != NULL &&
3159 ssl->session->length != 0 &&
3160 resume == 0 )
3161 {
Paul Bakker0a597072012-09-25 21:55:46 +00003162 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3163 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003164 }
Paul Bakker0a597072012-09-25 21:55:46 +00003165
Paul Bakker48916f92012-09-16 19:57:18 +00003166 ssl->state++;
3167
3168 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3169}
3170
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171int ssl_write_finished( ssl_context *ssl )
3172{
3173 int ret, hash_len;
3174
3175 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3176
Paul Bakker92be97b2013-01-02 17:30:03 +01003177 /*
3178 * Set the out_msg pointer to the correct location based on IV length
3179 */
3180 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3181 {
3182 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3183 ssl->transform_negotiate->fixed_ivlen;
3184 }
3185 else
3186 ssl->out_msg = ssl->out_iv;
3187
Paul Bakker48916f92012-09-16 19:57:18 +00003188 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003189
3190 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3192
Paul Bakker48916f92012-09-16 19:57:18 +00003193 ssl->verify_data_len = hash_len;
3194 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3195
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 ssl->out_msglen = 4 + hash_len;
3197 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3198 ssl->out_msg[0] = SSL_HS_FINISHED;
3199
3200 /*
3201 * In case of session resuming, invert the client and server
3202 * ChangeCipherSpec messages order.
3203 */
Paul Bakker0a597072012-09-25 21:55:46 +00003204 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 {
3206 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003207 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 else
3209 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3210 }
3211 else
3212 ssl->state++;
3213
Paul Bakker48916f92012-09-16 19:57:18 +00003214 /*
3215 * Switch to our negotiated transform and session parameters for outbound data.
3216 */
3217 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3218 ssl->transform_out = ssl->transform_negotiate;
3219 ssl->session_out = ssl->session_negotiate;
3220 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Paul Bakker07eb38b2012-12-19 14:42:06 +01003222#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3223 if( ssl_hw_record_activate != NULL)
3224 {
3225 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3226 {
3227 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3228 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3229 }
3230 }
3231#endif
3232
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3234 {
3235 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3236 return( ret );
3237 }
3238
3239 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3240
3241 return( 0 );
3242}
3243
3244int ssl_parse_finished( ssl_context *ssl )
3245{
Paul Bakker23986e52011-04-24 08:57:21 +00003246 int ret;
3247 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 unsigned char buf[36];
3249
3250 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3251
Paul Bakker48916f92012-09-16 19:57:18 +00003252 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Paul Bakker48916f92012-09-16 19:57:18 +00003254 /*
3255 * Switch to our negotiated transform and session parameters for inbound data.
3256 */
3257 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3258 ssl->transform_in = ssl->transform_negotiate;
3259 ssl->session_in = ssl->session_negotiate;
3260 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
Paul Bakker92be97b2013-01-02 17:30:03 +01003262 /*
3263 * Set the in_msg pointer to the correct location based on IV length
3264 */
3265 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3266 {
3267 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3268 ssl->transform_negotiate->fixed_ivlen;
3269 }
3270 else
3271 ssl->in_msg = ssl->in_iv;
3272
Paul Bakker07eb38b2012-12-19 14:42:06 +01003273#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3274 if( ssl_hw_record_activate != NULL)
3275 {
3276 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3277 {
3278 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3279 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3280 }
3281 }
3282#endif
3283
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3285 {
3286 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3287 return( ret );
3288 }
3289
3290 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3291 {
3292 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003293 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 }
3295
Paul Bakker1ef83d62012-04-11 12:09:53 +00003296 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3298
3299 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3300 ssl->in_hslen != 4 + hash_len )
3301 {
3302 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003303 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 }
3305
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003306 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 {
3308 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003309 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 }
3311
Paul Bakker48916f92012-09-16 19:57:18 +00003312 ssl->verify_data_len = hash_len;
3313 memcpy( ssl->peer_verify_data, buf, hash_len );
3314
Paul Bakker0a597072012-09-25 21:55:46 +00003315 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003316 {
3317 if( ssl->endpoint == SSL_IS_CLIENT )
3318 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3319
3320 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003321 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003322 }
3323 else
3324 ssl->state++;
3325
3326 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3327
3328 return( 0 );
3329}
3330
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003331static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003332{
3333 if( ssl->transform_negotiate )
3334 ssl_transform_free( ssl->transform_negotiate );
3335 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003336 {
3337 ssl->transform_negotiate =
3338 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003339
3340 if( ssl->transform_negotiate != NULL )
3341 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003342 }
Paul Bakker48916f92012-09-16 19:57:18 +00003343
3344 if( ssl->session_negotiate )
3345 ssl_session_free( ssl->session_negotiate );
3346 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003347 {
3348 ssl->session_negotiate =
3349 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003350
3351 if( ssl->session_negotiate != NULL )
3352 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003353 }
Paul Bakker48916f92012-09-16 19:57:18 +00003354
3355 if( ssl->handshake )
3356 ssl_handshake_free( ssl->handshake );
3357 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003358 {
3359 ssl->handshake = (ssl_handshake_params *)
3360 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003361
3362 if( ssl->handshake != NULL )
3363 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003364 }
Paul Bakker48916f92012-09-16 19:57:18 +00003365
3366 if( ssl->handshake == NULL ||
3367 ssl->transform_negotiate == NULL ||
3368 ssl->session_negotiate == NULL )
3369 {
3370 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3371 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3372 }
3373
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003374#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3375 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003376 md5_starts( &ssl->handshake->fin_md5 );
3377 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003378#endif
3379#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3380#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003381 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003382#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003383#if defined(POLARSSL_SHA512_C)
3384 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003385#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003386#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003387
3388 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003389 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003390
Paul Bakker61d113b2013-07-04 11:51:43 +02003391#if defined(POLARSSL_ECDH_C)
3392 ecdh_init( &ssl->handshake->ecdh_ctx );
3393#endif
3394
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003395#if defined(POLARSSL_X509_CRT_PARSE_C)
3396 ssl->handshake->key_cert = ssl->key_cert;
3397#endif
3398
Paul Bakker48916f92012-09-16 19:57:18 +00003399 return( 0 );
3400}
3401
Paul Bakker5121ce52009-01-03 21:22:43 +00003402/*
3403 * Initialize an SSL context
3404 */
3405int ssl_init( ssl_context *ssl )
3406{
Paul Bakker48916f92012-09-16 19:57:18 +00003407 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 int len = SSL_BUFFER_LEN;
3409
3410 memset( ssl, 0, sizeof( ssl_context ) );
3411
Paul Bakker62f2dee2012-09-28 07:31:51 +00003412 /*
3413 * Sane defaults
3414 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003415 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3416 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3417 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3418 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003419
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003420 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003421
Paul Bakker62f2dee2012-09-28 07:31:51 +00003422#if defined(POLARSSL_DHM_C)
3423 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3424 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3425 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3426 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3427 {
3428 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3429 return( ret );
3430 }
3431#endif
3432
3433 /*
3434 * Prepare base structures
3435 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003436 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003437 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003438 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003439 ssl->in_msg = ssl->in_ctr + 13;
3440
3441 if( ssl->in_ctr == NULL )
3442 {
3443 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003444 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003445 }
3446
Paul Bakker6e339b52013-07-03 13:37:05 +02003447 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003449 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003450 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003451
3452 if( ssl->out_ctr == NULL )
3453 {
3454 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003455 polarssl_free( ssl->in_ctr );
3456 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003457 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003458 }
3459
3460 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3461 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3462
Paul Bakker606b4ba2013-08-14 16:52:14 +02003463#if defined(POLARSSL_SSL_SESSION_TICKETS)
3464 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3465#endif
3466
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003467#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003468 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003469#endif
3470
Paul Bakker48916f92012-09-16 19:57:18 +00003471 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3472 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003473
3474 return( 0 );
3475}
3476
3477/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003478 * Reset an initialized and used SSL context for re-use while retaining
3479 * all application-set variables, function pointers and data.
3480 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003481int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003482{
Paul Bakker48916f92012-09-16 19:57:18 +00003483 int ret;
3484
Paul Bakker7eb013f2011-10-06 12:37:39 +00003485 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003486 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3487 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3488
3489 ssl->verify_data_len = 0;
3490 memset( ssl->own_verify_data, 0, 36 );
3491 memset( ssl->peer_verify_data, 0, 36 );
3492
Paul Bakker7eb013f2011-10-06 12:37:39 +00003493 ssl->in_offt = NULL;
3494
Paul Bakker92be97b2013-01-02 17:30:03 +01003495 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003496 ssl->in_msgtype = 0;
3497 ssl->in_msglen = 0;
3498 ssl->in_left = 0;
3499
3500 ssl->in_hslen = 0;
3501 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003502 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003503
Paul Bakker92be97b2013-01-02 17:30:03 +01003504 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003505 ssl->out_msgtype = 0;
3506 ssl->out_msglen = 0;
3507 ssl->out_left = 0;
3508
Paul Bakker48916f92012-09-16 19:57:18 +00003509 ssl->transform_in = NULL;
3510 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003511
3512 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3513 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003514
3515#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3516 if( ssl_hw_record_reset != NULL)
3517 {
3518 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003519 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520 {
3521 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3522 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3523 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003524 }
3525#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003526
Paul Bakker48916f92012-09-16 19:57:18 +00003527 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003528 {
Paul Bakker48916f92012-09-16 19:57:18 +00003529 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003530 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003531 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003532 }
Paul Bakker48916f92012-09-16 19:57:18 +00003533
Paul Bakkerc0463502013-02-14 11:19:38 +01003534 if( ssl->session )
3535 {
3536 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003537 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003538 ssl->session = NULL;
3539 }
3540
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003541#if defined(POLARSSL_SSL_ALPN)
3542 ssl->alpn_chosen = NULL;
3543#endif
3544
Paul Bakker48916f92012-09-16 19:57:18 +00003545 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3546 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003547
3548 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003549}
3550
Paul Bakkera503a632013-08-14 13:48:06 +02003551#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003552/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003553 * Allocate and initialize ticket keys
3554 */
3555static int ssl_ticket_keys_init( ssl_context *ssl )
3556{
3557 int ret;
3558 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003559 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003560
3561 if( ssl->ticket_keys != NULL )
3562 return( 0 );
3563
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003564 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3565 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003566 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3567
3568 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003569 {
3570 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003571 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003572 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003573
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003574 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3575 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3576 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3577 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003578 polarssl_free( tkeys );
3579 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003580 }
3581
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003582 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003583 {
3584 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003585 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003586 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003587
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003588 ssl->ticket_keys = tkeys;
3589
3590 return( 0 );
3591}
Paul Bakkera503a632013-08-14 13:48:06 +02003592#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003593
3594/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 * SSL set accessors
3596 */
3597void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3598{
3599 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003600
Paul Bakker606b4ba2013-08-14 16:52:14 +02003601#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003602 if( endpoint == SSL_IS_CLIENT )
3603 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003605}
3606
3607void ssl_set_authmode( ssl_context *ssl, int authmode )
3608{
3609 ssl->authmode = authmode;
3610}
3611
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003612#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003613void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003614 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003615 void *p_vrfy )
3616{
3617 ssl->f_vrfy = f_vrfy;
3618 ssl->p_vrfy = p_vrfy;
3619}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003620#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003621
Paul Bakker5121ce52009-01-03 21:22:43 +00003622void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003623 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 void *p_rng )
3625{
3626 ssl->f_rng = f_rng;
3627 ssl->p_rng = p_rng;
3628}
3629
3630void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003631 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003632 void *p_dbg )
3633{
3634 ssl->f_dbg = f_dbg;
3635 ssl->p_dbg = p_dbg;
3636}
3637
3638void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003639 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003640 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003641{
3642 ssl->f_recv = f_recv;
3643 ssl->f_send = f_send;
3644 ssl->p_recv = p_recv;
3645 ssl->p_send = p_send;
3646}
3647
Paul Bakker0a597072012-09-25 21:55:46 +00003648void ssl_set_session_cache( ssl_context *ssl,
3649 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3650 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003651{
Paul Bakker0a597072012-09-25 21:55:46 +00003652 ssl->f_get_cache = f_get_cache;
3653 ssl->p_get_cache = p_get_cache;
3654 ssl->f_set_cache = f_set_cache;
3655 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003656}
3657
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003658int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003659{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003660 int ret;
3661
3662 if( ssl == NULL ||
3663 session == NULL ||
3664 ssl->session_negotiate == NULL ||
3665 ssl->endpoint != SSL_IS_CLIENT )
3666 {
3667 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3668 }
3669
3670 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3671 return( ret );
3672
Paul Bakker0a597072012-09-25 21:55:46 +00003673 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003674
3675 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003676}
3677
Paul Bakkerb68cad62012-08-23 08:34:18 +00003678void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003679{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003680 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3681 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3682 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3683 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3684}
3685
3686void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3687 int major, int minor )
3688{
3689 if( major != SSL_MAJOR_VERSION_3 )
3690 return;
3691
3692 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3693 return;
3694
3695 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003696}
3697
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003698#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003699/* Add a new (empty) key_cert entry an return a pointer to it */
3700static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3701{
3702 ssl_key_cert *key_cert, *last;
3703
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003704 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3705 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003706 return( NULL );
3707
3708 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3709
3710 /* Append the new key_cert to the (possibly empty) current list */
3711 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003712 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003713 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003714 if( ssl->handshake != NULL )
3715 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003716 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003717 else
3718 {
3719 last = ssl->key_cert;
3720 while( last->next != NULL )
3721 last = last->next;
3722 last->next = key_cert;
3723 }
3724
3725 return key_cert;
3726}
3727
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003728void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003729 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003730{
3731 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003732 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003733 ssl->peer_cn = peer_cn;
3734}
3735
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003736int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003737 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003738{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003739 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3740
3741 if( key_cert == NULL )
3742 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3743
3744 key_cert->cert = own_cert;
3745 key_cert->key = pk_key;
3746
3747 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003748}
3749
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003750#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003751int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003752 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003753{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003754 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003755 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003756
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003757 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003758 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3759
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003760 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3761 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003762 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003763
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003764 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003765
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003766 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003767 if( ret != 0 )
3768 return( ret );
3769
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003770 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003771 return( ret );
3772
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003773 key_cert->cert = own_cert;
3774 key_cert->key_own_alloc = 1;
3775
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003776 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003778#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003779
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003780int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003781 void *rsa_key,
3782 rsa_decrypt_func rsa_decrypt,
3783 rsa_sign_func rsa_sign,
3784 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003785{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003786 int ret;
3787 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003788
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003789 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003790 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3791
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003792 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3793 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003794 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003795
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003796 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003797
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003798 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3799 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3800 return( ret );
3801
3802 key_cert->cert = own_cert;
3803 key_cert->key_own_alloc = 1;
3804
3805 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003806}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003807#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003808
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003809#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003810int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3811 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003812{
Paul Bakker6db455e2013-09-18 17:29:31 +02003813 if( psk == NULL || psk_identity == NULL )
3814 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3815
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003816 /*
3817 * The length will be check later anyway, but in case it is obviously
3818 * too large, better abort now. The PMS is as follows:
3819 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3820 */
3821 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3822 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3823
Paul Bakker6db455e2013-09-18 17:29:31 +02003824 if( ssl->psk != NULL )
3825 {
3826 polarssl_free( ssl->psk );
3827 polarssl_free( ssl->psk_identity );
3828 }
3829
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003830 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003831 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003832
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003833 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3834 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003835
3836 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3837 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3838
3839 memcpy( ssl->psk, psk, ssl->psk_len );
3840 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003841
3842 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003843}
3844
3845void ssl_set_psk_cb( ssl_context *ssl,
3846 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3847 size_t),
3848 void *p_psk )
3849{
3850 ssl->f_psk = f_psk;
3851 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003852}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003853#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003854
Paul Bakker48916f92012-09-16 19:57:18 +00003855#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003856int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003857{
3858 int ret;
3859
Paul Bakker48916f92012-09-16 19:57:18 +00003860 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003861 {
3862 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3863 return( ret );
3864 }
3865
Paul Bakker48916f92012-09-16 19:57:18 +00003866 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003867 {
3868 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3869 return( ret );
3870 }
3871
3872 return( 0 );
3873}
3874
Paul Bakker1b57b062011-01-06 15:48:19 +00003875int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3876{
3877 int ret;
3878
Paul Bakker48916f92012-09-16 19:57:18 +00003879 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003880 {
3881 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3882 return( ret );
3883 }
3884
Paul Bakker48916f92012-09-16 19:57:18 +00003885 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003886 {
3887 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3888 return( ret );
3889 }
3890
3891 return( 0 );
3892}
Paul Bakker48916f92012-09-16 19:57:18 +00003893#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003894
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003895#if defined(POLARSSL_SSL_SET_CURVES)
3896/*
3897 * Set the allowed elliptic curves
3898 */
3899void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3900{
3901 ssl->curve_list = curve_list;
3902}
3903#endif
3904
Paul Bakker0be444a2013-08-27 21:55:01 +02003905#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003906int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003907{
3908 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003909 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003910
3911 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003912
3913 if( ssl->hostname_len + 1 == 0 )
3914 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3915
Paul Bakker6e339b52013-07-03 13:37:05 +02003916 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003917
Paul Bakkerb15b8512012-01-13 13:44:06 +00003918 if( ssl->hostname == NULL )
3919 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3920
Paul Bakker3c2122f2013-06-24 19:03:14 +02003921 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003922 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003923
Paul Bakker40ea7de2009-05-03 10:18:48 +00003924 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003925
3926 return( 0 );
3927}
3928
Paul Bakker5701cdc2012-09-27 21:49:42 +00003929void ssl_set_sni( ssl_context *ssl,
3930 int (*f_sni)(void *, ssl_context *,
3931 const unsigned char *, size_t),
3932 void *p_sni )
3933{
3934 ssl->f_sni = f_sni;
3935 ssl->p_sni = p_sni;
3936}
Paul Bakker0be444a2013-08-27 21:55:01 +02003937#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003938
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003939#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003940int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003941{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003942 size_t cur_len, tot_len;
3943 const char **p;
3944
3945 /*
3946 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3947 * truncated". Check lengths now rather than later.
3948 */
3949 tot_len = 0;
3950 for( p = protos; *p != NULL; p++ )
3951 {
3952 cur_len = strlen( *p );
3953 tot_len += cur_len;
3954
3955 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3956 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3957 }
3958
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003959 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003960
3961 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003962}
3963
3964const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3965{
3966 return ssl->alpn_chosen;
3967}
3968#endif /* POLARSSL_SSL_ALPN */
3969
Paul Bakker490ecc82011-10-06 13:04:09 +00003970void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3971{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003972 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3973 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3974 {
3975 ssl->max_major_ver = major;
3976 ssl->max_minor_ver = minor;
3977 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003978}
3979
Paul Bakker1d29fb52012-09-28 13:28:45 +00003980void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3981{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003982 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3983 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3984 {
3985 ssl->min_major_ver = major;
3986 ssl->min_minor_ver = minor;
3987 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003988}
3989
Paul Bakker05decb22013-08-15 13:33:48 +02003990#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003991int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3992{
Paul Bakker77e257e2013-12-16 15:29:52 +01003993 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003994 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003995 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003996 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003997 }
3998
3999 ssl->mfl_code = mfl_code;
4000
4001 return( 0 );
4002}
Paul Bakker05decb22013-08-15 13:33:48 +02004003#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004004
Paul Bakker1f2bc622013-08-15 13:45:55 +02004005#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004006int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004007{
4008 if( ssl->endpoint != SSL_IS_CLIENT )
4009 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4010
Paul Bakker8c1ede62013-07-19 14:14:37 +02004011 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004012
4013 return( 0 );
4014}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004015#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004016
Paul Bakker48916f92012-09-16 19:57:18 +00004017void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4018{
4019 ssl->disable_renegotiation = renegotiation;
4020}
4021
4022void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4023{
4024 ssl->allow_legacy_renegotiation = allow_legacy;
4025}
4026
Paul Bakkera503a632013-08-14 13:48:06 +02004027#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004028int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4029{
4030 ssl->session_tickets = use_tickets;
4031
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004032 if( ssl->endpoint == SSL_IS_CLIENT )
4033 return( 0 );
4034
4035 if( ssl->f_rng == NULL )
4036 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4037
4038 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004039}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004040
4041void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4042{
4043 ssl->ticket_lifetime = lifetime;
4044}
Paul Bakkera503a632013-08-14 13:48:06 +02004045#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004046
Paul Bakker5121ce52009-01-03 21:22:43 +00004047/*
4048 * SSL get accessors
4049 */
Paul Bakker23986e52011-04-24 08:57:21 +00004050size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004051{
4052 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4053}
4054
Paul Bakkerff60ee62010-03-16 21:09:09 +00004055int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004056{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004057 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004058}
4059
Paul Bakkere3166ce2011-01-27 17:40:50 +00004060const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004061{
Paul Bakker926c8e42013-03-06 10:23:34 +01004062 if( ssl == NULL || ssl->session == NULL )
4063 return NULL;
4064
Paul Bakkere3166ce2011-01-27 17:40:50 +00004065 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004066}
4067
Paul Bakker43ca69c2011-01-15 17:35:19 +00004068const char *ssl_get_version( const ssl_context *ssl )
4069{
4070 switch( ssl->minor_ver )
4071 {
4072 case SSL_MINOR_VERSION_0:
4073 return( "SSLv3.0" );
4074
4075 case SSL_MINOR_VERSION_1:
4076 return( "TLSv1.0" );
4077
4078 case SSL_MINOR_VERSION_2:
4079 return( "TLSv1.1" );
4080
Paul Bakker1ef83d62012-04-11 12:09:53 +00004081 case SSL_MINOR_VERSION_3:
4082 return( "TLSv1.2" );
4083
Paul Bakker43ca69c2011-01-15 17:35:19 +00004084 default:
4085 break;
4086 }
4087 return( "unknown" );
4088}
4089
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004090#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004091const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004092{
4093 if( ssl == NULL || ssl->session == NULL )
4094 return NULL;
4095
4096 return ssl->session->peer_cert;
4097}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004098#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004099
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004100int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4101{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004102 if( ssl == NULL ||
4103 dst == NULL ||
4104 ssl->session == NULL ||
4105 ssl->endpoint != SSL_IS_CLIENT )
4106 {
4107 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4108 }
4109
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004110 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004111}
4112
Paul Bakker5121ce52009-01-03 21:22:43 +00004113/*
Paul Bakker1961b702013-01-25 14:49:24 +01004114 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004115 */
Paul Bakker1961b702013-01-25 14:49:24 +01004116int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004117{
Paul Bakker40e46942009-01-03 21:51:57 +00004118 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004119
Paul Bakker40e46942009-01-03 21:51:57 +00004120#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004121 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004122 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004123#endif
4124
Paul Bakker40e46942009-01-03 21:51:57 +00004125#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004126 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004127 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004128#endif
4129
Paul Bakker1961b702013-01-25 14:49:24 +01004130 return( ret );
4131}
4132
4133/*
4134 * Perform the SSL handshake
4135 */
4136int ssl_handshake( ssl_context *ssl )
4137{
4138 int ret = 0;
4139
4140 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4141
4142 while( ssl->state != SSL_HANDSHAKE_OVER )
4143 {
4144 ret = ssl_handshake_step( ssl );
4145
4146 if( ret != 0 )
4147 break;
4148 }
4149
Paul Bakker5121ce52009-01-03 21:22:43 +00004150 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4151
4152 return( ret );
4153}
4154
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004155#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004156/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004157 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004158 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004159static int ssl_write_hello_request( ssl_context *ssl )
4160{
4161 int ret;
4162
4163 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4164
4165 ssl->out_msglen = 4;
4166 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4167 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4168
4169 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4170 {
4171 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4172 return( ret );
4173 }
4174
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004175 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4176
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004177 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4178
4179 return( 0 );
4180}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004181#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004182
4183/*
4184 * Actually renegotiate current connection, triggered by either:
4185 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004186 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004187 * - receiving any handshake message on server during ssl_read() after the
4188 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004189 * If the handshake doesn't complete due to waiting for I/O, it will continue
4190 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004191 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004192static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004193{
4194 int ret;
4195
4196 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4197
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004198 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4199 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004200
4201 ssl->state = SSL_HELLO_REQUEST;
4202 ssl->renegotiation = SSL_RENEGOTIATION;
4203
Paul Bakker48916f92012-09-16 19:57:18 +00004204 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4205 {
4206 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4207 return( ret );
4208 }
4209
4210 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4211
4212 return( 0 );
4213}
4214
4215/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004216 * Renegotiate current connection on client,
4217 * or request renegotiation on server
4218 */
4219int ssl_renegotiate( ssl_context *ssl )
4220{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004221 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004222
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004223#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004224 /* On server, just send the request */
4225 if( ssl->endpoint == SSL_IS_SERVER )
4226 {
4227 if( ssl->state != SSL_HANDSHAKE_OVER )
4228 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4229
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004230 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004231 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004232#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004233
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004234#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004235 /*
4236 * On client, either start the renegotiation process or,
4237 * if already in progress, continue the handshake
4238 */
4239 if( ssl->renegotiation != SSL_RENEGOTIATION )
4240 {
4241 if( ssl->state != SSL_HANDSHAKE_OVER )
4242 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4243
4244 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4245 {
4246 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4247 return( ret );
4248 }
4249 }
4250 else
4251 {
4252 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4253 {
4254 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4255 return( ret );
4256 }
4257 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004258#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004259
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004260 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004261}
4262
4263/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004264 * Receive application data decrypted from the SSL layer
4265 */
Paul Bakker23986e52011-04-24 08:57:21 +00004266int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004267{
Paul Bakker23986e52011-04-24 08:57:21 +00004268 int ret;
4269 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004270
4271 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4272
4273 if( ssl->state != SSL_HANDSHAKE_OVER )
4274 {
4275 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4276 {
4277 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4278 return( ret );
4279 }
4280 }
4281
4282 if( ssl->in_offt == NULL )
4283 {
4284 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4285 {
Paul Bakker831a7552011-05-18 13:32:51 +00004286 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4287 return( 0 );
4288
Paul Bakker5121ce52009-01-03 21:22:43 +00004289 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4290 return( ret );
4291 }
4292
4293 if( ssl->in_msglen == 0 &&
4294 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4295 {
4296 /*
4297 * OpenSSL sends empty messages to randomize the IV
4298 */
4299 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4300 {
Paul Bakker831a7552011-05-18 13:32:51 +00004301 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4302 return( 0 );
4303
Paul Bakker5121ce52009-01-03 21:22:43 +00004304 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4305 return( ret );
4306 }
4307 }
4308
Paul Bakker48916f92012-09-16 19:57:18 +00004309 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4310 {
4311 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4312
4313 if( ssl->endpoint == SSL_IS_CLIENT &&
4314 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4315 ssl->in_hslen != 4 ) )
4316 {
4317 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4318 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4319 }
4320
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004321 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4322 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4323 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004324 {
4325 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4326
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004327#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004328 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004329 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004330 /*
4331 * SSLv3 does not have a "no_renegotiation" alert
4332 */
4333 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4334 return( ret );
4335 }
4336 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004337#endif
4338#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4339 defined(POLARSSL_SSL_PROTO_TLS1_2)
4340 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004341 {
4342 if( ( ret = ssl_send_alert_message( ssl,
4343 SSL_ALERT_LEVEL_WARNING,
4344 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4345 {
4346 return( ret );
4347 }
Paul Bakker48916f92012-09-16 19:57:18 +00004348 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004349 else
4350#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004351 {
4352 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004353 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004354 }
Paul Bakker48916f92012-09-16 19:57:18 +00004355 }
4356 else
4357 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004358 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004359 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004360 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004361 return( ret );
4362 }
4363
4364 return( POLARSSL_ERR_NET_WANT_READ );
4365 }
4366 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004367 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4368 {
4369 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4370 "but not honored by client" ) );
4371 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4372 }
Paul Bakker48916f92012-09-16 19:57:18 +00004373 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004374 {
4375 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004376 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004377 }
4378
4379 ssl->in_offt = ssl->in_msg;
4380 }
4381
4382 n = ( len < ssl->in_msglen )
4383 ? len : ssl->in_msglen;
4384
4385 memcpy( buf, ssl->in_offt, n );
4386 ssl->in_msglen -= n;
4387
4388 if( ssl->in_msglen == 0 )
4389 /* all bytes consumed */
4390 ssl->in_offt = NULL;
4391 else
4392 /* more data available */
4393 ssl->in_offt += n;
4394
4395 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4396
Paul Bakker23986e52011-04-24 08:57:21 +00004397 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004398}
4399
4400/*
4401 * Send application data to be encrypted by the SSL layer
4402 */
Paul Bakker23986e52011-04-24 08:57:21 +00004403int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004404{
Paul Bakker23986e52011-04-24 08:57:21 +00004405 int ret;
4406 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004407 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004408
4409 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4410
4411 if( ssl->state != SSL_HANDSHAKE_OVER )
4412 {
4413 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4414 {
4415 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4416 return( ret );
4417 }
4418 }
4419
Paul Bakker05decb22013-08-15 13:33:48 +02004420#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004421 /*
4422 * Assume mfl_code is correct since it was checked when set
4423 */
4424 max_len = mfl_code_to_length[ssl->mfl_code];
4425
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004426 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004427 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004428 */
4429 if( ssl->session_out != NULL &&
4430 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4431 {
4432 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4433 }
Paul Bakker05decb22013-08-15 13:33:48 +02004434#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004435
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004436 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004437
Paul Bakker5121ce52009-01-03 21:22:43 +00004438 if( ssl->out_left != 0 )
4439 {
4440 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4441 {
4442 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4443 return( ret );
4444 }
4445 }
Paul Bakker887bd502011-06-08 13:10:54 +00004446 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004447 {
Paul Bakker887bd502011-06-08 13:10:54 +00004448 ssl->out_msglen = n;
4449 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4450 memcpy( ssl->out_msg, buf, n );
4451
4452 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4453 {
4454 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4455 return( ret );
4456 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004457 }
4458
4459 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4460
Paul Bakker23986e52011-04-24 08:57:21 +00004461 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004462}
4463
4464/*
4465 * Notify the peer that the connection is being closed
4466 */
4467int ssl_close_notify( ssl_context *ssl )
4468{
4469 int ret;
4470
4471 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4472
4473 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4474 {
4475 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4476 return( ret );
4477 }
4478
4479 if( ssl->state == SSL_HANDSHAKE_OVER )
4480 {
Paul Bakker48916f92012-09-16 19:57:18 +00004481 if( ( ret = ssl_send_alert_message( ssl,
4482 SSL_ALERT_LEVEL_WARNING,
4483 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004484 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004485 return( ret );
4486 }
4487 }
4488
4489 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4490
4491 return( ret );
4492}
4493
Paul Bakker48916f92012-09-16 19:57:18 +00004494void ssl_transform_free( ssl_transform *transform )
4495{
4496#if defined(POLARSSL_ZLIB_SUPPORT)
4497 deflateEnd( &transform->ctx_deflate );
4498 inflateEnd( &transform->ctx_inflate );
4499#endif
4500
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004501 cipher_free_ctx( &transform->cipher_ctx_enc );
4502 cipher_free_ctx( &transform->cipher_ctx_dec );
4503
Paul Bakker61d113b2013-07-04 11:51:43 +02004504 md_free_ctx( &transform->md_ctx_enc );
4505 md_free_ctx( &transform->md_ctx_dec );
4506
Paul Bakker48916f92012-09-16 19:57:18 +00004507 memset( transform, 0, sizeof( ssl_transform ) );
4508}
4509
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004510#if defined(POLARSSL_X509_CRT_PARSE_C)
4511static void ssl_key_cert_free( ssl_key_cert *key_cert )
4512{
4513 ssl_key_cert *cur = key_cert, *next;
4514
4515 while( cur != NULL )
4516 {
4517 next = cur->next;
4518
4519 if( cur->key_own_alloc )
4520 {
4521 pk_free( cur->key );
4522 polarssl_free( cur->key );
4523 }
4524 polarssl_free( cur );
4525
4526 cur = next;
4527 }
4528}
4529#endif /* POLARSSL_X509_CRT_PARSE_C */
4530
Paul Bakker48916f92012-09-16 19:57:18 +00004531void ssl_handshake_free( ssl_handshake_params *handshake )
4532{
4533#if defined(POLARSSL_DHM_C)
4534 dhm_free( &handshake->dhm_ctx );
4535#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004536#if defined(POLARSSL_ECDH_C)
4537 ecdh_free( &handshake->ecdh_ctx );
4538#endif
4539
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004540#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004541 /* explicit void pointer cast for buggy MS compiler */
4542 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004543#endif
4544
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004545#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4546 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4547 /*
4548 * Free only the linked list wrapper, not the keys themselves
4549 * since the belong to the SNI callback
4550 */
4551 if( handshake->sni_key_cert != NULL )
4552 {
4553 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4554
4555 while( cur != NULL )
4556 {
4557 next = cur->next;
4558 polarssl_free( cur );
4559 cur = next;
4560 }
4561 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004562#endif
4563
Paul Bakker48916f92012-09-16 19:57:18 +00004564 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4565}
4566
4567void ssl_session_free( ssl_session *session )
4568{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004569#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004570 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004571 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004572 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004573 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004574 }
Paul Bakkered27a042013-04-18 22:46:23 +02004575#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004576
Paul Bakkera503a632013-08-14 13:48:06 +02004577#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004578 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004579#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004580
Paul Bakker0a597072012-09-25 21:55:46 +00004581 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004582}
4583
Paul Bakker5121ce52009-01-03 21:22:43 +00004584/*
4585 * Free an SSL context
4586 */
4587void ssl_free( ssl_context *ssl )
4588{
4589 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4590
Paul Bakker5121ce52009-01-03 21:22:43 +00004591 if( ssl->out_ctr != NULL )
4592 {
4593 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004594 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004595 }
4596
4597 if( ssl->in_ctr != NULL )
4598 {
4599 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004600 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 }
4602
Paul Bakker16770332013-10-11 09:59:44 +02004603#if defined(POLARSSL_ZLIB_SUPPORT)
4604 if( ssl->compress_buf != NULL )
4605 {
4606 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4607 polarssl_free( ssl->compress_buf );
4608 }
4609#endif
4610
Paul Bakker40e46942009-01-03 21:51:57 +00004611#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004612 mpi_free( &ssl->dhm_P );
4613 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004614#endif
4615
Paul Bakker48916f92012-09-16 19:57:18 +00004616 if( ssl->transform )
4617 {
4618 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004619 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004620 }
4621
4622 if( ssl->handshake )
4623 {
4624 ssl_handshake_free( ssl->handshake );
4625 ssl_transform_free( ssl->transform_negotiate );
4626 ssl_session_free( ssl->session_negotiate );
4627
Paul Bakker6e339b52013-07-03 13:37:05 +02004628 polarssl_free( ssl->handshake );
4629 polarssl_free( ssl->transform_negotiate );
4630 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004631 }
4632
Paul Bakkerc0463502013-02-14 11:19:38 +01004633 if( ssl->session )
4634 {
4635 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004636 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004637 }
4638
Paul Bakkera503a632013-08-14 13:48:06 +02004639#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004640 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004641#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004642
Paul Bakker0be444a2013-08-27 21:55:01 +02004643#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004644 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004645 {
4646 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004647 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 ssl->hostname_len = 0;
4649 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004650#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004651
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004652#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004653 if( ssl->psk != NULL )
4654 {
4655 memset( ssl->psk, 0, ssl->psk_len );
4656 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4657 polarssl_free( ssl->psk );
4658 polarssl_free( ssl->psk_identity );
4659 ssl->psk_len = 0;
4660 ssl->psk_identity_len = 0;
4661 }
4662#endif
4663
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004664#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004665 ssl_key_cert_free( ssl->key_cert );
4666#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004667
Paul Bakker05ef8352012-05-08 09:17:57 +00004668#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4669 if( ssl_hw_record_finish != NULL )
4670 {
4671 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4672 ssl_hw_record_finish( ssl );
4673 }
4674#endif
4675
Paul Bakker5121ce52009-01-03 21:22:43 +00004676 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004677
Paul Bakker86f04f42013-02-14 11:20:09 +01004678 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004679 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004680}
4681
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004682#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004683/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004684 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004685 */
4686unsigned char ssl_sig_from_pk( pk_context *pk )
4687{
4688#if defined(POLARSSL_RSA_C)
4689 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4690 return( SSL_SIG_RSA );
4691#endif
4692#if defined(POLARSSL_ECDSA_C)
4693 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4694 return( SSL_SIG_ECDSA );
4695#endif
4696 return( SSL_SIG_ANON );
4697}
4698
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004699pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4700{
4701 switch( sig )
4702 {
4703#if defined(POLARSSL_RSA_C)
4704 case SSL_SIG_RSA:
4705 return( POLARSSL_PK_RSA );
4706#endif
4707#if defined(POLARSSL_ECDSA_C)
4708 case SSL_SIG_ECDSA:
4709 return( POLARSSL_PK_ECDSA );
4710#endif
4711 default:
4712 return( POLARSSL_PK_NONE );
4713 }
4714}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004715#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004716
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004717/*
4718 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4719 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004720md_type_t ssl_md_alg_from_hash( unsigned char hash )
4721{
4722 switch( hash )
4723 {
4724#if defined(POLARSSL_MD5_C)
4725 case SSL_HASH_MD5:
4726 return( POLARSSL_MD_MD5 );
4727#endif
4728#if defined(POLARSSL_SHA1_C)
4729 case SSL_HASH_SHA1:
4730 return( POLARSSL_MD_SHA1 );
4731#endif
4732#if defined(POLARSSL_SHA256_C)
4733 case SSL_HASH_SHA224:
4734 return( POLARSSL_MD_SHA224 );
4735 case SSL_HASH_SHA256:
4736 return( POLARSSL_MD_SHA256 );
4737#endif
4738#if defined(POLARSSL_SHA512_C)
4739 case SSL_HASH_SHA384:
4740 return( POLARSSL_MD_SHA384 );
4741 case SSL_HASH_SHA512:
4742 return( POLARSSL_MD_SHA512 );
4743#endif
4744 default:
4745 return( POLARSSL_MD_NONE );
4746 }
4747}
4748
Paul Bakker5121ce52009-01-03 21:22:43 +00004749#endif
Gergely Budai987bfb52014-01-19 21:48:42 +01004750
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004751#if defined(POLARSSL_SSL_SET_CURVES)
4752/*
4753 * Check is a curve proposed by the peer is in our list.
4754 * Return 1 if we're willing to use it, 0 otherwise.
4755 */
4756int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4757{
4758 const ecp_group_id *gid;
4759
4760 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4761 if( *gid == grp_id )
4762 return( 1 );
4763
4764 return( 0 );
4765}
4766#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004767
4768int ssl_check_cert_usage( const x509_crt *cert,
4769 const ssl_ciphersuite_t *ciphersuite,
4770 int cert_endpoint )
4771{
Paul Bakkera77de8c2014-04-09 16:39:35 +02004772#if !defined(POLARSSL_X509_CHECK_KEY_USAGE)
4773 ((void) cert);
4774 ((void) ciphersuite);
4775 ((void) cert_endpoint);
4776#endif
4777
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004778#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4779 int usage = 0;
4780#endif
4781
4782#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4783 if( cert_endpoint == SSL_IS_SERVER )
4784 {
4785 /* Server part of the key exchange */
4786 switch( ciphersuite->key_exchange )
4787 {
4788 case POLARSSL_KEY_EXCHANGE_RSA:
4789 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4790 usage = KU_KEY_ENCIPHERMENT;
4791 break;
4792
4793 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4794 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4795 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4796 usage = KU_DIGITAL_SIGNATURE;
4797 break;
4798
4799 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4800 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4801 usage = KU_KEY_AGREEMENT;
4802 break;
4803
4804 /* Don't use default: we want warnings when adding new values */
4805 case POLARSSL_KEY_EXCHANGE_NONE:
4806 case POLARSSL_KEY_EXCHANGE_PSK:
4807 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4808 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4809 usage = 0;
4810 }
4811 }
4812 else
4813 {
4814 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4815 usage = KU_DIGITAL_SIGNATURE;
4816 }
4817
4818 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4819 return( -1 );
4820#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4821
4822 return( 0 );
4823}