blob: 4a9211f139a8fb780f6cd246b4325213b95a94a7 [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>; */
919 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
920 *(p++) = (unsigned char)( ssl->psk_len );
921 memcpy( p, ssl->psk, ssl->psk_len );
922 p += ssl->psk_len;
923
924 ssl->handshake->pmslen = p - ssl->handshake->premaster;
925
926 return( 0 );
927}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200928#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200929
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200930#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000931/*
932 * SSLv3.0 MAC functions
933 */
Paul Bakker68884e32013-01-07 18:20:04 +0100934static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
935 unsigned char *buf, size_t len,
936 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000937{
938 unsigned char header[11];
939 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100940 int padlen = 0;
941 int md_size = md_get_size( md_ctx->md_info );
942 int md_type = md_get_type( md_ctx->md_info );
943
944 if( md_type == POLARSSL_MD_MD5 )
945 padlen = 48;
946 else if( md_type == POLARSSL_MD_SHA1 )
947 padlen = 40;
948 else if( md_type == POLARSSL_MD_SHA256 )
949 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100950 else if( md_type == POLARSSL_MD_SHA384 )
951 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
953 memcpy( header, ctr, 8 );
954 header[ 8] = (unsigned char) type;
955 header[ 9] = (unsigned char)( len >> 8 );
956 header[10] = (unsigned char)( len );
957
Paul Bakker68884e32013-01-07 18:20:04 +0100958 memset( padding, 0x36, padlen );
959 md_starts( md_ctx );
960 md_update( md_ctx, secret, md_size );
961 md_update( md_ctx, padding, padlen );
962 md_update( md_ctx, header, 11 );
963 md_update( md_ctx, buf, len );
964 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Paul Bakker68884e32013-01-07 18:20:04 +0100966 memset( padding, 0x5C, padlen );
967 md_starts( md_ctx );
968 md_update( md_ctx, secret, md_size );
969 md_update( md_ctx, padding, padlen );
970 md_update( md_ctx, buf + len, md_size );
971 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000972}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200973#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000974
Paul Bakker5121ce52009-01-03 21:22:43 +0000975/*
976 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200977 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000978static int ssl_encrypt_buf( ssl_context *ssl )
979{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200980 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
982 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
983
984 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200985 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200987#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
988 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
989 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
990 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
991 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200993#if defined(POLARSSL_SSL_PROTO_SSL3)
994 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
995 {
996 ssl_mac( &ssl->transform_out->md_ctx_enc,
997 ssl->transform_out->mac_enc,
998 ssl->out_msg, ssl->out_msglen,
999 ssl->out_ctr, ssl->out_msgtype );
1000 }
1001 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001002#endif
1003#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001004 defined(POLARSSL_SSL_PROTO_TLS1_2)
1005 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1006 {
1007 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1008 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1009 ssl->out_msg, ssl->out_msglen );
1010 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1011 ssl->out_msg + ssl->out_msglen );
1012 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1013 }
1014 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001015#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001016 {
1017 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1018 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1019 }
1020
1021 SSL_DEBUG_BUF( 4, "computed mac",
1022 ssl->out_msg + ssl->out_msglen,
1023 ssl->transform_out->maclen );
1024
1025 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001026 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001027#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001029 /*
1030 * Encrypt
1031 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001032#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001033 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1034 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001036 int ret;
1037 size_t olen = 0;
1038
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1040 "including %d bytes of padding",
1041 ssl->out_msglen, 0 ) );
1042
1043 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1044 ssl->out_msg, ssl->out_msglen );
1045
Paul Bakker45125bc2013-09-04 16:47:11 +02001046 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001047 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001048 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1049 return( ret );
1050 }
1051
1052 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1053 ssl->transform_out->iv_enc,
1054 ssl->transform_out->ivlen ) ) != 0 )
1055 {
1056 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001057 return( ret );
1058 }
1059
1060 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1061 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1062 &olen ) ) != 0 )
1063 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001064 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001065 return( ret );
1066 }
1067
1068 if( ssl->out_msglen != olen )
1069 {
1070 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1071 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001072 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001073 }
1074
1075 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001076 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001077 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001078 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001079 return( ret );
1080 }
1081
1082 if( 0 != olen )
1083 {
1084 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1085 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001086 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001087 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 }
Paul Bakker68884e32013-01-07 18:20:04 +01001089 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001090#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001091#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001092 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1093 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001095 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001096 unsigned char *enc_msg;
1097 unsigned char add_data[13];
1098 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1099
Paul Bakkerca4ab492012-04-18 14:23:57 +00001100 memcpy( add_data, ssl->out_ctr, 8 );
1101 add_data[8] = ssl->out_msgtype;
1102 add_data[9] = ssl->major_ver;
1103 add_data[10] = ssl->minor_ver;
1104 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1105 add_data[12] = ssl->out_msglen & 0xFF;
1106
1107 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1108 add_data, 13 );
1109
Paul Bakker68884e32013-01-07 18:20:04 +01001110 /*
1111 * Generate IV
1112 */
1113 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001114 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001115 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1116 if( ret != 0 )
1117 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001118
Paul Bakker68884e32013-01-07 18:20:04 +01001119 memcpy( ssl->out_iv,
1120 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1121 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001122
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001123 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1124 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1125
Paul Bakker68884e32013-01-07 18:20:04 +01001126 /*
1127 * Fix pointer positions and message length with added IV
1128 */
1129 enc_msg = ssl->out_msg;
1130 enc_msglen = ssl->out_msglen;
1131 ssl->out_msglen += ssl->transform_out->ivlen -
1132 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001133
Paul Bakker68884e32013-01-07 18:20:04 +01001134 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1135 "including %d bytes of padding",
1136 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001137
Paul Bakker68884e32013-01-07 18:20:04 +01001138 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1139 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140
Paul Bakker68884e32013-01-07 18:20:04 +01001141 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001142 * Encrypt
1143 */
1144 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1145 ssl->transform_out->iv_enc,
1146 ssl->transform_out->ivlen ) ) != 0 ||
1147 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1148 {
1149 return( ret );
1150 }
1151
1152 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1153 add_data, 13 ) ) != 0 )
1154 {
1155 return( ret );
1156 }
1157
1158 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1159 enc_msg, enc_msglen,
1160 enc_msg, &olen ) ) != 0 )
1161 {
1162 return( ret );
1163 }
1164 totlen = olen;
1165
1166 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1167 enc_msg + olen, &olen ) ) != 0 )
1168 {
1169 return( ret );
1170 }
1171 totlen += olen;
1172
1173 if( totlen != enc_msglen )
1174 {
1175 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1176 return( -1 );
1177 }
1178
1179 /*
1180 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001181 */
1182 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001183
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001184 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1185 enc_msg + enc_msglen, 16 ) ) != 0 )
1186 {
1187 return( ret );
1188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001190 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001191 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 else
Paul Bakker68884e32013-01-07 18:20:04 +01001193#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001194#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1195 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001196 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1197 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001199 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001200 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001201 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001202
Paul Bakker48916f92012-09-16 19:57:18 +00001203 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1204 ssl->transform_out->ivlen;
1205 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 padlen = 0;
1207
1208 for( i = 0; i <= padlen; i++ )
1209 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1210
1211 ssl->out_msglen += padlen + 1;
1212
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 enc_msglen = ssl->out_msglen;
1214 enc_msg = ssl->out_msg;
1215
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001216#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001217 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001218 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1219 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001221 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001222 {
1223 /*
1224 * Generate IV
1225 */
Paul Bakker48916f92012-09-16 19:57:18 +00001226 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1227 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001228 if( ret != 0 )
1229 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230
Paul Bakker92be97b2013-01-02 17:30:03 +01001231 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001232 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233
1234 /*
1235 * Fix pointer positions and message length with added IV
1236 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001237 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001239 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001241#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001244 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001245 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
1247 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001248 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Paul Bakker45125bc2013-09-04 16:47:11 +02001250 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001252 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1253 return( ret );
1254 }
1255
1256 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1257 ssl->transform_out->iv_enc,
1258 ssl->transform_out->ivlen ) ) != 0 )
1259 {
1260 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001261 return( ret );
1262 }
Paul Bakker68884e32013-01-07 18:20:04 +01001263
Paul Bakkercca5b812013-08-31 17:40:26 +02001264 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1265 enc_msg, enc_msglen, enc_msg,
1266 &olen ) ) != 0 )
1267 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001268 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001269 return( ret );
1270 }
Paul Bakker68884e32013-01-07 18:20:04 +01001271
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001273
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001275 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001276 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001277 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 return( ret );
1279 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001280
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 if( enc_msglen != olen )
1282 {
1283 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1284 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001285 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001286 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001287
1288#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001289 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1290 {
1291 /*
1292 * Save IV in SSL3 and TLS1
1293 */
1294 memcpy( ssl->transform_out->iv_enc,
1295 ssl->transform_out->cipher_ctx_enc.iv,
1296 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001298#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001300 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001301#endif /* POLARSSL_CIPHER_MODE_CBC &&
1302 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001303 {
1304 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1305 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Paul Bakkerca4ab492012-04-18 14:23:57 +00001308 for( i = 8; i > 0; i-- )
1309 if( ++ssl->out_ctr[i - 1] != 0 )
1310 break;
1311
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1313
1314 return( 0 );
1315}
1316
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001317#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001318
Paul Bakker5121ce52009-01-03 21:22:43 +00001319static int ssl_decrypt_buf( ssl_context *ssl )
1320{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001321 size_t i;
1322#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1323 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1324 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1325 size_t padlen = 0, correct = 1;
1326#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
1328 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1329
Paul Bakker48916f92012-09-16 19:57:18 +00001330 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 {
1332 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001333 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001334 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 }
1336
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001337#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001338 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1339 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001340 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001341 int ret;
1342 size_t olen = 0;
1343
Paul Bakker68884e32013-01-07 18:20:04 +01001344 padlen = 0;
1345
Paul Bakker45125bc2013-09-04 16:47:11 +02001346 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001347 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001348 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1349 return( ret );
1350 }
1351
1352 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1353 ssl->transform_in->iv_dec,
1354 ssl->transform_in->ivlen ) ) != 0 )
1355 {
1356 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001357 return( ret );
1358 }
1359
1360 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1361 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1362 &olen ) ) != 0 )
1363 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001364 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001365 return( ret );
1366 }
1367
1368 if( ssl->in_msglen != olen )
1369 {
1370 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001371 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001372 }
1373
1374 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001375 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001376 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001377 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001378 return( ret );
1379 }
1380
1381 if( 0 != olen )
1382 {
1383 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001384 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 }
Paul Bakker68884e32013-01-07 18:20:04 +01001387 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001388#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001389#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001390 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1391 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001392 {
1393 unsigned char *dec_msg;
1394 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001395 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001396 unsigned char add_data[13];
1397 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1398
Paul Bakker68884e32013-01-07 18:20:04 +01001399 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1400 ssl->transform_in->fixed_ivlen );
1401 dec_msglen -= 16;
1402 dec_msg = ssl->in_msg;
1403 dec_msg_result = ssl->in_msg;
1404 ssl->in_msglen = dec_msglen;
1405
1406 memcpy( add_data, ssl->in_ctr, 8 );
1407 add_data[8] = ssl->in_msgtype;
1408 add_data[9] = ssl->major_ver;
1409 add_data[10] = ssl->minor_ver;
1410 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1411 add_data[12] = ssl->in_msglen & 0xFF;
1412
1413 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1414 add_data, 13 );
1415
1416 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1417 ssl->in_iv,
1418 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1419
1420 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1421 ssl->transform_in->ivlen );
1422 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1423
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001424 /*
1425 * Decrypt
1426 */
1427 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1428 ssl->transform_in->iv_dec,
1429 ssl->transform_in->ivlen ) ) != 0 ||
1430 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001431 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001432 return( ret );
1433 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001434
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001435 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1436 add_data, 13 ) ) != 0 )
1437 {
1438 return( ret );
1439 }
1440
1441 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1442 dec_msg, dec_msglen,
1443 dec_msg_result, &olen ) ) != 0 )
1444 {
1445 return( ret );
1446 }
1447 totlen = olen;
1448
1449 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1450 dec_msg_result + olen, &olen ) ) != 0 )
1451 {
1452 return( ret );
1453 }
1454 totlen += olen;
1455
1456 if( totlen != dec_msglen )
1457 {
1458 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1459 return( -1 );
1460 }
1461
1462 /*
1463 * Authenticate
1464 */
1465 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1466 dec_msg + dec_msglen, 16 ) ) != 0 )
1467 {
1468 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001469 return( POLARSSL_ERR_SSL_INVALID_MAC );
1470 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001471
Paul Bakkerca4ab492012-04-18 14:23:57 +00001472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 else
Paul Bakker68884e32013-01-07 18:20:04 +01001474#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001475#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1476 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001477 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1478 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001479 {
Paul Bakker45829992013-01-03 14:52:21 +01001480 /*
1481 * Decrypt and check the padding
1482 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001483 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001484 unsigned char *dec_msg;
1485 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001486 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001487 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001488 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001489
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 /*
Paul Bakker45829992013-01-03 14:52:21 +01001491 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 */
Paul Bakker48916f92012-09-16 19:57:18 +00001493 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 {
1495 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001496 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001497 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 }
1499
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001500#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001501 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1502 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001503#endif
Paul Bakker45829992013-01-03 14:52:21 +01001504
1505 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1506 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1507 {
1508 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1509 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1510 return( POLARSSL_ERR_SSL_INVALID_MAC );
1511 }
1512
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001513 dec_msglen = ssl->in_msglen;
1514 dec_msg = ssl->in_msg;
1515 dec_msg_result = ssl->in_msg;
1516
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001517#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001518 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001519 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001520 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001521 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001522 {
Paul Bakker48916f92012-09-16 19:57:18 +00001523 dec_msglen -= ssl->transform_in->ivlen;
1524 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001525
Paul Bakker48916f92012-09-16 19:57:18 +00001526 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001527 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001528 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001529#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001530
Paul Bakker45125bc2013-09-04 16:47:11 +02001531 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001533 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1534 return( ret );
1535 }
1536
1537 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1538 ssl->transform_in->iv_dec,
1539 ssl->transform_in->ivlen ) ) != 0 )
1540 {
1541 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001542 return( ret );
1543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
Paul Bakkercca5b812013-08-31 17:40:26 +02001545 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1546 dec_msg, dec_msglen, dec_msg_result,
1547 &olen ) ) != 0 )
1548 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001549 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001550 return( ret );
1551 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001552
Paul Bakkercca5b812013-08-31 17:40:26 +02001553 dec_msglen -= olen;
1554 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001555 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001556 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001557 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001558 return( ret );
1559 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001560
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 if( dec_msglen != olen )
1562 {
1563 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001564 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
1567#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001568 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1569 {
1570 /*
1571 * Save IV in SSL3 and TLS1
1572 */
1573 memcpy( ssl->transform_in->iv_dec,
1574 ssl->transform_in->cipher_ctx_dec.iv,
1575 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
1579 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001580
1581 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1582 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001583#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001584 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1585 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001586#endif
Paul Bakker45829992013-01-03 14:52:21 +01001587 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001588 correct = 0;
1589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001590
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001591#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1593 {
Paul Bakker48916f92012-09-16 19:57:18 +00001594 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001596#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1598 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001599 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001600#endif
Paul Bakker45829992013-01-03 14:52:21 +01001601 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 }
1603 }
1604 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001605#endif
1606#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1607 defined(POLARSSL_SSL_PROTO_TLS1_2)
1608 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 {
1610 /*
Paul Bakker45829992013-01-03 14:52:21 +01001611 * TLSv1+: always check the padding up to the first failure
1612 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001614 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001615 size_t padding_idx = ssl->in_msglen - padlen - 1;
1616
Paul Bakker956c9e02013-12-19 14:42:28 +01001617 /*
1618 * Padding is guaranteed to be incorrect if:
1619 * 1. padlen - 1 > ssl->in_msglen
1620 *
1621 * 2. ssl->in_msglen + padlen >
1622 * SSL_MAX_CONTENT_LEN + 256 (max padding)
1623 *
1624 * In both cases we reset padding_idx to a safe value (0) to
1625 * prevent out-of-buffer reads.
1626 */
1627 correct &= ( ssl->in_msglen >= padlen - 1 );
1628 correct &= ( ssl->in_msglen + padlen <= SSL_MAX_CONTENT_LEN + 256 );
1629
1630 padding_idx *= correct;
1631
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001632 for( i = 1; i <= 256; i++ )
1633 {
1634 real_count &= ( i <= padlen );
1635 pad_count += real_count *
1636 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1637 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001638
1639 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001640
Paul Bakkerd66f0702013-01-31 16:57:45 +01001641#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001642 if( padlen > 0 && correct == 0)
1643 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001644#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001645 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001647 else
1648#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1649 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001650 {
1651 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001652 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001655 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001656#endif /* POLARSSL_CIPHER_MODE_CBC &&
1657 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001658 {
1659 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1660 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1661 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
1663 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1664 ssl->in_msg, ssl->in_msglen );
1665
1666 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001667 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1670 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1671 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1672 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1673 POLARSSL_MODE_GCM )
1674 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001675 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1676
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001677 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1680 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001684#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001685 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1686 {
1687 ssl_mac( &ssl->transform_in->md_ctx_dec,
1688 ssl->transform_in->mac_dec,
1689 ssl->in_msg, ssl->in_msglen,
1690 ssl->in_ctr, ssl->in_msgtype );
1691 }
1692 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001693#endif /* POLARSSL_SSL_PROTO_SSL3 */
1694#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001695 defined(POLARSSL_SSL_PROTO_TLS1_2)
1696 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1697 {
1698 /*
1699 * Process MAC and always update for padlen afterwards to make
1700 * total time independent of padlen
1701 *
1702 * extra_run compensates MAC check for padlen
1703 *
1704 * Known timing attacks:
1705 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1706 *
1707 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1708 * correctly. (We round down instead of up, so -56 is the correct
1709 * value for our calculations instead of -55)
1710 */
1711 size_t j, extra_run = 0;
1712 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1713 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001714
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001715 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001716
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001717 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1718 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1719 ssl->in_msglen );
1720 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1721 ssl->in_msg + ssl->in_msglen );
1722 for( j = 0; j < extra_run; j++ )
1723 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001724
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1726 }
1727 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001728#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001729 POLARSSL_SSL_PROTO_TLS1_2 */
1730 {
1731 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1732 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1733 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001735 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1736 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1737 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001739 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001740 ssl->transform_in->maclen ) != 0 )
1741 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001742#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001743 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001744#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001745 correct = 0;
1746 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001748 /*
1749 * Finally check the correct flag
1750 */
1751 if( correct == 0 )
1752 return( POLARSSL_ERR_SSL_INVALID_MAC );
1753 }
1754#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
1756 if( ssl->in_msglen == 0 )
1757 {
1758 ssl->nb_zero++;
1759
1760 /*
1761 * Three or more empty messages may be a DoS attack
1762 * (excessive CPU consumption).
1763 */
1764 if( ssl->nb_zero > 3 )
1765 {
1766 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1767 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001768 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 }
1770 }
1771 else
1772 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001773
Paul Bakker23986e52011-04-24 08:57:21 +00001774 for( i = 8; i > 0; i-- )
1775 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 break;
1777
1778 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1779
1780 return( 0 );
1781}
1782
Paul Bakker2770fbd2012-07-03 13:30:23 +00001783#if defined(POLARSSL_ZLIB_SUPPORT)
1784/*
1785 * Compression/decompression functions
1786 */
1787static int ssl_compress_buf( ssl_context *ssl )
1788{
1789 int ret;
1790 unsigned char *msg_post = ssl->out_msg;
1791 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001792 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001793
1794 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1795
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001796 if( len_pre == 0 )
1797 return( 0 );
1798
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799 memcpy( msg_pre, ssl->out_msg, len_pre );
1800
1801 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1802 ssl->out_msglen ) );
1803
1804 SSL_DEBUG_BUF( 4, "before compression: output payload",
1805 ssl->out_msg, ssl->out_msglen );
1806
Paul Bakker48916f92012-09-16 19:57:18 +00001807 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1808 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1809 ssl->transform_out->ctx_deflate.next_out = msg_post;
1810 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811
Paul Bakker48916f92012-09-16 19:57:18 +00001812 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001813 if( ret != Z_OK )
1814 {
1815 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1816 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1817 }
1818
Paul Bakker48916f92012-09-16 19:57:18 +00001819 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001820
Paul Bakker2770fbd2012-07-03 13:30:23 +00001821 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1822 ssl->out_msglen ) );
1823
1824 SSL_DEBUG_BUF( 4, "after compression: output payload",
1825 ssl->out_msg, ssl->out_msglen );
1826
1827 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1828
1829 return( 0 );
1830}
1831
1832static int ssl_decompress_buf( ssl_context *ssl )
1833{
1834 int ret;
1835 unsigned char *msg_post = ssl->in_msg;
1836 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001837 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001838
1839 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1840
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001841 if( len_pre == 0 )
1842 return( 0 );
1843
Paul Bakker2770fbd2012-07-03 13:30:23 +00001844 memcpy( msg_pre, ssl->in_msg, len_pre );
1845
1846 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1847 ssl->in_msglen ) );
1848
1849 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1850 ssl->in_msg, ssl->in_msglen );
1851
Paul Bakker48916f92012-09-16 19:57:18 +00001852 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1853 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1854 ssl->transform_in->ctx_inflate.next_out = msg_post;
1855 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001856
Paul Bakker48916f92012-09-16 19:57:18 +00001857 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001858 if( ret != Z_OK )
1859 {
1860 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1861 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1862 }
1863
Paul Bakker48916f92012-09-16 19:57:18 +00001864 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001865
Paul Bakker2770fbd2012-07-03 13:30:23 +00001866 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1867 ssl->in_msglen ) );
1868
1869 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1870 ssl->in_msg, ssl->in_msglen );
1871
1872 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1873
1874 return( 0 );
1875}
1876#endif /* POLARSSL_ZLIB_SUPPORT */
1877
Paul Bakker5121ce52009-01-03 21:22:43 +00001878/*
1879 * Fill the input message buffer
1880 */
Paul Bakker23986e52011-04-24 08:57:21 +00001881int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001882{
Paul Bakker23986e52011-04-24 08:57:21 +00001883 int ret;
1884 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
1886 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1887
1888 while( ssl->in_left < nb_want )
1889 {
1890 len = nb_want - ssl->in_left;
1891 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1892
1893 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1894 ssl->in_left, nb_want ) );
1895 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1896
Paul Bakker831a7552011-05-18 13:32:51 +00001897 if( ret == 0 )
1898 return( POLARSSL_ERR_SSL_CONN_EOF );
1899
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 if( ret < 0 )
1901 return( ret );
1902
1903 ssl->in_left += ret;
1904 }
1905
1906 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1907
1908 return( 0 );
1909}
1910
1911/*
1912 * Flush any data not yet written
1913 */
1914int ssl_flush_output( ssl_context *ssl )
1915{
1916 int ret;
1917 unsigned char *buf;
1918
1919 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1920
1921 while( ssl->out_left > 0 )
1922 {
1923 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1924 5 + ssl->out_msglen, ssl->out_left ) );
1925
Paul Bakker5bd42292012-12-19 14:40:42 +01001926 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001928
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1930
1931 if( ret <= 0 )
1932 return( ret );
1933
1934 ssl->out_left -= ret;
1935 }
1936
1937 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1938
1939 return( 0 );
1940}
1941
1942/*
1943 * Record layer functions
1944 */
1945int ssl_write_record( ssl_context *ssl )
1946{
Paul Bakker05ef8352012-05-08 09:17:57 +00001947 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001948 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
1950 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1951
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1953 {
1954 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1955 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1956 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1957
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001958 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1959 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 }
1961
Paul Bakker2770fbd2012-07-03 13:30:23 +00001962#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001963 if( ssl->transform_out != NULL &&
1964 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001965 {
1966 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1967 {
1968 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1969 return( ret );
1970 }
1971
1972 len = ssl->out_msglen;
1973 }
1974#endif /*POLARSSL_ZLIB_SUPPORT */
1975
Paul Bakker05ef8352012-05-08 09:17:57 +00001976#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1977 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001979 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001980
Paul Bakker05ef8352012-05-08 09:17:57 +00001981 ret = ssl_hw_record_write( ssl );
1982 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1983 {
1984 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1985 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1986 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001987
1988 if( ret == 0 )
1989 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001990 }
1991#endif
1992 if( !done )
1993 {
1994 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1995 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1996 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1998 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001999
Paul Bakker48916f92012-09-16 19:57:18 +00002000 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002001 {
2002 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2003 {
2004 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2005 return( ret );
2006 }
2007
2008 len = ssl->out_msglen;
2009 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2010 ssl->out_hdr[4] = (unsigned char)( len );
2011 }
2012
2013 ssl->out_left = 5 + ssl->out_msglen;
2014
2015 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2016 "version = [%d:%d], msglen = %d",
2017 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2018 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2019
2020 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002021 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 }
2023
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2025 {
2026 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2027 return( ret );
2028 }
2029
2030 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2031
2032 return( 0 );
2033}
2034
2035int ssl_read_record( ssl_context *ssl )
2036{
Paul Bakker05ef8352012-05-08 09:17:57 +00002037 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002038
2039 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2040
Paul Bakker68884e32013-01-07 18:20:04 +01002041 SSL_DEBUG_BUF( 4, "input record from network",
2042 ssl->in_hdr, 5 + ssl->in_msglen );
2043
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 if( ssl->in_hslen != 0 &&
2045 ssl->in_hslen < ssl->in_msglen )
2046 {
2047 /*
2048 * Get next Handshake message in the current record
2049 */
2050 ssl->in_msglen -= ssl->in_hslen;
2051
Paul Bakker8934a982011-08-05 11:11:53 +00002052 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002053 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002054
2055 ssl->in_hslen = 4;
2056 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2057
2058 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2059 " %d, type = %d, hslen = %d",
2060 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2061
2062 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2063 {
2064 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002065 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 }
2067
2068 if( ssl->in_msglen < ssl->in_hslen )
2069 {
2070 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002071 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 }
2073
Paul Bakker48916f92012-09-16 19:57:18 +00002074 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002075
2076 return( 0 );
2077 }
2078
2079 ssl->in_hslen = 0;
2080
2081 /*
2082 * Read the record header and validate it
2083 */
2084 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2085 {
2086 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2087 return( ret );
2088 }
2089
2090 ssl->in_msgtype = ssl->in_hdr[0];
2091 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2092
2093 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2094 "version = [%d:%d], msglen = %d",
2095 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2096 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2097
2098 if( ssl->in_hdr[1] != ssl->major_ver )
2099 {
2100 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002101 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 }
2103
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002104 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 {
2106 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002107 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
2109
2110 /*
2111 * Make sure the message length is acceptable
2112 */
Paul Bakker48916f92012-09-16 19:57:18 +00002113 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 {
2115 if( ssl->in_msglen < 1 ||
2116 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2117 {
2118 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002119 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 }
2121 }
2122 else
2123 {
Paul Bakker48916f92012-09-16 19:57:18 +00002124 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 {
2126 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002127 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
2129
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002130#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002132 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 {
2134 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002135 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002137#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002139#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2140 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 /*
2142 * TLS encrypted messages can have up to 256 bytes of padding
2143 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002144 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002145 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 {
2147 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002148 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 }
2152
2153 /*
2154 * Read and optionally decrypt the message contents
2155 */
2156 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2157 {
2158 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2159 return( ret );
2160 }
2161
2162 SSL_DEBUG_BUF( 4, "input record from network",
2163 ssl->in_hdr, 5 + ssl->in_msglen );
2164
Paul Bakker05ef8352012-05-08 09:17:57 +00002165#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2166 if( ssl_hw_record_read != NULL)
2167 {
2168 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2169
2170 ret = ssl_hw_record_read( ssl );
2171 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2172 {
2173 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2174 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2175 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002176
2177 if( ret == 0 )
2178 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002179 }
2180#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002181 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 {
2183 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2184 {
Paul Bakker40865c82013-01-31 17:13:13 +01002185#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2186 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2187 {
2188 ssl_send_alert_message( ssl,
2189 SSL_ALERT_LEVEL_FATAL,
2190 SSL_ALERT_MSG_BAD_RECORD_MAC );
2191 }
2192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2194 return( ret );
2195 }
2196
2197 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2198 ssl->in_msg, ssl->in_msglen );
2199
2200 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002203 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 }
2205 }
2206
Paul Bakker2770fbd2012-07-03 13:30:23 +00002207#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002208 if( ssl->transform_in != NULL &&
2209 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002210 {
2211 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2212 {
2213 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2214 return( ret );
2215 }
2216
2217 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2218 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2219 }
2220#endif /* POLARSSL_ZLIB_SUPPORT */
2221
Paul Bakker0a925182012-04-16 06:46:41 +00002222 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2223 ssl->in_msgtype != SSL_MSG_ALERT &&
2224 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2225 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2226 {
2227 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2228
Paul Bakker48916f92012-09-16 19:57:18 +00002229 if( ( ret = ssl_send_alert_message( ssl,
2230 SSL_ALERT_LEVEL_FATAL,
2231 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002232 {
2233 return( ret );
2234 }
2235
2236 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2237 }
2238
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2240 {
2241 ssl->in_hslen = 4;
2242 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2243
2244 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2245 " %d, type = %d, hslen = %d",
2246 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2247
2248 /*
2249 * Additional checks to validate the handshake header
2250 */
2251 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2252 {
2253 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002254 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
2256
2257 if( ssl->in_msglen < ssl->in_hslen )
2258 {
2259 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002260 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262
Paul Bakker48916f92012-09-16 19:57:18 +00002263 if( ssl->state != SSL_HANDSHAKE_OVER )
2264 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
2266
2267 if( ssl->in_msgtype == SSL_MSG_ALERT )
2268 {
2269 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2270 ssl->in_msg[0], ssl->in_msg[1] ) );
2271
2272 /*
2273 * Ignore non-fatal alerts, except close_notify
2274 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002275 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002277 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2278 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002279 /**
2280 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2281 * error identifier.
2282 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002283 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 }
2285
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002286 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2287 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 {
2289 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002290 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 }
2292 }
2293
2294 ssl->in_left = 0;
2295
2296 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2297
2298 return( 0 );
2299}
2300
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002301int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2302{
2303 int ret;
2304
2305 if( ( ret = ssl_send_alert_message( ssl,
2306 SSL_ALERT_LEVEL_FATAL,
2307 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2308 {
2309 return( ret );
2310 }
2311
2312 return( 0 );
2313}
2314
Paul Bakker0a925182012-04-16 06:46:41 +00002315int ssl_send_alert_message( ssl_context *ssl,
2316 unsigned char level,
2317 unsigned char message )
2318{
2319 int ret;
2320
2321 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2322
2323 ssl->out_msgtype = SSL_MSG_ALERT;
2324 ssl->out_msglen = 2;
2325 ssl->out_msg[0] = level;
2326 ssl->out_msg[1] = message;
2327
2328 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2329 {
2330 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2331 return( ret );
2332 }
2333
2334 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2335
2336 return( 0 );
2337}
2338
Paul Bakker5121ce52009-01-03 21:22:43 +00002339/*
2340 * Handshake functions
2341 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002342#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2343 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2344 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2345 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2346 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2347 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2348 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002349int ssl_write_certificate( ssl_context *ssl )
2350{
Paul Bakkered27a042013-04-18 22:46:23 +02002351 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002352 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
2354 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2355
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002356 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002357 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2358 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002359 {
2360 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2361 ssl->state++;
2362 return( 0 );
2363 }
2364
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002365 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002366 return( ret );
2367}
2368
2369int ssl_parse_certificate( ssl_context *ssl )
2370{
2371 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2372 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2373
2374 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2375
2376 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002377 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2378 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002379 {
2380 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2381 ssl->state++;
2382 return( 0 );
2383 }
2384
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002385 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386 return( ret );
2387}
2388#else
2389int ssl_write_certificate( ssl_context *ssl )
2390{
2391 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2392 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002393 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2395
2396 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2397
2398 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002399 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2400 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002401 {
2402 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2403 ssl->state++;
2404 return( 0 );
2405 }
2406
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 if( ssl->endpoint == SSL_IS_CLIENT )
2408 {
2409 if( ssl->client_auth == 0 )
2410 {
2411 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2412 ssl->state++;
2413 return( 0 );
2414 }
2415
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002416#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 /*
2418 * If using SSLv3 and got no cert, send an Alert message
2419 * (otherwise an empty Certificate message will be sent).
2420 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002421 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2423 {
2424 ssl->out_msglen = 2;
2425 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2427 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
2429 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2430 goto write_msg;
2431 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002432#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 }
2434 else /* SSL_IS_SERVER */
2435 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002436 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 {
2438 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002439 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 }
2441 }
2442
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002443 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 /*
2446 * 0 . 0 handshake type
2447 * 1 . 3 handshake length
2448 * 4 . 6 length of all certs
2449 * 7 . 9 length of cert. 1
2450 * 10 . n-1 peer certificate
2451 * n . n+2 length of cert. 2
2452 * n+3 . ... upper level cert, etc.
2453 */
2454 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002455 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Paul Bakker29087132010-03-21 21:03:34 +00002457 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 {
2459 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002460 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 {
2462 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2463 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002464 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 }
2466
2467 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2468 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2469 ssl->out_msg[i + 2] = (unsigned char)( n );
2470
2471 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2472 i += n; crt = crt->next;
2473 }
2474
2475 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2476 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2477 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2478
2479 ssl->out_msglen = i;
2480 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2481 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2482
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002483#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002484write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002485#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
2487 ssl->state++;
2488
2489 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2490 {
2491 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2492 return( ret );
2493 }
2494
2495 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2496
Paul Bakkered27a042013-04-18 22:46:23 +02002497 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498}
2499
2500int ssl_parse_certificate( ssl_context *ssl )
2501{
Paul Bakkered27a042013-04-18 22:46:23 +02002502 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002503 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002504 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
2506 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2507
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002508 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002509 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002511 {
2512 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2513 ssl->state++;
2514 return( 0 );
2515 }
2516
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002518 ( ssl->authmode == SSL_VERIFY_NONE ||
2519 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002521 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2523 ssl->state++;
2524 return( 0 );
2525 }
2526
2527 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2528 {
2529 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2530 return( ret );
2531 }
2532
2533 ssl->state++;
2534
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 /*
2537 * Check if the client sent an empty certificate
2538 */
2539 if( ssl->endpoint == SSL_IS_SERVER &&
2540 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2541 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002542 if( ssl->in_msglen == 2 &&
2543 ssl->in_msgtype == SSL_MSG_ALERT &&
2544 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2545 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 {
2547 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2548
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002549 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2551 return( 0 );
2552 else
Paul Bakker40e46942009-01-03 21:51:57 +00002553 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002556#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002558#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2559 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 if( ssl->endpoint == SSL_IS_SERVER &&
2561 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2562 {
2563 if( ssl->in_hslen == 7 &&
2564 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2565 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2566 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2567 {
2568 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2569
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002570 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002572 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 else
2574 return( 0 );
2575 }
2576 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002577#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2578 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
2580 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2581 {
2582 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002583 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 }
2585
2586 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2587 {
2588 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002589 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 }
2591
2592 /*
2593 * Same message structure as in ssl_write_certificate()
2594 */
2595 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2596
2597 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2598 {
2599 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002600 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
2602
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002603 /* In case we tried to reuse a session but it failed */
2604 if( ssl->session_negotiate->peer_cert != NULL )
2605 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002606 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002607 polarssl_free( ssl->session_negotiate->peer_cert );
2608 }
2609
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002610 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2611 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 {
2613 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002614 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002615 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 }
2617
Paul Bakkerb6b09562013-09-18 14:17:41 +02002618 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
2620 i = 7;
2621
2622 while( i < ssl->in_hslen )
2623 {
2624 if( ssl->in_msg[i] != 0 )
2625 {
2626 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002627 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 }
2629
2630 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2631 | (unsigned int) ssl->in_msg[i + 2];
2632 i += 3;
2633
2634 if( n < 128 || i + n > ssl->in_hslen )
2635 {
2636 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002637 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 }
2639
Paul Bakkerddf26b42013-09-18 13:46:23 +02002640 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2641 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 if( ret != 0 )
2643 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002644 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 return( ret );
2646 }
2647
2648 i += n;
2649 }
2650
Paul Bakker48916f92012-09-16 19:57:18 +00002651 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
2653 if( ssl->authmode != SSL_VERIFY_NONE )
2654 {
2655 if( ssl->ca_chain == NULL )
2656 {
2657 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002658 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 }
2660
Paul Bakkerddf26b42013-09-18 13:46:23 +02002661 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2662 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2663 &ssl->session_negotiate->verify_result,
2664 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
2666 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002667 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002669 }
2670#if defined(POLARSSL_SSL_SET_CURVES)
2671 else
2672 {
2673 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
2674
2675 /* If certificate uses an EC key, make sure the curve is OK */
2676 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2677 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2678 {
2679 SSL_DEBUG_MSG( 1, ( "bad server certificate (EC key curve)" ) );
2680 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2681 }
2682 }
2683#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2686 ret = 0;
2687 }
2688
2689 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2690
2691 return( ret );
2692}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002693#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2694 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2695 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2696 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2697 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2698 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2699 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
2701int ssl_write_change_cipher_spec( ssl_context *ssl )
2702{
2703 int ret;
2704
2705 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2706
2707 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2708 ssl->out_msglen = 1;
2709 ssl->out_msg[0] = 1;
2710
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 ssl->state++;
2712
2713 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2714 {
2715 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2716 return( ret );
2717 }
2718
2719 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2720
2721 return( 0 );
2722}
2723
2724int ssl_parse_change_cipher_spec( ssl_context *ssl )
2725{
2726 int ret;
2727
2728 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2729
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2731 {
2732 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2733 return( ret );
2734 }
2735
2736 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2737 {
2738 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002739 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 }
2741
2742 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2743 {
2744 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002745 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 }
2747
2748 ssl->state++;
2749
2750 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2751
2752 return( 0 );
2753}
2754
Paul Bakker41c83d32013-03-20 14:39:14 +01002755void ssl_optimize_checksum( ssl_context *ssl,
2756 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002757{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002758 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002759
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002760#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2761 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002762 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002763 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002764 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002765#endif
2766#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2767#if defined(POLARSSL_SHA512_C)
2768 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2769 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2770 else
2771#endif
2772#if defined(POLARSSL_SHA256_C)
2773 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002774 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002775 else
2776#endif
2777#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2778 /* Should never happen */
2779 return;
Paul Bakker380da532012-04-18 16:10:25 +00002780}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002781
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002782static void ssl_update_checksum_start( ssl_context *ssl,
2783 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002784{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002785#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2786 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002787 md5_update( &ssl->handshake->fin_md5 , buf, len );
2788 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002789#endif
2790#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2791#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002792 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002793#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002794#if defined(POLARSSL_SHA512_C)
2795 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002796#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002797#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002798}
2799
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002800#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2801 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002802static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2803 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002804{
Paul Bakker48916f92012-09-16 19:57:18 +00002805 md5_update( &ssl->handshake->fin_md5 , buf, len );
2806 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002807}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002808#endif
Paul Bakker380da532012-04-18 16:10:25 +00002809
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002810#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2811#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002812static void ssl_update_checksum_sha256( ssl_context *ssl,
2813 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002814{
Paul Bakker9e36f042013-06-30 14:34:05 +02002815 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002816}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002817#endif
Paul Bakker380da532012-04-18 16:10:25 +00002818
Paul Bakker9e36f042013-06-30 14:34:05 +02002819#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002820static void ssl_update_checksum_sha384( ssl_context *ssl,
2821 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002822{
Paul Bakker9e36f042013-06-30 14:34:05 +02002823 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002824}
Paul Bakker769075d2012-11-24 11:26:46 +01002825#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002826#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002827
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002828#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002829static void ssl_calc_finished_ssl(
2830 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002831{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002832 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002833 md5_context md5;
2834 sha1_context sha1;
2835
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 unsigned char padbuf[48];
2837 unsigned char md5sum[16];
2838 unsigned char sha1sum[20];
2839
Paul Bakker48916f92012-09-16 19:57:18 +00002840 ssl_session *session = ssl->session_negotiate;
2841 if( !session )
2842 session = ssl->session;
2843
Paul Bakker1ef83d62012-04-11 12:09:53 +00002844 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2845
Paul Bakker48916f92012-09-16 19:57:18 +00002846 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2847 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
2849 /*
2850 * SSLv3:
2851 * hash =
2852 * MD5( master + pad2 +
2853 * MD5( handshake + sender + master + pad1 ) )
2854 * + SHA1( master + pad2 +
2855 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 */
2857
Paul Bakker90995b52013-06-24 19:20:35 +02002858#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002859 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002860 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002861#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Paul Bakker90995b52013-06-24 19:20:35 +02002863#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002864 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002866#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Paul Bakker3c2122f2013-06-24 19:03:14 +02002868 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2869 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Paul Bakker3c2122f2013-06-24 19:03:14 +02002873 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002874 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002875 md5_update( &md5, padbuf, 48 );
2876 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
Paul Bakker3c2122f2013-06-24 19:03:14 +02002878 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002879 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002880 sha1_update( &sha1, padbuf, 40 );
2881 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Paul Bakker1ef83d62012-04-11 12:09:53 +00002883 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Paul Bakker1ef83d62012-04-11 12:09:53 +00002885 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002886 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002887 md5_update( &md5, padbuf, 48 );
2888 md5_update( &md5, md5sum, 16 );
2889 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
Paul Bakker1ef83d62012-04-11 12:09:53 +00002891 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002892 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002893 sha1_update( &sha1, padbuf , 40 );
2894 sha1_update( &sha1, sha1sum, 20 );
2895 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
Paul Bakker1ef83d62012-04-11 12:09:53 +00002897 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899 memset( &md5, 0, sizeof( md5_context ) );
2900 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
2902 memset( padbuf, 0, sizeof( padbuf ) );
2903 memset( md5sum, 0, sizeof( md5sum ) );
2904 memset( sha1sum, 0, sizeof( sha1sum ) );
2905
2906 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2907}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002908#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002910#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911static void ssl_calc_finished_tls(
2912 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002913{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002914 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002915 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002916 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002919
Paul Bakker48916f92012-09-16 19:57:18 +00002920 ssl_session *session = ssl->session_negotiate;
2921 if( !session )
2922 session = ssl->session;
2923
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Paul Bakker48916f92012-09-16 19:57:18 +00002926 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2927 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Paul Bakker1ef83d62012-04-11 12:09:53 +00002929 /*
2930 * TLSv1:
2931 * hash = PRF( master, finished_label,
2932 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2933 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Paul Bakker90995b52013-06-24 19:20:35 +02002935#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2937 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002938#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939
Paul Bakker90995b52013-06-24 19:20:35 +02002940#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002941 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2942 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002943#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002944
2945 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002946 ? "client finished"
2947 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002948
2949 md5_finish( &md5, padbuf );
2950 sha1_finish( &sha1, padbuf + 16 );
2951
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002952 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002953 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954
2955 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2956
2957 memset( &md5, 0, sizeof( md5_context ) );
2958 memset( &sha1, 0, sizeof( sha1_context ) );
2959
2960 memset( padbuf, 0, sizeof( padbuf ) );
2961
2962 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2963}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002964#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002966#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2967#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002968static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969 ssl_context *ssl, unsigned char *buf, int from )
2970{
2971 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002972 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002973 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974 unsigned char padbuf[32];
2975
Paul Bakker48916f92012-09-16 19:57:18 +00002976 ssl_session *session = ssl->session_negotiate;
2977 if( !session )
2978 session = ssl->session;
2979
Paul Bakker380da532012-04-18 16:10:25 +00002980 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002981
Paul Bakker9e36f042013-06-30 14:34:05 +02002982 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002983
2984 /*
2985 * TLSv1.2:
2986 * hash = PRF( master, finished_label,
2987 * Hash( handshake ) )[0.11]
2988 */
2989
Paul Bakker9e36f042013-06-30 14:34:05 +02002990#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002991 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002992 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002993#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994
2995 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002996 ? "client finished"
2997 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002998
Paul Bakker9e36f042013-06-30 14:34:05 +02002999 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003000
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003001 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003002 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003003
3004 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3005
Paul Bakker9e36f042013-06-30 14:34:05 +02003006 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003007
3008 memset( padbuf, 0, sizeof( padbuf ) );
3009
3010 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3011}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003012#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003013
Paul Bakker9e36f042013-06-30 14:34:05 +02003014#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003015static void ssl_calc_finished_tls_sha384(
3016 ssl_context *ssl, unsigned char *buf, int from )
3017{
3018 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003019 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003020 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003021 unsigned char padbuf[48];
3022
Paul Bakker48916f92012-09-16 19:57:18 +00003023 ssl_session *session = ssl->session_negotiate;
3024 if( !session )
3025 session = ssl->session;
3026
Paul Bakker380da532012-04-18 16:10:25 +00003027 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003028
Paul Bakker9e36f042013-06-30 14:34:05 +02003029 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003030
3031 /*
3032 * TLSv1.2:
3033 * hash = PRF( master, finished_label,
3034 * Hash( handshake ) )[0.11]
3035 */
3036
Paul Bakker9e36f042013-06-30 14:34:05 +02003037#if !defined(POLARSSL_SHA512_ALT)
3038 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3039 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003040#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003041
3042 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003043 ? "client finished"
3044 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003045
Paul Bakker9e36f042013-06-30 14:34:05 +02003046 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003047
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003048 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003049 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003050
3051 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3052
Paul Bakker9e36f042013-06-30 14:34:05 +02003053 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003054
3055 memset( padbuf, 0, sizeof( padbuf ) );
3056
3057 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3058}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003059#endif /* POLARSSL_SHA512_C */
3060#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003061
Paul Bakker48916f92012-09-16 19:57:18 +00003062void ssl_handshake_wrapup( ssl_context *ssl )
3063{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003064 int resume = ssl->handshake->resume;
3065
Paul Bakker48916f92012-09-16 19:57:18 +00003066 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3067
3068 /*
3069 * Free our handshake params
3070 */
3071 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003072 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003073 ssl->handshake = NULL;
3074
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003075 if( ssl->renegotiation == SSL_RENEGOTIATION )
3076 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3077
Paul Bakker48916f92012-09-16 19:57:18 +00003078 /*
3079 * Switch in our now active transform context
3080 */
3081 if( ssl->transform )
3082 {
3083 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003084 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003085 }
3086 ssl->transform = ssl->transform_negotiate;
3087 ssl->transform_negotiate = NULL;
3088
Paul Bakker0a597072012-09-25 21:55:46 +00003089 if( ssl->session )
3090 {
3091 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003092 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003093 }
3094 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003095 ssl->session_negotiate = NULL;
3096
Paul Bakker0a597072012-09-25 21:55:46 +00003097 /*
3098 * Add cache entry
3099 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003100 if( ssl->f_set_cache != NULL &&
3101 ssl->session->length != 0 &&
3102 resume == 0 )
3103 {
Paul Bakker0a597072012-09-25 21:55:46 +00003104 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3105 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003106 }
Paul Bakker0a597072012-09-25 21:55:46 +00003107
Paul Bakker48916f92012-09-16 19:57:18 +00003108 ssl->state++;
3109
3110 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3111}
3112
Paul Bakker1ef83d62012-04-11 12:09:53 +00003113int ssl_write_finished( ssl_context *ssl )
3114{
3115 int ret, hash_len;
3116
3117 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3118
Paul Bakker92be97b2013-01-02 17:30:03 +01003119 /*
3120 * Set the out_msg pointer to the correct location based on IV length
3121 */
3122 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3123 {
3124 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3125 ssl->transform_negotiate->fixed_ivlen;
3126 }
3127 else
3128 ssl->out_msg = ssl->out_iv;
3129
Paul Bakker48916f92012-09-16 19:57:18 +00003130 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003131
3132 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3134
Paul Bakker48916f92012-09-16 19:57:18 +00003135 ssl->verify_data_len = hash_len;
3136 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3137
Paul Bakker5121ce52009-01-03 21:22:43 +00003138 ssl->out_msglen = 4 + hash_len;
3139 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3140 ssl->out_msg[0] = SSL_HS_FINISHED;
3141
3142 /*
3143 * In case of session resuming, invert the client and server
3144 * ChangeCipherSpec messages order.
3145 */
Paul Bakker0a597072012-09-25 21:55:46 +00003146 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003147 {
3148 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003149 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003150 else
3151 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3152 }
3153 else
3154 ssl->state++;
3155
Paul Bakker48916f92012-09-16 19:57:18 +00003156 /*
3157 * Switch to our negotiated transform and session parameters for outbound data.
3158 */
3159 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3160 ssl->transform_out = ssl->transform_negotiate;
3161 ssl->session_out = ssl->session_negotiate;
3162 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003163
Paul Bakker07eb38b2012-12-19 14:42:06 +01003164#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3165 if( ssl_hw_record_activate != NULL)
3166 {
3167 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3168 {
3169 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3170 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3171 }
3172 }
3173#endif
3174
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3176 {
3177 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3178 return( ret );
3179 }
3180
3181 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3182
3183 return( 0 );
3184}
3185
3186int ssl_parse_finished( ssl_context *ssl )
3187{
Paul Bakker23986e52011-04-24 08:57:21 +00003188 int ret;
3189 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003190 unsigned char buf[36];
3191
3192 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3193
Paul Bakker48916f92012-09-16 19:57:18 +00003194 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003195
Paul Bakker48916f92012-09-16 19:57:18 +00003196 /*
3197 * Switch to our negotiated transform and session parameters for inbound data.
3198 */
3199 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3200 ssl->transform_in = ssl->transform_negotiate;
3201 ssl->session_in = ssl->session_negotiate;
3202 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Paul Bakker92be97b2013-01-02 17:30:03 +01003204 /*
3205 * Set the in_msg pointer to the correct location based on IV length
3206 */
3207 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3208 {
3209 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3210 ssl->transform_negotiate->fixed_ivlen;
3211 }
3212 else
3213 ssl->in_msg = ssl->in_iv;
3214
Paul Bakker07eb38b2012-12-19 14:42:06 +01003215#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3216 if( ssl_hw_record_activate != NULL)
3217 {
3218 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3219 {
3220 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3221 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3222 }
3223 }
3224#endif
3225
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3227 {
3228 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3229 return( ret );
3230 }
3231
3232 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3233 {
3234 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003235 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
3237
Paul Bakker1ef83d62012-04-11 12:09:53 +00003238 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3240
3241 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3242 ssl->in_hslen != 4 + hash_len )
3243 {
3244 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003245 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 }
3247
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003248 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 {
3250 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003251 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 }
3253
Paul Bakker48916f92012-09-16 19:57:18 +00003254 ssl->verify_data_len = hash_len;
3255 memcpy( ssl->peer_verify_data, buf, hash_len );
3256
Paul Bakker0a597072012-09-25 21:55:46 +00003257 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 {
3259 if( ssl->endpoint == SSL_IS_CLIENT )
3260 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3261
3262 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003263 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003264 }
3265 else
3266 ssl->state++;
3267
3268 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3269
3270 return( 0 );
3271}
3272
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003273static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003274{
3275 if( ssl->transform_negotiate )
3276 ssl_transform_free( ssl->transform_negotiate );
3277 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003278 {
3279 ssl->transform_negotiate =
3280 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3281 }
Paul Bakker48916f92012-09-16 19:57:18 +00003282
3283 if( ssl->session_negotiate )
3284 ssl_session_free( ssl->session_negotiate );
3285 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003286 {
3287 ssl->session_negotiate =
3288 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3289 }
Paul Bakker48916f92012-09-16 19:57:18 +00003290
3291 if( ssl->handshake )
3292 ssl_handshake_free( ssl->handshake );
3293 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003294 {
3295 ssl->handshake = (ssl_handshake_params *)
3296 polarssl_malloc( sizeof(ssl_handshake_params) );
3297 }
Paul Bakker48916f92012-09-16 19:57:18 +00003298
3299 if( ssl->handshake == NULL ||
3300 ssl->transform_negotiate == NULL ||
3301 ssl->session_negotiate == NULL )
3302 {
3303 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3304 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3305 }
3306
3307 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3308 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3309 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3310
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003311#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3312 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003313 md5_starts( &ssl->handshake->fin_md5 );
3314 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003315#endif
3316#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3317#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003318 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003319#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003320#if defined(POLARSSL_SHA512_C)
3321 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003322#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003323#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003324
3325 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003326 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003327
Paul Bakker61d113b2013-07-04 11:51:43 +02003328#if defined(POLARSSL_ECDH_C)
3329 ecdh_init( &ssl->handshake->ecdh_ctx );
3330#endif
3331
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003332#if defined(POLARSSL_X509_CRT_PARSE_C)
3333 ssl->handshake->key_cert = ssl->key_cert;
3334#endif
3335
Paul Bakker48916f92012-09-16 19:57:18 +00003336 return( 0 );
3337}
3338
Paul Bakker5121ce52009-01-03 21:22:43 +00003339/*
3340 * Initialize an SSL context
3341 */
3342int ssl_init( ssl_context *ssl )
3343{
Paul Bakker48916f92012-09-16 19:57:18 +00003344 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 int len = SSL_BUFFER_LEN;
3346
3347 memset( ssl, 0, sizeof( ssl_context ) );
3348
Paul Bakker62f2dee2012-09-28 07:31:51 +00003349 /*
3350 * Sane defaults
3351 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003352 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3353 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3354 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3355 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003356
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003357 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003358
Paul Bakker62f2dee2012-09-28 07:31:51 +00003359#if defined(POLARSSL_DHM_C)
3360 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3361 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3362 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3363 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3364 {
3365 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3366 return( ret );
3367 }
3368#endif
3369
3370 /*
3371 * Prepare base structures
3372 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003373 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003375 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 ssl->in_msg = ssl->in_ctr + 13;
3377
3378 if( ssl->in_ctr == NULL )
3379 {
3380 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003381 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003382 }
3383
Paul Bakker6e339b52013-07-03 13:37:05 +02003384 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003385 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003386 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003387 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003388
3389 if( ssl->out_ctr == NULL )
3390 {
3391 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003392 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003393 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 }
3395
3396 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3397 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3398
Paul Bakker606b4ba2013-08-14 16:52:14 +02003399#if defined(POLARSSL_SSL_SESSION_TICKETS)
3400 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3401#endif
3402
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003403#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003404 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003405#endif
3406
Paul Bakker48916f92012-09-16 19:57:18 +00003407 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3408 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003409
3410 return( 0 );
3411}
3412
3413/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003414 * Reset an initialized and used SSL context for re-use while retaining
3415 * all application-set variables, function pointers and data.
3416 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003417int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003418{
Paul Bakker48916f92012-09-16 19:57:18 +00003419 int ret;
3420
Paul Bakker7eb013f2011-10-06 12:37:39 +00003421 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003422 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3423 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3424
3425 ssl->verify_data_len = 0;
3426 memset( ssl->own_verify_data, 0, 36 );
3427 memset( ssl->peer_verify_data, 0, 36 );
3428
Paul Bakker7eb013f2011-10-06 12:37:39 +00003429 ssl->in_offt = NULL;
3430
Paul Bakker92be97b2013-01-02 17:30:03 +01003431 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003432 ssl->in_msgtype = 0;
3433 ssl->in_msglen = 0;
3434 ssl->in_left = 0;
3435
3436 ssl->in_hslen = 0;
3437 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003438 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003439
Paul Bakker92be97b2013-01-02 17:30:03 +01003440 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003441 ssl->out_msgtype = 0;
3442 ssl->out_msglen = 0;
3443 ssl->out_left = 0;
3444
Paul Bakker48916f92012-09-16 19:57:18 +00003445 ssl->transform_in = NULL;
3446 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003447
3448 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3449 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003450
3451#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3452 if( ssl_hw_record_reset != NULL)
3453 {
3454 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003455 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003456 {
3457 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3458 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3459 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003460 }
3461#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003462
Paul Bakker48916f92012-09-16 19:57:18 +00003463 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003464 {
Paul Bakker48916f92012-09-16 19:57:18 +00003465 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003466 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003467 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003468 }
Paul Bakker48916f92012-09-16 19:57:18 +00003469
Paul Bakkerc0463502013-02-14 11:19:38 +01003470 if( ssl->session )
3471 {
3472 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003473 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003474 ssl->session = NULL;
3475 }
3476
Paul Bakker48916f92012-09-16 19:57:18 +00003477 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3478 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003479
3480 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003481}
3482
Paul Bakkera503a632013-08-14 13:48:06 +02003483#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003484/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003485 * Allocate and initialize ticket keys
3486 */
3487static int ssl_ticket_keys_init( ssl_context *ssl )
3488{
3489 int ret;
3490 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003491 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003492
3493 if( ssl->ticket_keys != NULL )
3494 return( 0 );
3495
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003496 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3497 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003498 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3499
3500 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003501 {
3502 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003503 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003504 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003505
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003506 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3507 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3508 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3509 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003510 polarssl_free( tkeys );
3511 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003512 }
3513
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003514 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003515 {
3516 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003517 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003518 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003519
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003520 ssl->ticket_keys = tkeys;
3521
3522 return( 0 );
3523}
Paul Bakkera503a632013-08-14 13:48:06 +02003524#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003525
3526/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003527 * SSL set accessors
3528 */
3529void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3530{
3531 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003532
Paul Bakker606b4ba2013-08-14 16:52:14 +02003533#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003534 if( endpoint == SSL_IS_CLIENT )
3535 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003536#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003537}
3538
3539void ssl_set_authmode( ssl_context *ssl, int authmode )
3540{
3541 ssl->authmode = authmode;
3542}
3543
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003544#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003545void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003546 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003547 void *p_vrfy )
3548{
3549 ssl->f_vrfy = f_vrfy;
3550 ssl->p_vrfy = p_vrfy;
3551}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003552#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003553
Paul Bakker5121ce52009-01-03 21:22:43 +00003554void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003555 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003556 void *p_rng )
3557{
3558 ssl->f_rng = f_rng;
3559 ssl->p_rng = p_rng;
3560}
3561
3562void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003563 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003564 void *p_dbg )
3565{
3566 ssl->f_dbg = f_dbg;
3567 ssl->p_dbg = p_dbg;
3568}
3569
3570void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003571 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003572 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003573{
3574 ssl->f_recv = f_recv;
3575 ssl->f_send = f_send;
3576 ssl->p_recv = p_recv;
3577 ssl->p_send = p_send;
3578}
3579
Paul Bakker0a597072012-09-25 21:55:46 +00003580void ssl_set_session_cache( ssl_context *ssl,
3581 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3582 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003583{
Paul Bakker0a597072012-09-25 21:55:46 +00003584 ssl->f_get_cache = f_get_cache;
3585 ssl->p_get_cache = p_get_cache;
3586 ssl->f_set_cache = f_set_cache;
3587 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003588}
3589
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003590int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003591{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003592 int ret;
3593
3594 if( ssl == NULL ||
3595 session == NULL ||
3596 ssl->session_negotiate == NULL ||
3597 ssl->endpoint != SSL_IS_CLIENT )
3598 {
3599 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3600 }
3601
3602 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3603 return( ret );
3604
Paul Bakker0a597072012-09-25 21:55:46 +00003605 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003606
3607 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003608}
3609
Paul Bakkerb68cad62012-08-23 08:34:18 +00003610void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003611{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003612 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3613 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3614 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3615 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3616}
3617
3618void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3619 int major, int minor )
3620{
3621 if( major != SSL_MAJOR_VERSION_3 )
3622 return;
3623
3624 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3625 return;
3626
3627 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003628}
3629
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003630#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003631/* Add a new (empty) key_cert entry an return a pointer to it */
3632static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3633{
3634 ssl_key_cert *key_cert, *last;
3635
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003636 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3637 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003638 return( NULL );
3639
3640 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3641
3642 /* Append the new key_cert to the (possibly empty) current list */
3643 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003644 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003645 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003646 if( ssl->handshake != NULL )
3647 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003648 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003649 else
3650 {
3651 last = ssl->key_cert;
3652 while( last->next != NULL )
3653 last = last->next;
3654 last->next = key_cert;
3655 }
3656
3657 return key_cert;
3658}
3659
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003660void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003661 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003662{
3663 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003664 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 ssl->peer_cn = peer_cn;
3666}
3667
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003668int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003669 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003670{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003671 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3672
3673 if( key_cert == NULL )
3674 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3675
3676 key_cert->cert = own_cert;
3677 key_cert->key = pk_key;
3678
3679 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003680}
3681
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003682#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003683int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003684 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003685{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003686 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003687 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003688
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003689 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003690 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3691
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003692 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3693 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003694 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003695
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003696 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003697
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003698 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003699 if( ret != 0 )
3700 return( ret );
3701
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003702 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003703 return( ret );
3704
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003705 key_cert->cert = own_cert;
3706 key_cert->key_own_alloc = 1;
3707
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003708 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003709}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003710#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003712int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003713 void *rsa_key,
3714 rsa_decrypt_func rsa_decrypt,
3715 rsa_sign_func rsa_sign,
3716 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003717{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003718 int ret;
3719 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003720
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003721 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003722 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3723
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003724 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3725 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003726 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003727
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003728 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003729
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003730 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3731 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3732 return( ret );
3733
3734 key_cert->cert = own_cert;
3735 key_cert->key_own_alloc = 1;
3736
3737 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003738}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003739#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003740
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003741#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003742int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3743 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003744{
Paul Bakker6db455e2013-09-18 17:29:31 +02003745 if( psk == NULL || psk_identity == NULL )
3746 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3747
3748 if( ssl->psk != NULL )
3749 {
3750 polarssl_free( ssl->psk );
3751 polarssl_free( ssl->psk_identity );
3752 }
3753
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003754 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003755 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003756
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003757 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3758 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003759
3760 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3761 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3762
3763 memcpy( ssl->psk, psk, ssl->psk_len );
3764 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003765
3766 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003767}
3768
3769void ssl_set_psk_cb( ssl_context *ssl,
3770 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3771 size_t),
3772 void *p_psk )
3773{
3774 ssl->f_psk = f_psk;
3775 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003776}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003777#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003778
Paul Bakker48916f92012-09-16 19:57:18 +00003779#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003780int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003781{
3782 int ret;
3783
Paul Bakker48916f92012-09-16 19:57:18 +00003784 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003785 {
3786 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3787 return( ret );
3788 }
3789
Paul Bakker48916f92012-09-16 19:57:18 +00003790 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003791 {
3792 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3793 return( ret );
3794 }
3795
3796 return( 0 );
3797}
3798
Paul Bakker1b57b062011-01-06 15:48:19 +00003799int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3800{
3801 int ret;
3802
Paul Bakker48916f92012-09-16 19:57:18 +00003803 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003804 {
3805 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3806 return( ret );
3807 }
3808
Paul Bakker48916f92012-09-16 19:57:18 +00003809 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003810 {
3811 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3812 return( ret );
3813 }
3814
3815 return( 0 );
3816}
Paul Bakker48916f92012-09-16 19:57:18 +00003817#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003818
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003819#if defined(POLARSSL_SSL_SET_CURVES)
3820/*
3821 * Set the allowed elliptic curves
3822 */
3823void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3824{
3825 ssl->curve_list = curve_list;
3826}
3827#endif
3828
Paul Bakker0be444a2013-08-27 21:55:01 +02003829#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003830int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003831{
3832 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003833 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003834
3835 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003836
3837 if( ssl->hostname_len + 1 == 0 )
3838 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3839
Paul Bakker6e339b52013-07-03 13:37:05 +02003840 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003841
Paul Bakkerb15b8512012-01-13 13:44:06 +00003842 if( ssl->hostname == NULL )
3843 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3844
Paul Bakker3c2122f2013-06-24 19:03:14 +02003845 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003846 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003847
Paul Bakker40ea7de2009-05-03 10:18:48 +00003848 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003849
3850 return( 0 );
3851}
3852
Paul Bakker5701cdc2012-09-27 21:49:42 +00003853void ssl_set_sni( ssl_context *ssl,
3854 int (*f_sni)(void *, ssl_context *,
3855 const unsigned char *, size_t),
3856 void *p_sni )
3857{
3858 ssl->f_sni = f_sni;
3859 ssl->p_sni = p_sni;
3860}
Paul Bakker0be444a2013-08-27 21:55:01 +02003861#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003862
Paul Bakker490ecc82011-10-06 13:04:09 +00003863void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3864{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003865 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3866 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3867 {
3868 ssl->max_major_ver = major;
3869 ssl->max_minor_ver = minor;
3870 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003871}
3872
Paul Bakker1d29fb52012-09-28 13:28:45 +00003873void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3874{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003875 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3876 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3877 {
3878 ssl->min_major_ver = major;
3879 ssl->min_minor_ver = minor;
3880 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003881}
3882
Paul Bakker05decb22013-08-15 13:33:48 +02003883#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003884int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3885{
Paul Bakker77e257e2013-12-16 15:29:52 +01003886 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003887 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003888 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003889 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003890 }
3891
3892 ssl->mfl_code = mfl_code;
3893
3894 return( 0 );
3895}
Paul Bakker05decb22013-08-15 13:33:48 +02003896#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003897
Paul Bakker1f2bc622013-08-15 13:45:55 +02003898#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003899int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003900{
3901 if( ssl->endpoint != SSL_IS_CLIENT )
3902 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3903
Paul Bakker8c1ede62013-07-19 14:14:37 +02003904 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003905
3906 return( 0 );
3907}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003908#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003909
Paul Bakker48916f92012-09-16 19:57:18 +00003910void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3911{
3912 ssl->disable_renegotiation = renegotiation;
3913}
3914
3915void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3916{
3917 ssl->allow_legacy_renegotiation = allow_legacy;
3918}
3919
Paul Bakkera503a632013-08-14 13:48:06 +02003920#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003921int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3922{
3923 ssl->session_tickets = use_tickets;
3924
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003925 if( ssl->endpoint == SSL_IS_CLIENT )
3926 return( 0 );
3927
3928 if( ssl->f_rng == NULL )
3929 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3930
3931 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003932}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003933
3934void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3935{
3936 ssl->ticket_lifetime = lifetime;
3937}
Paul Bakkera503a632013-08-14 13:48:06 +02003938#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003939
Paul Bakker5121ce52009-01-03 21:22:43 +00003940/*
3941 * SSL get accessors
3942 */
Paul Bakker23986e52011-04-24 08:57:21 +00003943size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003944{
3945 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3946}
3947
Paul Bakkerff60ee62010-03-16 21:09:09 +00003948int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003949{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003950 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003951}
3952
Paul Bakkere3166ce2011-01-27 17:40:50 +00003953const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003954{
Paul Bakker926c8e42013-03-06 10:23:34 +01003955 if( ssl == NULL || ssl->session == NULL )
3956 return NULL;
3957
Paul Bakkere3166ce2011-01-27 17:40:50 +00003958 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003959}
3960
Paul Bakker43ca69c2011-01-15 17:35:19 +00003961const char *ssl_get_version( const ssl_context *ssl )
3962{
3963 switch( ssl->minor_ver )
3964 {
3965 case SSL_MINOR_VERSION_0:
3966 return( "SSLv3.0" );
3967
3968 case SSL_MINOR_VERSION_1:
3969 return( "TLSv1.0" );
3970
3971 case SSL_MINOR_VERSION_2:
3972 return( "TLSv1.1" );
3973
Paul Bakker1ef83d62012-04-11 12:09:53 +00003974 case SSL_MINOR_VERSION_3:
3975 return( "TLSv1.2" );
3976
Paul Bakker43ca69c2011-01-15 17:35:19 +00003977 default:
3978 break;
3979 }
3980 return( "unknown" );
3981}
3982
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003983#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003984const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003985{
3986 if( ssl == NULL || ssl->session == NULL )
3987 return NULL;
3988
3989 return ssl->session->peer_cert;
3990}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003991#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003992
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003993int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3994{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003995 if( ssl == NULL ||
3996 dst == NULL ||
3997 ssl->session == NULL ||
3998 ssl->endpoint != SSL_IS_CLIENT )
3999 {
4000 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4001 }
4002
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004003 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004004}
4005
Paul Bakker5121ce52009-01-03 21:22:43 +00004006/*
Paul Bakker1961b702013-01-25 14:49:24 +01004007 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004008 */
Paul Bakker1961b702013-01-25 14:49:24 +01004009int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004010{
Paul Bakker40e46942009-01-03 21:51:57 +00004011 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004012
Paul Bakker40e46942009-01-03 21:51:57 +00004013#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004014 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004015 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004016#endif
4017
Paul Bakker40e46942009-01-03 21:51:57 +00004018#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004019 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004020 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004021#endif
4022
Paul Bakker1961b702013-01-25 14:49:24 +01004023 return( ret );
4024}
4025
4026/*
4027 * Perform the SSL handshake
4028 */
4029int ssl_handshake( ssl_context *ssl )
4030{
4031 int ret = 0;
4032
4033 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4034
4035 while( ssl->state != SSL_HANDSHAKE_OVER )
4036 {
4037 ret = ssl_handshake_step( ssl );
4038
4039 if( ret != 0 )
4040 break;
4041 }
4042
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4044
4045 return( ret );
4046}
4047
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004048#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004049/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004050 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004051 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004052static int ssl_write_hello_request( ssl_context *ssl )
4053{
4054 int ret;
4055
4056 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4057
4058 ssl->out_msglen = 4;
4059 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4060 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4061
4062 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4063 {
4064 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4065 return( ret );
4066 }
4067
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004068 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4069
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004070 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4071
4072 return( 0 );
4073}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004074#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004075
4076/*
4077 * Actually renegotiate current connection, triggered by either:
4078 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004079 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004080 * - receiving any handshake message on server during ssl_read() after the
4081 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004082 * If the handshake doesn't complete due to waiting for I/O, it will continue
4083 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004084 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004085static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004086{
4087 int ret;
4088
4089 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4090
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004091 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4092 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004093
4094 ssl->state = SSL_HELLO_REQUEST;
4095 ssl->renegotiation = SSL_RENEGOTIATION;
4096
Paul Bakker48916f92012-09-16 19:57:18 +00004097 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4098 {
4099 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4100 return( ret );
4101 }
4102
4103 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4104
4105 return( 0 );
4106}
4107
4108/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004109 * Renegotiate current connection on client,
4110 * or request renegotiation on server
4111 */
4112int ssl_renegotiate( ssl_context *ssl )
4113{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004114 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004115
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004116#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004117 /* On server, just send the request */
4118 if( ssl->endpoint == SSL_IS_SERVER )
4119 {
4120 if( ssl->state != SSL_HANDSHAKE_OVER )
4121 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4122
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004123 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004124 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004125#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004126
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004127#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004128 /*
4129 * On client, either start the renegotiation process or,
4130 * if already in progress, continue the handshake
4131 */
4132 if( ssl->renegotiation != SSL_RENEGOTIATION )
4133 {
4134 if( ssl->state != SSL_HANDSHAKE_OVER )
4135 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4136
4137 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4138 {
4139 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4140 return( ret );
4141 }
4142 }
4143 else
4144 {
4145 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4146 {
4147 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4148 return( ret );
4149 }
4150 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004151#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004152
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004153 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004154}
4155
4156/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004157 * Receive application data decrypted from the SSL layer
4158 */
Paul Bakker23986e52011-04-24 08:57:21 +00004159int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004160{
Paul Bakker23986e52011-04-24 08:57:21 +00004161 int ret;
4162 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004163
4164 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4165
4166 if( ssl->state != SSL_HANDSHAKE_OVER )
4167 {
4168 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4169 {
4170 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4171 return( ret );
4172 }
4173 }
4174
4175 if( ssl->in_offt == NULL )
4176 {
4177 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4178 {
Paul Bakker831a7552011-05-18 13:32:51 +00004179 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4180 return( 0 );
4181
Paul Bakker5121ce52009-01-03 21:22:43 +00004182 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4183 return( ret );
4184 }
4185
4186 if( ssl->in_msglen == 0 &&
4187 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4188 {
4189 /*
4190 * OpenSSL sends empty messages to randomize the IV
4191 */
4192 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4193 {
Paul Bakker831a7552011-05-18 13:32:51 +00004194 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4195 return( 0 );
4196
Paul Bakker5121ce52009-01-03 21:22:43 +00004197 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4198 return( ret );
4199 }
4200 }
4201
Paul Bakker48916f92012-09-16 19:57:18 +00004202 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4203 {
4204 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4205
4206 if( ssl->endpoint == SSL_IS_CLIENT &&
4207 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4208 ssl->in_hslen != 4 ) )
4209 {
4210 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4211 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4212 }
4213
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004214 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4215 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4216 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004217 {
4218 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4219
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004220#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004221 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004222 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004223 /*
4224 * SSLv3 does not have a "no_renegotiation" alert
4225 */
4226 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4227 return( ret );
4228 }
4229 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004230#endif
4231#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4232 defined(POLARSSL_SSL_PROTO_TLS1_2)
4233 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004234 {
4235 if( ( ret = ssl_send_alert_message( ssl,
4236 SSL_ALERT_LEVEL_WARNING,
4237 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4238 {
4239 return( ret );
4240 }
Paul Bakker48916f92012-09-16 19:57:18 +00004241 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004242 else
4243#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004244 {
4245 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004246 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004247 }
Paul Bakker48916f92012-09-16 19:57:18 +00004248 }
4249 else
4250 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004251 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004252 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004253 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004254 return( ret );
4255 }
4256
4257 return( POLARSSL_ERR_NET_WANT_READ );
4258 }
4259 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004260 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4261 {
4262 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4263 "but not honored by client" ) );
4264 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4265 }
Paul Bakker48916f92012-09-16 19:57:18 +00004266 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 {
4268 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004269 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004270 }
4271
4272 ssl->in_offt = ssl->in_msg;
4273 }
4274
4275 n = ( len < ssl->in_msglen )
4276 ? len : ssl->in_msglen;
4277
4278 memcpy( buf, ssl->in_offt, n );
4279 ssl->in_msglen -= n;
4280
4281 if( ssl->in_msglen == 0 )
4282 /* all bytes consumed */
4283 ssl->in_offt = NULL;
4284 else
4285 /* more data available */
4286 ssl->in_offt += n;
4287
4288 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4289
Paul Bakker23986e52011-04-24 08:57:21 +00004290 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004291}
4292
4293/*
4294 * Send application data to be encrypted by the SSL layer
4295 */
Paul Bakker23986e52011-04-24 08:57:21 +00004296int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004297{
Paul Bakker23986e52011-04-24 08:57:21 +00004298 int ret;
4299 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004300 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004301
4302 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4303
4304 if( ssl->state != SSL_HANDSHAKE_OVER )
4305 {
4306 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4307 {
4308 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4309 return( ret );
4310 }
4311 }
4312
Paul Bakker05decb22013-08-15 13:33:48 +02004313#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004314 /*
4315 * Assume mfl_code is correct since it was checked when set
4316 */
4317 max_len = mfl_code_to_length[ssl->mfl_code];
4318
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004319 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004320 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004321 */
4322 if( ssl->session_out != NULL &&
4323 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4324 {
4325 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4326 }
Paul Bakker05decb22013-08-15 13:33:48 +02004327#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004328
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004329 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004330
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 if( ssl->out_left != 0 )
4332 {
4333 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4334 {
4335 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4336 return( ret );
4337 }
4338 }
Paul Bakker887bd502011-06-08 13:10:54 +00004339 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004340 {
Paul Bakker887bd502011-06-08 13:10:54 +00004341 ssl->out_msglen = n;
4342 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4343 memcpy( ssl->out_msg, buf, n );
4344
4345 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4346 {
4347 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4348 return( ret );
4349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004350 }
4351
4352 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4353
Paul Bakker23986e52011-04-24 08:57:21 +00004354 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004355}
4356
4357/*
4358 * Notify the peer that the connection is being closed
4359 */
4360int ssl_close_notify( ssl_context *ssl )
4361{
4362 int ret;
4363
4364 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4365
4366 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4367 {
4368 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4369 return( ret );
4370 }
4371
4372 if( ssl->state == SSL_HANDSHAKE_OVER )
4373 {
Paul Bakker48916f92012-09-16 19:57:18 +00004374 if( ( ret = ssl_send_alert_message( ssl,
4375 SSL_ALERT_LEVEL_WARNING,
4376 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004377 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004378 return( ret );
4379 }
4380 }
4381
4382 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4383
4384 return( ret );
4385}
4386
Paul Bakker48916f92012-09-16 19:57:18 +00004387void ssl_transform_free( ssl_transform *transform )
4388{
4389#if defined(POLARSSL_ZLIB_SUPPORT)
4390 deflateEnd( &transform->ctx_deflate );
4391 inflateEnd( &transform->ctx_inflate );
4392#endif
4393
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004394 cipher_free_ctx( &transform->cipher_ctx_enc );
4395 cipher_free_ctx( &transform->cipher_ctx_dec );
4396
Paul Bakker61d113b2013-07-04 11:51:43 +02004397 md_free_ctx( &transform->md_ctx_enc );
4398 md_free_ctx( &transform->md_ctx_dec );
4399
Paul Bakker48916f92012-09-16 19:57:18 +00004400 memset( transform, 0, sizeof( ssl_transform ) );
4401}
4402
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004403#if defined(POLARSSL_X509_CRT_PARSE_C)
4404static void ssl_key_cert_free( ssl_key_cert *key_cert )
4405{
4406 ssl_key_cert *cur = key_cert, *next;
4407
4408 while( cur != NULL )
4409 {
4410 next = cur->next;
4411
4412 if( cur->key_own_alloc )
4413 {
4414 pk_free( cur->key );
4415 polarssl_free( cur->key );
4416 }
4417 polarssl_free( cur );
4418
4419 cur = next;
4420 }
4421}
4422#endif /* POLARSSL_X509_CRT_PARSE_C */
4423
Paul Bakker48916f92012-09-16 19:57:18 +00004424void ssl_handshake_free( ssl_handshake_params *handshake )
4425{
4426#if defined(POLARSSL_DHM_C)
4427 dhm_free( &handshake->dhm_ctx );
4428#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004429#if defined(POLARSSL_ECDH_C)
4430 ecdh_free( &handshake->ecdh_ctx );
4431#endif
4432
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004433#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004434 /* explicit void pointer cast for buggy MS compiler */
4435 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004436#endif
4437
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004438#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4439 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4440 /*
4441 * Free only the linked list wrapper, not the keys themselves
4442 * since the belong to the SNI callback
4443 */
4444 if( handshake->sni_key_cert != NULL )
4445 {
4446 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4447
4448 while( cur != NULL )
4449 {
4450 next = cur->next;
4451 polarssl_free( cur );
4452 cur = next;
4453 }
4454 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004455#endif
4456
Paul Bakker48916f92012-09-16 19:57:18 +00004457 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4458}
4459
4460void ssl_session_free( ssl_session *session )
4461{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004462#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004463 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004464 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004465 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004466 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004467 }
Paul Bakkered27a042013-04-18 22:46:23 +02004468#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004469
Paul Bakkera503a632013-08-14 13:48:06 +02004470#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004471 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004472#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004473
Paul Bakker0a597072012-09-25 21:55:46 +00004474 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004475}
4476
Paul Bakker5121ce52009-01-03 21:22:43 +00004477/*
4478 * Free an SSL context
4479 */
4480void ssl_free( ssl_context *ssl )
4481{
4482 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4483
Paul Bakker5121ce52009-01-03 21:22:43 +00004484 if( ssl->out_ctr != NULL )
4485 {
4486 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004487 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004488 }
4489
4490 if( ssl->in_ctr != NULL )
4491 {
4492 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004493 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 }
4495
Paul Bakker16770332013-10-11 09:59:44 +02004496#if defined(POLARSSL_ZLIB_SUPPORT)
4497 if( ssl->compress_buf != NULL )
4498 {
4499 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4500 polarssl_free( ssl->compress_buf );
4501 }
4502#endif
4503
Paul Bakker40e46942009-01-03 21:51:57 +00004504#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004505 mpi_free( &ssl->dhm_P );
4506 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004507#endif
4508
Paul Bakker48916f92012-09-16 19:57:18 +00004509 if( ssl->transform )
4510 {
4511 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004512 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004513 }
4514
4515 if( ssl->handshake )
4516 {
4517 ssl_handshake_free( ssl->handshake );
4518 ssl_transform_free( ssl->transform_negotiate );
4519 ssl_session_free( ssl->session_negotiate );
4520
Paul Bakker6e339b52013-07-03 13:37:05 +02004521 polarssl_free( ssl->handshake );
4522 polarssl_free( ssl->transform_negotiate );
4523 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004524 }
4525
Paul Bakkerc0463502013-02-14 11:19:38 +01004526 if( ssl->session )
4527 {
4528 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004529 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004530 }
4531
Paul Bakkera503a632013-08-14 13:48:06 +02004532#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004533 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004534#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004535
Paul Bakker0be444a2013-08-27 21:55:01 +02004536#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004537 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004538 {
4539 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004540 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004541 ssl->hostname_len = 0;
4542 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004543#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004544
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004545#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004546 if( ssl->psk != NULL )
4547 {
4548 memset( ssl->psk, 0, ssl->psk_len );
4549 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4550 polarssl_free( ssl->psk );
4551 polarssl_free( ssl->psk_identity );
4552 ssl->psk_len = 0;
4553 ssl->psk_identity_len = 0;
4554 }
4555#endif
4556
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004557#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004558 ssl_key_cert_free( ssl->key_cert );
4559#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004560
Paul Bakker05ef8352012-05-08 09:17:57 +00004561#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4562 if( ssl_hw_record_finish != NULL )
4563 {
4564 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4565 ssl_hw_record_finish( ssl );
4566 }
4567#endif
4568
Paul Bakker5121ce52009-01-03 21:22:43 +00004569 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004570
Paul Bakker86f04f42013-02-14 11:20:09 +01004571 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004572 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004573}
4574
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004575#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004576/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004577 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004578 */
4579unsigned char ssl_sig_from_pk( pk_context *pk )
4580{
4581#if defined(POLARSSL_RSA_C)
4582 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4583 return( SSL_SIG_RSA );
4584#endif
4585#if defined(POLARSSL_ECDSA_C)
4586 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4587 return( SSL_SIG_ECDSA );
4588#endif
4589 return( SSL_SIG_ANON );
4590}
4591
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004592pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4593{
4594 switch( sig )
4595 {
4596#if defined(POLARSSL_RSA_C)
4597 case SSL_SIG_RSA:
4598 return( POLARSSL_PK_RSA );
4599#endif
4600#if defined(POLARSSL_ECDSA_C)
4601 case SSL_SIG_ECDSA:
4602 return( POLARSSL_PK_ECDSA );
4603#endif
4604 default:
4605 return( POLARSSL_PK_NONE );
4606 }
4607}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004608#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004609
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004610/*
4611 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4612 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004613md_type_t ssl_md_alg_from_hash( unsigned char hash )
4614{
4615 switch( hash )
4616 {
4617#if defined(POLARSSL_MD5_C)
4618 case SSL_HASH_MD5:
4619 return( POLARSSL_MD_MD5 );
4620#endif
4621#if defined(POLARSSL_SHA1_C)
4622 case SSL_HASH_SHA1:
4623 return( POLARSSL_MD_SHA1 );
4624#endif
4625#if defined(POLARSSL_SHA256_C)
4626 case SSL_HASH_SHA224:
4627 return( POLARSSL_MD_SHA224 );
4628 case SSL_HASH_SHA256:
4629 return( POLARSSL_MD_SHA256 );
4630#endif
4631#if defined(POLARSSL_SHA512_C)
4632 case SSL_HASH_SHA384:
4633 return( POLARSSL_MD_SHA384 );
4634 case SSL_HASH_SHA512:
4635 return( POLARSSL_MD_SHA512 );
4636#endif
4637 default:
4638 return( POLARSSL_MD_NONE );
4639 }
4640}
4641
Paul Bakker5121ce52009-01-03 21:22:43 +00004642#endif
Gergely Budai987bfb52014-01-19 21:48:42 +01004643
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004644#if defined(POLARSSL_SSL_SET_CURVES)
4645/*
4646 * Check is a curve proposed by the peer is in our list.
4647 * Return 1 if we're willing to use it, 0 otherwise.
4648 */
4649int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4650{
4651 const ecp_group_id *gid;
4652
4653 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4654 if( *gid == grp_id )
4655 return( 1 );
4656
4657 return( 0 );
4658}
4659#endif