blob: 7c7adbcf2782970dd90567e4f9f0297afe262509 [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 enc_msglen = ssl->out_msglen;
1101
1102 memcpy( add_data, ssl->out_ctr, 8 );
1103 add_data[8] = ssl->out_msgtype;
1104 add_data[9] = ssl->major_ver;
1105 add_data[10] = ssl->minor_ver;
1106 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1107 add_data[12] = ssl->out_msglen & 0xFF;
1108
1109 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1110 add_data, 13 );
1111
Paul Bakker68884e32013-01-07 18:20:04 +01001112 /*
1113 * Generate IV
1114 */
1115 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001116 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001117 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1118 if( ret != 0 )
1119 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001120
Paul Bakker68884e32013-01-07 18:20:04 +01001121 memcpy( ssl->out_iv,
1122 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1123 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001124
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001125 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1126 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1127
Paul Bakker68884e32013-01-07 18:20:04 +01001128 /*
1129 * Fix pointer positions and message length with added IV
1130 */
1131 enc_msg = ssl->out_msg;
1132 enc_msglen = ssl->out_msglen;
1133 ssl->out_msglen += ssl->transform_out->ivlen -
1134 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135
Paul Bakker68884e32013-01-07 18:20:04 +01001136 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1137 "including %d bytes of padding",
1138 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001139
Paul Bakker68884e32013-01-07 18:20:04 +01001140 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1141 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001142
Paul Bakker68884e32013-01-07 18:20:04 +01001143 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001144 * Encrypt
1145 */
1146 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1147 ssl->transform_out->iv_enc,
1148 ssl->transform_out->ivlen ) ) != 0 ||
1149 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1150 {
1151 return( ret );
1152 }
1153
1154 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1155 add_data, 13 ) ) != 0 )
1156 {
1157 return( ret );
1158 }
1159
1160 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1161 enc_msg, enc_msglen,
1162 enc_msg, &olen ) ) != 0 )
1163 {
1164 return( ret );
1165 }
1166 totlen = olen;
1167
1168 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1169 enc_msg + olen, &olen ) ) != 0 )
1170 {
1171 return( ret );
1172 }
1173 totlen += olen;
1174
1175 if( totlen != enc_msglen )
1176 {
1177 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1178 return( -1 );
1179 }
1180
1181 /*
1182 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001183 */
1184 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001185
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001186 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1187 enc_msg + enc_msglen, 16 ) ) != 0 )
1188 {
1189 return( ret );
1190 }
Paul Bakker68884e32013-01-07 18:20:04 +01001191
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001192 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001193 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 else
Paul Bakker68884e32013-01-07 18:20:04 +01001195#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001196#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1197 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001198 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1199 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001201 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001202 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001203 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001204
Paul Bakker48916f92012-09-16 19:57:18 +00001205 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1206 ssl->transform_out->ivlen;
1207 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 padlen = 0;
1209
1210 for( i = 0; i <= padlen; i++ )
1211 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1212
1213 ssl->out_msglen += padlen + 1;
1214
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001215 enc_msglen = ssl->out_msglen;
1216 enc_msg = ssl->out_msg;
1217
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001218#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001219 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001220 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1221 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001222 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001223 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001224 {
1225 /*
1226 * Generate IV
1227 */
Paul Bakker48916f92012-09-16 19:57:18 +00001228 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1229 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001230 if( ret != 0 )
1231 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001232
Paul Bakker92be97b2013-01-02 17:30:03 +01001233 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001234 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235
1236 /*
1237 * Fix pointer positions and message length with added IV
1238 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001239 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001241 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001243#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001244
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001246 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001247 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
1249 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001250 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001251
Paul Bakker45125bc2013-09-04 16:47:11 +02001252 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001254 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1255 return( ret );
1256 }
1257
1258 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1259 ssl->transform_out->iv_enc,
1260 ssl->transform_out->ivlen ) ) != 0 )
1261 {
1262 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001263 return( ret );
1264 }
Paul Bakker68884e32013-01-07 18:20:04 +01001265
Paul Bakkercca5b812013-08-31 17:40:26 +02001266 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1267 enc_msg, enc_msglen, enc_msg,
1268 &olen ) ) != 0 )
1269 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001270 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001271 return( ret );
1272 }
Paul Bakker68884e32013-01-07 18:20:04 +01001273
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001275
Paul Bakkercca5b812013-08-31 17:40:26 +02001276 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001277 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001279 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001280 return( ret );
1281 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001282
Paul Bakkercca5b812013-08-31 17:40:26 +02001283 if( enc_msglen != olen )
1284 {
1285 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1286 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001287 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001288 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001289
1290#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001291 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1292 {
1293 /*
1294 * Save IV in SSL3 and TLS1
1295 */
1296 memcpy( ssl->transform_out->iv_enc,
1297 ssl->transform_out->cipher_ctx_enc.iv,
1298 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001300#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001301 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001302 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001303#endif /* POLARSSL_CIPHER_MODE_CBC &&
1304 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001305 {
1306 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1307 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1308 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
Paul Bakkerca4ab492012-04-18 14:23:57 +00001310 for( i = 8; i > 0; i-- )
1311 if( ++ssl->out_ctr[i - 1] != 0 )
1312 break;
1313
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1315
1316 return( 0 );
1317}
1318
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001319#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001320
Paul Bakker5121ce52009-01-03 21:22:43 +00001321static int ssl_decrypt_buf( ssl_context *ssl )
1322{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001323 size_t i;
1324#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1325 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1326 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1327 size_t padlen = 0, correct = 1;
1328#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
1330 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1331
Paul Bakker48916f92012-09-16 19:57:18 +00001332 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 {
1334 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001335 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001336 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 }
1338
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001339#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001340 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1341 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001342 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001343 int ret;
1344 size_t olen = 0;
1345
Paul Bakker68884e32013-01-07 18:20:04 +01001346 padlen = 0;
1347
Paul Bakker45125bc2013-09-04 16:47:11 +02001348 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001349 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001350 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1351 return( ret );
1352 }
1353
1354 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1355 ssl->transform_in->iv_dec,
1356 ssl->transform_in->ivlen ) ) != 0 )
1357 {
1358 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001359 return( ret );
1360 }
1361
1362 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1363 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1364 &olen ) ) != 0 )
1365 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001366 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001367 return( ret );
1368 }
1369
1370 if( ssl->in_msglen != olen )
1371 {
1372 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001373 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001374 }
1375
1376 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001377 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001378 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001379 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001380 return( ret );
1381 }
1382
1383 if( 0 != olen )
1384 {
1385 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001386 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 }
Paul Bakker68884e32013-01-07 18:20:04 +01001389 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001390#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001391#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001392 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1393 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001394 {
1395 unsigned char *dec_msg;
1396 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001397 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001398 unsigned char add_data[13];
1399 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1400
Paul Bakker68884e32013-01-07 18:20:04 +01001401 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1402 ssl->transform_in->fixed_ivlen );
1403 dec_msglen -= 16;
1404 dec_msg = ssl->in_msg;
1405 dec_msg_result = ssl->in_msg;
1406 ssl->in_msglen = dec_msglen;
1407
1408 memcpy( add_data, ssl->in_ctr, 8 );
1409 add_data[8] = ssl->in_msgtype;
1410 add_data[9] = ssl->major_ver;
1411 add_data[10] = ssl->minor_ver;
1412 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1413 add_data[12] = ssl->in_msglen & 0xFF;
1414
1415 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1416 add_data, 13 );
1417
1418 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1419 ssl->in_iv,
1420 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1421
1422 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1423 ssl->transform_in->ivlen );
1424 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1425
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001426 /*
1427 * Decrypt
1428 */
1429 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1430 ssl->transform_in->iv_dec,
1431 ssl->transform_in->ivlen ) ) != 0 ||
1432 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001433 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001434 return( ret );
1435 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001436
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001437 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1438 add_data, 13 ) ) != 0 )
1439 {
1440 return( ret );
1441 }
1442
1443 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1444 dec_msg, dec_msglen,
1445 dec_msg_result, &olen ) ) != 0 )
1446 {
1447 return( ret );
1448 }
1449 totlen = olen;
1450
1451 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1452 dec_msg_result + olen, &olen ) ) != 0 )
1453 {
1454 return( ret );
1455 }
1456 totlen += olen;
1457
1458 if( totlen != dec_msglen )
1459 {
1460 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1461 return( -1 );
1462 }
1463
1464 /*
1465 * Authenticate
1466 */
1467 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1468 dec_msg + dec_msglen, 16 ) ) != 0 )
1469 {
1470 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001471 return( POLARSSL_ERR_SSL_INVALID_MAC );
1472 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001473
Paul Bakkerca4ab492012-04-18 14:23:57 +00001474 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 else
Paul Bakker68884e32013-01-07 18:20:04 +01001476#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001477#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1478 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001479 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1480 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 {
Paul Bakker45829992013-01-03 14:52:21 +01001482 /*
1483 * Decrypt and check the padding
1484 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001485 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001486 unsigned char *dec_msg;
1487 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001488 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001489 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001490 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001491
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 /*
Paul Bakker45829992013-01-03 14:52:21 +01001493 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 */
Paul Bakker48916f92012-09-16 19:57:18 +00001495 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 {
1497 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001498 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001499 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 }
1501
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001502#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001503 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1504 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001505#endif
Paul Bakker45829992013-01-03 14:52:21 +01001506
1507 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1508 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1509 {
1510 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1511 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1512 return( POLARSSL_ERR_SSL_INVALID_MAC );
1513 }
1514
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001515 dec_msglen = ssl->in_msglen;
1516 dec_msg = ssl->in_msg;
1517 dec_msg_result = ssl->in_msg;
1518
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001519#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001520 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001521 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001522 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001523 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001524 {
Paul Bakker48916f92012-09-16 19:57:18 +00001525 dec_msglen -= ssl->transform_in->ivlen;
1526 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001527
Paul Bakker48916f92012-09-16 19:57:18 +00001528 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001529 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001530 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001531#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001532
Paul Bakker45125bc2013-09-04 16:47:11 +02001533 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001535 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1536 return( ret );
1537 }
1538
1539 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1540 ssl->transform_in->iv_dec,
1541 ssl->transform_in->ivlen ) ) != 0 )
1542 {
1543 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001544 return( ret );
1545 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001546
Paul Bakkercca5b812013-08-31 17:40:26 +02001547 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1548 dec_msg, dec_msglen, dec_msg_result,
1549 &olen ) ) != 0 )
1550 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001551 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001552 return( ret );
1553 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001554
Paul Bakkercca5b812013-08-31 17:40:26 +02001555 dec_msglen -= olen;
1556 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001557 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001558 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001559 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001560 return( ret );
1561 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001562
Paul Bakkercca5b812013-08-31 17:40:26 +02001563 if( dec_msglen != olen )
1564 {
1565 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001566 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001567 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001568
1569#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001570 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1571 {
1572 /*
1573 * Save IV in SSL3 and TLS1
1574 */
1575 memcpy( ssl->transform_in->iv_dec,
1576 ssl->transform_in->cipher_ctx_dec.iv,
1577 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001579#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
1581 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001582
1583 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1584 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001585#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001586 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1587 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001588#endif
Paul Bakker45829992013-01-03 14:52:21 +01001589 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001590 correct = 0;
1591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001593#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1595 {
Paul Bakker48916f92012-09-16 19:57:18 +00001596 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001598#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1600 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001601 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001602#endif
Paul Bakker45829992013-01-03 14:52:21 +01001603 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001604 }
1605 }
1606 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001607#endif
1608#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1609 defined(POLARSSL_SSL_PROTO_TLS1_2)
1610 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 {
1612 /*
Paul Bakker45829992013-01-03 14:52:21 +01001613 * TLSv1+: always check the padding up to the first failure
1614 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001616 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001617 size_t padding_idx = ssl->in_msglen - padlen - 1;
1618
Paul Bakker956c9e02013-12-19 14:42:28 +01001619 /*
1620 * Padding is guaranteed to be incorrect if:
1621 * 1. padlen - 1 > ssl->in_msglen
1622 *
1623 * 2. ssl->in_msglen + padlen >
1624 * SSL_MAX_CONTENT_LEN + 256 (max padding)
1625 *
1626 * In both cases we reset padding_idx to a safe value (0) to
1627 * prevent out-of-buffer reads.
1628 */
1629 correct &= ( ssl->in_msglen >= padlen - 1 );
1630 correct &= ( ssl->in_msglen + padlen <= SSL_MAX_CONTENT_LEN + 256 );
1631
1632 padding_idx *= correct;
1633
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001634 for( i = 1; i <= 256; i++ )
1635 {
1636 real_count &= ( i <= padlen );
1637 pad_count += real_count *
1638 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1639 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001640
1641 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001642
Paul Bakkerd66f0702013-01-31 16:57:45 +01001643#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001644 if( padlen > 0 && correct == 0)
1645 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001646#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001647 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001648 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001649 else
1650#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1651 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001652 {
1653 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001654 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001657 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001658#endif /* POLARSSL_CIPHER_MODE_CBC &&
1659 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001660 {
1661 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1662 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
1665 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1666 ssl->in_msg, ssl->in_msglen );
1667
1668 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001671#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1672 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1673 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1674 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1675 POLARSSL_MODE_GCM )
1676 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001677 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1678
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001681 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1682 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001684 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001686#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001687 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1688 {
1689 ssl_mac( &ssl->transform_in->md_ctx_dec,
1690 ssl->transform_in->mac_dec,
1691 ssl->in_msg, ssl->in_msglen,
1692 ssl->in_ctr, ssl->in_msgtype );
1693 }
1694 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001695#endif /* POLARSSL_SSL_PROTO_SSL3 */
1696#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001697 defined(POLARSSL_SSL_PROTO_TLS1_2)
1698 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1699 {
1700 /*
1701 * Process MAC and always update for padlen afterwards to make
1702 * total time independent of padlen
1703 *
1704 * extra_run compensates MAC check for padlen
1705 *
1706 * Known timing attacks:
1707 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1708 *
1709 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1710 * correctly. (We round down instead of up, so -56 is the correct
1711 * value for our calculations instead of -55)
1712 */
1713 size_t j, extra_run = 0;
1714 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1715 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001716
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001717 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001718
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001719 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1720 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1721 ssl->in_msglen );
1722 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1723 ssl->in_msg + ssl->in_msglen );
1724 for( j = 0; j < extra_run; j++ )
1725 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001726
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001727 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1728 }
1729 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001730#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001731 POLARSSL_SSL_PROTO_TLS1_2 */
1732 {
1733 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1734 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001737 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1738 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1739 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001740
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001741 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001742 ssl->transform_in->maclen ) != 0 )
1743 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001744#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001745 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001746#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001747 correct = 0;
1748 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001749
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001750 /*
1751 * Finally check the correct flag
1752 */
1753 if( correct == 0 )
1754 return( POLARSSL_ERR_SSL_INVALID_MAC );
1755 }
1756#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001757
1758 if( ssl->in_msglen == 0 )
1759 {
1760 ssl->nb_zero++;
1761
1762 /*
1763 * Three or more empty messages may be a DoS attack
1764 * (excessive CPU consumption).
1765 */
1766 if( ssl->nb_zero > 3 )
1767 {
1768 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1769 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001770 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 }
1772 }
1773 else
1774 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001775
Paul Bakker23986e52011-04-24 08:57:21 +00001776 for( i = 8; i > 0; i-- )
1777 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 break;
1779
1780 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1781
1782 return( 0 );
1783}
1784
Paul Bakker2770fbd2012-07-03 13:30:23 +00001785#if defined(POLARSSL_ZLIB_SUPPORT)
1786/*
1787 * Compression/decompression functions
1788 */
1789static int ssl_compress_buf( ssl_context *ssl )
1790{
1791 int ret;
1792 unsigned char *msg_post = ssl->out_msg;
1793 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001794 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795
1796 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1797
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001798 if( len_pre == 0 )
1799 return( 0 );
1800
Paul Bakker2770fbd2012-07-03 13:30:23 +00001801 memcpy( msg_pre, ssl->out_msg, len_pre );
1802
1803 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1804 ssl->out_msglen ) );
1805
1806 SSL_DEBUG_BUF( 4, "before compression: output payload",
1807 ssl->out_msg, ssl->out_msglen );
1808
Paul Bakker48916f92012-09-16 19:57:18 +00001809 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1810 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1811 ssl->transform_out->ctx_deflate.next_out = msg_post;
1812 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001813
Paul Bakker48916f92012-09-16 19:57:18 +00001814 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001815 if( ret != Z_OK )
1816 {
1817 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1818 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1819 }
1820
Paul Bakker48916f92012-09-16 19:57:18 +00001821 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001822
Paul Bakker2770fbd2012-07-03 13:30:23 +00001823 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1824 ssl->out_msglen ) );
1825
1826 SSL_DEBUG_BUF( 4, "after compression: output payload",
1827 ssl->out_msg, ssl->out_msglen );
1828
1829 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1830
1831 return( 0 );
1832}
1833
1834static int ssl_decompress_buf( ssl_context *ssl )
1835{
1836 int ret;
1837 unsigned char *msg_post = ssl->in_msg;
1838 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001839 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001840
1841 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1842
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001843 if( len_pre == 0 )
1844 return( 0 );
1845
Paul Bakker2770fbd2012-07-03 13:30:23 +00001846 memcpy( msg_pre, ssl->in_msg, len_pre );
1847
1848 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1849 ssl->in_msglen ) );
1850
1851 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1852 ssl->in_msg, ssl->in_msglen );
1853
Paul Bakker48916f92012-09-16 19:57:18 +00001854 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1855 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1856 ssl->transform_in->ctx_inflate.next_out = msg_post;
1857 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001858
Paul Bakker48916f92012-09-16 19:57:18 +00001859 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001860 if( ret != Z_OK )
1861 {
1862 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1863 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1864 }
1865
Paul Bakker48916f92012-09-16 19:57:18 +00001866 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001867
Paul Bakker2770fbd2012-07-03 13:30:23 +00001868 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1869 ssl->in_msglen ) );
1870
1871 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1872 ssl->in_msg, ssl->in_msglen );
1873
1874 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1875
1876 return( 0 );
1877}
1878#endif /* POLARSSL_ZLIB_SUPPORT */
1879
Paul Bakker5121ce52009-01-03 21:22:43 +00001880/*
1881 * Fill the input message buffer
1882 */
Paul Bakker23986e52011-04-24 08:57:21 +00001883int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001884{
Paul Bakker23986e52011-04-24 08:57:21 +00001885 int ret;
1886 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
1888 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1889
1890 while( ssl->in_left < nb_want )
1891 {
1892 len = nb_want - ssl->in_left;
1893 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1894
1895 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1896 ssl->in_left, nb_want ) );
1897 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1898
Paul Bakker831a7552011-05-18 13:32:51 +00001899 if( ret == 0 )
1900 return( POLARSSL_ERR_SSL_CONN_EOF );
1901
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 if( ret < 0 )
1903 return( ret );
1904
1905 ssl->in_left += ret;
1906 }
1907
1908 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1909
1910 return( 0 );
1911}
1912
1913/*
1914 * Flush any data not yet written
1915 */
1916int ssl_flush_output( ssl_context *ssl )
1917{
1918 int ret;
1919 unsigned char *buf;
1920
1921 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1922
1923 while( ssl->out_left > 0 )
1924 {
1925 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1926 5 + ssl->out_msglen, ssl->out_left ) );
1927
Paul Bakker5bd42292012-12-19 14:40:42 +01001928 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001930
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1932
1933 if( ret <= 0 )
1934 return( ret );
1935
1936 ssl->out_left -= ret;
1937 }
1938
1939 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1940
1941 return( 0 );
1942}
1943
1944/*
1945 * Record layer functions
1946 */
1947int ssl_write_record( ssl_context *ssl )
1948{
Paul Bakker05ef8352012-05-08 09:17:57 +00001949 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001950 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
1952 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1953
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1955 {
1956 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1957 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1958 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1959
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001960 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1961 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 }
1963
Paul Bakker2770fbd2012-07-03 13:30:23 +00001964#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001965 if( ssl->transform_out != NULL &&
1966 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001967 {
1968 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1969 {
1970 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1971 return( ret );
1972 }
1973
1974 len = ssl->out_msglen;
1975 }
1976#endif /*POLARSSL_ZLIB_SUPPORT */
1977
Paul Bakker05ef8352012-05-08 09:17:57 +00001978#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1979 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001981 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001982
Paul Bakker05ef8352012-05-08 09:17:57 +00001983 ret = ssl_hw_record_write( ssl );
1984 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1985 {
1986 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1987 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1988 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001989
1990 if( ret == 0 )
1991 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001992 }
1993#endif
1994 if( !done )
1995 {
1996 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1997 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1998 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2000 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002001
Paul Bakker48916f92012-09-16 19:57:18 +00002002 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002003 {
2004 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2005 {
2006 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2007 return( ret );
2008 }
2009
2010 len = ssl->out_msglen;
2011 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2012 ssl->out_hdr[4] = (unsigned char)( len );
2013 }
2014
2015 ssl->out_left = 5 + ssl->out_msglen;
2016
2017 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2018 "version = [%d:%d], msglen = %d",
2019 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2020 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2021
2022 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002023 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 }
2025
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2027 {
2028 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2029 return( ret );
2030 }
2031
2032 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2033
2034 return( 0 );
2035}
2036
2037int ssl_read_record( ssl_context *ssl )
2038{
Paul Bakker05ef8352012-05-08 09:17:57 +00002039 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002040
2041 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2042
Paul Bakker68884e32013-01-07 18:20:04 +01002043 SSL_DEBUG_BUF( 4, "input record from network",
2044 ssl->in_hdr, 5 + ssl->in_msglen );
2045
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 if( ssl->in_hslen != 0 &&
2047 ssl->in_hslen < ssl->in_msglen )
2048 {
2049 /*
2050 * Get next Handshake message in the current record
2051 */
2052 ssl->in_msglen -= ssl->in_hslen;
2053
Paul Bakker8934a982011-08-05 11:11:53 +00002054 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002055 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
2057 ssl->in_hslen = 4;
2058 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2059
2060 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2061 " %d, type = %d, hslen = %d",
2062 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2063
2064 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2065 {
2066 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002067 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069
2070 if( ssl->in_msglen < ssl->in_hslen )
2071 {
2072 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002073 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 }
2075
Paul Bakker48916f92012-09-16 19:57:18 +00002076 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
2078 return( 0 );
2079 }
2080
2081 ssl->in_hslen = 0;
2082
2083 /*
2084 * Read the record header and validate it
2085 */
2086 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2087 {
2088 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2089 return( ret );
2090 }
2091
2092 ssl->in_msgtype = ssl->in_hdr[0];
2093 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2094
2095 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2096 "version = [%d:%d], msglen = %d",
2097 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2098 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2099
2100 if( ssl->in_hdr[1] != ssl->major_ver )
2101 {
2102 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002103 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
2105
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002106 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 {
2108 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002109 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 }
2111
2112 /*
2113 * Make sure the message length is acceptable
2114 */
Paul Bakker48916f92012-09-16 19:57:18 +00002115 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 {
2117 if( ssl->in_msglen < 1 ||
2118 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2119 {
2120 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002121 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 }
2123 }
2124 else
2125 {
Paul Bakker48916f92012-09-16 19:57:18 +00002126 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 {
2128 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002129 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 }
2131
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002132#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002134 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 {
2136 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002137 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002139#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002141#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2142 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 /*
2144 * TLS encrypted messages can have up to 256 bytes of padding
2145 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002146 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002147 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 {
2149 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002150 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002152#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 }
2154
2155 /*
2156 * Read and optionally decrypt the message contents
2157 */
2158 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2159 {
2160 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2161 return( ret );
2162 }
2163
2164 SSL_DEBUG_BUF( 4, "input record from network",
2165 ssl->in_hdr, 5 + ssl->in_msglen );
2166
Paul Bakker05ef8352012-05-08 09:17:57 +00002167#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2168 if( ssl_hw_record_read != NULL)
2169 {
2170 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2171
2172 ret = ssl_hw_record_read( ssl );
2173 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2174 {
2175 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2176 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2177 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002178
2179 if( ret == 0 )
2180 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002181 }
2182#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002183 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 {
2185 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2186 {
Paul Bakker40865c82013-01-31 17:13:13 +01002187#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2188 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2189 {
2190 ssl_send_alert_message( ssl,
2191 SSL_ALERT_LEVEL_FATAL,
2192 SSL_ALERT_MSG_BAD_RECORD_MAC );
2193 }
2194#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2196 return( ret );
2197 }
2198
2199 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2200 ssl->in_msg, ssl->in_msglen );
2201
2202 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2203 {
2204 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002205 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
2207 }
2208
Paul Bakker2770fbd2012-07-03 13:30:23 +00002209#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002210 if( ssl->transform_in != NULL &&
2211 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002212 {
2213 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2214 {
2215 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2216 return( ret );
2217 }
2218
2219 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2220 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2221 }
2222#endif /* POLARSSL_ZLIB_SUPPORT */
2223
Paul Bakker0a925182012-04-16 06:46:41 +00002224 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2225 ssl->in_msgtype != SSL_MSG_ALERT &&
2226 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2227 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2228 {
2229 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2230
Paul Bakker48916f92012-09-16 19:57:18 +00002231 if( ( ret = ssl_send_alert_message( ssl,
2232 SSL_ALERT_LEVEL_FATAL,
2233 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002234 {
2235 return( ret );
2236 }
2237
2238 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2239 }
2240
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2242 {
2243 ssl->in_hslen = 4;
2244 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2245
2246 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2247 " %d, type = %d, hslen = %d",
2248 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2249
2250 /*
2251 * Additional checks to validate the handshake header
2252 */
2253 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2254 {
2255 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002256 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 }
2258
2259 if( ssl->in_msglen < ssl->in_hslen )
2260 {
2261 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002262 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 }
2264
Paul Bakker48916f92012-09-16 19:57:18 +00002265 if( ssl->state != SSL_HANDSHAKE_OVER )
2266 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 }
2268
2269 if( ssl->in_msgtype == SSL_MSG_ALERT )
2270 {
2271 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2272 ssl->in_msg[0], ssl->in_msg[1] ) );
2273
2274 /*
2275 * Ignore non-fatal alerts, except close_notify
2276 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002277 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002279 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2280 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002281 /**
2282 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2283 * error identifier.
2284 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002285 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 }
2287
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002288 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2289 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 {
2291 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002292 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 }
2294 }
2295
2296 ssl->in_left = 0;
2297
2298 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2299
2300 return( 0 );
2301}
2302
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002303int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2304{
2305 int ret;
2306
2307 if( ( ret = ssl_send_alert_message( ssl,
2308 SSL_ALERT_LEVEL_FATAL,
2309 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2310 {
2311 return( ret );
2312 }
2313
2314 return( 0 );
2315}
2316
Paul Bakker0a925182012-04-16 06:46:41 +00002317int ssl_send_alert_message( ssl_context *ssl,
2318 unsigned char level,
2319 unsigned char message )
2320{
2321 int ret;
2322
2323 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2324
2325 ssl->out_msgtype = SSL_MSG_ALERT;
2326 ssl->out_msglen = 2;
2327 ssl->out_msg[0] = level;
2328 ssl->out_msg[1] = message;
2329
2330 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2331 {
2332 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2333 return( ret );
2334 }
2335
2336 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2337
2338 return( 0 );
2339}
2340
Paul Bakker5121ce52009-01-03 21:22:43 +00002341/*
2342 * Handshake functions
2343 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002344#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2345 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2346 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2347 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2348 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2349 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2350 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002351int ssl_write_certificate( ssl_context *ssl )
2352{
Paul Bakkered27a042013-04-18 22:46:23 +02002353 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002354 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
2356 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2357
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002358 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002359 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2360 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002361 {
2362 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2363 ssl->state++;
2364 return( 0 );
2365 }
2366
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002367 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002368 return( ret );
2369}
2370
2371int ssl_parse_certificate( ssl_context *ssl )
2372{
2373 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2374 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2375
2376 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2377
2378 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002379 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2380 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002381 {
2382 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2383 ssl->state++;
2384 return( 0 );
2385 }
2386
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002387 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002388 return( ret );
2389}
2390#else
2391int ssl_write_certificate( ssl_context *ssl )
2392{
2393 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2394 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002395 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002396 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2397
2398 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2399
2400 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002401 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2402 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002403 {
2404 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2405 ssl->state++;
2406 return( 0 );
2407 }
2408
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 if( ssl->endpoint == SSL_IS_CLIENT )
2410 {
2411 if( ssl->client_auth == 0 )
2412 {
2413 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2414 ssl->state++;
2415 return( 0 );
2416 }
2417
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002418#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 /*
2420 * If using SSLv3 and got no cert, send an Alert message
2421 * (otherwise an empty Certificate message will be sent).
2422 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002423 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2425 {
2426 ssl->out_msglen = 2;
2427 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002428 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2429 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002430
2431 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2432 goto write_msg;
2433 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002434#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 }
2436 else /* SSL_IS_SERVER */
2437 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002438 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 {
2440 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002441 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 }
2443 }
2444
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002445 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
2447 /*
2448 * 0 . 0 handshake type
2449 * 1 . 3 handshake length
2450 * 4 . 6 length of all certs
2451 * 7 . 9 length of cert. 1
2452 * 10 . n-1 peer certificate
2453 * n . n+2 length of cert. 2
2454 * n+3 . ... upper level cert, etc.
2455 */
2456 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002457 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Paul Bakker29087132010-03-21 21:03:34 +00002459 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 {
2461 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002462 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 {
2464 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2465 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002466 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 }
2468
2469 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2470 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2471 ssl->out_msg[i + 2] = (unsigned char)( n );
2472
2473 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2474 i += n; crt = crt->next;
2475 }
2476
2477 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2478 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2479 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2480
2481 ssl->out_msglen = i;
2482 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2483 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2484
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002485#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002486write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002487#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
2489 ssl->state++;
2490
2491 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2492 {
2493 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2494 return( ret );
2495 }
2496
2497 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2498
Paul Bakkered27a042013-04-18 22:46:23 +02002499 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002500}
2501
2502int ssl_parse_certificate( ssl_context *ssl )
2503{
Paul Bakkered27a042013-04-18 22:46:23 +02002504 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002505 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002506 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
2508 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2509
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002510 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002511 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2512 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002513 {
2514 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2515 ssl->state++;
2516 return( 0 );
2517 }
2518
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002520 ( ssl->authmode == SSL_VERIFY_NONE ||
2521 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002523 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2525 ssl->state++;
2526 return( 0 );
2527 }
2528
2529 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2530 {
2531 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2532 return( ret );
2533 }
2534
2535 ssl->state++;
2536
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002537#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 /*
2539 * Check if the client sent an empty certificate
2540 */
2541 if( ssl->endpoint == SSL_IS_SERVER &&
2542 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2543 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002544 if( ssl->in_msglen == 2 &&
2545 ssl->in_msgtype == SSL_MSG_ALERT &&
2546 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2547 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 {
2549 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2550
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002551 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2553 return( 0 );
2554 else
Paul Bakker40e46942009-01-03 21:51:57 +00002555 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 }
2557 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002558#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002560#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2561 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 if( ssl->endpoint == SSL_IS_SERVER &&
2563 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2564 {
2565 if( ssl->in_hslen == 7 &&
2566 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2567 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2568 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2569 {
2570 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2571
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002572 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002574 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 else
2576 return( 0 );
2577 }
2578 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002579#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2580 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
2582 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2583 {
2584 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002585 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 }
2587
2588 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2589 {
2590 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002591 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 }
2593
2594 /*
2595 * Same message structure as in ssl_write_certificate()
2596 */
2597 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2598
2599 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2600 {
2601 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002602 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 }
2604
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002605 /* In case we tried to reuse a session but it failed */
2606 if( ssl->session_negotiate->peer_cert != NULL )
2607 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002608 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002609 polarssl_free( ssl->session_negotiate->peer_cert );
2610 }
2611
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002612 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2613 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 {
2615 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002616 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002617 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619
Paul Bakkerb6b09562013-09-18 14:17:41 +02002620 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
2622 i = 7;
2623
2624 while( i < ssl->in_hslen )
2625 {
2626 if( ssl->in_msg[i] != 0 )
2627 {
2628 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002629 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 }
2631
2632 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2633 | (unsigned int) ssl->in_msg[i + 2];
2634 i += 3;
2635
2636 if( n < 128 || i + n > ssl->in_hslen )
2637 {
2638 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002639 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 }
2641
Paul Bakkerddf26b42013-09-18 13:46:23 +02002642 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2643 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 if( ret != 0 )
2645 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002646 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 return( ret );
2648 }
2649
2650 i += n;
2651 }
2652
Paul Bakker48916f92012-09-16 19:57:18 +00002653 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
2655 if( ssl->authmode != SSL_VERIFY_NONE )
2656 {
2657 if( ssl->ca_chain == NULL )
2658 {
2659 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002660 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 }
2662
Paul Bakkerddf26b42013-09-18 13:46:23 +02002663 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2664 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2665 &ssl->session_negotiate->verify_result,
2666 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
2668 if( ret != 0 )
2669 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2670
2671 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2672 ret = 0;
2673 }
2674
2675 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2676
2677 return( ret );
2678}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002679#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2680 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2681 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2682 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2683 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2684 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2685 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
2687int ssl_write_change_cipher_spec( ssl_context *ssl )
2688{
2689 int ret;
2690
2691 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2692
2693 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2694 ssl->out_msglen = 1;
2695 ssl->out_msg[0] = 1;
2696
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 ssl->state++;
2698
2699 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2700 {
2701 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2702 return( ret );
2703 }
2704
2705 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2706
2707 return( 0 );
2708}
2709
2710int ssl_parse_change_cipher_spec( ssl_context *ssl )
2711{
2712 int ret;
2713
2714 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2715
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2717 {
2718 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2719 return( ret );
2720 }
2721
2722 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2723 {
2724 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002725 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
2728 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2729 {
2730 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002731 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 }
2733
2734 ssl->state++;
2735
2736 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2737
2738 return( 0 );
2739}
2740
Paul Bakker41c83d32013-03-20 14:39:14 +01002741void ssl_optimize_checksum( ssl_context *ssl,
2742 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002743{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002744 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002745
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002746#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2747 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002748 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002749 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002750 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002751#endif
2752#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2753#if defined(POLARSSL_SHA512_C)
2754 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2755 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2756 else
2757#endif
2758#if defined(POLARSSL_SHA256_C)
2759 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002760 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002761 else
2762#endif
2763#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2764 /* Should never happen */
2765 return;
Paul Bakker380da532012-04-18 16:10:25 +00002766}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002767
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002768static void ssl_update_checksum_start( ssl_context *ssl,
2769 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002770{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002771#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2772 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002773 md5_update( &ssl->handshake->fin_md5 , buf, len );
2774 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002775#endif
2776#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2777#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002778 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002779#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002780#if defined(POLARSSL_SHA512_C)
2781 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002782#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002783#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002784}
2785
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002786#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2787 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002788static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2789 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002790{
Paul Bakker48916f92012-09-16 19:57:18 +00002791 md5_update( &ssl->handshake->fin_md5 , buf, len );
2792 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002793}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#endif
Paul Bakker380da532012-04-18 16:10:25 +00002795
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002796#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2797#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002798static void ssl_update_checksum_sha256( ssl_context *ssl,
2799 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002800{
Paul Bakker9e36f042013-06-30 14:34:05 +02002801 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002802}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002803#endif
Paul Bakker380da532012-04-18 16:10:25 +00002804
Paul Bakker9e36f042013-06-30 14:34:05 +02002805#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002806static void ssl_update_checksum_sha384( ssl_context *ssl,
2807 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002808{
Paul Bakker9e36f042013-06-30 14:34:05 +02002809 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002810}
Paul Bakker769075d2012-11-24 11:26:46 +01002811#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002812#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002813
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002814#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002815static void ssl_calc_finished_ssl(
2816 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002817{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002818 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002819 md5_context md5;
2820 sha1_context sha1;
2821
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 unsigned char padbuf[48];
2823 unsigned char md5sum[16];
2824 unsigned char sha1sum[20];
2825
Paul Bakker48916f92012-09-16 19:57:18 +00002826 ssl_session *session = ssl->session_negotiate;
2827 if( !session )
2828 session = ssl->session;
2829
Paul Bakker1ef83d62012-04-11 12:09:53 +00002830 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2831
Paul Bakker48916f92012-09-16 19:57:18 +00002832 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2833 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
2835 /*
2836 * SSLv3:
2837 * hash =
2838 * MD5( master + pad2 +
2839 * MD5( handshake + sender + master + pad1 ) )
2840 * + SHA1( master + pad2 +
2841 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 */
2843
Paul Bakker90995b52013-06-24 19:20:35 +02002844#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002846 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002847#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
Paul Bakker90995b52013-06-24 19:20:35 +02002849#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002851 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002852#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Paul Bakker3c2122f2013-06-24 19:03:14 +02002854 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2855 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Paul Bakker1ef83d62012-04-11 12:09:53 +00002857 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Paul Bakker3c2122f2013-06-24 19:03:14 +02002859 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002860 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002861 md5_update( &md5, padbuf, 48 );
2862 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Paul Bakker3c2122f2013-06-24 19:03:14 +02002864 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002865 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002866 sha1_update( &sha1, padbuf, 40 );
2867 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002872 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 md5_update( &md5, padbuf, 48 );
2874 md5_update( &md5, md5sum, 16 );
2875 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Paul Bakker1ef83d62012-04-11 12:09:53 +00002877 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002878 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 sha1_update( &sha1, padbuf , 40 );
2880 sha1_update( &sha1, sha1sum, 20 );
2881 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Paul Bakker1ef83d62012-04-11 12:09:53 +00002883 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Paul Bakker1ef83d62012-04-11 12:09:53 +00002885 memset( &md5, 0, sizeof( md5_context ) );
2886 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
2888 memset( padbuf, 0, sizeof( padbuf ) );
2889 memset( md5sum, 0, sizeof( md5sum ) );
2890 memset( sha1sum, 0, sizeof( sha1sum ) );
2891
2892 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2893}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002894#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002895
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002896#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002897static void ssl_calc_finished_tls(
2898 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002899{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002901 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002904 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Paul Bakker48916f92012-09-16 19:57:18 +00002906 ssl_session *session = ssl->session_negotiate;
2907 if( !session )
2908 session = ssl->session;
2909
Paul Bakker1ef83d62012-04-11 12:09:53 +00002910 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Paul Bakker48916f92012-09-16 19:57:18 +00002912 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2913 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915 /*
2916 * TLSv1:
2917 * hash = PRF( master, finished_label,
2918 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2919 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Paul Bakker90995b52013-06-24 19:20:35 +02002921#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2923 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002924#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925
Paul Bakker90995b52013-06-24 19:20:35 +02002926#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002927 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2928 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002929#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930
2931 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002932 ? "client finished"
2933 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002934
2935 md5_finish( &md5, padbuf );
2936 sha1_finish( &sha1, padbuf + 16 );
2937
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002938 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002939 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940
2941 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2942
2943 memset( &md5, 0, sizeof( md5_context ) );
2944 memset( &sha1, 0, sizeof( sha1_context ) );
2945
2946 memset( padbuf, 0, sizeof( padbuf ) );
2947
2948 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2949}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002950#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002952#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2953#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002954static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955 ssl_context *ssl, unsigned char *buf, int from )
2956{
2957 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002958 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002959 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960 unsigned char padbuf[32];
2961
Paul Bakker48916f92012-09-16 19:57:18 +00002962 ssl_session *session = ssl->session_negotiate;
2963 if( !session )
2964 session = ssl->session;
2965
Paul Bakker380da532012-04-18 16:10:25 +00002966 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002967
Paul Bakker9e36f042013-06-30 14:34:05 +02002968 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969
2970 /*
2971 * TLSv1.2:
2972 * hash = PRF( master, finished_label,
2973 * Hash( handshake ) )[0.11]
2974 */
2975
Paul Bakker9e36f042013-06-30 14:34:05 +02002976#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002977 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002978 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002979#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002980
2981 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002982 ? "client finished"
2983 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002984
Paul Bakker9e36f042013-06-30 14:34:05 +02002985 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002987 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002988 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989
2990 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2991
Paul Bakker9e36f042013-06-30 14:34:05 +02002992 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002993
2994 memset( padbuf, 0, sizeof( padbuf ) );
2995
2996 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2997}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002998#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002999
Paul Bakker9e36f042013-06-30 14:34:05 +02003000#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003001static void ssl_calc_finished_tls_sha384(
3002 ssl_context *ssl, unsigned char *buf, int from )
3003{
3004 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003005 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003006 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003007 unsigned char padbuf[48];
3008
Paul Bakker48916f92012-09-16 19:57:18 +00003009 ssl_session *session = ssl->session_negotiate;
3010 if( !session )
3011 session = ssl->session;
3012
Paul Bakker380da532012-04-18 16:10:25 +00003013 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003014
Paul Bakker9e36f042013-06-30 14:34:05 +02003015 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003016
3017 /*
3018 * TLSv1.2:
3019 * hash = PRF( master, finished_label,
3020 * Hash( handshake ) )[0.11]
3021 */
3022
Paul Bakker9e36f042013-06-30 14:34:05 +02003023#if !defined(POLARSSL_SHA512_ALT)
3024 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3025 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003026#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003027
3028 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003029 ? "client finished"
3030 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003031
Paul Bakker9e36f042013-06-30 14:34:05 +02003032 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003033
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003034 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003035 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003036
3037 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3038
Paul Bakker9e36f042013-06-30 14:34:05 +02003039 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003040
3041 memset( padbuf, 0, sizeof( padbuf ) );
3042
3043 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3044}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003045#endif /* POLARSSL_SHA512_C */
3046#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003047
Paul Bakker48916f92012-09-16 19:57:18 +00003048void ssl_handshake_wrapup( ssl_context *ssl )
3049{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003050 int resume = ssl->handshake->resume;
3051
Paul Bakker48916f92012-09-16 19:57:18 +00003052 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3053
3054 /*
3055 * Free our handshake params
3056 */
3057 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003058 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003059 ssl->handshake = NULL;
3060
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003061 if( ssl->renegotiation == SSL_RENEGOTIATION )
3062 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3063
Paul Bakker48916f92012-09-16 19:57:18 +00003064 /*
3065 * Switch in our now active transform context
3066 */
3067 if( ssl->transform )
3068 {
3069 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003070 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003071 }
3072 ssl->transform = ssl->transform_negotiate;
3073 ssl->transform_negotiate = NULL;
3074
Paul Bakker0a597072012-09-25 21:55:46 +00003075 if( ssl->session )
3076 {
3077 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003078 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003079 }
3080 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003081 ssl->session_negotiate = NULL;
3082
Paul Bakker0a597072012-09-25 21:55:46 +00003083 /*
3084 * Add cache entry
3085 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003086 if( ssl->f_set_cache != NULL &&
3087 ssl->session->length != 0 &&
3088 resume == 0 )
3089 {
Paul Bakker0a597072012-09-25 21:55:46 +00003090 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3091 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003092 }
Paul Bakker0a597072012-09-25 21:55:46 +00003093
Paul Bakker48916f92012-09-16 19:57:18 +00003094 ssl->state++;
3095
3096 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3097}
3098
Paul Bakker1ef83d62012-04-11 12:09:53 +00003099int ssl_write_finished( ssl_context *ssl )
3100{
3101 int ret, hash_len;
3102
3103 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3104
Paul Bakker92be97b2013-01-02 17:30:03 +01003105 /*
3106 * Set the out_msg pointer to the correct location based on IV length
3107 */
3108 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3109 {
3110 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3111 ssl->transform_negotiate->fixed_ivlen;
3112 }
3113 else
3114 ssl->out_msg = ssl->out_iv;
3115
Paul Bakker48916f92012-09-16 19:57:18 +00003116 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003117
3118 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003119 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3120
Paul Bakker48916f92012-09-16 19:57:18 +00003121 ssl->verify_data_len = hash_len;
3122 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3123
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 ssl->out_msglen = 4 + hash_len;
3125 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3126 ssl->out_msg[0] = SSL_HS_FINISHED;
3127
3128 /*
3129 * In case of session resuming, invert the client and server
3130 * ChangeCipherSpec messages order.
3131 */
Paul Bakker0a597072012-09-25 21:55:46 +00003132 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 {
3134 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003135 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 else
3137 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3138 }
3139 else
3140 ssl->state++;
3141
Paul Bakker48916f92012-09-16 19:57:18 +00003142 /*
3143 * Switch to our negotiated transform and session parameters for outbound data.
3144 */
3145 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3146 ssl->transform_out = ssl->transform_negotiate;
3147 ssl->session_out = ssl->session_negotiate;
3148 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003149
Paul Bakker07eb38b2012-12-19 14:42:06 +01003150#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3151 if( ssl_hw_record_activate != NULL)
3152 {
3153 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3154 {
3155 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3156 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3157 }
3158 }
3159#endif
3160
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3162 {
3163 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3164 return( ret );
3165 }
3166
3167 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3168
3169 return( 0 );
3170}
3171
3172int ssl_parse_finished( ssl_context *ssl )
3173{
Paul Bakker23986e52011-04-24 08:57:21 +00003174 int ret;
3175 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 unsigned char buf[36];
3177
3178 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3179
Paul Bakker48916f92012-09-16 19:57:18 +00003180 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Paul Bakker48916f92012-09-16 19:57:18 +00003182 /*
3183 * Switch to our negotiated transform and session parameters for inbound data.
3184 */
3185 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3186 ssl->transform_in = ssl->transform_negotiate;
3187 ssl->session_in = ssl->session_negotiate;
3188 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003189
Paul Bakker92be97b2013-01-02 17:30:03 +01003190 /*
3191 * Set the in_msg pointer to the correct location based on IV length
3192 */
3193 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3194 {
3195 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3196 ssl->transform_negotiate->fixed_ivlen;
3197 }
3198 else
3199 ssl->in_msg = ssl->in_iv;
3200
Paul Bakker07eb38b2012-12-19 14:42:06 +01003201#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3202 if( ssl_hw_record_activate != NULL)
3203 {
3204 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3205 {
3206 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3207 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3208 }
3209 }
3210#endif
3211
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3213 {
3214 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3215 return( ret );
3216 }
3217
3218 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3219 {
3220 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003221 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003222 }
3223
Paul Bakker1ef83d62012-04-11 12:09:53 +00003224 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3226
3227 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3228 ssl->in_hslen != 4 + hash_len )
3229 {
3230 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003231 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 }
3233
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003234 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 {
3236 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003237 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 }
3239
Paul Bakker48916f92012-09-16 19:57:18 +00003240 ssl->verify_data_len = hash_len;
3241 memcpy( ssl->peer_verify_data, buf, hash_len );
3242
Paul Bakker0a597072012-09-25 21:55:46 +00003243 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 {
3245 if( ssl->endpoint == SSL_IS_CLIENT )
3246 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3247
3248 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003249 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 }
3251 else
3252 ssl->state++;
3253
3254 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3255
3256 return( 0 );
3257}
3258
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003259static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003260{
3261 if( ssl->transform_negotiate )
3262 ssl_transform_free( ssl->transform_negotiate );
3263 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003264 {
3265 ssl->transform_negotiate =
3266 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3267 }
Paul Bakker48916f92012-09-16 19:57:18 +00003268
3269 if( ssl->session_negotiate )
3270 ssl_session_free( ssl->session_negotiate );
3271 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003272 {
3273 ssl->session_negotiate =
3274 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3275 }
Paul Bakker48916f92012-09-16 19:57:18 +00003276
3277 if( ssl->handshake )
3278 ssl_handshake_free( ssl->handshake );
3279 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003280 {
3281 ssl->handshake = (ssl_handshake_params *)
3282 polarssl_malloc( sizeof(ssl_handshake_params) );
3283 }
Paul Bakker48916f92012-09-16 19:57:18 +00003284
3285 if( ssl->handshake == NULL ||
3286 ssl->transform_negotiate == NULL ||
3287 ssl->session_negotiate == NULL )
3288 {
3289 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3290 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3291 }
3292
3293 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3294 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3295 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3296
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003297#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3298 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003299 md5_starts( &ssl->handshake->fin_md5 );
3300 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003301#endif
3302#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3303#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003304 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003305#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003306#if defined(POLARSSL_SHA512_C)
3307 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003308#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003309#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003310
3311 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003312 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003313
Paul Bakker61d113b2013-07-04 11:51:43 +02003314#if defined(POLARSSL_ECDH_C)
3315 ecdh_init( &ssl->handshake->ecdh_ctx );
3316#endif
3317
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003318#if defined(POLARSSL_X509_CRT_PARSE_C)
3319 ssl->handshake->key_cert = ssl->key_cert;
3320#endif
3321
Paul Bakker48916f92012-09-16 19:57:18 +00003322 return( 0 );
3323}
3324
Paul Bakker5121ce52009-01-03 21:22:43 +00003325/*
3326 * Initialize an SSL context
3327 */
3328int ssl_init( ssl_context *ssl )
3329{
Paul Bakker48916f92012-09-16 19:57:18 +00003330 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 int len = SSL_BUFFER_LEN;
3332
3333 memset( ssl, 0, sizeof( ssl_context ) );
3334
Paul Bakker62f2dee2012-09-28 07:31:51 +00003335 /*
3336 * Sane defaults
3337 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003338 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3339 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3340 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3341 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003342
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003343 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003344
Paul Bakker62f2dee2012-09-28 07:31:51 +00003345#if defined(POLARSSL_DHM_C)
3346 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3347 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3348 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3349 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3350 {
3351 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3352 return( ret );
3353 }
3354#endif
3355
3356 /*
3357 * Prepare base structures
3358 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003359 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003361 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003362 ssl->in_msg = ssl->in_ctr + 13;
3363
3364 if( ssl->in_ctr == NULL )
3365 {
3366 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003367 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003368 }
3369
Paul Bakker6e339b52013-07-03 13:37:05 +02003370 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003371 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003372 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003373 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003374
3375 if( ssl->out_ctr == NULL )
3376 {
3377 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003378 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003379 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003380 }
3381
3382 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3383 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3384
Paul Bakker606b4ba2013-08-14 16:52:14 +02003385#if defined(POLARSSL_SSL_SESSION_TICKETS)
3386 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3387#endif
3388
Paul Bakker48916f92012-09-16 19:57:18 +00003389 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3390 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
3392 return( 0 );
3393}
3394
3395/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003396 * Reset an initialized and used SSL context for re-use while retaining
3397 * all application-set variables, function pointers and data.
3398 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003399int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003400{
Paul Bakker48916f92012-09-16 19:57:18 +00003401 int ret;
3402
Paul Bakker7eb013f2011-10-06 12:37:39 +00003403 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003404 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3405 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3406
3407 ssl->verify_data_len = 0;
3408 memset( ssl->own_verify_data, 0, 36 );
3409 memset( ssl->peer_verify_data, 0, 36 );
3410
Paul Bakker7eb013f2011-10-06 12:37:39 +00003411 ssl->in_offt = NULL;
3412
Paul Bakker92be97b2013-01-02 17:30:03 +01003413 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003414 ssl->in_msgtype = 0;
3415 ssl->in_msglen = 0;
3416 ssl->in_left = 0;
3417
3418 ssl->in_hslen = 0;
3419 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003420 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003421
Paul Bakker92be97b2013-01-02 17:30:03 +01003422 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003423 ssl->out_msgtype = 0;
3424 ssl->out_msglen = 0;
3425 ssl->out_left = 0;
3426
Paul Bakker48916f92012-09-16 19:57:18 +00003427 ssl->transform_in = NULL;
3428 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003429
3430 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3431 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003432
3433#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3434 if( ssl_hw_record_reset != NULL)
3435 {
3436 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003437 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003438 {
3439 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3440 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3441 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003442 }
3443#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003444
Paul Bakker48916f92012-09-16 19:57:18 +00003445 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003446 {
Paul Bakker48916f92012-09-16 19:57:18 +00003447 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003448 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003449 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003450 }
Paul Bakker48916f92012-09-16 19:57:18 +00003451
Paul Bakkerc0463502013-02-14 11:19:38 +01003452 if( ssl->session )
3453 {
3454 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003455 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003456 ssl->session = NULL;
3457 }
3458
Paul Bakker48916f92012-09-16 19:57:18 +00003459 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3460 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003461
3462 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003463}
3464
Paul Bakkera503a632013-08-14 13:48:06 +02003465#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003466/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003467 * Allocate and initialize ticket keys
3468 */
3469static int ssl_ticket_keys_init( ssl_context *ssl )
3470{
3471 int ret;
3472 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003473 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003474
3475 if( ssl->ticket_keys != NULL )
3476 return( 0 );
3477
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003478 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3479 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003480 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3481
3482 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003483 {
3484 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003485 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003486 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003487
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003488 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3489 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3490 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3491 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003492 polarssl_free( tkeys );
3493 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003494 }
3495
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003496 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003497 {
3498 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003499 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003500 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003501
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003502 ssl->ticket_keys = tkeys;
3503
3504 return( 0 );
3505}
Paul Bakkera503a632013-08-14 13:48:06 +02003506#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003507
3508/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003509 * SSL set accessors
3510 */
3511void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3512{
3513 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003514
Paul Bakker606b4ba2013-08-14 16:52:14 +02003515#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003516 if( endpoint == SSL_IS_CLIENT )
3517 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003518#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003519}
3520
3521void ssl_set_authmode( ssl_context *ssl, int authmode )
3522{
3523 ssl->authmode = authmode;
3524}
3525
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003526#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003527void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003528 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003529 void *p_vrfy )
3530{
3531 ssl->f_vrfy = f_vrfy;
3532 ssl->p_vrfy = p_vrfy;
3533}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003534#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003535
Paul Bakker5121ce52009-01-03 21:22:43 +00003536void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003537 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003538 void *p_rng )
3539{
3540 ssl->f_rng = f_rng;
3541 ssl->p_rng = p_rng;
3542}
3543
3544void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003545 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003546 void *p_dbg )
3547{
3548 ssl->f_dbg = f_dbg;
3549 ssl->p_dbg = p_dbg;
3550}
3551
3552void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003553 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003554 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003555{
3556 ssl->f_recv = f_recv;
3557 ssl->f_send = f_send;
3558 ssl->p_recv = p_recv;
3559 ssl->p_send = p_send;
3560}
3561
Paul Bakker0a597072012-09-25 21:55:46 +00003562void ssl_set_session_cache( ssl_context *ssl,
3563 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3564 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003565{
Paul Bakker0a597072012-09-25 21:55:46 +00003566 ssl->f_get_cache = f_get_cache;
3567 ssl->p_get_cache = p_get_cache;
3568 ssl->f_set_cache = f_set_cache;
3569 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003570}
3571
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003572int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003573{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003574 int ret;
3575
3576 if( ssl == NULL ||
3577 session == NULL ||
3578 ssl->session_negotiate == NULL ||
3579 ssl->endpoint != SSL_IS_CLIENT )
3580 {
3581 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3582 }
3583
3584 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3585 return( ret );
3586
Paul Bakker0a597072012-09-25 21:55:46 +00003587 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003588
3589 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003590}
3591
Paul Bakkerb68cad62012-08-23 08:34:18 +00003592void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003593{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003594 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3595 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3596 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3597 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3598}
3599
3600void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3601 int major, int minor )
3602{
3603 if( major != SSL_MAJOR_VERSION_3 )
3604 return;
3605
3606 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3607 return;
3608
3609 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003610}
3611
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003612#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003613/* Add a new (empty) key_cert entry an return a pointer to it */
3614static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3615{
3616 ssl_key_cert *key_cert, *last;
3617
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003618 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3619 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003620 return( NULL );
3621
3622 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3623
3624 /* Append the new key_cert to the (possibly empty) current list */
3625 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003626 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003627 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003628 if( ssl->handshake != NULL )
3629 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003630 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003631 else
3632 {
3633 last = ssl->key_cert;
3634 while( last->next != NULL )
3635 last = last->next;
3636 last->next = key_cert;
3637 }
3638
3639 return key_cert;
3640}
3641
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003642void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003643 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003644{
3645 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003646 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 ssl->peer_cn = peer_cn;
3648}
3649
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003650int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003651 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003652{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003653 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3654
3655 if( key_cert == NULL )
3656 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3657
3658 key_cert->cert = own_cert;
3659 key_cert->key = pk_key;
3660
3661 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003662}
3663
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003664#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003665int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003666 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003667{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003668 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003669 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003670
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003671 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003672 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3673
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003674 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3675 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003676 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003677
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003678 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003679
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003680 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003681 if( ret != 0 )
3682 return( ret );
3683
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003684 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003685 return( ret );
3686
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003687 key_cert->cert = own_cert;
3688 key_cert->key_own_alloc = 1;
3689
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003690 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003691}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003692#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003693
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003694int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003695 void *rsa_key,
3696 rsa_decrypt_func rsa_decrypt,
3697 rsa_sign_func rsa_sign,
3698 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003699{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003700 int ret;
3701 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003702
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003703 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003704 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3705
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003706 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3707 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003708 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003709
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003710 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003711
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003712 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3713 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3714 return( ret );
3715
3716 key_cert->cert = own_cert;
3717 key_cert->key_own_alloc = 1;
3718
3719 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003720}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003721#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003722
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003723#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003724int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3725 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003726{
Paul Bakker6db455e2013-09-18 17:29:31 +02003727 if( psk == NULL || psk_identity == NULL )
3728 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3729
3730 if( ssl->psk != NULL )
3731 {
3732 polarssl_free( ssl->psk );
3733 polarssl_free( ssl->psk_identity );
3734 }
3735
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003736 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003737 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003738
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003739 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3740 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003741
3742 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3743 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3744
3745 memcpy( ssl->psk, psk, ssl->psk_len );
3746 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003747
3748 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003749}
3750
3751void ssl_set_psk_cb( ssl_context *ssl,
3752 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3753 size_t),
3754 void *p_psk )
3755{
3756 ssl->f_psk = f_psk;
3757 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003758}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003759#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003760
Paul Bakker48916f92012-09-16 19:57:18 +00003761#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003762int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003763{
3764 int ret;
3765
Paul Bakker48916f92012-09-16 19:57:18 +00003766 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 {
3768 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3769 return( ret );
3770 }
3771
Paul Bakker48916f92012-09-16 19:57:18 +00003772 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 {
3774 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3775 return( ret );
3776 }
3777
3778 return( 0 );
3779}
3780
Paul Bakker1b57b062011-01-06 15:48:19 +00003781int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3782{
3783 int ret;
3784
Paul Bakker48916f92012-09-16 19:57:18 +00003785 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003786 {
3787 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3788 return( ret );
3789 }
3790
Paul Bakker48916f92012-09-16 19:57:18 +00003791 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003792 {
3793 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3794 return( ret );
3795 }
3796
3797 return( 0 );
3798}
Paul Bakker48916f92012-09-16 19:57:18 +00003799#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003800
Paul Bakker0be444a2013-08-27 21:55:01 +02003801#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003802int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003803{
3804 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003805 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003806
3807 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003808
3809 if( ssl->hostname_len + 1 == 0 )
3810 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3811
Paul Bakker6e339b52013-07-03 13:37:05 +02003812 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003813
Paul Bakkerb15b8512012-01-13 13:44:06 +00003814 if( ssl->hostname == NULL )
3815 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3816
Paul Bakker3c2122f2013-06-24 19:03:14 +02003817 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003818 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003819
Paul Bakker40ea7de2009-05-03 10:18:48 +00003820 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003821
3822 return( 0 );
3823}
3824
Paul Bakker5701cdc2012-09-27 21:49:42 +00003825void ssl_set_sni( ssl_context *ssl,
3826 int (*f_sni)(void *, ssl_context *,
3827 const unsigned char *, size_t),
3828 void *p_sni )
3829{
3830 ssl->f_sni = f_sni;
3831 ssl->p_sni = p_sni;
3832}
Paul Bakker0be444a2013-08-27 21:55:01 +02003833#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003834
Paul Bakker490ecc82011-10-06 13:04:09 +00003835void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3836{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003837 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3838 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3839 {
3840 ssl->max_major_ver = major;
3841 ssl->max_minor_ver = minor;
3842 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003843}
3844
Paul Bakker1d29fb52012-09-28 13:28:45 +00003845void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3846{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003847 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3848 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3849 {
3850 ssl->min_major_ver = major;
3851 ssl->min_minor_ver = minor;
3852 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003853}
3854
Paul Bakker05decb22013-08-15 13:33:48 +02003855#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003856int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3857{
Paul Bakker77e257e2013-12-16 15:29:52 +01003858 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003859 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003860 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003861 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003862 }
3863
3864 ssl->mfl_code = mfl_code;
3865
3866 return( 0 );
3867}
Paul Bakker05decb22013-08-15 13:33:48 +02003868#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003869
Paul Bakker1f2bc622013-08-15 13:45:55 +02003870#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003871int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003872{
3873 if( ssl->endpoint != SSL_IS_CLIENT )
3874 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3875
Paul Bakker8c1ede62013-07-19 14:14:37 +02003876 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003877
3878 return( 0 );
3879}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003880#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003881
Paul Bakker48916f92012-09-16 19:57:18 +00003882void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3883{
3884 ssl->disable_renegotiation = renegotiation;
3885}
3886
3887void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3888{
3889 ssl->allow_legacy_renegotiation = allow_legacy;
3890}
3891
Paul Bakkera503a632013-08-14 13:48:06 +02003892#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003893int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3894{
3895 ssl->session_tickets = use_tickets;
3896
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003897 if( ssl->endpoint == SSL_IS_CLIENT )
3898 return( 0 );
3899
3900 if( ssl->f_rng == NULL )
3901 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3902
3903 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003904}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003905
3906void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3907{
3908 ssl->ticket_lifetime = lifetime;
3909}
Paul Bakkera503a632013-08-14 13:48:06 +02003910#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003911
Paul Bakker5121ce52009-01-03 21:22:43 +00003912/*
3913 * SSL get accessors
3914 */
Paul Bakker23986e52011-04-24 08:57:21 +00003915size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003916{
3917 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3918}
3919
Paul Bakkerff60ee62010-03-16 21:09:09 +00003920int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003921{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003922 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003923}
3924
Paul Bakkere3166ce2011-01-27 17:40:50 +00003925const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003926{
Paul Bakker926c8e42013-03-06 10:23:34 +01003927 if( ssl == NULL || ssl->session == NULL )
3928 return NULL;
3929
Paul Bakkere3166ce2011-01-27 17:40:50 +00003930 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003931}
3932
Paul Bakker43ca69c2011-01-15 17:35:19 +00003933const char *ssl_get_version( const ssl_context *ssl )
3934{
3935 switch( ssl->minor_ver )
3936 {
3937 case SSL_MINOR_VERSION_0:
3938 return( "SSLv3.0" );
3939
3940 case SSL_MINOR_VERSION_1:
3941 return( "TLSv1.0" );
3942
3943 case SSL_MINOR_VERSION_2:
3944 return( "TLSv1.1" );
3945
Paul Bakker1ef83d62012-04-11 12:09:53 +00003946 case SSL_MINOR_VERSION_3:
3947 return( "TLSv1.2" );
3948
Paul Bakker43ca69c2011-01-15 17:35:19 +00003949 default:
3950 break;
3951 }
3952 return( "unknown" );
3953}
3954
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003955#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003956const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003957{
3958 if( ssl == NULL || ssl->session == NULL )
3959 return NULL;
3960
3961 return ssl->session->peer_cert;
3962}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003963#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003964
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003965int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3966{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003967 if( ssl == NULL ||
3968 dst == NULL ||
3969 ssl->session == NULL ||
3970 ssl->endpoint != SSL_IS_CLIENT )
3971 {
3972 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3973 }
3974
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003975 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003976}
3977
Paul Bakker5121ce52009-01-03 21:22:43 +00003978/*
Paul Bakker1961b702013-01-25 14:49:24 +01003979 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003980 */
Paul Bakker1961b702013-01-25 14:49:24 +01003981int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003982{
Paul Bakker40e46942009-01-03 21:51:57 +00003983 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003984
Paul Bakker40e46942009-01-03 21:51:57 +00003985#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003986 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003987 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003988#endif
3989
Paul Bakker40e46942009-01-03 21:51:57 +00003990#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003991 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003992 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003993#endif
3994
Paul Bakker1961b702013-01-25 14:49:24 +01003995 return( ret );
3996}
3997
3998/*
3999 * Perform the SSL handshake
4000 */
4001int ssl_handshake( ssl_context *ssl )
4002{
4003 int ret = 0;
4004
4005 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4006
4007 while( ssl->state != SSL_HANDSHAKE_OVER )
4008 {
4009 ret = ssl_handshake_step( ssl );
4010
4011 if( ret != 0 )
4012 break;
4013 }
4014
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4016
4017 return( ret );
4018}
4019
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004020#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004021/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004022 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004023 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004024static int ssl_write_hello_request( ssl_context *ssl )
4025{
4026 int ret;
4027
4028 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4029
4030 ssl->out_msglen = 4;
4031 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4032 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4033
4034 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4035 {
4036 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4037 return( ret );
4038 }
4039
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004040 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4041
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004042 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4043
4044 return( 0 );
4045}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004046#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004047
4048/*
4049 * Actually renegotiate current connection, triggered by either:
4050 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004051 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004052 * - receiving any handshake message on server during ssl_read() after the
4053 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004054 * If the handshake doesn't complete due to waiting for I/O, it will continue
4055 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004056 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004057static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004058{
4059 int ret;
4060
4061 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4062
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004063 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4064 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004065
4066 ssl->state = SSL_HELLO_REQUEST;
4067 ssl->renegotiation = SSL_RENEGOTIATION;
4068
Paul Bakker48916f92012-09-16 19:57:18 +00004069 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4070 {
4071 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4072 return( ret );
4073 }
4074
4075 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4076
4077 return( 0 );
4078}
4079
4080/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004081 * Renegotiate current connection on client,
4082 * or request renegotiation on server
4083 */
4084int ssl_renegotiate( ssl_context *ssl )
4085{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004086 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004087
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004088#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004089 /* On server, just send the request */
4090 if( ssl->endpoint == SSL_IS_SERVER )
4091 {
4092 if( ssl->state != SSL_HANDSHAKE_OVER )
4093 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4094
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004095 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004096 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004097#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004098
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004099#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004100 /*
4101 * On client, either start the renegotiation process or,
4102 * if already in progress, continue the handshake
4103 */
4104 if( ssl->renegotiation != SSL_RENEGOTIATION )
4105 {
4106 if( ssl->state != SSL_HANDSHAKE_OVER )
4107 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4108
4109 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4110 {
4111 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4112 return( ret );
4113 }
4114 }
4115 else
4116 {
4117 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4118 {
4119 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4120 return( ret );
4121 }
4122 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004123#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004124
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004125 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004126}
4127
4128/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004129 * Receive application data decrypted from the SSL layer
4130 */
Paul Bakker23986e52011-04-24 08:57:21 +00004131int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004132{
Paul Bakker23986e52011-04-24 08:57:21 +00004133 int ret;
4134 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004135
4136 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4137
4138 if( ssl->state != SSL_HANDSHAKE_OVER )
4139 {
4140 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4141 {
4142 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4143 return( ret );
4144 }
4145 }
4146
4147 if( ssl->in_offt == NULL )
4148 {
4149 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4150 {
Paul Bakker831a7552011-05-18 13:32:51 +00004151 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4152 return( 0 );
4153
Paul Bakker5121ce52009-01-03 21:22:43 +00004154 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4155 return( ret );
4156 }
4157
4158 if( ssl->in_msglen == 0 &&
4159 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4160 {
4161 /*
4162 * OpenSSL sends empty messages to randomize the IV
4163 */
4164 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4165 {
Paul Bakker831a7552011-05-18 13:32:51 +00004166 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4167 return( 0 );
4168
Paul Bakker5121ce52009-01-03 21:22:43 +00004169 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4170 return( ret );
4171 }
4172 }
4173
Paul Bakker48916f92012-09-16 19:57:18 +00004174 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4175 {
4176 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4177
4178 if( ssl->endpoint == SSL_IS_CLIENT &&
4179 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4180 ssl->in_hslen != 4 ) )
4181 {
4182 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4183 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4184 }
4185
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004186 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4187 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4188 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004189 {
4190 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4191
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004192#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004193 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004194 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004195 /*
4196 * SSLv3 does not have a "no_renegotiation" alert
4197 */
4198 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4199 return( ret );
4200 }
4201 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004202#endif
4203#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4204 defined(POLARSSL_SSL_PROTO_TLS1_2)
4205 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004206 {
4207 if( ( ret = ssl_send_alert_message( ssl,
4208 SSL_ALERT_LEVEL_WARNING,
4209 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4210 {
4211 return( ret );
4212 }
Paul Bakker48916f92012-09-16 19:57:18 +00004213 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004214 else
4215#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004216 {
4217 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004218 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004219 }
Paul Bakker48916f92012-09-16 19:57:18 +00004220 }
4221 else
4222 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004223 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004224 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004225 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004226 return( ret );
4227 }
4228
4229 return( POLARSSL_ERR_NET_WANT_READ );
4230 }
4231 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004232 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4233 {
4234 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4235 "but not honored by client" ) );
4236 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4237 }
Paul Bakker48916f92012-09-16 19:57:18 +00004238 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004239 {
4240 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004241 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004242 }
4243
4244 ssl->in_offt = ssl->in_msg;
4245 }
4246
4247 n = ( len < ssl->in_msglen )
4248 ? len : ssl->in_msglen;
4249
4250 memcpy( buf, ssl->in_offt, n );
4251 ssl->in_msglen -= n;
4252
4253 if( ssl->in_msglen == 0 )
4254 /* all bytes consumed */
4255 ssl->in_offt = NULL;
4256 else
4257 /* more data available */
4258 ssl->in_offt += n;
4259
4260 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4261
Paul Bakker23986e52011-04-24 08:57:21 +00004262 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004263}
4264
4265/*
4266 * Send application data to be encrypted by the SSL layer
4267 */
Paul Bakker23986e52011-04-24 08:57:21 +00004268int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004269{
Paul Bakker23986e52011-04-24 08:57:21 +00004270 int ret;
4271 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004272 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004273
4274 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4275
4276 if( ssl->state != SSL_HANDSHAKE_OVER )
4277 {
4278 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4279 {
4280 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4281 return( ret );
4282 }
4283 }
4284
Paul Bakker05decb22013-08-15 13:33:48 +02004285#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004286 /*
4287 * Assume mfl_code is correct since it was checked when set
4288 */
4289 max_len = mfl_code_to_length[ssl->mfl_code];
4290
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004291 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004292 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004293 */
4294 if( ssl->session_out != NULL &&
4295 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4296 {
4297 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4298 }
Paul Bakker05decb22013-08-15 13:33:48 +02004299#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004300
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004301 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004302
Paul Bakker5121ce52009-01-03 21:22:43 +00004303 if( ssl->out_left != 0 )
4304 {
4305 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4306 {
4307 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4308 return( ret );
4309 }
4310 }
Paul Bakker887bd502011-06-08 13:10:54 +00004311 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004312 {
Paul Bakker887bd502011-06-08 13:10:54 +00004313 ssl->out_msglen = n;
4314 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4315 memcpy( ssl->out_msg, buf, n );
4316
4317 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4318 {
4319 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4320 return( ret );
4321 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 }
4323
4324 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4325
Paul Bakker23986e52011-04-24 08:57:21 +00004326 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004327}
4328
4329/*
4330 * Notify the peer that the connection is being closed
4331 */
4332int ssl_close_notify( ssl_context *ssl )
4333{
4334 int ret;
4335
4336 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4337
4338 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4339 {
4340 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4341 return( ret );
4342 }
4343
4344 if( ssl->state == SSL_HANDSHAKE_OVER )
4345 {
Paul Bakker48916f92012-09-16 19:57:18 +00004346 if( ( ret = ssl_send_alert_message( ssl,
4347 SSL_ALERT_LEVEL_WARNING,
4348 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004349 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004350 return( ret );
4351 }
4352 }
4353
4354 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4355
4356 return( ret );
4357}
4358
Paul Bakker48916f92012-09-16 19:57:18 +00004359void ssl_transform_free( ssl_transform *transform )
4360{
4361#if defined(POLARSSL_ZLIB_SUPPORT)
4362 deflateEnd( &transform->ctx_deflate );
4363 inflateEnd( &transform->ctx_inflate );
4364#endif
4365
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004366 cipher_free_ctx( &transform->cipher_ctx_enc );
4367 cipher_free_ctx( &transform->cipher_ctx_dec );
4368
Paul Bakker61d113b2013-07-04 11:51:43 +02004369 md_free_ctx( &transform->md_ctx_enc );
4370 md_free_ctx( &transform->md_ctx_dec );
4371
Paul Bakker48916f92012-09-16 19:57:18 +00004372 memset( transform, 0, sizeof( ssl_transform ) );
4373}
4374
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004375#if defined(POLARSSL_X509_CRT_PARSE_C)
4376static void ssl_key_cert_free( ssl_key_cert *key_cert )
4377{
4378 ssl_key_cert *cur = key_cert, *next;
4379
4380 while( cur != NULL )
4381 {
4382 next = cur->next;
4383
4384 if( cur->key_own_alloc )
4385 {
4386 pk_free( cur->key );
4387 polarssl_free( cur->key );
4388 }
4389 polarssl_free( cur );
4390
4391 cur = next;
4392 }
4393}
4394#endif /* POLARSSL_X509_CRT_PARSE_C */
4395
Paul Bakker48916f92012-09-16 19:57:18 +00004396void ssl_handshake_free( ssl_handshake_params *handshake )
4397{
4398#if defined(POLARSSL_DHM_C)
4399 dhm_free( &handshake->dhm_ctx );
4400#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004401#if defined(POLARSSL_ECDH_C)
4402 ecdh_free( &handshake->ecdh_ctx );
4403#endif
4404
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004405#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004406 /* explicit void pointer cast for buggy MS compiler */
4407 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004408#endif
4409
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004410#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4411 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4412 /*
4413 * Free only the linked list wrapper, not the keys themselves
4414 * since the belong to the SNI callback
4415 */
4416 if( handshake->sni_key_cert != NULL )
4417 {
4418 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4419
4420 while( cur != NULL )
4421 {
4422 next = cur->next;
4423 polarssl_free( cur );
4424 cur = next;
4425 }
4426 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004427#endif
4428
Paul Bakker48916f92012-09-16 19:57:18 +00004429 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4430}
4431
4432void ssl_session_free( ssl_session *session )
4433{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004434#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004435 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004436 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004437 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004438 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004439 }
Paul Bakkered27a042013-04-18 22:46:23 +02004440#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004441
Paul Bakkera503a632013-08-14 13:48:06 +02004442#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004443 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004444#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004445
Paul Bakker0a597072012-09-25 21:55:46 +00004446 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004447}
4448
Paul Bakker5121ce52009-01-03 21:22:43 +00004449/*
4450 * Free an SSL context
4451 */
4452void ssl_free( ssl_context *ssl )
4453{
4454 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4455
Paul Bakker5121ce52009-01-03 21:22:43 +00004456 if( ssl->out_ctr != NULL )
4457 {
4458 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004459 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004460 }
4461
4462 if( ssl->in_ctr != NULL )
4463 {
4464 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004465 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004466 }
4467
Paul Bakker16770332013-10-11 09:59:44 +02004468#if defined(POLARSSL_ZLIB_SUPPORT)
4469 if( ssl->compress_buf != NULL )
4470 {
4471 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4472 polarssl_free( ssl->compress_buf );
4473 }
4474#endif
4475
Paul Bakker40e46942009-01-03 21:51:57 +00004476#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004477 mpi_free( &ssl->dhm_P );
4478 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004479#endif
4480
Paul Bakker48916f92012-09-16 19:57:18 +00004481 if( ssl->transform )
4482 {
4483 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004484 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004485 }
4486
4487 if( ssl->handshake )
4488 {
4489 ssl_handshake_free( ssl->handshake );
4490 ssl_transform_free( ssl->transform_negotiate );
4491 ssl_session_free( ssl->session_negotiate );
4492
Paul Bakker6e339b52013-07-03 13:37:05 +02004493 polarssl_free( ssl->handshake );
4494 polarssl_free( ssl->transform_negotiate );
4495 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004496 }
4497
Paul Bakkerc0463502013-02-14 11:19:38 +01004498 if( ssl->session )
4499 {
4500 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004501 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004502 }
4503
Paul Bakkera503a632013-08-14 13:48:06 +02004504#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004505 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004506#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004507
Paul Bakker0be444a2013-08-27 21:55:01 +02004508#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004509 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004510 {
4511 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004512 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004513 ssl->hostname_len = 0;
4514 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004515#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004516
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004517#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004518 if( ssl->psk != NULL )
4519 {
4520 memset( ssl->psk, 0, ssl->psk_len );
4521 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4522 polarssl_free( ssl->psk );
4523 polarssl_free( ssl->psk_identity );
4524 ssl->psk_len = 0;
4525 ssl->psk_identity_len = 0;
4526 }
4527#endif
4528
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004529#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004530 ssl_key_cert_free( ssl->key_cert );
4531#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004532
Paul Bakker05ef8352012-05-08 09:17:57 +00004533#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4534 if( ssl_hw_record_finish != NULL )
4535 {
4536 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4537 ssl_hw_record_finish( ssl );
4538 }
4539#endif
4540
Paul Bakker5121ce52009-01-03 21:22:43 +00004541 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004542
Paul Bakker86f04f42013-02-14 11:20:09 +01004543 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004544 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004545}
4546
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004547#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004548/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004549 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004550 */
4551unsigned char ssl_sig_from_pk( pk_context *pk )
4552{
4553#if defined(POLARSSL_RSA_C)
4554 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4555 return( SSL_SIG_RSA );
4556#endif
4557#if defined(POLARSSL_ECDSA_C)
4558 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4559 return( SSL_SIG_ECDSA );
4560#endif
4561 return( SSL_SIG_ANON );
4562}
4563
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004564pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4565{
4566 switch( sig )
4567 {
4568#if defined(POLARSSL_RSA_C)
4569 case SSL_SIG_RSA:
4570 return( POLARSSL_PK_RSA );
4571#endif
4572#if defined(POLARSSL_ECDSA_C)
4573 case SSL_SIG_ECDSA:
4574 return( POLARSSL_PK_ECDSA );
4575#endif
4576 default:
4577 return( POLARSSL_PK_NONE );
4578 }
4579}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004580#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004581
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004582/*
4583 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4584 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004585md_type_t ssl_md_alg_from_hash( unsigned char hash )
4586{
4587 switch( hash )
4588 {
4589#if defined(POLARSSL_MD5_C)
4590 case SSL_HASH_MD5:
4591 return( POLARSSL_MD_MD5 );
4592#endif
4593#if defined(POLARSSL_SHA1_C)
4594 case SSL_HASH_SHA1:
4595 return( POLARSSL_MD_SHA1 );
4596#endif
4597#if defined(POLARSSL_SHA256_C)
4598 case SSL_HASH_SHA224:
4599 return( POLARSSL_MD_SHA224 );
4600 case SSL_HASH_SHA256:
4601 return( POLARSSL_MD_SHA256 );
4602#endif
4603#if defined(POLARSSL_SHA512_C)
4604 case SSL_HASH_SHA384:
4605 return( POLARSSL_MD_SHA384 );
4606 case SSL_HASH_SHA512:
4607 return( POLARSSL_MD_SHA512 );
4608#endif
4609 default:
4610 return( POLARSSL_MD_NONE );
4611 }
4612}
4613
Paul Bakker5121ce52009-01-03 21:22:43 +00004614#endif