108 changes: 0 additions & 108 deletions lib/tcti/tpm2_tools_tcti_socket.c

This file was deleted.

47 changes: 0 additions & 47 deletions lib/tcti/tpm2_tools_tcti_socket.h

This file was deleted.

98 changes: 7 additions & 91 deletions lib/tpm2_options.c
Expand Up @@ -42,36 +42,9 @@

#include "log.h"
#include "tpm2_options.h"
#include "tpm2_tcti_ldr.h"
#include "tpm2_util.h"

#ifdef HAVE_TCTI_DEV
#include "tpm2_tools_tcti_device.h"
#endif
#ifdef HAVE_TCTI_SOCK
#include "tpm2_tools_tcti_socket.h"
#endif
#ifdef HAVE_TCTI_TABRMD
#include "tpm2_tools_tcti_abrmd.h"
#endif

/*
* Default TCTI: this is a bit awkward since we allow users to enable /
* disable TCTIs using ./configure --with/--without magic.
* As simply put as possible:
* if the tabrmd TCTI is enabled, it's the default.
* else if the socket TCTI is enabled it's the default.
* else if the device TCTI is enabled it's the default.
* We do this to preserve the current default / expected behavior (use of
* the socket TCTI).
*/
#ifdef HAVE_TCTI_TABRMD
#define TCTI_DEFAULT_STR "abrmd"
#elif HAVE_TCTI_SOCK
#define TCTI_DEFAULT_STR "socket"
#elif HAVE_TCTI_DEV
#define TCTI_DEFAULT_STR "device"
#endif

#ifndef VERSION
#warning "VERSION Not known at compile time, not embedding..."
#define VERSION "UNKNOWN"
Expand Down Expand Up @@ -161,27 +134,6 @@ void tpm2_options_free(tpm2_options *opts) {
free(opts);
}

#define ADD_TCTI(xname, xinit) { .name = xname, .init = xinit }

/*
* map a string "nice" name of a tcti to a tcti initialization
* routine.
*/
struct {
char *name;
tcti_init init;
} tcti_map_table[] = {
#ifdef HAVE_TCTI_DEV
ADD_TCTI("device", tpm2_tools_tcti_device_init),
#endif
#ifdef HAVE_TCTI_SOCK
ADD_TCTI("socket", tpm2_tools_tcti_socket_init),
#endif
#ifdef HAVE_TCTI_TABRMD
ADD_TCTI("abrmd", tpm2_tools_tcti_abrmd_init)
#endif
};

static char *tcti_get_opts(char *optstr) {

char *split = strchr(optstr, ':');
Expand Down Expand Up @@ -230,27 +182,7 @@ static bool execute_man(char *prog_name) {
}

static void show_version (const char *name) {
#ifdef HAVE_TCTI_TABRMD
#define TCTI_TABRMD_CONF "tabrmd,"
#else
#define TCTI_TABRMD_CONF ""
#endif

#ifdef HAVE_TCTI_SOCK
#define TCTI_SOCK_CONF "socket,"
#else
#define TCTI_SOCK_CONF ""
#endif

#ifdef HAVE_TCTI_DEV
#define TCTI_DEV_CONF "device,"
#else
#define TCTI_DEV_CONF ""
#endif

static const char *tcti_conf = TCTI_TABRMD_CONF TCTI_SOCK_CONF TCTI_DEV_CONF;
printf("tool=\"%s\" version=\"%s\" tctis=\"%s\"\n", name, VERSION,
tcti_conf);
printf("tool=\"%s\" version=\"%s\" tctis=\"dynamic\"\n", name, VERSION);
}

void tpm2_print_usage(const char *command, struct tpm2_options *tool_opts) {
Expand Down Expand Up @@ -305,7 +237,7 @@ tpm2_option_code tpm2_handle_options (int argc, char **argv, char **envp,
};

char *tcti_opts = NULL;
char *tcti_name = TCTI_DEFAULT_STR;
char *tcti_name = "abrmd";
char *env_str = getenv (TPM2TOOLS_ENV_TCTI_NAME);
tcti_name = env_str ? env_str : tcti_name;

Expand Down Expand Up @@ -392,26 +324,10 @@ tpm2_option_code tpm2_handle_options (int argc, char **argv, char **envp,
}
}

if (!(opts->flags & TPM2_OPTIONS_NO_SAPI)) {
size_t i;
bool found = false;
for(i=0; i < ARRAY_LEN(tcti_map_table); i++) {

char *name = tcti_map_table[i].name;
tcti_init init = tcti_map_table[i].init;
if (!strcmp(tcti_name, name)) {
found = true;
*tcti = init(tcti_opts);
if (!*tcti) {
goto out;
}
}
}

if (!found) {
LOG_ERR("Unknown tcti, got: \"%s\"", tcti_name);
goto out;
}
*tcti = tpm2_tcti_ldr_load(tcti_name, tcti_opts);
if (!*tcti) {
LOG_ERR("Unknown tcti, got: \"%s\"", tcti_name);
goto out;
}

if (!flags->enable_errata) {
Expand Down
119 changes: 119 additions & 0 deletions lib/tpm2_tcti_ldr.c
@@ -0,0 +1,119 @@
//**********************************************************************;
// Copyright (c) 2018, Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//**********************************************************************;

#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

#include <sapi/tpm20.h>

#include "log.h"
#include "tpm2_tcti_ldr.h"

static void *handle;

void tpm2_tcti_ldr_unload(void) {
if (handle) {
dlclose(handle);
}
}

TSS2_TCTI_CONTEXT *tpm2_tcti_ldr_load(const char *path, char *opts) {

static const char tabrmd[7] = { 't', 'a', 'b', 'r', 'm', 'd', '\0' };

TSS2_TCTI_CONTEXT *tcti_ctx = NULL;

if (handle) {
LOG_ERR("Attempting to load multiple tcti's simultaneously is not supported!");
return NULL;
}

/*
* Try what they gave us, if it doesn't load up, try
* libtcti-xxx.so replacing xxx with what they gave us.
*/
handle = dlopen (path, RTLD_LAZY);
if (!handle) {

//fixup users of abrmd as the tcti specifier
path = !strcmp(path, "abrmd") ? tabrmd : path;

char buf[PATH_MAX];
size_t size = snprintf(buf, sizeof(buf), "libtcti-%s.so", path);
if (size >= sizeof(buf)) {
LOG_ERR("Truncated friendly name conversion, got: \"%s\", made: \"%s\"",
path, buf);
return NULL;
}

handle = dlopen (buf, RTLD_LAZY);
if (!handle) {
LOG_ERR("Could not dlopen library: \"%s\"", buf);
return NULL;
}
}

TSS2_TCTI_INFO_FUNC infofn = (TSS2_TCTI_INFO_FUNC)dlsym(handle, TSS2_TCTI_INFO_SYMBOL);
if (!infofn) {
LOG_ERR("Symbol \"%s\"not found in library: \"%s\"",
TSS2_TCTI_INFO_SYMBOL, path);
goto err;
}

const TSS2_TCTI_INFO *info = infofn();

TSS2_TCTI_INIT_FUNC init = info->init;

size_t size;
TSS2_RC rc = init(NULL, &size, opts);
if (rc != TPM2_RC_SUCCESS) {
LOG_ERR("tcti init routine for getting size failed for library: \"%s\"", path);
goto err;
}

tcti_ctx = (TSS2_TCTI_CONTEXT*) calloc(1, size);
if (tcti_ctx == NULL) {
LOG_ERR("oom");
goto err;
}

rc = init(tcti_ctx, &size, opts);
if (rc != TPM2_RC_SUCCESS) {
LOG_ERR("tcti init routine for final initialization failed for library:"
" \"%s\"", path);
goto err;
}

return tcti_ctx;

err:
free(tcti_ctx);
dlclose(handle);
return NULL;
}
35 changes: 20 additions & 15 deletions lib/tcti/tpm2_tools_tcti_abrmd.h → lib/tpm2_tcti_ldr.h
@@ -1,5 +1,5 @@
//**********************************************************************;
// Copyright (c) 2015, Intel Corporation
// Copyright (c) 2018, Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand All @@ -12,10 +12,6 @@
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of Intel Corporation nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -28,21 +24,30 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//**********************************************************************;
#ifndef LIB_TCTI_TPM2_TOOLS_TCTI_ABRMD_H_
#define LIB_TCTI_TPM2_TOOLS_TCTI_ABRMD_H_

#include <sapi/tpm20.h>

#ifndef LIB_TPM2_TCTI_LDR_H_
#define LIB_TPM2_TCTI_LDR_H_

/**
* Initializes a abrmd TCTI from an option string.
@note
* abrmd currently accepts no options.
*
* Loads a TCTI from a friendly name, library name, or path.
* For example
* friendly: path = tabrmd
* library name: path = libtcti-socket.so
* full path: path = /home/user/lib/libtcti-custom.so
* @param path
* The path/library to load.
* @param opts
* The option string, ignored.
* The tcti option configs.
* @return
* NULL on error or an initialized abrmd TCTI.
* A tcti context on success or NULL on failure.
*/
TSS2_TCTI_CONTEXT *tpm2_tcti_ldr_load(const char *path, char *opts);

/**
* Unloads the tcti loaded via tpm2_tcti_ldr_load();
*/
TSS2_TCTI_CONTEXT *tpm2_tools_tcti_abrmd_init(char *opts);
void tpm2_tcti_ldr_unload(void);

#endif /* LIB_TCTI_TPM2_TOOLS_TCTI_ABRMD_H_ */
#endif /* LIB_TPM2_TCTI_LDR_H_ */
3 changes: 3 additions & 0 deletions tools/tpm2_tool.c
Expand Up @@ -33,6 +33,7 @@
#include <unistd.h>

#include "log.h"
#include "tpm2_tcti_ldr.h"
#include "tpm2_options.h"
#include "tpm2_tool.h"
#include "tpm2_util.h"
Expand Down Expand Up @@ -171,5 +172,7 @@ int main(int argc, char *argv[], char *envp[]) {
tpm2_tool_onexit();
}

tpm2_tcti_ldr_unload();

exit(ret);
}