blob: ac35289ab36312c80607049b11616d604f68fa67 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Mansour Moufid369e6c22015-02-15 17:35:38 -050095 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020096 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 {
Mansour Moufid369e6c22015-02-15 17:35:38 -0500114 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200115 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
Mansour Moufid369e6c22015-02-15 17:35:38 -05002750 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002751 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 {
Mansour Moufidbd1d44e2015-02-15 17:46:32 -05003547 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003548 }
Paul Bakker48916f92012-09-16 19:57:18 +00003549
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003550 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003551 {
Mansour Moufidbd1d44e2015-02-15 17:46:32 -05003552 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003553 }
Paul Bakker48916f92012-09-16 19:57:18 +00003554
Paul Bakker82788fb2014-10-20 13:59:19 +02003555 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003556 {
Mansour Moufidbd1d44e2015-02-15 17:46:32 -05003557 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003558 }
Paul Bakker48916f92012-09-16 19:57:18 +00003559
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003560 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003561 if( ssl->handshake == NULL ||
3562 ssl->transform_negotiate == NULL ||
3563 ssl->session_negotiate == NULL )
3564 {
3565 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003566
3567 polarssl_free( ssl->handshake );
3568 polarssl_free( ssl->transform_negotiate );
3569 polarssl_free( ssl->session_negotiate );
3570
3571 ssl->handshake = NULL;
3572 ssl->transform_negotiate = NULL;
3573 ssl->session_negotiate = NULL;
3574
Paul Bakker48916f92012-09-16 19:57:18 +00003575 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3576 }
3577
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003578 /* Initialize structures */
3579 ssl_session_init( ssl->session_negotiate );
3580 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003581 ssl_handshake_params_init( ssl->handshake );
3582
3583#if defined(POLARSSL_X509_CRT_PARSE_C)
3584 ssl->handshake->key_cert = ssl->key_cert;
3585#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003586
Paul Bakker48916f92012-09-16 19:57:18 +00003587 return( 0 );
3588}
3589
Paul Bakker5121ce52009-01-03 21:22:43 +00003590/*
3591 * Initialize an SSL context
3592 */
3593int ssl_init( ssl_context *ssl )
3594{
Paul Bakker48916f92012-09-16 19:57:18 +00003595 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 int len = SSL_BUFFER_LEN;
3597
3598 memset( ssl, 0, sizeof( ssl_context ) );
3599
Paul Bakker62f2dee2012-09-28 07:31:51 +00003600 /*
3601 * Sane defaults
3602 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003603 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3604 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3605 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3606 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003607
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003608 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003609
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003610#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003611 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003612 memset( ssl->renego_period, 0xFF, 7 );
3613 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003614#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003615
Paul Bakker62f2dee2012-09-28 07:31:51 +00003616#if defined(POLARSSL_DHM_C)
3617 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3618 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3619 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3620 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3621 {
3622 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3623 return( ret );
3624 }
3625#endif
3626
3627 /*
3628 * Prepare base structures
3629 */
Mansour Moufid369e6c22015-02-15 17:35:38 -05003630 ssl->in_ctr = polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003632 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003633 ssl->in_msg = ssl->in_ctr + 13;
3634
3635 if( ssl->in_ctr == NULL )
3636 {
3637 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003638 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 }
3640
Mansour Moufid369e6c22015-02-15 17:35:38 -05003641 ssl->out_ctr = polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003643 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003644 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
3646 if( ssl->out_ctr == NULL )
3647 {
3648 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003649 polarssl_free( ssl->in_ctr );
3650 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003651 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 }
3653
3654 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3655 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3656
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003657#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3658 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3659#endif
3660
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003661#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3662 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3663#endif
3664
Paul Bakker606b4ba2013-08-14 16:52:14 +02003665#if defined(POLARSSL_SSL_SESSION_TICKETS)
3666 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3667#endif
3668
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003669#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003670 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003671#endif
3672
Paul Bakker48916f92012-09-16 19:57:18 +00003673 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3674 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003675
3676 return( 0 );
3677}
3678
3679/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003680 * Reset an initialized and used SSL context for re-use while retaining
3681 * all application-set variables, function pointers and data.
3682 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003683int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003684{
Paul Bakker48916f92012-09-16 19:57:18 +00003685 int ret;
3686
Paul Bakker7eb013f2011-10-06 12:37:39 +00003687 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003688
3689#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003690 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003691 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003692
3693 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003694 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3695 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003696#endif
3697 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003698
Paul Bakker7eb013f2011-10-06 12:37:39 +00003699 ssl->in_offt = NULL;
3700
Paul Bakker92be97b2013-01-02 17:30:03 +01003701 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003702 ssl->in_msgtype = 0;
3703 ssl->in_msglen = 0;
3704 ssl->in_left = 0;
3705
3706 ssl->in_hslen = 0;
3707 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003708 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003709
Paul Bakker92be97b2013-01-02 17:30:03 +01003710 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003711 ssl->out_msgtype = 0;
3712 ssl->out_msglen = 0;
3713 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003714#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003715 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3716 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003717#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003718
Paul Bakker48916f92012-09-16 19:57:18 +00003719 ssl->transform_in = NULL;
3720 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003721
3722 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3723 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003724
3725#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003726 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003727 {
3728 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003729 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003730 {
3731 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3732 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3733 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003734 }
3735#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003736
Paul Bakker48916f92012-09-16 19:57:18 +00003737 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003738 {
Paul Bakker48916f92012-09-16 19:57:18 +00003739 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003740 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003741 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003742 }
Paul Bakker48916f92012-09-16 19:57:18 +00003743
Paul Bakkerc0463502013-02-14 11:19:38 +01003744 if( ssl->session )
3745 {
3746 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003747 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003748 ssl->session = NULL;
3749 }
3750
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003751#if defined(POLARSSL_SSL_ALPN)
3752 ssl->alpn_chosen = NULL;
3753#endif
3754
Paul Bakker48916f92012-09-16 19:57:18 +00003755 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3756 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003757
3758 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003759}
3760
Paul Bakkera503a632013-08-14 13:48:06 +02003761#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003762static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3763{
3764 aes_free( &tkeys->enc );
3765 aes_free( &tkeys->dec );
3766
3767 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3768}
3769
Paul Bakker7eb013f2011-10-06 12:37:39 +00003770/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003771 * Allocate and initialize ticket keys
3772 */
3773static int ssl_ticket_keys_init( ssl_context *ssl )
3774{
3775 int ret;
3776 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003777 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003778
3779 if( ssl->ticket_keys != NULL )
3780 return( 0 );
3781
Mansour Moufid369e6c22015-02-15 17:35:38 -05003782 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003783 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003784 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3785
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003786 aes_init( &tkeys->enc );
3787 aes_init( &tkeys->dec );
3788
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003789 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003790 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003791 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003792 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003793 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003794 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003795
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003796 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3797 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3798 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3799 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003800 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003801 polarssl_free( tkeys );
3802 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003803 }
3804
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003805 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003806 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003807 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003808 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003809 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003810 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003811
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003812 ssl->ticket_keys = tkeys;
3813
3814 return( 0 );
3815}
Paul Bakkera503a632013-08-14 13:48:06 +02003816#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003817
3818/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003819 * SSL set accessors
3820 */
3821void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3822{
3823 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003824
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003825#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3826 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003827 if( endpoint == SSL_IS_CLIENT )
3828 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003829#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003830
3831#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3832 if( endpoint == SSL_IS_SERVER )
3833 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3834#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003835}
3836
3837void ssl_set_authmode( ssl_context *ssl, int authmode )
3838{
3839 ssl->authmode = authmode;
3840}
3841
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003842#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003843void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003844 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003845 void *p_vrfy )
3846{
3847 ssl->f_vrfy = f_vrfy;
3848 ssl->p_vrfy = p_vrfy;
3849}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003850#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003851
Paul Bakker5121ce52009-01-03 21:22:43 +00003852void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003853 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 void *p_rng )
3855{
3856 ssl->f_rng = f_rng;
3857 ssl->p_rng = p_rng;
3858}
3859
3860void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003861 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003862 void *p_dbg )
3863{
3864 ssl->f_dbg = f_dbg;
3865 ssl->p_dbg = p_dbg;
3866}
3867
3868void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003869 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003870 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003871{
3872 ssl->f_recv = f_recv;
3873 ssl->f_send = f_send;
3874 ssl->p_recv = p_recv;
3875 ssl->p_send = p_send;
3876}
3877
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003878#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003879void ssl_set_session_cache( ssl_context *ssl,
3880 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3881 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003882{
Paul Bakker0a597072012-09-25 21:55:46 +00003883 ssl->f_get_cache = f_get_cache;
3884 ssl->p_get_cache = p_get_cache;
3885 ssl->f_set_cache = f_set_cache;
3886 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003887}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003888#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003889
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003890#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003891int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003892{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003893 int ret;
3894
3895 if( ssl == NULL ||
3896 session == NULL ||
3897 ssl->session_negotiate == NULL ||
3898 ssl->endpoint != SSL_IS_CLIENT )
3899 {
3900 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3901 }
3902
3903 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3904 return( ret );
3905
Paul Bakker0a597072012-09-25 21:55:46 +00003906 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003907
3908 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003909}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003910#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003911
Paul Bakkerb68cad62012-08-23 08:34:18 +00003912void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003913{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003914 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3915 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3916 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3917 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3918}
3919
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003920void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3921 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003922 int major, int minor )
3923{
3924 if( major != SSL_MAJOR_VERSION_3 )
3925 return;
3926
3927 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3928 return;
3929
3930 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003931}
3932
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003933#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003934/* Add a new (empty) key_cert entry an return a pointer to it */
3935static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3936{
3937 ssl_key_cert *key_cert, *last;
3938
Mansour Moufid369e6c22015-02-15 17:35:38 -05003939 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003940 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003941 return( NULL );
3942
3943 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3944
3945 /* Append the new key_cert to the (possibly empty) current list */
3946 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003947 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003948 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003949 if( ssl->handshake != NULL )
3950 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003951 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003952 else
3953 {
3954 last = ssl->key_cert;
3955 while( last->next != NULL )
3956 last = last->next;
3957 last->next = key_cert;
3958 }
3959
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003960 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003961}
3962
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003963void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003964 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003965{
3966 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003967 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 ssl->peer_cn = peer_cn;
3969}
3970
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003971int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003972 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003973{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003974 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3975
3976 if( key_cert == NULL )
3977 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3978
3979 key_cert->cert = own_cert;
3980 key_cert->key = pk_key;
3981
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003982 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003983}
3984
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003985#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003986int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003987 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003988{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003989 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003990 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003991
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003992 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003993 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3994
Mansour Moufid369e6c22015-02-15 17:35:38 -05003995 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003996 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003997 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003998
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003999 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004000
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004001 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004002 if( ret != 0 )
4003 return( ret );
4004
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004005 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004006 return( ret );
4007
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004008 key_cert->cert = own_cert;
4009 key_cert->key_own_alloc = 1;
4010
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01004011 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004012}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004013#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004014
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004015int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004016 void *rsa_key,
4017 rsa_decrypt_func rsa_decrypt,
4018 rsa_sign_func rsa_sign,
4019 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004020{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004021 int ret;
4022 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004023
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004024 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004025 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4026
Mansour Moufid369e6c22015-02-15 17:35:38 -05004027 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004028 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004029 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004030
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004031 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004032
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004033 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4034 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4035 return( ret );
4036
4037 key_cert->cert = own_cert;
4038 key_cert->key_own_alloc = 1;
4039
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01004040 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00004041}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004042#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004043
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004044#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004045int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4046 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004047{
Paul Bakker6db455e2013-09-18 17:29:31 +02004048 if( psk == NULL || psk_identity == NULL )
4049 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4050
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004051 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004052 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4053
Paul Bakker6db455e2013-09-18 17:29:31 +02004054 if( ssl->psk != NULL )
4055 {
4056 polarssl_free( ssl->psk );
4057 polarssl_free( ssl->psk_identity );
4058 }
4059
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004060 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004061 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004062
Mansour Moufid369e6c22015-02-15 17:35:38 -05004063 ssl->psk = polarssl_malloc( ssl->psk_len );
Mansour Moufidbd1d44e2015-02-15 17:46:32 -05004064 ssl->psk_identity = polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004065
4066 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4067 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4068
4069 memcpy( ssl->psk, psk, ssl->psk_len );
4070 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004071
4072 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004073}
4074
4075void ssl_set_psk_cb( ssl_context *ssl,
4076 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4077 size_t),
4078 void *p_psk )
4079{
4080 ssl->f_psk = f_psk;
4081 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004082}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004083#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004084
Paul Bakker48916f92012-09-16 19:57:18 +00004085#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004086int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004087{
4088 int ret;
4089
Paul Bakker48916f92012-09-16 19:57:18 +00004090 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 {
4092 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4093 return( ret );
4094 }
4095
Paul Bakker48916f92012-09-16 19:57:18 +00004096 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004097 {
4098 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4099 return( ret );
4100 }
4101
4102 return( 0 );
4103}
4104
Paul Bakker1b57b062011-01-06 15:48:19 +00004105int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4106{
4107 int ret;
4108
Paul Bakker66d5d072014-06-17 16:39:18 +02004109 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004110 {
4111 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4112 return( ret );
4113 }
4114
Paul Bakker66d5d072014-06-17 16:39:18 +02004115 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004116 {
4117 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4118 return( ret );
4119 }
4120
4121 return( 0 );
4122}
Paul Bakker48916f92012-09-16 19:57:18 +00004123#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004124
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004125#if defined(POLARSSL_SSL_SET_CURVES)
4126/*
4127 * Set the allowed elliptic curves
4128 */
4129void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4130{
4131 ssl->curve_list = curve_list;
4132}
4133#endif
4134
Paul Bakker0be444a2013-08-27 21:55:01 +02004135#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004136int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004137{
4138 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004139 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004140
4141 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004142
4143 if( ssl->hostname_len + 1 == 0 )
4144 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4145
Mansour Moufid369e6c22015-02-15 17:35:38 -05004146 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004147
Paul Bakkerb15b8512012-01-13 13:44:06 +00004148 if( ssl->hostname == NULL )
4149 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4150
Paul Bakker3c2122f2013-06-24 19:03:14 +02004151 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004152 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004153
Paul Bakker40ea7de2009-05-03 10:18:48 +00004154 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004155
4156 return( 0 );
4157}
4158
Paul Bakker5701cdc2012-09-27 21:49:42 +00004159void ssl_set_sni( ssl_context *ssl,
4160 int (*f_sni)(void *, ssl_context *,
4161 const unsigned char *, size_t),
4162 void *p_sni )
4163{
4164 ssl->f_sni = f_sni;
4165 ssl->p_sni = p_sni;
4166}
Paul Bakker0be444a2013-08-27 21:55:01 +02004167#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004168
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004169#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004170int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004171{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004172 size_t cur_len, tot_len;
4173 const char **p;
4174
4175 /*
4176 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4177 * truncated". Check lengths now rather than later.
4178 */
4179 tot_len = 0;
4180 for( p = protos; *p != NULL; p++ )
4181 {
4182 cur_len = strlen( *p );
4183 tot_len += cur_len;
4184
4185 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4186 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4187 }
4188
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004189 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004190
4191 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004192}
4193
4194const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4195{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004196 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004197}
4198#endif /* POLARSSL_SSL_ALPN */
4199
Paul Bakker490ecc82011-10-06 13:04:09 +00004200void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4201{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004202 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4203 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4204 {
4205 ssl->max_major_ver = major;
4206 ssl->max_minor_ver = minor;
4207 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004208}
4209
Paul Bakker1d29fb52012-09-28 13:28:45 +00004210void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4211{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004212 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4213 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4214 {
4215 ssl->min_major_ver = major;
4216 ssl->min_minor_ver = minor;
4217 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004218}
4219
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004220#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4221void ssl_set_fallback( ssl_context *ssl, char fallback )
4222{
4223 ssl->fallback = fallback;
4224}
4225#endif
4226
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004227#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4228void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4229{
4230 ssl->encrypt_then_mac = etm;
4231}
4232#endif
4233
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004234#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4235void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4236{
4237 ssl->extended_ms = ems;
4238}
4239#endif
4240
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004241void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4242{
4243 ssl->arc4_disabled = arc4;
4244}
4245
Paul Bakker05decb22013-08-15 13:33:48 +02004246#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004247int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4248{
Paul Bakker77e257e2013-12-16 15:29:52 +01004249 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004250 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004251 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004252 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004253 }
4254
4255 ssl->mfl_code = mfl_code;
4256
4257 return( 0 );
4258}
Paul Bakker05decb22013-08-15 13:33:48 +02004259#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004260
Paul Bakker1f2bc622013-08-15 13:45:55 +02004261#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004262int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004263{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004264 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004265
4266 return( 0 );
4267}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004268#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004269
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004270#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4271void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4272{
4273 ssl->split_done = split;
4274}
4275#endif
4276
Paul Bakker48916f92012-09-16 19:57:18 +00004277void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4278{
4279 ssl->allow_legacy_renegotiation = allow_legacy;
4280}
4281
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004282#if defined(POLARSSL_SSL_RENEGOTIATION)
4283void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4284{
4285 ssl->disable_renegotiation = renegotiation;
4286}
4287
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004288void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4289{
4290 ssl->renego_max_records = max_records;
4291}
4292
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004293void ssl_set_renegotiation_period( ssl_context *ssl,
4294 const unsigned char period[8] )
4295{
4296 memcpy( ssl->renego_period, period, 8 );
4297}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004298#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004299
Paul Bakkera503a632013-08-14 13:48:06 +02004300#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004301int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4302{
4303 ssl->session_tickets = use_tickets;
4304
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004305#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004306 if( ssl->endpoint == SSL_IS_CLIENT )
4307 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004308#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004309
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004310 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4311 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004312
4313 if( ssl->f_rng == NULL )
4314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4315
4316 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004317}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004318
4319void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4320{
4321 ssl->ticket_lifetime = lifetime;
4322}
Paul Bakkera503a632013-08-14 13:48:06 +02004323#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004324
Paul Bakker5121ce52009-01-03 21:22:43 +00004325/*
4326 * SSL get accessors
4327 */
4328size_t ssl_get_bytes_avail( const ssl_context *ssl )
4329{
4330 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4331}
4332
Paul Bakkerff60ee62010-03-16 21:09:09 +00004333int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004334{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004335 if( ssl->session != NULL )
4336 return( ssl->session->verify_result );
4337
4338 if( ssl->session_negotiate != NULL )
4339 return( ssl->session_negotiate->verify_result );
4340
4341 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004342}
4343
Paul Bakkere3166ce2011-01-27 17:40:50 +00004344const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004345{
Paul Bakker926c8e42013-03-06 10:23:34 +01004346 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004347 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004348
Paul Bakkere3166ce2011-01-27 17:40:50 +00004349 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004350}
4351
Paul Bakker43ca69c2011-01-15 17:35:19 +00004352const char *ssl_get_version( const ssl_context *ssl )
4353{
4354 switch( ssl->minor_ver )
4355 {
4356 case SSL_MINOR_VERSION_0:
4357 return( "SSLv3.0" );
4358
4359 case SSL_MINOR_VERSION_1:
4360 return( "TLSv1.0" );
4361
4362 case SSL_MINOR_VERSION_2:
4363 return( "TLSv1.1" );
4364
Paul Bakker1ef83d62012-04-11 12:09:53 +00004365 case SSL_MINOR_VERSION_3:
4366 return( "TLSv1.2" );
4367
Paul Bakker43ca69c2011-01-15 17:35:19 +00004368 default:
4369 break;
4370 }
4371 return( "unknown" );
4372}
4373
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004374#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004375const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004376{
4377 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004378 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004379
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004380 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004381}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004382#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004383
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004384#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004385int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4386{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004387 if( ssl == NULL ||
4388 dst == NULL ||
4389 ssl->session == NULL ||
4390 ssl->endpoint != SSL_IS_CLIENT )
4391 {
4392 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4393 }
4394
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004395 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004396}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004397#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004398
Paul Bakker5121ce52009-01-03 21:22:43 +00004399/*
Paul Bakker1961b702013-01-25 14:49:24 +01004400 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004401 */
Paul Bakker1961b702013-01-25 14:49:24 +01004402int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004403{
Paul Bakker40e46942009-01-03 21:51:57 +00004404 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004405
Paul Bakker40e46942009-01-03 21:51:57 +00004406#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004407 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004408 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004409#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004410#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004411 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004412 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004413#endif
4414
Paul Bakker1961b702013-01-25 14:49:24 +01004415 return( ret );
4416}
4417
4418/*
4419 * Perform the SSL handshake
4420 */
4421int ssl_handshake( ssl_context *ssl )
4422{
4423 int ret = 0;
4424
4425 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4426
4427 while( ssl->state != SSL_HANDSHAKE_OVER )
4428 {
4429 ret = ssl_handshake_step( ssl );
4430
4431 if( ret != 0 )
4432 break;
4433 }
4434
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4436
4437 return( ret );
4438}
4439
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004440#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004441#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004442/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004443 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004444 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004445static int ssl_write_hello_request( ssl_context *ssl )
4446{
4447 int ret;
4448
4449 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4450
4451 ssl->out_msglen = 4;
4452 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4453 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4454
4455 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4456 {
4457 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4458 return( ret );
4459 }
4460
4461 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4462
4463 return( 0 );
4464}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004465#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004466
4467/*
4468 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004469 * - any side: calling ssl_renegotiate(),
4470 * - client: receiving a HelloRequest during ssl_read(),
4471 * - server: receiving any handshake message on server during ssl_read() after
4472 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004473 * If the handshake doesn't complete due to waiting for I/O, it will continue
4474 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004475 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004476static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004477{
4478 int ret;
4479
4480 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4481
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004482 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4483 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004484
4485 ssl->state = SSL_HELLO_REQUEST;
4486 ssl->renegotiation = SSL_RENEGOTIATION;
4487
Paul Bakker48916f92012-09-16 19:57:18 +00004488 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4489 {
4490 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4491 return( ret );
4492 }
4493
4494 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4495
4496 return( 0 );
4497}
4498
4499/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004500 * Renegotiate current connection on client,
4501 * or request renegotiation on server
4502 */
4503int ssl_renegotiate( ssl_context *ssl )
4504{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004505 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004506
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004507#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004508 /* On server, just send the request */
4509 if( ssl->endpoint == SSL_IS_SERVER )
4510 {
4511 if( ssl->state != SSL_HANDSHAKE_OVER )
4512 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4513
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004514 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4515
4516 /* Did we already try/start sending HelloRequest? */
4517 if( ssl->out_left != 0 )
4518 return( ssl_flush_output( ssl ) );
4519
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004520 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004521 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004522#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004523
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004524#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004525 /*
4526 * On client, either start the renegotiation process or,
4527 * if already in progress, continue the handshake
4528 */
4529 if( ssl->renegotiation != SSL_RENEGOTIATION )
4530 {
4531 if( ssl->state != SSL_HANDSHAKE_OVER )
4532 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4533
4534 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4535 {
4536 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4537 return( ret );
4538 }
4539 }
4540 else
4541 {
4542 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4543 {
4544 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4545 return( ret );
4546 }
4547 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004548#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004549
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004550 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004551}
4552
4553/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004554 * Check record counters and renegotiate if they're above the limit.
4555 */
4556static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4557{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004558 if( ssl->state != SSL_HANDSHAKE_OVER ||
4559 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4560 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4561 {
4562 return( 0 );
4563 }
4564
4565 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004566 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4567 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004568 {
4569 return( 0 );
4570 }
4571
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004572 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004573 return( ssl_renegotiate( ssl ) );
4574}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004575#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004576
4577/*
4578 * Receive application data decrypted from the SSL layer
4579 */
Paul Bakker23986e52011-04-24 08:57:21 +00004580int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004581{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004582 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004583 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004584
4585 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4586
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004587#if defined(POLARSSL_SSL_RENEGOTIATION)
4588 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4589 {
4590 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4591 return( ret );
4592 }
4593#endif
4594
Paul Bakker5121ce52009-01-03 21:22:43 +00004595 if( ssl->state != SSL_HANDSHAKE_OVER )
4596 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004597 ret = ssl_handshake( ssl );
4598 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4599 {
4600 record_read = 1;
4601 }
4602 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004603 {
4604 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4605 return( ret );
4606 }
4607 }
4608
4609 if( ssl->in_offt == NULL )
4610 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004611 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004612 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004613 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4614 {
4615 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4616 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004617
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004618 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4619 return( ret );
4620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 }
4622
4623 if( ssl->in_msglen == 0 &&
4624 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4625 {
4626 /*
4627 * OpenSSL sends empty messages to randomize the IV
4628 */
4629 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4630 {
Paul Bakker831a7552011-05-18 13:32:51 +00004631 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4632 return( 0 );
4633
Paul Bakker5121ce52009-01-03 21:22:43 +00004634 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4635 return( ret );
4636 }
4637 }
4638
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004639#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004640 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4641 {
4642 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4643
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004644#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004645 if( ssl->endpoint == SSL_IS_CLIENT &&
4646 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4647 ssl->in_hslen != 4 ) )
4648 {
4649 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4650 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4651 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004652#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004653
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004654 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4655 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004656 ssl->allow_legacy_renegotiation ==
4657 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004658 {
4659 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4660
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004661#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004662 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004663 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004664 /*
4665 * SSLv3 does not have a "no_renegotiation" alert
4666 */
4667 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4668 return( ret );
4669 }
4670 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004671#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004672#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4673 defined(POLARSSL_SSL_PROTO_TLS1_2)
4674 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004675 {
4676 if( ( ret = ssl_send_alert_message( ssl,
4677 SSL_ALERT_LEVEL_WARNING,
4678 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4679 {
4680 return( ret );
4681 }
Paul Bakker48916f92012-09-16 19:57:18 +00004682 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004683 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004684#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4685 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004686 {
4687 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004688 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004689 }
Paul Bakker48916f92012-09-16 19:57:18 +00004690 }
4691 else
4692 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004693 ret = ssl_start_renegotiation( ssl );
4694 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4695 {
4696 record_read = 1;
4697 }
4698 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004699 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004700 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004701 return( ret );
4702 }
Paul Bakker48916f92012-09-16 19:57:18 +00004703 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004704
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004705 /* If a non-handshake record was read during renego, fallthrough,
4706 * else tell the user they should call ssl_read() again */
4707 if( ! record_read )
4708 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004709 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004710 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4711 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004712 ssl->renego_records_seen++;
4713
4714 if( ssl->renego_max_records >= 0 &&
4715 ssl->renego_records_seen > ssl->renego_max_records )
4716 {
4717 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4718 "but not honored by client" ) );
4719 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4720 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004721 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004722#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004723
4724 /* Fatal and closure alerts handled by ssl_read_record() */
4725 if( ssl->in_msgtype == SSL_MSG_ALERT )
4726 {
4727 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4728 return( POLARSSL_ERR_NET_WANT_READ );
4729 }
4730
4731 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004732 {
4733 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004734 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004735 }
4736
4737 ssl->in_offt = ssl->in_msg;
4738 }
4739
4740 n = ( len < ssl->in_msglen )
4741 ? len : ssl->in_msglen;
4742
4743 memcpy( buf, ssl->in_offt, n );
4744 ssl->in_msglen -= n;
4745
4746 if( ssl->in_msglen == 0 )
4747 /* all bytes consumed */
4748 ssl->in_offt = NULL;
4749 else
4750 /* more data available */
4751 ssl->in_offt += n;
4752
4753 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4754
Paul Bakker23986e52011-04-24 08:57:21 +00004755 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004756}
4757
4758/*
4759 * Send application data to be encrypted by the SSL layer
4760 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004761#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4762static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
4763#else
Paul Bakker23986e52011-04-24 08:57:21 +00004764int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004765#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004766{
Paul Bakker23986e52011-04-24 08:57:21 +00004767 int ret;
4768 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004769 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004770
4771 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4772
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004773#if defined(POLARSSL_SSL_RENEGOTIATION)
4774 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4775 {
4776 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4777 return( ret );
4778 }
4779#endif
4780
Paul Bakker5121ce52009-01-03 21:22:43 +00004781 if( ssl->state != SSL_HANDSHAKE_OVER )
4782 {
4783 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4784 {
4785 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4786 return( ret );
4787 }
4788 }
4789
Paul Bakker05decb22013-08-15 13:33:48 +02004790#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004791 /*
4792 * Assume mfl_code is correct since it was checked when set
4793 */
4794 max_len = mfl_code_to_length[ssl->mfl_code];
4795
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004796 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004797 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004798 */
4799 if( ssl->session_out != NULL &&
4800 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4801 {
4802 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4803 }
Paul Bakker05decb22013-08-15 13:33:48 +02004804#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004805
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004806 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004807
Paul Bakker5121ce52009-01-03 21:22:43 +00004808 if( ssl->out_left != 0 )
4809 {
4810 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4811 {
4812 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4813 return( ret );
4814 }
4815 }
Paul Bakker887bd502011-06-08 13:10:54 +00004816 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004817 {
Paul Bakker887bd502011-06-08 13:10:54 +00004818 ssl->out_msglen = n;
4819 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4820 memcpy( ssl->out_msg, buf, n );
4821
4822 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4823 {
4824 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4825 return( ret );
4826 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004827 }
4828
4829 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4830
Paul Bakker23986e52011-04-24 08:57:21 +00004831 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004832}
4833
4834/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004835 * Write application data, doing 1/n-1 splitting if necessary.
4836 *
4837 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004838 * then the caller will call us again with the same arguments, so
4839 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004840 */
4841#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4842int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4843{
4844 int ret;
4845
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004846 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4847 len <= 1 ||
4848 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004849 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4850 != POLARSSL_MODE_CBC )
4851 {
4852 return( ssl_write_real( ssl, buf, len ) );
4853 }
4854
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004855 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004856 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004857 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004858 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004859 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004860 }
4861
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004862 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4863 return( ret );
4864 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004865
4866 return( ret + 1 );
4867}
4868#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4869
4870/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004871 * Notify the peer that the connection is being closed
4872 */
4873int ssl_close_notify( ssl_context *ssl )
4874{
4875 int ret;
4876
4877 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4878
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004879 if( ssl->out_left != 0 )
4880 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004881
4882 if( ssl->state == SSL_HANDSHAKE_OVER )
4883 {
Paul Bakker48916f92012-09-16 19:57:18 +00004884 if( ( ret = ssl_send_alert_message( ssl,
4885 SSL_ALERT_LEVEL_WARNING,
4886 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004887 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004888 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004889 return( ret );
4890 }
4891 }
4892
4893 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4894
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004895 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004896}
4897
Paul Bakker48916f92012-09-16 19:57:18 +00004898void ssl_transform_free( ssl_transform *transform )
4899{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004900 if( transform == NULL )
4901 return;
4902
Paul Bakker48916f92012-09-16 19:57:18 +00004903#if defined(POLARSSL_ZLIB_SUPPORT)
4904 deflateEnd( &transform->ctx_deflate );
4905 inflateEnd( &transform->ctx_inflate );
4906#endif
4907
Paul Bakker84bbeb52014-07-01 14:53:22 +02004908 cipher_free( &transform->cipher_ctx_enc );
4909 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004910
Paul Bakker84bbeb52014-07-01 14:53:22 +02004911 md_free( &transform->md_ctx_enc );
4912 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004913
Paul Bakker34617722014-06-13 17:20:13 +02004914 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004915}
4916
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004917#if defined(POLARSSL_X509_CRT_PARSE_C)
4918static void ssl_key_cert_free( ssl_key_cert *key_cert )
4919{
4920 ssl_key_cert *cur = key_cert, *next;
4921
4922 while( cur != NULL )
4923 {
4924 next = cur->next;
4925
4926 if( cur->key_own_alloc )
4927 {
4928 pk_free( cur->key );
4929 polarssl_free( cur->key );
4930 }
4931 polarssl_free( cur );
4932
4933 cur = next;
4934 }
4935}
4936#endif /* POLARSSL_X509_CRT_PARSE_C */
4937
Paul Bakker48916f92012-09-16 19:57:18 +00004938void ssl_handshake_free( ssl_handshake_params *handshake )
4939{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004940 if( handshake == NULL )
4941 return;
4942
Paul Bakker48916f92012-09-16 19:57:18 +00004943#if defined(POLARSSL_DHM_C)
4944 dhm_free( &handshake->dhm_ctx );
4945#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004946#if defined(POLARSSL_ECDH_C)
4947 ecdh_free( &handshake->ecdh_ctx );
4948#endif
4949
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004950#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004951 /* explicit void pointer cast for buggy MS compiler */
4952 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004953#endif
4954
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004955#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4956 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4957 /*
4958 * Free only the linked list wrapper, not the keys themselves
4959 * since the belong to the SNI callback
4960 */
4961 if( handshake->sni_key_cert != NULL )
4962 {
4963 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4964
4965 while( cur != NULL )
4966 {
4967 next = cur->next;
4968 polarssl_free( cur );
4969 cur = next;
4970 }
4971 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004972#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004973
Paul Bakker34617722014-06-13 17:20:13 +02004974 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004975}
4976
4977void ssl_session_free( ssl_session *session )
4978{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004979 if( session == NULL )
4980 return;
4981
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004982#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004983 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004984 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004985 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004986 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004987 }
Paul Bakkered27a042013-04-18 22:46:23 +02004988#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004989
Paul Bakkera503a632013-08-14 13:48:06 +02004990#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004991 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004992#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004993
Paul Bakker34617722014-06-13 17:20:13 +02004994 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004995}
4996
Paul Bakker5121ce52009-01-03 21:22:43 +00004997/*
4998 * Free an SSL context
4999 */
5000void ssl_free( ssl_context *ssl )
5001{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005002 if( ssl == NULL )
5003 return;
5004
Paul Bakker5121ce52009-01-03 21:22:43 +00005005 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5006
Paul Bakker5121ce52009-01-03 21:22:43 +00005007 if( ssl->out_ctr != NULL )
5008 {
Paul Bakker34617722014-06-13 17:20:13 +02005009 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005010 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005011 }
5012
5013 if( ssl->in_ctr != NULL )
5014 {
Paul Bakker34617722014-06-13 17:20:13 +02005015 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005016 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005017 }
5018
Paul Bakker16770332013-10-11 09:59:44 +02005019#if defined(POLARSSL_ZLIB_SUPPORT)
5020 if( ssl->compress_buf != NULL )
5021 {
Paul Bakker34617722014-06-13 17:20:13 +02005022 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005023 polarssl_free( ssl->compress_buf );
5024 }
5025#endif
5026
Paul Bakker40e46942009-01-03 21:51:57 +00005027#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005028 mpi_free( &ssl->dhm_P );
5029 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005030#endif
5031
Paul Bakker48916f92012-09-16 19:57:18 +00005032 if( ssl->transform )
5033 {
5034 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005035 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005036 }
5037
5038 if( ssl->handshake )
5039 {
5040 ssl_handshake_free( ssl->handshake );
5041 ssl_transform_free( ssl->transform_negotiate );
5042 ssl_session_free( ssl->session_negotiate );
5043
Paul Bakker6e339b52013-07-03 13:37:05 +02005044 polarssl_free( ssl->handshake );
5045 polarssl_free( ssl->transform_negotiate );
5046 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005047 }
5048
Paul Bakkerc0463502013-02-14 11:19:38 +01005049 if( ssl->session )
5050 {
5051 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005052 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005053 }
5054
Paul Bakkera503a632013-08-14 13:48:06 +02005055#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005056 if( ssl->ticket_keys )
5057 {
5058 ssl_ticket_keys_free( ssl->ticket_keys );
5059 polarssl_free( ssl->ticket_keys );
5060 }
Paul Bakkera503a632013-08-14 13:48:06 +02005061#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005062
Paul Bakker0be444a2013-08-27 21:55:01 +02005063#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005064 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005065 {
Paul Bakker34617722014-06-13 17:20:13 +02005066 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005067 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005068 ssl->hostname_len = 0;
5069 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005071
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005072#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005073 if( ssl->psk != NULL )
5074 {
Paul Bakker34617722014-06-13 17:20:13 +02005075 polarssl_zeroize( ssl->psk, ssl->psk_len );
5076 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005077 polarssl_free( ssl->psk );
5078 polarssl_free( ssl->psk_identity );
5079 ssl->psk_len = 0;
5080 ssl->psk_identity_len = 0;
5081 }
5082#endif
5083
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005084#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005085 ssl_key_cert_free( ssl->key_cert );
5086#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005087
Paul Bakker05ef8352012-05-08 09:17:57 +00005088#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5089 if( ssl_hw_record_finish != NULL )
5090 {
5091 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5092 ssl_hw_record_finish( ssl );
5093 }
5094#endif
5095
Paul Bakker5121ce52009-01-03 21:22:43 +00005096 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005097
Paul Bakker86f04f42013-02-14 11:20:09 +01005098 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005099 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005100}
5101
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005102#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005103/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005104 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005105 */
5106unsigned char ssl_sig_from_pk( pk_context *pk )
5107{
5108#if defined(POLARSSL_RSA_C)
5109 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5110 return( SSL_SIG_RSA );
5111#endif
5112#if defined(POLARSSL_ECDSA_C)
5113 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5114 return( SSL_SIG_ECDSA );
5115#endif
5116 return( SSL_SIG_ANON );
5117}
5118
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005119pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5120{
5121 switch( sig )
5122 {
5123#if defined(POLARSSL_RSA_C)
5124 case SSL_SIG_RSA:
5125 return( POLARSSL_PK_RSA );
5126#endif
5127#if defined(POLARSSL_ECDSA_C)
5128 case SSL_SIG_ECDSA:
5129 return( POLARSSL_PK_ECDSA );
5130#endif
5131 default:
5132 return( POLARSSL_PK_NONE );
5133 }
5134}
Paul Bakker9af723c2014-05-01 13:03:14 +02005135#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005136
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005137/*
5138 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5139 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005140md_type_t ssl_md_alg_from_hash( unsigned char hash )
5141{
5142 switch( hash )
5143 {
5144#if defined(POLARSSL_MD5_C)
5145 case SSL_HASH_MD5:
5146 return( POLARSSL_MD_MD5 );
5147#endif
5148#if defined(POLARSSL_SHA1_C)
5149 case SSL_HASH_SHA1:
5150 return( POLARSSL_MD_SHA1 );
5151#endif
5152#if defined(POLARSSL_SHA256_C)
5153 case SSL_HASH_SHA224:
5154 return( POLARSSL_MD_SHA224 );
5155 case SSL_HASH_SHA256:
5156 return( POLARSSL_MD_SHA256 );
5157#endif
5158#if defined(POLARSSL_SHA512_C)
5159 case SSL_HASH_SHA384:
5160 return( POLARSSL_MD_SHA384 );
5161 case SSL_HASH_SHA512:
5162 return( POLARSSL_MD_SHA512 );
5163#endif
5164 default:
5165 return( POLARSSL_MD_NONE );
5166 }
5167}
5168
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005169#if defined(POLARSSL_SSL_SET_CURVES)
5170/*
5171 * Check is a curve proposed by the peer is in our list.
5172 * Return 1 if we're willing to use it, 0 otherwise.
5173 */
5174int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5175{
5176 const ecp_group_id *gid;
5177
5178 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5179 if( *gid == grp_id )
5180 return( 1 );
5181
5182 return( 0 );
5183}
Paul Bakker9af723c2014-05-01 13:03:14 +02005184#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005185
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005186#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005187int ssl_check_cert_usage( const x509_crt *cert,
5188 const ssl_ciphersuite_t *ciphersuite,
5189 int cert_endpoint )
5190{
5191#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5192 int usage = 0;
5193#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005194#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5195 const char *ext_oid;
5196 size_t ext_len;
5197#endif
5198
5199#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5200 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5201 ((void) cert);
5202 ((void) cert_endpoint);
5203#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005204
5205#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5206 if( cert_endpoint == SSL_IS_SERVER )
5207 {
5208 /* Server part of the key exchange */
5209 switch( ciphersuite->key_exchange )
5210 {
5211 case POLARSSL_KEY_EXCHANGE_RSA:
5212 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5213 usage = KU_KEY_ENCIPHERMENT;
5214 break;
5215
5216 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5217 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5218 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5219 usage = KU_DIGITAL_SIGNATURE;
5220 break;
5221
5222 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5223 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5224 usage = KU_KEY_AGREEMENT;
5225 break;
5226
5227 /* Don't use default: we want warnings when adding new values */
5228 case POLARSSL_KEY_EXCHANGE_NONE:
5229 case POLARSSL_KEY_EXCHANGE_PSK:
5230 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5231 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5232 usage = 0;
5233 }
5234 }
5235 else
5236 {
5237 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5238 usage = KU_DIGITAL_SIGNATURE;
5239 }
5240
5241 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5242 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005243#else
5244 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005245#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5246
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005247#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5248 if( cert_endpoint == SSL_IS_SERVER )
5249 {
5250 ext_oid = OID_SERVER_AUTH;
5251 ext_len = OID_SIZE( OID_SERVER_AUTH );
5252 }
5253 else
5254 {
5255 ext_oid = OID_CLIENT_AUTH;
5256 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5257 }
5258
5259 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5260 return( -1 );
5261#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5262
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005263 return( 0 );
5264}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005265#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005266
5267#endif /* POLARSSL_SSL_TLS_C */