| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //**********************************************************************; | ||
| // Copyright (c) 2015, 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. | ||
| // | ||
| // 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 | ||
| // 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 <inttypes.h> | ||
| #include <stdlib.h> | ||
| #include <tcti/tcti-tabrmd.h> | ||
|
|
||
| #include <sapi/tpm20.h> | ||
|
|
||
| #include "log.h" | ||
| #include "tpm2_tools_tcti_abrmd.h" | ||
| #include "tpm2_util.h" | ||
|
|
||
| TSS2_TCTI_CONTEXT *tpm2_tools_tcti_abrmd_init(char *opts) { | ||
|
|
||
| UNUSED(opts); | ||
|
|
||
| size_t size; | ||
| TSS2_RC rc = tss2_tcti_tabrmd_init(NULL, &size); | ||
| if (rc != TSS2_RC_SUCCESS) { | ||
| LOG_ERR("Failed to get size for TABRMD TCTI context: 0x%" PRIx32, rc); | ||
| return NULL; | ||
| } | ||
|
|
||
| TSS2_TCTI_CONTEXT *tcti_ctx = (TSS2_TCTI_CONTEXT*) calloc(1, size); | ||
| if (tcti_ctx == NULL) { | ||
| LOG_ERR("Allocation for TABRMD TCTI context failed: oom"); | ||
| return NULL; | ||
| } | ||
|
|
||
| rc = tss2_tcti_tabrmd_init(tcti_ctx, &size); | ||
| if (rc != TSS2_RC_SUCCESS) { | ||
| LOG_ERR ("Failed to initialize TABRMD TCTI context: 0x%" PRIx32, rc); | ||
| free(tcti_ctx); | ||
| return NULL; | ||
| } | ||
|
|
||
| return tcti_ctx; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| //**********************************************************************; | ||
| // Copyright (c) 2015, 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. | ||
| // | ||
| // 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 | ||
| // 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. | ||
| //**********************************************************************; | ||
| #ifndef LIB_TCTI_TPM2_TOOLS_TCTI_ABRMD_H_ | ||
| #define LIB_TCTI_TPM2_TOOLS_TCTI_ABRMD_H_ | ||
|
|
||
| #include <sapi/tpm20.h> | ||
|
|
||
| /** | ||
| * Initializes a abrmd TCTI from an option string. | ||
| @note | ||
| * abrmd currently accepts no options. | ||
| * | ||
| * @param opts | ||
| * The option string, ignored. | ||
| * @return | ||
| * NULL on error or an initialized abrmd TCTI. | ||
| */ | ||
| TSS2_TCTI_CONTEXT *tpm2_tools_tcti_abrmd_init(char *opts); | ||
|
|
||
| #endif /* LIB_TCTI_TPM2_TOOLS_TCTI_ABRMD_H_ */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| //**********************************************************************; | ||
| // Copyright (c) 2015, 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. | ||
| // | ||
| // 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 | ||
| // 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 <inttypes.h> | ||
| #include <stdlib.h> | ||
|
|
||
| #include <sapi/tpm20.h> | ||
| #include <tcti/tcti_device.h> | ||
|
|
||
| #include "log.h" | ||
| #include "tpm2_tools_tcti_device.h" | ||
| #include "tpm2_util.h" | ||
|
|
||
| #define TPM2TOOLS_ENV_DEVICE_FILE "TPM2TOOLS_DEVICE_FILE" | ||
| #define TCTI_DEVICE_DEFAULT_PATH "/dev/tpm0" | ||
|
|
||
| TSS2_TCTI_CONTEXT *tpm2_tools_tcti_device_init(char *opts) { | ||
|
|
||
| TCTI_DEVICE_CONF conf = { | ||
| .device_path = TCTI_DEVICE_DEFAULT_PATH, | ||
| .logCallback = NULL, | ||
| .logData = NULL, | ||
| }; | ||
|
|
||
| char *env_path = getenv(TPM2TOOLS_ENV_DEVICE_FILE); | ||
| if (env_path) { | ||
| conf.device_path = env_path; | ||
| } | ||
|
|
||
| if (opts) { | ||
| conf.device_path = opts; | ||
| } | ||
|
|
||
| size_t size; | ||
| TSS2_RC rc; | ||
| TSS2_TCTI_CONTEXT *tcti_ctx; | ||
|
|
||
| rc = InitDeviceTcti(NULL, &size, 0); | ||
| if (rc != TSS2_RC_SUCCESS) { | ||
| LOG_ERR("Failed to get allocation size for device tcti context: " | ||
| "0x%x", rc); | ||
| return NULL; | ||
| } | ||
| tcti_ctx = (TSS2_TCTI_CONTEXT*) calloc(1, size); | ||
| if (tcti_ctx == NULL) { | ||
| LOG_ERR("Allocation for device TCTI context failed: oom"); | ||
| return NULL; | ||
| } | ||
| rc = InitDeviceTcti(tcti_ctx, &size, &conf); | ||
| if (rc != TSS2_RC_SUCCESS) { | ||
| LOG_ERR("Failed to initialize device TCTI context: 0x%x", rc); | ||
| free(tcti_ctx); | ||
| return NULL; | ||
| } | ||
| return tcti_ctx; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //**********************************************************************; | ||
| // Copyright (c) 2017, 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. | ||
| // | ||
| // 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 | ||
| // 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. | ||
| //**********************************************************************; | ||
| #ifndef LIB_TCTI_TPM2_TOOLS_TCTI_DEVICE_H_ | ||
| #define LIB_TCTI_TPM2_TOOLS_TCTI_DEVICE_H_ | ||
|
|
||
| #include <sapi/tpm20.h> | ||
|
|
||
| /** | ||
| * Initializes a device tcti from opts. opts can be a filepath | ||
| * to a tpm device file or NULL. On NULL, it uses the environment | ||
| * value or, if not set, the default path. | ||
| * @param opts | ||
| * The option string, which can be a file path or NULL. | ||
| * @return | ||
| * NULL on error or an initialized device tcti. | ||
| */ | ||
| TSS2_TCTI_CONTEXT *tpm2_tools_tcti_device_init(char *opts); | ||
|
|
||
| #endif /* LIB_TCTI_TPM2_TOOLS_TCTI_DEVICE_H_ */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| //**********************************************************************; | ||
| // Copyright (c) 2015, 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. | ||
| // | ||
| // 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 | ||
| // 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 <inttypes.h> | ||
| #include <stdlib.h> | ||
|
|
||
| #include <tcti/tcti_socket.h> | ||
| #include <sapi/tpm20.h> | ||
|
|
||
| #include "log.h" | ||
| #include "tpm2_tools_tcti_socket.h" | ||
| #include "tpm2_util.h" | ||
|
|
||
| #define TCTI_SOCKET_DEFAULT_ADDRESS "127.0.0.1" | ||
| #define TCTI_SOCKET_DEFAULT_PORT 2321 | ||
|
|
||
| #define TPM2TOOLS_ENV_SOCKET_ADDRESS "TPM2TOOLS_SOCKET_ADDRESS" | ||
| #define TPM2TOOLS_ENV_SOCKET_PORT "TPM2TOOLS_SOCKET_PORT" | ||
|
|
||
| TSS2_TCTI_CONTEXT* | ||
| tpm2_tools_tcti_socket_init (char *opts) | ||
| { | ||
| TCTI_SOCKET_CONF conf = { | ||
| .hostname = TCTI_SOCKET_DEFAULT_ADDRESS, | ||
| .port = TCTI_SOCKET_DEFAULT_PORT, | ||
| .logCallback = NULL, | ||
| .logBufferCallback = NULL, | ||
| .logData = NULL, | ||
| }; | ||
|
|
||
| char *addr_env = getenv(TPM2TOOLS_ENV_SOCKET_ADDRESS); | ||
| if (addr_env) { | ||
| conf.hostname = addr_env; | ||
| } | ||
|
|
||
| char *port_env = getenv(TPM2TOOLS_ENV_SOCKET_PORT); | ||
| if (port_env) { | ||
| bool res = tpm2_util_string_to_uint16(port_env, &conf.port); | ||
| if (!res) { | ||
| LOG_ERR("Error getting env var\""TPM2TOOLS_ENV_SOCKET_PORT"\"," | ||
| "got: \"%s\", expected a number!", port_env); | ||
| return NULL; | ||
| } | ||
| } | ||
|
|
||
| /* opts should be something like: "hostname:port" */ | ||
| if (opts) { | ||
| char *port_sep = strrchr(opts, ':'); | ||
| if (port_sep) { | ||
| port_sep[0] = '\0'; | ||
| port_sep++; | ||
| bool res = tpm2_util_string_to_uint16(port_sep, &conf.port); | ||
| if (!res) { | ||
| LOG_ERR("Error getting env var\""TPM2TOOLS_ENV_SOCKET_PORT"\"," | ||
| "got: \"%s\", expected a number!", port_sep); | ||
| return NULL; | ||
| } | ||
| } | ||
| conf.hostname = opts; | ||
| } | ||
|
|
||
| size_t size; | ||
| TSS2_RC rc; | ||
| TSS2_TCTI_CONTEXT *tcti_ctx; | ||
|
|
||
| rc = InitSocketTcti (NULL, &size, &conf, 0); | ||
| if (rc != TSS2_RC_SUCCESS) { | ||
| LOG_ERR("Faled to get allocation size for tcti context: " | ||
| "0x%x", rc); | ||
| return NULL; | ||
| } | ||
| tcti_ctx = (TSS2_TCTI_CONTEXT*)calloc (1, size); | ||
| if (tcti_ctx == NULL) { | ||
| LOG_ERR("Allocation for tcti context failed: oom"); | ||
| return NULL; | ||
| } | ||
| rc = InitSocketTcti (tcti_ctx, &size, &conf, 0); | ||
| if (rc != TSS2_RC_SUCCESS) { | ||
| LOG_ERR("Failed to initialize tcti context: 0x%x\n", rc); | ||
| free (tcti_ctx); | ||
| return NULL; | ||
| } | ||
| return tcti_ctx; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //**********************************************************************; | ||
| // Copyright (c) 2017, 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. | ||
| // | ||
| // 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 | ||
| // 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. | ||
| //**********************************************************************; | ||
| #ifndef LIB_TCTI_TPM2_TOOLS_TCTI_SOCKET_H_ | ||
| #define LIB_TCTI_TPM2_TOOLS_TCTI_SOCKET_H_ | ||
|
|
||
| #include <sapi/tpm20.h> | ||
|
|
||
| /** | ||
| * Initializes a socket tcti from opts. opts can be either NULL or a | ||
| * <hostname>:<port> string. | ||
| * | ||
| * @param opts | ||
| * The option string, which can be a <hostname>:<port> specifier or NULL. | ||
| * @return | ||
| * NULL on error or an initialized socket tcti. | ||
| */ | ||
| TSS2_TCTI_CONTEXT *tpm2_tools_tcti_socket_init(char *opts); | ||
|
|
||
| #endif /* LIB_TCTI_TPM2_TOOLS_TCTI_SOCKET_H_ */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,372 @@ | ||
| /* | ||
| * Copyright (c) 2016, 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. | ||
| * | ||
| * 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 | ||
| * 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 <errno.h> | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #include <getopt.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include "log.h" | ||
| #include "tpm2_options.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" | ||
| #endif | ||
|
|
||
| #define TPM2TOOLS_ENV_TCTI_NAME "TPM2TOOLS_TCTI_NAME" | ||
|
|
||
| struct tpm2_options { | ||
| struct { | ||
| tpm2_option_handler on_opt; | ||
| tpm2_arg_handler on_arg; | ||
| } callbacks; | ||
| char *short_opts; | ||
| size_t len; | ||
| struct option long_opts[]; | ||
| }; | ||
|
|
||
| tpm2_options *tpm2_options_new(const char *short_opts, size_t len, | ||
| const struct option *long_opts, tpm2_option_handler on_opt, | ||
| tpm2_arg_handler on_arg) { | ||
|
|
||
| tpm2_options *opts = calloc(1, sizeof(*opts) + (sizeof(*long_opts) * len)); | ||
| if (!opts) { | ||
| LOG_ERR("oom"); | ||
| return NULL; | ||
| } | ||
|
|
||
| /* | ||
| * On NULL, just make it a zero length string so we don't have to keep | ||
| * checking it for NULL. | ||
| */ | ||
| if (!short_opts) { | ||
| short_opts = ""; | ||
| } | ||
|
|
||
| opts->short_opts = strdup(short_opts); | ||
| if (!opts->short_opts) { | ||
| LOG_ERR("oom"); | ||
| free(opts); | ||
| return NULL; | ||
| } | ||
|
|
||
| opts->callbacks.on_opt = on_opt; | ||
| opts->callbacks.on_arg = on_arg; | ||
| opts->len = len; | ||
| memcpy(opts->long_opts, long_opts, len * sizeof(*long_opts)); | ||
|
|
||
| return opts; | ||
| } | ||
|
|
||
| bool tpm2_options_cat(tpm2_options **dest, tpm2_options *src) { | ||
|
|
||
| tpm2_options *d = *dest; | ||
|
|
||
| /* move the nested char * pointer first */ | ||
| size_t opts_len = strlen(d->short_opts) + strlen(src->short_opts) + 1; | ||
| char *tmp_short = realloc(d->short_opts, opts_len); | ||
| if (!tmp_short) { | ||
| LOG_ERR("oom"); | ||
| return false; | ||
| } | ||
|
|
||
| strcat(tmp_short, src->short_opts); | ||
|
|
||
| d->short_opts = tmp_short; | ||
|
|
||
| /* now move the eclosing structure */ | ||
| size_t long_opts_len = d->len + src->len; | ||
| /* +1 for a terminating NULL at the end of options array for getopt_long */ | ||
| tpm2_options *tmp = realloc(d, sizeof(*d) + ((long_opts_len + 1) * sizeof(d->long_opts[0]))); | ||
| if (!tmp) { | ||
| LOG_ERR("oom"); | ||
| return false; | ||
| } | ||
|
|
||
| *dest = d = tmp; | ||
|
|
||
| d->callbacks.on_arg = src->callbacks.on_arg; | ||
| d->callbacks.on_opt = src->callbacks.on_opt; | ||
|
|
||
| memcpy(&d->long_opts[d->len], src->long_opts, src->len * sizeof(src->long_opts[0])); | ||
|
|
||
| /* length must be updated post memcpy as we need d->len to be the original offest */ | ||
| d->len = long_opts_len; | ||
|
|
||
| /* NULL term for getopt_long */ | ||
| memset(&d->long_opts[d->len], 0, sizeof(d->long_opts[0])); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void tpm2_options_free(tpm2_options *opts) { | ||
| free(opts->short_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, ':'); | ||
| if (!split) { | ||
| return NULL; | ||
| } | ||
|
|
||
| split[0] = '\0'; | ||
|
|
||
| /* | ||
| * make it so downstream consumers don't need to deal with the empty | ||
| * string, ie "". They can just check NULL. | ||
| */ | ||
| if (!split[1]) { | ||
| return NULL; | ||
| } | ||
|
|
||
| return &split[1]; | ||
| } | ||
|
|
||
| static void execute_man (char *prog_name, char *envp[]) { | ||
|
|
||
| char *manpage = basename(prog_name); | ||
| char *argv[] = { | ||
| "/man", // ARGv[0] needs to be something. | ||
| manpage, | ||
| NULL | ||
| }; | ||
| execvpe ("man", argv, envp); | ||
| LOG_ERR("Could not execute \"man %s\" error: %s", manpage, | ||
| strerror(errno)); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
| tpm2_option_code tpm2_handle_options (int argc, char **argv, char **envp, | ||
| tpm2_options *tool_opts, tpm2_option_flags *flags, | ||
| TSS2_TCTI_CONTEXT **tcti) { | ||
|
|
||
| tpm2_option_code rc = tpm2_option_code_err; | ||
| bool result = false; | ||
|
|
||
| UNUSED(envp); | ||
|
|
||
| struct option long_options [] = { | ||
| { "tcti", required_argument, NULL, 'T' }, | ||
| { "help", no_argument, NULL, 'h' }, | ||
| { "verbose", no_argument, NULL, 'v' }, | ||
| { "quiet", no_argument, NULL, 'Q' }, | ||
| { "version", no_argument, NULL, 'V' }, | ||
| }; | ||
|
|
||
| char *tcti_opts = NULL; | ||
| char *tcti_name = TCTI_DEFAULT_STR; | ||
| char *env_str = getenv (TPM2TOOLS_ENV_TCTI_NAME); | ||
| tcti_name = env_str ? env_str : tcti_name; | ||
|
|
||
| /* handle any options */ | ||
| tpm2_options *opts = tpm2_options_new("T:hvVQ", | ||
| ARRAY_LEN(long_options), long_options, NULL, NULL); | ||
| if (!opts) { | ||
| return tpm2_option_code_err; | ||
| } | ||
|
|
||
| /* Get the options from the tool */ | ||
| if (tool_opts) { | ||
| result = tpm2_options_cat(&opts, tool_opts); | ||
| if (!result) { | ||
| goto out; | ||
| } | ||
| } | ||
|
|
||
| /* Parse the options, calling the tool callback if unknown */ | ||
| int c; | ||
| while ((c = getopt_long (argc, argv, opts->short_opts, opts->long_opts, NULL)) | ||
| != -1) | ||
| { | ||
| switch (c) { | ||
| case 'T': | ||
| /* only attempt to get options from tcti option string */ | ||
| tcti_name = optarg; | ||
| tcti_opts = tcti_get_opts(optarg); | ||
| break; | ||
| case 'h': | ||
| execute_man(argv[0], envp); | ||
| result = false; | ||
| goto out; | ||
| break; | ||
| case 'V': | ||
| flags->verbose = 1; | ||
| break; | ||
| case 'Q': | ||
| flags->quiet = 1; | ||
| break; | ||
| case 'v': | ||
| show_version(argv[0]); | ||
| rc = tpm2_option_code_stop; | ||
| goto out; | ||
| break; | ||
| case ':': | ||
| LOG_ERR("Argument %c needs a value!", optopt); | ||
| goto out; | ||
| case '?': | ||
| LOG_ERR("Unknown Argument: %c", optopt); | ||
| result = false; | ||
| goto out; | ||
| default: | ||
| /* NULL on_opt handler and unkown option specified is an error */ | ||
| if (!tool_opts->callbacks.on_opt) { | ||
| LOG_ERR("Unknown options found: %c", c); | ||
| goto out; | ||
| } | ||
| result = tool_opts->callbacks.on_opt(c, optarg); | ||
| if (!result) { | ||
| goto out; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| char **tool_args = &argv[optind]; | ||
| int tool_argc = argc - optind; | ||
|
|
||
| /* have args and a handler to process */ | ||
| if (tool_argc && tool_opts->callbacks.on_arg) { | ||
| result = tool_opts->callbacks.on_arg(tool_argc, tool_args); | ||
| if (!result) { | ||
| goto out; | ||
| } | ||
| /* have args and no handler, error condition */ | ||
| } else if (tool_argc && !tool_opts->callbacks.on_arg) { | ||
| goto out; | ||
| } | ||
|
|
||
| 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) { | ||
| result = false; | ||
| goto out; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!found) { | ||
| LOG_ERR("Unknown tcti, got: \"%s\"", tcti_name); | ||
| result = false; | ||
| goto out; | ||
| } | ||
|
|
||
| rc = tpm2_option_code_continue; | ||
|
|
||
| out: | ||
| tpm2_options_free(opts); | ||
|
|
||
| return rc; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| * Copyright (c) 2016, 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. | ||
| * | ||
| * 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 | ||
| * 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. | ||
| */ | ||
| #ifndef OPTIONS_H | ||
| #define OPTIONS_H | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <stdio.h> | ||
|
|
||
| #include <getopt.h> | ||
|
|
||
| #include <sapi/tpm20.h> | ||
|
|
||
| typedef struct tpm2_options tpm2_options; | ||
|
|
||
| typedef union tpm2_option_flags tpm2_option_flags; | ||
| union tpm2_option_flags { | ||
| struct { | ||
| UINT8 verbose : 1; | ||
| UINT8 quiet : 1; | ||
| UINT8 unused : 6; | ||
| }; | ||
| UINT8 all; | ||
| }; | ||
|
|
||
| /** | ||
| * This function pointer defines the interface for tcti initialization. | ||
| * ALL tool supported TCTIs should implement this interface. | ||
| * @param opts | ||
| * An option string, that is defined by the tcti, and is passed | ||
| * via the --tcti= or -T options. | ||
| * | ||
| * Anything following the : in the --tcti option is provides as opts. | ||
| * @return | ||
| * NULL on error or an initialized TCTI. | ||
| */ | ||
| typedef TSS2_TCTI_CONTEXT *(*tcti_init)(char *opts); | ||
|
|
||
| /** | ||
| * Tools may implement this optional interface if they need | ||
| * to handle options. | ||
| * @param key | ||
| * The key of the option, ie short option return value from getopt_long(). | ||
| * @param value | ||
| * The getopt_long optarg value. | ||
| * @return | ||
| * true on success, false on error. | ||
| * @note | ||
| * LOG_INFO and TOOL_OUTPUT will not work correctly during this callback. | ||
| * This is called after onstart() finishes, but before | ||
| * onrun() is invoked. | ||
| * | ||
| */ | ||
| typedef bool (*tpm2_option_handler)(char key, char *value); | ||
|
|
||
| /** | ||
| * Called after option handling to process arguments, if specified. | ||
| * @param argc | ||
| * The number of args in argv. | ||
| * @param argv | ||
| * The arguments. | ||
| * @return | ||
| * true on success, false otherwise. | ||
| * @note | ||
| * LOG_INFO adn TOOL_OUTPUT will not work correctly during this callback. | ||
| * This is called after onstart() and tpm2_option_handler() (if specified), | ||
| * but before onrun() is invoked. | ||
| * | ||
| */ | ||
| typedef bool (*tpm2_arg_handler)(int argc, char **argv); | ||
|
|
||
| /** | ||
| * The onstart() routine expects a return of NULL or a tpm2_options structure. | ||
| * This routine initializes said object. | ||
| * @param short_opts | ||
| * Any short options you wish to specify to getopt_long. | ||
| * @param len | ||
| * The length of the long_opts array. | ||
| * @param long_opts | ||
| * Any long options you wish to specify to getopt_long(). | ||
| * @param on_opt | ||
| * An option handling callback, which may be null if you don't wish | ||
| * to handle options. | ||
| * @param on_arg | ||
| * An argument handling callback, which may be null if you don't wish | ||
| * to handle arguments. | ||
| * @return | ||
| * NULL on failure or an initialized tpm2_options object. | ||
| */ | ||
| tpm2_options *tpm2_options_new(const char *short_opts, size_t len, | ||
| const struct option *long_opts, tpm2_option_handler on_opt, | ||
| tpm2_arg_handler on_arg); | ||
|
|
||
| /** | ||
| * Concatenates two tpm2_options objects, with src appended on | ||
| * dest. The internal callbacks for tpm2_arg_handler and tpm2_option_handler | ||
| * which were specified during tpm2_options_new() are copied from src to | ||
| * dest, thus overwriting dest. Short and long options are concatenated. | ||
| * @param dest | ||
| * The tpm2_options object to append to. | ||
| * @param src | ||
| * The source tpm2_options to append onto dest. | ||
| * @return | ||
| * true on success, false otherwise. | ||
| */ | ||
| bool tpm2_options_cat(tpm2_options **dest, tpm2_options *src); | ||
|
|
||
| /** | ||
| * Free's a tpm2_options created via tpm2_options_new(). | ||
| * @param opts | ||
| * The tpm2_options object to deallocate. | ||
| */ | ||
| void tpm2_options_free(tpm2_options *opts); | ||
|
|
||
| typedef enum tpm2_option_code tpm2_option_code; | ||
| enum tpm2_option_code { | ||
| tpm2_option_code_continue, | ||
| tpm2_option_code_stop, | ||
| tpm2_option_code_err | ||
| }; | ||
|
|
||
| /** | ||
| * Parses the tpm2_tool command line. | ||
| * | ||
| * @param argc | ||
| * The argc from main. | ||
| * @param argv | ||
| * The argv from main. | ||
| * @param envp | ||
| * The envp from main. | ||
| * @param tool_opts | ||
| * The tool options gathered during onstart() lifecycle call. | ||
| * @param flags | ||
| * The tpm2_option_flags to set during parsing. | ||
| * @param tcti | ||
| * The tcti initialized from the tcti options. | ||
| * @return | ||
| * A tpm option code indicating if an error, further processing | ||
| * or an immediate exit is desired. | ||
| * @note | ||
| * Used by tpm2_tool, and likely should only be used there. | ||
| * | ||
| */ | ||
| tpm2_option_code tpm2_handle_options (int argc, char **argv, char **envp, | ||
| tpm2_options *tool_opts, tpm2_option_flags *flags, | ||
| TSS2_TCTI_CONTEXT **tcti); | ||
|
|
||
| #endif /* OPTIONS_H */ |