blob: 1fe3a95cab3a328fa551addb4b379a4c4318d6d5 [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
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
475 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
476 // XXX to be continued, WIP
477#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000478 handshake->tls_prf( handshake->premaster, handshake->pmslen,
479 "master secret",
480 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
Paul Bakker34617722014-06-13 17:20:13 +0200482 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 }
484 else
485 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
486
487 /*
488 * Swap the client and server random values.
489 */
Paul Bakker48916f92012-09-16 19:57:18 +0000490 memcpy( tmp, handshake->randbytes, 64 );
491 memcpy( handshake->randbytes, tmp + 32, 32 );
492 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200493 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495 /*
496 * SSLv3:
497 * key block =
498 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
499 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
500 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
501 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
502 * ...
503 *
504 * TLSv1:
505 * key block = PRF( master, "key expansion", randbytes )
506 */
Paul Bakker48916f92012-09-16 19:57:18 +0000507 handshake->tls_prf( session->master, 48, "key expansion",
508 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
Paul Bakker48916f92012-09-16 19:57:18 +0000510 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
511 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
512 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
513 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
515
Paul Bakker34617722014-06-13 17:20:13 +0200516 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 /*
519 * Determine the appropriate key, IV and MAC length.
520 */
Paul Bakker68884e32013-01-07 18:20:04 +0100521
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200522 transform->keylen = cipher_info->key_length / 8;
523
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200524 if( cipher_info->mode == POLARSSL_MODE_GCM ||
525 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200527 transform->maclen = 0;
528
Paul Bakker68884e32013-01-07 18:20:04 +0100529 transform->ivlen = 12;
530 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200531
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200532 /* Minimum length is expicit IV + tag */
533 transform->minlen = transform->ivlen - transform->fixed_ivlen
534 + ( transform->ciphersuite_info->flags &
535 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100536 }
537 else
538 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200539 int ret;
540
541 /* Initialize HMAC contexts */
542 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
543 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100544 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200545 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
546 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200549 /* Get MAC length */
550 transform->maclen = md_get_size( md_info );
551
552#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
553 /*
554 * If HMAC is to be truncated, we shall keep the leftmost bytes,
555 * (rfc 6066 page 13 or rfc 2104 section 4),
556 * so we only need to adjust the length here.
557 */
558 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
559 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
560#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
561
562 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100563 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200565 /* Minimum length */
566 if( cipher_info->mode == POLARSSL_MODE_STREAM )
567 transform->minlen = transform->maclen;
568 else
Paul Bakker68884e32013-01-07 18:20:04 +0100569 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200570 /*
571 * GenericBlockCipher:
572 * first multiple of blocklen greater than maclen
573 * + IV except for SSL3 and TLS 1.0
574 */
575 transform->minlen = transform->maclen
576 + cipher_info->block_size
577 - transform->maclen % cipher_info->block_size;
578
579#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
580 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
581 ssl->minor_ver == SSL_MINOR_VERSION_1 )
582 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100583 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200584#endif
585#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
586 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
587 ssl->minor_ver == SSL_MINOR_VERSION_3 )
588 {
589 transform->minlen += transform->ivlen;
590 }
591 else
592#endif
593 {
594 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
595 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
596 }
Paul Bakker68884e32013-01-07 18:20:04 +0100597 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 }
599
600 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000601 transform->keylen, transform->minlen, transform->ivlen,
602 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
604 /*
605 * Finally setup the cipher contexts, IVs and MAC secrets.
606 */
607 if( ssl->endpoint == SSL_IS_CLIENT )
608 {
Paul Bakker48916f92012-09-16 19:57:18 +0000609 key1 = keyblk + transform->maclen * 2;
610 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
Paul Bakker68884e32013-01-07 18:20:04 +0100612 mac_enc = keyblk;
613 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000615 /*
616 * This is not used in TLS v1.1.
617 */
Paul Bakker48916f92012-09-16 19:57:18 +0000618 iv_copy_len = ( transform->fixed_ivlen ) ?
619 transform->fixed_ivlen : transform->ivlen;
620 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
621 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000622 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 }
624 else
625 {
Paul Bakker48916f92012-09-16 19:57:18 +0000626 key1 = keyblk + transform->maclen * 2 + transform->keylen;
627 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
Paul Bakker68884e32013-01-07 18:20:04 +0100629 mac_enc = keyblk + transform->maclen;
630 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000632 /*
633 * This is not used in TLS v1.1.
634 */
Paul Bakker48916f92012-09-16 19:57:18 +0000635 iv_copy_len = ( transform->fixed_ivlen ) ?
636 transform->fixed_ivlen : transform->ivlen;
637 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
638 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000639 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 }
641
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200642#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100643 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
644 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100645 if( transform->maclen > sizeof transform->mac_enc )
646 {
647 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200648 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100649 }
650
Paul Bakker68884e32013-01-07 18:20:04 +0100651 memcpy( transform->mac_enc, mac_enc, transform->maclen );
652 memcpy( transform->mac_dec, mac_dec, transform->maclen );
653 }
654 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200655#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200656#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
657 defined(POLARSSL_SSL_PROTO_TLS1_2)
658 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100659 {
660 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
661 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
662 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200663 else
664#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200665 {
666 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200667 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200668 }
Paul Bakker68884e32013-01-07 18:20:04 +0100669
Paul Bakker05ef8352012-05-08 09:17:57 +0000670#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200671 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000672 {
673 int ret = 0;
674
675 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
676
Paul Bakker07eb38b2012-12-19 14:42:06 +0100677 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
678 transform->iv_enc, transform->iv_dec,
679 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100680 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100681 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000682 {
683 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200684 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000685 }
686 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200687#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000688
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200689 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
690 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200692 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
693 return( ret );
694 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200695
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200696 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
697 cipher_info ) ) != 0 )
698 {
699 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
700 return( ret );
701 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200702
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200703 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
704 cipher_info->key_length,
705 POLARSSL_ENCRYPT ) ) != 0 )
706 {
707 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
708 return( ret );
709 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200710
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200711 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
712 cipher_info->key_length,
713 POLARSSL_DECRYPT ) ) != 0 )
714 {
715 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
716 return( ret );
717 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200718
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200719#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200720 if( cipher_info->mode == POLARSSL_MODE_CBC )
721 {
722 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
723 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200724 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200725 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
726 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200727 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200728
729 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
730 POLARSSL_PADDING_NONE ) ) != 0 )
731 {
732 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
733 return( ret );
734 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200736#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
Paul Bakker34617722014-06-13 17:20:13 +0200738 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Paul Bakker2770fbd2012-07-03 13:30:23 +0000740#if defined(POLARSSL_ZLIB_SUPPORT)
741 // Initialize compression
742 //
Paul Bakker48916f92012-09-16 19:57:18 +0000743 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000744 {
Paul Bakker16770332013-10-11 09:59:44 +0200745 if( ssl->compress_buf == NULL )
746 {
747 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
748 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
749 if( ssl->compress_buf == NULL )
750 {
751 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
752 SSL_BUFFER_LEN ) );
753 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
754 }
755 }
756
Paul Bakker2770fbd2012-07-03 13:30:23 +0000757 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
758
Paul Bakker48916f92012-09-16 19:57:18 +0000759 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
760 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000761
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200762 if( deflateInit( &transform->ctx_deflate,
763 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000764 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000765 {
766 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
767 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
768 }
769 }
770#endif /* POLARSSL_ZLIB_SUPPORT */
771
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
773
774 return( 0 );
775}
776
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200777#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000778void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000779{
780 md5_context md5;
781 sha1_context sha1;
782 unsigned char pad_1[48];
783 unsigned char pad_2[48];
784
Paul Bakker380da532012-04-18 16:10:25 +0000785 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Paul Bakker48916f92012-09-16 19:57:18 +0000787 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
788 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000789
Paul Bakker380da532012-04-18 16:10:25 +0000790 memset( pad_1, 0x36, 48 );
791 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
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_1, 48 );
795 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000796
Paul Bakker380da532012-04-18 16:10:25 +0000797 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000798 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000799 md5_update( &md5, pad_2, 48 );
800 md5_update( &md5, hash, 16 );
801 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000802
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_1, 40 );
805 sha1_finish( &sha1, hash + 16 );
806
807 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000808 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000809 sha1_update( &sha1, pad_2, 40 );
810 sha1_update( &sha1, hash + 16, 20 );
811 sha1_finish( &sha1, hash + 16 );
812
813 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
814 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
815
Paul Bakker5b4af392014-06-26 12:09:34 +0200816 md5_free( &md5 );
817 sha1_free( &sha1 );
818
Paul Bakker380da532012-04-18 16:10:25 +0000819 return;
820}
Paul Bakker9af723c2014-05-01 13:03:14 +0200821#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000822
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200823#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000824void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
825{
826 md5_context md5;
827 sha1_context sha1;
828
829 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
830
Paul Bakker48916f92012-09-16 19:57:18 +0000831 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
832 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000833
Paul Bakker48916f92012-09-16 19:57:18 +0000834 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000835 sha1_finish( &sha1, hash + 16 );
836
837 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
838 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
839
Paul Bakker5b4af392014-06-26 12:09:34 +0200840 md5_free( &md5 );
841 sha1_free( &sha1 );
842
Paul Bakker380da532012-04-18 16:10:25 +0000843 return;
844}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200845#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000846
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200847#if defined(POLARSSL_SSL_PROTO_TLS1_2)
848#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000849void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
850{
Paul Bakker9e36f042013-06-30 14:34:05 +0200851 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000852
853 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
854
Paul Bakker9e36f042013-06-30 14:34:05 +0200855 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
856 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000857
858 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
859 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
860
Paul Bakker5b4af392014-06-26 12:09:34 +0200861 sha256_free( &sha256 );
862
Paul Bakker380da532012-04-18 16:10:25 +0000863 return;
864}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200865#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000866
Paul Bakker9e36f042013-06-30 14:34:05 +0200867#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000868void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
869{
Paul Bakker9e36f042013-06-30 14:34:05 +0200870 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000871
872 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
873
Paul Bakker9e36f042013-06-30 14:34:05 +0200874 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
875 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000876
Paul Bakkerca4ab492012-04-18 14:23:57 +0000877 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000878 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
879
Paul Bakker5b4af392014-06-26 12:09:34 +0200880 sha512_free( &sha512 );
881
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 return;
883}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200884#endif /* POLARSSL_SHA512_C */
885#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200887#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200888int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
889{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200890 unsigned char *p = ssl->handshake->premaster;
891 unsigned char *end = p + sizeof( ssl->handshake->premaster );
892
893 /*
894 * PMS = struct {
895 * opaque other_secret<0..2^16-1>;
896 * opaque psk<0..2^16-1>;
897 * };
898 * with "other_secret" depending on the particular key exchange
899 */
900#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
901 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
902 {
903 if( end - p < 2 + (int) ssl->psk_len )
904 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
905
906 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
907 *(p++) = (unsigned char)( ssl->psk_len );
908 p += ssl->psk_len;
909 }
910 else
911#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200912#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
913 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
914 {
915 /*
916 * other_secret already set by the ClientKeyExchange message,
917 * and is 48 bytes long
918 */
919 *p++ = 0;
920 *p++ = 48;
921 p += 48;
922 }
923 else
924#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200925#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
926 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
927 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200928 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200929 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200930
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200931 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200933 p + 2, &len,
934 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200935 {
936 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
937 return( ret );
938 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200939 *(p++) = (unsigned char)( len >> 8 );
940 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200941 p += len;
942
943 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
944 }
945 else
946#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
947#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
948 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
949 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200950 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200951 size_t zlen;
952
953 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200954 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200955 ssl->f_rng, ssl->p_rng ) ) != 0 )
956 {
957 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
958 return( ret );
959 }
960
961 *(p++) = (unsigned char)( zlen >> 8 );
962 *(p++) = (unsigned char)( zlen );
963 p += zlen;
964
965 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
966 }
967 else
968#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
969 {
970 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200971 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200972 }
973
974 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100975 if( end - p < 2 + (int) ssl->psk_len )
976 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
977
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200978 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
979 *(p++) = (unsigned char)( ssl->psk_len );
980 memcpy( p, ssl->psk, ssl->psk_len );
981 p += ssl->psk_len;
982
983 ssl->handshake->pmslen = p - ssl->handshake->premaster;
984
985 return( 0 );
986}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200987#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200988
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200989#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000990/*
991 * SSLv3.0 MAC functions
992 */
Paul Bakker68884e32013-01-07 18:20:04 +0100993static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
994 unsigned char *buf, size_t len,
995 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000996{
997 unsigned char header[11];
998 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +0200999 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001000 int md_size = md_get_size( md_ctx->md_info );
1001 int md_type = md_get_type( md_ctx->md_info );
1002
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001003 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001004 if( md_type == POLARSSL_MD_MD5 )
1005 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001006 else
Paul Bakker68884e32013-01-07 18:20:04 +01001007 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
1009 memcpy( header, ctr, 8 );
1010 header[ 8] = (unsigned char) type;
1011 header[ 9] = (unsigned char)( len >> 8 );
1012 header[10] = (unsigned char)( len );
1013
Paul Bakker68884e32013-01-07 18:20:04 +01001014 memset( padding, 0x36, padlen );
1015 md_starts( md_ctx );
1016 md_update( md_ctx, secret, md_size );
1017 md_update( md_ctx, padding, padlen );
1018 md_update( md_ctx, header, 11 );
1019 md_update( md_ctx, buf, len );
1020 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Paul Bakker68884e32013-01-07 18:20:04 +01001022 memset( padding, 0x5C, padlen );
1023 md_starts( md_ctx );
1024 md_update( md_ctx, secret, md_size );
1025 md_update( md_ctx, padding, padlen );
1026 md_update( md_ctx, buf + len, md_size );
1027 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001028}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001029#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001030
Paul Bakker5121ce52009-01-03 21:22:43 +00001031/*
1032 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001033 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001034static int ssl_encrypt_buf( ssl_context *ssl )
1035{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001036 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001037 const cipher_mode_t mode = cipher_get_cipher_mode(
1038 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
1040 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1041
1042 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001043 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001045#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1046 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1047 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001048 if( mode != POLARSSL_MODE_GCM &&
1049 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001051#if defined(POLARSSL_SSL_PROTO_SSL3)
1052 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1053 {
1054 ssl_mac( &ssl->transform_out->md_ctx_enc,
1055 ssl->transform_out->mac_enc,
1056 ssl->out_msg, ssl->out_msglen,
1057 ssl->out_ctr, ssl->out_msgtype );
1058 }
1059 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001060#endif
1061#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001062 defined(POLARSSL_SSL_PROTO_TLS1_2)
1063 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1064 {
1065 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1066 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1067 ssl->out_msg, ssl->out_msglen );
1068 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1069 ssl->out_msg + ssl->out_msglen );
1070 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1071 }
1072 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001073#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001074 {
1075 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001076 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001077 }
1078
1079 SSL_DEBUG_BUF( 4, "computed mac",
1080 ssl->out_msg + ssl->out_msglen,
1081 ssl->transform_out->maclen );
1082
1083 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001084 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001085#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001087 /*
1088 * Encrypt
1089 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001090#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001091 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001093 int ret;
1094 size_t olen = 0;
1095
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1097 "including %d bytes of padding",
1098 ssl->out_msglen, 0 ) );
1099
1100 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1101 ssl->out_msg, ssl->out_msglen );
1102
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001103 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001104 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001105 ssl->transform_out->ivlen,
1106 ssl->out_msg, ssl->out_msglen,
1107 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001108 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001109 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001110 return( ret );
1111 }
1112
1113 if( ssl->out_msglen != olen )
1114 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001115 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001116 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 }
Paul Bakker68884e32013-01-07 18:20:04 +01001119 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001120#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001121#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1122 if( mode == POLARSSL_MODE_GCM ||
1123 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001124 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001125 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001126 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001127 unsigned char *enc_msg;
1128 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001129 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1130 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132 memcpy( add_data, ssl->out_ctr, 8 );
1133 add_data[8] = ssl->out_msgtype;
1134 add_data[9] = ssl->major_ver;
1135 add_data[10] = ssl->minor_ver;
1136 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1137 add_data[12] = ssl->out_msglen & 0xFF;
1138
1139 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1140 add_data, 13 );
1141
Paul Bakker68884e32013-01-07 18:20:04 +01001142 /*
1143 * Generate IV
1144 */
1145 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001146 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1147 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001148 if( ret != 0 )
1149 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001150
Paul Bakker68884e32013-01-07 18:20:04 +01001151 memcpy( ssl->out_iv,
1152 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1153 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001154
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001155 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001156 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001157
Paul Bakker68884e32013-01-07 18:20:04 +01001158 /*
1159 * Fix pointer positions and message length with added IV
1160 */
1161 enc_msg = ssl->out_msg;
1162 enc_msglen = ssl->out_msglen;
1163 ssl->out_msglen += ssl->transform_out->ivlen -
1164 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001165
Paul Bakker68884e32013-01-07 18:20:04 +01001166 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1167 "including %d bytes of padding",
1168 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001169
Paul Bakker68884e32013-01-07 18:20:04 +01001170 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1171 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001172
Paul Bakker68884e32013-01-07 18:20:04 +01001173 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001174 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001175 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001176 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1177 ssl->transform_out->iv_enc,
1178 ssl->transform_out->ivlen,
1179 add_data, 13,
1180 enc_msg, enc_msglen,
1181 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001182 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001183 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001184 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001185 return( ret );
1186 }
1187
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001188 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001189 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001190 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001191 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001192 }
1193
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001194 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001195
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001196 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001199#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001200#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1201 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001202 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001204 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001205 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001206 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001207
Paul Bakker48916f92012-09-16 19:57:18 +00001208 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1209 ssl->transform_out->ivlen;
1210 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 padlen = 0;
1212
1213 for( i = 0; i <= padlen; i++ )
1214 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1215
1216 ssl->out_msglen += padlen + 1;
1217
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001218 enc_msglen = ssl->out_msglen;
1219 enc_msg = ssl->out_msg;
1220
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001221#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001222 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001223 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1224 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001225 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001226 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001227 {
1228 /*
1229 * Generate IV
1230 */
Paul Bakker48916f92012-09-16 19:57:18 +00001231 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1232 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001233 if( ret != 0 )
1234 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235
Paul Bakker92be97b2013-01-02 17:30:03 +01001236 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001237 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238
1239 /*
1240 * Fix pointer positions and message length with added IV
1241 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001242 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001244 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001245 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001246#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001247
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001249 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001250 ssl->out_msglen, ssl->transform_out->ivlen,
1251 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
1253 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001254 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001256 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001257 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001258 ssl->transform_out->ivlen,
1259 enc_msg, enc_msglen,
1260 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001261 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001262 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001263 return( ret );
1264 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001265
Paul Bakkercca5b812013-08-31 17:40:26 +02001266 if( enc_msglen != olen )
1267 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001268 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001269 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001270 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001271
1272#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001273 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1274 {
1275 /*
1276 * Save IV in SSL3 and TLS1
1277 */
1278 memcpy( ssl->transform_out->iv_enc,
1279 ssl->transform_out->cipher_ctx_enc.iv,
1280 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001284 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001285#endif /* POLARSSL_CIPHER_MODE_CBC &&
1286 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001287 {
1288 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001289 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Paul Bakkerca4ab492012-04-18 14:23:57 +00001292 for( i = 8; i > 0; i-- )
1293 if( ++ssl->out_ctr[i - 1] != 0 )
1294 break;
1295
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001296 /* The loops goes to its end iff the counter is wrapping */
1297 if( i == 0 )
1298 {
1299 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1300 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1301 }
1302
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1304
1305 return( 0 );
1306}
1307
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001308#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001309
Paul Bakker5121ce52009-01-03 21:22:43 +00001310static int ssl_decrypt_buf( ssl_context *ssl )
1311{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001312 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001313 const cipher_mode_t mode = cipher_get_cipher_mode(
1314 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001315#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1316 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1317 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1318 size_t padlen = 0, correct = 1;
1319#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
1321 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1322
Paul Bakker48916f92012-09-16 19:57:18 +00001323 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 {
1325 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001326 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001327 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 }
1329
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001330#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001331 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001332 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001333 int ret;
1334 size_t olen = 0;
1335
Paul Bakker68884e32013-01-07 18:20:04 +01001336 padlen = 0;
1337
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001338 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001339 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001340 ssl->transform_in->ivlen,
1341 ssl->in_msg, ssl->in_msglen,
1342 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001343 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001344 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001345 return( ret );
1346 }
1347
1348 if( ssl->in_msglen != olen )
1349 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001350 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001351 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001352 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 }
Paul Bakker68884e32013-01-07 18:20:04 +01001354 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001355#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001356#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1357 if( mode == POLARSSL_MODE_GCM ||
1358 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001359 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001360 int ret;
1361 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001362 unsigned char *dec_msg;
1363 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001364 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001365 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1366 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001367 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1368 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001369
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001370 if( ssl->in_msglen < explicit_iv_len + taglen )
1371 {
1372 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1373 "+ taglen (%d)", ssl->in_msglen,
1374 explicit_iv_len, taglen ) );
1375 return( POLARSSL_ERR_SSL_INVALID_MAC );
1376 }
1377 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1378
Paul Bakker68884e32013-01-07 18:20:04 +01001379 dec_msg = ssl->in_msg;
1380 dec_msg_result = ssl->in_msg;
1381 ssl->in_msglen = dec_msglen;
1382
1383 memcpy( add_data, ssl->in_ctr, 8 );
1384 add_data[8] = ssl->in_msgtype;
1385 add_data[9] = ssl->major_ver;
1386 add_data[10] = ssl->minor_ver;
1387 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1388 add_data[12] = ssl->in_msglen & 0xFF;
1389
1390 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1391 add_data, 13 );
1392
1393 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1394 ssl->in_iv,
1395 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1396
1397 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1398 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001399 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001400
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001401 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001402 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001403 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001404 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1405 ssl->transform_in->iv_dec,
1406 ssl->transform_in->ivlen,
1407 add_data, 13,
1408 dec_msg, dec_msglen,
1409 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001410 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001411 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001412 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1413
1414 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1415 return( POLARSSL_ERR_SSL_INVALID_MAC );
1416
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001417 return( ret );
1418 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001419
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001420 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001421 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001422 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001423 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001424 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001427#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001428#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1429 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001430 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 {
Paul Bakker45829992013-01-03 14:52:21 +01001432 /*
1433 * Decrypt and check the padding
1434 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001435 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001436 unsigned char *dec_msg;
1437 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001438 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001439 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001440 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001441
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 /*
Paul Bakker45829992013-01-03 14:52:21 +01001443 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 */
Paul Bakker48916f92012-09-16 19:57:18 +00001445 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 {
1447 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001448 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001449 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 }
1451
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001452#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001453 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1454 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001455#endif
Paul Bakker45829992013-01-03 14:52:21 +01001456
1457 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1458 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1459 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001460 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1461 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1462 ssl->transform_in->ivlen,
1463 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001464 return( POLARSSL_ERR_SSL_INVALID_MAC );
1465 }
1466
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001467 dec_msglen = ssl->in_msglen;
1468 dec_msg = ssl->in_msg;
1469 dec_msg_result = ssl->in_msg;
1470
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001471#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001472 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001473 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001474 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001475 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001476 {
Paul Bakker48916f92012-09-16 19:57:18 +00001477 dec_msglen -= ssl->transform_in->ivlen;
1478 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001479
Paul Bakker48916f92012-09-16 19:57:18 +00001480 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001481 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001482 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001483#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001484
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001485 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001486 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001487 ssl->transform_in->ivlen,
1488 dec_msg, dec_msglen,
1489 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001490 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001491 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001492 return( ret );
1493 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001494
Paul Bakkercca5b812013-08-31 17:40:26 +02001495 if( dec_msglen != olen )
1496 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001497 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001498 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001499 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001500
1501#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001502 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1503 {
1504 /*
1505 * Save IV in SSL3 and TLS1
1506 */
1507 memcpy( ssl->transform_in->iv_dec,
1508 ssl->transform_in->cipher_ctx_dec.iv,
1509 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001511#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
1513 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001514
1515 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1516 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001517#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001518 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1519 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001520#endif
Paul Bakker45829992013-01-03 14:52:21 +01001521 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001522 correct = 0;
1523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001525#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1527 {
Paul Bakker48916f92012-09-16 19:57:18 +00001528 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001530#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1532 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001533 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001534#endif
Paul Bakker45829992013-01-03 14:52:21 +01001535 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 }
1537 }
1538 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001539#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001540#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1541 defined(POLARSSL_SSL_PROTO_TLS1_2)
1542 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 {
1544 /*
Paul Bakker45829992013-01-03 14:52:21 +01001545 * TLSv1+: always check the padding up to the first failure
1546 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001548 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001549 size_t padding_idx = ssl->in_msglen - padlen - 1;
1550
Paul Bakker956c9e02013-12-19 14:42:28 +01001551 /*
1552 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001553 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001554 *
Paul Bakker61885c72014-04-25 12:59:03 +02001555 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1556 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001557 *
1558 * In both cases we reset padding_idx to a safe value (0) to
1559 * prevent out-of-buffer reads.
1560 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001561 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001562 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1563 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001564
1565 padding_idx *= correct;
1566
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001567 for( i = 1; i <= 256; i++ )
1568 {
1569 real_count &= ( i <= padlen );
1570 pad_count += real_count *
1571 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1572 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001573
1574 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001575
Paul Bakkerd66f0702013-01-31 16:57:45 +01001576#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001577 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001578 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001579#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001580 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001581 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001582 else
1583#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1584 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001585 {
1586 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001587 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001588 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001590 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001591#endif /* POLARSSL_CIPHER_MODE_CBC &&
1592 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001593 {
1594 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001595 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
1598 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1599 ssl->in_msg, ssl->in_msglen );
1600
1601 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001602 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001604#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1605 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1606 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001607 if( mode != POLARSSL_MODE_GCM &&
1608 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001609 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001610 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1611
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001612 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001614 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1615 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001617 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001619#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001620 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1621 {
1622 ssl_mac( &ssl->transform_in->md_ctx_dec,
1623 ssl->transform_in->mac_dec,
1624 ssl->in_msg, ssl->in_msglen,
1625 ssl->in_ctr, ssl->in_msgtype );
1626 }
1627 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001628#endif /* POLARSSL_SSL_PROTO_SSL3 */
1629#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001630 defined(POLARSSL_SSL_PROTO_TLS1_2)
1631 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1632 {
1633 /*
1634 * Process MAC and always update for padlen afterwards to make
1635 * total time independent of padlen
1636 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001637 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001638 *
1639 * Known timing attacks:
1640 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1641 *
1642 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1643 * correctly. (We round down instead of up, so -56 is the correct
1644 * value for our calculations instead of -55)
1645 */
1646 size_t j, extra_run = 0;
1647 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1648 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001649
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001650 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001651
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001652 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1653 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1654 ssl->in_msglen );
1655 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1656 ssl->in_msg + ssl->in_msglen );
1657 for( j = 0; j < extra_run; j++ )
1658 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001659
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001660 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1661 }
1662 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001663#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001664 POLARSSL_SSL_PROTO_TLS1_2 */
1665 {
1666 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001667 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001670 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1671 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1672 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001673
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001674 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001675 ssl->transform_in->maclen ) != 0 )
1676 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001677#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001679#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001680 correct = 0;
1681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001683 /*
1684 * Finally check the correct flag
1685 */
1686 if( correct == 0 )
1687 return( POLARSSL_ERR_SSL_INVALID_MAC );
1688 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001689#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
1691 if( ssl->in_msglen == 0 )
1692 {
1693 ssl->nb_zero++;
1694
1695 /*
1696 * Three or more empty messages may be a DoS attack
1697 * (excessive CPU consumption).
1698 */
1699 if( ssl->nb_zero > 3 )
1700 {
1701 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1702 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001703 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 }
1705 }
1706 else
1707 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001708
Paul Bakker23986e52011-04-24 08:57:21 +00001709 for( i = 8; i > 0; i-- )
1710 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001711 break;
1712
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001713 /* The loops goes to its end iff the counter is wrapping */
1714 if( i == 0 )
1715 {
1716 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1717 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1718 }
1719
Paul Bakker5121ce52009-01-03 21:22:43 +00001720 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1721
1722 return( 0 );
1723}
1724
Paul Bakker2770fbd2012-07-03 13:30:23 +00001725#if defined(POLARSSL_ZLIB_SUPPORT)
1726/*
1727 * Compression/decompression functions
1728 */
1729static int ssl_compress_buf( ssl_context *ssl )
1730{
1731 int ret;
1732 unsigned char *msg_post = ssl->out_msg;
1733 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001734 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001735
1736 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1737
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001738 if( len_pre == 0 )
1739 return( 0 );
1740
Paul Bakker2770fbd2012-07-03 13:30:23 +00001741 memcpy( msg_pre, ssl->out_msg, len_pre );
1742
1743 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1744 ssl->out_msglen ) );
1745
1746 SSL_DEBUG_BUF( 4, "before compression: output payload",
1747 ssl->out_msg, ssl->out_msglen );
1748
Paul Bakker48916f92012-09-16 19:57:18 +00001749 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1750 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1751 ssl->transform_out->ctx_deflate.next_out = msg_post;
1752 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001753
Paul Bakker48916f92012-09-16 19:57:18 +00001754 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001755 if( ret != Z_OK )
1756 {
1757 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1758 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1759 }
1760
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001761 ssl->out_msglen = SSL_BUFFER_LEN -
1762 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001763
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1765 ssl->out_msglen ) );
1766
1767 SSL_DEBUG_BUF( 4, "after compression: output payload",
1768 ssl->out_msg, ssl->out_msglen );
1769
1770 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1771
1772 return( 0 );
1773}
1774
1775static int ssl_decompress_buf( ssl_context *ssl )
1776{
1777 int ret;
1778 unsigned char *msg_post = ssl->in_msg;
1779 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001780 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001781
1782 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1783
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001784 if( len_pre == 0 )
1785 return( 0 );
1786
Paul Bakker2770fbd2012-07-03 13:30:23 +00001787 memcpy( msg_pre, ssl->in_msg, len_pre );
1788
1789 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1790 ssl->in_msglen ) );
1791
1792 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1793 ssl->in_msg, ssl->in_msglen );
1794
Paul Bakker48916f92012-09-16 19:57:18 +00001795 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1796 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1797 ssl->transform_in->ctx_inflate.next_out = msg_post;
1798 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799
Paul Bakker48916f92012-09-16 19:57:18 +00001800 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001801 if( ret != Z_OK )
1802 {
1803 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1804 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1805 }
1806
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001807 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1808 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001809
Paul Bakker2770fbd2012-07-03 13:30:23 +00001810 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1811 ssl->in_msglen ) );
1812
1813 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1814 ssl->in_msg, ssl->in_msglen );
1815
1816 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1817
1818 return( 0 );
1819}
1820#endif /* POLARSSL_ZLIB_SUPPORT */
1821
Paul Bakker5121ce52009-01-03 21:22:43 +00001822/*
1823 * Fill the input message buffer
1824 */
Paul Bakker23986e52011-04-24 08:57:21 +00001825int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001826{
Paul Bakker23986e52011-04-24 08:57:21 +00001827 int ret;
1828 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001829
1830 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1831
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001832 if( nb_want > SSL_BUFFER_LEN - 8 )
1833 {
1834 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1835 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1836 }
1837
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 while( ssl->in_left < nb_want )
1839 {
1840 len = nb_want - ssl->in_left;
1841 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1842
1843 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1844 ssl->in_left, nb_want ) );
1845 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1846
Paul Bakker831a7552011-05-18 13:32:51 +00001847 if( ret == 0 )
1848 return( POLARSSL_ERR_SSL_CONN_EOF );
1849
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 if( ret < 0 )
1851 return( ret );
1852
1853 ssl->in_left += ret;
1854 }
1855
1856 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1857
1858 return( 0 );
1859}
1860
1861/*
1862 * Flush any data not yet written
1863 */
1864int ssl_flush_output( ssl_context *ssl )
1865{
1866 int ret;
1867 unsigned char *buf;
1868
1869 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1870
1871 while( ssl->out_left > 0 )
1872 {
1873 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1874 5 + ssl->out_msglen, ssl->out_left ) );
1875
Paul Bakker5bd42292012-12-19 14:40:42 +01001876 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001878
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1880
1881 if( ret <= 0 )
1882 return( ret );
1883
1884 ssl->out_left -= ret;
1885 }
1886
1887 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1888
1889 return( 0 );
1890}
1891
1892/*
1893 * Record layer functions
1894 */
1895int ssl_write_record( ssl_context *ssl )
1896{
Paul Bakker05ef8352012-05-08 09:17:57 +00001897 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001898 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
1900 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1901
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1903 {
1904 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1905 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1906 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1907
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001908 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1909 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 }
1911
Paul Bakker2770fbd2012-07-03 13:30:23 +00001912#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001913 if( ssl->transform_out != NULL &&
1914 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001915 {
1916 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1917 {
1918 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1919 return( ret );
1920 }
1921
1922 len = ssl->out_msglen;
1923 }
1924#endif /*POLARSSL_ZLIB_SUPPORT */
1925
Paul Bakker05ef8352012-05-08 09:17:57 +00001926#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001927 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001929 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001930
Paul Bakker05ef8352012-05-08 09:17:57 +00001931 ret = ssl_hw_record_write( ssl );
1932 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1933 {
1934 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001935 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001936 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001937
1938 if( ret == 0 )
1939 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001940 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001941#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001942 if( !done )
1943 {
1944 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1945 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1946 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1948 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001949
Paul Bakker48916f92012-09-16 19:57:18 +00001950 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001951 {
1952 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1953 {
1954 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1955 return( ret );
1956 }
1957
1958 len = ssl->out_msglen;
1959 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1960 ssl->out_hdr[4] = (unsigned char)( len );
1961 }
1962
1963 ssl->out_left = 5 + ssl->out_msglen;
1964
1965 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1966 "version = [%d:%d], msglen = %d",
1967 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1968 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1969
1970 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001971 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 }
1973
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1975 {
1976 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1977 return( ret );
1978 }
1979
1980 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1981
1982 return( 0 );
1983}
1984
1985int ssl_read_record( ssl_context *ssl )
1986{
Paul Bakker05ef8352012-05-08 09:17:57 +00001987 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001988
1989 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1990
1991 if( ssl->in_hslen != 0 &&
1992 ssl->in_hslen < ssl->in_msglen )
1993 {
1994 /*
1995 * Get next Handshake message in the current record
1996 */
1997 ssl->in_msglen -= ssl->in_hslen;
1998
Paul Bakker8934a982011-08-05 11:11:53 +00001999 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002000 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
2002 ssl->in_hslen = 4;
2003 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2004
2005 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2006 " %d, type = %d, hslen = %d",
2007 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2008
2009 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2010 {
2011 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002012 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 }
2014
2015 if( ssl->in_msglen < ssl->in_hslen )
2016 {
2017 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002018 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 }
2020
Paul Bakker4224bc02014-04-08 14:36:50 +02002021 if( ssl->state != SSL_HANDSHAKE_OVER )
2022 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002023
2024 return( 0 );
2025 }
2026
2027 ssl->in_hslen = 0;
2028
2029 /*
2030 * Read the record header and validate it
2031 */
2032 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2033 {
2034 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2035 return( ret );
2036 }
2037
2038 ssl->in_msgtype = ssl->in_hdr[0];
2039 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2040
2041 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2042 "version = [%d:%d], msglen = %d",
2043 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2044 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2045
2046 if( ssl->in_hdr[1] != ssl->major_ver )
2047 {
2048 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002049 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 }
2051
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002052 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 {
2054 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002055 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 }
2057
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002058 /* Sanity check (outer boundaries) */
2059 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2060 {
2061 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2062 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2063 }
2064
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002066 * Make sure the message length is acceptable for the current transform
2067 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 */
Paul Bakker48916f92012-09-16 19:57:18 +00002069 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002071 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 {
2073 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002074 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 }
2076 }
2077 else
2078 {
Paul Bakker48916f92012-09-16 19:57:18 +00002079 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 {
2081 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002082 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 }
2084
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002085#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002087 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 {
2089 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002090 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002094#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2095 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 /*
2097 * TLS encrypted messages can have up to 256 bytes of padding
2098 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002099 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002100 ssl->in_msglen > ssl->transform_in->minlen +
2101 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 {
2103 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002104 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002106#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 }
2108
2109 /*
2110 * Read and optionally decrypt the message contents
2111 */
2112 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2113 {
2114 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2115 return( ret );
2116 }
2117
2118 SSL_DEBUG_BUF( 4, "input record from network",
2119 ssl->in_hdr, 5 + ssl->in_msglen );
2120
Paul Bakker05ef8352012-05-08 09:17:57 +00002121#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002122 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002123 {
2124 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2125
2126 ret = ssl_hw_record_read( ssl );
2127 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2128 {
2129 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002130 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002131 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002132
2133 if( ret == 0 )
2134 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002135 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002136#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002137 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 {
2139 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2140 {
Paul Bakker40865c82013-01-31 17:13:13 +01002141#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2142 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2143 {
2144 ssl_send_alert_message( ssl,
2145 SSL_ALERT_LEVEL_FATAL,
2146 SSL_ALERT_MSG_BAD_RECORD_MAC );
2147 }
2148#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2150 return( ret );
2151 }
2152
2153 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2154 ssl->in_msg, ssl->in_msglen );
2155
2156 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2157 {
2158 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002159 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 }
2161 }
2162
Paul Bakker2770fbd2012-07-03 13:30:23 +00002163#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002164 if( ssl->transform_in != NULL &&
2165 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002166 {
2167 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2168 {
2169 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2170 return( ret );
2171 }
2172
2173 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2174 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2175 }
2176#endif /* POLARSSL_ZLIB_SUPPORT */
2177
Paul Bakker0a925182012-04-16 06:46:41 +00002178 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2179 ssl->in_msgtype != SSL_MSG_ALERT &&
2180 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2181 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2182 {
2183 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2184
Paul Bakker48916f92012-09-16 19:57:18 +00002185 if( ( ret = ssl_send_alert_message( ssl,
2186 SSL_ALERT_LEVEL_FATAL,
2187 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002188 {
2189 return( ret );
2190 }
2191
2192 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2193 }
2194
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2196 {
2197 ssl->in_hslen = 4;
2198 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2199
2200 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2201 " %d, type = %d, hslen = %d",
2202 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2203
2204 /*
2205 * Additional checks to validate the handshake header
2206 */
2207 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2208 {
2209 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002210 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 }
2212
2213 if( ssl->in_msglen < ssl->in_hslen )
2214 {
2215 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002216 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 }
2218
Paul Bakker48916f92012-09-16 19:57:18 +00002219 if( ssl->state != SSL_HANDSHAKE_OVER )
2220 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 }
2222
2223 if( ssl->in_msgtype == SSL_MSG_ALERT )
2224 {
2225 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2226 ssl->in_msg[0], ssl->in_msg[1] ) );
2227
2228 /*
2229 * Ignore non-fatal alerts, except close_notify
2230 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002231 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002233 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2234 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002235 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 }
2237
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002238 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2239 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 {
2241 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002242 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 }
2244 }
2245
2246 ssl->in_left = 0;
2247
2248 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2249
2250 return( 0 );
2251}
2252
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002253int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2254{
2255 int ret;
2256
2257 if( ( ret = ssl_send_alert_message( ssl,
2258 SSL_ALERT_LEVEL_FATAL,
2259 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2260 {
2261 return( ret );
2262 }
2263
2264 return( 0 );
2265}
2266
Paul Bakker0a925182012-04-16 06:46:41 +00002267int ssl_send_alert_message( ssl_context *ssl,
2268 unsigned char level,
2269 unsigned char message )
2270{
2271 int ret;
2272
2273 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2274
2275 ssl->out_msgtype = SSL_MSG_ALERT;
2276 ssl->out_msglen = 2;
2277 ssl->out_msg[0] = level;
2278 ssl->out_msg[1] = message;
2279
2280 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2281 {
2282 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2283 return( ret );
2284 }
2285
2286 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2287
2288 return( 0 );
2289}
2290
Paul Bakker5121ce52009-01-03 21:22:43 +00002291/*
2292 * Handshake functions
2293 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002294#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2295 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2296 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2297 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2298 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2299 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2300 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002301int ssl_write_certificate( ssl_context *ssl )
2302{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002303 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002304
2305 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2306
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002308 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2309 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002310 {
2311 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2312 ssl->state++;
2313 return( 0 );
2314 }
2315
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002316 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2317 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002318}
2319
2320int ssl_parse_certificate( ssl_context *ssl )
2321{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002322 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2323
2324 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2325
2326 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002327 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2328 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002329 {
2330 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2331 ssl->state++;
2332 return( 0 );
2333 }
2334
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002335 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2336 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002337}
2338#else
2339int ssl_write_certificate( ssl_context *ssl )
2340{
2341 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2342 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002343 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002344 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2345
2346 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2347
2348 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002349 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2350 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002351 {
2352 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2353 ssl->state++;
2354 return( 0 );
2355 }
2356
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 if( ssl->endpoint == SSL_IS_CLIENT )
2358 {
2359 if( ssl->client_auth == 0 )
2360 {
2361 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2362 ssl->state++;
2363 return( 0 );
2364 }
2365
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002366#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 /*
2368 * If using SSLv3 and got no cert, send an Alert message
2369 * (otherwise an empty Certificate message will be sent).
2370 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002371 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2373 {
2374 ssl->out_msglen = 2;
2375 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002376 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2377 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
2379 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2380 goto write_msg;
2381 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002382#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 }
2384 else /* SSL_IS_SERVER */
2385 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002386 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 {
2388 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002389 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 }
2391 }
2392
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002393 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
2395 /*
2396 * 0 . 0 handshake type
2397 * 1 . 3 handshake length
2398 * 4 . 6 length of all certs
2399 * 7 . 9 length of cert. 1
2400 * 10 . n-1 peer certificate
2401 * n . n+2 length of cert. 2
2402 * n+3 . ... upper level cert, etc.
2403 */
2404 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002405 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Paul Bakker29087132010-03-21 21:03:34 +00002407 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
2409 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002410 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 {
2412 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2413 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002414 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 }
2416
2417 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2418 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2419 ssl->out_msg[i + 2] = (unsigned char)( n );
2420
2421 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2422 i += n; crt = crt->next;
2423 }
2424
2425 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2426 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2427 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2428
2429 ssl->out_msglen = i;
2430 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2431 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2432
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002433#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002434write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002435#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
2437 ssl->state++;
2438
2439 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2440 {
2441 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2442 return( ret );
2443 }
2444
2445 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2446
Paul Bakkered27a042013-04-18 22:46:23 +02002447 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448}
2449
2450int ssl_parse_certificate( ssl_context *ssl )
2451{
Paul Bakkered27a042013-04-18 22:46:23 +02002452 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002453 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002454 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
2456 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2457
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002458 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002459 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2460 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002461 {
2462 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2463 ssl->state++;
2464 return( 0 );
2465 }
2466
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002468 ( ssl->authmode == SSL_VERIFY_NONE ||
2469 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002471 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2473 ssl->state++;
2474 return( 0 );
2475 }
2476
2477 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2478 {
2479 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2480 return( ret );
2481 }
2482
2483 ssl->state++;
2484
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002485#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 /*
2487 * Check if the client sent an empty certificate
2488 */
2489 if( ssl->endpoint == SSL_IS_SERVER &&
2490 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2491 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002492 if( ssl->in_msglen == 2 &&
2493 ssl->in_msgtype == SSL_MSG_ALERT &&
2494 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2495 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 {
2497 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2498
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002499 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2501 return( 0 );
2502 else
Paul Bakker40e46942009-01-03 21:51:57 +00002503 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 }
2505 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002506#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002508#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2509 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 if( ssl->endpoint == SSL_IS_SERVER &&
2511 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2512 {
2513 if( ssl->in_hslen == 7 &&
2514 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2515 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2516 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2517 {
2518 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2519
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002520 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002522 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 else
2524 return( 0 );
2525 }
2526 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002527#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2528 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
2530 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2531 {
2532 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002533 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 }
2535
2536 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2537 {
2538 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002539 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 }
2541
2542 /*
2543 * Same message structure as in ssl_write_certificate()
2544 */
2545 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2546
2547 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2548 {
2549 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002550 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 }
2552
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002553 /* In case we tried to reuse a session but it failed */
2554 if( ssl->session_negotiate->peer_cert != NULL )
2555 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002556 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002557 polarssl_free( ssl->session_negotiate->peer_cert );
2558 }
2559
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002560 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2561 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 {
2563 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002564 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002565 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
2567
Paul Bakkerb6b09562013-09-18 14:17:41 +02002568 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
2570 i = 7;
2571
2572 while( i < ssl->in_hslen )
2573 {
2574 if( ssl->in_msg[i] != 0 )
2575 {
2576 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002577 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 }
2579
2580 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2581 | (unsigned int) ssl->in_msg[i + 2];
2582 i += 3;
2583
2584 if( n < 128 || i + n > ssl->in_hslen )
2585 {
2586 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002587 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 }
2589
Paul Bakkerddf26b42013-09-18 13:46:23 +02002590 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2591 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 if( ret != 0 )
2593 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002594 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 return( ret );
2596 }
2597
2598 i += n;
2599 }
2600
Paul Bakker48916f92012-09-16 19:57:18 +00002601 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002603 /*
2604 * On client, make sure the server cert doesn't change during renego to
2605 * avoid "triple handshake" attack: https://secure-resumption.com/
2606 */
2607 if( ssl->endpoint == SSL_IS_CLIENT &&
2608 ssl->renegotiation == SSL_RENEGOTIATION )
2609 {
2610 if( ssl->session->peer_cert == NULL )
2611 {
2612 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2613 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2614 }
2615
2616 if( ssl->session->peer_cert->raw.len !=
2617 ssl->session_negotiate->peer_cert->raw.len ||
2618 memcmp( ssl->session->peer_cert->raw.p,
2619 ssl->session_negotiate->peer_cert->raw.p,
2620 ssl->session->peer_cert->raw.len ) != 0 )
2621 {
2622 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2623 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2624 }
2625 }
2626
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 if( ssl->authmode != SSL_VERIFY_NONE )
2628 {
2629 if( ssl->ca_chain == NULL )
2630 {
2631 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002632 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 }
2634
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002635 /*
2636 * Main check: verify certificate
2637 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002638 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2639 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2640 &ssl->session_negotiate->verify_result,
2641 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
2643 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002644 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002646 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002647
2648 /*
2649 * Secondary checks: always done, but change 'ret' only if it was 0
2650 */
2651
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002652#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002653 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002654 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002655
2656 /* If certificate uses an EC key, make sure the curve is OK */
2657 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2658 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2659 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002660 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002661 if( ret == 0 )
2662 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002663 }
2664 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002665#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002667 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2668 ciphersuite_info,
2669 ! ssl->endpoint ) != 0 )
2670 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002671 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002672 if( ret == 0 )
2673 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2674 }
2675
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2677 ret = 0;
2678 }
2679
2680 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2681
2682 return( ret );
2683}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002684#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2685 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2686 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2687 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2688 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2689 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2690 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
2692int ssl_write_change_cipher_spec( ssl_context *ssl )
2693{
2694 int ret;
2695
2696 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2697
2698 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2699 ssl->out_msglen = 1;
2700 ssl->out_msg[0] = 1;
2701
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 ssl->state++;
2703
2704 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2705 {
2706 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2707 return( ret );
2708 }
2709
2710 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2711
2712 return( 0 );
2713}
2714
2715int ssl_parse_change_cipher_spec( ssl_context *ssl )
2716{
2717 int ret;
2718
2719 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2720
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2722 {
2723 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2724 return( ret );
2725 }
2726
2727 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2728 {
2729 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002730 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 }
2732
2733 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2734 {
2735 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002736 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 }
2738
2739 ssl->state++;
2740
2741 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2742
2743 return( 0 );
2744}
2745
Paul Bakker41c83d32013-03-20 14:39:14 +01002746void ssl_optimize_checksum( ssl_context *ssl,
2747 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002748{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002749 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002750
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002751#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2752 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002753 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002754 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002755 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002756#endif
2757#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2758#if defined(POLARSSL_SHA512_C)
2759 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2760 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2761 else
2762#endif
2763#if defined(POLARSSL_SHA256_C)
2764 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002765 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002766 else
2767#endif
2768#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002769 {
2770 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002771 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002772 }
Paul Bakker380da532012-04-18 16:10:25 +00002773}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002774
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002775static void ssl_update_checksum_start( ssl_context *ssl,
2776 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002777{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002778#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2779 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002780 md5_update( &ssl->handshake->fin_md5 , buf, len );
2781 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002782#endif
2783#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2784#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002785 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002786#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002787#if defined(POLARSSL_SHA512_C)
2788 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002789#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002790#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002791}
2792
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002793#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2794 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002795static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2796 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002797{
Paul Bakker48916f92012-09-16 19:57:18 +00002798 md5_update( &ssl->handshake->fin_md5 , buf, len );
2799 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002800}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002801#endif
Paul Bakker380da532012-04-18 16:10:25 +00002802
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002803#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2804#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002805static void ssl_update_checksum_sha256( ssl_context *ssl,
2806 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002807{
Paul Bakker9e36f042013-06-30 14:34:05 +02002808 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002809}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002810#endif
Paul Bakker380da532012-04-18 16:10:25 +00002811
Paul Bakker9e36f042013-06-30 14:34:05 +02002812#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002813static void ssl_update_checksum_sha384( ssl_context *ssl,
2814 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002815{
Paul Bakker9e36f042013-06-30 14:34:05 +02002816 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002817}
Paul Bakker769075d2012-11-24 11:26:46 +01002818#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002819#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002820
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002821#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002822static void ssl_calc_finished_ssl(
2823 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002824{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002825 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002826 md5_context md5;
2827 sha1_context sha1;
2828
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 unsigned char padbuf[48];
2830 unsigned char md5sum[16];
2831 unsigned char sha1sum[20];
2832
Paul Bakker48916f92012-09-16 19:57:18 +00002833 ssl_session *session = ssl->session_negotiate;
2834 if( !session )
2835 session = ssl->session;
2836
Paul Bakker1ef83d62012-04-11 12:09:53 +00002837 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2838
Paul Bakker48916f92012-09-16 19:57:18 +00002839 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2840 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
2842 /*
2843 * SSLv3:
2844 * hash =
2845 * MD5( master + pad2 +
2846 * MD5( handshake + sender + master + pad1 ) )
2847 * + SHA1( master + pad2 +
2848 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 */
2850
Paul Bakker90995b52013-06-24 19:20:35 +02002851#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002853 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Paul Bakker90995b52013-06-24 19:20:35 +02002856#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002858 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002859#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Paul Bakker3c2122f2013-06-24 19:03:14 +02002861 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2862 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Paul Bakker1ef83d62012-04-11 12:09:53 +00002864 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
Paul Bakker3c2122f2013-06-24 19:03:14 +02002866 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002867 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002868 md5_update( &md5, padbuf, 48 );
2869 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakker3c2122f2013-06-24 19:03:14 +02002871 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002872 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 sha1_update( &sha1, padbuf, 40 );
2874 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
Paul Bakker1ef83d62012-04-11 12:09:53 +00002876 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
Paul Bakker1ef83d62012-04-11 12:09:53 +00002878 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002879 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002880 md5_update( &md5, padbuf, 48 );
2881 md5_update( &md5, md5sum, 16 );
2882 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Paul Bakker1ef83d62012-04-11 12:09:53 +00002884 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002885 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002886 sha1_update( &sha1, padbuf , 40 );
2887 sha1_update( &sha1, sha1sum, 20 );
2888 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002889
Paul Bakker1ef83d62012-04-11 12:09:53 +00002890 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakker5b4af392014-06-26 12:09:34 +02002892 md5_free( &md5 );
2893 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
Paul Bakker34617722014-06-13 17:20:13 +02002895 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2896 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2897 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
2899 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2900}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002901#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002903#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002904static void ssl_calc_finished_tls(
2905 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002906{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002908 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Paul Bakker48916f92012-09-16 19:57:18 +00002913 ssl_session *session = ssl->session_negotiate;
2914 if( !session )
2915 session = ssl->session;
2916
Paul Bakker1ef83d62012-04-11 12:09:53 +00002917 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Paul Bakker48916f92012-09-16 19:57:18 +00002919 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2920 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922 /*
2923 * TLSv1:
2924 * hash = PRF( master, finished_label,
2925 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2926 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002927
Paul Bakker90995b52013-06-24 19:20:35 +02002928#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002929 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2930 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002931#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002932
Paul Bakker90995b52013-06-24 19:20:35 +02002933#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002934 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2935 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002936#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002937
2938 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002939 ? "client finished"
2940 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002941
2942 md5_finish( &md5, padbuf );
2943 sha1_finish( &sha1, padbuf + 16 );
2944
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002945 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002946 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002947
2948 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2949
Paul Bakker5b4af392014-06-26 12:09:34 +02002950 md5_free( &md5 );
2951 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952
Paul Bakker34617722014-06-13 17:20:13 +02002953 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954
2955 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2956}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002957#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002959#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2960#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002961static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962 ssl_context *ssl, unsigned char *buf, int from )
2963{
2964 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002965 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002966 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002967 unsigned char padbuf[32];
2968
Paul Bakker48916f92012-09-16 19:57:18 +00002969 ssl_session *session = ssl->session_negotiate;
2970 if( !session )
2971 session = ssl->session;
2972
Paul Bakker380da532012-04-18 16:10:25 +00002973 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974
Paul Bakker9e36f042013-06-30 14:34:05 +02002975 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976
2977 /*
2978 * TLSv1.2:
2979 * hash = PRF( master, finished_label,
2980 * Hash( handshake ) )[0.11]
2981 */
2982
Paul Bakker9e36f042013-06-30 14:34:05 +02002983#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002984 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002985 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002986#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002987
2988 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002989 ? "client finished"
2990 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002991
Paul Bakker9e36f042013-06-30 14:34:05 +02002992 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002993
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002994 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002995 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002996
2997 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2998
Paul Bakker5b4af392014-06-26 12:09:34 +02002999 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003000
Paul Bakker34617722014-06-13 17:20:13 +02003001 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003002
3003 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3004}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003005#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003006
Paul Bakker9e36f042013-06-30 14:34:05 +02003007#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003008static void ssl_calc_finished_tls_sha384(
3009 ssl_context *ssl, unsigned char *buf, int from )
3010{
3011 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003012 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003013 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003014 unsigned char padbuf[48];
3015
Paul Bakker48916f92012-09-16 19:57:18 +00003016 ssl_session *session = ssl->session_negotiate;
3017 if( !session )
3018 session = ssl->session;
3019
Paul Bakker380da532012-04-18 16:10:25 +00003020 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003021
Paul Bakker9e36f042013-06-30 14:34:05 +02003022 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003023
3024 /*
3025 * TLSv1.2:
3026 * hash = PRF( master, finished_label,
3027 * Hash( handshake ) )[0.11]
3028 */
3029
Paul Bakker9e36f042013-06-30 14:34:05 +02003030#if !defined(POLARSSL_SHA512_ALT)
3031 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3032 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003033#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003034
3035 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003036 ? "client finished"
3037 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003038
Paul Bakker9e36f042013-06-30 14:34:05 +02003039 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003040
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003041 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003042 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003043
3044 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3045
Paul Bakker5b4af392014-06-26 12:09:34 +02003046 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003047
Paul Bakker34617722014-06-13 17:20:13 +02003048 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003049
3050 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3051}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003052#endif /* POLARSSL_SHA512_C */
3053#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003054
Paul Bakker48916f92012-09-16 19:57:18 +00003055void ssl_handshake_wrapup( ssl_context *ssl )
3056{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003057 int resume = ssl->handshake->resume;
3058
Paul Bakker48916f92012-09-16 19:57:18 +00003059 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3060
3061 /*
3062 * Free our handshake params
3063 */
3064 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003065 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003066 ssl->handshake = NULL;
3067
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003068 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003069 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003070 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003071 ssl->renego_records_seen = 0;
3072 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003073
Paul Bakker48916f92012-09-16 19:57:18 +00003074 /*
3075 * Switch in our now active transform context
3076 */
3077 if( ssl->transform )
3078 {
3079 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003080 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003081 }
3082 ssl->transform = ssl->transform_negotiate;
3083 ssl->transform_negotiate = NULL;
3084
Paul Bakker0a597072012-09-25 21:55:46 +00003085 if( ssl->session )
3086 {
3087 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003088 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003089 }
3090 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003091 ssl->session_negotiate = NULL;
3092
Paul Bakker0a597072012-09-25 21:55:46 +00003093 /*
3094 * Add cache entry
3095 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003096 if( ssl->f_set_cache != NULL &&
3097 ssl->session->length != 0 &&
3098 resume == 0 )
3099 {
Paul Bakker0a597072012-09-25 21:55:46 +00003100 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3101 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003102 }
Paul Bakker0a597072012-09-25 21:55:46 +00003103
Paul Bakker48916f92012-09-16 19:57:18 +00003104 ssl->state++;
3105
3106 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3107}
3108
Paul Bakker1ef83d62012-04-11 12:09:53 +00003109int ssl_write_finished( ssl_context *ssl )
3110{
3111 int ret, hash_len;
3112
3113 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3114
Paul Bakker92be97b2013-01-02 17:30:03 +01003115 /*
3116 * Set the out_msg pointer to the correct location based on IV length
3117 */
3118 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3119 {
3120 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3121 ssl->transform_negotiate->fixed_ivlen;
3122 }
3123 else
3124 ssl->out_msg = ssl->out_iv;
3125
Paul Bakker48916f92012-09-16 19:57:18 +00003126 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127
3128 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003129 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3130
Paul Bakker48916f92012-09-16 19:57:18 +00003131 ssl->verify_data_len = hash_len;
3132 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3133
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 ssl->out_msglen = 4 + hash_len;
3135 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3136 ssl->out_msg[0] = SSL_HS_FINISHED;
3137
3138 /*
3139 * In case of session resuming, invert the client and server
3140 * ChangeCipherSpec messages order.
3141 */
Paul Bakker0a597072012-09-25 21:55:46 +00003142 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003143 {
3144 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003145 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 else
3147 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3148 }
3149 else
3150 ssl->state++;
3151
Paul Bakker48916f92012-09-16 19:57:18 +00003152 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003153 * Switch to our negotiated transform and session parameters for outbound
3154 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003155 */
3156 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3157 ssl->transform_out = ssl->transform_negotiate;
3158 ssl->session_out = ssl->session_negotiate;
3159 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160
Paul Bakker07eb38b2012-12-19 14:42:06 +01003161#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003162 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003163 {
3164 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3165 {
3166 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3167 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3168 }
3169 }
3170#endif
3171
Paul Bakker5121ce52009-01-03 21:22:43 +00003172 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3173 {
3174 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3175 return( ret );
3176 }
3177
3178 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3179
3180 return( 0 );
3181}
3182
3183int ssl_parse_finished( ssl_context *ssl )
3184{
Paul Bakker23986e52011-04-24 08:57:21 +00003185 int ret;
3186 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003187 unsigned char buf[36];
3188
3189 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3190
Paul Bakker48916f92012-09-16 19:57:18 +00003191 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003192
Paul Bakker48916f92012-09-16 19:57:18 +00003193 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003194 * Switch to our negotiated transform and session parameters for inbound
3195 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003196 */
3197 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3198 ssl->transform_in = ssl->transform_negotiate;
3199 ssl->session_in = ssl->session_negotiate;
3200 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Paul Bakker92be97b2013-01-02 17:30:03 +01003202 /*
3203 * Set the in_msg pointer to the correct location based on IV length
3204 */
3205 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3206 {
3207 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3208 ssl->transform_negotiate->fixed_ivlen;
3209 }
3210 else
3211 ssl->in_msg = ssl->in_iv;
3212
Paul Bakker07eb38b2012-12-19 14:42:06 +01003213#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003214 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003215 {
3216 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3217 {
3218 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3219 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3220 }
3221 }
3222#endif
3223
Paul Bakker5121ce52009-01-03 21:22:43 +00003224 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3225 {
3226 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3227 return( ret );
3228 }
3229
3230 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3231 {
3232 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003233 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003234 }
3235
Paul Bakker1ef83d62012-04-11 12:09:53 +00003236 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003237 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3238
3239 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3240 ssl->in_hslen != 4 + hash_len )
3241 {
3242 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003243 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 }
3245
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003246 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003247 {
3248 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003249 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 }
3251
Paul Bakker48916f92012-09-16 19:57:18 +00003252 ssl->verify_data_len = hash_len;
3253 memcpy( ssl->peer_verify_data, buf, hash_len );
3254
Paul Bakker0a597072012-09-25 21:55:46 +00003255 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 {
3257 if( ssl->endpoint == SSL_IS_CLIENT )
3258 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3259
3260 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003261 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003262 }
3263 else
3264 ssl->state++;
3265
3266 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3267
3268 return( 0 );
3269}
3270
Paul Bakker968afaa2014-07-09 11:09:24 +02003271static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003272{
3273 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3274
3275#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3276 defined(POLARSSL_SSL_PROTO_TLS1_1)
3277 md5_init( &handshake->fin_md5 );
3278 sha1_init( &handshake->fin_sha1 );
3279 md5_starts( &handshake->fin_md5 );
3280 sha1_starts( &handshake->fin_sha1 );
3281#endif
3282#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3283#if defined(POLARSSL_SHA256_C)
3284 sha256_init( &handshake->fin_sha256 );
3285 sha256_starts( &handshake->fin_sha256, 0 );
3286#endif
3287#if defined(POLARSSL_SHA512_C)
3288 sha512_init( &handshake->fin_sha512 );
3289 sha512_starts( &handshake->fin_sha512, 1 );
3290#endif
3291#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3292
3293 handshake->update_checksum = ssl_update_checksum_start;
3294 handshake->sig_alg = SSL_HASH_SHA1;
3295
3296#if defined(POLARSSL_DHM_C)
3297 dhm_init( &handshake->dhm_ctx );
3298#endif
3299#if defined(POLARSSL_ECDH_C)
3300 ecdh_init( &handshake->ecdh_ctx );
3301#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003302}
3303
3304static void ssl_transform_init( ssl_transform *transform )
3305{
3306 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003307
3308 cipher_init( &transform->cipher_ctx_enc );
3309 cipher_init( &transform->cipher_ctx_dec );
3310
3311 md_init( &transform->md_ctx_enc );
3312 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003313}
3314
3315void ssl_session_init( ssl_session *session )
3316{
3317 memset( session, 0, sizeof(ssl_session) );
3318}
3319
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003320static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003321{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003322 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003323 if( ssl->transform_negotiate )
3324 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003325 if( ssl->session_negotiate )
3326 ssl_session_free( ssl->session_negotiate );
3327 if( ssl->handshake )
3328 ssl_handshake_free( ssl->handshake );
3329
3330 /*
3331 * Either the pointers are now NULL or cleared properly and can be freed.
3332 * Now allocate missing structures.
3333 */
3334 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003335 {
3336 ssl->transform_negotiate =
3337 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3338 }
Paul Bakker48916f92012-09-16 19:57:18 +00003339
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003340 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003341 {
3342 ssl->session_negotiate =
3343 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3344 }
Paul Bakker48916f92012-09-16 19:57:18 +00003345
Paul Bakker82788fb2014-10-20 13:59:19 +02003346 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003347 {
3348 ssl->handshake = (ssl_handshake_params *)
3349 polarssl_malloc( sizeof(ssl_handshake_params) );
3350 }
Paul Bakker48916f92012-09-16 19:57:18 +00003351
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003352 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003353 if( ssl->handshake == NULL ||
3354 ssl->transform_negotiate == NULL ||
3355 ssl->session_negotiate == NULL )
3356 {
3357 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003358
3359 polarssl_free( ssl->handshake );
3360 polarssl_free( ssl->transform_negotiate );
3361 polarssl_free( ssl->session_negotiate );
3362
3363 ssl->handshake = NULL;
3364 ssl->transform_negotiate = NULL;
3365 ssl->session_negotiate = NULL;
3366
Paul Bakker48916f92012-09-16 19:57:18 +00003367 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3368 }
3369
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003370 /* Initialize structures */
3371 ssl_session_init( ssl->session_negotiate );
3372 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003373 ssl_handshake_params_init( ssl->handshake );
3374
3375#if defined(POLARSSL_X509_CRT_PARSE_C)
3376 ssl->handshake->key_cert = ssl->key_cert;
3377#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003378
Paul Bakker48916f92012-09-16 19:57:18 +00003379 return( 0 );
3380}
3381
Paul Bakker5121ce52009-01-03 21:22:43 +00003382/*
3383 * Initialize an SSL context
3384 */
3385int ssl_init( ssl_context *ssl )
3386{
Paul Bakker48916f92012-09-16 19:57:18 +00003387 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 int len = SSL_BUFFER_LEN;
3389
3390 memset( ssl, 0, sizeof( ssl_context ) );
3391
Paul Bakker62f2dee2012-09-28 07:31:51 +00003392 /*
3393 * Sane defaults
3394 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003395 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3396 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3397 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3398 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003399
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003400 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003401
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003402 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3403
Paul Bakker62f2dee2012-09-28 07:31:51 +00003404#if defined(POLARSSL_DHM_C)
3405 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3406 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3407 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3408 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3409 {
3410 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3411 return( ret );
3412 }
3413#endif
3414
3415 /*
3416 * Prepare base structures
3417 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003418 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003420 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 ssl->in_msg = ssl->in_ctr + 13;
3422
3423 if( ssl->in_ctr == NULL )
3424 {
3425 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003426 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003427 }
3428
Paul Bakker6e339b52013-07-03 13:37:05 +02003429 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003431 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003432 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003433
3434 if( ssl->out_ctr == NULL )
3435 {
3436 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003437 polarssl_free( ssl->in_ctr );
3438 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003439 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003440 }
3441
3442 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3443 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3444
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003445#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3446 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3447#endif
3448
Paul Bakker606b4ba2013-08-14 16:52:14 +02003449#if defined(POLARSSL_SSL_SESSION_TICKETS)
3450 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3451#endif
3452
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003453#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003454 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003455#endif
3456
Paul Bakker48916f92012-09-16 19:57:18 +00003457 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3458 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459
3460 return( 0 );
3461}
3462
3463/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003464 * Reset an initialized and used SSL context for re-use while retaining
3465 * all application-set variables, function pointers and data.
3466 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003467int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003468{
Paul Bakker48916f92012-09-16 19:57:18 +00003469 int ret;
3470
Paul Bakker7eb013f2011-10-06 12:37:39 +00003471 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003472 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3473 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3474
3475 ssl->verify_data_len = 0;
3476 memset( ssl->own_verify_data, 0, 36 );
3477 memset( ssl->peer_verify_data, 0, 36 );
3478
Paul Bakker7eb013f2011-10-06 12:37:39 +00003479 ssl->in_offt = NULL;
3480
Paul Bakker92be97b2013-01-02 17:30:03 +01003481 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003482 ssl->in_msgtype = 0;
3483 ssl->in_msglen = 0;
3484 ssl->in_left = 0;
3485
3486 ssl->in_hslen = 0;
3487 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003488 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003489
Paul Bakker92be97b2013-01-02 17:30:03 +01003490 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003491 ssl->out_msgtype = 0;
3492 ssl->out_msglen = 0;
3493 ssl->out_left = 0;
3494
Paul Bakker48916f92012-09-16 19:57:18 +00003495 ssl->transform_in = NULL;
3496 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003497
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003498 ssl->renego_records_seen = 0;
3499
Paul Bakker7eb013f2011-10-06 12:37:39 +00003500 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3501 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003502
3503#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003504 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003505 {
3506 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003507 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003508 {
3509 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3510 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3511 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003512 }
3513#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003514
Paul Bakker48916f92012-09-16 19:57:18 +00003515 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003516 {
Paul Bakker48916f92012-09-16 19:57:18 +00003517 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003518 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003519 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520 }
Paul Bakker48916f92012-09-16 19:57:18 +00003521
Paul Bakkerc0463502013-02-14 11:19:38 +01003522 if( ssl->session )
3523 {
3524 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003525 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003526 ssl->session = NULL;
3527 }
3528
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003529#if defined(POLARSSL_SSL_ALPN)
3530 ssl->alpn_chosen = NULL;
3531#endif
3532
Paul Bakker48916f92012-09-16 19:57:18 +00003533 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3534 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003535
3536 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003537}
3538
Paul Bakkera503a632013-08-14 13:48:06 +02003539#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003540static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3541{
3542 aes_free( &tkeys->enc );
3543 aes_free( &tkeys->dec );
3544
3545 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3546}
3547
Paul Bakker7eb013f2011-10-06 12:37:39 +00003548/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003549 * Allocate and initialize ticket keys
3550 */
3551static int ssl_ticket_keys_init( ssl_context *ssl )
3552{
3553 int ret;
3554 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003555 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003556
3557 if( ssl->ticket_keys != NULL )
3558 return( 0 );
3559
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003560 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3561 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003562 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3563
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003564 aes_init( &tkeys->enc );
3565 aes_init( &tkeys->dec );
3566
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003567 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003568 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003569 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003570 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003571 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003572 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003573
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003574 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3575 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3576 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3577 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003578 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003579 polarssl_free( tkeys );
3580 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003581 }
3582
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003583 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003584 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003585 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003586 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003587 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003588 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003589
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003590 ssl->ticket_keys = tkeys;
3591
3592 return( 0 );
3593}
Paul Bakkera503a632013-08-14 13:48:06 +02003594#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003595
3596/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 * SSL set accessors
3598 */
3599void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3600{
3601 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003602
Paul Bakker606b4ba2013-08-14 16:52:14 +02003603#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003604 if( endpoint == SSL_IS_CLIENT )
3605 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003606#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003607}
3608
3609void ssl_set_authmode( ssl_context *ssl, int authmode )
3610{
3611 ssl->authmode = authmode;
3612}
3613
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003614#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003615void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003616 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003617 void *p_vrfy )
3618{
3619 ssl->f_vrfy = f_vrfy;
3620 ssl->p_vrfy = p_vrfy;
3621}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003622#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003623
Paul Bakker5121ce52009-01-03 21:22:43 +00003624void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003625 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003626 void *p_rng )
3627{
3628 ssl->f_rng = f_rng;
3629 ssl->p_rng = p_rng;
3630}
3631
3632void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003633 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003634 void *p_dbg )
3635{
3636 ssl->f_dbg = f_dbg;
3637 ssl->p_dbg = p_dbg;
3638}
3639
3640void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003641 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003642 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003643{
3644 ssl->f_recv = f_recv;
3645 ssl->f_send = f_send;
3646 ssl->p_recv = p_recv;
3647 ssl->p_send = p_send;
3648}
3649
Paul Bakker0a597072012-09-25 21:55:46 +00003650void ssl_set_session_cache( ssl_context *ssl,
3651 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3652 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003653{
Paul Bakker0a597072012-09-25 21:55:46 +00003654 ssl->f_get_cache = f_get_cache;
3655 ssl->p_get_cache = p_get_cache;
3656 ssl->f_set_cache = f_set_cache;
3657 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003658}
3659
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003660int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003661{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003662 int ret;
3663
3664 if( ssl == NULL ||
3665 session == NULL ||
3666 ssl->session_negotiate == NULL ||
3667 ssl->endpoint != SSL_IS_CLIENT )
3668 {
3669 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3670 }
3671
3672 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3673 return( ret );
3674
Paul Bakker0a597072012-09-25 21:55:46 +00003675 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003676
3677 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003678}
3679
Paul Bakkerb68cad62012-08-23 08:34:18 +00003680void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003681{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003682 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3683 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3684 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3685 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3686}
3687
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003688void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3689 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003690 int major, int minor )
3691{
3692 if( major != SSL_MAJOR_VERSION_3 )
3693 return;
3694
3695 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3696 return;
3697
3698 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003699}
3700
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003701#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003702/* Add a new (empty) key_cert entry an return a pointer to it */
3703static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3704{
3705 ssl_key_cert *key_cert, *last;
3706
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003707 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3708 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003709 return( NULL );
3710
3711 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3712
3713 /* Append the new key_cert to the (possibly empty) current list */
3714 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003715 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003716 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003717 if( ssl->handshake != NULL )
3718 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003719 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003720 else
3721 {
3722 last = ssl->key_cert;
3723 while( last->next != NULL )
3724 last = last->next;
3725 last->next = key_cert;
3726 }
3727
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003728 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003729}
3730
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003731void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003732 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003733{
3734 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003735 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003736 ssl->peer_cn = peer_cn;
3737}
3738
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003739int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003740 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003741{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003742 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3743
3744 if( key_cert == NULL )
3745 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3746
3747 key_cert->cert = own_cert;
3748 key_cert->key = pk_key;
3749
3750 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003751}
3752
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003753#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003754int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003755 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003756{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003757 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003758 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003759
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003760 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003761 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3762
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003763 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3764 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003765 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003766
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003767 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003768
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003769 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003770 if( ret != 0 )
3771 return( ret );
3772
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003773 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003774 return( ret );
3775
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003776 key_cert->cert = own_cert;
3777 key_cert->key_own_alloc = 1;
3778
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003779 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003780}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003781#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003782
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003783int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003784 void *rsa_key,
3785 rsa_decrypt_func rsa_decrypt,
3786 rsa_sign_func rsa_sign,
3787 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003788{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003789 int ret;
3790 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003791
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003792 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003793 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3794
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003795 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3796 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003797 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003798
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003799 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003800
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003801 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3802 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3803 return( ret );
3804
3805 key_cert->cert = own_cert;
3806 key_cert->key_own_alloc = 1;
3807
3808 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003809}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003810#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003811
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003812#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003813int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3814 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003815{
Paul Bakker6db455e2013-09-18 17:29:31 +02003816 if( psk == NULL || psk_identity == NULL )
3817 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3818
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003819 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003820 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3821
Paul Bakker6db455e2013-09-18 17:29:31 +02003822 if( ssl->psk != NULL )
3823 {
3824 polarssl_free( ssl->psk );
3825 polarssl_free( ssl->psk_identity );
3826 }
3827
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003828 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003829 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003830
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003831 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003832 ssl->psk_identity = (unsigned char *)
3833 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003834
3835 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3836 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3837
3838 memcpy( ssl->psk, psk, ssl->psk_len );
3839 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003840
3841 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003842}
3843
3844void ssl_set_psk_cb( ssl_context *ssl,
3845 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3846 size_t),
3847 void *p_psk )
3848{
3849 ssl->f_psk = f_psk;
3850 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003851}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003852#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003853
Paul Bakker48916f92012-09-16 19:57:18 +00003854#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003855int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003856{
3857 int ret;
3858
Paul Bakker48916f92012-09-16 19:57:18 +00003859 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003860 {
3861 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3862 return( ret );
3863 }
3864
Paul Bakker48916f92012-09-16 19:57:18 +00003865 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 {
3867 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3868 return( ret );
3869 }
3870
3871 return( 0 );
3872}
3873
Paul Bakker1b57b062011-01-06 15:48:19 +00003874int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3875{
3876 int ret;
3877
Paul Bakker66d5d072014-06-17 16:39:18 +02003878 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003879 {
3880 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3881 return( ret );
3882 }
3883
Paul Bakker66d5d072014-06-17 16:39:18 +02003884 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003885 {
3886 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3887 return( ret );
3888 }
3889
3890 return( 0 );
3891}
Paul Bakker48916f92012-09-16 19:57:18 +00003892#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003893
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003894#if defined(POLARSSL_SSL_SET_CURVES)
3895/*
3896 * Set the allowed elliptic curves
3897 */
3898void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3899{
3900 ssl->curve_list = curve_list;
3901}
3902#endif
3903
Paul Bakker0be444a2013-08-27 21:55:01 +02003904#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003905int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003906{
3907 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003908 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003909
3910 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003911
3912 if( ssl->hostname_len + 1 == 0 )
3913 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3914
Paul Bakker6e339b52013-07-03 13:37:05 +02003915 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003916
Paul Bakkerb15b8512012-01-13 13:44:06 +00003917 if( ssl->hostname == NULL )
3918 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3919
Paul Bakker3c2122f2013-06-24 19:03:14 +02003920 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003921 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003922
Paul Bakker40ea7de2009-05-03 10:18:48 +00003923 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003924
3925 return( 0 );
3926}
3927
Paul Bakker5701cdc2012-09-27 21:49:42 +00003928void ssl_set_sni( ssl_context *ssl,
3929 int (*f_sni)(void *, ssl_context *,
3930 const unsigned char *, size_t),
3931 void *p_sni )
3932{
3933 ssl->f_sni = f_sni;
3934 ssl->p_sni = p_sni;
3935}
Paul Bakker0be444a2013-08-27 21:55:01 +02003936#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003937
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003938#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003939int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003940{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003941 size_t cur_len, tot_len;
3942 const char **p;
3943
3944 /*
3945 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3946 * truncated". Check lengths now rather than later.
3947 */
3948 tot_len = 0;
3949 for( p = protos; *p != NULL; p++ )
3950 {
3951 cur_len = strlen( *p );
3952 tot_len += cur_len;
3953
3954 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3955 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3956 }
3957
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003958 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003959
3960 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003961}
3962
3963const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3964{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003965 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003966}
3967#endif /* POLARSSL_SSL_ALPN */
3968
Paul Bakker490ecc82011-10-06 13:04:09 +00003969void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3970{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003971 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3972 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3973 {
3974 ssl->max_major_ver = major;
3975 ssl->max_minor_ver = minor;
3976 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003977}
3978
Paul Bakker1d29fb52012-09-28 13:28:45 +00003979void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3980{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003981 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3982 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3983 {
3984 ssl->min_major_ver = major;
3985 ssl->min_minor_ver = minor;
3986 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003987}
3988
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02003989#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
3990void ssl_set_fallback( ssl_context *ssl, char fallback )
3991{
3992 ssl->fallback = fallback;
3993}
3994#endif
3995
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003996#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3997void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
3998{
3999 ssl->extended_ms = ems;
4000}
4001#endif
4002
Paul Bakker05decb22013-08-15 13:33:48 +02004003#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004004int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4005{
Paul Bakker77e257e2013-12-16 15:29:52 +01004006 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004007 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004008 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004009 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004010 }
4011
4012 ssl->mfl_code = mfl_code;
4013
4014 return( 0 );
4015}
Paul Bakker05decb22013-08-15 13:33:48 +02004016#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004017
Paul Bakker1f2bc622013-08-15 13:45:55 +02004018#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004019int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004020{
4021 if( ssl->endpoint != SSL_IS_CLIENT )
4022 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4023
Paul Bakker8c1ede62013-07-19 14:14:37 +02004024 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004025
4026 return( 0 );
4027}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004028#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004029
Paul Bakker48916f92012-09-16 19:57:18 +00004030void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4031{
4032 ssl->disable_renegotiation = renegotiation;
4033}
4034
4035void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4036{
4037 ssl->allow_legacy_renegotiation = allow_legacy;
4038}
4039
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004040void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4041{
4042 ssl->renego_max_records = max_records;
4043}
4044
Paul Bakkera503a632013-08-14 13:48:06 +02004045#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004046int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4047{
4048 ssl->session_tickets = use_tickets;
4049
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004050 if( ssl->endpoint == SSL_IS_CLIENT )
4051 return( 0 );
4052
4053 if( ssl->f_rng == NULL )
4054 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4055
4056 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004057}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004058
4059void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4060{
4061 ssl->ticket_lifetime = lifetime;
4062}
Paul Bakkera503a632013-08-14 13:48:06 +02004063#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004064
Paul Bakker5121ce52009-01-03 21:22:43 +00004065/*
4066 * SSL get accessors
4067 */
Paul Bakker23986e52011-04-24 08:57:21 +00004068size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004069{
4070 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4071}
4072
Paul Bakkerff60ee62010-03-16 21:09:09 +00004073int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004074{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004075 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004076}
4077
Paul Bakkere3166ce2011-01-27 17:40:50 +00004078const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004079{
Paul Bakker926c8e42013-03-06 10:23:34 +01004080 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004081 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004082
Paul Bakkere3166ce2011-01-27 17:40:50 +00004083 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004084}
4085
Paul Bakker43ca69c2011-01-15 17:35:19 +00004086const char *ssl_get_version( const ssl_context *ssl )
4087{
4088 switch( ssl->minor_ver )
4089 {
4090 case SSL_MINOR_VERSION_0:
4091 return( "SSLv3.0" );
4092
4093 case SSL_MINOR_VERSION_1:
4094 return( "TLSv1.0" );
4095
4096 case SSL_MINOR_VERSION_2:
4097 return( "TLSv1.1" );
4098
Paul Bakker1ef83d62012-04-11 12:09:53 +00004099 case SSL_MINOR_VERSION_3:
4100 return( "TLSv1.2" );
4101
Paul Bakker43ca69c2011-01-15 17:35:19 +00004102 default:
4103 break;
4104 }
4105 return( "unknown" );
4106}
4107
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004108#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004109const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004110{
4111 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004112 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004113
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004114 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004115}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004116#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004117
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004118int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4119{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004120 if( ssl == NULL ||
4121 dst == NULL ||
4122 ssl->session == NULL ||
4123 ssl->endpoint != SSL_IS_CLIENT )
4124 {
4125 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4126 }
4127
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004128 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004129}
4130
Paul Bakker5121ce52009-01-03 21:22:43 +00004131/*
Paul Bakker1961b702013-01-25 14:49:24 +01004132 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004133 */
Paul Bakker1961b702013-01-25 14:49:24 +01004134int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004135{
Paul Bakker40e46942009-01-03 21:51:57 +00004136 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004137
Paul Bakker40e46942009-01-03 21:51:57 +00004138#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004139 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004140 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004141#endif
4142
Paul Bakker40e46942009-01-03 21:51:57 +00004143#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004144 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004145 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004146#endif
4147
Paul Bakker1961b702013-01-25 14:49:24 +01004148 return( ret );
4149}
4150
4151/*
4152 * Perform the SSL handshake
4153 */
4154int ssl_handshake( ssl_context *ssl )
4155{
4156 int ret = 0;
4157
4158 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4159
4160 while( ssl->state != SSL_HANDSHAKE_OVER )
4161 {
4162 ret = ssl_handshake_step( ssl );
4163
4164 if( ret != 0 )
4165 break;
4166 }
4167
Paul Bakker5121ce52009-01-03 21:22:43 +00004168 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4169
4170 return( ret );
4171}
4172
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004173#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004174/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004175 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004176 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004177static int ssl_write_hello_request( ssl_context *ssl )
4178{
4179 int ret;
4180
4181 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4182
4183 ssl->out_msglen = 4;
4184 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4185 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4186
4187 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4188 {
4189 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4190 return( ret );
4191 }
4192
4193 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4194
4195 return( 0 );
4196}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004197#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004198
4199/*
4200 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004201 * - any side: calling ssl_renegotiate(),
4202 * - client: receiving a HelloRequest during ssl_read(),
4203 * - server: receiving any handshake message on server during ssl_read() after
4204 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004205 * If the handshake doesn't complete due to waiting for I/O, it will continue
4206 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004207 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004208static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004209{
4210 int ret;
4211
4212 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4213
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004214 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4215 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004216
4217 ssl->state = SSL_HELLO_REQUEST;
4218 ssl->renegotiation = SSL_RENEGOTIATION;
4219
Paul Bakker48916f92012-09-16 19:57:18 +00004220 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4221 {
4222 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4223 return( ret );
4224 }
4225
4226 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4227
4228 return( 0 );
4229}
4230
4231/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004232 * Renegotiate current connection on client,
4233 * or request renegotiation on server
4234 */
4235int ssl_renegotiate( ssl_context *ssl )
4236{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004237 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004238
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004239#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004240 /* On server, just send the request */
4241 if( ssl->endpoint == SSL_IS_SERVER )
4242 {
4243 if( ssl->state != SSL_HANDSHAKE_OVER )
4244 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4245
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004246 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4247
4248 /* Did we already try/start sending HelloRequest? */
4249 if( ssl->out_left != 0 )
4250 return( ssl_flush_output( ssl ) );
4251
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004252 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004253 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004254#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004255
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004256#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004257 /*
4258 * On client, either start the renegotiation process or,
4259 * if already in progress, continue the handshake
4260 */
4261 if( ssl->renegotiation != SSL_RENEGOTIATION )
4262 {
4263 if( ssl->state != SSL_HANDSHAKE_OVER )
4264 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4265
4266 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4267 {
4268 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4269 return( ret );
4270 }
4271 }
4272 else
4273 {
4274 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4275 {
4276 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4277 return( ret );
4278 }
4279 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004280#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004281
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004282 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004283}
4284
4285/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004286 * Receive application data decrypted from the SSL layer
4287 */
Paul Bakker23986e52011-04-24 08:57:21 +00004288int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004289{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004290 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004291 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004292
4293 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4294
4295 if( ssl->state != SSL_HANDSHAKE_OVER )
4296 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004297 ret = ssl_handshake( ssl );
4298 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4299 {
4300 record_read = 1;
4301 }
4302 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004303 {
4304 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4305 return( ret );
4306 }
4307 }
4308
4309 if( ssl->in_offt == NULL )
4310 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004311 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004312 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004313 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4314 {
4315 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4316 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004317
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004318 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4319 return( ret );
4320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004321 }
4322
4323 if( ssl->in_msglen == 0 &&
4324 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4325 {
4326 /*
4327 * OpenSSL sends empty messages to randomize the IV
4328 */
4329 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4330 {
Paul Bakker831a7552011-05-18 13:32:51 +00004331 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4332 return( 0 );
4333
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4335 return( ret );
4336 }
4337 }
4338
Paul Bakker48916f92012-09-16 19:57:18 +00004339 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4340 {
4341 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4342
4343 if( ssl->endpoint == SSL_IS_CLIENT &&
4344 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4345 ssl->in_hslen != 4 ) )
4346 {
4347 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4348 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4349 }
4350
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004351 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4352 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004353 ssl->allow_legacy_renegotiation ==
4354 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004355 {
4356 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4357
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004358#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004359 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004360 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004361 /*
4362 * SSLv3 does not have a "no_renegotiation" alert
4363 */
4364 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4365 return( ret );
4366 }
4367 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004368#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004369#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4370 defined(POLARSSL_SSL_PROTO_TLS1_2)
4371 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004372 {
4373 if( ( ret = ssl_send_alert_message( ssl,
4374 SSL_ALERT_LEVEL_WARNING,
4375 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4376 {
4377 return( ret );
4378 }
Paul Bakker48916f92012-09-16 19:57:18 +00004379 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004380 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004381#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4382 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004383 {
4384 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004385 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004386 }
Paul Bakker48916f92012-09-16 19:57:18 +00004387 }
4388 else
4389 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004390 ret = ssl_start_renegotiation( ssl );
4391 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4392 {
4393 record_read = 1;
4394 }
4395 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004396 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004397 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004398 return( ret );
4399 }
Paul Bakker48916f92012-09-16 19:57:18 +00004400 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004401
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004402 /* If a non-handshake record was read during renego, fallthrough,
4403 * else tell the user they should call ssl_read() again */
4404 if( ! record_read )
4405 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004406 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004407 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4408 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004409 ssl->renego_records_seen++;
4410
4411 if( ssl->renego_max_records >= 0 &&
4412 ssl->renego_records_seen > ssl->renego_max_records )
4413 {
4414 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4415 "but not honored by client" ) );
4416 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4417 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004418 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004419
4420 /* Fatal and closure alerts handled by ssl_read_record() */
4421 if( ssl->in_msgtype == SSL_MSG_ALERT )
4422 {
4423 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4424 return( POLARSSL_ERR_NET_WANT_READ );
4425 }
4426
4427 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004428 {
4429 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004430 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004431 }
4432
4433 ssl->in_offt = ssl->in_msg;
4434 }
4435
4436 n = ( len < ssl->in_msglen )
4437 ? len : ssl->in_msglen;
4438
4439 memcpy( buf, ssl->in_offt, n );
4440 ssl->in_msglen -= n;
4441
4442 if( ssl->in_msglen == 0 )
4443 /* all bytes consumed */
4444 ssl->in_offt = NULL;
4445 else
4446 /* more data available */
4447 ssl->in_offt += n;
4448
4449 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4450
Paul Bakker23986e52011-04-24 08:57:21 +00004451 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004452}
4453
4454/*
4455 * Send application data to be encrypted by the SSL layer
4456 */
Paul Bakker23986e52011-04-24 08:57:21 +00004457int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004458{
Paul Bakker23986e52011-04-24 08:57:21 +00004459 int ret;
4460 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004461 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004462
4463 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4464
4465 if( ssl->state != SSL_HANDSHAKE_OVER )
4466 {
4467 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4468 {
4469 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4470 return( ret );
4471 }
4472 }
4473
Paul Bakker05decb22013-08-15 13:33:48 +02004474#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004475 /*
4476 * Assume mfl_code is correct since it was checked when set
4477 */
4478 max_len = mfl_code_to_length[ssl->mfl_code];
4479
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004480 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004481 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004482 */
4483 if( ssl->session_out != NULL &&
4484 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4485 {
4486 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4487 }
Paul Bakker05decb22013-08-15 13:33:48 +02004488#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004489
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004490 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004491
Paul Bakker5121ce52009-01-03 21:22:43 +00004492 if( ssl->out_left != 0 )
4493 {
4494 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4495 {
4496 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4497 return( ret );
4498 }
4499 }
Paul Bakker887bd502011-06-08 13:10:54 +00004500 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004501 {
Paul Bakker887bd502011-06-08 13:10:54 +00004502 ssl->out_msglen = n;
4503 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4504 memcpy( ssl->out_msg, buf, n );
4505
4506 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4507 {
4508 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4509 return( ret );
4510 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004511 }
4512
4513 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4514
Paul Bakker23986e52011-04-24 08:57:21 +00004515 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004516}
4517
4518/*
4519 * Notify the peer that the connection is being closed
4520 */
4521int ssl_close_notify( ssl_context *ssl )
4522{
4523 int ret;
4524
4525 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4526
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004527 if( ssl->out_left != 0 )
4528 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004529
4530 if( ssl->state == SSL_HANDSHAKE_OVER )
4531 {
Paul Bakker48916f92012-09-16 19:57:18 +00004532 if( ( ret = ssl_send_alert_message( ssl,
4533 SSL_ALERT_LEVEL_WARNING,
4534 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004535 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004536 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004537 return( ret );
4538 }
4539 }
4540
4541 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4542
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004543 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004544}
4545
Paul Bakker48916f92012-09-16 19:57:18 +00004546void ssl_transform_free( ssl_transform *transform )
4547{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004548 if( transform == NULL )
4549 return;
4550
Paul Bakker48916f92012-09-16 19:57:18 +00004551#if defined(POLARSSL_ZLIB_SUPPORT)
4552 deflateEnd( &transform->ctx_deflate );
4553 inflateEnd( &transform->ctx_inflate );
4554#endif
4555
Paul Bakker84bbeb52014-07-01 14:53:22 +02004556 cipher_free( &transform->cipher_ctx_enc );
4557 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004558
Paul Bakker84bbeb52014-07-01 14:53:22 +02004559 md_free( &transform->md_ctx_enc );
4560 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004561
Paul Bakker34617722014-06-13 17:20:13 +02004562 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004563}
4564
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004565#if defined(POLARSSL_X509_CRT_PARSE_C)
4566static void ssl_key_cert_free( ssl_key_cert *key_cert )
4567{
4568 ssl_key_cert *cur = key_cert, *next;
4569
4570 while( cur != NULL )
4571 {
4572 next = cur->next;
4573
4574 if( cur->key_own_alloc )
4575 {
4576 pk_free( cur->key );
4577 polarssl_free( cur->key );
4578 }
4579 polarssl_free( cur );
4580
4581 cur = next;
4582 }
4583}
4584#endif /* POLARSSL_X509_CRT_PARSE_C */
4585
Paul Bakker48916f92012-09-16 19:57:18 +00004586void ssl_handshake_free( ssl_handshake_params *handshake )
4587{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004588 if( handshake == NULL )
4589 return;
4590
Paul Bakker48916f92012-09-16 19:57:18 +00004591#if defined(POLARSSL_DHM_C)
4592 dhm_free( &handshake->dhm_ctx );
4593#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004594#if defined(POLARSSL_ECDH_C)
4595 ecdh_free( &handshake->ecdh_ctx );
4596#endif
4597
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004598#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004599 /* explicit void pointer cast for buggy MS compiler */
4600 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004601#endif
4602
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004603#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4604 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4605 /*
4606 * Free only the linked list wrapper, not the keys themselves
4607 * since the belong to the SNI callback
4608 */
4609 if( handshake->sni_key_cert != NULL )
4610 {
4611 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4612
4613 while( cur != NULL )
4614 {
4615 next = cur->next;
4616 polarssl_free( cur );
4617 cur = next;
4618 }
4619 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004620#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004621
Paul Bakker34617722014-06-13 17:20:13 +02004622 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004623}
4624
4625void ssl_session_free( ssl_session *session )
4626{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004627 if( session == NULL )
4628 return;
4629
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004630#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004631 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004632 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004633 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004634 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004635 }
Paul Bakkered27a042013-04-18 22:46:23 +02004636#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004637
Paul Bakkera503a632013-08-14 13:48:06 +02004638#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004639 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004640#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004641
Paul Bakker34617722014-06-13 17:20:13 +02004642 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004643}
4644
Paul Bakker5121ce52009-01-03 21:22:43 +00004645/*
4646 * Free an SSL context
4647 */
4648void ssl_free( ssl_context *ssl )
4649{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004650 if( ssl == NULL )
4651 return;
4652
Paul Bakker5121ce52009-01-03 21:22:43 +00004653 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4654
Paul Bakker5121ce52009-01-03 21:22:43 +00004655 if( ssl->out_ctr != NULL )
4656 {
Paul Bakker34617722014-06-13 17:20:13 +02004657 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004658 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004659 }
4660
4661 if( ssl->in_ctr != NULL )
4662 {
Paul Bakker34617722014-06-13 17:20:13 +02004663 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004664 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 }
4666
Paul Bakker16770332013-10-11 09:59:44 +02004667#if defined(POLARSSL_ZLIB_SUPPORT)
4668 if( ssl->compress_buf != NULL )
4669 {
Paul Bakker34617722014-06-13 17:20:13 +02004670 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004671 polarssl_free( ssl->compress_buf );
4672 }
4673#endif
4674
Paul Bakker40e46942009-01-03 21:51:57 +00004675#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004676 mpi_free( &ssl->dhm_P );
4677 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004678#endif
4679
Paul Bakker48916f92012-09-16 19:57:18 +00004680 if( ssl->transform )
4681 {
4682 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004683 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004684 }
4685
4686 if( ssl->handshake )
4687 {
4688 ssl_handshake_free( ssl->handshake );
4689 ssl_transform_free( ssl->transform_negotiate );
4690 ssl_session_free( ssl->session_negotiate );
4691
Paul Bakker6e339b52013-07-03 13:37:05 +02004692 polarssl_free( ssl->handshake );
4693 polarssl_free( ssl->transform_negotiate );
4694 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004695 }
4696
Paul Bakkerc0463502013-02-14 11:19:38 +01004697 if( ssl->session )
4698 {
4699 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004700 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004701 }
4702
Paul Bakkera503a632013-08-14 13:48:06 +02004703#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004704 if( ssl->ticket_keys )
4705 {
4706 ssl_ticket_keys_free( ssl->ticket_keys );
4707 polarssl_free( ssl->ticket_keys );
4708 }
Paul Bakkera503a632013-08-14 13:48:06 +02004709#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004710
Paul Bakker0be444a2013-08-27 21:55:01 +02004711#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004712 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004713 {
Paul Bakker34617722014-06-13 17:20:13 +02004714 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004715 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004716 ssl->hostname_len = 0;
4717 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004718#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004719
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004720#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004721 if( ssl->psk != NULL )
4722 {
Paul Bakker34617722014-06-13 17:20:13 +02004723 polarssl_zeroize( ssl->psk, ssl->psk_len );
4724 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004725 polarssl_free( ssl->psk );
4726 polarssl_free( ssl->psk_identity );
4727 ssl->psk_len = 0;
4728 ssl->psk_identity_len = 0;
4729 }
4730#endif
4731
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004732#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004733 ssl_key_cert_free( ssl->key_cert );
4734#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004735
Paul Bakker05ef8352012-05-08 09:17:57 +00004736#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4737 if( ssl_hw_record_finish != NULL )
4738 {
4739 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4740 ssl_hw_record_finish( ssl );
4741 }
4742#endif
4743
Paul Bakker5121ce52009-01-03 21:22:43 +00004744 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004745
Paul Bakker86f04f42013-02-14 11:20:09 +01004746 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004747 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004748}
4749
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004750#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004751/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004752 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004753 */
4754unsigned char ssl_sig_from_pk( pk_context *pk )
4755{
4756#if defined(POLARSSL_RSA_C)
4757 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4758 return( SSL_SIG_RSA );
4759#endif
4760#if defined(POLARSSL_ECDSA_C)
4761 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4762 return( SSL_SIG_ECDSA );
4763#endif
4764 return( SSL_SIG_ANON );
4765}
4766
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004767pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4768{
4769 switch( sig )
4770 {
4771#if defined(POLARSSL_RSA_C)
4772 case SSL_SIG_RSA:
4773 return( POLARSSL_PK_RSA );
4774#endif
4775#if defined(POLARSSL_ECDSA_C)
4776 case SSL_SIG_ECDSA:
4777 return( POLARSSL_PK_ECDSA );
4778#endif
4779 default:
4780 return( POLARSSL_PK_NONE );
4781 }
4782}
Paul Bakker9af723c2014-05-01 13:03:14 +02004783#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004784
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004785/*
4786 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4787 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004788md_type_t ssl_md_alg_from_hash( unsigned char hash )
4789{
4790 switch( hash )
4791 {
4792#if defined(POLARSSL_MD5_C)
4793 case SSL_HASH_MD5:
4794 return( POLARSSL_MD_MD5 );
4795#endif
4796#if defined(POLARSSL_SHA1_C)
4797 case SSL_HASH_SHA1:
4798 return( POLARSSL_MD_SHA1 );
4799#endif
4800#if defined(POLARSSL_SHA256_C)
4801 case SSL_HASH_SHA224:
4802 return( POLARSSL_MD_SHA224 );
4803 case SSL_HASH_SHA256:
4804 return( POLARSSL_MD_SHA256 );
4805#endif
4806#if defined(POLARSSL_SHA512_C)
4807 case SSL_HASH_SHA384:
4808 return( POLARSSL_MD_SHA384 );
4809 case SSL_HASH_SHA512:
4810 return( POLARSSL_MD_SHA512 );
4811#endif
4812 default:
4813 return( POLARSSL_MD_NONE );
4814 }
4815}
4816
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004817#if defined(POLARSSL_SSL_SET_CURVES)
4818/*
4819 * Check is a curve proposed by the peer is in our list.
4820 * Return 1 if we're willing to use it, 0 otherwise.
4821 */
4822int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4823{
4824 const ecp_group_id *gid;
4825
4826 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4827 if( *gid == grp_id )
4828 return( 1 );
4829
4830 return( 0 );
4831}
Paul Bakker9af723c2014-05-01 13:03:14 +02004832#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004833
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004834#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004835int ssl_check_cert_usage( const x509_crt *cert,
4836 const ssl_ciphersuite_t *ciphersuite,
4837 int cert_endpoint )
4838{
4839#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4840 int usage = 0;
4841#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004842#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4843 const char *ext_oid;
4844 size_t ext_len;
4845#endif
4846
4847#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4848 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4849 ((void) cert);
4850 ((void) cert_endpoint);
4851#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004852
4853#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4854 if( cert_endpoint == SSL_IS_SERVER )
4855 {
4856 /* Server part of the key exchange */
4857 switch( ciphersuite->key_exchange )
4858 {
4859 case POLARSSL_KEY_EXCHANGE_RSA:
4860 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4861 usage = KU_KEY_ENCIPHERMENT;
4862 break;
4863
4864 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4865 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4866 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4867 usage = KU_DIGITAL_SIGNATURE;
4868 break;
4869
4870 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4871 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4872 usage = KU_KEY_AGREEMENT;
4873 break;
4874
4875 /* Don't use default: we want warnings when adding new values */
4876 case POLARSSL_KEY_EXCHANGE_NONE:
4877 case POLARSSL_KEY_EXCHANGE_PSK:
4878 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4879 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4880 usage = 0;
4881 }
4882 }
4883 else
4884 {
4885 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4886 usage = KU_DIGITAL_SIGNATURE;
4887 }
4888
4889 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4890 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004891#else
4892 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004893#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4894
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004895#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4896 if( cert_endpoint == SSL_IS_SERVER )
4897 {
4898 ext_oid = OID_SERVER_AUTH;
4899 ext_len = OID_SIZE( OID_SERVER_AUTH );
4900 }
4901 else
4902 {
4903 ext_oid = OID_CLIENT_AUTH;
4904 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4905 }
4906
4907 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4908 return( -1 );
4909#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4910
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004911 return( 0 );
4912}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004913#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004914
4915#endif /* POLARSSL_SSL_TLS_C */