blob: 27ee058e405d4123d4c8c89266d83c4e52944c46 [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
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
70static inline size_t ssl_ep_len( const ssl_context *ssl )
71{
72#if defined(POLARSSL_SSL_PROTO_DTLS)
73 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
74 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Paul Bakker05decb22013-08-15 13:33:48 +020081#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020082/*
83 * Convert max_fragment_length codes to length.
84 * RFC 6066 says:
85 * enum{
86 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
87 * } MaxFragmentLength;
88 * and we add 0 -> extension unused
89 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020090static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020091{
92 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
93 512, /* SSL_MAX_FRAG_LEN_512 */
94 1024, /* SSL_MAX_FRAG_LEN_1024 */
95 2048, /* SSL_MAX_FRAG_LEN_2048 */
96 4096, /* SSL_MAX_FRAG_LEN_4096 */
97};
Paul Bakker05decb22013-08-15 13:33:48 +020098#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020099
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
101{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200102 ssl_session_free( dst );
103 memcpy( dst, src, sizeof( ssl_session ) );
104
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 if( src->peer_cert != NULL )
107 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200108 int ret;
109
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200110 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
111 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200112 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
113
Paul Bakkerb6b09562013-09-18 14:17:41 +0200114 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200116 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
117 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200118 {
119 polarssl_free( dst->peer_cert );
120 dst->peer_cert = NULL;
121 return( ret );
122 }
123 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200125
Paul Bakkera503a632013-08-14 13:48:06 +0200126#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200127 if( src->ticket != NULL )
128 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200129 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
130 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200131 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
132
133 memcpy( dst->ticket, src->ticket, src->ticket_len );
134 }
Paul Bakkera503a632013-08-14 13:48:06 +0200135#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200136
137 return( 0 );
138}
139
Paul Bakker05ef8352012-05-08 09:17:57 +0000140#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200141int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200142 const unsigned char *key_enc, const unsigned char *key_dec,
143 size_t keylen,
144 const unsigned char *iv_enc, const unsigned char *iv_dec,
145 size_t ivlen,
146 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200147 size_t maclen ) = NULL;
148int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
149int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
150int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
151int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
152int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200153#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000154
Paul Bakker5121ce52009-01-03 21:22:43 +0000155/*
156 * Key material generation
157 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200158#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200159static int ssl3_prf( const unsigned char *secret, size_t slen,
160 const char *label,
161 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 unsigned char *dstbuf, size_t dlen )
163{
164 size_t i;
165 md5_context md5;
166 sha1_context sha1;
167 unsigned char padding[16];
168 unsigned char sha1sum[20];
169 ((void)label);
170
Paul Bakker5b4af392014-06-26 12:09:34 +0200171 md5_init( &md5 );
172 sha1_init( &sha1 );
173
Paul Bakker5f70b252012-09-13 14:23:06 +0000174 /*
175 * SSLv3:
176 * block =
177 * MD5( secret + SHA1( 'A' + secret + random ) ) +
178 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
179 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
180 * ...
181 */
182 for( i = 0; i < dlen / 16; i++ )
183 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200184 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000185
186 sha1_starts( &sha1 );
187 sha1_update( &sha1, padding, 1 + i );
188 sha1_update( &sha1, secret, slen );
189 sha1_update( &sha1, random, rlen );
190 sha1_finish( &sha1, sha1sum );
191
192 md5_starts( &md5 );
193 md5_update( &md5, secret, slen );
194 md5_update( &md5, sha1sum, 20 );
195 md5_finish( &md5, dstbuf + i * 16 );
196 }
197
Paul Bakker5b4af392014-06-26 12:09:34 +0200198 md5_free( &md5 );
199 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000200
Paul Bakker34617722014-06-13 17:20:13 +0200201 polarssl_zeroize( padding, sizeof( padding ) );
202 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000203
204 return( 0 );
205}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200206#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000207
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200208#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200209static int tls1_prf( const unsigned char *secret, size_t slen,
210 const char *label,
211 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000212 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000213{
Paul Bakker23986e52011-04-24 08:57:21 +0000214 size_t nb, hs;
215 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200216 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 unsigned char tmp[128];
218 unsigned char h_i[20];
219
220 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000221 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223 hs = ( slen + 1 ) / 2;
224 S1 = secret;
225 S2 = secret + slen - hs;
226
227 nb = strlen( label );
228 memcpy( tmp + 20, label, nb );
229 memcpy( tmp + 20 + nb, random, rlen );
230 nb += rlen;
231
232 /*
233 * First compute P_md5(secret,label+random)[0..dlen]
234 */
235 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
236
237 for( i = 0; i < dlen; i += 16 )
238 {
239 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
240 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
241
242 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
243
244 for( j = 0; j < k; j++ )
245 dstbuf[i + j] = h_i[j];
246 }
247
248 /*
249 * XOR out with P_sha1(secret,label+random)[0..dlen]
250 */
251 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
252
253 for( i = 0; i < dlen; i += 20 )
254 {
255 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
256 sha1_hmac( S2, hs, tmp, 20, tmp );
257
258 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
259
260 for( j = 0; j < k; j++ )
261 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
262 }
263
Paul Bakker34617722014-06-13 17:20:13 +0200264 polarssl_zeroize( tmp, sizeof( tmp ) );
265 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267 return( 0 );
268}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200269#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200271#if defined(POLARSSL_SSL_PROTO_TLS1_2)
272#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200273static int tls_prf_sha256( const unsigned char *secret, size_t slen,
274 const char *label,
275 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000276 unsigned char *dstbuf, size_t dlen )
277{
278 size_t nb;
279 size_t i, j, k;
280 unsigned char tmp[128];
281 unsigned char h_i[32];
282
283 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
284 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
285
286 nb = strlen( label );
287 memcpy( tmp + 32, label, nb );
288 memcpy( tmp + 32 + nb, random, rlen );
289 nb += rlen;
290
291 /*
292 * Compute P_<hash>(secret, label + random)[0..dlen]
293 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200294 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000295
296 for( i = 0; i < dlen; i += 32 )
297 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200298 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
299 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000300
301 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
302
303 for( j = 0; j < k; j++ )
304 dstbuf[i + j] = h_i[j];
305 }
306
Paul Bakker34617722014-06-13 17:20:13 +0200307 polarssl_zeroize( tmp, sizeof( tmp ) );
308 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000309
310 return( 0 );
311}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200312#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000313
Paul Bakker9e36f042013-06-30 14:34:05 +0200314#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200315static int tls_prf_sha384( const unsigned char *secret, size_t slen,
316 const char *label,
317 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000318 unsigned char *dstbuf, size_t dlen )
319{
320 size_t nb;
321 size_t i, j, k;
322 unsigned char tmp[128];
323 unsigned char h_i[48];
324
325 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
326 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
327
328 nb = strlen( label );
329 memcpy( tmp + 48, label, nb );
330 memcpy( tmp + 48 + nb, random, rlen );
331 nb += rlen;
332
333 /*
334 * Compute P_<hash>(secret, label + random)[0..dlen]
335 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200336 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337
338 for( i = 0; i < dlen; i += 48 )
339 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200340 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
341 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342
343 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
344
345 for( j = 0; j < k; j++ )
346 dstbuf[i + j] = h_i[j];
347 }
348
Paul Bakker34617722014-06-13 17:20:13 +0200349 polarssl_zeroize( tmp, sizeof( tmp ) );
350 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000351
352 return( 0 );
353}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200354#endif /* POLARSSL_SHA512_C */
355#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000356
Paul Bakker66d5d072014-06-17 16:39:18 +0200357static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200358
359#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
360 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200361static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200362#endif
Paul Bakker380da532012-04-18 16:10:25 +0000363
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200365static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
366static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
368
369#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
371static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200372#endif
373
374#if defined(POLARSSL_SSL_PROTO_TLS1_2)
375#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200376static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
377static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
378static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200379#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100380
Paul Bakker9e36f042013-06-30 14:34:05 +0200381#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200382static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
383static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
384static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100385#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200386#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000387
Paul Bakker5121ce52009-01-03 21:22:43 +0000388int ssl_derive_keys( ssl_context *ssl )
389{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200390 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 unsigned char keyblk[256];
393 unsigned char *key1;
394 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100395 unsigned char *mac_enc;
396 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200397 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100398 const cipher_info_t *cipher_info;
399 const md_info_t *md_info;
400
Paul Bakker48916f92012-09-16 19:57:18 +0000401 ssl_session *session = ssl->session_negotiate;
402 ssl_transform *transform = ssl->transform_negotiate;
403 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
406
Paul Bakker68884e32013-01-07 18:20:04 +0100407 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
408 if( cipher_info == NULL )
409 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200410 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100411 transform->ciphersuite_info->cipher ) );
412 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
413 }
414
415 md_info = md_info_from_type( transform->ciphersuite_info->mac );
416 if( md_info == NULL )
417 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200418 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100419 transform->ciphersuite_info->mac ) );
420 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
421 }
422
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000424 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000425 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000427 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = ssl3_prf;
430 handshake->calc_verify = ssl_calc_verify_ssl;
431 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200433 else
434#endif
435#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
436 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 {
Paul Bakker48916f92012-09-16 19:57:18 +0000438 handshake->tls_prf = tls1_prf;
439 handshake->calc_verify = ssl_calc_verify_tls;
440 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000441 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442 else
443#endif
444#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200445#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200446 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
447 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000448 {
Paul Bakker48916f92012-09-16 19:57:18 +0000449 handshake->tls_prf = tls_prf_sha384;
450 handshake->calc_verify = ssl_calc_verify_tls_sha384;
451 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000452 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000453 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200454#endif
455#if defined(POLARSSL_SHA256_C)
456 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000457 {
Paul Bakker48916f92012-09-16 19:57:18 +0000458 handshake->tls_prf = tls_prf_sha256;
459 handshake->calc_verify = ssl_calc_verify_tls_sha256;
460 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000461 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200462 else
463#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200464#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200465 {
466 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200467 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200468 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000469
470 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 * SSLv3:
472 * master =
473 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
474 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
475 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200476 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200477 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 * master = PRF( premaster, "master secret", randbytes )[0..47]
479 */
Paul Bakker0a597072012-09-25 21:55:46 +0000480 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 {
Paul Bakker48916f92012-09-16 19:57:18 +0000482 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
483 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Paul Bakker48916f92012-09-16 19:57:18 +0000485 handshake->tls_prf( handshake->premaster, handshake->pmslen,
486 "master secret",
487 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
Paul Bakker34617722014-06-13 17:20:13 +0200489 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 }
491 else
492 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
493
494 /*
495 * Swap the client and server random values.
496 */
Paul Bakker48916f92012-09-16 19:57:18 +0000497 memcpy( tmp, handshake->randbytes, 64 );
498 memcpy( handshake->randbytes, tmp + 32, 32 );
499 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200500 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 /*
503 * SSLv3:
504 * key block =
505 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
506 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
507 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
508 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
509 * ...
510 *
511 * TLSv1:
512 * key block = PRF( master, "key expansion", randbytes )
513 */
Paul Bakker48916f92012-09-16 19:57:18 +0000514 handshake->tls_prf( session->master, 48, "key expansion",
515 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Paul Bakker48916f92012-09-16 19:57:18 +0000517 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
518 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
519 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
520 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
522
Paul Bakker34617722014-06-13 17:20:13 +0200523 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 /*
526 * Determine the appropriate key, IV and MAC length.
527 */
Paul Bakker68884e32013-01-07 18:20:04 +0100528
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200529 transform->keylen = cipher_info->key_length / 8;
530
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200531 if( cipher_info->mode == POLARSSL_MODE_GCM ||
532 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200534 transform->maclen = 0;
535
Paul Bakker68884e32013-01-07 18:20:04 +0100536 transform->ivlen = 12;
537 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200538
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200539 /* Minimum length is expicit IV + tag */
540 transform->minlen = transform->ivlen - transform->fixed_ivlen
541 + ( transform->ciphersuite_info->flags &
542 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100543 }
544 else
545 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200546 int ret;
547
548 /* Initialize HMAC contexts */
549 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
550 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100551 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200552 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
553 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100554 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200556 /* Get MAC length */
557 transform->maclen = md_get_size( md_info );
558
559#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
560 /*
561 * If HMAC is to be truncated, we shall keep the leftmost bytes,
562 * (rfc 6066 page 13 or rfc 2104 section 4),
563 * so we only need to adjust the length here.
564 */
565 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
566 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
567#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
568
569 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100570 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200572 /* Minimum length */
573 if( cipher_info->mode == POLARSSL_MODE_STREAM )
574 transform->minlen = transform->maclen;
575 else
Paul Bakker68884e32013-01-07 18:20:04 +0100576 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200577 /*
578 * GenericBlockCipher:
579 * first multiple of blocklen greater than maclen
580 * + IV except for SSL3 and TLS 1.0
581 */
582 transform->minlen = transform->maclen
583 + cipher_info->block_size
584 - transform->maclen % cipher_info->block_size;
585
586#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
587 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
588 ssl->minor_ver == SSL_MINOR_VERSION_1 )
589 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100590 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200591#endif
592#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
593 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
594 ssl->minor_ver == SSL_MINOR_VERSION_3 )
595 {
596 transform->minlen += transform->ivlen;
597 }
598 else
599#endif
600 {
601 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
602 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
603 }
Paul Bakker68884e32013-01-07 18:20:04 +0100604 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 }
606
607 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000608 transform->keylen, transform->minlen, transform->ivlen,
609 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
611 /*
612 * Finally setup the cipher contexts, IVs and MAC secrets.
613 */
614 if( ssl->endpoint == SSL_IS_CLIENT )
615 {
Paul Bakker48916f92012-09-16 19:57:18 +0000616 key1 = keyblk + transform->maclen * 2;
617 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Paul Bakker68884e32013-01-07 18:20:04 +0100619 mac_enc = keyblk;
620 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000622 /*
623 * This is not used in TLS v1.1.
624 */
Paul Bakker48916f92012-09-16 19:57:18 +0000625 iv_copy_len = ( transform->fixed_ivlen ) ?
626 transform->fixed_ivlen : transform->ivlen;
627 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
628 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000629 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 }
631 else
632 {
Paul Bakker48916f92012-09-16 19:57:18 +0000633 key1 = keyblk + transform->maclen * 2 + transform->keylen;
634 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
Paul Bakker68884e32013-01-07 18:20:04 +0100636 mac_enc = keyblk + transform->maclen;
637 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000639 /*
640 * This is not used in TLS v1.1.
641 */
Paul Bakker48916f92012-09-16 19:57:18 +0000642 iv_copy_len = ( transform->fixed_ivlen ) ?
643 transform->fixed_ivlen : transform->ivlen;
644 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
645 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000646 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 }
648
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200649#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100650 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
651 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100652 if( transform->maclen > sizeof transform->mac_enc )
653 {
654 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200655 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100656 }
657
Paul Bakker68884e32013-01-07 18:20:04 +0100658 memcpy( transform->mac_enc, mac_enc, transform->maclen );
659 memcpy( transform->mac_dec, mac_dec, transform->maclen );
660 }
661 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200662#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200663#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
664 defined(POLARSSL_SSL_PROTO_TLS1_2)
665 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100666 {
667 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
668 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
669 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200670 else
671#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200672 {
673 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200674 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200675 }
Paul Bakker68884e32013-01-07 18:20:04 +0100676
Paul Bakker05ef8352012-05-08 09:17:57 +0000677#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200678 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000679 {
680 int ret = 0;
681
682 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
683
Paul Bakker07eb38b2012-12-19 14:42:06 +0100684 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
685 transform->iv_enc, transform->iv_dec,
686 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100687 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100688 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000689 {
690 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200691 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000692 }
693 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200694#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000695
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200696 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
697 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200699 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_init_ctx( &transform->cipher_ctx_dec,
704 cipher_info ) ) != 0 )
705 {
706 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
707 return( ret );
708 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200709
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200710 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
711 cipher_info->key_length,
712 POLARSSL_ENCRYPT ) ) != 0 )
713 {
714 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
715 return( ret );
716 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200717
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200718 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
719 cipher_info->key_length,
720 POLARSSL_DECRYPT ) ) != 0 )
721 {
722 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
723 return( ret );
724 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200725
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200726#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200727 if( cipher_info->mode == POLARSSL_MODE_CBC )
728 {
729 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
730 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200731 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200732 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
733 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200734 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200735
736 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
737 POLARSSL_PADDING_NONE ) ) != 0 )
738 {
739 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
740 return( ret );
741 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Paul Bakker34617722014-06-13 17:20:13 +0200745 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Paul Bakker2770fbd2012-07-03 13:30:23 +0000747#if defined(POLARSSL_ZLIB_SUPPORT)
748 // Initialize compression
749 //
Paul Bakker48916f92012-09-16 19:57:18 +0000750 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000751 {
Paul Bakker16770332013-10-11 09:59:44 +0200752 if( ssl->compress_buf == NULL )
753 {
754 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
755 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
756 if( ssl->compress_buf == NULL )
757 {
758 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
759 SSL_BUFFER_LEN ) );
760 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
761 }
762 }
763
Paul Bakker2770fbd2012-07-03 13:30:23 +0000764 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
765
Paul Bakker48916f92012-09-16 19:57:18 +0000766 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
767 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000768
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200769 if( deflateInit( &transform->ctx_deflate,
770 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000771 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000772 {
773 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
774 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
775 }
776 }
777#endif /* POLARSSL_ZLIB_SUPPORT */
778
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
780
781 return( 0 );
782}
783
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200784#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000785void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000786{
787 md5_context md5;
788 sha1_context sha1;
789 unsigned char pad_1[48];
790 unsigned char pad_2[48];
791
Paul Bakker380da532012-04-18 16:10:25 +0000792 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Paul Bakker48916f92012-09-16 19:57:18 +0000794 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
795 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000796
Paul Bakker380da532012-04-18 16:10:25 +0000797 memset( pad_1, 0x36, 48 );
798 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000799
Paul Bakker48916f92012-09-16 19:57:18 +0000800 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000801 md5_update( &md5, pad_1, 48 );
802 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000803
Paul Bakker380da532012-04-18 16:10:25 +0000804 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000805 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000806 md5_update( &md5, pad_2, 48 );
807 md5_update( &md5, hash, 16 );
808 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000809
Paul Bakker48916f92012-09-16 19:57:18 +0000810 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000811 sha1_update( &sha1, pad_1, 40 );
812 sha1_finish( &sha1, hash + 16 );
813
814 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000815 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000816 sha1_update( &sha1, pad_2, 40 );
817 sha1_update( &sha1, hash + 16, 20 );
818 sha1_finish( &sha1, hash + 16 );
819
820 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
821 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
822
Paul Bakker5b4af392014-06-26 12:09:34 +0200823 md5_free( &md5 );
824 sha1_free( &sha1 );
825
Paul Bakker380da532012-04-18 16:10:25 +0000826 return;
827}
Paul Bakker9af723c2014-05-01 13:03:14 +0200828#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000829
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200830#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000831void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
832{
833 md5_context md5;
834 sha1_context sha1;
835
836 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
837
Paul Bakker48916f92012-09-16 19:57:18 +0000838 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
839 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000840
Paul Bakker48916f92012-09-16 19:57:18 +0000841 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000842 sha1_finish( &sha1, hash + 16 );
843
844 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
845 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
846
Paul Bakker5b4af392014-06-26 12:09:34 +0200847 md5_free( &md5 );
848 sha1_free( &sha1 );
849
Paul Bakker380da532012-04-18 16:10:25 +0000850 return;
851}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200852#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000853
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200854#if defined(POLARSSL_SSL_PROTO_TLS1_2)
855#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000856void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
857{
Paul Bakker9e36f042013-06-30 14:34:05 +0200858 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000859
860 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
861
Paul Bakker9e36f042013-06-30 14:34:05 +0200862 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
863 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000864
865 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
866 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
867
Paul Bakker5b4af392014-06-26 12:09:34 +0200868 sha256_free( &sha256 );
869
Paul Bakker380da532012-04-18 16:10:25 +0000870 return;
871}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000873
Paul Bakker9e36f042013-06-30 14:34:05 +0200874#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000875void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
876{
Paul Bakker9e36f042013-06-30 14:34:05 +0200877 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000878
879 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
880
Paul Bakker9e36f042013-06-30 14:34:05 +0200881 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
882 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Paul Bakkerca4ab492012-04-18 14:23:57 +0000884 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000885 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
886
Paul Bakker5b4af392014-06-26 12:09:34 +0200887 sha512_free( &sha512 );
888
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 return;
890}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200891#endif /* POLARSSL_SHA512_C */
892#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200894#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
896{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200897 unsigned char *p = ssl->handshake->premaster;
898 unsigned char *end = p + sizeof( ssl->handshake->premaster );
899
900 /*
901 * PMS = struct {
902 * opaque other_secret<0..2^16-1>;
903 * opaque psk<0..2^16-1>;
904 * };
905 * with "other_secret" depending on the particular key exchange
906 */
907#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
909 {
910 if( end - p < 2 + (int) ssl->psk_len )
911 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
912
913 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
914 *(p++) = (unsigned char)( ssl->psk_len );
915 p += ssl->psk_len;
916 }
917 else
918#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200919#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
920 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
921 {
922 /*
923 * other_secret already set by the ClientKeyExchange message,
924 * and is 48 bytes long
925 */
926 *p++ = 0;
927 *p++ = 48;
928 p += 48;
929 }
930 else
931#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
933 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
934 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200935 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200936 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200938 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200940 p + 2, &len,
941 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200942 {
943 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
944 return( ret );
945 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200946 *(p++) = (unsigned char)( len >> 8 );
947 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200948 p += len;
949
950 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
951 }
952 else
953#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
954#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
955 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
956 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200957 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200958 size_t zlen;
959
960 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200961 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962 ssl->f_rng, ssl->p_rng ) ) != 0 )
963 {
964 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
965 return( ret );
966 }
967
968 *(p++) = (unsigned char)( zlen >> 8 );
969 *(p++) = (unsigned char)( zlen );
970 p += zlen;
971
972 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
973 }
974 else
975#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
976 {
977 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200978 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200979 }
980
981 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100982 if( end - p < 2 + (int) ssl->psk_len )
983 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
984
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200985 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
986 *(p++) = (unsigned char)( ssl->psk_len );
987 memcpy( p, ssl->psk, ssl->psk_len );
988 p += ssl->psk_len;
989
990 ssl->handshake->pmslen = p - ssl->handshake->premaster;
991
992 return( 0 );
993}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200994#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200995
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200996#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000997/*
998 * SSLv3.0 MAC functions
999 */
Paul Bakker68884e32013-01-07 18:20:04 +01001000static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1001 unsigned char *buf, size_t len,
1002 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001003{
1004 unsigned char header[11];
1005 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001006 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001007 int md_size = md_get_size( md_ctx->md_info );
1008 int md_type = md_get_type( md_ctx->md_info );
1009
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001010 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001011 if( md_type == POLARSSL_MD_MD5 )
1012 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001013 else
Paul Bakker68884e32013-01-07 18:20:04 +01001014 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
1016 memcpy( header, ctr, 8 );
1017 header[ 8] = (unsigned char) type;
1018 header[ 9] = (unsigned char)( len >> 8 );
1019 header[10] = (unsigned char)( len );
1020
Paul Bakker68884e32013-01-07 18:20:04 +01001021 memset( padding, 0x36, padlen );
1022 md_starts( md_ctx );
1023 md_update( md_ctx, secret, md_size );
1024 md_update( md_ctx, padding, padlen );
1025 md_update( md_ctx, header, 11 );
1026 md_update( md_ctx, buf, len );
1027 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakker68884e32013-01-07 18:20:04 +01001029 memset( padding, 0x5C, padlen );
1030 md_starts( md_ctx );
1031 md_update( md_ctx, secret, md_size );
1032 md_update( md_ctx, padding, padlen );
1033 md_update( md_ctx, buf + len, md_size );
1034 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001035}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001036#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001037
Paul Bakker5121ce52009-01-03 21:22:43 +00001038/*
1039 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001040 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001041static int ssl_encrypt_buf( ssl_context *ssl )
1042{
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001043 const cipher_mode_t mode = cipher_get_cipher_mode(
1044 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
1046 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1047
1048 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001049 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001051#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1052 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1053 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001054 if( mode != POLARSSL_MODE_GCM &&
1055 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001057#if defined(POLARSSL_SSL_PROTO_SSL3)
1058 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1059 {
1060 ssl_mac( &ssl->transform_out->md_ctx_enc,
1061 ssl->transform_out->mac_enc,
1062 ssl->out_msg, ssl->out_msglen,
1063 ssl->out_ctr, ssl->out_msgtype );
1064 }
1065 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001066#endif
1067#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001068 defined(POLARSSL_SSL_PROTO_TLS1_2)
1069 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1070 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001071 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1072 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1073 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001074 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1075 ssl->out_msg, ssl->out_msglen );
1076 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1077 ssl->out_msg + ssl->out_msglen );
1078 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1079 }
1080 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001081#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001082 {
1083 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001084 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001085 }
1086
1087 SSL_DEBUG_BUF( 4, "computed mac",
1088 ssl->out_msg + ssl->out_msglen,
1089 ssl->transform_out->maclen );
1090
1091 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001092 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001093#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001095 /*
1096 * Encrypt
1097 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001098#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001099 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001101 int ret;
1102 size_t olen = 0;
1103
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1105 "including %d bytes of padding",
1106 ssl->out_msglen, 0 ) );
1107
1108 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1109 ssl->out_msg, ssl->out_msglen );
1110
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001111 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001112 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001113 ssl->transform_out->ivlen,
1114 ssl->out_msg, ssl->out_msglen,
1115 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001116 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001117 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001118 return( ret );
1119 }
1120
1121 if( ssl->out_msglen != olen )
1122 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001123 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001124 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001125 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 }
Paul Bakker68884e32013-01-07 18:20:04 +01001127 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001129#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1130 if( mode == POLARSSL_MODE_GCM ||
1131 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001133 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001134 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135 unsigned char *enc_msg;
1136 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001137 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1138 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001139
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140 memcpy( add_data, ssl->out_ctr, 8 );
1141 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001142 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1143 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001144 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1145 add_data[12] = ssl->out_msglen & 0xFF;
1146
1147 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1148 add_data, 13 );
1149
Paul Bakker68884e32013-01-07 18:20:04 +01001150 /*
1151 * Generate IV
1152 */
1153 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001154 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1155 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001156 if( ret != 0 )
1157 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001158
Paul Bakker68884e32013-01-07 18:20:04 +01001159 memcpy( ssl->out_iv,
1160 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1161 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001162
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001163 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001164 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001165
Paul Bakker68884e32013-01-07 18:20:04 +01001166 /*
1167 * Fix pointer positions and message length with added IV
1168 */
1169 enc_msg = ssl->out_msg;
1170 enc_msglen = ssl->out_msglen;
1171 ssl->out_msglen += ssl->transform_out->ivlen -
1172 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001173
Paul Bakker68884e32013-01-07 18:20:04 +01001174 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1175 "including %d bytes of padding",
1176 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001177
Paul Bakker68884e32013-01-07 18:20:04 +01001178 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1179 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001180
Paul Bakker68884e32013-01-07 18:20:04 +01001181 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001182 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001183 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001184 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1185 ssl->transform_out->iv_enc,
1186 ssl->transform_out->ivlen,
1187 add_data, 13,
1188 enc_msg, enc_msglen,
1189 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001190 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001191 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001192 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001193 return( ret );
1194 }
1195
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001196 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001197 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001198 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001199 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001200 }
1201
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001202 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001203
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001204 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001205 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001207#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001208#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1209 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001210 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001212 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001214 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001215
Paul Bakker48916f92012-09-16 19:57:18 +00001216 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1217 ssl->transform_out->ivlen;
1218 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 padlen = 0;
1220
1221 for( i = 0; i <= padlen; i++ )
1222 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1223
1224 ssl->out_msglen += padlen + 1;
1225
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226 enc_msglen = ssl->out_msglen;
1227 enc_msg = ssl->out_msg;
1228
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001229#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001231 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1232 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001234 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235 {
1236 /*
1237 * Generate IV
1238 */
Paul Bakker48916f92012-09-16 19:57:18 +00001239 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1240 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001241 if( ret != 0 )
1242 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243
Paul Bakker92be97b2013-01-02 17:30:03 +01001244 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001245 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001246
1247 /*
1248 * Fix pointer positions and message length with added IV
1249 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001250 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001251 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001252 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001253 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001254#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001255
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001257 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001258 ssl->out_msglen, ssl->transform_out->ivlen,
1259 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
1261 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001262 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001264 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001265 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001266 ssl->transform_out->ivlen,
1267 enc_msg, enc_msglen,
1268 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001269 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001270 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001271 return( ret );
1272 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001273
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 if( enc_msglen != olen )
1275 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001276 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001277 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001279
1280#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1282 {
1283 /*
1284 * Save IV in SSL3 and TLS1
1285 */
1286 memcpy( ssl->transform_out->iv_enc,
1287 ssl->transform_out->cipher_ctx_enc.iv,
1288 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001292 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001293#endif /* POLARSSL_CIPHER_MODE_CBC &&
1294 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001295 {
1296 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001297 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
1300 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1301
1302 return( 0 );
1303}
1304
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001305#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001306
Paul Bakker5121ce52009-01-03 21:22:43 +00001307static int ssl_decrypt_buf( ssl_context *ssl )
1308{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001309 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001310 const cipher_mode_t mode = cipher_get_cipher_mode(
1311 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001312#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1313 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1314 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1315 size_t padlen = 0, correct = 1;
1316#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
1318 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1319
Paul Bakker48916f92012-09-16 19:57:18 +00001320 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 {
1322 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001323 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001324 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
1326
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001327#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001328 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001329 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001330 int ret;
1331 size_t olen = 0;
1332
Paul Bakker68884e32013-01-07 18:20:04 +01001333 padlen = 0;
1334
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001335 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001336 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001337 ssl->transform_in->ivlen,
1338 ssl->in_msg, ssl->in_msglen,
1339 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001340 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001341 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001342 return( ret );
1343 }
1344
1345 if( ssl->in_msglen != olen )
1346 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001347 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001348 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
Paul Bakker68884e32013-01-07 18:20:04 +01001351 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001352#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001353#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1354 if( mode == POLARSSL_MODE_GCM ||
1355 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001356 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001357 int ret;
1358 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001359 unsigned char *dec_msg;
1360 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001361 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001362 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1363 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001364 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1365 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001366
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001367 if( ssl->in_msglen < explicit_iv_len + taglen )
1368 {
1369 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1370 "+ taglen (%d)", ssl->in_msglen,
1371 explicit_iv_len, taglen ) );
1372 return( POLARSSL_ERR_SSL_INVALID_MAC );
1373 }
1374 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1375
Paul Bakker68884e32013-01-07 18:20:04 +01001376 dec_msg = ssl->in_msg;
1377 dec_msg_result = ssl->in_msg;
1378 ssl->in_msglen = dec_msglen;
1379
1380 memcpy( add_data, ssl->in_ctr, 8 );
1381 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001382 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1383 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001384 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1385 add_data[12] = ssl->in_msglen & 0xFF;
1386
1387 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1388 add_data, 13 );
1389
1390 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1391 ssl->in_iv,
1392 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1393
1394 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1395 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001396 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001397
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001398 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001399 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001400 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001401 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1402 ssl->transform_in->iv_dec,
1403 ssl->transform_in->ivlen,
1404 add_data, 13,
1405 dec_msg, dec_msglen,
1406 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001407 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001408 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001409 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1410
1411 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1412 return( POLARSSL_ERR_SSL_INVALID_MAC );
1413
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001414 return( ret );
1415 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001416
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001417 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001418 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001419 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001420 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001421 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001424#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001425#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1426 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001427 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 {
Paul Bakker45829992013-01-03 14:52:21 +01001429 /*
1430 * Decrypt and check the padding
1431 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001432 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001433 unsigned char *dec_msg;
1434 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001435 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001436 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001437 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001438
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 /*
Paul Bakker45829992013-01-03 14:52:21 +01001440 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 */
Paul Bakker48916f92012-09-16 19:57:18 +00001442 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 {
1444 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001445 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001446 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
1448
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001449#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001450 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1451 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001452#endif
Paul Bakker45829992013-01-03 14:52:21 +01001453
1454 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1455 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1456 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001457 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1458 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1459 ssl->transform_in->ivlen,
1460 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001461 return( POLARSSL_ERR_SSL_INVALID_MAC );
1462 }
1463
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001464 dec_msglen = ssl->in_msglen;
1465 dec_msg = ssl->in_msg;
1466 dec_msg_result = ssl->in_msg;
1467
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001468#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001469 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001470 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001471 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001472 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001473 {
Paul Bakker48916f92012-09-16 19:57:18 +00001474 dec_msglen -= ssl->transform_in->ivlen;
1475 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001476
Paul Bakker48916f92012-09-16 19:57:18 +00001477 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001478 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001479 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001480#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001481
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001482 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001483 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001484 ssl->transform_in->ivlen,
1485 dec_msg, dec_msglen,
1486 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001487 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001488 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001489 return( ret );
1490 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001491
Paul Bakkercca5b812013-08-31 17:40:26 +02001492 if( dec_msglen != olen )
1493 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001494 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001495 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001496 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001497
1498#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001499 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1500 {
1501 /*
1502 * Save IV in SSL3 and TLS1
1503 */
1504 memcpy( ssl->transform_in->iv_dec,
1505 ssl->transform_in->cipher_ctx_dec.iv,
1506 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001508#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
1510 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001511
1512 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1513 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001514#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001515 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1516 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001517#endif
Paul Bakker45829992013-01-03 14:52:21 +01001518 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001519 correct = 0;
1520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001521
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001522#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1524 {
Paul Bakker48916f92012-09-16 19:57:18 +00001525 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001527#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1529 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001530 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001531#endif
Paul Bakker45829992013-01-03 14:52:21 +01001532 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 }
1534 }
1535 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001536#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001537#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1538 defined(POLARSSL_SSL_PROTO_TLS1_2)
1539 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 {
1541 /*
Paul Bakker45829992013-01-03 14:52:21 +01001542 * TLSv1+: always check the padding up to the first failure
1543 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001545 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001546 size_t padding_idx = ssl->in_msglen - padlen - 1;
1547
Paul Bakker956c9e02013-12-19 14:42:28 +01001548 /*
1549 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001550 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001551 *
Paul Bakker61885c72014-04-25 12:59:03 +02001552 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1553 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001554 *
1555 * In both cases we reset padding_idx to a safe value (0) to
1556 * prevent out-of-buffer reads.
1557 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001558 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001559 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1560 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001561
1562 padding_idx *= correct;
1563
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001564 for( i = 1; i <= 256; i++ )
1565 {
1566 real_count &= ( i <= padlen );
1567 pad_count += real_count *
1568 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1569 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001570
1571 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001572
Paul Bakkerd66f0702013-01-31 16:57:45 +01001573#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001574 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001575 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001576#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001577 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001579 else
1580#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1581 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001582 {
1583 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001584 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001587 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001588#endif /* POLARSSL_CIPHER_MODE_CBC &&
1589 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001590 {
1591 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001592 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
1595 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1596 ssl->in_msg, ssl->in_msglen );
1597
1598 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001599 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001601#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1602 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1603 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001604 if( mode != POLARSSL_MODE_GCM &&
1605 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001606 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001607 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1608
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001609 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001611 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1612 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001614 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001616#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001617 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1618 {
1619 ssl_mac( &ssl->transform_in->md_ctx_dec,
1620 ssl->transform_in->mac_dec,
1621 ssl->in_msg, ssl->in_msglen,
1622 ssl->in_ctr, ssl->in_msgtype );
1623 }
1624 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001625#endif /* POLARSSL_SSL_PROTO_SSL3 */
1626#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001627 defined(POLARSSL_SSL_PROTO_TLS1_2)
1628 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1629 {
1630 /*
1631 * Process MAC and always update for padlen afterwards to make
1632 * total time independent of padlen
1633 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001634 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001635 *
1636 * Known timing attacks:
1637 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1638 *
1639 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1640 * correctly. (We round down instead of up, so -56 is the correct
1641 * value for our calculations instead of -55)
1642 */
1643 size_t j, extra_run = 0;
1644 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1645 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001646
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001647 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001648
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001649 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1650 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1651 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001652 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1653 ssl->in_msglen );
1654 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1655 ssl->in_msg + ssl->in_msglen );
1656 for( j = 0; j < extra_run; j++ )
1657 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001658
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001659 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1660 }
1661 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001662#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663 POLARSSL_SSL_PROTO_TLS1_2 */
1664 {
1665 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001666 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1670 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1671 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001673 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001674 ssl->transform_in->maclen ) != 0 )
1675 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001676#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001677 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001678#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 correct = 0;
1680 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 /*
1683 * Finally check the correct flag
1684 */
1685 if( correct == 0 )
1686 return( POLARSSL_ERR_SSL_INVALID_MAC );
1687 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001688#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
1690 if( ssl->in_msglen == 0 )
1691 {
1692 ssl->nb_zero++;
1693
1694 /*
1695 * Three or more empty messages may be a DoS attack
1696 * (excessive CPU consumption).
1697 */
1698 if( ssl->nb_zero > 3 )
1699 {
1700 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1701 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001702 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 }
1704 }
1705 else
1706 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001707
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001708 /* Input counter not used with DTLS right now,
1709 * but it doesn't hurt to have this part ready */
1710 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1711 if( ++ssl->in_ctr[i - 1] != 0 )
1712 break;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001713
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001714 /* The loop goes to its end iff the counter is wrapping */
1715 if( i == ssl_ep_len( ssl ) )
1716 {
1717 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1718 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001719 }
1720
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1722
1723 return( 0 );
1724}
1725
Paul Bakker2770fbd2012-07-03 13:30:23 +00001726#if defined(POLARSSL_ZLIB_SUPPORT)
1727/*
1728 * Compression/decompression functions
1729 */
1730static int ssl_compress_buf( ssl_context *ssl )
1731{
1732 int ret;
1733 unsigned char *msg_post = ssl->out_msg;
1734 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001735 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001736
1737 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1738
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001739 if( len_pre == 0 )
1740 return( 0 );
1741
Paul Bakker2770fbd2012-07-03 13:30:23 +00001742 memcpy( msg_pre, ssl->out_msg, len_pre );
1743
1744 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1745 ssl->out_msglen ) );
1746
1747 SSL_DEBUG_BUF( 4, "before compression: output payload",
1748 ssl->out_msg, ssl->out_msglen );
1749
Paul Bakker48916f92012-09-16 19:57:18 +00001750 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1751 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1752 ssl->transform_out->ctx_deflate.next_out = msg_post;
1753 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001754
Paul Bakker48916f92012-09-16 19:57:18 +00001755 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001756 if( ret != Z_OK )
1757 {
1758 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1759 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1760 }
1761
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001762 ssl->out_msglen = SSL_BUFFER_LEN -
1763 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764
Paul Bakker2770fbd2012-07-03 13:30:23 +00001765 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1766 ssl->out_msglen ) );
1767
1768 SSL_DEBUG_BUF( 4, "after compression: output payload",
1769 ssl->out_msg, ssl->out_msglen );
1770
1771 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1772
1773 return( 0 );
1774}
1775
1776static int ssl_decompress_buf( ssl_context *ssl )
1777{
1778 int ret;
1779 unsigned char *msg_post = ssl->in_msg;
1780 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001781 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001782
1783 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1784
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001785 if( len_pre == 0 )
1786 return( 0 );
1787
Paul Bakker2770fbd2012-07-03 13:30:23 +00001788 memcpy( msg_pre, ssl->in_msg, len_pre );
1789
1790 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1791 ssl->in_msglen ) );
1792
1793 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1794 ssl->in_msg, ssl->in_msglen );
1795
Paul Bakker48916f92012-09-16 19:57:18 +00001796 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1797 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1798 ssl->transform_in->ctx_inflate.next_out = msg_post;
1799 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001800
Paul Bakker48916f92012-09-16 19:57:18 +00001801 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001802 if( ret != Z_OK )
1803 {
1804 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1805 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1806 }
1807
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001808 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1809 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001810
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1812 ssl->in_msglen ) );
1813
1814 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1815 ssl->in_msg, ssl->in_msglen );
1816
1817 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1818
1819 return( 0 );
1820}
1821#endif /* POLARSSL_ZLIB_SUPPORT */
1822
Paul Bakker5121ce52009-01-03 21:22:43 +00001823/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001824 * Fill the input message buffer by appending data to it.
1825 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001826 *
1827 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1828 * available (from this read and/or a previous one). Otherwise, an error code
1829 * is returned (possibly EOF or WANT_READ).
1830 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001831 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1832 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1833 * since we always read a whole datagram at once.
1834 *
1835 * For DTLS, It is up to the caller to set ssl->next_record_offset when
1836 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 */
Paul Bakker23986e52011-04-24 08:57:21 +00001838int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001839{
Paul Bakker23986e52011-04-24 08:57:21 +00001840 int ret;
1841 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
1843 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1844
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001845 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001846 {
1847 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1848 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1849 }
1850
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001851#if defined(POLARSSL_SSL_PROTO_DTLS)
1852 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001854 /*
1855 * The point is, we need to always read a full datagram at once, so we
1856 * sometimes read more then requested, and handle the additional data.
1857 * It could be the rest of the current record (while fetching the
1858 * header) and/or some other records in the same datagram.
1859 */
1860
1861 /*
1862 * Move to the next record in the already read datagram if applicable
1863 */
1864 if( ssl->next_record_offset != 0 )
1865 {
1866 if( ssl->in_left < ssl->next_record_offset )
1867 {
1868 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1869 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1870 }
1871
1872 ssl->in_left -= ssl->next_record_offset;
1873
1874 if( ssl->in_left != 0 )
1875 {
1876 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1877 ssl->next_record_offset ) );
1878 memmove( ssl->in_hdr,
1879 ssl->in_hdr + ssl->next_record_offset,
1880 ssl->in_left );
1881 }
1882
1883 ssl->next_record_offset = 0;
1884 }
1885
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1887 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001888
1889 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001890 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001891 */
1892 if( nb_want <= ssl->in_left)
1893 return( 0 );
1894
1895 /*
1896 * A record can't be split accross datagrams. If we need to read but
1897 * are not at the beginning of a new record, the caller did something
1898 * wrong.
1899 */
1900 if( ssl->in_left != 0 )
1901 {
1902 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1903 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1904 }
1905
1906 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
1907 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr, len );
1908
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1910
Paul Bakker831a7552011-05-18 13:32:51 +00001911 if( ret == 0 )
1912 return( POLARSSL_ERR_SSL_CONN_EOF );
1913
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 if( ret < 0 )
1915 return( ret );
1916
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001917 ssl->in_left = ret;
1918 }
1919 else
1920#endif
1921 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001922 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1923 ssl->in_left, nb_want ) );
1924
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001925 while( ssl->in_left < nb_want )
1926 {
1927 len = nb_want - ssl->in_left;
1928 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1929
1930 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1931 ssl->in_left, nb_want ) );
1932 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1933
1934 if( ret == 0 )
1935 return( POLARSSL_ERR_SSL_CONN_EOF );
1936
1937 if( ret < 0 )
1938 return( ret );
1939
1940 ssl->in_left += ret;
1941 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 }
1943
1944 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1945
1946 return( 0 );
1947}
1948
1949/*
1950 * Flush any data not yet written
1951 */
1952int ssl_flush_output( ssl_context *ssl )
1953{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001954 int ret;
1955 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001956
1957 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1958
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001959 /* Avoid incrementing counter if data is flushed */
1960 if( ssl->out_left == 0 )
1961 {
1962 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1963 return( 0 );
1964 }
1965
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 while( ssl->out_left > 0 )
1967 {
1968 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001969 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001971 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
1972 ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001974
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1976
1977 if( ret <= 0 )
1978 return( ret );
1979
1980 ssl->out_left -= ret;
1981 }
1982
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001983 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001984 if( ++ssl->out_ctr[i - 1] != 0 )
1985 break;
1986
1987 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001988 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001989 {
1990 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1991 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1992 }
1993
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1995
1996 return( 0 );
1997}
1998
1999/*
2000 * Record layer functions
2001 */
2002int ssl_write_record( ssl_context *ssl )
2003{
Paul Bakker05ef8352012-05-08 09:17:57 +00002004 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002005 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
2007 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2008
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2010 {
2011 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2012 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2013 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2014
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002015 /*
2016 * DTLS has additional fields in the Handshake layer,
2017 * between the length field and the actual payload:
2018 * uint16 message_seq;
2019 * uint24 fragment_offset;
2020 * uint24 fragment_length;
2021 */
2022#if defined(POLARSSL_SSL_PROTO_DTLS)
2023 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2024 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002025 /* Make room for the additional DTLS fields */
2026 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002027 ssl->out_msglen += 8;
2028 len += 8;
2029
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002030 /* Write message_seq and update it */
2031 ssl->out_msg[4] = ( ssl->handshake->msg_seq >> 8 ) & 0xFF;
2032 ssl->out_msg[5] = ( ssl->handshake->msg_seq ) & 0xFF;
2033 ++( ssl->handshake->msg_seq );
2034
2035 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2036 memset( ssl->out_msg + 6, 0x00, 3 );
2037 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002038 }
2039#endif /* POLARSSL_SSL_PROTO_DTLS */
2040
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002041 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2042 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 }
2044
Paul Bakker2770fbd2012-07-03 13:30:23 +00002045#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002046 if( ssl->transform_out != NULL &&
2047 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002048 {
2049 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2050 {
2051 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2052 return( ret );
2053 }
2054
2055 len = ssl->out_msglen;
2056 }
2057#endif /*POLARSSL_ZLIB_SUPPORT */
2058
Paul Bakker05ef8352012-05-08 09:17:57 +00002059#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002060 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002062 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002063
Paul Bakker05ef8352012-05-08 09:17:57 +00002064 ret = ssl_hw_record_write( ssl );
2065 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2066 {
2067 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002068 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002069 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002070
2071 if( ret == 0 )
2072 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002073 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002074#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002075 if( !done )
2076 {
2077 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002078 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2079 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002080
2081 ssl->out_len[0] = (unsigned char)( len >> 8 );
2082 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002083
Paul Bakker48916f92012-09-16 19:57:18 +00002084 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002085 {
2086 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2087 {
2088 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2089 return( ret );
2090 }
2091
2092 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002093 ssl->out_len[0] = (unsigned char)( len >> 8 );
2094 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002095 }
2096
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002097 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002098
2099 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2100 "version = [%d:%d], msglen = %d",
2101 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002102 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002103
2104 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002105 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 }
2107
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2109 {
2110 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2111 return( ret );
2112 }
2113
2114 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2115
2116 return( 0 );
2117}
2118
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002119static int ssl_prepare_handshake_record( ssl_context *ssl )
2120{
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002121 ssl->in_hslen = ssl->transport == SSL_TRANSPORT_DATAGRAM ? 12 : 4;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002122 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2123
2124 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2125 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002126 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002127
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002128 /* We don't handle handshake messages larger than one record (for now) */
2129 if( ssl->in_msg[1] != 0 ||
2130 ssl->in_msglen < ssl->in_hslen )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002131 {
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002132 SSL_DEBUG_MSG( 1, ( "handshake fragmentation not supported" ) );
2133 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002134 }
2135
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002136 if( ssl->state != SSL_HANDSHAKE_OVER )
2137 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2138
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002139 /*
2140 * For DTLS, we move data so that is looks like
2141 * TLS handshake format to other functions.
2142 */
2143#if defined(POLARSSL_SSL_PROTO_DTLS)
2144 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2145 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002146 // TODO: DTLS: check message_seq
2147
2148 /* For now we don't support fragmentation, so make sure
2149 * fragment_offset == 0 and fragment_length == length */
2150 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
2151 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
2152 {
2153 SSL_DEBUG_MSG( 1, ( "handshake fragmentation not supported" ) );
2154 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2155 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002156
2157 memmove( ssl->in_msg + 4, ssl->in_msg + 12, ssl->in_hslen - 12 );
2158 ssl->in_hslen -= 8;
2159 }
2160#endif /* POLARSSL_SSL_PROTO_DTLS */
2161
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002162 return( 0 );
2163}
2164
Paul Bakker5121ce52009-01-03 21:22:43 +00002165int ssl_read_record( ssl_context *ssl )
2166{
Paul Bakker05ef8352012-05-08 09:17:57 +00002167 int ret, done = 0;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002168 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
2170 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2171
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002172 /*
2173 * With DTLS, we cheated on in_hslen to make the handshake message look
2174 * like TLS format, restore the truth now
2175 */
2176#if defined(POLARSSL_SSL_PROTO_DTLS)
2177 if( ssl->in_hslen != 0 && ssl->transport == SSL_TRANSPORT_DATAGRAM )
2178 ssl->in_hslen += 8;
2179#endif
2180
2181 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 {
2183 /*
2184 * Get next Handshake message in the current record
2185 */
2186 ssl->in_msglen -= ssl->in_hslen;
2187
Paul Bakker8934a982011-08-05 11:11:53 +00002188 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002189 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002191 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
2192 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002193
2194 return( 0 );
2195 }
2196
2197 ssl->in_hslen = 0;
2198
2199 /*
2200 * Read the record header and validate it
2201 */
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002202 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 {
2204 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2205 return( ret );
2206 }
2207
2208 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002209 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
2211 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2212 "version = [%d:%d], msglen = %d",
2213 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002214 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002216 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
2217
2218 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 {
2220 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002221 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 }
2223
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002224 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 {
2226 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002227 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002228 }
2229
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002230 /* Sanity check (outer boundaries) */
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002231 if( ssl->in_msglen < 1 ||
2232 ssl->in_msglen > SSL_BUFFER_LEN - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002233 {
2234 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2235 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2236 }
2237
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002239 * Make sure the message length is acceptable for the current transform
2240 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 */
Paul Bakker48916f92012-09-16 19:57:18 +00002242 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002244 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 {
2246 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002247 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 }
2249 }
2250 else
2251 {
Paul Bakker48916f92012-09-16 19:57:18 +00002252 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
2254 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002255 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 }
2257
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002258#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002260 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 {
2262 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002263 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002265#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002266
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002267#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2268 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 /*
2270 * TLS encrypted messages can have up to 256 bytes of padding
2271 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002272 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002273 ssl->in_msglen > ssl->transform_in->minlen +
2274 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 {
2276 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002277 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002279#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 }
2281
2282 /*
2283 * Read and optionally decrypt the message contents
2284 */
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002285 if( ( ret = ssl_fetch_input( ssl,
2286 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 {
2288 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2289 return( ret );
2290 }
2291
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002292#if defined(POLARSSL_SSL_PROTO_DTLS)
2293 /* Done reading this record, get ready for the next one */
2294 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2295 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
2296#endif
2297
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002299 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
Paul Bakker05ef8352012-05-08 09:17:57 +00002301#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002302 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002303 {
2304 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2305
2306 ret = ssl_hw_record_read( ssl );
2307 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2308 {
2309 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002310 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002311 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002312
2313 if( ret == 0 )
2314 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002315 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002316#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002317 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 {
2319 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2320 {
Paul Bakker40865c82013-01-31 17:13:13 +01002321#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2322 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2323 {
2324 ssl_send_alert_message( ssl,
2325 SSL_ALERT_LEVEL_FATAL,
2326 SSL_ALERT_MSG_BAD_RECORD_MAC );
2327 }
2328#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2330 return( ret );
2331 }
2332
2333 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2334 ssl->in_msg, ssl->in_msglen );
2335
2336 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2337 {
2338 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002339 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 }
2341 }
2342
Paul Bakker2770fbd2012-07-03 13:30:23 +00002343#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002344 if( ssl->transform_in != NULL &&
2345 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346 {
2347 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2348 {
2349 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2350 return( ret );
2351 }
2352
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002353 // TODO: what's the purpose of these lines? is in_len used?
2354 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2355 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356 }
2357#endif /* POLARSSL_ZLIB_SUPPORT */
2358
Paul Bakker0a925182012-04-16 06:46:41 +00002359 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2360 ssl->in_msgtype != SSL_MSG_ALERT &&
2361 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2362 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2363 {
2364 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2365
Paul Bakker48916f92012-09-16 19:57:18 +00002366 if( ( ret = ssl_send_alert_message( ssl,
2367 SSL_ALERT_LEVEL_FATAL,
2368 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002369 {
2370 return( ret );
2371 }
2372
2373 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2374 }
2375
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2377 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002378 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
2379 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 }
2381
2382 if( ssl->in_msgtype == SSL_MSG_ALERT )
2383 {
2384 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2385 ssl->in_msg[0], ssl->in_msg[1] ) );
2386
2387 /*
2388 * Ignore non-fatal alerts, except close_notify
2389 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002390 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002392 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2393 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 }
2396
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002397 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2398 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 {
2400 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002401 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 }
2403 }
2404
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002405 /* With DTLS there might be other records in the same datagram */
2406#if defined(POLARSSL_SSL_PROTO_DTLS)
2407 if( ssl->transport != SSL_TRANSPORT_DATAGRAM )
2408#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 ssl->in_left = 0;
2410
2411 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2412
2413 return( 0 );
2414}
2415
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002416int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2417{
2418 int ret;
2419
2420 if( ( ret = ssl_send_alert_message( ssl,
2421 SSL_ALERT_LEVEL_FATAL,
2422 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2423 {
2424 return( ret );
2425 }
2426
2427 return( 0 );
2428}
2429
Paul Bakker0a925182012-04-16 06:46:41 +00002430int ssl_send_alert_message( ssl_context *ssl,
2431 unsigned char level,
2432 unsigned char message )
2433{
2434 int ret;
2435
2436 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2437
2438 ssl->out_msgtype = SSL_MSG_ALERT;
2439 ssl->out_msglen = 2;
2440 ssl->out_msg[0] = level;
2441 ssl->out_msg[1] = message;
2442
2443 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2444 {
2445 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2446 return( ret );
2447 }
2448
2449 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2450
2451 return( 0 );
2452}
2453
Paul Bakker5121ce52009-01-03 21:22:43 +00002454/*
2455 * Handshake functions
2456 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002457#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2458 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2459 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2460 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2461 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2462 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2463 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002464int ssl_write_certificate( ssl_context *ssl )
2465{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002466 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
2468 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2469
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002470 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002471 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2472 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002473 {
2474 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2475 ssl->state++;
2476 return( 0 );
2477 }
2478
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002479 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2480 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002481}
2482
2483int ssl_parse_certificate( ssl_context *ssl )
2484{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002485 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2486
2487 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2488
2489 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002490 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002492 {
2493 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2494 ssl->state++;
2495 return( 0 );
2496 }
2497
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002498 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2499 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002500}
2501#else
2502int ssl_write_certificate( ssl_context *ssl )
2503{
2504 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2505 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002506 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002507 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2508
2509 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2510
2511 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002512 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2513 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002514 {
2515 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2516 ssl->state++;
2517 return( 0 );
2518 }
2519
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 if( ssl->endpoint == SSL_IS_CLIENT )
2521 {
2522 if( ssl->client_auth == 0 )
2523 {
2524 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2525 ssl->state++;
2526 return( 0 );
2527 }
2528
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002529#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 /*
2531 * If using SSLv3 and got no cert, send an Alert message
2532 * (otherwise an empty Certificate message will be sent).
2533 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002534 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2536 {
2537 ssl->out_msglen = 2;
2538 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002539 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2540 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
2542 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2543 goto write_msg;
2544 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002545#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 }
2547 else /* SSL_IS_SERVER */
2548 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002549 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 {
2551 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002552 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 }
2554 }
2555
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002556 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
2558 /*
2559 * 0 . 0 handshake type
2560 * 1 . 3 handshake length
2561 * 4 . 6 length of all certs
2562 * 7 . 9 length of cert. 1
2563 * 10 . n-1 peer certificate
2564 * n . n+2 length of cert. 2
2565 * n+3 . ... upper level cert, etc.
2566 */
2567 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002568 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Paul Bakker29087132010-03-21 21:03:34 +00002570 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 {
2572 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002573 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 {
2575 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2576 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002577 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 }
2579
2580 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2581 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2582 ssl->out_msg[i + 2] = (unsigned char)( n );
2583
2584 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2585 i += n; crt = crt->next;
2586 }
2587
2588 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2589 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2590 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2591
2592 ssl->out_msglen = i;
2593 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2594 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2595
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002596#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002597write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002598#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
2600 ssl->state++;
2601
2602 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2603 {
2604 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2605 return( ret );
2606 }
2607
2608 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2609
Paul Bakkered27a042013-04-18 22:46:23 +02002610 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002611}
2612
2613int ssl_parse_certificate( ssl_context *ssl )
2614{
Paul Bakkered27a042013-04-18 22:46:23 +02002615 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002616 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002617 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
2619 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2620
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002621 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002622 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2623 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002624 {
2625 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2626 ssl->state++;
2627 return( 0 );
2628 }
2629
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002631 ( ssl->authmode == SSL_VERIFY_NONE ||
2632 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002634 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2636 ssl->state++;
2637 return( 0 );
2638 }
2639
2640 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2641 {
2642 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2643 return( ret );
2644 }
2645
2646 ssl->state++;
2647
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002648#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 /*
2650 * Check if the client sent an empty certificate
2651 */
2652 if( ssl->endpoint == SSL_IS_SERVER &&
2653 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2654 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002655 if( ssl->in_msglen == 2 &&
2656 ssl->in_msgtype == SSL_MSG_ALERT &&
2657 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2658 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 {
2660 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2661
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002662 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2664 return( 0 );
2665 else
Paul Bakker40e46942009-01-03 21:51:57 +00002666 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 }
2668 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002669#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002671#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2672 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 if( ssl->endpoint == SSL_IS_SERVER &&
2674 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2675 {
2676 if( ssl->in_hslen == 7 &&
2677 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2678 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2679 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2680 {
2681 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2682
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002683 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002685 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 else
2687 return( 0 );
2688 }
2689 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002690#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2691 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
2693 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2694 {
2695 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002696 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 }
2698
2699 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2700 {
2701 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002702 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 }
2704
2705 /*
2706 * Same message structure as in ssl_write_certificate()
2707 */
2708 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2709
2710 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2711 {
2712 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002713 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 }
2715
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002716 /* In case we tried to reuse a session but it failed */
2717 if( ssl->session_negotiate->peer_cert != NULL )
2718 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002719 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002720 polarssl_free( ssl->session_negotiate->peer_cert );
2721 }
2722
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002723 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2724 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 {
2726 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002727 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002728 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 }
2730
Paul Bakkerb6b09562013-09-18 14:17:41 +02002731 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
2733 i = 7;
2734
2735 while( i < ssl->in_hslen )
2736 {
2737 if( ssl->in_msg[i] != 0 )
2738 {
2739 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002740 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 }
2742
2743 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2744 | (unsigned int) ssl->in_msg[i + 2];
2745 i += 3;
2746
2747 if( n < 128 || i + n > ssl->in_hslen )
2748 {
2749 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002750 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 }
2752
Paul Bakkerddf26b42013-09-18 13:46:23 +02002753 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2754 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 if( ret != 0 )
2756 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002757 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 return( ret );
2759 }
2760
2761 i += n;
2762 }
2763
Paul Bakker48916f92012-09-16 19:57:18 +00002764 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002766 /*
2767 * On client, make sure the server cert doesn't change during renego to
2768 * avoid "triple handshake" attack: https://secure-resumption.com/
2769 */
2770 if( ssl->endpoint == SSL_IS_CLIENT &&
2771 ssl->renegotiation == SSL_RENEGOTIATION )
2772 {
2773 if( ssl->session->peer_cert == NULL )
2774 {
2775 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2776 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2777 }
2778
2779 if( ssl->session->peer_cert->raw.len !=
2780 ssl->session_negotiate->peer_cert->raw.len ||
2781 memcmp( ssl->session->peer_cert->raw.p,
2782 ssl->session_negotiate->peer_cert->raw.p,
2783 ssl->session->peer_cert->raw.len ) != 0 )
2784 {
2785 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2786 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2787 }
2788 }
2789
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 if( ssl->authmode != SSL_VERIFY_NONE )
2791 {
2792 if( ssl->ca_chain == NULL )
2793 {
2794 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002795 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 }
2797
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002798 /*
2799 * Main check: verify certificate
2800 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002801 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2802 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2803 &ssl->session_negotiate->verify_result,
2804 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
2806 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002807 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002809 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002810
2811 /*
2812 * Secondary checks: always done, but change 'ret' only if it was 0
2813 */
2814
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002815#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002816 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002817 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002818
2819 /* If certificate uses an EC key, make sure the curve is OK */
2820 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2821 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2822 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002823 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002824 if( ret == 0 )
2825 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002826 }
2827 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002828#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002830 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2831 ciphersuite_info,
2832 ! ssl->endpoint ) != 0 )
2833 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002834 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002835 if( ret == 0 )
2836 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2837 }
2838
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2840 ret = 0;
2841 }
2842
2843 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2844
2845 return( ret );
2846}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002847#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2848 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2849 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2850 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2851 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2852 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2853 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
2855int ssl_write_change_cipher_spec( ssl_context *ssl )
2856{
2857 int ret;
2858
2859 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2860
2861 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2862 ssl->out_msglen = 1;
2863 ssl->out_msg[0] = 1;
2864
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 ssl->state++;
2866
2867 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2868 {
2869 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2870 return( ret );
2871 }
2872
2873 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2874
2875 return( 0 );
2876}
2877
2878int ssl_parse_change_cipher_spec( ssl_context *ssl )
2879{
2880 int ret;
2881
2882 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2883
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2885 {
2886 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2887 return( ret );
2888 }
2889
2890 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2891 {
2892 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002893 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 }
2895
2896 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2897 {
2898 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002899 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 }
2901
2902 ssl->state++;
2903
2904 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2905
2906 return( 0 );
2907}
2908
Paul Bakker41c83d32013-03-20 14:39:14 +01002909void ssl_optimize_checksum( ssl_context *ssl,
2910 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002911{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002912 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002913
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002914#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2915 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002916 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002917 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002918 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002919#endif
2920#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2921#if defined(POLARSSL_SHA512_C)
2922 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2923 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2924 else
2925#endif
2926#if defined(POLARSSL_SHA256_C)
2927 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002928 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002929 else
2930#endif
2931#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002932 {
2933 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002934 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002935 }
Paul Bakker380da532012-04-18 16:10:25 +00002936}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002937
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02002938void ssl_reset_checksum( ssl_context *ssl )
2939{
2940#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2941 defined(POLARSSL_SSL_PROTO_TLS1_1)
2942 md5_starts( &ssl->handshake->fin_md5 );
2943 sha1_starts( &ssl->handshake->fin_sha1 );
2944#endif
2945#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2946#if defined(POLARSSL_SHA256_C)
2947 sha256_starts( &ssl->handshake->fin_sha256, 0 );
2948#endif
2949#if defined(POLARSSL_SHA512_C)
2950 sha512_starts( &ssl->handshake->fin_sha512, 1 );
2951#endif
2952#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2953}
2954
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002955static void ssl_update_checksum_start( ssl_context *ssl,
2956 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002957{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002958#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2959 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002960 md5_update( &ssl->handshake->fin_md5 , buf, len );
2961 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002962#endif
2963#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2964#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002965 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002966#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002967#if defined(POLARSSL_SHA512_C)
2968 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002969#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002970#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002971}
2972
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002973#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2974 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002975static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2976 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002977{
Paul Bakker48916f92012-09-16 19:57:18 +00002978 md5_update( &ssl->handshake->fin_md5 , buf, len );
2979 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002980}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002981#endif
Paul Bakker380da532012-04-18 16:10:25 +00002982
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002983#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2984#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002985static void ssl_update_checksum_sha256( ssl_context *ssl,
2986 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002987{
Paul Bakker9e36f042013-06-30 14:34:05 +02002988 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002989}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002990#endif
Paul Bakker380da532012-04-18 16:10:25 +00002991
Paul Bakker9e36f042013-06-30 14:34:05 +02002992#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002993static void ssl_update_checksum_sha384( ssl_context *ssl,
2994 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002995{
Paul Bakker9e36f042013-06-30 14:34:05 +02002996 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002997}
Paul Bakker769075d2012-11-24 11:26:46 +01002998#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002999#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003000
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003001#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003002static void ssl_calc_finished_ssl(
3003 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003004{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003005 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003006 md5_context md5;
3007 sha1_context sha1;
3008
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 unsigned char padbuf[48];
3010 unsigned char md5sum[16];
3011 unsigned char sha1sum[20];
3012
Paul Bakker48916f92012-09-16 19:57:18 +00003013 ssl_session *session = ssl->session_negotiate;
3014 if( !session )
3015 session = ssl->session;
3016
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3018
Paul Bakker48916f92012-09-16 19:57:18 +00003019 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3020 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003021
3022 /*
3023 * SSLv3:
3024 * hash =
3025 * MD5( master + pad2 +
3026 * MD5( handshake + sender + master + pad1 ) )
3027 * + SHA1( master + pad2 +
3028 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003029 */
3030
Paul Bakker90995b52013-06-24 19:20:35 +02003031#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003033 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Paul Bakker90995b52013-06-24 19:20:35 +02003036#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003038 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003039#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Paul Bakker3c2122f2013-06-24 19:03:14 +02003041 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3042 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Paul Bakker1ef83d62012-04-11 12:09:53 +00003044 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Paul Bakker3c2122f2013-06-24 19:03:14 +02003046 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003047 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048 md5_update( &md5, padbuf, 48 );
3049 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Paul Bakker3c2122f2013-06-24 19:03:14 +02003051 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003052 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003053 sha1_update( &sha1, padbuf, 40 );
3054 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Paul Bakker1ef83d62012-04-11 12:09:53 +00003056 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Paul Bakker1ef83d62012-04-11 12:09:53 +00003058 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003059 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003060 md5_update( &md5, padbuf, 48 );
3061 md5_update( &md5, md5sum, 16 );
3062 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Paul Bakker1ef83d62012-04-11 12:09:53 +00003064 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003065 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003066 sha1_update( &sha1, padbuf , 40 );
3067 sha1_update( &sha1, sha1sum, 20 );
3068 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003069
Paul Bakker1ef83d62012-04-11 12:09:53 +00003070 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003071
Paul Bakker5b4af392014-06-26 12:09:34 +02003072 md5_free( &md5 );
3073 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003074
Paul Bakker34617722014-06-13 17:20:13 +02003075 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3076 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3077 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
3079 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3080}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003081#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003083#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003084static void ssl_calc_finished_tls(
3085 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003086{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003087 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003088 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003089 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003091 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Paul Bakker48916f92012-09-16 19:57:18 +00003093 ssl_session *session = ssl->session_negotiate;
3094 if( !session )
3095 session = ssl->session;
3096
Paul Bakker1ef83d62012-04-11 12:09:53 +00003097 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003098
Paul Bakker48916f92012-09-16 19:57:18 +00003099 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3100 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003101
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 /*
3103 * TLSv1:
3104 * hash = PRF( master, finished_label,
3105 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3106 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003107
Paul Bakker90995b52013-06-24 19:20:35 +02003108#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003109 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3110 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003111#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003112
Paul Bakker90995b52013-06-24 19:20:35 +02003113#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003114 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3115 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003116#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003117
3118 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003119 ? "client finished"
3120 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003121
3122 md5_finish( &md5, padbuf );
3123 sha1_finish( &sha1, padbuf + 16 );
3124
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003125 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003126 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127
3128 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3129
Paul Bakker5b4af392014-06-26 12:09:34 +02003130 md5_free( &md5 );
3131 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003132
Paul Bakker34617722014-06-13 17:20:13 +02003133 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003134
3135 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3136}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003137#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003138
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003139#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3140#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003141static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003142 ssl_context *ssl, unsigned char *buf, int from )
3143{
3144 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003145 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003146 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003147 unsigned char padbuf[32];
3148
Paul Bakker48916f92012-09-16 19:57:18 +00003149 ssl_session *session = ssl->session_negotiate;
3150 if( !session )
3151 session = ssl->session;
3152
Paul Bakker380da532012-04-18 16:10:25 +00003153 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003154
Paul Bakker9e36f042013-06-30 14:34:05 +02003155 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003156
3157 /*
3158 * TLSv1.2:
3159 * hash = PRF( master, finished_label,
3160 * Hash( handshake ) )[0.11]
3161 */
3162
Paul Bakker9e36f042013-06-30 14:34:05 +02003163#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003164 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003165 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003166#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003167
3168 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003169 ? "client finished"
3170 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171
Paul Bakker9e36f042013-06-30 14:34:05 +02003172 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003173
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003174 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003175 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003176
3177 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3178
Paul Bakker5b4af392014-06-26 12:09:34 +02003179 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003180
Paul Bakker34617722014-06-13 17:20:13 +02003181 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003182
3183 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3184}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003185#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003186
Paul Bakker9e36f042013-06-30 14:34:05 +02003187#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003188static void ssl_calc_finished_tls_sha384(
3189 ssl_context *ssl, unsigned char *buf, int from )
3190{
3191 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003192 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003193 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003194 unsigned char padbuf[48];
3195
Paul Bakker48916f92012-09-16 19:57:18 +00003196 ssl_session *session = ssl->session_negotiate;
3197 if( !session )
3198 session = ssl->session;
3199
Paul Bakker380da532012-04-18 16:10:25 +00003200 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003201
Paul Bakker9e36f042013-06-30 14:34:05 +02003202 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003203
3204 /*
3205 * TLSv1.2:
3206 * hash = PRF( master, finished_label,
3207 * Hash( handshake ) )[0.11]
3208 */
3209
Paul Bakker9e36f042013-06-30 14:34:05 +02003210#if !defined(POLARSSL_SHA512_ALT)
3211 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3212 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003213#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003214
3215 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003216 ? "client finished"
3217 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003218
Paul Bakker9e36f042013-06-30 14:34:05 +02003219 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003220
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003221 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003222 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003223
3224 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3225
Paul Bakker5b4af392014-06-26 12:09:34 +02003226 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003227
Paul Bakker34617722014-06-13 17:20:13 +02003228 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003229
3230 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3231}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003232#endif /* POLARSSL_SHA512_C */
3233#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003234
Paul Bakker48916f92012-09-16 19:57:18 +00003235void ssl_handshake_wrapup( ssl_context *ssl )
3236{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003237 int resume = ssl->handshake->resume;
3238
Paul Bakker48916f92012-09-16 19:57:18 +00003239 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3240
3241 /*
3242 * Free our handshake params
3243 */
3244 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003245 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003246 ssl->handshake = NULL;
3247
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003248 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003249 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003250 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003251 ssl->renego_records_seen = 0;
3252 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003253
Paul Bakker48916f92012-09-16 19:57:18 +00003254 /*
3255 * Switch in our now active transform context
3256 */
3257 if( ssl->transform )
3258 {
3259 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003260 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003261 }
3262 ssl->transform = ssl->transform_negotiate;
3263 ssl->transform_negotiate = NULL;
3264
Paul Bakker0a597072012-09-25 21:55:46 +00003265 if( ssl->session )
3266 {
3267 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003268 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003269 }
3270 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003271 ssl->session_negotiate = NULL;
3272
Paul Bakker0a597072012-09-25 21:55:46 +00003273 /*
3274 * Add cache entry
3275 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003276 if( ssl->f_set_cache != NULL &&
3277 ssl->session->length != 0 &&
3278 resume == 0 )
3279 {
Paul Bakker0a597072012-09-25 21:55:46 +00003280 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3281 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003282 }
Paul Bakker0a597072012-09-25 21:55:46 +00003283
Paul Bakker48916f92012-09-16 19:57:18 +00003284 ssl->state++;
3285
3286 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3287}
3288
Paul Bakker1ef83d62012-04-11 12:09:53 +00003289int ssl_write_finished( ssl_context *ssl )
3290{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003291 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003292
3293 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3294
Paul Bakker92be97b2013-01-02 17:30:03 +01003295 /*
3296 * Set the out_msg pointer to the correct location based on IV length
3297 */
3298 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3299 {
3300 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3301 ssl->transform_negotiate->fixed_ivlen;
3302 }
3303 else
3304 ssl->out_msg = ssl->out_iv;
3305
Paul Bakker48916f92012-09-16 19:57:18 +00003306 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003307
3308 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3310
Paul Bakker48916f92012-09-16 19:57:18 +00003311 ssl->verify_data_len = hash_len;
3312 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3313
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 ssl->out_msglen = 4 + hash_len;
3315 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3316 ssl->out_msg[0] = SSL_HS_FINISHED;
3317
3318 /*
3319 * In case of session resuming, invert the client and server
3320 * ChangeCipherSpec messages order.
3321 */
Paul Bakker0a597072012-09-25 21:55:46 +00003322 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 {
3324 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003325 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 else
3327 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3328 }
3329 else
3330 ssl->state++;
3331
Paul Bakker48916f92012-09-16 19:57:18 +00003332 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003333 * Switch to our negotiated transform and session parameters for outbound
3334 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003335 */
3336 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3337 ssl->transform_out = ssl->transform_negotiate;
3338 ssl->session_out = ssl->session_negotiate;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003339
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003340#if defined(POLARSSL_SSL_PROTO_DTLS)
3341 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3342 {
3343 unsigned char i;
3344
3345 /* Set sequence_number to zero */
3346 memset( ssl->out_ctr + 2, 0, 6 );
3347
3348 /* Increment epoch */
3349 for( i = 2; i > 0; i-- )
3350 if( ++ssl->out_ctr[i - 1] != 0 )
3351 break;
3352
3353 /* The loop goes to its end iff the counter is wrapping */
3354 if( i == 0 )
3355 {
3356 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3357 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3358 }
3359 }
3360 else
3361#endif /* POLARSSL_SSL_PROTO_DTLS */
3362 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003363
Paul Bakker07eb38b2012-12-19 14:42:06 +01003364#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003365 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003366 {
3367 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3368 {
3369 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3370 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3371 }
3372 }
3373#endif
3374
Paul Bakker5121ce52009-01-03 21:22:43 +00003375 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3376 {
3377 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3378 return( ret );
3379 }
3380
3381 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3382
3383 return( 0 );
3384}
3385
3386int ssl_parse_finished( ssl_context *ssl )
3387{
Paul Bakker23986e52011-04-24 08:57:21 +00003388 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003389 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 unsigned char buf[36];
3391
3392 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3393
Paul Bakker48916f92012-09-16 19:57:18 +00003394 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003395
Paul Bakker48916f92012-09-16 19:57:18 +00003396 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003397 * Switch to our negotiated transform and session parameters for inbound
3398 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003399 */
3400 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3401 ssl->transform_in = ssl->transform_negotiate;
3402 ssl->session_in = ssl->session_negotiate;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003403
3404 /* Input counter/epoch not used with DTLS right now,
3405 * but it doesn't hurt to have this part ready */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003406#if defined(POLARSSL_SSL_PROTO_DTLS)
3407 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3408 {
3409 unsigned char i;
3410
3411 /* Set sequence_number to zero */
3412 memset( ssl->in_ctr + 2, 0, 6 );
3413
3414 /* Increment epoch */
3415 for( i = 2; i > 0; i-- )
3416 if( ++ssl->in_ctr[i - 1] != 0 )
3417 break;
3418
3419 /* The loop goes to its end iff the counter is wrapping */
3420 if( i == 0 )
3421 {
3422 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3423 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3424 }
3425 }
3426 else
3427#endif /* POLARSSL_SSL_PROTO_DTLS */
3428 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429
Paul Bakker92be97b2013-01-02 17:30:03 +01003430 /*
3431 * Set the in_msg pointer to the correct location based on IV length
3432 */
3433 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3434 {
3435 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3436 ssl->transform_negotiate->fixed_ivlen;
3437 }
3438 else
3439 ssl->in_msg = ssl->in_iv;
3440
Paul Bakker07eb38b2012-12-19 14:42:06 +01003441#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003442 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003443 {
3444 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3445 {
3446 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3447 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3448 }
3449 }
3450#endif
3451
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3453 {
3454 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3455 return( ret );
3456 }
3457
3458 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3459 {
3460 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003461 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 }
3463
Paul Bakker1ef83d62012-04-11 12:09:53 +00003464 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3466
3467 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3468 ssl->in_hslen != 4 + hash_len )
3469 {
3470 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003471 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 }
3473
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003474 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 {
3476 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003477 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003478 }
3479
Paul Bakker48916f92012-09-16 19:57:18 +00003480 ssl->verify_data_len = hash_len;
3481 memcpy( ssl->peer_verify_data, buf, hash_len );
3482
Paul Bakker0a597072012-09-25 21:55:46 +00003483 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 {
3485 if( ssl->endpoint == SSL_IS_CLIENT )
3486 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3487
3488 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003489 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003490 }
3491 else
3492 ssl->state++;
3493
3494 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3495
3496 return( 0 );
3497}
3498
Paul Bakker968afaa2014-07-09 11:09:24 +02003499static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003500{
3501 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3502
3503#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3504 defined(POLARSSL_SSL_PROTO_TLS1_1)
3505 md5_init( &handshake->fin_md5 );
3506 sha1_init( &handshake->fin_sha1 );
3507 md5_starts( &handshake->fin_md5 );
3508 sha1_starts( &handshake->fin_sha1 );
3509#endif
3510#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3511#if defined(POLARSSL_SHA256_C)
3512 sha256_init( &handshake->fin_sha256 );
3513 sha256_starts( &handshake->fin_sha256, 0 );
3514#endif
3515#if defined(POLARSSL_SHA512_C)
3516 sha512_init( &handshake->fin_sha512 );
3517 sha512_starts( &handshake->fin_sha512, 1 );
3518#endif
3519#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3520
3521 handshake->update_checksum = ssl_update_checksum_start;
3522 handshake->sig_alg = SSL_HASH_SHA1;
3523
3524#if defined(POLARSSL_DHM_C)
3525 dhm_init( &handshake->dhm_ctx );
3526#endif
3527#if defined(POLARSSL_ECDH_C)
3528 ecdh_init( &handshake->ecdh_ctx );
3529#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003530}
3531
3532static void ssl_transform_init( ssl_transform *transform )
3533{
3534 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003535
3536 cipher_init( &transform->cipher_ctx_enc );
3537 cipher_init( &transform->cipher_ctx_dec );
3538
3539 md_init( &transform->md_ctx_enc );
3540 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003541}
3542
3543void ssl_session_init( ssl_session *session )
3544{
3545 memset( session, 0, sizeof(ssl_session) );
3546}
3547
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003548static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003549{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003550 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003551 if( ssl->transform_negotiate )
3552 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003553 if( ssl->session_negotiate )
3554 ssl_session_free( ssl->session_negotiate );
3555 if( ssl->handshake )
3556 ssl_handshake_free( ssl->handshake );
3557
3558 /*
3559 * Either the pointers are now NULL or cleared properly and can be freed.
3560 * Now allocate missing structures.
3561 */
3562 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003563 {
3564 ssl->transform_negotiate =
3565 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3566 }
Paul Bakker48916f92012-09-16 19:57:18 +00003567
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003568 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003569 {
3570 ssl->session_negotiate =
3571 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3572 }
Paul Bakker48916f92012-09-16 19:57:18 +00003573
Paul Bakker82788fb2014-10-20 13:59:19 +02003574 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003575 {
3576 ssl->handshake = (ssl_handshake_params *)
3577 polarssl_malloc( sizeof(ssl_handshake_params) );
3578 }
Paul Bakker48916f92012-09-16 19:57:18 +00003579
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003580 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003581 if( ssl->handshake == NULL ||
3582 ssl->transform_negotiate == NULL ||
3583 ssl->session_negotiate == NULL )
3584 {
3585 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003586
3587 polarssl_free( ssl->handshake );
3588 polarssl_free( ssl->transform_negotiate );
3589 polarssl_free( ssl->session_negotiate );
3590
3591 ssl->handshake = NULL;
3592 ssl->transform_negotiate = NULL;
3593 ssl->session_negotiate = NULL;
3594
Paul Bakker48916f92012-09-16 19:57:18 +00003595 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3596 }
3597
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003598 /* Initialize structures */
3599 ssl_session_init( ssl->session_negotiate );
3600 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003601 ssl_handshake_params_init( ssl->handshake );
3602
3603#if defined(POLARSSL_X509_CRT_PARSE_C)
3604 ssl->handshake->key_cert = ssl->key_cert;
3605#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003606
Paul Bakker48916f92012-09-16 19:57:18 +00003607 return( 0 );
3608}
3609
Paul Bakker5121ce52009-01-03 21:22:43 +00003610/*
3611 * Initialize an SSL context
3612 */
3613int ssl_init( ssl_context *ssl )
3614{
Paul Bakker48916f92012-09-16 19:57:18 +00003615 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003616 int len = SSL_BUFFER_LEN;
3617
3618 memset( ssl, 0, sizeof( ssl_context ) );
3619
Paul Bakker62f2dee2012-09-28 07:31:51 +00003620 /*
3621 * Sane defaults
3622 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003623 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3624 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3625 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3626 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003627
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003628 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003629
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003630 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3631
Paul Bakker62f2dee2012-09-28 07:31:51 +00003632#if defined(POLARSSL_DHM_C)
3633 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3634 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3635 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3636 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3637 {
3638 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3639 return( ret );
3640 }
3641#endif
3642
3643 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003644 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003645 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003646 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003647 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003648
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003649 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003650 {
3651 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003652 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003653 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003654 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003655 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003656 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 }
3658
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003659 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
3660 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00003661
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003662 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
3663 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
3664
Paul Bakker606b4ba2013-08-14 16:52:14 +02003665#if defined(POLARSSL_SSL_SESSION_TICKETS)
3666 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3667#endif
3668
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003669#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003670 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003671#endif
3672
Paul Bakker48916f92012-09-16 19:57:18 +00003673 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3674 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003675
3676 return( 0 );
3677}
3678
3679/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003680 * Reset an initialized and used SSL context for re-use while retaining
3681 * all application-set variables, function pointers and data.
3682 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003683int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003684{
Paul Bakker48916f92012-09-16 19:57:18 +00003685 int ret;
3686
Paul Bakker7eb013f2011-10-06 12:37:39 +00003687 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003688 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3689 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3690
3691 ssl->verify_data_len = 0;
3692 memset( ssl->own_verify_data, 0, 36 );
3693 memset( ssl->peer_verify_data, 0, 36 );
3694
Paul Bakker7eb013f2011-10-06 12:37:39 +00003695 ssl->in_offt = NULL;
3696
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003697 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003698 ssl->in_msgtype = 0;
3699 ssl->in_msglen = 0;
3700 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003701#if defined(POLARSSL_SSL_PROTO_DTLS)
3702 ssl->next_record_offset = 0;
3703#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003704
3705 ssl->in_hslen = 0;
3706 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003707 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003708
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003709 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003710 ssl->out_msgtype = 0;
3711 ssl->out_msglen = 0;
3712 ssl->out_left = 0;
3713
Paul Bakker48916f92012-09-16 19:57:18 +00003714 ssl->transform_in = NULL;
3715 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003716
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003717 ssl->renego_records_seen = 0;
3718
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003719 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
3720 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003721
3722#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003723 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003724 {
3725 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003726 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003727 {
3728 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3729 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3730 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003731 }
3732#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003733
Paul Bakker48916f92012-09-16 19:57:18 +00003734 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003735 {
Paul Bakker48916f92012-09-16 19:57:18 +00003736 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003737 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003738 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003739 }
Paul Bakker48916f92012-09-16 19:57:18 +00003740
Paul Bakkerc0463502013-02-14 11:19:38 +01003741 if( ssl->session )
3742 {
3743 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003744 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003745 ssl->session = NULL;
3746 }
3747
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003748#if defined(POLARSSL_SSL_ALPN)
3749 ssl->alpn_chosen = NULL;
3750#endif
3751
Paul Bakker48916f92012-09-16 19:57:18 +00003752 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3753 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003754
3755 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003756}
3757
Paul Bakkera503a632013-08-14 13:48:06 +02003758#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003759static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3760{
3761 aes_free( &tkeys->enc );
3762 aes_free( &tkeys->dec );
3763
3764 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3765}
3766
Paul Bakker7eb013f2011-10-06 12:37:39 +00003767/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003768 * Allocate and initialize ticket keys
3769 */
3770static int ssl_ticket_keys_init( ssl_context *ssl )
3771{
3772 int ret;
3773 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003774 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003775
3776 if( ssl->ticket_keys != NULL )
3777 return( 0 );
3778
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003779 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3780 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003781 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3782
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003783 aes_init( &tkeys->enc );
3784 aes_init( &tkeys->dec );
3785
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003786 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003787 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003788 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003789 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003790 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003791 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003792
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003793 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3794 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3795 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3796 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003797 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003798 polarssl_free( tkeys );
3799 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003800 }
3801
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003802 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003803 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003804 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003805 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003806 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003807 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003808
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003809 ssl->ticket_keys = tkeys;
3810
3811 return( 0 );
3812}
Paul Bakkera503a632013-08-14 13:48:06 +02003813#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003814
3815/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003816 * SSL set accessors
3817 */
3818void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3819{
3820 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003821
Paul Bakker606b4ba2013-08-14 16:52:14 +02003822#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003823 if( endpoint == SSL_IS_CLIENT )
3824 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003825#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003826}
3827
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003828int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01003829{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003830#if defined(POLARSSL_SSL_PROTO_DTLS)
3831 if( transport == SSL_TRANSPORT_DATAGRAM )
3832 {
3833 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003834
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003835 ssl->out_hdr = ssl->out_buf;
3836 ssl->out_ctr = ssl->out_buf + 3;
3837 ssl->out_len = ssl->out_buf + 11;
3838 ssl->out_iv = ssl->out_buf + 13;
3839 ssl->out_msg = ssl->out_buf + 13;
3840
3841 ssl->in_hdr = ssl->in_buf;
3842 ssl->in_ctr = ssl->in_buf + 3;
3843 ssl->in_len = ssl->in_buf + 11;
3844 ssl->in_iv = ssl->in_buf + 13;
3845 ssl->in_msg = ssl->in_buf + 13;
3846
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003847 /* DTLS starts with TLS1.1 */
3848 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
3849 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003850
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003851 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
3852 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
3853
3854 return( 0 );
3855 }
3856#endif
3857
3858 if( transport == SSL_TRANSPORT_STREAM )
3859 {
3860 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003861
3862 ssl->out_ctr = ssl->out_buf;
3863 ssl->out_hdr = ssl->out_buf + 8;
3864 ssl->out_len = ssl->out_buf + 11;
3865 ssl->out_iv = ssl->out_buf + 13;
3866 ssl->out_msg = ssl->out_buf + 13;
3867
3868 ssl->in_ctr = ssl->in_buf;
3869 ssl->in_hdr = ssl->in_buf + 8;
3870 ssl->in_len = ssl->in_buf + 11;
3871 ssl->in_iv = ssl->in_buf + 13;
3872 ssl->in_msg = ssl->in_buf + 13;
3873
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003874 return( 0 );
3875 }
3876
3877 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01003878}
3879
Paul Bakker5121ce52009-01-03 21:22:43 +00003880void ssl_set_authmode( ssl_context *ssl, int authmode )
3881{
3882 ssl->authmode = authmode;
3883}
3884
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003885#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003886void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003887 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003888 void *p_vrfy )
3889{
3890 ssl->f_vrfy = f_vrfy;
3891 ssl->p_vrfy = p_vrfy;
3892}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003893#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003894
Paul Bakker5121ce52009-01-03 21:22:43 +00003895void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003896 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003897 void *p_rng )
3898{
3899 ssl->f_rng = f_rng;
3900 ssl->p_rng = p_rng;
3901}
3902
3903void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003904 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003905 void *p_dbg )
3906{
3907 ssl->f_dbg = f_dbg;
3908 ssl->p_dbg = p_dbg;
3909}
3910
3911void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003912 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003913 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003914{
3915 ssl->f_recv = f_recv;
3916 ssl->f_send = f_send;
3917 ssl->p_recv = p_recv;
3918 ssl->p_send = p_send;
3919}
3920
Paul Bakker0a597072012-09-25 21:55:46 +00003921void ssl_set_session_cache( ssl_context *ssl,
3922 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3923 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003924{
Paul Bakker0a597072012-09-25 21:55:46 +00003925 ssl->f_get_cache = f_get_cache;
3926 ssl->p_get_cache = p_get_cache;
3927 ssl->f_set_cache = f_set_cache;
3928 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003929}
3930
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003931int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003932{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003933 int ret;
3934
3935 if( ssl == NULL ||
3936 session == NULL ||
3937 ssl->session_negotiate == NULL ||
3938 ssl->endpoint != SSL_IS_CLIENT )
3939 {
3940 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3941 }
3942
3943 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3944 return( ret );
3945
Paul Bakker0a597072012-09-25 21:55:46 +00003946 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003947
3948 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003949}
3950
Paul Bakkerb68cad62012-08-23 08:34:18 +00003951void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003952{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003953 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3954 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3955 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3956 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3957}
3958
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003959void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3960 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003961 int major, int minor )
3962{
3963 if( major != SSL_MAJOR_VERSION_3 )
3964 return;
3965
3966 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3967 return;
3968
3969 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003970}
3971
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003972#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003973/* Add a new (empty) key_cert entry an return a pointer to it */
3974static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3975{
3976 ssl_key_cert *key_cert, *last;
3977
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003978 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3979 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003980 return( NULL );
3981
3982 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3983
3984 /* Append the new key_cert to the (possibly empty) current list */
3985 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003986 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003987 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003988 if( ssl->handshake != NULL )
3989 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003990 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003991 else
3992 {
3993 last = ssl->key_cert;
3994 while( last->next != NULL )
3995 last = last->next;
3996 last->next = key_cert;
3997 }
3998
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003999 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004000}
4001
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004002void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00004003 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00004004{
4005 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00004006 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 ssl->peer_cn = peer_cn;
4008}
4009
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004010int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004011 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004012{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004013 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4014
4015 if( key_cert == NULL )
4016 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4017
4018 key_cert->cert = own_cert;
4019 key_cert->key = pk_key;
4020
4021 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004022}
4023
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004024#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004025int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004026 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004027{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004028 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004029 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004030
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004031 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004032 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4033
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004034 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4035 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004036 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004037
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004038 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004039
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004040 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004041 if( ret != 0 )
4042 return( ret );
4043
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004044 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004045 return( ret );
4046
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004047 key_cert->cert = own_cert;
4048 key_cert->key_own_alloc = 1;
4049
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004050 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004051}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004052#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004053
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004054int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004055 void *rsa_key,
4056 rsa_decrypt_func rsa_decrypt,
4057 rsa_sign_func rsa_sign,
4058 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004059{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004060 int ret;
4061 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004062
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004063 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004064 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4065
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004066 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4067 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004068 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004069
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004070 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004071
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004072 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4073 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4074 return( ret );
4075
4076 key_cert->cert = own_cert;
4077 key_cert->key_own_alloc = 1;
4078
4079 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004080}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004081#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004082
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004083#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004084int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4085 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004086{
Paul Bakker6db455e2013-09-18 17:29:31 +02004087 if( psk == NULL || psk_identity == NULL )
4088 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4089
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004090 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004091 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4092
Paul Bakker6db455e2013-09-18 17:29:31 +02004093 if( ssl->psk != NULL )
4094 {
4095 polarssl_free( ssl->psk );
4096 polarssl_free( ssl->psk_identity );
4097 }
4098
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004099 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004100 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004101
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004102 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004103 ssl->psk_identity = (unsigned char *)
4104 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004105
4106 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4107 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4108
4109 memcpy( ssl->psk, psk, ssl->psk_len );
4110 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004111
4112 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004113}
4114
4115void ssl_set_psk_cb( ssl_context *ssl,
4116 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4117 size_t),
4118 void *p_psk )
4119{
4120 ssl->f_psk = f_psk;
4121 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004122}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004123#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004124
Paul Bakker48916f92012-09-16 19:57:18 +00004125#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004126int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004127{
4128 int ret;
4129
Paul Bakker48916f92012-09-16 19:57:18 +00004130 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004131 {
4132 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4133 return( ret );
4134 }
4135
Paul Bakker48916f92012-09-16 19:57:18 +00004136 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004137 {
4138 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4139 return( ret );
4140 }
4141
4142 return( 0 );
4143}
4144
Paul Bakker1b57b062011-01-06 15:48:19 +00004145int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4146{
4147 int ret;
4148
Paul Bakker66d5d072014-06-17 16:39:18 +02004149 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004150 {
4151 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4152 return( ret );
4153 }
4154
Paul Bakker66d5d072014-06-17 16:39:18 +02004155 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004156 {
4157 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4158 return( ret );
4159 }
4160
4161 return( 0 );
4162}
Paul Bakker48916f92012-09-16 19:57:18 +00004163#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004164
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004165#if defined(POLARSSL_SSL_SET_CURVES)
4166/*
4167 * Set the allowed elliptic curves
4168 */
4169void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4170{
4171 ssl->curve_list = curve_list;
4172}
4173#endif
4174
Paul Bakker0be444a2013-08-27 21:55:01 +02004175#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004176int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004177{
4178 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004179 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004180
4181 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004182
4183 if( ssl->hostname_len + 1 == 0 )
4184 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4185
Paul Bakker6e339b52013-07-03 13:37:05 +02004186 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004187
Paul Bakkerb15b8512012-01-13 13:44:06 +00004188 if( ssl->hostname == NULL )
4189 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4190
Paul Bakker3c2122f2013-06-24 19:03:14 +02004191 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004192 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004193
Paul Bakker40ea7de2009-05-03 10:18:48 +00004194 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004195
4196 return( 0 );
4197}
4198
Paul Bakker5701cdc2012-09-27 21:49:42 +00004199void ssl_set_sni( ssl_context *ssl,
4200 int (*f_sni)(void *, ssl_context *,
4201 const unsigned char *, size_t),
4202 void *p_sni )
4203{
4204 ssl->f_sni = f_sni;
4205 ssl->p_sni = p_sni;
4206}
Paul Bakker0be444a2013-08-27 21:55:01 +02004207#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004208
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004209#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004210int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004211{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004212 size_t cur_len, tot_len;
4213 const char **p;
4214
4215 /*
4216 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4217 * truncated". Check lengths now rather than later.
4218 */
4219 tot_len = 0;
4220 for( p = protos; *p != NULL; p++ )
4221 {
4222 cur_len = strlen( *p );
4223 tot_len += cur_len;
4224
4225 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4226 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4227 }
4228
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004229 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004230
4231 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004232}
4233
4234const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4235{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004236 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004237}
4238#endif /* POLARSSL_SSL_ALPN */
4239
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004240static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00004241{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004242 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
4243 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
4244 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4245 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004246 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004247 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004248 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004249
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004250 return( 0 );
4251}
4252
4253int ssl_set_max_version( ssl_context *ssl, int major, int minor )
4254{
4255 if( ssl_check_version( ssl, major, minor ) != 0 )
4256 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4257
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004258 ssl->max_major_ver = major;
4259 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004260
4261 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00004262}
4263
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004264int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00004265{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004266 if( ssl_check_version( ssl, major, minor ) != 0 )
4267 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004268
4269 ssl->min_major_ver = major;
4270 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004271
4272 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00004273}
4274
Paul Bakker05decb22013-08-15 13:33:48 +02004275#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004276int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4277{
Paul Bakker77e257e2013-12-16 15:29:52 +01004278 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004279 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004280 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004281 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004282 }
4283
4284 ssl->mfl_code = mfl_code;
4285
4286 return( 0 );
4287}
Paul Bakker05decb22013-08-15 13:33:48 +02004288#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004289
Paul Bakker1f2bc622013-08-15 13:45:55 +02004290#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004291int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004292{
4293 if( ssl->endpoint != SSL_IS_CLIENT )
4294 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4295
Paul Bakker8c1ede62013-07-19 14:14:37 +02004296 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004297
4298 return( 0 );
4299}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004300#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004301
Paul Bakker48916f92012-09-16 19:57:18 +00004302void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4303{
4304 ssl->disable_renegotiation = renegotiation;
4305}
4306
4307void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4308{
4309 ssl->allow_legacy_renegotiation = allow_legacy;
4310}
4311
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004312void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4313{
4314 ssl->renego_max_records = max_records;
4315}
4316
Paul Bakkera503a632013-08-14 13:48:06 +02004317#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004318int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4319{
4320 ssl->session_tickets = use_tickets;
4321
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004322 if( ssl->endpoint == SSL_IS_CLIENT )
4323 return( 0 );
4324
4325 if( ssl->f_rng == NULL )
4326 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4327
4328 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004329}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004330
4331void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4332{
4333 ssl->ticket_lifetime = lifetime;
4334}
Paul Bakkera503a632013-08-14 13:48:06 +02004335#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004336
Paul Bakker5121ce52009-01-03 21:22:43 +00004337/*
4338 * SSL get accessors
4339 */
Paul Bakker23986e52011-04-24 08:57:21 +00004340size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004341{
4342 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4343}
4344
Paul Bakkerff60ee62010-03-16 21:09:09 +00004345int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004346{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004347 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004348}
4349
Paul Bakkere3166ce2011-01-27 17:40:50 +00004350const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004351{
Paul Bakker926c8e42013-03-06 10:23:34 +01004352 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004353 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004354
Paul Bakkere3166ce2011-01-27 17:40:50 +00004355 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004356}
4357
Paul Bakker43ca69c2011-01-15 17:35:19 +00004358const char *ssl_get_version( const ssl_context *ssl )
4359{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004360#if defined(POLARSSL_SSL_PROTO_DTLS)
4361 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4362 {
4363 switch( ssl->minor_ver )
4364 {
4365 case SSL_MINOR_VERSION_2:
4366 return( "DTLSv1.0" );
4367
4368 case SSL_MINOR_VERSION_3:
4369 return( "DTLSv1.2" );
4370
4371 default:
4372 return( "unknown (DTLS)" );
4373 }
4374 }
4375#endif
4376
Paul Bakker43ca69c2011-01-15 17:35:19 +00004377 switch( ssl->minor_ver )
4378 {
4379 case SSL_MINOR_VERSION_0:
4380 return( "SSLv3.0" );
4381
4382 case SSL_MINOR_VERSION_1:
4383 return( "TLSv1.0" );
4384
4385 case SSL_MINOR_VERSION_2:
4386 return( "TLSv1.1" );
4387
Paul Bakker1ef83d62012-04-11 12:09:53 +00004388 case SSL_MINOR_VERSION_3:
4389 return( "TLSv1.2" );
4390
Paul Bakker43ca69c2011-01-15 17:35:19 +00004391 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004392 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00004393 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00004394}
4395
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004396#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004397const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004398{
4399 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004400 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004401
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004402 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004403}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004404#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004405
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004406int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4407{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004408 if( ssl == NULL ||
4409 dst == NULL ||
4410 ssl->session == NULL ||
4411 ssl->endpoint != SSL_IS_CLIENT )
4412 {
4413 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4414 }
4415
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004416 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004417}
4418
Paul Bakker5121ce52009-01-03 21:22:43 +00004419/*
Paul Bakker1961b702013-01-25 14:49:24 +01004420 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004421 */
Paul Bakker1961b702013-01-25 14:49:24 +01004422int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004423{
Paul Bakker40e46942009-01-03 21:51:57 +00004424 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004425
Paul Bakker40e46942009-01-03 21:51:57 +00004426#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004427 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004428 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004429#endif
4430
Paul Bakker40e46942009-01-03 21:51:57 +00004431#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004433 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434#endif
4435
Paul Bakker1961b702013-01-25 14:49:24 +01004436 return( ret );
4437}
4438
4439/*
4440 * Perform the SSL handshake
4441 */
4442int ssl_handshake( ssl_context *ssl )
4443{
4444 int ret = 0;
4445
4446 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4447
4448 while( ssl->state != SSL_HANDSHAKE_OVER )
4449 {
4450 ret = ssl_handshake_step( ssl );
4451
4452 if( ret != 0 )
4453 break;
4454 }
4455
Paul Bakker5121ce52009-01-03 21:22:43 +00004456 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4457
4458 return( ret );
4459}
4460
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004461#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004462/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004463 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004464 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004465static int ssl_write_hello_request( ssl_context *ssl )
4466{
4467 int ret;
4468
4469 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4470
4471 ssl->out_msglen = 4;
4472 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4473 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4474
4475 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4476 {
4477 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4478 return( ret );
4479 }
4480
4481 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4482
4483 return( 0 );
4484}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004485#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004486
4487/*
4488 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004489 * - any side: calling ssl_renegotiate(),
4490 * - client: receiving a HelloRequest during ssl_read(),
4491 * - server: receiving any handshake message on server during ssl_read() after
4492 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004493 * If the handshake doesn't complete due to waiting for I/O, it will continue
4494 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004495 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004496static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004497{
4498 int ret;
4499
4500 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4501
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004502 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4503 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004504
4505 ssl->state = SSL_HELLO_REQUEST;
4506 ssl->renegotiation = SSL_RENEGOTIATION;
4507
Paul Bakker48916f92012-09-16 19:57:18 +00004508 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4509 {
4510 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4511 return( ret );
4512 }
4513
4514 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4515
4516 return( 0 );
4517}
4518
4519/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004520 * Renegotiate current connection on client,
4521 * or request renegotiation on server
4522 */
4523int ssl_renegotiate( ssl_context *ssl )
4524{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004525 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004526
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004527#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004528 /* On server, just send the request */
4529 if( ssl->endpoint == SSL_IS_SERVER )
4530 {
4531 if( ssl->state != SSL_HANDSHAKE_OVER )
4532 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4533
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004534 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4535
4536 /* Did we already try/start sending HelloRequest? */
4537 if( ssl->out_left != 0 )
4538 return( ssl_flush_output( ssl ) );
4539
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004540 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004541 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004542#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004543
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004544#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004545 /*
4546 * On client, either start the renegotiation process or,
4547 * if already in progress, continue the handshake
4548 */
4549 if( ssl->renegotiation != SSL_RENEGOTIATION )
4550 {
4551 if( ssl->state != SSL_HANDSHAKE_OVER )
4552 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4553
4554 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4555 {
4556 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4557 return( ret );
4558 }
4559 }
4560 else
4561 {
4562 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4563 {
4564 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4565 return( ret );
4566 }
4567 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004568#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004569
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004570 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004571}
4572
4573/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004574 * Receive application data decrypted from the SSL layer
4575 */
Paul Bakker23986e52011-04-24 08:57:21 +00004576int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004577{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004578 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004579 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004580
4581 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4582
4583 if( ssl->state != SSL_HANDSHAKE_OVER )
4584 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004585 ret = ssl_handshake( ssl );
4586 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4587 {
4588 record_read = 1;
4589 }
4590 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004591 {
4592 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4593 return( ret );
4594 }
4595 }
4596
4597 if( ssl->in_offt == NULL )
4598 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004599 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004601 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4602 {
4603 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4604 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004605
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004606 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4607 return( ret );
4608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004609 }
4610
4611 if( ssl->in_msglen == 0 &&
4612 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4613 {
4614 /*
4615 * OpenSSL sends empty messages to randomize the IV
4616 */
4617 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4618 {
Paul Bakker831a7552011-05-18 13:32:51 +00004619 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4620 return( 0 );
4621
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4623 return( ret );
4624 }
4625 }
4626
Paul Bakker48916f92012-09-16 19:57:18 +00004627 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4628 {
4629 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4630
4631 if( ssl->endpoint == SSL_IS_CLIENT &&
4632 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4633 ssl->in_hslen != 4 ) )
4634 {
4635 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4636 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4637 }
4638
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004639 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4640 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004641 ssl->allow_legacy_renegotiation ==
4642 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004643 {
4644 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4645
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004646#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004647 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004648 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004649 /*
4650 * SSLv3 does not have a "no_renegotiation" alert
4651 */
4652 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4653 return( ret );
4654 }
4655 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004656#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004657#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4658 defined(POLARSSL_SSL_PROTO_TLS1_2)
4659 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004660 {
4661 if( ( ret = ssl_send_alert_message( ssl,
4662 SSL_ALERT_LEVEL_WARNING,
4663 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4664 {
4665 return( ret );
4666 }
Paul Bakker48916f92012-09-16 19:57:18 +00004667 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004668 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004669#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4670 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004671 {
4672 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004673 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004674 }
Paul Bakker48916f92012-09-16 19:57:18 +00004675 }
4676 else
4677 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004678 ret = ssl_start_renegotiation( ssl );
4679 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4680 {
4681 record_read = 1;
4682 }
4683 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004684 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004685 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004686 return( ret );
4687 }
Paul Bakker48916f92012-09-16 19:57:18 +00004688 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004689
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004690 /* If a non-handshake record was read during renego, fallthrough,
4691 * else tell the user they should call ssl_read() again */
4692 if( ! record_read )
4693 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004694 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004695 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4696 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004697 ssl->renego_records_seen++;
4698
4699 if( ssl->renego_max_records >= 0 &&
4700 ssl->renego_records_seen > ssl->renego_max_records )
4701 {
4702 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4703 "but not honored by client" ) );
4704 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4705 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004706 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004707
4708 /* Fatal and closure alerts handled by ssl_read_record() */
4709 if( ssl->in_msgtype == SSL_MSG_ALERT )
4710 {
4711 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4712 return( POLARSSL_ERR_NET_WANT_READ );
4713 }
4714
4715 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004716 {
4717 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004718 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 }
4720
4721 ssl->in_offt = ssl->in_msg;
4722 }
4723
4724 n = ( len < ssl->in_msglen )
4725 ? len : ssl->in_msglen;
4726
4727 memcpy( buf, ssl->in_offt, n );
4728 ssl->in_msglen -= n;
4729
4730 if( ssl->in_msglen == 0 )
4731 /* all bytes consumed */
4732 ssl->in_offt = NULL;
4733 else
4734 /* more data available */
4735 ssl->in_offt += n;
4736
4737 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4738
Paul Bakker23986e52011-04-24 08:57:21 +00004739 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004740}
4741
4742/*
4743 * Send application data to be encrypted by the SSL layer
4744 */
Paul Bakker23986e52011-04-24 08:57:21 +00004745int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004746{
Paul Bakker23986e52011-04-24 08:57:21 +00004747 int ret;
4748 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004749 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004750
4751 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4752
4753 if( ssl->state != SSL_HANDSHAKE_OVER )
4754 {
4755 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4756 {
4757 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4758 return( ret );
4759 }
4760 }
4761
Paul Bakker05decb22013-08-15 13:33:48 +02004762#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004763 /*
4764 * Assume mfl_code is correct since it was checked when set
4765 */
4766 max_len = mfl_code_to_length[ssl->mfl_code];
4767
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004768 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004769 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004770 */
4771 if( ssl->session_out != NULL &&
4772 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4773 {
4774 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4775 }
Paul Bakker05decb22013-08-15 13:33:48 +02004776#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004777
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004778 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004779
Paul Bakker5121ce52009-01-03 21:22:43 +00004780 if( ssl->out_left != 0 )
4781 {
4782 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4783 {
4784 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4785 return( ret );
4786 }
4787 }
Paul Bakker887bd502011-06-08 13:10:54 +00004788 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004789 {
Paul Bakker887bd502011-06-08 13:10:54 +00004790 ssl->out_msglen = n;
4791 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4792 memcpy( ssl->out_msg, buf, n );
4793
4794 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4795 {
4796 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4797 return( ret );
4798 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004799 }
4800
4801 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4802
Paul Bakker23986e52011-04-24 08:57:21 +00004803 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004804}
4805
4806/*
4807 * Notify the peer that the connection is being closed
4808 */
4809int ssl_close_notify( ssl_context *ssl )
4810{
4811 int ret;
4812
4813 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4814
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004815 if( ssl->out_left != 0 )
4816 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004817
4818 if( ssl->state == SSL_HANDSHAKE_OVER )
4819 {
Paul Bakker48916f92012-09-16 19:57:18 +00004820 if( ( ret = ssl_send_alert_message( ssl,
4821 SSL_ALERT_LEVEL_WARNING,
4822 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004823 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004824 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004825 return( ret );
4826 }
4827 }
4828
4829 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4830
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004831 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004832}
4833
Paul Bakker48916f92012-09-16 19:57:18 +00004834void ssl_transform_free( ssl_transform *transform )
4835{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004836 if( transform == NULL )
4837 return;
4838
Paul Bakker48916f92012-09-16 19:57:18 +00004839#if defined(POLARSSL_ZLIB_SUPPORT)
4840 deflateEnd( &transform->ctx_deflate );
4841 inflateEnd( &transform->ctx_inflate );
4842#endif
4843
Paul Bakker84bbeb52014-07-01 14:53:22 +02004844 cipher_free( &transform->cipher_ctx_enc );
4845 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004846
Paul Bakker84bbeb52014-07-01 14:53:22 +02004847 md_free( &transform->md_ctx_enc );
4848 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004849
Paul Bakker34617722014-06-13 17:20:13 +02004850 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004851}
4852
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004853#if defined(POLARSSL_X509_CRT_PARSE_C)
4854static void ssl_key_cert_free( ssl_key_cert *key_cert )
4855{
4856 ssl_key_cert *cur = key_cert, *next;
4857
4858 while( cur != NULL )
4859 {
4860 next = cur->next;
4861
4862 if( cur->key_own_alloc )
4863 {
4864 pk_free( cur->key );
4865 polarssl_free( cur->key );
4866 }
4867 polarssl_free( cur );
4868
4869 cur = next;
4870 }
4871}
4872#endif /* POLARSSL_X509_CRT_PARSE_C */
4873
Paul Bakker48916f92012-09-16 19:57:18 +00004874void ssl_handshake_free( ssl_handshake_params *handshake )
4875{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004876 if( handshake == NULL )
4877 return;
4878
Paul Bakker48916f92012-09-16 19:57:18 +00004879#if defined(POLARSSL_DHM_C)
4880 dhm_free( &handshake->dhm_ctx );
4881#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004882#if defined(POLARSSL_ECDH_C)
4883 ecdh_free( &handshake->ecdh_ctx );
4884#endif
4885
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004886#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004887 /* explicit void pointer cast for buggy MS compiler */
4888 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004889#endif
4890
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004891#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4892 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4893 /*
4894 * Free only the linked list wrapper, not the keys themselves
4895 * since the belong to the SNI callback
4896 */
4897 if( handshake->sni_key_cert != NULL )
4898 {
4899 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4900
4901 while( cur != NULL )
4902 {
4903 next = cur->next;
4904 polarssl_free( cur );
4905 cur = next;
4906 }
4907 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004908#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004909
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004910#if defined(POLARSSL_SSL_PROTO_DTLS)
4911 polarssl_free( handshake->verify_cookie );
4912#endif
4913
Paul Bakker34617722014-06-13 17:20:13 +02004914 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004915}
4916
4917void ssl_session_free( ssl_session *session )
4918{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004919 if( session == NULL )
4920 return;
4921
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004922#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004923 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004924 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004925 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004926 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004927 }
Paul Bakkered27a042013-04-18 22:46:23 +02004928#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004929
Paul Bakkera503a632013-08-14 13:48:06 +02004930#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004931 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004932#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004933
Paul Bakker34617722014-06-13 17:20:13 +02004934 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004935}
4936
Paul Bakker5121ce52009-01-03 21:22:43 +00004937/*
4938 * Free an SSL context
4939 */
4940void ssl_free( ssl_context *ssl )
4941{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004942 if( ssl == NULL )
4943 return;
4944
Paul Bakker5121ce52009-01-03 21:22:43 +00004945 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4946
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004947 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004948 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004949 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
4950 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004951 }
4952
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004953 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004954 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004955 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
4956 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004957 }
4958
Paul Bakker16770332013-10-11 09:59:44 +02004959#if defined(POLARSSL_ZLIB_SUPPORT)
4960 if( ssl->compress_buf != NULL )
4961 {
Paul Bakker34617722014-06-13 17:20:13 +02004962 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004963 polarssl_free( ssl->compress_buf );
4964 }
4965#endif
4966
Paul Bakker40e46942009-01-03 21:51:57 +00004967#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004968 mpi_free( &ssl->dhm_P );
4969 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004970#endif
4971
Paul Bakker48916f92012-09-16 19:57:18 +00004972 if( ssl->transform )
4973 {
4974 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004975 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004976 }
4977
4978 if( ssl->handshake )
4979 {
4980 ssl_handshake_free( ssl->handshake );
4981 ssl_transform_free( ssl->transform_negotiate );
4982 ssl_session_free( ssl->session_negotiate );
4983
Paul Bakker6e339b52013-07-03 13:37:05 +02004984 polarssl_free( ssl->handshake );
4985 polarssl_free( ssl->transform_negotiate );
4986 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004987 }
4988
Paul Bakkerc0463502013-02-14 11:19:38 +01004989 if( ssl->session )
4990 {
4991 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004992 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004993 }
4994
Paul Bakkera503a632013-08-14 13:48:06 +02004995#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004996 if( ssl->ticket_keys )
4997 {
4998 ssl_ticket_keys_free( ssl->ticket_keys );
4999 polarssl_free( ssl->ticket_keys );
5000 }
Paul Bakkera503a632013-08-14 13:48:06 +02005001#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005002
Paul Bakker0be444a2013-08-27 21:55:01 +02005003#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005004 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005005 {
Paul Bakker34617722014-06-13 17:20:13 +02005006 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005007 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005008 ssl->hostname_len = 0;
5009 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005010#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005011
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005012#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005013 if( ssl->psk != NULL )
5014 {
Paul Bakker34617722014-06-13 17:20:13 +02005015 polarssl_zeroize( ssl->psk, ssl->psk_len );
5016 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005017 polarssl_free( ssl->psk );
5018 polarssl_free( ssl->psk_identity );
5019 ssl->psk_len = 0;
5020 ssl->psk_identity_len = 0;
5021 }
5022#endif
5023
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005024#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005025 ssl_key_cert_free( ssl->key_cert );
5026#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005027
Paul Bakker05ef8352012-05-08 09:17:57 +00005028#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5029 if( ssl_hw_record_finish != NULL )
5030 {
5031 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5032 ssl_hw_record_finish( ssl );
5033 }
5034#endif
5035
Paul Bakker5121ce52009-01-03 21:22:43 +00005036 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005037
Paul Bakker86f04f42013-02-14 11:20:09 +01005038 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005039 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005040}
5041
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005042#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005043/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005044 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005045 */
5046unsigned char ssl_sig_from_pk( pk_context *pk )
5047{
5048#if defined(POLARSSL_RSA_C)
5049 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5050 return( SSL_SIG_RSA );
5051#endif
5052#if defined(POLARSSL_ECDSA_C)
5053 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5054 return( SSL_SIG_ECDSA );
5055#endif
5056 return( SSL_SIG_ANON );
5057}
5058
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005059pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5060{
5061 switch( sig )
5062 {
5063#if defined(POLARSSL_RSA_C)
5064 case SSL_SIG_RSA:
5065 return( POLARSSL_PK_RSA );
5066#endif
5067#if defined(POLARSSL_ECDSA_C)
5068 case SSL_SIG_ECDSA:
5069 return( POLARSSL_PK_ECDSA );
5070#endif
5071 default:
5072 return( POLARSSL_PK_NONE );
5073 }
5074}
Paul Bakker9af723c2014-05-01 13:03:14 +02005075#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005076
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005077/*
5078 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5079 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005080md_type_t ssl_md_alg_from_hash( unsigned char hash )
5081{
5082 switch( hash )
5083 {
5084#if defined(POLARSSL_MD5_C)
5085 case SSL_HASH_MD5:
5086 return( POLARSSL_MD_MD5 );
5087#endif
5088#if defined(POLARSSL_SHA1_C)
5089 case SSL_HASH_SHA1:
5090 return( POLARSSL_MD_SHA1 );
5091#endif
5092#if defined(POLARSSL_SHA256_C)
5093 case SSL_HASH_SHA224:
5094 return( POLARSSL_MD_SHA224 );
5095 case SSL_HASH_SHA256:
5096 return( POLARSSL_MD_SHA256 );
5097#endif
5098#if defined(POLARSSL_SHA512_C)
5099 case SSL_HASH_SHA384:
5100 return( POLARSSL_MD_SHA384 );
5101 case SSL_HASH_SHA512:
5102 return( POLARSSL_MD_SHA512 );
5103#endif
5104 default:
5105 return( POLARSSL_MD_NONE );
5106 }
5107}
5108
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005109#if defined(POLARSSL_SSL_SET_CURVES)
5110/*
5111 * Check is a curve proposed by the peer is in our list.
5112 * Return 1 if we're willing to use it, 0 otherwise.
5113 */
5114int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5115{
5116 const ecp_group_id *gid;
5117
5118 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5119 if( *gid == grp_id )
5120 return( 1 );
5121
5122 return( 0 );
5123}
Paul Bakker9af723c2014-05-01 13:03:14 +02005124#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005125
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005126#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005127int ssl_check_cert_usage( const x509_crt *cert,
5128 const ssl_ciphersuite_t *ciphersuite,
5129 int cert_endpoint )
5130{
5131#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5132 int usage = 0;
5133#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005134#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5135 const char *ext_oid;
5136 size_t ext_len;
5137#endif
5138
5139#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5140 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5141 ((void) cert);
5142 ((void) cert_endpoint);
5143#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005144
5145#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5146 if( cert_endpoint == SSL_IS_SERVER )
5147 {
5148 /* Server part of the key exchange */
5149 switch( ciphersuite->key_exchange )
5150 {
5151 case POLARSSL_KEY_EXCHANGE_RSA:
5152 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5153 usage = KU_KEY_ENCIPHERMENT;
5154 break;
5155
5156 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5157 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5158 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5159 usage = KU_DIGITAL_SIGNATURE;
5160 break;
5161
5162 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5163 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5164 usage = KU_KEY_AGREEMENT;
5165 break;
5166
5167 /* Don't use default: we want warnings when adding new values */
5168 case POLARSSL_KEY_EXCHANGE_NONE:
5169 case POLARSSL_KEY_EXCHANGE_PSK:
5170 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5171 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5172 usage = 0;
5173 }
5174 }
5175 else
5176 {
5177 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5178 usage = KU_DIGITAL_SIGNATURE;
5179 }
5180
5181 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5182 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005183#else
5184 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005185#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5186
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005187#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5188 if( cert_endpoint == SSL_IS_SERVER )
5189 {
5190 ext_oid = OID_SERVER_AUTH;
5191 ext_len = OID_SIZE( OID_SERVER_AUTH );
5192 }
5193 else
5194 {
5195 ext_oid = OID_CLIENT_AUTH;
5196 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5197 }
5198
5199 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5200 return( -1 );
5201#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5202
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005203 return( 0 );
5204}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005205#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005206
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005207/*
5208 * Convert version numbers to/from wire format
5209 * and, for DTLS, to/from TLS equivalent.
5210 *
5211 * For TLS this is the identity.
5212 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
5213 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5214 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5215 */
5216void ssl_write_version( int major, int minor, int transport,
5217 unsigned char ver[2] )
5218{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005219#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005220 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005221 {
5222 if( minor == SSL_MINOR_VERSION_2 )
5223 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5224
5225 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5226 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5227 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005228 else
5229#else
5230 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005231#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005232 {
5233 ver[0] = (unsigned char) major;
5234 ver[1] = (unsigned char) minor;
5235 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005236}
5237
5238void ssl_read_version( int *major, int *minor, int transport,
5239 const unsigned char ver[2] )
5240{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005241#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005242 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005243 {
5244 *major = 255 - ver[0] + 2;
5245 *minor = 255 - ver[1] + 1;
5246
5247 if( *minor == SSL_MINOR_VERSION_1 )
5248 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5249 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005250 else
5251#else
5252 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005253#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005254 {
5255 *major = ver[0];
5256 *minor = ver[1];
5257 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005258}
5259
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005260#endif /* POLARSSL_SSL_TLS_C */