blob: 681b7c39de82c9408ba5b21eb6c8c60033280a61 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, 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 Bakker7dc4c442014-02-01 22:50:26 +010041#if defined(POLARSSL_PLATFORM_C)
42#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020043#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 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100599 if( transform->maclen > sizeof transform->mac_enc )
600 {
601 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
602 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
603 }
604
Paul Bakker68884e32013-01-07 18:20:04 +0100605 memcpy( transform->mac_enc, mac_enc, transform->maclen );
606 memcpy( transform->mac_dec, mac_dec, transform->maclen );
607 }
608 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200609#endif
610#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
611 defined(POLARSSL_SSL_PROTO_TLS1_2)
612 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100613 {
614 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
615 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
616 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200617 else
618#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200619 {
620 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200621 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200622 }
Paul Bakker68884e32013-01-07 18:20:04 +0100623
Paul Bakker05ef8352012-05-08 09:17:57 +0000624#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
625 if( ssl_hw_record_init != NULL)
626 {
627 int ret = 0;
628
629 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
630
Paul Bakker07eb38b2012-12-19 14:42:06 +0100631 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
632 transform->iv_enc, transform->iv_dec,
633 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100634 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100635 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000636 {
637 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
638 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
639 }
640 }
641#endif
642
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200643 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
644 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200646 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
647 return( ret );
648 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200649
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200650 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
651 cipher_info ) ) != 0 )
652 {
653 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
654 return( ret );
655 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200656
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200657 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
658 cipher_info->key_length,
659 POLARSSL_ENCRYPT ) ) != 0 )
660 {
661 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
662 return( ret );
663 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200664
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200665 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
666 cipher_info->key_length,
667 POLARSSL_DECRYPT ) ) != 0 )
668 {
669 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
670 return( ret );
671 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200672
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200673#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200674 if( cipher_info->mode == POLARSSL_MODE_CBC )
675 {
676 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
677 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200678 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200679 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
680 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200681 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200682
683 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
684 POLARSSL_PADDING_NONE ) ) != 0 )
685 {
686 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
687 return( ret );
688 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200690#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
692 memset( keyblk, 0, sizeof( keyblk ) );
693
Paul Bakker2770fbd2012-07-03 13:30:23 +0000694#if defined(POLARSSL_ZLIB_SUPPORT)
695 // Initialize compression
696 //
Paul Bakker48916f92012-09-16 19:57:18 +0000697 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000698 {
Paul Bakker16770332013-10-11 09:59:44 +0200699 if( ssl->compress_buf == NULL )
700 {
701 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
702 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
703 if( ssl->compress_buf == NULL )
704 {
705 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
706 SSL_BUFFER_LEN ) );
707 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
708 }
709 }
710
Paul Bakker2770fbd2012-07-03 13:30:23 +0000711 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
712
Paul Bakker48916f92012-09-16 19:57:18 +0000713 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
714 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000715
Paul Bakker48916f92012-09-16 19:57:18 +0000716 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
717 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000718 {
719 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
720 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
721 }
722 }
723#endif /* POLARSSL_ZLIB_SUPPORT */
724
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
726
727 return( 0 );
728}
729
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200730#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000731void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000732{
733 md5_context md5;
734 sha1_context sha1;
735 unsigned char pad_1[48];
736 unsigned char pad_2[48];
737
Paul Bakker380da532012-04-18 16:10:25 +0000738 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Paul Bakker48916f92012-09-16 19:57:18 +0000740 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
741 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Paul Bakker380da532012-04-18 16:10:25 +0000743 memset( pad_1, 0x36, 48 );
744 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Paul Bakker48916f92012-09-16 19:57:18 +0000746 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000747 md5_update( &md5, pad_1, 48 );
748 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker380da532012-04-18 16:10:25 +0000750 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000751 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000752 md5_update( &md5, pad_2, 48 );
753 md5_update( &md5, hash, 16 );
754 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakker48916f92012-09-16 19:57:18 +0000756 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000757 sha1_update( &sha1, pad_1, 40 );
758 sha1_finish( &sha1, hash + 16 );
759
760 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000761 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000762 sha1_update( &sha1, pad_2, 40 );
763 sha1_update( &sha1, hash + 16, 20 );
764 sha1_finish( &sha1, hash + 16 );
765
766 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
767 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
768
769 return;
770}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200771#endif
Paul Bakker380da532012-04-18 16:10:25 +0000772
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000774void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
775{
776 md5_context md5;
777 sha1_context sha1;
778
779 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
780
Paul Bakker48916f92012-09-16 19:57:18 +0000781 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
782 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000783
Paul Bakker48916f92012-09-16 19:57:18 +0000784 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000785 sha1_finish( &sha1, hash + 16 );
786
787 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
788 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
789
790 return;
791}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200792#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000793
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200794#if defined(POLARSSL_SSL_PROTO_TLS1_2)
795#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000796void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
797{
Paul Bakker9e36f042013-06-30 14:34:05 +0200798 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000799
800 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
801
Paul Bakker9e36f042013-06-30 14:34:05 +0200802 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
803 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000804
805 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
806 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
807
808 return;
809}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000811
Paul Bakker9e36f042013-06-30 14:34:05 +0200812#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000813void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
814{
Paul Bakker9e36f042013-06-30 14:34:05 +0200815 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000816
817 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
818
Paul Bakker9e36f042013-06-30 14:34:05 +0200819 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
820 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821
Paul Bakkerca4ab492012-04-18 14:23:57 +0000822 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
824
825 return;
826}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200827#endif /* POLARSSL_SHA512_C */
828#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200830#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200831int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
832{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200833 unsigned char *p = ssl->handshake->premaster;
834 unsigned char *end = p + sizeof( ssl->handshake->premaster );
835
836 /*
837 * PMS = struct {
838 * opaque other_secret<0..2^16-1>;
839 * opaque psk<0..2^16-1>;
840 * };
841 * with "other_secret" depending on the particular key exchange
842 */
843#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
844 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
845 {
846 if( end - p < 2 + (int) ssl->psk_len )
847 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
848
849 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
850 *(p++) = (unsigned char)( ssl->psk_len );
851 p += ssl->psk_len;
852 }
853 else
854#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200855#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
856 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
857 {
858 /*
859 * other_secret already set by the ClientKeyExchange message,
860 * and is 48 bytes long
861 */
862 *p++ = 0;
863 *p++ = 48;
864 p += 48;
865 }
866 else
867#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200868#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
869 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
870 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200871 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200872 size_t len = ssl->handshake->dhm_ctx.len;
873
874 if( end - p < 2 + (int) len )
875 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
876
877 *(p++) = (unsigned char)( len >> 8 );
878 *(p++) = (unsigned char)( len );
879 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
880 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
881 {
882 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
883 return( ret );
884 }
885 p += len;
886
887 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
888 }
889 else
890#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
891#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
892 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
893 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200894 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895 size_t zlen;
896
897 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
898 p + 2, end - (p + 2),
899 ssl->f_rng, ssl->p_rng ) ) != 0 )
900 {
901 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
902 return( ret );
903 }
904
905 *(p++) = (unsigned char)( zlen >> 8 );
906 *(p++) = (unsigned char)( zlen );
907 p += zlen;
908
909 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
910 }
911 else
912#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
913 {
914 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
915 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
916 }
917
918 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100919 if( end - p < 2 + (int) ssl->psk_len )
920 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
921
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200922 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
923 *(p++) = (unsigned char)( ssl->psk_len );
924 memcpy( p, ssl->psk, ssl->psk_len );
925 p += ssl->psk_len;
926
927 ssl->handshake->pmslen = p - ssl->handshake->premaster;
928
929 return( 0 );
930}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200931#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200933#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000934/*
935 * SSLv3.0 MAC functions
936 */
Paul Bakker68884e32013-01-07 18:20:04 +0100937static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
938 unsigned char *buf, size_t len,
939 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000940{
941 unsigned char header[11];
942 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100943 int padlen = 0;
944 int md_size = md_get_size( md_ctx->md_info );
945 int md_type = md_get_type( md_ctx->md_info );
946
947 if( md_type == POLARSSL_MD_MD5 )
948 padlen = 48;
949 else if( md_type == POLARSSL_MD_SHA1 )
950 padlen = 40;
951 else if( md_type == POLARSSL_MD_SHA256 )
952 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100953 else if( md_type == POLARSSL_MD_SHA384 )
954 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
956 memcpy( header, ctr, 8 );
957 header[ 8] = (unsigned char) type;
958 header[ 9] = (unsigned char)( len >> 8 );
959 header[10] = (unsigned char)( len );
960
Paul Bakker68884e32013-01-07 18:20:04 +0100961 memset( padding, 0x36, padlen );
962 md_starts( md_ctx );
963 md_update( md_ctx, secret, md_size );
964 md_update( md_ctx, padding, padlen );
965 md_update( md_ctx, header, 11 );
966 md_update( md_ctx, buf, len );
967 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
Paul Bakker68884e32013-01-07 18:20:04 +0100969 memset( padding, 0x5C, padlen );
970 md_starts( md_ctx );
971 md_update( md_ctx, secret, md_size );
972 md_update( md_ctx, padding, padlen );
973 md_update( md_ctx, buf + len, md_size );
974 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000975}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200976#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000977
Paul Bakker5121ce52009-01-03 21:22:43 +0000978/*
979 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200980 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000981static int ssl_encrypt_buf( ssl_context *ssl )
982{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200983 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
985 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
986
987 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200988 * Add MAC before encrypt, except for GCM
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200990#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
991 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
992 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
993 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode !=
994 POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200996#if defined(POLARSSL_SSL_PROTO_SSL3)
997 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
998 {
999 ssl_mac( &ssl->transform_out->md_ctx_enc,
1000 ssl->transform_out->mac_enc,
1001 ssl->out_msg, ssl->out_msglen,
1002 ssl->out_ctr, ssl->out_msgtype );
1003 }
1004 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001005#endif
1006#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001007 defined(POLARSSL_SSL_PROTO_TLS1_2)
1008 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1009 {
1010 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1011 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1012 ssl->out_msg, ssl->out_msglen );
1013 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1014 ssl->out_msg + ssl->out_msglen );
1015 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1016 }
1017 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001018#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001019 {
1020 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1021 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1022 }
1023
1024 SSL_DEBUG_BUF( 4, "computed mac",
1025 ssl->out_msg + ssl->out_msglen,
1026 ssl->transform_out->maclen );
1027
1028 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001029 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001030#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001032 /*
1033 * Encrypt
1034 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001035#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001036 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1037 POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001039 int ret;
1040 size_t olen = 0;
1041
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1043 "including %d bytes of padding",
1044 ssl->out_msglen, 0 ) );
1045
1046 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1047 ssl->out_msg, ssl->out_msglen );
1048
Paul Bakker45125bc2013-09-04 16:47:11 +02001049 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001050 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001051 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1052 return( ret );
1053 }
1054
1055 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1056 ssl->transform_out->iv_enc,
1057 ssl->transform_out->ivlen ) ) != 0 )
1058 {
1059 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001060 return( ret );
1061 }
1062
1063 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1064 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1065 &olen ) ) != 0 )
1066 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001067 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001068 return( ret );
1069 }
1070
1071 if( ssl->out_msglen != olen )
1072 {
1073 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1074 ssl->out_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001075 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001076 }
1077
1078 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001079 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001080 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001081 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001082 return( ret );
1083 }
1084
1085 if( 0 != olen )
1086 {
1087 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1088 0, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001089 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001090 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 }
Paul Bakker68884e32013-01-07 18:20:04 +01001092 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001093#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001094#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001095 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1096 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001097 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001098 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001099 unsigned char *enc_msg;
1100 unsigned char add_data[13];
1101 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1102
Paul Bakkerca4ab492012-04-18 14:23:57 +00001103 memcpy( add_data, ssl->out_ctr, 8 );
1104 add_data[8] = ssl->out_msgtype;
1105 add_data[9] = ssl->major_ver;
1106 add_data[10] = ssl->minor_ver;
1107 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1108 add_data[12] = ssl->out_msglen & 0xFF;
1109
1110 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1111 add_data, 13 );
1112
Paul Bakker68884e32013-01-07 18:20:04 +01001113 /*
1114 * Generate IV
1115 */
1116 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001117 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001118 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1119 if( ret != 0 )
1120 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001121
Paul Bakker68884e32013-01-07 18:20:04 +01001122 memcpy( ssl->out_iv,
1123 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1124 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001125
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001126 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1127 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1128
Paul Bakker68884e32013-01-07 18:20:04 +01001129 /*
1130 * Fix pointer positions and message length with added IV
1131 */
1132 enc_msg = ssl->out_msg;
1133 enc_msglen = ssl->out_msglen;
1134 ssl->out_msglen += ssl->transform_out->ivlen -
1135 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001136
Paul Bakker68884e32013-01-07 18:20:04 +01001137 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1138 "including %d bytes of padding",
1139 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140
Paul Bakker68884e32013-01-07 18:20:04 +01001141 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1142 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001143
Paul Bakker68884e32013-01-07 18:20:04 +01001144 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001145 * Encrypt
1146 */
1147 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1148 ssl->transform_out->iv_enc,
1149 ssl->transform_out->ivlen ) ) != 0 ||
1150 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1151 {
1152 return( ret );
1153 }
1154
1155 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1156 add_data, 13 ) ) != 0 )
1157 {
1158 return( ret );
1159 }
1160
1161 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1162 enc_msg, enc_msglen,
1163 enc_msg, &olen ) ) != 0 )
1164 {
1165 return( ret );
1166 }
1167 totlen = olen;
1168
1169 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1170 enc_msg + olen, &olen ) ) != 0 )
1171 {
1172 return( ret );
1173 }
1174 totlen += olen;
1175
1176 if( totlen != enc_msglen )
1177 {
1178 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1179 return( -1 );
1180 }
1181
1182 /*
1183 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001184 */
1185 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001186
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001187 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1188 enc_msg + enc_msglen, 16 ) ) != 0 )
1189 {
1190 return( ret );
1191 }
Paul Bakker68884e32013-01-07 18:20:04 +01001192
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001193 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001194 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 else
Paul Bakker68884e32013-01-07 18:20:04 +01001196#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001197#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1198 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001199 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1200 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001202 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001203 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001204 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001205
Paul Bakker48916f92012-09-16 19:57:18 +00001206 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1207 ssl->transform_out->ivlen;
1208 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 padlen = 0;
1210
1211 for( i = 0; i <= padlen; i++ )
1212 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1213
1214 ssl->out_msglen += padlen + 1;
1215
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001216 enc_msglen = ssl->out_msglen;
1217 enc_msg = ssl->out_msg;
1218
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001219#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001221 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1222 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001223 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001224 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001225 {
1226 /*
1227 * Generate IV
1228 */
Paul Bakker48916f92012-09-16 19:57:18 +00001229 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1230 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001231 if( ret != 0 )
1232 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233
Paul Bakker92be97b2013-01-02 17:30:03 +01001234 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001235 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236
1237 /*
1238 * Fix pointer positions and message length with added IV
1239 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001240 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001241 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001242 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001244#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001245
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001247 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001248 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
1250 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001251 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Paul Bakker45125bc2013-09-04 16:47:11 +02001253 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001255 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1256 return( ret );
1257 }
1258
1259 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1260 ssl->transform_out->iv_enc,
1261 ssl->transform_out->ivlen ) ) != 0 )
1262 {
1263 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001264 return( ret );
1265 }
Paul Bakker68884e32013-01-07 18:20:04 +01001266
Paul Bakkercca5b812013-08-31 17:40:26 +02001267 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1268 enc_msg, enc_msglen, enc_msg,
1269 &olen ) ) != 0 )
1270 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001271 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 return( ret );
1273 }
Paul Bakker68884e32013-01-07 18:20:04 +01001274
Paul Bakkercca5b812013-08-31 17:40:26 +02001275 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001276
Paul Bakkercca5b812013-08-31 17:40:26 +02001277 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001278 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001279 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001280 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 return( ret );
1282 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001283
Paul Bakkercca5b812013-08-31 17:40:26 +02001284 if( enc_msglen != olen )
1285 {
1286 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1287 enc_msglen, olen ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001288 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001289 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001290
1291#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001292 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1293 {
1294 /*
1295 * Save IV in SSL3 and TLS1
1296 */
1297 memcpy( ssl->transform_out->iv_enc,
1298 ssl->transform_out->cipher_ctx_enc.iv,
1299 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001301#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001303 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001304#endif /* POLARSSL_CIPHER_MODE_CBC &&
1305 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001306 {
1307 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1308 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
Paul Bakkerca4ab492012-04-18 14:23:57 +00001311 for( i = 8; i > 0; i-- )
1312 if( ++ssl->out_ctr[i - 1] != 0 )
1313 break;
1314
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001315 /* The loops goes to its end iff the counter is wrapping */
1316 if( i == 0 )
1317 {
1318 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1319 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1320 }
1321
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1323
1324 return( 0 );
1325}
1326
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001327#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001328
Paul Bakker5121ce52009-01-03 21:22:43 +00001329static int ssl_decrypt_buf( ssl_context *ssl )
1330{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001331 size_t i;
1332#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1333 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1334 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1335 size_t padlen = 0, correct = 1;
1336#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
1338 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1339
Paul Bakker48916f92012-09-16 19:57:18 +00001340 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 {
1342 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001343 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001344 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
1346
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001347#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001348 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1349 POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001350 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001351 int ret;
1352 size_t olen = 0;
1353
Paul Bakker68884e32013-01-07 18:20:04 +01001354 padlen = 0;
1355
Paul Bakker45125bc2013-09-04 16:47:11 +02001356 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001357 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001358 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1359 return( ret );
1360 }
1361
1362 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1363 ssl->transform_in->iv_dec,
1364 ssl->transform_in->ivlen ) ) != 0 )
1365 {
1366 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001367 return( ret );
1368 }
1369
1370 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1371 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1372 &olen ) ) != 0 )
1373 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001374 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001375 return( ret );
1376 }
1377
1378 if( ssl->in_msglen != olen )
1379 {
1380 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001381 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001382 }
1383
1384 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001385 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001386 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001387 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001388 return( ret );
1389 }
1390
1391 if( 0 != olen )
1392 {
1393 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001394 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 }
Paul Bakker68884e32013-01-07 18:20:04 +01001397 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001398#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker68884e32013-01-07 18:20:04 +01001399#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001400 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1401 POLARSSL_MODE_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001402 {
1403 unsigned char *dec_msg;
1404 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001405 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001406 unsigned char add_data[13];
1407 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1408
Paul Bakker68884e32013-01-07 18:20:04 +01001409 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1410 ssl->transform_in->fixed_ivlen );
1411 dec_msglen -= 16;
1412 dec_msg = ssl->in_msg;
1413 dec_msg_result = ssl->in_msg;
1414 ssl->in_msglen = dec_msglen;
1415
1416 memcpy( add_data, ssl->in_ctr, 8 );
1417 add_data[8] = ssl->in_msgtype;
1418 add_data[9] = ssl->major_ver;
1419 add_data[10] = ssl->minor_ver;
1420 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1421 add_data[12] = ssl->in_msglen & 0xFF;
1422
1423 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1424 add_data, 13 );
1425
1426 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1427 ssl->in_iv,
1428 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1429
1430 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1431 ssl->transform_in->ivlen );
1432 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1433
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001434 /*
1435 * Decrypt
1436 */
1437 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1438 ssl->transform_in->iv_dec,
1439 ssl->transform_in->ivlen ) ) != 0 ||
1440 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001441 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001442 return( ret );
1443 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001444
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001445 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1446 add_data, 13 ) ) != 0 )
1447 {
1448 return( ret );
1449 }
1450
1451 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1452 dec_msg, dec_msglen,
1453 dec_msg_result, &olen ) ) != 0 )
1454 {
1455 return( ret );
1456 }
1457 totlen = olen;
1458
1459 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1460 dec_msg_result + olen, &olen ) ) != 0 )
1461 {
1462 return( ret );
1463 }
1464 totlen += olen;
1465
1466 if( totlen != dec_msglen )
1467 {
1468 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1469 return( -1 );
1470 }
1471
1472 /*
1473 * Authenticate
1474 */
1475 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1476 dec_msg + dec_msglen, 16 ) ) != 0 )
1477 {
1478 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001479 return( POLARSSL_ERR_SSL_INVALID_MAC );
1480 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001481
Paul Bakkerca4ab492012-04-18 14:23:57 +00001482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 else
Paul Bakker68884e32013-01-07 18:20:04 +01001484#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001485#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1486 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001487 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1488 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 {
Paul Bakker45829992013-01-03 14:52:21 +01001490 /*
1491 * Decrypt and check the padding
1492 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001493 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001494 unsigned char *dec_msg;
1495 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001496 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001497 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001498 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001499
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 /*
Paul Bakker45829992013-01-03 14:52:21 +01001501 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 */
Paul Bakker48916f92012-09-16 19:57:18 +00001503 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 {
1505 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001506 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001507 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 }
1509
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001510#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001511 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1512 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001513#endif
Paul Bakker45829992013-01-03 14:52:21 +01001514
1515 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1516 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1517 {
1518 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1519 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1520 return( POLARSSL_ERR_SSL_INVALID_MAC );
1521 }
1522
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001523 dec_msglen = ssl->in_msglen;
1524 dec_msg = ssl->in_msg;
1525 dec_msg_result = ssl->in_msg;
1526
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001527#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001528 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001529 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001530 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001531 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001532 {
Paul Bakker48916f92012-09-16 19:57:18 +00001533 dec_msglen -= ssl->transform_in->ivlen;
1534 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001535
Paul Bakker48916f92012-09-16 19:57:18 +00001536 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001537 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001538 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001539#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001540
Paul Bakker45125bc2013-09-04 16:47:11 +02001541 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001543 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1544 return( ret );
1545 }
1546
1547 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1548 ssl->transform_in->iv_dec,
1549 ssl->transform_in->ivlen ) ) != 0 )
1550 {
1551 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001552 return( ret );
1553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001554
Paul Bakkercca5b812013-08-31 17:40:26 +02001555 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1556 dec_msg, dec_msglen, dec_msg_result,
1557 &olen ) ) != 0 )
1558 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001559 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001560 return( ret );
1561 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001562
Paul Bakkercca5b812013-08-31 17:40:26 +02001563 dec_msglen -= olen;
1564 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001565 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001566 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001567 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001568 return( ret );
1569 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001570
Paul Bakkercca5b812013-08-31 17:40:26 +02001571 if( dec_msglen != olen )
1572 {
1573 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001574 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001575 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001576
1577#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001578 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1579 {
1580 /*
1581 * Save IV in SSL3 and TLS1
1582 */
1583 memcpy( ssl->transform_in->iv_dec,
1584 ssl->transform_in->cipher_ctx_dec.iv,
1585 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001587#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
1589 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001590
1591 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1592 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001593#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001594 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1595 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001596#endif
Paul Bakker45829992013-01-03 14:52:21 +01001597 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001598 correct = 0;
1599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001601#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1603 {
Paul Bakker48916f92012-09-16 19:57:18 +00001604 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001606#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1608 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001609 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001610#endif
Paul Bakker45829992013-01-03 14:52:21 +01001611 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001612 }
1613 }
1614 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001615#endif
1616#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1617 defined(POLARSSL_SSL_PROTO_TLS1_2)
1618 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 {
1620 /*
Paul Bakker45829992013-01-03 14:52:21 +01001621 * TLSv1+: always check the padding up to the first failure
1622 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001623 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001624 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001625 size_t padding_idx = ssl->in_msglen - padlen - 1;
1626
Paul Bakker956c9e02013-12-19 14:42:28 +01001627 /*
1628 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001629 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001630 *
Paul Bakker91c61bc2014-03-26 14:06:55 +01001631 * 2. padding_idx > SSL_MAX_CONTENT_LEN
Paul Bakker956c9e02013-12-19 14:42:28 +01001632 *
1633 * In both cases we reset padding_idx to a safe value (0) to
1634 * prevent out-of-buffer reads.
1635 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001636 correct &= ( ssl->in_msglen >= padlen + 1 );
1637 correct &= ( padding_idx <= SSL_MAX_CONTENT_LEN );
Paul Bakker956c9e02013-12-19 14:42:28 +01001638
1639 padding_idx *= correct;
1640
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001641 for( i = 1; i <= 256; i++ )
1642 {
1643 real_count &= ( i <= padlen );
1644 pad_count += real_count *
1645 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1646 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001647
1648 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001649
Paul Bakkerd66f0702013-01-31 16:57:45 +01001650#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001651 if( padlen > 0 && correct == 0)
1652 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001653#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001654 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001656 else
1657#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1658 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001659 {
1660 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001661 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001664 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001665#endif /* POLARSSL_CIPHER_MODE_CBC &&
1666 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001667 {
1668 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1669 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
1672 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1673 ssl->in_msg, ssl->in_msglen );
1674
1675 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1679 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1680 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1681 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1682 POLARSSL_MODE_GCM )
1683 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001684 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1685
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001686 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001688 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1689 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001691 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001693#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001694 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1695 {
1696 ssl_mac( &ssl->transform_in->md_ctx_dec,
1697 ssl->transform_in->mac_dec,
1698 ssl->in_msg, ssl->in_msglen,
1699 ssl->in_ctr, ssl->in_msgtype );
1700 }
1701 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001702#endif /* POLARSSL_SSL_PROTO_SSL3 */
1703#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001704 defined(POLARSSL_SSL_PROTO_TLS1_2)
1705 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1706 {
1707 /*
1708 * Process MAC and always update for padlen afterwards to make
1709 * total time independent of padlen
1710 *
1711 * extra_run compensates MAC check for padlen
1712 *
1713 * Known timing attacks:
1714 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1715 *
1716 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1717 * correctly. (We round down instead of up, so -56 is the correct
1718 * value for our calculations instead of -55)
1719 */
1720 size_t j, extra_run = 0;
1721 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1722 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001723
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001724 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001725
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001726 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1727 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1728 ssl->in_msglen );
1729 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1730 ssl->in_msg + ssl->in_msglen );
1731 for( j = 0; j < extra_run; j++ )
1732 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001733
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001734 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1735 }
1736 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001737#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001738 POLARSSL_SSL_PROTO_TLS1_2 */
1739 {
1740 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1741 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1742 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1745 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1746 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001748 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001749 ssl->transform_in->maclen ) != 0 )
1750 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001751#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001752 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001753#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001754 correct = 0;
1755 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001756
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001757 /*
1758 * Finally check the correct flag
1759 */
1760 if( correct == 0 )
1761 return( POLARSSL_ERR_SSL_INVALID_MAC );
1762 }
1763#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001764
1765 if( ssl->in_msglen == 0 )
1766 {
1767 ssl->nb_zero++;
1768
1769 /*
1770 * Three or more empty messages may be a DoS attack
1771 * (excessive CPU consumption).
1772 */
1773 if( ssl->nb_zero > 3 )
1774 {
1775 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1776 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001777 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 }
1779 }
1780 else
1781 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001782
Paul Bakker23986e52011-04-24 08:57:21 +00001783 for( i = 8; i > 0; i-- )
1784 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 break;
1786
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001787 /* The loops goes to its end iff the counter is wrapping */
1788 if( i == 0 )
1789 {
1790 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1791 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1792 }
1793
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1795
1796 return( 0 );
1797}
1798
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799#if defined(POLARSSL_ZLIB_SUPPORT)
1800/*
1801 * Compression/decompression functions
1802 */
1803static int ssl_compress_buf( ssl_context *ssl )
1804{
1805 int ret;
1806 unsigned char *msg_post = ssl->out_msg;
1807 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001808 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001809
1810 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1811
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001812 if( len_pre == 0 )
1813 return( 0 );
1814
Paul Bakker2770fbd2012-07-03 13:30:23 +00001815 memcpy( msg_pre, ssl->out_msg, len_pre );
1816
1817 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1818 ssl->out_msglen ) );
1819
1820 SSL_DEBUG_BUF( 4, "before compression: output payload",
1821 ssl->out_msg, ssl->out_msglen );
1822
Paul Bakker48916f92012-09-16 19:57:18 +00001823 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1824 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1825 ssl->transform_out->ctx_deflate.next_out = msg_post;
1826 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001827
Paul Bakker48916f92012-09-16 19:57:18 +00001828 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001829 if( ret != Z_OK )
1830 {
1831 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1832 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1833 }
1834
Paul Bakker48916f92012-09-16 19:57:18 +00001835 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001836
Paul Bakker2770fbd2012-07-03 13:30:23 +00001837 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1838 ssl->out_msglen ) );
1839
1840 SSL_DEBUG_BUF( 4, "after compression: output payload",
1841 ssl->out_msg, ssl->out_msglen );
1842
1843 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1844
1845 return( 0 );
1846}
1847
1848static int ssl_decompress_buf( ssl_context *ssl )
1849{
1850 int ret;
1851 unsigned char *msg_post = ssl->in_msg;
1852 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001853 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001854
1855 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1856
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001857 if( len_pre == 0 )
1858 return( 0 );
1859
Paul Bakker2770fbd2012-07-03 13:30:23 +00001860 memcpy( msg_pre, ssl->in_msg, len_pre );
1861
1862 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1863 ssl->in_msglen ) );
1864
1865 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1866 ssl->in_msg, ssl->in_msglen );
1867
Paul Bakker48916f92012-09-16 19:57:18 +00001868 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1869 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1870 ssl->transform_in->ctx_inflate.next_out = msg_post;
1871 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001872
Paul Bakker48916f92012-09-16 19:57:18 +00001873 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001874 if( ret != Z_OK )
1875 {
1876 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1877 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1878 }
1879
Paul Bakker48916f92012-09-16 19:57:18 +00001880 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001881
Paul Bakker2770fbd2012-07-03 13:30:23 +00001882 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1883 ssl->in_msglen ) );
1884
1885 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1886 ssl->in_msg, ssl->in_msglen );
1887
1888 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1889
1890 return( 0 );
1891}
1892#endif /* POLARSSL_ZLIB_SUPPORT */
1893
Paul Bakker5121ce52009-01-03 21:22:43 +00001894/*
1895 * Fill the input message buffer
1896 */
Paul Bakker23986e52011-04-24 08:57:21 +00001897int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001898{
Paul Bakker23986e52011-04-24 08:57:21 +00001899 int ret;
1900 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001901
1902 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1903
1904 while( ssl->in_left < nb_want )
1905 {
1906 len = nb_want - ssl->in_left;
1907 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1908
1909 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1910 ssl->in_left, nb_want ) );
1911 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1912
Paul Bakker831a7552011-05-18 13:32:51 +00001913 if( ret == 0 )
1914 return( POLARSSL_ERR_SSL_CONN_EOF );
1915
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 if( ret < 0 )
1917 return( ret );
1918
1919 ssl->in_left += ret;
1920 }
1921
1922 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1923
1924 return( 0 );
1925}
1926
1927/*
1928 * Flush any data not yet written
1929 */
1930int ssl_flush_output( ssl_context *ssl )
1931{
1932 int ret;
1933 unsigned char *buf;
1934
1935 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1936
1937 while( ssl->out_left > 0 )
1938 {
1939 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1940 5 + ssl->out_msglen, ssl->out_left ) );
1941
Paul Bakker5bd42292012-12-19 14:40:42 +01001942 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001944
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1946
1947 if( ret <= 0 )
1948 return( ret );
1949
1950 ssl->out_left -= ret;
1951 }
1952
1953 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1954
1955 return( 0 );
1956}
1957
1958/*
1959 * Record layer functions
1960 */
1961int ssl_write_record( ssl_context *ssl )
1962{
Paul Bakker05ef8352012-05-08 09:17:57 +00001963 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001964 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
1966 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1967
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1969 {
1970 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1971 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1972 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1973
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001974 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1975 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 }
1977
Paul Bakker2770fbd2012-07-03 13:30:23 +00001978#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001979 if( ssl->transform_out != NULL &&
1980 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001981 {
1982 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1983 {
1984 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1985 return( ret );
1986 }
1987
1988 len = ssl->out_msglen;
1989 }
1990#endif /*POLARSSL_ZLIB_SUPPORT */
1991
Paul Bakker05ef8352012-05-08 09:17:57 +00001992#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1993 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001995 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001996
Paul Bakker05ef8352012-05-08 09:17:57 +00001997 ret = ssl_hw_record_write( ssl );
1998 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1999 {
2000 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
2001 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2002 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002003
2004 if( ret == 0 )
2005 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002006 }
2007#endif
2008 if( !done )
2009 {
2010 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2011 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2012 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2014 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002015
Paul Bakker48916f92012-09-16 19:57:18 +00002016 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002017 {
2018 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2019 {
2020 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2021 return( ret );
2022 }
2023
2024 len = ssl->out_msglen;
2025 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2026 ssl->out_hdr[4] = (unsigned char)( len );
2027 }
2028
2029 ssl->out_left = 5 + ssl->out_msglen;
2030
2031 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2032 "version = [%d:%d], msglen = %d",
2033 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2034 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2035
2036 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002037 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 }
2039
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2041 {
2042 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2043 return( ret );
2044 }
2045
2046 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2047
2048 return( 0 );
2049}
2050
2051int ssl_read_record( ssl_context *ssl )
2052{
Paul Bakker05ef8352012-05-08 09:17:57 +00002053 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002054
2055 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2056
Paul Bakker68884e32013-01-07 18:20:04 +01002057 SSL_DEBUG_BUF( 4, "input record from network",
2058 ssl->in_hdr, 5 + ssl->in_msglen );
2059
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 if( ssl->in_hslen != 0 &&
2061 ssl->in_hslen < ssl->in_msglen )
2062 {
2063 /*
2064 * Get next Handshake message in the current record
2065 */
2066 ssl->in_msglen -= ssl->in_hslen;
2067
Paul Bakker8934a982011-08-05 11:11:53 +00002068 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002069 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
2071 ssl->in_hslen = 4;
2072 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2073
2074 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2075 " %d, type = %d, hslen = %d",
2076 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2077
2078 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2079 {
2080 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002081 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 }
2083
2084 if( ssl->in_msglen < ssl->in_hslen )
2085 {
2086 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002087 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 }
2089
Paul Bakker48916f92012-09-16 19:57:18 +00002090 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091
2092 return( 0 );
2093 }
2094
2095 ssl->in_hslen = 0;
2096
2097 /*
2098 * Read the record header and validate it
2099 */
2100 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2101 {
2102 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2103 return( ret );
2104 }
2105
2106 ssl->in_msgtype = ssl->in_hdr[0];
2107 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2108
2109 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2110 "version = [%d:%d], msglen = %d",
2111 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2112 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2113
2114 if( ssl->in_hdr[1] != ssl->major_ver )
2115 {
2116 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002117 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 }
2119
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002120 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
2122 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002123 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
2125
2126 /*
2127 * Make sure the message length is acceptable
2128 */
Paul Bakker48916f92012-09-16 19:57:18 +00002129 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 {
2131 if( ssl->in_msglen < 1 ||
2132 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2133 {
2134 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002135 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 }
2137 }
2138 else
2139 {
Paul Bakker48916f92012-09-16 19:57:18 +00002140 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 {
2142 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002143 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 }
2145
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002148 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 {
2150 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002151 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002153#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002154
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002155#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2156 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 /*
2158 * TLS encrypted messages can have up to 256 bytes of padding
2159 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002160 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002161 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 {
2163 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002164 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002166#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 }
2168
2169 /*
2170 * Read and optionally decrypt the message contents
2171 */
2172 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2173 {
2174 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2175 return( ret );
2176 }
2177
2178 SSL_DEBUG_BUF( 4, "input record from network",
2179 ssl->in_hdr, 5 + ssl->in_msglen );
2180
Paul Bakker05ef8352012-05-08 09:17:57 +00002181#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2182 if( ssl_hw_record_read != NULL)
2183 {
2184 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2185
2186 ret = ssl_hw_record_read( ssl );
2187 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2188 {
2189 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2190 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2191 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002192
2193 if( ret == 0 )
2194 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002195 }
2196#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002197 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 {
2199 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2200 {
Paul Bakker40865c82013-01-31 17:13:13 +01002201#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2202 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2203 {
2204 ssl_send_alert_message( ssl,
2205 SSL_ALERT_LEVEL_FATAL,
2206 SSL_ALERT_MSG_BAD_RECORD_MAC );
2207 }
2208#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2210 return( ret );
2211 }
2212
2213 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2214 ssl->in_msg, ssl->in_msglen );
2215
2216 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2217 {
2218 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002219 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 }
2221 }
2222
Paul Bakker2770fbd2012-07-03 13:30:23 +00002223#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002224 if( ssl->transform_in != NULL &&
2225 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002226 {
2227 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2228 {
2229 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2230 return( ret );
2231 }
2232
2233 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2234 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2235 }
2236#endif /* POLARSSL_ZLIB_SUPPORT */
2237
Paul Bakker0a925182012-04-16 06:46:41 +00002238 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2239 ssl->in_msgtype != SSL_MSG_ALERT &&
2240 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2241 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2242 {
2243 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2244
Paul Bakker48916f92012-09-16 19:57:18 +00002245 if( ( ret = ssl_send_alert_message( ssl,
2246 SSL_ALERT_LEVEL_FATAL,
2247 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002248 {
2249 return( ret );
2250 }
2251
2252 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2253 }
2254
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2256 {
2257 ssl->in_hslen = 4;
2258 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2259
2260 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2261 " %d, type = %d, hslen = %d",
2262 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2263
2264 /*
2265 * Additional checks to validate the handshake header
2266 */
2267 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2268 {
2269 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002270 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 }
2272
2273 if( ssl->in_msglen < ssl->in_hslen )
2274 {
2275 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002276 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 }
2278
Paul Bakker48916f92012-09-16 19:57:18 +00002279 if( ssl->state != SSL_HANDSHAKE_OVER )
2280 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 }
2282
2283 if( ssl->in_msgtype == SSL_MSG_ALERT )
2284 {
2285 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2286 ssl->in_msg[0], ssl->in_msg[1] ) );
2287
2288 /*
2289 * Ignore non-fatal alerts, except close_notify
2290 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002291 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002293 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2294 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002295 /**
2296 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2297 * error identifier.
2298 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002299 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 }
2301
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002302 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2303 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 {
2305 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002306 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002307 }
2308 }
2309
2310 ssl->in_left = 0;
2311
2312 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2313
2314 return( 0 );
2315}
2316
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002317int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2318{
2319 int ret;
2320
2321 if( ( ret = ssl_send_alert_message( ssl,
2322 SSL_ALERT_LEVEL_FATAL,
2323 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2324 {
2325 return( ret );
2326 }
2327
2328 return( 0 );
2329}
2330
Paul Bakker0a925182012-04-16 06:46:41 +00002331int ssl_send_alert_message( ssl_context *ssl,
2332 unsigned char level,
2333 unsigned char message )
2334{
2335 int ret;
2336
2337 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2338
2339 ssl->out_msgtype = SSL_MSG_ALERT;
2340 ssl->out_msglen = 2;
2341 ssl->out_msg[0] = level;
2342 ssl->out_msg[1] = message;
2343
2344 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2345 {
2346 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2347 return( ret );
2348 }
2349
2350 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2351
2352 return( 0 );
2353}
2354
Paul Bakker5121ce52009-01-03 21:22:43 +00002355/*
2356 * Handshake functions
2357 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002358#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2359 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2360 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2361 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2362 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2363 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2364 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002365int ssl_write_certificate( ssl_context *ssl )
2366{
Paul Bakkered27a042013-04-18 22:46:23 +02002367 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002368 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
2370 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2371
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002372 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002373 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2374 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002375 {
2376 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2377 ssl->state++;
2378 return( 0 );
2379 }
2380
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002381 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002382 return( ret );
2383}
2384
2385int ssl_parse_certificate( ssl_context *ssl )
2386{
2387 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2388 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2389
2390 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2391
2392 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002393 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2394 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002395 {
2396 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2397 ssl->state++;
2398 return( 0 );
2399 }
2400
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002401 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002402 return( ret );
2403}
2404#else
2405int ssl_write_certificate( ssl_context *ssl )
2406{
2407 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2408 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002409 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002410 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2411
2412 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2413
2414 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002415 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2416 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002417 {
2418 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2419 ssl->state++;
2420 return( 0 );
2421 }
2422
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 if( ssl->endpoint == SSL_IS_CLIENT )
2424 {
2425 if( ssl->client_auth == 0 )
2426 {
2427 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2428 ssl->state++;
2429 return( 0 );
2430 }
2431
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002432#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 /*
2434 * If using SSLv3 and got no cert, send an Alert message
2435 * (otherwise an empty Certificate message will be sent).
2436 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002437 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2439 {
2440 ssl->out_msglen = 2;
2441 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002442 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2443 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2446 goto write_msg;
2447 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002448#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 }
2450 else /* SSL_IS_SERVER */
2451 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002452 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 {
2454 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002455 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 }
2457 }
2458
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002459 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
2461 /*
2462 * 0 . 0 handshake type
2463 * 1 . 3 handshake length
2464 * 4 . 6 length of all certs
2465 * 7 . 9 length of cert. 1
2466 * 10 . n-1 peer certificate
2467 * n . n+2 length of cert. 2
2468 * n+3 . ... upper level cert, etc.
2469 */
2470 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002471 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
Paul Bakker29087132010-03-21 21:03:34 +00002473 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 {
2475 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002476 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 {
2478 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2479 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002480 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 }
2482
2483 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2484 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2485 ssl->out_msg[i + 2] = (unsigned char)( n );
2486
2487 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2488 i += n; crt = crt->next;
2489 }
2490
2491 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2492 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2493 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2494
2495 ssl->out_msglen = i;
2496 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2497 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2498
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002499#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002500write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002501#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
2503 ssl->state++;
2504
2505 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2506 {
2507 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2508 return( ret );
2509 }
2510
2511 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2512
Paul Bakkered27a042013-04-18 22:46:23 +02002513 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002514}
2515
2516int ssl_parse_certificate( ssl_context *ssl )
2517{
Paul Bakkered27a042013-04-18 22:46:23 +02002518 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002519 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002520 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
2522 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2523
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002524 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002525 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2526 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002527 {
2528 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2529 ssl->state++;
2530 return( 0 );
2531 }
2532
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002534 ( ssl->authmode == SSL_VERIFY_NONE ||
2535 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002537 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2539 ssl->state++;
2540 return( 0 );
2541 }
2542
2543 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2544 {
2545 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2546 return( ret );
2547 }
2548
2549 ssl->state++;
2550
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002551#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 /*
2553 * Check if the client sent an empty certificate
2554 */
2555 if( ssl->endpoint == SSL_IS_SERVER &&
2556 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2557 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002558 if( ssl->in_msglen == 2 &&
2559 ssl->in_msgtype == SSL_MSG_ALERT &&
2560 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2561 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 {
2563 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2564
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002565 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2567 return( 0 );
2568 else
Paul Bakker40e46942009-01-03 21:51:57 +00002569 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 }
2571 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002572#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002574#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2575 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 if( ssl->endpoint == SSL_IS_SERVER &&
2577 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2578 {
2579 if( ssl->in_hslen == 7 &&
2580 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2581 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2582 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2583 {
2584 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2585
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002586 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002588 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 else
2590 return( 0 );
2591 }
2592 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002593#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2594 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
2596 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2597 {
2598 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002599 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 }
2601
2602 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2603 {
2604 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002605 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 }
2607
2608 /*
2609 * Same message structure as in ssl_write_certificate()
2610 */
2611 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2612
2613 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2614 {
2615 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002616 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 }
2618
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002619 /* In case we tried to reuse a session but it failed */
2620 if( ssl->session_negotiate->peer_cert != NULL )
2621 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002622 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002623 polarssl_free( ssl->session_negotiate->peer_cert );
2624 }
2625
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002626 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2627 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 {
2629 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002630 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002631 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 }
2633
Paul Bakkerb6b09562013-09-18 14:17:41 +02002634 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
2636 i = 7;
2637
2638 while( i < ssl->in_hslen )
2639 {
2640 if( ssl->in_msg[i] != 0 )
2641 {
2642 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002643 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 }
2645
2646 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2647 | (unsigned int) ssl->in_msg[i + 2];
2648 i += 3;
2649
2650 if( n < 128 || i + n > ssl->in_hslen )
2651 {
2652 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002653 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 }
2655
Paul Bakkerddf26b42013-09-18 13:46:23 +02002656 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2657 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002658 if( ret != 0 )
2659 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002660 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 return( ret );
2662 }
2663
2664 i += n;
2665 }
2666
Paul Bakker48916f92012-09-16 19:57:18 +00002667 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002669 /*
2670 * On client, make sure the server cert doesn't change during renego to
2671 * avoid "triple handshake" attack: https://secure-resumption.com/
2672 */
2673 if( ssl->endpoint == SSL_IS_CLIENT &&
2674 ssl->renegotiation == SSL_RENEGOTIATION )
2675 {
2676 if( ssl->session->peer_cert == NULL )
2677 {
2678 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2679 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2680 }
2681
2682 if( ssl->session->peer_cert->raw.len !=
2683 ssl->session_negotiate->peer_cert->raw.len ||
2684 memcmp( ssl->session->peer_cert->raw.p,
2685 ssl->session_negotiate->peer_cert->raw.p,
2686 ssl->session->peer_cert->raw.len ) != 0 )
2687 {
2688 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2689 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2690 }
2691 }
2692
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 if( ssl->authmode != SSL_VERIFY_NONE )
2694 {
2695 if( ssl->ca_chain == NULL )
2696 {
2697 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002698 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
Paul Bakkerddf26b42013-09-18 13:46:23 +02002701 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2702 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2703 &ssl->session_negotiate->verify_result,
2704 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
2706 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002707 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002709 }
2710#if defined(POLARSSL_SSL_SET_CURVES)
2711 else
2712 {
2713 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
2714
2715 /* If certificate uses an EC key, make sure the curve is OK */
2716 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2717 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2718 {
2719 SSL_DEBUG_MSG( 1, ( "bad server certificate (EC key curve)" ) );
2720 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2721 }
2722 }
2723#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
2725 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2726 ret = 0;
2727 }
2728
2729 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2730
2731 return( ret );
2732}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002733#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2734 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2735 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2736 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2737 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2738 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2739 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
2741int ssl_write_change_cipher_spec( ssl_context *ssl )
2742{
2743 int ret;
2744
2745 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2746
2747 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2748 ssl->out_msglen = 1;
2749 ssl->out_msg[0] = 1;
2750
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 ssl->state++;
2752
2753 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2754 {
2755 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2756 return( ret );
2757 }
2758
2759 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2760
2761 return( 0 );
2762}
2763
2764int ssl_parse_change_cipher_spec( ssl_context *ssl )
2765{
2766 int ret;
2767
2768 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2769
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2771 {
2772 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2773 return( ret );
2774 }
2775
2776 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2777 {
2778 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002779 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 }
2781
2782 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2783 {
2784 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002785 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 }
2787
2788 ssl->state++;
2789
2790 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2791
2792 return( 0 );
2793}
2794
Paul Bakker41c83d32013-03-20 14:39:14 +01002795void ssl_optimize_checksum( ssl_context *ssl,
2796 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002797{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002798 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002799
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002800#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2801 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002802 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002803 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002804 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002805#endif
2806#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2807#if defined(POLARSSL_SHA512_C)
2808 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2809 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2810 else
2811#endif
2812#if defined(POLARSSL_SHA256_C)
2813 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002814 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002815 else
2816#endif
2817#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2818 /* Should never happen */
2819 return;
Paul Bakker380da532012-04-18 16:10:25 +00002820}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002821
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002822static void ssl_update_checksum_start( ssl_context *ssl,
2823 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002824{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002825#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2826 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002827 md5_update( &ssl->handshake->fin_md5 , buf, len );
2828 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002829#endif
2830#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2831#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002832 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002833#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002834#if defined(POLARSSL_SHA512_C)
2835 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002836#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002837#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002838}
2839
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002840#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2841 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002842static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2843 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002844{
Paul Bakker48916f92012-09-16 19:57:18 +00002845 md5_update( &ssl->handshake->fin_md5 , buf, len );
2846 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002847}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002848#endif
Paul Bakker380da532012-04-18 16:10:25 +00002849
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002850#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2851#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002852static void ssl_update_checksum_sha256( ssl_context *ssl,
2853 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002854{
Paul Bakker9e36f042013-06-30 14:34:05 +02002855 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002856}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002857#endif
Paul Bakker380da532012-04-18 16:10:25 +00002858
Paul Bakker9e36f042013-06-30 14:34:05 +02002859#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002860static void ssl_update_checksum_sha384( ssl_context *ssl,
2861 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002862{
Paul Bakker9e36f042013-06-30 14:34:05 +02002863 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002864}
Paul Bakker769075d2012-11-24 11:26:46 +01002865#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002866#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002867
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002868#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869static void ssl_calc_finished_ssl(
2870 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002871{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002872 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 md5_context md5;
2874 sha1_context sha1;
2875
Paul Bakker5121ce52009-01-03 21:22:43 +00002876 unsigned char padbuf[48];
2877 unsigned char md5sum[16];
2878 unsigned char sha1sum[20];
2879
Paul Bakker48916f92012-09-16 19:57:18 +00002880 ssl_session *session = ssl->session_negotiate;
2881 if( !session )
2882 session = ssl->session;
2883
Paul Bakker1ef83d62012-04-11 12:09:53 +00002884 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2885
Paul Bakker48916f92012-09-16 19:57:18 +00002886 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2887 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002888
2889 /*
2890 * SSLv3:
2891 * hash =
2892 * MD5( master + pad2 +
2893 * MD5( handshake + sender + master + pad1 ) )
2894 * + SHA1( master + pad2 +
2895 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002896 */
2897
Paul Bakker90995b52013-06-24 19:20:35 +02002898#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002901#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Paul Bakker90995b52013-06-24 19:20:35 +02002903#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002905 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002906#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
Paul Bakker3c2122f2013-06-24 19:03:14 +02002908 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2909 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Paul Bakker3c2122f2013-06-24 19:03:14 +02002913 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002914 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915 md5_update( &md5, padbuf, 48 );
2916 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002917
Paul Bakker3c2122f2013-06-24 19:03:14 +02002918 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002919 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002920 sha1_update( &sha1, padbuf, 40 );
2921 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002926 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002927 md5_update( &md5, padbuf, 48 );
2928 md5_update( &md5, md5sum, 16 );
2929 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Paul Bakker1ef83d62012-04-11 12:09:53 +00002931 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002932 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002933 sha1_update( &sha1, padbuf , 40 );
2934 sha1_update( &sha1, sha1sum, 20 );
2935 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Paul Bakker1ef83d62012-04-11 12:09:53 +00002937 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939 memset( &md5, 0, sizeof( md5_context ) );
2940 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
2942 memset( padbuf, 0, sizeof( padbuf ) );
2943 memset( md5sum, 0, sizeof( md5sum ) );
2944 memset( sha1sum, 0, sizeof( sha1sum ) );
2945
2946 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2947}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002948#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002950#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951static void ssl_calc_finished_tls(
2952 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002953{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002955 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Paul Bakker48916f92012-09-16 19:57:18 +00002960 ssl_session *session = ssl->session_negotiate;
2961 if( !session )
2962 session = ssl->session;
2963
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Paul Bakker48916f92012-09-16 19:57:18 +00002966 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2967 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969 /*
2970 * TLSv1:
2971 * hash = PRF( master, finished_label,
2972 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2973 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Paul Bakker90995b52013-06-24 19:20:35 +02002975#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2977 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002978#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979
Paul Bakker90995b52013-06-24 19:20:35 +02002980#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002981 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2982 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002983#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002984
2985 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002986 ? "client finished"
2987 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002988
2989 md5_finish( &md5, padbuf );
2990 sha1_finish( &sha1, padbuf + 16 );
2991
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002992 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002993 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994
2995 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2996
2997 memset( &md5, 0, sizeof( md5_context ) );
2998 memset( &sha1, 0, sizeof( sha1_context ) );
2999
3000 memset( padbuf, 0, sizeof( padbuf ) );
3001
3002 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3003}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003004#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003005
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003006#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3007#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003008static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003009 ssl_context *ssl, unsigned char *buf, int from )
3010{
3011 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003012 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003013 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003014 unsigned char padbuf[32];
3015
Paul Bakker48916f92012-09-16 19:57:18 +00003016 ssl_session *session = ssl->session_negotiate;
3017 if( !session )
3018 session = ssl->session;
3019
Paul Bakker380da532012-04-18 16:10:25 +00003020 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021
Paul Bakker9e36f042013-06-30 14:34:05 +02003022 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003023
3024 /*
3025 * TLSv1.2:
3026 * hash = PRF( master, finished_label,
3027 * Hash( handshake ) )[0.11]
3028 */
3029
Paul Bakker9e36f042013-06-30 14:34:05 +02003030#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003031 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003032 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003033#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003034
3035 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003036 ? "client finished"
3037 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003038
Paul Bakker9e36f042013-06-30 14:34:05 +02003039 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003040
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003041 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003042 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003043
3044 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3045
Paul Bakker9e36f042013-06-30 14:34:05 +02003046 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003047
3048 memset( padbuf, 0, sizeof( padbuf ) );
3049
3050 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3051}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003052#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003053
Paul Bakker9e36f042013-06-30 14:34:05 +02003054#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003055static void ssl_calc_finished_tls_sha384(
3056 ssl_context *ssl, unsigned char *buf, int from )
3057{
3058 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003059 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003060 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003061 unsigned char padbuf[48];
3062
Paul Bakker48916f92012-09-16 19:57:18 +00003063 ssl_session *session = ssl->session_negotiate;
3064 if( !session )
3065 session = ssl->session;
3066
Paul Bakker380da532012-04-18 16:10:25 +00003067 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003068
Paul Bakker9e36f042013-06-30 14:34:05 +02003069 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003070
3071 /*
3072 * TLSv1.2:
3073 * hash = PRF( master, finished_label,
3074 * Hash( handshake ) )[0.11]
3075 */
3076
Paul Bakker9e36f042013-06-30 14:34:05 +02003077#if !defined(POLARSSL_SHA512_ALT)
3078 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3079 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003080#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003081
3082 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003083 ? "client finished"
3084 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003085
Paul Bakker9e36f042013-06-30 14:34:05 +02003086 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003087
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003088 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003089 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003090
3091 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3092
Paul Bakker9e36f042013-06-30 14:34:05 +02003093 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003094
3095 memset( padbuf, 0, sizeof( padbuf ) );
3096
3097 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3098}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003099#endif /* POLARSSL_SHA512_C */
3100#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003101
Paul Bakker48916f92012-09-16 19:57:18 +00003102void ssl_handshake_wrapup( ssl_context *ssl )
3103{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003104 int resume = ssl->handshake->resume;
3105
Paul Bakker48916f92012-09-16 19:57:18 +00003106 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3107
3108 /*
3109 * Free our handshake params
3110 */
3111 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003112 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003113 ssl->handshake = NULL;
3114
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003115 if( ssl->renegotiation == SSL_RENEGOTIATION )
3116 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3117
Paul Bakker48916f92012-09-16 19:57:18 +00003118 /*
3119 * Switch in our now active transform context
3120 */
3121 if( ssl->transform )
3122 {
3123 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003124 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003125 }
3126 ssl->transform = ssl->transform_negotiate;
3127 ssl->transform_negotiate = NULL;
3128
Paul Bakker0a597072012-09-25 21:55:46 +00003129 if( ssl->session )
3130 {
3131 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003132 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003133 }
3134 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003135 ssl->session_negotiate = NULL;
3136
Paul Bakker0a597072012-09-25 21:55:46 +00003137 /*
3138 * Add cache entry
3139 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003140 if( ssl->f_set_cache != NULL &&
3141 ssl->session->length != 0 &&
3142 resume == 0 )
3143 {
Paul Bakker0a597072012-09-25 21:55:46 +00003144 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3145 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003146 }
Paul Bakker0a597072012-09-25 21:55:46 +00003147
Paul Bakker48916f92012-09-16 19:57:18 +00003148 ssl->state++;
3149
3150 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3151}
3152
Paul Bakker1ef83d62012-04-11 12:09:53 +00003153int ssl_write_finished( ssl_context *ssl )
3154{
3155 int ret, hash_len;
3156
3157 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3158
Paul Bakker92be97b2013-01-02 17:30:03 +01003159 /*
3160 * Set the out_msg pointer to the correct location based on IV length
3161 */
3162 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3163 {
3164 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3165 ssl->transform_negotiate->fixed_ivlen;
3166 }
3167 else
3168 ssl->out_msg = ssl->out_iv;
3169
Paul Bakker48916f92012-09-16 19:57:18 +00003170 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171
3172 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003173 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3174
Paul Bakker48916f92012-09-16 19:57:18 +00003175 ssl->verify_data_len = hash_len;
3176 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3177
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 ssl->out_msglen = 4 + hash_len;
3179 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3180 ssl->out_msg[0] = SSL_HS_FINISHED;
3181
3182 /*
3183 * In case of session resuming, invert the client and server
3184 * ChangeCipherSpec messages order.
3185 */
Paul Bakker0a597072012-09-25 21:55:46 +00003186 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003187 {
3188 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003189 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003190 else
3191 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3192 }
3193 else
3194 ssl->state++;
3195
Paul Bakker48916f92012-09-16 19:57:18 +00003196 /*
3197 * Switch to our negotiated transform and session parameters for outbound data.
3198 */
3199 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3200 ssl->transform_out = ssl->transform_negotiate;
3201 ssl->session_out = ssl->session_negotiate;
3202 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Paul Bakker07eb38b2012-12-19 14:42:06 +01003204#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3205 if( ssl_hw_record_activate != NULL)
3206 {
3207 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3208 {
3209 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3210 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3211 }
3212 }
3213#endif
3214
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3216 {
3217 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3218 return( ret );
3219 }
3220
3221 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3222
3223 return( 0 );
3224}
3225
3226int ssl_parse_finished( ssl_context *ssl )
3227{
Paul Bakker23986e52011-04-24 08:57:21 +00003228 int ret;
3229 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003230 unsigned char buf[36];
3231
3232 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3233
Paul Bakker48916f92012-09-16 19:57:18 +00003234 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Paul Bakker48916f92012-09-16 19:57:18 +00003236 /*
3237 * Switch to our negotiated transform and session parameters for inbound data.
3238 */
3239 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3240 ssl->transform_in = ssl->transform_negotiate;
3241 ssl->session_in = ssl->session_negotiate;
3242 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003243
Paul Bakker92be97b2013-01-02 17:30:03 +01003244 /*
3245 * Set the in_msg pointer to the correct location based on IV length
3246 */
3247 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3248 {
3249 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3250 ssl->transform_negotiate->fixed_ivlen;
3251 }
3252 else
3253 ssl->in_msg = ssl->in_iv;
3254
Paul Bakker07eb38b2012-12-19 14:42:06 +01003255#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3256 if( ssl_hw_record_activate != NULL)
3257 {
3258 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3259 {
3260 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3261 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3262 }
3263 }
3264#endif
3265
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3267 {
3268 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3269 return( ret );
3270 }
3271
3272 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3273 {
3274 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003275 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 }
3277
Paul Bakker1ef83d62012-04-11 12:09:53 +00003278 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3280
3281 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3282 ssl->in_hslen != 4 + hash_len )
3283 {
3284 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003285 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 }
3287
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003288 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 {
3290 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003291 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 }
3293
Paul Bakker48916f92012-09-16 19:57:18 +00003294 ssl->verify_data_len = hash_len;
3295 memcpy( ssl->peer_verify_data, buf, hash_len );
3296
Paul Bakker0a597072012-09-25 21:55:46 +00003297 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 {
3299 if( ssl->endpoint == SSL_IS_CLIENT )
3300 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3301
3302 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003303 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 }
3305 else
3306 ssl->state++;
3307
3308 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3309
3310 return( 0 );
3311}
3312
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003313static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003314{
3315 if( ssl->transform_negotiate )
3316 ssl_transform_free( ssl->transform_negotiate );
3317 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003318 {
3319 ssl->transform_negotiate =
3320 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003321
3322 if( ssl->transform_negotiate != NULL )
3323 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003324 }
Paul Bakker48916f92012-09-16 19:57:18 +00003325
3326 if( ssl->session_negotiate )
3327 ssl_session_free( ssl->session_negotiate );
3328 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003329 {
3330 ssl->session_negotiate =
3331 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003332
3333 if( ssl->session_negotiate != NULL )
3334 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003335 }
Paul Bakker48916f92012-09-16 19:57:18 +00003336
3337 if( ssl->handshake )
3338 ssl_handshake_free( ssl->handshake );
3339 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003340 {
3341 ssl->handshake = (ssl_handshake_params *)
3342 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003343
3344 if( ssl->handshake != NULL )
3345 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003346 }
Paul Bakker48916f92012-09-16 19:57:18 +00003347
3348 if( ssl->handshake == NULL ||
3349 ssl->transform_negotiate == NULL ||
3350 ssl->session_negotiate == NULL )
3351 {
3352 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3353 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3354 }
3355
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003356#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3357 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003358 md5_starts( &ssl->handshake->fin_md5 );
3359 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003360#endif
3361#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3362#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003363 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003364#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003365#if defined(POLARSSL_SHA512_C)
3366 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003367#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003368#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003369
3370 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003371 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003372
Paul Bakker61d113b2013-07-04 11:51:43 +02003373#if defined(POLARSSL_ECDH_C)
3374 ecdh_init( &ssl->handshake->ecdh_ctx );
3375#endif
3376
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003377#if defined(POLARSSL_X509_CRT_PARSE_C)
3378 ssl->handshake->key_cert = ssl->key_cert;
3379#endif
3380
Paul Bakker48916f92012-09-16 19:57:18 +00003381 return( 0 );
3382}
3383
Paul Bakker5121ce52009-01-03 21:22:43 +00003384/*
3385 * Initialize an SSL context
3386 */
3387int ssl_init( ssl_context *ssl )
3388{
Paul Bakker48916f92012-09-16 19:57:18 +00003389 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 int len = SSL_BUFFER_LEN;
3391
3392 memset( ssl, 0, sizeof( ssl_context ) );
3393
Paul Bakker62f2dee2012-09-28 07:31:51 +00003394 /*
3395 * Sane defaults
3396 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003397 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3398 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3399 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3400 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003401
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003402 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003403
Paul Bakker62f2dee2012-09-28 07:31:51 +00003404#if defined(POLARSSL_DHM_C)
3405 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3406 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3407 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3408 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3409 {
3410 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3411 return( ret );
3412 }
3413#endif
3414
3415 /*
3416 * Prepare base structures
3417 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003418 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003420 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 ssl->in_msg = ssl->in_ctr + 13;
3422
3423 if( ssl->in_ctr == NULL )
3424 {
3425 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003426 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003427 }
3428
Paul Bakker6e339b52013-07-03 13:37:05 +02003429 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003431 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003432 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003433
3434 if( ssl->out_ctr == NULL )
3435 {
3436 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003437 polarssl_free( ssl->in_ctr );
3438 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003439 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003440 }
3441
3442 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3443 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3444
Paul Bakker606b4ba2013-08-14 16:52:14 +02003445#if defined(POLARSSL_SSL_SESSION_TICKETS)
3446 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3447#endif
3448
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003449#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003450 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003451#endif
3452
Paul Bakker48916f92012-09-16 19:57:18 +00003453 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3454 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003455
3456 return( 0 );
3457}
3458
3459/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003460 * Reset an initialized and used SSL context for re-use while retaining
3461 * all application-set variables, function pointers and data.
3462 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003463int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003464{
Paul Bakker48916f92012-09-16 19:57:18 +00003465 int ret;
3466
Paul Bakker7eb013f2011-10-06 12:37:39 +00003467 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003468 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3469 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3470
3471 ssl->verify_data_len = 0;
3472 memset( ssl->own_verify_data, 0, 36 );
3473 memset( ssl->peer_verify_data, 0, 36 );
3474
Paul Bakker7eb013f2011-10-06 12:37:39 +00003475 ssl->in_offt = NULL;
3476
Paul Bakker92be97b2013-01-02 17:30:03 +01003477 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003478 ssl->in_msgtype = 0;
3479 ssl->in_msglen = 0;
3480 ssl->in_left = 0;
3481
3482 ssl->in_hslen = 0;
3483 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003484 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003485
Paul Bakker92be97b2013-01-02 17:30:03 +01003486 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003487 ssl->out_msgtype = 0;
3488 ssl->out_msglen = 0;
3489 ssl->out_left = 0;
3490
Paul Bakker48916f92012-09-16 19:57:18 +00003491 ssl->transform_in = NULL;
3492 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003493
3494 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3495 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003496
3497#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3498 if( ssl_hw_record_reset != NULL)
3499 {
3500 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003501 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003502 {
3503 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3504 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3505 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003506 }
3507#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003508
Paul Bakker48916f92012-09-16 19:57:18 +00003509 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003510 {
Paul Bakker48916f92012-09-16 19:57:18 +00003511 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003512 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003513 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003514 }
Paul Bakker48916f92012-09-16 19:57:18 +00003515
Paul Bakkerc0463502013-02-14 11:19:38 +01003516 if( ssl->session )
3517 {
3518 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003519 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003520 ssl->session = NULL;
3521 }
3522
Paul Bakker48916f92012-09-16 19:57:18 +00003523 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3524 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003525
3526 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003527}
3528
Paul Bakkera503a632013-08-14 13:48:06 +02003529#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003530/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003531 * Allocate and initialize ticket keys
3532 */
3533static int ssl_ticket_keys_init( ssl_context *ssl )
3534{
3535 int ret;
3536 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003537 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003538
3539 if( ssl->ticket_keys != NULL )
3540 return( 0 );
3541
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003542 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3543 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003544 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3545
3546 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003547 {
3548 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003549 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003550 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003551
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003552 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3553 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3554 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3555 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003556 polarssl_free( tkeys );
3557 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003558 }
3559
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003560 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003561 {
3562 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003563 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003564 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003565
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003566 ssl->ticket_keys = tkeys;
3567
3568 return( 0 );
3569}
Paul Bakkera503a632013-08-14 13:48:06 +02003570#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003571
3572/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 * SSL set accessors
3574 */
3575void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3576{
3577 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003578
Paul Bakker606b4ba2013-08-14 16:52:14 +02003579#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003580 if( endpoint == SSL_IS_CLIENT )
3581 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003582#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003583}
3584
3585void ssl_set_authmode( ssl_context *ssl, int authmode )
3586{
3587 ssl->authmode = authmode;
3588}
3589
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003590#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003591void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003592 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003593 void *p_vrfy )
3594{
3595 ssl->f_vrfy = f_vrfy;
3596 ssl->p_vrfy = p_vrfy;
3597}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003598#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003599
Paul Bakker5121ce52009-01-03 21:22:43 +00003600void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003601 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 void *p_rng )
3603{
3604 ssl->f_rng = f_rng;
3605 ssl->p_rng = p_rng;
3606}
3607
3608void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003609 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 void *p_dbg )
3611{
3612 ssl->f_dbg = f_dbg;
3613 ssl->p_dbg = p_dbg;
3614}
3615
3616void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003617 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003618 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003619{
3620 ssl->f_recv = f_recv;
3621 ssl->f_send = f_send;
3622 ssl->p_recv = p_recv;
3623 ssl->p_send = p_send;
3624}
3625
Paul Bakker0a597072012-09-25 21:55:46 +00003626void ssl_set_session_cache( ssl_context *ssl,
3627 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3628 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003629{
Paul Bakker0a597072012-09-25 21:55:46 +00003630 ssl->f_get_cache = f_get_cache;
3631 ssl->p_get_cache = p_get_cache;
3632 ssl->f_set_cache = f_set_cache;
3633 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003634}
3635
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003636int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003637{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003638 int ret;
3639
3640 if( ssl == NULL ||
3641 session == NULL ||
3642 ssl->session_negotiate == NULL ||
3643 ssl->endpoint != SSL_IS_CLIENT )
3644 {
3645 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3646 }
3647
3648 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3649 return( ret );
3650
Paul Bakker0a597072012-09-25 21:55:46 +00003651 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003652
3653 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003654}
3655
Paul Bakkerb68cad62012-08-23 08:34:18 +00003656void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003657{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003658 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3659 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3660 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3661 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3662}
3663
3664void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3665 int major, int minor )
3666{
3667 if( major != SSL_MAJOR_VERSION_3 )
3668 return;
3669
3670 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3671 return;
3672
3673 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003674}
3675
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003676#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003677/* Add a new (empty) key_cert entry an return a pointer to it */
3678static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3679{
3680 ssl_key_cert *key_cert, *last;
3681
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003682 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3683 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003684 return( NULL );
3685
3686 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3687
3688 /* Append the new key_cert to the (possibly empty) current list */
3689 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003690 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003691 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003692 if( ssl->handshake != NULL )
3693 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003694 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003695 else
3696 {
3697 last = ssl->key_cert;
3698 while( last->next != NULL )
3699 last = last->next;
3700 last->next = key_cert;
3701 }
3702
3703 return key_cert;
3704}
3705
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003706void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003707 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003708{
3709 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003710 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 ssl->peer_cn = peer_cn;
3712}
3713
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003714int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003715 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003716{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003717 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3718
3719 if( key_cert == NULL )
3720 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3721
3722 key_cert->cert = own_cert;
3723 key_cert->key = pk_key;
3724
3725 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003726}
3727
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003728#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003729int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003730 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003731{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003732 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003733 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003734
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003735 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003736 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3737
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003738 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3739 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003740 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003741
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003742 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003743
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003744 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003745 if( ret != 0 )
3746 return( ret );
3747
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003748 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003749 return( ret );
3750
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003751 key_cert->cert = own_cert;
3752 key_cert->key_own_alloc = 1;
3753
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003754 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003755}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003756#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003757
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003758int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003759 void *rsa_key,
3760 rsa_decrypt_func rsa_decrypt,
3761 rsa_sign_func rsa_sign,
3762 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003763{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003764 int ret;
3765 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003766
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003767 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003768 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3769
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003770 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3771 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003772 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003773
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003774 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003775
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003776 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3777 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3778 return( ret );
3779
3780 key_cert->cert = own_cert;
3781 key_cert->key_own_alloc = 1;
3782
3783 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003784}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003785#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003786
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003787#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003788int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3789 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003790{
Paul Bakker6db455e2013-09-18 17:29:31 +02003791 if( psk == NULL || psk_identity == NULL )
3792 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3793
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003794 /*
3795 * The length will be check later anyway, but in case it is obviously
3796 * too large, better abort now. The PMS is as follows:
3797 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3798 */
3799 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3800 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3801
Paul Bakker6db455e2013-09-18 17:29:31 +02003802 if( ssl->psk != NULL )
3803 {
3804 polarssl_free( ssl->psk );
3805 polarssl_free( ssl->psk_identity );
3806 }
3807
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003808 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003809 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003810
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003811 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3812 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003813
3814 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3815 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3816
3817 memcpy( ssl->psk, psk, ssl->psk_len );
3818 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003819
3820 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003821}
3822
3823void ssl_set_psk_cb( ssl_context *ssl,
3824 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3825 size_t),
3826 void *p_psk )
3827{
3828 ssl->f_psk = f_psk;
3829 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003830}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003831#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003832
Paul Bakker48916f92012-09-16 19:57:18 +00003833#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003834int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003835{
3836 int ret;
3837
Paul Bakker48916f92012-09-16 19:57:18 +00003838 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003839 {
3840 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3841 return( ret );
3842 }
3843
Paul Bakker48916f92012-09-16 19:57:18 +00003844 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 {
3846 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3847 return( ret );
3848 }
3849
3850 return( 0 );
3851}
3852
Paul Bakker1b57b062011-01-06 15:48:19 +00003853int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3854{
3855 int ret;
3856
Paul Bakker48916f92012-09-16 19:57:18 +00003857 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003858 {
3859 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3860 return( ret );
3861 }
3862
Paul Bakker48916f92012-09-16 19:57:18 +00003863 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003864 {
3865 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3866 return( ret );
3867 }
3868
3869 return( 0 );
3870}
Paul Bakker48916f92012-09-16 19:57:18 +00003871#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003872
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003873#if defined(POLARSSL_SSL_SET_CURVES)
3874/*
3875 * Set the allowed elliptic curves
3876 */
3877void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3878{
3879 ssl->curve_list = curve_list;
3880}
3881#endif
3882
Paul Bakker0be444a2013-08-27 21:55:01 +02003883#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003884int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003885{
3886 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003887 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003888
3889 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003890
3891 if( ssl->hostname_len + 1 == 0 )
3892 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3893
Paul Bakker6e339b52013-07-03 13:37:05 +02003894 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003895
Paul Bakkerb15b8512012-01-13 13:44:06 +00003896 if( ssl->hostname == NULL )
3897 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3898
Paul Bakker3c2122f2013-06-24 19:03:14 +02003899 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003900 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003901
Paul Bakker40ea7de2009-05-03 10:18:48 +00003902 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003903
3904 return( 0 );
3905}
3906
Paul Bakker5701cdc2012-09-27 21:49:42 +00003907void ssl_set_sni( ssl_context *ssl,
3908 int (*f_sni)(void *, ssl_context *,
3909 const unsigned char *, size_t),
3910 void *p_sni )
3911{
3912 ssl->f_sni = f_sni;
3913 ssl->p_sni = p_sni;
3914}
Paul Bakker0be444a2013-08-27 21:55:01 +02003915#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003916
Paul Bakker490ecc82011-10-06 13:04:09 +00003917void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3918{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003919 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3920 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3921 {
3922 ssl->max_major_ver = major;
3923 ssl->max_minor_ver = minor;
3924 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003925}
3926
Paul Bakker1d29fb52012-09-28 13:28:45 +00003927void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3928{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003929 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3930 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3931 {
3932 ssl->min_major_ver = major;
3933 ssl->min_minor_ver = minor;
3934 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003935}
3936
Paul Bakker05decb22013-08-15 13:33:48 +02003937#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003938int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3939{
Paul Bakker77e257e2013-12-16 15:29:52 +01003940 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003941 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003942 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003943 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003944 }
3945
3946 ssl->mfl_code = mfl_code;
3947
3948 return( 0 );
3949}
Paul Bakker05decb22013-08-15 13:33:48 +02003950#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003951
Paul Bakker1f2bc622013-08-15 13:45:55 +02003952#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003953int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003954{
3955 if( ssl->endpoint != SSL_IS_CLIENT )
3956 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3957
Paul Bakker8c1ede62013-07-19 14:14:37 +02003958 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003959
3960 return( 0 );
3961}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003962#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003963
Paul Bakker48916f92012-09-16 19:57:18 +00003964void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3965{
3966 ssl->disable_renegotiation = renegotiation;
3967}
3968
3969void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3970{
3971 ssl->allow_legacy_renegotiation = allow_legacy;
3972}
3973
Paul Bakkera503a632013-08-14 13:48:06 +02003974#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003975int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3976{
3977 ssl->session_tickets = use_tickets;
3978
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003979 if( ssl->endpoint == SSL_IS_CLIENT )
3980 return( 0 );
3981
3982 if( ssl->f_rng == NULL )
3983 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3984
3985 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003986}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003987
3988void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3989{
3990 ssl->ticket_lifetime = lifetime;
3991}
Paul Bakkera503a632013-08-14 13:48:06 +02003992#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003993
Paul Bakker5121ce52009-01-03 21:22:43 +00003994/*
3995 * SSL get accessors
3996 */
Paul Bakker23986e52011-04-24 08:57:21 +00003997size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003998{
3999 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4000}
4001
Paul Bakkerff60ee62010-03-16 21:09:09 +00004002int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004003{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004004 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004005}
4006
Paul Bakkere3166ce2011-01-27 17:40:50 +00004007const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004008{
Paul Bakker926c8e42013-03-06 10:23:34 +01004009 if( ssl == NULL || ssl->session == NULL )
4010 return NULL;
4011
Paul Bakkere3166ce2011-01-27 17:40:50 +00004012 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004013}
4014
Paul Bakker43ca69c2011-01-15 17:35:19 +00004015const char *ssl_get_version( const ssl_context *ssl )
4016{
4017 switch( ssl->minor_ver )
4018 {
4019 case SSL_MINOR_VERSION_0:
4020 return( "SSLv3.0" );
4021
4022 case SSL_MINOR_VERSION_1:
4023 return( "TLSv1.0" );
4024
4025 case SSL_MINOR_VERSION_2:
4026 return( "TLSv1.1" );
4027
Paul Bakker1ef83d62012-04-11 12:09:53 +00004028 case SSL_MINOR_VERSION_3:
4029 return( "TLSv1.2" );
4030
Paul Bakker43ca69c2011-01-15 17:35:19 +00004031 default:
4032 break;
4033 }
4034 return( "unknown" );
4035}
4036
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004037#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004038const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004039{
4040 if( ssl == NULL || ssl->session == NULL )
4041 return NULL;
4042
4043 return ssl->session->peer_cert;
4044}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004045#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004046
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004047int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4048{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004049 if( ssl == NULL ||
4050 dst == NULL ||
4051 ssl->session == NULL ||
4052 ssl->endpoint != SSL_IS_CLIENT )
4053 {
4054 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4055 }
4056
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004057 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004058}
4059
Paul Bakker5121ce52009-01-03 21:22:43 +00004060/*
Paul Bakker1961b702013-01-25 14:49:24 +01004061 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004062 */
Paul Bakker1961b702013-01-25 14:49:24 +01004063int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004064{
Paul Bakker40e46942009-01-03 21:51:57 +00004065 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004066
Paul Bakker40e46942009-01-03 21:51:57 +00004067#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004068 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004069 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004070#endif
4071
Paul Bakker40e46942009-01-03 21:51:57 +00004072#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004073 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004074 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004075#endif
4076
Paul Bakker1961b702013-01-25 14:49:24 +01004077 return( ret );
4078}
4079
4080/*
4081 * Perform the SSL handshake
4082 */
4083int ssl_handshake( ssl_context *ssl )
4084{
4085 int ret = 0;
4086
4087 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4088
4089 while( ssl->state != SSL_HANDSHAKE_OVER )
4090 {
4091 ret = ssl_handshake_step( ssl );
4092
4093 if( ret != 0 )
4094 break;
4095 }
4096
Paul Bakker5121ce52009-01-03 21:22:43 +00004097 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4098
4099 return( ret );
4100}
4101
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004102#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004103/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004104 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004105 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004106static int ssl_write_hello_request( ssl_context *ssl )
4107{
4108 int ret;
4109
4110 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4111
4112 ssl->out_msglen = 4;
4113 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4114 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4115
4116 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4117 {
4118 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4119 return( ret );
4120 }
4121
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004122 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4123
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004124 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4125
4126 return( 0 );
4127}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004128#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004129
4130/*
4131 * Actually renegotiate current connection, triggered by either:
4132 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004133 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004134 * - receiving any handshake message on server during ssl_read() after the
4135 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004136 * If the handshake doesn't complete due to waiting for I/O, it will continue
4137 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004138 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004139static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004140{
4141 int ret;
4142
4143 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4144
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004145 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4146 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004147
4148 ssl->state = SSL_HELLO_REQUEST;
4149 ssl->renegotiation = SSL_RENEGOTIATION;
4150
Paul Bakker48916f92012-09-16 19:57:18 +00004151 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4152 {
4153 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4154 return( ret );
4155 }
4156
4157 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4158
4159 return( 0 );
4160}
4161
4162/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004163 * Renegotiate current connection on client,
4164 * or request renegotiation on server
4165 */
4166int ssl_renegotiate( ssl_context *ssl )
4167{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004168 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004169
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004170#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004171 /* On server, just send the request */
4172 if( ssl->endpoint == SSL_IS_SERVER )
4173 {
4174 if( ssl->state != SSL_HANDSHAKE_OVER )
4175 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4176
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004177 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004178 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004179#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004180
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004181#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004182 /*
4183 * On client, either start the renegotiation process or,
4184 * if already in progress, continue the handshake
4185 */
4186 if( ssl->renegotiation != SSL_RENEGOTIATION )
4187 {
4188 if( ssl->state != SSL_HANDSHAKE_OVER )
4189 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4190
4191 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4192 {
4193 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4194 return( ret );
4195 }
4196 }
4197 else
4198 {
4199 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4200 {
4201 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4202 return( ret );
4203 }
4204 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004205#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004206
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004207 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004208}
4209
4210/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004211 * Receive application data decrypted from the SSL layer
4212 */
Paul Bakker23986e52011-04-24 08:57:21 +00004213int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004214{
Paul Bakker23986e52011-04-24 08:57:21 +00004215 int ret;
4216 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004217
4218 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4219
4220 if( ssl->state != SSL_HANDSHAKE_OVER )
4221 {
4222 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4223 {
4224 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4225 return( ret );
4226 }
4227 }
4228
4229 if( ssl->in_offt == NULL )
4230 {
4231 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4232 {
Paul Bakker831a7552011-05-18 13:32:51 +00004233 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4234 return( 0 );
4235
Paul Bakker5121ce52009-01-03 21:22:43 +00004236 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4237 return( ret );
4238 }
4239
4240 if( ssl->in_msglen == 0 &&
4241 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4242 {
4243 /*
4244 * OpenSSL sends empty messages to randomize the IV
4245 */
4246 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4247 {
Paul Bakker831a7552011-05-18 13:32:51 +00004248 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4249 return( 0 );
4250
Paul Bakker5121ce52009-01-03 21:22:43 +00004251 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4252 return( ret );
4253 }
4254 }
4255
Paul Bakker48916f92012-09-16 19:57:18 +00004256 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4257 {
4258 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4259
4260 if( ssl->endpoint == SSL_IS_CLIENT &&
4261 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4262 ssl->in_hslen != 4 ) )
4263 {
4264 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4265 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4266 }
4267
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004268 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4269 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4270 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004271 {
4272 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4273
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004274#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004275 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004276 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004277 /*
4278 * SSLv3 does not have a "no_renegotiation" alert
4279 */
4280 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4281 return( ret );
4282 }
4283 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004284#endif
4285#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4286 defined(POLARSSL_SSL_PROTO_TLS1_2)
4287 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004288 {
4289 if( ( ret = ssl_send_alert_message( ssl,
4290 SSL_ALERT_LEVEL_WARNING,
4291 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4292 {
4293 return( ret );
4294 }
Paul Bakker48916f92012-09-16 19:57:18 +00004295 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004296 else
4297#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004298 {
4299 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004300 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004301 }
Paul Bakker48916f92012-09-16 19:57:18 +00004302 }
4303 else
4304 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004305 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004306 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004307 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004308 return( ret );
4309 }
4310
4311 return( POLARSSL_ERR_NET_WANT_READ );
4312 }
4313 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004314 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4315 {
4316 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4317 "but not honored by client" ) );
4318 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4319 }
Paul Bakker48916f92012-09-16 19:57:18 +00004320 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004321 {
4322 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004323 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 }
4325
4326 ssl->in_offt = ssl->in_msg;
4327 }
4328
4329 n = ( len < ssl->in_msglen )
4330 ? len : ssl->in_msglen;
4331
4332 memcpy( buf, ssl->in_offt, n );
4333 ssl->in_msglen -= n;
4334
4335 if( ssl->in_msglen == 0 )
4336 /* all bytes consumed */
4337 ssl->in_offt = NULL;
4338 else
4339 /* more data available */
4340 ssl->in_offt += n;
4341
4342 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4343
Paul Bakker23986e52011-04-24 08:57:21 +00004344 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004345}
4346
4347/*
4348 * Send application data to be encrypted by the SSL layer
4349 */
Paul Bakker23986e52011-04-24 08:57:21 +00004350int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004351{
Paul Bakker23986e52011-04-24 08:57:21 +00004352 int ret;
4353 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004354 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004355
4356 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4357
4358 if( ssl->state != SSL_HANDSHAKE_OVER )
4359 {
4360 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4361 {
4362 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4363 return( ret );
4364 }
4365 }
4366
Paul Bakker05decb22013-08-15 13:33:48 +02004367#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004368 /*
4369 * Assume mfl_code is correct since it was checked when set
4370 */
4371 max_len = mfl_code_to_length[ssl->mfl_code];
4372
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004373 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004374 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004375 */
4376 if( ssl->session_out != NULL &&
4377 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4378 {
4379 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4380 }
Paul Bakker05decb22013-08-15 13:33:48 +02004381#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004382
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004383 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004384
Paul Bakker5121ce52009-01-03 21:22:43 +00004385 if( ssl->out_left != 0 )
4386 {
4387 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4388 {
4389 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4390 return( ret );
4391 }
4392 }
Paul Bakker887bd502011-06-08 13:10:54 +00004393 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004394 {
Paul Bakker887bd502011-06-08 13:10:54 +00004395 ssl->out_msglen = n;
4396 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4397 memcpy( ssl->out_msg, buf, n );
4398
4399 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4400 {
4401 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4402 return( ret );
4403 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004404 }
4405
4406 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4407
Paul Bakker23986e52011-04-24 08:57:21 +00004408 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004409}
4410
4411/*
4412 * Notify the peer that the connection is being closed
4413 */
4414int ssl_close_notify( ssl_context *ssl )
4415{
4416 int ret;
4417
4418 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4419
4420 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4421 {
4422 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4423 return( ret );
4424 }
4425
4426 if( ssl->state == SSL_HANDSHAKE_OVER )
4427 {
Paul Bakker48916f92012-09-16 19:57:18 +00004428 if( ( ret = ssl_send_alert_message( ssl,
4429 SSL_ALERT_LEVEL_WARNING,
4430 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004431 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 return( ret );
4433 }
4434 }
4435
4436 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4437
4438 return( ret );
4439}
4440
Paul Bakker48916f92012-09-16 19:57:18 +00004441void ssl_transform_free( ssl_transform *transform )
4442{
4443#if defined(POLARSSL_ZLIB_SUPPORT)
4444 deflateEnd( &transform->ctx_deflate );
4445 inflateEnd( &transform->ctx_inflate );
4446#endif
4447
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004448 cipher_free_ctx( &transform->cipher_ctx_enc );
4449 cipher_free_ctx( &transform->cipher_ctx_dec );
4450
Paul Bakker61d113b2013-07-04 11:51:43 +02004451 md_free_ctx( &transform->md_ctx_enc );
4452 md_free_ctx( &transform->md_ctx_dec );
4453
Paul Bakker48916f92012-09-16 19:57:18 +00004454 memset( transform, 0, sizeof( ssl_transform ) );
4455}
4456
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004457#if defined(POLARSSL_X509_CRT_PARSE_C)
4458static void ssl_key_cert_free( ssl_key_cert *key_cert )
4459{
4460 ssl_key_cert *cur = key_cert, *next;
4461
4462 while( cur != NULL )
4463 {
4464 next = cur->next;
4465
4466 if( cur->key_own_alloc )
4467 {
4468 pk_free( cur->key );
4469 polarssl_free( cur->key );
4470 }
4471 polarssl_free( cur );
4472
4473 cur = next;
4474 }
4475}
4476#endif /* POLARSSL_X509_CRT_PARSE_C */
4477
Paul Bakker48916f92012-09-16 19:57:18 +00004478void ssl_handshake_free( ssl_handshake_params *handshake )
4479{
4480#if defined(POLARSSL_DHM_C)
4481 dhm_free( &handshake->dhm_ctx );
4482#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004483#if defined(POLARSSL_ECDH_C)
4484 ecdh_free( &handshake->ecdh_ctx );
4485#endif
4486
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004487#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004488 /* explicit void pointer cast for buggy MS compiler */
4489 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004490#endif
4491
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004492#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4493 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4494 /*
4495 * Free only the linked list wrapper, not the keys themselves
4496 * since the belong to the SNI callback
4497 */
4498 if( handshake->sni_key_cert != NULL )
4499 {
4500 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4501
4502 while( cur != NULL )
4503 {
4504 next = cur->next;
4505 polarssl_free( cur );
4506 cur = next;
4507 }
4508 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004509#endif
4510
Paul Bakker48916f92012-09-16 19:57:18 +00004511 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4512}
4513
4514void ssl_session_free( ssl_session *session )
4515{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004516#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004517 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004518 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004519 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004520 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004521 }
Paul Bakkered27a042013-04-18 22:46:23 +02004522#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004523
Paul Bakkera503a632013-08-14 13:48:06 +02004524#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004525 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004526#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004527
Paul Bakker0a597072012-09-25 21:55:46 +00004528 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004529}
4530
Paul Bakker5121ce52009-01-03 21:22:43 +00004531/*
4532 * Free an SSL context
4533 */
4534void ssl_free( ssl_context *ssl )
4535{
4536 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4537
Paul Bakker5121ce52009-01-03 21:22:43 +00004538 if( ssl->out_ctr != NULL )
4539 {
4540 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004541 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004542 }
4543
4544 if( ssl->in_ctr != NULL )
4545 {
4546 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004547 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548 }
4549
Paul Bakker16770332013-10-11 09:59:44 +02004550#if defined(POLARSSL_ZLIB_SUPPORT)
4551 if( ssl->compress_buf != NULL )
4552 {
4553 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4554 polarssl_free( ssl->compress_buf );
4555 }
4556#endif
4557
Paul Bakker40e46942009-01-03 21:51:57 +00004558#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004559 mpi_free( &ssl->dhm_P );
4560 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004561#endif
4562
Paul Bakker48916f92012-09-16 19:57:18 +00004563 if( ssl->transform )
4564 {
4565 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004566 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004567 }
4568
4569 if( ssl->handshake )
4570 {
4571 ssl_handshake_free( ssl->handshake );
4572 ssl_transform_free( ssl->transform_negotiate );
4573 ssl_session_free( ssl->session_negotiate );
4574
Paul Bakker6e339b52013-07-03 13:37:05 +02004575 polarssl_free( ssl->handshake );
4576 polarssl_free( ssl->transform_negotiate );
4577 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004578 }
4579
Paul Bakkerc0463502013-02-14 11:19:38 +01004580 if( ssl->session )
4581 {
4582 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004583 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004584 }
4585
Paul Bakkera503a632013-08-14 13:48:06 +02004586#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004587 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004588#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004589
Paul Bakker0be444a2013-08-27 21:55:01 +02004590#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004591 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004592 {
4593 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004594 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004595 ssl->hostname_len = 0;
4596 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004597#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004598
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004599#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004600 if( ssl->psk != NULL )
4601 {
4602 memset( ssl->psk, 0, ssl->psk_len );
4603 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4604 polarssl_free( ssl->psk );
4605 polarssl_free( ssl->psk_identity );
4606 ssl->psk_len = 0;
4607 ssl->psk_identity_len = 0;
4608 }
4609#endif
4610
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004611#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004612 ssl_key_cert_free( ssl->key_cert );
4613#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004614
Paul Bakker05ef8352012-05-08 09:17:57 +00004615#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4616 if( ssl_hw_record_finish != NULL )
4617 {
4618 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4619 ssl_hw_record_finish( ssl );
4620 }
4621#endif
4622
Paul Bakker5121ce52009-01-03 21:22:43 +00004623 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004624
Paul Bakker86f04f42013-02-14 11:20:09 +01004625 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004626 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004627}
4628
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004629#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004630/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004631 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004632 */
4633unsigned char ssl_sig_from_pk( pk_context *pk )
4634{
4635#if defined(POLARSSL_RSA_C)
4636 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4637 return( SSL_SIG_RSA );
4638#endif
4639#if defined(POLARSSL_ECDSA_C)
4640 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4641 return( SSL_SIG_ECDSA );
4642#endif
4643 return( SSL_SIG_ANON );
4644}
4645
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004646pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4647{
4648 switch( sig )
4649 {
4650#if defined(POLARSSL_RSA_C)
4651 case SSL_SIG_RSA:
4652 return( POLARSSL_PK_RSA );
4653#endif
4654#if defined(POLARSSL_ECDSA_C)
4655 case SSL_SIG_ECDSA:
4656 return( POLARSSL_PK_ECDSA );
4657#endif
4658 default:
4659 return( POLARSSL_PK_NONE );
4660 }
4661}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004662#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004663
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004664/*
4665 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4666 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004667md_type_t ssl_md_alg_from_hash( unsigned char hash )
4668{
4669 switch( hash )
4670 {
4671#if defined(POLARSSL_MD5_C)
4672 case SSL_HASH_MD5:
4673 return( POLARSSL_MD_MD5 );
4674#endif
4675#if defined(POLARSSL_SHA1_C)
4676 case SSL_HASH_SHA1:
4677 return( POLARSSL_MD_SHA1 );
4678#endif
4679#if defined(POLARSSL_SHA256_C)
4680 case SSL_HASH_SHA224:
4681 return( POLARSSL_MD_SHA224 );
4682 case SSL_HASH_SHA256:
4683 return( POLARSSL_MD_SHA256 );
4684#endif
4685#if defined(POLARSSL_SHA512_C)
4686 case SSL_HASH_SHA384:
4687 return( POLARSSL_MD_SHA384 );
4688 case SSL_HASH_SHA512:
4689 return( POLARSSL_MD_SHA512 );
4690#endif
4691 default:
4692 return( POLARSSL_MD_NONE );
4693 }
4694}
4695
Paul Bakker5121ce52009-01-03 21:22:43 +00004696#endif
Gergely Budai987bfb52014-01-19 21:48:42 +01004697
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004698#if defined(POLARSSL_SSL_SET_CURVES)
4699/*
4700 * Check is a curve proposed by the peer is in our list.
4701 * Return 1 if we're willing to use it, 0 otherwise.
4702 */
4703int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4704{
4705 const ecp_group_id *gid;
4706
4707 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4708 if( *gid == grp_id )
4709 return( 1 );
4710
4711 return( 0 );
4712}
4713#endif