blob: 02f24a18ff9b91eff98754771bcecbe18631fbc7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker68884e32013-01-07 18:20:04 +01004 * Copyright (C) 2006-2013, 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 Bakker6e339b52013-07-03 13:37:05 +020041#if defined(POLARSSL_MEMORY_C)
42#include "polarssl/memory.h"
43#else
44#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakker6edcd412013-10-29 15:22:54 +010050#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
51 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000052#define strcasecmp _stricmp
53#endif
54
Paul Bakker05decb22013-08-15 13:33:48 +020055#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020056/*
57 * Convert max_fragment_length codes to length.
58 * RFC 6066 says:
59 * enum{
60 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
61 * } MaxFragmentLength;
62 * and we add 0 -> extension unused
63 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020064static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020065{
66 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
67 512, /* SSL_MAX_FRAG_LEN_512 */
68 1024, /* SSL_MAX_FRAG_LEN_1024 */
69 2048, /* SSL_MAX_FRAG_LEN_2048 */
70 4096, /* SSL_MAX_FRAG_LEN_4096 */
71};
Paul Bakker05decb22013-08-15 13:33:48 +020072#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020073
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020074static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
75{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020076 ssl_session_free( dst );
77 memcpy( dst, src, sizeof( ssl_session ) );
78
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020080 if( src->peer_cert != NULL )
81 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020082 int ret;
83
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020084 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
85 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020086 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
87
Paul Bakkerb6b09562013-09-18 14:17:41 +020088 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089
Paul Bakkerddf26b42013-09-18 13:46:23 +020090 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
91 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020092 {
93 polarssl_free( dst->peer_cert );
94 dst->peer_cert = NULL;
95 return( ret );
96 }
97 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020099
Paul Bakkera503a632013-08-14 13:48:06 +0200100#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101 if( src->ticket != NULL )
102 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200103 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
104 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200105 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
106
107 memcpy( dst->ticket, src->ticket, src->ticket_len );
108 }
Paul Bakkera503a632013-08-14 13:48:06 +0200109#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110
111 return( 0 );
112}
113
Paul Bakker05ef8352012-05-08 09:17:57 +0000114#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
115int (*ssl_hw_record_init)(ssl_context *ssl,
116 const unsigned char *key_enc, const unsigned char *key_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100117 size_t keylen,
Paul Bakker05ef8352012-05-08 09:17:57 +0000118 const unsigned char *iv_enc, const unsigned char *iv_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100119 size_t ivlen,
120 const unsigned char *mac_enc, const unsigned char *mac_dec,
121 size_t maclen) = NULL;
122int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000123int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
124int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
125int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
126int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
127#endif
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129/*
130 * Key material generation
131 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200132#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200133static int ssl3_prf( const unsigned char *secret, size_t slen,
134 const char *label,
135 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000136 unsigned char *dstbuf, size_t dlen )
137{
138 size_t i;
139 md5_context md5;
140 sha1_context sha1;
141 unsigned char padding[16];
142 unsigned char sha1sum[20];
143 ((void)label);
144
145 /*
146 * SSLv3:
147 * block =
148 * MD5( secret + SHA1( 'A' + secret + random ) ) +
149 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
150 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
151 * ...
152 */
153 for( i = 0; i < dlen / 16; i++ )
154 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200155 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000156
157 sha1_starts( &sha1 );
158 sha1_update( &sha1, padding, 1 + i );
159 sha1_update( &sha1, secret, slen );
160 sha1_update( &sha1, random, rlen );
161 sha1_finish( &sha1, sha1sum );
162
163 md5_starts( &md5 );
164 md5_update( &md5, secret, slen );
165 md5_update( &md5, sha1sum, 20 );
166 md5_finish( &md5, dstbuf + i * 16 );
167 }
168
169 memset( &md5, 0, sizeof( md5 ) );
170 memset( &sha1, 0, sizeof( sha1 ) );
171
172 memset( padding, 0, sizeof( padding ) );
173 memset( sha1sum, 0, sizeof( sha1sum ) );
174
175 return( 0 );
176}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200177#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000178
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200179#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200180static int tls1_prf( const unsigned char *secret, size_t slen,
181 const char *label,
182 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000183 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184{
Paul Bakker23986e52011-04-24 08:57:21 +0000185 size_t nb, hs;
186 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200187 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 unsigned char tmp[128];
189 unsigned char h_i[20];
190
191 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000192 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 hs = ( slen + 1 ) / 2;
195 S1 = secret;
196 S2 = secret + slen - hs;
197
198 nb = strlen( label );
199 memcpy( tmp + 20, label, nb );
200 memcpy( tmp + 20 + nb, random, rlen );
201 nb += rlen;
202
203 /*
204 * First compute P_md5(secret,label+random)[0..dlen]
205 */
206 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
207
208 for( i = 0; i < dlen; i += 16 )
209 {
210 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
211 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
212
213 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
214
215 for( j = 0; j < k; j++ )
216 dstbuf[i + j] = h_i[j];
217 }
218
219 /*
220 * XOR out with P_sha1(secret,label+random)[0..dlen]
221 */
222 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
223
224 for( i = 0; i < dlen; i += 20 )
225 {
226 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
227 sha1_hmac( S2, hs, tmp, 20, tmp );
228
229 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
230
231 for( j = 0; j < k; j++ )
232 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
233 }
234
235 memset( tmp, 0, sizeof( tmp ) );
236 memset( h_i, 0, sizeof( h_i ) );
237
238 return( 0 );
239}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200240#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200242#if defined(POLARSSL_SSL_PROTO_TLS1_2)
243#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200244static int tls_prf_sha256( const unsigned char *secret, size_t slen,
245 const char *label,
246 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000247 unsigned char *dstbuf, size_t dlen )
248{
249 size_t nb;
250 size_t i, j, k;
251 unsigned char tmp[128];
252 unsigned char h_i[32];
253
254 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
255 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
256
257 nb = strlen( label );
258 memcpy( tmp + 32, label, nb );
259 memcpy( tmp + 32 + nb, random, rlen );
260 nb += rlen;
261
262 /*
263 * Compute P_<hash>(secret, label + random)[0..dlen]
264 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200265 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000266
267 for( i = 0; i < dlen; i += 32 )
268 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200269 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
270 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000271
272 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
273
274 for( j = 0; j < k; j++ )
275 dstbuf[i + j] = h_i[j];
276 }
277
278 memset( tmp, 0, sizeof( tmp ) );
279 memset( h_i, 0, sizeof( h_i ) );
280
281 return( 0 );
282}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200283#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000284
Paul Bakker9e36f042013-06-30 14:34:05 +0200285#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200286static int tls_prf_sha384( const unsigned char *secret, size_t slen,
287 const char *label,
288 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000289 unsigned char *dstbuf, size_t dlen )
290{
291 size_t nb;
292 size_t i, j, k;
293 unsigned char tmp[128];
294 unsigned char h_i[48];
295
296 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
297 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
298
299 nb = strlen( label );
300 memcpy( tmp + 48, label, nb );
301 memcpy( tmp + 48 + nb, random, rlen );
302 nb += rlen;
303
304 /*
305 * Compute P_<hash>(secret, label + random)[0..dlen]
306 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200307 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000308
309 for( i = 0; i < dlen; i += 48 )
310 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200311 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
312 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000313
314 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
315
316 for( j = 0; j < k; j++ )
317 dstbuf[i + j] = h_i[j];
318 }
319
320 memset( tmp, 0, sizeof( tmp ) );
321 memset( h_i, 0, sizeof( h_i ) );
322
323 return( 0 );
324}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200325#endif /* POLARSSL_SHA512_C */
326#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200328static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200329
330#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
331 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200332static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200333#endif
Paul Bakker380da532012-04-18 16:10:25 +0000334
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200335#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000336static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338#endif
339
340#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
341static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343#endif
344
345#if defined(POLARSSL_SSL_PROTO_TLS1_2)
346#if defined(POLARSSL_SHA256_C)
347static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
348static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000349static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100351
Paul Bakker9e36f042013-06-30 14:34:05 +0200352#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200353static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100354static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000355static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100356#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +0000358
Paul Bakker5121ce52009-01-03 21:22:43 +0000359int ssl_derive_keys( ssl_context *ssl )
360{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200361 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 unsigned char keyblk[256];
364 unsigned char *key1;
365 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100366 unsigned char *mac_enc;
367 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200368 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100369 const cipher_info_t *cipher_info;
370 const md_info_t *md_info;
371
Paul Bakker48916f92012-09-16 19:57:18 +0000372 ssl_session *session = ssl->session_negotiate;
373 ssl_transform *transform = ssl->transform_negotiate;
374 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
377
Paul Bakker68884e32013-01-07 18:20:04 +0100378 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
379 if( cipher_info == NULL )
380 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200381 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100382 transform->ciphersuite_info->cipher ) );
383 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
384 }
385
386 md_info = md_info_from_type( transform->ciphersuite_info->mac );
387 if( md_info == NULL )
388 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200389 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100390 transform->ciphersuite_info->mac ) );
391 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
392 }
393
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000395 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000396 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200397#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000398 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000399 {
Paul Bakker48916f92012-09-16 19:57:18 +0000400 handshake->tls_prf = ssl3_prf;
401 handshake->calc_verify = ssl_calc_verify_ssl;
402 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000403 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200404 else
405#endif
406#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
407 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408 {
Paul Bakker48916f92012-09-16 19:57:18 +0000409 handshake->tls_prf = tls1_prf;
410 handshake->calc_verify = ssl_calc_verify_tls;
411 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200413 else
414#endif
415#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200416#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200417 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
418 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000419 {
Paul Bakker48916f92012-09-16 19:57:18 +0000420 handshake->tls_prf = tls_prf_sha384;
421 handshake->calc_verify = ssl_calc_verify_tls_sha384;
422 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000423 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000424 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200425#endif
426#if defined(POLARSSL_SHA256_C)
427 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = tls_prf_sha256;
430 handshake->calc_verify = ssl_calc_verify_tls_sha256;
431 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200433 else
434#endif
435#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200436 {
437 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200438 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200439 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000440
441 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 * SSLv3:
443 * master =
444 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
445 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
446 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200447 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200448 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 * master = PRF( premaster, "master secret", randbytes )[0..47]
450 */
Paul Bakker0a597072012-09-25 21:55:46 +0000451 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 {
Paul Bakker48916f92012-09-16 19:57:18 +0000453 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
454 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
Paul Bakker48916f92012-09-16 19:57:18 +0000456 handshake->tls_prf( handshake->premaster, handshake->pmslen,
457 "master secret",
458 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Paul Bakker48916f92012-09-16 19:57:18 +0000460 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 }
462 else
463 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
464
465 /*
466 * Swap the client and server random values.
467 */
Paul Bakker48916f92012-09-16 19:57:18 +0000468 memcpy( tmp, handshake->randbytes, 64 );
469 memcpy( handshake->randbytes, tmp + 32, 32 );
470 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 memset( tmp, 0, sizeof( tmp ) );
472
473 /*
474 * SSLv3:
475 * key block =
476 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
477 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
478 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
479 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
480 * ...
481 *
482 * TLSv1:
483 * key block = PRF( master, "key expansion", randbytes )
484 */
Paul Bakker48916f92012-09-16 19:57:18 +0000485 handshake->tls_prf( session->master, 48, "key expansion",
486 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
Paul Bakker48916f92012-09-16 19:57:18 +0000488 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
489 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
490 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
491 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
493
Paul Bakker48916f92012-09-16 19:57:18 +0000494 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 /*
497 * Determine the appropriate key, IV and MAC length.
498 */
Paul Bakker68884e32013-01-07 18:20:04 +0100499
500 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 {
Paul Bakker68884e32013-01-07 18:20:04 +0100502 transform->keylen = cipher_info->key_length;
503 transform->keylen /= 8;
504 transform->minlen = 1;
505 transform->ivlen = 12;
506 transform->fixed_ivlen = 4;
507 transform->maclen = 0;
508 }
509 else
510 {
511 if( md_info->type != POLARSSL_MD_NONE )
512 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200513 int ret;
514
Paul Bakker61d113b2013-07-04 11:51:43 +0200515 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
516 {
517 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
518 return( ret );
519 }
520
521 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
522 {
523 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
524 return( ret );
525 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
Paul Bakker68884e32013-01-07 18:20:04 +0100527 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200528
Paul Bakker1f2bc622013-08-15 13:45:55 +0200529#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200530 /*
531 * If HMAC is to be truncated, we shall keep the leftmost bytes,
532 * (rfc 6066 page 13 or rfc 2104 section 4),
533 * so we only need to adjust the length here.
534 */
535 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
536 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200537#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100538 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker68884e32013-01-07 18:20:04 +0100540 transform->keylen = cipher_info->key_length;
541 transform->keylen /= 8;
542 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Paul Bakker68884e32013-01-07 18:20:04 +0100544 transform->minlen = transform->keylen;
545 if( transform->minlen < transform->maclen )
546 {
547 if( cipher_info->mode == POLARSSL_MODE_STREAM )
548 transform->minlen = transform->maclen;
549 else
550 transform->minlen += transform->keylen;
551 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 }
553
554 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000555 transform->keylen, transform->minlen, transform->ivlen,
556 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 /*
559 * Finally setup the cipher contexts, IVs and MAC secrets.
560 */
561 if( ssl->endpoint == SSL_IS_CLIENT )
562 {
Paul Bakker48916f92012-09-16 19:57:18 +0000563 key1 = keyblk + transform->maclen * 2;
564 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Paul Bakker68884e32013-01-07 18:20:04 +0100566 mac_enc = keyblk;
567 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000569 /*
570 * This is not used in TLS v1.1.
571 */
Paul Bakker48916f92012-09-16 19:57:18 +0000572 iv_copy_len = ( transform->fixed_ivlen ) ?
573 transform->fixed_ivlen : transform->ivlen;
574 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
575 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000576 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 }
578 else
579 {
Paul Bakker48916f92012-09-16 19:57:18 +0000580 key1 = keyblk + transform->maclen * 2 + transform->keylen;
581 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Paul Bakker68884e32013-01-07 18:20:04 +0100583 mac_enc = keyblk + transform->maclen;
584 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000586 /*
587 * This is not used in TLS v1.1.
588 */
Paul Bakker48916f92012-09-16 19:57:18 +0000589 iv_copy_len = ( transform->fixed_ivlen ) ?
590 transform->fixed_ivlen : transform->ivlen;
591 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
592 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000593 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 }
595
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200596#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100597 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
598 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100599 if( transform->maclen > sizeof transform->mac_enc )
600 {
601 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
602 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
603 }
604
Paul Bakker68884e32013-01-07 18:20:04 +0100605 memcpy( transform->mac_enc, mac_enc, transform->maclen );
606 memcpy( transform->mac_dec, mac_dec, transform->maclen );
607 }
608 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200609#endif
610#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
611 defined(POLARSSL_SSL_PROTO_TLS1_2)
612 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100613 {
614 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
615 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
616 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200617 else
618#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200619 {
620 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200621 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200622 }
Paul Bakker68884e32013-01-07 18:20:04 +0100623
Paul Bakker05ef8352012-05-08 09:17:57 +0000624#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
625 if( ssl_hw_record_init != NULL)
626 {
627 int ret = 0;
628
629 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
630
Paul Bakker07eb38b2012-12-19 14:42:06 +0100631 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
632 transform->iv_enc, transform->iv_dec,
633 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100634 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100635 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000636 {
637 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
638 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
639 }
640 }
641#endif
642
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200643 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
644 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200646 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
647 return( ret );
648 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200649
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200650 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
651 cipher_info ) ) != 0 )
652 {
653 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
654 return( ret );
655 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200656
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200657 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
658 cipher_info->key_length,
659 POLARSSL_ENCRYPT ) ) != 0 )
660 {
661 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
662 return( ret );
663 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200664
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200665 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
666 cipher_info->key_length,
667 POLARSSL_DECRYPT ) ) != 0 )
668 {
669 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
670 return( ret );
671 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200672
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200673#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200674 if( cipher_info->mode == POLARSSL_MODE_CBC )
675 {
676 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
677 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200678 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200679 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
680 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200681 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200682
683 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
684 POLARSSL_PADDING_NONE ) ) != 0 )
685 {
686 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
687 return( ret );
688 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200690#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
692 memset( keyblk, 0, sizeof( keyblk ) );
693
Paul Bakker2770fbd2012-07-03 13:30:23 +0000694#if defined(POLARSSL_ZLIB_SUPPORT)
695 // Initialize compression
696 //
Paul Bakker48916f92012-09-16 19:57:18 +0000697 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000698 {
Paul Bakker16770332013-10-11 09:59:44 +0200699 if( ssl->compress_buf == NULL )
700 {
701 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
702 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
703 if( ssl->compress_buf == NULL )
704 {
705 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
706 SSL_BUFFER_LEN ) );
707 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
708 }
709 }
710
Paul Bakker2770fbd2012-07-03 13:30:23 +0000711 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
712
Paul Bakker48916f92012-09-16 19:57:18 +0000713 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
714 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000715
Paul Bakker48916f92012-09-16 19:57:18 +0000716 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
717 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000718 {
719 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
720 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
721 }
722 }
723#endif /* POLARSSL_ZLIB_SUPPORT */
724
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
726
727 return( 0 );
728}
729
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200730#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000731void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000732{
733 md5_context md5;
734 sha1_context sha1;
735 unsigned char pad_1[48];
736 unsigned char pad_2[48];
737
Paul Bakker380da532012-04-18 16:10:25 +0000738 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Paul Bakker48916f92012-09-16 19:57:18 +0000740 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
741 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Paul Bakker380da532012-04-18 16:10:25 +0000743 memset( pad_1, 0x36, 48 );
744 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Paul Bakker48916f92012-09-16 19:57:18 +0000746 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000747 md5_update( &md5, pad_1, 48 );
748 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker380da532012-04-18 16:10:25 +0000750 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000751 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000752 md5_update( &md5, pad_2, 48 );
753 md5_update( &md5, hash, 16 );
754 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakker48916f92012-09-16 19:57:18 +0000756 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000757 sha1_update( &sha1, pad_1, 40 );
758 sha1_finish( &sha1, hash + 16 );
759
760 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000761 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000762 sha1_update( &sha1, pad_2, 40 );
763 sha1_update( &sha1, hash + 16, 20 );
764 sha1_finish( &sha1, hash + 16 );
765
766 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
767 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
768
769 return;
770}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200771#endif
Paul Bakker380da532012-04-18 16:10:25 +0000772
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000774void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
775{
776 md5_context md5;
777 sha1_context sha1;
778
779 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
780
Paul Bakker48916f92012-09-16 19:57:18 +0000781 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
782 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000783
Paul Bakker48916f92012-09-16 19:57:18 +0000784 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000785 sha1_finish( &sha1, hash + 16 );
786
787 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
788 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
789
790 return;
791}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200792#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000793
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200794#if defined(POLARSSL_SSL_PROTO_TLS1_2)
795#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000796void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
797{
Paul Bakker9e36f042013-06-30 14:34:05 +0200798 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000799
800 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
801
Paul Bakker9e36f042013-06-30 14:34:05 +0200802 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
803 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000804
805 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
806 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
807
808 return;
809}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000811
Paul Bakker9e36f042013-06-30 14:34:05 +0200812#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000813void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
814{
Paul Bakker9e36f042013-06-30 14:34:05 +0200815 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000816
817 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
818
Paul Bakker9e36f042013-06-30 14:34:05 +0200819 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
820 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821
Paul Bakkerca4ab492012-04-18 14:23:57 +0000822 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
824
825 return;
826}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200827#endif /* POLARSSL_SHA512_C */
828#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200830#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200831int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
832{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200833 unsigned char *p = ssl->handshake->premaster;
834 unsigned char *end = p + sizeof( ssl->handshake->premaster );
835
836 /*
837 * PMS = struct {
838 * opaque other_secret<0..2^16-1>;
839 * opaque psk<0..2^16-1>;
840 * };
841 * with "other_secret" depending on the particular key exchange
842 */
843#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
844 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
845 {
846 if( end - p < 2 + (int) ssl->psk_len )
847 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
848
849 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
850 *(p++) = (unsigned char)( ssl->psk_len );
851 p += ssl->psk_len;
852 }
853 else
854#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200855#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
856 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
857 {
858 /*
859 * other_secret already set by the ClientKeyExchange message,
860 * and is 48 bytes long
861 */
862 *p++ = 0;
863 *p++ = 48;
864 p += 48;
865 }
866 else
867#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200868#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
869 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
870 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200871 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200872 size_t len = ssl->handshake->dhm_ctx.len;
873
874 if( end - p < 2 + (int) len )
875 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
876
877 *(p++) = (unsigned char)( len >> 8 );
878 *(p++) = (unsigned char)( len );
879 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
880 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
881 {
882 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
883 return( ret );
884 }
885 p += len;
886
887 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
888 }
889 else
890#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
891#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
892 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
893 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200894 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895 size_t zlen;
896
897 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
898 p + 2, end - (p + 2),
899 ssl->f_rng, ssl->p_rng ) ) != 0 )
900 {
901 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
902 return( ret );
903 }
904
905 *(p++) = (unsigned char)( zlen >> 8 );
906 *(p++) = (unsigned char)( zlen );
907 p += zlen;
908
909 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
910 }
911 else
912#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
913 {
914 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
915 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
916 }
917
918 /* opaque psk<0..2^16-1>; */
919 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
920 *(p++) = (unsigned char)( ssl->psk_len );
921 memcpy( p, ssl->psk, ssl->psk_len );
922 p += ssl->psk_len;
923
924 ssl->handshake->pmslen = p - ssl->handshake->premaster;
925
926 return( 0 );
927}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200928#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200929
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200930#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000931/*
932 * SSLv3.0 MAC functions
933 */
Paul Bakker68884e32013-01-07 18:20:04 +0100934static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
935 unsigned char *buf, size_t len,
936 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000937{
938 unsigned char header[11];
939 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100940 int padlen = 0;
941 int md_size = md_get_size( md_ctx->md_info );
942 int md_type = md_get_type( md_ctx->md_info );
943
944 if( md_type == POLARSSL_MD_MD5 )
945 padlen = 48;
946 else if( md_type == POLARSSL_MD_SHA1 )
947 padlen = 40;
948 else if( md_type == POLARSSL_MD_SHA256 )
949 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100950 else if( md_type == POLARSSL_MD_SHA384 )
951 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
953 memcpy( header, ctr, 8 );
954 header[ 8] = (unsigned char) type;
955 header[ 9] = (unsigned char)( len >> 8 );
956 header[10] = (unsigned char)( len );
957
Paul Bakker68884e32013-01-07 18:20:04 +0100958 memset( padding, 0x36, padlen );
959 md_starts( md_ctx );
960 md_update( md_ctx, secret, md_size );
961 md_update( md_ctx, padding, padlen );
962 md_update( md_ctx, header, 11 );
963 md_update( md_ctx, buf, len );
964 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Paul Bakker68884e32013-01-07 18:20:04 +0100966 memset( padding, 0x5C, padlen );
967 md_starts( md_ctx );
968 md_update( md_ctx, secret, md_size );
969 md_update( md_ctx, padding, padlen );
970 md_update( md_ctx, buf + len, md_size );
971 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000972}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200973#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000974
Paul Bakker5121ce52009-01-03 21:22:43 +0000975/*
976 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200977 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000978static int ssl_encrypt_buf( ssl_context *ssl )
979{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200980 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
982 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
983
984 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200985 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200987#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
988 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
989 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
990 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
991 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200993#if defined(POLARSSL_SSL_PROTO_SSL3)
994 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
995 {
996 ssl_mac( &ssl->transform_out->md_ctx_enc,
997 ssl->transform_out->mac_enc,
998 ssl->out_msg, ssl->out_msglen,
999 ssl->out_ctr, ssl->out_msgtype );
1000 }
1001 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001002#endif
1003#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001004 defined(POLARSSL_SSL_PROTO_TLS1_2)
1005 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1006 {
1007 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1008 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1009 ssl->out_msg, ssl->out_msglen );
1010 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1011 ssl->out_msg + ssl->out_msglen );
1012 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1013 }
1014 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001015#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001016 {
1017 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1018 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1019 }
1020
1021 SSL_DEBUG_BUF( 4, "computed mac",
1022 ssl->out_msg + ssl->out_msglen,
1023 ssl->transform_out->maclen );
1024
1025 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001026 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001027#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001029 /*
1030 * Encrypt
1031 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001032#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001033 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1034 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001036 int ret;
1037 size_t olen = 0;
1038
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1040 "including %d bytes of padding",
1041 ssl->out_msglen, 0 ) );
1042
1043 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1044 ssl->out_msg, ssl->out_msglen );
1045
Paul Bakker45125bc2013-09-04 16:47:11 +02001046 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001047 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001048 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1049 return( ret );
1050 }
1051
1052 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1053 ssl->transform_out->iv_enc,
1054 ssl->transform_out->ivlen ) ) != 0 )
1055 {
1056 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001057 return( ret );
1058 }
1059
1060 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1061 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1062 &olen ) ) != 0 )
1063 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001064 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001065 return( ret );
1066 }
1067
1068 if( ssl->out_msglen != olen )
1069 {
1070 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1071 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001072 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001073 }
1074
1075 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001076 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001077 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001078 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001079 return( ret );
1080 }
1081
1082 if( 0 != olen )
1083 {
1084 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1085 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001086 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001087 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 }
Paul Bakker68884e32013-01-07 18:20:04 +01001089 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001090#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001091#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001092 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1093 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001095 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001096 unsigned char *enc_msg;
1097 unsigned char add_data[13];
1098 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1099
Paul Bakkerca4ab492012-04-18 14:23:57 +00001100 memcpy( add_data, ssl->out_ctr, 8 );
1101 add_data[8] = ssl->out_msgtype;
1102 add_data[9] = ssl->major_ver;
1103 add_data[10] = ssl->minor_ver;
1104 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1105 add_data[12] = ssl->out_msglen & 0xFF;
1106
1107 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1108 add_data, 13 );
1109
Paul Bakker68884e32013-01-07 18:20:04 +01001110 /*
1111 * Generate IV
1112 */
1113 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001114 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001115 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1116 if( ret != 0 )
1117 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001118
Paul Bakker68884e32013-01-07 18:20:04 +01001119 memcpy( ssl->out_iv,
1120 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1121 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001122
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001123 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1124 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1125
Paul Bakker68884e32013-01-07 18:20:04 +01001126 /*
1127 * Fix pointer positions and message length with added IV
1128 */
1129 enc_msg = ssl->out_msg;
1130 enc_msglen = ssl->out_msglen;
1131 ssl->out_msglen += ssl->transform_out->ivlen -
1132 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001133
Paul Bakker68884e32013-01-07 18:20:04 +01001134 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1135 "including %d bytes of padding",
1136 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001137
Paul Bakker68884e32013-01-07 18:20:04 +01001138 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1139 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140
Paul Bakker68884e32013-01-07 18:20:04 +01001141 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001142 * Encrypt
1143 */
1144 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1145 ssl->transform_out->iv_enc,
1146 ssl->transform_out->ivlen ) ) != 0 ||
1147 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1148 {
1149 return( ret );
1150 }
1151
1152 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1153 add_data, 13 ) ) != 0 )
1154 {
1155 return( ret );
1156 }
1157
1158 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1159 enc_msg, enc_msglen,
1160 enc_msg, &olen ) ) != 0 )
1161 {
1162 return( ret );
1163 }
1164 totlen = olen;
1165
1166 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1167 enc_msg + olen, &olen ) ) != 0 )
1168 {
1169 return( ret );
1170 }
1171 totlen += olen;
1172
1173 if( totlen != enc_msglen )
1174 {
1175 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1176 return( -1 );
1177 }
1178
1179 /*
1180 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001181 */
1182 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001183
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001184 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1185 enc_msg + enc_msglen, 16 ) ) != 0 )
1186 {
1187 return( ret );
1188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001190 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001191 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 else
Paul Bakker68884e32013-01-07 18:20:04 +01001193#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001194#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1195 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001196 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1197 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001199 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001200 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001201 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001202
Paul Bakker48916f92012-09-16 19:57:18 +00001203 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1204 ssl->transform_out->ivlen;
1205 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 padlen = 0;
1207
1208 for( i = 0; i <= padlen; i++ )
1209 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1210
1211 ssl->out_msglen += padlen + 1;
1212
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 enc_msglen = ssl->out_msglen;
1214 enc_msg = ssl->out_msg;
1215
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001216#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001217 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001218 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1219 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001221 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001222 {
1223 /*
1224 * Generate IV
1225 */
Paul Bakker48916f92012-09-16 19:57:18 +00001226 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1227 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001228 if( ret != 0 )
1229 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230
Paul Bakker92be97b2013-01-02 17:30:03 +01001231 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001232 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233
1234 /*
1235 * Fix pointer positions and message length with added IV
1236 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001237 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001239 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001241#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001244 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001245 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
1247 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001248 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Paul Bakker45125bc2013-09-04 16:47:11 +02001250 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001252 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1253 return( ret );
1254 }
1255
1256 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1257 ssl->transform_out->iv_enc,
1258 ssl->transform_out->ivlen ) ) != 0 )
1259 {
1260 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001261 return( ret );
1262 }
Paul Bakker68884e32013-01-07 18:20:04 +01001263
Paul Bakkercca5b812013-08-31 17:40:26 +02001264 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1265 enc_msg, enc_msglen, enc_msg,
1266 &olen ) ) != 0 )
1267 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001268 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001269 return( ret );
1270 }
Paul Bakker68884e32013-01-07 18:20:04 +01001271
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001273
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001275 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001276 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001277 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 return( ret );
1279 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001280
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 if( enc_msglen != olen )
1282 {
1283 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1284 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001285 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001286 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001287
1288#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001289 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1290 {
1291 /*
1292 * Save IV in SSL3 and TLS1
1293 */
1294 memcpy( ssl->transform_out->iv_enc,
1295 ssl->transform_out->cipher_ctx_enc.iv,
1296 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001298#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001300 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001301#endif /* POLARSSL_CIPHER_MODE_CBC &&
1302 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001303 {
1304 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1305 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Paul Bakkerca4ab492012-04-18 14:23:57 +00001308 for( i = 8; i > 0; i-- )
1309 if( ++ssl->out_ctr[i - 1] != 0 )
1310 break;
1311
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1313
1314 return( 0 );
1315}
1316
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001317#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001318
Paul Bakker5121ce52009-01-03 21:22:43 +00001319static int ssl_decrypt_buf( ssl_context *ssl )
1320{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001321 size_t i;
1322#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1323 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1324 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1325 size_t padlen = 0, correct = 1;
1326#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
1328 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1329
Paul Bakker48916f92012-09-16 19:57:18 +00001330 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 {
1332 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001333 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001334 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 }
1336
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001337#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001338 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1339 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001340 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001341 int ret;
1342 size_t olen = 0;
1343
Paul Bakker68884e32013-01-07 18:20:04 +01001344 padlen = 0;
1345
Paul Bakker45125bc2013-09-04 16:47:11 +02001346 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001347 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001348 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1349 return( ret );
1350 }
1351
1352 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1353 ssl->transform_in->iv_dec,
1354 ssl->transform_in->ivlen ) ) != 0 )
1355 {
1356 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001357 return( ret );
1358 }
1359
1360 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1361 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1362 &olen ) ) != 0 )
1363 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001364 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001365 return( ret );
1366 }
1367
1368 if( ssl->in_msglen != olen )
1369 {
1370 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001371 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001372 }
1373
1374 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001375 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001376 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001377 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001378 return( ret );
1379 }
1380
1381 if( 0 != olen )
1382 {
1383 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001384 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 }
Paul Bakker68884e32013-01-07 18:20:04 +01001387 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001388#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001389#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001390 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1391 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001392 {
1393 unsigned char *dec_msg;
1394 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001395 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001396 unsigned char add_data[13];
1397 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1398
Paul Bakker68884e32013-01-07 18:20:04 +01001399 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1400 ssl->transform_in->fixed_ivlen );
1401 dec_msglen -= 16;
1402 dec_msg = ssl->in_msg;
1403 dec_msg_result = ssl->in_msg;
1404 ssl->in_msglen = dec_msglen;
1405
1406 memcpy( add_data, ssl->in_ctr, 8 );
1407 add_data[8] = ssl->in_msgtype;
1408 add_data[9] = ssl->major_ver;
1409 add_data[10] = ssl->minor_ver;
1410 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1411 add_data[12] = ssl->in_msglen & 0xFF;
1412
1413 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1414 add_data, 13 );
1415
1416 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1417 ssl->in_iv,
1418 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1419
1420 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1421 ssl->transform_in->ivlen );
1422 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1423
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001424 /*
1425 * Decrypt
1426 */
1427 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1428 ssl->transform_in->iv_dec,
1429 ssl->transform_in->ivlen ) ) != 0 ||
1430 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001431 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001432 return( ret );
1433 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001434
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001435 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1436 add_data, 13 ) ) != 0 )
1437 {
1438 return( ret );
1439 }
1440
1441 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1442 dec_msg, dec_msglen,
1443 dec_msg_result, &olen ) ) != 0 )
1444 {
1445 return( ret );
1446 }
1447 totlen = olen;
1448
1449 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1450 dec_msg_result + olen, &olen ) ) != 0 )
1451 {
1452 return( ret );
1453 }
1454 totlen += olen;
1455
1456 if( totlen != dec_msglen )
1457 {
1458 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1459 return( -1 );
1460 }
1461
1462 /*
1463 * Authenticate
1464 */
1465 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1466 dec_msg + dec_msglen, 16 ) ) != 0 )
1467 {
1468 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001469 return( POLARSSL_ERR_SSL_INVALID_MAC );
1470 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001471
Paul Bakkerca4ab492012-04-18 14:23:57 +00001472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 else
Paul Bakker68884e32013-01-07 18:20:04 +01001474#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001475#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1476 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001477 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1478 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001479 {
Paul Bakker45829992013-01-03 14:52:21 +01001480 /*
1481 * Decrypt and check the padding
1482 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001483 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001484 unsigned char *dec_msg;
1485 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001486 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001487 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001488 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001489
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 /*
Paul Bakker45829992013-01-03 14:52:21 +01001491 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 */
Paul Bakker48916f92012-09-16 19:57:18 +00001493 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 {
1495 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001496 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001497 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 }
1499
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001500#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001501 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1502 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001503#endif
Paul Bakker45829992013-01-03 14:52:21 +01001504
1505 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1506 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1507 {
1508 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1509 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1510 return( POLARSSL_ERR_SSL_INVALID_MAC );
1511 }
1512
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001513 dec_msglen = ssl->in_msglen;
1514 dec_msg = ssl->in_msg;
1515 dec_msg_result = ssl->in_msg;
1516
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001517#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001518 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001519 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001520 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001521 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001522 {
Paul Bakker48916f92012-09-16 19:57:18 +00001523 dec_msglen -= ssl->transform_in->ivlen;
1524 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001525
Paul Bakker48916f92012-09-16 19:57:18 +00001526 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001527 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001528 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001529#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001530
Paul Bakker45125bc2013-09-04 16:47:11 +02001531 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001533 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1534 return( ret );
1535 }
1536
1537 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1538 ssl->transform_in->iv_dec,
1539 ssl->transform_in->ivlen ) ) != 0 )
1540 {
1541 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001542 return( ret );
1543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
Paul Bakkercca5b812013-08-31 17:40:26 +02001545 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1546 dec_msg, dec_msglen, dec_msg_result,
1547 &olen ) ) != 0 )
1548 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001549 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001550 return( ret );
1551 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001552
Paul Bakkercca5b812013-08-31 17:40:26 +02001553 dec_msglen -= olen;
1554 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001555 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001556 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001557 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001558 return( ret );
1559 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001560
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 if( dec_msglen != olen )
1562 {
1563 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001564 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
1567#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001568 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1569 {
1570 /*
1571 * Save IV in SSL3 and TLS1
1572 */
1573 memcpy( ssl->transform_in->iv_dec,
1574 ssl->transform_in->cipher_ctx_dec.iv,
1575 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
1579 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001580
1581 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1582 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001583#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001584 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1585 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001586#endif
Paul Bakker45829992013-01-03 14:52:21 +01001587 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001588 correct = 0;
1589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001590
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001591#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1593 {
Paul Bakker48916f92012-09-16 19:57:18 +00001594 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001596#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1598 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001599 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001600#endif
Paul Bakker45829992013-01-03 14:52:21 +01001601 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 }
1603 }
1604 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001605#endif
1606#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1607 defined(POLARSSL_SSL_PROTO_TLS1_2)
1608 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 {
1610 /*
Paul Bakker45829992013-01-03 14:52:21 +01001611 * TLSv1+: always check the padding up to the first failure
1612 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001614 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001615 size_t padding_idx = ssl->in_msglen - padlen - 1;
1616
Paul Bakker956c9e02013-12-19 14:42:28 +01001617 /*
1618 * Padding is guaranteed to be incorrect if:
1619 * 1. padlen - 1 > ssl->in_msglen
1620 *
1621 * 2. ssl->in_msglen + padlen >
1622 * SSL_MAX_CONTENT_LEN + 256 (max padding)
1623 *
1624 * In both cases we reset padding_idx to a safe value (0) to
1625 * prevent out-of-buffer reads.
1626 */
1627 correct &= ( ssl->in_msglen >= padlen - 1 );
1628 correct &= ( ssl->in_msglen + padlen <= SSL_MAX_CONTENT_LEN + 256 );
1629
1630 padding_idx *= correct;
1631
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001632 for( i = 1; i <= 256; i++ )
1633 {
1634 real_count &= ( i <= padlen );
1635 pad_count += real_count *
1636 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1637 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001638
1639 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001640
Paul Bakkerd66f0702013-01-31 16:57:45 +01001641#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001642 if( padlen > 0 && correct == 0)
1643 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001644#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001645 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001647 else
1648#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1649 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001650 {
1651 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001652 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001655 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001656#endif /* POLARSSL_CIPHER_MODE_CBC &&
1657 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001658 {
1659 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1660 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1661 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
1663 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1664 ssl->in_msg, ssl->in_msglen );
1665
1666 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001667 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1670 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1671 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1672 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1673 POLARSSL_MODE_GCM )
1674 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001675 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1676
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001677 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1680 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001684#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001685 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1686 {
1687 ssl_mac( &ssl->transform_in->md_ctx_dec,
1688 ssl->transform_in->mac_dec,
1689 ssl->in_msg, ssl->in_msglen,
1690 ssl->in_ctr, ssl->in_msgtype );
1691 }
1692 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001693#endif /* POLARSSL_SSL_PROTO_SSL3 */
1694#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001695 defined(POLARSSL_SSL_PROTO_TLS1_2)
1696 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1697 {
1698 /*
1699 * Process MAC and always update for padlen afterwards to make
1700 * total time independent of padlen
1701 *
1702 * extra_run compensates MAC check for padlen
1703 *
1704 * Known timing attacks:
1705 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1706 *
1707 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1708 * correctly. (We round down instead of up, so -56 is the correct
1709 * value for our calculations instead of -55)
1710 */
1711 size_t j, extra_run = 0;
1712 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1713 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001714
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001715 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001716
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001717 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1718 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1719 ssl->in_msglen );
1720 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1721 ssl->in_msg + ssl->in_msglen );
1722 for( j = 0; j < extra_run; j++ )
1723 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001724
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1726 }
1727 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001728#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001729 POLARSSL_SSL_PROTO_TLS1_2 */
1730 {
1731 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1732 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1733 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001735 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1736 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1737 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001739 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001740 ssl->transform_in->maclen ) != 0 )
1741 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001742#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001743 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001744#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001745 correct = 0;
1746 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001748 /*
1749 * Finally check the correct flag
1750 */
1751 if( correct == 0 )
1752 return( POLARSSL_ERR_SSL_INVALID_MAC );
1753 }
1754#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
1756 if( ssl->in_msglen == 0 )
1757 {
1758 ssl->nb_zero++;
1759
1760 /*
1761 * Three or more empty messages may be a DoS attack
1762 * (excessive CPU consumption).
1763 */
1764 if( ssl->nb_zero > 3 )
1765 {
1766 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1767 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001768 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 }
1770 }
1771 else
1772 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001773
Paul Bakker23986e52011-04-24 08:57:21 +00001774 for( i = 8; i > 0; i-- )
1775 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 break;
1777
1778 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1779
1780 return( 0 );
1781}
1782
Paul Bakker2770fbd2012-07-03 13:30:23 +00001783#if defined(POLARSSL_ZLIB_SUPPORT)
1784/*
1785 * Compression/decompression functions
1786 */
1787static int ssl_compress_buf( ssl_context *ssl )
1788{
1789 int ret;
1790 unsigned char *msg_post = ssl->out_msg;
1791 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001792 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001793
1794 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1795
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001796 if( len_pre == 0 )
1797 return( 0 );
1798
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799 memcpy( msg_pre, ssl->out_msg, len_pre );
1800
1801 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1802 ssl->out_msglen ) );
1803
1804 SSL_DEBUG_BUF( 4, "before compression: output payload",
1805 ssl->out_msg, ssl->out_msglen );
1806
Paul Bakker48916f92012-09-16 19:57:18 +00001807 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1808 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1809 ssl->transform_out->ctx_deflate.next_out = msg_post;
1810 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811
Paul Bakker48916f92012-09-16 19:57:18 +00001812 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001813 if( ret != Z_OK )
1814 {
1815 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1816 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1817 }
1818
Paul Bakker48916f92012-09-16 19:57:18 +00001819 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001820
Paul Bakker2770fbd2012-07-03 13:30:23 +00001821 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1822 ssl->out_msglen ) );
1823
1824 SSL_DEBUG_BUF( 4, "after compression: output payload",
1825 ssl->out_msg, ssl->out_msglen );
1826
1827 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1828
1829 return( 0 );
1830}
1831
1832static int ssl_decompress_buf( ssl_context *ssl )
1833{
1834 int ret;
1835 unsigned char *msg_post = ssl->in_msg;
1836 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001837 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001838
1839 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1840
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001841 if( len_pre == 0 )
1842 return( 0 );
1843
Paul Bakker2770fbd2012-07-03 13:30:23 +00001844 memcpy( msg_pre, ssl->in_msg, len_pre );
1845
1846 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1847 ssl->in_msglen ) );
1848
1849 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1850 ssl->in_msg, ssl->in_msglen );
1851
Paul Bakker48916f92012-09-16 19:57:18 +00001852 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1853 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1854 ssl->transform_in->ctx_inflate.next_out = msg_post;
1855 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001856
Paul Bakker48916f92012-09-16 19:57:18 +00001857 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001858 if( ret != Z_OK )
1859 {
1860 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1861 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1862 }
1863
Paul Bakker48916f92012-09-16 19:57:18 +00001864 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001865
Paul Bakker2770fbd2012-07-03 13:30:23 +00001866 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1867 ssl->in_msglen ) );
1868
1869 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1870 ssl->in_msg, ssl->in_msglen );
1871
1872 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1873
1874 return( 0 );
1875}
1876#endif /* POLARSSL_ZLIB_SUPPORT */
1877
Paul Bakker5121ce52009-01-03 21:22:43 +00001878/*
1879 * Fill the input message buffer
1880 */
Paul Bakker23986e52011-04-24 08:57:21 +00001881int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001882{
Paul Bakker23986e52011-04-24 08:57:21 +00001883 int ret;
1884 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
1886 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1887
1888 while( ssl->in_left < nb_want )
1889 {
1890 len = nb_want - ssl->in_left;
1891 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1892
1893 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1894 ssl->in_left, nb_want ) );
1895 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1896
Paul Bakker831a7552011-05-18 13:32:51 +00001897 if( ret == 0 )
1898 return( POLARSSL_ERR_SSL_CONN_EOF );
1899
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 if( ret < 0 )
1901 return( ret );
1902
1903 ssl->in_left += ret;
1904 }
1905
1906 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1907
1908 return( 0 );
1909}
1910
1911/*
1912 * Flush any data not yet written
1913 */
1914int ssl_flush_output( ssl_context *ssl )
1915{
1916 int ret;
1917 unsigned char *buf;
1918
1919 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1920
1921 while( ssl->out_left > 0 )
1922 {
1923 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1924 5 + ssl->out_msglen, ssl->out_left ) );
1925
Paul Bakker5bd42292012-12-19 14:40:42 +01001926 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001928
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1930
1931 if( ret <= 0 )
1932 return( ret );
1933
1934 ssl->out_left -= ret;
1935 }
1936
1937 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1938
1939 return( 0 );
1940}
1941
1942/*
1943 * Record layer functions
1944 */
1945int ssl_write_record( ssl_context *ssl )
1946{
Paul Bakker05ef8352012-05-08 09:17:57 +00001947 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001948 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
1950 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1951
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1953 {
1954 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1955 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1956 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1957
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001958 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1959 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 }
1961
Paul Bakker2770fbd2012-07-03 13:30:23 +00001962#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001963 if( ssl->transform_out != NULL &&
1964 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001965 {
1966 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1967 {
1968 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1969 return( ret );
1970 }
1971
1972 len = ssl->out_msglen;
1973 }
1974#endif /*POLARSSL_ZLIB_SUPPORT */
1975
Paul Bakker05ef8352012-05-08 09:17:57 +00001976#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1977 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001979 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001980
Paul Bakker05ef8352012-05-08 09:17:57 +00001981 ret = ssl_hw_record_write( ssl );
1982 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1983 {
1984 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1985 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1986 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001987
1988 if( ret == 0 )
1989 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001990 }
1991#endif
1992 if( !done )
1993 {
1994 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1995 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1996 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1998 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001999
Paul Bakker48916f92012-09-16 19:57:18 +00002000 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002001 {
2002 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2003 {
2004 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2005 return( ret );
2006 }
2007
2008 len = ssl->out_msglen;
2009 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2010 ssl->out_hdr[4] = (unsigned char)( len );
2011 }
2012
2013 ssl->out_left = 5 + ssl->out_msglen;
2014
2015 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2016 "version = [%d:%d], msglen = %d",
2017 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2018 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2019
2020 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002021 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 }
2023
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2025 {
2026 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2027 return( ret );
2028 }
2029
2030 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2031
2032 return( 0 );
2033}
2034
2035int ssl_read_record( ssl_context *ssl )
2036{
Paul Bakker05ef8352012-05-08 09:17:57 +00002037 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002038
2039 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2040
Paul Bakker68884e32013-01-07 18:20:04 +01002041 SSL_DEBUG_BUF( 4, "input record from network",
2042 ssl->in_hdr, 5 + ssl->in_msglen );
2043
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 if( ssl->in_hslen != 0 &&
2045 ssl->in_hslen < ssl->in_msglen )
2046 {
2047 /*
2048 * Get next Handshake message in the current record
2049 */
2050 ssl->in_msglen -= ssl->in_hslen;
2051
Paul Bakker8934a982011-08-05 11:11:53 +00002052 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002053 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002054
2055 ssl->in_hslen = 4;
2056 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2057
2058 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2059 " %d, type = %d, hslen = %d",
2060 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2061
2062 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2063 {
2064 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002065 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 }
2067
2068 if( ssl->in_msglen < ssl->in_hslen )
2069 {
2070 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002071 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 }
2073
Paul Bakker48916f92012-09-16 19:57:18 +00002074 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002075
2076 return( 0 );
2077 }
2078
2079 ssl->in_hslen = 0;
2080
2081 /*
2082 * Read the record header and validate it
2083 */
2084 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2085 {
2086 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2087 return( ret );
2088 }
2089
2090 ssl->in_msgtype = ssl->in_hdr[0];
2091 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2092
2093 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2094 "version = [%d:%d], msglen = %d",
2095 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2096 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2097
2098 if( ssl->in_hdr[1] != ssl->major_ver )
2099 {
2100 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002101 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 }
2103
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002104 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 {
2106 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002107 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
2109
2110 /*
2111 * Make sure the message length is acceptable
2112 */
Paul Bakker48916f92012-09-16 19:57:18 +00002113 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 {
2115 if( ssl->in_msglen < 1 ||
2116 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2117 {
2118 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002119 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 }
2121 }
2122 else
2123 {
Paul Bakker48916f92012-09-16 19:57:18 +00002124 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 {
2126 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002127 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
2129
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002130#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002132 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 {
2134 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002135 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002137#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002139#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2140 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 /*
2142 * TLS encrypted messages can have up to 256 bytes of padding
2143 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002144 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002145 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 {
2147 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002148 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 }
2152
2153 /*
2154 * Read and optionally decrypt the message contents
2155 */
2156 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2157 {
2158 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2159 return( ret );
2160 }
2161
2162 SSL_DEBUG_BUF( 4, "input record from network",
2163 ssl->in_hdr, 5 + ssl->in_msglen );
2164
Paul Bakker05ef8352012-05-08 09:17:57 +00002165#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2166 if( ssl_hw_record_read != NULL)
2167 {
2168 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2169
2170 ret = ssl_hw_record_read( ssl );
2171 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2172 {
2173 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2174 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2175 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002176
2177 if( ret == 0 )
2178 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002179 }
2180#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002181 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 {
2183 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2184 {
Paul Bakker40865c82013-01-31 17:13:13 +01002185#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2186 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2187 {
2188 ssl_send_alert_message( ssl,
2189 SSL_ALERT_LEVEL_FATAL,
2190 SSL_ALERT_MSG_BAD_RECORD_MAC );
2191 }
2192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2194 return( ret );
2195 }
2196
2197 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2198 ssl->in_msg, ssl->in_msglen );
2199
2200 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002203 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 }
2205 }
2206
Paul Bakker2770fbd2012-07-03 13:30:23 +00002207#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002208 if( ssl->transform_in != NULL &&
2209 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002210 {
2211 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2212 {
2213 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2214 return( ret );
2215 }
2216
2217 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2218 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2219 }
2220#endif /* POLARSSL_ZLIB_SUPPORT */
2221
Paul Bakker0a925182012-04-16 06:46:41 +00002222 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2223 ssl->in_msgtype != SSL_MSG_ALERT &&
2224 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2225 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2226 {
2227 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2228
Paul Bakker48916f92012-09-16 19:57:18 +00002229 if( ( ret = ssl_send_alert_message( ssl,
2230 SSL_ALERT_LEVEL_FATAL,
2231 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002232 {
2233 return( ret );
2234 }
2235
2236 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2237 }
2238
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2240 {
2241 ssl->in_hslen = 4;
2242 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2243
2244 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2245 " %d, type = %d, hslen = %d",
2246 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2247
2248 /*
2249 * Additional checks to validate the handshake header
2250 */
2251 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2252 {
2253 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002254 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
2256
2257 if( ssl->in_msglen < ssl->in_hslen )
2258 {
2259 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002260 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262
Paul Bakker48916f92012-09-16 19:57:18 +00002263 if( ssl->state != SSL_HANDSHAKE_OVER )
2264 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
2266
2267 if( ssl->in_msgtype == SSL_MSG_ALERT )
2268 {
2269 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2270 ssl->in_msg[0], ssl->in_msg[1] ) );
2271
2272 /*
2273 * Ignore non-fatal alerts, except close_notify
2274 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002275 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002277 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2278 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002279 /**
2280 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2281 * error identifier.
2282 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002283 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 }
2285
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002286 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2287 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 {
2289 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002290 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 }
2292 }
2293
2294 ssl->in_left = 0;
2295
2296 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2297
2298 return( 0 );
2299}
2300
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002301int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2302{
2303 int ret;
2304
2305 if( ( ret = ssl_send_alert_message( ssl,
2306 SSL_ALERT_LEVEL_FATAL,
2307 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2308 {
2309 return( ret );
2310 }
2311
2312 return( 0 );
2313}
2314
Paul Bakker0a925182012-04-16 06:46:41 +00002315int ssl_send_alert_message( ssl_context *ssl,
2316 unsigned char level,
2317 unsigned char message )
2318{
2319 int ret;
2320
2321 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2322
2323 ssl->out_msgtype = SSL_MSG_ALERT;
2324 ssl->out_msglen = 2;
2325 ssl->out_msg[0] = level;
2326 ssl->out_msg[1] = message;
2327
2328 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2329 {
2330 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2331 return( ret );
2332 }
2333
2334 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2335
2336 return( 0 );
2337}
2338
Paul Bakker5121ce52009-01-03 21:22:43 +00002339/*
2340 * Handshake functions
2341 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002342#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2343 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2344 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2345 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2346 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2347 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2348 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002349int ssl_write_certificate( ssl_context *ssl )
2350{
Paul Bakkered27a042013-04-18 22:46:23 +02002351 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002352 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
2354 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2355
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002356 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002357 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2358 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002359 {
2360 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2361 ssl->state++;
2362 return( 0 );
2363 }
2364
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002365 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002366 return( ret );
2367}
2368
2369int ssl_parse_certificate( ssl_context *ssl )
2370{
2371 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2372 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2373
2374 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2375
2376 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002377 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2378 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002379 {
2380 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2381 ssl->state++;
2382 return( 0 );
2383 }
2384
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002385 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386 return( ret );
2387}
2388#else
2389int ssl_write_certificate( ssl_context *ssl )
2390{
2391 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2392 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002393 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2395
2396 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2397
2398 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002399 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2400 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002401 {
2402 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2403 ssl->state++;
2404 return( 0 );
2405 }
2406
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 if( ssl->endpoint == SSL_IS_CLIENT )
2408 {
2409 if( ssl->client_auth == 0 )
2410 {
2411 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2412 ssl->state++;
2413 return( 0 );
2414 }
2415
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002416#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 /*
2418 * If using SSLv3 and got no cert, send an Alert message
2419 * (otherwise an empty Certificate message will be sent).
2420 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002421 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2423 {
2424 ssl->out_msglen = 2;
2425 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2427 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
2429 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2430 goto write_msg;
2431 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002432#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 }
2434 else /* SSL_IS_SERVER */
2435 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002436 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 {
2438 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002439 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 }
2441 }
2442
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002443 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 /*
2446 * 0 . 0 handshake type
2447 * 1 . 3 handshake length
2448 * 4 . 6 length of all certs
2449 * 7 . 9 length of cert. 1
2450 * 10 . n-1 peer certificate
2451 * n . n+2 length of cert. 2
2452 * n+3 . ... upper level cert, etc.
2453 */
2454 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002455 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Paul Bakker29087132010-03-21 21:03:34 +00002457 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 {
2459 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002460 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 {
2462 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2463 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002464 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 }
2466
2467 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2468 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2469 ssl->out_msg[i + 2] = (unsigned char)( n );
2470
2471 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2472 i += n; crt = crt->next;
2473 }
2474
2475 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2476 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2477 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2478
2479 ssl->out_msglen = i;
2480 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2481 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2482
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002483#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002484write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002485#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
2487 ssl->state++;
2488
2489 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2490 {
2491 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2492 return( ret );
2493 }
2494
2495 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2496
Paul Bakkered27a042013-04-18 22:46:23 +02002497 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498}
2499
2500int ssl_parse_certificate( ssl_context *ssl )
2501{
Paul Bakkered27a042013-04-18 22:46:23 +02002502 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002503 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002504 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
2506 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2507
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002508 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002509 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002511 {
2512 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2513 ssl->state++;
2514 return( 0 );
2515 }
2516
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002518 ( ssl->authmode == SSL_VERIFY_NONE ||
2519 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002521 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2523 ssl->state++;
2524 return( 0 );
2525 }
2526
2527 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2528 {
2529 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2530 return( ret );
2531 }
2532
2533 ssl->state++;
2534
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 /*
2537 * Check if the client sent an empty certificate
2538 */
2539 if( ssl->endpoint == SSL_IS_SERVER &&
2540 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2541 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002542 if( ssl->in_msglen == 2 &&
2543 ssl->in_msgtype == SSL_MSG_ALERT &&
2544 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2545 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 {
2547 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2548
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002549 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2551 return( 0 );
2552 else
Paul Bakker40e46942009-01-03 21:51:57 +00002553 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002556#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002558#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2559 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 if( ssl->endpoint == SSL_IS_SERVER &&
2561 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2562 {
2563 if( ssl->in_hslen == 7 &&
2564 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2565 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2566 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2567 {
2568 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2569
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002570 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002572 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 else
2574 return( 0 );
2575 }
2576 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002577#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2578 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
2580 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2581 {
2582 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002583 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 }
2585
2586 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2587 {
2588 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002589 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 }
2591
2592 /*
2593 * Same message structure as in ssl_write_certificate()
2594 */
2595 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2596
2597 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2598 {
2599 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002600 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
2602
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002603 /* In case we tried to reuse a session but it failed */
2604 if( ssl->session_negotiate->peer_cert != NULL )
2605 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002606 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002607 polarssl_free( ssl->session_negotiate->peer_cert );
2608 }
2609
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002610 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2611 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 {
2613 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002614 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002615 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 }
2617
Paul Bakkerb6b09562013-09-18 14:17:41 +02002618 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
2620 i = 7;
2621
2622 while( i < ssl->in_hslen )
2623 {
2624 if( ssl->in_msg[i] != 0 )
2625 {
2626 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002627 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 }
2629
2630 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2631 | (unsigned int) ssl->in_msg[i + 2];
2632 i += 3;
2633
2634 if( n < 128 || i + n > ssl->in_hslen )
2635 {
2636 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002637 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 }
2639
Paul Bakkerddf26b42013-09-18 13:46:23 +02002640 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2641 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 if( ret != 0 )
2643 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002644 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 return( ret );
2646 }
2647
2648 i += n;
2649 }
2650
Paul Bakker48916f92012-09-16 19:57:18 +00002651 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
2653 if( ssl->authmode != SSL_VERIFY_NONE )
2654 {
2655 if( ssl->ca_chain == NULL )
2656 {
2657 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002658 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 }
2660
Paul Bakkerddf26b42013-09-18 13:46:23 +02002661 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2662 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2663 &ssl->session_negotiate->verify_result,
2664 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
2666 if( ret != 0 )
2667 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2668
2669 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2670 ret = 0;
2671 }
2672
2673 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2674
2675 return( ret );
2676}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002677#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2678 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2679 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2680 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2681 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2682 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2683 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685int ssl_write_change_cipher_spec( ssl_context *ssl )
2686{
2687 int ret;
2688
2689 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2690
2691 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2692 ssl->out_msglen = 1;
2693 ssl->out_msg[0] = 1;
2694
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 ssl->state++;
2696
2697 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2698 {
2699 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2700 return( ret );
2701 }
2702
2703 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2704
2705 return( 0 );
2706}
2707
2708int ssl_parse_change_cipher_spec( ssl_context *ssl )
2709{
2710 int ret;
2711
2712 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2713
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2715 {
2716 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2717 return( ret );
2718 }
2719
2720 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2721 {
2722 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002723 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 }
2725
2726 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2727 {
2728 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002729 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 }
2731
2732 ssl->state++;
2733
2734 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2735
2736 return( 0 );
2737}
2738
Paul Bakker41c83d32013-03-20 14:39:14 +01002739void ssl_optimize_checksum( ssl_context *ssl,
2740 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002741{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002742 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002743
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002744#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2745 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002746 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002747 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002748 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002749#endif
2750#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2751#if defined(POLARSSL_SHA512_C)
2752 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2753 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2754 else
2755#endif
2756#if defined(POLARSSL_SHA256_C)
2757 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002758 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002759 else
2760#endif
2761#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2762 /* Should never happen */
2763 return;
Paul Bakker380da532012-04-18 16:10:25 +00002764}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002765
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002766static void ssl_update_checksum_start( ssl_context *ssl,
2767 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002768{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002769#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2770 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002771 md5_update( &ssl->handshake->fin_md5 , buf, len );
2772 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002773#endif
2774#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2775#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002776 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002777#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002778#if defined(POLARSSL_SHA512_C)
2779 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002780#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002781#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002782}
2783
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 Bakkerb6c5d2e2013-06-25 16:25:17 +02002786static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2787 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002788{
Paul Bakker48916f92012-09-16 19:57:18 +00002789 md5_update( &ssl->handshake->fin_md5 , buf, len );
2790 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002791}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002792#endif
Paul Bakker380da532012-04-18 16:10:25 +00002793
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2795#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002796static void ssl_update_checksum_sha256( ssl_context *ssl,
2797 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002798{
Paul Bakker9e36f042013-06-30 14:34:05 +02002799 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002800}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002801#endif
Paul Bakker380da532012-04-18 16:10:25 +00002802
Paul Bakker9e36f042013-06-30 14:34:05 +02002803#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002804static void ssl_update_checksum_sha384( ssl_context *ssl,
2805 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002806{
Paul Bakker9e36f042013-06-30 14:34:05 +02002807 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002808}
Paul Bakker769075d2012-11-24 11:26:46 +01002809#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002810#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002811
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002812#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002813static void ssl_calc_finished_ssl(
2814 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002815{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002816 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002817 md5_context md5;
2818 sha1_context sha1;
2819
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 unsigned char padbuf[48];
2821 unsigned char md5sum[16];
2822 unsigned char sha1sum[20];
2823
Paul Bakker48916f92012-09-16 19:57:18 +00002824 ssl_session *session = ssl->session_negotiate;
2825 if( !session )
2826 session = ssl->session;
2827
Paul Bakker1ef83d62012-04-11 12:09:53 +00002828 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2829
Paul Bakker48916f92012-09-16 19:57:18 +00002830 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2831 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
2833 /*
2834 * SSLv3:
2835 * hash =
2836 * MD5( master + pad2 +
2837 * MD5( handshake + sender + master + pad1 ) )
2838 * + SHA1( master + pad2 +
2839 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 */
2841
Paul Bakker90995b52013-06-24 19:20:35 +02002842#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002844 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002845#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
Paul Bakker90995b52013-06-24 19:20:35 +02002847#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002849 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002850#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Paul Bakker3c2122f2013-06-24 19:03:14 +02002852 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2853 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
Paul Bakker1ef83d62012-04-11 12:09:53 +00002855 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Paul Bakker3c2122f2013-06-24 19:03:14 +02002857 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002858 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002859 md5_update( &md5, padbuf, 48 );
2860 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Paul Bakker3c2122f2013-06-24 19:03:14 +02002862 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002863 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002864 sha1_update( &sha1, padbuf, 40 );
2865 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Paul Bakker1ef83d62012-04-11 12:09:53 +00002867 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002870 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 md5_update( &md5, padbuf, 48 );
2872 md5_update( &md5, md5sum, 16 );
2873 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Paul Bakker1ef83d62012-04-11 12:09:53 +00002875 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002876 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002877 sha1_update( &sha1, padbuf , 40 );
2878 sha1_update( &sha1, sha1sum, 20 );
2879 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
Paul Bakker1ef83d62012-04-11 12:09:53 +00002881 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Paul Bakker1ef83d62012-04-11 12:09:53 +00002883 memset( &md5, 0, sizeof( md5_context ) );
2884 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
2886 memset( padbuf, 0, sizeof( padbuf ) );
2887 memset( md5sum, 0, sizeof( md5sum ) );
2888 memset( sha1sum, 0, sizeof( sha1sum ) );
2889
2890 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2891}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002892#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002894#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002895static void ssl_calc_finished_tls(
2896 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002897{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002899 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002903
Paul Bakker48916f92012-09-16 19:57:18 +00002904 ssl_session *session = ssl->session_negotiate;
2905 if( !session )
2906 session = ssl->session;
2907
Paul Bakker1ef83d62012-04-11 12:09:53 +00002908 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Paul Bakker48916f92012-09-16 19:57:18 +00002910 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2911 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Paul Bakker1ef83d62012-04-11 12:09:53 +00002913 /*
2914 * TLSv1:
2915 * hash = PRF( master, finished_label,
2916 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2917 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Paul Bakker90995b52013-06-24 19:20:35 +02002919#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002920 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2921 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002922#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923
Paul Bakker90995b52013-06-24 19:20:35 +02002924#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2926 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002927#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002928
2929 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002930 ? "client finished"
2931 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002932
2933 md5_finish( &md5, padbuf );
2934 sha1_finish( &sha1, padbuf + 16 );
2935
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002936 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002937 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002938
2939 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2940
2941 memset( &md5, 0, sizeof( md5_context ) );
2942 memset( &sha1, 0, sizeof( sha1_context ) );
2943
2944 memset( padbuf, 0, sizeof( padbuf ) );
2945
2946 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2947}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002948#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002950#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2951#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002952static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953 ssl_context *ssl, unsigned char *buf, int from )
2954{
2955 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002956 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002957 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958 unsigned char padbuf[32];
2959
Paul Bakker48916f92012-09-16 19:57:18 +00002960 ssl_session *session = ssl->session_negotiate;
2961 if( !session )
2962 session = ssl->session;
2963
Paul Bakker380da532012-04-18 16:10:25 +00002964 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965
Paul Bakker9e36f042013-06-30 14:34:05 +02002966 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002967
2968 /*
2969 * TLSv1.2:
2970 * hash = PRF( master, finished_label,
2971 * Hash( handshake ) )[0.11]
2972 */
2973
Paul Bakker9e36f042013-06-30 14:34:05 +02002974#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002975 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002976 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002977#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002978
2979 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002980 ? "client finished"
2981 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982
Paul Bakker9e36f042013-06-30 14:34:05 +02002983 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002984
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002985 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002986 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002987
2988 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2989
Paul Bakker9e36f042013-06-30 14:34:05 +02002990 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002991
2992 memset( padbuf, 0, sizeof( padbuf ) );
2993
2994 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2995}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002996#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002997
Paul Bakker9e36f042013-06-30 14:34:05 +02002998#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002999static void ssl_calc_finished_tls_sha384(
3000 ssl_context *ssl, unsigned char *buf, int from )
3001{
3002 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003003 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003004 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003005 unsigned char padbuf[48];
3006
Paul Bakker48916f92012-09-16 19:57:18 +00003007 ssl_session *session = ssl->session_negotiate;
3008 if( !session )
3009 session = ssl->session;
3010
Paul Bakker380da532012-04-18 16:10:25 +00003011 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003012
Paul Bakker9e36f042013-06-30 14:34:05 +02003013 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003014
3015 /*
3016 * TLSv1.2:
3017 * hash = PRF( master, finished_label,
3018 * Hash( handshake ) )[0.11]
3019 */
3020
Paul Bakker9e36f042013-06-30 14:34:05 +02003021#if !defined(POLARSSL_SHA512_ALT)
3022 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3023 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003024#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003025
3026 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003027 ? "client finished"
3028 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003029
Paul Bakker9e36f042013-06-30 14:34:05 +02003030 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003031
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003032 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003033 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003034
3035 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3036
Paul Bakker9e36f042013-06-30 14:34:05 +02003037 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003038
3039 memset( padbuf, 0, sizeof( padbuf ) );
3040
3041 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3042}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003043#endif /* POLARSSL_SHA512_C */
3044#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003045
Paul Bakker48916f92012-09-16 19:57:18 +00003046void ssl_handshake_wrapup( ssl_context *ssl )
3047{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003048 int resume = ssl->handshake->resume;
3049
Paul Bakker48916f92012-09-16 19:57:18 +00003050 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3051
3052 /*
3053 * Free our handshake params
3054 */
3055 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003056 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003057 ssl->handshake = NULL;
3058
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003059 if( ssl->renegotiation == SSL_RENEGOTIATION )
3060 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3061
Paul Bakker48916f92012-09-16 19:57:18 +00003062 /*
3063 * Switch in our now active transform context
3064 */
3065 if( ssl->transform )
3066 {
3067 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003068 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003069 }
3070 ssl->transform = ssl->transform_negotiate;
3071 ssl->transform_negotiate = NULL;
3072
Paul Bakker0a597072012-09-25 21:55:46 +00003073 if( ssl->session )
3074 {
3075 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003076 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003077 }
3078 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003079 ssl->session_negotiate = NULL;
3080
Paul Bakker0a597072012-09-25 21:55:46 +00003081 /*
3082 * Add cache entry
3083 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003084 if( ssl->f_set_cache != NULL &&
3085 ssl->session->length != 0 &&
3086 resume == 0 )
3087 {
Paul Bakker0a597072012-09-25 21:55:46 +00003088 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3089 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003090 }
Paul Bakker0a597072012-09-25 21:55:46 +00003091
Paul Bakker48916f92012-09-16 19:57:18 +00003092 ssl->state++;
3093
3094 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3095}
3096
Paul Bakker1ef83d62012-04-11 12:09:53 +00003097int ssl_write_finished( ssl_context *ssl )
3098{
3099 int ret, hash_len;
3100
3101 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3102
Paul Bakker92be97b2013-01-02 17:30:03 +01003103 /*
3104 * Set the out_msg pointer to the correct location based on IV length
3105 */
3106 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3107 {
3108 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3109 ssl->transform_negotiate->fixed_ivlen;
3110 }
3111 else
3112 ssl->out_msg = ssl->out_iv;
3113
Paul Bakker48916f92012-09-16 19:57:18 +00003114 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003115
3116 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3118
Paul Bakker48916f92012-09-16 19:57:18 +00003119 ssl->verify_data_len = hash_len;
3120 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3121
Paul Bakker5121ce52009-01-03 21:22:43 +00003122 ssl->out_msglen = 4 + hash_len;
3123 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3124 ssl->out_msg[0] = SSL_HS_FINISHED;
3125
3126 /*
3127 * In case of session resuming, invert the client and server
3128 * ChangeCipherSpec messages order.
3129 */
Paul Bakker0a597072012-09-25 21:55:46 +00003130 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003131 {
3132 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003133 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 else
3135 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3136 }
3137 else
3138 ssl->state++;
3139
Paul Bakker48916f92012-09-16 19:57:18 +00003140 /*
3141 * Switch to our negotiated transform and session parameters for outbound data.
3142 */
3143 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3144 ssl->transform_out = ssl->transform_negotiate;
3145 ssl->session_out = ssl->session_negotiate;
3146 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003147
Paul Bakker07eb38b2012-12-19 14:42:06 +01003148#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3149 if( ssl_hw_record_activate != NULL)
3150 {
3151 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3152 {
3153 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3154 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3155 }
3156 }
3157#endif
3158
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3160 {
3161 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3162 return( ret );
3163 }
3164
3165 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3166
3167 return( 0 );
3168}
3169
3170int ssl_parse_finished( ssl_context *ssl )
3171{
Paul Bakker23986e52011-04-24 08:57:21 +00003172 int ret;
3173 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 unsigned char buf[36];
3175
3176 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3177
Paul Bakker48916f92012-09-16 19:57:18 +00003178 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003179
Paul Bakker48916f92012-09-16 19:57:18 +00003180 /*
3181 * Switch to our negotiated transform and session parameters for inbound data.
3182 */
3183 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3184 ssl->transform_in = ssl->transform_negotiate;
3185 ssl->session_in = ssl->session_negotiate;
3186 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003187
Paul Bakker92be97b2013-01-02 17:30:03 +01003188 /*
3189 * Set the in_msg pointer to the correct location based on IV length
3190 */
3191 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3192 {
3193 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3194 ssl->transform_negotiate->fixed_ivlen;
3195 }
3196 else
3197 ssl->in_msg = ssl->in_iv;
3198
Paul Bakker07eb38b2012-12-19 14:42:06 +01003199#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3200 if( ssl_hw_record_activate != NULL)
3201 {
3202 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3203 {
3204 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3205 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3206 }
3207 }
3208#endif
3209
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3211 {
3212 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3213 return( ret );
3214 }
3215
3216 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3217 {
3218 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003219 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 }
3221
Paul Bakker1ef83d62012-04-11 12:09:53 +00003222 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3224
3225 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3226 ssl->in_hslen != 4 + hash_len )
3227 {
3228 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003229 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230 }
3231
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003232 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 {
3234 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003235 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
3237
Paul Bakker48916f92012-09-16 19:57:18 +00003238 ssl->verify_data_len = hash_len;
3239 memcpy( ssl->peer_verify_data, buf, hash_len );
3240
Paul Bakker0a597072012-09-25 21:55:46 +00003241 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003242 {
3243 if( ssl->endpoint == SSL_IS_CLIENT )
3244 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3245
3246 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003247 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 }
3249 else
3250 ssl->state++;
3251
3252 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3253
3254 return( 0 );
3255}
3256
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003257static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003258{
3259 if( ssl->transform_negotiate )
3260 ssl_transform_free( ssl->transform_negotiate );
3261 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003262 {
3263 ssl->transform_negotiate =
3264 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3265 }
Paul Bakker48916f92012-09-16 19:57:18 +00003266
3267 if( ssl->session_negotiate )
3268 ssl_session_free( ssl->session_negotiate );
3269 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003270 {
3271 ssl->session_negotiate =
3272 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3273 }
Paul Bakker48916f92012-09-16 19:57:18 +00003274
3275 if( ssl->handshake )
3276 ssl_handshake_free( ssl->handshake );
3277 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003278 {
3279 ssl->handshake = (ssl_handshake_params *)
3280 polarssl_malloc( sizeof(ssl_handshake_params) );
3281 }
Paul Bakker48916f92012-09-16 19:57:18 +00003282
3283 if( ssl->handshake == NULL ||
3284 ssl->transform_negotiate == NULL ||
3285 ssl->session_negotiate == NULL )
3286 {
3287 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3288 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3289 }
3290
3291 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3292 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3293 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3294
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003295#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3296 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003297 md5_starts( &ssl->handshake->fin_md5 );
3298 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003299#endif
3300#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3301#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003302 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003303#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003304#if defined(POLARSSL_SHA512_C)
3305 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003306#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003307#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003308
3309 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003310 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003311
Paul Bakker61d113b2013-07-04 11:51:43 +02003312#if defined(POLARSSL_ECDH_C)
3313 ecdh_init( &ssl->handshake->ecdh_ctx );
3314#endif
3315
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003316#if defined(POLARSSL_X509_CRT_PARSE_C)
3317 ssl->handshake->key_cert = ssl->key_cert;
3318#endif
3319
Paul Bakker48916f92012-09-16 19:57:18 +00003320 return( 0 );
3321}
3322
Paul Bakker5121ce52009-01-03 21:22:43 +00003323/*
3324 * Initialize an SSL context
3325 */
3326int ssl_init( ssl_context *ssl )
3327{
Gergely Budai987bfb52014-01-19 21:48:42 +01003328
3329#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
3330 /*
3331 * ECDHE allowed curves and preference list
3332 *
3333 * We start with the most secure curves. From the same size curves, we prefer
3334 * the SECP ones because they are much faster.
3335 *
3336 * TODO: Add the Montgomery curves
3337 */
3338 static const ecp_group_id ecdh_default_curve_list[] =
3339 {
3340#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
3341 POLARSSL_ECP_DP_SECP521R1,
3342#endif
3343#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
3344 POLARSSL_ECP_DP_BP512R1,
3345#endif
3346#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
3347 POLARSSL_ECP_DP_SECP384R1,
3348#endif
3349#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
3350 POLARSSL_ECP_DP_BP384R1,
3351#endif
3352#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
3353 POLARSSL_ECP_DP_SECP256R1,
3354#endif
3355#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
3356 POLARSSL_ECP_DP_BP256R1,
3357#endif
3358#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
3359 POLARSSL_ECP_DP_SECP224R1,
3360#endif
3361#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
3362 POLARSSL_ECP_DP_SECP192R1,
3363#endif
3364 POLARSSL_ECP_DP_NONE
3365 };
3366#endif
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
Gergely Budai987bfb52014-01-19 21:48:42 +01003427#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
3428 ssl->ecdh_curve_list = ecdh_default_curve_list;
3429#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
Paul Bakker0be444a2013-08-27 21:55:01 +02003843#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003844int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003845{
3846 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003847 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003848
3849 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003850
3851 if( ssl->hostname_len + 1 == 0 )
3852 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3853
Paul Bakker6e339b52013-07-03 13:37:05 +02003854 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003855
Paul Bakkerb15b8512012-01-13 13:44:06 +00003856 if( ssl->hostname == NULL )
3857 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3858
Paul Bakker3c2122f2013-06-24 19:03:14 +02003859 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003860 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003861
Paul Bakker40ea7de2009-05-03 10:18:48 +00003862 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003863
3864 return( 0 );
3865}
3866
Paul Bakker5701cdc2012-09-27 21:49:42 +00003867void ssl_set_sni( ssl_context *ssl,
3868 int (*f_sni)(void *, ssl_context *,
3869 const unsigned char *, size_t),
3870 void *p_sni )
3871{
3872 ssl->f_sni = f_sni;
3873 ssl->p_sni = p_sni;
3874}
Paul Bakker0be444a2013-08-27 21:55:01 +02003875#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003876
Paul Bakker490ecc82011-10-06 13:04:09 +00003877void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3878{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003879 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3880 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3881 {
3882 ssl->max_major_ver = major;
3883 ssl->max_minor_ver = minor;
3884 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003885}
3886
Paul Bakker1d29fb52012-09-28 13:28:45 +00003887void ssl_set_min_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->min_major_ver = major;
3893 ssl->min_minor_ver = minor;
3894 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003895}
3896
Paul Bakker05decb22013-08-15 13:33:48 +02003897#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003898int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3899{
Paul Bakker77e257e2013-12-16 15:29:52 +01003900 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003901 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003902 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003903 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003904 }
3905
3906 ssl->mfl_code = mfl_code;
3907
3908 return( 0 );
3909}
Paul Bakker05decb22013-08-15 13:33:48 +02003910#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003911
Paul Bakker1f2bc622013-08-15 13:45:55 +02003912#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003913int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003914{
3915 if( ssl->endpoint != SSL_IS_CLIENT )
3916 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3917
Paul Bakker8c1ede62013-07-19 14:14:37 +02003918 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003919
3920 return( 0 );
3921}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003922#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003923
Paul Bakker48916f92012-09-16 19:57:18 +00003924void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3925{
3926 ssl->disable_renegotiation = renegotiation;
3927}
3928
3929void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3930{
3931 ssl->allow_legacy_renegotiation = allow_legacy;
3932}
3933
Paul Bakkera503a632013-08-14 13:48:06 +02003934#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003935int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3936{
3937 ssl->session_tickets = use_tickets;
3938
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003939 if( ssl->endpoint == SSL_IS_CLIENT )
3940 return( 0 );
3941
3942 if( ssl->f_rng == NULL )
3943 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3944
3945 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003946}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003947
3948void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3949{
3950 ssl->ticket_lifetime = lifetime;
3951}
Paul Bakkera503a632013-08-14 13:48:06 +02003952#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003953
Paul Bakker5121ce52009-01-03 21:22:43 +00003954/*
3955 * SSL get accessors
3956 */
Paul Bakker23986e52011-04-24 08:57:21 +00003957size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003958{
3959 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3960}
3961
Paul Bakkerff60ee62010-03-16 21:09:09 +00003962int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003963{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003964 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003965}
3966
Paul Bakkere3166ce2011-01-27 17:40:50 +00003967const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003968{
Paul Bakker926c8e42013-03-06 10:23:34 +01003969 if( ssl == NULL || ssl->session == NULL )
3970 return NULL;
3971
Paul Bakkere3166ce2011-01-27 17:40:50 +00003972 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003973}
3974
Paul Bakker43ca69c2011-01-15 17:35:19 +00003975const char *ssl_get_version( const ssl_context *ssl )
3976{
3977 switch( ssl->minor_ver )
3978 {
3979 case SSL_MINOR_VERSION_0:
3980 return( "SSLv3.0" );
3981
3982 case SSL_MINOR_VERSION_1:
3983 return( "TLSv1.0" );
3984
3985 case SSL_MINOR_VERSION_2:
3986 return( "TLSv1.1" );
3987
Paul Bakker1ef83d62012-04-11 12:09:53 +00003988 case SSL_MINOR_VERSION_3:
3989 return( "TLSv1.2" );
3990
Paul Bakker43ca69c2011-01-15 17:35:19 +00003991 default:
3992 break;
3993 }
3994 return( "unknown" );
3995}
3996
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003997#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003998const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003999{
4000 if( ssl == NULL || ssl->session == NULL )
4001 return NULL;
4002
4003 return ssl->session->peer_cert;
4004}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004005#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004006
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004007int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4008{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004009 if( ssl == NULL ||
4010 dst == NULL ||
4011 ssl->session == NULL ||
4012 ssl->endpoint != SSL_IS_CLIENT )
4013 {
4014 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4015 }
4016
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004017 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004018}
4019
Paul Bakker5121ce52009-01-03 21:22:43 +00004020/*
Paul Bakker1961b702013-01-25 14:49:24 +01004021 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 */
Paul Bakker1961b702013-01-25 14:49:24 +01004023int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004024{
Paul Bakker40e46942009-01-03 21:51:57 +00004025 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004026
Paul Bakker40e46942009-01-03 21:51:57 +00004027#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004028 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004029 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004030#endif
4031
Paul Bakker40e46942009-01-03 21:51:57 +00004032#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004033 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004034 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004035#endif
4036
Paul Bakker1961b702013-01-25 14:49:24 +01004037 return( ret );
4038}
4039
4040/*
4041 * Perform the SSL handshake
4042 */
4043int ssl_handshake( ssl_context *ssl )
4044{
4045 int ret = 0;
4046
4047 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4048
4049 while( ssl->state != SSL_HANDSHAKE_OVER )
4050 {
4051 ret = ssl_handshake_step( ssl );
4052
4053 if( ret != 0 )
4054 break;
4055 }
4056
Paul Bakker5121ce52009-01-03 21:22:43 +00004057 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4058
4059 return( ret );
4060}
4061
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004062#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004063/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004064 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004065 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004066static int ssl_write_hello_request( ssl_context *ssl )
4067{
4068 int ret;
4069
4070 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4071
4072 ssl->out_msglen = 4;
4073 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4074 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4075
4076 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4077 {
4078 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4079 return( ret );
4080 }
4081
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004082 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4083
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004084 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4085
4086 return( 0 );
4087}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004088#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004089
4090/*
4091 * Actually renegotiate current connection, triggered by either:
4092 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004093 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004094 * - receiving any handshake message on server during ssl_read() after the
4095 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004096 * If the handshake doesn't complete due to waiting for I/O, it will continue
4097 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004098 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004099static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004100{
4101 int ret;
4102
4103 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4104
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004105 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4106 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004107
4108 ssl->state = SSL_HELLO_REQUEST;
4109 ssl->renegotiation = SSL_RENEGOTIATION;
4110
Paul Bakker48916f92012-09-16 19:57:18 +00004111 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4112 {
4113 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4114 return( ret );
4115 }
4116
4117 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4118
4119 return( 0 );
4120}
4121
4122/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004123 * Renegotiate current connection on client,
4124 * or request renegotiation on server
4125 */
4126int ssl_renegotiate( ssl_context *ssl )
4127{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004128 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004129
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004130#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004131 /* On server, just send the request */
4132 if( ssl->endpoint == SSL_IS_SERVER )
4133 {
4134 if( ssl->state != SSL_HANDSHAKE_OVER )
4135 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4136
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004137 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004138 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004139#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004140
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004141#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004142 /*
4143 * On client, either start the renegotiation process or,
4144 * if already in progress, continue the handshake
4145 */
4146 if( ssl->renegotiation != SSL_RENEGOTIATION )
4147 {
4148 if( ssl->state != SSL_HANDSHAKE_OVER )
4149 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4150
4151 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4152 {
4153 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4154 return( ret );
4155 }
4156 }
4157 else
4158 {
4159 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4160 {
4161 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4162 return( ret );
4163 }
4164 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004165#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004166
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004167 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004168}
4169
4170/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004171 * Receive application data decrypted from the SSL layer
4172 */
Paul Bakker23986e52011-04-24 08:57:21 +00004173int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004174{
Paul Bakker23986e52011-04-24 08:57:21 +00004175 int ret;
4176 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004177
4178 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4179
4180 if( ssl->state != SSL_HANDSHAKE_OVER )
4181 {
4182 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4183 {
4184 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4185 return( ret );
4186 }
4187 }
4188
4189 if( ssl->in_offt == NULL )
4190 {
4191 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4192 {
Paul Bakker831a7552011-05-18 13:32:51 +00004193 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4194 return( 0 );
4195
Paul Bakker5121ce52009-01-03 21:22:43 +00004196 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4197 return( ret );
4198 }
4199
4200 if( ssl->in_msglen == 0 &&
4201 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4202 {
4203 /*
4204 * OpenSSL sends empty messages to randomize the IV
4205 */
4206 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4207 {
Paul Bakker831a7552011-05-18 13:32:51 +00004208 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4209 return( 0 );
4210
Paul Bakker5121ce52009-01-03 21:22:43 +00004211 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4212 return( ret );
4213 }
4214 }
4215
Paul Bakker48916f92012-09-16 19:57:18 +00004216 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4217 {
4218 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4219
4220 if( ssl->endpoint == SSL_IS_CLIENT &&
4221 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4222 ssl->in_hslen != 4 ) )
4223 {
4224 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4225 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4226 }
4227
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004228 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4229 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4230 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004231 {
4232 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4233
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004234#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004235 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004236 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004237 /*
4238 * SSLv3 does not have a "no_renegotiation" alert
4239 */
4240 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4241 return( ret );
4242 }
4243 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004244#endif
4245#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4246 defined(POLARSSL_SSL_PROTO_TLS1_2)
4247 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004248 {
4249 if( ( ret = ssl_send_alert_message( ssl,
4250 SSL_ALERT_LEVEL_WARNING,
4251 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4252 {
4253 return( ret );
4254 }
Paul Bakker48916f92012-09-16 19:57:18 +00004255 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004256 else
4257#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004258 {
4259 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004260 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004261 }
Paul Bakker48916f92012-09-16 19:57:18 +00004262 }
4263 else
4264 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004265 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004266 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004267 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004268 return( ret );
4269 }
4270
4271 return( POLARSSL_ERR_NET_WANT_READ );
4272 }
4273 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004274 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4275 {
4276 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4277 "but not honored by client" ) );
4278 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4279 }
Paul Bakker48916f92012-09-16 19:57:18 +00004280 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004281 {
4282 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004283 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004284 }
4285
4286 ssl->in_offt = ssl->in_msg;
4287 }
4288
4289 n = ( len < ssl->in_msglen )
4290 ? len : ssl->in_msglen;
4291
4292 memcpy( buf, ssl->in_offt, n );
4293 ssl->in_msglen -= n;
4294
4295 if( ssl->in_msglen == 0 )
4296 /* all bytes consumed */
4297 ssl->in_offt = NULL;
4298 else
4299 /* more data available */
4300 ssl->in_offt += n;
4301
4302 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4303
Paul Bakker23986e52011-04-24 08:57:21 +00004304 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004305}
4306
4307/*
4308 * Send application data to be encrypted by the SSL layer
4309 */
Paul Bakker23986e52011-04-24 08:57:21 +00004310int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311{
Paul Bakker23986e52011-04-24 08:57:21 +00004312 int ret;
4313 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004314 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004315
4316 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4317
4318 if( ssl->state != SSL_HANDSHAKE_OVER )
4319 {
4320 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4321 {
4322 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4323 return( ret );
4324 }
4325 }
4326
Paul Bakker05decb22013-08-15 13:33:48 +02004327#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004328 /*
4329 * Assume mfl_code is correct since it was checked when set
4330 */
4331 max_len = mfl_code_to_length[ssl->mfl_code];
4332
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004333 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004334 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004335 */
4336 if( ssl->session_out != NULL &&
4337 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4338 {
4339 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4340 }
Paul Bakker05decb22013-08-15 13:33:48 +02004341#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004342
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004343 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004344
Paul Bakker5121ce52009-01-03 21:22:43 +00004345 if( ssl->out_left != 0 )
4346 {
4347 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4348 {
4349 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4350 return( ret );
4351 }
4352 }
Paul Bakker887bd502011-06-08 13:10:54 +00004353 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004354 {
Paul Bakker887bd502011-06-08 13:10:54 +00004355 ssl->out_msglen = n;
4356 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4357 memcpy( ssl->out_msg, buf, n );
4358
4359 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4360 {
4361 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4362 return( ret );
4363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004364 }
4365
4366 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4367
Paul Bakker23986e52011-04-24 08:57:21 +00004368 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004369}
4370
4371/*
4372 * Notify the peer that the connection is being closed
4373 */
4374int ssl_close_notify( ssl_context *ssl )
4375{
4376 int ret;
4377
4378 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4379
4380 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4381 {
4382 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4383 return( ret );
4384 }
4385
4386 if( ssl->state == SSL_HANDSHAKE_OVER )
4387 {
Paul Bakker48916f92012-09-16 19:57:18 +00004388 if( ( ret = ssl_send_alert_message( ssl,
4389 SSL_ALERT_LEVEL_WARNING,
4390 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004391 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 return( ret );
4393 }
4394 }
4395
4396 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4397
4398 return( ret );
4399}
4400
Paul Bakker48916f92012-09-16 19:57:18 +00004401void ssl_transform_free( ssl_transform *transform )
4402{
4403#if defined(POLARSSL_ZLIB_SUPPORT)
4404 deflateEnd( &transform->ctx_deflate );
4405 inflateEnd( &transform->ctx_inflate );
4406#endif
4407
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004408 cipher_free_ctx( &transform->cipher_ctx_enc );
4409 cipher_free_ctx( &transform->cipher_ctx_dec );
4410
Paul Bakker61d113b2013-07-04 11:51:43 +02004411 md_free_ctx( &transform->md_ctx_enc );
4412 md_free_ctx( &transform->md_ctx_dec );
4413
Paul Bakker48916f92012-09-16 19:57:18 +00004414 memset( transform, 0, sizeof( ssl_transform ) );
4415}
4416
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004417#if defined(POLARSSL_X509_CRT_PARSE_C)
4418static void ssl_key_cert_free( ssl_key_cert *key_cert )
4419{
4420 ssl_key_cert *cur = key_cert, *next;
4421
4422 while( cur != NULL )
4423 {
4424 next = cur->next;
4425
4426 if( cur->key_own_alloc )
4427 {
4428 pk_free( cur->key );
4429 polarssl_free( cur->key );
4430 }
4431 polarssl_free( cur );
4432
4433 cur = next;
4434 }
4435}
4436#endif /* POLARSSL_X509_CRT_PARSE_C */
4437
Paul Bakker48916f92012-09-16 19:57:18 +00004438void ssl_handshake_free( ssl_handshake_params *handshake )
4439{
4440#if defined(POLARSSL_DHM_C)
4441 dhm_free( &handshake->dhm_ctx );
4442#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004443#if defined(POLARSSL_ECDH_C)
4444 ecdh_free( &handshake->ecdh_ctx );
4445#endif
4446
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004447#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004448 /* explicit void pointer cast for buggy MS compiler */
4449 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004450#endif
4451
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004452#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4453 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4454 /*
4455 * Free only the linked list wrapper, not the keys themselves
4456 * since the belong to the SNI callback
4457 */
4458 if( handshake->sni_key_cert != NULL )
4459 {
4460 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4461
4462 while( cur != NULL )
4463 {
4464 next = cur->next;
4465 polarssl_free( cur );
4466 cur = next;
4467 }
4468 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004469#endif
4470
Paul Bakker48916f92012-09-16 19:57:18 +00004471 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4472}
4473
4474void ssl_session_free( ssl_session *session )
4475{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004476#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004477 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004478 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004479 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004480 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004481 }
Paul Bakkered27a042013-04-18 22:46:23 +02004482#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004483
Paul Bakkera503a632013-08-14 13:48:06 +02004484#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004485 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004486#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004487
Paul Bakker0a597072012-09-25 21:55:46 +00004488 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004489}
4490
Paul Bakker5121ce52009-01-03 21:22:43 +00004491/*
4492 * Free an SSL context
4493 */
4494void ssl_free( ssl_context *ssl )
4495{
4496 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4497
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 if( ssl->out_ctr != NULL )
4499 {
4500 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004501 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004502 }
4503
4504 if( ssl->in_ctr != NULL )
4505 {
4506 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004507 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004508 }
4509
Paul Bakker16770332013-10-11 09:59:44 +02004510#if defined(POLARSSL_ZLIB_SUPPORT)
4511 if( ssl->compress_buf != NULL )
4512 {
4513 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4514 polarssl_free( ssl->compress_buf );
4515 }
4516#endif
4517
Paul Bakker40e46942009-01-03 21:51:57 +00004518#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004519 mpi_free( &ssl->dhm_P );
4520 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004521#endif
4522
Paul Bakker48916f92012-09-16 19:57:18 +00004523 if( ssl->transform )
4524 {
4525 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004526 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004527 }
4528
4529 if( ssl->handshake )
4530 {
4531 ssl_handshake_free( ssl->handshake );
4532 ssl_transform_free( ssl->transform_negotiate );
4533 ssl_session_free( ssl->session_negotiate );
4534
Paul Bakker6e339b52013-07-03 13:37:05 +02004535 polarssl_free( ssl->handshake );
4536 polarssl_free( ssl->transform_negotiate );
4537 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004538 }
4539
Paul Bakkerc0463502013-02-14 11:19:38 +01004540 if( ssl->session )
4541 {
4542 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004543 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004544 }
4545
Paul Bakkera503a632013-08-14 13:48:06 +02004546#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004547 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004548#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004549
Paul Bakker0be444a2013-08-27 21:55:01 +02004550#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004551 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004552 {
4553 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004554 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004555 ssl->hostname_len = 0;
4556 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004557#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004558
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004559#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004560 if( ssl->psk != NULL )
4561 {
4562 memset( ssl->psk, 0, ssl->psk_len );
4563 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4564 polarssl_free( ssl->psk );
4565 polarssl_free( ssl->psk_identity );
4566 ssl->psk_len = 0;
4567 ssl->psk_identity_len = 0;
4568 }
4569#endif
4570
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004571#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004572 ssl_key_cert_free( ssl->key_cert );
4573#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004574
Paul Bakker05ef8352012-05-08 09:17:57 +00004575#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4576 if( ssl_hw_record_finish != NULL )
4577 {
4578 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4579 ssl_hw_record_finish( ssl );
4580 }
4581#endif
4582
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004584
Paul Bakker86f04f42013-02-14 11:20:09 +01004585 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004586 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004587}
4588
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004589#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004590/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004591 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004592 */
4593unsigned char ssl_sig_from_pk( pk_context *pk )
4594{
4595#if defined(POLARSSL_RSA_C)
4596 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4597 return( SSL_SIG_RSA );
4598#endif
4599#if defined(POLARSSL_ECDSA_C)
4600 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4601 return( SSL_SIG_ECDSA );
4602#endif
4603 return( SSL_SIG_ANON );
4604}
4605
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004606pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4607{
4608 switch( sig )
4609 {
4610#if defined(POLARSSL_RSA_C)
4611 case SSL_SIG_RSA:
4612 return( POLARSSL_PK_RSA );
4613#endif
4614#if defined(POLARSSL_ECDSA_C)
4615 case SSL_SIG_ECDSA:
4616 return( POLARSSL_PK_ECDSA );
4617#endif
4618 default:
4619 return( POLARSSL_PK_NONE );
4620 }
4621}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004622#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004623
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004624/*
4625 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4626 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004627md_type_t ssl_md_alg_from_hash( unsigned char hash )
4628{
4629 switch( hash )
4630 {
4631#if defined(POLARSSL_MD5_C)
4632 case SSL_HASH_MD5:
4633 return( POLARSSL_MD_MD5 );
4634#endif
4635#if defined(POLARSSL_SHA1_C)
4636 case SSL_HASH_SHA1:
4637 return( POLARSSL_MD_SHA1 );
4638#endif
4639#if defined(POLARSSL_SHA256_C)
4640 case SSL_HASH_SHA224:
4641 return( POLARSSL_MD_SHA224 );
4642 case SSL_HASH_SHA256:
4643 return( POLARSSL_MD_SHA256 );
4644#endif
4645#if defined(POLARSSL_SHA512_C)
4646 case SSL_HASH_SHA384:
4647 return( POLARSSL_MD_SHA384 );
4648 case SSL_HASH_SHA512:
4649 return( POLARSSL_MD_SHA512 );
4650#endif
4651 default:
4652 return( POLARSSL_MD_NONE );
4653 }
4654}
4655
Paul Bakker5121ce52009-01-03 21:22:43 +00004656#endif
Gergely Budai987bfb52014-01-19 21:48:42 +01004657
4658#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
4659/*
4660 * Set the allowed ECDH curves.
4661 */
4662void ssl_set_ecdh_curves( ssl_context *ssl, const ecp_group_id *ecdh_curve_list )
4663{
4664 ssl->ecdh_curve_list = ecdh_curve_list;
4665}
4666#endif