blob: 6ea282180b30faaa659703db659fcb13fa8a9e2b [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 Bakker1e5369c2013-12-19 16:40:57 +01001317 size_t i;
1318#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1319 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1320 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1321 size_t padlen = 0, correct = 1;
1322#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
1324 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1325
Paul Bakker48916f92012-09-16 19:57:18 +00001326 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 {
1328 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001329 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001330 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 }
1332
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001333#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001334 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1335 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001336 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001337 int ret;
1338 size_t olen = 0;
1339
Paul Bakker68884e32013-01-07 18:20:04 +01001340 padlen = 0;
1341
Paul Bakker45125bc2013-09-04 16:47:11 +02001342 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001343 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001344 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1345 return( ret );
1346 }
1347
1348 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1349 ssl->transform_in->iv_dec,
1350 ssl->transform_in->ivlen ) ) != 0 )
1351 {
1352 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001353 return( ret );
1354 }
1355
1356 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1357 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1358 &olen ) ) != 0 )
1359 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001360 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001361 return( ret );
1362 }
1363
1364 if( ssl->in_msglen != olen )
1365 {
1366 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001367 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001368 }
1369
1370 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001371 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001372 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001373 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001374 return( ret );
1375 }
1376
1377 if( 0 != olen )
1378 {
1379 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001380 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
Paul Bakker68884e32013-01-07 18:20:04 +01001383 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001384#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001385#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001386 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1387 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001388 {
1389 unsigned char *dec_msg;
1390 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001391 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001392 unsigned char add_data[13];
1393 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1394
Paul Bakker68884e32013-01-07 18:20:04 +01001395 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1396 ssl->transform_in->fixed_ivlen );
1397 dec_msglen -= 16;
1398 dec_msg = ssl->in_msg;
1399 dec_msg_result = ssl->in_msg;
1400 ssl->in_msglen = dec_msglen;
1401
1402 memcpy( add_data, ssl->in_ctr, 8 );
1403 add_data[8] = ssl->in_msgtype;
1404 add_data[9] = ssl->major_ver;
1405 add_data[10] = ssl->minor_ver;
1406 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1407 add_data[12] = ssl->in_msglen & 0xFF;
1408
1409 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1410 add_data, 13 );
1411
1412 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1413 ssl->in_iv,
1414 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1415
1416 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1417 ssl->transform_in->ivlen );
1418 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1419
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001420 /*
1421 * Decrypt
1422 */
1423 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1424 ssl->transform_in->iv_dec,
1425 ssl->transform_in->ivlen ) ) != 0 ||
1426 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001427 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001428 return( ret );
1429 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001430
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001431 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1432 add_data, 13 ) ) != 0 )
1433 {
1434 return( ret );
1435 }
1436
1437 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1438 dec_msg, dec_msglen,
1439 dec_msg_result, &olen ) ) != 0 )
1440 {
1441 return( ret );
1442 }
1443 totlen = olen;
1444
1445 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1446 dec_msg_result + olen, &olen ) ) != 0 )
1447 {
1448 return( ret );
1449 }
1450 totlen += olen;
1451
1452 if( totlen != dec_msglen )
1453 {
1454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1455 return( -1 );
1456 }
1457
1458 /*
1459 * Authenticate
1460 */
1461 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1462 dec_msg + dec_msglen, 16 ) ) != 0 )
1463 {
1464 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001465 return( POLARSSL_ERR_SSL_INVALID_MAC );
1466 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001467
Paul Bakkerca4ab492012-04-18 14:23:57 +00001468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 else
Paul Bakker68884e32013-01-07 18:20:04 +01001470#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001471#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1472 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001473 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1474 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 {
Paul Bakker45829992013-01-03 14:52:21 +01001476 /*
1477 * Decrypt and check the padding
1478 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001479 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001480 unsigned char *dec_msg;
1481 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001482 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001483 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001484 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001485
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 /*
Paul Bakker45829992013-01-03 14:52:21 +01001487 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 */
Paul Bakker48916f92012-09-16 19:57:18 +00001489 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 {
1491 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001492 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001493 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 }
1495
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001496#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001497 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1498 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001499#endif
Paul Bakker45829992013-01-03 14:52:21 +01001500
1501 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1502 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1503 {
1504 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1505 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1506 return( POLARSSL_ERR_SSL_INVALID_MAC );
1507 }
1508
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001509 dec_msglen = ssl->in_msglen;
1510 dec_msg = ssl->in_msg;
1511 dec_msg_result = ssl->in_msg;
1512
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001513#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001514 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001515 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001516 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001517 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001518 {
Paul Bakker48916f92012-09-16 19:57:18 +00001519 dec_msglen -= ssl->transform_in->ivlen;
1520 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001521
Paul Bakker48916f92012-09-16 19:57:18 +00001522 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001523 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001524 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001525#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001526
Paul Bakker45125bc2013-09-04 16:47:11 +02001527 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001529 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1530 return( ret );
1531 }
1532
1533 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1534 ssl->transform_in->iv_dec,
1535 ssl->transform_in->ivlen ) ) != 0 )
1536 {
1537 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001538 return( ret );
1539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
Paul Bakkercca5b812013-08-31 17:40:26 +02001541 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1542 dec_msg, dec_msglen, dec_msg_result,
1543 &olen ) ) != 0 )
1544 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001545 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001546 return( ret );
1547 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001548
Paul Bakkercca5b812013-08-31 17:40:26 +02001549 dec_msglen -= olen;
1550 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001551 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001552 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001553 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001554 return( ret );
1555 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001556
Paul Bakkercca5b812013-08-31 17:40:26 +02001557 if( dec_msglen != olen )
1558 {
1559 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001560 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001562
1563#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001564 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1565 {
1566 /*
1567 * Save IV in SSL3 and TLS1
1568 */
1569 memcpy( ssl->transform_in->iv_dec,
1570 ssl->transform_in->cipher_ctx_dec.iv,
1571 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001573#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001574
1575 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001576
1577 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1578 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001579#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001580 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1581 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001582#endif
Paul Bakker45829992013-01-03 14:52:21 +01001583 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001584 correct = 0;
1585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001586
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001587#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1589 {
Paul Bakker48916f92012-09-16 19:57:18 +00001590 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001592#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001593 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1594 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001595 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001596#endif
Paul Bakker45829992013-01-03 14:52:21 +01001597 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 }
1599 }
1600 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001601#endif
1602#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1603 defined(POLARSSL_SSL_PROTO_TLS1_2)
1604 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 {
1606 /*
Paul Bakker45829992013-01-03 14:52:21 +01001607 * TLSv1+: always check the padding up to the first failure
1608 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001610 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001611 size_t padding_idx = ssl->in_msglen - padlen - 1;
1612
Paul Bakker956c9e02013-12-19 14:42:28 +01001613 /*
1614 * Padding is guaranteed to be incorrect if:
1615 * 1. padlen - 1 > ssl->in_msglen
1616 *
1617 * 2. ssl->in_msglen + padlen >
1618 * SSL_MAX_CONTENT_LEN + 256 (max padding)
1619 *
1620 * In both cases we reset padding_idx to a safe value (0) to
1621 * prevent out-of-buffer reads.
1622 */
1623 correct &= ( ssl->in_msglen >= padlen - 1 );
1624 correct &= ( ssl->in_msglen + padlen <= SSL_MAX_CONTENT_LEN + 256 );
1625
1626 padding_idx *= correct;
1627
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001628 for( i = 1; i <= 256; i++ )
1629 {
1630 real_count &= ( i <= padlen );
1631 pad_count += real_count *
1632 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1633 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001634
1635 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001636
Paul Bakkerd66f0702013-01-31 16:57:45 +01001637#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001638 if( padlen > 0 && correct == 0)
1639 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001640#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001641 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001643 else
1644#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1645 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001646 {
1647 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001648 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001649 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001650 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001651 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001652#endif /* POLARSSL_CIPHER_MODE_CBC &&
1653 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001654 {
1655 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1656 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
1659 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1660 ssl->in_msg, ssl->in_msglen );
1661
1662 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001665#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1666 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1667 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1668 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1669 POLARSSL_MODE_GCM )
1670 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001671 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1672
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001673 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001674
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001675 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1676 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001680#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001681 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1682 {
1683 ssl_mac( &ssl->transform_in->md_ctx_dec,
1684 ssl->transform_in->mac_dec,
1685 ssl->in_msg, ssl->in_msglen,
1686 ssl->in_ctr, ssl->in_msgtype );
1687 }
1688 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001689#endif /* POLARSSL_SSL_PROTO_SSL3 */
1690#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001691 defined(POLARSSL_SSL_PROTO_TLS1_2)
1692 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1693 {
1694 /*
1695 * Process MAC and always update for padlen afterwards to make
1696 * total time independent of padlen
1697 *
1698 * extra_run compensates MAC check for padlen
1699 *
1700 * Known timing attacks:
1701 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1702 *
1703 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1704 * correctly. (We round down instead of up, so -56 is the correct
1705 * value for our calculations instead of -55)
1706 */
1707 size_t j, extra_run = 0;
1708 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1709 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001710
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001711 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001712
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001713 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1714 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1715 ssl->in_msglen );
1716 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1717 ssl->in_msg + ssl->in_msglen );
1718 for( j = 0; j < extra_run; j++ )
1719 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001720
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001721 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1722 }
1723 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001724#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 POLARSSL_SSL_PROTO_TLS1_2 */
1726 {
1727 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1728 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1729 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001731 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1732 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1733 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001735 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001736 ssl->transform_in->maclen ) != 0 )
1737 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001738#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001739 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001740#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001741 correct = 0;
1742 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 /*
1745 * Finally check the correct flag
1746 */
1747 if( correct == 0 )
1748 return( POLARSSL_ERR_SSL_INVALID_MAC );
1749 }
1750#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
1752 if( ssl->in_msglen == 0 )
1753 {
1754 ssl->nb_zero++;
1755
1756 /*
1757 * Three or more empty messages may be a DoS attack
1758 * (excessive CPU consumption).
1759 */
1760 if( ssl->nb_zero > 3 )
1761 {
1762 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1763 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001764 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 }
1766 }
1767 else
1768 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001769
Paul Bakker23986e52011-04-24 08:57:21 +00001770 for( i = 8; i > 0; i-- )
1771 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 break;
1773
1774 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1775
1776 return( 0 );
1777}
1778
Paul Bakker2770fbd2012-07-03 13:30:23 +00001779#if defined(POLARSSL_ZLIB_SUPPORT)
1780/*
1781 * Compression/decompression functions
1782 */
1783static int ssl_compress_buf( ssl_context *ssl )
1784{
1785 int ret;
1786 unsigned char *msg_post = ssl->out_msg;
1787 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001788 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001789
1790 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1791
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001792 if( len_pre == 0 )
1793 return( 0 );
1794
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795 memcpy( msg_pre, ssl->out_msg, len_pre );
1796
1797 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1798 ssl->out_msglen ) );
1799
1800 SSL_DEBUG_BUF( 4, "before compression: output payload",
1801 ssl->out_msg, ssl->out_msglen );
1802
Paul Bakker48916f92012-09-16 19:57:18 +00001803 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1804 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1805 ssl->transform_out->ctx_deflate.next_out = msg_post;
1806 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001807
Paul Bakker48916f92012-09-16 19:57:18 +00001808 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001809 if( ret != Z_OK )
1810 {
1811 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1812 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1813 }
1814
Paul Bakker48916f92012-09-16 19:57:18 +00001815 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001816
Paul Bakker2770fbd2012-07-03 13:30:23 +00001817 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1818 ssl->out_msglen ) );
1819
1820 SSL_DEBUG_BUF( 4, "after compression: output payload",
1821 ssl->out_msg, ssl->out_msglen );
1822
1823 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1824
1825 return( 0 );
1826}
1827
1828static int ssl_decompress_buf( ssl_context *ssl )
1829{
1830 int ret;
1831 unsigned char *msg_post = ssl->in_msg;
1832 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001833 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001834
1835 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1836
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001837 if( len_pre == 0 )
1838 return( 0 );
1839
Paul Bakker2770fbd2012-07-03 13:30:23 +00001840 memcpy( msg_pre, ssl->in_msg, len_pre );
1841
1842 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1843 ssl->in_msglen ) );
1844
1845 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1846 ssl->in_msg, ssl->in_msglen );
1847
Paul Bakker48916f92012-09-16 19:57:18 +00001848 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1849 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1850 ssl->transform_in->ctx_inflate.next_out = msg_post;
1851 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001852
Paul Bakker48916f92012-09-16 19:57:18 +00001853 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001854 if( ret != Z_OK )
1855 {
1856 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1857 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1858 }
1859
Paul Bakker48916f92012-09-16 19:57:18 +00001860 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001861
Paul Bakker2770fbd2012-07-03 13:30:23 +00001862 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1863 ssl->in_msglen ) );
1864
1865 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1866 ssl->in_msg, ssl->in_msglen );
1867
1868 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1869
1870 return( 0 );
1871}
1872#endif /* POLARSSL_ZLIB_SUPPORT */
1873
Paul Bakker5121ce52009-01-03 21:22:43 +00001874/*
1875 * Fill the input message buffer
1876 */
Paul Bakker23986e52011-04-24 08:57:21 +00001877int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001878{
Paul Bakker23986e52011-04-24 08:57:21 +00001879 int ret;
1880 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
1882 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1883
1884 while( ssl->in_left < nb_want )
1885 {
1886 len = nb_want - ssl->in_left;
1887 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1888
1889 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1890 ssl->in_left, nb_want ) );
1891 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1892
Paul Bakker831a7552011-05-18 13:32:51 +00001893 if( ret == 0 )
1894 return( POLARSSL_ERR_SSL_CONN_EOF );
1895
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 if( ret < 0 )
1897 return( ret );
1898
1899 ssl->in_left += ret;
1900 }
1901
1902 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1903
1904 return( 0 );
1905}
1906
1907/*
1908 * Flush any data not yet written
1909 */
1910int ssl_flush_output( ssl_context *ssl )
1911{
1912 int ret;
1913 unsigned char *buf;
1914
1915 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1916
1917 while( ssl->out_left > 0 )
1918 {
1919 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1920 5 + ssl->out_msglen, ssl->out_left ) );
1921
Paul Bakker5bd42292012-12-19 14:40:42 +01001922 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001924
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1926
1927 if( ret <= 0 )
1928 return( ret );
1929
1930 ssl->out_left -= ret;
1931 }
1932
1933 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1934
1935 return( 0 );
1936}
1937
1938/*
1939 * Record layer functions
1940 */
1941int ssl_write_record( ssl_context *ssl )
1942{
Paul Bakker05ef8352012-05-08 09:17:57 +00001943 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001944 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
1946 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1947
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1949 {
1950 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1951 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1952 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1953
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001954 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1955 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 }
1957
Paul Bakker2770fbd2012-07-03 13:30:23 +00001958#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001959 if( ssl->transform_out != NULL &&
1960 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001961 {
1962 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1963 {
1964 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1965 return( ret );
1966 }
1967
1968 len = ssl->out_msglen;
1969 }
1970#endif /*POLARSSL_ZLIB_SUPPORT */
1971
Paul Bakker05ef8352012-05-08 09:17:57 +00001972#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1973 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001975 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
Paul Bakker05ef8352012-05-08 09:17:57 +00001977 ret = ssl_hw_record_write( ssl );
1978 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1979 {
1980 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1981 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1982 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001983
1984 if( ret == 0 )
1985 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001986 }
1987#endif
1988 if( !done )
1989 {
1990 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1991 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1992 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001993 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1994 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001995
Paul Bakker48916f92012-09-16 19:57:18 +00001996 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001997 {
1998 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1999 {
2000 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2001 return( ret );
2002 }
2003
2004 len = ssl->out_msglen;
2005 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2006 ssl->out_hdr[4] = (unsigned char)( len );
2007 }
2008
2009 ssl->out_left = 5 + ssl->out_msglen;
2010
2011 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2012 "version = [%d:%d], msglen = %d",
2013 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2014 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2015
2016 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002017 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 }
2019
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2021 {
2022 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2023 return( ret );
2024 }
2025
2026 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2027
2028 return( 0 );
2029}
2030
2031int ssl_read_record( ssl_context *ssl )
2032{
Paul Bakker05ef8352012-05-08 09:17:57 +00002033 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
2035 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2036
Paul Bakker68884e32013-01-07 18:20:04 +01002037 SSL_DEBUG_BUF( 4, "input record from network",
2038 ssl->in_hdr, 5 + ssl->in_msglen );
2039
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 if( ssl->in_hslen != 0 &&
2041 ssl->in_hslen < ssl->in_msglen )
2042 {
2043 /*
2044 * Get next Handshake message in the current record
2045 */
2046 ssl->in_msglen -= ssl->in_hslen;
2047
Paul Bakker8934a982011-08-05 11:11:53 +00002048 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002049 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002050
2051 ssl->in_hslen = 4;
2052 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2053
2054 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2055 " %d, type = %d, hslen = %d",
2056 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2057
2058 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2059 {
2060 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002061 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 }
2063
2064 if( ssl->in_msglen < ssl->in_hslen )
2065 {
2066 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002067 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069
Paul Bakker48916f92012-09-16 19:57:18 +00002070 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
2072 return( 0 );
2073 }
2074
2075 ssl->in_hslen = 0;
2076
2077 /*
2078 * Read the record header and validate it
2079 */
2080 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2081 {
2082 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2083 return( ret );
2084 }
2085
2086 ssl->in_msgtype = ssl->in_hdr[0];
2087 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2088
2089 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2090 "version = [%d:%d], msglen = %d",
2091 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2092 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2093
2094 if( ssl->in_hdr[1] != ssl->major_ver )
2095 {
2096 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002097 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 }
2099
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002100 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 {
2102 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002103 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
2105
2106 /*
2107 * Make sure the message length is acceptable
2108 */
Paul Bakker48916f92012-09-16 19:57:18 +00002109 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
2111 if( ssl->in_msglen < 1 ||
2112 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2113 {
2114 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002115 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 }
2117 }
2118 else
2119 {
Paul Bakker48916f92012-09-16 19:57:18 +00002120 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
2122 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002123 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
2125
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002126#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002128 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 {
2130 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002131 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002134
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002135#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2136 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 /*
2138 * TLS encrypted messages can have up to 256 bytes of padding
2139 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002140 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002141 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 {
2143 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002144 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002146#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 }
2148
2149 /*
2150 * Read and optionally decrypt the message contents
2151 */
2152 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2153 {
2154 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2155 return( ret );
2156 }
2157
2158 SSL_DEBUG_BUF( 4, "input record from network",
2159 ssl->in_hdr, 5 + ssl->in_msglen );
2160
Paul Bakker05ef8352012-05-08 09:17:57 +00002161#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2162 if( ssl_hw_record_read != NULL)
2163 {
2164 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2165
2166 ret = ssl_hw_record_read( ssl );
2167 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2168 {
2169 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2170 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2171 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002172
2173 if( ret == 0 )
2174 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002175 }
2176#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002177 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 {
2179 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2180 {
Paul Bakker40865c82013-01-31 17:13:13 +01002181#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2182 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2183 {
2184 ssl_send_alert_message( ssl,
2185 SSL_ALERT_LEVEL_FATAL,
2186 SSL_ALERT_MSG_BAD_RECORD_MAC );
2187 }
2188#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2190 return( ret );
2191 }
2192
2193 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2194 ssl->in_msg, ssl->in_msglen );
2195
2196 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2197 {
2198 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002199 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 }
2201 }
2202
Paul Bakker2770fbd2012-07-03 13:30:23 +00002203#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002204 if( ssl->transform_in != NULL &&
2205 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002206 {
2207 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2208 {
2209 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2210 return( ret );
2211 }
2212
2213 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2214 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2215 }
2216#endif /* POLARSSL_ZLIB_SUPPORT */
2217
Paul Bakker0a925182012-04-16 06:46:41 +00002218 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2219 ssl->in_msgtype != SSL_MSG_ALERT &&
2220 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2221 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2222 {
2223 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2224
Paul Bakker48916f92012-09-16 19:57:18 +00002225 if( ( ret = ssl_send_alert_message( ssl,
2226 SSL_ALERT_LEVEL_FATAL,
2227 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002228 {
2229 return( ret );
2230 }
2231
2232 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2233 }
2234
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2236 {
2237 ssl->in_hslen = 4;
2238 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2239
2240 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2241 " %d, type = %d, hslen = %d",
2242 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2243
2244 /*
2245 * Additional checks to validate the handshake header
2246 */
2247 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2248 {
2249 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002250 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 }
2252
2253 if( ssl->in_msglen < ssl->in_hslen )
2254 {
2255 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002256 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 }
2258
Paul Bakker48916f92012-09-16 19:57:18 +00002259 if( ssl->state != SSL_HANDSHAKE_OVER )
2260 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262
2263 if( ssl->in_msgtype == SSL_MSG_ALERT )
2264 {
2265 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2266 ssl->in_msg[0], ssl->in_msg[1] ) );
2267
2268 /*
2269 * Ignore non-fatal alerts, except close_notify
2270 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002271 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002273 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2274 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002275 /**
2276 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2277 * error identifier.
2278 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002279 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 }
2281
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002282 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2283 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 {
2285 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002286 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
2288 }
2289
2290 ssl->in_left = 0;
2291
2292 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2293
2294 return( 0 );
2295}
2296
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002297int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2298{
2299 int ret;
2300
2301 if( ( ret = ssl_send_alert_message( ssl,
2302 SSL_ALERT_LEVEL_FATAL,
2303 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2304 {
2305 return( ret );
2306 }
2307
2308 return( 0 );
2309}
2310
Paul Bakker0a925182012-04-16 06:46:41 +00002311int ssl_send_alert_message( ssl_context *ssl,
2312 unsigned char level,
2313 unsigned char message )
2314{
2315 int ret;
2316
2317 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2318
2319 ssl->out_msgtype = SSL_MSG_ALERT;
2320 ssl->out_msglen = 2;
2321 ssl->out_msg[0] = level;
2322 ssl->out_msg[1] = message;
2323
2324 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2325 {
2326 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2327 return( ret );
2328 }
2329
2330 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2331
2332 return( 0 );
2333}
2334
Paul Bakker5121ce52009-01-03 21:22:43 +00002335/*
2336 * Handshake functions
2337 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002338#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2339 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2340 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2341 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2342 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2343 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2344 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002345int ssl_write_certificate( ssl_context *ssl )
2346{
Paul Bakkered27a042013-04-18 22:46:23 +02002347 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002348 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
2350 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2351
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002352 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002353 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2354 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002355 {
2356 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2357 ssl->state++;
2358 return( 0 );
2359 }
2360
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002361 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002362 return( ret );
2363}
2364
2365int ssl_parse_certificate( ssl_context *ssl )
2366{
2367 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2368 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2369
2370 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2371
2372 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002373 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2374 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002375 {
2376 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2377 ssl->state++;
2378 return( 0 );
2379 }
2380
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002381 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002382 return( ret );
2383}
2384#else
2385int ssl_write_certificate( ssl_context *ssl )
2386{
2387 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2388 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002389 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002390 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2391
2392 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2393
2394 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002395 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2396 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002397 {
2398 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2399 ssl->state++;
2400 return( 0 );
2401 }
2402
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 if( ssl->endpoint == SSL_IS_CLIENT )
2404 {
2405 if( ssl->client_auth == 0 )
2406 {
2407 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2408 ssl->state++;
2409 return( 0 );
2410 }
2411
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002412#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 /*
2414 * If using SSLv3 and got no cert, send an Alert message
2415 * (otherwise an empty Certificate message will be sent).
2416 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002417 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2419 {
2420 ssl->out_msglen = 2;
2421 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002422 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2423 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002424
2425 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2426 goto write_msg;
2427 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002428#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 }
2430 else /* SSL_IS_SERVER */
2431 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002432 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 {
2434 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002435 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 }
2437 }
2438
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002439 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
2441 /*
2442 * 0 . 0 handshake type
2443 * 1 . 3 handshake length
2444 * 4 . 6 length of all certs
2445 * 7 . 9 length of cert. 1
2446 * 10 . n-1 peer certificate
2447 * n . n+2 length of cert. 2
2448 * n+3 . ... upper level cert, etc.
2449 */
2450 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002451 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Paul Bakker29087132010-03-21 21:03:34 +00002453 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 {
2455 n = crt->raw.len;
2456 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
2457 {
2458 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2459 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002460 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
2462
2463 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2464 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2465 ssl->out_msg[i + 2] = (unsigned char)( n );
2466
2467 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2468 i += n; crt = crt->next;
2469 }
2470
2471 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2472 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2473 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2474
2475 ssl->out_msglen = i;
2476 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2477 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2478
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002479#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002480write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002481#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
2483 ssl->state++;
2484
2485 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2486 {
2487 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2488 return( ret );
2489 }
2490
2491 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2492
Paul Bakkered27a042013-04-18 22:46:23 +02002493 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494}
2495
2496int ssl_parse_certificate( ssl_context *ssl )
2497{
Paul Bakkered27a042013-04-18 22:46:23 +02002498 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002499 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002500 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002501
2502 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2503
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002504 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002505 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2506 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002507 {
2508 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2509 ssl->state++;
2510 return( 0 );
2511 }
2512
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002514 ( ssl->authmode == SSL_VERIFY_NONE ||
2515 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002517 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2519 ssl->state++;
2520 return( 0 );
2521 }
2522
2523 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2524 {
2525 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2526 return( ret );
2527 }
2528
2529 ssl->state++;
2530
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002531#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 /*
2533 * Check if the client sent an empty certificate
2534 */
2535 if( ssl->endpoint == SSL_IS_SERVER &&
2536 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2537 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002538 if( ssl->in_msglen == 2 &&
2539 ssl->in_msgtype == SSL_MSG_ALERT &&
2540 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2541 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 {
2543 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2544
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002545 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2547 return( 0 );
2548 else
Paul Bakker40e46942009-01-03 21:51:57 +00002549 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 }
2551 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002552#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002554#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2555 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 if( ssl->endpoint == SSL_IS_SERVER &&
2557 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2558 {
2559 if( ssl->in_hslen == 7 &&
2560 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2561 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2562 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2563 {
2564 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2565
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002566 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002568 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 else
2570 return( 0 );
2571 }
2572 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002573#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2574 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
2576 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2577 {
2578 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002579 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 }
2581
2582 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2583 {
2584 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002585 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 }
2587
2588 /*
2589 * Same message structure as in ssl_write_certificate()
2590 */
2591 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2592
2593 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2594 {
2595 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002596 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 }
2598
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002599 /* In case we tried to reuse a session but it failed */
2600 if( ssl->session_negotiate->peer_cert != NULL )
2601 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002602 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002603 polarssl_free( ssl->session_negotiate->peer_cert );
2604 }
2605
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002606 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2607 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 {
2609 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002610 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002611 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 }
2613
Paul Bakkerb6b09562013-09-18 14:17:41 +02002614 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
2616 i = 7;
2617
2618 while( i < ssl->in_hslen )
2619 {
2620 if( ssl->in_msg[i] != 0 )
2621 {
2622 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002623 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 }
2625
2626 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2627 | (unsigned int) ssl->in_msg[i + 2];
2628 i += 3;
2629
2630 if( n < 128 || i + n > ssl->in_hslen )
2631 {
2632 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002633 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 }
2635
Paul Bakkerddf26b42013-09-18 13:46:23 +02002636 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2637 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 if( ret != 0 )
2639 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002640 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 return( ret );
2642 }
2643
2644 i += n;
2645 }
2646
Paul Bakker48916f92012-09-16 19:57:18 +00002647 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
2649 if( ssl->authmode != SSL_VERIFY_NONE )
2650 {
2651 if( ssl->ca_chain == NULL )
2652 {
2653 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002654 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
Paul Bakkerddf26b42013-09-18 13:46:23 +02002657 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2658 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2659 &ssl->session_negotiate->verify_result,
2660 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
2662 if( ret != 0 )
2663 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2664
2665 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2666 ret = 0;
2667 }
2668
2669 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2670
2671 return( ret );
2672}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002673#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2674 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2675 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2676 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2677 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2678 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2679 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
2681int ssl_write_change_cipher_spec( ssl_context *ssl )
2682{
2683 int ret;
2684
2685 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2686
2687 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2688 ssl->out_msglen = 1;
2689 ssl->out_msg[0] = 1;
2690
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 ssl->state++;
2692
2693 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2694 {
2695 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2696 return( ret );
2697 }
2698
2699 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2700
2701 return( 0 );
2702}
2703
2704int ssl_parse_change_cipher_spec( ssl_context *ssl )
2705{
2706 int ret;
2707
2708 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2709
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2711 {
2712 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2713 return( ret );
2714 }
2715
2716 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2717 {
2718 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002719 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
2722 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2723 {
2724 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002725 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
2728 ssl->state++;
2729
2730 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2731
2732 return( 0 );
2733}
2734
Paul Bakker41c83d32013-03-20 14:39:14 +01002735void ssl_optimize_checksum( ssl_context *ssl,
2736 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002737{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002738 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002739
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002740#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2741 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002742 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002743 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002744 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002745#endif
2746#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2747#if defined(POLARSSL_SHA512_C)
2748 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2749 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2750 else
2751#endif
2752#if defined(POLARSSL_SHA256_C)
2753 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002754 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002755 else
2756#endif
2757#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2758 /* Should never happen */
2759 return;
Paul Bakker380da532012-04-18 16:10:25 +00002760}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002761
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002762static void ssl_update_checksum_start( ssl_context *ssl,
2763 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002764{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002765#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2766 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002767 md5_update( &ssl->handshake->fin_md5 , buf, len );
2768 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002769#endif
2770#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2771#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002772 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002773#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002774#if defined(POLARSSL_SHA512_C)
2775 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002776#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002777#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002778}
2779
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002780#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2781 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002782static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2783 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002784{
Paul Bakker48916f92012-09-16 19:57:18 +00002785 md5_update( &ssl->handshake->fin_md5 , buf, len );
2786 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002787}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002788#endif
Paul Bakker380da532012-04-18 16:10:25 +00002789
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002790#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2791#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002792static void ssl_update_checksum_sha256( ssl_context *ssl,
2793 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002794{
Paul Bakker9e36f042013-06-30 14:34:05 +02002795 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002796}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002797#endif
Paul Bakker380da532012-04-18 16:10:25 +00002798
Paul Bakker9e36f042013-06-30 14:34:05 +02002799#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002800static void ssl_update_checksum_sha384( ssl_context *ssl,
2801 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002802{
Paul Bakker9e36f042013-06-30 14:34:05 +02002803 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002804}
Paul Bakker769075d2012-11-24 11:26:46 +01002805#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002806#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002807
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002808#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002809static void ssl_calc_finished_ssl(
2810 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002811{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002812 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002813 md5_context md5;
2814 sha1_context sha1;
2815
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 unsigned char padbuf[48];
2817 unsigned char md5sum[16];
2818 unsigned char sha1sum[20];
2819
Paul Bakker48916f92012-09-16 19:57:18 +00002820 ssl_session *session = ssl->session_negotiate;
2821 if( !session )
2822 session = ssl->session;
2823
Paul Bakker1ef83d62012-04-11 12:09:53 +00002824 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2825
Paul Bakker48916f92012-09-16 19:57:18 +00002826 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2827 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
2829 /*
2830 * SSLv3:
2831 * hash =
2832 * MD5( master + pad2 +
2833 * MD5( handshake + sender + master + pad1 ) )
2834 * + SHA1( master + pad2 +
2835 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 */
2837
Paul Bakker90995b52013-06-24 19:20:35 +02002838#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002840 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002841#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Paul Bakker90995b52013-06-24 19:20:35 +02002843#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002845 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002846#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Paul Bakker3c2122f2013-06-24 19:03:14 +02002848 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2849 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
Paul Bakker1ef83d62012-04-11 12:09:53 +00002851 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
Paul Bakker3c2122f2013-06-24 19:03:14 +02002853 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002854 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002855 md5_update( &md5, padbuf, 48 );
2856 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002857
Paul Bakker3c2122f2013-06-24 19:03:14 +02002858 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002859 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002860 sha1_update( &sha1, padbuf, 40 );
2861 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Paul Bakker1ef83d62012-04-11 12:09:53 +00002863 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002866 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002867 md5_update( &md5, padbuf, 48 );
2868 md5_update( &md5, md5sum, 16 );
2869 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002872 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 sha1_update( &sha1, padbuf , 40 );
2874 sha1_update( &sha1, sha1sum, 20 );
2875 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Paul Bakker1ef83d62012-04-11 12:09:53 +00002877 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 memset( &md5, 0, sizeof( md5_context ) );
2880 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
2882 memset( padbuf, 0, sizeof( padbuf ) );
2883 memset( md5sum, 0, sizeof( md5sum ) );
2884 memset( sha1sum, 0, sizeof( sha1sum ) );
2885
2886 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2887}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002888#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002889
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002890#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002891static void ssl_calc_finished_tls(
2892 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002893{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002895 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
Paul Bakker48916f92012-09-16 19:57:18 +00002900 ssl_session *session = ssl->session_negotiate;
2901 if( !session )
2902 session = ssl->session;
2903
Paul Bakker1ef83d62012-04-11 12:09:53 +00002904 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Paul Bakker48916f92012-09-16 19:57:18 +00002906 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2907 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909 /*
2910 * TLSv1:
2911 * hash = PRF( master, finished_label,
2912 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2913 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Paul Bakker90995b52013-06-24 19:20:35 +02002915#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002916 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2917 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002918#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002919
Paul Bakker90995b52013-06-24 19:20:35 +02002920#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002921 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2922 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002923#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924
2925 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002926 ? "client finished"
2927 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002928
2929 md5_finish( &md5, padbuf );
2930 sha1_finish( &sha1, padbuf + 16 );
2931
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002932 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002933 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002934
2935 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2936
2937 memset( &md5, 0, sizeof( md5_context ) );
2938 memset( &sha1, 0, sizeof( sha1_context ) );
2939
2940 memset( padbuf, 0, sizeof( padbuf ) );
2941
2942 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2943}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002944#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2947#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002948static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949 ssl_context *ssl, unsigned char *buf, int from )
2950{
2951 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002952 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002953 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954 unsigned char padbuf[32];
2955
Paul Bakker48916f92012-09-16 19:57:18 +00002956 ssl_session *session = ssl->session_negotiate;
2957 if( !session )
2958 session = ssl->session;
2959
Paul Bakker380da532012-04-18 16:10:25 +00002960 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002961
Paul Bakker9e36f042013-06-30 14:34:05 +02002962 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963
2964 /*
2965 * TLSv1.2:
2966 * hash = PRF( master, finished_label,
2967 * Hash( handshake ) )[0.11]
2968 */
2969
Paul Bakker9e36f042013-06-30 14:34:05 +02002970#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002971 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002972 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002973#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974
2975 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002976 ? "client finished"
2977 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002978
Paul Bakker9e36f042013-06-30 14:34:05 +02002979 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002980
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002981 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002982 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002983
2984 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2985
Paul Bakker9e36f042013-06-30 14:34:05 +02002986 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002987
2988 memset( padbuf, 0, sizeof( padbuf ) );
2989
2990 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2991}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002992#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002993
Paul Bakker9e36f042013-06-30 14:34:05 +02002994#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002995static void ssl_calc_finished_tls_sha384(
2996 ssl_context *ssl, unsigned char *buf, int from )
2997{
2998 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002999 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003000 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003001 unsigned char padbuf[48];
3002
Paul Bakker48916f92012-09-16 19:57:18 +00003003 ssl_session *session = ssl->session_negotiate;
3004 if( !session )
3005 session = ssl->session;
3006
Paul Bakker380da532012-04-18 16:10:25 +00003007 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003008
Paul Bakker9e36f042013-06-30 14:34:05 +02003009 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003010
3011 /*
3012 * TLSv1.2:
3013 * hash = PRF( master, finished_label,
3014 * Hash( handshake ) )[0.11]
3015 */
3016
Paul Bakker9e36f042013-06-30 14:34:05 +02003017#if !defined(POLARSSL_SHA512_ALT)
3018 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3019 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003020#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003021
3022 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003023 ? "client finished"
3024 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003025
Paul Bakker9e36f042013-06-30 14:34:05 +02003026 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003027
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003028 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003029 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003030
3031 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3032
Paul Bakker9e36f042013-06-30 14:34:05 +02003033 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003034
3035 memset( padbuf, 0, sizeof( padbuf ) );
3036
3037 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3038}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003039#endif /* POLARSSL_SHA512_C */
3040#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003041
Paul Bakker48916f92012-09-16 19:57:18 +00003042void ssl_handshake_wrapup( ssl_context *ssl )
3043{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003044 int resume = ssl->handshake->resume;
3045
Paul Bakker48916f92012-09-16 19:57:18 +00003046 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3047
3048 /*
3049 * Free our handshake params
3050 */
3051 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003052 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003053 ssl->handshake = NULL;
3054
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003055 if( ssl->renegotiation == SSL_RENEGOTIATION )
3056 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3057
Paul Bakker48916f92012-09-16 19:57:18 +00003058 /*
3059 * Switch in our now active transform context
3060 */
3061 if( ssl->transform )
3062 {
3063 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003064 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003065 }
3066 ssl->transform = ssl->transform_negotiate;
3067 ssl->transform_negotiate = NULL;
3068
Paul Bakker0a597072012-09-25 21:55:46 +00003069 if( ssl->session )
3070 {
3071 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003072 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003073 }
3074 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003075 ssl->session_negotiate = NULL;
3076
Paul Bakker0a597072012-09-25 21:55:46 +00003077 /*
3078 * Add cache entry
3079 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003080 if( ssl->f_set_cache != NULL &&
3081 ssl->session->length != 0 &&
3082 resume == 0 )
3083 {
Paul Bakker0a597072012-09-25 21:55:46 +00003084 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3085 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003086 }
Paul Bakker0a597072012-09-25 21:55:46 +00003087
Paul Bakker48916f92012-09-16 19:57:18 +00003088 ssl->state++;
3089
3090 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3091}
3092
Paul Bakker1ef83d62012-04-11 12:09:53 +00003093int ssl_write_finished( ssl_context *ssl )
3094{
3095 int ret, hash_len;
3096
3097 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3098
Paul Bakker92be97b2013-01-02 17:30:03 +01003099 /*
3100 * Set the out_msg pointer to the correct location based on IV length
3101 */
3102 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3103 {
3104 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3105 ssl->transform_negotiate->fixed_ivlen;
3106 }
3107 else
3108 ssl->out_msg = ssl->out_iv;
3109
Paul Bakker48916f92012-09-16 19:57:18 +00003110 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003111
3112 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3114
Paul Bakker48916f92012-09-16 19:57:18 +00003115 ssl->verify_data_len = hash_len;
3116 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3117
Paul Bakker5121ce52009-01-03 21:22:43 +00003118 ssl->out_msglen = 4 + hash_len;
3119 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3120 ssl->out_msg[0] = SSL_HS_FINISHED;
3121
3122 /*
3123 * In case of session resuming, invert the client and server
3124 * ChangeCipherSpec messages order.
3125 */
Paul Bakker0a597072012-09-25 21:55:46 +00003126 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003127 {
3128 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003129 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 else
3131 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3132 }
3133 else
3134 ssl->state++;
3135
Paul Bakker48916f92012-09-16 19:57:18 +00003136 /*
3137 * Switch to our negotiated transform and session parameters for outbound data.
3138 */
3139 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3140 ssl->transform_out = ssl->transform_negotiate;
3141 ssl->session_out = ssl->session_negotiate;
3142 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
Paul Bakker07eb38b2012-12-19 14:42:06 +01003144#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3145 if( ssl_hw_record_activate != NULL)
3146 {
3147 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3148 {
3149 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3150 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3151 }
3152 }
3153#endif
3154
Paul Bakker5121ce52009-01-03 21:22:43 +00003155 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3156 {
3157 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3158 return( ret );
3159 }
3160
3161 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3162
3163 return( 0 );
3164}
3165
3166int ssl_parse_finished( ssl_context *ssl )
3167{
Paul Bakker23986e52011-04-24 08:57:21 +00003168 int ret;
3169 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003170 unsigned char buf[36];
3171
3172 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3173
Paul Bakker48916f92012-09-16 19:57:18 +00003174 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003175
Paul Bakker48916f92012-09-16 19:57:18 +00003176 /*
3177 * Switch to our negotiated transform and session parameters for inbound data.
3178 */
3179 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3180 ssl->transform_in = ssl->transform_negotiate;
3181 ssl->session_in = ssl->session_negotiate;
3182 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003183
Paul Bakker92be97b2013-01-02 17:30:03 +01003184 /*
3185 * Set the in_msg pointer to the correct location based on IV length
3186 */
3187 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3188 {
3189 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3190 ssl->transform_negotiate->fixed_ivlen;
3191 }
3192 else
3193 ssl->in_msg = ssl->in_iv;
3194
Paul Bakker07eb38b2012-12-19 14:42:06 +01003195#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3196 if( ssl_hw_record_activate != NULL)
3197 {
3198 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3199 {
3200 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3201 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3202 }
3203 }
3204#endif
3205
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3207 {
3208 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3209 return( ret );
3210 }
3211
3212 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3213 {
3214 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003215 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 }
3217
Paul Bakker1ef83d62012-04-11 12:09:53 +00003218 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3220
3221 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3222 ssl->in_hslen != 4 + hash_len )
3223 {
3224 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003225 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 }
3227
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003228 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 {
3230 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003231 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 }
3233
Paul Bakker48916f92012-09-16 19:57:18 +00003234 ssl->verify_data_len = hash_len;
3235 memcpy( ssl->peer_verify_data, buf, hash_len );
3236
Paul Bakker0a597072012-09-25 21:55:46 +00003237 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 {
3239 if( ssl->endpoint == SSL_IS_CLIENT )
3240 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3241
3242 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003243 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 }
3245 else
3246 ssl->state++;
3247
3248 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3249
3250 return( 0 );
3251}
3252
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003253static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003254{
3255 if( ssl->transform_negotiate )
3256 ssl_transform_free( ssl->transform_negotiate );
3257 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003258 {
3259 ssl->transform_negotiate =
3260 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3261 }
Paul Bakker48916f92012-09-16 19:57:18 +00003262
3263 if( ssl->session_negotiate )
3264 ssl_session_free( ssl->session_negotiate );
3265 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003266 {
3267 ssl->session_negotiate =
3268 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3269 }
Paul Bakker48916f92012-09-16 19:57:18 +00003270
3271 if( ssl->handshake )
3272 ssl_handshake_free( ssl->handshake );
3273 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003274 {
3275 ssl->handshake = (ssl_handshake_params *)
3276 polarssl_malloc( sizeof(ssl_handshake_params) );
3277 }
Paul Bakker48916f92012-09-16 19:57:18 +00003278
3279 if( ssl->handshake == NULL ||
3280 ssl->transform_negotiate == NULL ||
3281 ssl->session_negotiate == NULL )
3282 {
3283 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3284 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3285 }
3286
3287 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3288 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3289 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3290
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003291#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3292 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003293 md5_starts( &ssl->handshake->fin_md5 );
3294 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003295#endif
3296#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3297#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003298 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003299#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003300#if defined(POLARSSL_SHA512_C)
3301 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003302#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003303#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003304
3305 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003306 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003307
Paul Bakker61d113b2013-07-04 11:51:43 +02003308#if defined(POLARSSL_ECDH_C)
3309 ecdh_init( &ssl->handshake->ecdh_ctx );
3310#endif
3311
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003312#if defined(POLARSSL_X509_CRT_PARSE_C)
3313 ssl->handshake->key_cert = ssl->key_cert;
3314#endif
3315
Paul Bakker48916f92012-09-16 19:57:18 +00003316 return( 0 );
3317}
3318
Paul Bakker5121ce52009-01-03 21:22:43 +00003319/*
3320 * Initialize an SSL context
3321 */
3322int ssl_init( ssl_context *ssl )
3323{
Paul Bakker48916f92012-09-16 19:57:18 +00003324 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 int len = SSL_BUFFER_LEN;
3326
3327 memset( ssl, 0, sizeof( ssl_context ) );
3328
Paul Bakker62f2dee2012-09-28 07:31:51 +00003329 /*
3330 * Sane defaults
3331 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003332 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3333 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3334 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3335 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003336
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003337 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003338
Paul Bakker62f2dee2012-09-28 07:31:51 +00003339#if defined(POLARSSL_DHM_C)
3340 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3341 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3342 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3343 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3344 {
3345 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3346 return( ret );
3347 }
3348#endif
3349
3350 /*
3351 * Prepare base structures
3352 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003353 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003354 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003355 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003356 ssl->in_msg = ssl->in_ctr + 13;
3357
3358 if( ssl->in_ctr == NULL )
3359 {
3360 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003361 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003362 }
3363
Paul Bakker6e339b52013-07-03 13:37:05 +02003364 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003365 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003366 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003367 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003368
3369 if( ssl->out_ctr == NULL )
3370 {
3371 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003372 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003373 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 }
3375
3376 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3377 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3378
Paul Bakker606b4ba2013-08-14 16:52:14 +02003379#if defined(POLARSSL_SSL_SESSION_TICKETS)
3380 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3381#endif
3382
Paul Bakker48916f92012-09-16 19:57:18 +00003383 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3384 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003385
3386 return( 0 );
3387}
3388
3389/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003390 * Reset an initialized and used SSL context for re-use while retaining
3391 * all application-set variables, function pointers and data.
3392 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003393int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003394{
Paul Bakker48916f92012-09-16 19:57:18 +00003395 int ret;
3396
Paul Bakker7eb013f2011-10-06 12:37:39 +00003397 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003398 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3399 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3400
3401 ssl->verify_data_len = 0;
3402 memset( ssl->own_verify_data, 0, 36 );
3403 memset( ssl->peer_verify_data, 0, 36 );
3404
Paul Bakker7eb013f2011-10-06 12:37:39 +00003405 ssl->in_offt = NULL;
3406
Paul Bakker92be97b2013-01-02 17:30:03 +01003407 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003408 ssl->in_msgtype = 0;
3409 ssl->in_msglen = 0;
3410 ssl->in_left = 0;
3411
3412 ssl->in_hslen = 0;
3413 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003414 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003415
Paul Bakker92be97b2013-01-02 17:30:03 +01003416 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003417 ssl->out_msgtype = 0;
3418 ssl->out_msglen = 0;
3419 ssl->out_left = 0;
3420
Paul Bakker48916f92012-09-16 19:57:18 +00003421 ssl->transform_in = NULL;
3422 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003423
3424 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3425 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003426
3427#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3428 if( ssl_hw_record_reset != NULL)
3429 {
3430 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003431 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003432 {
3433 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3434 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3435 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003436 }
3437#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003438
Paul Bakker48916f92012-09-16 19:57:18 +00003439 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003440 {
Paul Bakker48916f92012-09-16 19:57:18 +00003441 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003442 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003443 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003444 }
Paul Bakker48916f92012-09-16 19:57:18 +00003445
Paul Bakkerc0463502013-02-14 11:19:38 +01003446 if( ssl->session )
3447 {
3448 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003449 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003450 ssl->session = NULL;
3451 }
3452
Paul Bakker48916f92012-09-16 19:57:18 +00003453 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3454 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003455
3456 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003457}
3458
Paul Bakkera503a632013-08-14 13:48:06 +02003459#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003460/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003461 * Allocate and initialize ticket keys
3462 */
3463static int ssl_ticket_keys_init( ssl_context *ssl )
3464{
3465 int ret;
3466 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003467 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003468
3469 if( ssl->ticket_keys != NULL )
3470 return( 0 );
3471
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003472 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3473 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003474 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3475
3476 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003477 {
3478 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003479 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003480 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003481
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003482 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3483 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3484 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3485 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003486 polarssl_free( tkeys );
3487 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003488 }
3489
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003490 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003491 {
3492 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003493 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003494 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003495
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003496 ssl->ticket_keys = tkeys;
3497
3498 return( 0 );
3499}
Paul Bakkera503a632013-08-14 13:48:06 +02003500#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003501
3502/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003503 * SSL set accessors
3504 */
3505void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3506{
3507 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003508
Paul Bakker606b4ba2013-08-14 16:52:14 +02003509#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003510 if( endpoint == SSL_IS_CLIENT )
3511 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003512#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003513}
3514
3515void ssl_set_authmode( ssl_context *ssl, int authmode )
3516{
3517 ssl->authmode = authmode;
3518}
3519
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003520#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003521void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003522 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003523 void *p_vrfy )
3524{
3525 ssl->f_vrfy = f_vrfy;
3526 ssl->p_vrfy = p_vrfy;
3527}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003528#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003529
Paul Bakker5121ce52009-01-03 21:22:43 +00003530void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003531 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003532 void *p_rng )
3533{
3534 ssl->f_rng = f_rng;
3535 ssl->p_rng = p_rng;
3536}
3537
3538void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003539 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003540 void *p_dbg )
3541{
3542 ssl->f_dbg = f_dbg;
3543 ssl->p_dbg = p_dbg;
3544}
3545
3546void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003547 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003548 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003549{
3550 ssl->f_recv = f_recv;
3551 ssl->f_send = f_send;
3552 ssl->p_recv = p_recv;
3553 ssl->p_send = p_send;
3554}
3555
Paul Bakker0a597072012-09-25 21:55:46 +00003556void ssl_set_session_cache( ssl_context *ssl,
3557 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3558 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003559{
Paul Bakker0a597072012-09-25 21:55:46 +00003560 ssl->f_get_cache = f_get_cache;
3561 ssl->p_get_cache = p_get_cache;
3562 ssl->f_set_cache = f_set_cache;
3563 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003564}
3565
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003566int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003567{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003568 int ret;
3569
3570 if( ssl == NULL ||
3571 session == NULL ||
3572 ssl->session_negotiate == NULL ||
3573 ssl->endpoint != SSL_IS_CLIENT )
3574 {
3575 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3576 }
3577
3578 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3579 return( ret );
3580
Paul Bakker0a597072012-09-25 21:55:46 +00003581 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003582
3583 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003584}
3585
Paul Bakkerb68cad62012-08-23 08:34:18 +00003586void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003587{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003588 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3589 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3590 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3591 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3592}
3593
3594void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3595 int major, int minor )
3596{
3597 if( major != SSL_MAJOR_VERSION_3 )
3598 return;
3599
3600 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3601 return;
3602
3603 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003604}
3605
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003606#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003607/* Add a new (empty) key_cert entry an return a pointer to it */
3608static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3609{
3610 ssl_key_cert *key_cert, *last;
3611
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003612 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3613 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003614 return( NULL );
3615
3616 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3617
3618 /* Append the new key_cert to the (possibly empty) current list */
3619 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003620 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003621 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003622 if( ssl->handshake != NULL )
3623 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003624 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003625 else
3626 {
3627 last = ssl->key_cert;
3628 while( last->next != NULL )
3629 last = last->next;
3630 last->next = key_cert;
3631 }
3632
3633 return key_cert;
3634}
3635
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003636void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003637 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003638{
3639 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003640 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 ssl->peer_cn = peer_cn;
3642}
3643
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003644int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003645 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003646{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003647 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3648
3649 if( key_cert == NULL )
3650 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3651
3652 key_cert->cert = own_cert;
3653 key_cert->key = pk_key;
3654
3655 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003656}
3657
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003658#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003659int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003660 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003661{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003662 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003663 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003664
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003665 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003666 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3667
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003668 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3669 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003670 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003671
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003672 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003673
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003674 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003675 if( ret != 0 )
3676 return( ret );
3677
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003678 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003679 return( ret );
3680
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003681 key_cert->cert = own_cert;
3682 key_cert->key_own_alloc = 1;
3683
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003684 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003685}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003686#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003688int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003689 void *rsa_key,
3690 rsa_decrypt_func rsa_decrypt,
3691 rsa_sign_func rsa_sign,
3692 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003693{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003694 int ret;
3695 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003696
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003697 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003698 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3699
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003700 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3701 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003702 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003703
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003704 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003705
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003706 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3707 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3708 return( ret );
3709
3710 key_cert->cert = own_cert;
3711 key_cert->key_own_alloc = 1;
3712
3713 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003714}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003715#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003716
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003717#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003718int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3719 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003720{
Paul Bakker6db455e2013-09-18 17:29:31 +02003721 if( psk == NULL || psk_identity == NULL )
3722 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3723
3724 if( ssl->psk != NULL )
3725 {
3726 polarssl_free( ssl->psk );
3727 polarssl_free( ssl->psk_identity );
3728 }
3729
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003730 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003731 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003732
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003733 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3734 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003735
3736 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3737 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3738
3739 memcpy( ssl->psk, psk, ssl->psk_len );
3740 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003741
3742 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003743}
3744
3745void ssl_set_psk_cb( ssl_context *ssl,
3746 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3747 size_t),
3748 void *p_psk )
3749{
3750 ssl->f_psk = f_psk;
3751 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003752}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003753#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003754
Paul Bakker48916f92012-09-16 19:57:18 +00003755#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003756int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003757{
3758 int ret;
3759
Paul Bakker48916f92012-09-16 19:57:18 +00003760 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003761 {
3762 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3763 return( ret );
3764 }
3765
Paul Bakker48916f92012-09-16 19:57:18 +00003766 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 {
3768 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3769 return( ret );
3770 }
3771
3772 return( 0 );
3773}
3774
Paul Bakker1b57b062011-01-06 15:48:19 +00003775int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3776{
3777 int ret;
3778
Paul Bakker48916f92012-09-16 19:57:18 +00003779 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003780 {
3781 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3782 return( ret );
3783 }
3784
Paul Bakker48916f92012-09-16 19:57:18 +00003785 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003786 {
3787 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3788 return( ret );
3789 }
3790
3791 return( 0 );
3792}
Paul Bakker48916f92012-09-16 19:57:18 +00003793#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003794
Paul Bakker0be444a2013-08-27 21:55:01 +02003795#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003796int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003797{
3798 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003799 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003800
3801 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003802
3803 if( ssl->hostname_len + 1 == 0 )
3804 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3805
Paul Bakker6e339b52013-07-03 13:37:05 +02003806 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003807
Paul Bakkerb15b8512012-01-13 13:44:06 +00003808 if( ssl->hostname == NULL )
3809 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3810
Paul Bakker3c2122f2013-06-24 19:03:14 +02003811 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003812 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003813
Paul Bakker40ea7de2009-05-03 10:18:48 +00003814 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003815
3816 return( 0 );
3817}
3818
Paul Bakker5701cdc2012-09-27 21:49:42 +00003819void ssl_set_sni( ssl_context *ssl,
3820 int (*f_sni)(void *, ssl_context *,
3821 const unsigned char *, size_t),
3822 void *p_sni )
3823{
3824 ssl->f_sni = f_sni;
3825 ssl->p_sni = p_sni;
3826}
Paul Bakker0be444a2013-08-27 21:55:01 +02003827#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003828
Paul Bakker490ecc82011-10-06 13:04:09 +00003829void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3830{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003831 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3832 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3833 {
3834 ssl->max_major_ver = major;
3835 ssl->max_minor_ver = minor;
3836 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003837}
3838
Paul Bakker1d29fb52012-09-28 13:28:45 +00003839void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3840{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003841 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3842 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3843 {
3844 ssl->min_major_ver = major;
3845 ssl->min_minor_ver = minor;
3846 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003847}
3848
Paul Bakker05decb22013-08-15 13:33:48 +02003849#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003850int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3851{
Paul Bakker77e257e2013-12-16 15:29:52 +01003852 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003853 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003854 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003855 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003856 }
3857
3858 ssl->mfl_code = mfl_code;
3859
3860 return( 0 );
3861}
Paul Bakker05decb22013-08-15 13:33:48 +02003862#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003863
Paul Bakker1f2bc622013-08-15 13:45:55 +02003864#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003865int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003866{
3867 if( ssl->endpoint != SSL_IS_CLIENT )
3868 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3869
Paul Bakker8c1ede62013-07-19 14:14:37 +02003870 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003871
3872 return( 0 );
3873}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003874#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003875
Paul Bakker48916f92012-09-16 19:57:18 +00003876void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3877{
3878 ssl->disable_renegotiation = renegotiation;
3879}
3880
3881void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3882{
3883 ssl->allow_legacy_renegotiation = allow_legacy;
3884}
3885
Paul Bakkera503a632013-08-14 13:48:06 +02003886#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003887int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3888{
3889 ssl->session_tickets = use_tickets;
3890
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003891 if( ssl->endpoint == SSL_IS_CLIENT )
3892 return( 0 );
3893
3894 if( ssl->f_rng == NULL )
3895 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3896
3897 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003898}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003899
3900void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3901{
3902 ssl->ticket_lifetime = lifetime;
3903}
Paul Bakkera503a632013-08-14 13:48:06 +02003904#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003905
Paul Bakker5121ce52009-01-03 21:22:43 +00003906/*
3907 * SSL get accessors
3908 */
Paul Bakker23986e52011-04-24 08:57:21 +00003909size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003910{
3911 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3912}
3913
Paul Bakkerff60ee62010-03-16 21:09:09 +00003914int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003915{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003916 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003917}
3918
Paul Bakkere3166ce2011-01-27 17:40:50 +00003919const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003920{
Paul Bakker926c8e42013-03-06 10:23:34 +01003921 if( ssl == NULL || ssl->session == NULL )
3922 return NULL;
3923
Paul Bakkere3166ce2011-01-27 17:40:50 +00003924 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003925}
3926
Paul Bakker43ca69c2011-01-15 17:35:19 +00003927const char *ssl_get_version( const ssl_context *ssl )
3928{
3929 switch( ssl->minor_ver )
3930 {
3931 case SSL_MINOR_VERSION_0:
3932 return( "SSLv3.0" );
3933
3934 case SSL_MINOR_VERSION_1:
3935 return( "TLSv1.0" );
3936
3937 case SSL_MINOR_VERSION_2:
3938 return( "TLSv1.1" );
3939
Paul Bakker1ef83d62012-04-11 12:09:53 +00003940 case SSL_MINOR_VERSION_3:
3941 return( "TLSv1.2" );
3942
Paul Bakker43ca69c2011-01-15 17:35:19 +00003943 default:
3944 break;
3945 }
3946 return( "unknown" );
3947}
3948
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003949#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003950const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003951{
3952 if( ssl == NULL || ssl->session == NULL )
3953 return NULL;
3954
3955 return ssl->session->peer_cert;
3956}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003957#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003958
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003959int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3960{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003961 if( ssl == NULL ||
3962 dst == NULL ||
3963 ssl->session == NULL ||
3964 ssl->endpoint != SSL_IS_CLIENT )
3965 {
3966 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3967 }
3968
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003969 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003970}
3971
Paul Bakker5121ce52009-01-03 21:22:43 +00003972/*
Paul Bakker1961b702013-01-25 14:49:24 +01003973 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003974 */
Paul Bakker1961b702013-01-25 14:49:24 +01003975int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003976{
Paul Bakker40e46942009-01-03 21:51:57 +00003977 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003978
Paul Bakker40e46942009-01-03 21:51:57 +00003979#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003980 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003981 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003982#endif
3983
Paul Bakker40e46942009-01-03 21:51:57 +00003984#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003985 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003986 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003987#endif
3988
Paul Bakker1961b702013-01-25 14:49:24 +01003989 return( ret );
3990}
3991
3992/*
3993 * Perform the SSL handshake
3994 */
3995int ssl_handshake( ssl_context *ssl )
3996{
3997 int ret = 0;
3998
3999 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4000
4001 while( ssl->state != SSL_HANDSHAKE_OVER )
4002 {
4003 ret = ssl_handshake_step( ssl );
4004
4005 if( ret != 0 )
4006 break;
4007 }
4008
Paul Bakker5121ce52009-01-03 21:22:43 +00004009 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4010
4011 return( ret );
4012}
4013
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004014#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004015/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004016 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004017 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004018static int ssl_write_hello_request( ssl_context *ssl )
4019{
4020 int ret;
4021
4022 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4023
4024 ssl->out_msglen = 4;
4025 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4026 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4027
4028 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4029 {
4030 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4031 return( ret );
4032 }
4033
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004034 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4035
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004036 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4037
4038 return( 0 );
4039}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004040#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004041
4042/*
4043 * Actually renegotiate current connection, triggered by either:
4044 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004045 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004046 * - receiving any handshake message on server during ssl_read() after the
4047 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004048 * If the handshake doesn't complete due to waiting for I/O, it will continue
4049 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004050 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004051static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004052{
4053 int ret;
4054
4055 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4056
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004057 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4058 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004059
4060 ssl->state = SSL_HELLO_REQUEST;
4061 ssl->renegotiation = SSL_RENEGOTIATION;
4062
Paul Bakker48916f92012-09-16 19:57:18 +00004063 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4064 {
4065 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4066 return( ret );
4067 }
4068
4069 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4070
4071 return( 0 );
4072}
4073
4074/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004075 * Renegotiate current connection on client,
4076 * or request renegotiation on server
4077 */
4078int ssl_renegotiate( ssl_context *ssl )
4079{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004080 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004081
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004082#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004083 /* On server, just send the request */
4084 if( ssl->endpoint == SSL_IS_SERVER )
4085 {
4086 if( ssl->state != SSL_HANDSHAKE_OVER )
4087 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4088
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004089 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004090 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004091#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004092
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004093#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004094 /*
4095 * On client, either start the renegotiation process or,
4096 * if already in progress, continue the handshake
4097 */
4098 if( ssl->renegotiation != SSL_RENEGOTIATION )
4099 {
4100 if( ssl->state != SSL_HANDSHAKE_OVER )
4101 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4102
4103 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4104 {
4105 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4106 return( ret );
4107 }
4108 }
4109 else
4110 {
4111 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4112 {
4113 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4114 return( ret );
4115 }
4116 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004117#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004118
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004119 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004120}
4121
4122/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004123 * Receive application data decrypted from the SSL layer
4124 */
Paul Bakker23986e52011-04-24 08:57:21 +00004125int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004126{
Paul Bakker23986e52011-04-24 08:57:21 +00004127 int ret;
4128 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004129
4130 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4131
4132 if( ssl->state != SSL_HANDSHAKE_OVER )
4133 {
4134 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4135 {
4136 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4137 return( ret );
4138 }
4139 }
4140
4141 if( ssl->in_offt == NULL )
4142 {
4143 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4144 {
Paul Bakker831a7552011-05-18 13:32:51 +00004145 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4146 return( 0 );
4147
Paul Bakker5121ce52009-01-03 21:22:43 +00004148 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4149 return( ret );
4150 }
4151
4152 if( ssl->in_msglen == 0 &&
4153 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4154 {
4155 /*
4156 * OpenSSL sends empty messages to randomize the IV
4157 */
4158 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4159 {
Paul Bakker831a7552011-05-18 13:32:51 +00004160 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4161 return( 0 );
4162
Paul Bakker5121ce52009-01-03 21:22:43 +00004163 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4164 return( ret );
4165 }
4166 }
4167
Paul Bakker48916f92012-09-16 19:57:18 +00004168 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4169 {
4170 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4171
4172 if( ssl->endpoint == SSL_IS_CLIENT &&
4173 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4174 ssl->in_hslen != 4 ) )
4175 {
4176 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4177 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4178 }
4179
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004180 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4181 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4182 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004183 {
4184 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4185
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004186#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004187 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004188 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004189 /*
4190 * SSLv3 does not have a "no_renegotiation" alert
4191 */
4192 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4193 return( ret );
4194 }
4195 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004196#endif
4197#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4198 defined(POLARSSL_SSL_PROTO_TLS1_2)
4199 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004200 {
4201 if( ( ret = ssl_send_alert_message( ssl,
4202 SSL_ALERT_LEVEL_WARNING,
4203 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4204 {
4205 return( ret );
4206 }
Paul Bakker48916f92012-09-16 19:57:18 +00004207 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004208 else
4209#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004210 {
4211 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004212 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004213 }
Paul Bakker48916f92012-09-16 19:57:18 +00004214 }
4215 else
4216 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004217 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004218 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004219 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004220 return( ret );
4221 }
4222
4223 return( POLARSSL_ERR_NET_WANT_READ );
4224 }
4225 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004226 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4227 {
4228 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4229 "but not honored by client" ) );
4230 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4231 }
Paul Bakker48916f92012-09-16 19:57:18 +00004232 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004233 {
4234 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004235 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004236 }
4237
4238 ssl->in_offt = ssl->in_msg;
4239 }
4240
4241 n = ( len < ssl->in_msglen )
4242 ? len : ssl->in_msglen;
4243
4244 memcpy( buf, ssl->in_offt, n );
4245 ssl->in_msglen -= n;
4246
4247 if( ssl->in_msglen == 0 )
4248 /* all bytes consumed */
4249 ssl->in_offt = NULL;
4250 else
4251 /* more data available */
4252 ssl->in_offt += n;
4253
4254 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4255
Paul Bakker23986e52011-04-24 08:57:21 +00004256 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004257}
4258
4259/*
4260 * Send application data to be encrypted by the SSL layer
4261 */
Paul Bakker23986e52011-04-24 08:57:21 +00004262int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004263{
Paul Bakker23986e52011-04-24 08:57:21 +00004264 int ret;
4265 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004266 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004267
4268 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4269
4270 if( ssl->state != SSL_HANDSHAKE_OVER )
4271 {
4272 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4273 {
4274 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4275 return( ret );
4276 }
4277 }
4278
Paul Bakker05decb22013-08-15 13:33:48 +02004279#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004280 /*
4281 * Assume mfl_code is correct since it was checked when set
4282 */
4283 max_len = mfl_code_to_length[ssl->mfl_code];
4284
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004285 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004286 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004287 */
4288 if( ssl->session_out != NULL &&
4289 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4290 {
4291 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4292 }
Paul Bakker05decb22013-08-15 13:33:48 +02004293#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004294
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004295 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004296
Paul Bakker5121ce52009-01-03 21:22:43 +00004297 if( ssl->out_left != 0 )
4298 {
4299 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4300 {
4301 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4302 return( ret );
4303 }
4304 }
Paul Bakker887bd502011-06-08 13:10:54 +00004305 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004306 {
Paul Bakker887bd502011-06-08 13:10:54 +00004307 ssl->out_msglen = n;
4308 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4309 memcpy( ssl->out_msg, buf, n );
4310
4311 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4312 {
4313 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4314 return( ret );
4315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004316 }
4317
4318 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4319
Paul Bakker23986e52011-04-24 08:57:21 +00004320 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004321}
4322
4323/*
4324 * Notify the peer that the connection is being closed
4325 */
4326int ssl_close_notify( ssl_context *ssl )
4327{
4328 int ret;
4329
4330 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4331
4332 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4333 {
4334 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4335 return( ret );
4336 }
4337
4338 if( ssl->state == SSL_HANDSHAKE_OVER )
4339 {
Paul Bakker48916f92012-09-16 19:57:18 +00004340 if( ( ret = ssl_send_alert_message( ssl,
4341 SSL_ALERT_LEVEL_WARNING,
4342 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004343 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004344 return( ret );
4345 }
4346 }
4347
4348 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4349
4350 return( ret );
4351}
4352
Paul Bakker48916f92012-09-16 19:57:18 +00004353void ssl_transform_free( ssl_transform *transform )
4354{
4355#if defined(POLARSSL_ZLIB_SUPPORT)
4356 deflateEnd( &transform->ctx_deflate );
4357 inflateEnd( &transform->ctx_inflate );
4358#endif
4359
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004360 cipher_free_ctx( &transform->cipher_ctx_enc );
4361 cipher_free_ctx( &transform->cipher_ctx_dec );
4362
Paul Bakker61d113b2013-07-04 11:51:43 +02004363 md_free_ctx( &transform->md_ctx_enc );
4364 md_free_ctx( &transform->md_ctx_dec );
4365
Paul Bakker48916f92012-09-16 19:57:18 +00004366 memset( transform, 0, sizeof( ssl_transform ) );
4367}
4368
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004369#if defined(POLARSSL_X509_CRT_PARSE_C)
4370static void ssl_key_cert_free( ssl_key_cert *key_cert )
4371{
4372 ssl_key_cert *cur = key_cert, *next;
4373
4374 while( cur != NULL )
4375 {
4376 next = cur->next;
4377
4378 if( cur->key_own_alloc )
4379 {
4380 pk_free( cur->key );
4381 polarssl_free( cur->key );
4382 }
4383 polarssl_free( cur );
4384
4385 cur = next;
4386 }
4387}
4388#endif /* POLARSSL_X509_CRT_PARSE_C */
4389
Paul Bakker48916f92012-09-16 19:57:18 +00004390void ssl_handshake_free( ssl_handshake_params *handshake )
4391{
4392#if defined(POLARSSL_DHM_C)
4393 dhm_free( &handshake->dhm_ctx );
4394#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004395#if defined(POLARSSL_ECDH_C)
4396 ecdh_free( &handshake->ecdh_ctx );
4397#endif
4398
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004399#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004400 /* explicit void pointer cast for buggy MS compiler */
4401 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004402#endif
4403
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004404#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4405 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4406 /*
4407 * Free only the linked list wrapper, not the keys themselves
4408 * since the belong to the SNI callback
4409 */
4410 if( handshake->sni_key_cert != NULL )
4411 {
4412 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4413
4414 while( cur != NULL )
4415 {
4416 next = cur->next;
4417 polarssl_free( cur );
4418 cur = next;
4419 }
4420 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004421#endif
4422
Paul Bakker48916f92012-09-16 19:57:18 +00004423 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4424}
4425
4426void ssl_session_free( ssl_session *session )
4427{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004428#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004429 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004430 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004431 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004432 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004433 }
Paul Bakkered27a042013-04-18 22:46:23 +02004434#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004435
Paul Bakkera503a632013-08-14 13:48:06 +02004436#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004437 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004438#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004439
Paul Bakker0a597072012-09-25 21:55:46 +00004440 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004441}
4442
Paul Bakker5121ce52009-01-03 21:22:43 +00004443/*
4444 * Free an SSL context
4445 */
4446void ssl_free( ssl_context *ssl )
4447{
4448 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4449
Paul Bakker5121ce52009-01-03 21:22:43 +00004450 if( ssl->out_ctr != NULL )
4451 {
4452 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004453 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 }
4455
4456 if( ssl->in_ctr != NULL )
4457 {
4458 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004459 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004460 }
4461
Paul Bakker16770332013-10-11 09:59:44 +02004462#if defined(POLARSSL_ZLIB_SUPPORT)
4463 if( ssl->compress_buf != NULL )
4464 {
4465 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4466 polarssl_free( ssl->compress_buf );
4467 }
4468#endif
4469
Paul Bakker40e46942009-01-03 21:51:57 +00004470#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004471 mpi_free( &ssl->dhm_P );
4472 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004473#endif
4474
Paul Bakker48916f92012-09-16 19:57:18 +00004475 if( ssl->transform )
4476 {
4477 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004478 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004479 }
4480
4481 if( ssl->handshake )
4482 {
4483 ssl_handshake_free( ssl->handshake );
4484 ssl_transform_free( ssl->transform_negotiate );
4485 ssl_session_free( ssl->session_negotiate );
4486
Paul Bakker6e339b52013-07-03 13:37:05 +02004487 polarssl_free( ssl->handshake );
4488 polarssl_free( ssl->transform_negotiate );
4489 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004490 }
4491
Paul Bakkerc0463502013-02-14 11:19:38 +01004492 if( ssl->session )
4493 {
4494 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004495 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004496 }
4497
Paul Bakkera503a632013-08-14 13:48:06 +02004498#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004499 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004500#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004501
Paul Bakker0be444a2013-08-27 21:55:01 +02004502#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004503 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 {
4505 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004506 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004507 ssl->hostname_len = 0;
4508 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004510
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004511#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004512 if( ssl->psk != NULL )
4513 {
4514 memset( ssl->psk, 0, ssl->psk_len );
4515 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4516 polarssl_free( ssl->psk );
4517 polarssl_free( ssl->psk_identity );
4518 ssl->psk_len = 0;
4519 ssl->psk_identity_len = 0;
4520 }
4521#endif
4522
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004523#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004524 ssl_key_cert_free( ssl->key_cert );
4525#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004526
Paul Bakker05ef8352012-05-08 09:17:57 +00004527#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4528 if( ssl_hw_record_finish != NULL )
4529 {
4530 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4531 ssl_hw_record_finish( ssl );
4532 }
4533#endif
4534
Paul Bakker5121ce52009-01-03 21:22:43 +00004535 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004536
Paul Bakker86f04f42013-02-14 11:20:09 +01004537 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004538 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004539}
4540
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004541#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004542/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004543 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004544 */
4545unsigned char ssl_sig_from_pk( pk_context *pk )
4546{
4547#if defined(POLARSSL_RSA_C)
4548 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4549 return( SSL_SIG_RSA );
4550#endif
4551#if defined(POLARSSL_ECDSA_C)
4552 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4553 return( SSL_SIG_ECDSA );
4554#endif
4555 return( SSL_SIG_ANON );
4556}
4557
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004558pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4559{
4560 switch( sig )
4561 {
4562#if defined(POLARSSL_RSA_C)
4563 case SSL_SIG_RSA:
4564 return( POLARSSL_PK_RSA );
4565#endif
4566#if defined(POLARSSL_ECDSA_C)
4567 case SSL_SIG_ECDSA:
4568 return( POLARSSL_PK_ECDSA );
4569#endif
4570 default:
4571 return( POLARSSL_PK_NONE );
4572 }
4573}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004574#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004575
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004576/*
4577 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4578 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004579md_type_t ssl_md_alg_from_hash( unsigned char hash )
4580{
4581 switch( hash )
4582 {
4583#if defined(POLARSSL_MD5_C)
4584 case SSL_HASH_MD5:
4585 return( POLARSSL_MD_MD5 );
4586#endif
4587#if defined(POLARSSL_SHA1_C)
4588 case SSL_HASH_SHA1:
4589 return( POLARSSL_MD_SHA1 );
4590#endif
4591#if defined(POLARSSL_SHA256_C)
4592 case SSL_HASH_SHA224:
4593 return( POLARSSL_MD_SHA224 );
4594 case SSL_HASH_SHA256:
4595 return( POLARSSL_MD_SHA256 );
4596#endif
4597#if defined(POLARSSL_SHA512_C)
4598 case SSL_HASH_SHA384:
4599 return( POLARSSL_MD_SHA384 );
4600 case SSL_HASH_SHA512:
4601 return( POLARSSL_MD_SHA512 );
4602#endif
4603 default:
4604 return( POLARSSL_MD_NONE );
4605 }
4606}
4607
Paul Bakker5121ce52009-01-03 21:22:43 +00004608#endif