blob: 4f3095caa40403c5e58256b36d26d5dc71612a8f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker0be444a2013-08-27 21:55:01 +020038#include "polarssl/debug.h"
39#include "polarssl/ssl.h"
40
Paul Bakker7dc4c442014-02-01 22:50:26 +010041#if defined(POLARSSL_PLATFORM_C)
42#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020043#else
44#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakker6edcd412013-10-29 15:22:54 +010050#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
51 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000052#define strcasecmp _stricmp
53#endif
54
Paul Bakker05decb22013-08-15 13:33:48 +020055#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020056/*
57 * Convert max_fragment_length codes to length.
58 * RFC 6066 says:
59 * enum{
60 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
61 * } MaxFragmentLength;
62 * and we add 0 -> extension unused
63 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020064static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020065{
66 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
67 512, /* SSL_MAX_FRAG_LEN_512 */
68 1024, /* SSL_MAX_FRAG_LEN_1024 */
69 2048, /* SSL_MAX_FRAG_LEN_2048 */
70 4096, /* SSL_MAX_FRAG_LEN_4096 */
71};
Paul Bakker05decb22013-08-15 13:33:48 +020072#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020073
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020074static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
75{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020076 ssl_session_free( dst );
77 memcpy( dst, src, sizeof( ssl_session ) );
78
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020080 if( src->peer_cert != NULL )
81 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020082 int ret;
83
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020084 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
85 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020086 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
87
Paul Bakkerb6b09562013-09-18 14:17:41 +020088 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089
Paul Bakkerddf26b42013-09-18 13:46:23 +020090 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
91 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020092 {
93 polarssl_free( dst->peer_cert );
94 dst->peer_cert = NULL;
95 return( ret );
96 }
97 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020099
Paul Bakkera503a632013-08-14 13:48:06 +0200100#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101 if( src->ticket != NULL )
102 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200103 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
104 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200105 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
106
107 memcpy( dst->ticket, src->ticket, src->ticket_len );
108 }
Paul Bakkera503a632013-08-14 13:48:06 +0200109#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110
111 return( 0 );
112}
113
Paul Bakker05ef8352012-05-08 09:17:57 +0000114#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
115int (*ssl_hw_record_init)(ssl_context *ssl,
116 const unsigned char *key_enc, const unsigned char *key_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100117 size_t keylen,
Paul Bakker05ef8352012-05-08 09:17:57 +0000118 const unsigned char *iv_enc, const unsigned char *iv_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100119 size_t ivlen,
120 const unsigned char *mac_enc, const unsigned char *mac_dec,
121 size_t maclen) = NULL;
122int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000123int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
124int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
125int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
126int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
127#endif
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129/*
130 * Key material generation
131 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200132#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200133static int ssl3_prf( const unsigned char *secret, size_t slen,
134 const char *label,
135 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000136 unsigned char *dstbuf, size_t dlen )
137{
138 size_t i;
139 md5_context md5;
140 sha1_context sha1;
141 unsigned char padding[16];
142 unsigned char sha1sum[20];
143 ((void)label);
144
145 /*
146 * SSLv3:
147 * block =
148 * MD5( secret + SHA1( 'A' + secret + random ) ) +
149 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
150 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
151 * ...
152 */
153 for( i = 0; i < dlen / 16; i++ )
154 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200155 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000156
157 sha1_starts( &sha1 );
158 sha1_update( &sha1, padding, 1 + i );
159 sha1_update( &sha1, secret, slen );
160 sha1_update( &sha1, random, rlen );
161 sha1_finish( &sha1, sha1sum );
162
163 md5_starts( &md5 );
164 md5_update( &md5, secret, slen );
165 md5_update( &md5, sha1sum, 20 );
166 md5_finish( &md5, dstbuf + i * 16 );
167 }
168
169 memset( &md5, 0, sizeof( md5 ) );
170 memset( &sha1, 0, sizeof( sha1 ) );
171
172 memset( padding, 0, sizeof( padding ) );
173 memset( sha1sum, 0, sizeof( sha1sum ) );
174
175 return( 0 );
176}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200177#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000178
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200179#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200180static int tls1_prf( const unsigned char *secret, size_t slen,
181 const char *label,
182 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000183 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184{
Paul Bakker23986e52011-04-24 08:57:21 +0000185 size_t nb, hs;
186 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200187 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 unsigned char tmp[128];
189 unsigned char h_i[20];
190
191 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000192 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 hs = ( slen + 1 ) / 2;
195 S1 = secret;
196 S2 = secret + slen - hs;
197
198 nb = strlen( label );
199 memcpy( tmp + 20, label, nb );
200 memcpy( tmp + 20 + nb, random, rlen );
201 nb += rlen;
202
203 /*
204 * First compute P_md5(secret,label+random)[0..dlen]
205 */
206 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
207
208 for( i = 0; i < dlen; i += 16 )
209 {
210 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
211 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
212
213 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
214
215 for( j = 0; j < k; j++ )
216 dstbuf[i + j] = h_i[j];
217 }
218
219 /*
220 * XOR out with P_sha1(secret,label+random)[0..dlen]
221 */
222 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
223
224 for( i = 0; i < dlen; i += 20 )
225 {
226 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
227 sha1_hmac( S2, hs, tmp, 20, tmp );
228
229 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
230
231 for( j = 0; j < k; j++ )
232 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
233 }
234
235 memset( tmp, 0, sizeof( tmp ) );
236 memset( h_i, 0, sizeof( h_i ) );
237
238 return( 0 );
239}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200240#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200242#if defined(POLARSSL_SSL_PROTO_TLS1_2)
243#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200244static int tls_prf_sha256( const unsigned char *secret, size_t slen,
245 const char *label,
246 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000247 unsigned char *dstbuf, size_t dlen )
248{
249 size_t nb;
250 size_t i, j, k;
251 unsigned char tmp[128];
252 unsigned char h_i[32];
253
254 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
255 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
256
257 nb = strlen( label );
258 memcpy( tmp + 32, label, nb );
259 memcpy( tmp + 32 + nb, random, rlen );
260 nb += rlen;
261
262 /*
263 * Compute P_<hash>(secret, label + random)[0..dlen]
264 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200265 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000266
267 for( i = 0; i < dlen; i += 32 )
268 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200269 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
270 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000271
272 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
273
274 for( j = 0; j < k; j++ )
275 dstbuf[i + j] = h_i[j];
276 }
277
278 memset( tmp, 0, sizeof( tmp ) );
279 memset( h_i, 0, sizeof( h_i ) );
280
281 return( 0 );
282}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200283#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000284
Paul Bakker9e36f042013-06-30 14:34:05 +0200285#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200286static int tls_prf_sha384( const unsigned char *secret, size_t slen,
287 const char *label,
288 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000289 unsigned char *dstbuf, size_t dlen )
290{
291 size_t nb;
292 size_t i, j, k;
293 unsigned char tmp[128];
294 unsigned char h_i[48];
295
296 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
297 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
298
299 nb = strlen( label );
300 memcpy( tmp + 48, label, nb );
301 memcpy( tmp + 48 + nb, random, rlen );
302 nb += rlen;
303
304 /*
305 * Compute P_<hash>(secret, label + random)[0..dlen]
306 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200307 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000308
309 for( i = 0; i < dlen; i += 48 )
310 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200311 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
312 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000313
314 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
315
316 for( j = 0; j < k; j++ )
317 dstbuf[i + j] = h_i[j];
318 }
319
320 memset( tmp, 0, sizeof( tmp ) );
321 memset( h_i, 0, sizeof( h_i ) );
322
323 return( 0 );
324}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200325#endif /* POLARSSL_SHA512_C */
326#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200328static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200329
330#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
331 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200332static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200333#endif
Paul Bakker380da532012-04-18 16:10:25 +0000334
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200335#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000336static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338#endif
339
340#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
341static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343#endif
344
345#if defined(POLARSSL_SSL_PROTO_TLS1_2)
346#if defined(POLARSSL_SHA256_C)
347static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
348static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000349static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100351
Paul Bakker9e36f042013-06-30 14:34:05 +0200352#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200353static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100354static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000355static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100356#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +0000358
Paul Bakker5121ce52009-01-03 21:22:43 +0000359int ssl_derive_keys( ssl_context *ssl )
360{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200361 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 unsigned char keyblk[256];
364 unsigned char *key1;
365 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100366 unsigned char *mac_enc;
367 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200368 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100369 const cipher_info_t *cipher_info;
370 const md_info_t *md_info;
371
Paul Bakker48916f92012-09-16 19:57:18 +0000372 ssl_session *session = ssl->session_negotiate;
373 ssl_transform *transform = ssl->transform_negotiate;
374 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
377
Paul Bakker68884e32013-01-07 18:20:04 +0100378 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
379 if( cipher_info == NULL )
380 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200381 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100382 transform->ciphersuite_info->cipher ) );
383 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
384 }
385
386 md_info = md_info_from_type( transform->ciphersuite_info->mac );
387 if( md_info == NULL )
388 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200389 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100390 transform->ciphersuite_info->mac ) );
391 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
392 }
393
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000395 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000396 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200397#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000398 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000399 {
Paul Bakker48916f92012-09-16 19:57:18 +0000400 handshake->tls_prf = ssl3_prf;
401 handshake->calc_verify = ssl_calc_verify_ssl;
402 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000403 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200404 else
405#endif
406#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
407 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408 {
Paul Bakker48916f92012-09-16 19:57:18 +0000409 handshake->tls_prf = tls1_prf;
410 handshake->calc_verify = ssl_calc_verify_tls;
411 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200413 else
414#endif
415#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200416#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200417 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
418 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000419 {
Paul Bakker48916f92012-09-16 19:57:18 +0000420 handshake->tls_prf = tls_prf_sha384;
421 handshake->calc_verify = ssl_calc_verify_tls_sha384;
422 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000423 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000424 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200425#endif
426#if defined(POLARSSL_SHA256_C)
427 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = tls_prf_sha256;
430 handshake->calc_verify = ssl_calc_verify_tls_sha256;
431 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200433 else
434#endif
435#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200436 {
437 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200438 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200439 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000440
441 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 * SSLv3:
443 * master =
444 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
445 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
446 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200447 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200448 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 * master = PRF( premaster, "master secret", randbytes )[0..47]
450 */
Paul Bakker0a597072012-09-25 21:55:46 +0000451 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 {
Paul Bakker48916f92012-09-16 19:57:18 +0000453 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
454 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
Paul Bakker48916f92012-09-16 19:57:18 +0000456 handshake->tls_prf( handshake->premaster, handshake->pmslen,
457 "master secret",
458 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Paul Bakker48916f92012-09-16 19:57:18 +0000460 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 }
462 else
463 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
464
465 /*
466 * Swap the client and server random values.
467 */
Paul Bakker48916f92012-09-16 19:57:18 +0000468 memcpy( tmp, handshake->randbytes, 64 );
469 memcpy( handshake->randbytes, tmp + 32, 32 );
470 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 memset( tmp, 0, sizeof( tmp ) );
472
473 /*
474 * SSLv3:
475 * key block =
476 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
477 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
478 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
479 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
480 * ...
481 *
482 * TLSv1:
483 * key block = PRF( master, "key expansion", randbytes )
484 */
Paul Bakker48916f92012-09-16 19:57:18 +0000485 handshake->tls_prf( session->master, 48, "key expansion",
486 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
Paul Bakker48916f92012-09-16 19:57:18 +0000488 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
489 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
490 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
491 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
493
Paul Bakker48916f92012-09-16 19:57:18 +0000494 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 /*
497 * Determine the appropriate key, IV and MAC length.
498 */
Paul Bakker68884e32013-01-07 18:20:04 +0100499
500 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 {
Paul Bakker68884e32013-01-07 18:20:04 +0100502 transform->keylen = cipher_info->key_length;
503 transform->keylen /= 8;
504 transform->minlen = 1;
505 transform->ivlen = 12;
506 transform->fixed_ivlen = 4;
507 transform->maclen = 0;
508 }
509 else
510 {
511 if( md_info->type != POLARSSL_MD_NONE )
512 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200513 int ret;
514
Paul Bakker61d113b2013-07-04 11:51:43 +0200515 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
516 {
517 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
518 return( ret );
519 }
520
521 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
522 {
523 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
524 return( ret );
525 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
Paul Bakker68884e32013-01-07 18:20:04 +0100527 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200528
Paul Bakker1f2bc622013-08-15 13:45:55 +0200529#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200530 /*
531 * If HMAC is to be truncated, we shall keep the leftmost bytes,
532 * (rfc 6066 page 13 or rfc 2104 section 4),
533 * so we only need to adjust the length here.
534 */
535 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
536 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200537#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100538 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker68884e32013-01-07 18:20:04 +0100540 transform->keylen = cipher_info->key_length;
541 transform->keylen /= 8;
542 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Paul Bakker68884e32013-01-07 18:20:04 +0100544 transform->minlen = transform->keylen;
545 if( transform->minlen < transform->maclen )
546 {
547 if( cipher_info->mode == POLARSSL_MODE_STREAM )
548 transform->minlen = transform->maclen;
549 else
550 transform->minlen += transform->keylen;
551 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 }
553
554 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000555 transform->keylen, transform->minlen, transform->ivlen,
556 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 /*
559 * Finally setup the cipher contexts, IVs and MAC secrets.
560 */
561 if( ssl->endpoint == SSL_IS_CLIENT )
562 {
Paul Bakker48916f92012-09-16 19:57:18 +0000563 key1 = keyblk + transform->maclen * 2;
564 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Paul Bakker68884e32013-01-07 18:20:04 +0100566 mac_enc = keyblk;
567 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000569 /*
570 * This is not used in TLS v1.1.
571 */
Paul Bakker48916f92012-09-16 19:57:18 +0000572 iv_copy_len = ( transform->fixed_ivlen ) ?
573 transform->fixed_ivlen : transform->ivlen;
574 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
575 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000576 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 }
578 else
579 {
Paul Bakker48916f92012-09-16 19:57:18 +0000580 key1 = keyblk + transform->maclen * 2 + transform->keylen;
581 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Paul Bakker68884e32013-01-07 18:20:04 +0100583 mac_enc = keyblk + transform->maclen;
584 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000586 /*
587 * This is not used in TLS v1.1.
588 */
Paul Bakker48916f92012-09-16 19:57:18 +0000589 iv_copy_len = ( transform->fixed_ivlen ) ?
590 transform->fixed_ivlen : transform->ivlen;
591 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
592 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000593 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 }
595
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200596#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100597 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
598 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100599 if( transform->maclen > sizeof transform->mac_enc )
600 {
601 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
602 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
603 }
604
Paul Bakker68884e32013-01-07 18:20:04 +0100605 memcpy( transform->mac_enc, mac_enc, transform->maclen );
606 memcpy( transform->mac_dec, mac_dec, transform->maclen );
607 }
608 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200609#endif
610#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
611 defined(POLARSSL_SSL_PROTO_TLS1_2)
612 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100613 {
614 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
615 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
616 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200617 else
618#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200619 {
620 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200621 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200622 }
Paul Bakker68884e32013-01-07 18:20:04 +0100623
Paul Bakker05ef8352012-05-08 09:17:57 +0000624#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
625 if( ssl_hw_record_init != NULL)
626 {
627 int ret = 0;
628
629 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
630
Paul Bakker07eb38b2012-12-19 14:42:06 +0100631 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
632 transform->iv_enc, transform->iv_dec,
633 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100634 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100635 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000636 {
637 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
638 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
639 }
640 }
641#endif
642
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200643 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
644 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200646 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
647 return( ret );
648 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200649
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200650 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
651 cipher_info ) ) != 0 )
652 {
653 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
654 return( ret );
655 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200656
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200657 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
658 cipher_info->key_length,
659 POLARSSL_ENCRYPT ) ) != 0 )
660 {
661 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
662 return( ret );
663 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200664
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200665 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
666 cipher_info->key_length,
667 POLARSSL_DECRYPT ) ) != 0 )
668 {
669 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
670 return( ret );
671 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200672
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200673#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200674 if( cipher_info->mode == POLARSSL_MODE_CBC )
675 {
676 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
677 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200678 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200679 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
680 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200681 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200682
683 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
684 POLARSSL_PADDING_NONE ) ) != 0 )
685 {
686 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
687 return( ret );
688 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200690#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
692 memset( keyblk, 0, sizeof( keyblk ) );
693
Paul Bakker2770fbd2012-07-03 13:30:23 +0000694#if defined(POLARSSL_ZLIB_SUPPORT)
695 // Initialize compression
696 //
Paul Bakker48916f92012-09-16 19:57:18 +0000697 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000698 {
Paul Bakker16770332013-10-11 09:59:44 +0200699 if( ssl->compress_buf == NULL )
700 {
701 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
702 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
703 if( ssl->compress_buf == NULL )
704 {
705 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
706 SSL_BUFFER_LEN ) );
707 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
708 }
709 }
710
Paul Bakker2770fbd2012-07-03 13:30:23 +0000711 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
712
Paul Bakker48916f92012-09-16 19:57:18 +0000713 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
714 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000715
Paul Bakker48916f92012-09-16 19:57:18 +0000716 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
717 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000718 {
719 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
720 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
721 }
722 }
723#endif /* POLARSSL_ZLIB_SUPPORT */
724
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
726
727 return( 0 );
728}
729
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200730#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000731void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000732{
733 md5_context md5;
734 sha1_context sha1;
735 unsigned char pad_1[48];
736 unsigned char pad_2[48];
737
Paul Bakker380da532012-04-18 16:10:25 +0000738 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Paul Bakker48916f92012-09-16 19:57:18 +0000740 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
741 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Paul Bakker380da532012-04-18 16:10:25 +0000743 memset( pad_1, 0x36, 48 );
744 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Paul Bakker48916f92012-09-16 19:57:18 +0000746 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000747 md5_update( &md5, pad_1, 48 );
748 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker380da532012-04-18 16:10:25 +0000750 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000751 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000752 md5_update( &md5, pad_2, 48 );
753 md5_update( &md5, hash, 16 );
754 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakker48916f92012-09-16 19:57:18 +0000756 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000757 sha1_update( &sha1, pad_1, 40 );
758 sha1_finish( &sha1, hash + 16 );
759
760 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000761 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000762 sha1_update( &sha1, pad_2, 40 );
763 sha1_update( &sha1, hash + 16, 20 );
764 sha1_finish( &sha1, hash + 16 );
765
766 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
767 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
768
769 return;
770}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200771#endif
Paul Bakker380da532012-04-18 16:10:25 +0000772
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000774void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
775{
776 md5_context md5;
777 sha1_context sha1;
778
779 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
780
Paul Bakker48916f92012-09-16 19:57:18 +0000781 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
782 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000783
Paul Bakker48916f92012-09-16 19:57:18 +0000784 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000785 sha1_finish( &sha1, hash + 16 );
786
787 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
788 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
789
790 return;
791}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200792#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000793
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200794#if defined(POLARSSL_SSL_PROTO_TLS1_2)
795#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000796void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
797{
Paul Bakker9e36f042013-06-30 14:34:05 +0200798 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000799
800 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
801
Paul Bakker9e36f042013-06-30 14:34:05 +0200802 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
803 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000804
805 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
806 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
807
808 return;
809}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000811
Paul Bakker9e36f042013-06-30 14:34:05 +0200812#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000813void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
814{
Paul Bakker9e36f042013-06-30 14:34:05 +0200815 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000816
817 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
818
Paul Bakker9e36f042013-06-30 14:34:05 +0200819 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
820 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821
Paul Bakkerca4ab492012-04-18 14:23:57 +0000822 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
824
825 return;
826}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200827#endif /* POLARSSL_SHA512_C */
828#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200830#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200831int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
832{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200833 unsigned char *p = ssl->handshake->premaster;
834 unsigned char *end = p + sizeof( ssl->handshake->premaster );
835
836 /*
837 * PMS = struct {
838 * opaque other_secret<0..2^16-1>;
839 * opaque psk<0..2^16-1>;
840 * };
841 * with "other_secret" depending on the particular key exchange
842 */
843#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
844 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
845 {
846 if( end - p < 2 + (int) ssl->psk_len )
847 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
848
849 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
850 *(p++) = (unsigned char)( ssl->psk_len );
851 p += ssl->psk_len;
852 }
853 else
854#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200855#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
856 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
857 {
858 /*
859 * other_secret already set by the ClientKeyExchange message,
860 * and is 48 bytes long
861 */
862 *p++ = 0;
863 *p++ = 48;
864 p += 48;
865 }
866 else
867#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200868#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
869 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
870 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200871 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200872 size_t len = ssl->handshake->dhm_ctx.len;
873
874 if( end - p < 2 + (int) len )
875 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
876
877 *(p++) = (unsigned char)( len >> 8 );
878 *(p++) = (unsigned char)( len );
879 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
880 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
881 {
882 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
883 return( ret );
884 }
885 p += len;
886
887 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
888 }
889 else
890#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
891#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
892 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
893 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200894 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895 size_t zlen;
896
897 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
898 p + 2, end - (p + 2),
899 ssl->f_rng, ssl->p_rng ) ) != 0 )
900 {
901 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
902 return( ret );
903 }
904
905 *(p++) = (unsigned char)( zlen >> 8 );
906 *(p++) = (unsigned char)( zlen );
907 p += zlen;
908
909 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
910 }
911 else
912#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
913 {
914 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
915 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
916 }
917
918 /* opaque psk<0..2^16-1>; */
919 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
920 *(p++) = (unsigned char)( ssl->psk_len );
921 memcpy( p, ssl->psk, ssl->psk_len );
922 p += ssl->psk_len;
923
924 ssl->handshake->pmslen = p - ssl->handshake->premaster;
925
926 return( 0 );
927}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200928#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200929
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200930#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000931/*
932 * SSLv3.0 MAC functions
933 */
Paul Bakker68884e32013-01-07 18:20:04 +0100934static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
935 unsigned char *buf, size_t len,
936 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000937{
938 unsigned char header[11];
939 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100940 int padlen = 0;
941 int md_size = md_get_size( md_ctx->md_info );
942 int md_type = md_get_type( md_ctx->md_info );
943
944 if( md_type == POLARSSL_MD_MD5 )
945 padlen = 48;
946 else if( md_type == POLARSSL_MD_SHA1 )
947 padlen = 40;
948 else if( md_type == POLARSSL_MD_SHA256 )
949 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100950 else if( md_type == POLARSSL_MD_SHA384 )
951 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
953 memcpy( header, ctr, 8 );
954 header[ 8] = (unsigned char) type;
955 header[ 9] = (unsigned char)( len >> 8 );
956 header[10] = (unsigned char)( len );
957
Paul Bakker68884e32013-01-07 18:20:04 +0100958 memset( padding, 0x36, padlen );
959 md_starts( md_ctx );
960 md_update( md_ctx, secret, md_size );
961 md_update( md_ctx, padding, padlen );
962 md_update( md_ctx, header, 11 );
963 md_update( md_ctx, buf, len );
964 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Paul Bakker68884e32013-01-07 18:20:04 +0100966 memset( padding, 0x5C, padlen );
967 md_starts( md_ctx );
968 md_update( md_ctx, secret, md_size );
969 md_update( md_ctx, padding, padlen );
970 md_update( md_ctx, buf + len, md_size );
971 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000972}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200973#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000974
Paul Bakker5121ce52009-01-03 21:22:43 +0000975/*
976 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200977 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000978static int ssl_encrypt_buf( ssl_context *ssl )
979{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200980 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
982 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
983
984 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200985 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200987#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
988 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
989 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
990 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
991 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200993#if defined(POLARSSL_SSL_PROTO_SSL3)
994 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
995 {
996 ssl_mac( &ssl->transform_out->md_ctx_enc,
997 ssl->transform_out->mac_enc,
998 ssl->out_msg, ssl->out_msglen,
999 ssl->out_ctr, ssl->out_msgtype );
1000 }
1001 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001002#endif
1003#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001004 defined(POLARSSL_SSL_PROTO_TLS1_2)
1005 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1006 {
1007 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1008 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1009 ssl->out_msg, ssl->out_msglen );
1010 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1011 ssl->out_msg + ssl->out_msglen );
1012 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1013 }
1014 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001015#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001016 {
1017 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1018 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1019 }
1020
1021 SSL_DEBUG_BUF( 4, "computed mac",
1022 ssl->out_msg + ssl->out_msglen,
1023 ssl->transform_out->maclen );
1024
1025 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001026 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001027#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001029 /*
1030 * Encrypt
1031 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001032#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001033 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1034 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001036 int ret;
1037 size_t olen = 0;
1038
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1040 "including %d bytes of padding",
1041 ssl->out_msglen, 0 ) );
1042
1043 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1044 ssl->out_msg, ssl->out_msglen );
1045
Paul Bakker45125bc2013-09-04 16:47:11 +02001046 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001047 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001048 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1049 return( ret );
1050 }
1051
1052 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1053 ssl->transform_out->iv_enc,
1054 ssl->transform_out->ivlen ) ) != 0 )
1055 {
1056 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001057 return( ret );
1058 }
1059
1060 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1061 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1062 &olen ) ) != 0 )
1063 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001064 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001065 return( ret );
1066 }
1067
1068 if( ssl->out_msglen != olen )
1069 {
1070 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1071 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001072 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001073 }
1074
1075 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001076 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001077 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001078 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001079 return( ret );
1080 }
1081
1082 if( 0 != olen )
1083 {
1084 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1085 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001086 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001087 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 }
Paul Bakker68884e32013-01-07 18:20:04 +01001089 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001090#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001091#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001092 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1093 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001095 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001096 unsigned char *enc_msg;
1097 unsigned char add_data[13];
1098 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1099
Paul Bakkerca4ab492012-04-18 14:23:57 +00001100 memcpy( add_data, ssl->out_ctr, 8 );
1101 add_data[8] = ssl->out_msgtype;
1102 add_data[9] = ssl->major_ver;
1103 add_data[10] = ssl->minor_ver;
1104 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1105 add_data[12] = ssl->out_msglen & 0xFF;
1106
1107 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1108 add_data, 13 );
1109
Paul Bakker68884e32013-01-07 18:20:04 +01001110 /*
1111 * Generate IV
1112 */
1113 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001114 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001115 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1116 if( ret != 0 )
1117 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001118
Paul Bakker68884e32013-01-07 18:20:04 +01001119 memcpy( ssl->out_iv,
1120 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1121 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001122
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001123 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1124 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1125
Paul Bakker68884e32013-01-07 18:20:04 +01001126 /*
1127 * Fix pointer positions and message length with added IV
1128 */
1129 enc_msg = ssl->out_msg;
1130 enc_msglen = ssl->out_msglen;
1131 ssl->out_msglen += ssl->transform_out->ivlen -
1132 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001133
Paul Bakker68884e32013-01-07 18:20:04 +01001134 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1135 "including %d bytes of padding",
1136 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001137
Paul Bakker68884e32013-01-07 18:20:04 +01001138 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1139 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140
Paul Bakker68884e32013-01-07 18:20:04 +01001141 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001142 * Encrypt
1143 */
1144 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1145 ssl->transform_out->iv_enc,
1146 ssl->transform_out->ivlen ) ) != 0 ||
1147 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1148 {
1149 return( ret );
1150 }
1151
1152 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1153 add_data, 13 ) ) != 0 )
1154 {
1155 return( ret );
1156 }
1157
1158 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1159 enc_msg, enc_msglen,
1160 enc_msg, &olen ) ) != 0 )
1161 {
1162 return( ret );
1163 }
1164 totlen = olen;
1165
1166 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1167 enc_msg + olen, &olen ) ) != 0 )
1168 {
1169 return( ret );
1170 }
1171 totlen += olen;
1172
1173 if( totlen != enc_msglen )
1174 {
1175 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1176 return( -1 );
1177 }
1178
1179 /*
1180 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001181 */
1182 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001183
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001184 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1185 enc_msg + enc_msglen, 16 ) ) != 0 )
1186 {
1187 return( ret );
1188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001190 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001191 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 else
Paul Bakker68884e32013-01-07 18:20:04 +01001193#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001194#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1195 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001196 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1197 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001199 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001200 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001201 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001202
Paul Bakker48916f92012-09-16 19:57:18 +00001203 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1204 ssl->transform_out->ivlen;
1205 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 padlen = 0;
1207
1208 for( i = 0; i <= padlen; i++ )
1209 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1210
1211 ssl->out_msglen += padlen + 1;
1212
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 enc_msglen = ssl->out_msglen;
1214 enc_msg = ssl->out_msg;
1215
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001216#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001217 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001218 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1219 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001221 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001222 {
1223 /*
1224 * Generate IV
1225 */
Paul Bakker48916f92012-09-16 19:57:18 +00001226 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1227 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001228 if( ret != 0 )
1229 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230
Paul Bakker92be97b2013-01-02 17:30:03 +01001231 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001232 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233
1234 /*
1235 * Fix pointer positions and message length with added IV
1236 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001237 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001239 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001241#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001244 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001245 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
1247 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001248 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Paul Bakker45125bc2013-09-04 16:47:11 +02001250 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001252 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1253 return( ret );
1254 }
1255
1256 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1257 ssl->transform_out->iv_enc,
1258 ssl->transform_out->ivlen ) ) != 0 )
1259 {
1260 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001261 return( ret );
1262 }
Paul Bakker68884e32013-01-07 18:20:04 +01001263
Paul Bakkercca5b812013-08-31 17:40:26 +02001264 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1265 enc_msg, enc_msglen, enc_msg,
1266 &olen ) ) != 0 )
1267 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001268 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001269 return( ret );
1270 }
Paul Bakker68884e32013-01-07 18:20:04 +01001271
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001273
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001275 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001276 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001277 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 return( ret );
1279 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001280
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 if( enc_msglen != olen )
1282 {
1283 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1284 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001285 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001286 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001287
1288#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001289 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1290 {
1291 /*
1292 * Save IV in SSL3 and TLS1
1293 */
1294 memcpy( ssl->transform_out->iv_enc,
1295 ssl->transform_out->cipher_ctx_enc.iv,
1296 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001298#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001300 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001301#endif /* POLARSSL_CIPHER_MODE_CBC &&
1302 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001303 {
1304 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1305 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Paul Bakkerca4ab492012-04-18 14:23:57 +00001308 for( i = 8; i > 0; i-- )
1309 if( ++ssl->out_ctr[i - 1] != 0 )
1310 break;
1311
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1313
1314 return( 0 );
1315}
1316
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001317#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001318
Paul Bakker5121ce52009-01-03 21:22:43 +00001319static int ssl_decrypt_buf( ssl_context *ssl )
1320{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001321 size_t i;
1322#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1323 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1324 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1325 size_t padlen = 0, correct = 1;
1326#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
1328 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1329
Paul Bakker48916f92012-09-16 19:57:18 +00001330 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 {
1332 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001333 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001334 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 }
1336
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001337#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001338 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1339 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001340 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001341 int ret;
1342 size_t olen = 0;
1343
Paul Bakker68884e32013-01-07 18:20:04 +01001344 padlen = 0;
1345
Paul Bakker45125bc2013-09-04 16:47:11 +02001346 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001347 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001348 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1349 return( ret );
1350 }
1351
1352 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1353 ssl->transform_in->iv_dec,
1354 ssl->transform_in->ivlen ) ) != 0 )
1355 {
1356 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001357 return( ret );
1358 }
1359
1360 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1361 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1362 &olen ) ) != 0 )
1363 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001364 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001365 return( ret );
1366 }
1367
1368 if( ssl->in_msglen != olen )
1369 {
1370 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001371 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001372 }
1373
1374 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001375 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001376 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001377 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001378 return( ret );
1379 }
1380
1381 if( 0 != olen )
1382 {
1383 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001384 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 }
Paul Bakker68884e32013-01-07 18:20:04 +01001387 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001388#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001389#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001390 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1391 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001392 {
1393 unsigned char *dec_msg;
1394 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001395 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001396 unsigned char add_data[13];
1397 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1398
Paul Bakker68884e32013-01-07 18:20:04 +01001399 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1400 ssl->transform_in->fixed_ivlen );
1401 dec_msglen -= 16;
1402 dec_msg = ssl->in_msg;
1403 dec_msg_result = ssl->in_msg;
1404 ssl->in_msglen = dec_msglen;
1405
1406 memcpy( add_data, ssl->in_ctr, 8 );
1407 add_data[8] = ssl->in_msgtype;
1408 add_data[9] = ssl->major_ver;
1409 add_data[10] = ssl->minor_ver;
1410 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1411 add_data[12] = ssl->in_msglen & 0xFF;
1412
1413 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1414 add_data, 13 );
1415
1416 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1417 ssl->in_iv,
1418 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1419
1420 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1421 ssl->transform_in->ivlen );
1422 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1423
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001424 /*
1425 * Decrypt
1426 */
1427 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1428 ssl->transform_in->iv_dec,
1429 ssl->transform_in->ivlen ) ) != 0 ||
1430 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001431 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001432 return( ret );
1433 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001434
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001435 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1436 add_data, 13 ) ) != 0 )
1437 {
1438 return( ret );
1439 }
1440
1441 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1442 dec_msg, dec_msglen,
1443 dec_msg_result, &olen ) ) != 0 )
1444 {
1445 return( ret );
1446 }
1447 totlen = olen;
1448
1449 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1450 dec_msg_result + olen, &olen ) ) != 0 )
1451 {
1452 return( ret );
1453 }
1454 totlen += olen;
1455
1456 if( totlen != dec_msglen )
1457 {
1458 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1459 return( -1 );
1460 }
1461
1462 /*
1463 * Authenticate
1464 */
1465 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1466 dec_msg + dec_msglen, 16 ) ) != 0 )
1467 {
1468 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001469 return( POLARSSL_ERR_SSL_INVALID_MAC );
1470 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001471
Paul Bakkerca4ab492012-04-18 14:23:57 +00001472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 else
Paul Bakker68884e32013-01-07 18:20:04 +01001474#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001475#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1476 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001477 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1478 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001479 {
Paul Bakker45829992013-01-03 14:52:21 +01001480 /*
1481 * Decrypt and check the padding
1482 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001483 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001484 unsigned char *dec_msg;
1485 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001486 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001487 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001488 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001489
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 /*
Paul Bakker45829992013-01-03 14:52:21 +01001491 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 */
Paul Bakker48916f92012-09-16 19:57:18 +00001493 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 {
1495 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001496 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001497 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 }
1499
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001500#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001501 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1502 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001503#endif
Paul Bakker45829992013-01-03 14:52:21 +01001504
1505 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1506 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1507 {
1508 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1509 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1510 return( POLARSSL_ERR_SSL_INVALID_MAC );
1511 }
1512
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001513 dec_msglen = ssl->in_msglen;
1514 dec_msg = ssl->in_msg;
1515 dec_msg_result = ssl->in_msg;
1516
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001517#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001518 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001519 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001520 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001521 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001522 {
Paul Bakker48916f92012-09-16 19:57:18 +00001523 dec_msglen -= ssl->transform_in->ivlen;
1524 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001525
Paul Bakker48916f92012-09-16 19:57:18 +00001526 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001527 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001528 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001529#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001530
Paul Bakker45125bc2013-09-04 16:47:11 +02001531 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001533 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1534 return( ret );
1535 }
1536
1537 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1538 ssl->transform_in->iv_dec,
1539 ssl->transform_in->ivlen ) ) != 0 )
1540 {
1541 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001542 return( ret );
1543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
Paul Bakkercca5b812013-08-31 17:40:26 +02001545 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1546 dec_msg, dec_msglen, dec_msg_result,
1547 &olen ) ) != 0 )
1548 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001549 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001550 return( ret );
1551 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001552
Paul Bakkercca5b812013-08-31 17:40:26 +02001553 dec_msglen -= olen;
1554 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001555 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001556 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001557 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001558 return( ret );
1559 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001560
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 if( dec_msglen != olen )
1562 {
1563 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001564 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
1567#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001568 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1569 {
1570 /*
1571 * Save IV in SSL3 and TLS1
1572 */
1573 memcpy( ssl->transform_in->iv_dec,
1574 ssl->transform_in->cipher_ctx_dec.iv,
1575 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
1579 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001580
1581 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1582 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001583#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001584 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1585 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001586#endif
Paul Bakker45829992013-01-03 14:52:21 +01001587 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001588 correct = 0;
1589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001590
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001591#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1593 {
Paul Bakker48916f92012-09-16 19:57:18 +00001594 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001596#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1598 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001599 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001600#endif
Paul Bakker45829992013-01-03 14:52:21 +01001601 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 }
1603 }
1604 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001605#endif
1606#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1607 defined(POLARSSL_SSL_PROTO_TLS1_2)
1608 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 {
1610 /*
Paul Bakker45829992013-01-03 14:52:21 +01001611 * TLSv1+: always check the padding up to the first failure
1612 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001614 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001615 size_t padding_idx = ssl->in_msglen - padlen - 1;
1616
Paul Bakker956c9e02013-12-19 14:42:28 +01001617 /*
1618 * Padding is guaranteed to be incorrect if:
1619 * 1. padlen - 1 > ssl->in_msglen
1620 *
1621 * 2. ssl->in_msglen + padlen >
1622 * SSL_MAX_CONTENT_LEN + 256 (max padding)
1623 *
1624 * In both cases we reset padding_idx to a safe value (0) to
1625 * prevent out-of-buffer reads.
1626 */
1627 correct &= ( ssl->in_msglen >= padlen - 1 );
1628 correct &= ( ssl->in_msglen + padlen <= SSL_MAX_CONTENT_LEN + 256 );
1629
1630 padding_idx *= correct;
1631
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001632 for( i = 1; i <= 256; i++ )
1633 {
1634 real_count &= ( i <= padlen );
1635 pad_count += real_count *
1636 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1637 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001638
1639 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001640
Paul Bakkerd66f0702013-01-31 16:57:45 +01001641#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001642 if( padlen > 0 && correct == 0)
1643 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001644#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001645 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001647 else
1648#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1649 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001650 {
1651 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001652 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001655 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001656#endif /* POLARSSL_CIPHER_MODE_CBC &&
1657 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001658 {
1659 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1660 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1661 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
1663 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1664 ssl->in_msg, ssl->in_msglen );
1665
1666 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001667 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1670 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1671 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1672 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1673 POLARSSL_MODE_GCM )
1674 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001675 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1676
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001677 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1680 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001684#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001685 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1686 {
1687 ssl_mac( &ssl->transform_in->md_ctx_dec,
1688 ssl->transform_in->mac_dec,
1689 ssl->in_msg, ssl->in_msglen,
1690 ssl->in_ctr, ssl->in_msgtype );
1691 }
1692 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001693#endif /* POLARSSL_SSL_PROTO_SSL3 */
1694#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001695 defined(POLARSSL_SSL_PROTO_TLS1_2)
1696 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1697 {
1698 /*
1699 * Process MAC and always update for padlen afterwards to make
1700 * total time independent of padlen
1701 *
1702 * extra_run compensates MAC check for padlen
1703 *
1704 * Known timing attacks:
1705 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1706 *
1707 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1708 * correctly. (We round down instead of up, so -56 is the correct
1709 * value for our calculations instead of -55)
1710 */
1711 size_t j, extra_run = 0;
1712 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1713 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001714
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001715 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001716
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001717 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1718 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1719 ssl->in_msglen );
1720 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1721 ssl->in_msg + ssl->in_msglen );
1722 for( j = 0; j < extra_run; j++ )
1723 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001724
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1726 }
1727 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001728#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001729 POLARSSL_SSL_PROTO_TLS1_2 */
1730 {
1731 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1732 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1733 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001735 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1736 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1737 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001739 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001740 ssl->transform_in->maclen ) != 0 )
1741 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001742#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001743 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001744#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001745 correct = 0;
1746 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001748 /*
1749 * Finally check the correct flag
1750 */
1751 if( correct == 0 )
1752 return( POLARSSL_ERR_SSL_INVALID_MAC );
1753 }
1754#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
1756 if( ssl->in_msglen == 0 )
1757 {
1758 ssl->nb_zero++;
1759
1760 /*
1761 * Three or more empty messages may be a DoS attack
1762 * (excessive CPU consumption).
1763 */
1764 if( ssl->nb_zero > 3 )
1765 {
1766 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1767 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001768 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 }
1770 }
1771 else
1772 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001773
Paul Bakker23986e52011-04-24 08:57:21 +00001774 for( i = 8; i > 0; i-- )
1775 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 break;
1777
1778 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1779
1780 return( 0 );
1781}
1782
Paul Bakker2770fbd2012-07-03 13:30:23 +00001783#if defined(POLARSSL_ZLIB_SUPPORT)
1784/*
1785 * Compression/decompression functions
1786 */
1787static int ssl_compress_buf( ssl_context *ssl )
1788{
1789 int ret;
1790 unsigned char *msg_post = ssl->out_msg;
1791 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001792 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001793
1794 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1795
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001796 if( len_pre == 0 )
1797 return( 0 );
1798
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799 memcpy( msg_pre, ssl->out_msg, len_pre );
1800
1801 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1802 ssl->out_msglen ) );
1803
1804 SSL_DEBUG_BUF( 4, "before compression: output payload",
1805 ssl->out_msg, ssl->out_msglen );
1806
Paul Bakker48916f92012-09-16 19:57:18 +00001807 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1808 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1809 ssl->transform_out->ctx_deflate.next_out = msg_post;
1810 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811
Paul Bakker48916f92012-09-16 19:57:18 +00001812 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001813 if( ret != Z_OK )
1814 {
1815 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1816 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1817 }
1818
Paul Bakker48916f92012-09-16 19:57:18 +00001819 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001820
Paul Bakker2770fbd2012-07-03 13:30:23 +00001821 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1822 ssl->out_msglen ) );
1823
1824 SSL_DEBUG_BUF( 4, "after compression: output payload",
1825 ssl->out_msg, ssl->out_msglen );
1826
1827 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1828
1829 return( 0 );
1830}
1831
1832static int ssl_decompress_buf( ssl_context *ssl )
1833{
1834 int ret;
1835 unsigned char *msg_post = ssl->in_msg;
1836 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001837 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001838
1839 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1840
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001841 if( len_pre == 0 )
1842 return( 0 );
1843
Paul Bakker2770fbd2012-07-03 13:30:23 +00001844 memcpy( msg_pre, ssl->in_msg, len_pre );
1845
1846 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1847 ssl->in_msglen ) );
1848
1849 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1850 ssl->in_msg, ssl->in_msglen );
1851
Paul Bakker48916f92012-09-16 19:57:18 +00001852 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1853 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1854 ssl->transform_in->ctx_inflate.next_out = msg_post;
1855 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001856
Paul Bakker48916f92012-09-16 19:57:18 +00001857 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001858 if( ret != Z_OK )
1859 {
1860 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1861 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1862 }
1863
Paul Bakker48916f92012-09-16 19:57:18 +00001864 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001865
Paul Bakker2770fbd2012-07-03 13:30:23 +00001866 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1867 ssl->in_msglen ) );
1868
1869 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1870 ssl->in_msg, ssl->in_msglen );
1871
1872 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1873
1874 return( 0 );
1875}
1876#endif /* POLARSSL_ZLIB_SUPPORT */
1877
Paul Bakker5121ce52009-01-03 21:22:43 +00001878/*
1879 * Fill the input message buffer
1880 */
Paul Bakker23986e52011-04-24 08:57:21 +00001881int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001882{
Paul Bakker23986e52011-04-24 08:57:21 +00001883 int ret;
1884 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
1886 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1887
1888 while( ssl->in_left < nb_want )
1889 {
1890 len = nb_want - ssl->in_left;
1891 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1892
1893 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1894 ssl->in_left, nb_want ) );
1895 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1896
Paul Bakker831a7552011-05-18 13:32:51 +00001897 if( ret == 0 )
1898 return( POLARSSL_ERR_SSL_CONN_EOF );
1899
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 if( ret < 0 )
1901 return( ret );
1902
1903 ssl->in_left += ret;
1904 }
1905
1906 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1907
1908 return( 0 );
1909}
1910
1911/*
1912 * Flush any data not yet written
1913 */
1914int ssl_flush_output( ssl_context *ssl )
1915{
1916 int ret;
1917 unsigned char *buf;
1918
1919 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1920
1921 while( ssl->out_left > 0 )
1922 {
1923 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1924 5 + ssl->out_msglen, ssl->out_left ) );
1925
Paul Bakker5bd42292012-12-19 14:40:42 +01001926 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001928
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1930
1931 if( ret <= 0 )
1932 return( ret );
1933
1934 ssl->out_left -= ret;
1935 }
1936
1937 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1938
1939 return( 0 );
1940}
1941
1942/*
1943 * Record layer functions
1944 */
1945int ssl_write_record( ssl_context *ssl )
1946{
Paul Bakker05ef8352012-05-08 09:17:57 +00001947 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001948 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
1950 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1951
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1953 {
1954 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1955 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1956 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1957
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001958 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1959 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 }
1961
Paul Bakker2770fbd2012-07-03 13:30:23 +00001962#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001963 if( ssl->transform_out != NULL &&
1964 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001965 {
1966 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1967 {
1968 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1969 return( ret );
1970 }
1971
1972 len = ssl->out_msglen;
1973 }
1974#endif /*POLARSSL_ZLIB_SUPPORT */
1975
Paul Bakker05ef8352012-05-08 09:17:57 +00001976#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1977 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001979 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001980
Paul Bakker05ef8352012-05-08 09:17:57 +00001981 ret = ssl_hw_record_write( ssl );
1982 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1983 {
1984 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1985 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1986 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001987
1988 if( ret == 0 )
1989 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001990 }
1991#endif
1992 if( !done )
1993 {
1994 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1995 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1996 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1998 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001999
Paul Bakker48916f92012-09-16 19:57:18 +00002000 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002001 {
2002 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2003 {
2004 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2005 return( ret );
2006 }
2007
2008 len = ssl->out_msglen;
2009 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2010 ssl->out_hdr[4] = (unsigned char)( len );
2011 }
2012
2013 ssl->out_left = 5 + ssl->out_msglen;
2014
2015 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2016 "version = [%d:%d], msglen = %d",
2017 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2018 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2019
2020 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002021 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 }
2023
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2025 {
2026 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2027 return( ret );
2028 }
2029
2030 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2031
2032 return( 0 );
2033}
2034
2035int ssl_read_record( ssl_context *ssl )
2036{
Paul Bakker05ef8352012-05-08 09:17:57 +00002037 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002038
2039 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2040
Paul Bakker68884e32013-01-07 18:20:04 +01002041 SSL_DEBUG_BUF( 4, "input record from network",
2042 ssl->in_hdr, 5 + ssl->in_msglen );
2043
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 if( ssl->in_hslen != 0 &&
2045 ssl->in_hslen < ssl->in_msglen )
2046 {
2047 /*
2048 * Get next Handshake message in the current record
2049 */
2050 ssl->in_msglen -= ssl->in_hslen;
2051
Paul Bakker8934a982011-08-05 11:11:53 +00002052 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002053 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002054
2055 ssl->in_hslen = 4;
2056 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2057
2058 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2059 " %d, type = %d, hslen = %d",
2060 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2061
2062 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2063 {
2064 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002065 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 }
2067
2068 if( ssl->in_msglen < ssl->in_hslen )
2069 {
2070 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002071 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 }
2073
Paul Bakker48916f92012-09-16 19:57:18 +00002074 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002075
2076 return( 0 );
2077 }
2078
2079 ssl->in_hslen = 0;
2080
2081 /*
2082 * Read the record header and validate it
2083 */
2084 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2085 {
2086 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2087 return( ret );
2088 }
2089
2090 ssl->in_msgtype = ssl->in_hdr[0];
2091 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2092
2093 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2094 "version = [%d:%d], msglen = %d",
2095 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2096 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2097
2098 if( ssl->in_hdr[1] != ssl->major_ver )
2099 {
2100 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002101 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 }
2103
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002104 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 {
2106 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002107 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
2109
2110 /*
2111 * Make sure the message length is acceptable
2112 */
Paul Bakker48916f92012-09-16 19:57:18 +00002113 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 {
2115 if( ssl->in_msglen < 1 ||
2116 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2117 {
2118 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002119 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 }
2121 }
2122 else
2123 {
Paul Bakker48916f92012-09-16 19:57:18 +00002124 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 {
2126 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002127 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
2129
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002130#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002132 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 {
2134 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002135 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002137#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002139#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2140 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 /*
2142 * TLS encrypted messages can have up to 256 bytes of padding
2143 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002144 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002145 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 {
2147 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002148 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 }
2152
2153 /*
2154 * Read and optionally decrypt the message contents
2155 */
2156 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2157 {
2158 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2159 return( ret );
2160 }
2161
2162 SSL_DEBUG_BUF( 4, "input record from network",
2163 ssl->in_hdr, 5 + ssl->in_msglen );
2164
Paul Bakker05ef8352012-05-08 09:17:57 +00002165#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2166 if( ssl_hw_record_read != NULL)
2167 {
2168 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2169
2170 ret = ssl_hw_record_read( ssl );
2171 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2172 {
2173 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2174 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2175 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002176
2177 if( ret == 0 )
2178 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002179 }
2180#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002181 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 {
2183 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2184 {
Paul Bakker40865c82013-01-31 17:13:13 +01002185#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2186 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2187 {
2188 ssl_send_alert_message( ssl,
2189 SSL_ALERT_LEVEL_FATAL,
2190 SSL_ALERT_MSG_BAD_RECORD_MAC );
2191 }
2192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2194 return( ret );
2195 }
2196
2197 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2198 ssl->in_msg, ssl->in_msglen );
2199
2200 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002203 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 }
2205 }
2206
Paul Bakker2770fbd2012-07-03 13:30:23 +00002207#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002208 if( ssl->transform_in != NULL &&
2209 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002210 {
2211 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2212 {
2213 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2214 return( ret );
2215 }
2216
2217 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2218 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2219 }
2220#endif /* POLARSSL_ZLIB_SUPPORT */
2221
Paul Bakker0a925182012-04-16 06:46:41 +00002222 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2223 ssl->in_msgtype != SSL_MSG_ALERT &&
2224 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2225 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2226 {
2227 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2228
Paul Bakker48916f92012-09-16 19:57:18 +00002229 if( ( ret = ssl_send_alert_message( ssl,
2230 SSL_ALERT_LEVEL_FATAL,
2231 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002232 {
2233 return( ret );
2234 }
2235
2236 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2237 }
2238
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2240 {
2241 ssl->in_hslen = 4;
2242 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2243
2244 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2245 " %d, type = %d, hslen = %d",
2246 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2247
2248 /*
2249 * Additional checks to validate the handshake header
2250 */
2251 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2252 {
2253 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002254 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
2256
2257 if( ssl->in_msglen < ssl->in_hslen )
2258 {
2259 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002260 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262
Paul Bakker48916f92012-09-16 19:57:18 +00002263 if( ssl->state != SSL_HANDSHAKE_OVER )
2264 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
2266
2267 if( ssl->in_msgtype == SSL_MSG_ALERT )
2268 {
2269 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2270 ssl->in_msg[0], ssl->in_msg[1] ) );
2271
2272 /*
2273 * Ignore non-fatal alerts, except close_notify
2274 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002275 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002277 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2278 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002279 /**
2280 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2281 * error identifier.
2282 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002283 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 }
2285
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002286 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2287 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 {
2289 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002290 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 }
2292 }
2293
2294 ssl->in_left = 0;
2295
2296 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2297
2298 return( 0 );
2299}
2300
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002301int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2302{
2303 int ret;
2304
2305 if( ( ret = ssl_send_alert_message( ssl,
2306 SSL_ALERT_LEVEL_FATAL,
2307 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2308 {
2309 return( ret );
2310 }
2311
2312 return( 0 );
2313}
2314
Paul Bakker0a925182012-04-16 06:46:41 +00002315int ssl_send_alert_message( ssl_context *ssl,
2316 unsigned char level,
2317 unsigned char message )
2318{
2319 int ret;
2320
2321 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2322
2323 ssl->out_msgtype = SSL_MSG_ALERT;
2324 ssl->out_msglen = 2;
2325 ssl->out_msg[0] = level;
2326 ssl->out_msg[1] = message;
2327
2328 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2329 {
2330 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2331 return( ret );
2332 }
2333
2334 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2335
2336 return( 0 );
2337}
2338
Paul Bakker5121ce52009-01-03 21:22:43 +00002339/*
2340 * Handshake functions
2341 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002342#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2343 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2344 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2345 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2346 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2347 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2348 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002349int ssl_write_certificate( ssl_context *ssl )
2350{
Paul Bakkered27a042013-04-18 22:46:23 +02002351 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002352 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
2354 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2355
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002356 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002357 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2358 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002359 {
2360 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2361 ssl->state++;
2362 return( 0 );
2363 }
2364
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002365 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002366 return( ret );
2367}
2368
2369int ssl_parse_certificate( ssl_context *ssl )
2370{
2371 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2372 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2373
2374 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2375
2376 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002377 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2378 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002379 {
2380 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2381 ssl->state++;
2382 return( 0 );
2383 }
2384
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002385 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386 return( ret );
2387}
2388#else
2389int ssl_write_certificate( ssl_context *ssl )
2390{
2391 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2392 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002393 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2395
2396 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2397
2398 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002399 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2400 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002401 {
2402 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2403 ssl->state++;
2404 return( 0 );
2405 }
2406
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 if( ssl->endpoint == SSL_IS_CLIENT )
2408 {
2409 if( ssl->client_auth == 0 )
2410 {
2411 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2412 ssl->state++;
2413 return( 0 );
2414 }
2415
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002416#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 /*
2418 * If using SSLv3 and got no cert, send an Alert message
2419 * (otherwise an empty Certificate message will be sent).
2420 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002421 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2423 {
2424 ssl->out_msglen = 2;
2425 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2427 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
2429 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2430 goto write_msg;
2431 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002432#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 }
2434 else /* SSL_IS_SERVER */
2435 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002436 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 {
2438 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002439 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 }
2441 }
2442
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002443 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 /*
2446 * 0 . 0 handshake type
2447 * 1 . 3 handshake length
2448 * 4 . 6 length of all certs
2449 * 7 . 9 length of cert. 1
2450 * 10 . n-1 peer certificate
2451 * n . n+2 length of cert. 2
2452 * n+3 . ... upper level cert, etc.
2453 */
2454 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002455 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Paul Bakker29087132010-03-21 21:03:34 +00002457 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 {
2459 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002460 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 {
2462 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2463 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002464 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 }
2466
2467 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2468 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2469 ssl->out_msg[i + 2] = (unsigned char)( n );
2470
2471 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2472 i += n; crt = crt->next;
2473 }
2474
2475 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2476 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2477 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2478
2479 ssl->out_msglen = i;
2480 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2481 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2482
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002483#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002484write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002485#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
2487 ssl->state++;
2488
2489 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2490 {
2491 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2492 return( ret );
2493 }
2494
2495 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2496
Paul Bakkered27a042013-04-18 22:46:23 +02002497 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498}
2499
2500int ssl_parse_certificate( ssl_context *ssl )
2501{
Paul Bakkered27a042013-04-18 22:46:23 +02002502 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002503 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002504 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
2506 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2507
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002508 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002509 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002511 {
2512 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2513 ssl->state++;
2514 return( 0 );
2515 }
2516
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002518 ( ssl->authmode == SSL_VERIFY_NONE ||
2519 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002521 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2523 ssl->state++;
2524 return( 0 );
2525 }
2526
2527 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2528 {
2529 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2530 return( ret );
2531 }
2532
2533 ssl->state++;
2534
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 /*
2537 * Check if the client sent an empty certificate
2538 */
2539 if( ssl->endpoint == SSL_IS_SERVER &&
2540 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2541 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002542 if( ssl->in_msglen == 2 &&
2543 ssl->in_msgtype == SSL_MSG_ALERT &&
2544 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2545 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 {
2547 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2548
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002549 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2551 return( 0 );
2552 else
Paul Bakker40e46942009-01-03 21:51:57 +00002553 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002556#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002558#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2559 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 if( ssl->endpoint == SSL_IS_SERVER &&
2561 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2562 {
2563 if( ssl->in_hslen == 7 &&
2564 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2565 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2566 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2567 {
2568 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2569
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002570 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002572 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 else
2574 return( 0 );
2575 }
2576 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002577#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2578 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
2580 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2581 {
2582 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002583 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 }
2585
2586 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2587 {
2588 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002589 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 }
2591
2592 /*
2593 * Same message structure as in ssl_write_certificate()
2594 */
2595 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2596
2597 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2598 {
2599 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002600 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
2602
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002603 /* In case we tried to reuse a session but it failed */
2604 if( ssl->session_negotiate->peer_cert != NULL )
2605 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002606 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002607 polarssl_free( ssl->session_negotiate->peer_cert );
2608 }
2609
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002610 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2611 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 {
2613 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002614 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002615 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 }
2617
Paul Bakkerb6b09562013-09-18 14:17:41 +02002618 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
2620 i = 7;
2621
2622 while( i < ssl->in_hslen )
2623 {
2624 if( ssl->in_msg[i] != 0 )
2625 {
2626 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002627 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 }
2629
2630 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2631 | (unsigned int) ssl->in_msg[i + 2];
2632 i += 3;
2633
2634 if( n < 128 || i + n > ssl->in_hslen )
2635 {
2636 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002637 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 }
2639
Paul Bakkerddf26b42013-09-18 13:46:23 +02002640 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2641 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 if( ret != 0 )
2643 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002644 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 return( ret );
2646 }
2647
2648 i += n;
2649 }
2650
Paul Bakker48916f92012-09-16 19:57:18 +00002651 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002653 /*
2654 * On client, make sure the server cert doesn't change during renego to
2655 * avoid "triple handshake" attack: https://secure-resumption.com/
2656 */
2657 if( ssl->endpoint == SSL_IS_CLIENT &&
2658 ssl->renegotiation == SSL_RENEGOTIATION )
2659 {
2660 if( ssl->session->peer_cert == NULL )
2661 {
2662 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2663 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2664 }
2665
2666 if( ssl->session->peer_cert->raw.len !=
2667 ssl->session_negotiate->peer_cert->raw.len ||
2668 memcmp( ssl->session->peer_cert->raw.p,
2669 ssl->session_negotiate->peer_cert->raw.p,
2670 ssl->session->peer_cert->raw.len ) != 0 )
2671 {
2672 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2673 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2674 }
2675 }
2676
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 if( ssl->authmode != SSL_VERIFY_NONE )
2678 {
2679 if( ssl->ca_chain == NULL )
2680 {
2681 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002682 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 }
2684
Paul Bakkerddf26b42013-09-18 13:46:23 +02002685 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2686 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2687 &ssl->session_negotiate->verify_result,
2688 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
2690 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002691 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002693 }
2694#if defined(POLARSSL_SSL_SET_CURVES)
2695 else
2696 {
2697 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
2698
2699 /* If certificate uses an EC key, make sure the curve is OK */
2700 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2701 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2702 {
2703 SSL_DEBUG_MSG( 1, ( "bad server certificate (EC key curve)" ) );
2704 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2705 }
2706 }
2707#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
2709 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2710 ret = 0;
2711 }
2712
2713 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2714
2715 return( ret );
2716}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002717#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2718 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2719 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2720 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2721 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2722 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2723 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
2725int ssl_write_change_cipher_spec( ssl_context *ssl )
2726{
2727 int ret;
2728
2729 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2730
2731 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2732 ssl->out_msglen = 1;
2733 ssl->out_msg[0] = 1;
2734
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 ssl->state++;
2736
2737 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2738 {
2739 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2740 return( ret );
2741 }
2742
2743 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2744
2745 return( 0 );
2746}
2747
2748int ssl_parse_change_cipher_spec( ssl_context *ssl )
2749{
2750 int ret;
2751
2752 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2753
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2755 {
2756 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2757 return( ret );
2758 }
2759
2760 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2761 {
2762 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002763 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 }
2765
2766 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2767 {
2768 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002769 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 }
2771
2772 ssl->state++;
2773
2774 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2775
2776 return( 0 );
2777}
2778
Paul Bakker41c83d32013-03-20 14:39:14 +01002779void ssl_optimize_checksum( ssl_context *ssl,
2780 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002781{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002782 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002783
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002784#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2785 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002786 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002787 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002788 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002789#endif
2790#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2791#if defined(POLARSSL_SHA512_C)
2792 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2793 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2794 else
2795#endif
2796#if defined(POLARSSL_SHA256_C)
2797 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002798 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002799 else
2800#endif
2801#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2802 /* Should never happen */
2803 return;
Paul Bakker380da532012-04-18 16:10:25 +00002804}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002805
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002806static void ssl_update_checksum_start( ssl_context *ssl,
2807 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002808{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002809#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2810 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002811 md5_update( &ssl->handshake->fin_md5 , buf, len );
2812 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002813#endif
2814#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2815#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002816 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002817#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002818#if defined(POLARSSL_SHA512_C)
2819 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002820#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002821#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002822}
2823
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002824#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2825 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002826static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2827 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002828{
Paul Bakker48916f92012-09-16 19:57:18 +00002829 md5_update( &ssl->handshake->fin_md5 , buf, len );
2830 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002831}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002832#endif
Paul Bakker380da532012-04-18 16:10:25 +00002833
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002834#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2835#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002836static void ssl_update_checksum_sha256( ssl_context *ssl,
2837 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002838{
Paul Bakker9e36f042013-06-30 14:34:05 +02002839 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002840}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002841#endif
Paul Bakker380da532012-04-18 16:10:25 +00002842
Paul Bakker9e36f042013-06-30 14:34:05 +02002843#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002844static void ssl_update_checksum_sha384( ssl_context *ssl,
2845 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002846{
Paul Bakker9e36f042013-06-30 14:34:05 +02002847 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002848}
Paul Bakker769075d2012-11-24 11:26:46 +01002849#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002850#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002851
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002852#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002853static void ssl_calc_finished_ssl(
2854 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002855{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002856 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002857 md5_context md5;
2858 sha1_context sha1;
2859
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 unsigned char padbuf[48];
2861 unsigned char md5sum[16];
2862 unsigned char sha1sum[20];
2863
Paul Bakker48916f92012-09-16 19:57:18 +00002864 ssl_session *session = ssl->session_negotiate;
2865 if( !session )
2866 session = ssl->session;
2867
Paul Bakker1ef83d62012-04-11 12:09:53 +00002868 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2869
Paul Bakker48916f92012-09-16 19:57:18 +00002870 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2871 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
2873 /*
2874 * SSLv3:
2875 * hash =
2876 * MD5( master + pad2 +
2877 * MD5( handshake + sender + master + pad1 ) )
2878 * + SHA1( master + pad2 +
2879 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 */
2881
Paul Bakker90995b52013-06-24 19:20:35 +02002882#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002884 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002885#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
Paul Bakker90995b52013-06-24 19:20:35 +02002887#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002888 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002889 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002890#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakker3c2122f2013-06-24 19:03:14 +02002892 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2893 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
Paul Bakker1ef83d62012-04-11 12:09:53 +00002895 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
Paul Bakker3c2122f2013-06-24 19:03:14 +02002897 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002898 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899 md5_update( &md5, padbuf, 48 );
2900 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
Paul Bakker3c2122f2013-06-24 19:03:14 +02002902 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002903 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002904 sha1_update( &sha1, padbuf, 40 );
2905 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002910 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911 md5_update( &md5, padbuf, 48 );
2912 md5_update( &md5, md5sum, 16 );
2913 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002916 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002917 sha1_update( &sha1, padbuf , 40 );
2918 sha1_update( &sha1, sha1sum, 20 );
2919 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Paul Bakker1ef83d62012-04-11 12:09:53 +00002921 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 memset( &md5, 0, sizeof( md5_context ) );
2924 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
2926 memset( padbuf, 0, sizeof( padbuf ) );
2927 memset( md5sum, 0, sizeof( md5sum ) );
2928 memset( sha1sum, 0, sizeof( sha1sum ) );
2929
2930 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2931}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002932#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002934#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002935static void ssl_calc_finished_tls(
2936 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002937{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002938 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002939 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002942 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Paul Bakker48916f92012-09-16 19:57:18 +00002944 ssl_session *session = ssl->session_negotiate;
2945 if( !session )
2946 session = ssl->session;
2947
Paul Bakker1ef83d62012-04-11 12:09:53 +00002948 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Paul Bakker48916f92012-09-16 19:57:18 +00002950 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2951 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002952
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953 /*
2954 * TLSv1:
2955 * hash = PRF( master, finished_label,
2956 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2957 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002958
Paul Bakker90995b52013-06-24 19:20:35 +02002959#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2961 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002962#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963
Paul Bakker90995b52013-06-24 19:20:35 +02002964#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2966 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002967#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968
2969 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002970 ? "client finished"
2971 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972
2973 md5_finish( &md5, padbuf );
2974 sha1_finish( &sha1, padbuf + 16 );
2975
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002976 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002977 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002978
2979 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2980
2981 memset( &md5, 0, sizeof( md5_context ) );
2982 memset( &sha1, 0, sizeof( sha1_context ) );
2983
2984 memset( padbuf, 0, sizeof( padbuf ) );
2985
2986 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2987}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002988#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002990#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2991#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002992static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002993 ssl_context *ssl, unsigned char *buf, int from )
2994{
2995 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002996 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002997 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002998 unsigned char padbuf[32];
2999
Paul Bakker48916f92012-09-16 19:57:18 +00003000 ssl_session *session = ssl->session_negotiate;
3001 if( !session )
3002 session = ssl->session;
3003
Paul Bakker380da532012-04-18 16:10:25 +00003004 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003005
Paul Bakker9e36f042013-06-30 14:34:05 +02003006 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003007
3008 /*
3009 * TLSv1.2:
3010 * hash = PRF( master, finished_label,
3011 * Hash( handshake ) )[0.11]
3012 */
3013
Paul Bakker9e36f042013-06-30 14:34:05 +02003014#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003015 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003016 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003017#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003018
3019 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003020 ? "client finished"
3021 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003022
Paul Bakker9e36f042013-06-30 14:34:05 +02003023 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003024
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003025 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003026 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003027
3028 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3029
Paul Bakker9e36f042013-06-30 14:34:05 +02003030 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003031
3032 memset( padbuf, 0, sizeof( padbuf ) );
3033
3034 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3035}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003036#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037
Paul Bakker9e36f042013-06-30 14:34:05 +02003038#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003039static void ssl_calc_finished_tls_sha384(
3040 ssl_context *ssl, unsigned char *buf, int from )
3041{
3042 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003043 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003044 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003045 unsigned char padbuf[48];
3046
Paul Bakker48916f92012-09-16 19:57:18 +00003047 ssl_session *session = ssl->session_negotiate;
3048 if( !session )
3049 session = ssl->session;
3050
Paul Bakker380da532012-04-18 16:10:25 +00003051 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003052
Paul Bakker9e36f042013-06-30 14:34:05 +02003053 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003054
3055 /*
3056 * TLSv1.2:
3057 * hash = PRF( master, finished_label,
3058 * Hash( handshake ) )[0.11]
3059 */
3060
Paul Bakker9e36f042013-06-30 14:34:05 +02003061#if !defined(POLARSSL_SHA512_ALT)
3062 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3063 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003064#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003065
3066 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003067 ? "client finished"
3068 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003069
Paul Bakker9e36f042013-06-30 14:34:05 +02003070 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003071
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003072 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003073 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003074
3075 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3076
Paul Bakker9e36f042013-06-30 14:34:05 +02003077 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003078
3079 memset( padbuf, 0, sizeof( padbuf ) );
3080
3081 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3082}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003083#endif /* POLARSSL_SHA512_C */
3084#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003085
Paul Bakker48916f92012-09-16 19:57:18 +00003086void ssl_handshake_wrapup( ssl_context *ssl )
3087{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003088 int resume = ssl->handshake->resume;
3089
Paul Bakker48916f92012-09-16 19:57:18 +00003090 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3091
3092 /*
3093 * Free our handshake params
3094 */
3095 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003096 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003097 ssl->handshake = NULL;
3098
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003099 if( ssl->renegotiation == SSL_RENEGOTIATION )
3100 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3101
Paul Bakker48916f92012-09-16 19:57:18 +00003102 /*
3103 * Switch in our now active transform context
3104 */
3105 if( ssl->transform )
3106 {
3107 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003108 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003109 }
3110 ssl->transform = ssl->transform_negotiate;
3111 ssl->transform_negotiate = NULL;
3112
Paul Bakker0a597072012-09-25 21:55:46 +00003113 if( ssl->session )
3114 {
3115 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003116 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003117 }
3118 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003119 ssl->session_negotiate = NULL;
3120
Paul Bakker0a597072012-09-25 21:55:46 +00003121 /*
3122 * Add cache entry
3123 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003124 if( ssl->f_set_cache != NULL &&
3125 ssl->session->length != 0 &&
3126 resume == 0 )
3127 {
Paul Bakker0a597072012-09-25 21:55:46 +00003128 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3129 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003130 }
Paul Bakker0a597072012-09-25 21:55:46 +00003131
Paul Bakker48916f92012-09-16 19:57:18 +00003132 ssl->state++;
3133
3134 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3135}
3136
Paul Bakker1ef83d62012-04-11 12:09:53 +00003137int ssl_write_finished( ssl_context *ssl )
3138{
3139 int ret, hash_len;
3140
3141 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3142
Paul Bakker92be97b2013-01-02 17:30:03 +01003143 /*
3144 * Set the out_msg pointer to the correct location based on IV length
3145 */
3146 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3147 {
3148 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3149 ssl->transform_negotiate->fixed_ivlen;
3150 }
3151 else
3152 ssl->out_msg = ssl->out_iv;
3153
Paul Bakker48916f92012-09-16 19:57:18 +00003154 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155
3156 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003157 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3158
Paul Bakker48916f92012-09-16 19:57:18 +00003159 ssl->verify_data_len = hash_len;
3160 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3161
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 ssl->out_msglen = 4 + hash_len;
3163 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3164 ssl->out_msg[0] = SSL_HS_FINISHED;
3165
3166 /*
3167 * In case of session resuming, invert the client and server
3168 * ChangeCipherSpec messages order.
3169 */
Paul Bakker0a597072012-09-25 21:55:46 +00003170 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003171 {
3172 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003173 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 else
3175 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3176 }
3177 else
3178 ssl->state++;
3179
Paul Bakker48916f92012-09-16 19:57:18 +00003180 /*
3181 * Switch to our negotiated transform and session parameters for outbound data.
3182 */
3183 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3184 ssl->transform_out = ssl->transform_negotiate;
3185 ssl->session_out = ssl->session_negotiate;
3186 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003187
Paul Bakker07eb38b2012-12-19 14:42:06 +01003188#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3189 if( ssl_hw_record_activate != NULL)
3190 {
3191 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3192 {
3193 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3194 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3195 }
3196 }
3197#endif
3198
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3200 {
3201 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3202 return( ret );
3203 }
3204
3205 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3206
3207 return( 0 );
3208}
3209
3210int ssl_parse_finished( ssl_context *ssl )
3211{
Paul Bakker23986e52011-04-24 08:57:21 +00003212 int ret;
3213 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 unsigned char buf[36];
3215
3216 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3217
Paul Bakker48916f92012-09-16 19:57:18 +00003218 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003219
Paul Bakker48916f92012-09-16 19:57:18 +00003220 /*
3221 * Switch to our negotiated transform and session parameters for inbound data.
3222 */
3223 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3224 ssl->transform_in = ssl->transform_negotiate;
3225 ssl->session_in = ssl->session_negotiate;
3226 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003227
Paul Bakker92be97b2013-01-02 17:30:03 +01003228 /*
3229 * Set the in_msg pointer to the correct location based on IV length
3230 */
3231 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3232 {
3233 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3234 ssl->transform_negotiate->fixed_ivlen;
3235 }
3236 else
3237 ssl->in_msg = ssl->in_iv;
3238
Paul Bakker07eb38b2012-12-19 14:42:06 +01003239#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3240 if( ssl_hw_record_activate != NULL)
3241 {
3242 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3243 {
3244 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3245 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3246 }
3247 }
3248#endif
3249
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3251 {
3252 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3253 return( ret );
3254 }
3255
3256 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3257 {
3258 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003259 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 }
3261
Paul Bakker1ef83d62012-04-11 12:09:53 +00003262 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003263 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3264
3265 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3266 ssl->in_hslen != 4 + hash_len )
3267 {
3268 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003269 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 }
3271
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003272 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 {
3274 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003275 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 }
3277
Paul Bakker48916f92012-09-16 19:57:18 +00003278 ssl->verify_data_len = hash_len;
3279 memcpy( ssl->peer_verify_data, buf, hash_len );
3280
Paul Bakker0a597072012-09-25 21:55:46 +00003281 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 {
3283 if( ssl->endpoint == SSL_IS_CLIENT )
3284 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3285
3286 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003287 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 }
3289 else
3290 ssl->state++;
3291
3292 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3293
3294 return( 0 );
3295}
3296
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003297static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003298{
3299 if( ssl->transform_negotiate )
3300 ssl_transform_free( ssl->transform_negotiate );
3301 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003302 {
3303 ssl->transform_negotiate =
3304 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3305 }
Paul Bakker48916f92012-09-16 19:57:18 +00003306
3307 if( ssl->session_negotiate )
3308 ssl_session_free( ssl->session_negotiate );
3309 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003310 {
3311 ssl->session_negotiate =
3312 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3313 }
Paul Bakker48916f92012-09-16 19:57:18 +00003314
3315 if( ssl->handshake )
3316 ssl_handshake_free( ssl->handshake );
3317 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003318 {
3319 ssl->handshake = (ssl_handshake_params *)
3320 polarssl_malloc( sizeof(ssl_handshake_params) );
3321 }
Paul Bakker48916f92012-09-16 19:57:18 +00003322
3323 if( ssl->handshake == NULL ||
3324 ssl->transform_negotiate == NULL ||
3325 ssl->session_negotiate == NULL )
3326 {
3327 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3328 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3329 }
3330
3331 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3332 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3333 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3334
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003335#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3336 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003337 md5_starts( &ssl->handshake->fin_md5 );
3338 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003339#endif
3340#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3341#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003342 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003343#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003344#if defined(POLARSSL_SHA512_C)
3345 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003346#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003347#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003348
3349 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003350 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003351
Paul Bakker61d113b2013-07-04 11:51:43 +02003352#if defined(POLARSSL_ECDH_C)
3353 ecdh_init( &ssl->handshake->ecdh_ctx );
3354#endif
3355
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003356#if defined(POLARSSL_X509_CRT_PARSE_C)
3357 ssl->handshake->key_cert = ssl->key_cert;
3358#endif
3359
Paul Bakker48916f92012-09-16 19:57:18 +00003360 return( 0 );
3361}
3362
Paul Bakker5121ce52009-01-03 21:22:43 +00003363/*
3364 * Initialize an SSL context
3365 */
3366int ssl_init( ssl_context *ssl )
3367{
Paul Bakker48916f92012-09-16 19:57:18 +00003368 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 int len = SSL_BUFFER_LEN;
3370
3371 memset( ssl, 0, sizeof( ssl_context ) );
3372
Paul Bakker62f2dee2012-09-28 07:31:51 +00003373 /*
3374 * Sane defaults
3375 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003376 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3377 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3378 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3379 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003380
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003381 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003382
Paul Bakker62f2dee2012-09-28 07:31:51 +00003383#if defined(POLARSSL_DHM_C)
3384 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3385 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3386 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3387 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3388 {
3389 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3390 return( ret );
3391 }
3392#endif
3393
3394 /*
3395 * Prepare base structures
3396 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003397 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003399 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 ssl->in_msg = ssl->in_ctr + 13;
3401
3402 if( ssl->in_ctr == NULL )
3403 {
3404 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003405 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003406 }
3407
Paul Bakker6e339b52013-07-03 13:37:05 +02003408 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003409 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003410 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003411 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003412
3413 if( ssl->out_ctr == NULL )
3414 {
3415 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003416 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003417 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003418 }
3419
3420 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3421 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3422
Paul Bakker606b4ba2013-08-14 16:52:14 +02003423#if defined(POLARSSL_SSL_SESSION_TICKETS)
3424 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3425#endif
3426
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003427#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003428 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003429#endif
3430
Paul Bakker48916f92012-09-16 19:57:18 +00003431 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3432 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003433
3434 return( 0 );
3435}
3436
3437/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003438 * Reset an initialized and used SSL context for re-use while retaining
3439 * all application-set variables, function pointers and data.
3440 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003441int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003442{
Paul Bakker48916f92012-09-16 19:57:18 +00003443 int ret;
3444
Paul Bakker7eb013f2011-10-06 12:37:39 +00003445 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003446 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3447 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3448
3449 ssl->verify_data_len = 0;
3450 memset( ssl->own_verify_data, 0, 36 );
3451 memset( ssl->peer_verify_data, 0, 36 );
3452
Paul Bakker7eb013f2011-10-06 12:37:39 +00003453 ssl->in_offt = NULL;
3454
Paul Bakker92be97b2013-01-02 17:30:03 +01003455 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003456 ssl->in_msgtype = 0;
3457 ssl->in_msglen = 0;
3458 ssl->in_left = 0;
3459
3460 ssl->in_hslen = 0;
3461 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003462 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003463
Paul Bakker92be97b2013-01-02 17:30:03 +01003464 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003465 ssl->out_msgtype = 0;
3466 ssl->out_msglen = 0;
3467 ssl->out_left = 0;
3468
Paul Bakker48916f92012-09-16 19:57:18 +00003469 ssl->transform_in = NULL;
3470 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003471
3472 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3473 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003474
3475#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3476 if( ssl_hw_record_reset != NULL)
3477 {
3478 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003479 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003480 {
3481 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3482 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3483 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003484 }
3485#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003486
Paul Bakker48916f92012-09-16 19:57:18 +00003487 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003488 {
Paul Bakker48916f92012-09-16 19:57:18 +00003489 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003490 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003491 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003492 }
Paul Bakker48916f92012-09-16 19:57:18 +00003493
Paul Bakkerc0463502013-02-14 11:19:38 +01003494 if( ssl->session )
3495 {
3496 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003497 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003498 ssl->session = NULL;
3499 }
3500
Paul Bakker48916f92012-09-16 19:57:18 +00003501 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3502 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003503
3504 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003505}
3506
Paul Bakkera503a632013-08-14 13:48:06 +02003507#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003508/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003509 * Allocate and initialize ticket keys
3510 */
3511static int ssl_ticket_keys_init( ssl_context *ssl )
3512{
3513 int ret;
3514 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003515 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003516
3517 if( ssl->ticket_keys != NULL )
3518 return( 0 );
3519
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003520 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3521 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003522 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3523
3524 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003525 {
3526 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003527 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003528 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003529
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003530 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3531 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3532 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3533 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003534 polarssl_free( tkeys );
3535 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003536 }
3537
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003538 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003539 {
3540 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003541 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003542 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003543
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003544 ssl->ticket_keys = tkeys;
3545
3546 return( 0 );
3547}
Paul Bakkera503a632013-08-14 13:48:06 +02003548#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003549
3550/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 * SSL set accessors
3552 */
3553void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3554{
3555 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003556
Paul Bakker606b4ba2013-08-14 16:52:14 +02003557#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003558 if( endpoint == SSL_IS_CLIENT )
3559 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003560#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003561}
3562
3563void ssl_set_authmode( ssl_context *ssl, int authmode )
3564{
3565 ssl->authmode = authmode;
3566}
3567
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003568#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003569void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003570 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003571 void *p_vrfy )
3572{
3573 ssl->f_vrfy = f_vrfy;
3574 ssl->p_vrfy = p_vrfy;
3575}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003576#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003577
Paul Bakker5121ce52009-01-03 21:22:43 +00003578void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003579 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 void *p_rng )
3581{
3582 ssl->f_rng = f_rng;
3583 ssl->p_rng = p_rng;
3584}
3585
3586void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003587 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003588 void *p_dbg )
3589{
3590 ssl->f_dbg = f_dbg;
3591 ssl->p_dbg = p_dbg;
3592}
3593
3594void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003595 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003596 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003597{
3598 ssl->f_recv = f_recv;
3599 ssl->f_send = f_send;
3600 ssl->p_recv = p_recv;
3601 ssl->p_send = p_send;
3602}
3603
Paul Bakker0a597072012-09-25 21:55:46 +00003604void ssl_set_session_cache( ssl_context *ssl,
3605 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3606 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003607{
Paul Bakker0a597072012-09-25 21:55:46 +00003608 ssl->f_get_cache = f_get_cache;
3609 ssl->p_get_cache = p_get_cache;
3610 ssl->f_set_cache = f_set_cache;
3611 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003612}
3613
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003614int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003615{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003616 int ret;
3617
3618 if( ssl == NULL ||
3619 session == NULL ||
3620 ssl->session_negotiate == NULL ||
3621 ssl->endpoint != SSL_IS_CLIENT )
3622 {
3623 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3624 }
3625
3626 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3627 return( ret );
3628
Paul Bakker0a597072012-09-25 21:55:46 +00003629 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003630
3631 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003632}
3633
Paul Bakkerb68cad62012-08-23 08:34:18 +00003634void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003635{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003636 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3637 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3638 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3639 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3640}
3641
3642void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3643 int major, int minor )
3644{
3645 if( major != SSL_MAJOR_VERSION_3 )
3646 return;
3647
3648 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3649 return;
3650
3651 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003652}
3653
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003654#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003655/* Add a new (empty) key_cert entry an return a pointer to it */
3656static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3657{
3658 ssl_key_cert *key_cert, *last;
3659
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003660 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3661 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003662 return( NULL );
3663
3664 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3665
3666 /* Append the new key_cert to the (possibly empty) current list */
3667 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003668 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003669 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003670 if( ssl->handshake != NULL )
3671 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003672 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003673 else
3674 {
3675 last = ssl->key_cert;
3676 while( last->next != NULL )
3677 last = last->next;
3678 last->next = key_cert;
3679 }
3680
3681 return key_cert;
3682}
3683
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003684void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003685 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003686{
3687 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003688 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003689 ssl->peer_cn = peer_cn;
3690}
3691
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003692int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003693 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003694{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003695 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3696
3697 if( key_cert == NULL )
3698 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3699
3700 key_cert->cert = own_cert;
3701 key_cert->key = pk_key;
3702
3703 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003704}
3705
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003706#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003707int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003708 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003709{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003710 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003711 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003712
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003713 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003714 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3715
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003716 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3717 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003718 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003719
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003720 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003721
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003722 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003723 if( ret != 0 )
3724 return( ret );
3725
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003726 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003727 return( ret );
3728
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003729 key_cert->cert = own_cert;
3730 key_cert->key_own_alloc = 1;
3731
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003732 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003734#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003735
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003736int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003737 void *rsa_key,
3738 rsa_decrypt_func rsa_decrypt,
3739 rsa_sign_func rsa_sign,
3740 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003741{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003742 int ret;
3743 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003744
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003745 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003746 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3747
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003748 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3749 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003750 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003751
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003752 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003753
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003754 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3755 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3756 return( ret );
3757
3758 key_cert->cert = own_cert;
3759 key_cert->key_own_alloc = 1;
3760
3761 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003762}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003763#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003764
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003765#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003766int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3767 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003768{
Paul Bakker6db455e2013-09-18 17:29:31 +02003769 if( psk == NULL || psk_identity == NULL )
3770 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3771
3772 if( ssl->psk != NULL )
3773 {
3774 polarssl_free( ssl->psk );
3775 polarssl_free( ssl->psk_identity );
3776 }
3777
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003778 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003779 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003780
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003781 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3782 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003783
3784 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3785 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3786
3787 memcpy( ssl->psk, psk, ssl->psk_len );
3788 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003789
3790 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003791}
3792
3793void ssl_set_psk_cb( ssl_context *ssl,
3794 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3795 size_t),
3796 void *p_psk )
3797{
3798 ssl->f_psk = f_psk;
3799 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003800}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003801#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003802
Paul Bakker48916f92012-09-16 19:57:18 +00003803#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003804int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003805{
3806 int ret;
3807
Paul Bakker48916f92012-09-16 19:57:18 +00003808 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 {
3810 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3811 return( ret );
3812 }
3813
Paul Bakker48916f92012-09-16 19:57:18 +00003814 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003815 {
3816 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3817 return( ret );
3818 }
3819
3820 return( 0 );
3821}
3822
Paul Bakker1b57b062011-01-06 15:48:19 +00003823int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3824{
3825 int ret;
3826
Paul Bakker48916f92012-09-16 19:57:18 +00003827 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003828 {
3829 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3830 return( ret );
3831 }
3832
Paul Bakker48916f92012-09-16 19:57:18 +00003833 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003834 {
3835 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3836 return( ret );
3837 }
3838
3839 return( 0 );
3840}
Paul Bakker48916f92012-09-16 19:57:18 +00003841#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003842
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003843#if defined(POLARSSL_SSL_SET_CURVES)
3844/*
3845 * Set the allowed elliptic curves
3846 */
3847void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3848{
3849 ssl->curve_list = curve_list;
3850}
3851#endif
3852
Paul Bakker0be444a2013-08-27 21:55:01 +02003853#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003854int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003855{
3856 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003857 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003858
3859 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003860
3861 if( ssl->hostname_len + 1 == 0 )
3862 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3863
Paul Bakker6e339b52013-07-03 13:37:05 +02003864 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003865
Paul Bakkerb15b8512012-01-13 13:44:06 +00003866 if( ssl->hostname == NULL )
3867 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3868
Paul Bakker3c2122f2013-06-24 19:03:14 +02003869 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003871
Paul Bakker40ea7de2009-05-03 10:18:48 +00003872 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003873
3874 return( 0 );
3875}
3876
Paul Bakker5701cdc2012-09-27 21:49:42 +00003877void ssl_set_sni( ssl_context *ssl,
3878 int (*f_sni)(void *, ssl_context *,
3879 const unsigned char *, size_t),
3880 void *p_sni )
3881{
3882 ssl->f_sni = f_sni;
3883 ssl->p_sni = p_sni;
3884}
Paul Bakker0be444a2013-08-27 21:55:01 +02003885#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003886
Paul Bakker490ecc82011-10-06 13:04:09 +00003887void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3888{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003889 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3890 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3891 {
3892 ssl->max_major_ver = major;
3893 ssl->max_minor_ver = minor;
3894 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003895}
3896
Paul Bakker1d29fb52012-09-28 13:28:45 +00003897void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3898{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003899 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3900 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3901 {
3902 ssl->min_major_ver = major;
3903 ssl->min_minor_ver = minor;
3904 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003905}
3906
Paul Bakker05decb22013-08-15 13:33:48 +02003907#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003908int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3909{
Paul Bakker77e257e2013-12-16 15:29:52 +01003910 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003911 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003912 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003913 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003914 }
3915
3916 ssl->mfl_code = mfl_code;
3917
3918 return( 0 );
3919}
Paul Bakker05decb22013-08-15 13:33:48 +02003920#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003921
Paul Bakker1f2bc622013-08-15 13:45:55 +02003922#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003923int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003924{
3925 if( ssl->endpoint != SSL_IS_CLIENT )
3926 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3927
Paul Bakker8c1ede62013-07-19 14:14:37 +02003928 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003929
3930 return( 0 );
3931}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003932#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003933
Paul Bakker48916f92012-09-16 19:57:18 +00003934void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3935{
3936 ssl->disable_renegotiation = renegotiation;
3937}
3938
3939void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3940{
3941 ssl->allow_legacy_renegotiation = allow_legacy;
3942}
3943
Paul Bakkera503a632013-08-14 13:48:06 +02003944#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003945int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3946{
3947 ssl->session_tickets = use_tickets;
3948
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003949 if( ssl->endpoint == SSL_IS_CLIENT )
3950 return( 0 );
3951
3952 if( ssl->f_rng == NULL )
3953 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3954
3955 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003956}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003957
3958void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3959{
3960 ssl->ticket_lifetime = lifetime;
3961}
Paul Bakkera503a632013-08-14 13:48:06 +02003962#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003963
Paul Bakker5121ce52009-01-03 21:22:43 +00003964/*
3965 * SSL get accessors
3966 */
Paul Bakker23986e52011-04-24 08:57:21 +00003967size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003968{
3969 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3970}
3971
Paul Bakkerff60ee62010-03-16 21:09:09 +00003972int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003973{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003974 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003975}
3976
Paul Bakkere3166ce2011-01-27 17:40:50 +00003977const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003978{
Paul Bakker926c8e42013-03-06 10:23:34 +01003979 if( ssl == NULL || ssl->session == NULL )
3980 return NULL;
3981
Paul Bakkere3166ce2011-01-27 17:40:50 +00003982 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003983}
3984
Paul Bakker43ca69c2011-01-15 17:35:19 +00003985const char *ssl_get_version( const ssl_context *ssl )
3986{
3987 switch( ssl->minor_ver )
3988 {
3989 case SSL_MINOR_VERSION_0:
3990 return( "SSLv3.0" );
3991
3992 case SSL_MINOR_VERSION_1:
3993 return( "TLSv1.0" );
3994
3995 case SSL_MINOR_VERSION_2:
3996 return( "TLSv1.1" );
3997
Paul Bakker1ef83d62012-04-11 12:09:53 +00003998 case SSL_MINOR_VERSION_3:
3999 return( "TLSv1.2" );
4000
Paul Bakker43ca69c2011-01-15 17:35:19 +00004001 default:
4002 break;
4003 }
4004 return( "unknown" );
4005}
4006
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004007#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004008const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004009{
4010 if( ssl == NULL || ssl->session == NULL )
4011 return NULL;
4012
4013 return ssl->session->peer_cert;
4014}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004015#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004016
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004017int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4018{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004019 if( ssl == NULL ||
4020 dst == NULL ||
4021 ssl->session == NULL ||
4022 ssl->endpoint != SSL_IS_CLIENT )
4023 {
4024 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4025 }
4026
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004027 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004028}
4029
Paul Bakker5121ce52009-01-03 21:22:43 +00004030/*
Paul Bakker1961b702013-01-25 14:49:24 +01004031 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004032 */
Paul Bakker1961b702013-01-25 14:49:24 +01004033int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004034{
Paul Bakker40e46942009-01-03 21:51:57 +00004035 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004036
Paul Bakker40e46942009-01-03 21:51:57 +00004037#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004038 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004039 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004040#endif
4041
Paul Bakker40e46942009-01-03 21:51:57 +00004042#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004044 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004045#endif
4046
Paul Bakker1961b702013-01-25 14:49:24 +01004047 return( ret );
4048}
4049
4050/*
4051 * Perform the SSL handshake
4052 */
4053int ssl_handshake( ssl_context *ssl )
4054{
4055 int ret = 0;
4056
4057 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4058
4059 while( ssl->state != SSL_HANDSHAKE_OVER )
4060 {
4061 ret = ssl_handshake_step( ssl );
4062
4063 if( ret != 0 )
4064 break;
4065 }
4066
Paul Bakker5121ce52009-01-03 21:22:43 +00004067 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4068
4069 return( ret );
4070}
4071
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004072#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004073/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004074 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004075 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004076static int ssl_write_hello_request( ssl_context *ssl )
4077{
4078 int ret;
4079
4080 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4081
4082 ssl->out_msglen = 4;
4083 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4084 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4085
4086 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4087 {
4088 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4089 return( ret );
4090 }
4091
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004092 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4093
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004094 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4095
4096 return( 0 );
4097}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004098#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004099
4100/*
4101 * Actually renegotiate current connection, triggered by either:
4102 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004103 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004104 * - receiving any handshake message on server during ssl_read() after the
4105 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004106 * If the handshake doesn't complete due to waiting for I/O, it will continue
4107 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004108 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004109static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004110{
4111 int ret;
4112
4113 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4114
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004115 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4116 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004117
4118 ssl->state = SSL_HELLO_REQUEST;
4119 ssl->renegotiation = SSL_RENEGOTIATION;
4120
Paul Bakker48916f92012-09-16 19:57:18 +00004121 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4122 {
4123 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4124 return( ret );
4125 }
4126
4127 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4128
4129 return( 0 );
4130}
4131
4132/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004133 * Renegotiate current connection on client,
4134 * or request renegotiation on server
4135 */
4136int ssl_renegotiate( ssl_context *ssl )
4137{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004138 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004139
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004140#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004141 /* On server, just send the request */
4142 if( ssl->endpoint == SSL_IS_SERVER )
4143 {
4144 if( ssl->state != SSL_HANDSHAKE_OVER )
4145 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4146
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004147 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004148 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004149#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004150
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004151#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004152 /*
4153 * On client, either start the renegotiation process or,
4154 * if already in progress, continue the handshake
4155 */
4156 if( ssl->renegotiation != SSL_RENEGOTIATION )
4157 {
4158 if( ssl->state != SSL_HANDSHAKE_OVER )
4159 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4160
4161 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4162 {
4163 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4164 return( ret );
4165 }
4166 }
4167 else
4168 {
4169 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4170 {
4171 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4172 return( ret );
4173 }
4174 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004175#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004176
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004177 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004178}
4179
4180/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004181 * Receive application data decrypted from the SSL layer
4182 */
Paul Bakker23986e52011-04-24 08:57:21 +00004183int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004184{
Paul Bakker23986e52011-04-24 08:57:21 +00004185 int ret;
4186 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004187
4188 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4189
4190 if( ssl->state != SSL_HANDSHAKE_OVER )
4191 {
4192 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4193 {
4194 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4195 return( ret );
4196 }
4197 }
4198
4199 if( ssl->in_offt == NULL )
4200 {
4201 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4202 {
Paul Bakker831a7552011-05-18 13:32:51 +00004203 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4204 return( 0 );
4205
Paul Bakker5121ce52009-01-03 21:22:43 +00004206 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4207 return( ret );
4208 }
4209
4210 if( ssl->in_msglen == 0 &&
4211 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4212 {
4213 /*
4214 * OpenSSL sends empty messages to randomize the IV
4215 */
4216 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4217 {
Paul Bakker831a7552011-05-18 13:32:51 +00004218 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4219 return( 0 );
4220
Paul Bakker5121ce52009-01-03 21:22:43 +00004221 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4222 return( ret );
4223 }
4224 }
4225
Paul Bakker48916f92012-09-16 19:57:18 +00004226 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4227 {
4228 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4229
4230 if( ssl->endpoint == SSL_IS_CLIENT &&
4231 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4232 ssl->in_hslen != 4 ) )
4233 {
4234 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4235 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4236 }
4237
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004238 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4239 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4240 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004241 {
4242 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4243
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004244#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004245 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004246 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004247 /*
4248 * SSLv3 does not have a "no_renegotiation" alert
4249 */
4250 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4251 return( ret );
4252 }
4253 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004254#endif
4255#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4256 defined(POLARSSL_SSL_PROTO_TLS1_2)
4257 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004258 {
4259 if( ( ret = ssl_send_alert_message( ssl,
4260 SSL_ALERT_LEVEL_WARNING,
4261 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4262 {
4263 return( ret );
4264 }
Paul Bakker48916f92012-09-16 19:57:18 +00004265 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004266 else
4267#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004268 {
4269 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004270 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004271 }
Paul Bakker48916f92012-09-16 19:57:18 +00004272 }
4273 else
4274 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004275 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004276 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004277 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004278 return( ret );
4279 }
4280
4281 return( POLARSSL_ERR_NET_WANT_READ );
4282 }
4283 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004284 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4285 {
4286 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4287 "but not honored by client" ) );
4288 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4289 }
Paul Bakker48916f92012-09-16 19:57:18 +00004290 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004291 {
4292 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004293 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004294 }
4295
4296 ssl->in_offt = ssl->in_msg;
4297 }
4298
4299 n = ( len < ssl->in_msglen )
4300 ? len : ssl->in_msglen;
4301
4302 memcpy( buf, ssl->in_offt, n );
4303 ssl->in_msglen -= n;
4304
4305 if( ssl->in_msglen == 0 )
4306 /* all bytes consumed */
4307 ssl->in_offt = NULL;
4308 else
4309 /* more data available */
4310 ssl->in_offt += n;
4311
4312 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4313
Paul Bakker23986e52011-04-24 08:57:21 +00004314 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004315}
4316
4317/*
4318 * Send application data to be encrypted by the SSL layer
4319 */
Paul Bakker23986e52011-04-24 08:57:21 +00004320int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004321{
Paul Bakker23986e52011-04-24 08:57:21 +00004322 int ret;
4323 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004324 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004325
4326 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4327
4328 if( ssl->state != SSL_HANDSHAKE_OVER )
4329 {
4330 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4331 {
4332 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4333 return( ret );
4334 }
4335 }
4336
Paul Bakker05decb22013-08-15 13:33:48 +02004337#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004338 /*
4339 * Assume mfl_code is correct since it was checked when set
4340 */
4341 max_len = mfl_code_to_length[ssl->mfl_code];
4342
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004343 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004344 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004345 */
4346 if( ssl->session_out != NULL &&
4347 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4348 {
4349 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4350 }
Paul Bakker05decb22013-08-15 13:33:48 +02004351#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004352
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004353 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004354
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 if( ssl->out_left != 0 )
4356 {
4357 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4358 {
4359 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4360 return( ret );
4361 }
4362 }
Paul Bakker887bd502011-06-08 13:10:54 +00004363 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004364 {
Paul Bakker887bd502011-06-08 13:10:54 +00004365 ssl->out_msglen = n;
4366 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4367 memcpy( ssl->out_msg, buf, n );
4368
4369 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4370 {
4371 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4372 return( ret );
4373 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004374 }
4375
4376 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4377
Paul Bakker23986e52011-04-24 08:57:21 +00004378 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004379}
4380
4381/*
4382 * Notify the peer that the connection is being closed
4383 */
4384int ssl_close_notify( ssl_context *ssl )
4385{
4386 int ret;
4387
4388 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4389
4390 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4391 {
4392 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4393 return( ret );
4394 }
4395
4396 if( ssl->state == SSL_HANDSHAKE_OVER )
4397 {
Paul Bakker48916f92012-09-16 19:57:18 +00004398 if( ( ret = ssl_send_alert_message( ssl,
4399 SSL_ALERT_LEVEL_WARNING,
4400 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004401 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004402 return( ret );
4403 }
4404 }
4405
4406 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4407
4408 return( ret );
4409}
4410
Paul Bakker48916f92012-09-16 19:57:18 +00004411void ssl_transform_free( ssl_transform *transform )
4412{
4413#if defined(POLARSSL_ZLIB_SUPPORT)
4414 deflateEnd( &transform->ctx_deflate );
4415 inflateEnd( &transform->ctx_inflate );
4416#endif
4417
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004418 cipher_free_ctx( &transform->cipher_ctx_enc );
4419 cipher_free_ctx( &transform->cipher_ctx_dec );
4420
Paul Bakker61d113b2013-07-04 11:51:43 +02004421 md_free_ctx( &transform->md_ctx_enc );
4422 md_free_ctx( &transform->md_ctx_dec );
4423
Paul Bakker48916f92012-09-16 19:57:18 +00004424 memset( transform, 0, sizeof( ssl_transform ) );
4425}
4426
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004427#if defined(POLARSSL_X509_CRT_PARSE_C)
4428static void ssl_key_cert_free( ssl_key_cert *key_cert )
4429{
4430 ssl_key_cert *cur = key_cert, *next;
4431
4432 while( cur != NULL )
4433 {
4434 next = cur->next;
4435
4436 if( cur->key_own_alloc )
4437 {
4438 pk_free( cur->key );
4439 polarssl_free( cur->key );
4440 }
4441 polarssl_free( cur );
4442
4443 cur = next;
4444 }
4445}
4446#endif /* POLARSSL_X509_CRT_PARSE_C */
4447
Paul Bakker48916f92012-09-16 19:57:18 +00004448void ssl_handshake_free( ssl_handshake_params *handshake )
4449{
4450#if defined(POLARSSL_DHM_C)
4451 dhm_free( &handshake->dhm_ctx );
4452#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004453#if defined(POLARSSL_ECDH_C)
4454 ecdh_free( &handshake->ecdh_ctx );
4455#endif
4456
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004457#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004458 /* explicit void pointer cast for buggy MS compiler */
4459 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004460#endif
4461
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004462#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4463 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4464 /*
4465 * Free only the linked list wrapper, not the keys themselves
4466 * since the belong to the SNI callback
4467 */
4468 if( handshake->sni_key_cert != NULL )
4469 {
4470 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4471
4472 while( cur != NULL )
4473 {
4474 next = cur->next;
4475 polarssl_free( cur );
4476 cur = next;
4477 }
4478 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004479#endif
4480
Paul Bakker48916f92012-09-16 19:57:18 +00004481 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4482}
4483
4484void ssl_session_free( ssl_session *session )
4485{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004486#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004487 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004488 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004489 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004490 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004491 }
Paul Bakkered27a042013-04-18 22:46:23 +02004492#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004493
Paul Bakkera503a632013-08-14 13:48:06 +02004494#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004495 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004496#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004497
Paul Bakker0a597072012-09-25 21:55:46 +00004498 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004499}
4500
Paul Bakker5121ce52009-01-03 21:22:43 +00004501/*
4502 * Free an SSL context
4503 */
4504void ssl_free( ssl_context *ssl )
4505{
4506 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4507
Paul Bakker5121ce52009-01-03 21:22:43 +00004508 if( ssl->out_ctr != NULL )
4509 {
4510 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004511 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004512 }
4513
4514 if( ssl->in_ctr != NULL )
4515 {
4516 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004517 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004518 }
4519
Paul Bakker16770332013-10-11 09:59:44 +02004520#if defined(POLARSSL_ZLIB_SUPPORT)
4521 if( ssl->compress_buf != NULL )
4522 {
4523 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4524 polarssl_free( ssl->compress_buf );
4525 }
4526#endif
4527
Paul Bakker40e46942009-01-03 21:51:57 +00004528#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004529 mpi_free( &ssl->dhm_P );
4530 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004531#endif
4532
Paul Bakker48916f92012-09-16 19:57:18 +00004533 if( ssl->transform )
4534 {
4535 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004536 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004537 }
4538
4539 if( ssl->handshake )
4540 {
4541 ssl_handshake_free( ssl->handshake );
4542 ssl_transform_free( ssl->transform_negotiate );
4543 ssl_session_free( ssl->session_negotiate );
4544
Paul Bakker6e339b52013-07-03 13:37:05 +02004545 polarssl_free( ssl->handshake );
4546 polarssl_free( ssl->transform_negotiate );
4547 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004548 }
4549
Paul Bakkerc0463502013-02-14 11:19:38 +01004550 if( ssl->session )
4551 {
4552 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004553 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004554 }
4555
Paul Bakkera503a632013-08-14 13:48:06 +02004556#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004557 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004558#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004559
Paul Bakker0be444a2013-08-27 21:55:01 +02004560#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004561 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004562 {
4563 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004564 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004565 ssl->hostname_len = 0;
4566 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004567#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004568
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004569#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004570 if( ssl->psk != NULL )
4571 {
4572 memset( ssl->psk, 0, ssl->psk_len );
4573 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4574 polarssl_free( ssl->psk );
4575 polarssl_free( ssl->psk_identity );
4576 ssl->psk_len = 0;
4577 ssl->psk_identity_len = 0;
4578 }
4579#endif
4580
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004581#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004582 ssl_key_cert_free( ssl->key_cert );
4583#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004584
Paul Bakker05ef8352012-05-08 09:17:57 +00004585#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4586 if( ssl_hw_record_finish != NULL )
4587 {
4588 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4589 ssl_hw_record_finish( ssl );
4590 }
4591#endif
4592
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004594
Paul Bakker86f04f42013-02-14 11:20:09 +01004595 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004596 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004597}
4598
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004599#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004600/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004601 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004602 */
4603unsigned char ssl_sig_from_pk( pk_context *pk )
4604{
4605#if defined(POLARSSL_RSA_C)
4606 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4607 return( SSL_SIG_RSA );
4608#endif
4609#if defined(POLARSSL_ECDSA_C)
4610 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4611 return( SSL_SIG_ECDSA );
4612#endif
4613 return( SSL_SIG_ANON );
4614}
4615
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004616pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4617{
4618 switch( sig )
4619 {
4620#if defined(POLARSSL_RSA_C)
4621 case SSL_SIG_RSA:
4622 return( POLARSSL_PK_RSA );
4623#endif
4624#if defined(POLARSSL_ECDSA_C)
4625 case SSL_SIG_ECDSA:
4626 return( POLARSSL_PK_ECDSA );
4627#endif
4628 default:
4629 return( POLARSSL_PK_NONE );
4630 }
4631}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004632#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004633
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004634/*
4635 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4636 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004637md_type_t ssl_md_alg_from_hash( unsigned char hash )
4638{
4639 switch( hash )
4640 {
4641#if defined(POLARSSL_MD5_C)
4642 case SSL_HASH_MD5:
4643 return( POLARSSL_MD_MD5 );
4644#endif
4645#if defined(POLARSSL_SHA1_C)
4646 case SSL_HASH_SHA1:
4647 return( POLARSSL_MD_SHA1 );
4648#endif
4649#if defined(POLARSSL_SHA256_C)
4650 case SSL_HASH_SHA224:
4651 return( POLARSSL_MD_SHA224 );
4652 case SSL_HASH_SHA256:
4653 return( POLARSSL_MD_SHA256 );
4654#endif
4655#if defined(POLARSSL_SHA512_C)
4656 case SSL_HASH_SHA384:
4657 return( POLARSSL_MD_SHA384 );
4658 case SSL_HASH_SHA512:
4659 return( POLARSSL_MD_SHA512 );
4660#endif
4661 default:
4662 return( POLARSSL_MD_NONE );
4663 }
4664}
4665
Paul Bakker5121ce52009-01-03 21:22:43 +00004666#endif
Gergely Budai987bfb52014-01-19 21:48:42 +01004667
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004668#if defined(POLARSSL_SSL_SET_CURVES)
4669/*
4670 * Check is a curve proposed by the peer is in our list.
4671 * Return 1 if we're willing to use it, 0 otherwise.
4672 */
4673int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4674{
4675 const ecp_group_id *gid;
4676
4677 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4678 if( *gid == grp_id )
4679 return( 1 );
4680
4681 return( 0 );
4682}
4683#endif