2 * SSLv3/TLSv1 server-side functions
4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
6 * This file is part of mbed TLS (https://tls.mbed.org)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #if !defined(POLARSSL_CONFIG_FILE)
24 #include "polarssl/config.h"
26 #include POLARSSL_CONFIG_FILE
29 #if defined(POLARSSL_SSL_SRV_C)
31 #include "polarssl/debug.h"
32 #include "polarssl/ssl.h"
36 #if defined(POLARSSL_ECP_C)
37 #include "polarssl/ecp.h"
40 #if defined(POLARSSL_PLATFORM_C)
41 #include "polarssl/platform.h"
44 #define polarssl_malloc malloc
45 #define polarssl_free free
48 #if defined(POLARSSL_HAVE_TIME)
52 #if defined(POLARSSL_SSL_SESSION_TICKETS)
53 /* Implementation that should never be optimized out by the compiler */
54 static void polarssl_zeroize( void *v
, size_t n
) {
55 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
59 * Serialize a session in the following format:
60 * 0 . n-1 session structure, n = sizeof(ssl_session)
61 * n . n+2 peer_cert length = m (0 if no certificate)
62 * n+3 . n+2+m peer cert ASN.1
64 * Assumes ticket is NULL (always true on server side).
66 static int ssl_save_session( const ssl_session
*session
,
67 unsigned char *buf
, size_t buf_len
,
70 unsigned char *p
= buf
;
71 size_t left
= buf_len
;
72 #if defined(POLARSSL_X509_CRT_PARSE_C)
74 #endif /* POLARSSL_X509_CRT_PARSE_C */
76 if( left
< sizeof( ssl_session
) )
79 memcpy( p
, session
, sizeof( ssl_session
) );
80 p
+= sizeof( ssl_session
);
81 left
-= sizeof( ssl_session
);
83 #if defined(POLARSSL_X509_CRT_PARSE_C)
84 if( session
->peer_cert
== NULL
)
87 cert_len
= session
->peer_cert
->raw
.len
;
89 if( left
< 3 + cert_len
)
92 *p
++ = (unsigned char)( cert_len
>> 16 & 0xFF );
93 *p
++ = (unsigned char)( cert_len
>> 8 & 0xFF );
94 *p
++ = (unsigned char)( cert_len
& 0xFF );
96 if( session
->peer_cert
!= NULL
)
97 memcpy( p
, session
->peer_cert
->raw
.p
, cert_len
);
100 #endif /* POLARSSL_X509_CRT_PARSE_C */
108 * Unserialise session, see ssl_save_session()
110 static int ssl_load_session( ssl_session
*session
,
111 const unsigned char *buf
, size_t len
)
113 const unsigned char *p
= buf
;
114 const unsigned char * const end
= buf
+ len
;
115 #if defined(POLARSSL_X509_CRT_PARSE_C)
117 #endif /* POLARSSL_X509_CRT_PARSE_C */
119 if( p
+ sizeof( ssl_session
) > end
)
120 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA
);
122 memcpy( session
, p
, sizeof( ssl_session
) );
123 p
+= sizeof( ssl_session
);
125 #if defined(POLARSSL_X509_CRT_PARSE_C)
127 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA
);
129 cert_len
= ( p
[0] << 16 ) | ( p
[1] << 8 ) | p
[2];
134 session
->peer_cert
= NULL
;
140 if( p
+ cert_len
> end
)
141 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA
);
143 session
->peer_cert
= polarssl_malloc( sizeof( x509_crt
) );
145 if( session
->peer_cert
== NULL
)
146 return( POLARSSL_ERR_SSL_MALLOC_FAILED
);
148 x509_crt_init( session
->peer_cert
);
150 if( ( ret
= x509_crt_parse_der( session
->peer_cert
,
151 p
, cert_len
) ) != 0 )
153 x509_crt_free( session
->peer_cert
);
154 polarssl_free( session
->peer_cert
);
155 session
->peer_cert
= NULL
;
161 #endif /* POLARSSL_X509_CRT_PARSE_C */
164 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA
);
170 * Create session ticket, secured as recommended in RFC 5077 section 4:
173 * opaque key_name[16];
175 * opaque encrypted_state<0..2^16-1>;
179 * (the internal state structure differs, however).
181 static int ssl_write_ticket( ssl_context
*ssl
, size_t *tlen
)
184 unsigned char * const start
= ssl
->out_msg
+ 10;
185 unsigned char *p
= start
;
186 unsigned char *state
;
187 unsigned char iv
[16];
188 size_t clear_len
, enc_len
, pad_len
, i
;
192 if( ssl
->ticket_keys
== NULL
)
193 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA
);
196 memcpy( p
, ssl
->ticket_keys
->key_name
, 16 );
199 /* Generate and write IV (with a copy for aes_crypt) */
200 if( ( ret
= ssl
->f_rng( ssl
->p_rng
, p
, 16 ) ) != 0 )
208 * After the session state itself, we still need room for 16 bytes of
209 * padding and 32 bytes of MAC, so there's only so much room left
212 if( ssl_save_session( ssl
->session_negotiate
, state
,
213 SSL_MAX_CONTENT_LEN
- ( state
- ssl
->out_ctr
) - 48,
216 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE
);
218 SSL_DEBUG_BUF( 3, "session ticket cleartext", state
, clear_len
);
220 /* Apply PKCS padding */
221 pad_len
= 16 - clear_len
% 16;
222 enc_len
= clear_len
+ pad_len
;
223 for( i
= clear_len
; i
< enc_len
; i
++ )
224 state
[i
] = (unsigned char) pad_len
;
227 if( ( ret
= aes_crypt_cbc( &ssl
->ticket_keys
->enc
, AES_ENCRYPT
,
228 enc_len
, iv
, state
, state
) ) != 0 )
234 *p
++ = (unsigned char)( ( enc_len
>> 8 ) & 0xFF );
235 *p
++ = (unsigned char)( ( enc_len
) & 0xFF );
238 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
239 sha256_hmac( ssl
->ticket_keys
->mac_key
, 16, start
, p
- start
, p
, 0 );
244 SSL_DEBUG_BUF( 3, "session ticket structure", start
, *tlen
);
250 * Load session ticket (see ssl_write_ticket for structure)
252 static int ssl_parse_ticket( ssl_context
*ssl
,
258 unsigned char *key_name
= buf
;
259 unsigned char *iv
= buf
+ 16;
260 unsigned char *enc_len_p
= iv
+ 16;
261 unsigned char *ticket
= enc_len_p
+ 2;
263 unsigned char computed_mac
[32];
264 size_t enc_len
, clear_len
, i
;
265 unsigned char pad_len
, diff
;
267 SSL_DEBUG_BUF( 3, "session ticket structure", buf
, len
);
269 if( len
< 34 || ssl
->ticket_keys
== NULL
)
270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA
);
272 enc_len
= ( enc_len_p
[0] << 8 ) | enc_len_p
[1];
273 mac
= ticket
+ enc_len
;
275 if( len
!= enc_len
+ 66 )
276 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA
);
278 /* Check name, in constant time though it's not a big secret */
280 for( i
= 0; i
< 16; i
++ )
281 diff
|= key_name
[i
] ^ ssl
->ticket_keys
->key_name
[i
];
282 /* don't return yet, check the MAC anyway */
284 /* Check mac, with constant-time buffer comparison */
285 sha256_hmac( ssl
->ticket_keys
->mac_key
, 16, buf
, len
- 32,
288 for( i
= 0; i
< 32; i
++ )
289 diff
|= mac
[i
] ^ computed_mac
[i
];
291 /* Now return if ticket is not authentic, since we want to avoid
292 * decrypting arbitrary attacker-chosen data */
294 return( POLARSSL_ERR_SSL_INVALID_MAC
);
297 if( ( ret
= aes_crypt_cbc( &ssl
->ticket_keys
->dec
, AES_DECRYPT
,
298 enc_len
, iv
, ticket
, ticket
) ) != 0 )
303 /* Check PKCS padding */
304 pad_len
= ticket
[enc_len
- 1];
307 for( i
= 2; i
< pad_len
; i
++ )
308 if( ticket
[enc_len
- i
] != pad_len
)
309 ret
= POLARSSL_ERR_SSL_BAD_INPUT_DATA
;
313 clear_len
= enc_len
- pad_len
;
315 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket
, clear_len
);
317 /* Actually load session */
318 if( ( ret
= ssl_load_session( &session
, ticket
, clear_len
) ) != 0 )
320 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
321 ssl_session_free( &session
);
325 #if defined(POLARSSL_HAVE_TIME)
326 /* Check if still valid */
327 if( (int) ( time( NULL
) - session
.start
) > ssl
->ticket_lifetime
)
329 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
330 ssl_session_free( &session
);
331 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED
);
336 * Keep the session ID sent by the client, since we MUST send it back to
337 * inform him we're accepting the ticket (RFC 5077 section 3.4)
339 session
.length
= ssl
->session_negotiate
->length
;
340 memcpy( &session
.id
, ssl
->session_negotiate
->id
, session
.length
);
342 ssl_session_free( ssl
->session_negotiate
);
343 memcpy( ssl
->session_negotiate
, &session
, sizeof( ssl_session
) );
345 /* Zeroize instead of free as we copied the content */
346 polarssl_zeroize( &session
, sizeof( ssl_session
) );
350 #endif /* POLARSSL_SSL_SESSION_TICKETS */
352 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
354 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
355 * making it act on ssl->hanshake->sni_key_cert instead.
357 static int ssl_sni_wrapper( ssl_context
*ssl
,
358 const unsigned char* name
, size_t len
)
361 ssl_key_cert
*key_cert_ori
= ssl
->key_cert
;
363 ssl
->key_cert
= NULL
;
364 ret
= ssl
->f_sni( ssl
->p_sni
, ssl
, name
, len
);
365 ssl
->handshake
->sni_key_cert
= ssl
->key_cert
;
367 ssl
->key_cert
= key_cert_ori
;
372 static int ssl_parse_servername_ext( ssl_context
*ssl
,
373 const unsigned char *buf
,
377 size_t servername_list_size
, hostname_len
;
378 const unsigned char *p
;
380 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
382 servername_list_size
= ( ( buf
[0] << 8 ) | ( buf
[1] ) );
383 if( servername_list_size
+ 2 != len
)
385 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
386 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
390 while( servername_list_size
> 0 )
392 hostname_len
= ( ( p
[1] << 8 ) | p
[2] );
393 if( hostname_len
+ 3 > servername_list_size
)
395 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
396 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
399 if( p
[0] == TLS_EXT_SERVERNAME_HOSTNAME
)
401 ret
= ssl_sni_wrapper( ssl
, p
+ 3, hostname_len
);
404 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret
);
405 ssl_send_alert_message( ssl
, SSL_ALERT_LEVEL_FATAL
,
406 SSL_ALERT_MSG_UNRECOGNIZED_NAME
);
407 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
412 servername_list_size
-= hostname_len
+ 3;
413 p
+= hostname_len
+ 3;
416 if( servername_list_size
!= 0 )
418 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
419 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
424 #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
426 static int ssl_parse_renegotiation_info( ssl_context
*ssl
,
427 const unsigned char *buf
,
432 #if defined(POLARSSL_SSL_RENEGOTIATION)
433 if( ssl
->renegotiation
!= SSL_INITIAL_HANDSHAKE
)
435 /* Check verify-data in constant-time. The length OTOH is no secret */
436 if( len
!= 1 + ssl
->verify_data_len
||
437 buf
[0] != ssl
->verify_data_len
||
438 safer_memcmp( buf
+ 1, ssl
->peer_verify_data
,
439 ssl
->verify_data_len
) != 0 )
441 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
443 if( ( ret
= ssl_send_fatal_handshake_failure( ssl
) ) != 0 )
446 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
450 #endif /* POLARSSL_SSL_RENEGOTIATION */
452 if( len
!= 1 || buf
[0] != 0x0 )
454 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
456 if( ( ret
= ssl_send_fatal_handshake_failure( ssl
) ) != 0 )
459 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
462 ssl
->secure_renegotiation
= SSL_SECURE_RENEGOTIATION
;
468 #if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
469 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
470 static int ssl_parse_signature_algorithms_ext( ssl_context
*ssl
,
471 const unsigned char *buf
,
474 size_t sig_alg_list_size
;
475 const unsigned char *p
;
476 const unsigned char *end
= buf
+ len
;
480 sig_alg_list_size
= ( ( buf
[0] << 8 ) | ( buf
[1] ) );
481 if( sig_alg_list_size
+ 2 != len
||
482 sig_alg_list_size
% 2 != 0 )
484 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
485 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
489 * For now, ignore the SignatureAlgorithm part and rely on offered
490 * ciphersuites only for that part. To be fixed later.
492 * So, just look at the HashAlgorithm part.
494 for( md_cur
= md_list(); *md_cur
!= POLARSSL_MD_NONE
; md_cur
++ ) {
495 for( p
= buf
+ 2; p
< end
; p
+= 2 ) {
496 if( *md_cur
== (int) ssl_md_alg_from_hash( p
[0] ) ) {
497 ssl
->handshake
->sig_alg
= p
[0];
503 /* Some key echanges do not need signatures at all */
504 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
508 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
509 ssl
->handshake
->sig_alg
) );
513 #endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
514 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
516 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
517 static int ssl_parse_supported_elliptic_curves( ssl_context
*ssl
,
518 const unsigned char *buf
,
521 size_t list_size
, our_size
;
522 const unsigned char *p
;
523 const ecp_curve_info
*curve_info
, **curves
;
525 list_size
= ( ( buf
[0] << 8 ) | ( buf
[1] ) );
526 if( list_size
+ 2 != len
||
529 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
530 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
533 /* Should never happen unless client duplicates the extension */
534 if( ssl
->handshake
->curves
!= NULL
)
536 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
537 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
540 /* Don't allow our peer to make us allocate too much memory,
541 * and leave room for a final 0 */
542 our_size
= list_size
/ 2 + 1;
543 if( our_size
> POLARSSL_ECP_DP_MAX
)
544 our_size
= POLARSSL_ECP_DP_MAX
;
546 if( ( curves
= polarssl_malloc( our_size
* sizeof( *curves
) ) ) == NULL
)
547 return( POLARSSL_ERR_SSL_MALLOC_FAILED
);
549 /* explicit void pointer cast for buggy MS compiler */
550 memset( (void *) curves
, 0, our_size
* sizeof( *curves
) );
551 ssl
->handshake
->curves
= curves
;
554 while( list_size
> 0 && our_size
> 1 )
556 curve_info
= ecp_curve_info_from_tls_id( ( p
[0] << 8 ) | p
[1] );
558 if( curve_info
!= NULL
)
560 *curves
++ = curve_info
;
571 static int ssl_parse_supported_point_formats( ssl_context
*ssl
,
572 const unsigned char *buf
,
576 const unsigned char *p
;
579 if( list_size
+ 1 != len
)
581 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
582 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
586 while( list_size
> 0 )
588 if( p
[0] == POLARSSL_ECP_PF_UNCOMPRESSED
||
589 p
[0] == POLARSSL_ECP_PF_COMPRESSED
)
591 ssl
->handshake
->ecdh_ctx
.point_format
= p
[0];
592 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p
[0] ) );
602 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
604 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
605 static int ssl_parse_max_fragment_length_ext( ssl_context
*ssl
,
606 const unsigned char *buf
,
609 if( len
!= 1 || buf
[0] >= SSL_MAX_FRAG_LEN_INVALID
)
611 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
612 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
615 ssl
->session_negotiate
->mfl_code
= buf
[0];
619 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
621 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
622 static int ssl_parse_truncated_hmac_ext( ssl_context
*ssl
,
623 const unsigned char *buf
,
628 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
629 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
634 if( ssl
->trunc_hmac
== SSL_TRUNC_HMAC_ENABLED
)
635 ssl
->session_negotiate
->trunc_hmac
= SSL_TRUNC_HMAC_ENABLED
;
639 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
641 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
642 static int ssl_parse_encrypt_then_mac_ext( ssl_context
*ssl
,
643 const unsigned char *buf
,
648 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
649 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
654 if( ssl
->encrypt_then_mac
== SSL_ETM_ENABLED
&&
655 ssl
->minor_ver
!= SSL_MINOR_VERSION_0
)
657 ssl
->session_negotiate
->encrypt_then_mac
= SSL_ETM_ENABLED
;
662 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
664 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
665 static int ssl_parse_extended_ms_ext( ssl_context
*ssl
,
666 const unsigned char *buf
,
671 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
672 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
677 if( ssl
->extended_ms
== SSL_EXTENDED_MS_ENABLED
&&
678 ssl
->minor_ver
!= SSL_MINOR_VERSION_0
)
680 ssl
->handshake
->extended_ms
= SSL_EXTENDED_MS_ENABLED
;
685 #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
687 #if defined(POLARSSL_SSL_SESSION_TICKETS)
688 static int ssl_parse_session_ticket_ext( ssl_context
*ssl
,
694 if( ssl
->session_tickets
== SSL_SESSION_TICKETS_DISABLED
)
697 /* Remember the client asked us to send a new ticket */
698 ssl
->handshake
->new_session_ticket
= 1;
700 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len
) );
705 #if defined(POLARSSL_SSL_RENEGOTIATION)
706 if( ssl
->renegotiation
!= SSL_INITIAL_HANDSHAKE
)
708 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
711 #endif /* POLARSSL_SSL_RENEGOTIATION */
714 * Failures are ok: just ignore the ticket and proceed.
716 if( ( ret
= ssl_parse_ticket( ssl
, buf
, len
) ) != 0 )
718 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret
);
722 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
724 ssl
->handshake
->resume
= 1;
726 /* Don't send a new ticket after all, this one is OK */
727 ssl
->handshake
->new_session_ticket
= 0;
731 #endif /* POLARSSL_SSL_SESSION_TICKETS */
733 #if defined(POLARSSL_SSL_ALPN)
734 static int ssl_parse_alpn_ext( ssl_context
*ssl
,
735 const unsigned char *buf
, size_t len
)
737 size_t list_len
, cur_len
, ours_len
;
738 const unsigned char *theirs
, *start
, *end
;
741 /* If ALPN not configured, just ignore the extension */
742 if( ssl
->alpn_list
== NULL
)
746 * opaque ProtocolName<1..2^8-1>;
749 * ProtocolName protocol_name_list<2..2^16-1>
750 * } ProtocolNameList;
753 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
755 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
757 list_len
= ( buf
[0] << 8 ) | buf
[1];
758 if( list_len
!= len
- 2 )
759 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
762 * Use our order of preference
766 for( ours
= ssl
->alpn_list
; *ours
!= NULL
; ours
++ )
768 ours_len
= strlen( *ours
);
769 for( theirs
= start
; theirs
!= end
; theirs
+= cur_len
)
771 /* If the list is well formed, we should get equality first */
773 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
777 /* Empty strings MUST NOT be included */
779 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
781 if( cur_len
== ours_len
&&
782 memcmp( theirs
, *ours
, cur_len
) == 0 )
784 ssl
->alpn_chosen
= *ours
;
790 /* If we get there, no match was found */
791 ssl_send_alert_message( ssl
, SSL_ALERT_LEVEL_FATAL
,
792 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL
);
793 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
795 #endif /* POLARSSL_SSL_ALPN */
798 * Auxiliary functions for ServerHello parsing and related actions
801 #if defined(POLARSSL_X509_CRT_PARSE_C)
803 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
805 #if defined(POLARSSL_ECDSA_C)
806 static int ssl_check_key_curve( pk_context
*pk
,
807 const ecp_curve_info
**curves
)
809 const ecp_curve_info
**crv
= curves
;
810 ecp_group_id grp_id
= pk_ec( *pk
)->grp
.id
;
812 while( *crv
!= NULL
)
814 if( (*crv
)->grp_id
== grp_id
)
821 #endif /* POLARSSL_ECDSA_C */
824 * Try picking a certificate for this ciphersuite,
825 * return 0 on success and -1 on failure.
827 static int ssl_pick_cert( ssl_context
*ssl
,
828 const ssl_ciphersuite_t
* ciphersuite_info
)
830 ssl_key_cert
*cur
, *list
, *fallback
= NULL
;
831 pk_type_t pk_alg
= ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info
);
834 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
835 if( ssl
->handshake
->sni_key_cert
!= NULL
)
836 list
= ssl
->handshake
->sni_key_cert
;
839 list
= ssl
->handshake
->key_cert
;
841 if( pk_alg
== POLARSSL_PK_NONE
)
844 SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
846 for( cur
= list
; cur
!= NULL
; cur
= cur
->next
)
848 SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
851 if( ! pk_can_do( cur
->key
, pk_alg
) )
853 SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
858 * This avoids sending the client a cert it'll reject based on
859 * keyUsage or other extensions.
861 * It also allows the user to provision different certificates for
862 * different uses based on keyUsage, eg if they want to avoid signing
863 * and decrypting with the same RSA key.
865 if( ssl_check_cert_usage( cur
->cert
, ciphersuite_info
,
866 SSL_IS_SERVER
, &flags
) != 0 )
868 SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
869 "(extended) key usage extension" ) );
873 #if defined(POLARSSL_ECDSA_C)
874 if( pk_alg
== POLARSSL_PK_ECDSA
&&
875 ssl_check_key_curve( cur
->key
, ssl
->handshake
->curves
) != 0 )
877 SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
883 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
884 * present them a SHA-higher cert rather than failing if it's the only
885 * one we got that satisfies the other conditions.
887 if( ssl
->minor_ver
< SSL_MINOR_VERSION_3
&&
888 cur
->cert
->sig_md
!= POLARSSL_MD_SHA1
)
890 if( fallback
== NULL
)
893 SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
894 "sha-2 with pre-TLS 1.2 client" ) );
899 /* If we get there, we got a winner */
907 /* Do not update ssl->handshake->key_cert unless the is a match */
910 ssl
->handshake
->key_cert
= cur
;
911 SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
912 ssl
->handshake
->key_cert
->cert
);
918 #endif /* POLARSSL_X509_CRT_PARSE_C */
921 * Check if a given ciphersuite is suitable for use with our config/keys/etc
922 * Sets ciphersuite_info only if the suite matches.
924 static int ssl_ciphersuite_match( ssl_context
*ssl
, int suite_id
,
925 const ssl_ciphersuite_t
**ciphersuite_info
)
927 const ssl_ciphersuite_t
*suite_info
;
929 suite_info
= ssl_ciphersuite_from_id( suite_id
);
930 if( suite_info
== NULL
)
932 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
933 return( POLARSSL_ERR_SSL_INTERNAL_ERROR
);
936 SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info
->name
) );
938 if( suite_info
->min_minor_ver
> ssl
->minor_ver
||
939 suite_info
->max_minor_ver
< ssl
->minor_ver
)
941 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
945 if( ssl
->arc4_disabled
== SSL_ARC4_DISABLED
&&
946 suite_info
->cipher
== POLARSSL_CIPHER_ARC4_128
)
948 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
952 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
953 if( ssl_ciphersuite_uses_ec( suite_info
) &&
954 ( ssl
->handshake
->curves
== NULL
||
955 ssl
->handshake
->curves
[0] == NULL
) )
957 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
958 "no common elliptic curve" ) );
963 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
964 /* If the ciphersuite requires a pre-shared key and we don't
965 * have one, skip it now rather than failing later */
966 if( ssl_ciphersuite_uses_psk( suite_info
) &&
967 ssl
->f_psk
== NULL
&&
968 ( ssl
->psk
== NULL
|| ssl
->psk_identity
== NULL
||
969 ssl
->psk_identity_len
== 0 || ssl
->psk_len
== 0 ) )
971 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
976 #if defined(POLARSSL_X509_CRT_PARSE_C)
978 * Final check: if ciphersuite requires us to have a
979 * certificate/key of a particular type:
980 * - select the appropriate certificate if we have one, or
981 * - try the next ciphersuite if we don't
982 * This must be done last since we modify the key_cert list.
984 if( ssl_pick_cert( ssl
, suite_info
) != 0 )
986 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
987 "no suitable certificate" ) );
992 *ciphersuite_info
= suite_info
;
996 #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
997 static int ssl_parse_client_hello_v2( ssl_context
*ssl
)
999 int ret
, got_common_suite
;
1002 unsigned int ciph_len
, sess_len
, chal_len
;
1003 unsigned char *buf
, *p
;
1004 const int *ciphersuites
;
1005 const ssl_ciphersuite_t
*ciphersuite_info
;
1007 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1009 #if defined(POLARSSL_SSL_RENEGOTIATION)
1010 if( ssl
->renegotiation
!= SSL_INITIAL_HANDSHAKE
)
1012 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1014 if( ( ret
= ssl_send_fatal_handshake_failure( ssl
) ) != 0 )
1017 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1019 #endif /* POLARSSL_SSL_RENEGOTIATION */
1023 SSL_DEBUG_BUF( 4, "record header", buf
, 5 );
1025 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1027 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1028 ( ( buf
[0] & 0x7F ) << 8 ) | buf
[1] ) );
1029 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1033 * SSLv2 Client Hello
1036 * 0 . 1 message length
1039 * 2 . 2 message type
1040 * 3 . 4 protocol version
1042 if( buf
[2] != SSL_HS_CLIENT_HELLO
||
1043 buf
[3] != SSL_MAJOR_VERSION_3
)
1045 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1046 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1049 n
= ( ( buf
[0] << 8 ) | buf
[1] ) & 0x7FFF;
1051 if( n
< 17 || n
> 512 )
1053 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1054 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1057 ssl
->major_ver
= SSL_MAJOR_VERSION_3
;
1058 ssl
->minor_ver
= ( buf
[4] <= ssl
->max_minor_ver
)
1059 ? buf
[4] : ssl
->max_minor_ver
;
1061 if( ssl
->minor_ver
< ssl
->min_minor_ver
)
1063 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1064 " [%d:%d] < [%d:%d]",
1065 ssl
->major_ver
, ssl
->minor_ver
,
1066 ssl
->min_major_ver
, ssl
->min_minor_ver
) );
1068 ssl_send_alert_message( ssl
, SSL_ALERT_LEVEL_FATAL
,
1069 SSL_ALERT_MSG_PROTOCOL_VERSION
);
1070 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION
);
1073 ssl
->handshake
->max_major_ver
= buf
[3];
1074 ssl
->handshake
->max_minor_ver
= buf
[4];
1076 if( ( ret
= ssl_fetch_input( ssl
, 2 + n
) ) != 0 )
1078 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret
);
1082 ssl
->handshake
->update_checksum( ssl
, buf
+ 2, n
);
1085 n
= ssl
->in_left
- 5;
1088 * 0 . 1 ciphersuitelist length
1089 * 2 . 3 session id length
1090 * 4 . 5 challenge length
1091 * 6 . .. ciphersuitelist
1092 * .. . .. session id
1095 SSL_DEBUG_BUF( 4, "record contents", buf
, n
);
1097 ciph_len
= ( buf
[0] << 8 ) | buf
[1];
1098 sess_len
= ( buf
[2] << 8 ) | buf
[3];
1099 chal_len
= ( buf
[4] << 8 ) | buf
[5];
1101 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1102 ciph_len
, sess_len
, chal_len
) );
1105 * Make sure each parameter length is valid
1107 if( ciph_len
< 3 || ( ciph_len
% 3 ) != 0 )
1109 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1110 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1115 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1116 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1119 if( chal_len
< 8 || chal_len
> 32 )
1121 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1122 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1125 if( n
!= 6 + ciph_len
+ sess_len
+ chal_len
)
1127 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1128 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1131 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1132 buf
+ 6, ciph_len
);
1133 SSL_DEBUG_BUF( 3, "client hello, session id",
1134 buf
+ 6 + ciph_len
, sess_len
);
1135 SSL_DEBUG_BUF( 3, "client hello, challenge",
1136 buf
+ 6 + ciph_len
+ sess_len
, chal_len
);
1138 p
= buf
+ 6 + ciph_len
;
1139 ssl
->session_negotiate
->length
= sess_len
;
1140 memset( ssl
->session_negotiate
->id
, 0,
1141 sizeof( ssl
->session_negotiate
->id
) );
1142 memcpy( ssl
->session_negotiate
->id
, p
, ssl
->session_negotiate
->length
);
1145 memset( ssl
->handshake
->randbytes
, 0, 64 );
1146 memcpy( ssl
->handshake
->randbytes
+ 32 - chal_len
, p
, chal_len
);
1149 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1151 for( i
= 0, p
= buf
+ 6; i
< ciph_len
; i
+= 3, p
+= 3 )
1153 if( p
[0] == 0 && p
[1] == 0 && p
[2] == SSL_EMPTY_RENEGOTIATION_INFO
)
1155 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1156 #if defined(POLARSSL_SSL_RENEGOTIATION)
1157 if( ssl
->renegotiation
== SSL_RENEGOTIATION
)
1159 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1160 "during renegotiation" ) );
1162 if( ( ret
= ssl_send_fatal_handshake_failure( ssl
) ) != 0 )
1165 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1167 #endif /* POLARSSL_SSL_RENEGOTIATION */
1168 ssl
->secure_renegotiation
= SSL_SECURE_RENEGOTIATION
;
1173 #if defined(POLARSSL_SSL_FALLBACK_SCSV)
1174 for( i
= 0, p
= buf
+ 6; i
< ciph_len
; i
+= 3, p
+= 3 )
1177 p
[1] == (unsigned char)( ( SSL_FALLBACK_SCSV
>> 8 ) & 0xff ) &&
1178 p
[2] == (unsigned char)( ( SSL_FALLBACK_SCSV
) & 0xff ) )
1180 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1182 if( ssl
->minor_ver
< ssl
->max_minor_ver
)
1184 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1186 ssl_send_alert_message( ssl
, SSL_ALERT_LEVEL_FATAL
,
1187 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK
);
1189 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1195 #endif /* POLARSSL_SSL_FALLBACK_SCSV */
1197 got_common_suite
= 0;
1198 ciphersuites
= ssl
->ciphersuite_list
[ssl
->minor_ver
];
1199 ciphersuite_info
= NULL
;
1200 #if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1201 for( j
= 0, p
= buf
+ 6; j
< ciph_len
; j
+= 3, p
+= 3 )
1203 for( i
= 0; ciphersuites
[i
] != 0; i
++ )
1205 for( i
= 0; ciphersuites
[i
] != 0; i
++ )
1207 for( j
= 0, p
= buf
+ 6; j
< ciph_len
; j
+= 3, p
+= 3 )
1211 p
[1] != ( ( ciphersuites
[i
] >> 8 ) & 0xFF ) ||
1212 p
[2] != ( ( ciphersuites
[i
] ) & 0xFF ) )
1215 got_common_suite
= 1;
1217 if( ( ret
= ssl_ciphersuite_match( ssl
, ciphersuites
[i
],
1218 &ciphersuite_info
) ) != 0 )
1221 if( ciphersuite_info
!= NULL
)
1222 goto have_ciphersuite_v2
;
1226 if( got_common_suite
)
1228 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1229 "but none of them usable" ) );
1230 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE
);
1234 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1235 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN
);
1238 have_ciphersuite_v2
:
1239 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info
->name
) );
1241 ssl
->session_negotiate
->ciphersuite
= ciphersuites
[i
];
1242 ssl
->transform_negotiate
->ciphersuite_info
= ciphersuite_info
;
1243 ssl_optimize_checksum( ssl
, ssl
->transform_negotiate
->ciphersuite_info
);
1246 * SSLv2 Client Hello relevant renegotiation security checks
1248 if( ssl
->secure_renegotiation
== SSL_LEGACY_RENEGOTIATION
&&
1249 ssl
->allow_legacy_renegotiation
== SSL_LEGACY_BREAK_HANDSHAKE
)
1251 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1253 if( ( ret
= ssl_send_fatal_handshake_failure( ssl
) ) != 0 )
1256 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1262 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1266 #endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1268 static int ssl_parse_client_hello( ssl_context
*ssl
)
1270 int ret
, got_common_suite
;
1273 unsigned int ciph_len
, sess_len
;
1274 unsigned int comp_len
;
1275 unsigned int ext_len
= 0;
1276 unsigned char *buf
, *p
, *ext
;
1277 #if defined(POLARSSL_SSL_RENEGOTIATION)
1278 int renegotiation_info_seen
= 0;
1280 int handshake_failure
= 0;
1281 const int *ciphersuites
;
1282 const ssl_ciphersuite_t
*ciphersuite_info
;
1284 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1286 #if defined(POLARSSL_SSL_RENEGOTIATION)
1287 if( ssl
->renegotiation
== SSL_INITIAL_HANDSHAKE
)
1290 if( ( ret
= ssl_fetch_input( ssl
, 5 ) ) != 0 )
1292 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret
);
1299 #if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1300 if( ( buf
[0] & 0x80 ) != 0 )
1301 return ssl_parse_client_hello_v2( ssl
);
1304 SSL_DEBUG_BUF( 4, "record header", buf
, 5 );
1306 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1308 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1309 ( buf
[3] << 8 ) | buf
[4] ) );
1310 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1314 * SSLv3/TLS Client Hello
1317 * 0 . 0 message type
1318 * 1 . 2 protocol version
1319 * 3 . 4 message length
1322 /* According to RFC 5246 Appendix E.1, the version here is typically
1323 * "{03,00}, the lowest version number supported by the client, [or] the
1324 * value of ClientHello.client_version", so the only meaningful check here
1325 * is the major version shouldn't be less than 3 */
1326 if( buf
[0] != SSL_MSG_HANDSHAKE
||
1327 buf
[1] < SSL_MAJOR_VERSION_3
)
1329 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1330 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1333 n
= ( buf
[3] << 8 ) | buf
[4];
1335 if( n
< 45 || n
> SSL_MAX_CONTENT_LEN
)
1337 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1338 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1341 #if defined(POLARSSL_SSL_RENEGOTIATION)
1342 if( ssl
->renegotiation
== SSL_INITIAL_HANDSHAKE
)
1345 if( ( ret
= ssl_fetch_input( ssl
, 5 + n
) ) != 0 )
1347 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret
);
1353 #if defined(POLARSSL_SSL_RENEGOTIATION)
1354 if( ssl
->renegotiation
!= SSL_INITIAL_HANDSHAKE
)
1358 n
= ssl
->in_left
- 5;
1360 ssl
->handshake
->update_checksum( ssl
, buf
, n
);
1364 * 0 . 0 handshake type
1365 * 1 . 3 handshake length
1366 * 4 . 5 protocol version
1368 * 10 . 37 random bytes
1369 * 38 . 38 session id length
1370 * 39 . 38+x session id
1371 * 39+x . 40+x ciphersuitelist length
1372 * 41+x . 40+y ciphersuitelist
1373 * 41+y . 41+y compression alg length
1374 * 42+y . 41+z compression algs
1375 * .. . .. extensions
1377 SSL_DEBUG_BUF( 4, "record contents", buf
, n
);
1379 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1381 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1382 ( buf
[1] << 16 ) | ( buf
[2] << 8 ) | buf
[3] ) );
1383 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1387 * Check the handshake type and protocol version
1389 if( buf
[0] != SSL_HS_CLIENT_HELLO
)
1391 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1392 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1395 ssl
->major_ver
= buf
[4];
1396 ssl
->minor_ver
= buf
[5];
1398 ssl
->handshake
->max_major_ver
= ssl
->major_ver
;
1399 ssl
->handshake
->max_minor_ver
= ssl
->minor_ver
;
1401 if( ssl
->major_ver
< ssl
->min_major_ver
||
1402 ssl
->minor_ver
< ssl
->min_minor_ver
)
1404 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1405 " [%d:%d] < [%d:%d]",
1406 ssl
->major_ver
, ssl
->minor_ver
,
1407 ssl
->min_major_ver
, ssl
->min_minor_ver
) );
1409 ssl_send_alert_message( ssl
, SSL_ALERT_LEVEL_FATAL
,
1410 SSL_ALERT_MSG_PROTOCOL_VERSION
);
1412 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION
);
1415 if( ssl
->major_ver
> ssl
->max_major_ver
)
1417 ssl
->major_ver
= ssl
->max_major_ver
;
1418 ssl
->minor_ver
= ssl
->max_minor_ver
;
1420 else if( ssl
->minor_ver
> ssl
->max_minor_ver
)
1421 ssl
->minor_ver
= ssl
->max_minor_ver
;
1423 memcpy( ssl
->handshake
->randbytes
, buf
+ 6, 32 );
1426 * Check the handshake message length
1428 if( buf
[1] != 0 || n
!= (unsigned int) 4 + ( ( buf
[2] << 8 ) | buf
[3] ) )
1430 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1431 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1435 * Check the session length
1439 if( sess_len
> 32 || sess_len
> n
- 42 )
1441 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1442 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1445 ssl
->session_negotiate
->length
= sess_len
;
1446 memset( ssl
->session_negotiate
->id
, 0,
1447 sizeof( ssl
->session_negotiate
->id
) );
1448 memcpy( ssl
->session_negotiate
->id
, buf
+ 39,
1449 ssl
->session_negotiate
->length
);
1452 * Check the ciphersuitelist length
1454 ciph_len
= ( buf
[39 + sess_len
] << 8 )
1455 | ( buf
[40 + sess_len
] );
1457 if( ciph_len
< 2 || ( ciph_len
% 2 ) != 0 || ciph_len
> n
- 42 - sess_len
)
1459 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1460 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1464 * Check the compression algorithms length
1466 comp_len
= buf
[41 + sess_len
+ ciph_len
];
1468 if( comp_len
< 1 || comp_len
> 16 ||
1469 comp_len
> n
- 42 - sess_len
- ciph_len
)
1471 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1472 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1476 * Check the extension length
1478 if( n
> 42 + sess_len
+ ciph_len
+ comp_len
)
1480 ext_len
= ( buf
[42 + sess_len
+ ciph_len
+ comp_len
] << 8 )
1481 | ( buf
[43 + sess_len
+ ciph_len
+ comp_len
] );
1483 if( ( ext_len
> 0 && ext_len
< 4 ) ||
1484 n
!= 44 + sess_len
+ ciph_len
+ comp_len
+ ext_len
)
1486 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1487 SSL_DEBUG_BUF( 3, "Ext", buf
+ 44 + sess_len
+ ciph_len
+ comp_len
, ext_len
);
1488 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1492 ssl
->session_negotiate
->compression
= SSL_COMPRESS_NULL
;
1493 #if defined(POLARSSL_ZLIB_SUPPORT)
1494 for( i
= 0; i
< comp_len
; ++i
)
1496 if( buf
[42 + sess_len
+ ciph_len
+ i
] == SSL_COMPRESS_DEFLATE
)
1498 ssl
->session_negotiate
->compression
= SSL_COMPRESS_DEFLATE
;
1504 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1506 SSL_DEBUG_BUF( 3, "client hello, session id",
1507 buf
+ 38, sess_len
);
1508 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1509 buf
+ 41 + sess_len
, ciph_len
);
1510 SSL_DEBUG_BUF( 3, "client hello, compression",
1511 buf
+ 42 + sess_len
+ ciph_len
, comp_len
);
1514 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1516 for( i
= 0, p
= buf
+ 41 + sess_len
; i
< ciph_len
; i
+= 2, p
+= 2 )
1518 if( p
[0] == 0 && p
[1] == SSL_EMPTY_RENEGOTIATION_INFO
)
1520 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1521 #if defined(POLARSSL_SSL_RENEGOTIATION)
1522 if( ssl
->renegotiation
== SSL_RENEGOTIATION
)
1524 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1526 if( ( ret
= ssl_send_fatal_handshake_failure( ssl
) ) != 0 )
1529 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1531 renegotiation_info_seen
= 1;
1532 #endif /* POLARSSL_SSL_RENEGOTIATION */
1533 ssl
->secure_renegotiation
= SSL_SECURE_RENEGOTIATION
;
1538 #if defined(POLARSSL_SSL_FALLBACK_SCSV)
1539 for( i
= 0, p
= buf
+ 41 + sess_len
; i
< ciph_len
; i
+= 2, p
+= 2 )
1541 if( p
[0] == (unsigned char)( ( SSL_FALLBACK_SCSV
>> 8 ) & 0xff ) &&
1542 p
[1] == (unsigned char)( ( SSL_FALLBACK_SCSV
) & 0xff ) )
1544 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1546 if( ssl
->minor_ver
< ssl
->max_minor_ver
)
1548 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1550 ssl_send_alert_message( ssl
, SSL_ALERT_LEVEL_FATAL
,
1551 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK
);
1553 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1559 #endif /* POLARSSL_SSL_FALLBACK_SCSV */
1561 ext
= buf
+ 44 + sess_len
+ ciph_len
+ comp_len
;
1565 unsigned int ext_id
= ( ( ext
[0] << 8 )
1567 unsigned int ext_size
= ( ( ext
[2] << 8 )
1570 if( ext_size
+ 4 > ext_len
)
1572 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1573 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1577 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1578 case TLS_EXT_SERVERNAME
:
1579 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1580 if( ssl
->f_sni
== NULL
)
1583 ret
= ssl_parse_servername_ext( ssl
, ext
+ 4, ext_size
);
1587 #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
1589 case TLS_EXT_RENEGOTIATION_INFO
:
1590 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1591 #if defined(POLARSSL_SSL_RENEGOTIATION)
1592 renegotiation_info_seen
= 1;
1595 ret
= ssl_parse_renegotiation_info( ssl
, ext
+ 4, ext_size
);
1600 #if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1601 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
1602 case TLS_EXT_SIG_ALG
:
1603 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1604 #if defined(POLARSSL_SSL_RENEGOTIATION)
1605 if( ssl
->renegotiation
== SSL_RENEGOTIATION
)
1609 ret
= ssl_parse_signature_algorithms_ext( ssl
, ext
+ 4, ext_size
);
1613 #endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1614 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
1616 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
1617 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES
:
1618 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1620 ret
= ssl_parse_supported_elliptic_curves( ssl
, ext
+ 4, ext_size
);
1625 case TLS_EXT_SUPPORTED_POINT_FORMATS
:
1626 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
1627 ssl
->handshake
->cli_exts
|= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT
;
1629 ret
= ssl_parse_supported_point_formats( ssl
, ext
+ 4, ext_size
);
1633 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1635 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1636 case TLS_EXT_MAX_FRAGMENT_LENGTH
:
1637 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1639 ret
= ssl_parse_max_fragment_length_ext( ssl
, ext
+ 4, ext_size
);
1643 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
1645 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1646 case TLS_EXT_TRUNCATED_HMAC
:
1647 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1649 ret
= ssl_parse_truncated_hmac_ext( ssl
, ext
+ 4, ext_size
);
1653 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
1655 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1656 case TLS_EXT_ENCRYPT_THEN_MAC
:
1657 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1659 ret
= ssl_parse_encrypt_then_mac_ext( ssl
, ext
+ 4, ext_size
);
1663 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1665 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1666 case TLS_EXT_EXTENDED_MASTER_SECRET
:
1667 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1669 ret
= ssl_parse_extended_ms_ext( ssl
, ext
+ 4, ext_size
);
1673 #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1675 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1676 case TLS_EXT_SESSION_TICKET
:
1677 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1679 ret
= ssl_parse_session_ticket_ext( ssl
, ext
+ 4, ext_size
);
1683 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1685 #if defined(POLARSSL_SSL_ALPN)
1687 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1689 ret
= ssl_parse_alpn_ext( ssl
, ext
+ 4, ext_size
);
1693 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1696 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1700 ext_len
-= 4 + ext_size
;
1701 ext
+= 4 + ext_size
;
1703 if( ext_len
> 0 && ext_len
< 4 )
1705 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1706 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1711 * Renegotiation security checks
1713 if( ssl
->secure_renegotiation
!= SSL_SECURE_RENEGOTIATION
&&
1714 ssl
->allow_legacy_renegotiation
== SSL_LEGACY_BREAK_HANDSHAKE
)
1716 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1717 handshake_failure
= 1;
1719 #if defined(POLARSSL_SSL_RENEGOTIATION)
1720 else if( ssl
->renegotiation
== SSL_RENEGOTIATION
&&
1721 ssl
->secure_renegotiation
== SSL_SECURE_RENEGOTIATION
&&
1722 renegotiation_info_seen
== 0 )
1724 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1725 handshake_failure
= 1;
1727 else if( ssl
->renegotiation
== SSL_RENEGOTIATION
&&
1728 ssl
->secure_renegotiation
== SSL_LEGACY_RENEGOTIATION
&&
1729 ssl
->allow_legacy_renegotiation
== SSL_LEGACY_NO_RENEGOTIATION
)
1731 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
1732 handshake_failure
= 1;
1734 else if( ssl
->renegotiation
== SSL_RENEGOTIATION
&&
1735 ssl
->secure_renegotiation
== SSL_LEGACY_RENEGOTIATION
&&
1736 renegotiation_info_seen
== 1 )
1738 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1739 handshake_failure
= 1;
1741 #endif /* POLARSSL_SSL_RENEGOTIATION */
1743 if( handshake_failure
== 1 )
1745 if( ( ret
= ssl_send_fatal_handshake_failure( ssl
) ) != 0 )
1748 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO
);
1752 * Search for a matching ciphersuite
1753 * (At the end because we need information from the EC-based extensions
1754 * and certificate from the SNI callback triggered by the SNI extension.)
1756 got_common_suite
= 0;
1757 ciphersuites
= ssl
->ciphersuite_list
[ssl
->minor_ver
];
1758 ciphersuite_info
= NULL
;
1759 #if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1760 for( j
= 0, p
= buf
+ 41 + sess_len
; j
< ciph_len
; j
+= 2, p
+= 2 )
1762 for( i
= 0; ciphersuites
[i
] != 0; i
++ )
1764 for( i
= 0; ciphersuites
[i
] != 0; i
++ )
1766 for( j
= 0, p
= buf
+ 41 + sess_len
; j
< ciph_len
; j
+= 2, p
+= 2 )
1769 if( p
[0] != ( ( ciphersuites
[i
] >> 8 ) & 0xFF ) ||
1770 p
[1] != ( ( ciphersuites
[i
] ) & 0xFF ) )
1773 got_common_suite
= 1;
1775 if( ( ret
= ssl_ciphersuite_match( ssl
, ciphersuites
[i
],
1776 &ciphersuite_info
) ) != 0 )
1779 if( ciphersuite_info
!= NULL
)
1780 goto have_ciphersuite
;
1784 if( got_common_suite
)
1786 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1787 "but none of them usable" ) );
1788 ssl_send_fatal_handshake_failure( ssl
);
1789 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE
);
1793 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1794 ssl_send_fatal_handshake_failure( ssl
);
1795 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN
);
1799 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info
->name
) );
1801 ssl
->session_negotiate
->ciphersuite
= ciphersuites
[i
];
1802 ssl
->transform_negotiate
->ciphersuite_info
= ciphersuite_info
;
1803 ssl_optimize_checksum( ssl
, ssl
->transform_negotiate
->ciphersuite_info
);
1808 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1813 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1814 static void ssl_write_truncated_hmac_ext( ssl_context
*ssl
,
1818 unsigned char *p
= buf
;
1820 if( ssl
->session_negotiate
->trunc_hmac
== SSL_TRUNC_HMAC_DISABLED
)
1826 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1828 *p
++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC
>> 8 ) & 0xFF );
1829 *p
++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC
) & 0xFF );
1836 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
1838 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1839 static void ssl_write_encrypt_then_mac_ext( ssl_context
*ssl
,
1843 unsigned char *p
= buf
;
1844 const ssl_ciphersuite_t
*suite
= NULL
;
1845 const cipher_info_t
*cipher
= NULL
;
1847 if( ssl
->session_negotiate
->encrypt_then_mac
== SSL_EXTENDED_MS_DISABLED
||
1848 ssl
->minor_ver
== SSL_MINOR_VERSION_0
)
1855 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1856 * from a client and then selects a stream or Authenticated Encryption
1857 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1858 * encrypt-then-MAC response extension back to the client."
1860 if( ( suite
= ssl_ciphersuite_from_id(
1861 ssl
->session_negotiate
->ciphersuite
) ) == NULL
||
1862 ( cipher
= cipher_info_from_type( suite
->cipher
) ) == NULL
||
1863 cipher
->mode
!= POLARSSL_MODE_CBC
)
1869 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1871 *p
++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC
>> 8 ) & 0xFF );
1872 *p
++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC
) & 0xFF );
1879 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1881 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1882 static void ssl_write_extended_ms_ext( ssl_context
*ssl
,
1886 unsigned char *p
= buf
;
1888 if( ssl
->handshake
->extended_ms
== SSL_EXTENDED_MS_DISABLED
||
1889 ssl
->minor_ver
== SSL_MINOR_VERSION_0
)
1895 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
1898 *p
++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET
>> 8 ) & 0xFF );
1899 *p
++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET
) & 0xFF );
1906 #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1908 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1909 static void ssl_write_session_ticket_ext( ssl_context
*ssl
,
1913 unsigned char *p
= buf
;
1915 if( ssl
->handshake
->new_session_ticket
== 0 )
1921 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1923 *p
++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET
>> 8 ) & 0xFF );
1924 *p
++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET
) & 0xFF );
1931 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1933 static void ssl_write_renegotiation_ext( ssl_context
*ssl
,
1937 unsigned char *p
= buf
;
1939 if( ssl
->secure_renegotiation
!= SSL_SECURE_RENEGOTIATION
)
1945 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1947 *p
++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO
>> 8 ) & 0xFF );
1948 *p
++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO
) & 0xFF );
1950 #if defined(POLARSSL_SSL_RENEGOTIATION)
1951 if( ssl
->renegotiation
!= SSL_INITIAL_HANDSHAKE
)
1954 *p
++ = ( ssl
->verify_data_len
* 2 + 1 ) & 0xFF;
1955 *p
++ = ssl
->verify_data_len
* 2 & 0xFF;
1957 memcpy( p
, ssl
->peer_verify_data
, ssl
->verify_data_len
);
1958 p
+= ssl
->verify_data_len
;
1959 memcpy( p
, ssl
->own_verify_data
, ssl
->verify_data_len
);
1960 p
+= ssl
->verify_data_len
;
1962 *olen
= 5 + ssl
->verify_data_len
* 2;
1965 #endif /* POLARSSL_SSL_RENEGOTIATION */
1975 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1976 static void ssl_write_max_fragment_length_ext( ssl_context
*ssl
,
1980 unsigned char *p
= buf
;
1982 if( ssl
->session_negotiate
->mfl_code
== SSL_MAX_FRAG_LEN_NONE
)
1988 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1990 *p
++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH
>> 8 ) & 0xFF );
1991 *p
++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH
) & 0xFF );
1996 *p
++ = ssl
->session_negotiate
->mfl_code
;
2000 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
2002 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
2003 static void ssl_write_supported_point_formats_ext( ssl_context
*ssl
,
2007 unsigned char *p
= buf
;
2010 if( ( ssl
->handshake
->cli_exts
&
2011 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT
) == 0 )
2017 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2019 *p
++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS
>> 8 ) & 0xFF );
2020 *p
++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS
) & 0xFF );
2026 *p
++ = POLARSSL_ECP_PF_UNCOMPRESSED
;
2030 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2032 #if defined(POLARSSL_SSL_ALPN )
2033 static void ssl_write_alpn_ext( ssl_context
*ssl
,
2034 unsigned char *buf
, size_t *olen
)
2036 if( ssl
->alpn_chosen
== NULL
)
2042 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
2045 * 0 . 1 ext identifier
2047 * 4 . 5 protocol list length
2048 * 6 . 6 protocol name length
2049 * 7 . 7+n protocol name
2051 buf
[0] = (unsigned char)( ( TLS_EXT_ALPN
>> 8 ) & 0xFF );
2052 buf
[1] = (unsigned char)( ( TLS_EXT_ALPN
) & 0xFF );
2054 *olen
= 7 + strlen( ssl
->alpn_chosen
);
2056 buf
[2] = (unsigned char)( ( ( *olen
- 4 ) >> 8 ) & 0xFF );
2057 buf
[3] = (unsigned char)( ( ( *olen
- 4 ) ) & 0xFF );
2059 buf
[4] = (unsigned char)( ( ( *olen
- 6 ) >> 8 ) & 0xFF );
2060 buf
[5] = (unsigned char)( ( ( *olen
- 6 ) ) & 0xFF );
2062 buf
[6] = (unsigned char)( ( ( *olen
- 7 ) ) & 0xFF );
2064 memcpy( buf
+ 7, ssl
->alpn_chosen
, *olen
- 7 );
2066 #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2068 static int ssl_write_server_hello( ssl_context
*ssl
)
2070 #if defined(POLARSSL_HAVE_TIME)
2074 size_t olen
, ext_len
= 0, n
;
2075 unsigned char *buf
, *p
;
2077 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2079 if( ssl
->f_rng
== NULL
)
2081 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2082 return( POLARSSL_ERR_SSL_NO_RNG
);
2086 * 0 . 0 handshake type
2087 * 1 . 3 handshake length
2088 * 4 . 5 protocol version
2090 * 10 . 37 random bytes
2095 *p
++ = (unsigned char) ssl
->major_ver
;
2096 *p
++ = (unsigned char) ssl
->minor_ver
;
2098 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2101 #if defined(POLARSSL_HAVE_TIME)
2103 *p
++ = (unsigned char)( t
>> 24 );
2104 *p
++ = (unsigned char)( t
>> 16 );
2105 *p
++ = (unsigned char)( t
>> 8 );
2106 *p
++ = (unsigned char)( t
);
2108 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t
) );
2110 if( ( ret
= ssl
->f_rng( ssl
->p_rng
, p
, 4 ) ) != 0 )
2114 #endif /* POLARSSL_HAVE_TIME */
2116 if( ( ret
= ssl
->f_rng( ssl
->p_rng
, p
, 28 ) ) != 0 )
2121 memcpy( ssl
->handshake
->randbytes
+ 32, buf
+ 6, 32 );
2123 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf
+ 6, 32 );
2126 * Resume is 0 by default, see ssl_handshake_init().
2127 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2128 * If not, try looking up session ID in our cache.
2130 if( ssl
->handshake
->resume
== 0 &&
2131 #if defined(POLARSSL_SSL_RENEGOTIATION)
2132 ssl
->renegotiation
== SSL_INITIAL_HANDSHAKE
&&
2134 ssl
->session_negotiate
->length
!= 0 &&
2135 ssl
->f_get_cache
!= NULL
&&
2136 ssl
->f_get_cache( ssl
->p_get_cache
, ssl
->session_negotiate
) == 0 )
2138 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
2139 ssl
->handshake
->resume
= 1;
2142 if( ssl
->handshake
->resume
== 0 )
2145 * New session, create a new session id,
2146 * unless we're about to issue a session ticket
2150 #if defined(POLARSSL_HAVE_TIME)
2151 ssl
->session_negotiate
->start
= time( NULL
);
2154 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2155 if( ssl
->handshake
->new_session_ticket
!= 0 )
2157 ssl
->session_negotiate
->length
= n
= 0;
2158 memset( ssl
->session_negotiate
->id
, 0, 32 );
2161 #endif /* POLARSSL_SSL_SESSION_TICKETS */
2163 ssl
->session_negotiate
->length
= n
= 32;
2164 if( ( ret
= ssl
->f_rng( ssl
->p_rng
, ssl
->session_negotiate
->id
,
2172 * Resuming a session
2174 n
= ssl
->session_negotiate
->length
;
2175 ssl
->state
= SSL_SERVER_CHANGE_CIPHER_SPEC
;
2177 if( ( ret
= ssl_derive_keys( ssl
) ) != 0 )
2179 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret
);
2185 * 38 . 38 session id length
2186 * 39 . 38+n session id
2187 * 39+n . 40+n chosen ciphersuite
2188 * 41+n . 41+n chosen compression alg.
2189 * 42+n . 43+n extensions length
2190 * 44+n . 43+n+m extensions
2192 *p
++ = (unsigned char) ssl
->session_negotiate
->length
;
2193 memcpy( p
, ssl
->session_negotiate
->id
, ssl
->session_negotiate
->length
);
2194 p
+= ssl
->session_negotiate
->length
;
2196 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n
) );
2197 SSL_DEBUG_BUF( 3, "server hello, session id", buf
+ 39, n
);
2198 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
2199 ssl
->handshake
->resume
? "a" : "no" ) );
2201 *p
++ = (unsigned char)( ssl
->session_negotiate
->ciphersuite
>> 8 );
2202 *p
++ = (unsigned char)( ssl
->session_negotiate
->ciphersuite
);
2203 *p
++ = (unsigned char)( ssl
->session_negotiate
->compression
);
2205 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2206 ssl_get_ciphersuite_name( ssl
->session_negotiate
->ciphersuite
) ) );
2207 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
2208 ssl
->session_negotiate
->compression
) );
2211 * First write extensions, then the total length
2213 ssl_write_renegotiation_ext( ssl
, p
+ 2 + ext_len
, &olen
);
2216 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
2217 ssl_write_max_fragment_length_ext( ssl
, p
+ 2 + ext_len
, &olen
);
2221 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
2222 ssl_write_truncated_hmac_ext( ssl
, p
+ 2 + ext_len
, &olen
);
2226 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2227 ssl_write_encrypt_then_mac_ext( ssl
, p
+ 2 + ext_len
, &olen
);
2231 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2232 ssl_write_extended_ms_ext( ssl
, p
+ 2 + ext_len
, &olen
);
2236 #if defined(POLARSSL_SSL_SESSION_TICKETS)
2237 ssl_write_session_ticket_ext( ssl
, p
+ 2 + ext_len
, &olen
);
2241 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
2242 ssl_write_supported_point_formats_ext( ssl
, p
+ 2 + ext_len
, &olen
);
2246 #if defined(POLARSSL_SSL_ALPN)
2247 ssl_write_alpn_ext( ssl
, p
+ 2 + ext_len
, &olen
);
2251 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len
) );
2255 *p
++ = (unsigned char)( ( ext_len
>> 8 ) & 0xFF );
2256 *p
++ = (unsigned char)( ( ext_len
) & 0xFF );
2260 ssl
->out_msglen
= p
- buf
;
2261 ssl
->out_msgtype
= SSL_MSG_HANDSHAKE
;
2262 ssl
->out_msg
[0] = SSL_HS_SERVER_HELLO
;
2264 ret
= ssl_write_record( ssl
);
2266 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2271 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2272 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2273 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2274 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2275 static int ssl_write_certificate_request( ssl_context
*ssl
)
2277 const ssl_ciphersuite_t
*ciphersuite_info
= ssl
->transform_negotiate
->ciphersuite_info
;
2279 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2281 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_PSK
||
2282 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_RSA_PSK
||
2283 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_DHE_PSK
||
2284 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_PSK
)
2286 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2291 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2292 return( POLARSSL_ERR_SSL_INTERNAL_ERROR
);
2295 static int ssl_write_certificate_request( ssl_context
*ssl
)
2297 int ret
= POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE
;
2298 const ssl_ciphersuite_t
*ciphersuite_info
= ssl
->transform_negotiate
->ciphersuite_info
;
2299 size_t dn_size
, total_dn_size
; /* excluding length bytes */
2300 size_t ct_len
, sa_len
; /* including length bytes */
2301 unsigned char *buf
, *p
;
2302 const x509_crt
*crt
;
2304 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2308 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_PSK
||
2309 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_RSA_PSK
||
2310 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_DHE_PSK
||
2311 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_PSK
||
2312 ssl
->authmode
== SSL_VERIFY_NONE
)
2314 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2319 * 0 . 0 handshake type
2320 * 1 . 3 handshake length
2321 * 4 . 4 cert type count
2322 * 5 .. m-1 cert types
2323 * m .. m+1 sig alg length (TLS 1.2 only)
2324 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
2325 * n .. n+1 length of all DNs
2326 * n+2 .. n+3 length of DN 1
2327 * n+4 .. ... Distinguished Name #1
2328 * ... .. ... length of DN 2, etc.
2334 * Supported certificate types
2336 * ClientCertificateType certificate_types<1..2^8-1>;
2337 * enum { (255) } ClientCertificateType;
2341 #if defined(POLARSSL_RSA_C)
2342 p
[1 + ct_len
++] = SSL_CERT_TYPE_RSA_SIGN
;
2344 #if defined(POLARSSL_ECDSA_C)
2345 p
[1 + ct_len
++] = SSL_CERT_TYPE_ECDSA_SIGN
;
2348 p
[0] = (unsigned char) ct_len
++;
2352 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2354 * Add signature_algorithms for verify (TLS 1.2)
2356 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2359 * HashAlgorithm hash;
2360 * SignatureAlgorithm signature;
2361 * } SignatureAndHashAlgorithm;
2363 * enum { (255) } HashAlgorithm;
2364 * enum { (255) } SignatureAlgorithm;
2366 if( ssl
->minor_ver
== SSL_MINOR_VERSION_3
)
2369 * Only use current running hash algorithm that is already required
2370 * for requested ciphersuite.
2372 ssl
->handshake
->verify_sig_alg
= SSL_HASH_SHA256
;
2374 if( ssl
->transform_negotiate
->ciphersuite_info
->mac
==
2375 POLARSSL_MD_SHA384
)
2377 ssl
->handshake
->verify_sig_alg
= SSL_HASH_SHA384
;
2381 * Supported signature algorithms
2383 #if defined(POLARSSL_RSA_C)
2384 p
[2 + sa_len
++] = ssl
->handshake
->verify_sig_alg
;
2385 p
[2 + sa_len
++] = SSL_SIG_RSA
;
2387 #if defined(POLARSSL_ECDSA_C)
2388 p
[2 + sa_len
++] = ssl
->handshake
->verify_sig_alg
;
2389 p
[2 + sa_len
++] = SSL_SIG_ECDSA
;
2392 p
[0] = (unsigned char)( sa_len
>> 8 );
2393 p
[1] = (unsigned char)( sa_len
);
2397 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2400 * DistinguishedName certificate_authorities<0..2^16-1>;
2401 * opaque DistinguishedName<1..2^16-1>;
2404 crt
= ssl
->ca_chain
;
2407 while( crt
!= NULL
&& crt
->version
!= 0 )
2409 if( p
- buf
> 4096 )
2412 dn_size
= crt
->subject_raw
.len
;
2413 *p
++ = (unsigned char)( dn_size
>> 8 );
2414 *p
++ = (unsigned char)( dn_size
);
2415 memcpy( p
, crt
->subject_raw
.p
, dn_size
);
2418 SSL_DEBUG_BUF( 3, "requested DN", p
, dn_size
);
2420 total_dn_size
+= 2 + dn_size
;
2424 ssl
->out_msglen
= p
- buf
;
2425 ssl
->out_msgtype
= SSL_MSG_HANDSHAKE
;
2426 ssl
->out_msg
[0] = SSL_HS_CERTIFICATE_REQUEST
;
2427 ssl
->out_msg
[4 + ct_len
+ sa_len
] = (unsigned char)( total_dn_size
>> 8 );
2428 ssl
->out_msg
[5 + ct_len
+ sa_len
] = (unsigned char)( total_dn_size
);
2430 ret
= ssl_write_record( ssl
);
2432 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2436 #endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2437 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2438 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2439 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2441 #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2442 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2443 static int ssl_get_ecdh_params_from_cert( ssl_context
*ssl
)
2447 if( ! pk_can_do( ssl_own_key( ssl
), POLARSSL_PK_ECKEY
) )
2449 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2450 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH
);
2453 if( ( ret
= ecdh_get_params( &ssl
->handshake
->ecdh_ctx
,
2454 pk_ec( *ssl_own_key( ssl
) ),
2455 POLARSSL_ECDH_OURS
) ) != 0 )
2457 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret
);
2463 #endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2464 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2466 static int ssl_write_server_key_exchange( ssl_context
*ssl
)
2470 const ssl_ciphersuite_t
*ciphersuite_info
=
2471 ssl
->transform_negotiate
->ciphersuite_info
;
2473 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2474 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2475 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2476 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
2477 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2478 unsigned char *p
= ssl
->out_msg
+ 4;
2479 unsigned char *dig_signed
= p
;
2480 size_t dig_signed_len
= 0, len
;
2481 ((void) dig_signed
);
2482 ((void) dig_signed_len
);
2485 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2487 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2488 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2489 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2490 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_RSA
||
2491 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_PSK
||
2492 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_RSA_PSK
)
2494 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2500 #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2501 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2502 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDH_RSA
||
2503 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDH_ECDSA
)
2505 ssl_get_ecdh_params_from_cert( ssl
);
2507 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2513 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2514 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2515 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_DHE_PSK
||
2516 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_PSK
)
2518 /* TODO: Support identity hints */
2524 #endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2525 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2527 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2528 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2529 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_DHE_RSA
||
2530 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_DHE_PSK
)
2533 * Ephemeral DH parameters:
2536 * opaque dh_p<1..2^16-1>;
2537 * opaque dh_g<1..2^16-1>;
2538 * opaque dh_Ys<1..2^16-1>;
2541 if( ( ret
= mpi_copy( &ssl
->handshake
->dhm_ctx
.P
, &ssl
->dhm_P
) ) != 0 ||
2542 ( ret
= mpi_copy( &ssl
->handshake
->dhm_ctx
.G
, &ssl
->dhm_G
) ) != 0 )
2544 SSL_DEBUG_RET( 1, "mpi_copy", ret
);
2548 if( ( ret
= dhm_make_params( &ssl
->handshake
->dhm_ctx
,
2549 (int) mpi_size( &ssl
->handshake
->dhm_ctx
.P
),
2550 p
, &len
, ssl
->f_rng
, ssl
->p_rng
) ) != 0 )
2552 SSL_DEBUG_RET( 1, "dhm_make_params", ret
);
2557 dig_signed_len
= len
;
2562 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl
->handshake
->dhm_ctx
.X
);
2563 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl
->handshake
->dhm_ctx
.P
);
2564 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl
->handshake
->dhm_ctx
.G
);
2565 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl
->handshake
->dhm_ctx
.GX
);
2567 #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2568 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2570 #if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
2571 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_RSA
||
2572 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA
||
2573 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_PSK
)
2576 * Ephemeral ECDH parameters:
2579 * ECParameters curve_params;
2581 * } ServerECDHParams;
2583 const ecp_curve_info
**curve
= NULL
;
2584 #if defined(POLARSSL_SSL_SET_CURVES)
2585 const ecp_group_id
*gid
;
2587 /* Match our preference list against the offered curves */
2588 for( gid
= ssl
->curve_list
; *gid
!= POLARSSL_ECP_DP_NONE
; gid
++ )
2589 for( curve
= ssl
->handshake
->curves
; *curve
!= NULL
; curve
++ )
2590 if( (*curve
)->grp_id
== *gid
)
2591 goto curve_matching_done
;
2593 curve_matching_done
:
2595 curve
= ssl
->handshake
->curves
;
2598 if( *curve
== NULL
)
2600 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2601 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN
);
2604 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve
)->name
) );
2606 if( ( ret
= ecp_use_known_dp( &ssl
->handshake
->ecdh_ctx
.grp
,
2607 (*curve
)->grp_id
) ) != 0 )
2609 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret
);
2613 if( ( ret
= ecdh_make_params( &ssl
->handshake
->ecdh_ctx
, &len
,
2614 p
, SSL_MAX_CONTENT_LEN
- n
,
2615 ssl
->f_rng
, ssl
->p_rng
) ) != 0 )
2617 SSL_DEBUG_RET( 1, "ecdh_make_params", ret
);
2622 dig_signed_len
= len
;
2627 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl
->handshake
->ecdh_ctx
.Q
);
2629 #endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
2631 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2632 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2633 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2634 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_DHE_RSA
||
2635 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_RSA
||
2636 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA
)
2638 size_t signature_len
= 0;
2639 unsigned int hashlen
= 0;
2640 unsigned char hash
[64];
2641 md_type_t md_alg
= POLARSSL_MD_NONE
;
2644 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2646 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2647 if( ssl
->minor_ver
== SSL_MINOR_VERSION_3
)
2649 md_alg
= ssl_md_alg_from_hash( ssl
->handshake
->sig_alg
);
2651 if( md_alg
== POLARSSL_MD_NONE
)
2653 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2654 return( POLARSSL_ERR_SSL_INTERNAL_ERROR
);
2658 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2659 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2660 defined(POLARSSL_SSL_PROTO_TLS1_1)
2661 if( ciphersuite_info
->key_exchange
==
2662 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA
)
2664 md_alg
= POLARSSL_MD_SHA1
;
2669 md_alg
= POLARSSL_MD_NONE
;
2673 * Compute the hash to be signed
2675 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2676 defined(POLARSSL_SSL_PROTO_TLS1_1)
2677 if( md_alg
== POLARSSL_MD_NONE
)
2686 * digitally-signed struct {
2687 * opaque md5_hash[16];
2688 * opaque sha_hash[20];
2692 * MD5(ClientHello.random + ServerHello.random
2695 * SHA(ClientHello.random + ServerHello.random
2699 md5_update( &md5
, ssl
->handshake
->randbytes
, 64 );
2700 md5_update( &md5
, dig_signed
, dig_signed_len
);
2701 md5_finish( &md5
, hash
);
2703 sha1_starts( &sha1
);
2704 sha1_update( &sha1
, ssl
->handshake
->randbytes
, 64 );
2705 sha1_update( &sha1
, dig_signed
, dig_signed_len
);
2706 sha1_finish( &sha1
, hash
+ 16 );
2714 #endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2715 POLARSSL_SSL_PROTO_TLS1_1 */
2716 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2717 defined(POLARSSL_SSL_PROTO_TLS1_2)
2718 if( md_alg
!= POLARSSL_MD_NONE
)
2721 const md_info_t
*md_info
= md_info_from_type( md_alg
);
2725 /* Info from md_alg will be used instead */
2729 * digitally-signed struct {
2730 * opaque client_random[32];
2731 * opaque server_random[32];
2732 * ServerDHParams params;
2735 if( ( ret
= md_init_ctx( &ctx
, md_info
) ) != 0 )
2737 SSL_DEBUG_RET( 1, "md_init_ctx", ret
);
2742 md_update( &ctx
, ssl
->handshake
->randbytes
, 64 );
2743 md_update( &ctx
, dig_signed
, dig_signed_len
);
2744 md_finish( &ctx
, hash
);
2748 #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2749 POLARSSL_SSL_PROTO_TLS1_2 */
2751 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2752 return( POLARSSL_ERR_SSL_INTERNAL_ERROR
);
2755 SSL_DEBUG_BUF( 3, "parameters hash", hash
, hashlen
!= 0 ? hashlen
:
2756 (unsigned int) ( md_info_from_type( md_alg
) )->size
);
2759 * Make the signature
2761 if( ssl_own_key( ssl
) == NULL
)
2763 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2764 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED
);
2767 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
2768 if( ssl
->minor_ver
== SSL_MINOR_VERSION_3
)
2770 *(p
++) = ssl
->handshake
->sig_alg
;
2771 *(p
++) = ssl_sig_from_pk( ssl_own_key( ssl
) );
2775 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2777 if( ( ret
= pk_sign( ssl_own_key( ssl
), md_alg
, hash
, hashlen
,
2778 p
+ 2 , &signature_len
,
2779 ssl
->f_rng
, ssl
->p_rng
) ) != 0 )
2781 SSL_DEBUG_RET( 1, "pk_sign", ret
);
2785 *(p
++) = (unsigned char)( signature_len
>> 8 );
2786 *(p
++) = (unsigned char)( signature_len
);
2789 SSL_DEBUG_BUF( 3, "my signature", p
, signature_len
);
2794 #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
2795 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2796 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
2798 ssl
->out_msglen
= 4 + n
;
2799 ssl
->out_msgtype
= SSL_MSG_HANDSHAKE
;
2800 ssl
->out_msg
[0] = SSL_HS_SERVER_KEY_EXCHANGE
;
2804 if( ( ret
= ssl_write_record( ssl
) ) != 0 )
2806 SSL_DEBUG_RET( 1, "ssl_write_record", ret
);
2810 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2815 static int ssl_write_server_hello_done( ssl_context
*ssl
)
2819 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2821 ssl
->out_msglen
= 4;
2822 ssl
->out_msgtype
= SSL_MSG_HANDSHAKE
;
2823 ssl
->out_msg
[0] = SSL_HS_SERVER_HELLO_DONE
;
2827 if( ( ret
= ssl_write_record( ssl
) ) != 0 )
2829 SSL_DEBUG_RET( 1, "ssl_write_record", ret
);
2833 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2838 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2839 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2840 static int ssl_parse_client_dh_public( ssl_context
*ssl
, unsigned char **p
,
2841 const unsigned char *end
)
2843 int ret
= POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE
;
2847 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2851 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2852 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
2855 n
= ( (*p
)[0] << 8 ) | (*p
)[1];
2860 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2861 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
2864 if( ( ret
= dhm_read_public( &ssl
->handshake
->dhm_ctx
, *p
, n
) ) != 0 )
2866 SSL_DEBUG_RET( 1, "dhm_read_public", ret
);
2867 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP
);
2872 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl
->handshake
->dhm_ctx
.GY
);
2876 #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2877 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
2879 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2880 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2881 static int ssl_parse_encrypted_pms( ssl_context
*ssl
,
2882 const unsigned char *p
,
2883 const unsigned char *end
,
2887 size_t len
= pk_get_len( ssl_own_key( ssl
) );
2888 unsigned char *pms
= ssl
->handshake
->premaster
+ pms_offset
;
2889 unsigned char fake_pms
[48], peer_pms
[48];
2891 size_t i
, diff
, peer_pmslen
;
2893 if( ! pk_can_do( ssl_own_key( ssl
), POLARSSL_PK_RSA
) )
2895 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
2896 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED
);
2900 * Decrypt the premaster using own private RSA key
2902 #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2903 defined(POLARSSL_SSL_PROTO_TLS1_2)
2904 if( ssl
->minor_ver
!= SSL_MINOR_VERSION_0
)
2906 if( *p
++ != ( ( len
>> 8 ) & 0xFF ) ||
2907 *p
++ != ( ( len
) & 0xFF ) )
2909 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2910 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
2915 if( p
+ len
!= end
)
2917 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2918 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
2922 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
2923 * must not cause the connection to end immediately; instead, send a
2924 * bad_record_mac later in the handshake.
2925 * Also, avoid data-dependant branches here to protect against
2926 * timing-based variants.
2928 ret
= ssl
->f_rng( ssl
->p_rng
, fake_pms
, sizeof( fake_pms
) );
2932 ret
= pk_decrypt( ssl_own_key( ssl
), p
, len
,
2933 peer_pms
, &peer_pmslen
,
2935 ssl
->f_rng
, ssl
->p_rng
);
2937 diff
= (size_t) ret
;
2938 diff
|= peer_pmslen
^ 48;
2939 diff
|= peer_pms
[0] ^ ssl
->handshake
->max_major_ver
;
2940 diff
|= peer_pms
[1] ^ ssl
->handshake
->max_minor_ver
;
2942 #if defined(POLARSSL_SSL_DEBUG_ALL)
2944 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2947 if( sizeof( ssl
->handshake
->premaster
) < pms_offset
||
2948 sizeof( ssl
->handshake
->premaster
) - pms_offset
< 48 )
2950 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2951 return( POLARSSL_ERR_SSL_INTERNAL_ERROR
);
2953 ssl
->handshake
->pmslen
= 48;
2955 mask
= ( diff
| - diff
) >> ( sizeof( size_t ) * 8 - 1 );
2956 mask
= (unsigned char)( - ( ret
!= 0 ) ); /* mask = diff ? 0xff : 0x00 */
2957 for( i
= 0; i
< ssl
->handshake
->pmslen
; i
++ )
2958 pms
[i
] = ( mask
& fake_pms
[i
] ) | ( (~mask
) & peer_pms
[i
] );
2962 #endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2963 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
2965 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
2966 static int ssl_parse_client_psk_identity( ssl_context
*ssl
, unsigned char **p
,
2967 const unsigned char *end
)
2972 if( ssl
->f_psk
== NULL
&&
2973 ( ssl
->psk
== NULL
|| ssl
->psk_identity
== NULL
||
2974 ssl
->psk_identity_len
== 0 || ssl
->psk_len
== 0 ) )
2976 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2977 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED
);
2981 * Receive client pre-shared key identity name
2985 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2986 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
2989 n
= ( (*p
)[0] << 8 ) | (*p
)[1];
2992 if( n
< 1 || n
> 65535 || *p
+ n
> end
)
2994 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2995 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
2998 if( ssl
->f_psk
!= NULL
)
3000 if( ssl
->f_psk( ssl
->p_psk
, ssl
, *p
, n
) != 0 )
3001 ret
= POLARSSL_ERR_SSL_UNKNOWN_IDENTITY
;
3005 /* Identity is not a big secret since clients send it in the clear,
3006 * but treat it carefully anyway, just in case */
3007 if( n
!= ssl
->psk_identity_len
||
3008 safer_memcmp( ssl
->psk_identity
, *p
, n
) != 0 )
3010 ret
= POLARSSL_ERR_SSL_UNKNOWN_IDENTITY
;
3014 if( ret
== POLARSSL_ERR_SSL_UNKNOWN_IDENTITY
)
3016 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p
, n
);
3017 if( ( ret
= ssl_send_alert_message( ssl
,
3018 SSL_ALERT_LEVEL_FATAL
,
3019 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY
) ) != 0 )
3024 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY
);
3031 #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
3033 static int ssl_parse_client_key_exchange( ssl_context
*ssl
)
3036 const ssl_ciphersuite_t
*ciphersuite_info
;
3038 ciphersuite_info
= ssl
->transform_negotiate
->ciphersuite_info
;
3040 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3042 if( ( ret
= ssl_read_record( ssl
) ) != 0 )
3044 SSL_DEBUG_RET( 1, "ssl_read_record", ret
);
3048 if( ssl
->in_msgtype
!= SSL_MSG_HANDSHAKE
)
3050 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3051 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3054 if( ssl
->in_msg
[0] != SSL_HS_CLIENT_KEY_EXCHANGE
)
3056 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3057 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3060 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
3061 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_DHE_RSA
)
3063 unsigned char *p
= ssl
->in_msg
+ 4;
3064 unsigned char *end
= ssl
->in_msg
+ ssl
->in_hslen
;
3066 if( ( ret
= ssl_parse_client_dh_public( ssl
, &p
, end
) ) != 0 )
3068 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret
);
3074 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3075 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3078 ssl
->handshake
->pmslen
= POLARSSL_PREMASTER_SIZE
;
3080 if( ( ret
= dhm_calc_secret( &ssl
->handshake
->dhm_ctx
,
3081 ssl
->handshake
->premaster
,
3082 &ssl
->handshake
->pmslen
,
3083 ssl
->f_rng
, ssl
->p_rng
) ) != 0 )
3085 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret
);
3086 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS
);
3089 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl
->handshake
->dhm_ctx
.K
);
3092 #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
3093 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3094 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3095 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3096 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3097 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_RSA
||
3098 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA
||
3099 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDH_RSA
||
3100 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDH_ECDSA
)
3102 if( ( ret
= ecdh_read_public( &ssl
->handshake
->ecdh_ctx
,
3103 ssl
->in_msg
+ 4, ssl
->in_hslen
- 4 ) ) != 0 )
3105 SSL_DEBUG_RET( 1, "ecdh_read_public", ret
);
3106 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP
);
3109 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl
->handshake
->ecdh_ctx
.Qp
);
3111 if( ( ret
= ecdh_calc_secret( &ssl
->handshake
->ecdh_ctx
,
3112 &ssl
->handshake
->pmslen
,
3113 ssl
->handshake
->premaster
,
3114 POLARSSL_MPI_MAX_SIZE
,
3115 ssl
->f_rng
, ssl
->p_rng
) ) != 0 )
3117 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret
);
3118 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS
);
3121 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl
->handshake
->ecdh_ctx
.z
);
3124 #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3125 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3126 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3127 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3128 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3129 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_PSK
)
3131 unsigned char *p
= ssl
->in_msg
+ 4;
3132 unsigned char *end
= ssl
->in_msg
+ ssl
->in_hslen
;
3134 if( ( ret
= ssl_parse_client_psk_identity( ssl
, &p
, end
) ) != 0 )
3136 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret
);
3142 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3143 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3146 if( ( ret
= ssl_psk_derive_premaster( ssl
,
3147 ciphersuite_info
->key_exchange
) ) != 0 )
3149 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret
);
3154 #endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
3155 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3156 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_RSA_PSK
)
3158 unsigned char *p
= ssl
->in_msg
+ 4;
3159 unsigned char *end
= ssl
->in_msg
+ ssl
->in_hslen
;
3161 if( ( ret
= ssl_parse_client_psk_identity( ssl
, &p
, end
) ) != 0 )
3163 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret
);
3167 if( ( ret
= ssl_parse_encrypted_pms( ssl
, p
, end
, 2 ) ) != 0 )
3169 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret
);
3173 if( ( ret
= ssl_psk_derive_premaster( ssl
,
3174 ciphersuite_info
->key_exchange
) ) != 0 )
3176 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret
);
3181 #endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
3182 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3183 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_DHE_PSK
)
3185 unsigned char *p
= ssl
->in_msg
+ 4;
3186 unsigned char *end
= ssl
->in_msg
+ ssl
->in_hslen
;
3188 if( ( ret
= ssl_parse_client_psk_identity( ssl
, &p
, end
) ) != 0 )
3190 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret
);
3193 if( ( ret
= ssl_parse_client_dh_public( ssl
, &p
, end
) ) != 0 )
3195 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret
);
3201 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3202 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE
);
3205 if( ( ret
= ssl_psk_derive_premaster( ssl
,
3206 ciphersuite_info
->key_exchange
) ) != 0 )
3208 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret
);
3213 #endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
3214 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3215 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_PSK
)
3217 unsigned char *p
= ssl
->in_msg
+ 4;
3218 unsigned char *end
= ssl
->in_msg
+ ssl
->in_hslen
;
3220 if( ( ret
= ssl_parse_client_psk_identity( ssl
, &p
, end
) ) != 0 )
3222 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret
);
3226 if( ( ret
= ecdh_read_public( &ssl
->handshake
->ecdh_ctx
,
3227 p
, end
- p
) ) != 0 )
3229 SSL_DEBUG_RET( 1, "ecdh_read_public", ret
);
3230 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP
);
3233 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl
->handshake
->ecdh_ctx
.Qp
);
3235 if( ( ret
= ssl_psk_derive_premaster( ssl
,
3236 ciphersuite_info
->key_exchange
) ) != 0 )
3238 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret
);
3243 #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3244 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3245 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_RSA
)
3247 if( ( ret
= ssl_parse_encrypted_pms( ssl
,
3249 ssl
->in_msg
+ ssl
->in_hslen
,
3252 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret
);
3257 #endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3259 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3260 return( POLARSSL_ERR_SSL_INTERNAL_ERROR
);
3263 if( ( ret
= ssl_derive_keys( ssl
) ) != 0 )
3265 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret
);
3271 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3276 #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3277 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3278 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3279 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
3280 static int ssl_parse_certificate_verify( ssl_context
*ssl
)
3282 const ssl_ciphersuite_t
*ciphersuite_info
= ssl
->transform_negotiate
->ciphersuite_info
;
3284 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3286 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_PSK
||
3287 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_RSA_PSK
||
3288 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_PSK
||
3289 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_DHE_PSK
)
3291 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3296 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3297 return( POLARSSL_ERR_SSL_INTERNAL_ERROR
);
3300 static int ssl_parse_certificate_verify( ssl_context
*ssl
)
3302 int ret
= POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE
;
3303 size_t sa_len
, sig_len
;
3304 unsigned char hash
[48];
3305 unsigned char *hash_start
= hash
;
3307 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
3311 const ssl_ciphersuite_t
*ciphersuite_info
= ssl
->transform_negotiate
->ciphersuite_info
;
3313 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3315 if( ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_PSK
||
3316 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_RSA_PSK
||
3317 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_ECDHE_PSK
||
3318 ciphersuite_info
->key_exchange
== POLARSSL_KEY_EXCHANGE_DHE_PSK
)
3320 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3325 if( ssl
->session_negotiate
->peer_cert
== NULL
)
3327 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3332 ssl
->handshake
->calc_verify( ssl
, hash
);
3334 if( ( ret
= ssl_read_record( ssl
) ) != 0 )
3336 SSL_DEBUG_RET( 1, "ssl_read_record", ret
);
3342 if( ssl
->in_msgtype
!= SSL_MSG_HANDSHAKE
)
3344 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3345 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
3348 if( ssl
->in_msg
[0] != SSL_HS_CERTIFICATE_VERIFY
)
3350 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3351 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
3355 * 0 . 0 handshake type
3356 * 1 . 3 handshake length
3357 * 4 . 5 sig alg (TLS 1.2 only)
3358 * 4+n . 5+n signature length (n = sa_len)
3359 * 6+n . 6+n+m signature (m = sig_len)
3362 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3363 defined(POLARSSL_SSL_PROTO_TLS1_1)
3364 if( ssl
->minor_ver
!= SSL_MINOR_VERSION_3
)
3368 md_alg
= POLARSSL_MD_NONE
;
3371 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3372 if( pk_can_do( &ssl
->session_negotiate
->peer_cert
->pk
,
3373 POLARSSL_PK_ECDSA
) )
3377 md_alg
= POLARSSL_MD_SHA1
;
3381 #endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3382 POLARSSL_SSL_PROTO_TLS1_1 */
3383 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
3384 if( ssl
->minor_ver
== SSL_MINOR_VERSION_3
)
3391 if( ssl
->in_msg
[4] != ssl
->handshake
->verify_sig_alg
)
3393 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3394 " for verify message" ) );
3395 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
3398 md_alg
= ssl_md_alg_from_hash( ssl
->handshake
->verify_sig_alg
);
3400 /* Info from md_alg will be used instead */
3406 if( ( pk_alg
= ssl_pk_alg_from_sig( ssl
->in_msg
[5] ) )
3407 == POLARSSL_PK_NONE
)
3409 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3410 " for verify message" ) );
3411 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
3415 * Check the certificate's key type matches the signature alg
3417 if( ! pk_can_do( &ssl
->session_negotiate
->peer_cert
->pk
, pk_alg
) )
3419 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3420 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
3424 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3426 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3427 return( POLARSSL_ERR_SSL_INTERNAL_ERROR
);
3430 sig_len
= ( ssl
->in_msg
[4 + sa_len
] << 8 ) | ssl
->in_msg
[5 + sa_len
];
3432 if( sa_len
+ sig_len
+ 6 != ssl
->in_hslen
)
3434 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3435 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY
);
3438 if( ( ret
= pk_verify( &ssl
->session_negotiate
->peer_cert
->pk
,
3439 md_alg
, hash_start
, hashlen
,
3440 ssl
->in_msg
+ 6 + sa_len
, sig_len
) ) != 0 )
3442 SSL_DEBUG_RET( 1, "pk_verify", ret
);
3446 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3450 #endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3451 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3452 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
3454 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3455 static int ssl_write_new_session_ticket( ssl_context
*ssl
)
3459 uint32_t lifetime
= (uint32_t) ssl
->ticket_lifetime
;
3461 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3463 ssl
->out_msgtype
= SSL_MSG_HANDSHAKE
;
3464 ssl
->out_msg
[0] = SSL_HS_NEW_SESSION_TICKET
;
3468 * uint32 ticket_lifetime_hint;
3469 * opaque ticket<0..2^16-1>;
3470 * } NewSessionTicket;
3472 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3473 * 8 . 9 ticket_len (n)
3474 * 10 . 9+n ticket content
3477 ssl
->out_msg
[4] = ( lifetime
>> 24 ) & 0xFF;
3478 ssl
->out_msg
[5] = ( lifetime
>> 16 ) & 0xFF;
3479 ssl
->out_msg
[6] = ( lifetime
>> 8 ) & 0xFF;
3480 ssl
->out_msg
[7] = ( lifetime
) & 0xFF;
3482 if( ( ret
= ssl_write_ticket( ssl
, &tlen
) ) != 0 )
3484 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret
);
3488 ssl
->out_msg
[8] = (unsigned char)( ( tlen
>> 8 ) & 0xFF );
3489 ssl
->out_msg
[9] = (unsigned char)( ( tlen
) & 0xFF );
3491 ssl
->out_msglen
= 10 + tlen
;
3494 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3495 * ChangeCipherSpec share the same state.
3497 ssl
->handshake
->new_session_ticket
= 0;
3499 if( ( ret
= ssl_write_record( ssl
) ) != 0 )
3501 SSL_DEBUG_RET( 1, "ssl_write_record", ret
);
3505 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3509 #endif /* POLARSSL_SSL_SESSION_TICKETS */
3512 * SSL handshake -- server side -- single step
3514 int ssl_handshake_server_step( ssl_context
*ssl
)
3518 if( ssl
->state
== SSL_HANDSHAKE_OVER
)
3519 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA
);
3521 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl
->state
) );
3523 if( ( ret
= ssl_flush_output( ssl
) ) != 0 )
3526 switch( ssl
->state
)
3528 case SSL_HELLO_REQUEST
:
3529 ssl
->state
= SSL_CLIENT_HELLO
;
3535 case SSL_CLIENT_HELLO
:
3536 ret
= ssl_parse_client_hello( ssl
);
3542 * ( ServerKeyExchange )
3543 * ( CertificateRequest )
3546 case SSL_SERVER_HELLO
:
3547 ret
= ssl_write_server_hello( ssl
);
3550 case SSL_SERVER_CERTIFICATE
:
3551 ret
= ssl_write_certificate( ssl
);
3554 case SSL_SERVER_KEY_EXCHANGE
:
3555 ret
= ssl_write_server_key_exchange( ssl
);
3558 case SSL_CERTIFICATE_REQUEST
:
3559 ret
= ssl_write_certificate_request( ssl
);
3562 case SSL_SERVER_HELLO_DONE
:
3563 ret
= ssl_write_server_hello_done( ssl
);
3567 * <== ( Certificate/Alert )
3569 * ( CertificateVerify )
3573 case SSL_CLIENT_CERTIFICATE
:
3574 ret
= ssl_parse_certificate( ssl
);
3577 case SSL_CLIENT_KEY_EXCHANGE
:
3578 ret
= ssl_parse_client_key_exchange( ssl
);
3581 case SSL_CERTIFICATE_VERIFY
:
3582 ret
= ssl_parse_certificate_verify( ssl
);
3585 case SSL_CLIENT_CHANGE_CIPHER_SPEC
:
3586 ret
= ssl_parse_change_cipher_spec( ssl
);
3589 case SSL_CLIENT_FINISHED
:
3590 ret
= ssl_parse_finished( ssl
);
3594 * ==> ( NewSessionTicket )
3598 case SSL_SERVER_CHANGE_CIPHER_SPEC
:
3599 #if defined(POLARSSL_SSL_SESSION_TICKETS)
3600 if( ssl
->handshake
->new_session_ticket
!= 0 )
3601 ret
= ssl_write_new_session_ticket( ssl
);
3604 ret
= ssl_write_change_cipher_spec( ssl
);
3607 case SSL_SERVER_FINISHED
:
3608 ret
= ssl_write_finished( ssl
);
3611 case SSL_FLUSH_BUFFERS
:
3612 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3613 ssl
->state
= SSL_HANDSHAKE_WRAPUP
;
3616 case SSL_HANDSHAKE_WRAPUP
:
3617 ssl_handshake_wrapup( ssl
);
3621 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl
->state
) );
3622 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA
);
3627 #endif /* POLARSSL_SSL_SRV_C */