blob: 3fd6e341ada0c842ae848ba8564dc4b86fc8303a [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
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001312 /* The loops goes to its end iff the counter is wrapping */
1313 if( i == 0 )
1314 {
1315 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1316 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1317 }
1318
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1320
1321 return( 0 );
1322}
1323
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001324#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001325
Paul Bakker5121ce52009-01-03 21:22:43 +00001326static int ssl_decrypt_buf( ssl_context *ssl )
1327{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001328 size_t i;
1329#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1330 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1331 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1332 size_t padlen = 0, correct = 1;
1333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
1335 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1336
Paul Bakker48916f92012-09-16 19:57:18 +00001337 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 {
1339 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001340 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001341 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 }
1343
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001344#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001345 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1346 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001347 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001348 int ret;
1349 size_t olen = 0;
1350
Paul Bakker68884e32013-01-07 18:20:04 +01001351 padlen = 0;
1352
Paul Bakker45125bc2013-09-04 16:47:11 +02001353 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001354 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001355 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1356 return( ret );
1357 }
1358
1359 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1360 ssl->transform_in->iv_dec,
1361 ssl->transform_in->ivlen ) ) != 0 )
1362 {
1363 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001364 return( ret );
1365 }
1366
1367 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1368 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1369 &olen ) ) != 0 )
1370 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001371 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001372 return( ret );
1373 }
1374
1375 if( ssl->in_msglen != olen )
1376 {
1377 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001378 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001379 }
1380
1381 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001382 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001383 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001384 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001385 return( ret );
1386 }
1387
1388 if( 0 != olen )
1389 {
1390 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001391 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 }
Paul Bakker68884e32013-01-07 18:20:04 +01001394 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001395#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001396#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001397 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1398 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001399 {
1400 unsigned char *dec_msg;
1401 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001402 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001403 unsigned char add_data[13];
1404 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1405
Paul Bakker68884e32013-01-07 18:20:04 +01001406 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1407 ssl->transform_in->fixed_ivlen );
1408 dec_msglen -= 16;
1409 dec_msg = ssl->in_msg;
1410 dec_msg_result = ssl->in_msg;
1411 ssl->in_msglen = dec_msglen;
1412
1413 memcpy( add_data, ssl->in_ctr, 8 );
1414 add_data[8] = ssl->in_msgtype;
1415 add_data[9] = ssl->major_ver;
1416 add_data[10] = ssl->minor_ver;
1417 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1418 add_data[12] = ssl->in_msglen & 0xFF;
1419
1420 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1421 add_data, 13 );
1422
1423 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1424 ssl->in_iv,
1425 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1426
1427 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1428 ssl->transform_in->ivlen );
1429 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1430
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001431 /*
1432 * Decrypt
1433 */
1434 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1435 ssl->transform_in->iv_dec,
1436 ssl->transform_in->ivlen ) ) != 0 ||
1437 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001438 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001439 return( ret );
1440 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001441
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001442 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1443 add_data, 13 ) ) != 0 )
1444 {
1445 return( ret );
1446 }
1447
1448 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1449 dec_msg, dec_msglen,
1450 dec_msg_result, &olen ) ) != 0 )
1451 {
1452 return( ret );
1453 }
1454 totlen = olen;
1455
1456 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1457 dec_msg_result + olen, &olen ) ) != 0 )
1458 {
1459 return( ret );
1460 }
1461 totlen += olen;
1462
1463 if( totlen != dec_msglen )
1464 {
1465 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1466 return( -1 );
1467 }
1468
1469 /*
1470 * Authenticate
1471 */
1472 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1473 dec_msg + dec_msglen, 16 ) ) != 0 )
1474 {
1475 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001476 return( POLARSSL_ERR_SSL_INVALID_MAC );
1477 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001478
Paul Bakkerca4ab492012-04-18 14:23:57 +00001479 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 else
Paul Bakker68884e32013-01-07 18:20:04 +01001481#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001482#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1483 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001484 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1485 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 {
Paul Bakker45829992013-01-03 14:52:21 +01001487 /*
1488 * Decrypt and check the padding
1489 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001490 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001491 unsigned char *dec_msg;
1492 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001493 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001494 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001495 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001496
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 /*
Paul Bakker45829992013-01-03 14:52:21 +01001498 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 */
Paul Bakker48916f92012-09-16 19:57:18 +00001500 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 {
1502 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001503 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001504 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 }
1506
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001507#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001508 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1509 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001510#endif
Paul Bakker45829992013-01-03 14:52:21 +01001511
1512 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1513 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1514 {
1515 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1516 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1517 return( POLARSSL_ERR_SSL_INVALID_MAC );
1518 }
1519
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001520 dec_msglen = ssl->in_msglen;
1521 dec_msg = ssl->in_msg;
1522 dec_msg_result = ssl->in_msg;
1523
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001524#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001525 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001526 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001527 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001528 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001529 {
Paul Bakker48916f92012-09-16 19:57:18 +00001530 dec_msglen -= ssl->transform_in->ivlen;
1531 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001532
Paul Bakker48916f92012-09-16 19:57:18 +00001533 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001534 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001535 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001536#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001537
Paul Bakker45125bc2013-09-04 16:47:11 +02001538 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001540 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1541 return( ret );
1542 }
1543
1544 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1545 ssl->transform_in->iv_dec,
1546 ssl->transform_in->ivlen ) ) != 0 )
1547 {
1548 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001549 return( ret );
1550 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001551
Paul Bakkercca5b812013-08-31 17:40:26 +02001552 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1553 dec_msg, dec_msglen, dec_msg_result,
1554 &olen ) ) != 0 )
1555 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001556 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001557 return( ret );
1558 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001559
Paul Bakkercca5b812013-08-31 17:40:26 +02001560 dec_msglen -= olen;
1561 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001562 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001563 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001564 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001565 return( ret );
1566 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001567
Paul Bakkercca5b812013-08-31 17:40:26 +02001568 if( dec_msglen != olen )
1569 {
1570 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001571 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001572 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001573
1574#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001575 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1576 {
1577 /*
1578 * Save IV in SSL3 and TLS1
1579 */
1580 memcpy( ssl->transform_in->iv_dec,
1581 ssl->transform_in->cipher_ctx_dec.iv,
1582 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001584#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
1586 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001587
1588 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1589 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001590#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001591 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1592 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001593#endif
Paul Bakker45829992013-01-03 14:52:21 +01001594 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001595 correct = 0;
1596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001598#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1600 {
Paul Bakker48916f92012-09-16 19:57:18 +00001601 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001603#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001604 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1605 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001606 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001607#endif
Paul Bakker45829992013-01-03 14:52:21 +01001608 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 }
1610 }
1611 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001612#endif
1613#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1614 defined(POLARSSL_SSL_PROTO_TLS1_2)
1615 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 {
1617 /*
Paul Bakker45829992013-01-03 14:52:21 +01001618 * TLSv1+: always check the padding up to the first failure
1619 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001621 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001622 size_t padding_idx = ssl->in_msglen - padlen - 1;
1623
Paul Bakker956c9e02013-12-19 14:42:28 +01001624 /*
1625 * Padding is guaranteed to be incorrect if:
1626 * 1. padlen - 1 > ssl->in_msglen
1627 *
1628 * 2. ssl->in_msglen + padlen >
1629 * SSL_MAX_CONTENT_LEN + 256 (max padding)
1630 *
1631 * In both cases we reset padding_idx to a safe value (0) to
1632 * prevent out-of-buffer reads.
1633 */
1634 correct &= ( ssl->in_msglen >= padlen - 1 );
1635 correct &= ( ssl->in_msglen + padlen <= SSL_MAX_CONTENT_LEN + 256 );
1636
1637 padding_idx *= correct;
1638
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001639 for( i = 1; i <= 256; i++ )
1640 {
1641 real_count &= ( i <= padlen );
1642 pad_count += real_count *
1643 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1644 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001645
1646 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001647
Paul Bakkerd66f0702013-01-31 16:57:45 +01001648#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001649 if( padlen > 0 && correct == 0)
1650 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001651#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001652 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001654 else
1655#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1656 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001657 {
1658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001659 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001661 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001662 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001663#endif /* POLARSSL_CIPHER_MODE_CBC &&
1664 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001665 {
1666 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1667 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
1670 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1671 ssl->in_msg, ssl->in_msglen );
1672
1673 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001674 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1677 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1678 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1679 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1680 POLARSSL_MODE_GCM )
1681 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001682 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1683
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001684 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001686 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1687 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001688
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001689 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001691#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001692 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1693 {
1694 ssl_mac( &ssl->transform_in->md_ctx_dec,
1695 ssl->transform_in->mac_dec,
1696 ssl->in_msg, ssl->in_msglen,
1697 ssl->in_ctr, ssl->in_msgtype );
1698 }
1699 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001700#endif /* POLARSSL_SSL_PROTO_SSL3 */
1701#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001702 defined(POLARSSL_SSL_PROTO_TLS1_2)
1703 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1704 {
1705 /*
1706 * Process MAC and always update for padlen afterwards to make
1707 * total time independent of padlen
1708 *
1709 * extra_run compensates MAC check for padlen
1710 *
1711 * Known timing attacks:
1712 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1713 *
1714 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1715 * correctly. (We round down instead of up, so -56 is the correct
1716 * value for our calculations instead of -55)
1717 */
1718 size_t j, extra_run = 0;
1719 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1720 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001721
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001722 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001723
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001724 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1725 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1726 ssl->in_msglen );
1727 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1728 ssl->in_msg + ssl->in_msglen );
1729 for( j = 0; j < extra_run; j++ )
1730 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001731
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001732 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1733 }
1734 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001735#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001736 POLARSSL_SSL_PROTO_TLS1_2 */
1737 {
1738 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1739 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1740 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001742 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1743 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1744 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001746 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001747 ssl->transform_in->maclen ) != 0 )
1748 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001749#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001750 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001751#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001752 correct = 0;
1753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001755 /*
1756 * Finally check the correct flag
1757 */
1758 if( correct == 0 )
1759 return( POLARSSL_ERR_SSL_INVALID_MAC );
1760 }
1761#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
1763 if( ssl->in_msglen == 0 )
1764 {
1765 ssl->nb_zero++;
1766
1767 /*
1768 * Three or more empty messages may be a DoS attack
1769 * (excessive CPU consumption).
1770 */
1771 if( ssl->nb_zero > 3 )
1772 {
1773 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1774 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001775 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 }
1777 }
1778 else
1779 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001780
Paul Bakker23986e52011-04-24 08:57:21 +00001781 for( i = 8; i > 0; i-- )
1782 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 break;
1784
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001785 /* The loops goes to its end iff the counter is wrapping */
1786 if( i == 0 )
1787 {
1788 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1789 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1790 }
1791
Paul Bakker5121ce52009-01-03 21:22:43 +00001792 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1793
1794 return( 0 );
1795}
1796
Paul Bakker2770fbd2012-07-03 13:30:23 +00001797#if defined(POLARSSL_ZLIB_SUPPORT)
1798/*
1799 * Compression/decompression functions
1800 */
1801static int ssl_compress_buf( ssl_context *ssl )
1802{
1803 int ret;
1804 unsigned char *msg_post = ssl->out_msg;
1805 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001806 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001807
1808 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1809
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001810 if( len_pre == 0 )
1811 return( 0 );
1812
Paul Bakker2770fbd2012-07-03 13:30:23 +00001813 memcpy( msg_pre, ssl->out_msg, len_pre );
1814
1815 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1816 ssl->out_msglen ) );
1817
1818 SSL_DEBUG_BUF( 4, "before compression: output payload",
1819 ssl->out_msg, ssl->out_msglen );
1820
Paul Bakker48916f92012-09-16 19:57:18 +00001821 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1822 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1823 ssl->transform_out->ctx_deflate.next_out = msg_post;
1824 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001825
Paul Bakker48916f92012-09-16 19:57:18 +00001826 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001827 if( ret != Z_OK )
1828 {
1829 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1830 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1831 }
1832
Paul Bakker48916f92012-09-16 19:57:18 +00001833 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001834
Paul Bakker2770fbd2012-07-03 13:30:23 +00001835 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1836 ssl->out_msglen ) );
1837
1838 SSL_DEBUG_BUF( 4, "after compression: output payload",
1839 ssl->out_msg, ssl->out_msglen );
1840
1841 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1842
1843 return( 0 );
1844}
1845
1846static int ssl_decompress_buf( ssl_context *ssl )
1847{
1848 int ret;
1849 unsigned char *msg_post = ssl->in_msg;
1850 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001851 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001852
1853 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1854
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001855 if( len_pre == 0 )
1856 return( 0 );
1857
Paul Bakker2770fbd2012-07-03 13:30:23 +00001858 memcpy( msg_pre, ssl->in_msg, len_pre );
1859
1860 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1861 ssl->in_msglen ) );
1862
1863 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1864 ssl->in_msg, ssl->in_msglen );
1865
Paul Bakker48916f92012-09-16 19:57:18 +00001866 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1867 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1868 ssl->transform_in->ctx_inflate.next_out = msg_post;
1869 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001870
Paul Bakker48916f92012-09-16 19:57:18 +00001871 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001872 if( ret != Z_OK )
1873 {
1874 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1875 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1876 }
1877
Paul Bakker48916f92012-09-16 19:57:18 +00001878 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001879
Paul Bakker2770fbd2012-07-03 13:30:23 +00001880 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1881 ssl->in_msglen ) );
1882
1883 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1884 ssl->in_msg, ssl->in_msglen );
1885
1886 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1887
1888 return( 0 );
1889}
1890#endif /* POLARSSL_ZLIB_SUPPORT */
1891
Paul Bakker5121ce52009-01-03 21:22:43 +00001892/*
1893 * Fill the input message buffer
1894 */
Paul Bakker23986e52011-04-24 08:57:21 +00001895int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001896{
Paul Bakker23986e52011-04-24 08:57:21 +00001897 int ret;
1898 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
1900 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1901
1902 while( ssl->in_left < nb_want )
1903 {
1904 len = nb_want - ssl->in_left;
1905 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1906
1907 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1908 ssl->in_left, nb_want ) );
1909 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1910
Paul Bakker831a7552011-05-18 13:32:51 +00001911 if( ret == 0 )
1912 return( POLARSSL_ERR_SSL_CONN_EOF );
1913
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 if( ret < 0 )
1915 return( ret );
1916
1917 ssl->in_left += ret;
1918 }
1919
1920 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1921
1922 return( 0 );
1923}
1924
1925/*
1926 * Flush any data not yet written
1927 */
1928int ssl_flush_output( ssl_context *ssl )
1929{
1930 int ret;
1931 unsigned char *buf;
1932
1933 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1934
1935 while( ssl->out_left > 0 )
1936 {
1937 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1938 5 + ssl->out_msglen, ssl->out_left ) );
1939
Paul Bakker5bd42292012-12-19 14:40:42 +01001940 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001942
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1944
1945 if( ret <= 0 )
1946 return( ret );
1947
1948 ssl->out_left -= ret;
1949 }
1950
1951 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1952
1953 return( 0 );
1954}
1955
1956/*
1957 * Record layer functions
1958 */
1959int ssl_write_record( ssl_context *ssl )
1960{
Paul Bakker05ef8352012-05-08 09:17:57 +00001961 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001962 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001963
1964 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1965
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1967 {
1968 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1969 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1970 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1971
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001972 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1973 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 }
1975
Paul Bakker2770fbd2012-07-03 13:30:23 +00001976#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001977 if( ssl->transform_out != NULL &&
1978 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001979 {
1980 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1981 {
1982 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1983 return( ret );
1984 }
1985
1986 len = ssl->out_msglen;
1987 }
1988#endif /*POLARSSL_ZLIB_SUPPORT */
1989
Paul Bakker05ef8352012-05-08 09:17:57 +00001990#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1991 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001993 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001994
Paul Bakker05ef8352012-05-08 09:17:57 +00001995 ret = ssl_hw_record_write( ssl );
1996 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1997 {
1998 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1999 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2000 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002001
2002 if( ret == 0 )
2003 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002004 }
2005#endif
2006 if( !done )
2007 {
2008 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2009 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2010 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2012 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002013
Paul Bakker48916f92012-09-16 19:57:18 +00002014 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002015 {
2016 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2017 {
2018 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2019 return( ret );
2020 }
2021
2022 len = ssl->out_msglen;
2023 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2024 ssl->out_hdr[4] = (unsigned char)( len );
2025 }
2026
2027 ssl->out_left = 5 + ssl->out_msglen;
2028
2029 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2030 "version = [%d:%d], msglen = %d",
2031 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2032 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2033
2034 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002035 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 }
2037
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2039 {
2040 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2041 return( ret );
2042 }
2043
2044 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2045
2046 return( 0 );
2047}
2048
2049int ssl_read_record( ssl_context *ssl )
2050{
Paul Bakker05ef8352012-05-08 09:17:57 +00002051 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002052
2053 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2054
Paul Bakker68884e32013-01-07 18:20:04 +01002055 SSL_DEBUG_BUF( 4, "input record from network",
2056 ssl->in_hdr, 5 + ssl->in_msglen );
2057
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 if( ssl->in_hslen != 0 &&
2059 ssl->in_hslen < ssl->in_msglen )
2060 {
2061 /*
2062 * Get next Handshake message in the current record
2063 */
2064 ssl->in_msglen -= ssl->in_hslen;
2065
Paul Bakker8934a982011-08-05 11:11:53 +00002066 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002067 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068
2069 ssl->in_hslen = 4;
2070 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2071
2072 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2073 " %d, type = %d, hslen = %d",
2074 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2075
2076 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2077 {
2078 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002079 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 }
2081
2082 if( ssl->in_msglen < ssl->in_hslen )
2083 {
2084 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002085 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 }
2087
Paul Bakker48916f92012-09-16 19:57:18 +00002088 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089
2090 return( 0 );
2091 }
2092
2093 ssl->in_hslen = 0;
2094
2095 /*
2096 * Read the record header and validate it
2097 */
2098 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2099 {
2100 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2101 return( ret );
2102 }
2103
2104 ssl->in_msgtype = ssl->in_hdr[0];
2105 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2106
2107 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2108 "version = [%d:%d], msglen = %d",
2109 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2110 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2111
2112 if( ssl->in_hdr[1] != ssl->major_ver )
2113 {
2114 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002115 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 }
2117
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002118 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 {
2120 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002121 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 }
2123
2124 /*
2125 * Make sure the message length is acceptable
2126 */
Paul Bakker48916f92012-09-16 19:57:18 +00002127 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 {
2129 if( ssl->in_msglen < 1 ||
2130 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2131 {
2132 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002133 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 }
2135 }
2136 else
2137 {
Paul Bakker48916f92012-09-16 19:57:18 +00002138 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 {
2140 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002141 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 }
2143
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002144#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002146 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 {
2148 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002149 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002151#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002153#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2154 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 /*
2156 * TLS encrypted messages can have up to 256 bytes of padding
2157 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002158 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002159 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 {
2161 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002162 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002164#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 }
2166
2167 /*
2168 * Read and optionally decrypt the message contents
2169 */
2170 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2171 {
2172 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2173 return( ret );
2174 }
2175
2176 SSL_DEBUG_BUF( 4, "input record from network",
2177 ssl->in_hdr, 5 + ssl->in_msglen );
2178
Paul Bakker05ef8352012-05-08 09:17:57 +00002179#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2180 if( ssl_hw_record_read != NULL)
2181 {
2182 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2183
2184 ret = ssl_hw_record_read( ssl );
2185 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2186 {
2187 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2188 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2189 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002190
2191 if( ret == 0 )
2192 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002193 }
2194#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002195 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 {
2197 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2198 {
Paul Bakker40865c82013-01-31 17:13:13 +01002199#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2200 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2201 {
2202 ssl_send_alert_message( ssl,
2203 SSL_ALERT_LEVEL_FATAL,
2204 SSL_ALERT_MSG_BAD_RECORD_MAC );
2205 }
2206#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2208 return( ret );
2209 }
2210
2211 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2212 ssl->in_msg, ssl->in_msglen );
2213
2214 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2215 {
2216 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002217 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 }
2219 }
2220
Paul Bakker2770fbd2012-07-03 13:30:23 +00002221#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002222 if( ssl->transform_in != NULL &&
2223 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002224 {
2225 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2226 {
2227 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2228 return( ret );
2229 }
2230
2231 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2232 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2233 }
2234#endif /* POLARSSL_ZLIB_SUPPORT */
2235
Paul Bakker0a925182012-04-16 06:46:41 +00002236 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2237 ssl->in_msgtype != SSL_MSG_ALERT &&
2238 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2239 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2240 {
2241 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2242
Paul Bakker48916f92012-09-16 19:57:18 +00002243 if( ( ret = ssl_send_alert_message( ssl,
2244 SSL_ALERT_LEVEL_FATAL,
2245 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002246 {
2247 return( ret );
2248 }
2249
2250 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2251 }
2252
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2254 {
2255 ssl->in_hslen = 4;
2256 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2257
2258 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2259 " %d, type = %d, hslen = %d",
2260 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2261
2262 /*
2263 * Additional checks to validate the handshake header
2264 */
2265 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2266 {
2267 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002268 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 }
2270
2271 if( ssl->in_msglen < ssl->in_hslen )
2272 {
2273 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002274 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 }
2276
Paul Bakker48916f92012-09-16 19:57:18 +00002277 if( ssl->state != SSL_HANDSHAKE_OVER )
2278 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 }
2280
2281 if( ssl->in_msgtype == SSL_MSG_ALERT )
2282 {
2283 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2284 ssl->in_msg[0], ssl->in_msg[1] ) );
2285
2286 /*
2287 * Ignore non-fatal alerts, except close_notify
2288 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002289 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002291 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2292 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002293 /**
2294 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2295 * error identifier.
2296 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002297 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 }
2299
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002300 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2301 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 {
2303 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002304 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 }
2306 }
2307
2308 ssl->in_left = 0;
2309
2310 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2311
2312 return( 0 );
2313}
2314
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002315int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2316{
2317 int ret;
2318
2319 if( ( ret = ssl_send_alert_message( ssl,
2320 SSL_ALERT_LEVEL_FATAL,
2321 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2322 {
2323 return( ret );
2324 }
2325
2326 return( 0 );
2327}
2328
Paul Bakker0a925182012-04-16 06:46:41 +00002329int ssl_send_alert_message( ssl_context *ssl,
2330 unsigned char level,
2331 unsigned char message )
2332{
2333 int ret;
2334
2335 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2336
2337 ssl->out_msgtype = SSL_MSG_ALERT;
2338 ssl->out_msglen = 2;
2339 ssl->out_msg[0] = level;
2340 ssl->out_msg[1] = message;
2341
2342 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2343 {
2344 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2345 return( ret );
2346 }
2347
2348 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2349
2350 return( 0 );
2351}
2352
Paul Bakker5121ce52009-01-03 21:22:43 +00002353/*
2354 * Handshake functions
2355 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002356#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2357 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2358 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2359 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2360 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2361 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2362 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002363int ssl_write_certificate( ssl_context *ssl )
2364{
Paul Bakkered27a042013-04-18 22:46:23 +02002365 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002366 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
2368 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2369
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002370 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002371 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2372 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002373 {
2374 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2375 ssl->state++;
2376 return( 0 );
2377 }
2378
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002379 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002380 return( ret );
2381}
2382
2383int ssl_parse_certificate( ssl_context *ssl )
2384{
2385 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2386 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2387
2388 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2389
2390 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002391 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2392 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002393 {
2394 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2395 ssl->state++;
2396 return( 0 );
2397 }
2398
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002399 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002400 return( ret );
2401}
2402#else
2403int ssl_write_certificate( ssl_context *ssl )
2404{
2405 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2406 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002407 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002408 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2409
2410 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2411
2412 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002413 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2414 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002415 {
2416 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2417 ssl->state++;
2418 return( 0 );
2419 }
2420
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 if( ssl->endpoint == SSL_IS_CLIENT )
2422 {
2423 if( ssl->client_auth == 0 )
2424 {
2425 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2426 ssl->state++;
2427 return( 0 );
2428 }
2429
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002430#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 /*
2432 * If using SSLv3 and got no cert, send an Alert message
2433 * (otherwise an empty Certificate message will be sent).
2434 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002435 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2437 {
2438 ssl->out_msglen = 2;
2439 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002440 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2441 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
2443 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2444 goto write_msg;
2445 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002446#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448 else /* SSL_IS_SERVER */
2449 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002450 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 {
2452 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002453 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 }
2455 }
2456
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002457 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
2459 /*
2460 * 0 . 0 handshake type
2461 * 1 . 3 handshake length
2462 * 4 . 6 length of all certs
2463 * 7 . 9 length of cert. 1
2464 * 10 . n-1 peer certificate
2465 * n . n+2 length of cert. 2
2466 * n+3 . ... upper level cert, etc.
2467 */
2468 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002469 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
Paul Bakker29087132010-03-21 21:03:34 +00002471 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 {
2473 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002474 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 {
2476 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2477 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002478 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 }
2480
2481 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2482 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2483 ssl->out_msg[i + 2] = (unsigned char)( n );
2484
2485 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2486 i += n; crt = crt->next;
2487 }
2488
2489 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2490 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2491 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2492
2493 ssl->out_msglen = i;
2494 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2495 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2496
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002497#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002498write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002499#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
2501 ssl->state++;
2502
2503 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2504 {
2505 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2506 return( ret );
2507 }
2508
2509 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2510
Paul Bakkered27a042013-04-18 22:46:23 +02002511 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512}
2513
2514int ssl_parse_certificate( ssl_context *ssl )
2515{
Paul Bakkered27a042013-04-18 22:46:23 +02002516 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002517 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002518 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
2520 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2521
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002522 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002523 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2524 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002525 {
2526 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2527 ssl->state++;
2528 return( 0 );
2529 }
2530
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002532 ( ssl->authmode == SSL_VERIFY_NONE ||
2533 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002535 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2537 ssl->state++;
2538 return( 0 );
2539 }
2540
2541 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2542 {
2543 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2544 return( ret );
2545 }
2546
2547 ssl->state++;
2548
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002549#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 /*
2551 * Check if the client sent an empty certificate
2552 */
2553 if( ssl->endpoint == SSL_IS_SERVER &&
2554 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2555 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002556 if( ssl->in_msglen == 2 &&
2557 ssl->in_msgtype == SSL_MSG_ALERT &&
2558 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2559 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 {
2561 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2562
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002563 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2565 return( 0 );
2566 else
Paul Bakker40e46942009-01-03 21:51:57 +00002567 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
2569 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002570#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002572#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2573 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 if( ssl->endpoint == SSL_IS_SERVER &&
2575 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2576 {
2577 if( ssl->in_hslen == 7 &&
2578 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2579 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2580 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2581 {
2582 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2583
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002584 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002586 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 else
2588 return( 0 );
2589 }
2590 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002591#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2592 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
2594 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2595 {
2596 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002597 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 }
2599
2600 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2601 {
2602 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002603 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605
2606 /*
2607 * Same message structure as in ssl_write_certificate()
2608 */
2609 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2610
2611 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2612 {
2613 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002614 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 }
2616
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002617 /* In case we tried to reuse a session but it failed */
2618 if( ssl->session_negotiate->peer_cert != NULL )
2619 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002620 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002621 polarssl_free( ssl->session_negotiate->peer_cert );
2622 }
2623
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002624 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2625 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 {
2627 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002628 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002629 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 }
2631
Paul Bakkerb6b09562013-09-18 14:17:41 +02002632 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
2634 i = 7;
2635
2636 while( i < ssl->in_hslen )
2637 {
2638 if( ssl->in_msg[i] != 0 )
2639 {
2640 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002641 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 }
2643
2644 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2645 | (unsigned int) ssl->in_msg[i + 2];
2646 i += 3;
2647
2648 if( n < 128 || i + n > ssl->in_hslen )
2649 {
2650 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002651 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 }
2653
Paul Bakkerddf26b42013-09-18 13:46:23 +02002654 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2655 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 if( ret != 0 )
2657 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002658 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 return( ret );
2660 }
2661
2662 i += n;
2663 }
2664
Paul Bakker48916f92012-09-16 19:57:18 +00002665 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002667 /*
2668 * On client, make sure the server cert doesn't change during renego to
2669 * avoid "triple handshake" attack: https://secure-resumption.com/
2670 */
2671 if( ssl->endpoint == SSL_IS_CLIENT &&
2672 ssl->renegotiation == SSL_RENEGOTIATION )
2673 {
2674 if( ssl->session->peer_cert == NULL )
2675 {
2676 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2677 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2678 }
2679
2680 if( ssl->session->peer_cert->raw.len !=
2681 ssl->session_negotiate->peer_cert->raw.len ||
2682 memcmp( ssl->session->peer_cert->raw.p,
2683 ssl->session_negotiate->peer_cert->raw.p,
2684 ssl->session->peer_cert->raw.len ) != 0 )
2685 {
2686 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2687 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2688 }
2689 }
2690
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 if( ssl->authmode != SSL_VERIFY_NONE )
2692 {
2693 if( ssl->ca_chain == NULL )
2694 {
2695 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002696 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 }
2698
Paul Bakkerddf26b42013-09-18 13:46:23 +02002699 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2700 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2701 &ssl->session_negotiate->verify_result,
2702 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
2704 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002705 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002707 }
2708#if defined(POLARSSL_SSL_SET_CURVES)
2709 else
2710 {
2711 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
2712
2713 /* If certificate uses an EC key, make sure the curve is OK */
2714 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2715 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2716 {
2717 SSL_DEBUG_MSG( 1, ( "bad server certificate (EC key curve)" ) );
2718 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2719 }
2720 }
2721#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
2723 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2724 ret = 0;
2725 }
2726
2727 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2728
2729 return( ret );
2730}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002731#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2732 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2733 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2734 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2735 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2736 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2737 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
2739int ssl_write_change_cipher_spec( ssl_context *ssl )
2740{
2741 int ret;
2742
2743 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2744
2745 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2746 ssl->out_msglen = 1;
2747 ssl->out_msg[0] = 1;
2748
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 ssl->state++;
2750
2751 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2752 {
2753 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2754 return( ret );
2755 }
2756
2757 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2758
2759 return( 0 );
2760}
2761
2762int ssl_parse_change_cipher_spec( ssl_context *ssl )
2763{
2764 int ret;
2765
2766 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2767
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2769 {
2770 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2771 return( ret );
2772 }
2773
2774 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2775 {
2776 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002777 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 }
2779
2780 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2781 {
2782 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002783 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 }
2785
2786 ssl->state++;
2787
2788 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2789
2790 return( 0 );
2791}
2792
Paul Bakker41c83d32013-03-20 14:39:14 +01002793void ssl_optimize_checksum( ssl_context *ssl,
2794 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002795{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002796 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002797
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002798#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2799 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002800 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002801 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002802 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002803#endif
2804#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2805#if defined(POLARSSL_SHA512_C)
2806 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2807 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2808 else
2809#endif
2810#if defined(POLARSSL_SHA256_C)
2811 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002812 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002813 else
2814#endif
2815#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2816 /* Should never happen */
2817 return;
Paul Bakker380da532012-04-18 16:10:25 +00002818}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002819
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002820static void ssl_update_checksum_start( ssl_context *ssl,
2821 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002822{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002823#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2824 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002825 md5_update( &ssl->handshake->fin_md5 , buf, len );
2826 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002827#endif
2828#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2829#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002830 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002831#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002832#if defined(POLARSSL_SHA512_C)
2833 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002834#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002835#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002836}
2837
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002838#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2839 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002840static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2841 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002842{
Paul Bakker48916f92012-09-16 19:57:18 +00002843 md5_update( &ssl->handshake->fin_md5 , buf, len );
2844 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002845}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002846#endif
Paul Bakker380da532012-04-18 16:10:25 +00002847
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002848#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2849#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002850static void ssl_update_checksum_sha256( ssl_context *ssl,
2851 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002852{
Paul Bakker9e36f042013-06-30 14:34:05 +02002853 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002854}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002855#endif
Paul Bakker380da532012-04-18 16:10:25 +00002856
Paul Bakker9e36f042013-06-30 14:34:05 +02002857#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002858static void ssl_update_checksum_sha384( ssl_context *ssl,
2859 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002860{
Paul Bakker9e36f042013-06-30 14:34:05 +02002861 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002862}
Paul Bakker769075d2012-11-24 11:26:46 +01002863#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002864#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002865
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002866#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002867static void ssl_calc_finished_ssl(
2868 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002869{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002870 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 md5_context md5;
2872 sha1_context sha1;
2873
Paul Bakker5121ce52009-01-03 21:22:43 +00002874 unsigned char padbuf[48];
2875 unsigned char md5sum[16];
2876 unsigned char sha1sum[20];
2877
Paul Bakker48916f92012-09-16 19:57:18 +00002878 ssl_session *session = ssl->session_negotiate;
2879 if( !session )
2880 session = ssl->session;
2881
Paul Bakker1ef83d62012-04-11 12:09:53 +00002882 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2883
Paul Bakker48916f92012-09-16 19:57:18 +00002884 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2885 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
2887 /*
2888 * SSLv3:
2889 * hash =
2890 * MD5( master + pad2 +
2891 * MD5( handshake + sender + master + pad1 ) )
2892 * + SHA1( master + pad2 +
2893 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 */
2895
Paul Bakker90995b52013-06-24 19:20:35 +02002896#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002899#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002900
Paul Bakker90995b52013-06-24 19:20:35 +02002901#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002902 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002903 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002904#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Paul Bakker3c2122f2013-06-24 19:03:14 +02002906 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2907 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
Paul Bakker3c2122f2013-06-24 19:03:14 +02002911 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002912 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002913 md5_update( &md5, padbuf, 48 );
2914 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakker3c2122f2013-06-24 19:03:14 +02002916 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002917 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918 sha1_update( &sha1, padbuf, 40 );
2919 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Paul Bakker1ef83d62012-04-11 12:09:53 +00002921 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002924 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925 md5_update( &md5, padbuf, 48 );
2926 md5_update( &md5, md5sum, 16 );
2927 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Paul Bakker1ef83d62012-04-11 12:09:53 +00002929 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002930 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002931 sha1_update( &sha1, padbuf , 40 );
2932 sha1_update( &sha1, sha1sum, 20 );
2933 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Paul Bakker1ef83d62012-04-11 12:09:53 +00002935 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Paul Bakker1ef83d62012-04-11 12:09:53 +00002937 memset( &md5, 0, sizeof( md5_context ) );
2938 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
2940 memset( padbuf, 0, sizeof( padbuf ) );
2941 memset( md5sum, 0, sizeof( md5sum ) );
2942 memset( sha1sum, 0, sizeof( sha1sum ) );
2943
2944 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2945}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002948#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949static void ssl_calc_finished_tls(
2950 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002951{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002953 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002955 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ssl_session *session = ssl->session_negotiate;
2959 if( !session )
2960 session = ssl->session;
2961
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Paul Bakker48916f92012-09-16 19:57:18 +00002964 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2965 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Paul Bakker1ef83d62012-04-11 12:09:53 +00002967 /*
2968 * TLSv1:
2969 * hash = PRF( master, finished_label,
2970 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2971 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Paul Bakker90995b52013-06-24 19:20:35 +02002973#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2975 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002976#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002977
Paul Bakker90995b52013-06-24 19:20:35 +02002978#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2980 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002981#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982
2983 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002984 ? "client finished"
2985 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986
2987 md5_finish( &md5, padbuf );
2988 sha1_finish( &sha1, padbuf + 16 );
2989
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002990 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002991 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002992
2993 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2994
2995 memset( &md5, 0, sizeof( md5_context ) );
2996 memset( &sha1, 0, sizeof( sha1_context ) );
2997
2998 memset( padbuf, 0, sizeof( padbuf ) );
2999
3000 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3001}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003002#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003003
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003004#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3005#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003006static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003007 ssl_context *ssl, unsigned char *buf, int from )
3008{
3009 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003010 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003011 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003012 unsigned char padbuf[32];
3013
Paul Bakker48916f92012-09-16 19:57:18 +00003014 ssl_session *session = ssl->session_negotiate;
3015 if( !session )
3016 session = ssl->session;
3017
Paul Bakker380da532012-04-18 16:10:25 +00003018 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003019
Paul Bakker9e36f042013-06-30 14:34:05 +02003020 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021
3022 /*
3023 * TLSv1.2:
3024 * hash = PRF( master, finished_label,
3025 * Hash( handshake ) )[0.11]
3026 */
3027
Paul Bakker9e36f042013-06-30 14:34:05 +02003028#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003029 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003030 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003031#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032
3033 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003034 ? "client finished"
3035 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003036
Paul Bakker9e36f042013-06-30 14:34:05 +02003037 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003038
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003039 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003040 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003041
3042 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3043
Paul Bakker9e36f042013-06-30 14:34:05 +02003044 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003045
3046 memset( padbuf, 0, sizeof( padbuf ) );
3047
3048 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3049}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003050#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003051
Paul Bakker9e36f042013-06-30 14:34:05 +02003052#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003053static void ssl_calc_finished_tls_sha384(
3054 ssl_context *ssl, unsigned char *buf, int from )
3055{
3056 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003057 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003058 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003059 unsigned char padbuf[48];
3060
Paul Bakker48916f92012-09-16 19:57:18 +00003061 ssl_session *session = ssl->session_negotiate;
3062 if( !session )
3063 session = ssl->session;
3064
Paul Bakker380da532012-04-18 16:10:25 +00003065 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003066
Paul Bakker9e36f042013-06-30 14:34:05 +02003067 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003068
3069 /*
3070 * TLSv1.2:
3071 * hash = PRF( master, finished_label,
3072 * Hash( handshake ) )[0.11]
3073 */
3074
Paul Bakker9e36f042013-06-30 14:34:05 +02003075#if !defined(POLARSSL_SHA512_ALT)
3076 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3077 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003078#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003079
3080 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003081 ? "client finished"
3082 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003083
Paul Bakker9e36f042013-06-30 14:34:05 +02003084 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003085
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003086 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003087 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003088
3089 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3090
Paul Bakker9e36f042013-06-30 14:34:05 +02003091 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003092
3093 memset( padbuf, 0, sizeof( padbuf ) );
3094
3095 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3096}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003097#endif /* POLARSSL_SHA512_C */
3098#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003099
Paul Bakker48916f92012-09-16 19:57:18 +00003100void ssl_handshake_wrapup( ssl_context *ssl )
3101{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003102 int resume = ssl->handshake->resume;
3103
Paul Bakker48916f92012-09-16 19:57:18 +00003104 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3105
3106 /*
3107 * Free our handshake params
3108 */
3109 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003110 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003111 ssl->handshake = NULL;
3112
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003113 if( ssl->renegotiation == SSL_RENEGOTIATION )
3114 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3115
Paul Bakker48916f92012-09-16 19:57:18 +00003116 /*
3117 * Switch in our now active transform context
3118 */
3119 if( ssl->transform )
3120 {
3121 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003122 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003123 }
3124 ssl->transform = ssl->transform_negotiate;
3125 ssl->transform_negotiate = NULL;
3126
Paul Bakker0a597072012-09-25 21:55:46 +00003127 if( ssl->session )
3128 {
3129 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003130 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003131 }
3132 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003133 ssl->session_negotiate = NULL;
3134
Paul Bakker0a597072012-09-25 21:55:46 +00003135 /*
3136 * Add cache entry
3137 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003138 if( ssl->f_set_cache != NULL &&
3139 ssl->session->length != 0 &&
3140 resume == 0 )
3141 {
Paul Bakker0a597072012-09-25 21:55:46 +00003142 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3143 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003144 }
Paul Bakker0a597072012-09-25 21:55:46 +00003145
Paul Bakker48916f92012-09-16 19:57:18 +00003146 ssl->state++;
3147
3148 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3149}
3150
Paul Bakker1ef83d62012-04-11 12:09:53 +00003151int ssl_write_finished( ssl_context *ssl )
3152{
3153 int ret, hash_len;
3154
3155 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3156
Paul Bakker92be97b2013-01-02 17:30:03 +01003157 /*
3158 * Set the out_msg pointer to the correct location based on IV length
3159 */
3160 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3161 {
3162 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3163 ssl->transform_negotiate->fixed_ivlen;
3164 }
3165 else
3166 ssl->out_msg = ssl->out_iv;
3167
Paul Bakker48916f92012-09-16 19:57:18 +00003168 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169
3170 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003171 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3172
Paul Bakker48916f92012-09-16 19:57:18 +00003173 ssl->verify_data_len = hash_len;
3174 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3175
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 ssl->out_msglen = 4 + hash_len;
3177 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3178 ssl->out_msg[0] = SSL_HS_FINISHED;
3179
3180 /*
3181 * In case of session resuming, invert the client and server
3182 * ChangeCipherSpec messages order.
3183 */
Paul Bakker0a597072012-09-25 21:55:46 +00003184 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003185 {
3186 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003187 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 else
3189 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3190 }
3191 else
3192 ssl->state++;
3193
Paul Bakker48916f92012-09-16 19:57:18 +00003194 /*
3195 * Switch to our negotiated transform and session parameters for outbound data.
3196 */
3197 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3198 ssl->transform_out = ssl->transform_negotiate;
3199 ssl->session_out = ssl->session_negotiate;
3200 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Paul Bakker07eb38b2012-12-19 14:42:06 +01003202#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3203 if( ssl_hw_record_activate != NULL)
3204 {
3205 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3206 {
3207 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3208 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3209 }
3210 }
3211#endif
3212
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3214 {
3215 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3216 return( ret );
3217 }
3218
3219 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3220
3221 return( 0 );
3222}
3223
3224int ssl_parse_finished( ssl_context *ssl )
3225{
Paul Bakker23986e52011-04-24 08:57:21 +00003226 int ret;
3227 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 unsigned char buf[36];
3229
3230 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3231
Paul Bakker48916f92012-09-16 19:57:18 +00003232 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Paul Bakker48916f92012-09-16 19:57:18 +00003234 /*
3235 * Switch to our negotiated transform and session parameters for inbound data.
3236 */
3237 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3238 ssl->transform_in = ssl->transform_negotiate;
3239 ssl->session_in = ssl->session_negotiate;
3240 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003241
Paul Bakker92be97b2013-01-02 17:30:03 +01003242 /*
3243 * Set the in_msg pointer to the correct location based on IV length
3244 */
3245 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3246 {
3247 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3248 ssl->transform_negotiate->fixed_ivlen;
3249 }
3250 else
3251 ssl->in_msg = ssl->in_iv;
3252
Paul Bakker07eb38b2012-12-19 14:42:06 +01003253#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3254 if( ssl_hw_record_activate != NULL)
3255 {
3256 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3257 {
3258 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3259 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3260 }
3261 }
3262#endif
3263
Paul Bakker5121ce52009-01-03 21:22:43 +00003264 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3265 {
3266 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3267 return( ret );
3268 }
3269
3270 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3271 {
3272 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003273 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 }
3275
Paul Bakker1ef83d62012-04-11 12:09:53 +00003276 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3278
3279 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3280 ssl->in_hslen != 4 + hash_len )
3281 {
3282 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003283 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 }
3285
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003286 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 {
3288 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003289 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 }
3291
Paul Bakker48916f92012-09-16 19:57:18 +00003292 ssl->verify_data_len = hash_len;
3293 memcpy( ssl->peer_verify_data, buf, hash_len );
3294
Paul Bakker0a597072012-09-25 21:55:46 +00003295 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 {
3297 if( ssl->endpoint == SSL_IS_CLIENT )
3298 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3299
3300 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003301 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 }
3303 else
3304 ssl->state++;
3305
3306 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3307
3308 return( 0 );
3309}
3310
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003311static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003312{
3313 if( ssl->transform_negotiate )
3314 ssl_transform_free( ssl->transform_negotiate );
3315 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003316 {
3317 ssl->transform_negotiate =
3318 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3319 }
Paul Bakker48916f92012-09-16 19:57:18 +00003320
3321 if( ssl->session_negotiate )
3322 ssl_session_free( ssl->session_negotiate );
3323 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003324 {
3325 ssl->session_negotiate =
3326 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3327 }
Paul Bakker48916f92012-09-16 19:57:18 +00003328
3329 if( ssl->handshake )
3330 ssl_handshake_free( ssl->handshake );
3331 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003332 {
3333 ssl->handshake = (ssl_handshake_params *)
3334 polarssl_malloc( sizeof(ssl_handshake_params) );
3335 }
Paul Bakker48916f92012-09-16 19:57:18 +00003336
3337 if( ssl->handshake == NULL ||
3338 ssl->transform_negotiate == NULL ||
3339 ssl->session_negotiate == NULL )
3340 {
3341 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3342 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3343 }
3344
3345 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3346 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3347 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3348
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003349#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3350 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003351 md5_starts( &ssl->handshake->fin_md5 );
3352 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003353#endif
3354#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3355#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003356 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003357#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003358#if defined(POLARSSL_SHA512_C)
3359 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003360#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003361#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003362
3363 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003364 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003365
Paul Bakker61d113b2013-07-04 11:51:43 +02003366#if defined(POLARSSL_ECDH_C)
3367 ecdh_init( &ssl->handshake->ecdh_ctx );
3368#endif
3369
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003370#if defined(POLARSSL_X509_CRT_PARSE_C)
3371 ssl->handshake->key_cert = ssl->key_cert;
3372#endif
3373
Paul Bakker48916f92012-09-16 19:57:18 +00003374 return( 0 );
3375}
3376
Paul Bakker5121ce52009-01-03 21:22:43 +00003377/*
3378 * Initialize an SSL context
3379 */
3380int ssl_init( ssl_context *ssl )
3381{
Paul Bakker48916f92012-09-16 19:57:18 +00003382 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 int len = SSL_BUFFER_LEN;
3384
3385 memset( ssl, 0, sizeof( ssl_context ) );
3386
Paul Bakker62f2dee2012-09-28 07:31:51 +00003387 /*
3388 * Sane defaults
3389 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003390 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3391 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3392 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3393 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003394
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003395 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003396
Paul Bakker62f2dee2012-09-28 07:31:51 +00003397#if defined(POLARSSL_DHM_C)
3398 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3399 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3400 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3401 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3402 {
3403 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3404 return( ret );
3405 }
3406#endif
3407
3408 /*
3409 * Prepare base structures
3410 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003411 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003413 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003414 ssl->in_msg = ssl->in_ctr + 13;
3415
3416 if( ssl->in_ctr == NULL )
3417 {
3418 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003419 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 }
3421
Paul Bakker6e339b52013-07-03 13:37:05 +02003422 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003423 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003424 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003425 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003426
3427 if( ssl->out_ctr == NULL )
3428 {
3429 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003430 polarssl_free( ssl->in_ctr );
3431 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003432 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003433 }
3434
3435 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3436 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3437
Paul Bakker606b4ba2013-08-14 16:52:14 +02003438#if defined(POLARSSL_SSL_SESSION_TICKETS)
3439 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3440#endif
3441
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003442#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003443 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003444#endif
3445
Paul Bakker48916f92012-09-16 19:57:18 +00003446 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3447 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003448
3449 return( 0 );
3450}
3451
3452/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003453 * Reset an initialized and used SSL context for re-use while retaining
3454 * all application-set variables, function pointers and data.
3455 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003456int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003457{
Paul Bakker48916f92012-09-16 19:57:18 +00003458 int ret;
3459
Paul Bakker7eb013f2011-10-06 12:37:39 +00003460 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003461 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3462 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3463
3464 ssl->verify_data_len = 0;
3465 memset( ssl->own_verify_data, 0, 36 );
3466 memset( ssl->peer_verify_data, 0, 36 );
3467
Paul Bakker7eb013f2011-10-06 12:37:39 +00003468 ssl->in_offt = NULL;
3469
Paul Bakker92be97b2013-01-02 17:30:03 +01003470 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003471 ssl->in_msgtype = 0;
3472 ssl->in_msglen = 0;
3473 ssl->in_left = 0;
3474
3475 ssl->in_hslen = 0;
3476 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003477 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003478
Paul Bakker92be97b2013-01-02 17:30:03 +01003479 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003480 ssl->out_msgtype = 0;
3481 ssl->out_msglen = 0;
3482 ssl->out_left = 0;
3483
Paul Bakker48916f92012-09-16 19:57:18 +00003484 ssl->transform_in = NULL;
3485 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003486
3487 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3488 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003489
3490#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3491 if( ssl_hw_record_reset != NULL)
3492 {
3493 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003494 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003495 {
3496 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3497 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3498 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003499 }
3500#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003501
Paul Bakker48916f92012-09-16 19:57:18 +00003502 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003503 {
Paul Bakker48916f92012-09-16 19:57:18 +00003504 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003505 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003506 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003507 }
Paul Bakker48916f92012-09-16 19:57:18 +00003508
Paul Bakkerc0463502013-02-14 11:19:38 +01003509 if( ssl->session )
3510 {
3511 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003512 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003513 ssl->session = NULL;
3514 }
3515
Paul Bakker48916f92012-09-16 19:57:18 +00003516 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3517 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003518
3519 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003520}
3521
Paul Bakkera503a632013-08-14 13:48:06 +02003522#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003523/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003524 * Allocate and initialize ticket keys
3525 */
3526static int ssl_ticket_keys_init( ssl_context *ssl )
3527{
3528 int ret;
3529 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003530 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003531
3532 if( ssl->ticket_keys != NULL )
3533 return( 0 );
3534
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003535 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3536 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003537 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3538
3539 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003540 {
3541 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003542 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003543 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003544
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003545 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3546 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3547 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3548 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003549 polarssl_free( tkeys );
3550 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003551 }
3552
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003553 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003554 {
3555 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003556 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003557 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003558
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003559 ssl->ticket_keys = tkeys;
3560
3561 return( 0 );
3562}
Paul Bakkera503a632013-08-14 13:48:06 +02003563#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003564
3565/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003566 * SSL set accessors
3567 */
3568void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3569{
3570 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003571
Paul Bakker606b4ba2013-08-14 16:52:14 +02003572#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003573 if( endpoint == SSL_IS_CLIENT )
3574 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003575#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003576}
3577
3578void ssl_set_authmode( ssl_context *ssl, int authmode )
3579{
3580 ssl->authmode = authmode;
3581}
3582
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003583#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003584void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003585 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003586 void *p_vrfy )
3587{
3588 ssl->f_vrfy = f_vrfy;
3589 ssl->p_vrfy = p_vrfy;
3590}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003591#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003592
Paul Bakker5121ce52009-01-03 21:22:43 +00003593void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003594 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 void *p_rng )
3596{
3597 ssl->f_rng = f_rng;
3598 ssl->p_rng = p_rng;
3599}
3600
3601void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003602 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003603 void *p_dbg )
3604{
3605 ssl->f_dbg = f_dbg;
3606 ssl->p_dbg = p_dbg;
3607}
3608
3609void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003610 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003611 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003612{
3613 ssl->f_recv = f_recv;
3614 ssl->f_send = f_send;
3615 ssl->p_recv = p_recv;
3616 ssl->p_send = p_send;
3617}
3618
Paul Bakker0a597072012-09-25 21:55:46 +00003619void ssl_set_session_cache( ssl_context *ssl,
3620 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3621 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003622{
Paul Bakker0a597072012-09-25 21:55:46 +00003623 ssl->f_get_cache = f_get_cache;
3624 ssl->p_get_cache = p_get_cache;
3625 ssl->f_set_cache = f_set_cache;
3626 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003627}
3628
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003629int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003630{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003631 int ret;
3632
3633 if( ssl == NULL ||
3634 session == NULL ||
3635 ssl->session_negotiate == NULL ||
3636 ssl->endpoint != SSL_IS_CLIENT )
3637 {
3638 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3639 }
3640
3641 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3642 return( ret );
3643
Paul Bakker0a597072012-09-25 21:55:46 +00003644 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003645
3646 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003647}
3648
Paul Bakkerb68cad62012-08-23 08:34:18 +00003649void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003650{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003651 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3652 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3653 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3654 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3655}
3656
3657void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3658 int major, int minor )
3659{
3660 if( major != SSL_MAJOR_VERSION_3 )
3661 return;
3662
3663 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3664 return;
3665
3666 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003667}
3668
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003669#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003670/* Add a new (empty) key_cert entry an return a pointer to it */
3671static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3672{
3673 ssl_key_cert *key_cert, *last;
3674
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003675 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3676 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003677 return( NULL );
3678
3679 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3680
3681 /* Append the new key_cert to the (possibly empty) current list */
3682 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003683 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003684 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003685 if( ssl->handshake != NULL )
3686 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003687 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003688 else
3689 {
3690 last = ssl->key_cert;
3691 while( last->next != NULL )
3692 last = last->next;
3693 last->next = key_cert;
3694 }
3695
3696 return key_cert;
3697}
3698
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003699void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003700 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003701{
3702 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003703 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003704 ssl->peer_cn = peer_cn;
3705}
3706
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003707int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003708 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003709{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003710 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3711
3712 if( key_cert == NULL )
3713 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3714
3715 key_cert->cert = own_cert;
3716 key_cert->key = pk_key;
3717
3718 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003719}
3720
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003721#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003722int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003723 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003724{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003725 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003726 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003727
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003728 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003729 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3730
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003731 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3732 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003733 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003734
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003735 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003736
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003737 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003738 if( ret != 0 )
3739 return( ret );
3740
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003741 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003742 return( ret );
3743
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003744 key_cert->cert = own_cert;
3745 key_cert->key_own_alloc = 1;
3746
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003747 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003748}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003749#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003750
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003751int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003752 void *rsa_key,
3753 rsa_decrypt_func rsa_decrypt,
3754 rsa_sign_func rsa_sign,
3755 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003756{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003757 int ret;
3758 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003759
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003760 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003761 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3762
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003763 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3764 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003765 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003766
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003767 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003768
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003769 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3770 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3771 return( ret );
3772
3773 key_cert->cert = own_cert;
3774 key_cert->key_own_alloc = 1;
3775
3776 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003777}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003778#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003779
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003780#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003781int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3782 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003783{
Paul Bakker6db455e2013-09-18 17:29:31 +02003784 if( psk == NULL || psk_identity == NULL )
3785 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3786
3787 if( ssl->psk != NULL )
3788 {
3789 polarssl_free( ssl->psk );
3790 polarssl_free( ssl->psk_identity );
3791 }
3792
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003793 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003794 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003795
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003796 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3797 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003798
3799 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3800 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3801
3802 memcpy( ssl->psk, psk, ssl->psk_len );
3803 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003804
3805 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003806}
3807
3808void ssl_set_psk_cb( ssl_context *ssl,
3809 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3810 size_t),
3811 void *p_psk )
3812{
3813 ssl->f_psk = f_psk;
3814 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003815}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003816#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003817
Paul Bakker48916f92012-09-16 19:57:18 +00003818#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003819int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003820{
3821 int ret;
3822
Paul Bakker48916f92012-09-16 19:57:18 +00003823 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003824 {
3825 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3826 return( ret );
3827 }
3828
Paul Bakker48916f92012-09-16 19:57:18 +00003829 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003830 {
3831 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3832 return( ret );
3833 }
3834
3835 return( 0 );
3836}
3837
Paul Bakker1b57b062011-01-06 15:48:19 +00003838int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3839{
3840 int ret;
3841
Paul Bakker48916f92012-09-16 19:57:18 +00003842 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003843 {
3844 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3845 return( ret );
3846 }
3847
Paul Bakker48916f92012-09-16 19:57:18 +00003848 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003849 {
3850 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3851 return( ret );
3852 }
3853
3854 return( 0 );
3855}
Paul Bakker48916f92012-09-16 19:57:18 +00003856#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003857
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003858#if defined(POLARSSL_SSL_SET_CURVES)
3859/*
3860 * Set the allowed elliptic curves
3861 */
3862void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3863{
3864 ssl->curve_list = curve_list;
3865}
3866#endif
3867
Paul Bakker0be444a2013-08-27 21:55:01 +02003868#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003869int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003870{
3871 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003872 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003873
3874 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003875
3876 if( ssl->hostname_len + 1 == 0 )
3877 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3878
Paul Bakker6e339b52013-07-03 13:37:05 +02003879 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003880
Paul Bakkerb15b8512012-01-13 13:44:06 +00003881 if( ssl->hostname == NULL )
3882 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3883
Paul Bakker3c2122f2013-06-24 19:03:14 +02003884 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003885 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003886
Paul Bakker40ea7de2009-05-03 10:18:48 +00003887 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003888
3889 return( 0 );
3890}
3891
Paul Bakker5701cdc2012-09-27 21:49:42 +00003892void ssl_set_sni( ssl_context *ssl,
3893 int (*f_sni)(void *, ssl_context *,
3894 const unsigned char *, size_t),
3895 void *p_sni )
3896{
3897 ssl->f_sni = f_sni;
3898 ssl->p_sni = p_sni;
3899}
Paul Bakker0be444a2013-08-27 21:55:01 +02003900#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003901
Paul Bakker490ecc82011-10-06 13:04:09 +00003902void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3903{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003904 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3905 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3906 {
3907 ssl->max_major_ver = major;
3908 ssl->max_minor_ver = minor;
3909 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003910}
3911
Paul Bakker1d29fb52012-09-28 13:28:45 +00003912void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3913{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003914 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3915 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3916 {
3917 ssl->min_major_ver = major;
3918 ssl->min_minor_ver = minor;
3919 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003920}
3921
Paul Bakker05decb22013-08-15 13:33:48 +02003922#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003923int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3924{
Paul Bakker77e257e2013-12-16 15:29:52 +01003925 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003926 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003927 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003928 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003929 }
3930
3931 ssl->mfl_code = mfl_code;
3932
3933 return( 0 );
3934}
Paul Bakker05decb22013-08-15 13:33:48 +02003935#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003936
Paul Bakker1f2bc622013-08-15 13:45:55 +02003937#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003938int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003939{
3940 if( ssl->endpoint != SSL_IS_CLIENT )
3941 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3942
Paul Bakker8c1ede62013-07-19 14:14:37 +02003943 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003944
3945 return( 0 );
3946}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003947#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003948
Paul Bakker48916f92012-09-16 19:57:18 +00003949void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3950{
3951 ssl->disable_renegotiation = renegotiation;
3952}
3953
3954void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3955{
3956 ssl->allow_legacy_renegotiation = allow_legacy;
3957}
3958
Paul Bakkera503a632013-08-14 13:48:06 +02003959#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003960int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3961{
3962 ssl->session_tickets = use_tickets;
3963
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003964 if( ssl->endpoint == SSL_IS_CLIENT )
3965 return( 0 );
3966
3967 if( ssl->f_rng == NULL )
3968 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3969
3970 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003971}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003972
3973void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3974{
3975 ssl->ticket_lifetime = lifetime;
3976}
Paul Bakkera503a632013-08-14 13:48:06 +02003977#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003978
Paul Bakker5121ce52009-01-03 21:22:43 +00003979/*
3980 * SSL get accessors
3981 */
Paul Bakker23986e52011-04-24 08:57:21 +00003982size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003983{
3984 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3985}
3986
Paul Bakkerff60ee62010-03-16 21:09:09 +00003987int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003988{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003989 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003990}
3991
Paul Bakkere3166ce2011-01-27 17:40:50 +00003992const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003993{
Paul Bakker926c8e42013-03-06 10:23:34 +01003994 if( ssl == NULL || ssl->session == NULL )
3995 return NULL;
3996
Paul Bakkere3166ce2011-01-27 17:40:50 +00003997 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003998}
3999
Paul Bakker43ca69c2011-01-15 17:35:19 +00004000const char *ssl_get_version( const ssl_context *ssl )
4001{
4002 switch( ssl->minor_ver )
4003 {
4004 case SSL_MINOR_VERSION_0:
4005 return( "SSLv3.0" );
4006
4007 case SSL_MINOR_VERSION_1:
4008 return( "TLSv1.0" );
4009
4010 case SSL_MINOR_VERSION_2:
4011 return( "TLSv1.1" );
4012
Paul Bakker1ef83d62012-04-11 12:09:53 +00004013 case SSL_MINOR_VERSION_3:
4014 return( "TLSv1.2" );
4015
Paul Bakker43ca69c2011-01-15 17:35:19 +00004016 default:
4017 break;
4018 }
4019 return( "unknown" );
4020}
4021
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004022#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004023const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004024{
4025 if( ssl == NULL || ssl->session == NULL )
4026 return NULL;
4027
4028 return ssl->session->peer_cert;
4029}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004030#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004031
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004032int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4033{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004034 if( ssl == NULL ||
4035 dst == NULL ||
4036 ssl->session == NULL ||
4037 ssl->endpoint != SSL_IS_CLIENT )
4038 {
4039 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4040 }
4041
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004042 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004043}
4044
Paul Bakker5121ce52009-01-03 21:22:43 +00004045/*
Paul Bakker1961b702013-01-25 14:49:24 +01004046 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004047 */
Paul Bakker1961b702013-01-25 14:49:24 +01004048int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004049{
Paul Bakker40e46942009-01-03 21:51:57 +00004050 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004051
Paul Bakker40e46942009-01-03 21:51:57 +00004052#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004054 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004055#endif
4056
Paul Bakker40e46942009-01-03 21:51:57 +00004057#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004058 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004059 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004060#endif
4061
Paul Bakker1961b702013-01-25 14:49:24 +01004062 return( ret );
4063}
4064
4065/*
4066 * Perform the SSL handshake
4067 */
4068int ssl_handshake( ssl_context *ssl )
4069{
4070 int ret = 0;
4071
4072 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4073
4074 while( ssl->state != SSL_HANDSHAKE_OVER )
4075 {
4076 ret = ssl_handshake_step( ssl );
4077
4078 if( ret != 0 )
4079 break;
4080 }
4081
Paul Bakker5121ce52009-01-03 21:22:43 +00004082 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4083
4084 return( ret );
4085}
4086
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004087#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004088/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004089 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004090 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004091static int ssl_write_hello_request( ssl_context *ssl )
4092{
4093 int ret;
4094
4095 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4096
4097 ssl->out_msglen = 4;
4098 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4099 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4100
4101 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4102 {
4103 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4104 return( ret );
4105 }
4106
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004107 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4108
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004109 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4110
4111 return( 0 );
4112}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004113#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004114
4115/*
4116 * Actually renegotiate current connection, triggered by either:
4117 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004118 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004119 * - receiving any handshake message on server during ssl_read() after the
4120 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004121 * If the handshake doesn't complete due to waiting for I/O, it will continue
4122 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004123 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004124static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004125{
4126 int ret;
4127
4128 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4129
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004130 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4131 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004132
4133 ssl->state = SSL_HELLO_REQUEST;
4134 ssl->renegotiation = SSL_RENEGOTIATION;
4135
Paul Bakker48916f92012-09-16 19:57:18 +00004136 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4137 {
4138 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4139 return( ret );
4140 }
4141
4142 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4143
4144 return( 0 );
4145}
4146
4147/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004148 * Renegotiate current connection on client,
4149 * or request renegotiation on server
4150 */
4151int ssl_renegotiate( ssl_context *ssl )
4152{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004153 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004154
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004155#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004156 /* On server, just send the request */
4157 if( ssl->endpoint == SSL_IS_SERVER )
4158 {
4159 if( ssl->state != SSL_HANDSHAKE_OVER )
4160 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4161
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004162 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004163 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004164#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004165
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004166#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004167 /*
4168 * On client, either start the renegotiation process or,
4169 * if already in progress, continue the handshake
4170 */
4171 if( ssl->renegotiation != SSL_RENEGOTIATION )
4172 {
4173 if( ssl->state != SSL_HANDSHAKE_OVER )
4174 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4175
4176 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4177 {
4178 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4179 return( ret );
4180 }
4181 }
4182 else
4183 {
4184 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4185 {
4186 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4187 return( ret );
4188 }
4189 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004190#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004191
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004192 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004193}
4194
4195/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004196 * Receive application data decrypted from the SSL layer
4197 */
Paul Bakker23986e52011-04-24 08:57:21 +00004198int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004199{
Paul Bakker23986e52011-04-24 08:57:21 +00004200 int ret;
4201 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004202
4203 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4204
4205 if( ssl->state != SSL_HANDSHAKE_OVER )
4206 {
4207 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4208 {
4209 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4210 return( ret );
4211 }
4212 }
4213
4214 if( ssl->in_offt == NULL )
4215 {
4216 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4217 {
Paul Bakker831a7552011-05-18 13:32:51 +00004218 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4219 return( 0 );
4220
Paul Bakker5121ce52009-01-03 21:22:43 +00004221 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4222 return( ret );
4223 }
4224
4225 if( ssl->in_msglen == 0 &&
4226 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4227 {
4228 /*
4229 * OpenSSL sends empty messages to randomize the IV
4230 */
4231 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4232 {
Paul Bakker831a7552011-05-18 13:32:51 +00004233 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4234 return( 0 );
4235
Paul Bakker5121ce52009-01-03 21:22:43 +00004236 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4237 return( ret );
4238 }
4239 }
4240
Paul Bakker48916f92012-09-16 19:57:18 +00004241 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4242 {
4243 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4244
4245 if( ssl->endpoint == SSL_IS_CLIENT &&
4246 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4247 ssl->in_hslen != 4 ) )
4248 {
4249 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4250 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4251 }
4252
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004253 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4254 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4255 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004256 {
4257 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4258
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004259#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004260 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004261 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004262 /*
4263 * SSLv3 does not have a "no_renegotiation" alert
4264 */
4265 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4266 return( ret );
4267 }
4268 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004269#endif
4270#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4271 defined(POLARSSL_SSL_PROTO_TLS1_2)
4272 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004273 {
4274 if( ( ret = ssl_send_alert_message( ssl,
4275 SSL_ALERT_LEVEL_WARNING,
4276 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4277 {
4278 return( ret );
4279 }
Paul Bakker48916f92012-09-16 19:57:18 +00004280 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004281 else
4282#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004283 {
4284 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004285 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004286 }
Paul Bakker48916f92012-09-16 19:57:18 +00004287 }
4288 else
4289 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004290 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004291 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004292 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004293 return( ret );
4294 }
4295
4296 return( POLARSSL_ERR_NET_WANT_READ );
4297 }
4298 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004299 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4300 {
4301 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4302 "but not honored by client" ) );
4303 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4304 }
Paul Bakker48916f92012-09-16 19:57:18 +00004305 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004306 {
4307 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004308 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004309 }
4310
4311 ssl->in_offt = ssl->in_msg;
4312 }
4313
4314 n = ( len < ssl->in_msglen )
4315 ? len : ssl->in_msglen;
4316
4317 memcpy( buf, ssl->in_offt, n );
4318 ssl->in_msglen -= n;
4319
4320 if( ssl->in_msglen == 0 )
4321 /* all bytes consumed */
4322 ssl->in_offt = NULL;
4323 else
4324 /* more data available */
4325 ssl->in_offt += n;
4326
4327 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4328
Paul Bakker23986e52011-04-24 08:57:21 +00004329 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004330}
4331
4332/*
4333 * Send application data to be encrypted by the SSL layer
4334 */
Paul Bakker23986e52011-04-24 08:57:21 +00004335int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004336{
Paul Bakker23986e52011-04-24 08:57:21 +00004337 int ret;
4338 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004339 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004340
4341 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4342
4343 if( ssl->state != SSL_HANDSHAKE_OVER )
4344 {
4345 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4346 {
4347 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4348 return( ret );
4349 }
4350 }
4351
Paul Bakker05decb22013-08-15 13:33:48 +02004352#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004353 /*
4354 * Assume mfl_code is correct since it was checked when set
4355 */
4356 max_len = mfl_code_to_length[ssl->mfl_code];
4357
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004358 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004359 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004360 */
4361 if( ssl->session_out != NULL &&
4362 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4363 {
4364 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4365 }
Paul Bakker05decb22013-08-15 13:33:48 +02004366#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004367
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004368 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004369
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 if( ssl->out_left != 0 )
4371 {
4372 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4373 {
4374 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4375 return( ret );
4376 }
4377 }
Paul Bakker887bd502011-06-08 13:10:54 +00004378 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004379 {
Paul Bakker887bd502011-06-08 13:10:54 +00004380 ssl->out_msglen = n;
4381 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4382 memcpy( ssl->out_msg, buf, n );
4383
4384 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4385 {
4386 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4387 return( ret );
4388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004389 }
4390
4391 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4392
Paul Bakker23986e52011-04-24 08:57:21 +00004393 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004394}
4395
4396/*
4397 * Notify the peer that the connection is being closed
4398 */
4399int ssl_close_notify( ssl_context *ssl )
4400{
4401 int ret;
4402
4403 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4404
4405 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4406 {
4407 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4408 return( ret );
4409 }
4410
4411 if( ssl->state == SSL_HANDSHAKE_OVER )
4412 {
Paul Bakker48916f92012-09-16 19:57:18 +00004413 if( ( ret = ssl_send_alert_message( ssl,
4414 SSL_ALERT_LEVEL_WARNING,
4415 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004416 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004417 return( ret );
4418 }
4419 }
4420
4421 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4422
4423 return( ret );
4424}
4425
Paul Bakker48916f92012-09-16 19:57:18 +00004426void ssl_transform_free( ssl_transform *transform )
4427{
4428#if defined(POLARSSL_ZLIB_SUPPORT)
4429 deflateEnd( &transform->ctx_deflate );
4430 inflateEnd( &transform->ctx_inflate );
4431#endif
4432
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004433 cipher_free_ctx( &transform->cipher_ctx_enc );
4434 cipher_free_ctx( &transform->cipher_ctx_dec );
4435
Paul Bakker61d113b2013-07-04 11:51:43 +02004436 md_free_ctx( &transform->md_ctx_enc );
4437 md_free_ctx( &transform->md_ctx_dec );
4438
Paul Bakker48916f92012-09-16 19:57:18 +00004439 memset( transform, 0, sizeof( ssl_transform ) );
4440}
4441
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004442#if defined(POLARSSL_X509_CRT_PARSE_C)
4443static void ssl_key_cert_free( ssl_key_cert *key_cert )
4444{
4445 ssl_key_cert *cur = key_cert, *next;
4446
4447 while( cur != NULL )
4448 {
4449 next = cur->next;
4450
4451 if( cur->key_own_alloc )
4452 {
4453 pk_free( cur->key );
4454 polarssl_free( cur->key );
4455 }
4456 polarssl_free( cur );
4457
4458 cur = next;
4459 }
4460}
4461#endif /* POLARSSL_X509_CRT_PARSE_C */
4462
Paul Bakker48916f92012-09-16 19:57:18 +00004463void ssl_handshake_free( ssl_handshake_params *handshake )
4464{
4465#if defined(POLARSSL_DHM_C)
4466 dhm_free( &handshake->dhm_ctx );
4467#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004468#if defined(POLARSSL_ECDH_C)
4469 ecdh_free( &handshake->ecdh_ctx );
4470#endif
4471
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004472#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004473 /* explicit void pointer cast for buggy MS compiler */
4474 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004475#endif
4476
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004477#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4478 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4479 /*
4480 * Free only the linked list wrapper, not the keys themselves
4481 * since the belong to the SNI callback
4482 */
4483 if( handshake->sni_key_cert != NULL )
4484 {
4485 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4486
4487 while( cur != NULL )
4488 {
4489 next = cur->next;
4490 polarssl_free( cur );
4491 cur = next;
4492 }
4493 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004494#endif
4495
Paul Bakker48916f92012-09-16 19:57:18 +00004496 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4497}
4498
4499void ssl_session_free( ssl_session *session )
4500{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004501#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004502 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004503 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004504 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004505 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004506 }
Paul Bakkered27a042013-04-18 22:46:23 +02004507#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004508
Paul Bakkera503a632013-08-14 13:48:06 +02004509#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004510 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004511#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004512
Paul Bakker0a597072012-09-25 21:55:46 +00004513 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004514}
4515
Paul Bakker5121ce52009-01-03 21:22:43 +00004516/*
4517 * Free an SSL context
4518 */
4519void ssl_free( ssl_context *ssl )
4520{
4521 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4522
Paul Bakker5121ce52009-01-03 21:22:43 +00004523 if( ssl->out_ctr != NULL )
4524 {
4525 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004526 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004527 }
4528
4529 if( ssl->in_ctr != NULL )
4530 {
4531 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004532 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004533 }
4534
Paul Bakker16770332013-10-11 09:59:44 +02004535#if defined(POLARSSL_ZLIB_SUPPORT)
4536 if( ssl->compress_buf != NULL )
4537 {
4538 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4539 polarssl_free( ssl->compress_buf );
4540 }
4541#endif
4542
Paul Bakker40e46942009-01-03 21:51:57 +00004543#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004544 mpi_free( &ssl->dhm_P );
4545 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004546#endif
4547
Paul Bakker48916f92012-09-16 19:57:18 +00004548 if( ssl->transform )
4549 {
4550 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004551 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004552 }
4553
4554 if( ssl->handshake )
4555 {
4556 ssl_handshake_free( ssl->handshake );
4557 ssl_transform_free( ssl->transform_negotiate );
4558 ssl_session_free( ssl->session_negotiate );
4559
Paul Bakker6e339b52013-07-03 13:37:05 +02004560 polarssl_free( ssl->handshake );
4561 polarssl_free( ssl->transform_negotiate );
4562 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004563 }
4564
Paul Bakkerc0463502013-02-14 11:19:38 +01004565 if( ssl->session )
4566 {
4567 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004568 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004569 }
4570
Paul Bakkera503a632013-08-14 13:48:06 +02004571#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004572 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004573#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004574
Paul Bakker0be444a2013-08-27 21:55:01 +02004575#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004576 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004577 {
4578 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004579 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004580 ssl->hostname_len = 0;
4581 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004582#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004583
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004584#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004585 if( ssl->psk != NULL )
4586 {
4587 memset( ssl->psk, 0, ssl->psk_len );
4588 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4589 polarssl_free( ssl->psk );
4590 polarssl_free( ssl->psk_identity );
4591 ssl->psk_len = 0;
4592 ssl->psk_identity_len = 0;
4593 }
4594#endif
4595
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004596#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004597 ssl_key_cert_free( ssl->key_cert );
4598#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004599
Paul Bakker05ef8352012-05-08 09:17:57 +00004600#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4601 if( ssl_hw_record_finish != NULL )
4602 {
4603 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4604 ssl_hw_record_finish( ssl );
4605 }
4606#endif
4607
Paul Bakker5121ce52009-01-03 21:22:43 +00004608 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004609
Paul Bakker86f04f42013-02-14 11:20:09 +01004610 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004611 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004612}
4613
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004614#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004615/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004616 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004617 */
4618unsigned char ssl_sig_from_pk( pk_context *pk )
4619{
4620#if defined(POLARSSL_RSA_C)
4621 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4622 return( SSL_SIG_RSA );
4623#endif
4624#if defined(POLARSSL_ECDSA_C)
4625 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4626 return( SSL_SIG_ECDSA );
4627#endif
4628 return( SSL_SIG_ANON );
4629}
4630
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004631pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4632{
4633 switch( sig )
4634 {
4635#if defined(POLARSSL_RSA_C)
4636 case SSL_SIG_RSA:
4637 return( POLARSSL_PK_RSA );
4638#endif
4639#if defined(POLARSSL_ECDSA_C)
4640 case SSL_SIG_ECDSA:
4641 return( POLARSSL_PK_ECDSA );
4642#endif
4643 default:
4644 return( POLARSSL_PK_NONE );
4645 }
4646}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004647#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004648
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004649/*
4650 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4651 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004652md_type_t ssl_md_alg_from_hash( unsigned char hash )
4653{
4654 switch( hash )
4655 {
4656#if defined(POLARSSL_MD5_C)
4657 case SSL_HASH_MD5:
4658 return( POLARSSL_MD_MD5 );
4659#endif
4660#if defined(POLARSSL_SHA1_C)
4661 case SSL_HASH_SHA1:
4662 return( POLARSSL_MD_SHA1 );
4663#endif
4664#if defined(POLARSSL_SHA256_C)
4665 case SSL_HASH_SHA224:
4666 return( POLARSSL_MD_SHA224 );
4667 case SSL_HASH_SHA256:
4668 return( POLARSSL_MD_SHA256 );
4669#endif
4670#if defined(POLARSSL_SHA512_C)
4671 case SSL_HASH_SHA384:
4672 return( POLARSSL_MD_SHA384 );
4673 case SSL_HASH_SHA512:
4674 return( POLARSSL_MD_SHA512 );
4675#endif
4676 default:
4677 return( POLARSSL_MD_NONE );
4678 }
4679}
4680
Paul Bakker5121ce52009-01-03 21:22:43 +00004681#endif
Gergely Budai987bfb52014-01-19 21:48:42 +01004682
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004683#if defined(POLARSSL_SSL_SET_CURVES)
4684/*
4685 * Check is a curve proposed by the peer is in our list.
4686 * Return 1 if we're willing to use it, 0 otherwise.
4687 */
4688int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4689{
4690 const ecp_group_id *gid;
4691
4692 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4693 if( *gid == grp_id )
4694 return( 1 );
4695
4696 return( 0 );
4697}
4698#endif