blob: 20cb9bdc75702d09f55ed47bab65ef3216372a17 [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 Bakker6e339b52013-07-03 13:37:05 +02003430 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003431 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 }
3433
3434 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3435 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3436
Paul Bakker606b4ba2013-08-14 16:52:14 +02003437#if defined(POLARSSL_SSL_SESSION_TICKETS)
3438 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3439#endif
3440
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003441#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003442 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003443#endif
3444
Paul Bakker48916f92012-09-16 19:57:18 +00003445 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3446 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003447
3448 return( 0 );
3449}
3450
3451/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003452 * Reset an initialized and used SSL context for re-use while retaining
3453 * all application-set variables, function pointers and data.
3454 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003455int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003456{
Paul Bakker48916f92012-09-16 19:57:18 +00003457 int ret;
3458
Paul Bakker7eb013f2011-10-06 12:37:39 +00003459 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003460 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3461 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3462
3463 ssl->verify_data_len = 0;
3464 memset( ssl->own_verify_data, 0, 36 );
3465 memset( ssl->peer_verify_data, 0, 36 );
3466
Paul Bakker7eb013f2011-10-06 12:37:39 +00003467 ssl->in_offt = NULL;
3468
Paul Bakker92be97b2013-01-02 17:30:03 +01003469 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003470 ssl->in_msgtype = 0;
3471 ssl->in_msglen = 0;
3472 ssl->in_left = 0;
3473
3474 ssl->in_hslen = 0;
3475 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003476 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003477
Paul Bakker92be97b2013-01-02 17:30:03 +01003478 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003479 ssl->out_msgtype = 0;
3480 ssl->out_msglen = 0;
3481 ssl->out_left = 0;
3482
Paul Bakker48916f92012-09-16 19:57:18 +00003483 ssl->transform_in = NULL;
3484 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003485
3486 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3487 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003488
3489#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3490 if( ssl_hw_record_reset != NULL)
3491 {
3492 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003493 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003494 {
3495 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3496 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3497 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003498 }
3499#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003500
Paul Bakker48916f92012-09-16 19:57:18 +00003501 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003502 {
Paul Bakker48916f92012-09-16 19:57:18 +00003503 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003504 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003505 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003506 }
Paul Bakker48916f92012-09-16 19:57:18 +00003507
Paul Bakkerc0463502013-02-14 11:19:38 +01003508 if( ssl->session )
3509 {
3510 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003511 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003512 ssl->session = NULL;
3513 }
3514
Paul Bakker48916f92012-09-16 19:57:18 +00003515 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3516 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003517
3518 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003519}
3520
Paul Bakkera503a632013-08-14 13:48:06 +02003521#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003522/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003523 * Allocate and initialize ticket keys
3524 */
3525static int ssl_ticket_keys_init( ssl_context *ssl )
3526{
3527 int ret;
3528 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003529 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003530
3531 if( ssl->ticket_keys != NULL )
3532 return( 0 );
3533
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003534 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3535 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003536 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3537
3538 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003539 {
3540 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003541 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003542 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003543
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003544 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3545 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3546 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3547 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003548 polarssl_free( tkeys );
3549 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003550 }
3551
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003552 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003553 {
3554 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003555 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003556 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003557
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003558 ssl->ticket_keys = tkeys;
3559
3560 return( 0 );
3561}
Paul Bakkera503a632013-08-14 13:48:06 +02003562#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003563
3564/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 * SSL set accessors
3566 */
3567void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3568{
3569 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003570
Paul Bakker606b4ba2013-08-14 16:52:14 +02003571#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003572 if( endpoint == SSL_IS_CLIENT )
3573 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003574#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003575}
3576
3577void ssl_set_authmode( ssl_context *ssl, int authmode )
3578{
3579 ssl->authmode = authmode;
3580}
3581
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003582#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003583void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003584 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003585 void *p_vrfy )
3586{
3587 ssl->f_vrfy = f_vrfy;
3588 ssl->p_vrfy = p_vrfy;
3589}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003590#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003591
Paul Bakker5121ce52009-01-03 21:22:43 +00003592void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003593 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 void *p_rng )
3595{
3596 ssl->f_rng = f_rng;
3597 ssl->p_rng = p_rng;
3598}
3599
3600void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003601 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 void *p_dbg )
3603{
3604 ssl->f_dbg = f_dbg;
3605 ssl->p_dbg = p_dbg;
3606}
3607
3608void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003609 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003610 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003611{
3612 ssl->f_recv = f_recv;
3613 ssl->f_send = f_send;
3614 ssl->p_recv = p_recv;
3615 ssl->p_send = p_send;
3616}
3617
Paul Bakker0a597072012-09-25 21:55:46 +00003618void ssl_set_session_cache( ssl_context *ssl,
3619 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3620 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003621{
Paul Bakker0a597072012-09-25 21:55:46 +00003622 ssl->f_get_cache = f_get_cache;
3623 ssl->p_get_cache = p_get_cache;
3624 ssl->f_set_cache = f_set_cache;
3625 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003626}
3627
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003628int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003629{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003630 int ret;
3631
3632 if( ssl == NULL ||
3633 session == NULL ||
3634 ssl->session_negotiate == NULL ||
3635 ssl->endpoint != SSL_IS_CLIENT )
3636 {
3637 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3638 }
3639
3640 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3641 return( ret );
3642
Paul Bakker0a597072012-09-25 21:55:46 +00003643 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003644
3645 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003646}
3647
Paul Bakkerb68cad62012-08-23 08:34:18 +00003648void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003649{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003650 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3651 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3652 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3653 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3654}
3655
3656void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3657 int major, int minor )
3658{
3659 if( major != SSL_MAJOR_VERSION_3 )
3660 return;
3661
3662 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3663 return;
3664
3665 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003666}
3667
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003668#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003669/* Add a new (empty) key_cert entry an return a pointer to it */
3670static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3671{
3672 ssl_key_cert *key_cert, *last;
3673
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003674 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3675 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003676 return( NULL );
3677
3678 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3679
3680 /* Append the new key_cert to the (possibly empty) current list */
3681 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003682 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003683 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003684 if( ssl->handshake != NULL )
3685 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003686 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003687 else
3688 {
3689 last = ssl->key_cert;
3690 while( last->next != NULL )
3691 last = last->next;
3692 last->next = key_cert;
3693 }
3694
3695 return key_cert;
3696}
3697
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003698void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003699 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003700{
3701 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003702 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003703 ssl->peer_cn = peer_cn;
3704}
3705
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003706int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003707 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003708{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003709 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3710
3711 if( key_cert == NULL )
3712 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3713
3714 key_cert->cert = own_cert;
3715 key_cert->key = pk_key;
3716
3717 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003718}
3719
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003720#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003721int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003722 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003723{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003724 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003725 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003726
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003727 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003728 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3729
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003730 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3731 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003732 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003733
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003734 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003735
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003736 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003737 if( ret != 0 )
3738 return( ret );
3739
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003740 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003741 return( ret );
3742
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003743 key_cert->cert = own_cert;
3744 key_cert->key_own_alloc = 1;
3745
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003746 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003747}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003748#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003749
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003750int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003751 void *rsa_key,
3752 rsa_decrypt_func rsa_decrypt,
3753 rsa_sign_func rsa_sign,
3754 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003755{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003756 int ret;
3757 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003758
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003759 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003760 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3761
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003762 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3763 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003764 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003765
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003766 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003767
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003768 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3769 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3770 return( ret );
3771
3772 key_cert->cert = own_cert;
3773 key_cert->key_own_alloc = 1;
3774
3775 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003776}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003777#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003778
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003779#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003780int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3781 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003782{
Paul Bakker6db455e2013-09-18 17:29:31 +02003783 if( psk == NULL || psk_identity == NULL )
3784 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3785
3786 if( ssl->psk != NULL )
3787 {
3788 polarssl_free( ssl->psk );
3789 polarssl_free( ssl->psk_identity );
3790 }
3791
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003792 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003793 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003794
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003795 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3796 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003797
3798 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3799 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3800
3801 memcpy( ssl->psk, psk, ssl->psk_len );
3802 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003803
3804 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003805}
3806
3807void ssl_set_psk_cb( ssl_context *ssl,
3808 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3809 size_t),
3810 void *p_psk )
3811{
3812 ssl->f_psk = f_psk;
3813 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003814}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003815#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003816
Paul Bakker48916f92012-09-16 19:57:18 +00003817#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003818int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003819{
3820 int ret;
3821
Paul Bakker48916f92012-09-16 19:57:18 +00003822 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003823 {
3824 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3825 return( ret );
3826 }
3827
Paul Bakker48916f92012-09-16 19:57:18 +00003828 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003829 {
3830 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3831 return( ret );
3832 }
3833
3834 return( 0 );
3835}
3836
Paul Bakker1b57b062011-01-06 15:48:19 +00003837int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3838{
3839 int ret;
3840
Paul Bakker48916f92012-09-16 19:57:18 +00003841 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003842 {
3843 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3844 return( ret );
3845 }
3846
Paul Bakker48916f92012-09-16 19:57:18 +00003847 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003848 {
3849 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3850 return( ret );
3851 }
3852
3853 return( 0 );
3854}
Paul Bakker48916f92012-09-16 19:57:18 +00003855#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003856
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003857#if defined(POLARSSL_SSL_SET_CURVES)
3858/*
3859 * Set the allowed elliptic curves
3860 */
3861void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3862{
3863 ssl->curve_list = curve_list;
3864}
3865#endif
3866
Paul Bakker0be444a2013-08-27 21:55:01 +02003867#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003868int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003869{
3870 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003871 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003872
3873 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003874
3875 if( ssl->hostname_len + 1 == 0 )
3876 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3877
Paul Bakker6e339b52013-07-03 13:37:05 +02003878 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003879
Paul Bakkerb15b8512012-01-13 13:44:06 +00003880 if( ssl->hostname == NULL )
3881 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3882
Paul Bakker3c2122f2013-06-24 19:03:14 +02003883 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003884 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003885
Paul Bakker40ea7de2009-05-03 10:18:48 +00003886 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003887
3888 return( 0 );
3889}
3890
Paul Bakker5701cdc2012-09-27 21:49:42 +00003891void ssl_set_sni( ssl_context *ssl,
3892 int (*f_sni)(void *, ssl_context *,
3893 const unsigned char *, size_t),
3894 void *p_sni )
3895{
3896 ssl->f_sni = f_sni;
3897 ssl->p_sni = p_sni;
3898}
Paul Bakker0be444a2013-08-27 21:55:01 +02003899#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003900
Paul Bakker490ecc82011-10-06 13:04:09 +00003901void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3902{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003903 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3904 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3905 {
3906 ssl->max_major_ver = major;
3907 ssl->max_minor_ver = minor;
3908 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003909}
3910
Paul Bakker1d29fb52012-09-28 13:28:45 +00003911void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3912{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003913 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3914 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3915 {
3916 ssl->min_major_ver = major;
3917 ssl->min_minor_ver = minor;
3918 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003919}
3920
Paul Bakker05decb22013-08-15 13:33:48 +02003921#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003922int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3923{
Paul Bakker77e257e2013-12-16 15:29:52 +01003924 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003925 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003926 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003927 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003928 }
3929
3930 ssl->mfl_code = mfl_code;
3931
3932 return( 0 );
3933}
Paul Bakker05decb22013-08-15 13:33:48 +02003934#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003935
Paul Bakker1f2bc622013-08-15 13:45:55 +02003936#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003937int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003938{
3939 if( ssl->endpoint != SSL_IS_CLIENT )
3940 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3941
Paul Bakker8c1ede62013-07-19 14:14:37 +02003942 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003943
3944 return( 0 );
3945}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003946#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003947
Paul Bakker48916f92012-09-16 19:57:18 +00003948void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3949{
3950 ssl->disable_renegotiation = renegotiation;
3951}
3952
3953void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3954{
3955 ssl->allow_legacy_renegotiation = allow_legacy;
3956}
3957
Paul Bakkera503a632013-08-14 13:48:06 +02003958#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003959int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3960{
3961 ssl->session_tickets = use_tickets;
3962
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003963 if( ssl->endpoint == SSL_IS_CLIENT )
3964 return( 0 );
3965
3966 if( ssl->f_rng == NULL )
3967 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3968
3969 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003970}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003971
3972void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3973{
3974 ssl->ticket_lifetime = lifetime;
3975}
Paul Bakkera503a632013-08-14 13:48:06 +02003976#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003977
Paul Bakker5121ce52009-01-03 21:22:43 +00003978/*
3979 * SSL get accessors
3980 */
Paul Bakker23986e52011-04-24 08:57:21 +00003981size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003982{
3983 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3984}
3985
Paul Bakkerff60ee62010-03-16 21:09:09 +00003986int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003987{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003988 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003989}
3990
Paul Bakkere3166ce2011-01-27 17:40:50 +00003991const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003992{
Paul Bakker926c8e42013-03-06 10:23:34 +01003993 if( ssl == NULL || ssl->session == NULL )
3994 return NULL;
3995
Paul Bakkere3166ce2011-01-27 17:40:50 +00003996 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003997}
3998
Paul Bakker43ca69c2011-01-15 17:35:19 +00003999const char *ssl_get_version( const ssl_context *ssl )
4000{
4001 switch( ssl->minor_ver )
4002 {
4003 case SSL_MINOR_VERSION_0:
4004 return( "SSLv3.0" );
4005
4006 case SSL_MINOR_VERSION_1:
4007 return( "TLSv1.0" );
4008
4009 case SSL_MINOR_VERSION_2:
4010 return( "TLSv1.1" );
4011
Paul Bakker1ef83d62012-04-11 12:09:53 +00004012 case SSL_MINOR_VERSION_3:
4013 return( "TLSv1.2" );
4014
Paul Bakker43ca69c2011-01-15 17:35:19 +00004015 default:
4016 break;
4017 }
4018 return( "unknown" );
4019}
4020
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004021#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004022const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004023{
4024 if( ssl == NULL || ssl->session == NULL )
4025 return NULL;
4026
4027 return ssl->session->peer_cert;
4028}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004029#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004030
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004031int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4032{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004033 if( ssl == NULL ||
4034 dst == NULL ||
4035 ssl->session == NULL ||
4036 ssl->endpoint != SSL_IS_CLIENT )
4037 {
4038 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4039 }
4040
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004041 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004042}
4043
Paul Bakker5121ce52009-01-03 21:22:43 +00004044/*
Paul Bakker1961b702013-01-25 14:49:24 +01004045 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004046 */
Paul Bakker1961b702013-01-25 14:49:24 +01004047int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004048{
Paul Bakker40e46942009-01-03 21:51:57 +00004049 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004050
Paul Bakker40e46942009-01-03 21:51:57 +00004051#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004052 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004053 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004054#endif
4055
Paul Bakker40e46942009-01-03 21:51:57 +00004056#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004057 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004058 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004059#endif
4060
Paul Bakker1961b702013-01-25 14:49:24 +01004061 return( ret );
4062}
4063
4064/*
4065 * Perform the SSL handshake
4066 */
4067int ssl_handshake( ssl_context *ssl )
4068{
4069 int ret = 0;
4070
4071 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4072
4073 while( ssl->state != SSL_HANDSHAKE_OVER )
4074 {
4075 ret = ssl_handshake_step( ssl );
4076
4077 if( ret != 0 )
4078 break;
4079 }
4080
Paul Bakker5121ce52009-01-03 21:22:43 +00004081 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4082
4083 return( ret );
4084}
4085
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004086#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004087/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004088 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004089 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004090static int ssl_write_hello_request( ssl_context *ssl )
4091{
4092 int ret;
4093
4094 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4095
4096 ssl->out_msglen = 4;
4097 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4098 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4099
4100 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4101 {
4102 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4103 return( ret );
4104 }
4105
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004106 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4107
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004108 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4109
4110 return( 0 );
4111}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004112#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004113
4114/*
4115 * Actually renegotiate current connection, triggered by either:
4116 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004117 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004118 * - receiving any handshake message on server during ssl_read() after the
4119 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004120 * If the handshake doesn't complete due to waiting for I/O, it will continue
4121 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004122 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004123static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004124{
4125 int ret;
4126
4127 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4128
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004129 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4130 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004131
4132 ssl->state = SSL_HELLO_REQUEST;
4133 ssl->renegotiation = SSL_RENEGOTIATION;
4134
Paul Bakker48916f92012-09-16 19:57:18 +00004135 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4136 {
4137 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4138 return( ret );
4139 }
4140
4141 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4142
4143 return( 0 );
4144}
4145
4146/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004147 * Renegotiate current connection on client,
4148 * or request renegotiation on server
4149 */
4150int ssl_renegotiate( ssl_context *ssl )
4151{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004152 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004153
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004154#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004155 /* On server, just send the request */
4156 if( ssl->endpoint == SSL_IS_SERVER )
4157 {
4158 if( ssl->state != SSL_HANDSHAKE_OVER )
4159 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4160
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004161 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004162 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004163#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004164
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004165#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004166 /*
4167 * On client, either start the renegotiation process or,
4168 * if already in progress, continue the handshake
4169 */
4170 if( ssl->renegotiation != SSL_RENEGOTIATION )
4171 {
4172 if( ssl->state != SSL_HANDSHAKE_OVER )
4173 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4174
4175 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4176 {
4177 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4178 return( ret );
4179 }
4180 }
4181 else
4182 {
4183 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4184 {
4185 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4186 return( ret );
4187 }
4188 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004189#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004190
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004191 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004192}
4193
4194/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004195 * Receive application data decrypted from the SSL layer
4196 */
Paul Bakker23986e52011-04-24 08:57:21 +00004197int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004198{
Paul Bakker23986e52011-04-24 08:57:21 +00004199 int ret;
4200 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004201
4202 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4203
4204 if( ssl->state != SSL_HANDSHAKE_OVER )
4205 {
4206 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4207 {
4208 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4209 return( ret );
4210 }
4211 }
4212
4213 if( ssl->in_offt == NULL )
4214 {
4215 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4216 {
Paul Bakker831a7552011-05-18 13:32:51 +00004217 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4218 return( 0 );
4219
Paul Bakker5121ce52009-01-03 21:22:43 +00004220 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4221 return( ret );
4222 }
4223
4224 if( ssl->in_msglen == 0 &&
4225 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4226 {
4227 /*
4228 * OpenSSL sends empty messages to randomize the IV
4229 */
4230 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4231 {
Paul Bakker831a7552011-05-18 13:32:51 +00004232 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4233 return( 0 );
4234
Paul Bakker5121ce52009-01-03 21:22:43 +00004235 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4236 return( ret );
4237 }
4238 }
4239
Paul Bakker48916f92012-09-16 19:57:18 +00004240 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4241 {
4242 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4243
4244 if( ssl->endpoint == SSL_IS_CLIENT &&
4245 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4246 ssl->in_hslen != 4 ) )
4247 {
4248 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4249 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4250 }
4251
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004252 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4253 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4254 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004255 {
4256 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4257
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004258#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004259 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004260 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004261 /*
4262 * SSLv3 does not have a "no_renegotiation" alert
4263 */
4264 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4265 return( ret );
4266 }
4267 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004268#endif
4269#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4270 defined(POLARSSL_SSL_PROTO_TLS1_2)
4271 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004272 {
4273 if( ( ret = ssl_send_alert_message( ssl,
4274 SSL_ALERT_LEVEL_WARNING,
4275 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4276 {
4277 return( ret );
4278 }
Paul Bakker48916f92012-09-16 19:57:18 +00004279 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004280 else
4281#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004282 {
4283 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004284 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004285 }
Paul Bakker48916f92012-09-16 19:57:18 +00004286 }
4287 else
4288 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004289 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004290 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004291 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004292 return( ret );
4293 }
4294
4295 return( POLARSSL_ERR_NET_WANT_READ );
4296 }
4297 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004298 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4299 {
4300 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4301 "but not honored by client" ) );
4302 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4303 }
Paul Bakker48916f92012-09-16 19:57:18 +00004304 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004305 {
4306 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004307 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 }
4309
4310 ssl->in_offt = ssl->in_msg;
4311 }
4312
4313 n = ( len < ssl->in_msglen )
4314 ? len : ssl->in_msglen;
4315
4316 memcpy( buf, ssl->in_offt, n );
4317 ssl->in_msglen -= n;
4318
4319 if( ssl->in_msglen == 0 )
4320 /* all bytes consumed */
4321 ssl->in_offt = NULL;
4322 else
4323 /* more data available */
4324 ssl->in_offt += n;
4325
4326 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4327
Paul Bakker23986e52011-04-24 08:57:21 +00004328 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004329}
4330
4331/*
4332 * Send application data to be encrypted by the SSL layer
4333 */
Paul Bakker23986e52011-04-24 08:57:21 +00004334int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004335{
Paul Bakker23986e52011-04-24 08:57:21 +00004336 int ret;
4337 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004338 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004339
4340 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4341
4342 if( ssl->state != SSL_HANDSHAKE_OVER )
4343 {
4344 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4345 {
4346 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4347 return( ret );
4348 }
4349 }
4350
Paul Bakker05decb22013-08-15 13:33:48 +02004351#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004352 /*
4353 * Assume mfl_code is correct since it was checked when set
4354 */
4355 max_len = mfl_code_to_length[ssl->mfl_code];
4356
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004357 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004358 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004359 */
4360 if( ssl->session_out != NULL &&
4361 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4362 {
4363 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4364 }
Paul Bakker05decb22013-08-15 13:33:48 +02004365#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004366
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004367 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004368
Paul Bakker5121ce52009-01-03 21:22:43 +00004369 if( ssl->out_left != 0 )
4370 {
4371 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4372 {
4373 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4374 return( ret );
4375 }
4376 }
Paul Bakker887bd502011-06-08 13:10:54 +00004377 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004378 {
Paul Bakker887bd502011-06-08 13:10:54 +00004379 ssl->out_msglen = n;
4380 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4381 memcpy( ssl->out_msg, buf, n );
4382
4383 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4384 {
4385 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4386 return( ret );
4387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004388 }
4389
4390 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4391
Paul Bakker23986e52011-04-24 08:57:21 +00004392 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004393}
4394
4395/*
4396 * Notify the peer that the connection is being closed
4397 */
4398int ssl_close_notify( ssl_context *ssl )
4399{
4400 int ret;
4401
4402 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4403
4404 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4405 {
4406 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4407 return( ret );
4408 }
4409
4410 if( ssl->state == SSL_HANDSHAKE_OVER )
4411 {
Paul Bakker48916f92012-09-16 19:57:18 +00004412 if( ( ret = ssl_send_alert_message( ssl,
4413 SSL_ALERT_LEVEL_WARNING,
4414 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004415 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004416 return( ret );
4417 }
4418 }
4419
4420 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4421
4422 return( ret );
4423}
4424
Paul Bakker48916f92012-09-16 19:57:18 +00004425void ssl_transform_free( ssl_transform *transform )
4426{
4427#if defined(POLARSSL_ZLIB_SUPPORT)
4428 deflateEnd( &transform->ctx_deflate );
4429 inflateEnd( &transform->ctx_inflate );
4430#endif
4431
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004432 cipher_free_ctx( &transform->cipher_ctx_enc );
4433 cipher_free_ctx( &transform->cipher_ctx_dec );
4434
Paul Bakker61d113b2013-07-04 11:51:43 +02004435 md_free_ctx( &transform->md_ctx_enc );
4436 md_free_ctx( &transform->md_ctx_dec );
4437
Paul Bakker48916f92012-09-16 19:57:18 +00004438 memset( transform, 0, sizeof( ssl_transform ) );
4439}
4440
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004441#if defined(POLARSSL_X509_CRT_PARSE_C)
4442static void ssl_key_cert_free( ssl_key_cert *key_cert )
4443{
4444 ssl_key_cert *cur = key_cert, *next;
4445
4446 while( cur != NULL )
4447 {
4448 next = cur->next;
4449
4450 if( cur->key_own_alloc )
4451 {
4452 pk_free( cur->key );
4453 polarssl_free( cur->key );
4454 }
4455 polarssl_free( cur );
4456
4457 cur = next;
4458 }
4459}
4460#endif /* POLARSSL_X509_CRT_PARSE_C */
4461
Paul Bakker48916f92012-09-16 19:57:18 +00004462void ssl_handshake_free( ssl_handshake_params *handshake )
4463{
4464#if defined(POLARSSL_DHM_C)
4465 dhm_free( &handshake->dhm_ctx );
4466#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004467#if defined(POLARSSL_ECDH_C)
4468 ecdh_free( &handshake->ecdh_ctx );
4469#endif
4470
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004471#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004472 /* explicit void pointer cast for buggy MS compiler */
4473 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004474#endif
4475
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004476#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4477 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4478 /*
4479 * Free only the linked list wrapper, not the keys themselves
4480 * since the belong to the SNI callback
4481 */
4482 if( handshake->sni_key_cert != NULL )
4483 {
4484 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4485
4486 while( cur != NULL )
4487 {
4488 next = cur->next;
4489 polarssl_free( cur );
4490 cur = next;
4491 }
4492 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004493#endif
4494
Paul Bakker48916f92012-09-16 19:57:18 +00004495 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4496}
4497
4498void ssl_session_free( ssl_session *session )
4499{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004500#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004501 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004502 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004503 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004504 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004505 }
Paul Bakkered27a042013-04-18 22:46:23 +02004506#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004507
Paul Bakkera503a632013-08-14 13:48:06 +02004508#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004509 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004510#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004511
Paul Bakker0a597072012-09-25 21:55:46 +00004512 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004513}
4514
Paul Bakker5121ce52009-01-03 21:22:43 +00004515/*
4516 * Free an SSL context
4517 */
4518void ssl_free( ssl_context *ssl )
4519{
4520 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4521
Paul Bakker5121ce52009-01-03 21:22:43 +00004522 if( ssl->out_ctr != NULL )
4523 {
4524 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004525 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004526 }
4527
4528 if( ssl->in_ctr != NULL )
4529 {
4530 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004531 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004532 }
4533
Paul Bakker16770332013-10-11 09:59:44 +02004534#if defined(POLARSSL_ZLIB_SUPPORT)
4535 if( ssl->compress_buf != NULL )
4536 {
4537 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4538 polarssl_free( ssl->compress_buf );
4539 }
4540#endif
4541
Paul Bakker40e46942009-01-03 21:51:57 +00004542#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004543 mpi_free( &ssl->dhm_P );
4544 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004545#endif
4546
Paul Bakker48916f92012-09-16 19:57:18 +00004547 if( ssl->transform )
4548 {
4549 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004550 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004551 }
4552
4553 if( ssl->handshake )
4554 {
4555 ssl_handshake_free( ssl->handshake );
4556 ssl_transform_free( ssl->transform_negotiate );
4557 ssl_session_free( ssl->session_negotiate );
4558
Paul Bakker6e339b52013-07-03 13:37:05 +02004559 polarssl_free( ssl->handshake );
4560 polarssl_free( ssl->transform_negotiate );
4561 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004562 }
4563
Paul Bakkerc0463502013-02-14 11:19:38 +01004564 if( ssl->session )
4565 {
4566 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004567 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004568 }
4569
Paul Bakkera503a632013-08-14 13:48:06 +02004570#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004571 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004572#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004573
Paul Bakker0be444a2013-08-27 21:55:01 +02004574#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004575 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576 {
4577 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004578 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 ssl->hostname_len = 0;
4580 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004581#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004582
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004583#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004584 if( ssl->psk != NULL )
4585 {
4586 memset( ssl->psk, 0, ssl->psk_len );
4587 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4588 polarssl_free( ssl->psk );
4589 polarssl_free( ssl->psk_identity );
4590 ssl->psk_len = 0;
4591 ssl->psk_identity_len = 0;
4592 }
4593#endif
4594
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004595#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004596 ssl_key_cert_free( ssl->key_cert );
4597#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004598
Paul Bakker05ef8352012-05-08 09:17:57 +00004599#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4600 if( ssl_hw_record_finish != NULL )
4601 {
4602 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4603 ssl_hw_record_finish( ssl );
4604 }
4605#endif
4606
Paul Bakker5121ce52009-01-03 21:22:43 +00004607 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004608
Paul Bakker86f04f42013-02-14 11:20:09 +01004609 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004610 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004611}
4612
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004613#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004614/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004615 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004616 */
4617unsigned char ssl_sig_from_pk( pk_context *pk )
4618{
4619#if defined(POLARSSL_RSA_C)
4620 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4621 return( SSL_SIG_RSA );
4622#endif
4623#if defined(POLARSSL_ECDSA_C)
4624 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4625 return( SSL_SIG_ECDSA );
4626#endif
4627 return( SSL_SIG_ANON );
4628}
4629
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004630pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4631{
4632 switch( sig )
4633 {
4634#if defined(POLARSSL_RSA_C)
4635 case SSL_SIG_RSA:
4636 return( POLARSSL_PK_RSA );
4637#endif
4638#if defined(POLARSSL_ECDSA_C)
4639 case SSL_SIG_ECDSA:
4640 return( POLARSSL_PK_ECDSA );
4641#endif
4642 default:
4643 return( POLARSSL_PK_NONE );
4644 }
4645}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004646#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004647
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004648/*
4649 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4650 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004651md_type_t ssl_md_alg_from_hash( unsigned char hash )
4652{
4653 switch( hash )
4654 {
4655#if defined(POLARSSL_MD5_C)
4656 case SSL_HASH_MD5:
4657 return( POLARSSL_MD_MD5 );
4658#endif
4659#if defined(POLARSSL_SHA1_C)
4660 case SSL_HASH_SHA1:
4661 return( POLARSSL_MD_SHA1 );
4662#endif
4663#if defined(POLARSSL_SHA256_C)
4664 case SSL_HASH_SHA224:
4665 return( POLARSSL_MD_SHA224 );
4666 case SSL_HASH_SHA256:
4667 return( POLARSSL_MD_SHA256 );
4668#endif
4669#if defined(POLARSSL_SHA512_C)
4670 case SSL_HASH_SHA384:
4671 return( POLARSSL_MD_SHA384 );
4672 case SSL_HASH_SHA512:
4673 return( POLARSSL_MD_SHA512 );
4674#endif
4675 default:
4676 return( POLARSSL_MD_NONE );
4677 }
4678}
4679
Paul Bakker5121ce52009-01-03 21:22:43 +00004680#endif
Gergely Budai987bfb52014-01-19 21:48:42 +01004681
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004682#if defined(POLARSSL_SSL_SET_CURVES)
4683/*
4684 * Check is a curve proposed by the peer is in our list.
4685 * Return 1 if we're willing to use it, 0 otherwise.
4686 */
4687int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4688{
4689 const ecp_group_id *gid;
4690
4691 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4692 if( *gid == grp_id )
4693 return( 1 );
4694
4695 return( 0 );
4696}
4697#endif