blob: a903b3e0df5032ba3256cba94373d0eb527be80d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker68884e32013-01-07 18:20:04 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker0be444a2013-08-27 21:55:01 +020038#include "polarssl/debug.h"
39#include "polarssl/ssl.h"
40
Paul Bakker6e339b52013-07-03 13:37:05 +020041#if defined(POLARSSL_MEMORY_C)
42#include "polarssl/memory.h"
43#else
44#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakker6edcd412013-10-29 15:22:54 +010050#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
51 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000052#define strcasecmp _stricmp
53#endif
54
Paul Bakker05decb22013-08-15 13:33:48 +020055#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020056/*
57 * Convert max_fragment_length codes to length.
58 * RFC 6066 says:
59 * enum{
60 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
61 * } MaxFragmentLength;
62 * and we add 0 -> extension unused
63 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020064static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020065{
66 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
67 512, /* SSL_MAX_FRAG_LEN_512 */
68 1024, /* SSL_MAX_FRAG_LEN_1024 */
69 2048, /* SSL_MAX_FRAG_LEN_2048 */
70 4096, /* SSL_MAX_FRAG_LEN_4096 */
71};
Paul Bakker05decb22013-08-15 13:33:48 +020072#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020073
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020074static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
75{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020076 ssl_session_free( dst );
77 memcpy( dst, src, sizeof( ssl_session ) );
78
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020080 if( src->peer_cert != NULL )
81 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020082 int ret;
83
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020084 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
85 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020086 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
87
Paul Bakkerb6b09562013-09-18 14:17:41 +020088 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020089
Paul Bakkerddf26b42013-09-18 13:46:23 +020090 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
91 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020092 {
93 polarssl_free( dst->peer_cert );
94 dst->peer_cert = NULL;
95 return( ret );
96 }
97 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020099
Paul Bakkera503a632013-08-14 13:48:06 +0200100#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101 if( src->ticket != NULL )
102 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200103 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
104 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200105 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
106
107 memcpy( dst->ticket, src->ticket, src->ticket_len );
108 }
Paul Bakkera503a632013-08-14 13:48:06 +0200109#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110
111 return( 0 );
112}
113
Paul Bakker05ef8352012-05-08 09:17:57 +0000114#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
115int (*ssl_hw_record_init)(ssl_context *ssl,
116 const unsigned char *key_enc, const unsigned char *key_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100117 size_t keylen,
Paul Bakker05ef8352012-05-08 09:17:57 +0000118 const unsigned char *iv_enc, const unsigned char *iv_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100119 size_t ivlen,
120 const unsigned char *mac_enc, const unsigned char *mac_dec,
121 size_t maclen) = NULL;
122int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000123int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
124int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
125int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
126int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
127#endif
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129/*
130 * Key material generation
131 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200132#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200133static int ssl3_prf( const unsigned char *secret, size_t slen,
134 const char *label,
135 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000136 unsigned char *dstbuf, size_t dlen )
137{
138 size_t i;
139 md5_context md5;
140 sha1_context sha1;
141 unsigned char padding[16];
142 unsigned char sha1sum[20];
143 ((void)label);
144
145 /*
146 * SSLv3:
147 * block =
148 * MD5( secret + SHA1( 'A' + secret + random ) ) +
149 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
150 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
151 * ...
152 */
153 for( i = 0; i < dlen / 16; i++ )
154 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200155 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000156
157 sha1_starts( &sha1 );
158 sha1_update( &sha1, padding, 1 + i );
159 sha1_update( &sha1, secret, slen );
160 sha1_update( &sha1, random, rlen );
161 sha1_finish( &sha1, sha1sum );
162
163 md5_starts( &md5 );
164 md5_update( &md5, secret, slen );
165 md5_update( &md5, sha1sum, 20 );
166 md5_finish( &md5, dstbuf + i * 16 );
167 }
168
169 memset( &md5, 0, sizeof( md5 ) );
170 memset( &sha1, 0, sizeof( sha1 ) );
171
172 memset( padding, 0, sizeof( padding ) );
173 memset( sha1sum, 0, sizeof( sha1sum ) );
174
175 return( 0 );
176}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200177#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000178
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200179#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200180static int tls1_prf( const unsigned char *secret, size_t slen,
181 const char *label,
182 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000183 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184{
Paul Bakker23986e52011-04-24 08:57:21 +0000185 size_t nb, hs;
186 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200187 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 unsigned char tmp[128];
189 unsigned char h_i[20];
190
191 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000192 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 hs = ( slen + 1 ) / 2;
195 S1 = secret;
196 S2 = secret + slen - hs;
197
198 nb = strlen( label );
199 memcpy( tmp + 20, label, nb );
200 memcpy( tmp + 20 + nb, random, rlen );
201 nb += rlen;
202
203 /*
204 * First compute P_md5(secret,label+random)[0..dlen]
205 */
206 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
207
208 for( i = 0; i < dlen; i += 16 )
209 {
210 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
211 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
212
213 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
214
215 for( j = 0; j < k; j++ )
216 dstbuf[i + j] = h_i[j];
217 }
218
219 /*
220 * XOR out with P_sha1(secret,label+random)[0..dlen]
221 */
222 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
223
224 for( i = 0; i < dlen; i += 20 )
225 {
226 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
227 sha1_hmac( S2, hs, tmp, 20, tmp );
228
229 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
230
231 for( j = 0; j < k; j++ )
232 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
233 }
234
235 memset( tmp, 0, sizeof( tmp ) );
236 memset( h_i, 0, sizeof( h_i ) );
237
238 return( 0 );
239}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200240#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200242#if defined(POLARSSL_SSL_PROTO_TLS1_2)
243#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200244static int tls_prf_sha256( const unsigned char *secret, size_t slen,
245 const char *label,
246 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000247 unsigned char *dstbuf, size_t dlen )
248{
249 size_t nb;
250 size_t i, j, k;
251 unsigned char tmp[128];
252 unsigned char h_i[32];
253
254 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
255 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
256
257 nb = strlen( label );
258 memcpy( tmp + 32, label, nb );
259 memcpy( tmp + 32 + nb, random, rlen );
260 nb += rlen;
261
262 /*
263 * Compute P_<hash>(secret, label + random)[0..dlen]
264 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200265 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000266
267 for( i = 0; i < dlen; i += 32 )
268 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200269 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
270 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000271
272 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
273
274 for( j = 0; j < k; j++ )
275 dstbuf[i + j] = h_i[j];
276 }
277
278 memset( tmp, 0, sizeof( tmp ) );
279 memset( h_i, 0, sizeof( h_i ) );
280
281 return( 0 );
282}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200283#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000284
Paul Bakker9e36f042013-06-30 14:34:05 +0200285#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200286static int tls_prf_sha384( const unsigned char *secret, size_t slen,
287 const char *label,
288 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000289 unsigned char *dstbuf, size_t dlen )
290{
291 size_t nb;
292 size_t i, j, k;
293 unsigned char tmp[128];
294 unsigned char h_i[48];
295
296 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
297 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
298
299 nb = strlen( label );
300 memcpy( tmp + 48, label, nb );
301 memcpy( tmp + 48 + nb, random, rlen );
302 nb += rlen;
303
304 /*
305 * Compute P_<hash>(secret, label + random)[0..dlen]
306 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200307 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000308
309 for( i = 0; i < dlen; i += 48 )
310 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200311 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
312 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000313
314 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
315
316 for( j = 0; j < k; j++ )
317 dstbuf[i + j] = h_i[j];
318 }
319
320 memset( tmp, 0, sizeof( tmp ) );
321 memset( h_i, 0, sizeof( h_i ) );
322
323 return( 0 );
324}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200325#endif /* POLARSSL_SHA512_C */
326#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200328static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200329
330#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
331 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200332static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200333#endif
Paul Bakker380da532012-04-18 16:10:25 +0000334
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200335#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000336static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338#endif
339
340#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
341static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343#endif
344
345#if defined(POLARSSL_SSL_PROTO_TLS1_2)
346#if defined(POLARSSL_SHA256_C)
347static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
348static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000349static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100351
Paul Bakker9e36f042013-06-30 14:34:05 +0200352#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200353static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100354static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000355static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100356#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +0000358
Paul Bakker5121ce52009-01-03 21:22:43 +0000359int ssl_derive_keys( ssl_context *ssl )
360{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200361 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 unsigned char keyblk[256];
364 unsigned char *key1;
365 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100366 unsigned char *mac_enc;
367 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200368 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100369 const cipher_info_t *cipher_info;
370 const md_info_t *md_info;
371
Paul Bakker48916f92012-09-16 19:57:18 +0000372 ssl_session *session = ssl->session_negotiate;
373 ssl_transform *transform = ssl->transform_negotiate;
374 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
377
Paul Bakker68884e32013-01-07 18:20:04 +0100378 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
379 if( cipher_info == NULL )
380 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200381 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100382 transform->ciphersuite_info->cipher ) );
383 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
384 }
385
386 md_info = md_info_from_type( transform->ciphersuite_info->mac );
387 if( md_info == NULL )
388 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200389 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100390 transform->ciphersuite_info->mac ) );
391 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
392 }
393
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000395 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000396 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200397#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000398 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000399 {
Paul Bakker48916f92012-09-16 19:57:18 +0000400 handshake->tls_prf = ssl3_prf;
401 handshake->calc_verify = ssl_calc_verify_ssl;
402 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000403 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200404 else
405#endif
406#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
407 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000408 {
Paul Bakker48916f92012-09-16 19:57:18 +0000409 handshake->tls_prf = tls1_prf;
410 handshake->calc_verify = ssl_calc_verify_tls;
411 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200413 else
414#endif
415#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200416#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200417 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
418 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000419 {
Paul Bakker48916f92012-09-16 19:57:18 +0000420 handshake->tls_prf = tls_prf_sha384;
421 handshake->calc_verify = ssl_calc_verify_tls_sha384;
422 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000423 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000424 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200425#endif
426#if defined(POLARSSL_SHA256_C)
427 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = tls_prf_sha256;
430 handshake->calc_verify = ssl_calc_verify_tls_sha256;
431 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200433 else
434#endif
435#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200436 {
437 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200438 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200439 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000440
441 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 * SSLv3:
443 * master =
444 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
445 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
446 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200447 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200448 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 * master = PRF( premaster, "master secret", randbytes )[0..47]
450 */
Paul Bakker0a597072012-09-25 21:55:46 +0000451 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 {
Paul Bakker48916f92012-09-16 19:57:18 +0000453 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
454 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
Paul Bakker48916f92012-09-16 19:57:18 +0000456 handshake->tls_prf( handshake->premaster, handshake->pmslen,
457 "master secret",
458 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Paul Bakker48916f92012-09-16 19:57:18 +0000460 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 }
462 else
463 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
464
465 /*
466 * Swap the client and server random values.
467 */
Paul Bakker48916f92012-09-16 19:57:18 +0000468 memcpy( tmp, handshake->randbytes, 64 );
469 memcpy( handshake->randbytes, tmp + 32, 32 );
470 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 memset( tmp, 0, sizeof( tmp ) );
472
473 /*
474 * SSLv3:
475 * key block =
476 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
477 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
478 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
479 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
480 * ...
481 *
482 * TLSv1:
483 * key block = PRF( master, "key expansion", randbytes )
484 */
Paul Bakker48916f92012-09-16 19:57:18 +0000485 handshake->tls_prf( session->master, 48, "key expansion",
486 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
Paul Bakker48916f92012-09-16 19:57:18 +0000488 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
489 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
490 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
491 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
493
Paul Bakker48916f92012-09-16 19:57:18 +0000494 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 /*
497 * Determine the appropriate key, IV and MAC length.
498 */
Paul Bakker68884e32013-01-07 18:20:04 +0100499
500 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 {
Paul Bakker68884e32013-01-07 18:20:04 +0100502 transform->keylen = cipher_info->key_length;
503 transform->keylen /= 8;
504 transform->minlen = 1;
505 transform->ivlen = 12;
506 transform->fixed_ivlen = 4;
507 transform->maclen = 0;
508 }
509 else
510 {
511 if( md_info->type != POLARSSL_MD_NONE )
512 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200513 int ret;
514
Paul Bakker61d113b2013-07-04 11:51:43 +0200515 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
516 {
517 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
518 return( ret );
519 }
520
521 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
522 {
523 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
524 return( ret );
525 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
Paul Bakker68884e32013-01-07 18:20:04 +0100527 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200528
Paul Bakker1f2bc622013-08-15 13:45:55 +0200529#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200530 /*
531 * If HMAC is to be truncated, we shall keep the leftmost bytes,
532 * (rfc 6066 page 13 or rfc 2104 section 4),
533 * so we only need to adjust the length here.
534 */
535 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
536 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200537#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100538 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker68884e32013-01-07 18:20:04 +0100540 transform->keylen = cipher_info->key_length;
541 transform->keylen /= 8;
542 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Paul Bakker68884e32013-01-07 18:20:04 +0100544 transform->minlen = transform->keylen;
545 if( transform->minlen < transform->maclen )
546 {
547 if( cipher_info->mode == POLARSSL_MODE_STREAM )
548 transform->minlen = transform->maclen;
549 else
550 transform->minlen += transform->keylen;
551 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 }
553
554 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000555 transform->keylen, transform->minlen, transform->ivlen,
556 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 /*
559 * Finally setup the cipher contexts, IVs and MAC secrets.
560 */
561 if( ssl->endpoint == SSL_IS_CLIENT )
562 {
Paul Bakker48916f92012-09-16 19:57:18 +0000563 key1 = keyblk + transform->maclen * 2;
564 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Paul Bakker68884e32013-01-07 18:20:04 +0100566 mac_enc = keyblk;
567 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000569 /*
570 * This is not used in TLS v1.1.
571 */
Paul Bakker48916f92012-09-16 19:57:18 +0000572 iv_copy_len = ( transform->fixed_ivlen ) ?
573 transform->fixed_ivlen : transform->ivlen;
574 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
575 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000576 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 }
578 else
579 {
Paul Bakker48916f92012-09-16 19:57:18 +0000580 key1 = keyblk + transform->maclen * 2 + transform->keylen;
581 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Paul Bakker68884e32013-01-07 18:20:04 +0100583 mac_enc = keyblk + transform->maclen;
584 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000586 /*
587 * This is not used in TLS v1.1.
588 */
Paul Bakker48916f92012-09-16 19:57:18 +0000589 iv_copy_len = ( transform->fixed_ivlen ) ?
590 transform->fixed_ivlen : transform->ivlen;
591 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
592 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000593 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 }
595
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200596#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100597 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
598 {
599 memcpy( transform->mac_enc, mac_enc, transform->maclen );
600 memcpy( transform->mac_dec, mac_dec, transform->maclen );
601 }
602 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200603#endif
604#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
605 defined(POLARSSL_SSL_PROTO_TLS1_2)
606 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100607 {
608 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
609 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
610 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200611 else
612#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200613 {
614 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200615 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200616 }
Paul Bakker68884e32013-01-07 18:20:04 +0100617
Paul Bakker05ef8352012-05-08 09:17:57 +0000618#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
619 if( ssl_hw_record_init != NULL)
620 {
621 int ret = 0;
622
623 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
624
Paul Bakker07eb38b2012-12-19 14:42:06 +0100625 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
626 transform->iv_enc, transform->iv_dec,
627 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100628 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100629 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000630 {
631 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
632 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
633 }
634 }
635#endif
636
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200637 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
638 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200640 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
641 return( ret );
642 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200643
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200644 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
645 cipher_info ) ) != 0 )
646 {
647 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
648 return( ret );
649 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200650
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200651 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
652 cipher_info->key_length,
653 POLARSSL_ENCRYPT ) ) != 0 )
654 {
655 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
656 return( ret );
657 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200658
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200659 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
660 cipher_info->key_length,
661 POLARSSL_DECRYPT ) ) != 0 )
662 {
663 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
664 return( ret );
665 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200666
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200667#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200668 if( cipher_info->mode == POLARSSL_MODE_CBC )
669 {
670 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
671 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200672 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200673 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
674 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200675 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200676
677 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
678 POLARSSL_PADDING_NONE ) ) != 0 )
679 {
680 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
681 return( ret );
682 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686 memset( keyblk, 0, sizeof( keyblk ) );
687
Paul Bakker2770fbd2012-07-03 13:30:23 +0000688#if defined(POLARSSL_ZLIB_SUPPORT)
689 // Initialize compression
690 //
Paul Bakker48916f92012-09-16 19:57:18 +0000691 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000692 {
Paul Bakker16770332013-10-11 09:59:44 +0200693 if( ssl->compress_buf == NULL )
694 {
695 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
696 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
697 if( ssl->compress_buf == NULL )
698 {
699 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
700 SSL_BUFFER_LEN ) );
701 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
702 }
703 }
704
Paul Bakker2770fbd2012-07-03 13:30:23 +0000705 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
706
Paul Bakker48916f92012-09-16 19:57:18 +0000707 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
708 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000709
Paul Bakker48916f92012-09-16 19:57:18 +0000710 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
711 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000712 {
713 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
714 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
715 }
716 }
717#endif /* POLARSSL_ZLIB_SUPPORT */
718
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
720
721 return( 0 );
722}
723
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200724#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000725void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000726{
727 md5_context md5;
728 sha1_context sha1;
729 unsigned char pad_1[48];
730 unsigned char pad_2[48];
731
Paul Bakker380da532012-04-18 16:10:25 +0000732 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Paul Bakker48916f92012-09-16 19:57:18 +0000734 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
735 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
Paul Bakker380da532012-04-18 16:10:25 +0000737 memset( pad_1, 0x36, 48 );
738 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Paul Bakker48916f92012-09-16 19:57:18 +0000740 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000741 md5_update( &md5, pad_1, 48 );
742 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
Paul Bakker380da532012-04-18 16:10:25 +0000744 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000745 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000746 md5_update( &md5, pad_2, 48 );
747 md5_update( &md5, hash, 16 );
748 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker48916f92012-09-16 19:57:18 +0000750 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000751 sha1_update( &sha1, pad_1, 40 );
752 sha1_finish( &sha1, hash + 16 );
753
754 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000755 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000756 sha1_update( &sha1, pad_2, 40 );
757 sha1_update( &sha1, hash + 16, 20 );
758 sha1_finish( &sha1, hash + 16 );
759
760 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
761 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
762
763 return;
764}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200765#endif
Paul Bakker380da532012-04-18 16:10:25 +0000766
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200767#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000768void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
769{
770 md5_context md5;
771 sha1_context sha1;
772
773 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
774
Paul Bakker48916f92012-09-16 19:57:18 +0000775 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
776 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000777
Paul Bakker48916f92012-09-16 19:57:18 +0000778 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000779 sha1_finish( &sha1, hash + 16 );
780
781 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
782 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
783
784 return;
785}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200786#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000787
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200788#if defined(POLARSSL_SSL_PROTO_TLS1_2)
789#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000790void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
791{
Paul Bakker9e36f042013-06-30 14:34:05 +0200792 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000793
794 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
795
Paul Bakker9e36f042013-06-30 14:34:05 +0200796 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
797 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000798
799 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
800 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
801
802 return;
803}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000805
Paul Bakker9e36f042013-06-30 14:34:05 +0200806#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000807void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
808{
Paul Bakker9e36f042013-06-30 14:34:05 +0200809 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000810
811 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
812
Paul Bakker9e36f042013-06-30 14:34:05 +0200813 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
814 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000815
Paul Bakkerca4ab492012-04-18 14:23:57 +0000816 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
818
819 return;
820}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200821#endif /* POLARSSL_SHA512_C */
822#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000823
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200824#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200825int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
826{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200827 unsigned char *p = ssl->handshake->premaster;
828 unsigned char *end = p + sizeof( ssl->handshake->premaster );
829
830 /*
831 * PMS = struct {
832 * opaque other_secret<0..2^16-1>;
833 * opaque psk<0..2^16-1>;
834 * };
835 * with "other_secret" depending on the particular key exchange
836 */
837#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
838 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
839 {
840 if( end - p < 2 + (int) ssl->psk_len )
841 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
842
843 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
844 *(p++) = (unsigned char)( ssl->psk_len );
845 p += ssl->psk_len;
846 }
847 else
848#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200849#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
850 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
851 {
852 /*
853 * other_secret already set by the ClientKeyExchange message,
854 * and is 48 bytes long
855 */
856 *p++ = 0;
857 *p++ = 48;
858 p += 48;
859 }
860 else
861#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200862#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
863 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
864 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200865 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200866 size_t len = ssl->handshake->dhm_ctx.len;
867
868 if( end - p < 2 + (int) len )
869 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
870
871 *(p++) = (unsigned char)( len >> 8 );
872 *(p++) = (unsigned char)( len );
873 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
874 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
875 {
876 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
877 return( ret );
878 }
879 p += len;
880
881 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
882 }
883 else
884#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
885#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
886 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
887 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200888 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200889 size_t zlen;
890
891 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
892 p + 2, end - (p + 2),
893 ssl->f_rng, ssl->p_rng ) ) != 0 )
894 {
895 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
896 return( ret );
897 }
898
899 *(p++) = (unsigned char)( zlen >> 8 );
900 *(p++) = (unsigned char)( zlen );
901 p += zlen;
902
903 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
904 }
905 else
906#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
907 {
908 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
909 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
910 }
911
912 /* opaque psk<0..2^16-1>; */
913 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
914 *(p++) = (unsigned char)( ssl->psk_len );
915 memcpy( p, ssl->psk, ssl->psk_len );
916 p += ssl->psk_len;
917
918 ssl->handshake->pmslen = p - ssl->handshake->premaster;
919
920 return( 0 );
921}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200922#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200923
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200924#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000925/*
926 * SSLv3.0 MAC functions
927 */
Paul Bakker68884e32013-01-07 18:20:04 +0100928static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
929 unsigned char *buf, size_t len,
930 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000931{
932 unsigned char header[11];
933 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100934 int padlen = 0;
935 int md_size = md_get_size( md_ctx->md_info );
936 int md_type = md_get_type( md_ctx->md_info );
937
938 if( md_type == POLARSSL_MD_MD5 )
939 padlen = 48;
940 else if( md_type == POLARSSL_MD_SHA1 )
941 padlen = 40;
942 else if( md_type == POLARSSL_MD_SHA256 )
943 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100944 else if( md_type == POLARSSL_MD_SHA384 )
945 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
947 memcpy( header, ctr, 8 );
948 header[ 8] = (unsigned char) type;
949 header[ 9] = (unsigned char)( len >> 8 );
950 header[10] = (unsigned char)( len );
951
Paul Bakker68884e32013-01-07 18:20:04 +0100952 memset( padding, 0x36, padlen );
953 md_starts( md_ctx );
954 md_update( md_ctx, secret, md_size );
955 md_update( md_ctx, padding, padlen );
956 md_update( md_ctx, header, 11 );
957 md_update( md_ctx, buf, len );
958 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Paul Bakker68884e32013-01-07 18:20:04 +0100960 memset( padding, 0x5C, padlen );
961 md_starts( md_ctx );
962 md_update( md_ctx, secret, md_size );
963 md_update( md_ctx, padding, padlen );
964 md_update( md_ctx, buf + len, md_size );
965 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000966}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200967#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000968
Paul Bakker5121ce52009-01-03 21:22:43 +0000969/*
970 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200971 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000972static int ssl_encrypt_buf( ssl_context *ssl )
973{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200974 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
976 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
977
978 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200979 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200981#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
982 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
983 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
984 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
985 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200987#if defined(POLARSSL_SSL_PROTO_SSL3)
988 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
989 {
990 ssl_mac( &ssl->transform_out->md_ctx_enc,
991 ssl->transform_out->mac_enc,
992 ssl->out_msg, ssl->out_msglen,
993 ssl->out_ctr, ssl->out_msgtype );
994 }
995 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200996#endif
997#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200998 defined(POLARSSL_SSL_PROTO_TLS1_2)
999 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1000 {
1001 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1002 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1003 ssl->out_msg, ssl->out_msglen );
1004 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1005 ssl->out_msg + ssl->out_msglen );
1006 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1007 }
1008 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001009#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001010 {
1011 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1012 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1013 }
1014
1015 SSL_DEBUG_BUF( 4, "computed mac",
1016 ssl->out_msg + ssl->out_msglen,
1017 ssl->transform_out->maclen );
1018
1019 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001020 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001021#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001023 /*
1024 * Encrypt
1025 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001026#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001027 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1028 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001030 int ret;
1031 size_t olen = 0;
1032
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1034 "including %d bytes of padding",
1035 ssl->out_msglen, 0 ) );
1036
1037 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1038 ssl->out_msg, ssl->out_msglen );
1039
Paul Bakker45125bc2013-09-04 16:47:11 +02001040 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001041 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001042 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1043 return( ret );
1044 }
1045
1046 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1047 ssl->transform_out->iv_enc,
1048 ssl->transform_out->ivlen ) ) != 0 )
1049 {
1050 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001051 return( ret );
1052 }
1053
1054 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1055 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1056 &olen ) ) != 0 )
1057 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001058 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001059 return( ret );
1060 }
1061
1062 if( ssl->out_msglen != olen )
1063 {
1064 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1065 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001066 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001067 }
1068
1069 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001070 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001071 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001072 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001073 return( ret );
1074 }
1075
1076 if( 0 != olen )
1077 {
1078 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1079 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001080 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001081 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 }
Paul Bakker68884e32013-01-07 18:20:04 +01001083 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001084#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001085#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001086 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1087 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001088 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001089 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001090 unsigned char *enc_msg;
1091 unsigned char add_data[13];
1092 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1093
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094 enc_msglen = ssl->out_msglen;
1095
1096 memcpy( add_data, ssl->out_ctr, 8 );
1097 add_data[8] = ssl->out_msgtype;
1098 add_data[9] = ssl->major_ver;
1099 add_data[10] = ssl->minor_ver;
1100 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1101 add_data[12] = ssl->out_msglen & 0xFF;
1102
1103 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1104 add_data, 13 );
1105
Paul Bakker68884e32013-01-07 18:20:04 +01001106 /*
1107 * Generate IV
1108 */
1109 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001110 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001111 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1112 if( ret != 0 )
1113 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001114
Paul Bakker68884e32013-01-07 18:20:04 +01001115 memcpy( ssl->out_iv,
1116 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1117 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001118
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001119 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1120 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1121
Paul Bakker68884e32013-01-07 18:20:04 +01001122 /*
1123 * Fix pointer positions and message length with added IV
1124 */
1125 enc_msg = ssl->out_msg;
1126 enc_msglen = ssl->out_msglen;
1127 ssl->out_msglen += ssl->transform_out->ivlen -
1128 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001129
Paul Bakker68884e32013-01-07 18:20:04 +01001130 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1131 "including %d bytes of padding",
1132 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001133
Paul Bakker68884e32013-01-07 18:20:04 +01001134 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1135 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001136
Paul Bakker68884e32013-01-07 18:20:04 +01001137 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001138 * Encrypt
1139 */
1140 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1141 ssl->transform_out->iv_enc,
1142 ssl->transform_out->ivlen ) ) != 0 ||
1143 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1144 {
1145 return( ret );
1146 }
1147
1148 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1149 add_data, 13 ) ) != 0 )
1150 {
1151 return( ret );
1152 }
1153
1154 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1155 enc_msg, enc_msglen,
1156 enc_msg, &olen ) ) != 0 )
1157 {
1158 return( ret );
1159 }
1160 totlen = olen;
1161
1162 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1163 enc_msg + olen, &olen ) ) != 0 )
1164 {
1165 return( ret );
1166 }
1167 totlen += olen;
1168
1169 if( totlen != enc_msglen )
1170 {
1171 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1172 return( -1 );
1173 }
1174
1175 /*
1176 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001177 */
1178 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001179
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001180 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1181 enc_msg + enc_msglen, 16 ) ) != 0 )
1182 {
1183 return( ret );
1184 }
Paul Bakker68884e32013-01-07 18:20:04 +01001185
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001186 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 else
Paul Bakker68884e32013-01-07 18:20:04 +01001189#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001190#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1191 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001192 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1193 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001195 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001196 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001197 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001198
Paul Bakker48916f92012-09-16 19:57:18 +00001199 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1200 ssl->transform_out->ivlen;
1201 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 padlen = 0;
1203
1204 for( i = 0; i <= padlen; i++ )
1205 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1206
1207 ssl->out_msglen += padlen + 1;
1208
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001209 enc_msglen = ssl->out_msglen;
1210 enc_msg = ssl->out_msg;
1211
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001212#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001214 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1215 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001216 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001217 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001218 {
1219 /*
1220 * Generate IV
1221 */
Paul Bakker48916f92012-09-16 19:57:18 +00001222 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1223 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001224 if( ret != 0 )
1225 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226
Paul Bakker92be97b2013-01-02 17:30:03 +01001227 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001228 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001229
1230 /*
1231 * Fix pointer positions and message length with added IV
1232 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001233 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001234 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001235 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001237#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001241 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
1243 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001244 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Paul Bakker45125bc2013-09-04 16:47:11 +02001246 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001248 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1249 return( ret );
1250 }
1251
1252 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1253 ssl->transform_out->iv_enc,
1254 ssl->transform_out->ivlen ) ) != 0 )
1255 {
1256 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001257 return( ret );
1258 }
Paul Bakker68884e32013-01-07 18:20:04 +01001259
Paul Bakkercca5b812013-08-31 17:40:26 +02001260 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1261 enc_msg, enc_msglen, enc_msg,
1262 &olen ) ) != 0 )
1263 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001264 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001265 return( ret );
1266 }
Paul Bakker68884e32013-01-07 18:20:04 +01001267
Paul Bakkercca5b812013-08-31 17:40:26 +02001268 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001269
Paul Bakkercca5b812013-08-31 17:40:26 +02001270 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001271 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001273 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 return( ret );
1275 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001276
Paul Bakkercca5b812013-08-31 17:40:26 +02001277 if( enc_msglen != olen )
1278 {
1279 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1280 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001281 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001282 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001283
1284#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001285 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1286 {
1287 /*
1288 * Save IV in SSL3 and TLS1
1289 */
1290 memcpy( ssl->transform_out->iv_enc,
1291 ssl->transform_out->cipher_ctx_enc.iv,
1292 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001294#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001296 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001297#endif /* POLARSSL_CIPHER_MODE_CBC &&
1298 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001299 {
1300 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1301 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
Paul Bakkerca4ab492012-04-18 14:23:57 +00001304 for( i = 8; i > 0; i-- )
1305 if( ++ssl->out_ctr[i - 1] != 0 )
1306 break;
1307
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1309
1310 return( 0 );
1311}
1312
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001313#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001314
Paul Bakker5121ce52009-01-03 21:22:43 +00001315static int ssl_decrypt_buf( ssl_context *ssl )
1316{
Paul Bakker45829992013-01-03 14:52:21 +01001317 size_t i, padlen = 0, correct = 1;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001318 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +00001319
1320 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1321
Paul Bakker48916f92012-09-16 19:57:18 +00001322 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 {
1324 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001325 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001326 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 }
1328
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001329#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001330 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1331 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001332 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001333 int ret;
1334 size_t olen = 0;
1335
Paul Bakker68884e32013-01-07 18:20:04 +01001336 padlen = 0;
1337
Paul Bakker45125bc2013-09-04 16:47:11 +02001338 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001339 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001340 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1341 return( ret );
1342 }
1343
1344 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1345 ssl->transform_in->iv_dec,
1346 ssl->transform_in->ivlen ) ) != 0 )
1347 {
1348 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001349 return( ret );
1350 }
1351
1352 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1353 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1354 &olen ) ) != 0 )
1355 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001356 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001357 return( ret );
1358 }
1359
1360 if( ssl->in_msglen != olen )
1361 {
1362 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001363 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001364 }
1365
1366 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001367 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001368 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001369 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001370 return( ret );
1371 }
1372
1373 if( 0 != olen )
1374 {
1375 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001376 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 }
Paul Bakker68884e32013-01-07 18:20:04 +01001379 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001380#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001381#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001382 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1383 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001384 {
1385 unsigned char *dec_msg;
1386 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001387 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001388 unsigned char add_data[13];
1389 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1390
Paul Bakker68884e32013-01-07 18:20:04 +01001391 padlen = 0;
1392
1393 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1394 ssl->transform_in->fixed_ivlen );
1395 dec_msglen -= 16;
1396 dec_msg = ssl->in_msg;
1397 dec_msg_result = ssl->in_msg;
1398 ssl->in_msglen = dec_msglen;
1399
1400 memcpy( add_data, ssl->in_ctr, 8 );
1401 add_data[8] = ssl->in_msgtype;
1402 add_data[9] = ssl->major_ver;
1403 add_data[10] = ssl->minor_ver;
1404 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1405 add_data[12] = ssl->in_msglen & 0xFF;
1406
1407 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1408 add_data, 13 );
1409
1410 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1411 ssl->in_iv,
1412 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1413
1414 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1415 ssl->transform_in->ivlen );
1416 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1417
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001418 /*
1419 * Decrypt
1420 */
1421 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1422 ssl->transform_in->iv_dec,
1423 ssl->transform_in->ivlen ) ) != 0 ||
1424 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001425 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001426 return( ret );
1427 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001428
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001429 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1430 add_data, 13 ) ) != 0 )
1431 {
1432 return( ret );
1433 }
1434
1435 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1436 dec_msg, dec_msglen,
1437 dec_msg_result, &olen ) ) != 0 )
1438 {
1439 return( ret );
1440 }
1441 totlen = olen;
1442
1443 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1444 dec_msg_result + olen, &olen ) ) != 0 )
1445 {
1446 return( ret );
1447 }
1448 totlen += olen;
1449
1450 if( totlen != dec_msglen )
1451 {
1452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1453 return( -1 );
1454 }
1455
1456 /*
1457 * Authenticate
1458 */
1459 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1460 dec_msg + dec_msglen, 16 ) ) != 0 )
1461 {
1462 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001463 return( POLARSSL_ERR_SSL_INVALID_MAC );
1464 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001465
Paul Bakkerca4ab492012-04-18 14:23:57 +00001466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 else
Paul Bakker68884e32013-01-07 18:20:04 +01001468#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001469#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1470 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001471 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1472 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 {
Paul Bakker45829992013-01-03 14:52:21 +01001474 /*
1475 * Decrypt and check the padding
1476 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001477 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001478 unsigned char *dec_msg;
1479 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001480 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001481 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001482 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001483
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 /*
Paul Bakker45829992013-01-03 14:52:21 +01001485 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 */
Paul Bakker48916f92012-09-16 19:57:18 +00001487 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 {
1489 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001490 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001491 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 }
1493
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001494#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001495 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1496 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001497#endif
Paul Bakker45829992013-01-03 14:52:21 +01001498
1499 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1500 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1501 {
1502 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1503 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1504 return( POLARSSL_ERR_SSL_INVALID_MAC );
1505 }
1506
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001507 dec_msglen = ssl->in_msglen;
1508 dec_msg = ssl->in_msg;
1509 dec_msg_result = ssl->in_msg;
1510
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001511#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001512 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001513 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001514 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001515 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001516 {
Paul Bakker48916f92012-09-16 19:57:18 +00001517 dec_msglen -= ssl->transform_in->ivlen;
1518 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001519
Paul Bakker48916f92012-09-16 19:57:18 +00001520 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001521 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001522 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001523#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001524
Paul Bakker45125bc2013-09-04 16:47:11 +02001525 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001527 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1528 return( ret );
1529 }
1530
1531 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1532 ssl->transform_in->iv_dec,
1533 ssl->transform_in->ivlen ) ) != 0 )
1534 {
1535 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001536 return( ret );
1537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
Paul Bakkercca5b812013-08-31 17:40:26 +02001539 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1540 dec_msg, dec_msglen, dec_msg_result,
1541 &olen ) ) != 0 )
1542 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001543 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001544 return( ret );
1545 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001546
Paul Bakkercca5b812013-08-31 17:40:26 +02001547 dec_msglen -= olen;
1548 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001549 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001550 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001551 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001552 return( ret );
1553 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001554
Paul Bakkercca5b812013-08-31 17:40:26 +02001555 if( dec_msglen != olen )
1556 {
1557 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001558 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001559 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001560
1561#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001562 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1563 {
1564 /*
1565 * Save IV in SSL3 and TLS1
1566 */
1567 memcpy( ssl->transform_in->iv_dec,
1568 ssl->transform_in->cipher_ctx_dec.iv,
1569 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001570 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001571#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
1573 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001574
1575 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1576 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001577#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001578 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1579 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001580#endif
Paul Bakker45829992013-01-03 14:52:21 +01001581 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001582 correct = 0;
1583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001584
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001585#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1587 {
Paul Bakker48916f92012-09-16 19:57:18 +00001588 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001590#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1592 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001593 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001594#endif
Paul Bakker45829992013-01-03 14:52:21 +01001595 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001596 }
1597 }
1598 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001599#endif
1600#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1601 defined(POLARSSL_SSL_PROTO_TLS1_2)
1602 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 {
1604 /*
Paul Bakker45829992013-01-03 14:52:21 +01001605 * TLSv1+: always check the padding up to the first failure
1606 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001608 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001609 size_t padding_idx = ssl->in_msglen - padlen - 1;
1610
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001611 for( i = 1; i <= 256; i++ )
1612 {
1613 real_count &= ( i <= padlen );
1614 pad_count += real_count *
1615 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1616 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001617
1618 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001619
Paul Bakkerd66f0702013-01-31 16:57:45 +01001620#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001621 if( padlen > 0 && correct == 0)
1622 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001623#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001624 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001626 else
1627#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1628 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001629 {
1630 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001631 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001632 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001633 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001634 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001635#endif /* POLARSSL_CIPHER_MODE_CBC &&
1636 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001637 {
1638 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1639 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1640 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
1642 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1643 ssl->in_msg, ssl->in_msglen );
1644
1645 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001646 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001647 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001648#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1649 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1650 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1651 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1652 POLARSSL_MODE_GCM )
1653 {
1654 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001656 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1657 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001659 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001661#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001662 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1663 {
1664 ssl_mac( &ssl->transform_in->md_ctx_dec,
1665 ssl->transform_in->mac_dec,
1666 ssl->in_msg, ssl->in_msglen,
1667 ssl->in_ctr, ssl->in_msgtype );
1668 }
1669 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001670#endif /* POLARSSL_SSL_PROTO_SSL3 */
1671#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001672 defined(POLARSSL_SSL_PROTO_TLS1_2)
1673 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1674 {
1675 /*
1676 * Process MAC and always update for padlen afterwards to make
1677 * total time independent of padlen
1678 *
1679 * extra_run compensates MAC check for padlen
1680 *
1681 * Known timing attacks:
1682 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1683 *
1684 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1685 * correctly. (We round down instead of up, so -56 is the correct
1686 * value for our calculations instead of -55)
1687 */
1688 size_t j, extra_run = 0;
1689 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1690 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001691
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001692 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001693
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001694 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1695 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1696 ssl->in_msglen );
1697 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1698 ssl->in_msg + ssl->in_msglen );
1699 for( j = 0; j < extra_run; j++ )
1700 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001701
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001702 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1703 }
1704 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001705#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001706 POLARSSL_SSL_PROTO_TLS1_2 */
1707 {
1708 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1709 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1710 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001711
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001712 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1713 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1714 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001716 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001717 ssl->transform_in->maclen ) != 0 )
1718 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001719#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001720 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001721#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001722 correct = 0;
1723 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 /*
1726 * Finally check the correct flag
1727 */
1728 if( correct == 0 )
1729 return( POLARSSL_ERR_SSL_INVALID_MAC );
1730 }
1731#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
1733 if( ssl->in_msglen == 0 )
1734 {
1735 ssl->nb_zero++;
1736
1737 /*
1738 * Three or more empty messages may be a DoS attack
1739 * (excessive CPU consumption).
1740 */
1741 if( ssl->nb_zero > 3 )
1742 {
1743 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1744 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001745 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 }
1747 }
1748 else
1749 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001750
Paul Bakker23986e52011-04-24 08:57:21 +00001751 for( i = 8; i > 0; i-- )
1752 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001753 break;
1754
1755 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1756
1757 return( 0 );
1758}
1759
Paul Bakker2770fbd2012-07-03 13:30:23 +00001760#if defined(POLARSSL_ZLIB_SUPPORT)
1761/*
1762 * Compression/decompression functions
1763 */
1764static int ssl_compress_buf( ssl_context *ssl )
1765{
1766 int ret;
1767 unsigned char *msg_post = ssl->out_msg;
1768 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001769 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001770
1771 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1772
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001773 if( len_pre == 0 )
1774 return( 0 );
1775
Paul Bakker2770fbd2012-07-03 13:30:23 +00001776 memcpy( msg_pre, ssl->out_msg, len_pre );
1777
1778 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1779 ssl->out_msglen ) );
1780
1781 SSL_DEBUG_BUF( 4, "before compression: output payload",
1782 ssl->out_msg, ssl->out_msglen );
1783
Paul Bakker48916f92012-09-16 19:57:18 +00001784 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1785 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1786 ssl->transform_out->ctx_deflate.next_out = msg_post;
1787 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001788
Paul Bakker48916f92012-09-16 19:57:18 +00001789 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001790 if( ret != Z_OK )
1791 {
1792 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1793 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1794 }
1795
Paul Bakker48916f92012-09-16 19:57:18 +00001796 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001797
Paul Bakker2770fbd2012-07-03 13:30:23 +00001798 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1799 ssl->out_msglen ) );
1800
1801 SSL_DEBUG_BUF( 4, "after compression: output payload",
1802 ssl->out_msg, ssl->out_msglen );
1803
1804 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1805
1806 return( 0 );
1807}
1808
1809static int ssl_decompress_buf( ssl_context *ssl )
1810{
1811 int ret;
1812 unsigned char *msg_post = ssl->in_msg;
1813 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001814 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001815
1816 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1817
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001818 if( len_pre == 0 )
1819 return( 0 );
1820
Paul Bakker2770fbd2012-07-03 13:30:23 +00001821 memcpy( msg_pre, ssl->in_msg, len_pre );
1822
1823 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1824 ssl->in_msglen ) );
1825
1826 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1827 ssl->in_msg, ssl->in_msglen );
1828
Paul Bakker48916f92012-09-16 19:57:18 +00001829 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1830 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1831 ssl->transform_in->ctx_inflate.next_out = msg_post;
1832 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001833
Paul Bakker48916f92012-09-16 19:57:18 +00001834 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001835 if( ret != Z_OK )
1836 {
1837 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1838 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1839 }
1840
Paul Bakker48916f92012-09-16 19:57:18 +00001841 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001842
Paul Bakker2770fbd2012-07-03 13:30:23 +00001843 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1844 ssl->in_msglen ) );
1845
1846 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1847 ssl->in_msg, ssl->in_msglen );
1848
1849 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1850
1851 return( 0 );
1852}
1853#endif /* POLARSSL_ZLIB_SUPPORT */
1854
Paul Bakker5121ce52009-01-03 21:22:43 +00001855/*
1856 * Fill the input message buffer
1857 */
Paul Bakker23986e52011-04-24 08:57:21 +00001858int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001859{
Paul Bakker23986e52011-04-24 08:57:21 +00001860 int ret;
1861 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
1863 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1864
1865 while( ssl->in_left < nb_want )
1866 {
1867 len = nb_want - ssl->in_left;
1868 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1869
1870 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1871 ssl->in_left, nb_want ) );
1872 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1873
Paul Bakker831a7552011-05-18 13:32:51 +00001874 if( ret == 0 )
1875 return( POLARSSL_ERR_SSL_CONN_EOF );
1876
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 if( ret < 0 )
1878 return( ret );
1879
1880 ssl->in_left += ret;
1881 }
1882
1883 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1884
1885 return( 0 );
1886}
1887
1888/*
1889 * Flush any data not yet written
1890 */
1891int ssl_flush_output( ssl_context *ssl )
1892{
1893 int ret;
1894 unsigned char *buf;
1895
1896 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1897
1898 while( ssl->out_left > 0 )
1899 {
1900 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1901 5 + ssl->out_msglen, ssl->out_left ) );
1902
Paul Bakker5bd42292012-12-19 14:40:42 +01001903 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001905
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1907
1908 if( ret <= 0 )
1909 return( ret );
1910
1911 ssl->out_left -= ret;
1912 }
1913
1914 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1915
1916 return( 0 );
1917}
1918
1919/*
1920 * Record layer functions
1921 */
1922int ssl_write_record( ssl_context *ssl )
1923{
Paul Bakker05ef8352012-05-08 09:17:57 +00001924 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001925 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001926
1927 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1928
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1930 {
1931 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1932 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1933 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1934
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001935 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1936 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 }
1938
Paul Bakker2770fbd2012-07-03 13:30:23 +00001939#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001940 if( ssl->transform_out != NULL &&
1941 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001942 {
1943 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1944 {
1945 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1946 return( ret );
1947 }
1948
1949 len = ssl->out_msglen;
1950 }
1951#endif /*POLARSSL_ZLIB_SUPPORT */
1952
Paul Bakker05ef8352012-05-08 09:17:57 +00001953#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1954 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001956 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
Paul Bakker05ef8352012-05-08 09:17:57 +00001958 ret = ssl_hw_record_write( ssl );
1959 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1960 {
1961 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1962 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1963 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001964
1965 if( ret == 0 )
1966 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001967 }
1968#endif
1969 if( !done )
1970 {
1971 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1972 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1973 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1975 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001976
Paul Bakker48916f92012-09-16 19:57:18 +00001977 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001978 {
1979 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1980 {
1981 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1982 return( ret );
1983 }
1984
1985 len = ssl->out_msglen;
1986 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1987 ssl->out_hdr[4] = (unsigned char)( len );
1988 }
1989
1990 ssl->out_left = 5 + ssl->out_msglen;
1991
1992 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1993 "version = [%d:%d], msglen = %d",
1994 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1995 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1996
1997 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001998 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 }
2000
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2002 {
2003 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2004 return( ret );
2005 }
2006
2007 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2008
2009 return( 0 );
2010}
2011
2012int ssl_read_record( ssl_context *ssl )
2013{
Paul Bakker05ef8352012-05-08 09:17:57 +00002014 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002015
2016 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2017
Paul Bakker68884e32013-01-07 18:20:04 +01002018 SSL_DEBUG_BUF( 4, "input record from network",
2019 ssl->in_hdr, 5 + ssl->in_msglen );
2020
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 if( ssl->in_hslen != 0 &&
2022 ssl->in_hslen < ssl->in_msglen )
2023 {
2024 /*
2025 * Get next Handshake message in the current record
2026 */
2027 ssl->in_msglen -= ssl->in_hslen;
2028
Paul Bakker8934a982011-08-05 11:11:53 +00002029 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002030 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031
2032 ssl->in_hslen = 4;
2033 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2034
2035 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2036 " %d, type = %d, hslen = %d",
2037 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2038
2039 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2040 {
2041 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002042 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 }
2044
2045 if( ssl->in_msglen < ssl->in_hslen )
2046 {
2047 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002048 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 }
2050
Paul Bakker48916f92012-09-16 19:57:18 +00002051 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002052
2053 return( 0 );
2054 }
2055
2056 ssl->in_hslen = 0;
2057
2058 /*
2059 * Read the record header and validate it
2060 */
2061 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2062 {
2063 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2064 return( ret );
2065 }
2066
2067 ssl->in_msgtype = ssl->in_hdr[0];
2068 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2069
2070 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2071 "version = [%d:%d], msglen = %d",
2072 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2073 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2074
2075 if( ssl->in_hdr[1] != ssl->major_ver )
2076 {
2077 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002078 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 }
2080
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002081 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 {
2083 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002084 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 }
2086
2087 /*
2088 * Make sure the message length is acceptable
2089 */
Paul Bakker48916f92012-09-16 19:57:18 +00002090 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 {
2092 if( ssl->in_msglen < 1 ||
2093 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2094 {
2095 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002096 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
2098 }
2099 else
2100 {
Paul Bakker48916f92012-09-16 19:57:18 +00002101 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 {
2103 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002104 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
2106
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002107#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002109 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
2111 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002112 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002116#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2117 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 /*
2119 * TLS encrypted messages can have up to 256 bytes of padding
2120 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002121 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002122 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 {
2124 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002125 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
2129
2130 /*
2131 * Read and optionally decrypt the message contents
2132 */
2133 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2134 {
2135 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2136 return( ret );
2137 }
2138
2139 SSL_DEBUG_BUF( 4, "input record from network",
2140 ssl->in_hdr, 5 + ssl->in_msglen );
2141
Paul Bakker05ef8352012-05-08 09:17:57 +00002142#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2143 if( ssl_hw_record_read != NULL)
2144 {
2145 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2146
2147 ret = ssl_hw_record_read( ssl );
2148 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2149 {
2150 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2151 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2152 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002153
2154 if( ret == 0 )
2155 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002156 }
2157#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002158 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 {
2160 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2161 {
Paul Bakker40865c82013-01-31 17:13:13 +01002162#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2163 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2164 {
2165 ssl_send_alert_message( ssl,
2166 SSL_ALERT_LEVEL_FATAL,
2167 SSL_ALERT_MSG_BAD_RECORD_MAC );
2168 }
2169#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2171 return( ret );
2172 }
2173
2174 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2175 ssl->in_msg, ssl->in_msglen );
2176
2177 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2178 {
2179 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002180 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 }
2182 }
2183
Paul Bakker2770fbd2012-07-03 13:30:23 +00002184#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002185 if( ssl->transform_in != NULL &&
2186 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002187 {
2188 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2189 {
2190 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2191 return( ret );
2192 }
2193
2194 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2195 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2196 }
2197#endif /* POLARSSL_ZLIB_SUPPORT */
2198
Paul Bakker0a925182012-04-16 06:46:41 +00002199 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2200 ssl->in_msgtype != SSL_MSG_ALERT &&
2201 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2202 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2203 {
2204 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2205
Paul Bakker48916f92012-09-16 19:57:18 +00002206 if( ( ret = ssl_send_alert_message( ssl,
2207 SSL_ALERT_LEVEL_FATAL,
2208 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002209 {
2210 return( ret );
2211 }
2212
2213 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2214 }
2215
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2217 {
2218 ssl->in_hslen = 4;
2219 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2220
2221 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2222 " %d, type = %d, hslen = %d",
2223 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2224
2225 /*
2226 * Additional checks to validate the handshake header
2227 */
2228 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2229 {
2230 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002231 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 }
2233
2234 if( ssl->in_msglen < ssl->in_hslen )
2235 {
2236 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002237 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239
Paul Bakker48916f92012-09-16 19:57:18 +00002240 if( ssl->state != SSL_HANDSHAKE_OVER )
2241 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 }
2243
2244 if( ssl->in_msgtype == SSL_MSG_ALERT )
2245 {
2246 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2247 ssl->in_msg[0], ssl->in_msg[1] ) );
2248
2249 /*
2250 * Ignore non-fatal alerts, except close_notify
2251 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002252 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002254 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2255 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002256 /**
2257 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2258 * error identifier.
2259 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002260 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002263 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2264 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 {
2266 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002267 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 }
2269 }
2270
2271 ssl->in_left = 0;
2272
2273 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2274
2275 return( 0 );
2276}
2277
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002278int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2279{
2280 int ret;
2281
2282 if( ( ret = ssl_send_alert_message( ssl,
2283 SSL_ALERT_LEVEL_FATAL,
2284 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2285 {
2286 return( ret );
2287 }
2288
2289 return( 0 );
2290}
2291
Paul Bakker0a925182012-04-16 06:46:41 +00002292int ssl_send_alert_message( ssl_context *ssl,
2293 unsigned char level,
2294 unsigned char message )
2295{
2296 int ret;
2297
2298 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2299
2300 ssl->out_msgtype = SSL_MSG_ALERT;
2301 ssl->out_msglen = 2;
2302 ssl->out_msg[0] = level;
2303 ssl->out_msg[1] = message;
2304
2305 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2306 {
2307 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2308 return( ret );
2309 }
2310
2311 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2312
2313 return( 0 );
2314}
2315
Paul Bakker5121ce52009-01-03 21:22:43 +00002316/*
2317 * Handshake functions
2318 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002319#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002320 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002321 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002322 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2323 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002324int ssl_write_certificate( ssl_context *ssl )
2325{
Paul Bakkered27a042013-04-18 22:46:23 +02002326 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002327 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
2329 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2330
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002331 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002332 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2333 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002334 {
2335 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2336 ssl->state++;
2337 return( 0 );
2338 }
2339
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002340 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002341 return( ret );
2342}
2343
2344int ssl_parse_certificate( ssl_context *ssl )
2345{
2346 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2347 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2348
2349 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2350
2351 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002352 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2353 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002354 {
2355 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2356 ssl->state++;
2357 return( 0 );
2358 }
2359
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002360 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002361 return( ret );
2362}
2363#else
2364int ssl_write_certificate( ssl_context *ssl )
2365{
2366 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2367 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002368 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002369 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2370
2371 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2372
2373 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002374 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2375 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002376 {
2377 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2378 ssl->state++;
2379 return( 0 );
2380 }
2381
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 if( ssl->endpoint == SSL_IS_CLIENT )
2383 {
2384 if( ssl->client_auth == 0 )
2385 {
2386 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2387 ssl->state++;
2388 return( 0 );
2389 }
2390
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002391#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 /*
2393 * If using SSLv3 and got no cert, send an Alert message
2394 * (otherwise an empty Certificate message will be sent).
2395 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002396 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2398 {
2399 ssl->out_msglen = 2;
2400 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002401 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2402 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
2404 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2405 goto write_msg;
2406 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002407#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 }
2409 else /* SSL_IS_SERVER */
2410 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002411 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 {
2413 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002414 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 }
2416 }
2417
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002418 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
2420 /*
2421 * 0 . 0 handshake type
2422 * 1 . 3 handshake length
2423 * 4 . 6 length of all certs
2424 * 7 . 9 length of cert. 1
2425 * 10 . n-1 peer certificate
2426 * n . n+2 length of cert. 2
2427 * n+3 . ... upper level cert, etc.
2428 */
2429 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002430 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
Paul Bakker29087132010-03-21 21:03:34 +00002432 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 {
2434 n = crt->raw.len;
2435 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
2436 {
2437 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2438 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002439 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 }
2441
2442 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2443 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2444 ssl->out_msg[i + 2] = (unsigned char)( n );
2445
2446 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2447 i += n; crt = crt->next;
2448 }
2449
2450 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2451 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2452 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2453
2454 ssl->out_msglen = i;
2455 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2456 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2457
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002458#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002459write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002460#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
2462 ssl->state++;
2463
2464 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2465 {
2466 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2467 return( ret );
2468 }
2469
2470 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2471
Paul Bakkered27a042013-04-18 22:46:23 +02002472 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473}
2474
2475int ssl_parse_certificate( ssl_context *ssl )
2476{
Paul Bakkered27a042013-04-18 22:46:23 +02002477 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002478 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002479 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
2481 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2482
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002483 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002484 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2485 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002486 {
2487 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2488 ssl->state++;
2489 return( 0 );
2490 }
2491
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002493 ( ssl->authmode == SSL_VERIFY_NONE ||
2494 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002496 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2498 ssl->state++;
2499 return( 0 );
2500 }
2501
2502 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2503 {
2504 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2505 return( ret );
2506 }
2507
2508 ssl->state++;
2509
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002510#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 /*
2512 * Check if the client sent an empty certificate
2513 */
2514 if( ssl->endpoint == SSL_IS_SERVER &&
2515 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2516 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002517 if( ssl->in_msglen == 2 &&
2518 ssl->in_msgtype == SSL_MSG_ALERT &&
2519 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2520 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 {
2522 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2523
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002524 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2526 return( 0 );
2527 else
Paul Bakker40e46942009-01-03 21:51:57 +00002528 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 }
2530 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002531#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2534 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 if( ssl->endpoint == SSL_IS_SERVER &&
2536 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2537 {
2538 if( ssl->in_hslen == 7 &&
2539 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2540 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2541 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2542 {
2543 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2544
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002545 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002547 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 else
2549 return( 0 );
2550 }
2551 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002552#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2553 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
2555 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2556 {
2557 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002558 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 }
2560
2561 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2562 {
2563 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002564 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 }
2566
2567 /*
2568 * Same message structure as in ssl_write_certificate()
2569 */
2570 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2571
2572 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2573 {
2574 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002575 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 }
2577
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002578 /* In case we tried to reuse a session but it failed */
2579 if( ssl->session_negotiate->peer_cert != NULL )
2580 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002581 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002582 polarssl_free( ssl->session_negotiate->peer_cert );
2583 }
2584
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002585 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2586 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 {
2588 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002589 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002590 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 }
2592
Paul Bakkerb6b09562013-09-18 14:17:41 +02002593 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
2595 i = 7;
2596
2597 while( i < ssl->in_hslen )
2598 {
2599 if( ssl->in_msg[i] != 0 )
2600 {
2601 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002602 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 }
2604
2605 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2606 | (unsigned int) ssl->in_msg[i + 2];
2607 i += 3;
2608
2609 if( n < 128 || i + n > ssl->in_hslen )
2610 {
2611 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002612 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 }
2614
Paul Bakkerddf26b42013-09-18 13:46:23 +02002615 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2616 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 if( ret != 0 )
2618 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002619 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 return( ret );
2621 }
2622
2623 i += n;
2624 }
2625
Paul Bakker48916f92012-09-16 19:57:18 +00002626 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
2628 if( ssl->authmode != SSL_VERIFY_NONE )
2629 {
2630 if( ssl->ca_chain == NULL )
2631 {
2632 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002633 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 }
2635
Paul Bakkerddf26b42013-09-18 13:46:23 +02002636 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2637 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2638 &ssl->session_negotiate->verify_result,
2639 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
2641 if( ret != 0 )
2642 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2643
2644 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2645 ret = 0;
2646 }
2647
2648 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2649
2650 return( ret );
2651}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002652#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2653 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2654 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
2656int ssl_write_change_cipher_spec( ssl_context *ssl )
2657{
2658 int ret;
2659
2660 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2661
2662 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2663 ssl->out_msglen = 1;
2664 ssl->out_msg[0] = 1;
2665
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 ssl->state++;
2667
2668 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2669 {
2670 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2671 return( ret );
2672 }
2673
2674 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2675
2676 return( 0 );
2677}
2678
2679int ssl_parse_change_cipher_spec( ssl_context *ssl )
2680{
2681 int ret;
2682
2683 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2684
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2686 {
2687 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2688 return( ret );
2689 }
2690
2691 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2692 {
2693 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002694 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 }
2696
2697 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2698 {
2699 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002700 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 }
2702
2703 ssl->state++;
2704
2705 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2706
2707 return( 0 );
2708}
2709
Paul Bakker41c83d32013-03-20 14:39:14 +01002710void ssl_optimize_checksum( ssl_context *ssl,
2711 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002712{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002713 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002714
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002715#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2716 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002717 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002718 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002719 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002720#endif
2721#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2722#if defined(POLARSSL_SHA512_C)
2723 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2724 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2725 else
2726#endif
2727#if defined(POLARSSL_SHA256_C)
2728 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002729 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002730 else
2731#endif
2732#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2733 /* Should never happen */
2734 return;
Paul Bakker380da532012-04-18 16:10:25 +00002735}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002736
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002737static void ssl_update_checksum_start( ssl_context *ssl,
2738 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002739{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002740#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2741 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002742 md5_update( &ssl->handshake->fin_md5 , buf, len );
2743 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002744#endif
2745#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2746#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002747 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002748#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002749#if defined(POLARSSL_SHA512_C)
2750 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002751#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002752#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002753}
2754
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002755#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2756 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002757static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2758 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002759{
Paul Bakker48916f92012-09-16 19:57:18 +00002760 md5_update( &ssl->handshake->fin_md5 , buf, len );
2761 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002762}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002763#endif
Paul Bakker380da532012-04-18 16:10:25 +00002764
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002765#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2766#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002767static void ssl_update_checksum_sha256( ssl_context *ssl,
2768 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002769{
Paul Bakker9e36f042013-06-30 14:34:05 +02002770 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002771}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002772#endif
Paul Bakker380da532012-04-18 16:10:25 +00002773
Paul Bakker9e36f042013-06-30 14:34:05 +02002774#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002775static void ssl_update_checksum_sha384( ssl_context *ssl,
2776 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002777{
Paul Bakker9e36f042013-06-30 14:34:05 +02002778 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002779}
Paul Bakker769075d2012-11-24 11:26:46 +01002780#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002781#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002782
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002783#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002784static void ssl_calc_finished_ssl(
2785 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002787 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002788 md5_context md5;
2789 sha1_context sha1;
2790
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 unsigned char padbuf[48];
2792 unsigned char md5sum[16];
2793 unsigned char sha1sum[20];
2794
Paul Bakker48916f92012-09-16 19:57:18 +00002795 ssl_session *session = ssl->session_negotiate;
2796 if( !session )
2797 session = ssl->session;
2798
Paul Bakker1ef83d62012-04-11 12:09:53 +00002799 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2800
Paul Bakker48916f92012-09-16 19:57:18 +00002801 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2802 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
2804 /*
2805 * SSLv3:
2806 * hash =
2807 * MD5( master + pad2 +
2808 * MD5( handshake + sender + master + pad1 ) )
2809 * + SHA1( master + pad2 +
2810 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 */
2812
Paul Bakker90995b52013-06-24 19:20:35 +02002813#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002815 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002816#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
Paul Bakker90995b52013-06-24 19:20:35 +02002818#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002820 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002821#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
Paul Bakker3c2122f2013-06-24 19:03:14 +02002823 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2824 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Paul Bakker1ef83d62012-04-11 12:09:53 +00002826 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
Paul Bakker3c2122f2013-06-24 19:03:14 +02002828 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002829 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002830 md5_update( &md5, padbuf, 48 );
2831 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Paul Bakker3c2122f2013-06-24 19:03:14 +02002833 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002834 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002835 sha1_update( &sha1, padbuf, 40 );
2836 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Paul Bakker1ef83d62012-04-11 12:09:53 +00002838 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Paul Bakker1ef83d62012-04-11 12:09:53 +00002840 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002841 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002842 md5_update( &md5, padbuf, 48 );
2843 md5_update( &md5, md5sum, 16 );
2844 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Paul Bakker1ef83d62012-04-11 12:09:53 +00002846 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002847 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002848 sha1_update( &sha1, padbuf , 40 );
2849 sha1_update( &sha1, sha1sum, 20 );
2850 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Paul Bakker1ef83d62012-04-11 12:09:53 +00002852 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Paul Bakker1ef83d62012-04-11 12:09:53 +00002854 memset( &md5, 0, sizeof( md5_context ) );
2855 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
2857 memset( padbuf, 0, sizeof( padbuf ) );
2858 memset( md5sum, 0, sizeof( md5sum ) );
2859 memset( sha1sum, 0, sizeof( sha1sum ) );
2860
2861 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2862}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002863#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002865#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002866static void ssl_calc_finished_tls(
2867 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002868{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002870 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Paul Bakker48916f92012-09-16 19:57:18 +00002875 ssl_session *session = ssl->session_negotiate;
2876 if( !session )
2877 session = ssl->session;
2878
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
Paul Bakker48916f92012-09-16 19:57:18 +00002881 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2882 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Paul Bakker1ef83d62012-04-11 12:09:53 +00002884 /*
2885 * TLSv1:
2886 * hash = PRF( master, finished_label,
2887 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2888 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002889
Paul Bakker90995b52013-06-24 19:20:35 +02002890#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002891 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2892 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002893#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894
Paul Bakker90995b52013-06-24 19:20:35 +02002895#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2897 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002898#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899
2900 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002901 ? "client finished"
2902 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002903
2904 md5_finish( &md5, padbuf );
2905 sha1_finish( &sha1, padbuf + 16 );
2906
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002907 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002908 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909
2910 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2911
2912 memset( &md5, 0, sizeof( md5_context ) );
2913 memset( &sha1, 0, sizeof( sha1_context ) );
2914
2915 memset( padbuf, 0, sizeof( padbuf ) );
2916
2917 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2918}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002919#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002920
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002921#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2922#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002923static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924 ssl_context *ssl, unsigned char *buf, int from )
2925{
2926 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002927 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002928 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002929 unsigned char padbuf[32];
2930
Paul Bakker48916f92012-09-16 19:57:18 +00002931 ssl_session *session = ssl->session_negotiate;
2932 if( !session )
2933 session = ssl->session;
2934
Paul Bakker380da532012-04-18 16:10:25 +00002935 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936
Paul Bakker9e36f042013-06-30 14:34:05 +02002937 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002938
2939 /*
2940 * TLSv1.2:
2941 * hash = PRF( master, finished_label,
2942 * Hash( handshake ) )[0.11]
2943 */
2944
Paul Bakker9e36f042013-06-30 14:34:05 +02002945#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002946 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002947 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002948#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949
2950 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002951 ? "client finished"
2952 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953
Paul Bakker9e36f042013-06-30 14:34:05 +02002954 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002956 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002957 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958
2959 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2960
Paul Bakker9e36f042013-06-30 14:34:05 +02002961 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962
2963 memset( padbuf, 0, sizeof( padbuf ) );
2964
2965 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2966}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002967#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968
Paul Bakker9e36f042013-06-30 14:34:05 +02002969#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002970static void ssl_calc_finished_tls_sha384(
2971 ssl_context *ssl, unsigned char *buf, int from )
2972{
2973 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002974 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002975 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002976 unsigned char padbuf[48];
2977
Paul Bakker48916f92012-09-16 19:57:18 +00002978 ssl_session *session = ssl->session_negotiate;
2979 if( !session )
2980 session = ssl->session;
2981
Paul Bakker380da532012-04-18 16:10:25 +00002982 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002983
Paul Bakker9e36f042013-06-30 14:34:05 +02002984 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002985
2986 /*
2987 * TLSv1.2:
2988 * hash = PRF( master, finished_label,
2989 * Hash( handshake ) )[0.11]
2990 */
2991
Paul Bakker9e36f042013-06-30 14:34:05 +02002992#if !defined(POLARSSL_SHA512_ALT)
2993 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2994 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002995#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00002996
2997 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002998 ? "client finished"
2999 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000
Paul Bakker9e36f042013-06-30 14:34:05 +02003001 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003002
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003003 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003004 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003005
3006 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3007
Paul Bakker9e36f042013-06-30 14:34:05 +02003008 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003009
3010 memset( padbuf, 0, sizeof( padbuf ) );
3011
3012 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3013}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003014#endif /* POLARSSL_SHA512_C */
3015#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003016
Paul Bakker48916f92012-09-16 19:57:18 +00003017void ssl_handshake_wrapup( ssl_context *ssl )
3018{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003019 int resume = ssl->handshake->resume;
3020
Paul Bakker48916f92012-09-16 19:57:18 +00003021 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3022
3023 /*
3024 * Free our handshake params
3025 */
3026 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003027 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003028 ssl->handshake = NULL;
3029
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003030 if( ssl->renegotiation == SSL_RENEGOTIATION )
3031 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3032
Paul Bakker48916f92012-09-16 19:57:18 +00003033 /*
3034 * Switch in our now active transform context
3035 */
3036 if( ssl->transform )
3037 {
3038 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003039 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003040 }
3041 ssl->transform = ssl->transform_negotiate;
3042 ssl->transform_negotiate = NULL;
3043
Paul Bakker0a597072012-09-25 21:55:46 +00003044 if( ssl->session )
3045 {
3046 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003047 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003048 }
3049 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003050 ssl->session_negotiate = NULL;
3051
Paul Bakker0a597072012-09-25 21:55:46 +00003052 /*
3053 * Add cache entry
3054 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003055 if( ssl->f_set_cache != NULL &&
3056 ssl->session->length != 0 &&
3057 resume == 0 )
3058 {
Paul Bakker0a597072012-09-25 21:55:46 +00003059 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3060 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003061 }
Paul Bakker0a597072012-09-25 21:55:46 +00003062
Paul Bakker48916f92012-09-16 19:57:18 +00003063 ssl->state++;
3064
3065 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3066}
3067
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068int ssl_write_finished( ssl_context *ssl )
3069{
3070 int ret, hash_len;
3071
3072 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3073
Paul Bakker92be97b2013-01-02 17:30:03 +01003074 /*
3075 * Set the out_msg pointer to the correct location based on IV length
3076 */
3077 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3078 {
3079 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3080 ssl->transform_negotiate->fixed_ivlen;
3081 }
3082 else
3083 ssl->out_msg = ssl->out_iv;
3084
Paul Bakker48916f92012-09-16 19:57:18 +00003085 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003086
3087 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3089
Paul Bakker48916f92012-09-16 19:57:18 +00003090 ssl->verify_data_len = hash_len;
3091 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3092
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 ssl->out_msglen = 4 + hash_len;
3094 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3095 ssl->out_msg[0] = SSL_HS_FINISHED;
3096
3097 /*
3098 * In case of session resuming, invert the client and server
3099 * ChangeCipherSpec messages order.
3100 */
Paul Bakker0a597072012-09-25 21:55:46 +00003101 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 {
3103 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003104 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 else
3106 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3107 }
3108 else
3109 ssl->state++;
3110
Paul Bakker48916f92012-09-16 19:57:18 +00003111 /*
3112 * Switch to our negotiated transform and session parameters for outbound data.
3113 */
3114 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3115 ssl->transform_out = ssl->transform_negotiate;
3116 ssl->session_out = ssl->session_negotiate;
3117 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003118
Paul Bakker07eb38b2012-12-19 14:42:06 +01003119#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3120 if( ssl_hw_record_activate != NULL)
3121 {
3122 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3123 {
3124 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3125 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3126 }
3127 }
3128#endif
3129
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3131 {
3132 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3133 return( ret );
3134 }
3135
3136 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3137
3138 return( 0 );
3139}
3140
3141int ssl_parse_finished( ssl_context *ssl )
3142{
Paul Bakker23986e52011-04-24 08:57:21 +00003143 int ret;
3144 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 unsigned char buf[36];
3146
3147 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3148
Paul Bakker48916f92012-09-16 19:57:18 +00003149 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Paul Bakker48916f92012-09-16 19:57:18 +00003151 /*
3152 * Switch to our negotiated transform and session parameters for inbound data.
3153 */
3154 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3155 ssl->transform_in = ssl->transform_negotiate;
3156 ssl->session_in = ssl->session_negotiate;
3157 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
Paul Bakker92be97b2013-01-02 17:30:03 +01003159 /*
3160 * Set the in_msg pointer to the correct location based on IV length
3161 */
3162 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3163 {
3164 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3165 ssl->transform_negotiate->fixed_ivlen;
3166 }
3167 else
3168 ssl->in_msg = ssl->in_iv;
3169
Paul Bakker07eb38b2012-12-19 14:42:06 +01003170#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3171 if( ssl_hw_record_activate != NULL)
3172 {
3173 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3174 {
3175 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3176 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3177 }
3178 }
3179#endif
3180
Paul Bakker5121ce52009-01-03 21:22:43 +00003181 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3182 {
3183 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3184 return( ret );
3185 }
3186
3187 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3188 {
3189 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003190 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 }
3192
Paul Bakker1ef83d62012-04-11 12:09:53 +00003193 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3195
3196 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3197 ssl->in_hslen != 4 + hash_len )
3198 {
3199 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003200 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 }
3202
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003203 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 {
3205 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003206 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 }
3208
Paul Bakker48916f92012-09-16 19:57:18 +00003209 ssl->verify_data_len = hash_len;
3210 memcpy( ssl->peer_verify_data, buf, hash_len );
3211
Paul Bakker0a597072012-09-25 21:55:46 +00003212 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 {
3214 if( ssl->endpoint == SSL_IS_CLIENT )
3215 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3216
3217 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003218 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 }
3220 else
3221 ssl->state++;
3222
3223 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3224
3225 return( 0 );
3226}
3227
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003228static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003229{
3230 if( ssl->transform_negotiate )
3231 ssl_transform_free( ssl->transform_negotiate );
3232 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003233 {
3234 ssl->transform_negotiate =
3235 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3236 }
Paul Bakker48916f92012-09-16 19:57:18 +00003237
3238 if( ssl->session_negotiate )
3239 ssl_session_free( ssl->session_negotiate );
3240 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003241 {
3242 ssl->session_negotiate =
3243 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3244 }
Paul Bakker48916f92012-09-16 19:57:18 +00003245
3246 if( ssl->handshake )
3247 ssl_handshake_free( ssl->handshake );
3248 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003249 {
3250 ssl->handshake = (ssl_handshake_params *)
3251 polarssl_malloc( sizeof(ssl_handshake_params) );
3252 }
Paul Bakker48916f92012-09-16 19:57:18 +00003253
3254 if( ssl->handshake == NULL ||
3255 ssl->transform_negotiate == NULL ||
3256 ssl->session_negotiate == NULL )
3257 {
3258 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3259 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3260 }
3261
3262 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3263 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3264 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3265
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003266#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3267 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003268 md5_starts( &ssl->handshake->fin_md5 );
3269 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003270#endif
3271#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3272#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003273 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003274#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003275#if defined(POLARSSL_SHA512_C)
3276 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003277#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003278#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003279
3280 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003281 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003282
Paul Bakker61d113b2013-07-04 11:51:43 +02003283#if defined(POLARSSL_ECDH_C)
3284 ecdh_init( &ssl->handshake->ecdh_ctx );
3285#endif
3286
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003287#if defined(POLARSSL_X509_CRT_PARSE_C)
3288 ssl->handshake->key_cert = ssl->key_cert;
3289#endif
3290
Paul Bakker48916f92012-09-16 19:57:18 +00003291 return( 0 );
3292}
3293
Paul Bakker5121ce52009-01-03 21:22:43 +00003294/*
3295 * Initialize an SSL context
3296 */
3297int ssl_init( ssl_context *ssl )
3298{
Paul Bakker48916f92012-09-16 19:57:18 +00003299 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 int len = SSL_BUFFER_LEN;
3301
3302 memset( ssl, 0, sizeof( ssl_context ) );
3303
Paul Bakker62f2dee2012-09-28 07:31:51 +00003304 /*
3305 * Sane defaults
3306 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003307 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3308 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3309 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3310 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003311
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003312 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003313
Paul Bakker62f2dee2012-09-28 07:31:51 +00003314#if defined(POLARSSL_DHM_C)
3315 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3316 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3317 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3318 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3319 {
3320 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3321 return( ret );
3322 }
3323#endif
3324
3325 /*
3326 * Prepare base structures
3327 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003328 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003330 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 ssl->in_msg = ssl->in_ctr + 13;
3332
3333 if( ssl->in_ctr == NULL )
3334 {
3335 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003336 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 }
3338
Paul Bakker6e339b52013-07-03 13:37:05 +02003339 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003340 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003341 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003342 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003343
3344 if( ssl->out_ctr == NULL )
3345 {
3346 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003347 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003348 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 }
3350
3351 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3352 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3353
Paul Bakker606b4ba2013-08-14 16:52:14 +02003354#if defined(POLARSSL_SSL_SESSION_TICKETS)
3355 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3356#endif
3357
Paul Bakker48916f92012-09-16 19:57:18 +00003358 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3359 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003360
3361 return( 0 );
3362}
3363
3364/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003365 * Reset an initialized and used SSL context for re-use while retaining
3366 * all application-set variables, function pointers and data.
3367 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003368int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003369{
Paul Bakker48916f92012-09-16 19:57:18 +00003370 int ret;
3371
Paul Bakker7eb013f2011-10-06 12:37:39 +00003372 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003373 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3374 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3375
3376 ssl->verify_data_len = 0;
3377 memset( ssl->own_verify_data, 0, 36 );
3378 memset( ssl->peer_verify_data, 0, 36 );
3379
Paul Bakker7eb013f2011-10-06 12:37:39 +00003380 ssl->in_offt = NULL;
3381
Paul Bakker92be97b2013-01-02 17:30:03 +01003382 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003383 ssl->in_msgtype = 0;
3384 ssl->in_msglen = 0;
3385 ssl->in_left = 0;
3386
3387 ssl->in_hslen = 0;
3388 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003389 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003390
Paul Bakker92be97b2013-01-02 17:30:03 +01003391 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003392 ssl->out_msgtype = 0;
3393 ssl->out_msglen = 0;
3394 ssl->out_left = 0;
3395
Paul Bakker48916f92012-09-16 19:57:18 +00003396 ssl->transform_in = NULL;
3397 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003398
3399 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3400 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003401
3402#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3403 if( ssl_hw_record_reset != NULL)
3404 {
3405 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003406 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003407 {
3408 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3409 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3410 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003411 }
3412#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003413
Paul Bakker48916f92012-09-16 19:57:18 +00003414 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003415 {
Paul Bakker48916f92012-09-16 19:57:18 +00003416 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003417 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003418 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003419 }
Paul Bakker48916f92012-09-16 19:57:18 +00003420
Paul Bakkerc0463502013-02-14 11:19:38 +01003421 if( ssl->session )
3422 {
3423 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003424 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003425 ssl->session = NULL;
3426 }
3427
Paul Bakker48916f92012-09-16 19:57:18 +00003428 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3429 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003430
3431 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003432}
3433
Paul Bakkera503a632013-08-14 13:48:06 +02003434#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003435/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003436 * Allocate and initialize ticket keys
3437 */
3438static int ssl_ticket_keys_init( ssl_context *ssl )
3439{
3440 int ret;
3441 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003442 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003443
3444 if( ssl->ticket_keys != NULL )
3445 return( 0 );
3446
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003447 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3448 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003449 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3450
3451 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
3452 return( ret );
3453
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003454 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3455 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3456 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3457 {
3458 return( ret );
3459 }
3460
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003461 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
3462 return( ret );
3463
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003464 ssl->ticket_keys = tkeys;
3465
3466 return( 0 );
3467}
Paul Bakkera503a632013-08-14 13:48:06 +02003468#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003469
3470/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 * SSL set accessors
3472 */
3473void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3474{
3475 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003476
Paul Bakker606b4ba2013-08-14 16:52:14 +02003477#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003478 if( endpoint == SSL_IS_CLIENT )
3479 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003480#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003481}
3482
3483void ssl_set_authmode( ssl_context *ssl, int authmode )
3484{
3485 ssl->authmode = authmode;
3486}
3487
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003488#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003489void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003490 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003491 void *p_vrfy )
3492{
3493 ssl->f_vrfy = f_vrfy;
3494 ssl->p_vrfy = p_vrfy;
3495}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003496#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003497
Paul Bakker5121ce52009-01-03 21:22:43 +00003498void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003499 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003500 void *p_rng )
3501{
3502 ssl->f_rng = f_rng;
3503 ssl->p_rng = p_rng;
3504}
3505
3506void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003507 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003508 void *p_dbg )
3509{
3510 ssl->f_dbg = f_dbg;
3511 ssl->p_dbg = p_dbg;
3512}
3513
3514void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003515 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003516 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003517{
3518 ssl->f_recv = f_recv;
3519 ssl->f_send = f_send;
3520 ssl->p_recv = p_recv;
3521 ssl->p_send = p_send;
3522}
3523
Paul Bakker0a597072012-09-25 21:55:46 +00003524void ssl_set_session_cache( ssl_context *ssl,
3525 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3526 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003527{
Paul Bakker0a597072012-09-25 21:55:46 +00003528 ssl->f_get_cache = f_get_cache;
3529 ssl->p_get_cache = p_get_cache;
3530 ssl->f_set_cache = f_set_cache;
3531 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003532}
3533
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003534int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003535{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003536 int ret;
3537
3538 if( ssl == NULL ||
3539 session == NULL ||
3540 ssl->session_negotiate == NULL ||
3541 ssl->endpoint != SSL_IS_CLIENT )
3542 {
3543 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3544 }
3545
3546 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3547 return( ret );
3548
Paul Bakker0a597072012-09-25 21:55:46 +00003549 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003550
3551 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003552}
3553
Paul Bakkerb68cad62012-08-23 08:34:18 +00003554void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003555{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003556 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3557 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3558 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3559 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3560}
3561
3562void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3563 int major, int minor )
3564{
3565 if( major != SSL_MAJOR_VERSION_3 )
3566 return;
3567
3568 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3569 return;
3570
3571 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003572}
3573
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003574#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003575/* Add a new (empty) key_cert entry an return a pointer to it */
3576static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3577{
3578 ssl_key_cert *key_cert, *last;
3579
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003580 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3581 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003582 return( NULL );
3583
3584 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3585
3586 /* Append the new key_cert to the (possibly empty) current list */
3587 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003588 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003589 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003590 if( ssl->handshake != NULL )
3591 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003592 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003593 else
3594 {
3595 last = ssl->key_cert;
3596 while( last->next != NULL )
3597 last = last->next;
3598 last->next = key_cert;
3599 }
3600
3601 return key_cert;
3602}
3603
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003604void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003605 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003606{
3607 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003608 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003609 ssl->peer_cn = peer_cn;
3610}
3611
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003612int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003613 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003614{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003615 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3616
3617 if( key_cert == NULL )
3618 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3619
3620 key_cert->cert = own_cert;
3621 key_cert->key = pk_key;
3622
3623 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003624}
3625
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003626#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003627int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003628 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003629{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003630 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003631 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003632
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003633 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003634 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3635
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003636 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3637 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003638 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003639
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003640 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003641
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003642 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003643 if( ret != 0 )
3644 return( ret );
3645
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003646 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003647 return( ret );
3648
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003649 key_cert->cert = own_cert;
3650 key_cert->key_own_alloc = 1;
3651
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003652 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003653}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003654#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003655
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003656int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003657 void *rsa_key,
3658 rsa_decrypt_func rsa_decrypt,
3659 rsa_sign_func rsa_sign,
3660 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003661{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003662 int ret;
3663 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003664
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003665 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003666 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3667
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003668 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3669 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003670 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003671
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003672 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003673
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003674 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3675 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3676 return( ret );
3677
3678 key_cert->cert = own_cert;
3679 key_cert->key_own_alloc = 1;
3680
3681 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003682}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003683#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003684
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003685#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003686int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3687 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003688{
Paul Bakker6db455e2013-09-18 17:29:31 +02003689 if( psk == NULL || psk_identity == NULL )
3690 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3691
3692 if( ssl->psk != NULL )
3693 {
3694 polarssl_free( ssl->psk );
3695 polarssl_free( ssl->psk_identity );
3696 }
3697
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003698 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003699 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003700
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003701 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3702 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003703
3704 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3705 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3706
3707 memcpy( ssl->psk, psk, ssl->psk_len );
3708 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003709
3710 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003711}
3712
3713void ssl_set_psk_cb( ssl_context *ssl,
3714 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3715 size_t),
3716 void *p_psk )
3717{
3718 ssl->f_psk = f_psk;
3719 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003720}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003721#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003722
Paul Bakker48916f92012-09-16 19:57:18 +00003723#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003724int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003725{
3726 int ret;
3727
Paul Bakker48916f92012-09-16 19:57:18 +00003728 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003729 {
3730 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3731 return( ret );
3732 }
3733
Paul Bakker48916f92012-09-16 19:57:18 +00003734 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 {
3736 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3737 return( ret );
3738 }
3739
3740 return( 0 );
3741}
3742
Paul Bakker1b57b062011-01-06 15:48:19 +00003743int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3744{
3745 int ret;
3746
Paul Bakker48916f92012-09-16 19:57:18 +00003747 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003748 {
3749 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3750 return( ret );
3751 }
3752
Paul Bakker48916f92012-09-16 19:57:18 +00003753 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003754 {
3755 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3756 return( ret );
3757 }
3758
3759 return( 0 );
3760}
Paul Bakker48916f92012-09-16 19:57:18 +00003761#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003762
Paul Bakker0be444a2013-08-27 21:55:01 +02003763#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003764int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003765{
3766 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003767 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003768
3769 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003770
3771 if( ssl->hostname_len + 1 == 0 )
3772 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3773
Paul Bakker6e339b52013-07-03 13:37:05 +02003774 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003775
Paul Bakkerb15b8512012-01-13 13:44:06 +00003776 if( ssl->hostname == NULL )
3777 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3778
Paul Bakker3c2122f2013-06-24 19:03:14 +02003779 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003780 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003781
Paul Bakker40ea7de2009-05-03 10:18:48 +00003782 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003783
3784 return( 0 );
3785}
3786
Paul Bakker5701cdc2012-09-27 21:49:42 +00003787void ssl_set_sni( ssl_context *ssl,
3788 int (*f_sni)(void *, ssl_context *,
3789 const unsigned char *, size_t),
3790 void *p_sni )
3791{
3792 ssl->f_sni = f_sni;
3793 ssl->p_sni = p_sni;
3794}
Paul Bakker0be444a2013-08-27 21:55:01 +02003795#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003796
Paul Bakker490ecc82011-10-06 13:04:09 +00003797void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3798{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003799 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3800 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3801 {
3802 ssl->max_major_ver = major;
3803 ssl->max_minor_ver = minor;
3804 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003805}
3806
Paul Bakker1d29fb52012-09-28 13:28:45 +00003807void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3808{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003809 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3810 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3811 {
3812 ssl->min_major_ver = major;
3813 ssl->min_minor_ver = minor;
3814 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003815}
3816
Paul Bakker05decb22013-08-15 13:33:48 +02003817#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003818int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3819{
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003820 if( mfl_code >= sizeof( mfl_code_to_length ) ||
3821 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003822 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003823 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003824 }
3825
3826 ssl->mfl_code = mfl_code;
3827
3828 return( 0 );
3829}
Paul Bakker05decb22013-08-15 13:33:48 +02003830#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003831
Paul Bakker1f2bc622013-08-15 13:45:55 +02003832#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003833int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003834{
3835 if( ssl->endpoint != SSL_IS_CLIENT )
3836 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3837
Paul Bakker8c1ede62013-07-19 14:14:37 +02003838 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003839
3840 return( 0 );
3841}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003842#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003843
Paul Bakker48916f92012-09-16 19:57:18 +00003844void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3845{
3846 ssl->disable_renegotiation = renegotiation;
3847}
3848
3849void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3850{
3851 ssl->allow_legacy_renegotiation = allow_legacy;
3852}
3853
Paul Bakkera503a632013-08-14 13:48:06 +02003854#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003855int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3856{
3857 ssl->session_tickets = use_tickets;
3858
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003859 if( ssl->endpoint == SSL_IS_CLIENT )
3860 return( 0 );
3861
3862 if( ssl->f_rng == NULL )
3863 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3864
3865 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003866}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003867
3868void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3869{
3870 ssl->ticket_lifetime = lifetime;
3871}
Paul Bakkera503a632013-08-14 13:48:06 +02003872#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003873
Paul Bakker5121ce52009-01-03 21:22:43 +00003874/*
3875 * SSL get accessors
3876 */
Paul Bakker23986e52011-04-24 08:57:21 +00003877size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003878{
3879 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3880}
3881
Paul Bakkerff60ee62010-03-16 21:09:09 +00003882int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003883{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003884 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003885}
3886
Paul Bakkere3166ce2011-01-27 17:40:50 +00003887const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003888{
Paul Bakker926c8e42013-03-06 10:23:34 +01003889 if( ssl == NULL || ssl->session == NULL )
3890 return NULL;
3891
Paul Bakkere3166ce2011-01-27 17:40:50 +00003892 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003893}
3894
Paul Bakker43ca69c2011-01-15 17:35:19 +00003895const char *ssl_get_version( const ssl_context *ssl )
3896{
3897 switch( ssl->minor_ver )
3898 {
3899 case SSL_MINOR_VERSION_0:
3900 return( "SSLv3.0" );
3901
3902 case SSL_MINOR_VERSION_1:
3903 return( "TLSv1.0" );
3904
3905 case SSL_MINOR_VERSION_2:
3906 return( "TLSv1.1" );
3907
Paul Bakker1ef83d62012-04-11 12:09:53 +00003908 case SSL_MINOR_VERSION_3:
3909 return( "TLSv1.2" );
3910
Paul Bakker43ca69c2011-01-15 17:35:19 +00003911 default:
3912 break;
3913 }
3914 return( "unknown" );
3915}
3916
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003917#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003918const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003919{
3920 if( ssl == NULL || ssl->session == NULL )
3921 return NULL;
3922
3923 return ssl->session->peer_cert;
3924}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003925#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003926
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003927int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3928{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003929 if( ssl == NULL ||
3930 dst == NULL ||
3931 ssl->session == NULL ||
3932 ssl->endpoint != SSL_IS_CLIENT )
3933 {
3934 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3935 }
3936
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003937 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003938}
3939
Paul Bakker5121ce52009-01-03 21:22:43 +00003940/*
Paul Bakker1961b702013-01-25 14:49:24 +01003941 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 */
Paul Bakker1961b702013-01-25 14:49:24 +01003943int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003944{
Paul Bakker40e46942009-01-03 21:51:57 +00003945 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003946
Paul Bakker40e46942009-01-03 21:51:57 +00003947#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003948 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003949 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003950#endif
3951
Paul Bakker40e46942009-01-03 21:51:57 +00003952#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003954 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003955#endif
3956
Paul Bakker1961b702013-01-25 14:49:24 +01003957 return( ret );
3958}
3959
3960/*
3961 * Perform the SSL handshake
3962 */
3963int ssl_handshake( ssl_context *ssl )
3964{
3965 int ret = 0;
3966
3967 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
3968
3969 while( ssl->state != SSL_HANDSHAKE_OVER )
3970 {
3971 ret = ssl_handshake_step( ssl );
3972
3973 if( ret != 0 )
3974 break;
3975 }
3976
Paul Bakker5121ce52009-01-03 21:22:43 +00003977 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
3978
3979 return( ret );
3980}
3981
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003982#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003983/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003984 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003985 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003986static int ssl_write_hello_request( ssl_context *ssl )
3987{
3988 int ret;
3989
3990 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
3991
3992 ssl->out_msglen = 4;
3993 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3994 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
3995
3996 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3997 {
3998 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3999 return( ret );
4000 }
4001
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004002 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4003
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004004 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4005
4006 return( 0 );
4007}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004008#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004009
4010/*
4011 * Actually renegotiate current connection, triggered by either:
4012 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004013 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004014 * - receiving any handshake message on server during ssl_read() after the
4015 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004016 * If the handshake doesn't complete due to waiting for I/O, it will continue
4017 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004018 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004019static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004020{
4021 int ret;
4022
4023 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4024
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004025 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4026 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004027
4028 ssl->state = SSL_HELLO_REQUEST;
4029 ssl->renegotiation = SSL_RENEGOTIATION;
4030
Paul Bakker48916f92012-09-16 19:57:18 +00004031 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4032 {
4033 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4034 return( ret );
4035 }
4036
4037 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4038
4039 return( 0 );
4040}
4041
4042/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004043 * Renegotiate current connection on client,
4044 * or request renegotiation on server
4045 */
4046int ssl_renegotiate( ssl_context *ssl )
4047{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004048 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004049
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004050#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004051 /* On server, just send the request */
4052 if( ssl->endpoint == SSL_IS_SERVER )
4053 {
4054 if( ssl->state != SSL_HANDSHAKE_OVER )
4055 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4056
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004057 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004058 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004059#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004060
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004061#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004062 /*
4063 * On client, either start the renegotiation process or,
4064 * if already in progress, continue the handshake
4065 */
4066 if( ssl->renegotiation != SSL_RENEGOTIATION )
4067 {
4068 if( ssl->state != SSL_HANDSHAKE_OVER )
4069 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4070
4071 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4072 {
4073 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4074 return( ret );
4075 }
4076 }
4077 else
4078 {
4079 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4080 {
4081 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4082 return( ret );
4083 }
4084 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004085#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004086
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004087 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004088}
4089
4090/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 * Receive application data decrypted from the SSL layer
4092 */
Paul Bakker23986e52011-04-24 08:57:21 +00004093int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004094{
Paul Bakker23986e52011-04-24 08:57:21 +00004095 int ret;
4096 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004097
4098 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4099
4100 if( ssl->state != SSL_HANDSHAKE_OVER )
4101 {
4102 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4103 {
4104 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4105 return( ret );
4106 }
4107 }
4108
4109 if( ssl->in_offt == NULL )
4110 {
4111 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4112 {
Paul Bakker831a7552011-05-18 13:32:51 +00004113 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4114 return( 0 );
4115
Paul Bakker5121ce52009-01-03 21:22:43 +00004116 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4117 return( ret );
4118 }
4119
4120 if( ssl->in_msglen == 0 &&
4121 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4122 {
4123 /*
4124 * OpenSSL sends empty messages to randomize the IV
4125 */
4126 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4127 {
Paul Bakker831a7552011-05-18 13:32:51 +00004128 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4129 return( 0 );
4130
Paul Bakker5121ce52009-01-03 21:22:43 +00004131 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4132 return( ret );
4133 }
4134 }
4135
Paul Bakker48916f92012-09-16 19:57:18 +00004136 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4137 {
4138 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4139
4140 if( ssl->endpoint == SSL_IS_CLIENT &&
4141 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4142 ssl->in_hslen != 4 ) )
4143 {
4144 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4145 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4146 }
4147
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004148 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4149 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4150 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004151 {
4152 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4153
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004154#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004155 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004156 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004157 /*
4158 * SSLv3 does not have a "no_renegotiation" alert
4159 */
4160 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4161 return( ret );
4162 }
4163 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004164#endif
4165#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4166 defined(POLARSSL_SSL_PROTO_TLS1_2)
4167 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004168 {
4169 if( ( ret = ssl_send_alert_message( ssl,
4170 SSL_ALERT_LEVEL_WARNING,
4171 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4172 {
4173 return( ret );
4174 }
Paul Bakker48916f92012-09-16 19:57:18 +00004175 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004176 else
4177#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004178 {
4179 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004180 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004181 }
Paul Bakker48916f92012-09-16 19:57:18 +00004182 }
4183 else
4184 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004185 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004186 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004187 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004188 return( ret );
4189 }
4190
4191 return( POLARSSL_ERR_NET_WANT_READ );
4192 }
4193 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004194 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4195 {
4196 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4197 "but not honored by client" ) );
4198 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4199 }
Paul Bakker48916f92012-09-16 19:57:18 +00004200 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004201 {
4202 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004203 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004204 }
4205
4206 ssl->in_offt = ssl->in_msg;
4207 }
4208
4209 n = ( len < ssl->in_msglen )
4210 ? len : ssl->in_msglen;
4211
4212 memcpy( buf, ssl->in_offt, n );
4213 ssl->in_msglen -= n;
4214
4215 if( ssl->in_msglen == 0 )
4216 /* all bytes consumed */
4217 ssl->in_offt = NULL;
4218 else
4219 /* more data available */
4220 ssl->in_offt += n;
4221
4222 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4223
Paul Bakker23986e52011-04-24 08:57:21 +00004224 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004225}
4226
4227/*
4228 * Send application data to be encrypted by the SSL layer
4229 */
Paul Bakker23986e52011-04-24 08:57:21 +00004230int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004231{
Paul Bakker23986e52011-04-24 08:57:21 +00004232 int ret;
4233 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004234 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004235
4236 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4237
4238 if( ssl->state != SSL_HANDSHAKE_OVER )
4239 {
4240 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4241 {
4242 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4243 return( ret );
4244 }
4245 }
4246
Paul Bakker05decb22013-08-15 13:33:48 +02004247#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004248 /*
4249 * Assume mfl_code is correct since it was checked when set
4250 */
4251 max_len = mfl_code_to_length[ssl->mfl_code];
4252
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004253 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004254 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004255 */
4256 if( ssl->session_out != NULL &&
4257 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4258 {
4259 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4260 }
Paul Bakker05decb22013-08-15 13:33:48 +02004261#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004262
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004263 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004264
Paul Bakker5121ce52009-01-03 21:22:43 +00004265 if( ssl->out_left != 0 )
4266 {
4267 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4268 {
4269 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4270 return( ret );
4271 }
4272 }
Paul Bakker887bd502011-06-08 13:10:54 +00004273 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004274 {
Paul Bakker887bd502011-06-08 13:10:54 +00004275 ssl->out_msglen = n;
4276 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4277 memcpy( ssl->out_msg, buf, n );
4278
4279 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4280 {
4281 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4282 return( ret );
4283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004284 }
4285
4286 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4287
Paul Bakker23986e52011-04-24 08:57:21 +00004288 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004289}
4290
4291/*
4292 * Notify the peer that the connection is being closed
4293 */
4294int ssl_close_notify( ssl_context *ssl )
4295{
4296 int ret;
4297
4298 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4299
4300 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4301 {
4302 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4303 return( ret );
4304 }
4305
4306 if( ssl->state == SSL_HANDSHAKE_OVER )
4307 {
Paul Bakker48916f92012-09-16 19:57:18 +00004308 if( ( ret = ssl_send_alert_message( ssl,
4309 SSL_ALERT_LEVEL_WARNING,
4310 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004312 return( ret );
4313 }
4314 }
4315
4316 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4317
4318 return( ret );
4319}
4320
Paul Bakker48916f92012-09-16 19:57:18 +00004321void ssl_transform_free( ssl_transform *transform )
4322{
4323#if defined(POLARSSL_ZLIB_SUPPORT)
4324 deflateEnd( &transform->ctx_deflate );
4325 inflateEnd( &transform->ctx_inflate );
4326#endif
4327
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004328 cipher_free_ctx( &transform->cipher_ctx_enc );
4329 cipher_free_ctx( &transform->cipher_ctx_dec );
4330
Paul Bakker61d113b2013-07-04 11:51:43 +02004331 md_free_ctx( &transform->md_ctx_enc );
4332 md_free_ctx( &transform->md_ctx_dec );
4333
Paul Bakker48916f92012-09-16 19:57:18 +00004334 memset( transform, 0, sizeof( ssl_transform ) );
4335}
4336
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004337#if defined(POLARSSL_X509_CRT_PARSE_C)
4338static void ssl_key_cert_free( ssl_key_cert *key_cert )
4339{
4340 ssl_key_cert *cur = key_cert, *next;
4341
4342 while( cur != NULL )
4343 {
4344 next = cur->next;
4345
4346 if( cur->key_own_alloc )
4347 {
4348 pk_free( cur->key );
4349 polarssl_free( cur->key );
4350 }
4351 polarssl_free( cur );
4352
4353 cur = next;
4354 }
4355}
4356#endif /* POLARSSL_X509_CRT_PARSE_C */
4357
Paul Bakker48916f92012-09-16 19:57:18 +00004358void ssl_handshake_free( ssl_handshake_params *handshake )
4359{
4360#if defined(POLARSSL_DHM_C)
4361 dhm_free( &handshake->dhm_ctx );
4362#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004363#if defined(POLARSSL_ECDH_C)
4364 ecdh_free( &handshake->ecdh_ctx );
4365#endif
4366
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004367#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004368 /* explicit void pointer cast for buggy MS compiler */
4369 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004370#endif
4371
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004372#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4373 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4374 /*
4375 * Free only the linked list wrapper, not the keys themselves
4376 * since the belong to the SNI callback
4377 */
4378 if( handshake->sni_key_cert != NULL )
4379 {
4380 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4381
4382 while( cur != NULL )
4383 {
4384 next = cur->next;
4385 polarssl_free( cur );
4386 cur = next;
4387 }
4388 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004389#endif
4390
Paul Bakker48916f92012-09-16 19:57:18 +00004391 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4392}
4393
4394void ssl_session_free( ssl_session *session )
4395{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004396#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004397 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004398 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004399 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004400 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004401 }
Paul Bakkered27a042013-04-18 22:46:23 +02004402#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004403
Paul Bakkera503a632013-08-14 13:48:06 +02004404#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004405 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004406#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004407
Paul Bakker0a597072012-09-25 21:55:46 +00004408 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004409}
4410
Paul Bakker5121ce52009-01-03 21:22:43 +00004411/*
4412 * Free an SSL context
4413 */
4414void ssl_free( ssl_context *ssl )
4415{
4416 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4417
Paul Bakker5121ce52009-01-03 21:22:43 +00004418 if( ssl->out_ctr != NULL )
4419 {
4420 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004421 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004422 }
4423
4424 if( ssl->in_ctr != NULL )
4425 {
4426 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004427 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004428 }
4429
Paul Bakker16770332013-10-11 09:59:44 +02004430#if defined(POLARSSL_ZLIB_SUPPORT)
4431 if( ssl->compress_buf != NULL )
4432 {
4433 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4434 polarssl_free( ssl->compress_buf );
4435 }
4436#endif
4437
Paul Bakker40e46942009-01-03 21:51:57 +00004438#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004439 mpi_free( &ssl->dhm_P );
4440 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004441#endif
4442
Paul Bakker48916f92012-09-16 19:57:18 +00004443 if( ssl->transform )
4444 {
4445 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004446 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004447 }
4448
4449 if( ssl->handshake )
4450 {
4451 ssl_handshake_free( ssl->handshake );
4452 ssl_transform_free( ssl->transform_negotiate );
4453 ssl_session_free( ssl->session_negotiate );
4454
Paul Bakker6e339b52013-07-03 13:37:05 +02004455 polarssl_free( ssl->handshake );
4456 polarssl_free( ssl->transform_negotiate );
4457 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004458 }
4459
Paul Bakkerc0463502013-02-14 11:19:38 +01004460 if( ssl->session )
4461 {
4462 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004463 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004464 }
4465
Paul Bakkera503a632013-08-14 13:48:06 +02004466#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004467 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004468#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004469
Paul Bakker0be444a2013-08-27 21:55:01 +02004470#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004471 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004472 {
4473 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004474 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004475 ssl->hostname_len = 0;
4476 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004477#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004478
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004479#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004480 if( ssl->psk != NULL )
4481 {
4482 memset( ssl->psk, 0, ssl->psk_len );
4483 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4484 polarssl_free( ssl->psk );
4485 polarssl_free( ssl->psk_identity );
4486 ssl->psk_len = 0;
4487 ssl->psk_identity_len = 0;
4488 }
4489#endif
4490
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004491#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004492 ssl_key_cert_free( ssl->key_cert );
4493#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004494
Paul Bakker05ef8352012-05-08 09:17:57 +00004495#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4496 if( ssl_hw_record_finish != NULL )
4497 {
4498 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4499 ssl_hw_record_finish( ssl );
4500 }
4501#endif
4502
Paul Bakker5121ce52009-01-03 21:22:43 +00004503 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004504
Paul Bakker86f04f42013-02-14 11:20:09 +01004505 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004506 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004507}
4508
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004509#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004510/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004511 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004512 */
4513unsigned char ssl_sig_from_pk( pk_context *pk )
4514{
4515#if defined(POLARSSL_RSA_C)
4516 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4517 return( SSL_SIG_RSA );
4518#endif
4519#if defined(POLARSSL_ECDSA_C)
4520 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4521 return( SSL_SIG_ECDSA );
4522#endif
4523 return( SSL_SIG_ANON );
4524}
4525
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004526pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4527{
4528 switch( sig )
4529 {
4530#if defined(POLARSSL_RSA_C)
4531 case SSL_SIG_RSA:
4532 return( POLARSSL_PK_RSA );
4533#endif
4534#if defined(POLARSSL_ECDSA_C)
4535 case SSL_SIG_ECDSA:
4536 return( POLARSSL_PK_ECDSA );
4537#endif
4538 default:
4539 return( POLARSSL_PK_NONE );
4540 }
4541}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004542#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004543
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004544/*
4545 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4546 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004547md_type_t ssl_md_alg_from_hash( unsigned char hash )
4548{
4549 switch( hash )
4550 {
4551#if defined(POLARSSL_MD5_C)
4552 case SSL_HASH_MD5:
4553 return( POLARSSL_MD_MD5 );
4554#endif
4555#if defined(POLARSSL_SHA1_C)
4556 case SSL_HASH_SHA1:
4557 return( POLARSSL_MD_SHA1 );
4558#endif
4559#if defined(POLARSSL_SHA256_C)
4560 case SSL_HASH_SHA224:
4561 return( POLARSSL_MD_SHA224 );
4562 case SSL_HASH_SHA256:
4563 return( POLARSSL_MD_SHA256 );
4564#endif
4565#if defined(POLARSSL_SHA512_C)
4566 case SSL_HASH_SHA384:
4567 return( POLARSSL_MD_SHA384 );
4568 case SSL_HASH_SHA512:
4569 return( POLARSSL_MD_SHA512 );
4570#endif
4571 default:
4572 return( POLARSSL_MD_NONE );
4573 }
4574}
4575
Paul Bakker5121ce52009-01-03 21:22:43 +00004576#endif