blob: ce05d7a2e5f1a1b0754b94c3dcd7645968f6e40e [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 {
599 memcpy( transform->mac_enc, mac_enc, transform->maclen );
600 memcpy( transform->mac_dec, mac_dec, transform->maclen );
601 }
602 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200603#endif
604#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
605 defined(POLARSSL_SSL_PROTO_TLS1_2)
606 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100607 {
608 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
609 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
610 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200611 else
612#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200613 {
614 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200615 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200616 }
Paul Bakker68884e32013-01-07 18:20:04 +0100617
Paul Bakker05ef8352012-05-08 09:17:57 +0000618#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
619 if( ssl_hw_record_init != NULL)
620 {
621 int ret = 0;
622
623 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
624
Paul Bakker07eb38b2012-12-19 14:42:06 +0100625 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
626 transform->iv_enc, transform->iv_dec,
627 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100628 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100629 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000630 {
631 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
632 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
633 }
634 }
635#endif
636
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200637 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
638 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200640 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
641 return( ret );
642 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200643
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200644 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
645 cipher_info ) ) != 0 )
646 {
647 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
648 return( ret );
649 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200650
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200651 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
652 cipher_info->key_length,
653 POLARSSL_ENCRYPT ) ) != 0 )
654 {
655 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
656 return( ret );
657 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200658
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200659 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
660 cipher_info->key_length,
661 POLARSSL_DECRYPT ) ) != 0 )
662 {
663 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
664 return( ret );
665 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200666
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200667#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200668 if( cipher_info->mode == POLARSSL_MODE_CBC )
669 {
670 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
671 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200672 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200673 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
674 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200675 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200676
677 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
678 POLARSSL_PADDING_NONE ) ) != 0 )
679 {
680 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
681 return( ret );
682 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686 memset( keyblk, 0, sizeof( keyblk ) );
687
Paul Bakker2770fbd2012-07-03 13:30:23 +0000688#if defined(POLARSSL_ZLIB_SUPPORT)
689 // Initialize compression
690 //
Paul Bakker48916f92012-09-16 19:57:18 +0000691 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000692 {
Paul Bakker16770332013-10-11 09:59:44 +0200693 if( ssl->compress_buf == NULL )
694 {
695 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
696 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
697 if( ssl->compress_buf == NULL )
698 {
699 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
700 SSL_BUFFER_LEN ) );
701 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
702 }
703 }
704
Paul Bakker2770fbd2012-07-03 13:30:23 +0000705 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
706
Paul Bakker48916f92012-09-16 19:57:18 +0000707 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
708 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000709
Paul Bakker48916f92012-09-16 19:57:18 +0000710 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
711 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000712 {
713 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
714 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
715 }
716 }
717#endif /* POLARSSL_ZLIB_SUPPORT */
718
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
720
721 return( 0 );
722}
723
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200724#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000725void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000726{
727 md5_context md5;
728 sha1_context sha1;
729 unsigned char pad_1[48];
730 unsigned char pad_2[48];
731
Paul Bakker380da532012-04-18 16:10:25 +0000732 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Paul Bakker48916f92012-09-16 19:57:18 +0000734 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
735 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
Paul Bakker380da532012-04-18 16:10:25 +0000737 memset( pad_1, 0x36, 48 );
738 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Paul Bakker48916f92012-09-16 19:57:18 +0000740 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000741 md5_update( &md5, pad_1, 48 );
742 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
Paul Bakker380da532012-04-18 16:10:25 +0000744 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000745 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000746 md5_update( &md5, pad_2, 48 );
747 md5_update( &md5, hash, 16 );
748 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker48916f92012-09-16 19:57:18 +0000750 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000751 sha1_update( &sha1, pad_1, 40 );
752 sha1_finish( &sha1, hash + 16 );
753
754 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000755 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000756 sha1_update( &sha1, pad_2, 40 );
757 sha1_update( &sha1, hash + 16, 20 );
758 sha1_finish( &sha1, hash + 16 );
759
760 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
761 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
762
763 return;
764}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200765#endif
Paul Bakker380da532012-04-18 16:10:25 +0000766
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200767#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000768void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
769{
770 md5_context md5;
771 sha1_context sha1;
772
773 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
774
Paul Bakker48916f92012-09-16 19:57:18 +0000775 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
776 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000777
Paul Bakker48916f92012-09-16 19:57:18 +0000778 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000779 sha1_finish( &sha1, hash + 16 );
780
781 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
782 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
783
784 return;
785}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200786#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000787
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200788#if defined(POLARSSL_SSL_PROTO_TLS1_2)
789#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000790void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
791{
Paul Bakker9e36f042013-06-30 14:34:05 +0200792 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000793
794 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
795
Paul Bakker9e36f042013-06-30 14:34:05 +0200796 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
797 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000798
799 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
800 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
801
802 return;
803}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000805
Paul Bakker9e36f042013-06-30 14:34:05 +0200806#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000807void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
808{
Paul Bakker9e36f042013-06-30 14:34:05 +0200809 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000810
811 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
812
Paul Bakker9e36f042013-06-30 14:34:05 +0200813 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
814 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000815
Paul Bakkerca4ab492012-04-18 14:23:57 +0000816 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
818
819 return;
820}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200821#endif /* POLARSSL_SHA512_C */
822#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000823
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200824#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200825int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
826{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200827 unsigned char *p = ssl->handshake->premaster;
828 unsigned char *end = p + sizeof( ssl->handshake->premaster );
829
830 /*
831 * PMS = struct {
832 * opaque other_secret<0..2^16-1>;
833 * opaque psk<0..2^16-1>;
834 * };
835 * with "other_secret" depending on the particular key exchange
836 */
837#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
838 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
839 {
840 if( end - p < 2 + (int) ssl->psk_len )
841 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
842
843 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
844 *(p++) = (unsigned char)( ssl->psk_len );
845 p += ssl->psk_len;
846 }
847 else
848#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200849#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
850 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
851 {
852 /*
853 * other_secret already set by the ClientKeyExchange message,
854 * and is 48 bytes long
855 */
856 *p++ = 0;
857 *p++ = 48;
858 p += 48;
859 }
860 else
861#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200862#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
863 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
864 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200865 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200866 size_t len = ssl->handshake->dhm_ctx.len;
867
868 if( end - p < 2 + (int) len )
869 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
870
871 *(p++) = (unsigned char)( len >> 8 );
872 *(p++) = (unsigned char)( len );
873 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
874 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
875 {
876 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
877 return( ret );
878 }
879 p += len;
880
881 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
882 }
883 else
884#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
885#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
886 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
887 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200888 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200889 size_t zlen;
890
891 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
892 p + 2, end - (p + 2),
893 ssl->f_rng, ssl->p_rng ) ) != 0 )
894 {
895 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
896 return( ret );
897 }
898
899 *(p++) = (unsigned char)( zlen >> 8 );
900 *(p++) = (unsigned char)( zlen );
901 p += zlen;
902
903 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
904 }
905 else
906#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
907 {
908 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
909 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
910 }
911
912 /* opaque psk<0..2^16-1>; */
913 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
914 *(p++) = (unsigned char)( ssl->psk_len );
915 memcpy( p, ssl->psk, ssl->psk_len );
916 p += ssl->psk_len;
917
918 ssl->handshake->pmslen = p - ssl->handshake->premaster;
919
920 return( 0 );
921}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200922#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200923
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200924#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000925/*
926 * SSLv3.0 MAC functions
927 */
Paul Bakker68884e32013-01-07 18:20:04 +0100928static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
929 unsigned char *buf, size_t len,
930 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000931{
932 unsigned char header[11];
933 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100934 int padlen = 0;
935 int md_size = md_get_size( md_ctx->md_info );
936 int md_type = md_get_type( md_ctx->md_info );
937
938 if( md_type == POLARSSL_MD_MD5 )
939 padlen = 48;
940 else if( md_type == POLARSSL_MD_SHA1 )
941 padlen = 40;
942 else if( md_type == POLARSSL_MD_SHA256 )
943 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100944 else if( md_type == POLARSSL_MD_SHA384 )
945 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
947 memcpy( header, ctr, 8 );
948 header[ 8] = (unsigned char) type;
949 header[ 9] = (unsigned char)( len >> 8 );
950 header[10] = (unsigned char)( len );
951
Paul Bakker68884e32013-01-07 18:20:04 +0100952 memset( padding, 0x36, padlen );
953 md_starts( md_ctx );
954 md_update( md_ctx, secret, md_size );
955 md_update( md_ctx, padding, padlen );
956 md_update( md_ctx, header, 11 );
957 md_update( md_ctx, buf, len );
958 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Paul Bakker68884e32013-01-07 18:20:04 +0100960 memset( padding, 0x5C, padlen );
961 md_starts( md_ctx );
962 md_update( md_ctx, secret, md_size );
963 md_update( md_ctx, padding, padlen );
964 md_update( md_ctx, buf + len, md_size );
965 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000966}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200967#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000968
Paul Bakker5121ce52009-01-03 21:22:43 +0000969/*
970 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200971 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000972static int ssl_encrypt_buf( ssl_context *ssl )
973{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200974 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
976 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
977
978 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200979 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200981#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
982 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
983 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
984 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
985 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200987#if defined(POLARSSL_SSL_PROTO_SSL3)
988 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
989 {
990 ssl_mac( &ssl->transform_out->md_ctx_enc,
991 ssl->transform_out->mac_enc,
992 ssl->out_msg, ssl->out_msglen,
993 ssl->out_ctr, ssl->out_msgtype );
994 }
995 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200996#endif
997#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200998 defined(POLARSSL_SSL_PROTO_TLS1_2)
999 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1000 {
1001 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1002 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1003 ssl->out_msg, ssl->out_msglen );
1004 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1005 ssl->out_msg + ssl->out_msglen );
1006 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1007 }
1008 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001009#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001010 {
1011 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1012 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1013 }
1014
1015 SSL_DEBUG_BUF( 4, "computed mac",
1016 ssl->out_msg + ssl->out_msglen,
1017 ssl->transform_out->maclen );
1018
1019 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001020 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001021#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001023 /*
1024 * Encrypt
1025 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001026#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001027 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1028 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001030 int ret;
1031 size_t olen = 0;
1032
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1034 "including %d bytes of padding",
1035 ssl->out_msglen, 0 ) );
1036
1037 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1038 ssl->out_msg, ssl->out_msglen );
1039
Paul Bakker45125bc2013-09-04 16:47:11 +02001040 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001041 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001042 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1043 return( ret );
1044 }
1045
1046 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1047 ssl->transform_out->iv_enc,
1048 ssl->transform_out->ivlen ) ) != 0 )
1049 {
1050 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001051 return( ret );
1052 }
1053
1054 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1055 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1056 &olen ) ) != 0 )
1057 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001058 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001059 return( ret );
1060 }
1061
1062 if( ssl->out_msglen != olen )
1063 {
1064 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1065 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001066 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001067 }
1068
1069 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001070 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001071 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001072 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001073 return( ret );
1074 }
1075
1076 if( 0 != olen )
1077 {
1078 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1079 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001080 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001081 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 }
Paul Bakker68884e32013-01-07 18:20:04 +01001083 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001084#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001085#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001086 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1087 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001088 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001089 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001090 unsigned char *enc_msg;
1091 unsigned char add_data[13];
1092 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1093
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094 enc_msglen = ssl->out_msglen;
1095
1096 memcpy( add_data, ssl->out_ctr, 8 );
1097 add_data[8] = ssl->out_msgtype;
1098 add_data[9] = ssl->major_ver;
1099 add_data[10] = ssl->minor_ver;
1100 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1101 add_data[12] = ssl->out_msglen & 0xFF;
1102
1103 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1104 add_data, 13 );
1105
Paul Bakker68884e32013-01-07 18:20:04 +01001106 /*
1107 * Generate IV
1108 */
1109 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001110 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001111 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1112 if( ret != 0 )
1113 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001114
Paul Bakker68884e32013-01-07 18:20:04 +01001115 memcpy( ssl->out_iv,
1116 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1117 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001118
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001119 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1120 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1121
Paul Bakker68884e32013-01-07 18:20:04 +01001122 /*
1123 * Fix pointer positions and message length with added IV
1124 */
1125 enc_msg = ssl->out_msg;
1126 enc_msglen = ssl->out_msglen;
1127 ssl->out_msglen += ssl->transform_out->ivlen -
1128 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001129
Paul Bakker68884e32013-01-07 18:20:04 +01001130 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1131 "including %d bytes of padding",
1132 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001133
Paul Bakker68884e32013-01-07 18:20:04 +01001134 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1135 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001136
Paul Bakker68884e32013-01-07 18:20:04 +01001137 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001138 * Encrypt
1139 */
1140 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1141 ssl->transform_out->iv_enc,
1142 ssl->transform_out->ivlen ) ) != 0 ||
1143 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1144 {
1145 return( ret );
1146 }
1147
1148 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1149 add_data, 13 ) ) != 0 )
1150 {
1151 return( ret );
1152 }
1153
1154 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1155 enc_msg, enc_msglen,
1156 enc_msg, &olen ) ) != 0 )
1157 {
1158 return( ret );
1159 }
1160 totlen = olen;
1161
1162 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1163 enc_msg + olen, &olen ) ) != 0 )
1164 {
1165 return( ret );
1166 }
1167 totlen += olen;
1168
1169 if( totlen != enc_msglen )
1170 {
1171 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1172 return( -1 );
1173 }
1174
1175 /*
1176 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001177 */
1178 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001179
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001180 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1181 enc_msg + enc_msglen, 16 ) ) != 0 )
1182 {
1183 return( ret );
1184 }
Paul Bakker68884e32013-01-07 18:20:04 +01001185
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001186 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 else
Paul Bakker68884e32013-01-07 18:20:04 +01001189#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001190#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1191 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001192 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1193 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001195 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001196 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001197 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001198
Paul Bakker48916f92012-09-16 19:57:18 +00001199 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1200 ssl->transform_out->ivlen;
1201 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 padlen = 0;
1203
1204 for( i = 0; i <= padlen; i++ )
1205 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1206
1207 ssl->out_msglen += padlen + 1;
1208
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001209 enc_msglen = ssl->out_msglen;
1210 enc_msg = ssl->out_msg;
1211
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001212#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001214 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1215 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001216 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001217 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001218 {
1219 /*
1220 * Generate IV
1221 */
Paul Bakker48916f92012-09-16 19:57:18 +00001222 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1223 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001224 if( ret != 0 )
1225 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226
Paul Bakker92be97b2013-01-02 17:30:03 +01001227 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001228 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001229
1230 /*
1231 * Fix pointer positions and message length with added IV
1232 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001233 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001234 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001235 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001237#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001241 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
1243 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001244 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Paul Bakker45125bc2013-09-04 16:47:11 +02001246 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001248 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1249 return( ret );
1250 }
1251
1252 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1253 ssl->transform_out->iv_enc,
1254 ssl->transform_out->ivlen ) ) != 0 )
1255 {
1256 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001257 return( ret );
1258 }
Paul Bakker68884e32013-01-07 18:20:04 +01001259
Paul Bakkercca5b812013-08-31 17:40:26 +02001260 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1261 enc_msg, enc_msglen, enc_msg,
1262 &olen ) ) != 0 )
1263 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001264 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001265 return( ret );
1266 }
Paul Bakker68884e32013-01-07 18:20:04 +01001267
Paul Bakkercca5b812013-08-31 17:40:26 +02001268 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001269
Paul Bakkercca5b812013-08-31 17:40:26 +02001270 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001271 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001273 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 return( ret );
1275 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001276
Paul Bakkercca5b812013-08-31 17:40:26 +02001277 if( enc_msglen != olen )
1278 {
1279 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1280 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001281 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001282 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001283
1284#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001285 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1286 {
1287 /*
1288 * Save IV in SSL3 and TLS1
1289 */
1290 memcpy( ssl->transform_out->iv_enc,
1291 ssl->transform_out->cipher_ctx_enc.iv,
1292 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001294#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001296 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001297#endif /* POLARSSL_CIPHER_MODE_CBC &&
1298 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001299 {
1300 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1301 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
Paul Bakkerca4ab492012-04-18 14:23:57 +00001304 for( i = 8; i > 0; i-- )
1305 if( ++ssl->out_ctr[i - 1] != 0 )
1306 break;
1307
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1309
1310 return( 0 );
1311}
1312
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001313#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001314
Paul Bakker5121ce52009-01-03 21:22:43 +00001315static int ssl_decrypt_buf( ssl_context *ssl )
1316{
Paul Bakker45829992013-01-03 14:52:21 +01001317 size_t i, padlen = 0, correct = 1;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001318 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
1320 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1321
Paul Bakker48916f92012-09-16 19:57:18 +00001322 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 {
1324 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001325 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001326 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 }
1328
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001329#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001330 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1331 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001332 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001333 int ret;
1334 size_t olen = 0;
1335
Paul Bakker68884e32013-01-07 18:20:04 +01001336 padlen = 0;
1337
Paul Bakker45125bc2013-09-04 16:47:11 +02001338 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001339 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001340 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1341 return( ret );
1342 }
1343
1344 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1345 ssl->transform_in->iv_dec,
1346 ssl->transform_in->ivlen ) ) != 0 )
1347 {
1348 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001349 return( ret );
1350 }
1351
1352 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1353 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1354 &olen ) ) != 0 )
1355 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001356 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001357 return( ret );
1358 }
1359
1360 if( ssl->in_msglen != olen )
1361 {
1362 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001363 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001364 }
1365
1366 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001367 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001368 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001369 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001370 return( ret );
1371 }
1372
1373 if( 0 != olen )
1374 {
1375 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001376 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 }
Paul Bakker68884e32013-01-07 18:20:04 +01001379 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001380#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001381#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001382 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1383 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001384 {
1385 unsigned char *dec_msg;
1386 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001387 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001388 unsigned char add_data[13];
1389 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1390
Paul Bakker68884e32013-01-07 18:20:04 +01001391 padlen = 0;
1392
1393 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1394 ssl->transform_in->fixed_ivlen );
1395 dec_msglen -= 16;
1396 dec_msg = ssl->in_msg;
1397 dec_msg_result = ssl->in_msg;
1398 ssl->in_msglen = dec_msglen;
1399
1400 memcpy( add_data, ssl->in_ctr, 8 );
1401 add_data[8] = ssl->in_msgtype;
1402 add_data[9] = ssl->major_ver;
1403 add_data[10] = ssl->minor_ver;
1404 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1405 add_data[12] = ssl->in_msglen & 0xFF;
1406
1407 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1408 add_data, 13 );
1409
1410 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1411 ssl->in_iv,
1412 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1413
1414 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1415 ssl->transform_in->ivlen );
1416 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1417
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001418 /*
1419 * Decrypt
1420 */
1421 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1422 ssl->transform_in->iv_dec,
1423 ssl->transform_in->ivlen ) ) != 0 ||
1424 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001425 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001426 return( ret );
1427 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001428
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001429 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1430 add_data, 13 ) ) != 0 )
1431 {
1432 return( ret );
1433 }
1434
1435 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1436 dec_msg, dec_msglen,
1437 dec_msg_result, &olen ) ) != 0 )
1438 {
1439 return( ret );
1440 }
1441 totlen = olen;
1442
1443 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1444 dec_msg_result + olen, &olen ) ) != 0 )
1445 {
1446 return( ret );
1447 }
1448 totlen += olen;
1449
1450 if( totlen != dec_msglen )
1451 {
1452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1453 return( -1 );
1454 }
1455
1456 /*
1457 * Authenticate
1458 */
1459 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1460 dec_msg + dec_msglen, 16 ) ) != 0 )
1461 {
1462 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001463 return( POLARSSL_ERR_SSL_INVALID_MAC );
1464 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001465
Paul Bakkerca4ab492012-04-18 14:23:57 +00001466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 else
Paul Bakker68884e32013-01-07 18:20:04 +01001468#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001469#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1470 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001471 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1472 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 {
Paul Bakker45829992013-01-03 14:52:21 +01001474 /*
1475 * Decrypt and check the padding
1476 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001477 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001478 unsigned char *dec_msg;
1479 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001480 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001481 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001482 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001483
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 /*
Paul Bakker45829992013-01-03 14:52:21 +01001485 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 */
Paul Bakker48916f92012-09-16 19:57:18 +00001487 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 {
1489 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001490 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001491 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 }
1493
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001494#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001495 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1496 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001497#endif
Paul Bakker45829992013-01-03 14:52:21 +01001498
1499 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1500 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1501 {
1502 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1503 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1504 return( POLARSSL_ERR_SSL_INVALID_MAC );
1505 }
1506
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001507 dec_msglen = ssl->in_msglen;
1508 dec_msg = ssl->in_msg;
1509 dec_msg_result = ssl->in_msg;
1510
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001511#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001512 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001513 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001514 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001515 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001516 {
Paul Bakker48916f92012-09-16 19:57:18 +00001517 dec_msglen -= ssl->transform_in->ivlen;
1518 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001519
Paul Bakker48916f92012-09-16 19:57:18 +00001520 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001521 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001522 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001523#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001524
Paul Bakker45125bc2013-09-04 16:47:11 +02001525 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001527 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1528 return( ret );
1529 }
1530
1531 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1532 ssl->transform_in->iv_dec,
1533 ssl->transform_in->ivlen ) ) != 0 )
1534 {
1535 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001536 return( ret );
1537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
Paul Bakkercca5b812013-08-31 17:40:26 +02001539 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1540 dec_msg, dec_msglen, dec_msg_result,
1541 &olen ) ) != 0 )
1542 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001543 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001544 return( ret );
1545 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001546
Paul Bakkercca5b812013-08-31 17:40:26 +02001547 dec_msglen -= olen;
1548 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001549 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001550 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001551 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001552 return( ret );
1553 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001554
Paul Bakkercca5b812013-08-31 17:40:26 +02001555 if( dec_msglen != olen )
1556 {
1557 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001558 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001559 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001560
1561#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001562 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1563 {
1564 /*
1565 * Save IV in SSL3 and TLS1
1566 */
1567 memcpy( ssl->transform_in->iv_dec,
1568 ssl->transform_in->cipher_ctx_dec.iv,
1569 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001570 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001571#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
1573 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001574
1575 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1576 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001577#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001578 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1579 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001580#endif
Paul Bakker45829992013-01-03 14:52:21 +01001581 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001582 correct = 0;
1583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001584
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001585#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1587 {
Paul Bakker48916f92012-09-16 19:57:18 +00001588 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001590#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1592 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001593 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001594#endif
Paul Bakker45829992013-01-03 14:52:21 +01001595 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001596 }
1597 }
1598 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001599#endif
1600#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1601 defined(POLARSSL_SSL_PROTO_TLS1_2)
1602 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 {
1604 /*
Paul Bakker45829992013-01-03 14:52:21 +01001605 * TLSv1+: always check the padding up to the first failure
1606 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001608 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001609 size_t padding_idx = ssl->in_msglen - padlen - 1;
1610
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001611 for( i = 1; i <= 256; i++ )
1612 {
1613 real_count &= ( i <= padlen );
1614 pad_count += real_count *
1615 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1616 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001617
1618 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001619
Paul Bakkerd66f0702013-01-31 16:57:45 +01001620#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001621 if( padlen > 0 && correct == 0)
1622 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001623#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001624 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001626 else
1627#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1628 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001629 {
1630 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001631 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001632 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001633 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001634 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001635#endif /* POLARSSL_CIPHER_MODE_CBC &&
1636 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001637 {
1638 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1639 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1640 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
1642 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1643 ssl->in_msg, ssl->in_msglen );
1644
1645 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001646 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001647 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001648#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1649 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1650 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1651 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1652 POLARSSL_MODE_GCM )
1653 {
1654 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001656 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1657 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001659 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001661#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001662 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1663 {
1664 ssl_mac( &ssl->transform_in->md_ctx_dec,
1665 ssl->transform_in->mac_dec,
1666 ssl->in_msg, ssl->in_msglen,
1667 ssl->in_ctr, ssl->in_msgtype );
1668 }
1669 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001670#endif /* POLARSSL_SSL_PROTO_SSL3 */
1671#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001672 defined(POLARSSL_SSL_PROTO_TLS1_2)
1673 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1674 {
1675 /*
1676 * Process MAC and always update for padlen afterwards to make
1677 * total time independent of padlen
1678 *
1679 * extra_run compensates MAC check for padlen
1680 *
1681 * Known timing attacks:
1682 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1683 *
1684 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1685 * correctly. (We round down instead of up, so -56 is the correct
1686 * value for our calculations instead of -55)
1687 */
1688 size_t j, extra_run = 0;
1689 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1690 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001691
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001692 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001693
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001694 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1695 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1696 ssl->in_msglen );
1697 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1698 ssl->in_msg + ssl->in_msglen );
1699 for( j = 0; j < extra_run; j++ )
1700 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001701
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001702 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1703 }
1704 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001705#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001706 POLARSSL_SSL_PROTO_TLS1_2 */
1707 {
1708 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1709 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1710 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001711
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001712 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1713 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1714 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001716 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001717 ssl->transform_in->maclen ) != 0 )
1718 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001719#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001720 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001721#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001722 correct = 0;
1723 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 /*
1726 * Finally check the correct flag
1727 */
1728 if( correct == 0 )
1729 return( POLARSSL_ERR_SSL_INVALID_MAC );
1730 }
1731#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
1733 if( ssl->in_msglen == 0 )
1734 {
1735 ssl->nb_zero++;
1736
1737 /*
1738 * Three or more empty messages may be a DoS attack
1739 * (excessive CPU consumption).
1740 */
1741 if( ssl->nb_zero > 3 )
1742 {
1743 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1744 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001745 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 }
1747 }
1748 else
1749 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001750
Paul Bakker23986e52011-04-24 08:57:21 +00001751 for( i = 8; i > 0; i-- )
1752 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001753 break;
1754
1755 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1756
1757 return( 0 );
1758}
1759
Paul Bakker2770fbd2012-07-03 13:30:23 +00001760#if defined(POLARSSL_ZLIB_SUPPORT)
1761/*
1762 * Compression/decompression functions
1763 */
1764static int ssl_compress_buf( ssl_context *ssl )
1765{
1766 int ret;
1767 unsigned char *msg_post = ssl->out_msg;
1768 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001769 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001770
1771 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1772
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001773 if( len_pre == 0 )
1774 return( 0 );
1775
Paul Bakker2770fbd2012-07-03 13:30:23 +00001776 memcpy( msg_pre, ssl->out_msg, len_pre );
1777
1778 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1779 ssl->out_msglen ) );
1780
1781 SSL_DEBUG_BUF( 4, "before compression: output payload",
1782 ssl->out_msg, ssl->out_msglen );
1783
Paul Bakker48916f92012-09-16 19:57:18 +00001784 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1785 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1786 ssl->transform_out->ctx_deflate.next_out = msg_post;
1787 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001788
Paul Bakker48916f92012-09-16 19:57:18 +00001789 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001790 if( ret != Z_OK )
1791 {
1792 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1793 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1794 }
1795
Paul Bakker48916f92012-09-16 19:57:18 +00001796 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001797
Paul Bakker2770fbd2012-07-03 13:30:23 +00001798 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1799 ssl->out_msglen ) );
1800
1801 SSL_DEBUG_BUF( 4, "after compression: output payload",
1802 ssl->out_msg, ssl->out_msglen );
1803
1804 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1805
1806 return( 0 );
1807}
1808
1809static int ssl_decompress_buf( ssl_context *ssl )
1810{
1811 int ret;
1812 unsigned char *msg_post = ssl->in_msg;
1813 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001814 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001815
1816 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1817
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001818 if( len_pre == 0 )
1819 return( 0 );
1820
Paul Bakker2770fbd2012-07-03 13:30:23 +00001821 memcpy( msg_pre, ssl->in_msg, len_pre );
1822
1823 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1824 ssl->in_msglen ) );
1825
1826 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1827 ssl->in_msg, ssl->in_msglen );
1828
Paul Bakker48916f92012-09-16 19:57:18 +00001829 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1830 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1831 ssl->transform_in->ctx_inflate.next_out = msg_post;
1832 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001833
Paul Bakker48916f92012-09-16 19:57:18 +00001834 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001835 if( ret != Z_OK )
1836 {
1837 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1838 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1839 }
1840
Paul Bakker48916f92012-09-16 19:57:18 +00001841 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001842
Paul Bakker2770fbd2012-07-03 13:30:23 +00001843 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1844 ssl->in_msglen ) );
1845
1846 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1847 ssl->in_msg, ssl->in_msglen );
1848
1849 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1850
1851 return( 0 );
1852}
1853#endif /* POLARSSL_ZLIB_SUPPORT */
1854
Paul Bakker5121ce52009-01-03 21:22:43 +00001855/*
1856 * Fill the input message buffer
1857 */
Paul Bakker23986e52011-04-24 08:57:21 +00001858int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001859{
Paul Bakker23986e52011-04-24 08:57:21 +00001860 int ret;
1861 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
1863 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1864
1865 while( ssl->in_left < nb_want )
1866 {
1867 len = nb_want - ssl->in_left;
1868 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1869
1870 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1871 ssl->in_left, nb_want ) );
1872 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1873
Paul Bakker831a7552011-05-18 13:32:51 +00001874 if( ret == 0 )
1875 return( POLARSSL_ERR_SSL_CONN_EOF );
1876
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 if( ret < 0 )
1878 return( ret );
1879
1880 ssl->in_left += ret;
1881 }
1882
1883 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1884
1885 return( 0 );
1886}
1887
1888/*
1889 * Flush any data not yet written
1890 */
1891int ssl_flush_output( ssl_context *ssl )
1892{
1893 int ret;
1894 unsigned char *buf;
1895
1896 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1897
1898 while( ssl->out_left > 0 )
1899 {
1900 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1901 5 + ssl->out_msglen, ssl->out_left ) );
1902
Paul Bakker5bd42292012-12-19 14:40:42 +01001903 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001905
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1907
1908 if( ret <= 0 )
1909 return( ret );
1910
1911 ssl->out_left -= ret;
1912 }
1913
1914 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1915
1916 return( 0 );
1917}
1918
1919/*
1920 * Record layer functions
1921 */
1922int ssl_write_record( ssl_context *ssl )
1923{
Paul Bakker05ef8352012-05-08 09:17:57 +00001924 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001925 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001926
1927 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1928
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1930 {
1931 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1932 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1933 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1934
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001935 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1936 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 }
1938
Paul Bakker2770fbd2012-07-03 13:30:23 +00001939#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001940 if( ssl->transform_out != NULL &&
1941 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001942 {
1943 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1944 {
1945 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1946 return( ret );
1947 }
1948
1949 len = ssl->out_msglen;
1950 }
1951#endif /*POLARSSL_ZLIB_SUPPORT */
1952
Paul Bakker05ef8352012-05-08 09:17:57 +00001953#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1954 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001956 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
Paul Bakker05ef8352012-05-08 09:17:57 +00001958 ret = ssl_hw_record_write( ssl );
1959 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1960 {
1961 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1962 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1963 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001964
1965 if( ret == 0 )
1966 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001967 }
1968#endif
1969 if( !done )
1970 {
1971 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1972 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1973 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1975 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001976
Paul Bakker48916f92012-09-16 19:57:18 +00001977 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001978 {
1979 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1980 {
1981 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1982 return( ret );
1983 }
1984
1985 len = ssl->out_msglen;
1986 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1987 ssl->out_hdr[4] = (unsigned char)( len );
1988 }
1989
1990 ssl->out_left = 5 + ssl->out_msglen;
1991
1992 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1993 "version = [%d:%d], msglen = %d",
1994 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1995 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1996
1997 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001998 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 }
2000
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2002 {
2003 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2004 return( ret );
2005 }
2006
2007 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2008
2009 return( 0 );
2010}
2011
2012int ssl_read_record( ssl_context *ssl )
2013{
Paul Bakker05ef8352012-05-08 09:17:57 +00002014 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002015
2016 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2017
Paul Bakker68884e32013-01-07 18:20:04 +01002018 SSL_DEBUG_BUF( 4, "input record from network",
2019 ssl->in_hdr, 5 + ssl->in_msglen );
2020
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 if( ssl->in_hslen != 0 &&
2022 ssl->in_hslen < ssl->in_msglen )
2023 {
2024 /*
2025 * Get next Handshake message in the current record
2026 */
2027 ssl->in_msglen -= ssl->in_hslen;
2028
Paul Bakker8934a982011-08-05 11:11:53 +00002029 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002030 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031
2032 ssl->in_hslen = 4;
2033 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2034
2035 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2036 " %d, type = %d, hslen = %d",
2037 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2038
2039 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2040 {
2041 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002042 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 }
2044
2045 if( ssl->in_msglen < ssl->in_hslen )
2046 {
2047 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002048 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 }
2050
Paul Bakker48916f92012-09-16 19:57:18 +00002051 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002052
2053 return( 0 );
2054 }
2055
2056 ssl->in_hslen = 0;
2057
2058 /*
2059 * Read the record header and validate it
2060 */
2061 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2062 {
2063 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2064 return( ret );
2065 }
2066
2067 ssl->in_msgtype = ssl->in_hdr[0];
2068 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2069
2070 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2071 "version = [%d:%d], msglen = %d",
2072 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2073 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2074
2075 if( ssl->in_hdr[1] != ssl->major_ver )
2076 {
2077 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002078 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 }
2080
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002081 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 {
2083 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002084 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 }
2086
2087 /*
2088 * Make sure the message length is acceptable
2089 */
Paul Bakker48916f92012-09-16 19:57:18 +00002090 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 {
2092 if( ssl->in_msglen < 1 ||
2093 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2094 {
2095 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002096 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
2098 }
2099 else
2100 {
Paul Bakker48916f92012-09-16 19:57:18 +00002101 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 {
2103 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002104 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
2106
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002107#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002109 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
2111 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002112 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002116#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2117 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 /*
2119 * TLS encrypted messages can have up to 256 bytes of padding
2120 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002121 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002122 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 {
2124 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002125 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
2129
2130 /*
2131 * Read and optionally decrypt the message contents
2132 */
2133 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2134 {
2135 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2136 return( ret );
2137 }
2138
2139 SSL_DEBUG_BUF( 4, "input record from network",
2140 ssl->in_hdr, 5 + ssl->in_msglen );
2141
Paul Bakker05ef8352012-05-08 09:17:57 +00002142#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2143 if( ssl_hw_record_read != NULL)
2144 {
2145 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2146
2147 ret = ssl_hw_record_read( ssl );
2148 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2149 {
2150 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2151 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2152 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002153
2154 if( ret == 0 )
2155 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002156 }
2157#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002158 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 {
2160 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2161 {
Paul Bakker40865c82013-01-31 17:13:13 +01002162#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2163 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2164 {
2165 ssl_send_alert_message( ssl,
2166 SSL_ALERT_LEVEL_FATAL,
2167 SSL_ALERT_MSG_BAD_RECORD_MAC );
2168 }
2169#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2171 return( ret );
2172 }
2173
2174 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2175 ssl->in_msg, ssl->in_msglen );
2176
2177 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2178 {
2179 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002180 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 }
2182 }
2183
Paul Bakker2770fbd2012-07-03 13:30:23 +00002184#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002185 if( ssl->transform_in != NULL &&
2186 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002187 {
2188 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2189 {
2190 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2191 return( ret );
2192 }
2193
2194 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2195 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2196 }
2197#endif /* POLARSSL_ZLIB_SUPPORT */
2198
Paul Bakker0a925182012-04-16 06:46:41 +00002199 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2200 ssl->in_msgtype != SSL_MSG_ALERT &&
2201 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2202 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2203 {
2204 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2205
Paul Bakker48916f92012-09-16 19:57:18 +00002206 if( ( ret = ssl_send_alert_message( ssl,
2207 SSL_ALERT_LEVEL_FATAL,
2208 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002209 {
2210 return( ret );
2211 }
2212
2213 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2214 }
2215
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2217 {
2218 ssl->in_hslen = 4;
2219 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2220
2221 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2222 " %d, type = %d, hslen = %d",
2223 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2224
2225 /*
2226 * Additional checks to validate the handshake header
2227 */
2228 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2229 {
2230 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002231 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 }
2233
2234 if( ssl->in_msglen < ssl->in_hslen )
2235 {
2236 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002237 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239
Paul Bakker48916f92012-09-16 19:57:18 +00002240 if( ssl->state != SSL_HANDSHAKE_OVER )
2241 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 }
2243
2244 if( ssl->in_msgtype == SSL_MSG_ALERT )
2245 {
2246 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2247 ssl->in_msg[0], ssl->in_msg[1] ) );
2248
2249 /*
2250 * Ignore non-fatal alerts, except close_notify
2251 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002252 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002254 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2255 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002256 /**
2257 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2258 * error identifier.
2259 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002260 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002263 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2264 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 {
2266 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002267 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 }
2269 }
2270
2271 ssl->in_left = 0;
2272
2273 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2274
2275 return( 0 );
2276}
2277
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002278int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2279{
2280 int ret;
2281
2282 if( ( ret = ssl_send_alert_message( ssl,
2283 SSL_ALERT_LEVEL_FATAL,
2284 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2285 {
2286 return( ret );
2287 }
2288
2289 return( 0 );
2290}
2291
Paul Bakker0a925182012-04-16 06:46:41 +00002292int ssl_send_alert_message( ssl_context *ssl,
2293 unsigned char level,
2294 unsigned char message )
2295{
2296 int ret;
2297
2298 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2299
2300 ssl->out_msgtype = SSL_MSG_ALERT;
2301 ssl->out_msglen = 2;
2302 ssl->out_msg[0] = level;
2303 ssl->out_msg[1] = message;
2304
2305 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2306 {
2307 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2308 return( ret );
2309 }
2310
2311 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2312
2313 return( 0 );
2314}
2315
Paul Bakker5121ce52009-01-03 21:22:43 +00002316/*
2317 * Handshake functions
2318 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002319#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2320 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2321 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2322 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2323 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2324 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2325 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002326int ssl_write_certificate( ssl_context *ssl )
2327{
Paul Bakkered27a042013-04-18 22:46:23 +02002328 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002329 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
2331 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2332
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002333 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002334 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2335 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002336 {
2337 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2338 ssl->state++;
2339 return( 0 );
2340 }
2341
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002342 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002343 return( ret );
2344}
2345
2346int ssl_parse_certificate( ssl_context *ssl )
2347{
2348 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2349 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2350
2351 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2352
2353 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002354 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2355 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002356 {
2357 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2358 ssl->state++;
2359 return( 0 );
2360 }
2361
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002362 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002363 return( ret );
2364}
2365#else
2366int ssl_write_certificate( ssl_context *ssl )
2367{
2368 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2369 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002370 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002371 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2372
2373 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2374
2375 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002376 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2377 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002378 {
2379 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2380 ssl->state++;
2381 return( 0 );
2382 }
2383
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 if( ssl->endpoint == SSL_IS_CLIENT )
2385 {
2386 if( ssl->client_auth == 0 )
2387 {
2388 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2389 ssl->state++;
2390 return( 0 );
2391 }
2392
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002393#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 /*
2395 * If using SSLv3 and got no cert, send an Alert message
2396 * (otherwise an empty Certificate message will be sent).
2397 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002398 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2400 {
2401 ssl->out_msglen = 2;
2402 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002403 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2404 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
2406 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2407 goto write_msg;
2408 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002409#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 }
2411 else /* SSL_IS_SERVER */
2412 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002413 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 {
2415 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002416 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 }
2418 }
2419
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002420 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
2422 /*
2423 * 0 . 0 handshake type
2424 * 1 . 3 handshake length
2425 * 4 . 6 length of all certs
2426 * 7 . 9 length of cert. 1
2427 * 10 . n-1 peer certificate
2428 * n . n+2 length of cert. 2
2429 * n+3 . ... upper level cert, etc.
2430 */
2431 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002432 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
Paul Bakker29087132010-03-21 21:03:34 +00002434 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 {
2436 n = crt->raw.len;
2437 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
2438 {
2439 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2440 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002441 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 }
2443
2444 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2445 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2446 ssl->out_msg[i + 2] = (unsigned char)( n );
2447
2448 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2449 i += n; crt = crt->next;
2450 }
2451
2452 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2453 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2454 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2455
2456 ssl->out_msglen = i;
2457 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2458 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2459
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002460#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002461write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002462#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
2464 ssl->state++;
2465
2466 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2467 {
2468 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2469 return( ret );
2470 }
2471
2472 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2473
Paul Bakkered27a042013-04-18 22:46:23 +02002474 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002475}
2476
2477int ssl_parse_certificate( ssl_context *ssl )
2478{
Paul Bakkered27a042013-04-18 22:46:23 +02002479 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002480 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002481 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
2483 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2484
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002485 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002486 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2487 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002488 {
2489 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2490 ssl->state++;
2491 return( 0 );
2492 }
2493
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002495 ( ssl->authmode == SSL_VERIFY_NONE ||
2496 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002498 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2500 ssl->state++;
2501 return( 0 );
2502 }
2503
2504 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2505 {
2506 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2507 return( ret );
2508 }
2509
2510 ssl->state++;
2511
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002512#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 /*
2514 * Check if the client sent an empty certificate
2515 */
2516 if( ssl->endpoint == SSL_IS_SERVER &&
2517 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2518 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002519 if( ssl->in_msglen == 2 &&
2520 ssl->in_msgtype == SSL_MSG_ALERT &&
2521 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2522 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 {
2524 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2525
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002526 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2528 return( 0 );
2529 else
Paul Bakker40e46942009-01-03 21:51:57 +00002530 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 }
2532 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2536 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 if( ssl->endpoint == SSL_IS_SERVER &&
2538 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2539 {
2540 if( ssl->in_hslen == 7 &&
2541 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2542 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2543 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2544 {
2545 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2546
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002547 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002549 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 else
2551 return( 0 );
2552 }
2553 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002554#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2555 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
2557 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2558 {
2559 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002560 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 }
2562
2563 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2564 {
2565 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002566 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 }
2568
2569 /*
2570 * Same message structure as in ssl_write_certificate()
2571 */
2572 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2573
2574 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2575 {
2576 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002577 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 }
2579
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002580 /* In case we tried to reuse a session but it failed */
2581 if( ssl->session_negotiate->peer_cert != NULL )
2582 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002583 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002584 polarssl_free( ssl->session_negotiate->peer_cert );
2585 }
2586
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002587 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2588 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 {
2590 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002591 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002592 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 }
2594
Paul Bakkerb6b09562013-09-18 14:17:41 +02002595 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
2597 i = 7;
2598
2599 while( i < ssl->in_hslen )
2600 {
2601 if( ssl->in_msg[i] != 0 )
2602 {
2603 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002604 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 }
2606
2607 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2608 | (unsigned int) ssl->in_msg[i + 2];
2609 i += 3;
2610
2611 if( n < 128 || i + n > ssl->in_hslen )
2612 {
2613 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002614 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 }
2616
Paul Bakkerddf26b42013-09-18 13:46:23 +02002617 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2618 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 if( ret != 0 )
2620 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002621 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 return( ret );
2623 }
2624
2625 i += n;
2626 }
2627
Paul Bakker48916f92012-09-16 19:57:18 +00002628 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
2630 if( ssl->authmode != SSL_VERIFY_NONE )
2631 {
2632 if( ssl->ca_chain == NULL )
2633 {
2634 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002635 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 }
2637
Paul Bakkerddf26b42013-09-18 13:46:23 +02002638 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2639 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2640 &ssl->session_negotiate->verify_result,
2641 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
2643 if( ret != 0 )
2644 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2645
2646 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2647 ret = 0;
2648 }
2649
2650 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2651
2652 return( ret );
2653}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002654#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2655 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2656 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2657 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2658 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2659 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2660 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
2662int ssl_write_change_cipher_spec( ssl_context *ssl )
2663{
2664 int ret;
2665
2666 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2667
2668 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2669 ssl->out_msglen = 1;
2670 ssl->out_msg[0] = 1;
2671
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 ssl->state++;
2673
2674 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2675 {
2676 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2677 return( ret );
2678 }
2679
2680 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2681
2682 return( 0 );
2683}
2684
2685int ssl_parse_change_cipher_spec( ssl_context *ssl )
2686{
2687 int ret;
2688
2689 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2690
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2692 {
2693 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2694 return( ret );
2695 }
2696
2697 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2698 {
2699 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002700 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 }
2702
2703 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2704 {
2705 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002706 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 }
2708
2709 ssl->state++;
2710
2711 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2712
2713 return( 0 );
2714}
2715
Paul Bakker41c83d32013-03-20 14:39:14 +01002716void ssl_optimize_checksum( ssl_context *ssl,
2717 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002718{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002719 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002720
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002721#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2722 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002723 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002724 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002725 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002726#endif
2727#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2728#if defined(POLARSSL_SHA512_C)
2729 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2730 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2731 else
2732#endif
2733#if defined(POLARSSL_SHA256_C)
2734 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002735 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002736 else
2737#endif
2738#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2739 /* Should never happen */
2740 return;
Paul Bakker380da532012-04-18 16:10:25 +00002741}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002742
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002743static void ssl_update_checksum_start( ssl_context *ssl,
2744 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002745{
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 Bakker48916f92012-09-16 19:57:18 +00002748 md5_update( &ssl->handshake->fin_md5 , buf, len );
2749 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002750#endif
2751#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2752#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002753 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002754#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002755#if defined(POLARSSL_SHA512_C)
2756 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002757#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002758#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002759}
2760
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002761#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2762 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002763static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2764 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002765{
Paul Bakker48916f92012-09-16 19:57:18 +00002766 md5_update( &ssl->handshake->fin_md5 , buf, len );
2767 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002768}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002769#endif
Paul Bakker380da532012-04-18 16:10:25 +00002770
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002771#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2772#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002773static void ssl_update_checksum_sha256( ssl_context *ssl,
2774 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002775{
Paul Bakker9e36f042013-06-30 14:34:05 +02002776 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002777}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002778#endif
Paul Bakker380da532012-04-18 16:10:25 +00002779
Paul Bakker9e36f042013-06-30 14:34:05 +02002780#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002781static void ssl_update_checksum_sha384( ssl_context *ssl,
2782 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002783{
Paul Bakker9e36f042013-06-30 14:34:05 +02002784 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002785}
Paul Bakker769075d2012-11-24 11:26:46 +01002786#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002787#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002788
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002789#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002790static void ssl_calc_finished_ssl(
2791 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002792{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002793 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002794 md5_context md5;
2795 sha1_context sha1;
2796
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 unsigned char padbuf[48];
2798 unsigned char md5sum[16];
2799 unsigned char sha1sum[20];
2800
Paul Bakker48916f92012-09-16 19:57:18 +00002801 ssl_session *session = ssl->session_negotiate;
2802 if( !session )
2803 session = ssl->session;
2804
Paul Bakker1ef83d62012-04-11 12:09:53 +00002805 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2806
Paul Bakker48916f92012-09-16 19:57:18 +00002807 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2808 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
2810 /*
2811 * SSLv3:
2812 * hash =
2813 * MD5( master + pad2 +
2814 * MD5( handshake + sender + master + pad1 ) )
2815 * + SHA1( master + pad2 +
2816 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 */
2818
Paul Bakker90995b52013-06-24 19:20:35 +02002819#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002821 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002822#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
Paul Bakker90995b52013-06-24 19:20:35 +02002824#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002826 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002827#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
Paul Bakker3c2122f2013-06-24 19:03:14 +02002829 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2830 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Paul Bakker1ef83d62012-04-11 12:09:53 +00002832 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Paul Bakker3c2122f2013-06-24 19:03:14 +02002834 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002835 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002836 md5_update( &md5, padbuf, 48 );
2837 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
Paul Bakker3c2122f2013-06-24 19:03:14 +02002839 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002840 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002841 sha1_update( &sha1, padbuf, 40 );
2842 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Paul Bakker1ef83d62012-04-11 12:09:53 +00002844 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Paul Bakker1ef83d62012-04-11 12:09:53 +00002846 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002847 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002848 md5_update( &md5, padbuf, 48 );
2849 md5_update( &md5, md5sum, 16 );
2850 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Paul Bakker1ef83d62012-04-11 12:09:53 +00002852 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002853 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002854 sha1_update( &sha1, padbuf , 40 );
2855 sha1_update( &sha1, sha1sum, 20 );
2856 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002857
Paul Bakker1ef83d62012-04-11 12:09:53 +00002858 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Paul Bakker1ef83d62012-04-11 12:09:53 +00002860 memset( &md5, 0, sizeof( md5_context ) );
2861 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
2863 memset( padbuf, 0, sizeof( padbuf ) );
2864 memset( md5sum, 0, sizeof( md5sum ) );
2865 memset( sha1sum, 0, sizeof( sha1sum ) );
2866
2867 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2868}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002869#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002871#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002872static void ssl_calc_finished_tls(
2873 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002874{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002875 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002876 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002877 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002878 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
Paul Bakker48916f92012-09-16 19:57:18 +00002881 ssl_session *session = ssl->session_negotiate;
2882 if( !session )
2883 session = ssl->session;
2884
Paul Bakker1ef83d62012-04-11 12:09:53 +00002885 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
Paul Bakker48916f92012-09-16 19:57:18 +00002887 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2888 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002889
Paul Bakker1ef83d62012-04-11 12:09:53 +00002890 /*
2891 * TLSv1:
2892 * hash = PRF( master, finished_label,
2893 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2894 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002895
Paul Bakker90995b52013-06-24 19:20:35 +02002896#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002897 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2898 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002899#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900
Paul Bakker90995b52013-06-24 19:20:35 +02002901#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2903 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002904#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002905
2906 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002907 ? "client finished"
2908 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909
2910 md5_finish( &md5, padbuf );
2911 sha1_finish( &sha1, padbuf + 16 );
2912
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002913 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002914 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915
2916 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2917
2918 memset( &md5, 0, sizeof( md5_context ) );
2919 memset( &sha1, 0, sizeof( sha1_context ) );
2920
2921 memset( padbuf, 0, sizeof( padbuf ) );
2922
2923 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2924}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002925#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002926
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002927#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2928#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002929static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930 ssl_context *ssl, unsigned char *buf, int from )
2931{
2932 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002933 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002934 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002935 unsigned char padbuf[32];
2936
Paul Bakker48916f92012-09-16 19:57:18 +00002937 ssl_session *session = ssl->session_negotiate;
2938 if( !session )
2939 session = ssl->session;
2940
Paul Bakker380da532012-04-18 16:10:25 +00002941 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002942
Paul Bakker9e36f042013-06-30 14:34:05 +02002943 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002944
2945 /*
2946 * TLSv1.2:
2947 * hash = PRF( master, finished_label,
2948 * Hash( handshake ) )[0.11]
2949 */
2950
Paul Bakker9e36f042013-06-30 14:34:05 +02002951#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002953 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002954#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955
2956 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002957 ? "client finished"
2958 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002959
Paul Bakker9e36f042013-06-30 14:34:05 +02002960 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002961
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002962 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002963 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964
2965 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2966
Paul Bakker9e36f042013-06-30 14:34:05 +02002967 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968
2969 memset( padbuf, 0, sizeof( padbuf ) );
2970
2971 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2972}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002973#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974
Paul Bakker9e36f042013-06-30 14:34:05 +02002975#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002976static void ssl_calc_finished_tls_sha384(
2977 ssl_context *ssl, unsigned char *buf, int from )
2978{
2979 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002980 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002981 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002982 unsigned char padbuf[48];
2983
Paul Bakker48916f92012-09-16 19:57:18 +00002984 ssl_session *session = ssl->session_negotiate;
2985 if( !session )
2986 session = ssl->session;
2987
Paul Bakker380da532012-04-18 16:10:25 +00002988 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002989
Paul Bakker9e36f042013-06-30 14:34:05 +02002990 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002991
2992 /*
2993 * TLSv1.2:
2994 * hash = PRF( master, finished_label,
2995 * Hash( handshake ) )[0.11]
2996 */
2997
Paul Bakker9e36f042013-06-30 14:34:05 +02002998#if !defined(POLARSSL_SHA512_ALT)
2999 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3000 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003001#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003002
3003 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003004 ? "client finished"
3005 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003006
Paul Bakker9e36f042013-06-30 14:34:05 +02003007 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003008
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003009 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003010 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003011
3012 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3013
Paul Bakker9e36f042013-06-30 14:34:05 +02003014 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003015
3016 memset( padbuf, 0, sizeof( padbuf ) );
3017
3018 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3019}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003020#endif /* POLARSSL_SHA512_C */
3021#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003022
Paul Bakker48916f92012-09-16 19:57:18 +00003023void ssl_handshake_wrapup( ssl_context *ssl )
3024{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003025 int resume = ssl->handshake->resume;
3026
Paul Bakker48916f92012-09-16 19:57:18 +00003027 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3028
3029 /*
3030 * Free our handshake params
3031 */
3032 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003033 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003034 ssl->handshake = NULL;
3035
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003036 if( ssl->renegotiation == SSL_RENEGOTIATION )
3037 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3038
Paul Bakker48916f92012-09-16 19:57:18 +00003039 /*
3040 * Switch in our now active transform context
3041 */
3042 if( ssl->transform )
3043 {
3044 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003045 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003046 }
3047 ssl->transform = ssl->transform_negotiate;
3048 ssl->transform_negotiate = NULL;
3049
Paul Bakker0a597072012-09-25 21:55:46 +00003050 if( ssl->session )
3051 {
3052 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003053 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003054 }
3055 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003056 ssl->session_negotiate = NULL;
3057
Paul Bakker0a597072012-09-25 21:55:46 +00003058 /*
3059 * Add cache entry
3060 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003061 if( ssl->f_set_cache != NULL &&
3062 ssl->session->length != 0 &&
3063 resume == 0 )
3064 {
Paul Bakker0a597072012-09-25 21:55:46 +00003065 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3066 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003067 }
Paul Bakker0a597072012-09-25 21:55:46 +00003068
Paul Bakker48916f92012-09-16 19:57:18 +00003069 ssl->state++;
3070
3071 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3072}
3073
Paul Bakker1ef83d62012-04-11 12:09:53 +00003074int ssl_write_finished( ssl_context *ssl )
3075{
3076 int ret, hash_len;
3077
3078 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3079
Paul Bakker92be97b2013-01-02 17:30:03 +01003080 /*
3081 * Set the out_msg pointer to the correct location based on IV length
3082 */
3083 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3084 {
3085 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3086 ssl->transform_negotiate->fixed_ivlen;
3087 }
3088 else
3089 ssl->out_msg = ssl->out_iv;
3090
Paul Bakker48916f92012-09-16 19:57:18 +00003091 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003092
3093 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3095
Paul Bakker48916f92012-09-16 19:57:18 +00003096 ssl->verify_data_len = hash_len;
3097 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3098
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 ssl->out_msglen = 4 + hash_len;
3100 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3101 ssl->out_msg[0] = SSL_HS_FINISHED;
3102
3103 /*
3104 * In case of session resuming, invert the client and server
3105 * ChangeCipherSpec messages order.
3106 */
Paul Bakker0a597072012-09-25 21:55:46 +00003107 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003108 {
3109 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003110 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003111 else
3112 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3113 }
3114 else
3115 ssl->state++;
3116
Paul Bakker48916f92012-09-16 19:57:18 +00003117 /*
3118 * Switch to our negotiated transform and session parameters for outbound data.
3119 */
3120 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3121 ssl->transform_out = ssl->transform_negotiate;
3122 ssl->session_out = ssl->session_negotiate;
3123 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003124
Paul Bakker07eb38b2012-12-19 14:42:06 +01003125#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3126 if( ssl_hw_record_activate != NULL)
3127 {
3128 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3129 {
3130 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3131 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3132 }
3133 }
3134#endif
3135
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3137 {
3138 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3139 return( ret );
3140 }
3141
3142 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3143
3144 return( 0 );
3145}
3146
3147int ssl_parse_finished( ssl_context *ssl )
3148{
Paul Bakker23986e52011-04-24 08:57:21 +00003149 int ret;
3150 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 unsigned char buf[36];
3152
3153 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3154
Paul Bakker48916f92012-09-16 19:57:18 +00003155 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003156
Paul Bakker48916f92012-09-16 19:57:18 +00003157 /*
3158 * Switch to our negotiated transform and session parameters for inbound data.
3159 */
3160 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3161 ssl->transform_in = ssl->transform_negotiate;
3162 ssl->session_in = ssl->session_negotiate;
3163 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
Paul Bakker92be97b2013-01-02 17:30:03 +01003165 /*
3166 * Set the in_msg pointer to the correct location based on IV length
3167 */
3168 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3169 {
3170 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3171 ssl->transform_negotiate->fixed_ivlen;
3172 }
3173 else
3174 ssl->in_msg = ssl->in_iv;
3175
Paul Bakker07eb38b2012-12-19 14:42:06 +01003176#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3177 if( ssl_hw_record_activate != NULL)
3178 {
3179 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3180 {
3181 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3182 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3183 }
3184 }
3185#endif
3186
Paul Bakker5121ce52009-01-03 21:22:43 +00003187 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3188 {
3189 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3190 return( ret );
3191 }
3192
3193 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3194 {
3195 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003196 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003197 }
3198
Paul Bakker1ef83d62012-04-11 12:09:53 +00003199 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3201
3202 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3203 ssl->in_hslen != 4 + hash_len )
3204 {
3205 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003206 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 }
3208
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003209 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 {
3211 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003212 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 }
3214
Paul Bakker48916f92012-09-16 19:57:18 +00003215 ssl->verify_data_len = hash_len;
3216 memcpy( ssl->peer_verify_data, buf, hash_len );
3217
Paul Bakker0a597072012-09-25 21:55:46 +00003218 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 {
3220 if( ssl->endpoint == SSL_IS_CLIENT )
3221 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3222
3223 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003224 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 }
3226 else
3227 ssl->state++;
3228
3229 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3230
3231 return( 0 );
3232}
3233
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003234static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003235{
3236 if( ssl->transform_negotiate )
3237 ssl_transform_free( ssl->transform_negotiate );
3238 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003239 {
3240 ssl->transform_negotiate =
3241 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3242 }
Paul Bakker48916f92012-09-16 19:57:18 +00003243
3244 if( ssl->session_negotiate )
3245 ssl_session_free( ssl->session_negotiate );
3246 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003247 {
3248 ssl->session_negotiate =
3249 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3250 }
Paul Bakker48916f92012-09-16 19:57:18 +00003251
3252 if( ssl->handshake )
3253 ssl_handshake_free( ssl->handshake );
3254 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003255 {
3256 ssl->handshake = (ssl_handshake_params *)
3257 polarssl_malloc( sizeof(ssl_handshake_params) );
3258 }
Paul Bakker48916f92012-09-16 19:57:18 +00003259
3260 if( ssl->handshake == NULL ||
3261 ssl->transform_negotiate == NULL ||
3262 ssl->session_negotiate == NULL )
3263 {
3264 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3265 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3266 }
3267
3268 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3269 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3270 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3271
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003272#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3273 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003274 md5_starts( &ssl->handshake->fin_md5 );
3275 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003276#endif
3277#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3278#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003279 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003280#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003281#if defined(POLARSSL_SHA512_C)
3282 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003283#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003284#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003285
3286 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003287 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003288
Paul Bakker61d113b2013-07-04 11:51:43 +02003289#if defined(POLARSSL_ECDH_C)
3290 ecdh_init( &ssl->handshake->ecdh_ctx );
3291#endif
3292
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003293#if defined(POLARSSL_X509_CRT_PARSE_C)
3294 ssl->handshake->key_cert = ssl->key_cert;
3295#endif
3296
Paul Bakker48916f92012-09-16 19:57:18 +00003297 return( 0 );
3298}
3299
Paul Bakker5121ce52009-01-03 21:22:43 +00003300/*
3301 * Initialize an SSL context
3302 */
3303int ssl_init( ssl_context *ssl )
3304{
Paul Bakker48916f92012-09-16 19:57:18 +00003305 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 int len = SSL_BUFFER_LEN;
3307
3308 memset( ssl, 0, sizeof( ssl_context ) );
3309
Paul Bakker62f2dee2012-09-28 07:31:51 +00003310 /*
3311 * Sane defaults
3312 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003313 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3314 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3315 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3316 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003317
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003318 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003319
Paul Bakker62f2dee2012-09-28 07:31:51 +00003320#if defined(POLARSSL_DHM_C)
3321 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3322 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3323 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3324 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3325 {
3326 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3327 return( ret );
3328 }
3329#endif
3330
3331 /*
3332 * Prepare base structures
3333 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003334 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003336 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 ssl->in_msg = ssl->in_ctr + 13;
3338
3339 if( ssl->in_ctr == NULL )
3340 {
3341 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003342 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 }
3344
Paul Bakker6e339b52013-07-03 13:37:05 +02003345 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003347 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003348 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349
3350 if( ssl->out_ctr == NULL )
3351 {
3352 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003353 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003354 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 }
3356
3357 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3358 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3359
Paul Bakker606b4ba2013-08-14 16:52:14 +02003360#if defined(POLARSSL_SSL_SESSION_TICKETS)
3361 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3362#endif
3363
Paul Bakker48916f92012-09-16 19:57:18 +00003364 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3365 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366
3367 return( 0 );
3368}
3369
3370/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003371 * Reset an initialized and used SSL context for re-use while retaining
3372 * all application-set variables, function pointers and data.
3373 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003374int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003375{
Paul Bakker48916f92012-09-16 19:57:18 +00003376 int ret;
3377
Paul Bakker7eb013f2011-10-06 12:37:39 +00003378 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003379 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3380 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3381
3382 ssl->verify_data_len = 0;
3383 memset( ssl->own_verify_data, 0, 36 );
3384 memset( ssl->peer_verify_data, 0, 36 );
3385
Paul Bakker7eb013f2011-10-06 12:37:39 +00003386 ssl->in_offt = NULL;
3387
Paul Bakker92be97b2013-01-02 17:30:03 +01003388 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003389 ssl->in_msgtype = 0;
3390 ssl->in_msglen = 0;
3391 ssl->in_left = 0;
3392
3393 ssl->in_hslen = 0;
3394 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003395 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003396
Paul Bakker92be97b2013-01-02 17:30:03 +01003397 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003398 ssl->out_msgtype = 0;
3399 ssl->out_msglen = 0;
3400 ssl->out_left = 0;
3401
Paul Bakker48916f92012-09-16 19:57:18 +00003402 ssl->transform_in = NULL;
3403 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003404
3405 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3406 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003407
3408#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3409 if( ssl_hw_record_reset != NULL)
3410 {
3411 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003412 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003413 {
3414 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3415 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3416 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003417 }
3418#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003419
Paul Bakker48916f92012-09-16 19:57:18 +00003420 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003421 {
Paul Bakker48916f92012-09-16 19:57:18 +00003422 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003423 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003424 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003425 }
Paul Bakker48916f92012-09-16 19:57:18 +00003426
Paul Bakkerc0463502013-02-14 11:19:38 +01003427 if( ssl->session )
3428 {
3429 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003430 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003431 ssl->session = NULL;
3432 }
3433
Paul Bakker48916f92012-09-16 19:57:18 +00003434 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3435 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003436
3437 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003438}
3439
Paul Bakkera503a632013-08-14 13:48:06 +02003440#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003441/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003442 * Allocate and initialize ticket keys
3443 */
3444static int ssl_ticket_keys_init( ssl_context *ssl )
3445{
3446 int ret;
3447 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003448 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003449
3450 if( ssl->ticket_keys != NULL )
3451 return( 0 );
3452
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003453 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3454 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003455 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3456
3457 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
3458 return( ret );
3459
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003460 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3461 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3462 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3463 {
3464 return( ret );
3465 }
3466
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003467 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
3468 return( ret );
3469
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003470 ssl->ticket_keys = tkeys;
3471
3472 return( 0 );
3473}
Paul Bakkera503a632013-08-14 13:48:06 +02003474#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003475
3476/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003477 * SSL set accessors
3478 */
3479void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3480{
3481 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003482
Paul Bakker606b4ba2013-08-14 16:52:14 +02003483#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003484 if( endpoint == SSL_IS_CLIENT )
3485 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003486#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003487}
3488
3489void ssl_set_authmode( ssl_context *ssl, int authmode )
3490{
3491 ssl->authmode = authmode;
3492}
3493
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003494#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003495void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003496 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003497 void *p_vrfy )
3498{
3499 ssl->f_vrfy = f_vrfy;
3500 ssl->p_vrfy = p_vrfy;
3501}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003502#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003503
Paul Bakker5121ce52009-01-03 21:22:43 +00003504void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003505 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003506 void *p_rng )
3507{
3508 ssl->f_rng = f_rng;
3509 ssl->p_rng = p_rng;
3510}
3511
3512void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003513 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003514 void *p_dbg )
3515{
3516 ssl->f_dbg = f_dbg;
3517 ssl->p_dbg = p_dbg;
3518}
3519
3520void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003521 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003522 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003523{
3524 ssl->f_recv = f_recv;
3525 ssl->f_send = f_send;
3526 ssl->p_recv = p_recv;
3527 ssl->p_send = p_send;
3528}
3529
Paul Bakker0a597072012-09-25 21:55:46 +00003530void ssl_set_session_cache( ssl_context *ssl,
3531 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3532 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003533{
Paul Bakker0a597072012-09-25 21:55:46 +00003534 ssl->f_get_cache = f_get_cache;
3535 ssl->p_get_cache = p_get_cache;
3536 ssl->f_set_cache = f_set_cache;
3537 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003538}
3539
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003540int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003541{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003542 int ret;
3543
3544 if( ssl == NULL ||
3545 session == NULL ||
3546 ssl->session_negotiate == NULL ||
3547 ssl->endpoint != SSL_IS_CLIENT )
3548 {
3549 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3550 }
3551
3552 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3553 return( ret );
3554
Paul Bakker0a597072012-09-25 21:55:46 +00003555 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003556
3557 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003558}
3559
Paul Bakkerb68cad62012-08-23 08:34:18 +00003560void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003561{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003562 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3563 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3564 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3565 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3566}
3567
3568void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3569 int major, int minor )
3570{
3571 if( major != SSL_MAJOR_VERSION_3 )
3572 return;
3573
3574 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3575 return;
3576
3577 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003578}
3579
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003580#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003581/* Add a new (empty) key_cert entry an return a pointer to it */
3582static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3583{
3584 ssl_key_cert *key_cert, *last;
3585
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003586 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3587 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003588 return( NULL );
3589
3590 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3591
3592 /* Append the new key_cert to the (possibly empty) current list */
3593 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003594 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003595 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003596 if( ssl->handshake != NULL )
3597 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003598 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003599 else
3600 {
3601 last = ssl->key_cert;
3602 while( last->next != NULL )
3603 last = last->next;
3604 last->next = key_cert;
3605 }
3606
3607 return key_cert;
3608}
3609
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003610void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003611 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003612{
3613 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003614 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003615 ssl->peer_cn = peer_cn;
3616}
3617
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003618int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003619 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003620{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003621 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3622
3623 if( key_cert == NULL )
3624 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3625
3626 key_cert->cert = own_cert;
3627 key_cert->key = pk_key;
3628
3629 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003630}
3631
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003632#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003633int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003634 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003635{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003636 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003637 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003638
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003639 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003640 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3641
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003642 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3643 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003644 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003645
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003646 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003647
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003648 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003649 if( ret != 0 )
3650 return( ret );
3651
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003652 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003653 return( ret );
3654
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003655 key_cert->cert = own_cert;
3656 key_cert->key_own_alloc = 1;
3657
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003658 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003659}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003660#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003661
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003662int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003663 void *rsa_key,
3664 rsa_decrypt_func rsa_decrypt,
3665 rsa_sign_func rsa_sign,
3666 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003667{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003668 int ret;
3669 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003670
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003671 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +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é-Gonnard070cc7f2013-08-21 15:09:31 +02003677
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003678 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003679
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003680 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3681 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3682 return( ret );
3683
3684 key_cert->cert = own_cert;
3685 key_cert->key_own_alloc = 1;
3686
3687 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003688}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003689#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003690
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003691#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003692int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3693 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003694{
Paul Bakker6db455e2013-09-18 17:29:31 +02003695 if( psk == NULL || psk_identity == NULL )
3696 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3697
3698 if( ssl->psk != NULL )
3699 {
3700 polarssl_free( ssl->psk );
3701 polarssl_free( ssl->psk_identity );
3702 }
3703
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003704 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003705 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003706
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003707 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3708 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003709
3710 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3711 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3712
3713 memcpy( ssl->psk, psk, ssl->psk_len );
3714 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003715
3716 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003717}
3718
3719void ssl_set_psk_cb( ssl_context *ssl,
3720 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3721 size_t),
3722 void *p_psk )
3723{
3724 ssl->f_psk = f_psk;
3725 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003726}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003727#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003728
Paul Bakker48916f92012-09-16 19:57:18 +00003729#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003730int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003731{
3732 int ret;
3733
Paul Bakker48916f92012-09-16 19:57:18 +00003734 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 {
3736 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3737 return( ret );
3738 }
3739
Paul Bakker48916f92012-09-16 19:57:18 +00003740 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003741 {
3742 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3743 return( ret );
3744 }
3745
3746 return( 0 );
3747}
3748
Paul Bakker1b57b062011-01-06 15:48:19 +00003749int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3750{
3751 int ret;
3752
Paul Bakker48916f92012-09-16 19:57:18 +00003753 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003754 {
3755 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3756 return( ret );
3757 }
3758
Paul Bakker48916f92012-09-16 19:57:18 +00003759 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003760 {
3761 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3762 return( ret );
3763 }
3764
3765 return( 0 );
3766}
Paul Bakker48916f92012-09-16 19:57:18 +00003767#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003768
Paul Bakker0be444a2013-08-27 21:55:01 +02003769#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003770int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003771{
3772 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003773 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003774
3775 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003776
3777 if( ssl->hostname_len + 1 == 0 )
3778 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3779
Paul Bakker6e339b52013-07-03 13:37:05 +02003780 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003781
Paul Bakkerb15b8512012-01-13 13:44:06 +00003782 if( ssl->hostname == NULL )
3783 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3784
Paul Bakker3c2122f2013-06-24 19:03:14 +02003785 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003786 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003787
Paul Bakker40ea7de2009-05-03 10:18:48 +00003788 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003789
3790 return( 0 );
3791}
3792
Paul Bakker5701cdc2012-09-27 21:49:42 +00003793void ssl_set_sni( ssl_context *ssl,
3794 int (*f_sni)(void *, ssl_context *,
3795 const unsigned char *, size_t),
3796 void *p_sni )
3797{
3798 ssl->f_sni = f_sni;
3799 ssl->p_sni = p_sni;
3800}
Paul Bakker0be444a2013-08-27 21:55:01 +02003801#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003802
Paul Bakker490ecc82011-10-06 13:04:09 +00003803void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3804{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003805 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3806 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3807 {
3808 ssl->max_major_ver = major;
3809 ssl->max_minor_ver = minor;
3810 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003811}
3812
Paul Bakker1d29fb52012-09-28 13:28:45 +00003813void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3814{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003815 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3816 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3817 {
3818 ssl->min_major_ver = major;
3819 ssl->min_minor_ver = minor;
3820 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003821}
3822
Paul Bakker05decb22013-08-15 13:33:48 +02003823#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003824int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3825{
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003826 if( mfl_code >= sizeof( mfl_code_to_length ) ||
3827 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003828 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003829 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003830 }
3831
3832 ssl->mfl_code = mfl_code;
3833
3834 return( 0 );
3835}
Paul Bakker05decb22013-08-15 13:33:48 +02003836#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003837
Paul Bakker1f2bc622013-08-15 13:45:55 +02003838#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003839int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003840{
3841 if( ssl->endpoint != SSL_IS_CLIENT )
3842 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3843
Paul Bakker8c1ede62013-07-19 14:14:37 +02003844 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003845
3846 return( 0 );
3847}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003848#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003849
Paul Bakker48916f92012-09-16 19:57:18 +00003850void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3851{
3852 ssl->disable_renegotiation = renegotiation;
3853}
3854
3855void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3856{
3857 ssl->allow_legacy_renegotiation = allow_legacy;
3858}
3859
Paul Bakkera503a632013-08-14 13:48:06 +02003860#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003861int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3862{
3863 ssl->session_tickets = use_tickets;
3864
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003865 if( ssl->endpoint == SSL_IS_CLIENT )
3866 return( 0 );
3867
3868 if( ssl->f_rng == NULL )
3869 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3870
3871 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003872}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003873
3874void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3875{
3876 ssl->ticket_lifetime = lifetime;
3877}
Paul Bakkera503a632013-08-14 13:48:06 +02003878#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003879
Paul Bakker5121ce52009-01-03 21:22:43 +00003880/*
3881 * SSL get accessors
3882 */
Paul Bakker23986e52011-04-24 08:57:21 +00003883size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003884{
3885 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3886}
3887
Paul Bakkerff60ee62010-03-16 21:09:09 +00003888int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003889{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003890 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003891}
3892
Paul Bakkere3166ce2011-01-27 17:40:50 +00003893const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003894{
Paul Bakker926c8e42013-03-06 10:23:34 +01003895 if( ssl == NULL || ssl->session == NULL )
3896 return NULL;
3897
Paul Bakkere3166ce2011-01-27 17:40:50 +00003898 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003899}
3900
Paul Bakker43ca69c2011-01-15 17:35:19 +00003901const char *ssl_get_version( const ssl_context *ssl )
3902{
3903 switch( ssl->minor_ver )
3904 {
3905 case SSL_MINOR_VERSION_0:
3906 return( "SSLv3.0" );
3907
3908 case SSL_MINOR_VERSION_1:
3909 return( "TLSv1.0" );
3910
3911 case SSL_MINOR_VERSION_2:
3912 return( "TLSv1.1" );
3913
Paul Bakker1ef83d62012-04-11 12:09:53 +00003914 case SSL_MINOR_VERSION_3:
3915 return( "TLSv1.2" );
3916
Paul Bakker43ca69c2011-01-15 17:35:19 +00003917 default:
3918 break;
3919 }
3920 return( "unknown" );
3921}
3922
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003923#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003924const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003925{
3926 if( ssl == NULL || ssl->session == NULL )
3927 return NULL;
3928
3929 return ssl->session->peer_cert;
3930}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003931#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003932
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003933int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3934{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003935 if( ssl == NULL ||
3936 dst == NULL ||
3937 ssl->session == NULL ||
3938 ssl->endpoint != SSL_IS_CLIENT )
3939 {
3940 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3941 }
3942
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003943 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003944}
3945
Paul Bakker5121ce52009-01-03 21:22:43 +00003946/*
Paul Bakker1961b702013-01-25 14:49:24 +01003947 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003948 */
Paul Bakker1961b702013-01-25 14:49:24 +01003949int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003950{
Paul Bakker40e46942009-01-03 21:51:57 +00003951 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003952
Paul Bakker40e46942009-01-03 21:51:57 +00003953#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003954 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003955 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003956#endif
3957
Paul Bakker40e46942009-01-03 21:51:57 +00003958#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003959 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003960 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003961#endif
3962
Paul Bakker1961b702013-01-25 14:49:24 +01003963 return( ret );
3964}
3965
3966/*
3967 * Perform the SSL handshake
3968 */
3969int ssl_handshake( ssl_context *ssl )
3970{
3971 int ret = 0;
3972
3973 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
3974
3975 while( ssl->state != SSL_HANDSHAKE_OVER )
3976 {
3977 ret = ssl_handshake_step( ssl );
3978
3979 if( ret != 0 )
3980 break;
3981 }
3982
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
3984
3985 return( ret );
3986}
3987
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003988#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003989/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003990 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003991 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003992static int ssl_write_hello_request( ssl_context *ssl )
3993{
3994 int ret;
3995
3996 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
3997
3998 ssl->out_msglen = 4;
3999 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4000 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4001
4002 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4003 {
4004 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4005 return( ret );
4006 }
4007
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004008 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4009
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004010 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4011
4012 return( 0 );
4013}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004014#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004015
4016/*
4017 * Actually renegotiate current connection, triggered by either:
4018 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004019 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004020 * - receiving any handshake message on server during ssl_read() after the
4021 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004022 * If the handshake doesn't complete due to waiting for I/O, it will continue
4023 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004024 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004025static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004026{
4027 int ret;
4028
4029 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4030
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004031 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4032 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004033
4034 ssl->state = SSL_HELLO_REQUEST;
4035 ssl->renegotiation = SSL_RENEGOTIATION;
4036
Paul Bakker48916f92012-09-16 19:57:18 +00004037 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4038 {
4039 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4040 return( ret );
4041 }
4042
4043 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4044
4045 return( 0 );
4046}
4047
4048/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004049 * Renegotiate current connection on client,
4050 * or request renegotiation on server
4051 */
4052int ssl_renegotiate( ssl_context *ssl )
4053{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004054 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004055
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004056#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004057 /* On server, just send the request */
4058 if( ssl->endpoint == SSL_IS_SERVER )
4059 {
4060 if( ssl->state != SSL_HANDSHAKE_OVER )
4061 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4062
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004063 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004064 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004065#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004066
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004067#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004068 /*
4069 * On client, either start the renegotiation process or,
4070 * if already in progress, continue the handshake
4071 */
4072 if( ssl->renegotiation != SSL_RENEGOTIATION )
4073 {
4074 if( ssl->state != SSL_HANDSHAKE_OVER )
4075 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4076
4077 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4078 {
4079 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4080 return( ret );
4081 }
4082 }
4083 else
4084 {
4085 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4086 {
4087 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4088 return( ret );
4089 }
4090 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004091#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004092
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004093 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004094}
4095
4096/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004097 * Receive application data decrypted from the SSL layer
4098 */
Paul Bakker23986e52011-04-24 08:57:21 +00004099int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004100{
Paul Bakker23986e52011-04-24 08:57:21 +00004101 int ret;
4102 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004103
4104 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4105
4106 if( ssl->state != SSL_HANDSHAKE_OVER )
4107 {
4108 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4109 {
4110 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4111 return( ret );
4112 }
4113 }
4114
4115 if( ssl->in_offt == NULL )
4116 {
4117 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4118 {
Paul Bakker831a7552011-05-18 13:32:51 +00004119 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4120 return( 0 );
4121
Paul Bakker5121ce52009-01-03 21:22:43 +00004122 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4123 return( ret );
4124 }
4125
4126 if( ssl->in_msglen == 0 &&
4127 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4128 {
4129 /*
4130 * OpenSSL sends empty messages to randomize the IV
4131 */
4132 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4133 {
Paul Bakker831a7552011-05-18 13:32:51 +00004134 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4135 return( 0 );
4136
Paul Bakker5121ce52009-01-03 21:22:43 +00004137 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4138 return( ret );
4139 }
4140 }
4141
Paul Bakker48916f92012-09-16 19:57:18 +00004142 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4143 {
4144 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4145
4146 if( ssl->endpoint == SSL_IS_CLIENT &&
4147 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4148 ssl->in_hslen != 4 ) )
4149 {
4150 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4151 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4152 }
4153
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004154 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4155 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4156 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004157 {
4158 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4159
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004160#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004161 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004162 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004163 /*
4164 * SSLv3 does not have a "no_renegotiation" alert
4165 */
4166 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4167 return( ret );
4168 }
4169 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004170#endif
4171#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4172 defined(POLARSSL_SSL_PROTO_TLS1_2)
4173 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004174 {
4175 if( ( ret = ssl_send_alert_message( ssl,
4176 SSL_ALERT_LEVEL_WARNING,
4177 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4178 {
4179 return( ret );
4180 }
Paul Bakker48916f92012-09-16 19:57:18 +00004181 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004182 else
4183#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004184 {
4185 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004186 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004187 }
Paul Bakker48916f92012-09-16 19:57:18 +00004188 }
4189 else
4190 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004191 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004192 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004193 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004194 return( ret );
4195 }
4196
4197 return( POLARSSL_ERR_NET_WANT_READ );
4198 }
4199 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004200 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4201 {
4202 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4203 "but not honored by client" ) );
4204 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4205 }
Paul Bakker48916f92012-09-16 19:57:18 +00004206 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004207 {
4208 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004209 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004210 }
4211
4212 ssl->in_offt = ssl->in_msg;
4213 }
4214
4215 n = ( len < ssl->in_msglen )
4216 ? len : ssl->in_msglen;
4217
4218 memcpy( buf, ssl->in_offt, n );
4219 ssl->in_msglen -= n;
4220
4221 if( ssl->in_msglen == 0 )
4222 /* all bytes consumed */
4223 ssl->in_offt = NULL;
4224 else
4225 /* more data available */
4226 ssl->in_offt += n;
4227
4228 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4229
Paul Bakker23986e52011-04-24 08:57:21 +00004230 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004231}
4232
4233/*
4234 * Send application data to be encrypted by the SSL layer
4235 */
Paul Bakker23986e52011-04-24 08:57:21 +00004236int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004237{
Paul Bakker23986e52011-04-24 08:57:21 +00004238 int ret;
4239 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004240 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004241
4242 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4243
4244 if( ssl->state != SSL_HANDSHAKE_OVER )
4245 {
4246 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4247 {
4248 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4249 return( ret );
4250 }
4251 }
4252
Paul Bakker05decb22013-08-15 13:33:48 +02004253#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004254 /*
4255 * Assume mfl_code is correct since it was checked when set
4256 */
4257 max_len = mfl_code_to_length[ssl->mfl_code];
4258
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004259 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004260 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004261 */
4262 if( ssl->session_out != NULL &&
4263 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4264 {
4265 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4266 }
Paul Bakker05decb22013-08-15 13:33:48 +02004267#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004268
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004269 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004270
Paul Bakker5121ce52009-01-03 21:22:43 +00004271 if( ssl->out_left != 0 )
4272 {
4273 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4274 {
4275 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4276 return( ret );
4277 }
4278 }
Paul Bakker887bd502011-06-08 13:10:54 +00004279 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004280 {
Paul Bakker887bd502011-06-08 13:10:54 +00004281 ssl->out_msglen = n;
4282 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4283 memcpy( ssl->out_msg, buf, n );
4284
4285 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4286 {
4287 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4288 return( ret );
4289 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004290 }
4291
4292 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4293
Paul Bakker23986e52011-04-24 08:57:21 +00004294 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004295}
4296
4297/*
4298 * Notify the peer that the connection is being closed
4299 */
4300int ssl_close_notify( ssl_context *ssl )
4301{
4302 int ret;
4303
4304 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4305
4306 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4307 {
4308 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4309 return( ret );
4310 }
4311
4312 if( ssl->state == SSL_HANDSHAKE_OVER )
4313 {
Paul Bakker48916f92012-09-16 19:57:18 +00004314 if( ( ret = ssl_send_alert_message( ssl,
4315 SSL_ALERT_LEVEL_WARNING,
4316 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004317 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004318 return( ret );
4319 }
4320 }
4321
4322 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4323
4324 return( ret );
4325}
4326
Paul Bakker48916f92012-09-16 19:57:18 +00004327void ssl_transform_free( ssl_transform *transform )
4328{
4329#if defined(POLARSSL_ZLIB_SUPPORT)
4330 deflateEnd( &transform->ctx_deflate );
4331 inflateEnd( &transform->ctx_inflate );
4332#endif
4333
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004334 cipher_free_ctx( &transform->cipher_ctx_enc );
4335 cipher_free_ctx( &transform->cipher_ctx_dec );
4336
Paul Bakker61d113b2013-07-04 11:51:43 +02004337 md_free_ctx( &transform->md_ctx_enc );
4338 md_free_ctx( &transform->md_ctx_dec );
4339
Paul Bakker48916f92012-09-16 19:57:18 +00004340 memset( transform, 0, sizeof( ssl_transform ) );
4341}
4342
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004343#if defined(POLARSSL_X509_CRT_PARSE_C)
4344static void ssl_key_cert_free( ssl_key_cert *key_cert )
4345{
4346 ssl_key_cert *cur = key_cert, *next;
4347
4348 while( cur != NULL )
4349 {
4350 next = cur->next;
4351
4352 if( cur->key_own_alloc )
4353 {
4354 pk_free( cur->key );
4355 polarssl_free( cur->key );
4356 }
4357 polarssl_free( cur );
4358
4359 cur = next;
4360 }
4361}
4362#endif /* POLARSSL_X509_CRT_PARSE_C */
4363
Paul Bakker48916f92012-09-16 19:57:18 +00004364void ssl_handshake_free( ssl_handshake_params *handshake )
4365{
4366#if defined(POLARSSL_DHM_C)
4367 dhm_free( &handshake->dhm_ctx );
4368#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004369#if defined(POLARSSL_ECDH_C)
4370 ecdh_free( &handshake->ecdh_ctx );
4371#endif
4372
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004373#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004374 /* explicit void pointer cast for buggy MS compiler */
4375 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004376#endif
4377
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004378#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4379 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4380 /*
4381 * Free only the linked list wrapper, not the keys themselves
4382 * since the belong to the SNI callback
4383 */
4384 if( handshake->sni_key_cert != NULL )
4385 {
4386 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4387
4388 while( cur != NULL )
4389 {
4390 next = cur->next;
4391 polarssl_free( cur );
4392 cur = next;
4393 }
4394 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004395#endif
4396
Paul Bakker48916f92012-09-16 19:57:18 +00004397 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4398}
4399
4400void ssl_session_free( ssl_session *session )
4401{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004402#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004403 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004404 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004405 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004406 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004407 }
Paul Bakkered27a042013-04-18 22:46:23 +02004408#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004409
Paul Bakkera503a632013-08-14 13:48:06 +02004410#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004411 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004412#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004413
Paul Bakker0a597072012-09-25 21:55:46 +00004414 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004415}
4416
Paul Bakker5121ce52009-01-03 21:22:43 +00004417/*
4418 * Free an SSL context
4419 */
4420void ssl_free( ssl_context *ssl )
4421{
4422 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4423
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 if( ssl->out_ctr != NULL )
4425 {
4426 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004427 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004428 }
4429
4430 if( ssl->in_ctr != NULL )
4431 {
4432 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004433 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434 }
4435
Paul Bakker16770332013-10-11 09:59:44 +02004436#if defined(POLARSSL_ZLIB_SUPPORT)
4437 if( ssl->compress_buf != NULL )
4438 {
4439 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4440 polarssl_free( ssl->compress_buf );
4441 }
4442#endif
4443
Paul Bakker40e46942009-01-03 21:51:57 +00004444#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004445 mpi_free( &ssl->dhm_P );
4446 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004447#endif
4448
Paul Bakker48916f92012-09-16 19:57:18 +00004449 if( ssl->transform )
4450 {
4451 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004452 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004453 }
4454
4455 if( ssl->handshake )
4456 {
4457 ssl_handshake_free( ssl->handshake );
4458 ssl_transform_free( ssl->transform_negotiate );
4459 ssl_session_free( ssl->session_negotiate );
4460
Paul Bakker6e339b52013-07-03 13:37:05 +02004461 polarssl_free( ssl->handshake );
4462 polarssl_free( ssl->transform_negotiate );
4463 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004464 }
4465
Paul Bakkerc0463502013-02-14 11:19:38 +01004466 if( ssl->session )
4467 {
4468 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004469 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004470 }
4471
Paul Bakkera503a632013-08-14 13:48:06 +02004472#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004473 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004474#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004475
Paul Bakker0be444a2013-08-27 21:55:01 +02004476#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004477 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004478 {
4479 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004480 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004481 ssl->hostname_len = 0;
4482 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004483#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004484
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004485#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004486 if( ssl->psk != NULL )
4487 {
4488 memset( ssl->psk, 0, ssl->psk_len );
4489 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4490 polarssl_free( ssl->psk );
4491 polarssl_free( ssl->psk_identity );
4492 ssl->psk_len = 0;
4493 ssl->psk_identity_len = 0;
4494 }
4495#endif
4496
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004497#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004498 ssl_key_cert_free( ssl->key_cert );
4499#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004500
Paul Bakker05ef8352012-05-08 09:17:57 +00004501#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4502 if( ssl_hw_record_finish != NULL )
4503 {
4504 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4505 ssl_hw_record_finish( ssl );
4506 }
4507#endif
4508
Paul Bakker5121ce52009-01-03 21:22:43 +00004509 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004510
Paul Bakker86f04f42013-02-14 11:20:09 +01004511 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004512 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004513}
4514
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004515#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004516/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004517 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004518 */
4519unsigned char ssl_sig_from_pk( pk_context *pk )
4520{
4521#if defined(POLARSSL_RSA_C)
4522 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4523 return( SSL_SIG_RSA );
4524#endif
4525#if defined(POLARSSL_ECDSA_C)
4526 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4527 return( SSL_SIG_ECDSA );
4528#endif
4529 return( SSL_SIG_ANON );
4530}
4531
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004532pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4533{
4534 switch( sig )
4535 {
4536#if defined(POLARSSL_RSA_C)
4537 case SSL_SIG_RSA:
4538 return( POLARSSL_PK_RSA );
4539#endif
4540#if defined(POLARSSL_ECDSA_C)
4541 case SSL_SIG_ECDSA:
4542 return( POLARSSL_PK_ECDSA );
4543#endif
4544 default:
4545 return( POLARSSL_PK_NONE );
4546 }
4547}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004548#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004549
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004550/*
4551 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4552 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004553md_type_t ssl_md_alg_from_hash( unsigned char hash )
4554{
4555 switch( hash )
4556 {
4557#if defined(POLARSSL_MD5_C)
4558 case SSL_HASH_MD5:
4559 return( POLARSSL_MD_MD5 );
4560#endif
4561#if defined(POLARSSL_SHA1_C)
4562 case SSL_HASH_SHA1:
4563 return( POLARSSL_MD_SHA1 );
4564#endif
4565#if defined(POLARSSL_SHA256_C)
4566 case SSL_HASH_SHA224:
4567 return( POLARSSL_MD_SHA224 );
4568 case SSL_HASH_SHA256:
4569 return( POLARSSL_MD_SHA256 );
4570#endif
4571#if defined(POLARSSL_SHA512_C)
4572 case SSL_HASH_SHA384:
4573 return( POLARSSL_MD_SHA384 );
4574 case SSL_HASH_SHA512:
4575 return( POLARSSL_MD_SHA512 );
4576#endif
4577 default:
4578 return( POLARSSL_MD_NONE );
4579 }
4580}
4581
Paul Bakker5121ce52009-01-03 21:22:43 +00004582#endif