Skip to content

Commit

Permalink
libteec: allow passing settings to session open
Browse files Browse the repository at this point in the history
Add a new function, TEEC_OpenSession2, that allows a developer to pass
configuration parameters in the form of distinct settings. One of the new
settings is for attaching an arbitrary pointer to the session. This is
useful when an OCALL arrives from the TA and the handler requires
contextual information to proceed that changes depending on which session
the OCALL arrived through.

Having multiple settings in this manner helps reduce the number of
auxiliary functions necessary. Were new functionality to be added in the
future, no new functions would need to be introduced. Instead, one would
only require a new setting.

Signed-off-by: Hernan Gatta <hegatta@microsoft.com>
  • Loading branch information
HernanGatta committed Jun 18, 2020
1 parent aabf0d2 commit bad9015
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
47 changes: 47 additions & 0 deletions libteec/src/tee_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *ctx, TEEC_Session *session,
if (res == TEEC_SUCCESS) {
session->ctx = ctx;
session->session_id = arg->session;
session->data_setting.data = NULL;
}
teec_post_process_operation(operation, params, shm);

Expand All @@ -680,6 +681,52 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *ctx, TEEC_Session *session,
return res;
}

TEEC_Result TEEC_OpenSession2(TEEC_Context *context,
TEEC_Session *session,
const TEEC_UUID *destination,
uint32_t connectionMethod,
const void *connectionData,
TEEC_Operation *operation,
uint32_t *returnOrigin,
const TEEC_SessionSetting *settings,
uint32_t numSettings)
{
uint32_t n;
TEEC_Result res;

if ((!settings && numSettings) || (settings && !numSettings))
return TEEC_ERROR_BAD_PARAMETERS;

if (settings) {
for (n = 0; n < numSettings; n++) {
switch (settings[n].type) {
case TEEC_SESSION_SETTING_DATA:
break;
default:
return TEEC_ERROR_BAD_PARAMETERS;
}
}
}

res = TEEC_OpenSession(context, session, destination, connectionMethod,
connectionData, operation, returnOrigin);
if (res != TEEC_SUCCESS)
return res;

for (n = 0; n < numSettings; n++) {
switch (settings[n].type) {
case TEEC_SESSION_SETTING_DATA:
session->data_setting.data = settings[n].u.data->data;
break;
default:
/* Not reached */
break;
}
}

return res;
}

void TEEC_CloseSession(TEEC_Session *session)
{
struct tee_ioctl_close_session_arg arg;
Expand Down
33 changes: 33 additions & 0 deletions public/tee_client_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,38 @@ typedef struct {
TEEC_ContextSettingOcall ocall_setting;
} TEEC_Context;

/**
* enum TEEC_SessionSettingType - List of available settings when initializing a
* session.
*/
typedef enum {
TEEC_SESSION_SETTING_DATA = 1
} TEEC_SessionSettingType;

/**
* struct TEEC_SessionSettingData - Setting to attach an arbitrary pointer to a
* session; useful when handling OCALLs if per-session data is required by the
* OCALL handler.
*
* @param data Arbitrary pointer.
*/
typedef struct {
void *data;
} TEEC_SessionSettingData;

/**
* struct TEEC_SessionSetting - A setting to be used when opening a session.
*
* @param type The type of setting this is (i.e., how to interpret the union).
* @param u Union of all possible settings.
*/
typedef struct {
TEEC_SessionSettingType type;
union {
const TEEC_SessionSettingData *data;
} u;
} TEEC_SessionSetting;

/**
* struct TEEC_Session - Represents a connection between a client application
* and a trusted application.
Expand All @@ -437,6 +469,7 @@ typedef struct {
/* Implementation defined */
TEEC_Context *ctx;
uint32_t session_id;
TEEC_SessionSettingData data_setting;
} TEEC_Session;

/**
Expand Down
36 changes: 36 additions & 0 deletions public/tee_client_api_extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,42 @@ TEEC_Result TEEC_InitializeContext2(const char *name, TEEC_Context *ctx,
const TEEC_ContextSetting *settings,
uint32_t numSettings);

/**
* TEEC_OpenSession2() - Opens a new session with the specified trusted
* application.
*
* @param context The initialized TEE context structure in which scope
* to open the session.
* @param session The session to initialize.
* @param destination A structure identifying the trusted application with
* which to open a session.
* @param connectionMethod The connection method to use.
* @param connectionData Any data necessary to connect with the chosen
* connection method. Not supported, should be set to
* NULL.
* @param operation An operation structure to use in the session. May be
* set to NULL to signify no operation structure
* needed.
* @param returnOrigin A parameter which will hold the error origin if this
* function returns any value other than TEEC_SUCCESS.
* @param settings A list of settings to use to configure the new
* session, or NULL.
* @param numSettings The number of settings, if any.
*
* @return TEEC_SUCCESS Successfully opened a new session.
* @return TEEC_ERROR_BAD_PARAMETERS One or more parameters are wrong.
* @return TEEC_Result Something else failed.
*/
TEEC_Result TEEC_OpenSession2(TEEC_Context *context,
TEEC_Session *session,
const TEEC_UUID *destination,
uint32_t connectionMethod,
const void *connectionData,
TEEC_Operation *operation,
uint32_t *returnOrigin,
const TEEC_SessionSetting *settings,
uint32_t numSettings);

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions typedefs.checkpatch
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ TEEC_Value
TEEC_UUID
TEEC_TempMemoryReference
TEEC_SharedMemory
TEEC_SessionSettingType
TEEC_SessionSettingData
TEEC_SessionSetting
TEEC_Session
TEEC_Result
TEEC_RegisteredMemoryReference
Expand Down

0 comments on commit bad9015

Please sign in to comment.