Commit 71ac6d7e authored by Krzysztof Karas's avatar Krzysztof Karas Committed by Tomasz Zawadzki
Browse files

hello_sock: Use unhexlified PSK in global array.



Later in the series asynchronous TLS handshake is
introduced. This means that memory holding the
unhexlified PSK may be used outside hello_sock_connect()
function in which it was previously declared, so move
unhexlifying to hello_sock_parse_arg() and use binary
PSK in global array.

Additionally group impl_opts.* assignments together.

Change-Id: I7a93247bedb00e850c1f8a979f837603e35fd563
Signed-off-by: default avatarKrzysztof Karas <krzysztof.karas@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/19813


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Community-CI: Mellanox Build Bot
parent 0463742f
Loading
Loading
Loading
Loading
+23 −39
Original line number Diff line number Diff line
@@ -29,7 +29,8 @@ static int g_zcopy;
static int g_ktls;
static int g_tls_version;
static bool g_verbose;
static char *g_psk_key;
static uint8_t g_psk_key[SPDK_TLS_PSK_MAX_LEN];
static uint32_t g_psk_key_size;
static char *g_psk_identity;

/*
@@ -44,7 +45,8 @@ struct hello_context_t {
	int zcopy;
	int ktls;
	int tls_version;
	char *psk_key;
	uint8_t *psk_key;
	uint32_t psk_key_size;
	char *psk_identity;

	bool verbose;
@@ -89,9 +91,22 @@ hello_sock_usage(void)
static int
hello_sock_parse_arg(int ch, char *arg)
{
	char *unhexlified;

	switch (ch) {
	case 'E':
		g_psk_key = arg;
		g_psk_key_size = strlen(arg) / 2;
		if (g_psk_key_size > SPDK_TLS_PSK_MAX_LEN) {
			fprintf(stderr, "Invalid PSK: too long (%"PRIu32")\n", g_psk_key_size);
			return -EINVAL;
		}
		unhexlified = spdk_unhexlify(arg);
		if (unhexlified == NULL) {
			fprintf(stderr, "Invalid PSK: not in a hex format\n");
			return -EINVAL;
		}
		memcpy(g_psk_key, unhexlified, g_psk_key_size);
		free(unhexlified);
		break;
	case 'H':
		g_host = arg;
@@ -268,14 +283,14 @@ hello_sock_connect(struct hello_context_t *ctx)
	struct spdk_sock_impl_opts impl_opts;
	size_t impl_opts_size = sizeof(impl_opts);
	struct spdk_sock_opts opts;
	char psk[SPDK_TLS_PSK_MAX_LEN] = {};
	char *unhexlified;

	spdk_sock_impl_get_opts(ctx->sock_impl_name, &impl_opts, &impl_opts_size);
	impl_opts.enable_ktls = ctx->ktls;
	impl_opts.tls_version = ctx->tls_version;
	impl_opts.psk_identity = ctx->psk_identity;
	impl_opts.tls_cipher_suites = "TLS_AES_128_GCM_SHA256";
	impl_opts.psk_key = ctx->psk_key;
	impl_opts.psk_key_size = ctx->psk_key_size;

	opts.opts_size = sizeof(opts);
	spdk_sock_get_default_opts(&opts);
@@ -283,22 +298,6 @@ hello_sock_connect(struct hello_context_t *ctx)
	opts.impl_opts = &impl_opts;
	opts.impl_opts_size = sizeof(impl_opts);

	if (ctx->psk_key) {
		impl_opts.psk_key_size = strlen(ctx->psk_key) / 2;
		if (impl_opts.psk_key_size > SPDK_TLS_PSK_MAX_LEN) {
			SPDK_ERRLOG("Insufficient buffer size for PSK");
			return -EINVAL;
		}
		unhexlified = spdk_unhexlify(ctx->psk_key);
		if (unhexlified == NULL) {
			SPDK_ERRLOG("Could not unhexlify PSK");
			return -EINVAL;
		}
		memcpy(psk, unhexlified, impl_opts.psk_key_size);
		free(unhexlified);
		impl_opts.psk_key = psk;
	}

	SPDK_NOTICELOG("Connecting to the server on %s:%d with sock_impl(%s)\n", ctx->host, ctx->port,
		       ctx->sock_impl_name);

@@ -446,14 +445,14 @@ hello_sock_listen(struct hello_context_t *ctx)
	struct spdk_sock_impl_opts impl_opts;
	size_t impl_opts_size = sizeof(impl_opts);
	struct spdk_sock_opts opts;
	static char psk[SPDK_TLS_PSK_MAX_LEN] = {};
	char *unhexlified;

	spdk_sock_impl_get_opts(ctx->sock_impl_name, &impl_opts, &impl_opts_size);
	impl_opts.enable_ktls = ctx->ktls;
	impl_opts.tls_version = ctx->tls_version;
	impl_opts.psk_identity = ctx->psk_identity;
	impl_opts.tls_cipher_suites = "TLS_AES_128_GCM_SHA256";
	impl_opts.psk_key = ctx->psk_key;
	impl_opts.psk_key_size = ctx->psk_key_size;

	opts.opts_size = sizeof(opts);
	spdk_sock_get_default_opts(&opts);
@@ -461,22 +460,6 @@ hello_sock_listen(struct hello_context_t *ctx)
	opts.impl_opts = &impl_opts;
	opts.impl_opts_size = sizeof(impl_opts);

	if (ctx->psk_key) {
		impl_opts.psk_key_size = strlen(ctx->psk_key) / 2;
		if (impl_opts.psk_key_size > SPDK_TLS_PSK_MAX_LEN) {
			SPDK_ERRLOG("Insufficient buffer size for PSK");
			return -EINVAL;
		}
		unhexlified = spdk_unhexlify(ctx->psk_key);
		if (unhexlified == NULL) {
			SPDK_ERRLOG("Could not unhexlify PSK");
			return -EINVAL;
		}
		memcpy(psk, unhexlified, impl_opts.psk_key_size);
		free(unhexlified);
		impl_opts.psk_key = psk;
	}

	ctx->sock = spdk_sock_listen_ext(ctx->host, ctx->port, ctx->sock_impl_name, &opts);
	if (ctx->sock == NULL) {
		SPDK_ERRLOG("Cannot create server socket\n");
@@ -563,6 +546,7 @@ main(int argc, char **argv)
	hello_context.ktls = g_ktls;
	hello_context.tls_version = g_tls_version;
	hello_context.psk_key = g_psk_key;
	hello_context.psk_key_size = g_psk_key_size;
	hello_context.psk_identity = g_psk_identity;
	hello_context.verbose = g_verbose;