blob: 8613b8d0e3d9b2c5b372fc3f51692076cc44661b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Paul Bakker48916f92012-09-16 19:57:18 +0000473 handshake->tls_prf( handshake->premaster, handshake->pmslen,
474 "master secret",
475 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Paul Bakker34617722014-06-13 17:20:13 +0200477 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 }
479 else
480 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
481
482 /*
483 * Swap the client and server random values.
484 */
Paul Bakker48916f92012-09-16 19:57:18 +0000485 memcpy( tmp, handshake->randbytes, 64 );
486 memcpy( handshake->randbytes, tmp + 32, 32 );
487 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200488 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 /*
491 * SSLv3:
492 * key block =
493 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
494 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
495 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
496 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
497 * ...
498 *
499 * TLSv1:
500 * key block = PRF( master, "key expansion", randbytes )
501 */
Paul Bakker48916f92012-09-16 19:57:18 +0000502 handshake->tls_prf( session->master, 48, "key expansion",
503 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Paul Bakker48916f92012-09-16 19:57:18 +0000505 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
506 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
507 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
508 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
510
Paul Bakker34617722014-06-13 17:20:13 +0200511 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 /*
514 * Determine the appropriate key, IV and MAC length.
515 */
Paul Bakker68884e32013-01-07 18:20:04 +0100516
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200517 transform->keylen = cipher_info->key_length / 8;
518
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200519 if( cipher_info->mode == POLARSSL_MODE_GCM ||
520 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200522 transform->maclen = 0;
523
Paul Bakker68884e32013-01-07 18:20:04 +0100524 transform->ivlen = 12;
525 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200526
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200527 /* Minimum length is expicit IV + tag */
528 transform->minlen = transform->ivlen - transform->fixed_ivlen
529 + ( transform->ciphersuite_info->flags &
530 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100531 }
532 else
533 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200534 int ret;
535
536 /* Initialize HMAC contexts */
537 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
538 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100539 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200540 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
541 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100542 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200544 /* Get MAC length */
545 transform->maclen = md_get_size( md_info );
546
547#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
548 /*
549 * If HMAC is to be truncated, we shall keep the leftmost bytes,
550 * (rfc 6066 page 13 or rfc 2104 section 4),
551 * so we only need to adjust the length here.
552 */
553 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
554 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
555#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
556
557 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100558 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200560 /* Minimum length */
561 if( cipher_info->mode == POLARSSL_MODE_STREAM )
562 transform->minlen = transform->maclen;
563 else
Paul Bakker68884e32013-01-07 18:20:04 +0100564 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200565 /*
566 * GenericBlockCipher:
567 * first multiple of blocklen greater than maclen
568 * + IV except for SSL3 and TLS 1.0
569 */
570 transform->minlen = transform->maclen
571 + cipher_info->block_size
572 - transform->maclen % cipher_info->block_size;
573
574#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
575 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
576 ssl->minor_ver == SSL_MINOR_VERSION_1 )
577 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100578 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200579#endif
580#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
581 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
582 ssl->minor_ver == SSL_MINOR_VERSION_3 )
583 {
584 transform->minlen += transform->ivlen;
585 }
586 else
587#endif
588 {
589 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
590 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
591 }
Paul Bakker68884e32013-01-07 18:20:04 +0100592 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 }
594
595 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000596 transform->keylen, transform->minlen, transform->ivlen,
597 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 /*
600 * Finally setup the cipher contexts, IVs and MAC secrets.
601 */
602 if( ssl->endpoint == SSL_IS_CLIENT )
603 {
Paul Bakker48916f92012-09-16 19:57:18 +0000604 key1 = keyblk + transform->maclen * 2;
605 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Paul Bakker68884e32013-01-07 18:20:04 +0100607 mac_enc = keyblk;
608 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000610 /*
611 * This is not used in TLS v1.1.
612 */
Paul Bakker48916f92012-09-16 19:57:18 +0000613 iv_copy_len = ( transform->fixed_ivlen ) ?
614 transform->fixed_ivlen : transform->ivlen;
615 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
616 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000617 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 }
619 else
620 {
Paul Bakker48916f92012-09-16 19:57:18 +0000621 key1 = keyblk + transform->maclen * 2 + transform->keylen;
622 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Paul Bakker68884e32013-01-07 18:20:04 +0100624 mac_enc = keyblk + transform->maclen;
625 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000627 /*
628 * This is not used in TLS v1.1.
629 */
Paul Bakker48916f92012-09-16 19:57:18 +0000630 iv_copy_len = ( transform->fixed_ivlen ) ?
631 transform->fixed_ivlen : transform->ivlen;
632 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
633 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000634 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 }
636
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200637#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100638 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
639 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100640 if( transform->maclen > sizeof transform->mac_enc )
641 {
642 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200643 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100644 }
645
Paul Bakker68884e32013-01-07 18:20:04 +0100646 memcpy( transform->mac_enc, mac_enc, transform->maclen );
647 memcpy( transform->mac_dec, mac_dec, transform->maclen );
648 }
649 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200650#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200651#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
652 defined(POLARSSL_SSL_PROTO_TLS1_2)
653 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100654 {
655 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
656 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
657 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200658 else
659#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200660 {
661 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200662 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200663 }
Paul Bakker68884e32013-01-07 18:20:04 +0100664
Paul Bakker05ef8352012-05-08 09:17:57 +0000665#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200666 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000667 {
668 int ret = 0;
669
670 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
671
Paul Bakker07eb38b2012-12-19 14:42:06 +0100672 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
673 transform->iv_enc, transform->iv_dec,
674 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100675 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100676 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000677 {
678 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200679 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000680 }
681 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200682#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000683
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
685 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200687 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
688 return( ret );
689 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200690
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200691 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
692 cipher_info ) ) != 0 )
693 {
694 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
695 return( ret );
696 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200697
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200698 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
699 cipher_info->key_length,
700 POLARSSL_ENCRYPT ) ) != 0 )
701 {
702 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
703 return( ret );
704 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200705
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200706 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
707 cipher_info->key_length,
708 POLARSSL_DECRYPT ) ) != 0 )
709 {
710 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
711 return( ret );
712 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200713
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200714#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200715 if( cipher_info->mode == POLARSSL_MODE_CBC )
716 {
717 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
718 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200719 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200720 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
721 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200722 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200723
724 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
725 POLARSSL_PADDING_NONE ) ) != 0 )
726 {
727 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
728 return( ret );
729 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200731#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Paul Bakker34617722014-06-13 17:20:13 +0200733 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Paul Bakker2770fbd2012-07-03 13:30:23 +0000735#if defined(POLARSSL_ZLIB_SUPPORT)
736 // Initialize compression
737 //
Paul Bakker48916f92012-09-16 19:57:18 +0000738 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000739 {
Paul Bakker16770332013-10-11 09:59:44 +0200740 if( ssl->compress_buf == NULL )
741 {
742 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
743 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
744 if( ssl->compress_buf == NULL )
745 {
746 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
747 SSL_BUFFER_LEN ) );
748 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
749 }
750 }
751
Paul Bakker2770fbd2012-07-03 13:30:23 +0000752 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
753
Paul Bakker48916f92012-09-16 19:57:18 +0000754 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
755 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000756
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200757 if( deflateInit( &transform->ctx_deflate,
758 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000759 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000760 {
761 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
762 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
763 }
764 }
765#endif /* POLARSSL_ZLIB_SUPPORT */
766
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
768
769 return( 0 );
770}
771
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200772#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000773void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000774{
775 md5_context md5;
776 sha1_context sha1;
777 unsigned char pad_1[48];
778 unsigned char pad_2[48];
779
Paul Bakker380da532012-04-18 16:10:25 +0000780 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Paul Bakker48916f92012-09-16 19:57:18 +0000782 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
783 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Paul Bakker380da532012-04-18 16:10:25 +0000785 memset( pad_1, 0x36, 48 );
786 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000787
Paul Bakker48916f92012-09-16 19:57:18 +0000788 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000789 md5_update( &md5, pad_1, 48 );
790 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
Paul Bakker380da532012-04-18 16:10:25 +0000792 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000793 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000794 md5_update( &md5, pad_2, 48 );
795 md5_update( &md5, hash, 16 );
796 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Paul Bakker48916f92012-09-16 19:57:18 +0000798 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000799 sha1_update( &sha1, pad_1, 40 );
800 sha1_finish( &sha1, hash + 16 );
801
802 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000803 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000804 sha1_update( &sha1, pad_2, 40 );
805 sha1_update( &sha1, hash + 16, 20 );
806 sha1_finish( &sha1, hash + 16 );
807
808 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
809 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
810
Paul Bakker5b4af392014-06-26 12:09:34 +0200811 md5_free( &md5 );
812 sha1_free( &sha1 );
813
Paul Bakker380da532012-04-18 16:10:25 +0000814 return;
815}
Paul Bakker9af723c2014-05-01 13:03:14 +0200816#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000817
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200818#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000819void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
820{
821 md5_context md5;
822 sha1_context sha1;
823
824 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
825
Paul Bakker48916f92012-09-16 19:57:18 +0000826 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
827 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000828
Paul Bakker48916f92012-09-16 19:57:18 +0000829 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000830 sha1_finish( &sha1, hash + 16 );
831
832 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
833 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
834
Paul Bakker5b4af392014-06-26 12:09:34 +0200835 md5_free( &md5 );
836 sha1_free( &sha1 );
837
Paul Bakker380da532012-04-18 16:10:25 +0000838 return;
839}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200840#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000841
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200842#if defined(POLARSSL_SSL_PROTO_TLS1_2)
843#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000844void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
845{
Paul Bakker9e36f042013-06-30 14:34:05 +0200846 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000847
848 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
849
Paul Bakker9e36f042013-06-30 14:34:05 +0200850 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
851 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000852
853 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
854 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
855
Paul Bakker5b4af392014-06-26 12:09:34 +0200856 sha256_free( &sha256 );
857
Paul Bakker380da532012-04-18 16:10:25 +0000858 return;
859}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000861
Paul Bakker9e36f042013-06-30 14:34:05 +0200862#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000863void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
864{
Paul Bakker9e36f042013-06-30 14:34:05 +0200865 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000866
867 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
868
Paul Bakker9e36f042013-06-30 14:34:05 +0200869 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
870 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Paul Bakkerca4ab492012-04-18 14:23:57 +0000872 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
874
Paul Bakker5b4af392014-06-26 12:09:34 +0200875 sha512_free( &sha512 );
876
Paul Bakker5121ce52009-01-03 21:22:43 +0000877 return;
878}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200879#endif /* POLARSSL_SHA512_C */
880#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200882#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200883int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
884{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200885 unsigned char *p = ssl->handshake->premaster;
886 unsigned char *end = p + sizeof( ssl->handshake->premaster );
887
888 /*
889 * PMS = struct {
890 * opaque other_secret<0..2^16-1>;
891 * opaque psk<0..2^16-1>;
892 * };
893 * with "other_secret" depending on the particular key exchange
894 */
895#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
896 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
897 {
898 if( end - p < 2 + (int) ssl->psk_len )
899 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
900
901 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
902 *(p++) = (unsigned char)( ssl->psk_len );
903 p += ssl->psk_len;
904 }
905 else
906#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200907#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
909 {
910 /*
911 * other_secret already set by the ClientKeyExchange message,
912 * and is 48 bytes long
913 */
914 *p++ = 0;
915 *p++ = 48;
916 p += 48;
917 }
918 else
919#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200920#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
921 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
922 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200923 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200924 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200925
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200926 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200927 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200928 p + 2, &len,
929 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200930 {
931 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
932 return( ret );
933 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200934 *(p++) = (unsigned char)( len >> 8 );
935 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200936 p += len;
937
938 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
939 }
940 else
941#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
942#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
943 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
944 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200945 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200946 size_t zlen;
947
948 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200949 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200950 ssl->f_rng, ssl->p_rng ) ) != 0 )
951 {
952 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
953 return( ret );
954 }
955
956 *(p++) = (unsigned char)( zlen >> 8 );
957 *(p++) = (unsigned char)( zlen );
958 p += zlen;
959
960 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
961 }
962 else
963#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
964 {
965 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200966 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200967 }
968
969 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100970 if( end - p < 2 + (int) ssl->psk_len )
971 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
972
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
974 *(p++) = (unsigned char)( ssl->psk_len );
975 memcpy( p, ssl->psk, ssl->psk_len );
976 p += ssl->psk_len;
977
978 ssl->handshake->pmslen = p - ssl->handshake->premaster;
979
980 return( 0 );
981}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200982#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200984#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000985/*
986 * SSLv3.0 MAC functions
987 */
Paul Bakker68884e32013-01-07 18:20:04 +0100988static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
989 unsigned char *buf, size_t len,
990 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000991{
992 unsigned char header[11];
993 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100994 int padlen = 0;
995 int md_size = md_get_size( md_ctx->md_info );
996 int md_type = md_get_type( md_ctx->md_info );
997
998 if( md_type == POLARSSL_MD_MD5 )
999 padlen = 48;
1000 else if( md_type == POLARSSL_MD_SHA1 )
1001 padlen = 40;
1002 else if( md_type == POLARSSL_MD_SHA256 )
1003 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +01001004 else if( md_type == POLARSSL_MD_SHA384 )
1005 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
1007 memcpy( header, ctr, 8 );
1008 header[ 8] = (unsigned char) type;
1009 header[ 9] = (unsigned char)( len >> 8 );
1010 header[10] = (unsigned char)( len );
1011
Paul Bakker68884e32013-01-07 18:20:04 +01001012 memset( padding, 0x36, padlen );
1013 md_starts( md_ctx );
1014 md_update( md_ctx, secret, md_size );
1015 md_update( md_ctx, padding, padlen );
1016 md_update( md_ctx, header, 11 );
1017 md_update( md_ctx, buf, len );
1018 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
Paul Bakker68884e32013-01-07 18:20:04 +01001020 memset( padding, 0x5C, padlen );
1021 md_starts( md_ctx );
1022 md_update( md_ctx, secret, md_size );
1023 md_update( md_ctx, padding, padlen );
1024 md_update( md_ctx, buf + len, md_size );
1025 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001026}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001027#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001028
Paul Bakker5121ce52009-01-03 21:22:43 +00001029/*
1030 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001031 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001032static int ssl_encrypt_buf( ssl_context *ssl )
1033{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001034 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001035 const cipher_mode_t mode = cipher_get_cipher_mode(
1036 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
1038 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1039
1040 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001041 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001043#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1044 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1045 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001046 if( mode != POLARSSL_MODE_GCM &&
1047 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001049#if defined(POLARSSL_SSL_PROTO_SSL3)
1050 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1051 {
1052 ssl_mac( &ssl->transform_out->md_ctx_enc,
1053 ssl->transform_out->mac_enc,
1054 ssl->out_msg, ssl->out_msglen,
1055 ssl->out_ctr, ssl->out_msgtype );
1056 }
1057 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001058#endif
1059#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001060 defined(POLARSSL_SSL_PROTO_TLS1_2)
1061 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1062 {
1063 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1064 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1065 ssl->out_msg, ssl->out_msglen );
1066 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1067 ssl->out_msg + ssl->out_msglen );
1068 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1069 }
1070 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001071#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001072 {
1073 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001074 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001075 }
1076
1077 SSL_DEBUG_BUF( 4, "computed mac",
1078 ssl->out_msg + ssl->out_msglen,
1079 ssl->transform_out->maclen );
1080
1081 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001082 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001083#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001085 /*
1086 * Encrypt
1087 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001088#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001089 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001090 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001091 int ret;
1092 size_t olen = 0;
1093
Paul Bakker5121ce52009-01-03 21:22:43 +00001094 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1095 "including %d bytes of padding",
1096 ssl->out_msglen, 0 ) );
1097
1098 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1099 ssl->out_msg, ssl->out_msglen );
1100
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001101 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001102 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001103 ssl->transform_out->ivlen,
1104 ssl->out_msg, ssl->out_msglen,
1105 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001106 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001107 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001108 return( ret );
1109 }
1110
1111 if( ssl->out_msglen != olen )
1112 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001113 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001114 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001115 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 }
Paul Bakker68884e32013-01-07 18:20:04 +01001117 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001118#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001119#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1120 if( mode == POLARSSL_MODE_GCM ||
1121 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001122 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001123 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001124 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001125 unsigned char *enc_msg;
1126 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001127 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1128 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001129
Paul Bakkerca4ab492012-04-18 14:23:57 +00001130 memcpy( add_data, ssl->out_ctr, 8 );
1131 add_data[8] = ssl->out_msgtype;
1132 add_data[9] = ssl->major_ver;
1133 add_data[10] = ssl->minor_ver;
1134 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1135 add_data[12] = ssl->out_msglen & 0xFF;
1136
1137 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1138 add_data, 13 );
1139
Paul Bakker68884e32013-01-07 18:20:04 +01001140 /*
1141 * Generate IV
1142 */
1143 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001144 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1145 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001146 if( ret != 0 )
1147 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001148
Paul Bakker68884e32013-01-07 18:20:04 +01001149 memcpy( ssl->out_iv,
1150 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1151 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001152
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001153 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001154 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001155
Paul Bakker68884e32013-01-07 18:20:04 +01001156 /*
1157 * Fix pointer positions and message length with added IV
1158 */
1159 enc_msg = ssl->out_msg;
1160 enc_msglen = ssl->out_msglen;
1161 ssl->out_msglen += ssl->transform_out->ivlen -
1162 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001163
Paul Bakker68884e32013-01-07 18:20:04 +01001164 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1165 "including %d bytes of padding",
1166 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001167
Paul Bakker68884e32013-01-07 18:20:04 +01001168 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1169 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001170
Paul Bakker68884e32013-01-07 18:20:04 +01001171 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001172 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001173 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001174 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1175 ssl->transform_out->iv_enc,
1176 ssl->transform_out->ivlen,
1177 add_data, 13,
1178 enc_msg, enc_msglen,
1179 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001180 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001181 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001182 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001183 return( ret );
1184 }
1185
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001186 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001187 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001188 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001189 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001190 }
1191
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001192 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001193
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001194 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001195 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001197#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001198#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1199 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001200 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001202 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001203 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001204 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001205
Paul Bakker48916f92012-09-16 19:57:18 +00001206 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1207 ssl->transform_out->ivlen;
1208 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 padlen = 0;
1210
1211 for( i = 0; i <= padlen; i++ )
1212 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1213
1214 ssl->out_msglen += padlen + 1;
1215
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001216 enc_msglen = ssl->out_msglen;
1217 enc_msg = ssl->out_msg;
1218
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001219#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001221 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1222 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001223 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001224 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001225 {
1226 /*
1227 * Generate IV
1228 */
Paul Bakker48916f92012-09-16 19:57:18 +00001229 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1230 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001231 if( ret != 0 )
1232 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233
Paul Bakker92be97b2013-01-02 17:30:03 +01001234 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001235 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236
1237 /*
1238 * Fix pointer positions and message length with added IV
1239 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001240 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001241 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001242 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001244#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001245
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001247 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001248 ssl->out_msglen, ssl->transform_out->ivlen,
1249 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
1251 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001252 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001254 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001255 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001256 ssl->transform_out->ivlen,
1257 enc_msg, enc_msglen,
1258 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001259 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001260 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001261 return( ret );
1262 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001263
Paul Bakkercca5b812013-08-31 17:40:26 +02001264 if( enc_msglen != olen )
1265 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001266 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001267 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001268 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001269
1270#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001271 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1272 {
1273 /*
1274 * Save IV in SSL3 and TLS1
1275 */
1276 memcpy( ssl->transform_out->iv_enc,
1277 ssl->transform_out->cipher_ctx_enc.iv,
1278 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001282 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001283#endif /* POLARSSL_CIPHER_MODE_CBC &&
1284 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001285 {
1286 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001287 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
Paul Bakkerca4ab492012-04-18 14:23:57 +00001290 for( i = 8; i > 0; i-- )
1291 if( ++ssl->out_ctr[i - 1] != 0 )
1292 break;
1293
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001294 /* The loops goes to its end iff the counter is wrapping */
1295 if( i == 0 )
1296 {
1297 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1298 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1299 }
1300
Paul Bakker5121ce52009-01-03 21:22:43 +00001301 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1302
1303 return( 0 );
1304}
1305
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001306#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001307
Paul Bakker5121ce52009-01-03 21:22:43 +00001308static int ssl_decrypt_buf( ssl_context *ssl )
1309{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001310 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001311 const cipher_mode_t mode = cipher_get_cipher_mode(
1312 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001313#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1314 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1315 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1316 size_t padlen = 0, correct = 1;
1317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
1319 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1320
Paul Bakker48916f92012-09-16 19:57:18 +00001321 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 {
1323 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001324 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001325 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
1327
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001328#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001329 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001330 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001331 int ret;
1332 size_t olen = 0;
1333
Paul Bakker68884e32013-01-07 18:20:04 +01001334 padlen = 0;
1335
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001336 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001337 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001338 ssl->transform_in->ivlen,
1339 ssl->in_msg, ssl->in_msglen,
1340 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001341 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001342 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001343 return( ret );
1344 }
1345
1346 if( ssl->in_msglen != olen )
1347 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001348 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001349 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001350 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 }
Paul Bakker68884e32013-01-07 18:20:04 +01001352 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001353#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001354#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1355 if( mode == POLARSSL_MODE_GCM ||
1356 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001357 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001358 int ret;
1359 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001360 unsigned char *dec_msg;
1361 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001362 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001363 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1364 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001365 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1366 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001367
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001368 if( ssl->in_msglen < explicit_iv_len + taglen )
1369 {
1370 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1371 "+ taglen (%d)", ssl->in_msglen,
1372 explicit_iv_len, taglen ) );
1373 return( POLARSSL_ERR_SSL_INVALID_MAC );
1374 }
1375 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1376
Paul Bakker68884e32013-01-07 18:20:04 +01001377 dec_msg = ssl->in_msg;
1378 dec_msg_result = ssl->in_msg;
1379 ssl->in_msglen = dec_msglen;
1380
1381 memcpy( add_data, ssl->in_ctr, 8 );
1382 add_data[8] = ssl->in_msgtype;
1383 add_data[9] = ssl->major_ver;
1384 add_data[10] = ssl->minor_ver;
1385 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1386 add_data[12] = ssl->in_msglen & 0xFF;
1387
1388 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1389 add_data, 13 );
1390
1391 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1392 ssl->in_iv,
1393 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1394
1395 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1396 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001397 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001398
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001399 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001400 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001401 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001402 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1403 ssl->transform_in->iv_dec,
1404 ssl->transform_in->ivlen,
1405 add_data, 13,
1406 dec_msg, dec_msglen,
1407 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001408 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001409 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001410 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1411
1412 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1413 return( POLARSSL_ERR_SSL_INVALID_MAC );
1414
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001415 return( ret );
1416 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001417
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001418 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001419 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001420 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001421 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001422 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001425#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001426#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1427 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001428 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 {
Paul Bakker45829992013-01-03 14:52:21 +01001430 /*
1431 * Decrypt and check the padding
1432 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001433 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001434 unsigned char *dec_msg;
1435 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001436 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001437 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001438 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001439
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 /*
Paul Bakker45829992013-01-03 14:52:21 +01001441 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 */
Paul Bakker48916f92012-09-16 19:57:18 +00001443 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 {
1445 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001446 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001447 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 }
1449
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001450#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001451 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1452 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001453#endif
Paul Bakker45829992013-01-03 14:52:21 +01001454
1455 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1456 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1457 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001458 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1459 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1460 ssl->transform_in->ivlen,
1461 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001462 return( POLARSSL_ERR_SSL_INVALID_MAC );
1463 }
1464
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001465 dec_msglen = ssl->in_msglen;
1466 dec_msg = ssl->in_msg;
1467 dec_msg_result = ssl->in_msg;
1468
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001469#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001470 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001471 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001472 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001473 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001474 {
Paul Bakker48916f92012-09-16 19:57:18 +00001475 dec_msglen -= ssl->transform_in->ivlen;
1476 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001477
Paul Bakker48916f92012-09-16 19:57:18 +00001478 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001479 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001480 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001481#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001482
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001483 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001484 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001485 ssl->transform_in->ivlen,
1486 dec_msg, dec_msglen,
1487 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001488 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001489 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001490 return( ret );
1491 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001492
Paul Bakkercca5b812013-08-31 17:40:26 +02001493 if( dec_msglen != olen )
1494 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001495 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001496 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001497 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001498
1499#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001500 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1501 {
1502 /*
1503 * Save IV in SSL3 and TLS1
1504 */
1505 memcpy( ssl->transform_in->iv_dec,
1506 ssl->transform_in->cipher_ctx_dec.iv,
1507 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
1511 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001512
1513 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1514 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001515#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001516 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1517 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001518#endif
Paul Bakker45829992013-01-03 14:52:21 +01001519 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001520 correct = 0;
1521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001522
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001523#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1525 {
Paul Bakker48916f92012-09-16 19:57:18 +00001526 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001528#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1530 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001531 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001532#endif
Paul Bakker45829992013-01-03 14:52:21 +01001533 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 }
1535 }
1536 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001537#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001538#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1539 defined(POLARSSL_SSL_PROTO_TLS1_2)
1540 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001541 {
1542 /*
Paul Bakker45829992013-01-03 14:52:21 +01001543 * TLSv1+: always check the padding up to the first failure
1544 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001546 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001547 size_t padding_idx = ssl->in_msglen - padlen - 1;
1548
Paul Bakker956c9e02013-12-19 14:42:28 +01001549 /*
1550 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001551 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001552 *
Paul Bakker61885c72014-04-25 12:59:03 +02001553 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1554 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001555 *
1556 * In both cases we reset padding_idx to a safe value (0) to
1557 * prevent out-of-buffer reads.
1558 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001559 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001560 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1561 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001562
1563 padding_idx *= correct;
1564
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001565 for( i = 1; i <= 256; i++ )
1566 {
1567 real_count &= ( i <= padlen );
1568 pad_count += real_count *
1569 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1570 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001571
1572 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001573
Paul Bakkerd66f0702013-01-31 16:57:45 +01001574#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001575 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001576 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001577#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001578 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001580 else
1581#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1582 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001583 {
1584 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001585 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001587 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001588 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001589#endif /* POLARSSL_CIPHER_MODE_CBC &&
1590 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001591 {
1592 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001593 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001595
1596 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1597 ssl->in_msg, ssl->in_msglen );
1598
1599 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001600 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001601 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001602#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1603 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1604 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001605 if( mode != POLARSSL_MODE_GCM &&
1606 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001607 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001608 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1609
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001610 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001612 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1613 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001615 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001617#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001618 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1619 {
1620 ssl_mac( &ssl->transform_in->md_ctx_dec,
1621 ssl->transform_in->mac_dec,
1622 ssl->in_msg, ssl->in_msglen,
1623 ssl->in_ctr, ssl->in_msgtype );
1624 }
1625 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001626#endif /* POLARSSL_SSL_PROTO_SSL3 */
1627#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001628 defined(POLARSSL_SSL_PROTO_TLS1_2)
1629 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1630 {
1631 /*
1632 * Process MAC and always update for padlen afterwards to make
1633 * total time independent of padlen
1634 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001635 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001636 *
1637 * Known timing attacks:
1638 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1639 *
1640 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1641 * correctly. (We round down instead of up, so -56 is the correct
1642 * value for our calculations instead of -55)
1643 */
1644 size_t j, extra_run = 0;
1645 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1646 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001647
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001648 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001649
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001650 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1651 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1652 ssl->in_msglen );
1653 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1654 ssl->in_msg + ssl->in_msglen );
1655 for( j = 0; j < extra_run; j++ )
1656 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001657
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001658 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1659 }
1660 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001661#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001662 POLARSSL_SSL_PROTO_TLS1_2 */
1663 {
1664 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001665 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1669 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1670 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001672 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001673 ssl->transform_in->maclen ) != 0 )
1674 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001675#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001677#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678 correct = 0;
1679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001681 /*
1682 * Finally check the correct flag
1683 */
1684 if( correct == 0 )
1685 return( POLARSSL_ERR_SSL_INVALID_MAC );
1686 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001687#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001688
1689 if( ssl->in_msglen == 0 )
1690 {
1691 ssl->nb_zero++;
1692
1693 /*
1694 * Three or more empty messages may be a DoS attack
1695 * (excessive CPU consumption).
1696 */
1697 if( ssl->nb_zero > 3 )
1698 {
1699 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1700 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001701 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 }
1703 }
1704 else
1705 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001706
Paul Bakker23986e52011-04-24 08:57:21 +00001707 for( i = 8; i > 0; i-- )
1708 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 break;
1710
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001711 /* The loops goes to its end iff the counter is wrapping */
1712 if( i == 0 )
1713 {
1714 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1715 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1716 }
1717
Paul Bakker5121ce52009-01-03 21:22:43 +00001718 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1719
1720 return( 0 );
1721}
1722
Paul Bakker2770fbd2012-07-03 13:30:23 +00001723#if defined(POLARSSL_ZLIB_SUPPORT)
1724/*
1725 * Compression/decompression functions
1726 */
1727static int ssl_compress_buf( ssl_context *ssl )
1728{
1729 int ret;
1730 unsigned char *msg_post = ssl->out_msg;
1731 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001732 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001733
1734 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1735
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001736 if( len_pre == 0 )
1737 return( 0 );
1738
Paul Bakker2770fbd2012-07-03 13:30:23 +00001739 memcpy( msg_pre, ssl->out_msg, len_pre );
1740
1741 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1742 ssl->out_msglen ) );
1743
1744 SSL_DEBUG_BUF( 4, "before compression: output payload",
1745 ssl->out_msg, ssl->out_msglen );
1746
Paul Bakker48916f92012-09-16 19:57:18 +00001747 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1748 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1749 ssl->transform_out->ctx_deflate.next_out = msg_post;
1750 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001751
Paul Bakker48916f92012-09-16 19:57:18 +00001752 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001753 if( ret != Z_OK )
1754 {
1755 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1756 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1757 }
1758
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001759 ssl->out_msglen = SSL_BUFFER_LEN -
1760 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001761
Paul Bakker2770fbd2012-07-03 13:30:23 +00001762 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1763 ssl->out_msglen ) );
1764
1765 SSL_DEBUG_BUF( 4, "after compression: output payload",
1766 ssl->out_msg, ssl->out_msglen );
1767
1768 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1769
1770 return( 0 );
1771}
1772
1773static int ssl_decompress_buf( ssl_context *ssl )
1774{
1775 int ret;
1776 unsigned char *msg_post = ssl->in_msg;
1777 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001778 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001779
1780 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1781
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001782 if( len_pre == 0 )
1783 return( 0 );
1784
Paul Bakker2770fbd2012-07-03 13:30:23 +00001785 memcpy( msg_pre, ssl->in_msg, len_pre );
1786
1787 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1788 ssl->in_msglen ) );
1789
1790 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1791 ssl->in_msg, ssl->in_msglen );
1792
Paul Bakker48916f92012-09-16 19:57:18 +00001793 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1794 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1795 ssl->transform_in->ctx_inflate.next_out = msg_post;
1796 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001797
Paul Bakker48916f92012-09-16 19:57:18 +00001798 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799 if( ret != Z_OK )
1800 {
1801 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1802 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1803 }
1804
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001805 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1806 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001807
Paul Bakker2770fbd2012-07-03 13:30:23 +00001808 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1809 ssl->in_msglen ) );
1810
1811 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1812 ssl->in_msg, ssl->in_msglen );
1813
1814 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1815
1816 return( 0 );
1817}
1818#endif /* POLARSSL_ZLIB_SUPPORT */
1819
Paul Bakker5121ce52009-01-03 21:22:43 +00001820/*
1821 * Fill the input message buffer
1822 */
Paul Bakker23986e52011-04-24 08:57:21 +00001823int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001824{
Paul Bakker23986e52011-04-24 08:57:21 +00001825 int ret;
1826 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001827
1828 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1829
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001830 if( nb_want > SSL_BUFFER_LEN - 8 )
1831 {
1832 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1833 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1834 }
1835
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 while( ssl->in_left < nb_want )
1837 {
1838 len = nb_want - ssl->in_left;
1839 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1840
1841 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1842 ssl->in_left, nb_want ) );
1843 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1844
Paul Bakker831a7552011-05-18 13:32:51 +00001845 if( ret == 0 )
1846 return( POLARSSL_ERR_SSL_CONN_EOF );
1847
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 if( ret < 0 )
1849 return( ret );
1850
1851 ssl->in_left += ret;
1852 }
1853
1854 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1855
1856 return( 0 );
1857}
1858
1859/*
1860 * Flush any data not yet written
1861 */
1862int ssl_flush_output( ssl_context *ssl )
1863{
1864 int ret;
1865 unsigned char *buf;
1866
1867 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1868
1869 while( ssl->out_left > 0 )
1870 {
1871 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1872 5 + ssl->out_msglen, ssl->out_left ) );
1873
Paul Bakker5bd42292012-12-19 14:40:42 +01001874 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001876
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1878
1879 if( ret <= 0 )
1880 return( ret );
1881
1882 ssl->out_left -= ret;
1883 }
1884
1885 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1886
1887 return( 0 );
1888}
1889
1890/*
1891 * Record layer functions
1892 */
1893int ssl_write_record( ssl_context *ssl )
1894{
Paul Bakker05ef8352012-05-08 09:17:57 +00001895 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001896 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001897
1898 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1899
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1901 {
1902 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1903 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1904 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1905
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001906 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1907 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 }
1909
Paul Bakker2770fbd2012-07-03 13:30:23 +00001910#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001911 if( ssl->transform_out != NULL &&
1912 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001913 {
1914 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1915 {
1916 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1917 return( ret );
1918 }
1919
1920 len = ssl->out_msglen;
1921 }
1922#endif /*POLARSSL_ZLIB_SUPPORT */
1923
Paul Bakker05ef8352012-05-08 09:17:57 +00001924#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001925 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001927 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Paul Bakker05ef8352012-05-08 09:17:57 +00001929 ret = ssl_hw_record_write( ssl );
1930 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1931 {
1932 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001933 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001934 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001935
1936 if( ret == 0 )
1937 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001938 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001939#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001940 if( !done )
1941 {
1942 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1943 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1944 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1946 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001947
Paul Bakker48916f92012-09-16 19:57:18 +00001948 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001949 {
1950 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1951 {
1952 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1953 return( ret );
1954 }
1955
1956 len = ssl->out_msglen;
1957 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1958 ssl->out_hdr[4] = (unsigned char)( len );
1959 }
1960
1961 ssl->out_left = 5 + ssl->out_msglen;
1962
1963 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1964 "version = [%d:%d], msglen = %d",
1965 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1966 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1967
1968 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001969 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 }
1971
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1973 {
1974 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1975 return( ret );
1976 }
1977
1978 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1979
1980 return( 0 );
1981}
1982
1983int ssl_read_record( ssl_context *ssl )
1984{
Paul Bakker05ef8352012-05-08 09:17:57 +00001985 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
1987 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1988
1989 if( ssl->in_hslen != 0 &&
1990 ssl->in_hslen < ssl->in_msglen )
1991 {
1992 /*
1993 * Get next Handshake message in the current record
1994 */
1995 ssl->in_msglen -= ssl->in_hslen;
1996
Paul Bakker8934a982011-08-05 11:11:53 +00001997 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001998 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999
2000 ssl->in_hslen = 4;
2001 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2002
2003 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2004 " %d, type = %d, hslen = %d",
2005 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2006
2007 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2008 {
2009 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002010 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 }
2012
2013 if( ssl->in_msglen < ssl->in_hslen )
2014 {
2015 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002016 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 }
2018
Paul Bakker4224bc02014-04-08 14:36:50 +02002019 if( ssl->state != SSL_HANDSHAKE_OVER )
2020 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002021
2022 return( 0 );
2023 }
2024
2025 ssl->in_hslen = 0;
2026
2027 /*
2028 * Read the record header and validate it
2029 */
2030 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2031 {
2032 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2033 return( ret );
2034 }
2035
2036 ssl->in_msgtype = ssl->in_hdr[0];
2037 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2038
2039 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2040 "version = [%d:%d], msglen = %d",
2041 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2042 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2043
2044 if( ssl->in_hdr[1] != ssl->major_ver )
2045 {
2046 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002047 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 }
2049
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002050 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 {
2052 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002053 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 }
2055
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002056 /* Sanity check (outer boundaries) */
2057 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2058 {
2059 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2060 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2061 }
2062
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002064 * Make sure the message length is acceptable for the current transform
2065 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 */
Paul Bakker48916f92012-09-16 19:57:18 +00002067 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002069 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 {
2071 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002072 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 }
2074 }
2075 else
2076 {
Paul Bakker48916f92012-09-16 19:57:18 +00002077 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 {
2079 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002080 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 }
2082
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002083#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002085 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 {
2087 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002088 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002090#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002091
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002092#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2093 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 /*
2095 * TLS encrypted messages can have up to 256 bytes of padding
2096 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002097 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002098 ssl->in_msglen > ssl->transform_in->minlen +
2099 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 {
2101 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002102 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002104#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
2106
2107 /*
2108 * Read and optionally decrypt the message contents
2109 */
2110 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2111 {
2112 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2113 return( ret );
2114 }
2115
2116 SSL_DEBUG_BUF( 4, "input record from network",
2117 ssl->in_hdr, 5 + ssl->in_msglen );
2118
Paul Bakker05ef8352012-05-08 09:17:57 +00002119#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002120 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002121 {
2122 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2123
2124 ret = ssl_hw_record_read( ssl );
2125 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2126 {
2127 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002128 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002129 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002130
2131 if( ret == 0 )
2132 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002133 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002134#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002135 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 {
2137 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2138 {
Paul Bakker40865c82013-01-31 17:13:13 +01002139#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2140 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2141 {
2142 ssl_send_alert_message( ssl,
2143 SSL_ALERT_LEVEL_FATAL,
2144 SSL_ALERT_MSG_BAD_RECORD_MAC );
2145 }
2146#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2148 return( ret );
2149 }
2150
2151 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2152 ssl->in_msg, ssl->in_msglen );
2153
2154 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2155 {
2156 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002157 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 }
2159 }
2160
Paul Bakker2770fbd2012-07-03 13:30:23 +00002161#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002162 if( ssl->transform_in != NULL &&
2163 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002164 {
2165 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2166 {
2167 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2168 return( ret );
2169 }
2170
2171 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2172 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2173 }
2174#endif /* POLARSSL_ZLIB_SUPPORT */
2175
Paul Bakker0a925182012-04-16 06:46:41 +00002176 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2177 ssl->in_msgtype != SSL_MSG_ALERT &&
2178 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2179 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2180 {
2181 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2182
Paul Bakker48916f92012-09-16 19:57:18 +00002183 if( ( ret = ssl_send_alert_message( ssl,
2184 SSL_ALERT_LEVEL_FATAL,
2185 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002186 {
2187 return( ret );
2188 }
2189
2190 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2191 }
2192
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2194 {
2195 ssl->in_hslen = 4;
2196 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2197
2198 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2199 " %d, type = %d, hslen = %d",
2200 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2201
2202 /*
2203 * Additional checks to validate the handshake header
2204 */
2205 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2206 {
2207 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002208 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 }
2210
2211 if( ssl->in_msglen < ssl->in_hslen )
2212 {
2213 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002214 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 }
2216
Paul Bakker48916f92012-09-16 19:57:18 +00002217 if( ssl->state != SSL_HANDSHAKE_OVER )
2218 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 }
2220
2221 if( ssl->in_msgtype == SSL_MSG_ALERT )
2222 {
2223 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2224 ssl->in_msg[0], ssl->in_msg[1] ) );
2225
2226 /*
2227 * Ignore non-fatal alerts, except close_notify
2228 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002229 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002231 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2232 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002233 /**
2234 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2235 * error identifier.
2236 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002237 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002240 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2241 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 {
2243 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002244 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 }
2246 }
2247
2248 ssl->in_left = 0;
2249
2250 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2251
2252 return( 0 );
2253}
2254
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002255int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2256{
2257 int ret;
2258
2259 if( ( ret = ssl_send_alert_message( ssl,
2260 SSL_ALERT_LEVEL_FATAL,
2261 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2262 {
2263 return( ret );
2264 }
2265
2266 return( 0 );
2267}
2268
Paul Bakker0a925182012-04-16 06:46:41 +00002269int ssl_send_alert_message( ssl_context *ssl,
2270 unsigned char level,
2271 unsigned char message )
2272{
2273 int ret;
2274
2275 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2276
2277 ssl->out_msgtype = SSL_MSG_ALERT;
2278 ssl->out_msglen = 2;
2279 ssl->out_msg[0] = level;
2280 ssl->out_msg[1] = message;
2281
2282 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2283 {
2284 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2285 return( ret );
2286 }
2287
2288 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2289
2290 return( 0 );
2291}
2292
Paul Bakker5121ce52009-01-03 21:22:43 +00002293/*
2294 * Handshake functions
2295 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002296#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2297 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2298 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2299 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2300 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2301 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2302 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002303int ssl_write_certificate( ssl_context *ssl )
2304{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002305 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002306
2307 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2308
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002309 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002310 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2311 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002312 {
2313 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2314 ssl->state++;
2315 return( 0 );
2316 }
2317
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002318 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2319 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002320}
2321
2322int ssl_parse_certificate( ssl_context *ssl )
2323{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002324 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2325
2326 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2327
2328 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002329 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2330 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002331 {
2332 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2333 ssl->state++;
2334 return( 0 );
2335 }
2336
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002337 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2338 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002339}
2340#else
2341int ssl_write_certificate( ssl_context *ssl )
2342{
2343 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2344 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002345 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002346 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2347
2348 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2349
2350 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002351 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2352 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002353 {
2354 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2355 ssl->state++;
2356 return( 0 );
2357 }
2358
Paul Bakker5121ce52009-01-03 21:22:43 +00002359 if( ssl->endpoint == SSL_IS_CLIENT )
2360 {
2361 if( ssl->client_auth == 0 )
2362 {
2363 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2364 ssl->state++;
2365 return( 0 );
2366 }
2367
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002368#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 /*
2370 * If using SSLv3 and got no cert, send an Alert message
2371 * (otherwise an empty Certificate message will be sent).
2372 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002373 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2375 {
2376 ssl->out_msglen = 2;
2377 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002378 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2379 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
2381 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2382 goto write_msg;
2383 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002384#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
2386 else /* SSL_IS_SERVER */
2387 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002388 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 {
2390 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002391 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 }
2393 }
2394
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002395 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
2397 /*
2398 * 0 . 0 handshake type
2399 * 1 . 3 handshake length
2400 * 4 . 6 length of all certs
2401 * 7 . 9 length of cert. 1
2402 * 10 . n-1 peer certificate
2403 * n . n+2 length of cert. 2
2404 * n+3 . ... upper level cert, etc.
2405 */
2406 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002407 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
Paul Bakker29087132010-03-21 21:03:34 +00002409 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 {
2411 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002412 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 {
2414 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2415 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002416 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 }
2418
2419 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2420 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2421 ssl->out_msg[i + 2] = (unsigned char)( n );
2422
2423 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2424 i += n; crt = crt->next;
2425 }
2426
2427 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2428 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2429 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2430
2431 ssl->out_msglen = i;
2432 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2433 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2434
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002435#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002436write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002437#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
2439 ssl->state++;
2440
2441 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2442 {
2443 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2444 return( ret );
2445 }
2446
2447 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2448
Paul Bakkered27a042013-04-18 22:46:23 +02002449 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002450}
2451
2452int ssl_parse_certificate( ssl_context *ssl )
2453{
Paul Bakkered27a042013-04-18 22:46:23 +02002454 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002455 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002456 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
2458 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2459
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002460 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002461 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2462 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002463 {
2464 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2465 ssl->state++;
2466 return( 0 );
2467 }
2468
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002470 ( ssl->authmode == SSL_VERIFY_NONE ||
2471 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002473 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2475 ssl->state++;
2476 return( 0 );
2477 }
2478
2479 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2480 {
2481 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2482 return( ret );
2483 }
2484
2485 ssl->state++;
2486
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002487#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 /*
2489 * Check if the client sent an empty certificate
2490 */
2491 if( ssl->endpoint == SSL_IS_SERVER &&
2492 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2493 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002494 if( ssl->in_msglen == 2 &&
2495 ssl->in_msgtype == SSL_MSG_ALERT &&
2496 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2497 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 {
2499 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2500
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002501 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2503 return( 0 );
2504 else
Paul Bakker40e46942009-01-03 21:51:57 +00002505 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 }
2507 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002508#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002510#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2511 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 if( ssl->endpoint == SSL_IS_SERVER &&
2513 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2514 {
2515 if( ssl->in_hslen == 7 &&
2516 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2517 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2518 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2519 {
2520 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2521
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002522 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002524 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 else
2526 return( 0 );
2527 }
2528 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002529#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2530 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
2532 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2533 {
2534 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002535 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 }
2537
2538 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2539 {
2540 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002541 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 }
2543
2544 /*
2545 * Same message structure as in ssl_write_certificate()
2546 */
2547 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2548
2549 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2550 {
2551 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002552 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 }
2554
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002555 /* In case we tried to reuse a session but it failed */
2556 if( ssl->session_negotiate->peer_cert != NULL )
2557 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002558 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002559 polarssl_free( ssl->session_negotiate->peer_cert );
2560 }
2561
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002562 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2563 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 {
2565 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002566 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002567 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
2569
Paul Bakkerb6b09562013-09-18 14:17:41 +02002570 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
2572 i = 7;
2573
2574 while( i < ssl->in_hslen )
2575 {
2576 if( ssl->in_msg[i] != 0 )
2577 {
2578 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002579 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 }
2581
2582 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2583 | (unsigned int) ssl->in_msg[i + 2];
2584 i += 3;
2585
2586 if( n < 128 || i + n > ssl->in_hslen )
2587 {
2588 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002589 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 }
2591
Paul Bakkerddf26b42013-09-18 13:46:23 +02002592 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2593 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 if( ret != 0 )
2595 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002596 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 return( ret );
2598 }
2599
2600 i += n;
2601 }
2602
Paul Bakker48916f92012-09-16 19:57:18 +00002603 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002605 /*
2606 * On client, make sure the server cert doesn't change during renego to
2607 * avoid "triple handshake" attack: https://secure-resumption.com/
2608 */
2609 if( ssl->endpoint == SSL_IS_CLIENT &&
2610 ssl->renegotiation == SSL_RENEGOTIATION )
2611 {
2612 if( ssl->session->peer_cert == NULL )
2613 {
2614 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2615 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2616 }
2617
2618 if( ssl->session->peer_cert->raw.len !=
2619 ssl->session_negotiate->peer_cert->raw.len ||
2620 memcmp( ssl->session->peer_cert->raw.p,
2621 ssl->session_negotiate->peer_cert->raw.p,
2622 ssl->session->peer_cert->raw.len ) != 0 )
2623 {
2624 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2625 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2626 }
2627 }
2628
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 if( ssl->authmode != SSL_VERIFY_NONE )
2630 {
2631 if( ssl->ca_chain == NULL )
2632 {
2633 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002634 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 }
2636
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002637 /*
2638 * Main check: verify certificate
2639 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002640 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2641 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2642 &ssl->session_negotiate->verify_result,
2643 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
2645 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002646 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002648 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002649
2650 /*
2651 * Secondary checks: always done, but change 'ret' only if it was 0
2652 */
2653
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002654#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002655 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002656 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002657
2658 /* If certificate uses an EC key, make sure the curve is OK */
2659 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2660 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2661 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002662 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002663 if( ret == 0 )
2664 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002665 }
2666 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002667#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002669 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2670 ciphersuite_info,
2671 ! ssl->endpoint ) != 0 )
2672 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002673 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002674 if( ret == 0 )
2675 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2676 }
2677
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2679 ret = 0;
2680 }
2681
2682 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2683
2684 return( ret );
2685}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002686#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2687 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2688 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2689 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2690 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2691 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2692 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
2694int ssl_write_change_cipher_spec( ssl_context *ssl )
2695{
2696 int ret;
2697
2698 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2699
2700 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2701 ssl->out_msglen = 1;
2702 ssl->out_msg[0] = 1;
2703
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 ssl->state++;
2705
2706 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2707 {
2708 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2709 return( ret );
2710 }
2711
2712 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2713
2714 return( 0 );
2715}
2716
2717int ssl_parse_change_cipher_spec( ssl_context *ssl )
2718{
2719 int ret;
2720
2721 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2722
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2724 {
2725 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2726 return( ret );
2727 }
2728
2729 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2730 {
2731 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002732 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
2734
2735 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2736 {
2737 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002738 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 }
2740
2741 ssl->state++;
2742
2743 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2744
2745 return( 0 );
2746}
2747
Paul Bakker41c83d32013-03-20 14:39:14 +01002748void ssl_optimize_checksum( ssl_context *ssl,
2749 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002750{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002751 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002752
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002753#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2754 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002755 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002756 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002757 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002758#endif
2759#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2760#if defined(POLARSSL_SHA512_C)
2761 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2762 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2763 else
2764#endif
2765#if defined(POLARSSL_SHA256_C)
2766 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002767 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002768 else
2769#endif
2770#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002771 {
2772 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002773 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002774 }
Paul Bakker380da532012-04-18 16:10:25 +00002775}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002776
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002777static void ssl_update_checksum_start( ssl_context *ssl,
2778 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002779{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002780#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2781 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002782 md5_update( &ssl->handshake->fin_md5 , buf, len );
2783 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002784#endif
2785#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2786#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002787 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002788#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002789#if defined(POLARSSL_SHA512_C)
2790 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002791#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002792#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002793}
2794
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002795#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2796 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002797static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2798 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002799{
Paul Bakker48916f92012-09-16 19:57:18 +00002800 md5_update( &ssl->handshake->fin_md5 , buf, len );
2801 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002802}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002803#endif
Paul Bakker380da532012-04-18 16:10:25 +00002804
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002805#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2806#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002807static void ssl_update_checksum_sha256( ssl_context *ssl,
2808 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002809{
Paul Bakker9e36f042013-06-30 14:34:05 +02002810 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002811}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002812#endif
Paul Bakker380da532012-04-18 16:10:25 +00002813
Paul Bakker9e36f042013-06-30 14:34:05 +02002814#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002815static void ssl_update_checksum_sha384( ssl_context *ssl,
2816 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002817{
Paul Bakker9e36f042013-06-30 14:34:05 +02002818 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002819}
Paul Bakker769075d2012-11-24 11:26:46 +01002820#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002821#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002822
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002823#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002824static void ssl_calc_finished_ssl(
2825 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002826{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002827 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002828 md5_context md5;
2829 sha1_context sha1;
2830
Paul Bakker5121ce52009-01-03 21:22:43 +00002831 unsigned char padbuf[48];
2832 unsigned char md5sum[16];
2833 unsigned char sha1sum[20];
2834
Paul Bakker48916f92012-09-16 19:57:18 +00002835 ssl_session *session = ssl->session_negotiate;
2836 if( !session )
2837 session = ssl->session;
2838
Paul Bakker1ef83d62012-04-11 12:09:53 +00002839 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2840
Paul Bakker48916f92012-09-16 19:57:18 +00002841 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2842 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
2844 /*
2845 * SSLv3:
2846 * hash =
2847 * MD5( master + pad2 +
2848 * MD5( handshake + sender + master + pad1 ) )
2849 * + SHA1( master + pad2 +
2850 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002851 */
2852
Paul Bakker90995b52013-06-24 19:20:35 +02002853#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002854 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002855 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002856#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002857
Paul Bakker90995b52013-06-24 19:20:35 +02002858#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002859 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002860 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002861#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Paul Bakker3c2122f2013-06-24 19:03:14 +02002863 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2864 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
Paul Bakker1ef83d62012-04-11 12:09:53 +00002866 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Paul Bakker3c2122f2013-06-24 19:03:14 +02002868 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002869 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002870 md5_update( &md5, padbuf, 48 );
2871 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Paul Bakker3c2122f2013-06-24 19:03:14 +02002873 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002874 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002875 sha1_update( &sha1, padbuf, 40 );
2876 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
Paul Bakker1ef83d62012-04-11 12:09:53 +00002878 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
Paul Bakker1ef83d62012-04-11 12:09:53 +00002880 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002881 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002882 md5_update( &md5, padbuf, 48 );
2883 md5_update( &md5, md5sum, 16 );
2884 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
Paul Bakker1ef83d62012-04-11 12:09:53 +00002886 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002887 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002888 sha1_update( &sha1, padbuf , 40 );
2889 sha1_update( &sha1, sha1sum, 20 );
2890 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakker1ef83d62012-04-11 12:09:53 +00002892 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Paul Bakker5b4af392014-06-26 12:09:34 +02002894 md5_free( &md5 );
2895 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
Paul Bakker34617722014-06-13 17:20:13 +02002897 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2898 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2899 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002900
2901 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2902}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002903#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002904
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002905#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002906static void ssl_calc_finished_tls(
2907 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002908{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002910 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002912 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002913 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Paul Bakker48916f92012-09-16 19:57:18 +00002915 ssl_session *session = ssl->session_negotiate;
2916 if( !session )
2917 session = ssl->session;
2918
Paul Bakker1ef83d62012-04-11 12:09:53 +00002919 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Paul Bakker48916f92012-09-16 19:57:18 +00002921 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2922 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924 /*
2925 * TLSv1:
2926 * hash = PRF( master, finished_label,
2927 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2928 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002929
Paul Bakker90995b52013-06-24 19:20:35 +02002930#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002931 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2932 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002933#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002934
Paul Bakker90995b52013-06-24 19:20:35 +02002935#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2937 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002938#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939
2940 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002941 ? "client finished"
2942 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943
2944 md5_finish( &md5, padbuf );
2945 sha1_finish( &sha1, padbuf + 16 );
2946
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002947 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002948 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949
2950 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2951
Paul Bakker5b4af392014-06-26 12:09:34 +02002952 md5_free( &md5 );
2953 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954
Paul Bakker34617722014-06-13 17:20:13 +02002955 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956
2957 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2958}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002959#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002961#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2962#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002963static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964 ssl_context *ssl, unsigned char *buf, int from )
2965{
2966 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002967 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002968 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969 unsigned char padbuf[32];
2970
Paul Bakker48916f92012-09-16 19:57:18 +00002971 ssl_session *session = ssl->session_negotiate;
2972 if( !session )
2973 session = ssl->session;
2974
Paul Bakker380da532012-04-18 16:10:25 +00002975 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976
Paul Bakker9e36f042013-06-30 14:34:05 +02002977 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002978
2979 /*
2980 * TLSv1.2:
2981 * hash = PRF( master, finished_label,
2982 * Hash( handshake ) )[0.11]
2983 */
2984
Paul Bakker9e36f042013-06-30 14:34:05 +02002985#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002987 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002988#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989
2990 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002991 ? "client finished"
2992 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002993
Paul Bakker9e36f042013-06-30 14:34:05 +02002994 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002996 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002997 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002998
2999 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3000
Paul Bakker5b4af392014-06-26 12:09:34 +02003001 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003002
Paul Bakker34617722014-06-13 17:20:13 +02003003 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003004
3005 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3006}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003007#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003008
Paul Bakker9e36f042013-06-30 14:34:05 +02003009#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003010static void ssl_calc_finished_tls_sha384(
3011 ssl_context *ssl, unsigned char *buf, int from )
3012{
3013 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003014 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003015 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003016 unsigned char padbuf[48];
3017
Paul Bakker48916f92012-09-16 19:57:18 +00003018 ssl_session *session = ssl->session_negotiate;
3019 if( !session )
3020 session = ssl->session;
3021
Paul Bakker380da532012-04-18 16:10:25 +00003022 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003023
Paul Bakker9e36f042013-06-30 14:34:05 +02003024 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003025
3026 /*
3027 * TLSv1.2:
3028 * hash = PRF( master, finished_label,
3029 * Hash( handshake ) )[0.11]
3030 */
3031
Paul Bakker9e36f042013-06-30 14:34:05 +02003032#if !defined(POLARSSL_SHA512_ALT)
3033 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3034 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003035#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003036
3037 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003038 ? "client finished"
3039 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003040
Paul Bakker9e36f042013-06-30 14:34:05 +02003041 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003042
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003043 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003044 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003045
3046 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3047
Paul Bakker5b4af392014-06-26 12:09:34 +02003048 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003049
Paul Bakker34617722014-06-13 17:20:13 +02003050 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003051
3052 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3053}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003054#endif /* POLARSSL_SHA512_C */
3055#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003056
Paul Bakker48916f92012-09-16 19:57:18 +00003057void ssl_handshake_wrapup( ssl_context *ssl )
3058{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003059 int resume = ssl->handshake->resume;
3060
Paul Bakker48916f92012-09-16 19:57:18 +00003061 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3062
3063 /*
3064 * Free our handshake params
3065 */
3066 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003067 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003068 ssl->handshake = NULL;
3069
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003070 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003071 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003072 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003073 ssl->renego_records_seen = 0;
3074 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003075
Paul Bakker48916f92012-09-16 19:57:18 +00003076 /*
3077 * Switch in our now active transform context
3078 */
3079 if( ssl->transform )
3080 {
3081 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003082 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003083 }
3084 ssl->transform = ssl->transform_negotiate;
3085 ssl->transform_negotiate = NULL;
3086
Paul Bakker0a597072012-09-25 21:55:46 +00003087 if( ssl->session )
3088 {
3089 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003090 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003091 }
3092 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003093 ssl->session_negotiate = NULL;
3094
Paul Bakker0a597072012-09-25 21:55:46 +00003095 /*
3096 * Add cache entry
3097 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003098 if( ssl->f_set_cache != NULL &&
3099 ssl->session->length != 0 &&
3100 resume == 0 )
3101 {
Paul Bakker0a597072012-09-25 21:55:46 +00003102 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3103 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003104 }
Paul Bakker0a597072012-09-25 21:55:46 +00003105
Paul Bakker48916f92012-09-16 19:57:18 +00003106 ssl->state++;
3107
3108 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3109}
3110
Paul Bakker1ef83d62012-04-11 12:09:53 +00003111int ssl_write_finished( ssl_context *ssl )
3112{
3113 int ret, hash_len;
3114
3115 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3116
Paul Bakker92be97b2013-01-02 17:30:03 +01003117 /*
3118 * Set the out_msg pointer to the correct location based on IV length
3119 */
3120 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3121 {
3122 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3123 ssl->transform_negotiate->fixed_ivlen;
3124 }
3125 else
3126 ssl->out_msg = ssl->out_iv;
3127
Paul Bakker48916f92012-09-16 19:57:18 +00003128 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003129
3130 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003131 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3132
Paul Bakker48916f92012-09-16 19:57:18 +00003133 ssl->verify_data_len = hash_len;
3134 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3135
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 ssl->out_msglen = 4 + hash_len;
3137 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3138 ssl->out_msg[0] = SSL_HS_FINISHED;
3139
3140 /*
3141 * In case of session resuming, invert the client and server
3142 * ChangeCipherSpec messages order.
3143 */
Paul Bakker0a597072012-09-25 21:55:46 +00003144 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 {
3146 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003147 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003148 else
3149 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3150 }
3151 else
3152 ssl->state++;
3153
Paul Bakker48916f92012-09-16 19:57:18 +00003154 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003155 * Switch to our negotiated transform and session parameters for outbound
3156 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003157 */
3158 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3159 ssl->transform_out = ssl->transform_negotiate;
3160 ssl->session_out = ssl->session_negotiate;
3161 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
Paul Bakker07eb38b2012-12-19 14:42:06 +01003163#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003164 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003165 {
3166 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3167 {
3168 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3169 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3170 }
3171 }
3172#endif
3173
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3175 {
3176 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3177 return( ret );
3178 }
3179
3180 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3181
3182 return( 0 );
3183}
3184
3185int ssl_parse_finished( ssl_context *ssl )
3186{
Paul Bakker23986e52011-04-24 08:57:21 +00003187 int ret;
3188 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003189 unsigned char buf[36];
3190
3191 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3192
Paul Bakker48916f92012-09-16 19:57:18 +00003193 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003194
Paul Bakker48916f92012-09-16 19:57:18 +00003195 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003196 * Switch to our negotiated transform and session parameters for inbound
3197 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003198 */
3199 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3200 ssl->transform_in = ssl->transform_negotiate;
3201 ssl->session_in = ssl->session_negotiate;
3202 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Paul Bakker92be97b2013-01-02 17:30:03 +01003204 /*
3205 * Set the in_msg pointer to the correct location based on IV length
3206 */
3207 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3208 {
3209 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3210 ssl->transform_negotiate->fixed_ivlen;
3211 }
3212 else
3213 ssl->in_msg = ssl->in_iv;
3214
Paul Bakker07eb38b2012-12-19 14:42:06 +01003215#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003216 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003217 {
3218 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3219 {
3220 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3221 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3222 }
3223 }
3224#endif
3225
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3227 {
3228 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3229 return( ret );
3230 }
3231
3232 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3233 {
3234 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003235 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
3237
Paul Bakker1ef83d62012-04-11 12:09:53 +00003238 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3240
3241 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3242 ssl->in_hslen != 4 + hash_len )
3243 {
3244 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003245 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 }
3247
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003248 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 {
3250 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003251 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 }
3253
Paul Bakker48916f92012-09-16 19:57:18 +00003254 ssl->verify_data_len = hash_len;
3255 memcpy( ssl->peer_verify_data, buf, hash_len );
3256
Paul Bakker0a597072012-09-25 21:55:46 +00003257 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 {
3259 if( ssl->endpoint == SSL_IS_CLIENT )
3260 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3261
3262 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003263 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003264 }
3265 else
3266 ssl->state++;
3267
3268 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3269
3270 return( 0 );
3271}
3272
Paul Bakker968afaa2014-07-09 11:09:24 +02003273static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003274{
3275 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3276
3277#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3278 defined(POLARSSL_SSL_PROTO_TLS1_1)
3279 md5_init( &handshake->fin_md5 );
3280 sha1_init( &handshake->fin_sha1 );
3281 md5_starts( &handshake->fin_md5 );
3282 sha1_starts( &handshake->fin_sha1 );
3283#endif
3284#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3285#if defined(POLARSSL_SHA256_C)
3286 sha256_init( &handshake->fin_sha256 );
3287 sha256_starts( &handshake->fin_sha256, 0 );
3288#endif
3289#if defined(POLARSSL_SHA512_C)
3290 sha512_init( &handshake->fin_sha512 );
3291 sha512_starts( &handshake->fin_sha512, 1 );
3292#endif
3293#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3294
3295 handshake->update_checksum = ssl_update_checksum_start;
3296 handshake->sig_alg = SSL_HASH_SHA1;
3297
3298#if defined(POLARSSL_DHM_C)
3299 dhm_init( &handshake->dhm_ctx );
3300#endif
3301#if defined(POLARSSL_ECDH_C)
3302 ecdh_init( &handshake->ecdh_ctx );
3303#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003304}
3305
3306static void ssl_transform_init( ssl_transform *transform )
3307{
3308 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003309
3310 cipher_init( &transform->cipher_ctx_enc );
3311 cipher_init( &transform->cipher_ctx_dec );
3312
3313 md_init( &transform->md_ctx_enc );
3314 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003315}
3316
3317void ssl_session_init( ssl_session *session )
3318{
3319 memset( session, 0, sizeof(ssl_session) );
3320}
3321
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003322static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003323{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003324 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003325 if( ssl->transform_negotiate )
3326 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003327 if( ssl->session_negotiate )
3328 ssl_session_free( ssl->session_negotiate );
3329 if( ssl->handshake )
3330 ssl_handshake_free( ssl->handshake );
3331
3332 /*
3333 * Either the pointers are now NULL or cleared properly and can be freed.
3334 * Now allocate missing structures.
3335 */
3336 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003337 {
3338 ssl->transform_negotiate =
3339 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3340 }
Paul Bakker48916f92012-09-16 19:57:18 +00003341
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003342 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003343 {
3344 ssl->session_negotiate =
3345 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3346 }
Paul Bakker48916f92012-09-16 19:57:18 +00003347
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003348 if( ssl->handshake == NULL)
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003349 {
3350 ssl->handshake = (ssl_handshake_params *)
3351 polarssl_malloc( sizeof(ssl_handshake_params) );
3352 }
Paul Bakker48916f92012-09-16 19:57:18 +00003353
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003354 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003355 if( ssl->handshake == NULL ||
3356 ssl->transform_negotiate == NULL ||
3357 ssl->session_negotiate == NULL )
3358 {
3359 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003360
3361 polarssl_free( ssl->handshake );
3362 polarssl_free( ssl->transform_negotiate );
3363 polarssl_free( ssl->session_negotiate );
3364
3365 ssl->handshake = NULL;
3366 ssl->transform_negotiate = NULL;
3367 ssl->session_negotiate = NULL;
3368
Paul Bakker48916f92012-09-16 19:57:18 +00003369 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3370 }
3371
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003372 /* Initialize structures */
3373 ssl_session_init( ssl->session_negotiate );
3374 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003375 ssl_handshake_params_init( ssl->handshake );
3376
3377#if defined(POLARSSL_X509_CRT_PARSE_C)
3378 ssl->handshake->key_cert = ssl->key_cert;
3379#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003380
Paul Bakker48916f92012-09-16 19:57:18 +00003381 return( 0 );
3382}
3383
Paul Bakker5121ce52009-01-03 21:22:43 +00003384/*
3385 * Initialize an SSL context
3386 */
3387int ssl_init( ssl_context *ssl )
3388{
Paul Bakker48916f92012-09-16 19:57:18 +00003389 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 int len = SSL_BUFFER_LEN;
3391
3392 memset( ssl, 0, sizeof( ssl_context ) );
3393
Paul Bakker62f2dee2012-09-28 07:31:51 +00003394 /*
3395 * Sane defaults
3396 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003397 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3398 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3399 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3400 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003401
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003402 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003403
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003404 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3405
Paul Bakker62f2dee2012-09-28 07:31:51 +00003406#if defined(POLARSSL_DHM_C)
3407 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3408 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3409 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3410 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3411 {
3412 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3413 return( ret );
3414 }
3415#endif
3416
3417 /*
3418 * Prepare base structures
3419 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003420 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003422 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003423 ssl->in_msg = ssl->in_ctr + 13;
3424
3425 if( ssl->in_ctr == NULL )
3426 {
3427 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003428 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 }
3430
Paul Bakker6e339b52013-07-03 13:37:05 +02003431 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003433 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003434 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003435
3436 if( ssl->out_ctr == NULL )
3437 {
3438 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003439 polarssl_free( ssl->in_ctr );
3440 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003441 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 }
3443
3444 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3445 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3446
Paul Bakker606b4ba2013-08-14 16:52:14 +02003447#if defined(POLARSSL_SSL_SESSION_TICKETS)
3448 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3449#endif
3450
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003451#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003452 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003453#endif
3454
Paul Bakker48916f92012-09-16 19:57:18 +00003455 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3456 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457
3458 return( 0 );
3459}
3460
3461/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003462 * Reset an initialized and used SSL context for re-use while retaining
3463 * all application-set variables, function pointers and data.
3464 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003465int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003466{
Paul Bakker48916f92012-09-16 19:57:18 +00003467 int ret;
3468
Paul Bakker7eb013f2011-10-06 12:37:39 +00003469 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003470 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3471 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3472
3473 ssl->verify_data_len = 0;
3474 memset( ssl->own_verify_data, 0, 36 );
3475 memset( ssl->peer_verify_data, 0, 36 );
3476
Paul Bakker7eb013f2011-10-06 12:37:39 +00003477 ssl->in_offt = NULL;
3478
Paul Bakker92be97b2013-01-02 17:30:03 +01003479 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003480 ssl->in_msgtype = 0;
3481 ssl->in_msglen = 0;
3482 ssl->in_left = 0;
3483
3484 ssl->in_hslen = 0;
3485 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003486 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003487
Paul Bakker92be97b2013-01-02 17:30:03 +01003488 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003489 ssl->out_msgtype = 0;
3490 ssl->out_msglen = 0;
3491 ssl->out_left = 0;
3492
Paul Bakker48916f92012-09-16 19:57:18 +00003493 ssl->transform_in = NULL;
3494 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003495
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003496 ssl->renego_records_seen = 0;
3497
Paul Bakker7eb013f2011-10-06 12:37:39 +00003498 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3499 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003500
3501#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003502 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003503 {
3504 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003505 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003506 {
3507 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3508 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3509 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003510 }
3511#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003512
Paul Bakker48916f92012-09-16 19:57:18 +00003513 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003514 {
Paul Bakker48916f92012-09-16 19:57:18 +00003515 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003516 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003517 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003518 }
Paul Bakker48916f92012-09-16 19:57:18 +00003519
Paul Bakkerc0463502013-02-14 11:19:38 +01003520 if( ssl->session )
3521 {
3522 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003523 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003524 ssl->session = NULL;
3525 }
3526
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003527#if defined(POLARSSL_SSL_ALPN)
3528 ssl->alpn_chosen = NULL;
3529#endif
3530
Paul Bakker48916f92012-09-16 19:57:18 +00003531 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3532 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003533
3534 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003535}
3536
Paul Bakkera503a632013-08-14 13:48:06 +02003537#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003538static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3539{
3540 aes_free( &tkeys->enc );
3541 aes_free( &tkeys->dec );
3542
3543 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3544}
3545
Paul Bakker7eb013f2011-10-06 12:37:39 +00003546/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003547 * Allocate and initialize ticket keys
3548 */
3549static int ssl_ticket_keys_init( ssl_context *ssl )
3550{
3551 int ret;
3552 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003553 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003554
3555 if( ssl->ticket_keys != NULL )
3556 return( 0 );
3557
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003558 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3559 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003560 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3561
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003562 aes_init( &tkeys->enc );
3563 aes_init( &tkeys->dec );
3564
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003565 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003566 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003567 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003568 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003569 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003570 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003571
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003572 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3573 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3574 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3575 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003576 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003577 polarssl_free( tkeys );
3578 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003579 }
3580
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003581 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003582 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003583 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003584 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003585 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003586 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003587
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003588 ssl->ticket_keys = tkeys;
3589
3590 return( 0 );
3591}
Paul Bakkera503a632013-08-14 13:48:06 +02003592#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003593
3594/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 * SSL set accessors
3596 */
3597void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3598{
3599 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003600
Paul Bakker606b4ba2013-08-14 16:52:14 +02003601#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003602 if( endpoint == SSL_IS_CLIENT )
3603 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003605}
3606
3607void ssl_set_authmode( ssl_context *ssl, int authmode )
3608{
3609 ssl->authmode = authmode;
3610}
3611
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003612#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003613void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003614 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003615 void *p_vrfy )
3616{
3617 ssl->f_vrfy = f_vrfy;
3618 ssl->p_vrfy = p_vrfy;
3619}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003620#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003621
Paul Bakker5121ce52009-01-03 21:22:43 +00003622void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003623 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 void *p_rng )
3625{
3626 ssl->f_rng = f_rng;
3627 ssl->p_rng = p_rng;
3628}
3629
3630void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003631 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003632 void *p_dbg )
3633{
3634 ssl->f_dbg = f_dbg;
3635 ssl->p_dbg = p_dbg;
3636}
3637
3638void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003639 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003640 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003641{
3642 ssl->f_recv = f_recv;
3643 ssl->f_send = f_send;
3644 ssl->p_recv = p_recv;
3645 ssl->p_send = p_send;
3646}
3647
Paul Bakker0a597072012-09-25 21:55:46 +00003648void ssl_set_session_cache( ssl_context *ssl,
3649 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3650 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003651{
Paul Bakker0a597072012-09-25 21:55:46 +00003652 ssl->f_get_cache = f_get_cache;
3653 ssl->p_get_cache = p_get_cache;
3654 ssl->f_set_cache = f_set_cache;
3655 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003656}
3657
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003658int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003659{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003660 int ret;
3661
3662 if( ssl == NULL ||
3663 session == NULL ||
3664 ssl->session_negotiate == NULL ||
3665 ssl->endpoint != SSL_IS_CLIENT )
3666 {
3667 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3668 }
3669
3670 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3671 return( ret );
3672
Paul Bakker0a597072012-09-25 21:55:46 +00003673 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003674
3675 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003676}
3677
Paul Bakkerb68cad62012-08-23 08:34:18 +00003678void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003679{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003680 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3681 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3682 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3683 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3684}
3685
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003686void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3687 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003688 int major, int minor )
3689{
3690 if( major != SSL_MAJOR_VERSION_3 )
3691 return;
3692
3693 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3694 return;
3695
3696 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003697}
3698
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003699#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003700/* Add a new (empty) key_cert entry an return a pointer to it */
3701static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3702{
3703 ssl_key_cert *key_cert, *last;
3704
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003705 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3706 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003707 return( NULL );
3708
3709 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3710
3711 /* Append the new key_cert to the (possibly empty) current list */
3712 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003713 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003714 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003715 if( ssl->handshake != NULL )
3716 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003717 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003718 else
3719 {
3720 last = ssl->key_cert;
3721 while( last->next != NULL )
3722 last = last->next;
3723 last->next = key_cert;
3724 }
3725
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003726 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003727}
3728
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003729void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003730 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003731{
3732 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003733 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003734 ssl->peer_cn = peer_cn;
3735}
3736
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003737int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003738 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003739{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003740 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3741
3742 if( key_cert == NULL )
3743 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3744
3745 key_cert->cert = own_cert;
3746 key_cert->key = pk_key;
3747
3748 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003749}
3750
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003751#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003752int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003753 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003754{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003755 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003756 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003757
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003758 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003759 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3760
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003761 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3762 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003763 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003764
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003765 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003766
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003767 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003768 if( ret != 0 )
3769 return( ret );
3770
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003771 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003772 return( ret );
3773
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003774 key_cert->cert = own_cert;
3775 key_cert->key_own_alloc = 1;
3776
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003777 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003778}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003779#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003780
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003781int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003782 void *rsa_key,
3783 rsa_decrypt_func rsa_decrypt,
3784 rsa_sign_func rsa_sign,
3785 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003786{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003787 int ret;
3788 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003789
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003790 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003791 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3792
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003793 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3794 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003795 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003796
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003797 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003798
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003799 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3800 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3801 return( ret );
3802
3803 key_cert->cert = own_cert;
3804 key_cert->key_own_alloc = 1;
3805
3806 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003807}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003808#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003809
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003810#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003811int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3812 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003813{
Paul Bakker6db455e2013-09-18 17:29:31 +02003814 if( psk == NULL || psk_identity == NULL )
3815 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3816
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003817 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003818 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3819
Paul Bakker6db455e2013-09-18 17:29:31 +02003820 if( ssl->psk != NULL )
3821 {
3822 polarssl_free( ssl->psk );
3823 polarssl_free( ssl->psk_identity );
3824 }
3825
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003826 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003827 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003828
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003829 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003830 ssl->psk_identity = (unsigned char *)
3831 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003832
3833 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3834 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3835
3836 memcpy( ssl->psk, psk, ssl->psk_len );
3837 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003838
3839 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003840}
3841
3842void ssl_set_psk_cb( ssl_context *ssl,
3843 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3844 size_t),
3845 void *p_psk )
3846{
3847 ssl->f_psk = f_psk;
3848 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003849}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003850#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003851
Paul Bakker48916f92012-09-16 19:57:18 +00003852#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003853int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003854{
3855 int ret;
3856
Paul Bakker48916f92012-09-16 19:57:18 +00003857 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 {
3859 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3860 return( ret );
3861 }
3862
Paul Bakker48916f92012-09-16 19:57:18 +00003863 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003864 {
3865 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3866 return( ret );
3867 }
3868
3869 return( 0 );
3870}
3871
Paul Bakker1b57b062011-01-06 15:48:19 +00003872int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3873{
3874 int ret;
3875
Paul Bakker66d5d072014-06-17 16:39:18 +02003876 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003877 {
3878 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3879 return( ret );
3880 }
3881
Paul Bakker66d5d072014-06-17 16:39:18 +02003882 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003883 {
3884 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3885 return( ret );
3886 }
3887
3888 return( 0 );
3889}
Paul Bakker48916f92012-09-16 19:57:18 +00003890#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003891
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003892#if defined(POLARSSL_SSL_SET_CURVES)
3893/*
3894 * Set the allowed elliptic curves
3895 */
3896void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3897{
3898 ssl->curve_list = curve_list;
3899}
3900#endif
3901
Paul Bakker0be444a2013-08-27 21:55:01 +02003902#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003903int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003904{
3905 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003906 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003907
3908 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003909
3910 if( ssl->hostname_len + 1 == 0 )
3911 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3912
Paul Bakker6e339b52013-07-03 13:37:05 +02003913 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003914
Paul Bakkerb15b8512012-01-13 13:44:06 +00003915 if( ssl->hostname == NULL )
3916 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3917
Paul Bakker3c2122f2013-06-24 19:03:14 +02003918 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003919 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003920
Paul Bakker40ea7de2009-05-03 10:18:48 +00003921 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003922
3923 return( 0 );
3924}
3925
Paul Bakker5701cdc2012-09-27 21:49:42 +00003926void ssl_set_sni( ssl_context *ssl,
3927 int (*f_sni)(void *, ssl_context *,
3928 const unsigned char *, size_t),
3929 void *p_sni )
3930{
3931 ssl->f_sni = f_sni;
3932 ssl->p_sni = p_sni;
3933}
Paul Bakker0be444a2013-08-27 21:55:01 +02003934#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003935
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003936#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003937int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003938{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003939 size_t cur_len, tot_len;
3940 const char **p;
3941
3942 /*
3943 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3944 * truncated". Check lengths now rather than later.
3945 */
3946 tot_len = 0;
3947 for( p = protos; *p != NULL; p++ )
3948 {
3949 cur_len = strlen( *p );
3950 tot_len += cur_len;
3951
3952 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3953 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3954 }
3955
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003956 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003957
3958 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003959}
3960
3961const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3962{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003963 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003964}
3965#endif /* POLARSSL_SSL_ALPN */
3966
Paul Bakker490ecc82011-10-06 13:04:09 +00003967void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3968{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003969 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3970 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3971 {
3972 ssl->max_major_ver = major;
3973 ssl->max_minor_ver = minor;
3974 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003975}
3976
Paul Bakker1d29fb52012-09-28 13:28:45 +00003977void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3978{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003979 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3980 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3981 {
3982 ssl->min_major_ver = major;
3983 ssl->min_minor_ver = minor;
3984 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003985}
3986
Paul Bakker05decb22013-08-15 13:33:48 +02003987#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003988int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3989{
Paul Bakker77e257e2013-12-16 15:29:52 +01003990 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003991 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003992 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003993 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003994 }
3995
3996 ssl->mfl_code = mfl_code;
3997
3998 return( 0 );
3999}
Paul Bakker05decb22013-08-15 13:33:48 +02004000#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004001
Paul Bakker1f2bc622013-08-15 13:45:55 +02004002#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004003int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004004{
4005 if( ssl->endpoint != SSL_IS_CLIENT )
4006 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4007
Paul Bakker8c1ede62013-07-19 14:14:37 +02004008 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004009
4010 return( 0 );
4011}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004012#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004013
Paul Bakker48916f92012-09-16 19:57:18 +00004014void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4015{
4016 ssl->disable_renegotiation = renegotiation;
4017}
4018
4019void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4020{
4021 ssl->allow_legacy_renegotiation = allow_legacy;
4022}
4023
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004024void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4025{
4026 ssl->renego_max_records = max_records;
4027}
4028
Paul Bakkera503a632013-08-14 13:48:06 +02004029#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004030int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4031{
4032 ssl->session_tickets = use_tickets;
4033
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004034 if( ssl->endpoint == SSL_IS_CLIENT )
4035 return( 0 );
4036
4037 if( ssl->f_rng == NULL )
4038 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4039
4040 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004041}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004042
4043void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4044{
4045 ssl->ticket_lifetime = lifetime;
4046}
Paul Bakkera503a632013-08-14 13:48:06 +02004047#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004048
Paul Bakker5121ce52009-01-03 21:22:43 +00004049/*
4050 * SSL get accessors
4051 */
Paul Bakker23986e52011-04-24 08:57:21 +00004052size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004053{
4054 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4055}
4056
Paul Bakkerff60ee62010-03-16 21:09:09 +00004057int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004058{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004059 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004060}
4061
Paul Bakkere3166ce2011-01-27 17:40:50 +00004062const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004063{
Paul Bakker926c8e42013-03-06 10:23:34 +01004064 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004065 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004066
Paul Bakkere3166ce2011-01-27 17:40:50 +00004067 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004068}
4069
Paul Bakker43ca69c2011-01-15 17:35:19 +00004070const char *ssl_get_version( const ssl_context *ssl )
4071{
4072 switch( ssl->minor_ver )
4073 {
4074 case SSL_MINOR_VERSION_0:
4075 return( "SSLv3.0" );
4076
4077 case SSL_MINOR_VERSION_1:
4078 return( "TLSv1.0" );
4079
4080 case SSL_MINOR_VERSION_2:
4081 return( "TLSv1.1" );
4082
Paul Bakker1ef83d62012-04-11 12:09:53 +00004083 case SSL_MINOR_VERSION_3:
4084 return( "TLSv1.2" );
4085
Paul Bakker43ca69c2011-01-15 17:35:19 +00004086 default:
4087 break;
4088 }
4089 return( "unknown" );
4090}
4091
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004092#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004093const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004094{
4095 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004096 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004097
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004098 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004099}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004100#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004101
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004102int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4103{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004104 if( ssl == NULL ||
4105 dst == NULL ||
4106 ssl->session == NULL ||
4107 ssl->endpoint != SSL_IS_CLIENT )
4108 {
4109 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4110 }
4111
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004112 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004113}
4114
Paul Bakker5121ce52009-01-03 21:22:43 +00004115/*
Paul Bakker1961b702013-01-25 14:49:24 +01004116 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004117 */
Paul Bakker1961b702013-01-25 14:49:24 +01004118int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004119{
Paul Bakker40e46942009-01-03 21:51:57 +00004120 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004121
Paul Bakker40e46942009-01-03 21:51:57 +00004122#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004123 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004124 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004125#endif
4126
Paul Bakker40e46942009-01-03 21:51:57 +00004127#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004128 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004129 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004130#endif
4131
Paul Bakker1961b702013-01-25 14:49:24 +01004132 return( ret );
4133}
4134
4135/*
4136 * Perform the SSL handshake
4137 */
4138int ssl_handshake( ssl_context *ssl )
4139{
4140 int ret = 0;
4141
4142 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4143
4144 while( ssl->state != SSL_HANDSHAKE_OVER )
4145 {
4146 ret = ssl_handshake_step( ssl );
4147
4148 if( ret != 0 )
4149 break;
4150 }
4151
Paul Bakker5121ce52009-01-03 21:22:43 +00004152 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4153
4154 return( ret );
4155}
4156
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004157#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004158/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004159 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004160 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004161static int ssl_write_hello_request( ssl_context *ssl )
4162{
4163 int ret;
4164
4165 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4166
4167 ssl->out_msglen = 4;
4168 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4169 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4170
4171 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4172 {
4173 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4174 return( ret );
4175 }
4176
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004177 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4178
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004179 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4180
4181 return( 0 );
4182}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004183#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004184
4185/*
4186 * Actually renegotiate current connection, triggered by either:
4187 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004188 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004189 * - receiving any handshake message on server during ssl_read() after the
4190 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004191 * If the handshake doesn't complete due to waiting for I/O, it will continue
4192 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004193 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004194static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004195{
4196 int ret;
4197
4198 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4199
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004200 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4201 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004202
4203 ssl->state = SSL_HELLO_REQUEST;
4204 ssl->renegotiation = SSL_RENEGOTIATION;
4205
Paul Bakker48916f92012-09-16 19:57:18 +00004206 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4207 {
4208 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4209 return( ret );
4210 }
4211
4212 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4213
4214 return( 0 );
4215}
4216
4217/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004218 * Renegotiate current connection on client,
4219 * or request renegotiation on server
4220 */
4221int ssl_renegotiate( ssl_context *ssl )
4222{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004223 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004224
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004225#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004226 /* On server, just send the request */
4227 if( ssl->endpoint == SSL_IS_SERVER )
4228 {
4229 if( ssl->state != SSL_HANDSHAKE_OVER )
4230 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4231
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004232 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004233 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004234#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004235
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004236#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004237 /*
4238 * On client, either start the renegotiation process or,
4239 * if already in progress, continue the handshake
4240 */
4241 if( ssl->renegotiation != SSL_RENEGOTIATION )
4242 {
4243 if( ssl->state != SSL_HANDSHAKE_OVER )
4244 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4245
4246 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4247 {
4248 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4249 return( ret );
4250 }
4251 }
4252 else
4253 {
4254 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4255 {
4256 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4257 return( ret );
4258 }
4259 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004260#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004261
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004262 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004263}
4264
4265/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004266 * Receive application data decrypted from the SSL layer
4267 */
Paul Bakker23986e52011-04-24 08:57:21 +00004268int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004269{
Paul Bakker23986e52011-04-24 08:57:21 +00004270 int ret;
4271 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004272
4273 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4274
4275 if( ssl->state != SSL_HANDSHAKE_OVER )
4276 {
4277 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4278 {
4279 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4280 return( ret );
4281 }
4282 }
4283
4284 if( ssl->in_offt == NULL )
4285 {
4286 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4287 {
Paul Bakker831a7552011-05-18 13:32:51 +00004288 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4289 return( 0 );
4290
Paul Bakker5121ce52009-01-03 21:22:43 +00004291 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4292 return( ret );
4293 }
4294
4295 if( ssl->in_msglen == 0 &&
4296 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4297 {
4298 /*
4299 * OpenSSL sends empty messages to randomize the IV
4300 */
4301 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4302 {
Paul Bakker831a7552011-05-18 13:32:51 +00004303 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4304 return( 0 );
4305
Paul Bakker5121ce52009-01-03 21:22:43 +00004306 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4307 return( ret );
4308 }
4309 }
4310
Paul Bakker48916f92012-09-16 19:57:18 +00004311 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4312 {
4313 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4314
4315 if( ssl->endpoint == SSL_IS_CLIENT &&
4316 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4317 ssl->in_hslen != 4 ) )
4318 {
4319 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4320 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4321 }
4322
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004323 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4324 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004325 ssl->allow_legacy_renegotiation ==
4326 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004327 {
4328 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4329
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004330#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004331 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004332 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004333 /*
4334 * SSLv3 does not have a "no_renegotiation" alert
4335 */
4336 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4337 return( ret );
4338 }
4339 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004340#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004341#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4342 defined(POLARSSL_SSL_PROTO_TLS1_2)
4343 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004344 {
4345 if( ( ret = ssl_send_alert_message( ssl,
4346 SSL_ALERT_LEVEL_WARNING,
4347 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4348 {
4349 return( ret );
4350 }
Paul Bakker48916f92012-09-16 19:57:18 +00004351 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004352 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004353#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4354 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004355 {
4356 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004357 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004358 }
Paul Bakker48916f92012-09-16 19:57:18 +00004359 }
4360 else
4361 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004362 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004363 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004364 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004365 return( ret );
4366 }
4367
4368 return( POLARSSL_ERR_NET_WANT_READ );
4369 }
4370 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004371 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4372 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004373 ssl->renego_records_seen++;
4374
4375 if( ssl->renego_max_records >= 0 &&
4376 ssl->renego_records_seen > ssl->renego_max_records )
4377 {
4378 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4379 "but not honored by client" ) );
4380 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4381 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004382 }
Paul Bakker48916f92012-09-16 19:57:18 +00004383 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004384 {
4385 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004386 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004387 }
4388
4389 ssl->in_offt = ssl->in_msg;
4390 }
4391
4392 n = ( len < ssl->in_msglen )
4393 ? len : ssl->in_msglen;
4394
4395 memcpy( buf, ssl->in_offt, n );
4396 ssl->in_msglen -= n;
4397
4398 if( ssl->in_msglen == 0 )
4399 /* all bytes consumed */
4400 ssl->in_offt = NULL;
4401 else
4402 /* more data available */
4403 ssl->in_offt += n;
4404
4405 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4406
Paul Bakker23986e52011-04-24 08:57:21 +00004407 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004408}
4409
4410/*
4411 * Send application data to be encrypted by the SSL layer
4412 */
Paul Bakker23986e52011-04-24 08:57:21 +00004413int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004414{
Paul Bakker23986e52011-04-24 08:57:21 +00004415 int ret;
4416 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004417 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004418
4419 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4420
4421 if( ssl->state != SSL_HANDSHAKE_OVER )
4422 {
4423 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4424 {
4425 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4426 return( ret );
4427 }
4428 }
4429
Paul Bakker05decb22013-08-15 13:33:48 +02004430#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004431 /*
4432 * Assume mfl_code is correct since it was checked when set
4433 */
4434 max_len = mfl_code_to_length[ssl->mfl_code];
4435
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004436 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004437 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004438 */
4439 if( ssl->session_out != NULL &&
4440 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4441 {
4442 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4443 }
Paul Bakker05decb22013-08-15 13:33:48 +02004444#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004445
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004446 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004447
Paul Bakker5121ce52009-01-03 21:22:43 +00004448 if( ssl->out_left != 0 )
4449 {
4450 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4451 {
4452 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4453 return( ret );
4454 }
4455 }
Paul Bakker887bd502011-06-08 13:10:54 +00004456 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004457 {
Paul Bakker887bd502011-06-08 13:10:54 +00004458 ssl->out_msglen = n;
4459 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4460 memcpy( ssl->out_msg, buf, n );
4461
4462 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4463 {
4464 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4465 return( ret );
4466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004467 }
4468
4469 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4470
Paul Bakker23986e52011-04-24 08:57:21 +00004471 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004472}
4473
4474/*
4475 * Notify the peer that the connection is being closed
4476 */
4477int ssl_close_notify( ssl_context *ssl )
4478{
4479 int ret;
4480
4481 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4482
4483 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4484 {
4485 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4486 return( ret );
4487 }
4488
4489 if( ssl->state == SSL_HANDSHAKE_OVER )
4490 {
Paul Bakker48916f92012-09-16 19:57:18 +00004491 if( ( ret = ssl_send_alert_message( ssl,
4492 SSL_ALERT_LEVEL_WARNING,
4493 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004495 return( ret );
4496 }
4497 }
4498
4499 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4500
4501 return( ret );
4502}
4503
Paul Bakker48916f92012-09-16 19:57:18 +00004504void ssl_transform_free( ssl_transform *transform )
4505{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004506 if( transform == NULL )
4507 return;
4508
Paul Bakker48916f92012-09-16 19:57:18 +00004509#if defined(POLARSSL_ZLIB_SUPPORT)
4510 deflateEnd( &transform->ctx_deflate );
4511 inflateEnd( &transform->ctx_inflate );
4512#endif
4513
Paul Bakker84bbeb52014-07-01 14:53:22 +02004514 cipher_free( &transform->cipher_ctx_enc );
4515 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004516
Paul Bakker84bbeb52014-07-01 14:53:22 +02004517 md_free( &transform->md_ctx_enc );
4518 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004519
Paul Bakker34617722014-06-13 17:20:13 +02004520 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004521}
4522
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004523#if defined(POLARSSL_X509_CRT_PARSE_C)
4524static void ssl_key_cert_free( ssl_key_cert *key_cert )
4525{
4526 ssl_key_cert *cur = key_cert, *next;
4527
4528 while( cur != NULL )
4529 {
4530 next = cur->next;
4531
4532 if( cur->key_own_alloc )
4533 {
4534 pk_free( cur->key );
4535 polarssl_free( cur->key );
4536 }
4537 polarssl_free( cur );
4538
4539 cur = next;
4540 }
4541}
4542#endif /* POLARSSL_X509_CRT_PARSE_C */
4543
Paul Bakker48916f92012-09-16 19:57:18 +00004544void ssl_handshake_free( ssl_handshake_params *handshake )
4545{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004546 if( handshake == NULL )
4547 return;
4548
Paul Bakker48916f92012-09-16 19:57:18 +00004549#if defined(POLARSSL_DHM_C)
4550 dhm_free( &handshake->dhm_ctx );
4551#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004552#if defined(POLARSSL_ECDH_C)
4553 ecdh_free( &handshake->ecdh_ctx );
4554#endif
4555
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004556#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004557 /* explicit void pointer cast for buggy MS compiler */
4558 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004559#endif
4560
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004561#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4562 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4563 /*
4564 * Free only the linked list wrapper, not the keys themselves
4565 * since the belong to the SNI callback
4566 */
4567 if( handshake->sni_key_cert != NULL )
4568 {
4569 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4570
4571 while( cur != NULL )
4572 {
4573 next = cur->next;
4574 polarssl_free( cur );
4575 cur = next;
4576 }
4577 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004578#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004579
Paul Bakker34617722014-06-13 17:20:13 +02004580 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004581}
4582
4583void ssl_session_free( ssl_session *session )
4584{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004585 if( session == NULL )
4586 return;
4587
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004588#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004589 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004590 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004591 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004592 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004593 }
Paul Bakkered27a042013-04-18 22:46:23 +02004594#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004595
Paul Bakkera503a632013-08-14 13:48:06 +02004596#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004597 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004598#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004599
Paul Bakker34617722014-06-13 17:20:13 +02004600 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004601}
4602
Paul Bakker5121ce52009-01-03 21:22:43 +00004603/*
4604 * Free an SSL context
4605 */
4606void ssl_free( ssl_context *ssl )
4607{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004608 if( ssl == NULL )
4609 return;
4610
Paul Bakker5121ce52009-01-03 21:22:43 +00004611 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4612
Paul Bakker5121ce52009-01-03 21:22:43 +00004613 if( ssl->out_ctr != NULL )
4614 {
Paul Bakker34617722014-06-13 17:20:13 +02004615 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004616 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 }
4618
4619 if( ssl->in_ctr != NULL )
4620 {
Paul Bakker34617722014-06-13 17:20:13 +02004621 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004622 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004623 }
4624
Paul Bakker16770332013-10-11 09:59:44 +02004625#if defined(POLARSSL_ZLIB_SUPPORT)
4626 if( ssl->compress_buf != NULL )
4627 {
Paul Bakker34617722014-06-13 17:20:13 +02004628 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004629 polarssl_free( ssl->compress_buf );
4630 }
4631#endif
4632
Paul Bakker40e46942009-01-03 21:51:57 +00004633#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004634 mpi_free( &ssl->dhm_P );
4635 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004636#endif
4637
Paul Bakker48916f92012-09-16 19:57:18 +00004638 if( ssl->transform )
4639 {
4640 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004641 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004642 }
4643
4644 if( ssl->handshake )
4645 {
4646 ssl_handshake_free( ssl->handshake );
4647 ssl_transform_free( ssl->transform_negotiate );
4648 ssl_session_free( ssl->session_negotiate );
4649
Paul Bakker6e339b52013-07-03 13:37:05 +02004650 polarssl_free( ssl->handshake );
4651 polarssl_free( ssl->transform_negotiate );
4652 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004653 }
4654
Paul Bakkerc0463502013-02-14 11:19:38 +01004655 if( ssl->session )
4656 {
4657 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004658 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004659 }
4660
Paul Bakkera503a632013-08-14 13:48:06 +02004661#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004662 if( ssl->ticket_keys )
4663 {
4664 ssl_ticket_keys_free( ssl->ticket_keys );
4665 polarssl_free( ssl->ticket_keys );
4666 }
Paul Bakkera503a632013-08-14 13:48:06 +02004667#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004668
Paul Bakker0be444a2013-08-27 21:55:01 +02004669#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004670 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004671 {
Paul Bakker34617722014-06-13 17:20:13 +02004672 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004673 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004674 ssl->hostname_len = 0;
4675 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004676#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004677
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004678#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004679 if( ssl->psk != NULL )
4680 {
Paul Bakker34617722014-06-13 17:20:13 +02004681 polarssl_zeroize( ssl->psk, ssl->psk_len );
4682 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004683 polarssl_free( ssl->psk );
4684 polarssl_free( ssl->psk_identity );
4685 ssl->psk_len = 0;
4686 ssl->psk_identity_len = 0;
4687 }
4688#endif
4689
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004690#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004691 ssl_key_cert_free( ssl->key_cert );
4692#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004693
Paul Bakker05ef8352012-05-08 09:17:57 +00004694#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4695 if( ssl_hw_record_finish != NULL )
4696 {
4697 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4698 ssl_hw_record_finish( ssl );
4699 }
4700#endif
4701
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004703
Paul Bakker86f04f42013-02-14 11:20:09 +01004704 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004705 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004706}
4707
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004708#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004709/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004710 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004711 */
4712unsigned char ssl_sig_from_pk( pk_context *pk )
4713{
4714#if defined(POLARSSL_RSA_C)
4715 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4716 return( SSL_SIG_RSA );
4717#endif
4718#if defined(POLARSSL_ECDSA_C)
4719 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4720 return( SSL_SIG_ECDSA );
4721#endif
4722 return( SSL_SIG_ANON );
4723}
4724
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004725pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4726{
4727 switch( sig )
4728 {
4729#if defined(POLARSSL_RSA_C)
4730 case SSL_SIG_RSA:
4731 return( POLARSSL_PK_RSA );
4732#endif
4733#if defined(POLARSSL_ECDSA_C)
4734 case SSL_SIG_ECDSA:
4735 return( POLARSSL_PK_ECDSA );
4736#endif
4737 default:
4738 return( POLARSSL_PK_NONE );
4739 }
4740}
Paul Bakker9af723c2014-05-01 13:03:14 +02004741#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004742
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004743/*
4744 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4745 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004746md_type_t ssl_md_alg_from_hash( unsigned char hash )
4747{
4748 switch( hash )
4749 {
4750#if defined(POLARSSL_MD5_C)
4751 case SSL_HASH_MD5:
4752 return( POLARSSL_MD_MD5 );
4753#endif
4754#if defined(POLARSSL_SHA1_C)
4755 case SSL_HASH_SHA1:
4756 return( POLARSSL_MD_SHA1 );
4757#endif
4758#if defined(POLARSSL_SHA256_C)
4759 case SSL_HASH_SHA224:
4760 return( POLARSSL_MD_SHA224 );
4761 case SSL_HASH_SHA256:
4762 return( POLARSSL_MD_SHA256 );
4763#endif
4764#if defined(POLARSSL_SHA512_C)
4765 case SSL_HASH_SHA384:
4766 return( POLARSSL_MD_SHA384 );
4767 case SSL_HASH_SHA512:
4768 return( POLARSSL_MD_SHA512 );
4769#endif
4770 default:
4771 return( POLARSSL_MD_NONE );
4772 }
4773}
4774
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004775#if defined(POLARSSL_SSL_SET_CURVES)
4776/*
4777 * Check is a curve proposed by the peer is in our list.
4778 * Return 1 if we're willing to use it, 0 otherwise.
4779 */
4780int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4781{
4782 const ecp_group_id *gid;
4783
4784 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4785 if( *gid == grp_id )
4786 return( 1 );
4787
4788 return( 0 );
4789}
Paul Bakker9af723c2014-05-01 13:03:14 +02004790#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004791
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004792#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004793int ssl_check_cert_usage( const x509_crt *cert,
4794 const ssl_ciphersuite_t *ciphersuite,
4795 int cert_endpoint )
4796{
4797#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4798 int usage = 0;
4799#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004800#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4801 const char *ext_oid;
4802 size_t ext_len;
4803#endif
4804
4805#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4806 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4807 ((void) cert);
4808 ((void) cert_endpoint);
4809#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004810
4811#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4812 if( cert_endpoint == SSL_IS_SERVER )
4813 {
4814 /* Server part of the key exchange */
4815 switch( ciphersuite->key_exchange )
4816 {
4817 case POLARSSL_KEY_EXCHANGE_RSA:
4818 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4819 usage = KU_KEY_ENCIPHERMENT;
4820 break;
4821
4822 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4823 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4824 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4825 usage = KU_DIGITAL_SIGNATURE;
4826 break;
4827
4828 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4829 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4830 usage = KU_KEY_AGREEMENT;
4831 break;
4832
4833 /* Don't use default: we want warnings when adding new values */
4834 case POLARSSL_KEY_EXCHANGE_NONE:
4835 case POLARSSL_KEY_EXCHANGE_PSK:
4836 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4837 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4838 usage = 0;
4839 }
4840 }
4841 else
4842 {
4843 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4844 usage = KU_DIGITAL_SIGNATURE;
4845 }
4846
4847 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4848 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004849#else
4850 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004851#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4852
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004853#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4854 if( cert_endpoint == SSL_IS_SERVER )
4855 {
4856 ext_oid = OID_SERVER_AUTH;
4857 ext_len = OID_SIZE( OID_SERVER_AUTH );
4858 }
4859 else
4860 {
4861 ext_oid = OID_CLIENT_AUTH;
4862 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4863 }
4864
4865 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4866 return( -1 );
4867#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4868
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004869 return( 0 );
4870}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004871#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004872
4873#endif /* POLARSSL_SSL_TLS_C */