Add support for change of CID to ssl_client2 / ssl_server2
And add tests for various CID configuration changes during
renegotiation to ssl-opt.sh.
diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c
index 910bd52..6b5edb2 100644
--- a/programs/ssl/ssl_server2.c
+++ b/programs/ssl/ssl_server2.c
@@ -139,6 +139,8 @@
#define DFL_SHA1 -1
#define DFL_CID_ENABLED 0
#define DFL_CID_VALUE ""
+#define DFL_CID_ENABLED_RENEGO -1
+#define DFL_CID_VALUE_RENEGO NULL
#define DFL_AUTH_MODE -1
#define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
#define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
@@ -228,8 +230,12 @@
#define USAGE_CID \
" cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
" default: 0 (disabled)\n" \
+ " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
+ " default: same as 'cid'\n" \
" cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
- " default: \"\"\n"
+ " default: \"\"\n" \
+ " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
+ " default: same as 'cid_val'\n"
#else /* MBEDTLS_SSL_CID */
#define USAGE_CID ""
#endif /* MBEDTLS_SSL_CID */
@@ -523,7 +529,11 @@
int dgram_packing; /* allow/forbid datagram packing */
int badmac_limit; /* Limit of records with bad MAC */
int cid_enabled; /* whether to use the CID extension or not */
+ int cid_enabled_renego; /* whether to use the CID extension or not
+ * during renegotiation */
const char *cid_val; /* the CID to use for incoming messages */
+ const char *cid_val_renego; /* the CID to use for incoming messages
+ * after renegotiation */
} opt;
int query_config( const char *config );
@@ -1214,6 +1224,56 @@
return( 0 );
}
+#if defined(MBEDTLS_SSL_CID)
+int report_cid_usage( mbedtls_ssl_context *ssl,
+ const char *additional_description )
+{
+ int ret;
+ unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
+ size_t peer_cid_len;
+ int cid_negotiated;
+
+ if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
+ return( 0 );
+
+ /* Check if the use of a CID has been negotiated */
+ ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
+ peer_cid, &peer_cid_len );
+ if( ret != 0 )
+ {
+ mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
+ -ret );
+ return( ret );
+ }
+
+ if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
+ {
+ if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
+ {
+ mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
+ additional_description );
+ }
+ }
+ else
+ {
+ size_t idx=0;
+ mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
+ additional_description );
+ mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
+ additional_description,
+ (unsigned) peer_cid_len );
+ while( idx < peer_cid_len )
+ {
+ mbedtls_printf( "%02x ", peer_cid[ idx ] );
+ idx++;
+ }
+ mbedtls_printf( "\n" );
+ }
+
+ return( 0 );
+}
+#endif /* MBEDTLS_SSL_CID */
+
int main( int argc, char *argv[] )
{
int ret = 0, len, written, frags, exchanges_left;
@@ -1281,7 +1341,9 @@
#if defined(MBEDTLS_SSL_CID)
unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
+ unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
size_t cid_len = 0;
+ size_t cid_renego_len = 0;
#endif
int i;
@@ -1362,7 +1424,9 @@
opt.response_size = DFL_RESPONSE_SIZE;
opt.nbio = DFL_NBIO;
opt.cid_enabled = DFL_CID_ENABLED;
+ opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
opt.cid_val = DFL_CID_VALUE;
+ opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
opt.read_timeout = DFL_READ_TIMEOUT;
opt.ca_file = DFL_CA_FILE;
opt.ca_path = DFL_CA_PATH;
@@ -1508,10 +1572,20 @@
if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
goto usage;
}
+ else if( strcmp( p, "cid_renego" ) == 0 )
+ {
+ opt.cid_enabled_renego = atoi( q );
+ if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
+ goto usage;
+ }
else if( strcmp( p, "cid_val" ) == 0 )
{
opt.cid_val = q;
}
+ else if( strcmp( p, "cid_val_renego" ) == 0 )
+ {
+ opt.cid_val_renego = q;
+ }
#endif /* MBEDTLS_SSL_CID */
else if( strcmp( p, "psk" ) == 0 )
opt.psk = q;
@@ -1920,22 +1994,26 @@
}
}
-#if defined(MBEDTLS_SSL_CID)
- if( strlen( opt.cid_val ) )
- {
- cid_len = strlen( opt.cid_val ) / 2;
- if( cid_len > sizeof( cid ) )
- {
- mbedtls_printf( "CID too long\n" );
- goto exit;
- }
- if( unhexify( cid, opt.cid_val, &cid_len ) != 0 )
- {
- mbedtls_printf( "CID not valid hex\n" );
- goto exit;
- }
- }
+#if defined(MBEDTLS_SSL_CID)
+ if( unhexify( cid, opt.cid_val, &cid_len ) != 0 )
+ {
+ mbedtls_printf( "CID not valid hex\n" );
+ goto exit;
+ }
+
+ /* Keep CID settings for renegotiation unless
+ * specified otherwise. */
+ if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
+ opt.cid_enabled_renego = opt.cid_enabled;
+ if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
+ opt.cid_val_renego = opt.cid_val;
+
+ if( unhexify( cid_renego, opt.cid_val_renego, &cid_renego_len ) != 0 )
+ {
+ mbedtls_printf( "CID not valid hex\n" );
+ goto exit;
+ }
#endif /* MBEDTLS_SSL_CID */
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
@@ -2310,9 +2388,21 @@
#endif
#if defined(MBEDTLS_SSL_CID)
- if( opt.cid_enabled == 1 )
+ if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
{
- ret = mbedtls_ssl_conf_cid_len( &conf, cid_len );
+ if( opt.cid_enabled == 1 &&
+ opt.cid_enabled_renego == 1 &&
+ cid_len != cid_renego_len )
+ {
+ mbedtls_printf( "CID length must not change during renegotiation\n" );
+ goto usage;
+ }
+
+ if( opt.cid_enabled == 1 )
+ ret = mbedtls_ssl_conf_cid_len( &conf, cid_len );
+ else
+ ret = mbedtls_ssl_conf_cid_len( &conf, cid_renego_len );
+
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned %d\n\n",
@@ -2869,42 +2959,19 @@
#endif /* MBEDTLS_X509_CRT_PARSE_C */
#if defined(MBEDTLS_SSL_CID)
+ ret = report_cid_usage( &ssl, "initial handshake" );
+ if( ret != 0 )
+ goto exit;
+
if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
{
- unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
- size_t peer_cid_len;
- int cid_negotiated;
-
- /* Check if the use of a CID has been negotiated */
- ret = mbedtls_ssl_get_peer_cid( &ssl, &cid_negotiated,
- peer_cid, &peer_cid_len );
- if( ret != 0 )
+ if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
+ cid_renego, cid_renego_len ) ) != 0 )
{
- mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
- -ret );
+ mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
+ ret );
goto exit;
}
-
- if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
- {
- if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
- {
- mbedtls_printf( "Use of Connection ID was not offered by the client.\n" );
- }
- }
- else
- {
- size_t idx=0;
- mbedtls_printf( "Use of Connection ID has been negotiated.\n" );
- mbedtls_printf( "Peer CID (length %u Bytes): ",
- (unsigned) peer_cid_len );
- while( idx < peer_cid_len )
- {
- mbedtls_printf( "%02x ", peer_cid[ idx ] );
- idx++;
- }
- mbedtls_printf( "\n" );
- }
}
#endif /* MBEDTLS_SSL_CID */
@@ -3118,6 +3185,10 @@
}
#endif /* MBEDTLS_SSL_RENEGOTIATION */
+ ret = report_cid_usage( &ssl, "after renegotiation" );
+ if( ret != 0 )
+ goto exit;
+
/*
* 7. Write the 200 Response
*/