blob: 8c60428587b51f44dfbe9bb405c0732cd7a07ff3 [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:
1629 * 1. padlen - 1 > ssl->in_msglen
1630 *
1631 * 2. ssl->in_msglen + padlen >
1632 * SSL_MAX_CONTENT_LEN + 256 (max padding)
1633 *
1634 * In both cases we reset padding_idx to a safe value (0) to
1635 * prevent out-of-buffer reads.
1636 */
1637 correct &= ( ssl->in_msglen >= padlen - 1 );
1638 correct &= ( ssl->in_msglen + padlen <= SSL_MAX_CONTENT_LEN + 256 );
1639
1640 padding_idx *= correct;
1641
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001642 for( i = 1; i <= 256; i++ )
1643 {
1644 real_count &= ( i <= padlen );
1645 pad_count += real_count *
1646 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1647 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001648
1649 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001650
Paul Bakkerd66f0702013-01-31 16:57:45 +01001651#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001652 if( padlen > 0 && correct == 0)
1653 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001654#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001655 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001657 else
1658#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1659 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001660 {
1661 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001662 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001665 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001666#endif /* POLARSSL_CIPHER_MODE_CBC &&
1667 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001668 {
1669 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1670 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1671 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
1673 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1674 ssl->in_msg, ssl->in_msglen );
1675
1676 /*
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001677 * Always compute the MAC (RFC4346, CBCTIME), except for GCM of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1680 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1681 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1682 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode !=
1683 POLARSSL_MODE_GCM )
1684 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001685 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1686
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001687 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001688
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001689 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1690 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001692 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001694#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001695 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1696 {
1697 ssl_mac( &ssl->transform_in->md_ctx_dec,
1698 ssl->transform_in->mac_dec,
1699 ssl->in_msg, ssl->in_msglen,
1700 ssl->in_ctr, ssl->in_msgtype );
1701 }
1702 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001703#endif /* POLARSSL_SSL_PROTO_SSL3 */
1704#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001705 defined(POLARSSL_SSL_PROTO_TLS1_2)
1706 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1707 {
1708 /*
1709 * Process MAC and always update for padlen afterwards to make
1710 * total time independent of padlen
1711 *
1712 * extra_run compensates MAC check for padlen
1713 *
1714 * Known timing attacks:
1715 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1716 *
1717 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1718 * correctly. (We round down instead of up, so -56 is the correct
1719 * value for our calculations instead of -55)
1720 */
1721 size_t j, extra_run = 0;
1722 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1723 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001724
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001726
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001727 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1728 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1729 ssl->in_msglen );
1730 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1731 ssl->in_msg + ssl->in_msglen );
1732 for( j = 0; j < extra_run; j++ )
1733 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001734
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001735 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1736 }
1737 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001738#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001739 POLARSSL_SSL_PROTO_TLS1_2 */
1740 {
1741 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1742 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1743 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001745 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1746 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1747 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001749 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001750 ssl->transform_in->maclen ) != 0 )
1751 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001752#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001753 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001754#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001755 correct = 0;
1756 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001757
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001758 /*
1759 * Finally check the correct flag
1760 */
1761 if( correct == 0 )
1762 return( POLARSSL_ERR_SSL_INVALID_MAC );
1763 }
1764#endif /* GCM not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
1766 if( ssl->in_msglen == 0 )
1767 {
1768 ssl->nb_zero++;
1769
1770 /*
1771 * Three or more empty messages may be a DoS attack
1772 * (excessive CPU consumption).
1773 */
1774 if( ssl->nb_zero > 3 )
1775 {
1776 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1777 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001778 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 }
1780 }
1781 else
1782 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001783
Paul Bakker23986e52011-04-24 08:57:21 +00001784 for( i = 8; i > 0; i-- )
1785 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001786 break;
1787
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001788 /* The loops goes to its end iff the counter is wrapping */
1789 if( i == 0 )
1790 {
1791 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1792 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1793 }
1794
Paul Bakker5121ce52009-01-03 21:22:43 +00001795 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1796
1797 return( 0 );
1798}
1799
Paul Bakker2770fbd2012-07-03 13:30:23 +00001800#if defined(POLARSSL_ZLIB_SUPPORT)
1801/*
1802 * Compression/decompression functions
1803 */
1804static int ssl_compress_buf( ssl_context *ssl )
1805{
1806 int ret;
1807 unsigned char *msg_post = ssl->out_msg;
1808 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001809 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001810
1811 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1812
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001813 if( len_pre == 0 )
1814 return( 0 );
1815
Paul Bakker2770fbd2012-07-03 13:30:23 +00001816 memcpy( msg_pre, ssl->out_msg, len_pre );
1817
1818 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1819 ssl->out_msglen ) );
1820
1821 SSL_DEBUG_BUF( 4, "before compression: output payload",
1822 ssl->out_msg, ssl->out_msglen );
1823
Paul Bakker48916f92012-09-16 19:57:18 +00001824 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1825 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1826 ssl->transform_out->ctx_deflate.next_out = msg_post;
1827 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001828
Paul Bakker48916f92012-09-16 19:57:18 +00001829 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001830 if( ret != Z_OK )
1831 {
1832 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1833 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1834 }
1835
Paul Bakker48916f92012-09-16 19:57:18 +00001836 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001837
Paul Bakker2770fbd2012-07-03 13:30:23 +00001838 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1839 ssl->out_msglen ) );
1840
1841 SSL_DEBUG_BUF( 4, "after compression: output payload",
1842 ssl->out_msg, ssl->out_msglen );
1843
1844 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1845
1846 return( 0 );
1847}
1848
1849static int ssl_decompress_buf( ssl_context *ssl )
1850{
1851 int ret;
1852 unsigned char *msg_post = ssl->in_msg;
1853 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001854 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001855
1856 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1857
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001858 if( len_pre == 0 )
1859 return( 0 );
1860
Paul Bakker2770fbd2012-07-03 13:30:23 +00001861 memcpy( msg_pre, ssl->in_msg, len_pre );
1862
1863 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1864 ssl->in_msglen ) );
1865
1866 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1867 ssl->in_msg, ssl->in_msglen );
1868
Paul Bakker48916f92012-09-16 19:57:18 +00001869 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1870 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1871 ssl->transform_in->ctx_inflate.next_out = msg_post;
1872 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001873
Paul Bakker48916f92012-09-16 19:57:18 +00001874 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001875 if( ret != Z_OK )
1876 {
1877 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1878 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1879 }
1880
Paul Bakker48916f92012-09-16 19:57:18 +00001881 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001882
Paul Bakker2770fbd2012-07-03 13:30:23 +00001883 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1884 ssl->in_msglen ) );
1885
1886 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1887 ssl->in_msg, ssl->in_msglen );
1888
1889 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1890
1891 return( 0 );
1892}
1893#endif /* POLARSSL_ZLIB_SUPPORT */
1894
Paul Bakker5121ce52009-01-03 21:22:43 +00001895/*
1896 * Fill the input message buffer
1897 */
Paul Bakker23986e52011-04-24 08:57:21 +00001898int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001899{
Paul Bakker23986e52011-04-24 08:57:21 +00001900 int ret;
1901 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001902
1903 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1904
1905 while( ssl->in_left < nb_want )
1906 {
1907 len = nb_want - ssl->in_left;
1908 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1909
1910 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1911 ssl->in_left, nb_want ) );
1912 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1913
Paul Bakker831a7552011-05-18 13:32:51 +00001914 if( ret == 0 )
1915 return( POLARSSL_ERR_SSL_CONN_EOF );
1916
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 if( ret < 0 )
1918 return( ret );
1919
1920 ssl->in_left += ret;
1921 }
1922
1923 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1924
1925 return( 0 );
1926}
1927
1928/*
1929 * Flush any data not yet written
1930 */
1931int ssl_flush_output( ssl_context *ssl )
1932{
1933 int ret;
1934 unsigned char *buf;
1935
1936 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1937
1938 while( ssl->out_left > 0 )
1939 {
1940 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1941 5 + ssl->out_msglen, ssl->out_left ) );
1942
Paul Bakker5bd42292012-12-19 14:40:42 +01001943 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001945
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1947
1948 if( ret <= 0 )
1949 return( ret );
1950
1951 ssl->out_left -= ret;
1952 }
1953
1954 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1955
1956 return( 0 );
1957}
1958
1959/*
1960 * Record layer functions
1961 */
1962int ssl_write_record( ssl_context *ssl )
1963{
Paul Bakker05ef8352012-05-08 09:17:57 +00001964 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001965 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001966
1967 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1968
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1970 {
1971 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1972 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1973 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1974
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001975 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1976 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 }
1978
Paul Bakker2770fbd2012-07-03 13:30:23 +00001979#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001980 if( ssl->transform_out != NULL &&
1981 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001982 {
1983 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1984 {
1985 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1986 return( ret );
1987 }
1988
1989 len = ssl->out_msglen;
1990 }
1991#endif /*POLARSSL_ZLIB_SUPPORT */
1992
Paul Bakker05ef8352012-05-08 09:17:57 +00001993#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1994 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001995 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001996 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
Paul Bakker05ef8352012-05-08 09:17:57 +00001998 ret = ssl_hw_record_write( ssl );
1999 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2000 {
2001 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
2002 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2003 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002004
2005 if( ret == 0 )
2006 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002007 }
2008#endif
2009 if( !done )
2010 {
2011 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2012 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2013 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2015 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002016
Paul Bakker48916f92012-09-16 19:57:18 +00002017 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002018 {
2019 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2020 {
2021 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2022 return( ret );
2023 }
2024
2025 len = ssl->out_msglen;
2026 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2027 ssl->out_hdr[4] = (unsigned char)( len );
2028 }
2029
2030 ssl->out_left = 5 + ssl->out_msglen;
2031
2032 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2033 "version = [%d:%d], msglen = %d",
2034 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2035 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2036
2037 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002038 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 }
2040
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2042 {
2043 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2044 return( ret );
2045 }
2046
2047 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2048
2049 return( 0 );
2050}
2051
2052int ssl_read_record( ssl_context *ssl )
2053{
Paul Bakker05ef8352012-05-08 09:17:57 +00002054 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002055
2056 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2057
Paul Bakker68884e32013-01-07 18:20:04 +01002058 SSL_DEBUG_BUF( 4, "input record from network",
2059 ssl->in_hdr, 5 + ssl->in_msglen );
2060
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 if( ssl->in_hslen != 0 &&
2062 ssl->in_hslen < ssl->in_msglen )
2063 {
2064 /*
2065 * Get next Handshake message in the current record
2066 */
2067 ssl->in_msglen -= ssl->in_hslen;
2068
Paul Bakker8934a982011-08-05 11:11:53 +00002069 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002070 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
2072 ssl->in_hslen = 4;
2073 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2074
2075 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2076 " %d, type = %d, hslen = %d",
2077 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2078
2079 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2080 {
2081 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002082 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 }
2084
2085 if( ssl->in_msglen < ssl->in_hslen )
2086 {
2087 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002088 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
2090
Paul Bakker48916f92012-09-16 19:57:18 +00002091 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092
2093 return( 0 );
2094 }
2095
2096 ssl->in_hslen = 0;
2097
2098 /*
2099 * Read the record header and validate it
2100 */
2101 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2102 {
2103 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2104 return( ret );
2105 }
2106
2107 ssl->in_msgtype = ssl->in_hdr[0];
2108 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2109
2110 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2111 "version = [%d:%d], msglen = %d",
2112 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2113 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2114
2115 if( ssl->in_hdr[1] != ssl->major_ver )
2116 {
2117 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002118 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 }
2120
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002121 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 {
2123 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002124 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 }
2126
2127 /*
2128 * Make sure the message length is acceptable
2129 */
Paul Bakker48916f92012-09-16 19:57:18 +00002130 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 {
2132 if( ssl->in_msglen < 1 ||
2133 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2134 {
2135 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002136 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 }
2138 }
2139 else
2140 {
Paul Bakker48916f92012-09-16 19:57:18 +00002141 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 {
2143 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002144 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
2146
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002147#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002149 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 {
2151 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002152 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002154#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002156#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2157 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 /*
2159 * TLS encrypted messages can have up to 256 bytes of padding
2160 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002161 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002162 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 {
2164 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002165 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002167#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 }
2169
2170 /*
2171 * Read and optionally decrypt the message contents
2172 */
2173 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2174 {
2175 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2176 return( ret );
2177 }
2178
2179 SSL_DEBUG_BUF( 4, "input record from network",
2180 ssl->in_hdr, 5 + ssl->in_msglen );
2181
Paul Bakker05ef8352012-05-08 09:17:57 +00002182#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2183 if( ssl_hw_record_read != NULL)
2184 {
2185 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2186
2187 ret = ssl_hw_record_read( ssl );
2188 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2189 {
2190 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2191 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2192 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002193
2194 if( ret == 0 )
2195 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002196 }
2197#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002198 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 {
2200 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2201 {
Paul Bakker40865c82013-01-31 17:13:13 +01002202#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2203 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2204 {
2205 ssl_send_alert_message( ssl,
2206 SSL_ALERT_LEVEL_FATAL,
2207 SSL_ALERT_MSG_BAD_RECORD_MAC );
2208 }
2209#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2211 return( ret );
2212 }
2213
2214 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2215 ssl->in_msg, ssl->in_msglen );
2216
2217 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2218 {
2219 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002220 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 }
2222 }
2223
Paul Bakker2770fbd2012-07-03 13:30:23 +00002224#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002225 if( ssl->transform_in != NULL &&
2226 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002227 {
2228 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2229 {
2230 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2231 return( ret );
2232 }
2233
2234 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2235 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2236 }
2237#endif /* POLARSSL_ZLIB_SUPPORT */
2238
Paul Bakker0a925182012-04-16 06:46:41 +00002239 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2240 ssl->in_msgtype != SSL_MSG_ALERT &&
2241 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2242 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2243 {
2244 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2245
Paul Bakker48916f92012-09-16 19:57:18 +00002246 if( ( ret = ssl_send_alert_message( ssl,
2247 SSL_ALERT_LEVEL_FATAL,
2248 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002249 {
2250 return( ret );
2251 }
2252
2253 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2254 }
2255
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2257 {
2258 ssl->in_hslen = 4;
2259 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2260
2261 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2262 " %d, type = %d, hslen = %d",
2263 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2264
2265 /*
2266 * Additional checks to validate the handshake header
2267 */
2268 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2269 {
2270 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002271 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 }
2273
2274 if( ssl->in_msglen < ssl->in_hslen )
2275 {
2276 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002277 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 }
2279
Paul Bakker48916f92012-09-16 19:57:18 +00002280 if( ssl->state != SSL_HANDSHAKE_OVER )
2281 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 }
2283
2284 if( ssl->in_msgtype == SSL_MSG_ALERT )
2285 {
2286 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2287 ssl->in_msg[0], ssl->in_msg[1] ) );
2288
2289 /*
2290 * Ignore non-fatal alerts, except close_notify
2291 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002292 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002294 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2295 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002296 /**
2297 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2298 * error identifier.
2299 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002300 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 }
2302
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002303 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2304 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 {
2306 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002307 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
2309 }
2310
2311 ssl->in_left = 0;
2312
2313 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2314
2315 return( 0 );
2316}
2317
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002318int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2319{
2320 int ret;
2321
2322 if( ( ret = ssl_send_alert_message( ssl,
2323 SSL_ALERT_LEVEL_FATAL,
2324 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2325 {
2326 return( ret );
2327 }
2328
2329 return( 0 );
2330}
2331
Paul Bakker0a925182012-04-16 06:46:41 +00002332int ssl_send_alert_message( ssl_context *ssl,
2333 unsigned char level,
2334 unsigned char message )
2335{
2336 int ret;
2337
2338 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2339
2340 ssl->out_msgtype = SSL_MSG_ALERT;
2341 ssl->out_msglen = 2;
2342 ssl->out_msg[0] = level;
2343 ssl->out_msg[1] = message;
2344
2345 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2346 {
2347 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2348 return( ret );
2349 }
2350
2351 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2352
2353 return( 0 );
2354}
2355
Paul Bakker5121ce52009-01-03 21:22:43 +00002356/*
2357 * Handshake functions
2358 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002359#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2360 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2361 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2362 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2363 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2364 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2365 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002366int ssl_write_certificate( ssl_context *ssl )
2367{
Paul Bakkered27a042013-04-18 22:46:23 +02002368 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002369 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002370
2371 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2372
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002374 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2375 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002376 {
2377 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2378 ssl->state++;
2379 return( 0 );
2380 }
2381
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002382 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002383 return( ret );
2384}
2385
2386int ssl_parse_certificate( ssl_context *ssl )
2387{
2388 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2389 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2390
2391 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2392
2393 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002394 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2395 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002396 {
2397 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2398 ssl->state++;
2399 return( 0 );
2400 }
2401
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002402 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002403 return( ret );
2404}
2405#else
2406int ssl_write_certificate( ssl_context *ssl )
2407{
2408 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2409 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002410 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002411 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2412
2413 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2414
2415 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002416 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2417 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002418 {
2419 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2420 ssl->state++;
2421 return( 0 );
2422 }
2423
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 if( ssl->endpoint == SSL_IS_CLIENT )
2425 {
2426 if( ssl->client_auth == 0 )
2427 {
2428 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2429 ssl->state++;
2430 return( 0 );
2431 }
2432
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002433#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 /*
2435 * If using SSLv3 and got no cert, send an Alert message
2436 * (otherwise an empty Certificate message will be sent).
2437 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002438 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2440 {
2441 ssl->out_msglen = 2;
2442 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002443 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2444 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
2446 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2447 goto write_msg;
2448 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002449#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 }
2451 else /* SSL_IS_SERVER */
2452 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002453 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 {
2455 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002456 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 }
2458 }
2459
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002460 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
2462 /*
2463 * 0 . 0 handshake type
2464 * 1 . 3 handshake length
2465 * 4 . 6 length of all certs
2466 * 7 . 9 length of cert. 1
2467 * 10 . n-1 peer certificate
2468 * n . n+2 length of cert. 2
2469 * n+3 . ... upper level cert, etc.
2470 */
2471 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002472 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Paul Bakker29087132010-03-21 21:03:34 +00002474 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 {
2476 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002477 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 {
2479 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2480 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002481 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 }
2483
2484 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2485 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2486 ssl->out_msg[i + 2] = (unsigned char)( n );
2487
2488 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2489 i += n; crt = crt->next;
2490 }
2491
2492 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2493 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2494 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2495
2496 ssl->out_msglen = i;
2497 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2498 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2499
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002500#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002501write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002502#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
2504 ssl->state++;
2505
2506 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2507 {
2508 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2509 return( ret );
2510 }
2511
2512 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2513
Paul Bakkered27a042013-04-18 22:46:23 +02002514 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515}
2516
2517int ssl_parse_certificate( ssl_context *ssl )
2518{
Paul Bakkered27a042013-04-18 22:46:23 +02002519 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002520 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002521 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
2523 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2524
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002525 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002526 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2527 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002528 {
2529 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2530 ssl->state++;
2531 return( 0 );
2532 }
2533
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002535 ( ssl->authmode == SSL_VERIFY_NONE ||
2536 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002538 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2540 ssl->state++;
2541 return( 0 );
2542 }
2543
2544 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2545 {
2546 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2547 return( ret );
2548 }
2549
2550 ssl->state++;
2551
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002552#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 /*
2554 * Check if the client sent an empty certificate
2555 */
2556 if( ssl->endpoint == SSL_IS_SERVER &&
2557 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2558 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002559 if( ssl->in_msglen == 2 &&
2560 ssl->in_msgtype == SSL_MSG_ALERT &&
2561 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2562 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 {
2564 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2565
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002566 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2568 return( 0 );
2569 else
Paul Bakker40e46942009-01-03 21:51:57 +00002570 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 }
2572 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002573#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002575#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2576 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 if( ssl->endpoint == SSL_IS_SERVER &&
2578 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2579 {
2580 if( ssl->in_hslen == 7 &&
2581 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2582 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2583 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2584 {
2585 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2586
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002587 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002589 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 else
2591 return( 0 );
2592 }
2593 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002594#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2595 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
2597 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2598 {
2599 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002600 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
2602
2603 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2604 {
2605 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002606 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607 }
2608
2609 /*
2610 * Same message structure as in ssl_write_certificate()
2611 */
2612 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2613
2614 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2615 {
2616 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002617 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002620 /* In case we tried to reuse a session but it failed */
2621 if( ssl->session_negotiate->peer_cert != NULL )
2622 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002623 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002624 polarssl_free( ssl->session_negotiate->peer_cert );
2625 }
2626
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002627 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2628 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 {
2630 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002631 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002632 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 }
2634
Paul Bakkerb6b09562013-09-18 14:17:41 +02002635 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
2637 i = 7;
2638
2639 while( i < ssl->in_hslen )
2640 {
2641 if( ssl->in_msg[i] != 0 )
2642 {
2643 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002644 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 }
2646
2647 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2648 | (unsigned int) ssl->in_msg[i + 2];
2649 i += 3;
2650
2651 if( n < 128 || i + n > ssl->in_hslen )
2652 {
2653 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002654 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
Paul Bakkerddf26b42013-09-18 13:46:23 +02002657 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2658 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 if( ret != 0 )
2660 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002661 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 return( ret );
2663 }
2664
2665 i += n;
2666 }
2667
Paul Bakker48916f92012-09-16 19:57:18 +00002668 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002670 /*
2671 * On client, make sure the server cert doesn't change during renego to
2672 * avoid "triple handshake" attack: https://secure-resumption.com/
2673 */
2674 if( ssl->endpoint == SSL_IS_CLIENT &&
2675 ssl->renegotiation == SSL_RENEGOTIATION )
2676 {
2677 if( ssl->session->peer_cert == NULL )
2678 {
2679 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2680 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2681 }
2682
2683 if( ssl->session->peer_cert->raw.len !=
2684 ssl->session_negotiate->peer_cert->raw.len ||
2685 memcmp( ssl->session->peer_cert->raw.p,
2686 ssl->session_negotiate->peer_cert->raw.p,
2687 ssl->session->peer_cert->raw.len ) != 0 )
2688 {
2689 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2690 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2691 }
2692 }
2693
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 if( ssl->authmode != SSL_VERIFY_NONE )
2695 {
2696 if( ssl->ca_chain == NULL )
2697 {
2698 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002699 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 }
2701
Paul Bakkerddf26b42013-09-18 13:46:23 +02002702 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2703 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2704 &ssl->session_negotiate->verify_result,
2705 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
2707 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002708 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002710 }
2711#if defined(POLARSSL_SSL_SET_CURVES)
2712 else
2713 {
2714 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
2715
2716 /* If certificate uses an EC key, make sure the curve is OK */
2717 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2718 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2719 {
2720 SSL_DEBUG_MSG( 1, ( "bad server certificate (EC key curve)" ) );
2721 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2722 }
2723 }
2724#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
2726 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2727 ret = 0;
2728 }
2729
2730 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2731
2732 return( ret );
2733}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002734#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2735 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2736 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2737 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2738 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2739 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2740 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
2742int ssl_write_change_cipher_spec( ssl_context *ssl )
2743{
2744 int ret;
2745
2746 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2747
2748 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2749 ssl->out_msglen = 1;
2750 ssl->out_msg[0] = 1;
2751
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 ssl->state++;
2753
2754 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2755 {
2756 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2757 return( ret );
2758 }
2759
2760 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2761
2762 return( 0 );
2763}
2764
2765int ssl_parse_change_cipher_spec( ssl_context *ssl )
2766{
2767 int ret;
2768
2769 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2770
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2772 {
2773 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2774 return( ret );
2775 }
2776
2777 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2778 {
2779 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002780 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 }
2782
2783 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2784 {
2785 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002786 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 }
2788
2789 ssl->state++;
2790
2791 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2792
2793 return( 0 );
2794}
2795
Paul Bakker41c83d32013-03-20 14:39:14 +01002796void ssl_optimize_checksum( ssl_context *ssl,
2797 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002798{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002799 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002800
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002801#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2802 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002803 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002804 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002805 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002806#endif
2807#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2808#if defined(POLARSSL_SHA512_C)
2809 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2810 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2811 else
2812#endif
2813#if defined(POLARSSL_SHA256_C)
2814 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002815 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002816 else
2817#endif
2818#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2819 /* Should never happen */
2820 return;
Paul Bakker380da532012-04-18 16:10:25 +00002821}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002822
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002823static void ssl_update_checksum_start( ssl_context *ssl,
2824 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002825{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002826#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2827 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002828 md5_update( &ssl->handshake->fin_md5 , buf, len );
2829 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002830#endif
2831#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2832#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002833 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002834#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002835#if defined(POLARSSL_SHA512_C)
2836 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002837#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002838#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002839}
2840
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002841#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2842 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002843static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2844 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002845{
Paul Bakker48916f92012-09-16 19:57:18 +00002846 md5_update( &ssl->handshake->fin_md5 , buf, len );
2847 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002848}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002849#endif
Paul Bakker380da532012-04-18 16:10:25 +00002850
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002851#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2852#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002853static void ssl_update_checksum_sha256( ssl_context *ssl,
2854 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002855{
Paul Bakker9e36f042013-06-30 14:34:05 +02002856 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002857}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002858#endif
Paul Bakker380da532012-04-18 16:10:25 +00002859
Paul Bakker9e36f042013-06-30 14:34:05 +02002860#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002861static void ssl_update_checksum_sha384( ssl_context *ssl,
2862 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002863{
Paul Bakker9e36f042013-06-30 14:34:05 +02002864 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002865}
Paul Bakker769075d2012-11-24 11:26:46 +01002866#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002867#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002868
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002869#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002870static void ssl_calc_finished_ssl(
2871 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002872{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002873 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002874 md5_context md5;
2875 sha1_context sha1;
2876
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 unsigned char padbuf[48];
2878 unsigned char md5sum[16];
2879 unsigned char sha1sum[20];
2880
Paul Bakker48916f92012-09-16 19:57:18 +00002881 ssl_session *session = ssl->session_negotiate;
2882 if( !session )
2883 session = ssl->session;
2884
Paul Bakker1ef83d62012-04-11 12:09:53 +00002885 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2886
Paul Bakker48916f92012-09-16 19:57:18 +00002887 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2888 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002889
2890 /*
2891 * SSLv3:
2892 * hash =
2893 * MD5( master + pad2 +
2894 * MD5( handshake + sender + master + pad1 ) )
2895 * + SHA1( master + pad2 +
2896 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 */
2898
Paul Bakker90995b52013-06-24 19:20:35 +02002899#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002901 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002902#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002903
Paul Bakker90995b52013-06-24 19:20:35 +02002904#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002906 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002907#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
Paul Bakker3c2122f2013-06-24 19:03:14 +02002909 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2910 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Paul Bakker1ef83d62012-04-11 12:09:53 +00002912 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
Paul Bakker3c2122f2013-06-24 19:03:14 +02002914 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002915 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002916 md5_update( &md5, padbuf, 48 );
2917 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Paul Bakker3c2122f2013-06-24 19:03:14 +02002919 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002920 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002921 sha1_update( &sha1, padbuf, 40 );
2922 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Paul Bakker1ef83d62012-04-11 12:09:53 +00002926 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002927 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002928 md5_update( &md5, padbuf, 48 );
2929 md5_update( &md5, md5sum, 16 );
2930 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
Paul Bakker1ef83d62012-04-11 12:09:53 +00002932 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002933 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002934 sha1_update( &sha1, padbuf , 40 );
2935 sha1_update( &sha1, sha1sum, 20 );
2936 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
Paul Bakker1ef83d62012-04-11 12:09:53 +00002938 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940 memset( &md5, 0, sizeof( md5_context ) );
2941 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
2943 memset( padbuf, 0, sizeof( padbuf ) );
2944 memset( md5sum, 0, sizeof( md5sum ) );
2945 memset( sha1sum, 0, sizeof( sha1sum ) );
2946
2947 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2948}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002949#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002951#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952static void ssl_calc_finished_tls(
2953 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002954{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002956 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002957 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002959 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
Paul Bakker48916f92012-09-16 19:57:18 +00002961 ssl_session *session = ssl->session_negotiate;
2962 if( !session )
2963 session = ssl->session;
2964
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Paul Bakker48916f92012-09-16 19:57:18 +00002967 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2968 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Paul Bakker1ef83d62012-04-11 12:09:53 +00002970 /*
2971 * TLSv1:
2972 * hash = PRF( master, finished_label,
2973 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2974 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Paul Bakker90995b52013-06-24 19:20:35 +02002976#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002977 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2978 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002979#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002980
Paul Bakker90995b52013-06-24 19:20:35 +02002981#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2983 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002984#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002985
2986 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002987 ? "client finished"
2988 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989
2990 md5_finish( &md5, padbuf );
2991 sha1_finish( &sha1, padbuf + 16 );
2992
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002993 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002994 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995
2996 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2997
2998 memset( &md5, 0, sizeof( md5_context ) );
2999 memset( &sha1, 0, sizeof( sha1_context ) );
3000
3001 memset( padbuf, 0, sizeof( padbuf ) );
3002
3003 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3004}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003005#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003006
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003007#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3008#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003009static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003010 ssl_context *ssl, unsigned char *buf, int from )
3011{
3012 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003013 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003014 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003015 unsigned char padbuf[32];
3016
Paul Bakker48916f92012-09-16 19:57:18 +00003017 ssl_session *session = ssl->session_negotiate;
3018 if( !session )
3019 session = ssl->session;
3020
Paul Bakker380da532012-04-18 16:10:25 +00003021 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003022
Paul Bakker9e36f042013-06-30 14:34:05 +02003023 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003024
3025 /*
3026 * TLSv1.2:
3027 * hash = PRF( master, finished_label,
3028 * Hash( handshake ) )[0.11]
3029 */
3030
Paul Bakker9e36f042013-06-30 14:34:05 +02003031#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003033 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003034#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003035
3036 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003037 ? "client finished"
3038 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003039
Paul Bakker9e36f042013-06-30 14:34:05 +02003040 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003041
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003042 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003043 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003044
3045 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3046
Paul Bakker9e36f042013-06-30 14:34:05 +02003047 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048
3049 memset( padbuf, 0, sizeof( padbuf ) );
3050
3051 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3052}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003053#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003054
Paul Bakker9e36f042013-06-30 14:34:05 +02003055#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003056static void ssl_calc_finished_tls_sha384(
3057 ssl_context *ssl, unsigned char *buf, int from )
3058{
3059 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003060 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003061 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003062 unsigned char padbuf[48];
3063
Paul Bakker48916f92012-09-16 19:57:18 +00003064 ssl_session *session = ssl->session_negotiate;
3065 if( !session )
3066 session = ssl->session;
3067
Paul Bakker380da532012-04-18 16:10:25 +00003068 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003069
Paul Bakker9e36f042013-06-30 14:34:05 +02003070 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003071
3072 /*
3073 * TLSv1.2:
3074 * hash = PRF( master, finished_label,
3075 * Hash( handshake ) )[0.11]
3076 */
3077
Paul Bakker9e36f042013-06-30 14:34:05 +02003078#if !defined(POLARSSL_SHA512_ALT)
3079 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3080 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003081#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003082
3083 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003084 ? "client finished"
3085 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003086
Paul Bakker9e36f042013-06-30 14:34:05 +02003087 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003088
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003089 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003090 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003091
3092 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3093
Paul Bakker9e36f042013-06-30 14:34:05 +02003094 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003095
3096 memset( padbuf, 0, sizeof( padbuf ) );
3097
3098 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3099}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003100#endif /* POLARSSL_SHA512_C */
3101#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003102
Paul Bakker48916f92012-09-16 19:57:18 +00003103void ssl_handshake_wrapup( ssl_context *ssl )
3104{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003105 int resume = ssl->handshake->resume;
3106
Paul Bakker48916f92012-09-16 19:57:18 +00003107 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3108
3109 /*
3110 * Free our handshake params
3111 */
3112 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003113 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003114 ssl->handshake = NULL;
3115
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003116 if( ssl->renegotiation == SSL_RENEGOTIATION )
3117 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3118
Paul Bakker48916f92012-09-16 19:57:18 +00003119 /*
3120 * Switch in our now active transform context
3121 */
3122 if( ssl->transform )
3123 {
3124 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003125 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003126 }
3127 ssl->transform = ssl->transform_negotiate;
3128 ssl->transform_negotiate = NULL;
3129
Paul Bakker0a597072012-09-25 21:55:46 +00003130 if( ssl->session )
3131 {
3132 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003133 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003134 }
3135 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003136 ssl->session_negotiate = NULL;
3137
Paul Bakker0a597072012-09-25 21:55:46 +00003138 /*
3139 * Add cache entry
3140 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003141 if( ssl->f_set_cache != NULL &&
3142 ssl->session->length != 0 &&
3143 resume == 0 )
3144 {
Paul Bakker0a597072012-09-25 21:55:46 +00003145 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3146 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003147 }
Paul Bakker0a597072012-09-25 21:55:46 +00003148
Paul Bakker48916f92012-09-16 19:57:18 +00003149 ssl->state++;
3150
3151 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3152}
3153
Paul Bakker1ef83d62012-04-11 12:09:53 +00003154int ssl_write_finished( ssl_context *ssl )
3155{
3156 int ret, hash_len;
3157
3158 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3159
Paul Bakker92be97b2013-01-02 17:30:03 +01003160 /*
3161 * Set the out_msg pointer to the correct location based on IV length
3162 */
3163 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3164 {
3165 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3166 ssl->transform_negotiate->fixed_ivlen;
3167 }
3168 else
3169 ssl->out_msg = ssl->out_iv;
3170
Paul Bakker48916f92012-09-16 19:57:18 +00003171 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003172
3173 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3175
Paul Bakker48916f92012-09-16 19:57:18 +00003176 ssl->verify_data_len = hash_len;
3177 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3178
Paul Bakker5121ce52009-01-03 21:22:43 +00003179 ssl->out_msglen = 4 + hash_len;
3180 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3181 ssl->out_msg[0] = SSL_HS_FINISHED;
3182
3183 /*
3184 * In case of session resuming, invert the client and server
3185 * ChangeCipherSpec messages order.
3186 */
Paul Bakker0a597072012-09-25 21:55:46 +00003187 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003188 {
3189 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003190 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 else
3192 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3193 }
3194 else
3195 ssl->state++;
3196
Paul Bakker48916f92012-09-16 19:57:18 +00003197 /*
3198 * Switch to our negotiated transform and session parameters for outbound data.
3199 */
3200 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3201 ssl->transform_out = ssl->transform_negotiate;
3202 ssl->session_out = ssl->session_negotiate;
3203 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
Paul Bakker07eb38b2012-12-19 14:42:06 +01003205#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3206 if( ssl_hw_record_activate != NULL)
3207 {
3208 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3209 {
3210 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3211 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3212 }
3213 }
3214#endif
3215
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3217 {
3218 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3219 return( ret );
3220 }
3221
3222 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3223
3224 return( 0 );
3225}
3226
3227int ssl_parse_finished( ssl_context *ssl )
3228{
Paul Bakker23986e52011-04-24 08:57:21 +00003229 int ret;
3230 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 unsigned char buf[36];
3232
3233 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3234
Paul Bakker48916f92012-09-16 19:57:18 +00003235 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
Paul Bakker48916f92012-09-16 19:57:18 +00003237 /*
3238 * Switch to our negotiated transform and session parameters for inbound data.
3239 */
3240 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3241 ssl->transform_in = ssl->transform_negotiate;
3242 ssl->session_in = ssl->session_negotiate;
3243 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003244
Paul Bakker92be97b2013-01-02 17:30:03 +01003245 /*
3246 * Set the in_msg pointer to the correct location based on IV length
3247 */
3248 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3249 {
3250 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3251 ssl->transform_negotiate->fixed_ivlen;
3252 }
3253 else
3254 ssl->in_msg = ssl->in_iv;
3255
Paul Bakker07eb38b2012-12-19 14:42:06 +01003256#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3257 if( ssl_hw_record_activate != NULL)
3258 {
3259 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3260 {
3261 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3262 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3263 }
3264 }
3265#endif
3266
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3268 {
3269 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3270 return( ret );
3271 }
3272
3273 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3274 {
3275 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003276 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 }
3278
Paul Bakker1ef83d62012-04-11 12:09:53 +00003279 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3281
3282 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3283 ssl->in_hslen != 4 + hash_len )
3284 {
3285 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003286 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 }
3288
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003289 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 {
3291 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003292 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 }
3294
Paul Bakker48916f92012-09-16 19:57:18 +00003295 ssl->verify_data_len = hash_len;
3296 memcpy( ssl->peer_verify_data, buf, hash_len );
3297
Paul Bakker0a597072012-09-25 21:55:46 +00003298 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 {
3300 if( ssl->endpoint == SSL_IS_CLIENT )
3301 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3302
3303 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003304 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 }
3306 else
3307 ssl->state++;
3308
3309 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3310
3311 return( 0 );
3312}
3313
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003314static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003315{
3316 if( ssl->transform_negotiate )
3317 ssl_transform_free( ssl->transform_negotiate );
3318 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003319 {
3320 ssl->transform_negotiate =
3321 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3322 }
Paul Bakker48916f92012-09-16 19:57:18 +00003323
3324 if( ssl->session_negotiate )
3325 ssl_session_free( ssl->session_negotiate );
3326 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003327 {
3328 ssl->session_negotiate =
3329 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3330 }
Paul Bakker48916f92012-09-16 19:57:18 +00003331
3332 if( ssl->handshake )
3333 ssl_handshake_free( ssl->handshake );
3334 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003335 {
3336 ssl->handshake = (ssl_handshake_params *)
3337 polarssl_malloc( sizeof(ssl_handshake_params) );
3338 }
Paul Bakker48916f92012-09-16 19:57:18 +00003339
3340 if( ssl->handshake == NULL ||
3341 ssl->transform_negotiate == NULL ||
3342 ssl->session_negotiate == NULL )
3343 {
3344 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3345 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3346 }
3347
3348 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3349 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3350 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3351
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003352#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3353 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003354 md5_starts( &ssl->handshake->fin_md5 );
3355 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003356#endif
3357#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3358#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003359 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003360#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003361#if defined(POLARSSL_SHA512_C)
3362 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003363#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003364#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003365
3366 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003367 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003368
Paul Bakker61d113b2013-07-04 11:51:43 +02003369#if defined(POLARSSL_ECDH_C)
3370 ecdh_init( &ssl->handshake->ecdh_ctx );
3371#endif
3372
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003373#if defined(POLARSSL_X509_CRT_PARSE_C)
3374 ssl->handshake->key_cert = ssl->key_cert;
3375#endif
3376
Paul Bakker48916f92012-09-16 19:57:18 +00003377 return( 0 );
3378}
3379
Paul Bakker5121ce52009-01-03 21:22:43 +00003380/*
3381 * Initialize an SSL context
3382 */
3383int ssl_init( ssl_context *ssl )
3384{
Paul Bakker48916f92012-09-16 19:57:18 +00003385 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 int len = SSL_BUFFER_LEN;
3387
3388 memset( ssl, 0, sizeof( ssl_context ) );
3389
Paul Bakker62f2dee2012-09-28 07:31:51 +00003390 /*
3391 * Sane defaults
3392 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003393 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3394 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3395 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3396 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003397
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003398 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003399
Paul Bakker62f2dee2012-09-28 07:31:51 +00003400#if defined(POLARSSL_DHM_C)
3401 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3402 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3403 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3404 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3405 {
3406 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3407 return( ret );
3408 }
3409#endif
3410
3411 /*
3412 * Prepare base structures
3413 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003414 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003415 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003416 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003417 ssl->in_msg = ssl->in_ctr + 13;
3418
3419 if( ssl->in_ctr == NULL )
3420 {
3421 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003422 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003423 }
3424
Paul Bakker6e339b52013-07-03 13:37:05 +02003425 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003427 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003428 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003429
3430 if( ssl->out_ctr == NULL )
3431 {
3432 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003433 polarssl_free( ssl->in_ctr );
3434 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003435 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 }
3437
3438 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3439 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3440
Paul Bakker606b4ba2013-08-14 16:52:14 +02003441#if defined(POLARSSL_SSL_SESSION_TICKETS)
3442 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3443#endif
3444
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003445#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003446 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003447#endif
3448
Paul Bakker48916f92012-09-16 19:57:18 +00003449 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3450 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003451
3452 return( 0 );
3453}
3454
3455/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003456 * Reset an initialized and used SSL context for re-use while retaining
3457 * all application-set variables, function pointers and data.
3458 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003459int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003460{
Paul Bakker48916f92012-09-16 19:57:18 +00003461 int ret;
3462
Paul Bakker7eb013f2011-10-06 12:37:39 +00003463 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003464 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3465 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3466
3467 ssl->verify_data_len = 0;
3468 memset( ssl->own_verify_data, 0, 36 );
3469 memset( ssl->peer_verify_data, 0, 36 );
3470
Paul Bakker7eb013f2011-10-06 12:37:39 +00003471 ssl->in_offt = NULL;
3472
Paul Bakker92be97b2013-01-02 17:30:03 +01003473 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003474 ssl->in_msgtype = 0;
3475 ssl->in_msglen = 0;
3476 ssl->in_left = 0;
3477
3478 ssl->in_hslen = 0;
3479 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003480 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003481
Paul Bakker92be97b2013-01-02 17:30:03 +01003482 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003483 ssl->out_msgtype = 0;
3484 ssl->out_msglen = 0;
3485 ssl->out_left = 0;
3486
Paul Bakker48916f92012-09-16 19:57:18 +00003487 ssl->transform_in = NULL;
3488 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003489
3490 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3491 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003492
3493#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3494 if( ssl_hw_record_reset != NULL)
3495 {
3496 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003497 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003498 {
3499 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3500 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3501 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003502 }
3503#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003504
Paul Bakker48916f92012-09-16 19:57:18 +00003505 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003506 {
Paul Bakker48916f92012-09-16 19:57:18 +00003507 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003508 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003509 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003510 }
Paul Bakker48916f92012-09-16 19:57:18 +00003511
Paul Bakkerc0463502013-02-14 11:19:38 +01003512 if( ssl->session )
3513 {
3514 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003515 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003516 ssl->session = NULL;
3517 }
3518
Paul Bakker48916f92012-09-16 19:57:18 +00003519 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3520 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003521
3522 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003523}
3524
Paul Bakkera503a632013-08-14 13:48:06 +02003525#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003526/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003527 * Allocate and initialize ticket keys
3528 */
3529static int ssl_ticket_keys_init( ssl_context *ssl )
3530{
3531 int ret;
3532 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003533 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003534
3535 if( ssl->ticket_keys != NULL )
3536 return( 0 );
3537
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003538 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3539 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003540 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3541
3542 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003543 {
3544 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003545 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003546 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003547
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003548 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3549 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3550 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3551 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003552 polarssl_free( tkeys );
3553 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003554 }
3555
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003556 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003557 {
3558 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003559 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003560 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003561
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003562 ssl->ticket_keys = tkeys;
3563
3564 return( 0 );
3565}
Paul Bakkera503a632013-08-14 13:48:06 +02003566#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003567
3568/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 * SSL set accessors
3570 */
3571void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3572{
3573 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003574
Paul Bakker606b4ba2013-08-14 16:52:14 +02003575#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003576 if( endpoint == SSL_IS_CLIENT )
3577 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003578#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003579}
3580
3581void ssl_set_authmode( ssl_context *ssl, int authmode )
3582{
3583 ssl->authmode = authmode;
3584}
3585
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003586#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003587void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003588 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003589 void *p_vrfy )
3590{
3591 ssl->f_vrfy = f_vrfy;
3592 ssl->p_vrfy = p_vrfy;
3593}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003594#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003595
Paul Bakker5121ce52009-01-03 21:22:43 +00003596void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003597 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003598 void *p_rng )
3599{
3600 ssl->f_rng = f_rng;
3601 ssl->p_rng = p_rng;
3602}
3603
3604void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003605 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 void *p_dbg )
3607{
3608 ssl->f_dbg = f_dbg;
3609 ssl->p_dbg = p_dbg;
3610}
3611
3612void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003613 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003614 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003615{
3616 ssl->f_recv = f_recv;
3617 ssl->f_send = f_send;
3618 ssl->p_recv = p_recv;
3619 ssl->p_send = p_send;
3620}
3621
Paul Bakker0a597072012-09-25 21:55:46 +00003622void ssl_set_session_cache( ssl_context *ssl,
3623 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3624 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003625{
Paul Bakker0a597072012-09-25 21:55:46 +00003626 ssl->f_get_cache = f_get_cache;
3627 ssl->p_get_cache = p_get_cache;
3628 ssl->f_set_cache = f_set_cache;
3629 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003630}
3631
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003632int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003633{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003634 int ret;
3635
3636 if( ssl == NULL ||
3637 session == NULL ||
3638 ssl->session_negotiate == NULL ||
3639 ssl->endpoint != SSL_IS_CLIENT )
3640 {
3641 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3642 }
3643
3644 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3645 return( ret );
3646
Paul Bakker0a597072012-09-25 21:55:46 +00003647 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003648
3649 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003650}
3651
Paul Bakkerb68cad62012-08-23 08:34:18 +00003652void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003653{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003654 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3655 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3656 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3657 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3658}
3659
3660void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3661 int major, int minor )
3662{
3663 if( major != SSL_MAJOR_VERSION_3 )
3664 return;
3665
3666 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3667 return;
3668
3669 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003670}
3671
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003672#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003673/* Add a new (empty) key_cert entry an return a pointer to it */
3674static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3675{
3676 ssl_key_cert *key_cert, *last;
3677
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003678 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3679 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003680 return( NULL );
3681
3682 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3683
3684 /* Append the new key_cert to the (possibly empty) current list */
3685 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003686 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003687 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003688 if( ssl->handshake != NULL )
3689 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003690 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003691 else
3692 {
3693 last = ssl->key_cert;
3694 while( last->next != NULL )
3695 last = last->next;
3696 last->next = key_cert;
3697 }
3698
3699 return key_cert;
3700}
3701
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003702void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003703 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003704{
3705 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003706 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 ssl->peer_cn = peer_cn;
3708}
3709
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003710int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003711 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003712{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003713 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3714
3715 if( key_cert == NULL )
3716 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3717
3718 key_cert->cert = own_cert;
3719 key_cert->key = pk_key;
3720
3721 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003722}
3723
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003724#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003725int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003726 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003727{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003728 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003729 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003730
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003731 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003732 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3733
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003734 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3735 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003736 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003737
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003738 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003739
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003740 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003741 if( ret != 0 )
3742 return( ret );
3743
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003744 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003745 return( ret );
3746
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003747 key_cert->cert = own_cert;
3748 key_cert->key_own_alloc = 1;
3749
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003750 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003751}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003752#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003753
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003754int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003755 void *rsa_key,
3756 rsa_decrypt_func rsa_decrypt,
3757 rsa_sign_func rsa_sign,
3758 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003759{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003760 int ret;
3761 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003762
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003763 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003764 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3765
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003766 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3767 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003768 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003769
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003770 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003771
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003772 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3773 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3774 return( ret );
3775
3776 key_cert->cert = own_cert;
3777 key_cert->key_own_alloc = 1;
3778
3779 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003780}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003781#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003782
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003783#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003784int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3785 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003786{
Paul Bakker6db455e2013-09-18 17:29:31 +02003787 if( psk == NULL || psk_identity == NULL )
3788 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3789
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003790 /*
3791 * The length will be check later anyway, but in case it is obviously
3792 * too large, better abort now. The PMS is as follows:
3793 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3794 */
3795 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3796 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3797
Paul Bakker6db455e2013-09-18 17:29:31 +02003798 if( ssl->psk != NULL )
3799 {
3800 polarssl_free( ssl->psk );
3801 polarssl_free( ssl->psk_identity );
3802 }
3803
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003804 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003805 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003806
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003807 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3808 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003809
3810 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3811 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3812
3813 memcpy( ssl->psk, psk, ssl->psk_len );
3814 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003815
3816 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003817}
3818
3819void ssl_set_psk_cb( ssl_context *ssl,
3820 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3821 size_t),
3822 void *p_psk )
3823{
3824 ssl->f_psk = f_psk;
3825 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003826}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003827#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003828
Paul Bakker48916f92012-09-16 19:57:18 +00003829#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003830int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003831{
3832 int ret;
3833
Paul Bakker48916f92012-09-16 19:57:18 +00003834 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 {
3836 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3837 return( ret );
3838 }
3839
Paul Bakker48916f92012-09-16 19:57:18 +00003840 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003841 {
3842 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3843 return( ret );
3844 }
3845
3846 return( 0 );
3847}
3848
Paul Bakker1b57b062011-01-06 15:48:19 +00003849int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3850{
3851 int ret;
3852
Paul Bakker48916f92012-09-16 19:57:18 +00003853 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003854 {
3855 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3856 return( ret );
3857 }
3858
Paul Bakker48916f92012-09-16 19:57:18 +00003859 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003860 {
3861 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3862 return( ret );
3863 }
3864
3865 return( 0 );
3866}
Paul Bakker48916f92012-09-16 19:57:18 +00003867#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003868
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003869#if defined(POLARSSL_SSL_SET_CURVES)
3870/*
3871 * Set the allowed elliptic curves
3872 */
3873void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3874{
3875 ssl->curve_list = curve_list;
3876}
3877#endif
3878
Paul Bakker0be444a2013-08-27 21:55:01 +02003879#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003880int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003881{
3882 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003883 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003884
3885 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003886
3887 if( ssl->hostname_len + 1 == 0 )
3888 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3889
Paul Bakker6e339b52013-07-03 13:37:05 +02003890 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003891
Paul Bakkerb15b8512012-01-13 13:44:06 +00003892 if( ssl->hostname == NULL )
3893 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3894
Paul Bakker3c2122f2013-06-24 19:03:14 +02003895 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003896 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003897
Paul Bakker40ea7de2009-05-03 10:18:48 +00003898 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003899
3900 return( 0 );
3901}
3902
Paul Bakker5701cdc2012-09-27 21:49:42 +00003903void ssl_set_sni( ssl_context *ssl,
3904 int (*f_sni)(void *, ssl_context *,
3905 const unsigned char *, size_t),
3906 void *p_sni )
3907{
3908 ssl->f_sni = f_sni;
3909 ssl->p_sni = p_sni;
3910}
Paul Bakker0be444a2013-08-27 21:55:01 +02003911#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003912
Paul Bakker490ecc82011-10-06 13:04:09 +00003913void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3914{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003915 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3916 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3917 {
3918 ssl->max_major_ver = major;
3919 ssl->max_minor_ver = minor;
3920 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003921}
3922
Paul Bakker1d29fb52012-09-28 13:28:45 +00003923void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3924{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003925 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3926 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3927 {
3928 ssl->min_major_ver = major;
3929 ssl->min_minor_ver = minor;
3930 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003931}
3932
Paul Bakker05decb22013-08-15 13:33:48 +02003933#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003934int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3935{
Paul Bakker77e257e2013-12-16 15:29:52 +01003936 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003937 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003938 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003939 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003940 }
3941
3942 ssl->mfl_code = mfl_code;
3943
3944 return( 0 );
3945}
Paul Bakker05decb22013-08-15 13:33:48 +02003946#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003947
Paul Bakker1f2bc622013-08-15 13:45:55 +02003948#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003949int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003950{
3951 if( ssl->endpoint != SSL_IS_CLIENT )
3952 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3953
Paul Bakker8c1ede62013-07-19 14:14:37 +02003954 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003955
3956 return( 0 );
3957}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003958#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003959
Paul Bakker48916f92012-09-16 19:57:18 +00003960void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3961{
3962 ssl->disable_renegotiation = renegotiation;
3963}
3964
3965void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3966{
3967 ssl->allow_legacy_renegotiation = allow_legacy;
3968}
3969
Paul Bakkera503a632013-08-14 13:48:06 +02003970#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003971int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3972{
3973 ssl->session_tickets = use_tickets;
3974
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003975 if( ssl->endpoint == SSL_IS_CLIENT )
3976 return( 0 );
3977
3978 if( ssl->f_rng == NULL )
3979 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3980
3981 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003982}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003983
3984void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3985{
3986 ssl->ticket_lifetime = lifetime;
3987}
Paul Bakkera503a632013-08-14 13:48:06 +02003988#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003989
Paul Bakker5121ce52009-01-03 21:22:43 +00003990/*
3991 * SSL get accessors
3992 */
Paul Bakker23986e52011-04-24 08:57:21 +00003993size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003994{
3995 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3996}
3997
Paul Bakkerff60ee62010-03-16 21:09:09 +00003998int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003999{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004000 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004001}
4002
Paul Bakkere3166ce2011-01-27 17:40:50 +00004003const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004004{
Paul Bakker926c8e42013-03-06 10:23:34 +01004005 if( ssl == NULL || ssl->session == NULL )
4006 return NULL;
4007
Paul Bakkere3166ce2011-01-27 17:40:50 +00004008 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004009}
4010
Paul Bakker43ca69c2011-01-15 17:35:19 +00004011const char *ssl_get_version( const ssl_context *ssl )
4012{
4013 switch( ssl->minor_ver )
4014 {
4015 case SSL_MINOR_VERSION_0:
4016 return( "SSLv3.0" );
4017
4018 case SSL_MINOR_VERSION_1:
4019 return( "TLSv1.0" );
4020
4021 case SSL_MINOR_VERSION_2:
4022 return( "TLSv1.1" );
4023
Paul Bakker1ef83d62012-04-11 12:09:53 +00004024 case SSL_MINOR_VERSION_3:
4025 return( "TLSv1.2" );
4026
Paul Bakker43ca69c2011-01-15 17:35:19 +00004027 default:
4028 break;
4029 }
4030 return( "unknown" );
4031}
4032
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004033#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004034const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004035{
4036 if( ssl == NULL || ssl->session == NULL )
4037 return NULL;
4038
4039 return ssl->session->peer_cert;
4040}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004041#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004042
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004043int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4044{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004045 if( ssl == NULL ||
4046 dst == NULL ||
4047 ssl->session == NULL ||
4048 ssl->endpoint != SSL_IS_CLIENT )
4049 {
4050 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4051 }
4052
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004053 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004054}
4055
Paul Bakker5121ce52009-01-03 21:22:43 +00004056/*
Paul Bakker1961b702013-01-25 14:49:24 +01004057 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004058 */
Paul Bakker1961b702013-01-25 14:49:24 +01004059int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004060{
Paul Bakker40e46942009-01-03 21:51:57 +00004061 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004062
Paul Bakker40e46942009-01-03 21:51:57 +00004063#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004064 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004065 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004066#endif
4067
Paul Bakker40e46942009-01-03 21:51:57 +00004068#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004069 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004070 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004071#endif
4072
Paul Bakker1961b702013-01-25 14:49:24 +01004073 return( ret );
4074}
4075
4076/*
4077 * Perform the SSL handshake
4078 */
4079int ssl_handshake( ssl_context *ssl )
4080{
4081 int ret = 0;
4082
4083 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4084
4085 while( ssl->state != SSL_HANDSHAKE_OVER )
4086 {
4087 ret = ssl_handshake_step( ssl );
4088
4089 if( ret != 0 )
4090 break;
4091 }
4092
Paul Bakker5121ce52009-01-03 21:22:43 +00004093 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4094
4095 return( ret );
4096}
4097
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004098#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004099/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004100 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004101 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004102static int ssl_write_hello_request( ssl_context *ssl )
4103{
4104 int ret;
4105
4106 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4107
4108 ssl->out_msglen = 4;
4109 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4110 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4111
4112 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4113 {
4114 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4115 return( ret );
4116 }
4117
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004118 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4119
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004120 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4121
4122 return( 0 );
4123}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004124#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004125
4126/*
4127 * Actually renegotiate current connection, triggered by either:
4128 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004129 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004130 * - receiving any handshake message on server during ssl_read() after the
4131 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004132 * If the handshake doesn't complete due to waiting for I/O, it will continue
4133 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004134 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004135static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004136{
4137 int ret;
4138
4139 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4140
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004141 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4142 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004143
4144 ssl->state = SSL_HELLO_REQUEST;
4145 ssl->renegotiation = SSL_RENEGOTIATION;
4146
Paul Bakker48916f92012-09-16 19:57:18 +00004147 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4148 {
4149 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4150 return( ret );
4151 }
4152
4153 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4154
4155 return( 0 );
4156}
4157
4158/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004159 * Renegotiate current connection on client,
4160 * or request renegotiation on server
4161 */
4162int ssl_renegotiate( ssl_context *ssl )
4163{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004164 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004165
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004166#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004167 /* On server, just send the request */
4168 if( ssl->endpoint == SSL_IS_SERVER )
4169 {
4170 if( ssl->state != SSL_HANDSHAKE_OVER )
4171 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4172
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004173 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004174 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004175#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004176
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004177#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004178 /*
4179 * On client, either start the renegotiation process or,
4180 * if already in progress, continue the handshake
4181 */
4182 if( ssl->renegotiation != SSL_RENEGOTIATION )
4183 {
4184 if( ssl->state != SSL_HANDSHAKE_OVER )
4185 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4186
4187 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4188 {
4189 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4190 return( ret );
4191 }
4192 }
4193 else
4194 {
4195 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4196 {
4197 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4198 return( ret );
4199 }
4200 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004201#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004202
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004203 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004204}
4205
4206/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004207 * Receive application data decrypted from the SSL layer
4208 */
Paul Bakker23986e52011-04-24 08:57:21 +00004209int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004210{
Paul Bakker23986e52011-04-24 08:57:21 +00004211 int ret;
4212 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004213
4214 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4215
4216 if( ssl->state != SSL_HANDSHAKE_OVER )
4217 {
4218 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4219 {
4220 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4221 return( ret );
4222 }
4223 }
4224
4225 if( ssl->in_offt == NULL )
4226 {
4227 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4228 {
Paul Bakker831a7552011-05-18 13:32:51 +00004229 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4230 return( 0 );
4231
Paul Bakker5121ce52009-01-03 21:22:43 +00004232 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4233 return( ret );
4234 }
4235
4236 if( ssl->in_msglen == 0 &&
4237 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4238 {
4239 /*
4240 * OpenSSL sends empty messages to randomize the IV
4241 */
4242 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4243 {
Paul Bakker831a7552011-05-18 13:32:51 +00004244 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4245 return( 0 );
4246
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4248 return( ret );
4249 }
4250 }
4251
Paul Bakker48916f92012-09-16 19:57:18 +00004252 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4253 {
4254 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4255
4256 if( ssl->endpoint == SSL_IS_CLIENT &&
4257 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4258 ssl->in_hslen != 4 ) )
4259 {
4260 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4261 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4262 }
4263
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004264 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4265 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4266 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004267 {
4268 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4269
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004270#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004271 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004272 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004273 /*
4274 * SSLv3 does not have a "no_renegotiation" alert
4275 */
4276 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4277 return( ret );
4278 }
4279 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004280#endif
4281#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4282 defined(POLARSSL_SSL_PROTO_TLS1_2)
4283 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004284 {
4285 if( ( ret = ssl_send_alert_message( ssl,
4286 SSL_ALERT_LEVEL_WARNING,
4287 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4288 {
4289 return( ret );
4290 }
Paul Bakker48916f92012-09-16 19:57:18 +00004291 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004292 else
4293#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004294 {
4295 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004296 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004297 }
Paul Bakker48916f92012-09-16 19:57:18 +00004298 }
4299 else
4300 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004301 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004302 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004303 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004304 return( ret );
4305 }
4306
4307 return( POLARSSL_ERR_NET_WANT_READ );
4308 }
4309 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004310 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4311 {
4312 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4313 "but not honored by client" ) );
4314 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4315 }
Paul Bakker48916f92012-09-16 19:57:18 +00004316 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004317 {
4318 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004319 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004320 }
4321
4322 ssl->in_offt = ssl->in_msg;
4323 }
4324
4325 n = ( len < ssl->in_msglen )
4326 ? len : ssl->in_msglen;
4327
4328 memcpy( buf, ssl->in_offt, n );
4329 ssl->in_msglen -= n;
4330
4331 if( ssl->in_msglen == 0 )
4332 /* all bytes consumed */
4333 ssl->in_offt = NULL;
4334 else
4335 /* more data available */
4336 ssl->in_offt += n;
4337
4338 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4339
Paul Bakker23986e52011-04-24 08:57:21 +00004340 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004341}
4342
4343/*
4344 * Send application data to be encrypted by the SSL layer
4345 */
Paul Bakker23986e52011-04-24 08:57:21 +00004346int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004347{
Paul Bakker23986e52011-04-24 08:57:21 +00004348 int ret;
4349 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004350 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004351
4352 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4353
4354 if( ssl->state != SSL_HANDSHAKE_OVER )
4355 {
4356 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4357 {
4358 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4359 return( ret );
4360 }
4361 }
4362
Paul Bakker05decb22013-08-15 13:33:48 +02004363#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004364 /*
4365 * Assume mfl_code is correct since it was checked when set
4366 */
4367 max_len = mfl_code_to_length[ssl->mfl_code];
4368
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004369 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004370 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004371 */
4372 if( ssl->session_out != NULL &&
4373 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4374 {
4375 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4376 }
Paul Bakker05decb22013-08-15 13:33:48 +02004377#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004378
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004379 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004380
Paul Bakker5121ce52009-01-03 21:22:43 +00004381 if( ssl->out_left != 0 )
4382 {
4383 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4384 {
4385 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4386 return( ret );
4387 }
4388 }
Paul Bakker887bd502011-06-08 13:10:54 +00004389 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004390 {
Paul Bakker887bd502011-06-08 13:10:54 +00004391 ssl->out_msglen = n;
4392 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4393 memcpy( ssl->out_msg, buf, n );
4394
4395 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4396 {
4397 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4398 return( ret );
4399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004400 }
4401
4402 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4403
Paul Bakker23986e52011-04-24 08:57:21 +00004404 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004405}
4406
4407/*
4408 * Notify the peer that the connection is being closed
4409 */
4410int ssl_close_notify( ssl_context *ssl )
4411{
4412 int ret;
4413
4414 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4415
4416 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4417 {
4418 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4419 return( ret );
4420 }
4421
4422 if( ssl->state == SSL_HANDSHAKE_OVER )
4423 {
Paul Bakker48916f92012-09-16 19:57:18 +00004424 if( ( ret = ssl_send_alert_message( ssl,
4425 SSL_ALERT_LEVEL_WARNING,
4426 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004427 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004428 return( ret );
4429 }
4430 }
4431
4432 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4433
4434 return( ret );
4435}
4436
Paul Bakker48916f92012-09-16 19:57:18 +00004437void ssl_transform_free( ssl_transform *transform )
4438{
4439#if defined(POLARSSL_ZLIB_SUPPORT)
4440 deflateEnd( &transform->ctx_deflate );
4441 inflateEnd( &transform->ctx_inflate );
4442#endif
4443
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004444 cipher_free_ctx( &transform->cipher_ctx_enc );
4445 cipher_free_ctx( &transform->cipher_ctx_dec );
4446
Paul Bakker61d113b2013-07-04 11:51:43 +02004447 md_free_ctx( &transform->md_ctx_enc );
4448 md_free_ctx( &transform->md_ctx_dec );
4449
Paul Bakker48916f92012-09-16 19:57:18 +00004450 memset( transform, 0, sizeof( ssl_transform ) );
4451}
4452
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004453#if defined(POLARSSL_X509_CRT_PARSE_C)
4454static void ssl_key_cert_free( ssl_key_cert *key_cert )
4455{
4456 ssl_key_cert *cur = key_cert, *next;
4457
4458 while( cur != NULL )
4459 {
4460 next = cur->next;
4461
4462 if( cur->key_own_alloc )
4463 {
4464 pk_free( cur->key );
4465 polarssl_free( cur->key );
4466 }
4467 polarssl_free( cur );
4468
4469 cur = next;
4470 }
4471}
4472#endif /* POLARSSL_X509_CRT_PARSE_C */
4473
Paul Bakker48916f92012-09-16 19:57:18 +00004474void ssl_handshake_free( ssl_handshake_params *handshake )
4475{
4476#if defined(POLARSSL_DHM_C)
4477 dhm_free( &handshake->dhm_ctx );
4478#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004479#if defined(POLARSSL_ECDH_C)
4480 ecdh_free( &handshake->ecdh_ctx );
4481#endif
4482
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004483#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004484 /* explicit void pointer cast for buggy MS compiler */
4485 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004486#endif
4487
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004488#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4489 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4490 /*
4491 * Free only the linked list wrapper, not the keys themselves
4492 * since the belong to the SNI callback
4493 */
4494 if( handshake->sni_key_cert != NULL )
4495 {
4496 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4497
4498 while( cur != NULL )
4499 {
4500 next = cur->next;
4501 polarssl_free( cur );
4502 cur = next;
4503 }
4504 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004505#endif
4506
Paul Bakker48916f92012-09-16 19:57:18 +00004507 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4508}
4509
4510void ssl_session_free( ssl_session *session )
4511{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004512#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004513 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004514 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004515 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004516 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004517 }
Paul Bakkered27a042013-04-18 22:46:23 +02004518#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004519
Paul Bakkera503a632013-08-14 13:48:06 +02004520#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004521 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004522#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004523
Paul Bakker0a597072012-09-25 21:55:46 +00004524 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004525}
4526
Paul Bakker5121ce52009-01-03 21:22:43 +00004527/*
4528 * Free an SSL context
4529 */
4530void ssl_free( ssl_context *ssl )
4531{
4532 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4533
Paul Bakker5121ce52009-01-03 21:22:43 +00004534 if( ssl->out_ctr != NULL )
4535 {
4536 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004537 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004538 }
4539
4540 if( ssl->in_ctr != NULL )
4541 {
4542 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004543 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004544 }
4545
Paul Bakker16770332013-10-11 09:59:44 +02004546#if defined(POLARSSL_ZLIB_SUPPORT)
4547 if( ssl->compress_buf != NULL )
4548 {
4549 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4550 polarssl_free( ssl->compress_buf );
4551 }
4552#endif
4553
Paul Bakker40e46942009-01-03 21:51:57 +00004554#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004555 mpi_free( &ssl->dhm_P );
4556 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004557#endif
4558
Paul Bakker48916f92012-09-16 19:57:18 +00004559 if( ssl->transform )
4560 {
4561 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004562 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004563 }
4564
4565 if( ssl->handshake )
4566 {
4567 ssl_handshake_free( ssl->handshake );
4568 ssl_transform_free( ssl->transform_negotiate );
4569 ssl_session_free( ssl->session_negotiate );
4570
Paul Bakker6e339b52013-07-03 13:37:05 +02004571 polarssl_free( ssl->handshake );
4572 polarssl_free( ssl->transform_negotiate );
4573 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004574 }
4575
Paul Bakkerc0463502013-02-14 11:19:38 +01004576 if( ssl->session )
4577 {
4578 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004579 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004580 }
4581
Paul Bakkera503a632013-08-14 13:48:06 +02004582#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004583 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004584#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004585
Paul Bakker0be444a2013-08-27 21:55:01 +02004586#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004587 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004588 {
4589 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004590 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004591 ssl->hostname_len = 0;
4592 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004593#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004594
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004595#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004596 if( ssl->psk != NULL )
4597 {
4598 memset( ssl->psk, 0, ssl->psk_len );
4599 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4600 polarssl_free( ssl->psk );
4601 polarssl_free( ssl->psk_identity );
4602 ssl->psk_len = 0;
4603 ssl->psk_identity_len = 0;
4604 }
4605#endif
4606
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004607#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004608 ssl_key_cert_free( ssl->key_cert );
4609#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004610
Paul Bakker05ef8352012-05-08 09:17:57 +00004611#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4612 if( ssl_hw_record_finish != NULL )
4613 {
4614 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4615 ssl_hw_record_finish( ssl );
4616 }
4617#endif
4618
Paul Bakker5121ce52009-01-03 21:22:43 +00004619 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004620
Paul Bakker86f04f42013-02-14 11:20:09 +01004621 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004622 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004623}
4624
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004625#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004626/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004627 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004628 */
4629unsigned char ssl_sig_from_pk( pk_context *pk )
4630{
4631#if defined(POLARSSL_RSA_C)
4632 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4633 return( SSL_SIG_RSA );
4634#endif
4635#if defined(POLARSSL_ECDSA_C)
4636 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4637 return( SSL_SIG_ECDSA );
4638#endif
4639 return( SSL_SIG_ANON );
4640}
4641
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004642pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4643{
4644 switch( sig )
4645 {
4646#if defined(POLARSSL_RSA_C)
4647 case SSL_SIG_RSA:
4648 return( POLARSSL_PK_RSA );
4649#endif
4650#if defined(POLARSSL_ECDSA_C)
4651 case SSL_SIG_ECDSA:
4652 return( POLARSSL_PK_ECDSA );
4653#endif
4654 default:
4655 return( POLARSSL_PK_NONE );
4656 }
4657}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004658#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004659
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004660/*
4661 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4662 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004663md_type_t ssl_md_alg_from_hash( unsigned char hash )
4664{
4665 switch( hash )
4666 {
4667#if defined(POLARSSL_MD5_C)
4668 case SSL_HASH_MD5:
4669 return( POLARSSL_MD_MD5 );
4670#endif
4671#if defined(POLARSSL_SHA1_C)
4672 case SSL_HASH_SHA1:
4673 return( POLARSSL_MD_SHA1 );
4674#endif
4675#if defined(POLARSSL_SHA256_C)
4676 case SSL_HASH_SHA224:
4677 return( POLARSSL_MD_SHA224 );
4678 case SSL_HASH_SHA256:
4679 return( POLARSSL_MD_SHA256 );
4680#endif
4681#if defined(POLARSSL_SHA512_C)
4682 case SSL_HASH_SHA384:
4683 return( POLARSSL_MD_SHA384 );
4684 case SSL_HASH_SHA512:
4685 return( POLARSSL_MD_SHA512 );
4686#endif
4687 default:
4688 return( POLARSSL_MD_NONE );
4689 }
4690}
4691
Paul Bakker5121ce52009-01-03 21:22:43 +00004692#endif
Gergely Budai987bfb52014-01-19 21:48:42 +01004693
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004694#if defined(POLARSSL_SSL_SET_CURVES)
4695/*
4696 * Check is a curve proposed by the peer is in our list.
4697 * Return 1 if we're willing to use it, 0 otherwise.
4698 */
4699int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4700{
4701 const ecp_group_id *gid;
4702
4703 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4704 if( *gid == grp_id )
4705 return( 1 );
4706
4707 return( 0 );
4708}
4709#endif