blob: a05b21e3ac09042f53847f1a4072e81b72ef2417 [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;
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
945 memcpy( header, ctr, 8 );
946 header[ 8] = (unsigned char) type;
947 header[ 9] = (unsigned char)( len >> 8 );
948 header[10] = (unsigned char)( len );
949
Paul Bakker68884e32013-01-07 18:20:04 +0100950 memset( padding, 0x36, padlen );
951 md_starts( md_ctx );
952 md_update( md_ctx, secret, md_size );
953 md_update( md_ctx, padding, padlen );
954 md_update( md_ctx, header, 11 );
955 md_update( md_ctx, buf, len );
956 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000957
Paul Bakker68884e32013-01-07 18:20:04 +0100958 memset( padding, 0x5C, padlen );
959 md_starts( md_ctx );
960 md_update( md_ctx, secret, md_size );
961 md_update( md_ctx, padding, padlen );
962 md_update( md_ctx, buf + len, md_size );
963 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000964}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200965#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000966
Paul Bakker5121ce52009-01-03 21:22:43 +0000967/*
968 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200969 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000970static int ssl_encrypt_buf( ssl_context *ssl )
971{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200972 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
974 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
975
976 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200977 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200979#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
980 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
981 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
982 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
983 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200985#if defined(POLARSSL_SSL_PROTO_SSL3)
986 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
987 {
988 ssl_mac( &ssl->transform_out->md_ctx_enc,
989 ssl->transform_out->mac_enc,
990 ssl->out_msg, ssl->out_msglen,
991 ssl->out_ctr, ssl->out_msgtype );
992 }
993 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200994#endif
995#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200996 defined(POLARSSL_SSL_PROTO_TLS1_2)
997 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
998 {
999 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1000 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1001 ssl->out_msg, ssl->out_msglen );
1002 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1003 ssl->out_msg + ssl->out_msglen );
1004 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1005 }
1006 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001007#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001008 {
1009 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1010 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1011 }
1012
1013 SSL_DEBUG_BUF( 4, "computed mac",
1014 ssl->out_msg + ssl->out_msglen,
1015 ssl->transform_out->maclen );
1016
1017 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001018 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001019#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001021 /*
1022 * Encrypt
1023 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001024#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001025 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1026 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001028 int ret;
1029 size_t olen = 0;
1030
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1032 "including %d bytes of padding",
1033 ssl->out_msglen, 0 ) );
1034
1035 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1036 ssl->out_msg, ssl->out_msglen );
1037
Paul Bakker45125bc2013-09-04 16:47:11 +02001038 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001039 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001040 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1041 return( ret );
1042 }
1043
1044 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1045 ssl->transform_out->iv_enc,
1046 ssl->transform_out->ivlen ) ) != 0 )
1047 {
1048 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001049 return( ret );
1050 }
1051
1052 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1053 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1054 &olen ) ) != 0 )
1055 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001056 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001057 return( ret );
1058 }
1059
1060 if( ssl->out_msglen != olen )
1061 {
1062 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1063 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001064 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001065 }
1066
1067 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001068 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001069 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001070 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001071 return( ret );
1072 }
1073
1074 if( 0 != olen )
1075 {
1076 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1077 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001078 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001079 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 }
Paul Bakker68884e32013-01-07 18:20:04 +01001081 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001082#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001083#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001084 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1085 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001086 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001087 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001088 unsigned char *enc_msg;
1089 unsigned char add_data[13];
1090 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1091
Paul Bakkerca4ab492012-04-18 14:23:57 +00001092 enc_msglen = ssl->out_msglen;
1093
1094 memcpy( add_data, ssl->out_ctr, 8 );
1095 add_data[8] = ssl->out_msgtype;
1096 add_data[9] = ssl->major_ver;
1097 add_data[10] = ssl->minor_ver;
1098 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1099 add_data[12] = ssl->out_msglen & 0xFF;
1100
1101 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1102 add_data, 13 );
1103
Paul Bakker68884e32013-01-07 18:20:04 +01001104 /*
1105 * Generate IV
1106 */
1107 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001108 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001109 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1110 if( ret != 0 )
1111 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001112
Paul Bakker68884e32013-01-07 18:20:04 +01001113 memcpy( ssl->out_iv,
1114 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1115 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001116
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001117 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1118 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1119
Paul Bakker68884e32013-01-07 18:20:04 +01001120 /*
1121 * Fix pointer positions and message length with added IV
1122 */
1123 enc_msg = ssl->out_msg;
1124 enc_msglen = ssl->out_msglen;
1125 ssl->out_msglen += ssl->transform_out->ivlen -
1126 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001127
Paul Bakker68884e32013-01-07 18:20:04 +01001128 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1129 "including %d bytes of padding",
1130 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131
Paul Bakker68884e32013-01-07 18:20:04 +01001132 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1133 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001134
Paul Bakker68884e32013-01-07 18:20:04 +01001135 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001136 * Encrypt
1137 */
1138 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1139 ssl->transform_out->iv_enc,
1140 ssl->transform_out->ivlen ) ) != 0 ||
1141 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1142 {
1143 return( ret );
1144 }
1145
1146 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1147 add_data, 13 ) ) != 0 )
1148 {
1149 return( ret );
1150 }
1151
1152 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1153 enc_msg, enc_msglen,
1154 enc_msg, &olen ) ) != 0 )
1155 {
1156 return( ret );
1157 }
1158 totlen = olen;
1159
1160 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1161 enc_msg + olen, &olen ) ) != 0 )
1162 {
1163 return( ret );
1164 }
1165 totlen += olen;
1166
1167 if( totlen != enc_msglen )
1168 {
1169 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1170 return( -1 );
1171 }
1172
1173 /*
1174 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001175 */
1176 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001177
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001178 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1179 enc_msg + enc_msglen, 16 ) ) != 0 )
1180 {
1181 return( ret );
1182 }
Paul Bakker68884e32013-01-07 18:20:04 +01001183
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001184 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 else
Paul Bakker68884e32013-01-07 18:20:04 +01001187#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001188#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1189 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001190 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1191 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001193 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001194 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001195 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001196
Paul Bakker48916f92012-09-16 19:57:18 +00001197 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1198 ssl->transform_out->ivlen;
1199 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 padlen = 0;
1201
1202 for( i = 0; i <= padlen; i++ )
1203 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1204
1205 ssl->out_msglen += padlen + 1;
1206
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001207 enc_msglen = ssl->out_msglen;
1208 enc_msg = ssl->out_msg;
1209
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001210#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001211 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001212 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1213 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001214 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001215 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001216 {
1217 /*
1218 * Generate IV
1219 */
Paul Bakker48916f92012-09-16 19:57:18 +00001220 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1221 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001222 if( ret != 0 )
1223 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001224
Paul Bakker92be97b2013-01-02 17:30:03 +01001225 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001226 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001227
1228 /*
1229 * Fix pointer positions and message length with added IV
1230 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001231 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001232 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001233 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001234 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001235#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001239 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
1241 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001242 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Paul Bakker45125bc2013-09-04 16:47:11 +02001244 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001246 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1247 return( ret );
1248 }
1249
1250 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1251 ssl->transform_out->iv_enc,
1252 ssl->transform_out->ivlen ) ) != 0 )
1253 {
1254 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001255 return( ret );
1256 }
Paul Bakker68884e32013-01-07 18:20:04 +01001257
Paul Bakkercca5b812013-08-31 17:40:26 +02001258 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1259 enc_msg, enc_msglen, enc_msg,
1260 &olen ) ) != 0 )
1261 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001262 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001263 return( ret );
1264 }
Paul Bakker68884e32013-01-07 18:20:04 +01001265
Paul Bakkercca5b812013-08-31 17:40:26 +02001266 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001267
Paul Bakkercca5b812013-08-31 17:40:26 +02001268 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001269 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001270 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001271 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 return( ret );
1273 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001274
Paul Bakkercca5b812013-08-31 17:40:26 +02001275 if( enc_msglen != olen )
1276 {
1277 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1278 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001279 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001280 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001281
1282#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001283 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1284 {
1285 /*
1286 * Save IV in SSL3 and TLS1
1287 */
1288 memcpy( ssl->transform_out->iv_enc,
1289 ssl->transform_out->cipher_ctx_enc.iv,
1290 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001292#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001294 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001295#endif /* POLARSSL_CIPHER_MODE_CBC &&
1296 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001297 {
1298 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1299 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
Paul Bakkerca4ab492012-04-18 14:23:57 +00001302 for( i = 8; i > 0; i-- )
1303 if( ++ssl->out_ctr[i - 1] != 0 )
1304 break;
1305
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1307
1308 return( 0 );
1309}
1310
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001311#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001312
Paul Bakker5121ce52009-01-03 21:22:43 +00001313static int ssl_decrypt_buf( ssl_context *ssl )
1314{
Paul Bakker45829992013-01-03 14:52:21 +01001315 size_t i, padlen = 0, correct = 1;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001316 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
1318 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1319
Paul Bakker48916f92012-09-16 19:57:18 +00001320 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 {
1322 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001323 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001324 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
1326
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001327#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001328 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1329 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001330 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001331 int ret;
1332 size_t olen = 0;
1333
Paul Bakker68884e32013-01-07 18:20:04 +01001334 padlen = 0;
1335
Paul Bakker45125bc2013-09-04 16:47:11 +02001336 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001337 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001338 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1339 return( ret );
1340 }
1341
1342 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1343 ssl->transform_in->iv_dec,
1344 ssl->transform_in->ivlen ) ) != 0 )
1345 {
1346 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001347 return( ret );
1348 }
1349
1350 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1351 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1352 &olen ) ) != 0 )
1353 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001354 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001355 return( ret );
1356 }
1357
1358 if( ssl->in_msglen != olen )
1359 {
1360 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001361 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001362 }
1363
1364 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001365 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001366 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001367 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001368 return( ret );
1369 }
1370
1371 if( 0 != olen )
1372 {
1373 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001374 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 }
Paul Bakker68884e32013-01-07 18:20:04 +01001377 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001378#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001379#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001380 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1381 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001382 {
1383 unsigned char *dec_msg;
1384 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001385 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001386 unsigned char add_data[13];
1387 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1388
Paul Bakker68884e32013-01-07 18:20:04 +01001389 padlen = 0;
1390
1391 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1392 ssl->transform_in->fixed_ivlen );
1393 dec_msglen -= 16;
1394 dec_msg = ssl->in_msg;
1395 dec_msg_result = ssl->in_msg;
1396 ssl->in_msglen = dec_msglen;
1397
1398 memcpy( add_data, ssl->in_ctr, 8 );
1399 add_data[8] = ssl->in_msgtype;
1400 add_data[9] = ssl->major_ver;
1401 add_data[10] = ssl->minor_ver;
1402 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1403 add_data[12] = ssl->in_msglen & 0xFF;
1404
1405 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1406 add_data, 13 );
1407
1408 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1409 ssl->in_iv,
1410 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1411
1412 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1413 ssl->transform_in->ivlen );
1414 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1415
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001416 /*
1417 * Decrypt
1418 */
1419 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1420 ssl->transform_in->iv_dec,
1421 ssl->transform_in->ivlen ) ) != 0 ||
1422 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001423 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001424 return( ret );
1425 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001426
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001427 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1428 add_data, 13 ) ) != 0 )
1429 {
1430 return( ret );
1431 }
1432
1433 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1434 dec_msg, dec_msglen,
1435 dec_msg_result, &olen ) ) != 0 )
1436 {
1437 return( ret );
1438 }
1439 totlen = olen;
1440
1441 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1442 dec_msg_result + olen, &olen ) ) != 0 )
1443 {
1444 return( ret );
1445 }
1446 totlen += olen;
1447
1448 if( totlen != dec_msglen )
1449 {
1450 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1451 return( -1 );
1452 }
1453
1454 /*
1455 * Authenticate
1456 */
1457 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1458 dec_msg + dec_msglen, 16 ) ) != 0 )
1459 {
1460 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001461 return( POLARSSL_ERR_SSL_INVALID_MAC );
1462 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001463
Paul Bakkerca4ab492012-04-18 14:23:57 +00001464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001465 else
Paul Bakker68884e32013-01-07 18:20:04 +01001466#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001467#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1468 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001469 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1470 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 {
Paul Bakker45829992013-01-03 14:52:21 +01001472 /*
1473 * Decrypt and check the padding
1474 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001475 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001476 unsigned char *dec_msg;
1477 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001478 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001479 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001480 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001481
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 /*
Paul Bakker45829992013-01-03 14:52:21 +01001483 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 */
Paul Bakker48916f92012-09-16 19:57:18 +00001485 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 {
1487 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001488 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001489 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 }
1491
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001492#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001493 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1494 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001495#endif
Paul Bakker45829992013-01-03 14:52:21 +01001496
1497 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1498 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1499 {
1500 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1501 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1502 return( POLARSSL_ERR_SSL_INVALID_MAC );
1503 }
1504
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001505 dec_msglen = ssl->in_msglen;
1506 dec_msg = ssl->in_msg;
1507 dec_msg_result = ssl->in_msg;
1508
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001509#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001510 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001511 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001512 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001513 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001514 {
Paul Bakker48916f92012-09-16 19:57:18 +00001515 dec_msglen -= ssl->transform_in->ivlen;
1516 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001517
Paul Bakker48916f92012-09-16 19:57:18 +00001518 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001519 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001520 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001521#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001522
Paul Bakker45125bc2013-09-04 16:47:11 +02001523 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001525 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1526 return( ret );
1527 }
1528
1529 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1530 ssl->transform_in->iv_dec,
1531 ssl->transform_in->ivlen ) ) != 0 )
1532 {
1533 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001534 return( ret );
1535 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001536
Paul Bakkercca5b812013-08-31 17:40:26 +02001537 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1538 dec_msg, dec_msglen, dec_msg_result,
1539 &olen ) ) != 0 )
1540 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001541 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001542 return( ret );
1543 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001544
Paul Bakkercca5b812013-08-31 17:40:26 +02001545 dec_msglen -= olen;
1546 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001547 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001548 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001549 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001550 return( ret );
1551 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001552
Paul Bakkercca5b812013-08-31 17:40:26 +02001553 if( dec_msglen != olen )
1554 {
1555 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001556 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001557 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001558
1559#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001560 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1561 {
1562 /*
1563 * Save IV in SSL3 and TLS1
1564 */
1565 memcpy( ssl->transform_in->iv_dec,
1566 ssl->transform_in->cipher_ctx_dec.iv,
1567 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001569#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001570
1571 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001572
1573 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1574 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001575#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001576 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1577 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001578#endif
Paul Bakker45829992013-01-03 14:52:21 +01001579 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001580 correct = 0;
1581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001583#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001584 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1585 {
Paul Bakker48916f92012-09-16 19:57:18 +00001586 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001587 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001588#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1590 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001591 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001592#endif
Paul Bakker45829992013-01-03 14:52:21 +01001593 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 }
1595 }
1596 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001597#endif
1598#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1599 defined(POLARSSL_SSL_PROTO_TLS1_2)
1600 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001601 {
1602 /*
Paul Bakker45829992013-01-03 14:52:21 +01001603 * TLSv1+: always check the padding up to the first failure
1604 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001606 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001607 size_t padding_idx = ssl->in_msglen - padlen - 1;
1608
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001609 for( i = 1; i <= 256; i++ )
1610 {
1611 real_count &= ( i <= padlen );
1612 pad_count += real_count *
1613 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1614 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001615
1616 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001617
Paul Bakkerd66f0702013-01-31 16:57:45 +01001618#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001619 if( padlen > 0 && correct == 0)
1620 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001621#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001622 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001623 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001624 else
1625#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1626 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001627 {
1628 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001629 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001630 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001631 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001632 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001633#endif /* POLARSSL_CIPHER_MODE_CBC &&
1634 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001635 {
1636 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1637 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1638 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001639
1640 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1641 ssl->in_msg, ssl->in_msglen );
1642
1643 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001644 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001646#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1647 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1648 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1649 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1650 POLARSSL_MODE_GCM )
1651 {
1652 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001654 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1655 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001657 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001659#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001660 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1661 {
1662 ssl_mac( &ssl->transform_in->md_ctx_dec,
1663 ssl->transform_in->mac_dec,
1664 ssl->in_msg, ssl->in_msglen,
1665 ssl->in_ctr, ssl->in_msgtype );
1666 }
1667 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001668#endif /* POLARSSL_SSL_PROTO_SSL3 */
1669#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001670 defined(POLARSSL_SSL_PROTO_TLS1_2)
1671 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1672 {
1673 /*
1674 * Process MAC and always update for padlen afterwards to make
1675 * total time independent of padlen
1676 *
1677 * extra_run compensates MAC check for padlen
1678 *
1679 * Known timing attacks:
1680 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1681 *
1682 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1683 * correctly. (We round down instead of up, so -56 is the correct
1684 * value for our calculations instead of -55)
1685 */
1686 size_t j, extra_run = 0;
1687 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1688 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001689
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001690 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001691
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001692 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1693 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1694 ssl->in_msglen );
1695 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1696 ssl->in_msg + ssl->in_msglen );
1697 for( j = 0; j < extra_run; j++ )
1698 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001699
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001700 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1701 }
1702 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001703#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001704 POLARSSL_SSL_PROTO_TLS1_2 */
1705 {
1706 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1707 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1708 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001709
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001710 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1711 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1712 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001713
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001714 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001715 ssl->transform_in->maclen ) != 0 )
1716 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001717#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001718 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001719#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001720 correct = 0;
1721 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001723 /*
1724 * Finally check the correct flag
1725 */
1726 if( correct == 0 )
1727 return( POLARSSL_ERR_SSL_INVALID_MAC );
1728 }
1729#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
1731 if( ssl->in_msglen == 0 )
1732 {
1733 ssl->nb_zero++;
1734
1735 /*
1736 * Three or more empty messages may be a DoS attack
1737 * (excessive CPU consumption).
1738 */
1739 if( ssl->nb_zero > 3 )
1740 {
1741 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1742 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001743 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744 }
1745 }
1746 else
1747 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001748
Paul Bakker23986e52011-04-24 08:57:21 +00001749 for( i = 8; i > 0; i-- )
1750 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001751 break;
1752
1753 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1754
1755 return( 0 );
1756}
1757
Paul Bakker2770fbd2012-07-03 13:30:23 +00001758#if defined(POLARSSL_ZLIB_SUPPORT)
1759/*
1760 * Compression/decompression functions
1761 */
1762static int ssl_compress_buf( ssl_context *ssl )
1763{
1764 int ret;
1765 unsigned char *msg_post = ssl->out_msg;
1766 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001767 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001768
1769 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1770
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001771 if( len_pre == 0 )
1772 return( 0 );
1773
Paul Bakker2770fbd2012-07-03 13:30:23 +00001774 memcpy( msg_pre, ssl->out_msg, len_pre );
1775
1776 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1777 ssl->out_msglen ) );
1778
1779 SSL_DEBUG_BUF( 4, "before compression: output payload",
1780 ssl->out_msg, ssl->out_msglen );
1781
Paul Bakker48916f92012-09-16 19:57:18 +00001782 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1783 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1784 ssl->transform_out->ctx_deflate.next_out = msg_post;
1785 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001786
Paul Bakker48916f92012-09-16 19:57:18 +00001787 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001788 if( ret != Z_OK )
1789 {
1790 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1791 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1792 }
1793
Paul Bakker48916f92012-09-16 19:57:18 +00001794 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795
Paul Bakker2770fbd2012-07-03 13:30:23 +00001796 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1797 ssl->out_msglen ) );
1798
1799 SSL_DEBUG_BUF( 4, "after compression: output payload",
1800 ssl->out_msg, ssl->out_msglen );
1801
1802 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1803
1804 return( 0 );
1805}
1806
1807static int ssl_decompress_buf( ssl_context *ssl )
1808{
1809 int ret;
1810 unsigned char *msg_post = ssl->in_msg;
1811 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001812 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001813
1814 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1815
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001816 if( len_pre == 0 )
1817 return( 0 );
1818
Paul Bakker2770fbd2012-07-03 13:30:23 +00001819 memcpy( msg_pre, ssl->in_msg, len_pre );
1820
1821 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1822 ssl->in_msglen ) );
1823
1824 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1825 ssl->in_msg, ssl->in_msglen );
1826
Paul Bakker48916f92012-09-16 19:57:18 +00001827 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1828 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1829 ssl->transform_in->ctx_inflate.next_out = msg_post;
1830 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001831
Paul Bakker48916f92012-09-16 19:57:18 +00001832 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001833 if( ret != Z_OK )
1834 {
1835 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1836 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1837 }
1838
Paul Bakker48916f92012-09-16 19:57:18 +00001839 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001840
Paul Bakker2770fbd2012-07-03 13:30:23 +00001841 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1842 ssl->in_msglen ) );
1843
1844 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1845 ssl->in_msg, ssl->in_msglen );
1846
1847 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1848
1849 return( 0 );
1850}
1851#endif /* POLARSSL_ZLIB_SUPPORT */
1852
Paul Bakker5121ce52009-01-03 21:22:43 +00001853/*
1854 * Fill the input message buffer
1855 */
Paul Bakker23986e52011-04-24 08:57:21 +00001856int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001857{
Paul Bakker23986e52011-04-24 08:57:21 +00001858 int ret;
1859 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
1861 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1862
1863 while( ssl->in_left < nb_want )
1864 {
1865 len = nb_want - ssl->in_left;
1866 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1867
1868 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1869 ssl->in_left, nb_want ) );
1870 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1871
Paul Bakker831a7552011-05-18 13:32:51 +00001872 if( ret == 0 )
1873 return( POLARSSL_ERR_SSL_CONN_EOF );
1874
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 if( ret < 0 )
1876 return( ret );
1877
1878 ssl->in_left += ret;
1879 }
1880
1881 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1882
1883 return( 0 );
1884}
1885
1886/*
1887 * Flush any data not yet written
1888 */
1889int ssl_flush_output( ssl_context *ssl )
1890{
1891 int ret;
1892 unsigned char *buf;
1893
1894 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1895
1896 while( ssl->out_left > 0 )
1897 {
1898 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1899 5 + ssl->out_msglen, ssl->out_left ) );
1900
Paul Bakker5bd42292012-12-19 14:40:42 +01001901 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001903
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1905
1906 if( ret <= 0 )
1907 return( ret );
1908
1909 ssl->out_left -= ret;
1910 }
1911
1912 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1913
1914 return( 0 );
1915}
1916
1917/*
1918 * Record layer functions
1919 */
1920int ssl_write_record( ssl_context *ssl )
1921{
Paul Bakker05ef8352012-05-08 09:17:57 +00001922 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001923 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
1925 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1928 {
1929 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1930 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1931 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1932
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001933 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1934 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 }
1936
Paul Bakker2770fbd2012-07-03 13:30:23 +00001937#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001938 if( ssl->transform_out != NULL &&
1939 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001940 {
1941 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1942 {
1943 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1944 return( ret );
1945 }
1946
1947 len = ssl->out_msglen;
1948 }
1949#endif /*POLARSSL_ZLIB_SUPPORT */
1950
Paul Bakker05ef8352012-05-08 09:17:57 +00001951#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1952 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001954 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001955
Paul Bakker05ef8352012-05-08 09:17:57 +00001956 ret = ssl_hw_record_write( ssl );
1957 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1958 {
1959 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1960 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1961 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001962
1963 if( ret == 0 )
1964 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001965 }
1966#endif
1967 if( !done )
1968 {
1969 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1970 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1971 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1973 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001974
Paul Bakker48916f92012-09-16 19:57:18 +00001975 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001976 {
1977 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1978 {
1979 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1980 return( ret );
1981 }
1982
1983 len = ssl->out_msglen;
1984 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1985 ssl->out_hdr[4] = (unsigned char)( len );
1986 }
1987
1988 ssl->out_left = 5 + ssl->out_msglen;
1989
1990 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1991 "version = [%d:%d], msglen = %d",
1992 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1993 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1994
1995 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001996 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 }
1998
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2000 {
2001 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2002 return( ret );
2003 }
2004
2005 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2006
2007 return( 0 );
2008}
2009
2010int ssl_read_record( ssl_context *ssl )
2011{
Paul Bakker05ef8352012-05-08 09:17:57 +00002012 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
2014 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2015
Paul Bakker68884e32013-01-07 18:20:04 +01002016 SSL_DEBUG_BUF( 4, "input record from network",
2017 ssl->in_hdr, 5 + ssl->in_msglen );
2018
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 if( ssl->in_hslen != 0 &&
2020 ssl->in_hslen < ssl->in_msglen )
2021 {
2022 /*
2023 * Get next Handshake message in the current record
2024 */
2025 ssl->in_msglen -= ssl->in_hslen;
2026
Paul Bakker8934a982011-08-05 11:11:53 +00002027 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002028 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002029
2030 ssl->in_hslen = 4;
2031 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2032
2033 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2034 " %d, type = %d, hslen = %d",
2035 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2036
2037 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2038 {
2039 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002040 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 }
2042
2043 if( ssl->in_msglen < ssl->in_hslen )
2044 {
2045 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002046 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 }
2048
Paul Bakker48916f92012-09-16 19:57:18 +00002049 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002050
2051 return( 0 );
2052 }
2053
2054 ssl->in_hslen = 0;
2055
2056 /*
2057 * Read the record header and validate it
2058 */
2059 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2060 {
2061 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2062 return( ret );
2063 }
2064
2065 ssl->in_msgtype = ssl->in_hdr[0];
2066 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2067
2068 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2069 "version = [%d:%d], msglen = %d",
2070 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2071 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2072
2073 if( ssl->in_hdr[1] != ssl->major_ver )
2074 {
2075 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002076 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 }
2078
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002079 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 {
2081 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002082 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 }
2084
2085 /*
2086 * Make sure the message length is acceptable
2087 */
Paul Bakker48916f92012-09-16 19:57:18 +00002088 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 {
2090 if( ssl->in_msglen < 1 ||
2091 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2092 {
2093 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002094 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 }
2096 }
2097 else
2098 {
Paul Bakker48916f92012-09-16 19:57:18 +00002099 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 {
2101 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002102 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 }
2104
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002105#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002107 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 {
2109 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002110 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002112#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002113
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002114#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2115 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 /*
2117 * TLS encrypted messages can have up to 256 bytes of padding
2118 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002119 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002120 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
2122 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002123 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002125#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
2127
2128 /*
2129 * Read and optionally decrypt the message contents
2130 */
2131 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2132 {
2133 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2134 return( ret );
2135 }
2136
2137 SSL_DEBUG_BUF( 4, "input record from network",
2138 ssl->in_hdr, 5 + ssl->in_msglen );
2139
Paul Bakker05ef8352012-05-08 09:17:57 +00002140#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2141 if( ssl_hw_record_read != NULL)
2142 {
2143 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2144
2145 ret = ssl_hw_record_read( ssl );
2146 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2147 {
2148 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2149 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2150 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002151
2152 if( ret == 0 )
2153 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002154 }
2155#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002156 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 {
2158 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2159 {
Paul Bakker40865c82013-01-31 17:13:13 +01002160#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2161 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2162 {
2163 ssl_send_alert_message( ssl,
2164 SSL_ALERT_LEVEL_FATAL,
2165 SSL_ALERT_MSG_BAD_RECORD_MAC );
2166 }
2167#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2169 return( ret );
2170 }
2171
2172 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2173 ssl->in_msg, ssl->in_msglen );
2174
2175 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2176 {
2177 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002178 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 }
2180 }
2181
Paul Bakker2770fbd2012-07-03 13:30:23 +00002182#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002183 if( ssl->transform_in != NULL &&
2184 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002185 {
2186 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2187 {
2188 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2189 return( ret );
2190 }
2191
2192 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2193 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2194 }
2195#endif /* POLARSSL_ZLIB_SUPPORT */
2196
Paul Bakker0a925182012-04-16 06:46:41 +00002197 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2198 ssl->in_msgtype != SSL_MSG_ALERT &&
2199 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2200 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2203
Paul Bakker48916f92012-09-16 19:57:18 +00002204 if( ( ret = ssl_send_alert_message( ssl,
2205 SSL_ALERT_LEVEL_FATAL,
2206 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002207 {
2208 return( ret );
2209 }
2210
2211 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2212 }
2213
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2215 {
2216 ssl->in_hslen = 4;
2217 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2218
2219 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2220 " %d, type = %d, hslen = %d",
2221 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2222
2223 /*
2224 * Additional checks to validate the handshake header
2225 */
2226 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2227 {
2228 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002229 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 }
2231
2232 if( ssl->in_msglen < ssl->in_hslen )
2233 {
2234 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002235 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 }
2237
Paul Bakker48916f92012-09-16 19:57:18 +00002238 if( ssl->state != SSL_HANDSHAKE_OVER )
2239 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 }
2241
2242 if( ssl->in_msgtype == SSL_MSG_ALERT )
2243 {
2244 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2245 ssl->in_msg[0], ssl->in_msg[1] ) );
2246
2247 /*
2248 * Ignore non-fatal alerts, except close_notify
2249 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002250 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002252 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2253 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002254 /**
2255 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2256 * error identifier.
2257 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002258 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 }
2260
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002261 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2262 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 {
2264 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002265 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 }
2267 }
2268
2269 ssl->in_left = 0;
2270
2271 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2272
2273 return( 0 );
2274}
2275
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002276int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2277{
2278 int ret;
2279
2280 if( ( ret = ssl_send_alert_message( ssl,
2281 SSL_ALERT_LEVEL_FATAL,
2282 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2283 {
2284 return( ret );
2285 }
2286
2287 return( 0 );
2288}
2289
Paul Bakker0a925182012-04-16 06:46:41 +00002290int ssl_send_alert_message( ssl_context *ssl,
2291 unsigned char level,
2292 unsigned char message )
2293{
2294 int ret;
2295
2296 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2297
2298 ssl->out_msgtype = SSL_MSG_ALERT;
2299 ssl->out_msglen = 2;
2300 ssl->out_msg[0] = level;
2301 ssl->out_msg[1] = message;
2302
2303 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2304 {
2305 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2306 return( ret );
2307 }
2308
2309 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2310
2311 return( 0 );
2312}
2313
Paul Bakker5121ce52009-01-03 21:22:43 +00002314/*
2315 * Handshake functions
2316 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002317#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2318 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002319 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2320 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002321int ssl_write_certificate( ssl_context *ssl )
2322{
Paul Bakkered27a042013-04-18 22:46:23 +02002323 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002324 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
2326 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2327
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002328 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002329 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2330 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002331 {
2332 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2333 ssl->state++;
2334 return( 0 );
2335 }
2336
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002337 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002338 return( ret );
2339}
2340
2341int ssl_parse_certificate( ssl_context *ssl )
2342{
2343 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2344 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2345
2346 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2347
2348 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002349 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2350 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002351 {
2352 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2353 ssl->state++;
2354 return( 0 );
2355 }
2356
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002357 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002358 return( ret );
2359}
2360#else
2361int ssl_write_certificate( ssl_context *ssl )
2362{
2363 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2364 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002365 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002366 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2367
2368 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2369
2370 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002371 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2372 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373 {
2374 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2375 ssl->state++;
2376 return( 0 );
2377 }
2378
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 if( ssl->endpoint == SSL_IS_CLIENT )
2380 {
2381 if( ssl->client_auth == 0 )
2382 {
2383 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2384 ssl->state++;
2385 return( 0 );
2386 }
2387
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002388#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 /*
2390 * If using SSLv3 and got no cert, send an Alert message
2391 * (otherwise an empty Certificate message will be sent).
2392 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002393 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2395 {
2396 ssl->out_msglen = 2;
2397 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002398 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2399 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
2401 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2402 goto write_msg;
2403 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002404#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 }
2406 else /* SSL_IS_SERVER */
2407 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002408 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 {
2410 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002411 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 }
2413 }
2414
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002415 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002416
2417 /*
2418 * 0 . 0 handshake type
2419 * 1 . 3 handshake length
2420 * 4 . 6 length of all certs
2421 * 7 . 9 length of cert. 1
2422 * 10 . n-1 peer certificate
2423 * n . n+2 length of cert. 2
2424 * n+3 . ... upper level cert, etc.
2425 */
2426 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002427 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
Paul Bakker29087132010-03-21 21:03:34 +00002429 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 {
2431 n = crt->raw.len;
2432 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
2433 {
2434 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2435 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002436 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 }
2438
2439 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2440 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2441 ssl->out_msg[i + 2] = (unsigned char)( n );
2442
2443 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2444 i += n; crt = crt->next;
2445 }
2446
2447 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2448 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2449 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2450
2451 ssl->out_msglen = i;
2452 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2453 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2454
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002455#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002456write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002457#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
2459 ssl->state++;
2460
2461 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2462 {
2463 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2464 return( ret );
2465 }
2466
2467 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2468
Paul Bakkered27a042013-04-18 22:46:23 +02002469 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470}
2471
2472int ssl_parse_certificate( ssl_context *ssl )
2473{
Paul Bakkered27a042013-04-18 22:46:23 +02002474 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002475 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002476 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
2478 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2479
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002480 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002481 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2482 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002483 {
2484 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2485 ssl->state++;
2486 return( 0 );
2487 }
2488
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 if( ssl->endpoint == SSL_IS_SERVER &&
2490 ssl->authmode == SSL_VERIFY_NONE )
2491 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002492 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2494 ssl->state++;
2495 return( 0 );
2496 }
2497
2498 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2499 {
2500 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2501 return( ret );
2502 }
2503
2504 ssl->state++;
2505
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002506#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 /*
2508 * Check if the client sent an empty certificate
2509 */
2510 if( ssl->endpoint == SSL_IS_SERVER &&
2511 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2512 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002513 if( ssl->in_msglen == 2 &&
2514 ssl->in_msgtype == SSL_MSG_ALERT &&
2515 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2516 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 {
2518 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2519
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002520 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2522 return( 0 );
2523 else
Paul Bakker40e46942009-01-03 21:51:57 +00002524 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 }
2526 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002527#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002529#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2530 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 if( ssl->endpoint == SSL_IS_SERVER &&
2532 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2533 {
2534 if( ssl->in_hslen == 7 &&
2535 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2536 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2537 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2538 {
2539 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2540
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002541 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002543 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 else
2545 return( 0 );
2546 }
2547 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002548#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2549 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
2551 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2552 {
2553 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002554 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 }
2556
2557 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2558 {
2559 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002560 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 }
2562
2563 /*
2564 * Same message structure as in ssl_write_certificate()
2565 */
2566 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2567
2568 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2569 {
2570 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002571 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 }
2573
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002574 /* In case we tried to reuse a session but it failed */
2575 if( ssl->session_negotiate->peer_cert != NULL )
2576 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002577 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002578 polarssl_free( ssl->session_negotiate->peer_cert );
2579 }
2580
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002581 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2582 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 {
2584 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002585 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002586 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 }
2588
Paul Bakkerb6b09562013-09-18 14:17:41 +02002589 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590
2591 i = 7;
2592
2593 while( i < ssl->in_hslen )
2594 {
2595 if( ssl->in_msg[i] != 0 )
2596 {
2597 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002598 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
2600
2601 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2602 | (unsigned int) ssl->in_msg[i + 2];
2603 i += 3;
2604
2605 if( n < 128 || i + n > ssl->in_hslen )
2606 {
2607 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002608 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
2610
Paul Bakkerddf26b42013-09-18 13:46:23 +02002611 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2612 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 if( ret != 0 )
2614 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002615 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 return( ret );
2617 }
2618
2619 i += n;
2620 }
2621
Paul Bakker48916f92012-09-16 19:57:18 +00002622 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
2624 if( ssl->authmode != SSL_VERIFY_NONE )
2625 {
2626 if( ssl->ca_chain == NULL )
2627 {
2628 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002629 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 }
2631
Paul Bakkerddf26b42013-09-18 13:46:23 +02002632 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2633 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2634 &ssl->session_negotiate->verify_result,
2635 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
2637 if( ret != 0 )
2638 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2639
2640 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2641 ret = 0;
2642 }
2643
2644 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2645
2646 return( ret );
2647}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002648#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2649 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2650 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
2652int ssl_write_change_cipher_spec( ssl_context *ssl )
2653{
2654 int ret;
2655
2656 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2657
2658 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2659 ssl->out_msglen = 1;
2660 ssl->out_msg[0] = 1;
2661
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 ssl->state++;
2663
2664 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2665 {
2666 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2667 return( ret );
2668 }
2669
2670 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2671
2672 return( 0 );
2673}
2674
2675int ssl_parse_change_cipher_spec( ssl_context *ssl )
2676{
2677 int ret;
2678
2679 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2680
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2682 {
2683 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2684 return( ret );
2685 }
2686
2687 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2688 {
2689 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002690 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 }
2692
2693 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2694 {
2695 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002696 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 }
2698
2699 ssl->state++;
2700
2701 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2702
2703 return( 0 );
2704}
2705
Paul Bakker41c83d32013-03-20 14:39:14 +01002706void ssl_optimize_checksum( ssl_context *ssl,
2707 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002708{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002709 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002710
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002711#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2712 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002713 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002714 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002715 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002716#endif
2717#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2718#if defined(POLARSSL_SHA512_C)
2719 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2720 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2721 else
2722#endif
2723#if defined(POLARSSL_SHA256_C)
2724 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002725 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002726 else
2727#endif
2728#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2729 /* Should never happen */
2730 return;
Paul Bakker380da532012-04-18 16:10:25 +00002731}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002732
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002733static void ssl_update_checksum_start( ssl_context *ssl,
2734 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002735{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002736#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2737 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002738 md5_update( &ssl->handshake->fin_md5 , buf, len );
2739 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002740#endif
2741#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2742#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002743 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002744#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002745#if defined(POLARSSL_SHA512_C)
2746 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002747#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002748#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002749}
2750
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002751#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2752 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002753static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2754 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002755{
Paul Bakker48916f92012-09-16 19:57:18 +00002756 md5_update( &ssl->handshake->fin_md5 , buf, len );
2757 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002758}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002759#endif
Paul Bakker380da532012-04-18 16:10:25 +00002760
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002761#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2762#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002763static void ssl_update_checksum_sha256( ssl_context *ssl,
2764 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002765{
Paul Bakker9e36f042013-06-30 14:34:05 +02002766 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002767}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002768#endif
Paul Bakker380da532012-04-18 16:10:25 +00002769
Paul Bakker9e36f042013-06-30 14:34:05 +02002770#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002771static void ssl_update_checksum_sha384( ssl_context *ssl,
2772 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002773{
Paul Bakker9e36f042013-06-30 14:34:05 +02002774 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002775}
Paul Bakker769075d2012-11-24 11:26:46 +01002776#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002777#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002778
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002779#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002780static void ssl_calc_finished_ssl(
2781 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002782{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002783 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002784 md5_context md5;
2785 sha1_context sha1;
2786
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 unsigned char padbuf[48];
2788 unsigned char md5sum[16];
2789 unsigned char sha1sum[20];
2790
Paul Bakker48916f92012-09-16 19:57:18 +00002791 ssl_session *session = ssl->session_negotiate;
2792 if( !session )
2793 session = ssl->session;
2794
Paul Bakker1ef83d62012-04-11 12:09:53 +00002795 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2796
Paul Bakker48916f92012-09-16 19:57:18 +00002797 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2798 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
2800 /*
2801 * SSLv3:
2802 * hash =
2803 * MD5( master + pad2 +
2804 * MD5( handshake + sender + master + pad1 ) )
2805 * + SHA1( master + pad2 +
2806 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 */
2808
Paul Bakker90995b52013-06-24 19:20:35 +02002809#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002810 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002811 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002812#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Paul Bakker90995b52013-06-24 19:20:35 +02002814#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002816 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002817#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002818
Paul Bakker3c2122f2013-06-24 19:03:14 +02002819 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2820 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
Paul Bakker1ef83d62012-04-11 12:09:53 +00002822 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
Paul Bakker3c2122f2013-06-24 19:03:14 +02002824 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002825 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002826 md5_update( &md5, padbuf, 48 );
2827 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
Paul Bakker3c2122f2013-06-24 19:03:14 +02002829 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002830 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002831 sha1_update( &sha1, padbuf, 40 );
2832 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Paul Bakker1ef83d62012-04-11 12:09:53 +00002834 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Paul Bakker1ef83d62012-04-11 12:09:53 +00002836 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002837 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002838 md5_update( &md5, padbuf, 48 );
2839 md5_update( &md5, md5sum, 16 );
2840 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Paul Bakker1ef83d62012-04-11 12:09:53 +00002842 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002843 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002844 sha1_update( &sha1, padbuf , 40 );
2845 sha1_update( &sha1, sha1sum, 20 );
2846 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Paul Bakker1ef83d62012-04-11 12:09:53 +00002848 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Paul Bakker1ef83d62012-04-11 12:09:53 +00002850 memset( &md5, 0, sizeof( md5_context ) );
2851 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
2853 memset( padbuf, 0, sizeof( padbuf ) );
2854 memset( md5sum, 0, sizeof( md5sum ) );
2855 memset( sha1sum, 0, sizeof( sha1sum ) );
2856
2857 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2858}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002859#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002861#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002862static void ssl_calc_finished_tls(
2863 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002864{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002866 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002867 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakker48916f92012-09-16 19:57:18 +00002871 ssl_session *session = ssl->session_negotiate;
2872 if( !session )
2873 session = ssl->session;
2874
Paul Bakker1ef83d62012-04-11 12:09:53 +00002875 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Paul Bakker48916f92012-09-16 19:57:18 +00002877 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2878 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
Paul Bakker1ef83d62012-04-11 12:09:53 +00002880 /*
2881 * TLSv1:
2882 * hash = PRF( master, finished_label,
2883 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2884 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
Paul Bakker90995b52013-06-24 19:20:35 +02002886#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002887 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2888 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002889#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002890
Paul Bakker90995b52013-06-24 19:20:35 +02002891#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002892 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2893 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002894#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002895
2896 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002897 ? "client finished"
2898 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899
2900 md5_finish( &md5, padbuf );
2901 sha1_finish( &sha1, padbuf + 16 );
2902
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002903 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002904 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002905
2906 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2907
2908 memset( &md5, 0, sizeof( md5_context ) );
2909 memset( &sha1, 0, sizeof( sha1_context ) );
2910
2911 memset( padbuf, 0, sizeof( padbuf ) );
2912
2913 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2914}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002915#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002916
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002917#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2918#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002919static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002920 ssl_context *ssl, unsigned char *buf, int from )
2921{
2922 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002923 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002924 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925 unsigned char padbuf[32];
2926
Paul Bakker48916f92012-09-16 19:57:18 +00002927 ssl_session *session = ssl->session_negotiate;
2928 if( !session )
2929 session = ssl->session;
2930
Paul Bakker380da532012-04-18 16:10:25 +00002931 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002932
Paul Bakker9e36f042013-06-30 14:34:05 +02002933 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002934
2935 /*
2936 * TLSv1.2:
2937 * hash = PRF( master, finished_label,
2938 * Hash( handshake ) )[0.11]
2939 */
2940
Paul Bakker9e36f042013-06-30 14:34:05 +02002941#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002942 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002943 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002944#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945
2946 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002947 ? "client finished"
2948 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949
Paul Bakker9e36f042013-06-30 14:34:05 +02002950 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002952 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002953 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954
2955 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2956
Paul Bakker9e36f042013-06-30 14:34:05 +02002957 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958
2959 memset( padbuf, 0, sizeof( padbuf ) );
2960
2961 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2962}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002963#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964
Paul Bakker9e36f042013-06-30 14:34:05 +02002965#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002966static void ssl_calc_finished_tls_sha384(
2967 ssl_context *ssl, unsigned char *buf, int from )
2968{
2969 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002970 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002971 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002972 unsigned char padbuf[48];
2973
Paul Bakker48916f92012-09-16 19:57:18 +00002974 ssl_session *session = ssl->session_negotiate;
2975 if( !session )
2976 session = ssl->session;
2977
Paul Bakker380da532012-04-18 16:10:25 +00002978 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002979
Paul Bakker9e36f042013-06-30 14:34:05 +02002980 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002981
2982 /*
2983 * TLSv1.2:
2984 * hash = PRF( master, finished_label,
2985 * Hash( handshake ) )[0.11]
2986 */
2987
Paul Bakker9e36f042013-06-30 14:34:05 +02002988#if !defined(POLARSSL_SHA512_ALT)
2989 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2990 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002991#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00002992
2993 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002994 ? "client finished"
2995 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00002996
Paul Bakker9e36f042013-06-30 14:34:05 +02002997 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002998
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002999 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003000 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003001
3002 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3003
Paul Bakker9e36f042013-06-30 14:34:05 +02003004 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003005
3006 memset( padbuf, 0, sizeof( padbuf ) );
3007
3008 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3009}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003010#endif /* POLARSSL_SHA512_C */
3011#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003012
Paul Bakker48916f92012-09-16 19:57:18 +00003013void ssl_handshake_wrapup( ssl_context *ssl )
3014{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003015 int resume = ssl->handshake->resume;
3016
Paul Bakker48916f92012-09-16 19:57:18 +00003017 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3018
3019 /*
3020 * Free our handshake params
3021 */
3022 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003023 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003024 ssl->handshake = NULL;
3025
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003026 if( ssl->renegotiation == SSL_RENEGOTIATION )
3027 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3028
Paul Bakker48916f92012-09-16 19:57:18 +00003029 /*
3030 * Switch in our now active transform context
3031 */
3032 if( ssl->transform )
3033 {
3034 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003035 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003036 }
3037 ssl->transform = ssl->transform_negotiate;
3038 ssl->transform_negotiate = NULL;
3039
Paul Bakker0a597072012-09-25 21:55:46 +00003040 if( ssl->session )
3041 {
3042 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003043 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003044 }
3045 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003046 ssl->session_negotiate = NULL;
3047
Paul Bakker0a597072012-09-25 21:55:46 +00003048 /*
3049 * Add cache entry
3050 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003051 if( ssl->f_set_cache != NULL &&
3052 ssl->session->length != 0 &&
3053 resume == 0 )
3054 {
Paul Bakker0a597072012-09-25 21:55:46 +00003055 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3056 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003057 }
Paul Bakker0a597072012-09-25 21:55:46 +00003058
Paul Bakker48916f92012-09-16 19:57:18 +00003059 ssl->state++;
3060
3061 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3062}
3063
Paul Bakker1ef83d62012-04-11 12:09:53 +00003064int ssl_write_finished( ssl_context *ssl )
3065{
3066 int ret, hash_len;
3067
3068 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3069
Paul Bakker92be97b2013-01-02 17:30:03 +01003070 /*
3071 * Set the out_msg pointer to the correct location based on IV length
3072 */
3073 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3074 {
3075 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3076 ssl->transform_negotiate->fixed_ivlen;
3077 }
3078 else
3079 ssl->out_msg = ssl->out_iv;
3080
Paul Bakker48916f92012-09-16 19:57:18 +00003081 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003082
3083 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3085
Paul Bakker48916f92012-09-16 19:57:18 +00003086 ssl->verify_data_len = hash_len;
3087 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3088
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 ssl->out_msglen = 4 + hash_len;
3090 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3091 ssl->out_msg[0] = SSL_HS_FINISHED;
3092
3093 /*
3094 * In case of session resuming, invert the client and server
3095 * ChangeCipherSpec messages order.
3096 */
Paul Bakker0a597072012-09-25 21:55:46 +00003097 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 {
3099 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003100 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 else
3102 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3103 }
3104 else
3105 ssl->state++;
3106
Paul Bakker48916f92012-09-16 19:57:18 +00003107 /*
3108 * Switch to our negotiated transform and session parameters for outbound data.
3109 */
3110 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3111 ssl->transform_out = ssl->transform_negotiate;
3112 ssl->session_out = ssl->session_negotiate;
3113 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Paul Bakker07eb38b2012-12-19 14:42:06 +01003115#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3116 if( ssl_hw_record_activate != NULL)
3117 {
3118 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3119 {
3120 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3121 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3122 }
3123 }
3124#endif
3125
Paul Bakker5121ce52009-01-03 21:22:43 +00003126 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3127 {
3128 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3129 return( ret );
3130 }
3131
3132 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3133
3134 return( 0 );
3135}
3136
3137int ssl_parse_finished( ssl_context *ssl )
3138{
Paul Bakker23986e52011-04-24 08:57:21 +00003139 int ret;
3140 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003141 unsigned char buf[36];
3142
3143 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3144
Paul Bakker48916f92012-09-16 19:57:18 +00003145 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
Paul Bakker48916f92012-09-16 19:57:18 +00003147 /*
3148 * Switch to our negotiated transform and session parameters for inbound data.
3149 */
3150 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3151 ssl->transform_in = ssl->transform_negotiate;
3152 ssl->session_in = ssl->session_negotiate;
3153 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Paul Bakker92be97b2013-01-02 17:30:03 +01003155 /*
3156 * Set the in_msg pointer to the correct location based on IV length
3157 */
3158 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3159 {
3160 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3161 ssl->transform_negotiate->fixed_ivlen;
3162 }
3163 else
3164 ssl->in_msg = ssl->in_iv;
3165
Paul Bakker07eb38b2012-12-19 14:42:06 +01003166#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3167 if( ssl_hw_record_activate != NULL)
3168 {
3169 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3170 {
3171 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3172 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3173 }
3174 }
3175#endif
3176
Paul Bakker5121ce52009-01-03 21:22:43 +00003177 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3178 {
3179 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3180 return( ret );
3181 }
3182
3183 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3184 {
3185 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003186 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003187 }
3188
Paul Bakker1ef83d62012-04-11 12:09:53 +00003189 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003190 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3191
3192 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3193 ssl->in_hslen != 4 + hash_len )
3194 {
3195 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003196 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003197 }
3198
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003199 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 {
3201 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003202 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 }
3204
Paul Bakker48916f92012-09-16 19:57:18 +00003205 ssl->verify_data_len = hash_len;
3206 memcpy( ssl->peer_verify_data, buf, hash_len );
3207
Paul Bakker0a597072012-09-25 21:55:46 +00003208 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 {
3210 if( ssl->endpoint == SSL_IS_CLIENT )
3211 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3212
3213 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003214 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 }
3216 else
3217 ssl->state++;
3218
3219 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3220
3221 return( 0 );
3222}
3223
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003224static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003225{
3226 if( ssl->transform_negotiate )
3227 ssl_transform_free( ssl->transform_negotiate );
3228 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003229 {
3230 ssl->transform_negotiate =
3231 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3232 }
Paul Bakker48916f92012-09-16 19:57:18 +00003233
3234 if( ssl->session_negotiate )
3235 ssl_session_free( ssl->session_negotiate );
3236 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003237 {
3238 ssl->session_negotiate =
3239 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3240 }
Paul Bakker48916f92012-09-16 19:57:18 +00003241
3242 if( ssl->handshake )
3243 ssl_handshake_free( ssl->handshake );
3244 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003245 {
3246 ssl->handshake = (ssl_handshake_params *)
3247 polarssl_malloc( sizeof(ssl_handshake_params) );
3248 }
Paul Bakker48916f92012-09-16 19:57:18 +00003249
3250 if( ssl->handshake == NULL ||
3251 ssl->transform_negotiate == NULL ||
3252 ssl->session_negotiate == NULL )
3253 {
3254 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3255 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3256 }
3257
3258 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3259 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3260 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3261
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003262#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3263 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003264 md5_starts( &ssl->handshake->fin_md5 );
3265 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003266#endif
3267#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3268#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003269 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003270#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003271#if defined(POLARSSL_SHA512_C)
3272 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003273#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003274#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003275
3276 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003277 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003278
Paul Bakker61d113b2013-07-04 11:51:43 +02003279#if defined(POLARSSL_ECDH_C)
3280 ecdh_init( &ssl->handshake->ecdh_ctx );
3281#endif
3282
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003283#if defined(POLARSSL_X509_CRT_PARSE_C)
3284 ssl->handshake->key_cert = ssl->key_cert;
3285#endif
3286
Paul Bakker48916f92012-09-16 19:57:18 +00003287 return( 0 );
3288}
3289
Paul Bakker5121ce52009-01-03 21:22:43 +00003290/*
3291 * Initialize an SSL context
3292 */
3293int ssl_init( ssl_context *ssl )
3294{
Paul Bakker48916f92012-09-16 19:57:18 +00003295 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 int len = SSL_BUFFER_LEN;
3297
3298 memset( ssl, 0, sizeof( ssl_context ) );
3299
Paul Bakker62f2dee2012-09-28 07:31:51 +00003300 /*
3301 * Sane defaults
3302 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003303 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3304 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3305 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3306 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003307
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003308 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003309
Paul Bakker62f2dee2012-09-28 07:31:51 +00003310#if defined(POLARSSL_DHM_C)
3311 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3312 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3313 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3314 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3315 {
3316 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3317 return( ret );
3318 }
3319#endif
3320
3321 /*
3322 * Prepare base structures
3323 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003324 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003326 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003327 ssl->in_msg = ssl->in_ctr + 13;
3328
3329 if( ssl->in_ctr == NULL )
3330 {
3331 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003332 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 }
3334
Paul Bakker6e339b52013-07-03 13:37:05 +02003335 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003337 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003338 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003339
3340 if( ssl->out_ctr == NULL )
3341 {
3342 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003343 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003344 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 }
3346
3347 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3348 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3349
Paul Bakker606b4ba2013-08-14 16:52:14 +02003350#if defined(POLARSSL_SSL_SESSION_TICKETS)
3351 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3352#endif
3353
Paul Bakker48916f92012-09-16 19:57:18 +00003354 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3355 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003356
3357 return( 0 );
3358}
3359
3360/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003361 * Reset an initialized and used SSL context for re-use while retaining
3362 * all application-set variables, function pointers and data.
3363 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003364int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003365{
Paul Bakker48916f92012-09-16 19:57:18 +00003366 int ret;
3367
Paul Bakker7eb013f2011-10-06 12:37:39 +00003368 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003369 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3370 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3371
3372 ssl->verify_data_len = 0;
3373 memset( ssl->own_verify_data, 0, 36 );
3374 memset( ssl->peer_verify_data, 0, 36 );
3375
Paul Bakker7eb013f2011-10-06 12:37:39 +00003376 ssl->in_offt = NULL;
3377
Paul Bakker92be97b2013-01-02 17:30:03 +01003378 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003379 ssl->in_msgtype = 0;
3380 ssl->in_msglen = 0;
3381 ssl->in_left = 0;
3382
3383 ssl->in_hslen = 0;
3384 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003385 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003386
Paul Bakker92be97b2013-01-02 17:30:03 +01003387 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003388 ssl->out_msgtype = 0;
3389 ssl->out_msglen = 0;
3390 ssl->out_left = 0;
3391
Paul Bakker48916f92012-09-16 19:57:18 +00003392 ssl->transform_in = NULL;
3393 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003394
3395 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3396 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003397
3398#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3399 if( ssl_hw_record_reset != NULL)
3400 {
3401 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003402 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003403 {
3404 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3405 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3406 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003407 }
3408#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003409
Paul Bakker48916f92012-09-16 19:57:18 +00003410 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003411 {
Paul Bakker48916f92012-09-16 19:57:18 +00003412 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003413 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003414 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003415 }
Paul Bakker48916f92012-09-16 19:57:18 +00003416
Paul Bakkerc0463502013-02-14 11:19:38 +01003417 if( ssl->session )
3418 {
3419 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003420 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003421 ssl->session = NULL;
3422 }
3423
Paul Bakker48916f92012-09-16 19:57:18 +00003424 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3425 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003426
3427 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003428}
3429
Paul Bakkera503a632013-08-14 13:48:06 +02003430#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003431/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003432 * Allocate and initialize ticket keys
3433 */
3434static int ssl_ticket_keys_init( ssl_context *ssl )
3435{
3436 int ret;
3437 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003438 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003439
3440 if( ssl->ticket_keys != NULL )
3441 return( 0 );
3442
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003443 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3444 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003445 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3446
3447 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
3448 return( ret );
3449
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003450 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3451 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3452 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3453 {
3454 return( ret );
3455 }
3456
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003457 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
3458 return( ret );
3459
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003460 ssl->ticket_keys = tkeys;
3461
3462 return( 0 );
3463}
Paul Bakkera503a632013-08-14 13:48:06 +02003464#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003465
3466/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 * SSL set accessors
3468 */
3469void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3470{
3471 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003472
Paul Bakker606b4ba2013-08-14 16:52:14 +02003473#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003474 if( endpoint == SSL_IS_CLIENT )
3475 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003476#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003477}
3478
3479void ssl_set_authmode( ssl_context *ssl, int authmode )
3480{
3481 ssl->authmode = authmode;
3482}
3483
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003484#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003485void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003486 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003487 void *p_vrfy )
3488{
3489 ssl->f_vrfy = f_vrfy;
3490 ssl->p_vrfy = p_vrfy;
3491}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003492#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003493
Paul Bakker5121ce52009-01-03 21:22:43 +00003494void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003495 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003496 void *p_rng )
3497{
3498 ssl->f_rng = f_rng;
3499 ssl->p_rng = p_rng;
3500}
3501
3502void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003503 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003504 void *p_dbg )
3505{
3506 ssl->f_dbg = f_dbg;
3507 ssl->p_dbg = p_dbg;
3508}
3509
3510void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003511 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003512 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003513{
3514 ssl->f_recv = f_recv;
3515 ssl->f_send = f_send;
3516 ssl->p_recv = p_recv;
3517 ssl->p_send = p_send;
3518}
3519
Paul Bakker0a597072012-09-25 21:55:46 +00003520void ssl_set_session_cache( ssl_context *ssl,
3521 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3522 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003523{
Paul Bakker0a597072012-09-25 21:55:46 +00003524 ssl->f_get_cache = f_get_cache;
3525 ssl->p_get_cache = p_get_cache;
3526 ssl->f_set_cache = f_set_cache;
3527 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003528}
3529
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003530int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003531{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003532 int ret;
3533
3534 if( ssl == NULL ||
3535 session == NULL ||
3536 ssl->session_negotiate == NULL ||
3537 ssl->endpoint != SSL_IS_CLIENT )
3538 {
3539 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3540 }
3541
3542 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3543 return( ret );
3544
Paul Bakker0a597072012-09-25 21:55:46 +00003545 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003546
3547 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003548}
3549
Paul Bakkerb68cad62012-08-23 08:34:18 +00003550void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003551{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003552 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3553 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3554 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3555 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3556}
3557
3558void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3559 int major, int minor )
3560{
3561 if( major != SSL_MAJOR_VERSION_3 )
3562 return;
3563
3564 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3565 return;
3566
3567 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003568}
3569
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003570#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003571/* Add a new (empty) key_cert entry an return a pointer to it */
3572static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3573{
3574 ssl_key_cert *key_cert, *last;
3575
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003576 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3577 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003578 return( NULL );
3579
3580 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3581
3582 /* Append the new key_cert to the (possibly empty) current list */
3583 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003584 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003585 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003586 if( ssl->handshake != NULL )
3587 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003588 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003589 else
3590 {
3591 last = ssl->key_cert;
3592 while( last->next != NULL )
3593 last = last->next;
3594 last->next = key_cert;
3595 }
3596
3597 return key_cert;
3598}
3599
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003600void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003601 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003602{
3603 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003604 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 ssl->peer_cn = peer_cn;
3606}
3607
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003608int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003609 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003610{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003611 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3612
3613 if( key_cert == NULL )
3614 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3615
3616 key_cert->cert = own_cert;
3617 key_cert->key = pk_key;
3618
3619 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003620}
3621
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003622#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003623int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003624 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003625{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003626 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003627 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003628
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003629 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003630 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3631
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003632 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3633 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003634 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003635
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003636 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003637
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003638 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003639 if( ret != 0 )
3640 return( ret );
3641
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003642 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003643 return( ret );
3644
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003645 key_cert->cert = own_cert;
3646 key_cert->key_own_alloc = 1;
3647
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003648 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003649}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003650#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003651
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003652int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003653 void *rsa_key,
3654 rsa_decrypt_func rsa_decrypt,
3655 rsa_sign_func rsa_sign,
3656 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003657{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003658 int ret;
3659 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003660
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003661 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003662 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3663
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003664 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3665 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003666 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003667
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003668 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003669
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003670 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3671 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3672 return( ret );
3673
3674 key_cert->cert = own_cert;
3675 key_cert->key_own_alloc = 1;
3676
3677 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003678}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003679#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003680
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003681#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003682int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3683 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003684{
Paul Bakker6db455e2013-09-18 17:29:31 +02003685 if( psk == NULL || psk_identity == NULL )
3686 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3687
3688 if( ssl->psk != NULL )
3689 {
3690 polarssl_free( ssl->psk );
3691 polarssl_free( ssl->psk_identity );
3692 }
3693
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003694 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003695 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003696
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003697 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3698 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003699
3700 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3701 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3702
3703 memcpy( ssl->psk, psk, ssl->psk_len );
3704 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003705
3706 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003707}
3708
3709void ssl_set_psk_cb( ssl_context *ssl,
3710 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3711 size_t),
3712 void *p_psk )
3713{
3714 ssl->f_psk = f_psk;
3715 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003716}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003717#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003718
Paul Bakker48916f92012-09-16 19:57:18 +00003719#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003720int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003721{
3722 int ret;
3723
Paul Bakker48916f92012-09-16 19:57:18 +00003724 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003725 {
3726 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3727 return( ret );
3728 }
3729
Paul Bakker48916f92012-09-16 19:57:18 +00003730 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 {
3732 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3733 return( ret );
3734 }
3735
3736 return( 0 );
3737}
3738
Paul Bakker1b57b062011-01-06 15:48:19 +00003739int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3740{
3741 int ret;
3742
Paul Bakker48916f92012-09-16 19:57:18 +00003743 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003744 {
3745 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3746 return( ret );
3747 }
3748
Paul Bakker48916f92012-09-16 19:57:18 +00003749 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003750 {
3751 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3752 return( ret );
3753 }
3754
3755 return( 0 );
3756}
Paul Bakker48916f92012-09-16 19:57:18 +00003757#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003758
Paul Bakker0be444a2013-08-27 21:55:01 +02003759#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003760int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003761{
3762 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003763 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003764
3765 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003766
3767 if( ssl->hostname_len + 1 == 0 )
3768 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3769
Paul Bakker6e339b52013-07-03 13:37:05 +02003770 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003771
Paul Bakkerb15b8512012-01-13 13:44:06 +00003772 if( ssl->hostname == NULL )
3773 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3774
Paul Bakker3c2122f2013-06-24 19:03:14 +02003775 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003777
Paul Bakker40ea7de2009-05-03 10:18:48 +00003778 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003779
3780 return( 0 );
3781}
3782
Paul Bakker5701cdc2012-09-27 21:49:42 +00003783void ssl_set_sni( ssl_context *ssl,
3784 int (*f_sni)(void *, ssl_context *,
3785 const unsigned char *, size_t),
3786 void *p_sni )
3787{
3788 ssl->f_sni = f_sni;
3789 ssl->p_sni = p_sni;
3790}
Paul Bakker0be444a2013-08-27 21:55:01 +02003791#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003792
Paul Bakker490ecc82011-10-06 13:04:09 +00003793void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3794{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003795 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3796 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3797 {
3798 ssl->max_major_ver = major;
3799 ssl->max_minor_ver = minor;
3800 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003801}
3802
Paul Bakker1d29fb52012-09-28 13:28:45 +00003803void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3804{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003805 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3806 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3807 {
3808 ssl->min_major_ver = major;
3809 ssl->min_minor_ver = minor;
3810 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003811}
3812
Paul Bakker05decb22013-08-15 13:33:48 +02003813#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003814int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3815{
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003816 if( mfl_code >= sizeof( mfl_code_to_length ) ||
3817 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003818 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003819 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003820 }
3821
3822 ssl->mfl_code = mfl_code;
3823
3824 return( 0 );
3825}
Paul Bakker05decb22013-08-15 13:33:48 +02003826#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003827
Paul Bakker1f2bc622013-08-15 13:45:55 +02003828#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003829int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003830{
3831 if( ssl->endpoint != SSL_IS_CLIENT )
3832 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3833
Paul Bakker8c1ede62013-07-19 14:14:37 +02003834 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003835
3836 return( 0 );
3837}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003838#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003839
Paul Bakker48916f92012-09-16 19:57:18 +00003840void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3841{
3842 ssl->disable_renegotiation = renegotiation;
3843}
3844
3845void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3846{
3847 ssl->allow_legacy_renegotiation = allow_legacy;
3848}
3849
Paul Bakkera503a632013-08-14 13:48:06 +02003850#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003851int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3852{
3853 ssl->session_tickets = use_tickets;
3854
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003855 if( ssl->endpoint == SSL_IS_CLIENT )
3856 return( 0 );
3857
3858 if( ssl->f_rng == NULL )
3859 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3860
3861 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003862}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003863
3864void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3865{
3866 ssl->ticket_lifetime = lifetime;
3867}
Paul Bakkera503a632013-08-14 13:48:06 +02003868#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003869
Paul Bakker5121ce52009-01-03 21:22:43 +00003870/*
3871 * SSL get accessors
3872 */
Paul Bakker23986e52011-04-24 08:57:21 +00003873size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003874{
3875 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3876}
3877
Paul Bakkerff60ee62010-03-16 21:09:09 +00003878int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003879{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003880 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003881}
3882
Paul Bakkere3166ce2011-01-27 17:40:50 +00003883const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003884{
Paul Bakker926c8e42013-03-06 10:23:34 +01003885 if( ssl == NULL || ssl->session == NULL )
3886 return NULL;
3887
Paul Bakkere3166ce2011-01-27 17:40:50 +00003888 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003889}
3890
Paul Bakker43ca69c2011-01-15 17:35:19 +00003891const char *ssl_get_version( const ssl_context *ssl )
3892{
3893 switch( ssl->minor_ver )
3894 {
3895 case SSL_MINOR_VERSION_0:
3896 return( "SSLv3.0" );
3897
3898 case SSL_MINOR_VERSION_1:
3899 return( "TLSv1.0" );
3900
3901 case SSL_MINOR_VERSION_2:
3902 return( "TLSv1.1" );
3903
Paul Bakker1ef83d62012-04-11 12:09:53 +00003904 case SSL_MINOR_VERSION_3:
3905 return( "TLSv1.2" );
3906
Paul Bakker43ca69c2011-01-15 17:35:19 +00003907 default:
3908 break;
3909 }
3910 return( "unknown" );
3911}
3912
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003913#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003914const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003915{
3916 if( ssl == NULL || ssl->session == NULL )
3917 return NULL;
3918
3919 return ssl->session->peer_cert;
3920}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003921#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003922
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003923int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3924{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003925 if( ssl == NULL ||
3926 dst == NULL ||
3927 ssl->session == NULL ||
3928 ssl->endpoint != SSL_IS_CLIENT )
3929 {
3930 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3931 }
3932
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003933 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003934}
3935
Paul Bakker5121ce52009-01-03 21:22:43 +00003936/*
Paul Bakker1961b702013-01-25 14:49:24 +01003937 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003938 */
Paul Bakker1961b702013-01-25 14:49:24 +01003939int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003940{
Paul Bakker40e46942009-01-03 21:51:57 +00003941 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
Paul Bakker40e46942009-01-03 21:51:57 +00003943#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003944 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003945 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003946#endif
3947
Paul Bakker40e46942009-01-03 21:51:57 +00003948#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003949 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003950 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003951#endif
3952
Paul Bakker1961b702013-01-25 14:49:24 +01003953 return( ret );
3954}
3955
3956/*
3957 * Perform the SSL handshake
3958 */
3959int ssl_handshake( ssl_context *ssl )
3960{
3961 int ret = 0;
3962
3963 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
3964
3965 while( ssl->state != SSL_HANDSHAKE_OVER )
3966 {
3967 ret = ssl_handshake_step( ssl );
3968
3969 if( ret != 0 )
3970 break;
3971 }
3972
Paul Bakker5121ce52009-01-03 21:22:43 +00003973 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
3974
3975 return( ret );
3976}
3977
Paul Bakker37ce0ff2013-10-31 14:32:04 +01003978#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003979/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003980 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003981 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003982static int ssl_write_hello_request( ssl_context *ssl )
3983{
3984 int ret;
3985
3986 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
3987
3988 ssl->out_msglen = 4;
3989 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3990 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
3991
3992 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3993 {
3994 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3995 return( ret );
3996 }
3997
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01003998 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
3999
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004000 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4001
4002 return( 0 );
4003}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004004#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004005
4006/*
4007 * Actually renegotiate current connection, triggered by either:
4008 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004009 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004010 * - receiving any handshake message on server during ssl_read() after the
4011 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004012 * If the handshake doesn't complete due to waiting for I/O, it will continue
4013 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004014 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004015static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004016{
4017 int ret;
4018
4019 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4020
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004021 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4022 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004023
4024 ssl->state = SSL_HELLO_REQUEST;
4025 ssl->renegotiation = SSL_RENEGOTIATION;
4026
Paul Bakker48916f92012-09-16 19:57:18 +00004027 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4028 {
4029 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4030 return( ret );
4031 }
4032
4033 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4034
4035 return( 0 );
4036}
4037
4038/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004039 * Renegotiate current connection on client,
4040 * or request renegotiation on server
4041 */
4042int ssl_renegotiate( ssl_context *ssl )
4043{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004044 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004045
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004046#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004047 /* On server, just send the request */
4048 if( ssl->endpoint == SSL_IS_SERVER )
4049 {
4050 if( ssl->state != SSL_HANDSHAKE_OVER )
4051 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4052
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004053 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004054 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004055#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004056
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004057#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004058 /*
4059 * On client, either start the renegotiation process or,
4060 * if already in progress, continue the handshake
4061 */
4062 if( ssl->renegotiation != SSL_RENEGOTIATION )
4063 {
4064 if( ssl->state != SSL_HANDSHAKE_OVER )
4065 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4066
4067 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4068 {
4069 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4070 return( ret );
4071 }
4072 }
4073 else
4074 {
4075 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4076 {
4077 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4078 return( ret );
4079 }
4080 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004081#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004082
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004083 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004084}
4085
4086/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004087 * Receive application data decrypted from the SSL layer
4088 */
Paul Bakker23986e52011-04-24 08:57:21 +00004089int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004090{
Paul Bakker23986e52011-04-24 08:57:21 +00004091 int ret;
4092 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004093
4094 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4095
4096 if( ssl->state != SSL_HANDSHAKE_OVER )
4097 {
4098 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4099 {
4100 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4101 return( ret );
4102 }
4103 }
4104
4105 if( ssl->in_offt == NULL )
4106 {
4107 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4108 {
Paul Bakker831a7552011-05-18 13:32:51 +00004109 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4110 return( 0 );
4111
Paul Bakker5121ce52009-01-03 21:22:43 +00004112 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4113 return( ret );
4114 }
4115
4116 if( ssl->in_msglen == 0 &&
4117 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4118 {
4119 /*
4120 * OpenSSL sends empty messages to randomize the IV
4121 */
4122 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4123 {
Paul Bakker831a7552011-05-18 13:32:51 +00004124 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4125 return( 0 );
4126
Paul Bakker5121ce52009-01-03 21:22:43 +00004127 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4128 return( ret );
4129 }
4130 }
4131
Paul Bakker48916f92012-09-16 19:57:18 +00004132 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4133 {
4134 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4135
4136 if( ssl->endpoint == SSL_IS_CLIENT &&
4137 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4138 ssl->in_hslen != 4 ) )
4139 {
4140 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4141 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4142 }
4143
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004144 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4145 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4146 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004147 {
4148 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4149
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004150#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004151 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004152 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004153 /*
4154 * SSLv3 does not have a "no_renegotiation" alert
4155 */
4156 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4157 return( ret );
4158 }
4159 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004160#endif
4161#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4162 defined(POLARSSL_SSL_PROTO_TLS1_2)
4163 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004164 {
4165 if( ( ret = ssl_send_alert_message( ssl,
4166 SSL_ALERT_LEVEL_WARNING,
4167 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4168 {
4169 return( ret );
4170 }
Paul Bakker48916f92012-09-16 19:57:18 +00004171 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004172 else
4173#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004174 {
4175 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004176 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004177 }
Paul Bakker48916f92012-09-16 19:57:18 +00004178 }
4179 else
4180 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004181 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004182 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004183 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004184 return( ret );
4185 }
4186
4187 return( POLARSSL_ERR_NET_WANT_READ );
4188 }
4189 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004190 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4191 {
4192 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4193 "but not honored by client" ) );
4194 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4195 }
Paul Bakker48916f92012-09-16 19:57:18 +00004196 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004197 {
4198 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004199 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004200 }
4201
4202 ssl->in_offt = ssl->in_msg;
4203 }
4204
4205 n = ( len < ssl->in_msglen )
4206 ? len : ssl->in_msglen;
4207
4208 memcpy( buf, ssl->in_offt, n );
4209 ssl->in_msglen -= n;
4210
4211 if( ssl->in_msglen == 0 )
4212 /* all bytes consumed */
4213 ssl->in_offt = NULL;
4214 else
4215 /* more data available */
4216 ssl->in_offt += n;
4217
4218 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4219
Paul Bakker23986e52011-04-24 08:57:21 +00004220 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004221}
4222
4223/*
4224 * Send application data to be encrypted by the SSL layer
4225 */
Paul Bakker23986e52011-04-24 08:57:21 +00004226int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004227{
Paul Bakker23986e52011-04-24 08:57:21 +00004228 int ret;
4229 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004230 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004231
4232 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4233
4234 if( ssl->state != SSL_HANDSHAKE_OVER )
4235 {
4236 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4237 {
4238 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4239 return( ret );
4240 }
4241 }
4242
Paul Bakker05decb22013-08-15 13:33:48 +02004243#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004244 /*
4245 * Assume mfl_code is correct since it was checked when set
4246 */
4247 max_len = mfl_code_to_length[ssl->mfl_code];
4248
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004249 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004250 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004251 */
4252 if( ssl->session_out != NULL &&
4253 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4254 {
4255 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4256 }
Paul Bakker05decb22013-08-15 13:33:48 +02004257#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004258
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004259 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004260
Paul Bakker5121ce52009-01-03 21:22:43 +00004261 if( ssl->out_left != 0 )
4262 {
4263 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4264 {
4265 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4266 return( ret );
4267 }
4268 }
Paul Bakker887bd502011-06-08 13:10:54 +00004269 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004270 {
Paul Bakker887bd502011-06-08 13:10:54 +00004271 ssl->out_msglen = n;
4272 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4273 memcpy( ssl->out_msg, buf, n );
4274
4275 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4276 {
4277 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4278 return( ret );
4279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004280 }
4281
4282 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4283
Paul Bakker23986e52011-04-24 08:57:21 +00004284 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004285}
4286
4287/*
4288 * Notify the peer that the connection is being closed
4289 */
4290int ssl_close_notify( ssl_context *ssl )
4291{
4292 int ret;
4293
4294 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4295
4296 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4297 {
4298 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4299 return( ret );
4300 }
4301
4302 if( ssl->state == SSL_HANDSHAKE_OVER )
4303 {
Paul Bakker48916f92012-09-16 19:57:18 +00004304 if( ( ret = ssl_send_alert_message( ssl,
4305 SSL_ALERT_LEVEL_WARNING,
4306 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004307 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 return( ret );
4309 }
4310 }
4311
4312 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4313
4314 return( ret );
4315}
4316
Paul Bakker48916f92012-09-16 19:57:18 +00004317void ssl_transform_free( ssl_transform *transform )
4318{
4319#if defined(POLARSSL_ZLIB_SUPPORT)
4320 deflateEnd( &transform->ctx_deflate );
4321 inflateEnd( &transform->ctx_inflate );
4322#endif
4323
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004324 cipher_free_ctx( &transform->cipher_ctx_enc );
4325 cipher_free_ctx( &transform->cipher_ctx_dec );
4326
Paul Bakker61d113b2013-07-04 11:51:43 +02004327 md_free_ctx( &transform->md_ctx_enc );
4328 md_free_ctx( &transform->md_ctx_dec );
4329
Paul Bakker48916f92012-09-16 19:57:18 +00004330 memset( transform, 0, sizeof( ssl_transform ) );
4331}
4332
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004333#if defined(POLARSSL_X509_CRT_PARSE_C)
4334static void ssl_key_cert_free( ssl_key_cert *key_cert )
4335{
4336 ssl_key_cert *cur = key_cert, *next;
4337
4338 while( cur != NULL )
4339 {
4340 next = cur->next;
4341
4342 if( cur->key_own_alloc )
4343 {
4344 pk_free( cur->key );
4345 polarssl_free( cur->key );
4346 }
4347 polarssl_free( cur );
4348
4349 cur = next;
4350 }
4351}
4352#endif /* POLARSSL_X509_CRT_PARSE_C */
4353
Paul Bakker48916f92012-09-16 19:57:18 +00004354void ssl_handshake_free( ssl_handshake_params *handshake )
4355{
4356#if defined(POLARSSL_DHM_C)
4357 dhm_free( &handshake->dhm_ctx );
4358#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004359#if defined(POLARSSL_ECDH_C)
4360 ecdh_free( &handshake->ecdh_ctx );
4361#endif
4362
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004363#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004364 /* explicit void pointer cast for buggy MS compiler */
4365 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004366#endif
4367
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004368#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4369 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4370 /*
4371 * Free only the linked list wrapper, not the keys themselves
4372 * since the belong to the SNI callback
4373 */
4374 if( handshake->sni_key_cert != NULL )
4375 {
4376 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4377
4378 while( cur != NULL )
4379 {
4380 next = cur->next;
4381 polarssl_free( cur );
4382 cur = next;
4383 }
4384 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004385#endif
4386
Paul Bakker48916f92012-09-16 19:57:18 +00004387 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4388}
4389
4390void ssl_session_free( ssl_session *session )
4391{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004392#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004393 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004394 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004395 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004396 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004397 }
Paul Bakkered27a042013-04-18 22:46:23 +02004398#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004399
Paul Bakkera503a632013-08-14 13:48:06 +02004400#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004401 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004402#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004403
Paul Bakker0a597072012-09-25 21:55:46 +00004404 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004405}
4406
Paul Bakker5121ce52009-01-03 21:22:43 +00004407/*
4408 * Free an SSL context
4409 */
4410void ssl_free( ssl_context *ssl )
4411{
4412 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4413
Paul Bakker5121ce52009-01-03 21:22:43 +00004414 if( ssl->out_ctr != NULL )
4415 {
4416 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004417 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004418 }
4419
4420 if( ssl->in_ctr != NULL )
4421 {
4422 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004423 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 }
4425
Paul Bakker16770332013-10-11 09:59:44 +02004426#if defined(POLARSSL_ZLIB_SUPPORT)
4427 if( ssl->compress_buf != NULL )
4428 {
4429 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4430 polarssl_free( ssl->compress_buf );
4431 }
4432#endif
4433
Paul Bakker40e46942009-01-03 21:51:57 +00004434#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004435 mpi_free( &ssl->dhm_P );
4436 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004437#endif
4438
Paul Bakker48916f92012-09-16 19:57:18 +00004439 if( ssl->transform )
4440 {
4441 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004442 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004443 }
4444
4445 if( ssl->handshake )
4446 {
4447 ssl_handshake_free( ssl->handshake );
4448 ssl_transform_free( ssl->transform_negotiate );
4449 ssl_session_free( ssl->session_negotiate );
4450
Paul Bakker6e339b52013-07-03 13:37:05 +02004451 polarssl_free( ssl->handshake );
4452 polarssl_free( ssl->transform_negotiate );
4453 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004454 }
4455
Paul Bakkerc0463502013-02-14 11:19:38 +01004456 if( ssl->session )
4457 {
4458 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004459 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004460 }
4461
Paul Bakkera503a632013-08-14 13:48:06 +02004462#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004463 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004464#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004465
Paul Bakker0be444a2013-08-27 21:55:01 +02004466#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004467 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004468 {
4469 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004470 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004471 ssl->hostname_len = 0;
4472 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004473#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004474
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004475#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004476 if( ssl->psk != NULL )
4477 {
4478 memset( ssl->psk, 0, ssl->psk_len );
4479 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4480 polarssl_free( ssl->psk );
4481 polarssl_free( ssl->psk_identity );
4482 ssl->psk_len = 0;
4483 ssl->psk_identity_len = 0;
4484 }
4485#endif
4486
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004487#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004488 ssl_key_cert_free( ssl->key_cert );
4489#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004490
Paul Bakker05ef8352012-05-08 09:17:57 +00004491#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4492 if( ssl_hw_record_finish != NULL )
4493 {
4494 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4495 ssl_hw_record_finish( ssl );
4496 }
4497#endif
4498
Paul Bakker5121ce52009-01-03 21:22:43 +00004499 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004500
Paul Bakker86f04f42013-02-14 11:20:09 +01004501 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004502 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004503}
4504
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004505#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004506/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004507 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004508 */
4509unsigned char ssl_sig_from_pk( pk_context *pk )
4510{
4511#if defined(POLARSSL_RSA_C)
4512 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4513 return( SSL_SIG_RSA );
4514#endif
4515#if defined(POLARSSL_ECDSA_C)
4516 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4517 return( SSL_SIG_ECDSA );
4518#endif
4519 return( SSL_SIG_ANON );
4520}
4521
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004522pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4523{
4524 switch( sig )
4525 {
4526#if defined(POLARSSL_RSA_C)
4527 case SSL_SIG_RSA:
4528 return( POLARSSL_PK_RSA );
4529#endif
4530#if defined(POLARSSL_ECDSA_C)
4531 case SSL_SIG_ECDSA:
4532 return( POLARSSL_PK_ECDSA );
4533#endif
4534 default:
4535 return( POLARSSL_PK_NONE );
4536 }
4537}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004538#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004539
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004540/*
4541 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4542 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004543md_type_t ssl_md_alg_from_hash( unsigned char hash )
4544{
4545 switch( hash )
4546 {
4547#if defined(POLARSSL_MD5_C)
4548 case SSL_HASH_MD5:
4549 return( POLARSSL_MD_MD5 );
4550#endif
4551#if defined(POLARSSL_SHA1_C)
4552 case SSL_HASH_SHA1:
4553 return( POLARSSL_MD_SHA1 );
4554#endif
4555#if defined(POLARSSL_SHA256_C)
4556 case SSL_HASH_SHA224:
4557 return( POLARSSL_MD_SHA224 );
4558 case SSL_HASH_SHA256:
4559 return( POLARSSL_MD_SHA256 );
4560#endif
4561#if defined(POLARSSL_SHA512_C)
4562 case SSL_HASH_SHA384:
4563 return( POLARSSL_MD_SHA384 );
4564 case SSL_HASH_SHA512:
4565 return( POLARSSL_MD_SHA512 );
4566#endif
4567 default:
4568 return( POLARSSL_MD_NONE );
4569 }
4570}
4571
Paul Bakker5121ce52009-01-03 21:22:43 +00004572#endif