aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Tricca <philip.b.tricca@intel.com>2018-03-03 09:07:48 -0800
committerAndreas Fuchs <andreas.fuchs@sit.fraunhofer.de>2018-03-05 10:07:49 +0100
commitc21dbf3e2bc3fe37cfcff2147e1bad167a8c89cb (patch)
tree6b88bf3d658b165842f4ef90a1ab7e740ed78886
parentc560204d4b0151776cad75f4f812a76206d3c120 (diff)
downloadtpm2-tss-c21dbf3e2bc3fe37cfcff2147e1bad167a8c89cb.tar.gz
libtcti-socket: Remove old init function, data & refactor std init.
Removing all of the old config stuff produced a number of simplification in the new init function. This commit also adds some unit tests for the new function that parses the conf string. Signed-off-by: Philip Tricca <philip.b.tricca@intel.com>
-rw-r--r--include/tcti/tcti_socket.h31
-rw-r--r--tcti/tcti_socket.c201
-rw-r--r--tcti/tcti_socket.map1
-rw-r--r--test/tpmclient/tpmclient.int.c5
-rw-r--r--test/unit/tcti-socket.c74
5 files changed, 192 insertions, 120 deletions
diff --git a/include/tcti/tcti_socket.h b/include/tcti/tcti_socket.h
index d828417f..cdc5e215 100644
--- a/include/tcti/tcti_socket.h
+++ b/include/tcti/tcti_socket.h
@@ -27,20 +27,7 @@
#ifndef TCTI_SOCKET_H
#define TCTI_SOCKET_H
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include "common.h"
-
-#include "sapi/tpm20.h"
-
-#define DEFAULT_SIMULATOR_TPM_PORT 2321
-#define TSS2_SIMULATOR_INTERFACE_INIT_FAILED ((TSS2_RC)(1 + TSS2_DRIVER_ERROR_LEVEL))
-
-#define DEFAULT_HOSTNAME "127.0.0.1"
-
-#define TCTI_SOCKET_DEFAULT "tcp://127.0.0.1:2321"
+#include <sapi/tss2_tcti.h>
/*
* Command codes that may be sent to simulator through out of band command
@@ -54,22 +41,14 @@ extern "C" {
#define MS_SIM_NV_ON 11
#define TPM_SESSION_END 20
+#ifdef __cplusplus
+extern "C" {
+#endif
+
TSS2_RC PlatformCommand(
TSS2_TCTI_CONTEXT *tctiContext,
UINT32 cmd);
-typedef struct {
- const char *hostname;
- uint16_t port;
-} TCTI_SOCKET_CONF;
-
-TSS2_RC InitSocketTcti (
- TSS2_TCTI_CONTEXT *tctiContext, // OUT
- size_t *contextSize, // IN/OUT
- const TCTI_SOCKET_CONF *config, // IN
- const uint8_t serverSockets
- ) COMPILER_ATTR (deprecated);
-
TSS2_RC Tss2_Tcti_Socket_Init (
TSS2_TCTI_CONTEXT *tctiContext,
size_t *size,
diff --git a/tcti/tcti_socket.c b/tcti/tcti_socket.c
index 837bfd7e..d4bf9069 100644
--- a/tcti/tcti_socket.c
+++ b/tcti/tcti_socket.c
@@ -34,7 +34,6 @@
#include <uriparser/Uri.h>
-#include "sapi/tpm20.h"
#include "sapi/tss2_mu.h"
#include "tcti/tcti_socket.h"
#include "sysapi_util.h"
@@ -44,6 +43,9 @@
#define LOGMODULE tcti
#include "log/log.h"
+#define TCTI_SOCKET_DEFAULT_CONF "tcp://127.0.0.1:2321"
+#define TCTI_SOCKET_DEFAULT_PORT 2321
+
static TSS2_RC tctiRecvBytes (
TSS2_TCTI_CONTEXT *tctiContext,
SOCKET sock,
@@ -479,69 +481,6 @@ static TSS2_RC InitializeMsTpm2Simulator(
return rval;
}
-TSS2_RC
-_InitSocketTcti (
- TSS2_TCTI_CONTEXT *tctiContext,
- size_t *contextSize,
- const TCTI_SOCKET_CONF *conf,
- const uint8_t serverSockets
- )
-{
- TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext);
- TSS2_RC rval = TSS2_RC_SUCCESS;
- SOCKET otherSock;
- SOCKET tpmSock;
-
- if (tctiContext == NULL && contextSize == NULL) {
- return TSS2_TCTI_RC_BAD_VALUE;
- } else if( tctiContext == NULL ) {
- *contextSize = sizeof (TSS2_TCTI_CONTEXT_INTEL);
- return TSS2_RC_SUCCESS;
- } else if( conf == NULL ) {
- return TSS2_TCTI_RC_BAD_VALUE;
- }
-
- TSS2_TCTI_MAGIC (tctiContext) = TCTI_MAGIC;
- TSS2_TCTI_VERSION (tctiContext) = TCTI_VERSION;
- TSS2_TCTI_TRANSMIT (tctiContext) = tcti_socket_transmit;
- TSS2_TCTI_RECEIVE (tctiContext) = SocketReceiveTpmResponse;
- TSS2_TCTI_FINALIZE (tctiContext) = SocketFinalize;
- TSS2_TCTI_CANCEL (tctiContext) = SocketCancel;
- TSS2_TCTI_GET_POLL_HANDLES (tctiContext) = SocketGetPollHandles;
- TSS2_TCTI_SET_LOCALITY (tctiContext) = SocketSetLocality;
- TSS2_TCTI_MAKE_STICKY (tctiContext) = tcti_make_sticky_not_implemented;
- tcti_intel->status.locality = 3;
- tcti_intel->status.commandSent = 0;
- tcti_intel->status.tagReceived = 0;
- tcti_intel->status.responseSizeReceived = 0;
- tcti_intel->status.protocolResponseSizeReceived = 0;
- tcti_intel->currentTctiContext = 0;
- tcti_intel->previousStage = TCTI_STAGE_INITIALIZE;
-
- rval = (TSS2_RC) InitSockets (conf->hostname,
- conf->port,
- &otherSock,
- &tpmSock);
- if (rval == TSS2_RC_SUCCESS) {
- tcti_intel->otherSock = otherSock;
- tcti_intel->tpmSock = tpmSock;
- rval = InitializeMsTpm2Simulator (tctiContext);
- } else {
- CloseSockets (otherSock, tpmSock);
- }
-
- return rval;
-}
-TSS2_RC
-InitSocketTcti (
- TSS2_TCTI_CONTEXT *tctiContext,
- size_t *contextSize,
- const TCTI_SOCKET_CONF *conf,
- const uint8_t serverSockets
- )
-{
- return _InitSocketTcti (tctiContext, contextSize, conf, serverSockets);
-}
/*
* This is a utility function to extract a TCP port number from a string.
* The string must be 6 characters long. If the supplied string contains an
@@ -557,25 +496,35 @@ string_to_port (char port_str[6])
}
return port;
}
-
-TSS2_RC Tss2_Tcti_Socket_Init (
- TSS2_TCTI_CONTEXT *tctiContext,
- size_t *size,
- const char *conf
- )
+/*
+ * This function extracts the hostname and port part of the provided conf
+ * string (which is really just a URI). The hostname parameter is an output
+ * buffer that must be large enough to hold the hostname. HOST_NAME_MAX is
+ * probably a good size. The 'port' parameter is an output parameter where
+ * we store the port from the URI after we convert it to a uint16.
+ * If the URI does not contain a port number then the contents of the 'port'
+ * parameter will not be changed.
+ * This function returns TSS2_RC_SUCCESS when the 'hostname' and 'port' have
+ * been populated successfully. On failure it will return
+ * TSS2_TCTI_RC_BAD_VALUE to indicate that the provided conf string contains
+ * values that we can't parse or are invalid.
+ */
+TSS2_RC
+conf_str_to_host_port (
+ const char *conf,
+ char *hostname,
+ uint16_t *port)
{
- TCTI_SOCKET_CONF sock_conf = { 0 };
- TSS2_RC rc;
UriParserStateA state;
UriUriA uri;
- const char *uri_str = conf != NULL ? conf : TCTI_SOCKET_DEFAULT;
- size_t range;
/* maximum 5 digits in uint16_t + 1 for \0 */
- char port[6] = { 0 };
- char hostname[HOST_NAME_MAX + 1] = { 0 };
+ char port_str[6] = { 0 };
+ size_t range;
+ TSS2_RC rc = TSS2_RC_SUCCESS;
state.uri = &uri;
- if (uriParseUriA (&state, uri_str) != URI_SUCCESS) {
+ if (uriParseUriA (&state, conf) != URI_SUCCESS) {
+ LOG_WARNING ("Failed to parse provided conf string: %s", conf);
rc = TSS2_TCTI_RC_BAD_VALUE;
goto out;
}
@@ -583,33 +532,109 @@ TSS2_RC Tss2_Tcti_Socket_Init (
/* extract host & domain name / fqdn */
range = uri.hostText.afterLast - uri.hostText.first;
if (range > HOST_NAME_MAX) {
+ LOG_WARNING ("Provided conf string has hostname that exceeds "
+ "HOST_NAME_MAX.");
rc = TSS2_TCTI_RC_BAD_VALUE;
goto out;
}
strncpy (hostname, uri.hostText.first, range);
- sock_conf.hostname = hostname;
/* extract port number */
range = uri.portText.afterLast - uri.portText.first;
- if (range <= 5 && range > 0) {
- strncpy (port, uri.portText.first, range);
- sock_conf.port = string_to_port (port);
- if (sock_conf.port == 0) {
- rc = TSS2_TCTI_RC_BAD_VALUE;
- goto out;
- }
+ if (range > 5) {
+ LOG_WARNING ("conf string contains invalid port.");
+ rc = TSS2_TCTI_RC_BAD_VALUE;
+ goto out;
} else if (range == 0) {
- sock_conf.port = DEFAULT_SIMULATOR_TPM_PORT;
- } else { /* range > 5 */
+ LOG_INFO ("conf string does not contain a port.");
+ goto out;
+ }
+
+ strncpy (port_str, uri.portText.first, range);
+ *port = string_to_port (port_str);
+ if (*port == 0) {
+ LOG_WARNING ("Provided conf string contains invalid port: 0");
rc = TSS2_TCTI_RC_BAD_VALUE;
goto out;
}
- rc = _InitSocketTcti (tctiContext, size, &sock_conf, 0);
out:
uriFreeUriMembersA (&uri);
return rc;
}
+void
+tcti_socket_init_context_data (
+ TSS2_TCTI_CONTEXT *tcti_ctx)
+{
+ TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tcti_ctx);
+
+ TSS2_TCTI_MAGIC (tcti_ctx) = TCTI_MAGIC;
+ TSS2_TCTI_VERSION (tcti_ctx) = TCTI_VERSION;
+ TSS2_TCTI_TRANSMIT (tcti_ctx) = tcti_socket_transmit;
+ TSS2_TCTI_RECEIVE (tcti_ctx) = SocketReceiveTpmResponse;
+ TSS2_TCTI_FINALIZE (tcti_ctx) = SocketFinalize;
+ TSS2_TCTI_CANCEL (tcti_ctx) = SocketCancel;
+ TSS2_TCTI_GET_POLL_HANDLES (tcti_ctx) = SocketGetPollHandles;
+ TSS2_TCTI_SET_LOCALITY (tcti_ctx) = SocketSetLocality;
+ TSS2_TCTI_MAKE_STICKY (tcti_ctx) = tcti_make_sticky_not_implemented;
+ tcti_intel->status.locality = 3;
+ tcti_intel->status.commandSent = 0;
+ tcti_intel->status.tagReceived = 0;
+ tcti_intel->status.responseSizeReceived = 0;
+ tcti_intel->status.protocolResponseSizeReceived = 0;
+ tcti_intel->currentTctiContext = 0;
+ tcti_intel->previousStage = TCTI_STAGE_INITIALIZE;
+}
+/*
+ * This is an implementation of the standard TCTI initialization function for
+ * this module.
+ */
+TSS2_RC
+Tss2_Tcti_Socket_Init (
+ TSS2_TCTI_CONTEXT *tctiContext,
+ size_t *size,
+ const char *conf)
+{
+ TSS2_TCTI_CONTEXT_INTEL *tcti_intel = tcti_context_intel_cast (tctiContext);
+ TSS2_RC rc;
+ const char *uri_str = conf != NULL ? conf : TCTI_SOCKET_DEFAULT_CONF;
+ char hostname[HOST_NAME_MAX + 1] = { 0 };
+ uint16_t port = TCTI_SOCKET_DEFAULT_PORT;
+
+ if (tctiContext == NULL && size == NULL) {
+ return TSS2_TCTI_RC_BAD_VALUE;
+ } else if( tctiContext == NULL ) {
+ *size = sizeof (TSS2_TCTI_CONTEXT_INTEL);
+ return TSS2_RC_SUCCESS;
+ } else if( conf == NULL ) {
+ return TSS2_TCTI_RC_BAD_VALUE;
+ }
+
+ rc = conf_str_to_host_port (uri_str, hostname, &port);
+ if (rc != TSS2_RC_SUCCESS) {
+ return rc;
+ }
+
+ rc = (TSS2_RC) InitSockets (hostname,
+ port,
+ &tcti_intel->otherSock,
+ &tcti_intel->tpmSock);
+ if (rc != TSS2_RC_SUCCESS) {
+ CloseSockets (tcti_intel->otherSock, tcti_intel->tpmSock);
+ goto out;
+ }
+
+ rc = InitializeMsTpm2Simulator (tctiContext);
+ if (rc != TSS2_RC_SUCCESS) {
+ CloseSockets (tcti_intel->otherSock, tcti_intel->tpmSock);
+ goto out;
+ }
+
+ tcti_socket_init_context_data (tctiContext);
+out:
+ return rc;
+}
+
/* public info structure */
const static TSS2_TCTI_INFO tss2_tcti_info = {
.version = {
diff --git a/tcti/tcti_socket.map b/tcti/tcti_socket.map
index 94e2f17a..14731276 100644
--- a/tcti/tcti_socket.map
+++ b/tcti/tcti_socket.map
@@ -1,6 +1,5 @@
{
global:
- InitSocketTcti;
PlatformCommand;
Tss2_Tcti_Info;
Tss2_Tcti_Socket_Init;
diff --git a/test/tpmclient/tpmclient.int.c b/test/tpmclient/tpmclient.int.c
index 0946bfe9..ea3b0059 100644
--- a/test/tpmclient/tpmclient.int.c
+++ b/test/tpmclient/tpmclient.int.c
@@ -95,11 +95,6 @@ UINT8 indent = 0;
TSS2_SYS_CONTEXT *sysContext;
-TCTI_SOCKET_CONF rmInterfaceConfig = {
- DEFAULT_HOSTNAME,
- DEFAULT_SIMULATOR_TPM_PORT
-};
-
TSS2_TCTI_CONTEXT *resMgrTctiContext = 0;
TSS2_ABI_VERSION abiVersion = { TSSWG_INTEROP, TSS_SAPI_FIRST_FAMILY, TSS_SAPI_FIRST_LEVEL, TSS_SAPI_FIRST_VERSION };
diff --git a/test/unit/tcti-socket.c b/test/unit/tcti-socket.c
index 2b1d4dd8..e94664c5 100644
--- a/test/unit/tcti-socket.c
+++ b/test/unit/tcti-socket.c
@@ -26,6 +26,7 @@
//**********************************************************************;
#include <inttypes.h>
+#include <limits.h>
#include <stdio.h>
#include <stdbool.h>
@@ -36,6 +37,75 @@
#include "tcti/tcti.h"
#include "tcti/tcti_socket.h"
+/*
+ * This function is implemented in the socket TCTI module but not exposed
+ * through the public headers.
+ */
+TSS2_RC
+conf_str_to_host_port (
+ const char *conf,
+ char *hostname,
+ uint16_t *port);
+/* */
+static void
+conf_str_to_host_port_success_test (void **state)
+{
+ TSS2_RC rc;
+ char *conf = "tcp://127.0.0.1:2321";
+ char hostname [HOST_NAME_MAX] = { 0 };
+ uint16_t port;
+
+ rc = conf_str_to_host_port (conf, hostname, &port);
+ assert_int_equal (rc, TSS2_RC_SUCCESS);
+ assert_int_equal (port, 2321);
+ assert_string_equal (hostname, "127.0.0.1");
+}
+/*
+ * This tests our ability to handle conf strings that don't have the port
+ * component of the URI. In this case the 'conf_str_to_host_port' function
+ * should not touch the 'port' parameter and so we check to be sure it's
+ * unchanged.
+ */
+#define NO_PORT_VALUE 646
+static void
+conf_str_to_host_port_no_port_test (void **state)
+{
+ TSS2_RC rc;
+ char *conf = "tcp://127.0.0.1";
+ char hostname [HOST_NAME_MAX] = { 0 };
+ uint16_t port = NO_PORT_VALUE;
+
+ rc = conf_str_to_host_port (conf, hostname, &port);
+ assert_int_equal (rc, TSS2_RC_SUCCESS);
+ assert_int_equal (port, NO_PORT_VALUE);
+}
+/*
+ * The 'conf_str_to_host_port' function rejects ports over UINT16_MAX.
+ */
+static void
+conf_str_to_host_port_invalid_port_large_test (void **state)
+{
+ TSS2_RC rc;
+ char *conf = "tcp://127.0.0.1:99999";
+ char hostname [HOST_NAME_MAX] = { 0 };
+ uint16_t port;
+
+ rc = conf_str_to_host_port (conf, hostname, &port);
+ assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
+}
+/* The 'conf_str_to_host_port' function rejects URIs with port == 0 */
+static void
+conf_str_to_host_port_invalid_port_0_test (void **state)
+{
+ TSS2_RC rc;
+ char *conf = "tcp://127.0.0.1:0";
+ char hostname [HOST_NAME_MAX] = { 0 };
+ uint16_t port;
+
+ rc = conf_str_to_host_port (conf, hostname, &port);
+ assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
+}
+
/* When passed all NULL values ensure that we get back the expected RC. */
static void
tcti_socket_init_all_null_test (void **state)
@@ -261,6 +331,10 @@ main (int argc,
char *argv[])
{
const struct CMUnitTest tests[] = {
+ cmocka_unit_test (conf_str_to_host_port_success_test),
+ cmocka_unit_test (conf_str_to_host_port_no_port_test),
+ cmocka_unit_test (conf_str_to_host_port_invalid_port_large_test),
+ cmocka_unit_test (conf_str_to_host_port_invalid_port_0_test),
cmocka_unit_test (tcti_socket_init_all_null_test),
cmocka_unit_test (tcti_socket_init_size_test),
cmocka_unit_test_setup_teardown (tcti_socket_receive_success_test,