blob: 75ba9075a8fa7a321f417170a7847d08cc02e0ce [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 Bakkerca9c87e2013-09-25 18:52:37 +02001613 for( i = 1; i <= 256; i++ )
1614 {
1615 real_count &= ( i <= padlen );
1616 pad_count += real_count *
1617 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1618 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001619
1620 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001621
Paul Bakkerd66f0702013-01-31 16:57:45 +01001622#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001623 if( padlen > 0 && correct == 0)
1624 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001625#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001626 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001627 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001628 else
1629#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1630 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001631 {
1632 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001633 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001634 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001636 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001637#endif /* POLARSSL_CIPHER_MODE_CBC &&
1638 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001639 {
1640 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1641 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1642 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
1644 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1645 ssl->in_msg, ssl->in_msglen );
1646
1647 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001648 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001650#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1651 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1652 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1653 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1654 POLARSSL_MODE_GCM )
1655 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001656 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1657
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001658 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001660 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1661 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001665#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001666 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1667 {
1668 ssl_mac( &ssl->transform_in->md_ctx_dec,
1669 ssl->transform_in->mac_dec,
1670 ssl->in_msg, ssl->in_msglen,
1671 ssl->in_ctr, ssl->in_msgtype );
1672 }
1673 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001674#endif /* POLARSSL_SSL_PROTO_SSL3 */
1675#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 defined(POLARSSL_SSL_PROTO_TLS1_2)
1677 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1678 {
1679 /*
1680 * Process MAC and always update for padlen afterwards to make
1681 * total time independent of padlen
1682 *
1683 * extra_run compensates MAC check for padlen
1684 *
1685 * Known timing attacks:
1686 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1687 *
1688 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1689 * correctly. (We round down instead of up, so -56 is the correct
1690 * value for our calculations instead of -55)
1691 */
1692 size_t j, extra_run = 0;
1693 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1694 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001695
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001696 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001697
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001698 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1699 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1700 ssl->in_msglen );
1701 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1702 ssl->in_msg + ssl->in_msglen );
1703 for( j = 0; j < extra_run; j++ )
1704 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001705
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001706 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1707 }
1708 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001709#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001710 POLARSSL_SSL_PROTO_TLS1_2 */
1711 {
1712 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1713 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1714 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001716 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1717 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1718 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001719
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001720 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001721 ssl->transform_in->maclen ) != 0 )
1722 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001723#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001724 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001725#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001726 correct = 0;
1727 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001728
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001729 /*
1730 * Finally check the correct flag
1731 */
1732 if( correct == 0 )
1733 return( POLARSSL_ERR_SSL_INVALID_MAC );
1734 }
1735#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
1737 if( ssl->in_msglen == 0 )
1738 {
1739 ssl->nb_zero++;
1740
1741 /*
1742 * Three or more empty messages may be a DoS attack
1743 * (excessive CPU consumption).
1744 */
1745 if( ssl->nb_zero > 3 )
1746 {
1747 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1748 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001749 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750 }
1751 }
1752 else
1753 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001754
Paul Bakker23986e52011-04-24 08:57:21 +00001755 for( i = 8; i > 0; i-- )
1756 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 break;
1758
1759 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1760
1761 return( 0 );
1762}
1763
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764#if defined(POLARSSL_ZLIB_SUPPORT)
1765/*
1766 * Compression/decompression functions
1767 */
1768static int ssl_compress_buf( ssl_context *ssl )
1769{
1770 int ret;
1771 unsigned char *msg_post = ssl->out_msg;
1772 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001773 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001774
1775 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1776
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001777 if( len_pre == 0 )
1778 return( 0 );
1779
Paul Bakker2770fbd2012-07-03 13:30:23 +00001780 memcpy( msg_pre, ssl->out_msg, len_pre );
1781
1782 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1783 ssl->out_msglen ) );
1784
1785 SSL_DEBUG_BUF( 4, "before compression: output payload",
1786 ssl->out_msg, ssl->out_msglen );
1787
Paul Bakker48916f92012-09-16 19:57:18 +00001788 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1789 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1790 ssl->transform_out->ctx_deflate.next_out = msg_post;
1791 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001792
Paul Bakker48916f92012-09-16 19:57:18 +00001793 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001794 if( ret != Z_OK )
1795 {
1796 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1797 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1798 }
1799
Paul Bakker48916f92012-09-16 19:57:18 +00001800 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001801
Paul Bakker2770fbd2012-07-03 13:30:23 +00001802 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1803 ssl->out_msglen ) );
1804
1805 SSL_DEBUG_BUF( 4, "after compression: output payload",
1806 ssl->out_msg, ssl->out_msglen );
1807
1808 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1809
1810 return( 0 );
1811}
1812
1813static int ssl_decompress_buf( ssl_context *ssl )
1814{
1815 int ret;
1816 unsigned char *msg_post = ssl->in_msg;
1817 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001818 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001819
1820 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1821
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001822 if( len_pre == 0 )
1823 return( 0 );
1824
Paul Bakker2770fbd2012-07-03 13:30:23 +00001825 memcpy( msg_pre, ssl->in_msg, len_pre );
1826
1827 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1828 ssl->in_msglen ) );
1829
1830 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1831 ssl->in_msg, ssl->in_msglen );
1832
Paul Bakker48916f92012-09-16 19:57:18 +00001833 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1834 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1835 ssl->transform_in->ctx_inflate.next_out = msg_post;
1836 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001837
Paul Bakker48916f92012-09-16 19:57:18 +00001838 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001839 if( ret != Z_OK )
1840 {
1841 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1842 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1843 }
1844
Paul Bakker48916f92012-09-16 19:57:18 +00001845 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001846
Paul Bakker2770fbd2012-07-03 13:30:23 +00001847 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1848 ssl->in_msglen ) );
1849
1850 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1851 ssl->in_msg, ssl->in_msglen );
1852
1853 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1854
1855 return( 0 );
1856}
1857#endif /* POLARSSL_ZLIB_SUPPORT */
1858
Paul Bakker5121ce52009-01-03 21:22:43 +00001859/*
1860 * Fill the input message buffer
1861 */
Paul Bakker23986e52011-04-24 08:57:21 +00001862int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001863{
Paul Bakker23986e52011-04-24 08:57:21 +00001864 int ret;
1865 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001866
1867 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1868
1869 while( ssl->in_left < nb_want )
1870 {
1871 len = nb_want - ssl->in_left;
1872 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1873
1874 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1875 ssl->in_left, nb_want ) );
1876 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1877
Paul Bakker831a7552011-05-18 13:32:51 +00001878 if( ret == 0 )
1879 return( POLARSSL_ERR_SSL_CONN_EOF );
1880
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 if( ret < 0 )
1882 return( ret );
1883
1884 ssl->in_left += ret;
1885 }
1886
1887 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1888
1889 return( 0 );
1890}
1891
1892/*
1893 * Flush any data not yet written
1894 */
1895int ssl_flush_output( ssl_context *ssl )
1896{
1897 int ret;
1898 unsigned char *buf;
1899
1900 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1901
1902 while( ssl->out_left > 0 )
1903 {
1904 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1905 5 + ssl->out_msglen, ssl->out_left ) );
1906
Paul Bakker5bd42292012-12-19 14:40:42 +01001907 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001909
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1911
1912 if( ret <= 0 )
1913 return( ret );
1914
1915 ssl->out_left -= ret;
1916 }
1917
1918 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1919
1920 return( 0 );
1921}
1922
1923/*
1924 * Record layer functions
1925 */
1926int ssl_write_record( ssl_context *ssl )
1927{
Paul Bakker05ef8352012-05-08 09:17:57 +00001928 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001929 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001930
1931 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1932
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1934 {
1935 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1936 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1937 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1938
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001939 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1940 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 }
1942
Paul Bakker2770fbd2012-07-03 13:30:23 +00001943#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001944 if( ssl->transform_out != NULL &&
1945 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001946 {
1947 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1948 {
1949 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1950 return( ret );
1951 }
1952
1953 len = ssl->out_msglen;
1954 }
1955#endif /*POLARSSL_ZLIB_SUPPORT */
1956
Paul Bakker05ef8352012-05-08 09:17:57 +00001957#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1958 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001960 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001961
Paul Bakker05ef8352012-05-08 09:17:57 +00001962 ret = ssl_hw_record_write( ssl );
1963 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1964 {
1965 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1966 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1967 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001968
1969 if( ret == 0 )
1970 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001971 }
1972#endif
1973 if( !done )
1974 {
1975 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1976 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1977 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1979 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001980
Paul Bakker48916f92012-09-16 19:57:18 +00001981 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001982 {
1983 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1984 {
1985 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1986 return( ret );
1987 }
1988
1989 len = ssl->out_msglen;
1990 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1991 ssl->out_hdr[4] = (unsigned char)( len );
1992 }
1993
1994 ssl->out_left = 5 + ssl->out_msglen;
1995
1996 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1997 "version = [%d:%d], msglen = %d",
1998 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1999 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2000
2001 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002002 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 }
2004
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2006 {
2007 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2008 return( ret );
2009 }
2010
2011 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2012
2013 return( 0 );
2014}
2015
2016int ssl_read_record( ssl_context *ssl )
2017{
Paul Bakker05ef8352012-05-08 09:17:57 +00002018 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
2020 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2021
Paul Bakker68884e32013-01-07 18:20:04 +01002022 SSL_DEBUG_BUF( 4, "input record from network",
2023 ssl->in_hdr, 5 + ssl->in_msglen );
2024
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 if( ssl->in_hslen != 0 &&
2026 ssl->in_hslen < ssl->in_msglen )
2027 {
2028 /*
2029 * Get next Handshake message in the current record
2030 */
2031 ssl->in_msglen -= ssl->in_hslen;
2032
Paul Bakker8934a982011-08-05 11:11:53 +00002033 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002034 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035
2036 ssl->in_hslen = 4;
2037 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2038
2039 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2040 " %d, type = %d, hslen = %d",
2041 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2042
2043 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2044 {
2045 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002046 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 }
2048
2049 if( ssl->in_msglen < ssl->in_hslen )
2050 {
2051 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002052 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 }
2054
Paul Bakker48916f92012-09-16 19:57:18 +00002055 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
2057 return( 0 );
2058 }
2059
2060 ssl->in_hslen = 0;
2061
2062 /*
2063 * Read the record header and validate it
2064 */
2065 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2066 {
2067 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2068 return( ret );
2069 }
2070
2071 ssl->in_msgtype = ssl->in_hdr[0];
2072 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2073
2074 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2075 "version = [%d:%d], msglen = %d",
2076 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2077 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2078
2079 if( ssl->in_hdr[1] != ssl->major_ver )
2080 {
2081 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002082 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 }
2084
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002085 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 {
2087 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002088 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
2090
2091 /*
2092 * Make sure the message length is acceptable
2093 */
Paul Bakker48916f92012-09-16 19:57:18 +00002094 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 {
2096 if( ssl->in_msglen < 1 ||
2097 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2098 {
2099 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002100 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
2102 }
2103 else
2104 {
Paul Bakker48916f92012-09-16 19:57:18 +00002105 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 {
2107 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002108 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 }
2110
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002111#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002113 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 {
2115 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002116 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002118#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002120#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2121 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 /*
2123 * TLS encrypted messages can have up to 256 bytes of padding
2124 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002125 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002126 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 {
2128 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002129 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
2133
2134 /*
2135 * Read and optionally decrypt the message contents
2136 */
2137 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2138 {
2139 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2140 return( ret );
2141 }
2142
2143 SSL_DEBUG_BUF( 4, "input record from network",
2144 ssl->in_hdr, 5 + ssl->in_msglen );
2145
Paul Bakker05ef8352012-05-08 09:17:57 +00002146#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2147 if( ssl_hw_record_read != NULL)
2148 {
2149 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2150
2151 ret = ssl_hw_record_read( ssl );
2152 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2153 {
2154 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2155 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2156 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002157
2158 if( ret == 0 )
2159 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002160 }
2161#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002162 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 {
2164 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2165 {
Paul Bakker40865c82013-01-31 17:13:13 +01002166#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2167 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2168 {
2169 ssl_send_alert_message( ssl,
2170 SSL_ALERT_LEVEL_FATAL,
2171 SSL_ALERT_MSG_BAD_RECORD_MAC );
2172 }
2173#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2175 return( ret );
2176 }
2177
2178 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2179 ssl->in_msg, ssl->in_msglen );
2180
2181 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2182 {
2183 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002184 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 }
2186 }
2187
Paul Bakker2770fbd2012-07-03 13:30:23 +00002188#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002189 if( ssl->transform_in != NULL &&
2190 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002191 {
2192 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2193 {
2194 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2195 return( ret );
2196 }
2197
2198 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2199 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2200 }
2201#endif /* POLARSSL_ZLIB_SUPPORT */
2202
Paul Bakker0a925182012-04-16 06:46:41 +00002203 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2204 ssl->in_msgtype != SSL_MSG_ALERT &&
2205 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2206 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2207 {
2208 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2209
Paul Bakker48916f92012-09-16 19:57:18 +00002210 if( ( ret = ssl_send_alert_message( ssl,
2211 SSL_ALERT_LEVEL_FATAL,
2212 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002213 {
2214 return( ret );
2215 }
2216
2217 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2218 }
2219
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2221 {
2222 ssl->in_hslen = 4;
2223 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2224
2225 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2226 " %d, type = %d, hslen = %d",
2227 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2228
2229 /*
2230 * Additional checks to validate the handshake header
2231 */
2232 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2233 {
2234 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002235 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 }
2237
2238 if( ssl->in_msglen < ssl->in_hslen )
2239 {
2240 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002241 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 }
2243
Paul Bakker48916f92012-09-16 19:57:18 +00002244 if( ssl->state != SSL_HANDSHAKE_OVER )
2245 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 }
2247
2248 if( ssl->in_msgtype == SSL_MSG_ALERT )
2249 {
2250 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2251 ssl->in_msg[0], ssl->in_msg[1] ) );
2252
2253 /*
2254 * Ignore non-fatal alerts, except close_notify
2255 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002256 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002258 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2259 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002260 /**
2261 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2262 * error identifier.
2263 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002264 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
2266
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002267 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2268 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 {
2270 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002271 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 }
2273 }
2274
2275 ssl->in_left = 0;
2276
2277 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2278
2279 return( 0 );
2280}
2281
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002282int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2283{
2284 int ret;
2285
2286 if( ( ret = ssl_send_alert_message( ssl,
2287 SSL_ALERT_LEVEL_FATAL,
2288 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2289 {
2290 return( ret );
2291 }
2292
2293 return( 0 );
2294}
2295
Paul Bakker0a925182012-04-16 06:46:41 +00002296int ssl_send_alert_message( ssl_context *ssl,
2297 unsigned char level,
2298 unsigned char message )
2299{
2300 int ret;
2301
2302 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2303
2304 ssl->out_msgtype = SSL_MSG_ALERT;
2305 ssl->out_msglen = 2;
2306 ssl->out_msg[0] = level;
2307 ssl->out_msg[1] = message;
2308
2309 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2310 {
2311 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2312 return( ret );
2313 }
2314
2315 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2316
2317 return( 0 );
2318}
2319
Paul Bakker5121ce52009-01-03 21:22:43 +00002320/*
2321 * Handshake functions
2322 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002323#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2324 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2325 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2326 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2327 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2328 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2329 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002330int ssl_write_certificate( ssl_context *ssl )
2331{
Paul Bakkered27a042013-04-18 22:46:23 +02002332 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002333 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
2335 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2336
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002337 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002338 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2339 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002340 {
2341 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2342 ssl->state++;
2343 return( 0 );
2344 }
2345
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002346 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002347 return( ret );
2348}
2349
2350int ssl_parse_certificate( ssl_context *ssl )
2351{
2352 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2353 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2354
2355 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2356
2357 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002358 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2359 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002360 {
2361 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2362 ssl->state++;
2363 return( 0 );
2364 }
2365
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002366 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002367 return( ret );
2368}
2369#else
2370int ssl_write_certificate( ssl_context *ssl )
2371{
2372 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2373 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002374 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002375 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2376
2377 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2378
2379 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002380 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2381 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002382 {
2383 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2384 ssl->state++;
2385 return( 0 );
2386 }
2387
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 if( ssl->endpoint == SSL_IS_CLIENT )
2389 {
2390 if( ssl->client_auth == 0 )
2391 {
2392 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2393 ssl->state++;
2394 return( 0 );
2395 }
2396
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002397#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 /*
2399 * If using SSLv3 and got no cert, send an Alert message
2400 * (otherwise an empty Certificate message will be sent).
2401 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002402 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2404 {
2405 ssl->out_msglen = 2;
2406 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002407 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2408 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
2410 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2411 goto write_msg;
2412 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002413#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 }
2415 else /* SSL_IS_SERVER */
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 {
2419 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002420 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 }
2422 }
2423
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002424 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
2426 /*
2427 * 0 . 0 handshake type
2428 * 1 . 3 handshake length
2429 * 4 . 6 length of all certs
2430 * 7 . 9 length of cert. 1
2431 * 10 . n-1 peer certificate
2432 * n . n+2 length of cert. 2
2433 * n+3 . ... upper level cert, etc.
2434 */
2435 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002436 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
Paul Bakker29087132010-03-21 21:03:34 +00002438 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 {
2440 n = crt->raw.len;
2441 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
2442 {
2443 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2444 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002445 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 }
2447
2448 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2449 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2450 ssl->out_msg[i + 2] = (unsigned char)( n );
2451
2452 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2453 i += n; crt = crt->next;
2454 }
2455
2456 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2457 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2458 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2459
2460 ssl->out_msglen = i;
2461 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2462 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2463
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002464#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002465write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002466#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
2468 ssl->state++;
2469
2470 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2471 {
2472 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2473 return( ret );
2474 }
2475
2476 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2477
Paul Bakkered27a042013-04-18 22:46:23 +02002478 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479}
2480
2481int ssl_parse_certificate( ssl_context *ssl )
2482{
Paul Bakkered27a042013-04-18 22:46:23 +02002483 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002484 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002485 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
2487 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2488
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002489 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002490 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002492 {
2493 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2494 ssl->state++;
2495 return( 0 );
2496 }
2497
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002499 ( ssl->authmode == SSL_VERIFY_NONE ||
2500 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002502 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2504 ssl->state++;
2505 return( 0 );
2506 }
2507
2508 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2509 {
2510 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2511 return( ret );
2512 }
2513
2514 ssl->state++;
2515
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002516#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 /*
2518 * Check if the client sent an empty certificate
2519 */
2520 if( ssl->endpoint == SSL_IS_SERVER &&
2521 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2522 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002523 if( ssl->in_msglen == 2 &&
2524 ssl->in_msgtype == SSL_MSG_ALERT &&
2525 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2526 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 {
2528 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2529
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002530 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2532 return( 0 );
2533 else
Paul Bakker40e46942009-01-03 21:51:57 +00002534 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 }
2536 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002537#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002539#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2540 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 if( ssl->endpoint == SSL_IS_SERVER &&
2542 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2543 {
2544 if( ssl->in_hslen == 7 &&
2545 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2546 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2547 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2548 {
2549 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2550
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002551 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002553 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 else
2555 return( 0 );
2556 }
2557 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002558#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2559 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
2561 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2562 {
2563 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002564 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 }
2566
2567 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2568 {
2569 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002570 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 }
2572
2573 /*
2574 * Same message structure as in ssl_write_certificate()
2575 */
2576 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2577
2578 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2579 {
2580 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002581 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 }
2583
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002584 /* In case we tried to reuse a session but it failed */
2585 if( ssl->session_negotiate->peer_cert != NULL )
2586 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002587 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002588 polarssl_free( ssl->session_negotiate->peer_cert );
2589 }
2590
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002591 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2592 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 {
2594 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002595 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002596 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 }
2598
Paul Bakkerb6b09562013-09-18 14:17:41 +02002599 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
2601 i = 7;
2602
2603 while( i < ssl->in_hslen )
2604 {
2605 if( ssl->in_msg[i] != 0 )
2606 {
2607 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002608 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
2610
2611 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2612 | (unsigned int) ssl->in_msg[i + 2];
2613 i += 3;
2614
2615 if( n < 128 || i + n > ssl->in_hslen )
2616 {
2617 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002618 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620
Paul Bakkerddf26b42013-09-18 13:46:23 +02002621 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2622 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 if( ret != 0 )
2624 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002625 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 return( ret );
2627 }
2628
2629 i += n;
2630 }
2631
Paul Bakker48916f92012-09-16 19:57:18 +00002632 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
2634 if( ssl->authmode != SSL_VERIFY_NONE )
2635 {
2636 if( ssl->ca_chain == NULL )
2637 {
2638 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002639 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 }
2641
Paul Bakkerddf26b42013-09-18 13:46:23 +02002642 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2643 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2644 &ssl->session_negotiate->verify_result,
2645 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
2647 if( ret != 0 )
2648 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2649
2650 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2651 ret = 0;
2652 }
2653
2654 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2655
2656 return( ret );
2657}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002658#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2659 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2660 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2661 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2662 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2663 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2664 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
2666int ssl_write_change_cipher_spec( ssl_context *ssl )
2667{
2668 int ret;
2669
2670 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2671
2672 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2673 ssl->out_msglen = 1;
2674 ssl->out_msg[0] = 1;
2675
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 ssl->state++;
2677
2678 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2679 {
2680 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2681 return( ret );
2682 }
2683
2684 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2685
2686 return( 0 );
2687}
2688
2689int ssl_parse_change_cipher_spec( ssl_context *ssl )
2690{
2691 int ret;
2692
2693 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2694
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2696 {
2697 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2698 return( ret );
2699 }
2700
2701 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2702 {
2703 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002704 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 }
2706
2707 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2708 {
2709 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002710 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 }
2712
2713 ssl->state++;
2714
2715 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2716
2717 return( 0 );
2718}
2719
Paul Bakker41c83d32013-03-20 14:39:14 +01002720void ssl_optimize_checksum( ssl_context *ssl,
2721 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002722{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002723 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002724
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002725#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2726 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002727 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002728 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002729 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002730#endif
2731#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2732#if defined(POLARSSL_SHA512_C)
2733 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2734 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2735 else
2736#endif
2737#if defined(POLARSSL_SHA256_C)
2738 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002739 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002740 else
2741#endif
2742#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2743 /* Should never happen */
2744 return;
Paul Bakker380da532012-04-18 16:10:25 +00002745}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002746
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002747static void ssl_update_checksum_start( ssl_context *ssl,
2748 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002749{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002750#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2751 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002752 md5_update( &ssl->handshake->fin_md5 , buf, len );
2753 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002754#endif
2755#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2756#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002757 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002758#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002759#if defined(POLARSSL_SHA512_C)
2760 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002761#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002762#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002763}
2764
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 Bakkerb6c5d2e2013-06-25 16:25:17 +02002767static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2768 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002769{
Paul Bakker48916f92012-09-16 19:57:18 +00002770 md5_update( &ssl->handshake->fin_md5 , buf, len );
2771 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002772}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002773#endif
Paul Bakker380da532012-04-18 16:10:25 +00002774
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002775#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2776#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002777static void ssl_update_checksum_sha256( ssl_context *ssl,
2778 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002779{
Paul Bakker9e36f042013-06-30 14:34:05 +02002780 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002781}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002782#endif
Paul Bakker380da532012-04-18 16:10:25 +00002783
Paul Bakker9e36f042013-06-30 14:34:05 +02002784#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002785static void ssl_update_checksum_sha384( ssl_context *ssl,
2786 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002787{
Paul Bakker9e36f042013-06-30 14:34:05 +02002788 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002789}
Paul Bakker769075d2012-11-24 11:26:46 +01002790#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002791#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002792
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002793#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002794static void ssl_calc_finished_ssl(
2795 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002796{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002797 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002798 md5_context md5;
2799 sha1_context sha1;
2800
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 unsigned char padbuf[48];
2802 unsigned char md5sum[16];
2803 unsigned char sha1sum[20];
2804
Paul Bakker48916f92012-09-16 19:57:18 +00002805 ssl_session *session = ssl->session_negotiate;
2806 if( !session )
2807 session = ssl->session;
2808
Paul Bakker1ef83d62012-04-11 12:09:53 +00002809 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2810
Paul Bakker48916f92012-09-16 19:57:18 +00002811 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2812 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
2814 /*
2815 * SSLv3:
2816 * hash =
2817 * MD5( master + pad2 +
2818 * MD5( handshake + sender + master + pad1 ) )
2819 * + SHA1( master + pad2 +
2820 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 */
2822
Paul Bakker90995b52013-06-24 19:20:35 +02002823#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002825 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002826#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
Paul Bakker90995b52013-06-24 19:20:35 +02002828#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002830 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002831#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Paul Bakker3c2122f2013-06-24 19:03:14 +02002833 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2834 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Paul Bakker1ef83d62012-04-11 12:09:53 +00002836 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Paul Bakker3c2122f2013-06-24 19:03:14 +02002838 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002839 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002840 md5_update( &md5, padbuf, 48 );
2841 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Paul Bakker3c2122f2013-06-24 19:03:14 +02002843 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002844 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002845 sha1_update( &sha1, padbuf, 40 );
2846 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Paul Bakker1ef83d62012-04-11 12:09:53 +00002848 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Paul Bakker1ef83d62012-04-11 12:09:53 +00002850 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002851 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002852 md5_update( &md5, padbuf, 48 );
2853 md5_update( &md5, md5sum, 16 );
2854 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Paul Bakker1ef83d62012-04-11 12:09:53 +00002856 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002857 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002858 sha1_update( &sha1, padbuf , 40 );
2859 sha1_update( &sha1, sha1sum, 20 );
2860 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Paul Bakker1ef83d62012-04-11 12:09:53 +00002862 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Paul Bakker1ef83d62012-04-11 12:09:53 +00002864 memset( &md5, 0, sizeof( md5_context ) );
2865 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
2867 memset( padbuf, 0, sizeof( padbuf ) );
2868 memset( md5sum, 0, sizeof( md5sum ) );
2869 memset( sha1sum, 0, sizeof( sha1sum ) );
2870
2871 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2872}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002873#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002875#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002876static void ssl_calc_finished_tls(
2877 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002878{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002880 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002881 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002883 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Paul Bakker48916f92012-09-16 19:57:18 +00002885 ssl_session *session = ssl->session_negotiate;
2886 if( !session )
2887 session = ssl->session;
2888
Paul Bakker1ef83d62012-04-11 12:09:53 +00002889 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
Paul Bakker48916f92012-09-16 19:57:18 +00002891 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2892 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894 /*
2895 * TLSv1:
2896 * hash = PRF( master, finished_label,
2897 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2898 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
Paul Bakker90995b52013-06-24 19:20:35 +02002900#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002901 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2902 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002903#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002904
Paul Bakker90995b52013-06-24 19:20:35 +02002905#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002906 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2907 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002908#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909
2910 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002911 ? "client finished"
2912 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002913
2914 md5_finish( &md5, padbuf );
2915 sha1_finish( &sha1, padbuf + 16 );
2916
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002917 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002918 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002919
2920 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2921
2922 memset( &md5, 0, sizeof( md5_context ) );
2923 memset( &sha1, 0, sizeof( sha1_context ) );
2924
2925 memset( padbuf, 0, sizeof( padbuf ) );
2926
2927 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2928}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002929#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002931#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2932#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002933static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002934 ssl_context *ssl, unsigned char *buf, int from )
2935{
2936 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002937 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002938 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939 unsigned char padbuf[32];
2940
Paul Bakker48916f92012-09-16 19:57:18 +00002941 ssl_session *session = ssl->session_negotiate;
2942 if( !session )
2943 session = ssl->session;
2944
Paul Bakker380da532012-04-18 16:10:25 +00002945 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002946
Paul Bakker9e36f042013-06-30 14:34:05 +02002947 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002948
2949 /*
2950 * TLSv1.2:
2951 * hash = PRF( master, finished_label,
2952 * Hash( handshake ) )[0.11]
2953 */
2954
Paul Bakker9e36f042013-06-30 14:34:05 +02002955#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002957 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002958#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002959
2960 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002961 ? "client finished"
2962 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963
Paul Bakker9e36f042013-06-30 14:34:05 +02002964 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002966 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002967 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968
2969 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2970
Paul Bakker9e36f042013-06-30 14:34:05 +02002971 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972
2973 memset( padbuf, 0, sizeof( padbuf ) );
2974
2975 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2976}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002977#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002978
Paul Bakker9e36f042013-06-30 14:34:05 +02002979#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002980static void ssl_calc_finished_tls_sha384(
2981 ssl_context *ssl, unsigned char *buf, int from )
2982{
2983 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002984 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002985 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002986 unsigned char padbuf[48];
2987
Paul Bakker48916f92012-09-16 19:57:18 +00002988 ssl_session *session = ssl->session_negotiate;
2989 if( !session )
2990 session = ssl->session;
2991
Paul Bakker380da532012-04-18 16:10:25 +00002992 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002993
Paul Bakker9e36f042013-06-30 14:34:05 +02002994 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002995
2996 /*
2997 * TLSv1.2:
2998 * hash = PRF( master, finished_label,
2999 * Hash( handshake ) )[0.11]
3000 */
3001
Paul Bakker9e36f042013-06-30 14:34:05 +02003002#if !defined(POLARSSL_SHA512_ALT)
3003 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3004 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003005#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003006
3007 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003008 ? "client finished"
3009 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003010
Paul Bakker9e36f042013-06-30 14:34:05 +02003011 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003012
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003013 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003014 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003015
3016 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3017
Paul Bakker9e36f042013-06-30 14:34:05 +02003018 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003019
3020 memset( padbuf, 0, sizeof( padbuf ) );
3021
3022 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3023}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003024#endif /* POLARSSL_SHA512_C */
3025#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003026
Paul Bakker48916f92012-09-16 19:57:18 +00003027void ssl_handshake_wrapup( ssl_context *ssl )
3028{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003029 int resume = ssl->handshake->resume;
3030
Paul Bakker48916f92012-09-16 19:57:18 +00003031 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3032
3033 /*
3034 * Free our handshake params
3035 */
3036 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003037 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003038 ssl->handshake = NULL;
3039
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003040 if( ssl->renegotiation == SSL_RENEGOTIATION )
3041 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3042
Paul Bakker48916f92012-09-16 19:57:18 +00003043 /*
3044 * Switch in our now active transform context
3045 */
3046 if( ssl->transform )
3047 {
3048 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003049 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003050 }
3051 ssl->transform = ssl->transform_negotiate;
3052 ssl->transform_negotiate = NULL;
3053
Paul Bakker0a597072012-09-25 21:55:46 +00003054 if( ssl->session )
3055 {
3056 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003057 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003058 }
3059 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003060 ssl->session_negotiate = NULL;
3061
Paul Bakker0a597072012-09-25 21:55:46 +00003062 /*
3063 * Add cache entry
3064 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003065 if( ssl->f_set_cache != NULL &&
3066 ssl->session->length != 0 &&
3067 resume == 0 )
3068 {
Paul Bakker0a597072012-09-25 21:55:46 +00003069 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3070 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003071 }
Paul Bakker0a597072012-09-25 21:55:46 +00003072
Paul Bakker48916f92012-09-16 19:57:18 +00003073 ssl->state++;
3074
3075 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3076}
3077
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078int ssl_write_finished( ssl_context *ssl )
3079{
3080 int ret, hash_len;
3081
3082 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3083
Paul Bakker92be97b2013-01-02 17:30:03 +01003084 /*
3085 * Set the out_msg pointer to the correct location based on IV length
3086 */
3087 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3088 {
3089 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3090 ssl->transform_negotiate->fixed_ivlen;
3091 }
3092 else
3093 ssl->out_msg = ssl->out_iv;
3094
Paul Bakker48916f92012-09-16 19:57:18 +00003095 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003096
3097 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3099
Paul Bakker48916f92012-09-16 19:57:18 +00003100 ssl->verify_data_len = hash_len;
3101 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3102
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 ssl->out_msglen = 4 + hash_len;
3104 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3105 ssl->out_msg[0] = SSL_HS_FINISHED;
3106
3107 /*
3108 * In case of session resuming, invert the client and server
3109 * ChangeCipherSpec messages order.
3110 */
Paul Bakker0a597072012-09-25 21:55:46 +00003111 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003112 {
3113 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003114 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 else
3116 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3117 }
3118 else
3119 ssl->state++;
3120
Paul Bakker48916f92012-09-16 19:57:18 +00003121 /*
3122 * Switch to our negotiated transform and session parameters for outbound data.
3123 */
3124 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3125 ssl->transform_out = ssl->transform_negotiate;
3126 ssl->session_out = ssl->session_negotiate;
3127 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003128
Paul Bakker07eb38b2012-12-19 14:42:06 +01003129#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3130 if( ssl_hw_record_activate != NULL)
3131 {
3132 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3133 {
3134 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3135 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3136 }
3137 }
3138#endif
3139
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3141 {
3142 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3143 return( ret );
3144 }
3145
3146 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3147
3148 return( 0 );
3149}
3150
3151int ssl_parse_finished( ssl_context *ssl )
3152{
Paul Bakker23986e52011-04-24 08:57:21 +00003153 int ret;
3154 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003155 unsigned char buf[36];
3156
3157 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3158
Paul Bakker48916f92012-09-16 19:57:18 +00003159 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160
Paul Bakker48916f92012-09-16 19:57:18 +00003161 /*
3162 * Switch to our negotiated transform and session parameters for inbound data.
3163 */
3164 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3165 ssl->transform_in = ssl->transform_negotiate;
3166 ssl->session_in = ssl->session_negotiate;
3167 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
Paul Bakker92be97b2013-01-02 17:30:03 +01003169 /*
3170 * Set the in_msg pointer to the correct location based on IV length
3171 */
3172 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3173 {
3174 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3175 ssl->transform_negotiate->fixed_ivlen;
3176 }
3177 else
3178 ssl->in_msg = ssl->in_iv;
3179
Paul Bakker07eb38b2012-12-19 14:42:06 +01003180#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3181 if( ssl_hw_record_activate != NULL)
3182 {
3183 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3184 {
3185 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3186 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3187 }
3188 }
3189#endif
3190
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3192 {
3193 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3194 return( ret );
3195 }
3196
3197 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3198 {
3199 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003200 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 }
3202
Paul Bakker1ef83d62012-04-11 12:09:53 +00003203 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3205
3206 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3207 ssl->in_hslen != 4 + hash_len )
3208 {
3209 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003210 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 }
3212
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003213 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 {
3215 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003216 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 }
3218
Paul Bakker48916f92012-09-16 19:57:18 +00003219 ssl->verify_data_len = hash_len;
3220 memcpy( ssl->peer_verify_data, buf, hash_len );
3221
Paul Bakker0a597072012-09-25 21:55:46 +00003222 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 {
3224 if( ssl->endpoint == SSL_IS_CLIENT )
3225 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3226
3227 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003228 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 }
3230 else
3231 ssl->state++;
3232
3233 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3234
3235 return( 0 );
3236}
3237
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003238static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003239{
3240 if( ssl->transform_negotiate )
3241 ssl_transform_free( ssl->transform_negotiate );
3242 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003243 {
3244 ssl->transform_negotiate =
3245 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3246 }
Paul Bakker48916f92012-09-16 19:57:18 +00003247
3248 if( ssl->session_negotiate )
3249 ssl_session_free( ssl->session_negotiate );
3250 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003251 {
3252 ssl->session_negotiate =
3253 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3254 }
Paul Bakker48916f92012-09-16 19:57:18 +00003255
3256 if( ssl->handshake )
3257 ssl_handshake_free( ssl->handshake );
3258 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003259 {
3260 ssl->handshake = (ssl_handshake_params *)
3261 polarssl_malloc( sizeof(ssl_handshake_params) );
3262 }
Paul Bakker48916f92012-09-16 19:57:18 +00003263
3264 if( ssl->handshake == NULL ||
3265 ssl->transform_negotiate == NULL ||
3266 ssl->session_negotiate == NULL )
3267 {
3268 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3269 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3270 }
3271
3272 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3273 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3274 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3275
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003276#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3277 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003278 md5_starts( &ssl->handshake->fin_md5 );
3279 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003280#endif
3281#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3282#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003283 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003284#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003285#if defined(POLARSSL_SHA512_C)
3286 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003287#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003288#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003289
3290 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003291 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003292
Paul Bakker61d113b2013-07-04 11:51:43 +02003293#if defined(POLARSSL_ECDH_C)
3294 ecdh_init( &ssl->handshake->ecdh_ctx );
3295#endif
3296
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003297#if defined(POLARSSL_X509_CRT_PARSE_C)
3298 ssl->handshake->key_cert = ssl->key_cert;
3299#endif
3300
Paul Bakker48916f92012-09-16 19:57:18 +00003301 return( 0 );
3302}
3303
Paul Bakker5121ce52009-01-03 21:22:43 +00003304/*
3305 * Initialize an SSL context
3306 */
3307int ssl_init( ssl_context *ssl )
3308{
Paul Bakker48916f92012-09-16 19:57:18 +00003309 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 int len = SSL_BUFFER_LEN;
3311
3312 memset( ssl, 0, sizeof( ssl_context ) );
3313
Paul Bakker62f2dee2012-09-28 07:31:51 +00003314 /*
3315 * Sane defaults
3316 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003317 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3318 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3319 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3320 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003321
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003322 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003323
Paul Bakker62f2dee2012-09-28 07:31:51 +00003324#if defined(POLARSSL_DHM_C)
3325 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3326 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3327 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3328 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3329 {
3330 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3331 return( ret );
3332 }
3333#endif
3334
3335 /*
3336 * Prepare base structures
3337 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003338 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003340 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003341 ssl->in_msg = ssl->in_ctr + 13;
3342
3343 if( ssl->in_ctr == NULL )
3344 {
3345 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003346 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 }
3348
Paul Bakker6e339b52013-07-03 13:37:05 +02003349 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003351 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003352 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003353
3354 if( ssl->out_ctr == NULL )
3355 {
3356 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003357 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003358 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003359 }
3360
3361 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3362 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3363
Paul Bakker606b4ba2013-08-14 16:52:14 +02003364#if defined(POLARSSL_SSL_SESSION_TICKETS)
3365 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3366#endif
3367
Paul Bakker48916f92012-09-16 19:57:18 +00003368 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3369 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003370
3371 return( 0 );
3372}
3373
3374/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003375 * Reset an initialized and used SSL context for re-use while retaining
3376 * all application-set variables, function pointers and data.
3377 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003378int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003379{
Paul Bakker48916f92012-09-16 19:57:18 +00003380 int ret;
3381
Paul Bakker7eb013f2011-10-06 12:37:39 +00003382 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003383 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3384 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3385
3386 ssl->verify_data_len = 0;
3387 memset( ssl->own_verify_data, 0, 36 );
3388 memset( ssl->peer_verify_data, 0, 36 );
3389
Paul Bakker7eb013f2011-10-06 12:37:39 +00003390 ssl->in_offt = NULL;
3391
Paul Bakker92be97b2013-01-02 17:30:03 +01003392 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003393 ssl->in_msgtype = 0;
3394 ssl->in_msglen = 0;
3395 ssl->in_left = 0;
3396
3397 ssl->in_hslen = 0;
3398 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003399 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003400
Paul Bakker92be97b2013-01-02 17:30:03 +01003401 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003402 ssl->out_msgtype = 0;
3403 ssl->out_msglen = 0;
3404 ssl->out_left = 0;
3405
Paul Bakker48916f92012-09-16 19:57:18 +00003406 ssl->transform_in = NULL;
3407 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003408
3409 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3410 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003411
3412#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3413 if( ssl_hw_record_reset != NULL)
3414 {
3415 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003416 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003417 {
3418 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3419 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3420 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003421 }
3422#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003423
Paul Bakker48916f92012-09-16 19:57:18 +00003424 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003425 {
Paul Bakker48916f92012-09-16 19:57:18 +00003426 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003427 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003428 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003429 }
Paul Bakker48916f92012-09-16 19:57:18 +00003430
Paul Bakkerc0463502013-02-14 11:19:38 +01003431 if( ssl->session )
3432 {
3433 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003434 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003435 ssl->session = NULL;
3436 }
3437
Paul Bakker48916f92012-09-16 19:57:18 +00003438 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3439 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003440
3441 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003442}
3443
Paul Bakkera503a632013-08-14 13:48:06 +02003444#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003445/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003446 * Allocate and initialize ticket keys
3447 */
3448static int ssl_ticket_keys_init( ssl_context *ssl )
3449{
3450 int ret;
3451 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003452 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003453
3454 if( ssl->ticket_keys != NULL )
3455 return( 0 );
3456
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003457 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3458 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003459 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3460
3461 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003462 {
3463 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003464 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003465 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003466
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003467 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3468 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3469 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3470 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003471 polarssl_free( tkeys );
3472 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003473 }
3474
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003475 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003476 {
3477 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003478 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003479 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003480
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003481 ssl->ticket_keys = tkeys;
3482
3483 return( 0 );
3484}
Paul Bakkera503a632013-08-14 13:48:06 +02003485#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003486
3487/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003488 * SSL set accessors
3489 */
3490void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3491{
3492 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003493
Paul Bakker606b4ba2013-08-14 16:52:14 +02003494#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003495 if( endpoint == SSL_IS_CLIENT )
3496 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003498}
3499
3500void ssl_set_authmode( ssl_context *ssl, int authmode )
3501{
3502 ssl->authmode = authmode;
3503}
3504
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003505#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003506void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003507 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003508 void *p_vrfy )
3509{
3510 ssl->f_vrfy = f_vrfy;
3511 ssl->p_vrfy = p_vrfy;
3512}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003513#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003514
Paul Bakker5121ce52009-01-03 21:22:43 +00003515void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003516 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003517 void *p_rng )
3518{
3519 ssl->f_rng = f_rng;
3520 ssl->p_rng = p_rng;
3521}
3522
3523void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003524 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003525 void *p_dbg )
3526{
3527 ssl->f_dbg = f_dbg;
3528 ssl->p_dbg = p_dbg;
3529}
3530
3531void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003532 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003533 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003534{
3535 ssl->f_recv = f_recv;
3536 ssl->f_send = f_send;
3537 ssl->p_recv = p_recv;
3538 ssl->p_send = p_send;
3539}
3540
Paul Bakker0a597072012-09-25 21:55:46 +00003541void ssl_set_session_cache( ssl_context *ssl,
3542 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3543 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003544{
Paul Bakker0a597072012-09-25 21:55:46 +00003545 ssl->f_get_cache = f_get_cache;
3546 ssl->p_get_cache = p_get_cache;
3547 ssl->f_set_cache = f_set_cache;
3548 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003549}
3550
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003551int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003552{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003553 int ret;
3554
3555 if( ssl == NULL ||
3556 session == NULL ||
3557 ssl->session_negotiate == NULL ||
3558 ssl->endpoint != SSL_IS_CLIENT )
3559 {
3560 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3561 }
3562
3563 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3564 return( ret );
3565
Paul Bakker0a597072012-09-25 21:55:46 +00003566 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003567
3568 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003569}
3570
Paul Bakkerb68cad62012-08-23 08:34:18 +00003571void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003572{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003573 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3574 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3575 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3576 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3577}
3578
3579void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3580 int major, int minor )
3581{
3582 if( major != SSL_MAJOR_VERSION_3 )
3583 return;
3584
3585 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3586 return;
3587
3588 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003589}
3590
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003591#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003592/* Add a new (empty) key_cert entry an return a pointer to it */
3593static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3594{
3595 ssl_key_cert *key_cert, *last;
3596
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003597 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3598 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003599 return( NULL );
3600
3601 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3602
3603 /* Append the new key_cert to the (possibly empty) current list */
3604 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003605 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003606 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003607 if( ssl->handshake != NULL )
3608 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003609 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003610 else
3611 {
3612 last = ssl->key_cert;
3613 while( last->next != NULL )
3614 last = last->next;
3615 last->next = key_cert;
3616 }
3617
3618 return key_cert;
3619}
3620
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003621void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003622 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003623{
3624 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003625 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003626 ssl->peer_cn = peer_cn;
3627}
3628
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003629int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003630 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003631{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003632 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3633
3634 if( key_cert == NULL )
3635 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3636
3637 key_cert->cert = own_cert;
3638 key_cert->key = pk_key;
3639
3640 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003641}
3642
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003643#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003644int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003645 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003646{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003647 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003648 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003649
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003650 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003651 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3652
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003653 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3654 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003655 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003656
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003657 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003658
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003659 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003660 if( ret != 0 )
3661 return( ret );
3662
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003663 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003664 return( ret );
3665
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003666 key_cert->cert = own_cert;
3667 key_cert->key_own_alloc = 1;
3668
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003669 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003670}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003671#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003672
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003673int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003674 void *rsa_key,
3675 rsa_decrypt_func rsa_decrypt,
3676 rsa_sign_func rsa_sign,
3677 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003678{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003679 int ret;
3680 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003681
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003682 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003683 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3684
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003685 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3686 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003687 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003688
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003689 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003690
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003691 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3692 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3693 return( ret );
3694
3695 key_cert->cert = own_cert;
3696 key_cert->key_own_alloc = 1;
3697
3698 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003699}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003700#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003701
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003702#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003703int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3704 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003705{
Paul Bakker6db455e2013-09-18 17:29:31 +02003706 if( psk == NULL || psk_identity == NULL )
3707 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3708
3709 if( ssl->psk != NULL )
3710 {
3711 polarssl_free( ssl->psk );
3712 polarssl_free( ssl->psk_identity );
3713 }
3714
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003715 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003716 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003717
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003718 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3719 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003720
3721 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3722 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3723
3724 memcpy( ssl->psk, psk, ssl->psk_len );
3725 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003726
3727 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003728}
3729
3730void ssl_set_psk_cb( ssl_context *ssl,
3731 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3732 size_t),
3733 void *p_psk )
3734{
3735 ssl->f_psk = f_psk;
3736 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003737}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003738#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003739
Paul Bakker48916f92012-09-16 19:57:18 +00003740#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003741int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003742{
3743 int ret;
3744
Paul Bakker48916f92012-09-16 19:57:18 +00003745 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 {
3747 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3748 return( ret );
3749 }
3750
Paul Bakker48916f92012-09-16 19:57:18 +00003751 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003752 {
3753 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3754 return( ret );
3755 }
3756
3757 return( 0 );
3758}
3759
Paul Bakker1b57b062011-01-06 15:48:19 +00003760int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3761{
3762 int ret;
3763
Paul Bakker48916f92012-09-16 19:57:18 +00003764 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003765 {
3766 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3767 return( ret );
3768 }
3769
Paul Bakker48916f92012-09-16 19:57:18 +00003770 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003771 {
3772 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3773 return( ret );
3774 }
3775
3776 return( 0 );
3777}
Paul Bakker48916f92012-09-16 19:57:18 +00003778#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003779
Paul Bakker0be444a2013-08-27 21:55:01 +02003780#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003781int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003782{
3783 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003784 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003785
3786 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003787
3788 if( ssl->hostname_len + 1 == 0 )
3789 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3790
Paul Bakker6e339b52013-07-03 13:37:05 +02003791 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003792
Paul Bakkerb15b8512012-01-13 13:44:06 +00003793 if( ssl->hostname == NULL )
3794 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3795
Paul Bakker3c2122f2013-06-24 19:03:14 +02003796 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003797 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003798
Paul Bakker40ea7de2009-05-03 10:18:48 +00003799 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003800
3801 return( 0 );
3802}
3803
Paul Bakker5701cdc2012-09-27 21:49:42 +00003804void ssl_set_sni( ssl_context *ssl,
3805 int (*f_sni)(void *, ssl_context *,
3806 const unsigned char *, size_t),
3807 void *p_sni )
3808{
3809 ssl->f_sni = f_sni;
3810 ssl->p_sni = p_sni;
3811}
Paul Bakker0be444a2013-08-27 21:55:01 +02003812#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003813
Paul Bakker490ecc82011-10-06 13:04:09 +00003814void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3815{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003816 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3817 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3818 {
3819 ssl->max_major_ver = major;
3820 ssl->max_minor_ver = minor;
3821 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003822}
3823
Paul Bakker1d29fb52012-09-28 13:28:45 +00003824void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3825{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003826 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3827 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3828 {
3829 ssl->min_major_ver = major;
3830 ssl->min_minor_ver = minor;
3831 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003832}
3833
Paul Bakker05decb22013-08-15 13:33:48 +02003834#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003835int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3836{
Paul Bakker77e257e2013-12-16 15:29:52 +01003837 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003838 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003839 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003840 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003841 }
3842
3843 ssl->mfl_code = mfl_code;
3844
3845 return( 0 );
3846}
Paul Bakker05decb22013-08-15 13:33:48 +02003847#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003848
Paul Bakker1f2bc622013-08-15 13:45:55 +02003849#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003850int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003851{
3852 if( ssl->endpoint != SSL_IS_CLIENT )
3853 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3854
Paul Bakker8c1ede62013-07-19 14:14:37 +02003855 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003856
3857 return( 0 );
3858}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003859#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003860
Paul Bakker48916f92012-09-16 19:57:18 +00003861void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3862{
3863 ssl->disable_renegotiation = renegotiation;
3864}
3865
3866void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3867{
3868 ssl->allow_legacy_renegotiation = allow_legacy;
3869}
3870
Paul Bakkera503a632013-08-14 13:48:06 +02003871#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003872int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3873{
3874 ssl->session_tickets = use_tickets;
3875
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003876 if( ssl->endpoint == SSL_IS_CLIENT )
3877 return( 0 );
3878
3879 if( ssl->f_rng == NULL )
3880 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3881
3882 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003883}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003884
3885void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3886{
3887 ssl->ticket_lifetime = lifetime;
3888}
Paul Bakkera503a632013-08-14 13:48:06 +02003889#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003890
Paul Bakker5121ce52009-01-03 21:22:43 +00003891/*
3892 * SSL get accessors
3893 */
Paul Bakker23986e52011-04-24 08:57:21 +00003894size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003895{
3896 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3897}
3898
Paul Bakkerff60ee62010-03-16 21:09:09 +00003899int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003900{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003901 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003902}
3903
Paul Bakkere3166ce2011-01-27 17:40:50 +00003904const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003905{
Paul Bakker926c8e42013-03-06 10:23:34 +01003906 if( ssl == NULL || ssl->session == NULL )
3907 return NULL;
3908
Paul Bakkere3166ce2011-01-27 17:40:50 +00003909 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003910}
3911
Paul Bakker43ca69c2011-01-15 17:35:19 +00003912const char *ssl_get_version( const ssl_context *ssl )
3913{
3914 switch( ssl->minor_ver )
3915 {
3916 case SSL_MINOR_VERSION_0:
3917 return( "SSLv3.0" );
3918
3919 case SSL_MINOR_VERSION_1:
3920 return( "TLSv1.0" );
3921
3922 case SSL_MINOR_VERSION_2:
3923 return( "TLSv1.1" );
3924
Paul Bakker1ef83d62012-04-11 12:09:53 +00003925 case SSL_MINOR_VERSION_3:
3926 return( "TLSv1.2" );
3927
Paul Bakker43ca69c2011-01-15 17:35:19 +00003928 default:
3929 break;
3930 }
3931 return( "unknown" );
3932}
3933
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003934#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003935const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003936{
3937 if( ssl == NULL || ssl->session == NULL )
3938 return NULL;
3939
3940 return ssl->session->peer_cert;
3941}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003942#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003943
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003944int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3945{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003946 if( ssl == NULL ||
3947 dst == NULL ||
3948 ssl->session == NULL ||
3949 ssl->endpoint != SSL_IS_CLIENT )
3950 {
3951 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3952 }
3953
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003954 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003955}
3956
Paul Bakker5121ce52009-01-03 21:22:43 +00003957/*
Paul Bakker1961b702013-01-25 14:49:24 +01003958 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003959 */
Paul Bakker1961b702013-01-25 14:49:24 +01003960int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003961{
Paul Bakker40e46942009-01-03 21:51:57 +00003962 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003963
Paul Bakker40e46942009-01-03 21:51:57 +00003964#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003965 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003966 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003967#endif
3968
Paul Bakker40e46942009-01-03 21:51:57 +00003969#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003970 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003971 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003972#endif
3973
Paul Bakker1961b702013-01-25 14:49:24 +01003974 return( ret );
3975}
3976
3977/*
3978 * Perform the SSL handshake
3979 */
3980int ssl_handshake( ssl_context *ssl )
3981{
3982 int ret = 0;
3983
3984 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
3985
3986 while( ssl->state != SSL_HANDSHAKE_OVER )
3987 {
3988 ret = ssl_handshake_step( ssl );
3989
3990 if( ret != 0 )
3991 break;
3992 }
3993
Paul Bakker5121ce52009-01-03 21:22:43 +00003994 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
3995
3996 return( ret );
3997}
3998
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003999#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004000/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004001 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004002 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004003static int ssl_write_hello_request( ssl_context *ssl )
4004{
4005 int ret;
4006
4007 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4008
4009 ssl->out_msglen = 4;
4010 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4011 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4012
4013 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4014 {
4015 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4016 return( ret );
4017 }
4018
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004019 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4020
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004021 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4022
4023 return( 0 );
4024}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004025#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004026
4027/*
4028 * Actually renegotiate current connection, triggered by either:
4029 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004030 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004031 * - receiving any handshake message on server during ssl_read() after the
4032 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004033 * If the handshake doesn't complete due to waiting for I/O, it will continue
4034 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004035 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004036static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004037{
4038 int ret;
4039
4040 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4041
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004042 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4043 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004044
4045 ssl->state = SSL_HELLO_REQUEST;
4046 ssl->renegotiation = SSL_RENEGOTIATION;
4047
Paul Bakker48916f92012-09-16 19:57:18 +00004048 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4049 {
4050 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4051 return( ret );
4052 }
4053
4054 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4055
4056 return( 0 );
4057}
4058
4059/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004060 * Renegotiate current connection on client,
4061 * or request renegotiation on server
4062 */
4063int ssl_renegotiate( ssl_context *ssl )
4064{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004065 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004066
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004067#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004068 /* On server, just send the request */
4069 if( ssl->endpoint == SSL_IS_SERVER )
4070 {
4071 if( ssl->state != SSL_HANDSHAKE_OVER )
4072 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4073
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004074 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004075 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004076#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004077
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004078#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004079 /*
4080 * On client, either start the renegotiation process or,
4081 * if already in progress, continue the handshake
4082 */
4083 if( ssl->renegotiation != SSL_RENEGOTIATION )
4084 {
4085 if( ssl->state != SSL_HANDSHAKE_OVER )
4086 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4087
4088 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4089 {
4090 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4091 return( ret );
4092 }
4093 }
4094 else
4095 {
4096 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4097 {
4098 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4099 return( ret );
4100 }
4101 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004102#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004103
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004104 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004105}
4106
4107/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004108 * Receive application data decrypted from the SSL layer
4109 */
Paul Bakker23986e52011-04-24 08:57:21 +00004110int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004111{
Paul Bakker23986e52011-04-24 08:57:21 +00004112 int ret;
4113 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004114
4115 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4116
4117 if( ssl->state != SSL_HANDSHAKE_OVER )
4118 {
4119 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4120 {
4121 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4122 return( ret );
4123 }
4124 }
4125
4126 if( ssl->in_offt == NULL )
4127 {
4128 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4129 {
Paul Bakker831a7552011-05-18 13:32:51 +00004130 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4131 return( 0 );
4132
Paul Bakker5121ce52009-01-03 21:22:43 +00004133 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4134 return( ret );
4135 }
4136
4137 if( ssl->in_msglen == 0 &&
4138 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4139 {
4140 /*
4141 * OpenSSL sends empty messages to randomize the IV
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
Paul Bakker48916f92012-09-16 19:57:18 +00004153 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4154 {
4155 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4156
4157 if( ssl->endpoint == SSL_IS_CLIENT &&
4158 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4159 ssl->in_hslen != 4 ) )
4160 {
4161 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4162 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4163 }
4164
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004165 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4166 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4167 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004168 {
4169 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4170
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004171#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004172 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004173 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004174 /*
4175 * SSLv3 does not have a "no_renegotiation" alert
4176 */
4177 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4178 return( ret );
4179 }
4180 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004181#endif
4182#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4183 defined(POLARSSL_SSL_PROTO_TLS1_2)
4184 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004185 {
4186 if( ( ret = ssl_send_alert_message( ssl,
4187 SSL_ALERT_LEVEL_WARNING,
4188 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4189 {
4190 return( ret );
4191 }
Paul Bakker48916f92012-09-16 19:57:18 +00004192 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004193 else
4194#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004195 {
4196 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004197 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004198 }
Paul Bakker48916f92012-09-16 19:57:18 +00004199 }
4200 else
4201 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004202 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004203 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004204 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004205 return( ret );
4206 }
4207
4208 return( POLARSSL_ERR_NET_WANT_READ );
4209 }
4210 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004211 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4212 {
4213 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4214 "but not honored by client" ) );
4215 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4216 }
Paul Bakker48916f92012-09-16 19:57:18 +00004217 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004218 {
4219 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004220 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004221 }
4222
4223 ssl->in_offt = ssl->in_msg;
4224 }
4225
4226 n = ( len < ssl->in_msglen )
4227 ? len : ssl->in_msglen;
4228
4229 memcpy( buf, ssl->in_offt, n );
4230 ssl->in_msglen -= n;
4231
4232 if( ssl->in_msglen == 0 )
4233 /* all bytes consumed */
4234 ssl->in_offt = NULL;
4235 else
4236 /* more data available */
4237 ssl->in_offt += n;
4238
4239 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4240
Paul Bakker23986e52011-04-24 08:57:21 +00004241 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004242}
4243
4244/*
4245 * Send application data to be encrypted by the SSL layer
4246 */
Paul Bakker23986e52011-04-24 08:57:21 +00004247int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004248{
Paul Bakker23986e52011-04-24 08:57:21 +00004249 int ret;
4250 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004251 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004252
4253 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4254
4255 if( ssl->state != SSL_HANDSHAKE_OVER )
4256 {
4257 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4258 {
4259 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4260 return( ret );
4261 }
4262 }
4263
Paul Bakker05decb22013-08-15 13:33:48 +02004264#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004265 /*
4266 * Assume mfl_code is correct since it was checked when set
4267 */
4268 max_len = mfl_code_to_length[ssl->mfl_code];
4269
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004270 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004271 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004272 */
4273 if( ssl->session_out != NULL &&
4274 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4275 {
4276 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4277 }
Paul Bakker05decb22013-08-15 13:33:48 +02004278#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004279
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004280 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004281
Paul Bakker5121ce52009-01-03 21:22:43 +00004282 if( ssl->out_left != 0 )
4283 {
4284 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4285 {
4286 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4287 return( ret );
4288 }
4289 }
Paul Bakker887bd502011-06-08 13:10:54 +00004290 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004291 {
Paul Bakker887bd502011-06-08 13:10:54 +00004292 ssl->out_msglen = n;
4293 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4294 memcpy( ssl->out_msg, buf, n );
4295
4296 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4297 {
4298 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4299 return( ret );
4300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004301 }
4302
4303 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4304
Paul Bakker23986e52011-04-24 08:57:21 +00004305 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004306}
4307
4308/*
4309 * Notify the peer that the connection is being closed
4310 */
4311int ssl_close_notify( ssl_context *ssl )
4312{
4313 int ret;
4314
4315 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4316
4317 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4318 {
4319 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4320 return( ret );
4321 }
4322
4323 if( ssl->state == SSL_HANDSHAKE_OVER )
4324 {
Paul Bakker48916f92012-09-16 19:57:18 +00004325 if( ( ret = ssl_send_alert_message( ssl,
4326 SSL_ALERT_LEVEL_WARNING,
4327 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004328 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004329 return( ret );
4330 }
4331 }
4332
4333 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4334
4335 return( ret );
4336}
4337
Paul Bakker48916f92012-09-16 19:57:18 +00004338void ssl_transform_free( ssl_transform *transform )
4339{
4340#if defined(POLARSSL_ZLIB_SUPPORT)
4341 deflateEnd( &transform->ctx_deflate );
4342 inflateEnd( &transform->ctx_inflate );
4343#endif
4344
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004345 cipher_free_ctx( &transform->cipher_ctx_enc );
4346 cipher_free_ctx( &transform->cipher_ctx_dec );
4347
Paul Bakker61d113b2013-07-04 11:51:43 +02004348 md_free_ctx( &transform->md_ctx_enc );
4349 md_free_ctx( &transform->md_ctx_dec );
4350
Paul Bakker48916f92012-09-16 19:57:18 +00004351 memset( transform, 0, sizeof( ssl_transform ) );
4352}
4353
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004354#if defined(POLARSSL_X509_CRT_PARSE_C)
4355static void ssl_key_cert_free( ssl_key_cert *key_cert )
4356{
4357 ssl_key_cert *cur = key_cert, *next;
4358
4359 while( cur != NULL )
4360 {
4361 next = cur->next;
4362
4363 if( cur->key_own_alloc )
4364 {
4365 pk_free( cur->key );
4366 polarssl_free( cur->key );
4367 }
4368 polarssl_free( cur );
4369
4370 cur = next;
4371 }
4372}
4373#endif /* POLARSSL_X509_CRT_PARSE_C */
4374
Paul Bakker48916f92012-09-16 19:57:18 +00004375void ssl_handshake_free( ssl_handshake_params *handshake )
4376{
4377#if defined(POLARSSL_DHM_C)
4378 dhm_free( &handshake->dhm_ctx );
4379#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004380#if defined(POLARSSL_ECDH_C)
4381 ecdh_free( &handshake->ecdh_ctx );
4382#endif
4383
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004384#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004385 /* explicit void pointer cast for buggy MS compiler */
4386 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004387#endif
4388
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004389#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4390 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4391 /*
4392 * Free only the linked list wrapper, not the keys themselves
4393 * since the belong to the SNI callback
4394 */
4395 if( handshake->sni_key_cert != NULL )
4396 {
4397 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4398
4399 while( cur != NULL )
4400 {
4401 next = cur->next;
4402 polarssl_free( cur );
4403 cur = next;
4404 }
4405 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004406#endif
4407
Paul Bakker48916f92012-09-16 19:57:18 +00004408 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4409}
4410
4411void ssl_session_free( ssl_session *session )
4412{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004413#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004414 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004415 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004416 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004417 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004418 }
Paul Bakkered27a042013-04-18 22:46:23 +02004419#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004420
Paul Bakkera503a632013-08-14 13:48:06 +02004421#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004422 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004423#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004424
Paul Bakker0a597072012-09-25 21:55:46 +00004425 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004426}
4427
Paul Bakker5121ce52009-01-03 21:22:43 +00004428/*
4429 * Free an SSL context
4430 */
4431void ssl_free( ssl_context *ssl )
4432{
4433 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4434
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 if( ssl->out_ctr != NULL )
4436 {
4437 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004438 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004439 }
4440
4441 if( ssl->in_ctr != NULL )
4442 {
4443 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004444 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004445 }
4446
Paul Bakker16770332013-10-11 09:59:44 +02004447#if defined(POLARSSL_ZLIB_SUPPORT)
4448 if( ssl->compress_buf != NULL )
4449 {
4450 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4451 polarssl_free( ssl->compress_buf );
4452 }
4453#endif
4454
Paul Bakker40e46942009-01-03 21:51:57 +00004455#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004456 mpi_free( &ssl->dhm_P );
4457 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004458#endif
4459
Paul Bakker48916f92012-09-16 19:57:18 +00004460 if( ssl->transform )
4461 {
4462 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004463 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004464 }
4465
4466 if( ssl->handshake )
4467 {
4468 ssl_handshake_free( ssl->handshake );
4469 ssl_transform_free( ssl->transform_negotiate );
4470 ssl_session_free( ssl->session_negotiate );
4471
Paul Bakker6e339b52013-07-03 13:37:05 +02004472 polarssl_free( ssl->handshake );
4473 polarssl_free( ssl->transform_negotiate );
4474 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004475 }
4476
Paul Bakkerc0463502013-02-14 11:19:38 +01004477 if( ssl->session )
4478 {
4479 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004480 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004481 }
4482
Paul Bakkera503a632013-08-14 13:48:06 +02004483#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004484 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004485#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004486
Paul Bakker0be444a2013-08-27 21:55:01 +02004487#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004488 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004489 {
4490 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004491 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004492 ssl->hostname_len = 0;
4493 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004494#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004495
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004496#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004497 if( ssl->psk != NULL )
4498 {
4499 memset( ssl->psk, 0, ssl->psk_len );
4500 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4501 polarssl_free( ssl->psk );
4502 polarssl_free( ssl->psk_identity );
4503 ssl->psk_len = 0;
4504 ssl->psk_identity_len = 0;
4505 }
4506#endif
4507
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004508#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004509 ssl_key_cert_free( ssl->key_cert );
4510#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004511
Paul Bakker05ef8352012-05-08 09:17:57 +00004512#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4513 if( ssl_hw_record_finish != NULL )
4514 {
4515 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4516 ssl_hw_record_finish( ssl );
4517 }
4518#endif
4519
Paul Bakker5121ce52009-01-03 21:22:43 +00004520 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004521
Paul Bakker86f04f42013-02-14 11:20:09 +01004522 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004523 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004524}
4525
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004526#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004527/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004528 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004529 */
4530unsigned char ssl_sig_from_pk( pk_context *pk )
4531{
4532#if defined(POLARSSL_RSA_C)
4533 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4534 return( SSL_SIG_RSA );
4535#endif
4536#if defined(POLARSSL_ECDSA_C)
4537 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4538 return( SSL_SIG_ECDSA );
4539#endif
4540 return( SSL_SIG_ANON );
4541}
4542
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004543pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4544{
4545 switch( sig )
4546 {
4547#if defined(POLARSSL_RSA_C)
4548 case SSL_SIG_RSA:
4549 return( POLARSSL_PK_RSA );
4550#endif
4551#if defined(POLARSSL_ECDSA_C)
4552 case SSL_SIG_ECDSA:
4553 return( POLARSSL_PK_ECDSA );
4554#endif
4555 default:
4556 return( POLARSSL_PK_NONE );
4557 }
4558}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004559#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004560
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004561/*
4562 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4563 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004564md_type_t ssl_md_alg_from_hash( unsigned char hash )
4565{
4566 switch( hash )
4567 {
4568#if defined(POLARSSL_MD5_C)
4569 case SSL_HASH_MD5:
4570 return( POLARSSL_MD_MD5 );
4571#endif
4572#if defined(POLARSSL_SHA1_C)
4573 case SSL_HASH_SHA1:
4574 return( POLARSSL_MD_SHA1 );
4575#endif
4576#if defined(POLARSSL_SHA256_C)
4577 case SSL_HASH_SHA224:
4578 return( POLARSSL_MD_SHA224 );
4579 case SSL_HASH_SHA256:
4580 return( POLARSSL_MD_SHA256 );
4581#endif
4582#if defined(POLARSSL_SHA512_C)
4583 case SSL_HASH_SHA384:
4584 return( POLARSSL_MD_SHA384 );
4585 case SSL_HASH_SHA512:
4586 return( POLARSSL_MD_SHA512 );
4587#endif
4588 default:
4589 return( POLARSSL_MD_NONE );
4590 }
4591}
4592
Paul Bakker5121ce52009-01-03 21:22:43 +00004593#endif