Added pre-shared key handling for the server side of SSL / TLS
Server side handling of the pure PSK ciphersuites is now in the base
code.
diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c
index abca186..73c3327 100644
--- a/programs/ssl/ssl_server2.c
+++ b/programs/ssl/ssl_server2.c
@@ -56,6 +56,8 @@
#define DFL_CA_PATH ""
#define DFL_CRT_FILE ""
#define DFL_KEY_FILE ""
+#define DFL_PSK ""
+#define DFL_PSK_IDENTITY "Client_identity"
#define DFL_FORCE_CIPHER 0
#define DFL_RENEGOTIATION SSL_RENEGOTIATION_ENABLED
#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
@@ -78,6 +80,8 @@
char *ca_path; /* the path with the CA certificate(s) reside */
char *crt_file; /* the file with the client certificate */
char *key_file; /* the file with the client key */
+ char *psk; /* the pre-shared key */
+ char *psk_identity; /* the pre-shared key identity */
int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
int renegotiation; /* enable / disable renegotiation */
int allow_legacy; /* allow legacy renegotiation */
@@ -121,6 +125,9 @@
" options: ssl3, tls1, tls1_1, tls1_2\n" \
" auth_mode=%%s default: \"optional\"\n" \
" options: none, optional, required\n" \
+ " psk=%%s default: \"\" (in hex, without 0x)\n" \
+ " psk_identity=%%s default: \"Client_identity\"\n" \
+ "\n" \
" force_ciphersuite=<name> default: all enabled\n"\
" acceptable ciphersuite names:\n"
@@ -146,6 +153,8 @@
int listen_fd;
int client_fd = -1;
unsigned char buf[1024];
+ unsigned char psk[256];
+ size_t psk_len = 0;
char *pers = "ssl_server2";
entropy_context entropy;
@@ -197,6 +206,8 @@
opt.ca_path = DFL_CA_PATH;
opt.crt_file = DFL_CRT_FILE;
opt.key_file = DFL_KEY_FILE;
+ opt.psk = DFL_PSK;
+ opt.psk_identity = DFL_PSK_IDENTITY;
opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
opt.renegotiation = DFL_RENEGOTIATION;
opt.allow_legacy = DFL_ALLOW_LEGACY;
@@ -230,6 +241,10 @@
opt.crt_file = q;
else if( strcmp( p, "key_file" ) == 0 )
opt.key_file = q;
+ else if( strcmp( p, "psk" ) == 0 )
+ opt.psk = q;
+ else if( strcmp( p, "psk_identity" ) == 0 )
+ opt.psk_identity = q;
else if( strcmp( p, "force_ciphersuite" ) == 0 )
{
opt.force_ciphersuite[0] = -1;
@@ -283,6 +298,54 @@
}
/*
+ * Unhexify the pre-shared key if any is given
+ */
+ if( strlen( opt.psk ) )
+ {
+ unsigned char c;
+ size_t j;
+
+ if( strlen( opt.psk ) % 2 != 0 )
+ {
+ printf("pre-shared key not valid hex\n");
+ goto exit;
+ }
+
+ psk_len = strlen( opt.psk ) / 2;
+
+ for( j = 0; j < strlen( opt.psk ); j += 2 )
+ {
+ c = opt.psk[j];
+ if( c >= '0' && c <= '9' )
+ c -= '0';
+ else if( c >= 'a' && c <= 'f' )
+ c -= 'a' - 10;
+ else if( c >= 'A' && c <= 'F' )
+ c -= 'A' - 10;
+ else
+ {
+ printf("pre-shared key not valid hex\n");
+ goto exit;
+ }
+ psk[ j / 2 ] = c << 4;
+
+ c = opt.psk[j + 1];
+ if( c >= '0' && c <= '9' )
+ c -= '0';
+ else if( c >= 'a' && c <= 'f' )
+ c -= 'a' - 10;
+ else if( c >= 'A' && c <= 'F' )
+ c -= 'A' - 10;
+ else
+ {
+ printf("pre-shared key not valid hex\n");
+ goto exit;
+ }
+ psk[ j / 2 ] |= c;
+ }
+ }
+
+ /*
* 0. Initialize the RNG and the session data
*/
printf( "\n . Seeding the random number generator..." );
@@ -421,6 +484,8 @@
ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
ssl_set_own_cert( &ssl, &srvcert, &rsa );
+ ssl_set_psk( &ssl, psk, psk_len, (unsigned char *) opt.psk_identity,
+ strlen( opt.psk_identity ) );
#if defined(POLARSSL_DHM_C)
/*