blob: 7383e1c3b71e35bcb60383f9c0f3b2ac10d48887 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker0be444a2013-08-27 21:55:01 +020039#include "polarssl/debug.h"
40#include "polarssl/ssl.h"
41
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020042#if defined(POLARSSL_X509_CRT_PARSE_C) && \
43 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
44#include "polarssl/oid.h"
45#endif
46
Paul Bakker7dc4c442014-02-01 22:50:26 +010047#if defined(POLARSSL_PLATFORM_C)
48#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020049#else
50#define polarssl_malloc malloc
51#define polarssl_free free
52#endif
53
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Paul Bakker6edcd412013-10-29 15:22:54 +010056#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
57 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000058#define strcasecmp _stricmp
59#endif
60
Paul Bakker34617722014-06-13 17:20:13 +020061/* Implementation that should never be optimized out by the compiler */
62static void polarssl_zeroize( void *v, size_t n ) {
63 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
64}
65
Paul Bakker05decb22013-08-15 13:33:48 +020066#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020067/*
68 * Convert max_fragment_length codes to length.
69 * RFC 6066 says:
70 * enum{
71 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
72 * } MaxFragmentLength;
73 * and we add 0 -> extension unused
74 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020075static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020076{
77 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
78 512, /* SSL_MAX_FRAG_LEN_512 */
79 1024, /* SSL_MAX_FRAG_LEN_1024 */
80 2048, /* SSL_MAX_FRAG_LEN_2048 */
81 4096, /* SSL_MAX_FRAG_LEN_4096 */
82};
Paul Bakker05decb22013-08-15 13:33:48 +020083#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020084
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020085static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
86{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020087 ssl_session_free( dst );
88 memcpy( dst, src, sizeof( ssl_session ) );
89
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020091 if( src->peer_cert != NULL )
92 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020093 int ret;
94
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020095 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
96 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020097 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
98
Paul Bakkerb6b09562013-09-18 14:17:41 +020099 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200101 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
102 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103 {
104 polarssl_free( dst->peer_cert );
105 dst->peer_cert = NULL;
106 return( ret );
107 }
108 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110
Paul Bakkera503a632013-08-14 13:48:06 +0200111#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200112 if( src->ticket != NULL )
113 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200114 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
115 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200116 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
117
118 memcpy( dst->ticket, src->ticket, src->ticket_len );
119 }
Paul Bakkera503a632013-08-14 13:48:06 +0200120#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200121
122 return( 0 );
123}
124
Paul Bakker05ef8352012-05-08 09:17:57 +0000125#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200126int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200127 const unsigned char *key_enc, const unsigned char *key_dec,
128 size_t keylen,
129 const unsigned char *iv_enc, const unsigned char *iv_dec,
130 size_t ivlen,
131 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200132 size_t maclen ) = NULL;
133int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
134int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
135int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
136int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
137int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200138#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140/*
141 * Key material generation
142 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200143#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200144static int ssl3_prf( const unsigned char *secret, size_t slen,
145 const char *label,
146 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000147 unsigned char *dstbuf, size_t dlen )
148{
149 size_t i;
150 md5_context md5;
151 sha1_context sha1;
152 unsigned char padding[16];
153 unsigned char sha1sum[20];
154 ((void)label);
155
Paul Bakker5b4af392014-06-26 12:09:34 +0200156 md5_init( &md5 );
157 sha1_init( &sha1 );
158
Paul Bakker5f70b252012-09-13 14:23:06 +0000159 /*
160 * SSLv3:
161 * block =
162 * MD5( secret + SHA1( 'A' + secret + random ) ) +
163 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
164 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
165 * ...
166 */
167 for( i = 0; i < dlen / 16; i++ )
168 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200169 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000170
171 sha1_starts( &sha1 );
172 sha1_update( &sha1, padding, 1 + i );
173 sha1_update( &sha1, secret, slen );
174 sha1_update( &sha1, random, rlen );
175 sha1_finish( &sha1, sha1sum );
176
177 md5_starts( &md5 );
178 md5_update( &md5, secret, slen );
179 md5_update( &md5, sha1sum, 20 );
180 md5_finish( &md5, dstbuf + i * 16 );
181 }
182
Paul Bakker5b4af392014-06-26 12:09:34 +0200183 md5_free( &md5 );
184 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000185
Paul Bakker34617722014-06-13 17:20:13 +0200186 polarssl_zeroize( padding, sizeof( padding ) );
187 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
189 return( 0 );
190}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200191#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000192
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200193#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200194static int tls1_prf( const unsigned char *secret, size_t slen,
195 const char *label,
196 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000197 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Paul Bakker23986e52011-04-24 08:57:21 +0000199 size_t nb, hs;
200 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200201 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char tmp[128];
203 unsigned char h_i[20];
204
205 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000206 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 hs = ( slen + 1 ) / 2;
209 S1 = secret;
210 S2 = secret + slen - hs;
211
212 nb = strlen( label );
213 memcpy( tmp + 20, label, nb );
214 memcpy( tmp + 20 + nb, random, rlen );
215 nb += rlen;
216
217 /*
218 * First compute P_md5(secret,label+random)[0..dlen]
219 */
220 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
221
222 for( i = 0; i < dlen; i += 16 )
223 {
224 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
225 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
226
227 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
228
229 for( j = 0; j < k; j++ )
230 dstbuf[i + j] = h_i[j];
231 }
232
233 /*
234 * XOR out with P_sha1(secret,label+random)[0..dlen]
235 */
236 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
237
238 for( i = 0; i < dlen; i += 20 )
239 {
240 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
241 sha1_hmac( S2, hs, tmp, 20, tmp );
242
243 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
244
245 for( j = 0; j < k; j++ )
246 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
247 }
248
Paul Bakker34617722014-06-13 17:20:13 +0200249 polarssl_zeroize( tmp, sizeof( tmp ) );
250 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252 return( 0 );
253}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200254#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200256#if defined(POLARSSL_SSL_PROTO_TLS1_2)
257#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200258static int tls_prf_sha256( const unsigned char *secret, size_t slen,
259 const char *label,
260 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000261 unsigned char *dstbuf, size_t dlen )
262{
263 size_t nb;
264 size_t i, j, k;
265 unsigned char tmp[128];
266 unsigned char h_i[32];
267
268 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
270
271 nb = strlen( label );
272 memcpy( tmp + 32, label, nb );
273 memcpy( tmp + 32 + nb, random, rlen );
274 nb += rlen;
275
276 /*
277 * Compute P_<hash>(secret, label + random)[0..dlen]
278 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200279 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 for( i = 0; i < dlen; i += 32 )
282 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200283 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
284 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000285
286 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
287
288 for( j = 0; j < k; j++ )
289 dstbuf[i + j] = h_i[j];
290 }
291
Paul Bakker34617722014-06-13 17:20:13 +0200292 polarssl_zeroize( tmp, sizeof( tmp ) );
293 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000294
295 return( 0 );
296}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200297#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000298
Paul Bakker9e36f042013-06-30 14:34:05 +0200299#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200300static int tls_prf_sha384( const unsigned char *secret, size_t slen,
301 const char *label,
302 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000303 unsigned char *dstbuf, size_t dlen )
304{
305 size_t nb;
306 size_t i, j, k;
307 unsigned char tmp[128];
308 unsigned char h_i[48];
309
310 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
311 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
312
313 nb = strlen( label );
314 memcpy( tmp + 48, label, nb );
315 memcpy( tmp + 48 + nb, random, rlen );
316 nb += rlen;
317
318 /*
319 * Compute P_<hash>(secret, label + random)[0..dlen]
320 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200321 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 for( i = 0; i < dlen; i += 48 )
324 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200325 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
326 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
328 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
329
330 for( j = 0; j < k; j++ )
331 dstbuf[i + j] = h_i[j];
332 }
333
Paul Bakker34617722014-06-13 17:20:13 +0200334 polarssl_zeroize( tmp, sizeof( tmp ) );
335 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
337 return( 0 );
338}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200339#endif /* POLARSSL_SHA512_C */
340#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000341
Paul Bakker66d5d072014-06-17 16:39:18 +0200342static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343
344#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
345 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200346static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
Paul Bakker380da532012-04-18 16:10:25 +0000348
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200349#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200350static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
351static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200355static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
356static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
358
359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
360#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200361static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
362static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
363static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100365
Paul Bakker9e36f042013-06-30 14:34:05 +0200366#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200367static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
368static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
369static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100370#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200371#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000372
Paul Bakker5121ce52009-01-03 21:22:43 +0000373int ssl_derive_keys( ssl_context *ssl )
374{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200375 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char keyblk[256];
378 unsigned char *key1;
379 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100380 unsigned char *mac_enc;
381 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200382 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 const cipher_info_t *cipher_info;
384 const md_info_t *md_info;
385
Paul Bakker48916f92012-09-16 19:57:18 +0000386 ssl_session *session = ssl->session_negotiate;
387 ssl_transform *transform = ssl->transform_negotiate;
388 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
391
Paul Bakker68884e32013-01-07 18:20:04 +0100392 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
393 if( cipher_info == NULL )
394 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200395 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100396 transform->ciphersuite_info->cipher ) );
397 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
398 }
399
400 md_info = md_info_from_type( transform->ciphersuite_info->mac );
401 if( md_info == NULL )
402 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200403 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100404 transform->ciphersuite_info->mac ) );
405 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
406 }
407
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000409 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000410 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200411#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000412 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413 {
Paul Bakker48916f92012-09-16 19:57:18 +0000414 handshake->tls_prf = ssl3_prf;
415 handshake->calc_verify = ssl_calc_verify_ssl;
416 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418 else
419#endif
420#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
421 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000422 {
Paul Bakker48916f92012-09-16 19:57:18 +0000423 handshake->tls_prf = tls1_prf;
424 handshake->calc_verify = ssl_calc_verify_tls;
425 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000426 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200427 else
428#endif
429#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200430#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
432 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000433 {
Paul Bakker48916f92012-09-16 19:57:18 +0000434 handshake->tls_prf = tls_prf_sha384;
435 handshake->calc_verify = ssl_calc_verify_tls_sha384;
436 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000438 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif
440#if defined(POLARSSL_SHA256_C)
441 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000442 {
Paul Bakker48916f92012-09-16 19:57:18 +0000443 handshake->tls_prf = tls_prf_sha256;
444 handshake->calc_verify = ssl_calc_verify_tls_sha256;
445 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 else
448#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200449#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200450 {
451 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200453 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000454
455 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 * SSLv3:
457 * master =
458 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
459 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200461 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200462 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 * master = PRF( premaster, "master secret", randbytes )[0..47]
464 */
Paul Bakker0a597072012-09-25 21:55:46 +0000465 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 {
Paul Bakker48916f92012-09-16 19:57:18 +0000467 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
468 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200470#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
471 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200472 {
473 unsigned char session_hash[48];
474 size_t hash_len;
475
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200476 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200477
478 ssl->handshake->calc_verify( ssl, session_hash );
479
480#if defined(POLARSSL_SSL_PROTO_TLS1_2)
481 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
482 {
483#if defined(POLARSSL_SHA512_C)
484 if( ssl->transform_negotiate->ciphersuite_info->mac ==
485 POLARSSL_MD_SHA384 )
486 {
487 hash_len = 48;
488 }
489 else
490#endif
491 hash_len = 32;
492 }
493 else
494#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
495 hash_len = 36;
496
497 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
498
499 handshake->tls_prf( handshake->premaster, handshake->pmslen,
500 "extended master secret",
501 session_hash, hash_len, session->master, 48 );
502
503 }
504 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200505#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000506 handshake->tls_prf( handshake->premaster, handshake->pmslen,
507 "master secret",
508 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200510
Paul Bakker34617722014-06-13 17:20:13 +0200511 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 }
513 else
514 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
515
516 /*
517 * Swap the client and server random values.
518 */
Paul Bakker48916f92012-09-16 19:57:18 +0000519 memcpy( tmp, handshake->randbytes, 64 );
520 memcpy( handshake->randbytes, tmp + 32, 32 );
521 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200522 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524 /*
525 * SSLv3:
526 * key block =
527 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
528 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
529 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
530 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
531 * ...
532 *
533 * TLSv1:
534 * key block = PRF( master, "key expansion", randbytes )
535 */
Paul Bakker48916f92012-09-16 19:57:18 +0000536 handshake->tls_prf( session->master, 48, "key expansion",
537 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Paul Bakker48916f92012-09-16 19:57:18 +0000539 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
540 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
541 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
542 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
544
Paul Bakker34617722014-06-13 17:20:13 +0200545 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 /*
548 * Determine the appropriate key, IV and MAC length.
549 */
Paul Bakker68884e32013-01-07 18:20:04 +0100550
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200551 transform->keylen = cipher_info->key_length / 8;
552
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200553 if( cipher_info->mode == POLARSSL_MODE_GCM ||
554 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200556 transform->maclen = 0;
557
Paul Bakker68884e32013-01-07 18:20:04 +0100558 transform->ivlen = 12;
559 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200560
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200561 /* Minimum length is expicit IV + tag */
562 transform->minlen = transform->ivlen - transform->fixed_ivlen
563 + ( transform->ciphersuite_info->flags &
564 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100565 }
566 else
567 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200568 int ret;
569
570 /* Initialize HMAC contexts */
571 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
572 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100573 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200574 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
575 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100576 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200578 /* Get MAC length */
579 transform->maclen = md_get_size( md_info );
580
581#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
582 /*
583 * If HMAC is to be truncated, we shall keep the leftmost bytes,
584 * (rfc 6066 page 13 or rfc 2104 section 4),
585 * so we only need to adjust the length here.
586 */
587 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
588 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
589#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
590
591 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100592 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200594 /* Minimum length */
595 if( cipher_info->mode == POLARSSL_MODE_STREAM )
596 transform->minlen = transform->maclen;
597 else
Paul Bakker68884e32013-01-07 18:20:04 +0100598 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200599 /*
600 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100601 * 1. if EtM is in use: one block plus MAC
602 * otherwise: * first multiple of blocklen greater than maclen
603 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200604 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100605#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
606 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
607 {
608 transform->minlen = transform->maclen
609 + cipher_info->block_size;
610 }
611 else
612#endif
613 {
614 transform->minlen = transform->maclen
615 + cipher_info->block_size
616 - transform->maclen % cipher_info->block_size;
617 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200618
619#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
620 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
621 ssl->minor_ver == SSL_MINOR_VERSION_1 )
622 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100623 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200624#endif
625#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
626 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
627 ssl->minor_ver == SSL_MINOR_VERSION_3 )
628 {
629 transform->minlen += transform->ivlen;
630 }
631 else
632#endif
633 {
634 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
635 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
636 }
Paul Bakker68884e32013-01-07 18:20:04 +0100637 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 }
639
640 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000641 transform->keylen, transform->minlen, transform->ivlen,
642 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644 /*
645 * Finally setup the cipher contexts, IVs and MAC secrets.
646 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100647#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 if( ssl->endpoint == SSL_IS_CLIENT )
649 {
Paul Bakker48916f92012-09-16 19:57:18 +0000650 key1 = keyblk + transform->maclen * 2;
651 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Paul Bakker68884e32013-01-07 18:20:04 +0100653 mac_enc = keyblk;
654 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000656 /*
657 * This is not used in TLS v1.1.
658 */
Paul Bakker48916f92012-09-16 19:57:18 +0000659 iv_copy_len = ( transform->fixed_ivlen ) ?
660 transform->fixed_ivlen : transform->ivlen;
661 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
662 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000663 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 }
665 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100666#endif /* POLARSSL_SSL_CLI_C */
667#if defined(POLARSSL_SSL_SRV_C)
668 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 {
Paul Bakker48916f92012-09-16 19:57:18 +0000670 key1 = keyblk + transform->maclen * 2 + transform->keylen;
671 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
Paul Bakker68884e32013-01-07 18:20:04 +0100673 mac_enc = keyblk + transform->maclen;
674 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000676 /*
677 * This is not used in TLS v1.1.
678 */
Paul Bakker48916f92012-09-16 19:57:18 +0000679 iv_copy_len = ( transform->fixed_ivlen ) ?
680 transform->fixed_ivlen : transform->ivlen;
681 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
682 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000683 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100685 else
686#endif /* POLARSSL_SSL_SRV_C */
687 {
688 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
689 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
690 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200692#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100693 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
694 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100695 if( transform->maclen > sizeof transform->mac_enc )
696 {
697 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200698 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100699 }
700
Paul Bakker68884e32013-01-07 18:20:04 +0100701 memcpy( transform->mac_enc, mac_enc, transform->maclen );
702 memcpy( transform->mac_dec, mac_dec, transform->maclen );
703 }
704 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200705#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200706#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
707 defined(POLARSSL_SSL_PROTO_TLS1_2)
708 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100709 {
710 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
711 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
712 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200713 else
714#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200715 {
716 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200717 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200718 }
Paul Bakker68884e32013-01-07 18:20:04 +0100719
Paul Bakker05ef8352012-05-08 09:17:57 +0000720#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200721 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000722 {
723 int ret = 0;
724
725 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
726
Paul Bakker07eb38b2012-12-19 14:42:06 +0100727 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
728 transform->iv_enc, transform->iv_dec,
729 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100730 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100731 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000732 {
733 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200734 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000735 }
736 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200737#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000738
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200739 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
740 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000741 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200742 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
743 return( ret );
744 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200745
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200746 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
747 cipher_info ) ) != 0 )
748 {
749 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
750 return( ret );
751 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200752
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200753 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
754 cipher_info->key_length,
755 POLARSSL_ENCRYPT ) ) != 0 )
756 {
757 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
758 return( ret );
759 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200760
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200761 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
762 cipher_info->key_length,
763 POLARSSL_DECRYPT ) ) != 0 )
764 {
765 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
766 return( ret );
767 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200768
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200769#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200770 if( cipher_info->mode == POLARSSL_MODE_CBC )
771 {
772 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
773 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200774 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200775 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
776 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200777 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200778
779 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
780 POLARSSL_PADDING_NONE ) ) != 0 )
781 {
782 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
783 return( ret );
784 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000785 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200786#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000787
Paul Bakker34617722014-06-13 17:20:13 +0200788 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000789
Paul Bakker2770fbd2012-07-03 13:30:23 +0000790#if defined(POLARSSL_ZLIB_SUPPORT)
791 // Initialize compression
792 //
Paul Bakker48916f92012-09-16 19:57:18 +0000793 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000794 {
Paul Bakker16770332013-10-11 09:59:44 +0200795 if( ssl->compress_buf == NULL )
796 {
797 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
798 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
799 if( ssl->compress_buf == NULL )
800 {
801 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
802 SSL_BUFFER_LEN ) );
803 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
804 }
805 }
806
Paul Bakker2770fbd2012-07-03 13:30:23 +0000807 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
808
Paul Bakker48916f92012-09-16 19:57:18 +0000809 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
810 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000811
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200812 if( deflateInit( &transform->ctx_deflate,
813 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000814 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000815 {
816 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
817 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
818 }
819 }
820#endif /* POLARSSL_ZLIB_SUPPORT */
821
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
823
824 return( 0 );
825}
826
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200827#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000828void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000829{
830 md5_context md5;
831 sha1_context sha1;
832 unsigned char pad_1[48];
833 unsigned char pad_2[48];
834
Paul Bakker380da532012-04-18 16:10:25 +0000835 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000836
Paul Bakker48916f92012-09-16 19:57:18 +0000837 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
838 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Paul Bakker380da532012-04-18 16:10:25 +0000840 memset( pad_1, 0x36, 48 );
841 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000842
Paul Bakker48916f92012-09-16 19:57:18 +0000843 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000844 md5_update( &md5, pad_1, 48 );
845 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Paul Bakker380da532012-04-18 16:10:25 +0000847 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000848 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000849 md5_update( &md5, pad_2, 48 );
850 md5_update( &md5, hash, 16 );
851 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000852
Paul Bakker48916f92012-09-16 19:57:18 +0000853 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000854 sha1_update( &sha1, pad_1, 40 );
855 sha1_finish( &sha1, hash + 16 );
856
857 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000858 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000859 sha1_update( &sha1, pad_2, 40 );
860 sha1_update( &sha1, hash + 16, 20 );
861 sha1_finish( &sha1, hash + 16 );
862
863 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
864 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
865
Paul Bakker5b4af392014-06-26 12:09:34 +0200866 md5_free( &md5 );
867 sha1_free( &sha1 );
868
Paul Bakker380da532012-04-18 16:10:25 +0000869 return;
870}
Paul Bakker9af723c2014-05-01 13:03:14 +0200871#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000872
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200873#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000874void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
875{
876 md5_context md5;
877 sha1_context sha1;
878
879 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
880
Paul Bakker48916f92012-09-16 19:57:18 +0000881 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
882 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000883
Paul Bakker48916f92012-09-16 19:57:18 +0000884 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000885 sha1_finish( &sha1, hash + 16 );
886
887 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
888 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
889
Paul Bakker5b4af392014-06-26 12:09:34 +0200890 md5_free( &md5 );
891 sha1_free( &sha1 );
892
Paul Bakker380da532012-04-18 16:10:25 +0000893 return;
894}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200895#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000896
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200897#if defined(POLARSSL_SSL_PROTO_TLS1_2)
898#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000899void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
900{
Paul Bakker9e36f042013-06-30 14:34:05 +0200901 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000902
903 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
904
Paul Bakker9e36f042013-06-30 14:34:05 +0200905 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
906 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000907
908 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
909 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
910
Paul Bakker5b4af392014-06-26 12:09:34 +0200911 sha256_free( &sha256 );
912
Paul Bakker380da532012-04-18 16:10:25 +0000913 return;
914}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200915#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000916
Paul Bakker9e36f042013-06-30 14:34:05 +0200917#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000918void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
919{
Paul Bakker9e36f042013-06-30 14:34:05 +0200920 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000921
922 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
923
Paul Bakker9e36f042013-06-30 14:34:05 +0200924 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
925 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Paul Bakkerca4ab492012-04-18 14:23:57 +0000927 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
929
Paul Bakker5b4af392014-06-26 12:09:34 +0200930 sha512_free( &sha512 );
931
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 return;
933}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200934#endif /* POLARSSL_SHA512_C */
935#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200937#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200938int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
939{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200940 unsigned char *p = ssl->handshake->premaster;
941 unsigned char *end = p + sizeof( ssl->handshake->premaster );
942
943 /*
944 * PMS = struct {
945 * opaque other_secret<0..2^16-1>;
946 * opaque psk<0..2^16-1>;
947 * };
948 * with "other_secret" depending on the particular key exchange
949 */
950#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
951 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
952 {
953 if( end - p < 2 + (int) ssl->psk_len )
954 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
955
956 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
957 *(p++) = (unsigned char)( ssl->psk_len );
958 p += ssl->psk_len;
959 }
960 else
961#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200962#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
963 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
964 {
965 /*
966 * other_secret already set by the ClientKeyExchange message,
967 * and is 48 bytes long
968 */
969 *p++ = 0;
970 *p++ = 48;
971 p += 48;
972 }
973 else
974#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200975#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
976 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
977 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200978 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200979 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200980
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200981 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200982 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200983 p + 2, &len,
984 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200985 {
986 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
987 return( ret );
988 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200989 *(p++) = (unsigned char)( len >> 8 );
990 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200991 p += len;
992
993 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
994 }
995 else
996#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
997#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
998 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
999 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001000 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001001 size_t zlen;
1002
1003 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001004 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001005 ssl->f_rng, ssl->p_rng ) ) != 0 )
1006 {
1007 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1008 return( ret );
1009 }
1010
1011 *(p++) = (unsigned char)( zlen >> 8 );
1012 *(p++) = (unsigned char)( zlen );
1013 p += zlen;
1014
1015 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1016 }
1017 else
1018#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1019 {
1020 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001021 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001022 }
1023
1024 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001025 if( end - p < 2 + (int) ssl->psk_len )
1026 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1027
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001028 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1029 *(p++) = (unsigned char)( ssl->psk_len );
1030 memcpy( p, ssl->psk, ssl->psk_len );
1031 p += ssl->psk_len;
1032
1033 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1034
1035 return( 0 );
1036}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001037#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001038
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001039#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001040/*
1041 * SSLv3.0 MAC functions
1042 */
Paul Bakker68884e32013-01-07 18:20:04 +01001043static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1044 unsigned char *buf, size_t len,
1045 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001046{
1047 unsigned char header[11];
1048 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001049 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001050 int md_size = md_get_size( md_ctx->md_info );
1051 int md_type = md_get_type( md_ctx->md_info );
1052
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001053 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001054 if( md_type == POLARSSL_MD_MD5 )
1055 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001056 else
Paul Bakker68884e32013-01-07 18:20:04 +01001057 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
1059 memcpy( header, ctr, 8 );
1060 header[ 8] = (unsigned char) type;
1061 header[ 9] = (unsigned char)( len >> 8 );
1062 header[10] = (unsigned char)( len );
1063
Paul Bakker68884e32013-01-07 18:20:04 +01001064 memset( padding, 0x36, padlen );
1065 md_starts( md_ctx );
1066 md_update( md_ctx, secret, md_size );
1067 md_update( md_ctx, padding, padlen );
1068 md_update( md_ctx, header, 11 );
1069 md_update( md_ctx, buf, len );
1070 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
Paul Bakker68884e32013-01-07 18:20:04 +01001072 memset( padding, 0x5C, padlen );
1073 md_starts( md_ctx );
1074 md_update( md_ctx, secret, md_size );
1075 md_update( md_ctx, padding, padlen );
1076 md_update( md_ctx, buf + len, md_size );
1077 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001078}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001079#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001080
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001081#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1082 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1083 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1084#define POLARSSL_SOME_MODES_USE_MAC
1085#endif
1086
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001087/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001089 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001090static int ssl_encrypt_buf( ssl_context *ssl )
1091{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001092 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001093 cipher_mode_t mode;
1094 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
1096 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1097
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001098 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1099 {
1100 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1101 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1102 }
1103
1104 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1105
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001106 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1107 ssl->out_msg, ssl->out_msglen );
1108
Paul Bakker5121ce52009-01-03 21:22:43 +00001109 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001110 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001112#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001113 if( mode == POLARSSL_MODE_STREAM ||
1114 ( mode == POLARSSL_MODE_CBC
1115#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1116 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1117#endif
1118 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001120#if defined(POLARSSL_SSL_PROTO_SSL3)
1121 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1122 {
1123 ssl_mac( &ssl->transform_out->md_ctx_enc,
1124 ssl->transform_out->mac_enc,
1125 ssl->out_msg, ssl->out_msglen,
1126 ssl->out_ctr, ssl->out_msgtype );
1127 }
1128 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001129#endif
1130#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001131 defined(POLARSSL_SSL_PROTO_TLS1_2)
1132 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1133 {
1134 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1135 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1136 ssl->out_msg, ssl->out_msglen );
1137 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1138 ssl->out_msg + ssl->out_msglen );
1139 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1140 }
1141 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001142#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001143 {
1144 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001145 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001146 }
1147
1148 SSL_DEBUG_BUF( 4, "computed mac",
1149 ssl->out_msg + ssl->out_msglen,
1150 ssl->transform_out->maclen );
1151
1152 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001153 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001154 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001155#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001157 /*
1158 * Encrypt
1159 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001160#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001161 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001163 int ret;
1164 size_t olen = 0;
1165
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1167 "including %d bytes of padding",
1168 ssl->out_msglen, 0 ) );
1169
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001170 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001171 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001172 ssl->transform_out->ivlen,
1173 ssl->out_msg, ssl->out_msglen,
1174 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001175 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001176 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001177 return( ret );
1178 }
1179
1180 if( ssl->out_msglen != olen )
1181 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001182 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001183 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 }
Paul Bakker68884e32013-01-07 18:20:04 +01001186 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001187#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001188#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1189 if( mode == POLARSSL_MODE_GCM ||
1190 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001191 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001192 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001193 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001194 unsigned char *enc_msg;
1195 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001196 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1197 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001198
Paul Bakkerca4ab492012-04-18 14:23:57 +00001199 memcpy( add_data, ssl->out_ctr, 8 );
1200 add_data[8] = ssl->out_msgtype;
1201 add_data[9] = ssl->major_ver;
1202 add_data[10] = ssl->minor_ver;
1203 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1204 add_data[12] = ssl->out_msglen & 0xFF;
1205
1206 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1207 add_data, 13 );
1208
Paul Bakker68884e32013-01-07 18:20:04 +01001209 /*
1210 * Generate IV
1211 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001212#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001213 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001214 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1215 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001216 if( ret != 0 )
1217 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001218
Paul Bakker68884e32013-01-07 18:20:04 +01001219 memcpy( ssl->out_iv,
1220 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1221 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001222#else
1223 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1224 {
1225 /* Reminder if we ever add an AEAD mode with a different size */
1226 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1227 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1228 }
1229
1230 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1231 ssl->out_ctr, 8 );
1232 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1233#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001234
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001235 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001236 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001237
Paul Bakker68884e32013-01-07 18:20:04 +01001238 /*
1239 * Fix pointer positions and message length with added IV
1240 */
1241 enc_msg = ssl->out_msg;
1242 enc_msglen = ssl->out_msglen;
1243 ssl->out_msglen += ssl->transform_out->ivlen -
1244 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001245
Paul Bakker68884e32013-01-07 18:20:04 +01001246 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1247 "including %d bytes of padding",
1248 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001249
Paul Bakker68884e32013-01-07 18:20:04 +01001250 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001251 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001252 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001253 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1254 ssl->transform_out->iv_enc,
1255 ssl->transform_out->ivlen,
1256 add_data, 13,
1257 enc_msg, enc_msglen,
1258 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001259 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001260 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001261 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001262 return( ret );
1263 }
1264
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001265 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001266 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001267 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001268 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001269 }
1270
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001271 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001272 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001273
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001274 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001277#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001278#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1279 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001280 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001282 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001283 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001284 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001285
Paul Bakker48916f92012-09-16 19:57:18 +00001286 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1287 ssl->transform_out->ivlen;
1288 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 padlen = 0;
1290
1291 for( i = 0; i <= padlen; i++ )
1292 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1293
1294 ssl->out_msglen += padlen + 1;
1295
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001296 enc_msglen = ssl->out_msglen;
1297 enc_msg = ssl->out_msg;
1298
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001299#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001300 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001301 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1302 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001303 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001304 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305 {
1306 /*
1307 * Generate IV
1308 */
Paul Bakker48916f92012-09-16 19:57:18 +00001309 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1310 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001311 if( ret != 0 )
1312 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001313
Paul Bakker92be97b2013-01-02 17:30:03 +01001314 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001315 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001316
1317 /*
1318 * Fix pointer positions and message length with added IV
1319 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001320 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001321 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001322 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001323 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001324#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001325
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001327 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001328 ssl->out_msglen, ssl->transform_out->ivlen,
1329 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001331 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001332 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001333 ssl->transform_out->ivlen,
1334 enc_msg, enc_msglen,
1335 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001336 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001337 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001338 return( ret );
1339 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001340
Paul Bakkercca5b812013-08-31 17:40:26 +02001341 if( enc_msglen != olen )
1342 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001343 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001344 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001345 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001346
1347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001348 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1349 {
1350 /*
1351 * Save IV in SSL3 and TLS1
1352 */
1353 memcpy( ssl->transform_out->iv_enc,
1354 ssl->transform_out->cipher_ctx_enc.iv,
1355 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001357#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001358
1359#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001360 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001361 {
1362 /*
1363 * MAC(MAC_write_key, seq_num +
1364 * TLSCipherText.type +
1365 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001366 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001367 * IV + // except for TLS 1.0
1368 * ENC(content + padding + padding_length));
1369 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001370 unsigned char pseudo_hdr[13];
1371
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001372 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1373
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001374 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1375 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001376 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1377 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1378
1379 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001380
1381 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1382 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1383 ssl->out_iv, ssl->out_msglen );
1384 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1385 ssl->out_iv + ssl->out_msglen );
1386 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1387
1388 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001389 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001390 }
1391#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001393 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001394#endif /* POLARSSL_CIPHER_MODE_CBC &&
1395 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001396 {
1397 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001398 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001401 /* Make extra sure authentication was performed, exactly once */
1402 if( auth_done != 1 )
1403 {
1404 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1405 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1406 }
1407
Paul Bakkerca4ab492012-04-18 14:23:57 +00001408 for( i = 8; i > 0; i-- )
1409 if( ++ssl->out_ctr[i - 1] != 0 )
1410 break;
1411
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001412 /* The loops goes to its end iff the counter is wrapping */
1413 if( i == 0 )
1414 {
1415 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1416 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1417 }
1418
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1420
1421 return( 0 );
1422}
1423
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001424#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001425
Paul Bakker5121ce52009-01-03 21:22:43 +00001426static int ssl_decrypt_buf( ssl_context *ssl )
1427{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001428 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001429 cipher_mode_t mode;
1430 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001431#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001432 size_t padlen = 0, correct = 1;
1433#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
1435 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1436
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001437 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1438 {
1439 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1440 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1441 }
1442
1443 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1444
Paul Bakker48916f92012-09-16 19:57:18 +00001445 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 {
1447 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001448 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001449 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 }
1451
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001452#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001453 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001454 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001455 int ret;
1456 size_t olen = 0;
1457
Paul Bakker68884e32013-01-07 18:20:04 +01001458 padlen = 0;
1459
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001460 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001461 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001462 ssl->transform_in->ivlen,
1463 ssl->in_msg, ssl->in_msglen,
1464 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001465 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001466 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001467 return( ret );
1468 }
1469
1470 if( ssl->in_msglen != olen )
1471 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001472 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001473 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001474 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 }
Paul Bakker68884e32013-01-07 18:20:04 +01001476 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001477#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001478#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1479 if( mode == POLARSSL_MODE_GCM ||
1480 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001481 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001482 int ret;
1483 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001484 unsigned char *dec_msg;
1485 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001486 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001487 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1488 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001489 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1490 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001491
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001492 if( ssl->in_msglen < explicit_iv_len + taglen )
1493 {
1494 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1495 "+ taglen (%d)", ssl->in_msglen,
1496 explicit_iv_len, taglen ) );
1497 return( POLARSSL_ERR_SSL_INVALID_MAC );
1498 }
1499 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1500
Paul Bakker68884e32013-01-07 18:20:04 +01001501 dec_msg = ssl->in_msg;
1502 dec_msg_result = ssl->in_msg;
1503 ssl->in_msglen = dec_msglen;
1504
1505 memcpy( add_data, ssl->in_ctr, 8 );
1506 add_data[8] = ssl->in_msgtype;
1507 add_data[9] = ssl->major_ver;
1508 add_data[10] = ssl->minor_ver;
1509 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1510 add_data[12] = ssl->in_msglen & 0xFF;
1511
1512 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1513 add_data, 13 );
1514
1515 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1516 ssl->in_iv,
1517 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1518
1519 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1520 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001521 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001522
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001523 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001524 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001525 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001526 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1527 ssl->transform_in->iv_dec,
1528 ssl->transform_in->ivlen,
1529 add_data, 13,
1530 dec_msg, dec_msglen,
1531 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001532 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001533 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001534 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1535
1536 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1537 return( POLARSSL_ERR_SSL_INVALID_MAC );
1538
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001539 return( ret );
1540 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001541 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001542
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001543 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001544 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001545 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001546 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001547 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001549 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001550#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001551#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1552 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001553 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 {
Paul Bakker45829992013-01-03 14:52:21 +01001555 /*
1556 * Decrypt and check the padding
1557 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001558 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001559 unsigned char *dec_msg;
1560 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001561 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001562 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001563 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001564
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 /*
Paul Bakker45829992013-01-03 14:52:21 +01001566 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001568#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001569 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1570 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001571#endif
Paul Bakker45829992013-01-03 14:52:21 +01001572
1573 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1574 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1575 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001576 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1577 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1578 ssl->transform_in->ivlen,
1579 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001580 return( POLARSSL_ERR_SSL_INVALID_MAC );
1581 }
1582
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001583 dec_msglen = ssl->in_msglen;
1584 dec_msg = ssl->in_msg;
1585 dec_msg_result = ssl->in_msg;
1586
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001587 /*
1588 * Authenticate before decrypt if enabled
1589 */
1590#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001591 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001592 {
1593 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001594 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001595
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001596 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1597
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001598 dec_msglen -= ssl->transform_in->maclen;
1599 ssl->in_msglen -= ssl->transform_in->maclen;
1600
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001601 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1602 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1603 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1604 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1605
1606 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1607
1608 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001609 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1610 ssl->in_iv, ssl->in_msglen );
1611 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1612 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1613
1614 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1615 ssl->transform_in->maclen );
1616 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1617 ssl->transform_in->maclen );
1618
1619 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1620 ssl->transform_in->maclen ) != 0 )
1621 {
1622 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1623
1624 return( POLARSSL_ERR_SSL_INVALID_MAC );
1625 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001626 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001627 }
1628#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1629
1630 /*
1631 * Check length sanity
1632 */
1633 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1634 {
1635 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1636 ssl->in_msglen, ssl->transform_in->ivlen ) );
1637 return( POLARSSL_ERR_SSL_INVALID_MAC );
1638 }
1639
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001640#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001641 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001642 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001643 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001644 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001645 {
Paul Bakker48916f92012-09-16 19:57:18 +00001646 dec_msglen -= ssl->transform_in->ivlen;
1647 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648
Paul Bakker48916f92012-09-16 19:57:18 +00001649 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001650 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001652#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001653
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001654 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001655 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001656 ssl->transform_in->ivlen,
1657 dec_msg, dec_msglen,
1658 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001659 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001660 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001661 return( ret );
1662 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001663
Paul Bakkercca5b812013-08-31 17:40:26 +02001664 if( dec_msglen != olen )
1665 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001666 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001667 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001668 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001669
1670#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001671 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1672 {
1673 /*
1674 * Save IV in SSL3 and TLS1
1675 */
1676 memcpy( ssl->transform_in->iv_dec,
1677 ssl->transform_in->cipher_ctx_dec.iv,
1678 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001680#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
1682 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001683
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001684 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001685 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001686 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001687#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001688 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1689 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001690#endif
Paul Bakker45829992013-01-03 14:52:21 +01001691 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001692 correct = 0;
1693 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001695#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001696 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1697 {
Paul Bakker48916f92012-09-16 19:57:18 +00001698 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001700#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1702 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001703 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001704#endif
Paul Bakker45829992013-01-03 14:52:21 +01001705 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 }
1707 }
1708 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001709#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001710#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1711 defined(POLARSSL_SSL_PROTO_TLS1_2)
1712 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001713 {
1714 /*
Paul Bakker45829992013-01-03 14:52:21 +01001715 * TLSv1+: always check the padding up to the first failure
1716 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001717 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001718 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001719 size_t padding_idx = ssl->in_msglen - padlen - 1;
1720
Paul Bakker956c9e02013-12-19 14:42:28 +01001721 /*
1722 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001723 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001724 *
Paul Bakker61885c72014-04-25 12:59:03 +02001725 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1726 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001727 *
1728 * In both cases we reset padding_idx to a safe value (0) to
1729 * prevent out-of-buffer reads.
1730 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001731 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001732 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1733 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001734
1735 padding_idx *= correct;
1736
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001737 for( i = 1; i <= 256; i++ )
1738 {
1739 real_count &= ( i <= padlen );
1740 pad_count += real_count *
1741 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1742 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001743
1744 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001745
Paul Bakkerd66f0702013-01-31 16:57:45 +01001746#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001747 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001748 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001749#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001750 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001751 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001752 else
1753#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1754 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001755 {
1756 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001757 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001758 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001759
1760 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001762 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001763#endif /* POLARSSL_CIPHER_MODE_CBC &&
1764 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001765 {
1766 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001767 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001768 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001769
1770 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1771 ssl->in_msg, ssl->in_msglen );
1772
1773 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001774 * Authenticate if not done yet.
1775 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001777#if defined(POLARSSL_SOME_MODES_USE_MAC)
1778 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001779 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001780 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1781
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001782 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001784 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1785 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001787 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001789#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001790 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1791 {
1792 ssl_mac( &ssl->transform_in->md_ctx_dec,
1793 ssl->transform_in->mac_dec,
1794 ssl->in_msg, ssl->in_msglen,
1795 ssl->in_ctr, ssl->in_msgtype );
1796 }
1797 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001798#endif /* POLARSSL_SSL_PROTO_SSL3 */
1799#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001800 defined(POLARSSL_SSL_PROTO_TLS1_2)
1801 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1802 {
1803 /*
1804 * Process MAC and always update for padlen afterwards to make
1805 * total time independent of padlen
1806 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001807 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001808 *
1809 * Known timing attacks:
1810 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1811 *
1812 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1813 * correctly. (We round down instead of up, so -56 is the correct
1814 * value for our calculations instead of -55)
1815 */
1816 size_t j, extra_run = 0;
1817 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1818 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001819
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001821
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001822 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1823 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1824 ssl->in_msglen );
1825 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1826 ssl->in_msg + ssl->in_msglen );
1827 for( j = 0; j < extra_run; j++ )
1828 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001829
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1831 }
1832 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001833#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001834 POLARSSL_SSL_PROTO_TLS1_2 */
1835 {
1836 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001837 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001840 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1841 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1842 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001844 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001845 ssl->transform_in->maclen ) != 0 )
1846 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001847#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001848 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001849#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001850 correct = 0;
1851 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001852 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 /*
1855 * Finally check the correct flag
1856 */
1857 if( correct == 0 )
1858 return( POLARSSL_ERR_SSL_INVALID_MAC );
1859 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001860#endif /* POLARSSL_SOME_MODES_USE_MAC */
1861
1862 /* Make extra sure authentication was performed, exactly once */
1863 if( auth_done != 1 )
1864 {
1865 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1866 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
1869 if( ssl->in_msglen == 0 )
1870 {
1871 ssl->nb_zero++;
1872
1873 /*
1874 * Three or more empty messages may be a DoS attack
1875 * (excessive CPU consumption).
1876 */
1877 if( ssl->nb_zero > 3 )
1878 {
1879 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1880 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001881 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 }
1883 }
1884 else
1885 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001886
Paul Bakker23986e52011-04-24 08:57:21 +00001887 for( i = 8; i > 0; i-- )
1888 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 break;
1890
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001891 /* The loops goes to its end iff the counter is wrapping */
1892 if( i == 0 )
1893 {
1894 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1895 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1896 }
1897
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1899
1900 return( 0 );
1901}
1902
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001903#undef MAC_NONE
1904#undef MAC_PLAINTEXT
1905#undef MAC_CIPHERTEXT
1906
Paul Bakker2770fbd2012-07-03 13:30:23 +00001907#if defined(POLARSSL_ZLIB_SUPPORT)
1908/*
1909 * Compression/decompression functions
1910 */
1911static int ssl_compress_buf( ssl_context *ssl )
1912{
1913 int ret;
1914 unsigned char *msg_post = ssl->out_msg;
1915 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001916 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001917
1918 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1919
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001920 if( len_pre == 0 )
1921 return( 0 );
1922
Paul Bakker2770fbd2012-07-03 13:30:23 +00001923 memcpy( msg_pre, ssl->out_msg, len_pre );
1924
1925 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1926 ssl->out_msglen ) );
1927
1928 SSL_DEBUG_BUF( 4, "before compression: output payload",
1929 ssl->out_msg, ssl->out_msglen );
1930
Paul Bakker48916f92012-09-16 19:57:18 +00001931 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1932 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1933 ssl->transform_out->ctx_deflate.next_out = msg_post;
1934 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001935
Paul Bakker48916f92012-09-16 19:57:18 +00001936 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001937 if( ret != Z_OK )
1938 {
1939 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1940 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1941 }
1942
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001943 ssl->out_msglen = SSL_BUFFER_LEN -
1944 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001945
Paul Bakker2770fbd2012-07-03 13:30:23 +00001946 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1947 ssl->out_msglen ) );
1948
1949 SSL_DEBUG_BUF( 4, "after compression: output payload",
1950 ssl->out_msg, ssl->out_msglen );
1951
1952 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1953
1954 return( 0 );
1955}
1956
1957static int ssl_decompress_buf( ssl_context *ssl )
1958{
1959 int ret;
1960 unsigned char *msg_post = ssl->in_msg;
1961 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001962 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001963
1964 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1965
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001966 if( len_pre == 0 )
1967 return( 0 );
1968
Paul Bakker2770fbd2012-07-03 13:30:23 +00001969 memcpy( msg_pre, ssl->in_msg, len_pre );
1970
1971 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1972 ssl->in_msglen ) );
1973
1974 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1975 ssl->in_msg, ssl->in_msglen );
1976
Paul Bakker48916f92012-09-16 19:57:18 +00001977 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1978 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1979 ssl->transform_in->ctx_inflate.next_out = msg_post;
1980 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001981
Paul Bakker48916f92012-09-16 19:57:18 +00001982 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001983 if( ret != Z_OK )
1984 {
1985 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1986 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1987 }
1988
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001989 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1990 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001991
Paul Bakker2770fbd2012-07-03 13:30:23 +00001992 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1993 ssl->in_msglen ) );
1994
1995 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1996 ssl->in_msg, ssl->in_msglen );
1997
1998 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1999
2000 return( 0 );
2001}
2002#endif /* POLARSSL_ZLIB_SUPPORT */
2003
Paul Bakker5121ce52009-01-03 21:22:43 +00002004/*
2005 * Fill the input message buffer
2006 */
Paul Bakker23986e52011-04-24 08:57:21 +00002007int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002008{
Paul Bakker23986e52011-04-24 08:57:21 +00002009 int ret;
2010 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002011
2012 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2013
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002014 if( nb_want > SSL_BUFFER_LEN - 8 )
2015 {
2016 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2017 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2018 }
2019
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 while( ssl->in_left < nb_want )
2021 {
2022 len = nb_want - ssl->in_left;
2023 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2024
2025 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2026 ssl->in_left, nb_want ) );
2027 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2028
Paul Bakker831a7552011-05-18 13:32:51 +00002029 if( ret == 0 )
2030 return( POLARSSL_ERR_SSL_CONN_EOF );
2031
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 if( ret < 0 )
2033 return( ret );
2034
2035 ssl->in_left += ret;
2036 }
2037
2038 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2039
2040 return( 0 );
2041}
2042
2043/*
2044 * Flush any data not yet written
2045 */
2046int ssl_flush_output( ssl_context *ssl )
2047{
2048 int ret;
2049 unsigned char *buf;
2050
2051 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2052
2053 while( ssl->out_left > 0 )
2054 {
2055 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2056 5 + ssl->out_msglen, ssl->out_left ) );
2057
Paul Bakker5bd42292012-12-19 14:40:42 +01002058 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002060
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2062
2063 if( ret <= 0 )
2064 return( ret );
2065
2066 ssl->out_left -= ret;
2067 }
2068
2069 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2070
2071 return( 0 );
2072}
2073
2074/*
2075 * Record layer functions
2076 */
2077int ssl_write_record( ssl_context *ssl )
2078{
Paul Bakker05ef8352012-05-08 09:17:57 +00002079 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002080 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
2082 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2083
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2085 {
2086 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2087 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2088 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2089
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002090 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2091 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
Paul Bakker2770fbd2012-07-03 13:30:23 +00002094#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002095 if( ssl->transform_out != NULL &&
2096 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002097 {
2098 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2099 {
2100 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2101 return( ret );
2102 }
2103
2104 len = ssl->out_msglen;
2105 }
2106#endif /*POLARSSL_ZLIB_SUPPORT */
2107
Paul Bakker05ef8352012-05-08 09:17:57 +00002108#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002109 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002111 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002112
Paul Bakker05ef8352012-05-08 09:17:57 +00002113 ret = ssl_hw_record_write( ssl );
2114 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2115 {
2116 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002117 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002118 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002119
2120 if( ret == 0 )
2121 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002122 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002123#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002124 if( !done )
2125 {
2126 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2127 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2128 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2130 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002131
Paul Bakker48916f92012-09-16 19:57:18 +00002132 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002133 {
2134 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2135 {
2136 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2137 return( ret );
2138 }
2139
2140 len = ssl->out_msglen;
2141 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2142 ssl->out_hdr[4] = (unsigned char)( len );
2143 }
2144
2145 ssl->out_left = 5 + ssl->out_msglen;
2146
2147 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2148 "version = [%d:%d], msglen = %d",
2149 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2150 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2151
2152 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002153 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 }
2155
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2157 {
2158 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2159 return( ret );
2160 }
2161
2162 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2163
2164 return( 0 );
2165}
2166
2167int ssl_read_record( ssl_context *ssl )
2168{
Paul Bakker05ef8352012-05-08 09:17:57 +00002169 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
2171 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2172
2173 if( ssl->in_hslen != 0 &&
2174 ssl->in_hslen < ssl->in_msglen )
2175 {
2176 /*
2177 * Get next Handshake message in the current record
2178 */
2179 ssl->in_msglen -= ssl->in_hslen;
2180
Paul Bakker8934a982011-08-05 11:11:53 +00002181 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002182 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
2184 ssl->in_hslen = 4;
2185 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2186
2187 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2188 " %d, type = %d, hslen = %d",
2189 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2190
2191 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2192 {
2193 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002194 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 }
2196
2197 if( ssl->in_msglen < ssl->in_hslen )
2198 {
2199 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002200 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 }
2202
Paul Bakker4224bc02014-04-08 14:36:50 +02002203 if( ssl->state != SSL_HANDSHAKE_OVER )
2204 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
2206 return( 0 );
2207 }
2208
2209 ssl->in_hslen = 0;
2210
2211 /*
2212 * Read the record header and validate it
2213 */
2214 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2215 {
2216 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2217 return( ret );
2218 }
2219
2220 ssl->in_msgtype = ssl->in_hdr[0];
2221 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2222
2223 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2224 "version = [%d:%d], msglen = %d",
2225 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2226 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2227
2228 if( ssl->in_hdr[1] != ssl->major_ver )
2229 {
2230 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002231 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 }
2233
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002234 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 {
2236 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002237 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002240 /* Sanity check (outer boundaries) */
2241 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2242 {
2243 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2244 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2245 }
2246
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002248 * Make sure the message length is acceptable for the current transform
2249 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 */
Paul Bakker48916f92012-09-16 19:57:18 +00002251 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002253 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 {
2255 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002256 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 }
2258 }
2259 else
2260 {
Paul Bakker48916f92012-09-16 19:57:18 +00002261 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 {
2263 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002264 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
2266
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002267#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002269 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 {
2271 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002272 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002274#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002276#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2277 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 /*
2279 * TLS encrypted messages can have up to 256 bytes of padding
2280 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002281 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002282 ssl->in_msglen > ssl->transform_in->minlen +
2283 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 {
2285 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002286 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002288#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
2290
2291 /*
2292 * Read and optionally decrypt the message contents
2293 */
2294 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2295 {
2296 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2297 return( ret );
2298 }
2299
2300 SSL_DEBUG_BUF( 4, "input record from network",
2301 ssl->in_hdr, 5 + ssl->in_msglen );
2302
Paul Bakker05ef8352012-05-08 09:17:57 +00002303#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002304 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002305 {
2306 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2307
2308 ret = ssl_hw_record_read( ssl );
2309 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2310 {
2311 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002312 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002313 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002314
2315 if( ret == 0 )
2316 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002317 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002318#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002319 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002320 {
2321 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2322 {
Paul Bakker40865c82013-01-31 17:13:13 +01002323#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2324 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2325 {
2326 ssl_send_alert_message( ssl,
2327 SSL_ALERT_LEVEL_FATAL,
2328 SSL_ALERT_MSG_BAD_RECORD_MAC );
2329 }
2330#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2332 return( ret );
2333 }
2334
2335 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2336 ssl->in_msg, ssl->in_msglen );
2337
2338 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2339 {
2340 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002341 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 }
2343 }
2344
Paul Bakker2770fbd2012-07-03 13:30:23 +00002345#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002346 if( ssl->transform_in != NULL &&
2347 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348 {
2349 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2350 {
2351 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2352 return( ret );
2353 }
2354
2355 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2356 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2357 }
2358#endif /* POLARSSL_ZLIB_SUPPORT */
2359
Paul Bakker0a925182012-04-16 06:46:41 +00002360 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2361 ssl->in_msgtype != SSL_MSG_ALERT &&
2362 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2363 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2364 {
2365 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2366
Paul Bakker48916f92012-09-16 19:57:18 +00002367 if( ( ret = ssl_send_alert_message( ssl,
2368 SSL_ALERT_LEVEL_FATAL,
2369 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002370 {
2371 return( ret );
2372 }
2373
2374 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2375 }
2376
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2378 {
2379 ssl->in_hslen = 4;
2380 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2381
2382 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2383 " %d, type = %d, hslen = %d",
2384 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2385
2386 /*
2387 * Additional checks to validate the handshake header
2388 */
2389 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2390 {
2391 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002392 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 }
2394
2395 if( ssl->in_msglen < ssl->in_hslen )
2396 {
2397 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002398 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 }
2400
Paul Bakker48916f92012-09-16 19:57:18 +00002401 if( ssl->state != SSL_HANDSHAKE_OVER )
2402 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 }
2404
2405 if( ssl->in_msgtype == SSL_MSG_ALERT )
2406 {
2407 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2408 ssl->in_msg[0], ssl->in_msg[1] ) );
2409
2410 /*
2411 * Ignore non-fatal alerts, except close_notify
2412 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002413 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002415 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2416 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002417 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 }
2419
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002420 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2421 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 {
2423 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002424 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 }
2426 }
2427
2428 ssl->in_left = 0;
2429
2430 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2431
2432 return( 0 );
2433}
2434
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002435int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2436{
2437 int ret;
2438
2439 if( ( ret = ssl_send_alert_message( ssl,
2440 SSL_ALERT_LEVEL_FATAL,
2441 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2442 {
2443 return( ret );
2444 }
2445
2446 return( 0 );
2447}
2448
Paul Bakker0a925182012-04-16 06:46:41 +00002449int ssl_send_alert_message( ssl_context *ssl,
2450 unsigned char level,
2451 unsigned char message )
2452{
2453 int ret;
2454
2455 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2456
2457 ssl->out_msgtype = SSL_MSG_ALERT;
2458 ssl->out_msglen = 2;
2459 ssl->out_msg[0] = level;
2460 ssl->out_msg[1] = message;
2461
2462 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2463 {
2464 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2465 return( ret );
2466 }
2467
2468 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2469
2470 return( 0 );
2471}
2472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473/*
2474 * Handshake functions
2475 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002476#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2477 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2478 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2479 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2480 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2481 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2482 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002483int ssl_write_certificate( ssl_context *ssl )
2484{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002485 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
2487 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2488
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002489 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002490 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002492 {
2493 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2494 ssl->state++;
2495 return( 0 );
2496 }
2497
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002498 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2499 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002500}
2501
2502int ssl_parse_certificate( ssl_context *ssl )
2503{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002504 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2505
2506 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2507
2508 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002509 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002511 {
2512 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2513 ssl->state++;
2514 return( 0 );
2515 }
2516
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002517 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2518 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002519}
2520#else
2521int ssl_write_certificate( ssl_context *ssl )
2522{
2523 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2524 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002525 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002526 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2527
2528 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2529
2530 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002531 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2532 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002533 {
2534 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2535 ssl->state++;
2536 return( 0 );
2537 }
2538
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002539#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 if( ssl->endpoint == SSL_IS_CLIENT )
2541 {
2542 if( ssl->client_auth == 0 )
2543 {
2544 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2545 ssl->state++;
2546 return( 0 );
2547 }
2548
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002549#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 /*
2551 * If using SSLv3 and got no cert, send an Alert message
2552 * (otherwise an empty Certificate message will be sent).
2553 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002554 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2556 {
2557 ssl->out_msglen = 2;
2558 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002559 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2560 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
2562 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2563 goto write_msg;
2564 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002565#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002567#endif /* POLARSSL_SSL_CLI_C */
2568#if defined(POLARSSL_SSL_SRV_C)
2569 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002571 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 {
2573 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002574 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
2576 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002579 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
2581 /*
2582 * 0 . 0 handshake type
2583 * 1 . 3 handshake length
2584 * 4 . 6 length of all certs
2585 * 7 . 9 length of cert. 1
2586 * 10 . n-1 peer certificate
2587 * n . n+2 length of cert. 2
2588 * n+3 . ... upper level cert, etc.
2589 */
2590 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002591 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Paul Bakker29087132010-03-21 21:03:34 +00002593 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 {
2595 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002596 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
2598 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2599 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002600 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
2602
2603 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2604 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2605 ssl->out_msg[i + 2] = (unsigned char)( n );
2606
2607 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2608 i += n; crt = crt->next;
2609 }
2610
2611 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2612 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2613 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2614
2615 ssl->out_msglen = i;
2616 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2617 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2618
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002619#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002620write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002621#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
2623 ssl->state++;
2624
2625 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2626 {
2627 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2628 return( ret );
2629 }
2630
2631 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2632
Paul Bakkered27a042013-04-18 22:46:23 +02002633 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634}
2635
2636int ssl_parse_certificate( ssl_context *ssl )
2637{
Paul Bakkered27a042013-04-18 22:46:23 +02002638 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002639 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002640 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
2642 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2643
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002644 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002645 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2646 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002647 {
2648 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2649 ssl->state++;
2650 return( 0 );
2651 }
2652
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002653#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002655 ( ssl->authmode == SSL_VERIFY_NONE ||
2656 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002658 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2660 ssl->state++;
2661 return( 0 );
2662 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002663#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
2665 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2666 {
2667 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2668 return( ret );
2669 }
2670
2671 ssl->state++;
2672
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002673#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002674#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 /*
2676 * Check if the client sent an empty certificate
2677 */
2678 if( ssl->endpoint == SSL_IS_SERVER &&
2679 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2680 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002681 if( ssl->in_msglen == 2 &&
2682 ssl->in_msgtype == SSL_MSG_ALERT &&
2683 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2684 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 {
2686 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2687
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002688 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2690 return( 0 );
2691 else
Paul Bakker40e46942009-01-03 21:51:57 +00002692 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 }
2694 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002695#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002697#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2698 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 if( ssl->endpoint == SSL_IS_SERVER &&
2700 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2701 {
2702 if( ssl->in_hslen == 7 &&
2703 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2704 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2705 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2706 {
2707 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2708
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002709 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002711 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 else
2713 return( 0 );
2714 }
2715 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002716#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2717 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002718#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
2720 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2721 {
2722 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002723 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 }
2725
2726 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2727 {
2728 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002729 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 }
2731
2732 /*
2733 * Same message structure as in ssl_write_certificate()
2734 */
2735 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2736
2737 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2738 {
2739 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002740 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 }
2742
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002743 /* In case we tried to reuse a session but it failed */
2744 if( ssl->session_negotiate->peer_cert != NULL )
2745 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002746 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002747 polarssl_free( ssl->session_negotiate->peer_cert );
2748 }
2749
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002750 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2751 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 {
2753 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002754 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002755 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 }
2757
Paul Bakkerb6b09562013-09-18 14:17:41 +02002758 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
2760 i = 7;
2761
2762 while( i < ssl->in_hslen )
2763 {
2764 if( ssl->in_msg[i] != 0 )
2765 {
2766 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002767 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 }
2769
2770 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2771 | (unsigned int) ssl->in_msg[i + 2];
2772 i += 3;
2773
2774 if( n < 128 || i + n > ssl->in_hslen )
2775 {
2776 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002777 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 }
2779
Paul Bakkerddf26b42013-09-18 13:46:23 +02002780 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2781 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 if( ret != 0 )
2783 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002784 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 return( ret );
2786 }
2787
2788 i += n;
2789 }
2790
Paul Bakker48916f92012-09-16 19:57:18 +00002791 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002793 /*
2794 * On client, make sure the server cert doesn't change during renego to
2795 * avoid "triple handshake" attack: https://secure-resumption.com/
2796 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002797#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002798 if( ssl->endpoint == SSL_IS_CLIENT &&
2799 ssl->renegotiation == SSL_RENEGOTIATION )
2800 {
2801 if( ssl->session->peer_cert == NULL )
2802 {
2803 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2804 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2805 }
2806
2807 if( ssl->session->peer_cert->raw.len !=
2808 ssl->session_negotiate->peer_cert->raw.len ||
2809 memcmp( ssl->session->peer_cert->raw.p,
2810 ssl->session_negotiate->peer_cert->raw.p,
2811 ssl->session->peer_cert->raw.len ) != 0 )
2812 {
2813 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2814 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2815 }
2816 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002817#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002818
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 if( ssl->authmode != SSL_VERIFY_NONE )
2820 {
2821 if( ssl->ca_chain == NULL )
2822 {
2823 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002824 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 }
2826
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002827 /*
2828 * Main check: verify certificate
2829 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002830 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2831 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2832 &ssl->session_negotiate->verify_result,
2833 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
2835 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002836 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002838 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002839
2840 /*
2841 * Secondary checks: always done, but change 'ret' only if it was 0
2842 */
2843
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002844#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002845 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002846 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002847
2848 /* If certificate uses an EC key, make sure the curve is OK */
2849 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2850 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2851 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002852 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002853 if( ret == 0 )
2854 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002855 }
2856 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002857#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002859 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2860 ciphersuite_info,
2861 ! ssl->endpoint ) != 0 )
2862 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002863 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002864 if( ret == 0 )
2865 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2866 }
2867
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2869 ret = 0;
2870 }
2871
2872 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2873
2874 return( ret );
2875}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002876#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2877 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2878 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2879 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2880 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2881 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2882 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
2884int ssl_write_change_cipher_spec( ssl_context *ssl )
2885{
2886 int ret;
2887
2888 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2889
2890 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2891 ssl->out_msglen = 1;
2892 ssl->out_msg[0] = 1;
2893
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 ssl->state++;
2895
2896 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2897 {
2898 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2899 return( ret );
2900 }
2901
2902 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2903
2904 return( 0 );
2905}
2906
2907int ssl_parse_change_cipher_spec( ssl_context *ssl )
2908{
2909 int ret;
2910
2911 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2912
Paul Bakker5121ce52009-01-03 21:22:43 +00002913 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2914 {
2915 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2916 return( ret );
2917 }
2918
2919 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2920 {
2921 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002922 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 }
2924
2925 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2926 {
2927 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002928 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 }
2930
2931 ssl->state++;
2932
2933 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2934
2935 return( 0 );
2936}
2937
Paul Bakker41c83d32013-03-20 14:39:14 +01002938void ssl_optimize_checksum( ssl_context *ssl,
2939 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002940{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002941 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002942
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002943#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2944 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002945 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002946 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002947 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002948#endif
2949#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2950#if defined(POLARSSL_SHA512_C)
2951 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2952 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2953 else
2954#endif
2955#if defined(POLARSSL_SHA256_C)
2956 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002957 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002958 else
2959#endif
2960#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002961 {
2962 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002963 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002964 }
Paul Bakker380da532012-04-18 16:10:25 +00002965}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002966
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002967static void ssl_update_checksum_start( ssl_context *ssl,
2968 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002969{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002970#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2971 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002972 md5_update( &ssl->handshake->fin_md5 , buf, len );
2973 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002974#endif
2975#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2976#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002977 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002978#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002979#if defined(POLARSSL_SHA512_C)
2980 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002981#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002982#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002983}
2984
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002985#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2986 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002987static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2988 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002989{
Paul Bakker48916f92012-09-16 19:57:18 +00002990 md5_update( &ssl->handshake->fin_md5 , buf, len );
2991 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002992}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002993#endif
Paul Bakker380da532012-04-18 16:10:25 +00002994
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002995#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2996#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002997static void ssl_update_checksum_sha256( ssl_context *ssl,
2998 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002999{
Paul Bakker9e36f042013-06-30 14:34:05 +02003000 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003001}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003002#endif
Paul Bakker380da532012-04-18 16:10:25 +00003003
Paul Bakker9e36f042013-06-30 14:34:05 +02003004#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003005static void ssl_update_checksum_sha384( ssl_context *ssl,
3006 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003007{
Paul Bakker9e36f042013-06-30 14:34:05 +02003008 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003009}
Paul Bakker769075d2012-11-24 11:26:46 +01003010#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003011#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003012
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003013#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003014static void ssl_calc_finished_ssl(
3015 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003016{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003017 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003018 md5_context md5;
3019 sha1_context sha1;
3020
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 unsigned char padbuf[48];
3022 unsigned char md5sum[16];
3023 unsigned char sha1sum[20];
3024
Paul Bakker48916f92012-09-16 19:57:18 +00003025 ssl_session *session = ssl->session_negotiate;
3026 if( !session )
3027 session = ssl->session;
3028
Paul Bakker1ef83d62012-04-11 12:09:53 +00003029 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3030
Paul Bakker48916f92012-09-16 19:57:18 +00003031 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3032 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003033
3034 /*
3035 * SSLv3:
3036 * hash =
3037 * MD5( master + pad2 +
3038 * MD5( handshake + sender + master + pad1 ) )
3039 * + SHA1( master + pad2 +
3040 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 */
3042
Paul Bakker90995b52013-06-24 19:20:35 +02003043#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003045 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Paul Bakker90995b52013-06-24 19:20:35 +02003048#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003049 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003050 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003052
Paul Bakker3c2122f2013-06-24 19:03:14 +02003053 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3054 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Paul Bakker1ef83d62012-04-11 12:09:53 +00003056 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Paul Bakker3c2122f2013-06-24 19:03:14 +02003058 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003059 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003060 md5_update( &md5, padbuf, 48 );
3061 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Paul Bakker3c2122f2013-06-24 19:03:14 +02003063 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003064 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003065 sha1_update( &sha1, padbuf, 40 );
3066 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003069
Paul Bakker1ef83d62012-04-11 12:09:53 +00003070 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003071 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072 md5_update( &md5, padbuf, 48 );
3073 md5_update( &md5, md5sum, 16 );
3074 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003075
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003077 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078 sha1_update( &sha1, padbuf , 40 );
3079 sha1_update( &sha1, sha1sum, 20 );
3080 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003081
Paul Bakker1ef83d62012-04-11 12:09:53 +00003082 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003083
Paul Bakker5b4af392014-06-26 12:09:34 +02003084 md5_free( &md5 );
3085 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Paul Bakker34617722014-06-13 17:20:13 +02003087 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3088 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3089 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
3091 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3092}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003093#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003095#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003096static void ssl_calc_finished_tls(
3097 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003098{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003099 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003100 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003101 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003103 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003104
Paul Bakker48916f92012-09-16 19:57:18 +00003105 ssl_session *session = ssl->session_negotiate;
3106 if( !session )
3107 session = ssl->session;
3108
Paul Bakker1ef83d62012-04-11 12:09:53 +00003109 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003110
Paul Bakker48916f92012-09-16 19:57:18 +00003111 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3112 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Paul Bakker1ef83d62012-04-11 12:09:53 +00003114 /*
3115 * TLSv1:
3116 * hash = PRF( master, finished_label,
3117 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3118 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
Paul Bakker90995b52013-06-24 19:20:35 +02003120#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003121 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3122 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003123#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003124
Paul Bakker90995b52013-06-24 19:20:35 +02003125#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003126 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3127 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003128#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003129
3130 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003131 ? "client finished"
3132 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003133
3134 md5_finish( &md5, padbuf );
3135 sha1_finish( &sha1, padbuf + 16 );
3136
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003137 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003138 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003139
3140 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3141
Paul Bakker5b4af392014-06-26 12:09:34 +02003142 md5_free( &md5 );
3143 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003144
Paul Bakker34617722014-06-13 17:20:13 +02003145 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003146
3147 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3148}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003149#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003150
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003151#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3152#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003153static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003154 ssl_context *ssl, unsigned char *buf, int from )
3155{
3156 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003157 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003158 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003159 unsigned char padbuf[32];
3160
Paul Bakker48916f92012-09-16 19:57:18 +00003161 ssl_session *session = ssl->session_negotiate;
3162 if( !session )
3163 session = ssl->session;
3164
Paul Bakker380da532012-04-18 16:10:25 +00003165 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003166
Paul Bakker9e36f042013-06-30 14:34:05 +02003167 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003168
3169 /*
3170 * TLSv1.2:
3171 * hash = PRF( master, finished_label,
3172 * Hash( handshake ) )[0.11]
3173 */
3174
Paul Bakker9e36f042013-06-30 14:34:05 +02003175#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003176 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003177 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003178#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003179
3180 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003181 ? "client finished"
3182 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003183
Paul Bakker9e36f042013-06-30 14:34:05 +02003184 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003185
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003186 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003187 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003188
3189 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3190
Paul Bakker5b4af392014-06-26 12:09:34 +02003191 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003192
Paul Bakker34617722014-06-13 17:20:13 +02003193 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003194
3195 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3196}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003197#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003198
Paul Bakker9e36f042013-06-30 14:34:05 +02003199#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003200static void ssl_calc_finished_tls_sha384(
3201 ssl_context *ssl, unsigned char *buf, int from )
3202{
3203 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003204 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003205 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003206 unsigned char padbuf[48];
3207
Paul Bakker48916f92012-09-16 19:57:18 +00003208 ssl_session *session = ssl->session_negotiate;
3209 if( !session )
3210 session = ssl->session;
3211
Paul Bakker380da532012-04-18 16:10:25 +00003212 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003213
Paul Bakker9e36f042013-06-30 14:34:05 +02003214 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003215
3216 /*
3217 * TLSv1.2:
3218 * hash = PRF( master, finished_label,
3219 * Hash( handshake ) )[0.11]
3220 */
3221
Paul Bakker9e36f042013-06-30 14:34:05 +02003222#if !defined(POLARSSL_SHA512_ALT)
3223 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3224 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003225#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003226
3227 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003228 ? "client finished"
3229 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003230
Paul Bakker9e36f042013-06-30 14:34:05 +02003231 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003232
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003233 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003234 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003235
3236 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3237
Paul Bakker5b4af392014-06-26 12:09:34 +02003238 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003239
Paul Bakker34617722014-06-13 17:20:13 +02003240 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003241
3242 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3243}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003244#endif /* POLARSSL_SHA512_C */
3245#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003246
Paul Bakker48916f92012-09-16 19:57:18 +00003247void ssl_handshake_wrapup( ssl_context *ssl )
3248{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003249 int resume = ssl->handshake->resume;
3250
Paul Bakker48916f92012-09-16 19:57:18 +00003251 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3252
3253 /*
3254 * Free our handshake params
3255 */
3256 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003257 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003258 ssl->handshake = NULL;
3259
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003260#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003261 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003262 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003263 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003264 ssl->renego_records_seen = 0;
3265 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003266#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003267
Paul Bakker48916f92012-09-16 19:57:18 +00003268 /*
3269 * Switch in our now active transform context
3270 */
3271 if( ssl->transform )
3272 {
3273 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003274 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003275 }
3276 ssl->transform = ssl->transform_negotiate;
3277 ssl->transform_negotiate = NULL;
3278
Paul Bakker0a597072012-09-25 21:55:46 +00003279 if( ssl->session )
3280 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003281#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3282 /* RFC 7366 3.1: keep the EtM state */
3283 ssl->session_negotiate->encrypt_then_mac =
3284 ssl->session->encrypt_then_mac;
3285#endif
3286
Paul Bakker0a597072012-09-25 21:55:46 +00003287 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003288 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003289 }
3290 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003291 ssl->session_negotiate = NULL;
3292
Paul Bakker0a597072012-09-25 21:55:46 +00003293 /*
3294 * Add cache entry
3295 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003296 if( ssl->f_set_cache != NULL &&
3297 ssl->session->length != 0 &&
3298 resume == 0 )
3299 {
Paul Bakker0a597072012-09-25 21:55:46 +00003300 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3301 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003302 }
Paul Bakker0a597072012-09-25 21:55:46 +00003303
Paul Bakker48916f92012-09-16 19:57:18 +00003304 ssl->state++;
3305
3306 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3307}
3308
Paul Bakker1ef83d62012-04-11 12:09:53 +00003309int ssl_write_finished( ssl_context *ssl )
3310{
3311 int ret, hash_len;
3312
3313 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3314
Paul Bakker92be97b2013-01-02 17:30:03 +01003315 /*
3316 * Set the out_msg pointer to the correct location based on IV length
3317 */
3318 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3319 {
3320 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3321 ssl->transform_negotiate->fixed_ivlen;
3322 }
3323 else
3324 ssl->out_msg = ssl->out_iv;
3325
Paul Bakker48916f92012-09-16 19:57:18 +00003326 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003327
3328 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3330
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003331#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003332 ssl->verify_data_len = hash_len;
3333 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003334#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003335
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 ssl->out_msglen = 4 + hash_len;
3337 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3338 ssl->out_msg[0] = SSL_HS_FINISHED;
3339
3340 /*
3341 * In case of session resuming, invert the client and server
3342 * ChangeCipherSpec messages order.
3343 */
Paul Bakker0a597072012-09-25 21:55:46 +00003344 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003346#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003348 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003349#endif
3350#if defined(POLARSSL_SSL_SRV_C)
3351 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003353#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003354 }
3355 else
3356 ssl->state++;
3357
Paul Bakker48916f92012-09-16 19:57:18 +00003358 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003359 * Switch to our negotiated transform and session parameters for outbound
3360 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003361 */
3362 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3363 ssl->transform_out = ssl->transform_negotiate;
3364 ssl->session_out = ssl->session_negotiate;
3365 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366
Paul Bakker07eb38b2012-12-19 14:42:06 +01003367#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003368 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003369 {
3370 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3371 {
3372 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3373 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3374 }
3375 }
3376#endif
3377
Paul Bakker5121ce52009-01-03 21:22:43 +00003378 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3379 {
3380 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3381 return( ret );
3382 }
3383
3384 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3385
3386 return( 0 );
3387}
3388
3389int ssl_parse_finished( ssl_context *ssl )
3390{
Paul Bakker23986e52011-04-24 08:57:21 +00003391 int ret;
3392 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 unsigned char buf[36];
3394
3395 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3396
Paul Bakker48916f92012-09-16 19:57:18 +00003397 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398
Paul Bakker48916f92012-09-16 19:57:18 +00003399 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003400 * Switch to our negotiated transform and session parameters for inbound
3401 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003402 */
3403 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3404 ssl->transform_in = ssl->transform_negotiate;
3405 ssl->session_in = ssl->session_negotiate;
3406 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003407
Paul Bakker92be97b2013-01-02 17:30:03 +01003408 /*
3409 * Set the in_msg pointer to the correct location based on IV length
3410 */
3411 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3412 {
3413 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3414 ssl->transform_negotiate->fixed_ivlen;
3415 }
3416 else
3417 ssl->in_msg = ssl->in_iv;
3418
Paul Bakker07eb38b2012-12-19 14:42:06 +01003419#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003420 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003421 {
3422 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3423 {
3424 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3425 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3426 }
3427 }
3428#endif
3429
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3431 {
3432 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3433 return( ret );
3434 }
3435
3436 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3437 {
3438 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003439 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003440 }
3441
Paul Bakker1ef83d62012-04-11 12:09:53 +00003442 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3444
3445 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3446 ssl->in_hslen != 4 + hash_len )
3447 {
3448 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003449 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 }
3451
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003452 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 {
3454 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003455 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003456 }
3457
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003458#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003459 ssl->verify_data_len = hash_len;
3460 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003461#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003462
Paul Bakker0a597072012-09-25 21:55:46 +00003463 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003464 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003465#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003466 if( ssl->endpoint == SSL_IS_CLIENT )
3467 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003468#endif
3469#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003471 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003472#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003473 }
3474 else
3475 ssl->state++;
3476
3477 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3478
3479 return( 0 );
3480}
3481
Paul Bakker968afaa2014-07-09 11:09:24 +02003482static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003483{
3484 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3485
3486#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3487 defined(POLARSSL_SSL_PROTO_TLS1_1)
3488 md5_init( &handshake->fin_md5 );
3489 sha1_init( &handshake->fin_sha1 );
3490 md5_starts( &handshake->fin_md5 );
3491 sha1_starts( &handshake->fin_sha1 );
3492#endif
3493#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3494#if defined(POLARSSL_SHA256_C)
3495 sha256_init( &handshake->fin_sha256 );
3496 sha256_starts( &handshake->fin_sha256, 0 );
3497#endif
3498#if defined(POLARSSL_SHA512_C)
3499 sha512_init( &handshake->fin_sha512 );
3500 sha512_starts( &handshake->fin_sha512, 1 );
3501#endif
3502#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3503
3504 handshake->update_checksum = ssl_update_checksum_start;
3505 handshake->sig_alg = SSL_HASH_SHA1;
3506
3507#if defined(POLARSSL_DHM_C)
3508 dhm_init( &handshake->dhm_ctx );
3509#endif
3510#if defined(POLARSSL_ECDH_C)
3511 ecdh_init( &handshake->ecdh_ctx );
3512#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003513}
3514
3515static void ssl_transform_init( ssl_transform *transform )
3516{
3517 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003518
3519 cipher_init( &transform->cipher_ctx_enc );
3520 cipher_init( &transform->cipher_ctx_dec );
3521
3522 md_init( &transform->md_ctx_enc );
3523 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003524}
3525
3526void ssl_session_init( ssl_session *session )
3527{
3528 memset( session, 0, sizeof(ssl_session) );
3529}
3530
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003531static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003532{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003533 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003534 if( ssl->transform_negotiate )
3535 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003536 if( ssl->session_negotiate )
3537 ssl_session_free( ssl->session_negotiate );
3538 if( ssl->handshake )
3539 ssl_handshake_free( ssl->handshake );
3540
3541 /*
3542 * Either the pointers are now NULL or cleared properly and can be freed.
3543 * Now allocate missing structures.
3544 */
3545 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003546 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003547 ssl->transform_negotiate = (ssl_transform *) polarssl_malloc(
3548 sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003549 }
Paul Bakker48916f92012-09-16 19:57:18 +00003550
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003551 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003552 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003553 ssl->session_negotiate = (ssl_session *) polarssl_malloc(
3554 sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003555 }
Paul Bakker48916f92012-09-16 19:57:18 +00003556
Paul Bakker82788fb2014-10-20 13:59:19 +02003557 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003558 {
3559 ssl->handshake = (ssl_handshake_params *)
3560 polarssl_malloc( sizeof(ssl_handshake_params) );
3561 }
Paul Bakker48916f92012-09-16 19:57:18 +00003562
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003563 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003564 if( ssl->handshake == NULL ||
3565 ssl->transform_negotiate == NULL ||
3566 ssl->session_negotiate == NULL )
3567 {
3568 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003569
3570 polarssl_free( ssl->handshake );
3571 polarssl_free( ssl->transform_negotiate );
3572 polarssl_free( ssl->session_negotiate );
3573
3574 ssl->handshake = NULL;
3575 ssl->transform_negotiate = NULL;
3576 ssl->session_negotiate = NULL;
3577
Paul Bakker48916f92012-09-16 19:57:18 +00003578 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3579 }
3580
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003581 /* Initialize structures */
3582 ssl_session_init( ssl->session_negotiate );
3583 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003584 ssl_handshake_params_init( ssl->handshake );
3585
3586#if defined(POLARSSL_X509_CRT_PARSE_C)
3587 ssl->handshake->key_cert = ssl->key_cert;
3588#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003589
Paul Bakker48916f92012-09-16 19:57:18 +00003590 return( 0 );
3591}
3592
Paul Bakker5121ce52009-01-03 21:22:43 +00003593/*
3594 * Initialize an SSL context
3595 */
3596int ssl_init( ssl_context *ssl )
3597{
Paul Bakker48916f92012-09-16 19:57:18 +00003598 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 int len = SSL_BUFFER_LEN;
3600
3601 memset( ssl, 0, sizeof( ssl_context ) );
3602
Paul Bakker62f2dee2012-09-28 07:31:51 +00003603 /*
3604 * Sane defaults
3605 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003606 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3607 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3608 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3609 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003610
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003611 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003612
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003613#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003614 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003615 memset( ssl->renego_period, 0xFF, 7 );
3616 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003617#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003618
Paul Bakker62f2dee2012-09-28 07:31:51 +00003619#if defined(POLARSSL_DHM_C)
3620 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3621 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3622 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3623 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3624 {
3625 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3626 return( ret );
3627 }
3628#endif
3629
3630 /*
3631 * Prepare base structures
3632 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003633 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003634 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003635 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003636 ssl->in_msg = ssl->in_ctr + 13;
3637
3638 if( ssl->in_ctr == NULL )
3639 {
3640 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003641 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 }
3643
Paul Bakker6e339b52013-07-03 13:37:05 +02003644 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003646 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003647 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003648
3649 if( ssl->out_ctr == NULL )
3650 {
3651 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003652 polarssl_free( ssl->in_ctr );
3653 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003654 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003655 }
3656
3657 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3658 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3659
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003660#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3661 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3662#endif
3663
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003664#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3665 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3666#endif
3667
Paul Bakker606b4ba2013-08-14 16:52:14 +02003668#if defined(POLARSSL_SSL_SESSION_TICKETS)
3669 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3670#endif
3671
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003672#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003673 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003674#endif
3675
Paul Bakker48916f92012-09-16 19:57:18 +00003676 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3677 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003678
3679 return( 0 );
3680}
3681
3682/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003683 * Reset an initialized and used SSL context for re-use while retaining
3684 * all application-set variables, function pointers and data.
3685 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003686int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003687{
Paul Bakker48916f92012-09-16 19:57:18 +00003688 int ret;
3689
Paul Bakker7eb013f2011-10-06 12:37:39 +00003690 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003691
3692#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003693 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003694 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003695
3696 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003697 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3698 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003699#endif
3700 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003701
Paul Bakker7eb013f2011-10-06 12:37:39 +00003702 ssl->in_offt = NULL;
3703
Paul Bakker92be97b2013-01-02 17:30:03 +01003704 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003705 ssl->in_msgtype = 0;
3706 ssl->in_msglen = 0;
3707 ssl->in_left = 0;
3708
3709 ssl->in_hslen = 0;
3710 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003711 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003712
Paul Bakker92be97b2013-01-02 17:30:03 +01003713 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003714 ssl->out_msgtype = 0;
3715 ssl->out_msglen = 0;
3716 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003717#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003718 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3719 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003720#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003721
Paul Bakker48916f92012-09-16 19:57:18 +00003722 ssl->transform_in = NULL;
3723 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003724
3725 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3726 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003727
3728#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003729 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003730 {
3731 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003732 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003733 {
3734 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3735 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3736 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003737 }
3738#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003739
Paul Bakker48916f92012-09-16 19:57:18 +00003740 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003741 {
Paul Bakker48916f92012-09-16 19:57:18 +00003742 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003743 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003744 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003745 }
Paul Bakker48916f92012-09-16 19:57:18 +00003746
Paul Bakkerc0463502013-02-14 11:19:38 +01003747 if( ssl->session )
3748 {
3749 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003750 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003751 ssl->session = NULL;
3752 }
3753
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003754#if defined(POLARSSL_SSL_ALPN)
3755 ssl->alpn_chosen = NULL;
3756#endif
3757
Paul Bakker48916f92012-09-16 19:57:18 +00003758 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3759 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003760
3761 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003762}
3763
Paul Bakkera503a632013-08-14 13:48:06 +02003764#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003765static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3766{
3767 aes_free( &tkeys->enc );
3768 aes_free( &tkeys->dec );
3769
3770 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3771}
3772
Paul Bakker7eb013f2011-10-06 12:37:39 +00003773/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003774 * Allocate and initialize ticket keys
3775 */
3776static int ssl_ticket_keys_init( ssl_context *ssl )
3777{
3778 int ret;
3779 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003780 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003781
3782 if( ssl->ticket_keys != NULL )
3783 return( 0 );
3784
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003785 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3786 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003787 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3788
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003789 aes_init( &tkeys->enc );
3790 aes_init( &tkeys->dec );
3791
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003792 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003793 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003794 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003795 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003796 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003797 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003798
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003799 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3800 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3801 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3802 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003803 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003804 polarssl_free( tkeys );
3805 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003806 }
3807
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003808 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003809 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003810 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003811 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003812 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003813 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003814
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003815 ssl->ticket_keys = tkeys;
3816
3817 return( 0 );
3818}
Paul Bakkera503a632013-08-14 13:48:06 +02003819#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003820
3821/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003822 * SSL set accessors
3823 */
3824void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3825{
3826 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003827
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003828#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3829 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003830 if( endpoint == SSL_IS_CLIENT )
3831 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003832#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003833
3834#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3835 if( endpoint == SSL_IS_SERVER )
3836 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3837#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003838}
3839
3840void ssl_set_authmode( ssl_context *ssl, int authmode )
3841{
3842 ssl->authmode = authmode;
3843}
3844
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003845#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003846void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003847 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003848 void *p_vrfy )
3849{
3850 ssl->f_vrfy = f_vrfy;
3851 ssl->p_vrfy = p_vrfy;
3852}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003853#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003854
Paul Bakker5121ce52009-01-03 21:22:43 +00003855void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003856 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 void *p_rng )
3858{
3859 ssl->f_rng = f_rng;
3860 ssl->p_rng = p_rng;
3861}
3862
3863void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003864 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 void *p_dbg )
3866{
3867 ssl->f_dbg = f_dbg;
3868 ssl->p_dbg = p_dbg;
3869}
3870
3871void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003872 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003873 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003874{
3875 ssl->f_recv = f_recv;
3876 ssl->f_send = f_send;
3877 ssl->p_recv = p_recv;
3878 ssl->p_send = p_send;
3879}
3880
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003881#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003882void ssl_set_session_cache( ssl_context *ssl,
3883 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3884 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003885{
Paul Bakker0a597072012-09-25 21:55:46 +00003886 ssl->f_get_cache = f_get_cache;
3887 ssl->p_get_cache = p_get_cache;
3888 ssl->f_set_cache = f_set_cache;
3889 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003890}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003891#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003892
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003893#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003894int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003895{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003896 int ret;
3897
3898 if( ssl == NULL ||
3899 session == NULL ||
3900 ssl->session_negotiate == NULL ||
3901 ssl->endpoint != SSL_IS_CLIENT )
3902 {
3903 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3904 }
3905
3906 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3907 return( ret );
3908
Paul Bakker0a597072012-09-25 21:55:46 +00003909 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003910
3911 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003912}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003913#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003914
Paul Bakkerb68cad62012-08-23 08:34:18 +00003915void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003916{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003917 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3918 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3919 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3920 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3921}
3922
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003923void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3924 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003925 int major, int minor )
3926{
3927 if( major != SSL_MAJOR_VERSION_3 )
3928 return;
3929
3930 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3931 return;
3932
3933 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003934}
3935
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003936#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003937/* Add a new (empty) key_cert entry an return a pointer to it */
3938static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3939{
3940 ssl_key_cert *key_cert, *last;
3941
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003942 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3943 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003944 return( NULL );
3945
3946 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3947
3948 /* Append the new key_cert to the (possibly empty) current list */
3949 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003950 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003951 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003952 if( ssl->handshake != NULL )
3953 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003954 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003955 else
3956 {
3957 last = ssl->key_cert;
3958 while( last->next != NULL )
3959 last = last->next;
3960 last->next = key_cert;
3961 }
3962
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003963 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003964}
3965
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003966void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003967 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003968{
3969 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003970 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003971 ssl->peer_cn = peer_cn;
3972}
3973
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003974int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003975 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003976{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003977 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3978
3979 if( key_cert == NULL )
3980 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3981
3982 key_cert->cert = own_cert;
3983 key_cert->key = pk_key;
3984
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003985 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003986}
3987
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003988#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003989int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003990 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003991{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003992 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003993 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003994
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003995 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003996 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3997
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003998 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3999 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004000 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004001
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004002 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004003
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004004 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004005 if( ret != 0 )
4006 return( ret );
4007
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004008 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004009 return( ret );
4010
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004011 key_cert->cert = own_cert;
4012 key_cert->key_own_alloc = 1;
4013
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01004014 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004016#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004017
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004018int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004019 void *rsa_key,
4020 rsa_decrypt_func rsa_decrypt,
4021 rsa_sign_func rsa_sign,
4022 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004023{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004024 int ret;
4025 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004026
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004027 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004028 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4029
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004030 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4031 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004032 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004033
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004034 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004035
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004036 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4037 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4038 return( ret );
4039
4040 key_cert->cert = own_cert;
4041 key_cert->key_own_alloc = 1;
4042
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01004043 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00004044}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004045#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004046
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004047#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004048int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4049 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004050{
Paul Bakker6db455e2013-09-18 17:29:31 +02004051 if( psk == NULL || psk_identity == NULL )
4052 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4053
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004054 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004055 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4056
Paul Bakker6db455e2013-09-18 17:29:31 +02004057 if( ssl->psk != NULL )
4058 {
4059 polarssl_free( ssl->psk );
4060 polarssl_free( ssl->psk_identity );
4061 }
4062
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004063 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004064 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004065
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004066 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004067 ssl->psk_identity = (unsigned char *)
4068 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004069
4070 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4071 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4072
4073 memcpy( ssl->psk, psk, ssl->psk_len );
4074 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004075
4076 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004077}
4078
4079void ssl_set_psk_cb( ssl_context *ssl,
4080 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4081 size_t),
4082 void *p_psk )
4083{
4084 ssl->f_psk = f_psk;
4085 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004086}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004087#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004088
Paul Bakker48916f92012-09-16 19:57:18 +00004089#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004090int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004091{
4092 int ret;
4093
Paul Bakker48916f92012-09-16 19:57:18 +00004094 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004095 {
4096 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4097 return( ret );
4098 }
4099
Paul Bakker48916f92012-09-16 19:57:18 +00004100 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004101 {
4102 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4103 return( ret );
4104 }
4105
4106 return( 0 );
4107}
4108
Paul Bakker1b57b062011-01-06 15:48:19 +00004109int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4110{
4111 int ret;
4112
Paul Bakker66d5d072014-06-17 16:39:18 +02004113 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004114 {
4115 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4116 return( ret );
4117 }
4118
Paul Bakker66d5d072014-06-17 16:39:18 +02004119 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004120 {
4121 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4122 return( ret );
4123 }
4124
4125 return( 0 );
4126}
Paul Bakker48916f92012-09-16 19:57:18 +00004127#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004128
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004129#if defined(POLARSSL_SSL_SET_CURVES)
4130/*
4131 * Set the allowed elliptic curves
4132 */
4133void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4134{
4135 ssl->curve_list = curve_list;
4136}
4137#endif
4138
Paul Bakker0be444a2013-08-27 21:55:01 +02004139#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004140int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004141{
4142 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004144
4145 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004146
4147 if( ssl->hostname_len + 1 == 0 )
4148 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4149
Paul Bakker6e339b52013-07-03 13:37:05 +02004150 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004151
Paul Bakkerb15b8512012-01-13 13:44:06 +00004152 if( ssl->hostname == NULL )
4153 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4154
Paul Bakker3c2122f2013-06-24 19:03:14 +02004155 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004156 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004157
Paul Bakker40ea7de2009-05-03 10:18:48 +00004158 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004159
4160 return( 0 );
4161}
4162
Paul Bakker5701cdc2012-09-27 21:49:42 +00004163void ssl_set_sni( ssl_context *ssl,
4164 int (*f_sni)(void *, ssl_context *,
4165 const unsigned char *, size_t),
4166 void *p_sni )
4167{
4168 ssl->f_sni = f_sni;
4169 ssl->p_sni = p_sni;
4170}
Paul Bakker0be444a2013-08-27 21:55:01 +02004171#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004172
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004173#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004174int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004175{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004176 size_t cur_len, tot_len;
4177 const char **p;
4178
4179 /*
4180 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4181 * truncated". Check lengths now rather than later.
4182 */
4183 tot_len = 0;
4184 for( p = protos; *p != NULL; p++ )
4185 {
4186 cur_len = strlen( *p );
4187 tot_len += cur_len;
4188
4189 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4190 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4191 }
4192
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004193 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004194
4195 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004196}
4197
4198const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4199{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004200 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004201}
4202#endif /* POLARSSL_SSL_ALPN */
4203
Paul Bakker490ecc82011-10-06 13:04:09 +00004204void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4205{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004206 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4207 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4208 {
4209 ssl->max_major_ver = major;
4210 ssl->max_minor_ver = minor;
4211 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004212}
4213
Paul Bakker1d29fb52012-09-28 13:28:45 +00004214void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4215{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004216 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4217 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4218 {
4219 ssl->min_major_ver = major;
4220 ssl->min_minor_ver = minor;
4221 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004222}
4223
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004224#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4225void ssl_set_fallback( ssl_context *ssl, char fallback )
4226{
4227 ssl->fallback = fallback;
4228}
4229#endif
4230
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004231#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4232void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4233{
4234 ssl->encrypt_then_mac = etm;
4235}
4236#endif
4237
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004238#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4239void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4240{
4241 ssl->extended_ms = ems;
4242}
4243#endif
4244
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004245void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4246{
4247 ssl->arc4_disabled = arc4;
4248}
4249
Paul Bakker05decb22013-08-15 13:33:48 +02004250#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004251int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4252{
Paul Bakker77e257e2013-12-16 15:29:52 +01004253 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004254 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004255 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004256 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004257 }
4258
4259 ssl->mfl_code = mfl_code;
4260
4261 return( 0 );
4262}
Paul Bakker05decb22013-08-15 13:33:48 +02004263#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004264
Paul Bakker1f2bc622013-08-15 13:45:55 +02004265#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004266int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004267{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004268 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004269
4270 return( 0 );
4271}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004272#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004273
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004274#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4275void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4276{
4277 ssl->split_done = split;
4278}
4279#endif
4280
Paul Bakker48916f92012-09-16 19:57:18 +00004281void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4282{
4283 ssl->allow_legacy_renegotiation = allow_legacy;
4284}
4285
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004286#if defined(POLARSSL_SSL_RENEGOTIATION)
4287void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4288{
4289 ssl->disable_renegotiation = renegotiation;
4290}
4291
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004292void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4293{
4294 ssl->renego_max_records = max_records;
4295}
4296
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004297void ssl_set_renegotiation_period( ssl_context *ssl,
4298 const unsigned char period[8] )
4299{
4300 memcpy( ssl->renego_period, period, 8 );
4301}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004302#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004303
Paul Bakkera503a632013-08-14 13:48:06 +02004304#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004305int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4306{
4307 ssl->session_tickets = use_tickets;
4308
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004309#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004310 if( ssl->endpoint == SSL_IS_CLIENT )
4311 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004312#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004313
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004314 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4315 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004316
4317 if( ssl->f_rng == NULL )
4318 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4319
4320 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004321}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004322
4323void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4324{
4325 ssl->ticket_lifetime = lifetime;
4326}
Paul Bakkera503a632013-08-14 13:48:06 +02004327#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004328
Paul Bakker5121ce52009-01-03 21:22:43 +00004329/*
4330 * SSL get accessors
4331 */
4332size_t ssl_get_bytes_avail( const ssl_context *ssl )
4333{
4334 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4335}
4336
Paul Bakkerff60ee62010-03-16 21:09:09 +00004337int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004338{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004339 if( ssl->session != NULL )
4340 return( ssl->session->verify_result );
4341
4342 if( ssl->session_negotiate != NULL )
4343 return( ssl->session_negotiate->verify_result );
4344
4345 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004346}
4347
Paul Bakkere3166ce2011-01-27 17:40:50 +00004348const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004349{
Paul Bakker926c8e42013-03-06 10:23:34 +01004350 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004351 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004352
Paul Bakkere3166ce2011-01-27 17:40:50 +00004353 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004354}
4355
Paul Bakker43ca69c2011-01-15 17:35:19 +00004356const char *ssl_get_version( const ssl_context *ssl )
4357{
4358 switch( ssl->minor_ver )
4359 {
4360 case SSL_MINOR_VERSION_0:
4361 return( "SSLv3.0" );
4362
4363 case SSL_MINOR_VERSION_1:
4364 return( "TLSv1.0" );
4365
4366 case SSL_MINOR_VERSION_2:
4367 return( "TLSv1.1" );
4368
Paul Bakker1ef83d62012-04-11 12:09:53 +00004369 case SSL_MINOR_VERSION_3:
4370 return( "TLSv1.2" );
4371
Paul Bakker43ca69c2011-01-15 17:35:19 +00004372 default:
4373 break;
4374 }
4375 return( "unknown" );
4376}
4377
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004378#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004379const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004380{
4381 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004382 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004383
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004384 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004385}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004386#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004387
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004388#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004389int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4390{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004391 if( ssl == NULL ||
4392 dst == NULL ||
4393 ssl->session == NULL ||
4394 ssl->endpoint != SSL_IS_CLIENT )
4395 {
4396 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4397 }
4398
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004399 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004400}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004401#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004402
Paul Bakker5121ce52009-01-03 21:22:43 +00004403/*
Paul Bakker1961b702013-01-25 14:49:24 +01004404 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004405 */
Paul Bakker1961b702013-01-25 14:49:24 +01004406int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004407{
Paul Bakker40e46942009-01-03 21:51:57 +00004408 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004409
Paul Bakker40e46942009-01-03 21:51:57 +00004410#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004411 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004412 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004413#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004414#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004415 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004416 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004417#endif
4418
Paul Bakker1961b702013-01-25 14:49:24 +01004419 return( ret );
4420}
4421
4422/*
4423 * Perform the SSL handshake
4424 */
4425int ssl_handshake( ssl_context *ssl )
4426{
4427 int ret = 0;
4428
4429 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4430
4431 while( ssl->state != SSL_HANDSHAKE_OVER )
4432 {
4433 ret = ssl_handshake_step( ssl );
4434
4435 if( ret != 0 )
4436 break;
4437 }
4438
Paul Bakker5121ce52009-01-03 21:22:43 +00004439 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4440
4441 return( ret );
4442}
4443
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004444#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004445#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004446/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004447 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004448 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004449static int ssl_write_hello_request( ssl_context *ssl )
4450{
4451 int ret;
4452
4453 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4454
4455 ssl->out_msglen = 4;
4456 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4457 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4458
4459 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4460 {
4461 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4462 return( ret );
4463 }
4464
4465 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4466
4467 return( 0 );
4468}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004469#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004470
4471/*
4472 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004473 * - any side: calling ssl_renegotiate(),
4474 * - client: receiving a HelloRequest during ssl_read(),
4475 * - server: receiving any handshake message on server during ssl_read() after
4476 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004477 * If the handshake doesn't complete due to waiting for I/O, it will continue
4478 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004479 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004480static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004481{
4482 int ret;
4483
4484 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4485
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004486 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4487 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004488
4489 ssl->state = SSL_HELLO_REQUEST;
4490 ssl->renegotiation = SSL_RENEGOTIATION;
4491
Paul Bakker48916f92012-09-16 19:57:18 +00004492 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4493 {
4494 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4495 return( ret );
4496 }
4497
4498 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4499
4500 return( 0 );
4501}
4502
4503/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004504 * Renegotiate current connection on client,
4505 * or request renegotiation on server
4506 */
4507int ssl_renegotiate( ssl_context *ssl )
4508{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004509 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004510
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004511#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004512 /* On server, just send the request */
4513 if( ssl->endpoint == SSL_IS_SERVER )
4514 {
4515 if( ssl->state != SSL_HANDSHAKE_OVER )
4516 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4517
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004518 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4519
4520 /* Did we already try/start sending HelloRequest? */
4521 if( ssl->out_left != 0 )
4522 return( ssl_flush_output( ssl ) );
4523
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004524 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004525 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004526#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004527
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004528#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004529 /*
4530 * On client, either start the renegotiation process or,
4531 * if already in progress, continue the handshake
4532 */
4533 if( ssl->renegotiation != SSL_RENEGOTIATION )
4534 {
4535 if( ssl->state != SSL_HANDSHAKE_OVER )
4536 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4537
4538 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4539 {
4540 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4541 return( ret );
4542 }
4543 }
4544 else
4545 {
4546 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4547 {
4548 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4549 return( ret );
4550 }
4551 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004552#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004553
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004554 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004555}
4556
4557/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004558 * Check record counters and renegotiate if they're above the limit.
4559 */
4560static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4561{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004562 if( ssl->state != SSL_HANDSHAKE_OVER ||
4563 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4564 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4565 {
4566 return( 0 );
4567 }
4568
4569 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004570 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4571 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004572 {
4573 return( 0 );
4574 }
4575
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004576 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004577 return( ssl_renegotiate( ssl ) );
4578}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004579#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004580
4581/*
4582 * Receive application data decrypted from the SSL layer
4583 */
Paul Bakker23986e52011-04-24 08:57:21 +00004584int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004585{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004586 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004587 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004588
4589 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4590
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004591#if defined(POLARSSL_SSL_RENEGOTIATION)
4592 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4593 {
4594 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4595 return( ret );
4596 }
4597#endif
4598
Paul Bakker5121ce52009-01-03 21:22:43 +00004599 if( ssl->state != SSL_HANDSHAKE_OVER )
4600 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004601 ret = ssl_handshake( ssl );
4602 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4603 {
4604 record_read = 1;
4605 }
4606 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004607 {
4608 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4609 return( ret );
4610 }
4611 }
4612
4613 if( ssl->in_offt == NULL )
4614 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004615 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004616 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004617 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4618 {
4619 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4620 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004621
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004622 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4623 return( ret );
4624 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004625 }
4626
4627 if( ssl->in_msglen == 0 &&
4628 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4629 {
4630 /*
4631 * OpenSSL sends empty messages to randomize the IV
4632 */
4633 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4634 {
Paul Bakker831a7552011-05-18 13:32:51 +00004635 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4636 return( 0 );
4637
Paul Bakker5121ce52009-01-03 21:22:43 +00004638 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4639 return( ret );
4640 }
4641 }
4642
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004643#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004644 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4645 {
4646 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4647
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004648#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004649 if( ssl->endpoint == SSL_IS_CLIENT &&
4650 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4651 ssl->in_hslen != 4 ) )
4652 {
4653 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4654 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4655 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004656#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004657
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004658 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4659 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004660 ssl->allow_legacy_renegotiation ==
4661 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004662 {
4663 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4664
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004665#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004666 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004667 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004668 /*
4669 * SSLv3 does not have a "no_renegotiation" alert
4670 */
4671 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4672 return( ret );
4673 }
4674 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004675#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004676#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4677 defined(POLARSSL_SSL_PROTO_TLS1_2)
4678 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004679 {
4680 if( ( ret = ssl_send_alert_message( ssl,
4681 SSL_ALERT_LEVEL_WARNING,
4682 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4683 {
4684 return( ret );
4685 }
Paul Bakker48916f92012-09-16 19:57:18 +00004686 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004687 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004688#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4689 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004690 {
4691 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004692 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004693 }
Paul Bakker48916f92012-09-16 19:57:18 +00004694 }
4695 else
4696 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004697 ret = ssl_start_renegotiation( ssl );
4698 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4699 {
4700 record_read = 1;
4701 }
4702 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004703 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004704 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004705 return( ret );
4706 }
Paul Bakker48916f92012-09-16 19:57:18 +00004707 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004708
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004709 /* If a non-handshake record was read during renego, fallthrough,
4710 * else tell the user they should call ssl_read() again */
4711 if( ! record_read )
4712 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004713 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004714 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4715 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004716 ssl->renego_records_seen++;
4717
4718 if( ssl->renego_max_records >= 0 &&
4719 ssl->renego_records_seen > ssl->renego_max_records )
4720 {
4721 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4722 "but not honored by client" ) );
4723 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4724 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004725 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004726#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004727
4728 /* Fatal and closure alerts handled by ssl_read_record() */
4729 if( ssl->in_msgtype == SSL_MSG_ALERT )
4730 {
4731 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4732 return( POLARSSL_ERR_NET_WANT_READ );
4733 }
4734
4735 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004736 {
4737 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004738 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004739 }
4740
4741 ssl->in_offt = ssl->in_msg;
4742 }
4743
4744 n = ( len < ssl->in_msglen )
4745 ? len : ssl->in_msglen;
4746
4747 memcpy( buf, ssl->in_offt, n );
4748 ssl->in_msglen -= n;
4749
4750 if( ssl->in_msglen == 0 )
4751 /* all bytes consumed */
4752 ssl->in_offt = NULL;
4753 else
4754 /* more data available */
4755 ssl->in_offt += n;
4756
4757 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4758
Paul Bakker23986e52011-04-24 08:57:21 +00004759 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004760}
4761
4762/*
4763 * Send application data to be encrypted by the SSL layer
4764 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004765#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4766static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
4767#else
Paul Bakker23986e52011-04-24 08:57:21 +00004768int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004769#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004770{
Paul Bakker23986e52011-04-24 08:57:21 +00004771 int ret;
4772 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004773 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004774
4775 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4776
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004777#if defined(POLARSSL_SSL_RENEGOTIATION)
4778 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4779 {
4780 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4781 return( ret );
4782 }
4783#endif
4784
Paul Bakker5121ce52009-01-03 21:22:43 +00004785 if( ssl->state != SSL_HANDSHAKE_OVER )
4786 {
4787 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4788 {
4789 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4790 return( ret );
4791 }
4792 }
4793
Paul Bakker05decb22013-08-15 13:33:48 +02004794#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004795 /*
4796 * Assume mfl_code is correct since it was checked when set
4797 */
4798 max_len = mfl_code_to_length[ssl->mfl_code];
4799
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004800 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004801 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004802 */
4803 if( ssl->session_out != NULL &&
4804 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4805 {
4806 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4807 }
Paul Bakker05decb22013-08-15 13:33:48 +02004808#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004809
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004810 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004811
Paul Bakker5121ce52009-01-03 21:22:43 +00004812 if( ssl->out_left != 0 )
4813 {
4814 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4815 {
4816 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4817 return( ret );
4818 }
4819 }
Paul Bakker887bd502011-06-08 13:10:54 +00004820 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004821 {
Paul Bakker887bd502011-06-08 13:10:54 +00004822 ssl->out_msglen = n;
4823 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4824 memcpy( ssl->out_msg, buf, n );
4825
4826 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4827 {
4828 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4829 return( ret );
4830 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004831 }
4832
4833 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4834
Paul Bakker23986e52011-04-24 08:57:21 +00004835 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004836}
4837
4838/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004839 * Write application data, doing 1/n-1 splitting if necessary.
4840 *
4841 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004842 * then the caller will call us again with the same arguments, so
4843 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004844 */
4845#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4846int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4847{
4848 int ret;
4849
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004850 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4851 len <= 1 ||
4852 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004853 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4854 != POLARSSL_MODE_CBC )
4855 {
4856 return( ssl_write_real( ssl, buf, len ) );
4857 }
4858
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004859 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004860 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004861 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004862 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004863 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004864 }
4865
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004866 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4867 return( ret );
4868 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004869
4870 return( ret + 1 );
4871}
4872#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4873
4874/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004875 * Notify the peer that the connection is being closed
4876 */
4877int ssl_close_notify( ssl_context *ssl )
4878{
4879 int ret;
4880
4881 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4882
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004883 if( ssl->out_left != 0 )
4884 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004885
4886 if( ssl->state == SSL_HANDSHAKE_OVER )
4887 {
Paul Bakker48916f92012-09-16 19:57:18 +00004888 if( ( ret = ssl_send_alert_message( ssl,
4889 SSL_ALERT_LEVEL_WARNING,
4890 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004891 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004892 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004893 return( ret );
4894 }
4895 }
4896
4897 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4898
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004899 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004900}
4901
Paul Bakker48916f92012-09-16 19:57:18 +00004902void ssl_transform_free( ssl_transform *transform )
4903{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004904 if( transform == NULL )
4905 return;
4906
Paul Bakker48916f92012-09-16 19:57:18 +00004907#if defined(POLARSSL_ZLIB_SUPPORT)
4908 deflateEnd( &transform->ctx_deflate );
4909 inflateEnd( &transform->ctx_inflate );
4910#endif
4911
Paul Bakker84bbeb52014-07-01 14:53:22 +02004912 cipher_free( &transform->cipher_ctx_enc );
4913 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004914
Paul Bakker84bbeb52014-07-01 14:53:22 +02004915 md_free( &transform->md_ctx_enc );
4916 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004917
Paul Bakker34617722014-06-13 17:20:13 +02004918 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004919}
4920
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004921#if defined(POLARSSL_X509_CRT_PARSE_C)
4922static void ssl_key_cert_free( ssl_key_cert *key_cert )
4923{
4924 ssl_key_cert *cur = key_cert, *next;
4925
4926 while( cur != NULL )
4927 {
4928 next = cur->next;
4929
4930 if( cur->key_own_alloc )
4931 {
4932 pk_free( cur->key );
4933 polarssl_free( cur->key );
4934 }
4935 polarssl_free( cur );
4936
4937 cur = next;
4938 }
4939}
4940#endif /* POLARSSL_X509_CRT_PARSE_C */
4941
Paul Bakker48916f92012-09-16 19:57:18 +00004942void ssl_handshake_free( ssl_handshake_params *handshake )
4943{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004944 if( handshake == NULL )
4945 return;
4946
Paul Bakker48916f92012-09-16 19:57:18 +00004947#if defined(POLARSSL_DHM_C)
4948 dhm_free( &handshake->dhm_ctx );
4949#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004950#if defined(POLARSSL_ECDH_C)
4951 ecdh_free( &handshake->ecdh_ctx );
4952#endif
4953
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004954#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004955 /* explicit void pointer cast for buggy MS compiler */
4956 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004957#endif
4958
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004959#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4960 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4961 /*
4962 * Free only the linked list wrapper, not the keys themselves
4963 * since the belong to the SNI callback
4964 */
4965 if( handshake->sni_key_cert != NULL )
4966 {
4967 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4968
4969 while( cur != NULL )
4970 {
4971 next = cur->next;
4972 polarssl_free( cur );
4973 cur = next;
4974 }
4975 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004976#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004977
Paul Bakker34617722014-06-13 17:20:13 +02004978 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004979}
4980
4981void ssl_session_free( ssl_session *session )
4982{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004983 if( session == NULL )
4984 return;
4985
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004986#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004987 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004988 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004989 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004990 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004991 }
Paul Bakkered27a042013-04-18 22:46:23 +02004992#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004993
Paul Bakkera503a632013-08-14 13:48:06 +02004994#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004995 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004996#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004997
Paul Bakker34617722014-06-13 17:20:13 +02004998 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004999}
5000
Paul Bakker5121ce52009-01-03 21:22:43 +00005001/*
5002 * Free an SSL context
5003 */
5004void ssl_free( ssl_context *ssl )
5005{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005006 if( ssl == NULL )
5007 return;
5008
Paul Bakker5121ce52009-01-03 21:22:43 +00005009 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5010
Paul Bakker5121ce52009-01-03 21:22:43 +00005011 if( ssl->out_ctr != NULL )
5012 {
Paul Bakker34617722014-06-13 17:20:13 +02005013 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005014 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005015 }
5016
5017 if( ssl->in_ctr != NULL )
5018 {
Paul Bakker34617722014-06-13 17:20:13 +02005019 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005020 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005021 }
5022
Paul Bakker16770332013-10-11 09:59:44 +02005023#if defined(POLARSSL_ZLIB_SUPPORT)
5024 if( ssl->compress_buf != NULL )
5025 {
Paul Bakker34617722014-06-13 17:20:13 +02005026 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005027 polarssl_free( ssl->compress_buf );
5028 }
5029#endif
5030
Paul Bakker40e46942009-01-03 21:51:57 +00005031#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005032 mpi_free( &ssl->dhm_P );
5033 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005034#endif
5035
Paul Bakker48916f92012-09-16 19:57:18 +00005036 if( ssl->transform )
5037 {
5038 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005039 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005040 }
5041
5042 if( ssl->handshake )
5043 {
5044 ssl_handshake_free( ssl->handshake );
5045 ssl_transform_free( ssl->transform_negotiate );
5046 ssl_session_free( ssl->session_negotiate );
5047
Paul Bakker6e339b52013-07-03 13:37:05 +02005048 polarssl_free( ssl->handshake );
5049 polarssl_free( ssl->transform_negotiate );
5050 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005051 }
5052
Paul Bakkerc0463502013-02-14 11:19:38 +01005053 if( ssl->session )
5054 {
5055 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005056 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005057 }
5058
Paul Bakkera503a632013-08-14 13:48:06 +02005059#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005060 if( ssl->ticket_keys )
5061 {
5062 ssl_ticket_keys_free( ssl->ticket_keys );
5063 polarssl_free( ssl->ticket_keys );
5064 }
Paul Bakkera503a632013-08-14 13:48:06 +02005065#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005066
Paul Bakker0be444a2013-08-27 21:55:01 +02005067#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005068 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005069 {
Paul Bakker34617722014-06-13 17:20:13 +02005070 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005071 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005072 ssl->hostname_len = 0;
5073 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005074#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005075
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005076#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005077 if( ssl->psk != NULL )
5078 {
Paul Bakker34617722014-06-13 17:20:13 +02005079 polarssl_zeroize( ssl->psk, ssl->psk_len );
5080 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005081 polarssl_free( ssl->psk );
5082 polarssl_free( ssl->psk_identity );
5083 ssl->psk_len = 0;
5084 ssl->psk_identity_len = 0;
5085 }
5086#endif
5087
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005088#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005089 ssl_key_cert_free( ssl->key_cert );
5090#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005091
Paul Bakker05ef8352012-05-08 09:17:57 +00005092#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5093 if( ssl_hw_record_finish != NULL )
5094 {
5095 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5096 ssl_hw_record_finish( ssl );
5097 }
5098#endif
5099
Paul Bakker5121ce52009-01-03 21:22:43 +00005100 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005101
Paul Bakker86f04f42013-02-14 11:20:09 +01005102 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005103 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005104}
5105
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005106#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005107/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005108 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005109 */
5110unsigned char ssl_sig_from_pk( pk_context *pk )
5111{
5112#if defined(POLARSSL_RSA_C)
5113 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5114 return( SSL_SIG_RSA );
5115#endif
5116#if defined(POLARSSL_ECDSA_C)
5117 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5118 return( SSL_SIG_ECDSA );
5119#endif
5120 return( SSL_SIG_ANON );
5121}
5122
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005123pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5124{
5125 switch( sig )
5126 {
5127#if defined(POLARSSL_RSA_C)
5128 case SSL_SIG_RSA:
5129 return( POLARSSL_PK_RSA );
5130#endif
5131#if defined(POLARSSL_ECDSA_C)
5132 case SSL_SIG_ECDSA:
5133 return( POLARSSL_PK_ECDSA );
5134#endif
5135 default:
5136 return( POLARSSL_PK_NONE );
5137 }
5138}
Paul Bakker9af723c2014-05-01 13:03:14 +02005139#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005140
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005141/*
5142 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5143 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005144md_type_t ssl_md_alg_from_hash( unsigned char hash )
5145{
5146 switch( hash )
5147 {
5148#if defined(POLARSSL_MD5_C)
5149 case SSL_HASH_MD5:
5150 return( POLARSSL_MD_MD5 );
5151#endif
5152#if defined(POLARSSL_SHA1_C)
5153 case SSL_HASH_SHA1:
5154 return( POLARSSL_MD_SHA1 );
5155#endif
5156#if defined(POLARSSL_SHA256_C)
5157 case SSL_HASH_SHA224:
5158 return( POLARSSL_MD_SHA224 );
5159 case SSL_HASH_SHA256:
5160 return( POLARSSL_MD_SHA256 );
5161#endif
5162#if defined(POLARSSL_SHA512_C)
5163 case SSL_HASH_SHA384:
5164 return( POLARSSL_MD_SHA384 );
5165 case SSL_HASH_SHA512:
5166 return( POLARSSL_MD_SHA512 );
5167#endif
5168 default:
5169 return( POLARSSL_MD_NONE );
5170 }
5171}
5172
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005173#if defined(POLARSSL_SSL_SET_CURVES)
5174/*
5175 * Check is a curve proposed by the peer is in our list.
5176 * Return 1 if we're willing to use it, 0 otherwise.
5177 */
5178int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5179{
5180 const ecp_group_id *gid;
5181
5182 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5183 if( *gid == grp_id )
5184 return( 1 );
5185
5186 return( 0 );
5187}
Paul Bakker9af723c2014-05-01 13:03:14 +02005188#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005189
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005190#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005191int ssl_check_cert_usage( const x509_crt *cert,
5192 const ssl_ciphersuite_t *ciphersuite,
5193 int cert_endpoint )
5194{
5195#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5196 int usage = 0;
5197#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005198#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5199 const char *ext_oid;
5200 size_t ext_len;
5201#endif
5202
5203#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5204 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5205 ((void) cert);
5206 ((void) cert_endpoint);
5207#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005208
5209#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5210 if( cert_endpoint == SSL_IS_SERVER )
5211 {
5212 /* Server part of the key exchange */
5213 switch( ciphersuite->key_exchange )
5214 {
5215 case POLARSSL_KEY_EXCHANGE_RSA:
5216 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5217 usage = KU_KEY_ENCIPHERMENT;
5218 break;
5219
5220 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5221 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5222 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5223 usage = KU_DIGITAL_SIGNATURE;
5224 break;
5225
5226 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5227 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5228 usage = KU_KEY_AGREEMENT;
5229 break;
5230
5231 /* Don't use default: we want warnings when adding new values */
5232 case POLARSSL_KEY_EXCHANGE_NONE:
5233 case POLARSSL_KEY_EXCHANGE_PSK:
5234 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5235 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5236 usage = 0;
5237 }
5238 }
5239 else
5240 {
5241 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5242 usage = KU_DIGITAL_SIGNATURE;
5243 }
5244
5245 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5246 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005247#else
5248 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005249#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5250
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005251#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5252 if( cert_endpoint == SSL_IS_SERVER )
5253 {
5254 ext_oid = OID_SERVER_AUTH;
5255 ext_len = OID_SIZE( OID_SERVER_AUTH );
5256 }
5257 else
5258 {
5259 ext_oid = OID_CLIENT_AUTH;
5260 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5261 }
5262
5263 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5264 return( -1 );
5265#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5266
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005267 return( 0 );
5268}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005269#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005270
5271#endif /* POLARSSL_SSL_TLS_C */