Skip to content

Commit

Permalink
Builds minus link files
Browse files Browse the repository at this point in the history
Signed-off-by: Bill Roberts <[email protected]>
  • Loading branch information
williamcroberts committed Jan 14, 2024
1 parent 5ea8fc9 commit 0a955c4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 128 deletions.
211 changes: 83 additions & 128 deletions src/tss2-tcti/tcti-py.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include <config.h>
#endif

#include <Python.h>
#include <python3.12/Python.h>

#include "tss2_Tcti_py.h"
#include "tss2_tcti_py.h"

#include "tcti-cmd.h"
#include "tcti-py.h"
#include "tcti-common.h"
#define LOGMODULE tcti
#include "util/log.h"
Expand Down Expand Up @@ -56,13 +56,7 @@ TSS2_RC tcti_py_transmit (TSS2_TCTI_CONTEXT *tcti_ctx, size_t size,
return rc;
}

size_t bytes = tcti_py_fwrite (py_buf, 1, size, tcti_cmd->sink);
if (tcti_py_ferror (tcti_cmd->sink) || bytes != size) {
LOG_ERROR ("Transmitting to subprocess failed: %s", strerror (errno));
return TSS2_TCTI_RC_IO_ERROR;
}

fflush (tcti_cmd->sink);
UNUSED(size);

tcti_common->state = TCTI_STATE_RECEIVE;

Expand All @@ -85,38 +79,26 @@ TSS2_RC tcti_py_get_poll_handles (TSS2_TCTI_CONTEXT *tctiContext,

*num_handles = 1;
if (handles != NULL) {
handles->fd = fileno (py_tcti->source);
if (handles->fd < 0) {
LOG_ERROR ("Could not get fileno: %s", strerror (errno));
return TSS2_TCTI_RC_IO_ERROR;
}
handles->events = POLLIN | POLLOUT;
}

return TSS2_RC_SUCCESS;
}

void tcti_py_finalize (TSS2_TCTI_CONTEXT *tctiContext)
{
TSS2_TCTI_PY_CONTEXT *tcti_cmd = tcti_py_context_cast (tctiContext);
TSS2_TCTI_PY_CONTEXT *tcti_py = tcti_py_context_cast (tctiContext);

if (tcti_cmd == NULL) {
if (tcti_py == NULL) {
return;
}

reap_child (tcti_cmd->child_pid);

fclose (tcti_cmd->source);
fclose (tcti_cmd->sink);
Py_XDECREF(tcti_py->py_tcti);
Py_Finalize();
}

TSS2_RC tcti_py_receive (TSS2_TCTI_CONTEXT *tctiContext, size_t *response_size,
unsigned char *response_buffer, int32_t timeout)
{
#ifdef TEST_FAPI_ASYNC
/* Used for simulating a timeout. */
static int wait = 0;
#endif
TSS2_TCTI_PY_CONTEXT *tcti_cmd = tcti_py_context_cast (tctiContext);
TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_py_down_cast (tcti_cmd);
TSS2_RC rc;
Expand All @@ -127,82 +109,10 @@ TSS2_RC tcti_py_receive (TSS2_TCTI_CONTEXT *tctiContext, size_t *response_size,
return rc;
}

if (timeout != TSS2_TCTI_TIMEOUT_BLOCK) {
LOG_TRACE ("Asynchronous I/O not actually implemented.");
#ifdef TEST_FAPI_ASYNC
if (wait < 1) {
LOG_TRACE ("Simulating Async by requesting another invocation.");
wait += 1;
return TSS2_TCTI_RC_TRY_AGAIN;
} else {
LOG_TRACE ("Sending the actual result.");
wait = 0;
}
#endif /* TEST_FAPI_ASYNC */
}

if (!response_buffer) {
*response_size = 4096;
return TSS2_RC_SUCCESS;
}

/* block until we have a header or the child closes the pipe */
size_t size = tcti_py_fread (response_buffer, 1, TPM_HEADER_SIZE, tcti_cmd->source);
rc = tcti_py_ferror (tcti_cmd->source);
if (rc) {
LOG_ERROR ("Reading from command TCTI: %s", strerror (errno));
rc = TSS2_TCTI_RC_IO_ERROR;
goto out;
}

if (size != TPM_HEADER_SIZE) {
LOG_ERROR ("Read was not size of header, got %zu expected %zu",
size, TPM_HEADER_SIZE);
rc = TSS2_TCTI_RC_MALFORMED_RESPONSE;
goto out;
}

/*
* we have a header, get the size field to compute the rest of
* the data.
*/
rc = header_unmarshal (response_buffer, &tcti_common->header);
if (rc) {
goto out;
}

if (tcti_common->header.size < TPM_HEADER_SIZE) {
LOG_ERROR ("Header response size is less than TPM_HEADER_SIZE,"
" got %" PRIu32 " expected greater than or equal to %zu",
tcti_common->header.size, TPM_HEADER_SIZE);
rc = TSS2_TCTI_RC_MALFORMED_RESPONSE;
goto out;
}

/*
* Read the remaining data that is past the header size
*/
size_t data_size = tcti_common->header.size - TPM_HEADER_SIZE;

size = tcti_py_fread (&response_buffer[TPM_HEADER_SIZE], 1, data_size,
tcti_cmd->source);
rc = tcti_py_ferror (tcti_cmd->source);
if (rc) {
rc = errno == EFAULT ?
TSS2_TCTI_RC_INSUFFICIENT_BUFFER : TSS2_TCTI_RC_IO_ERROR;
LOG_ERROR ("Reading from command TCTI: %s", strerror (errno));
goto out;
}

if (size != data_size) {
LOG_ERROR ("Command response body read was not for expected size, "
"got %zu expected %zu", size, data_size);
rc = TSS2_TCTI_RC_MALFORMED_RESPONSE;
goto out;
}

*response_size = tcti_common->header.size;

// TODO BILL
UNUSED(response_buffer);
UNUSED(timeout);
goto out;
/*
* Executing code beyond this point transitions the state machine to
* TRANSMIT. Another call to this function will not be possible until
Expand Down Expand Up @@ -236,9 +146,11 @@ void tcti_py_init_context_data (TSS2_TCTI_COMMON_CONTEXT *tcti_common)
TSS2_RC Tss2_Tcti_Py_Init (TSS2_TCTI_CONTEXT *tctiContext, size_t *size,
const char *conf)
{
TSS2_TCTI_PY_CONTEXT *tcti_command =
TSS2_RC rc = TSS2_TCTI_RC_GENERAL_FAILURE;

TSS2_TCTI_PY_CONTEXT *tcti_py =
(TSS2_TCTI_PY_CONTEXT*) tctiContext;
TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_py_down_cast (tcti_command);
TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_py_down_cast (tcti_py);

if (size == NULL || conf == NULL) {
return TSS2_TCTI_RC_BAD_VALUE;
Expand All @@ -249,43 +161,86 @@ TSS2_RC Tss2_Tcti_Py_Init (TSS2_TCTI_CONTEXT *tctiContext, size_t *size,
return TSS2_RC_SUCCESS;
}

LOG_DEBUG ("Initializing Python TCTI with command: %s", conf);
/* get a mutable copy of the conf string that can be modified */
char *conf_copy = strdup(conf);
if (!conf_copy) {
return TSS2_TCTI_RC_MEMORY;
}

char *arg = NULL;
char *module = NULL;
char *sep = strchr(conf_copy, ':');
if (!sep) {
/* no arguments specified, only module */
module = conf_copy;
} else {
/* split the string mod:arg so its mod\0arg */
sep = '\0';
module = conf_copy;
arg = &sep[1];
}

/* Initialize the Python Interpreter */
Py_Initialize();
LOG_DEBUG ("Initializing Python TCTI with module: \"%s\" and arg\"%s\"",
conf, arg ? arg : "(null)");

PyObject *mod_name = PyUnicode_FromString(conf);
/*
* Initialize the Python Interpreter TODO is this safe to call more than once?
* It seems so.
*/
Py_Initialize();

// Load the module object
PyObject *pModule = PyImport_Import(pName);
if (!pModule) {
PyErr_Print();
return 1;
PyObject *py_module = NULL, *py_dict = NULL, *py_initfn = NULL;
PyObject *py_modname = PyUnicode_FromString(module);
if (!py_modname) {
goto error;
}

// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
py_module = PyImport_Import(py_modname);
if (!py_module) {
goto error;
}

py_dict = PyModule_GetDict(py_module);
if (!py_dict) {
goto error;
}

// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, (char*)"someFunction");
/*
* modules implement a method called tcti_init(args) -> Object
* Where the returned object implements the required attributes.
*/
py_initfn = PyDict_GetItemString(py_dict, (char*)"tcti_init");
if (!py_dict) {
goto error;
}

if (!PyCallable_Check(py_initfn)) {
goto error;
}

tcti_command->sink = NULL;
tcti_command->source = NULL;
tcti_command->child_pid = -1;
PyObject *py_arg = PyUnicode_FromString(arg);
if (!py_arg) {
goto error;
}

int rc = popen_w_pipes (conf, &tcti_command->child_pid, &tcti_command->sink,
&tcti_command->source);
if (rc != 0) {
LOG_ERROR ("Open subprocess command \"%s\", failed with: %s", conf,
strerror (rc));
return TSS2_TCTI_RC_GENERAL_FAILURE;
/* This is the instance object that implements the TCTI */
tcti_py->py_tcti = PyObject_CallObject(py_initfn, py_arg);
if (!tcti_py->py_tcti) {
goto error;
}

tcti_py_init_context_data (tcti_common);

return TSS2_RC_SUCCESS;
rc = TSS2_RC_SUCCESS;
out:
Py_XDECREF(py_module);
Py_XDECREF(py_dict);
Py_XDECREF(py_initfn);
Py_XDECREF(py_modname);
return rc;
error:
PyErr_Print();
Py_Finalize();
goto out;
}

/* public info structure */
Expand Down
1 change: 1 addition & 0 deletions src/tss2-tcti/tcti-py.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
typedef struct TSS2_TCTI_PY_CONTEXT TSS2_TCTI_PY_CONTEXT;
struct TSS2_TCTI_PY_CONTEXT {
TSS2_TCTI_COMMON_CONTEXT common;
PyObject *py_tcti;
};

#endif /* TCTI_CMD_H */

0 comments on commit 0a955c4

Please sign in to comment.