[iot] Update Trusty IPC code to latest
Update to commit bb39a2b12dce8b6c9df9012faf231648de795e6d List of changes: bb39a2b ql-tipc: Support ATAP operations from bootloader 62b8d61 ql-tipc: Move serialization code to keymaster_serializable 8283307 avoid dead loop if tipc is closed by peer cf3f7f5 [ql-tipc] Refactor U-boot rpmb_storage_send 4b1d74d Make logging more readable Test: build + manual TIPC tests Change-Id: Ib2c0e7a4a8313b6e62c1fe4f58b923c0c2d3f695zero-sugar
parent
4f39afdd55
commit
fe28fb6482
|
@ -53,9 +53,14 @@ enum keymaster_command {
|
|||
KM_GET_SUPPORTED_EXPORT_FORMATS = (14 << KEYMASTER_REQ_SHIFT),
|
||||
KM_GET_KEY_CHARACTERISTICS = (15 << KEYMASTER_REQ_SHIFT),
|
||||
|
||||
// Bootloader calls.
|
||||
KM_SET_BOOT_PARAMS = (0x1000 << KEYMASTER_REQ_SHIFT),
|
||||
KM_SET_ATTESTATION_KEY = (0x2000 << KEYMASTER_REQ_SHIFT),
|
||||
KM_APPEND_ATTESTATION_CERT_CHAIN = (0x3000 << KEYMASTER_REQ_SHIFT),
|
||||
KM_ATAP_GET_CA_REQUEST = (0x4000 << KEYMASTER_REQ_SHIFT),
|
||||
KM_ATAP_SET_CA_RESPONSE_BEGIN = (0x5000 << KEYMASTER_REQ_SHIFT),
|
||||
KM_ATAP_SET_CA_RESPONSE_UPDATE = (0x6000 << KEYMASTER_REQ_SHIFT),
|
||||
KM_ATAP_SET_CA_RESPONSE_FINISH = (0x7000 << KEYMASTER_REQ_SHIFT),
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
|
@ -195,6 +200,24 @@ struct km_get_version_resp {
|
|||
uint8_t subminor_ver;
|
||||
} TRUSTY_ATTR_PACKED;
|
||||
|
||||
/**
|
||||
* km_raw_buffer_resp - response format for a raw buffer
|
||||
*/
|
||||
struct km_raw_buffer_resp {
|
||||
int32_t error;
|
||||
uint32_t data_size;
|
||||
int8_t data[0];
|
||||
} TRUSTY_ATTR_PACKED;
|
||||
|
||||
/**
|
||||
* km_set_ca_response_begin_req - starts the process to set the ATAP CA Response
|
||||
*
|
||||
* @ca_response_size: total size of the CA Response message
|
||||
*/
|
||||
struct km_set_ca_response_begin_req {
|
||||
uint32_t ca_response_size;
|
||||
} TRUSTY_ATTR_PACKED;
|
||||
|
||||
/**
|
||||
* km_boot_params - Parameters sent from the bootloader to the Keymaster TA
|
||||
*
|
||||
|
@ -236,19 +259,14 @@ struct km_attestation_data {
|
|||
} TRUSTY_ATTR_PACKED;
|
||||
|
||||
/**
|
||||
* Serializes a km_boot_params structure. On success, allocates |*out_size|
|
||||
* bytes to |*out| and writes the serialized |params| to |*out|. Caller takes
|
||||
* ownership of |*out|. Returns one of trusty_err.
|
||||
* km_raw_buffer - represents a single raw buffer
|
||||
*
|
||||
* @data_size: size of |data|
|
||||
* @data: pointer to the buffer
|
||||
*/
|
||||
int km_boot_params_serialize(const struct km_boot_params *params, uint8_t **out,
|
||||
uint32_t *out_size);
|
||||
|
||||
/**
|
||||
* Serializes a km_attestation_data structure. On success, allocates |*out_size|
|
||||
* bytes to |*out| and writes the serialized |data| to |*out|. Caller takes
|
||||
* ownership of |*out|. Returns one of trusty_err.
|
||||
*/
|
||||
int km_attestation_data_serialize(const struct km_attestation_data *data,
|
||||
uint8_t **out, uint32_t *out_size);
|
||||
struct km_raw_buffer {
|
||||
uint32_t data_size;
|
||||
const uint8_t *data;
|
||||
} TRUSTY_ATTR_PACKED;
|
||||
|
||||
#endif /* TRUSTY_INTERFACE_KEYMASTER_H_ */
|
||||
|
|
|
@ -85,5 +85,27 @@ int trusty_set_attestation_key(const uint8_t *key, uint32_t key_size,
|
|||
int trusty_append_attestation_cert_chain(const uint8_t *cert,
|
||||
uint32_t cert_size,
|
||||
keymaster_algorithm_t algorithm);
|
||||
/*
|
||||
* Reads a CA Request from Keymaster. On success allocates a new CA Request
|
||||
* message at |*ca_request_p|, and the caller takes ownership. Returns one
|
||||
* of trusty_err.
|
||||
*
|
||||
* @operation_start: Operation Start message
|
||||
* @operation_start_size: size of operation_start
|
||||
* @ca_request_p: location of newly allocated CA Request message
|
||||
* @ca_request_size_p: location of size of the CA Request message
|
||||
*/
|
||||
int trusty_atap_get_ca_request(const uint8_t *operation_start,
|
||||
uint32_t operation_start_size,
|
||||
uint8_t** ca_request_p,
|
||||
uint32_t* ca_request_size_p);
|
||||
/*
|
||||
* Sends the CA Response to Keymaster. Returns one of trusty_err.
|
||||
*
|
||||
* @ca_response: CA Response message
|
||||
* @ca_response_size: size of ca_response
|
||||
*/
|
||||
int trusty_atap_set_ca_response(const uint8_t *ca_response,
|
||||
uint32_t ca_response_size);
|
||||
|
||||
#endif /* TRUSTY_KEYMASTER_H_ */
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use, copy,
|
||||
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
* of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef TRUSTY_KEYMASTER_SERIALIZABLE_H_
|
||||
#define TRUSTY_KEYMASTER_SERIALIZABLE_H_
|
||||
|
||||
#include <trusty/keymaster.h>
|
||||
|
||||
/**
|
||||
* Simple serialization routines for dynamically sized keymaster messages.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Appends |data_len| bytes at |data| to |buf|. Performs no bounds checking,
|
||||
* assumes sufficient memory allocated at |buf|. Returns |buf| + |data_len|.
|
||||
*/
|
||||
uint8_t *append_to_buf(uint8_t *buf, const void *data, size_t data_len);
|
||||
|
||||
/**
|
||||
* Appends |val| to |buf|. Performs no bounds checking. Returns |buf| +
|
||||
* sizeof(uint32_t).
|
||||
*/
|
||||
uint8_t *append_uint32_to_buf(uint8_t *buf, uint32_t val);
|
||||
|
||||
/**
|
||||
* Appends a sized buffer to |buf|. First appends |data_len| to |buf|, then
|
||||
* appends |data_len| bytes at |data| to |buf|. Performs no bounds checking.
|
||||
* Returns |buf| + sizeof(uint32_t) + |data_len|.
|
||||
*/
|
||||
uint8_t *append_sized_buf_to_buf(uint8_t *buf, const uint8_t *data,
|
||||
uint32_t data_len);
|
||||
|
||||
/**
|
||||
* Serializes a km_boot_params structure. On success, allocates |*out_size|
|
||||
* bytes to |*out| and writes the serialized |params| to |*out|. Caller takes
|
||||
* ownership of |*out|. Returns one of trusty_err.
|
||||
*/
|
||||
int km_boot_params_serialize(const struct km_boot_params *params, uint8_t **out,
|
||||
uint32_t *out_size);
|
||||
|
||||
/**
|
||||
* Serializes a km_attestation_data structure. On success, allocates |*out_size|
|
||||
* bytes to |*out| and writes the serialized |data| to |*out|. Caller takes
|
||||
* ownership of |*out|. Returns one of trusty_err.
|
||||
*/
|
||||
int km_attestation_data_serialize(const struct km_attestation_data *data,
|
||||
uint8_t **out, uint32_t *out_size);
|
||||
|
||||
/**
|
||||
* Serializes a km_raw_buffer structure. On success, allocates |*out_size|
|
||||
* bytes to |*out| and writes the serialized |data| to |*out|. Caller takes
|
||||
* ownership of |*out|. Returns one of trusty_err.
|
||||
*/
|
||||
int km_raw_buffer_serialize(const struct km_raw_buffer *buf, uint8_t** out,
|
||||
uint32_t *out_size);
|
||||
|
||||
#endif /* TRUSTY_KEYMASTER_SERIALIZABLE_H_ */
|
|
@ -78,7 +78,7 @@ void trusty_abort(void) TRUSTY_ATTR_NO_RETURN;
|
|||
* Print a formatted string. @format must point to a NULL-terminated UTF-8
|
||||
* string, and is followed by arguments to be printed.
|
||||
*/
|
||||
void trusty_printv(const char *format, ...);
|
||||
void trusty_printf(const char *format, ...);
|
||||
/*
|
||||
* Copy @n bytes from @src to @dest.
|
||||
*/
|
||||
|
|
|
@ -27,6 +27,12 @@
|
|||
|
||||
#include <trusty/sysdeps.h>
|
||||
|
||||
/* Returns the basename of |str|. This is defined as the last path
|
||||
* component, assuming the normal POSIX separator '/'. If there are no
|
||||
* separators, returns |str|.
|
||||
*/
|
||||
const char* trusty_basename(const char* str);
|
||||
|
||||
#define TRUSTY_STRINGIFY(x) #x
|
||||
#define TRUSTY_TO_STRING(x) TRUSTY_STRINGIFY(x)
|
||||
|
||||
|
@ -52,12 +58,13 @@
|
|||
* This has no effect unless TIPC_ENABLE_DEBUG and LOCAL_LOG is defined.
|
||||
*/
|
||||
#ifdef TIPC_ENABLE_DEBUG
|
||||
#define trusty_debug(message, ...) \
|
||||
do { \
|
||||
if (LOCAL_LOG) { \
|
||||
trusty_printv(__FILE__ ":" TRUSTY_TO_STRING(__LINE__) ": DEBUG "); \
|
||||
trusty_printv(message, ##__VA_ARGS__, NULL); \
|
||||
} \
|
||||
#define trusty_debug(message, ...) \
|
||||
do { \
|
||||
if (LOCAL_LOG) { \
|
||||
trusty_printf(trusty_basename(__FILE__)); \
|
||||
trusty_printf(":" TRUSTY_TO_STRING(__LINE__) ": DEBUG "); \
|
||||
trusty_printf(message, ##__VA_ARGS__); \
|
||||
} \
|
||||
} while(0)
|
||||
#else
|
||||
#define trusty_debug(message, ...)
|
||||
|
@ -66,29 +73,32 @@
|
|||
/*
|
||||
* Prints info message.
|
||||
*/
|
||||
#define trusty_info(message, ...) \
|
||||
do { \
|
||||
trusty_printv(__FILE__ ": INFO "); \
|
||||
trusty_printv(message, ##__VA_ARGS__, NULL); \
|
||||
#define trusty_info(message, ...) \
|
||||
do { \
|
||||
trusty_printf(trusty_basename(__FILE__)); \
|
||||
trusty_printf(": INFO "); \
|
||||
trusty_printf(message, ##__VA_ARGS__); \
|
||||
} while(0)
|
||||
|
||||
/*
|
||||
* Prints error message.
|
||||
*/
|
||||
#define trusty_error(message, ...) \
|
||||
do { \
|
||||
trusty_printv(__FILE__ ":" TRUSTY_TO_STRING(__LINE__) ": ERROR "); \
|
||||
trusty_printv(message, ##__VA_ARGS__, NULL); \
|
||||
#define trusty_error(message, ...) \
|
||||
do { \
|
||||
trusty_printf(trusty_basename(__FILE__)); \
|
||||
trusty_printf(":" TRUSTY_TO_STRING(__LINE__) ": ERROR "); \
|
||||
trusty_printf(message, ##__VA_ARGS__); \
|
||||
} while(0)
|
||||
|
||||
/*
|
||||
* Prints message and calls trusty_abort.
|
||||
*/
|
||||
#define trusty_fatal(message, ...) \
|
||||
do { \
|
||||
trusty_printv(__FILE__ ":" TRUSTY_TO_STRING(__LINE__) ": FATAL "); \
|
||||
trusty_printv(message, ##__VA_ARGS__, NULL); \
|
||||
trusty_abort(); \
|
||||
#define trusty_fatal(message, ...) \
|
||||
do { \
|
||||
trusty_printf(trusty_basename(__FILE__)); \
|
||||
trusty_printf(":" TRUSTY_TO_STRING(__LINE__) ": FATAL "); \
|
||||
trusty_printf(message, ##__VA_ARGS__); \
|
||||
trusty_abort(); \
|
||||
} while(0)
|
||||
|
||||
#endif /* TRUSTY_UTIL_H_ */
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
*/
|
||||
|
||||
#include <trusty/keymaster.h>
|
||||
#include <trusty/keymaster_serializable.h>
|
||||
#include <trusty/rpmb.h>
|
||||
#include <trusty/trusty_ipc.h>
|
||||
#include <trusty/util.h>
|
||||
|
@ -32,50 +33,165 @@
|
|||
static struct trusty_ipc_chan km_chan;
|
||||
static bool initialized;
|
||||
static int trusty_km_version = 2;
|
||||
static const size_t max_ca_request_size = 10000;
|
||||
static const size_t max_send_size = 4000;
|
||||
|
||||
static int km_send_request(struct keymaster_message *msg, void *req,
|
||||
size_t req_len)
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifndef NELEMS
|
||||
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
|
||||
#endif
|
||||
|
||||
static int km_send_request(uint32_t cmd, const void *req, size_t req_len)
|
||||
{
|
||||
struct keymaster_message header = { .cmd = cmd };
|
||||
int num_iovecs = req ? 2 : 1;
|
||||
|
||||
struct trusty_ipc_iovec req_iovs[2] = {
|
||||
{ .base = msg, .len = sizeof(*msg) },
|
||||
{ .base = req, .len = req_len },
|
||||
{ .base = &header, .len = sizeof(header) },
|
||||
{ .base = (void*)req, .len = req_len },
|
||||
};
|
||||
|
||||
return trusty_ipc_send(&km_chan, req_iovs, num_iovecs, true);
|
||||
}
|
||||
|
||||
static int km_read_response(struct keymaster_message *msg, uint32_t cmd,
|
||||
void *resp, size_t resp_len)
|
||||
/* Checks that the command opcode in |header| matches |ex-ected_cmd|. Checks
|
||||
* that |tipc_result| is a valid response size. Returns negative on error.
|
||||
*/
|
||||
static int check_response_error(uint32_t expected_cmd,
|
||||
struct keymaster_message header,
|
||||
int32_t tipc_result)
|
||||
{
|
||||
int rc = TRUSTY_ERR_GENERIC;
|
||||
struct trusty_ipc_iovec resp_iovs[2] = {
|
||||
{ .base = msg, .len = sizeof(*msg) },
|
||||
{ .base = resp, .len = resp_len },
|
||||
};
|
||||
|
||||
rc = trusty_ipc_recv(&km_chan, resp_iovs, resp ? 2 : 1, true);
|
||||
if (rc < 0) {
|
||||
trusty_error("failed (%d) to recv response\n", rc);
|
||||
return rc;
|
||||
if (tipc_result < 0) {
|
||||
trusty_error("failed (%d) to recv response\n", tipc_result);
|
||||
return tipc_result;
|
||||
}
|
||||
if ((msg->cmd & ~(KEYMASTER_STOP_BIT)) != (cmd | KEYMASTER_RESP_BIT)) {
|
||||
if ((size_t) tipc_result < sizeof(struct keymaster_message)) {
|
||||
trusty_error("invalid response size (%d)\n", tipc_result);
|
||||
return TRUSTY_ERR_GENERIC;
|
||||
}
|
||||
if ((header.cmd & ~(KEYMASTER_STOP_BIT)) !=
|
||||
(expected_cmd | KEYMASTER_RESP_BIT)) {
|
||||
trusty_error("malformed response\n");
|
||||
return TRUSTY_ERR_GENERIC;
|
||||
}
|
||||
|
||||
return rc;
|
||||
return tipc_result;
|
||||
}
|
||||
|
||||
static int km_do_tipc(uint32_t cmd, void *req, uint32_t req_len,
|
||||
bool handle_rpmb)
|
||||
/* Reads the raw response to |resp| up to a maximum size of |resp_len|. Format
|
||||
* of each message frame read from the secure side:
|
||||
*
|
||||
* command header : 4 bytes
|
||||
* opaque bytes : MAX(KEYMASTER_MAX_BUFFER_LENGTH, x) bytes
|
||||
*
|
||||
* The individual message frames from the secure side are reassembled
|
||||
* into |resp|, stripping each frame's command header. Returns the number
|
||||
* of bytes written to |resp| on success, negative on error.
|
||||
*/
|
||||
static int km_read_raw_response(uint32_t cmd, void *resp, size_t resp_len)
|
||||
{
|
||||
struct keymaster_message header = { .cmd = cmd };
|
||||
int rc = TRUSTY_ERR_GENERIC;
|
||||
size_t max_resp_len = resp_len;
|
||||
struct trusty_ipc_iovec resp_iovs[2] = {
|
||||
{ .base = &header, .len = sizeof(header) },
|
||||
{ .base = resp, .len = MIN(KEYMASTER_MAX_BUFFER_LENGTH, max_resp_len) }
|
||||
};
|
||||
|
||||
if (!resp) {
|
||||
return TRUSTY_ERR_GENERIC;
|
||||
}
|
||||
resp_len = 0;
|
||||
while (true) {
|
||||
resp_iovs[1].base = (uint8_t*)resp + resp_len;
|
||||
resp_iovs[1].len = MIN(KEYMASTER_MAX_BUFFER_LENGTH,
|
||||
(int)max_resp_len - (int)resp_len);
|
||||
|
||||
rc = trusty_ipc_recv(&km_chan, resp_iovs, NELEMS(resp_iovs), true);
|
||||
rc = check_response_error(cmd, header, rc);
|
||||
if (rc < 0) {
|
||||
return rc;
|
||||
}
|
||||
resp_len += ((size_t)rc - sizeof(struct keymaster_message));
|
||||
if (header.cmd & KEYMASTER_STOP_BIT || resp_len >= max_resp_len) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return resp_len;
|
||||
}
|
||||
|
||||
/* Reads a Keymaster Response message with a sized buffer. The format
|
||||
* of the response is as follows:
|
||||
*
|
||||
* command header : 4 bytes
|
||||
* error : 4 bytes
|
||||
* data length : 4 bytes
|
||||
* data : |data length| bytes
|
||||
*
|
||||
* On success, |error|, |resp_data|, and |resp_data_len| are filled
|
||||
* successfully. Returns a trusty_err.
|
||||
*/
|
||||
static int km_read_data_response(uint32_t cmd, int32_t *error,
|
||||
uint8_t* resp_data, uint32_t* resp_data_len)
|
||||
{
|
||||
struct keymaster_message header = { .cmd = cmd };
|
||||
int rc = TRUSTY_ERR_GENERIC;
|
||||
size_t max_resp_len = *resp_data_len;
|
||||
uint32_t resp_data_bytes = 0;
|
||||
/* On the first read, recv the keymaster_message header, error code,
|
||||
* response data length, and response data. On subsequent iterations,
|
||||
* only recv the keymaster_message header and response data.
|
||||
*/
|
||||
struct trusty_ipc_iovec resp_iovs[4] = {
|
||||
{ .base = &header, .len = sizeof(header) },
|
||||
{ .base = error, .len = sizeof(int32_t) },
|
||||
{ .base = resp_data_len, .len = sizeof(uint32_t) },
|
||||
{ .base = resp_data, .len = MIN(KEYMASTER_MAX_BUFFER_LENGTH, max_resp_len) }
|
||||
};
|
||||
|
||||
rc = trusty_ipc_recv(&km_chan, resp_iovs, NELEMS(resp_iovs), true);
|
||||
rc = check_response_error(cmd, header, rc);
|
||||
if (rc < 0) {
|
||||
return rc;
|
||||
}
|
||||
/* resp_data_bytes does not include the error or response data length */
|
||||
resp_data_bytes += ((size_t)rc - sizeof(struct keymaster_message) -
|
||||
2 * sizeof(uint32_t));
|
||||
if (header.cmd & KEYMASTER_STOP_BIT) {
|
||||
return TRUSTY_ERR_NONE;
|
||||
}
|
||||
|
||||
/* Read the remaining response data */
|
||||
uint8_t* resp_data_start = resp_data + resp_data_bytes;
|
||||
size_t resp_data_remaining = *resp_data_len - resp_data_bytes;
|
||||
rc = km_read_raw_response(cmd, resp_data_start, resp_data_remaining);
|
||||
if (rc < 0) {
|
||||
return rc;
|
||||
}
|
||||
resp_data_bytes += rc;
|
||||
if (*resp_data_len != resp_data_bytes) {
|
||||
return TRUSTY_ERR_GENERIC;
|
||||
}
|
||||
return TRUSTY_ERR_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to send a request to the secure side, handle rpmb
|
||||
* operations, and receive the response. If |resp_data| is not NULL, the
|
||||
* caller expects an additional data buffer to be returned from the secure
|
||||
* side.
|
||||
*/
|
||||
static int km_do_tipc(uint32_t cmd, bool handle_rpmb, void* req,
|
||||
uint32_t req_len, void* resp_data,
|
||||
uint32_t* resp_data_len)
|
||||
{
|
||||
int rc = TRUSTY_ERR_GENERIC;
|
||||
struct keymaster_message msg = { .cmd = cmd };
|
||||
struct km_no_response resp;
|
||||
struct km_no_response resp_header;
|
||||
|
||||
rc = km_send_request(&msg, req, req_len);
|
||||
rc = km_send_request(cmd, req, req_len);
|
||||
if (rc < 0) {
|
||||
trusty_error("%s: failed (%d) to send km request\n", __func__, rc);
|
||||
return rc;
|
||||
|
@ -85,18 +201,28 @@ static int km_do_tipc(uint32_t cmd, void *req, uint32_t req_len,
|
|||
/* handle any incoming RPMB requests */
|
||||
rc = rpmb_storage_proxy_poll();
|
||||
if (rc < 0) {
|
||||
trusty_error("%s: failed (%d) to get RPMB requests\n", __func__,
|
||||
rc);
|
||||
trusty_error("%s: failed (%d) to get RPMB requests\n", __func__, rc);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
rc = km_read_response(&msg, cmd, &resp, sizeof(resp));
|
||||
if (!resp_data) {
|
||||
rc = km_read_raw_response(cmd, &resp_header, sizeof(resp_header));
|
||||
} else {
|
||||
rc = km_read_data_response(cmd, &resp_header.error, resp_data,
|
||||
resp_data_len);
|
||||
}
|
||||
|
||||
if (rc < 0) {
|
||||
trusty_error("%s: failed (%d) to read km response\n", __func__, rc);
|
||||
return rc;
|
||||
}
|
||||
return resp.error;
|
||||
if (resp_header.error != KM_ERROR_OK) {
|
||||
trusty_error("%s: keymaster returned error (%d)\n", __func__,
|
||||
resp_header.error);
|
||||
return TRUSTY_ERR_GENERIC;
|
||||
}
|
||||
return TRUSTY_ERR_NONE;
|
||||
}
|
||||
|
||||
static int32_t MessageVersion(uint8_t major_ver, uint8_t minor_ver,
|
||||
|
@ -126,16 +252,15 @@ static int32_t MessageVersion(uint8_t major_ver, uint8_t minor_ver,
|
|||
static int km_get_version(int32_t *version)
|
||||
{
|
||||
int rc = TRUSTY_ERR_GENERIC;
|
||||
struct keymaster_message msg = { .cmd = KM_GET_VERSION };
|
||||
struct km_get_version_resp resp;
|
||||
|
||||
rc = km_send_request(&msg, NULL, 0);
|
||||
rc = km_send_request(KM_GET_VERSION, NULL, 0);
|
||||
if (rc < 0) {
|
||||
trusty_error("failed to send km version request", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = km_read_response(&msg, KM_GET_VERSION, &resp, sizeof(resp));
|
||||
rc = km_read_raw_response(KM_GET_VERSION, &resp, sizeof(resp));
|
||||
if (rc < 0) {
|
||||
trusty_error("%s: failed (%d) to read km response\n", __func__, rc);
|
||||
return rc;
|
||||
|
@ -143,7 +268,7 @@ static int km_get_version(int32_t *version)
|
|||
|
||||
*version = MessageVersion(resp.major_ver, resp.minor_ver,
|
||||
resp.subminor_ver);
|
||||
return rc;
|
||||
return TRUSTY_ERR_NONE;
|
||||
}
|
||||
|
||||
int km_tipc_init(struct trusty_ipc_dev *dev)
|
||||
|
@ -187,92 +312,6 @@ void km_tipc_shutdown(struct trusty_ipc_dev *dev)
|
|||
initialized = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends |data_len| bytes at |data| to |buf|. Performs no bounds checking,
|
||||
* assumes sufficient memory allocated at |buf|. Returns |buf| + |data_len|.
|
||||
*/
|
||||
static uint8_t *append_to_buf(uint8_t *buf, const void *data, size_t data_len)
|
||||
{
|
||||
if (data && data_len) {
|
||||
trusty_memcpy(buf, data, data_len);
|
||||
}
|
||||
return buf + data_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends |val| to |buf|. Performs no bounds checking. Returns |buf| +
|
||||
* sizeof(uint32_t).
|
||||
*/
|
||||
static uint8_t *append_uint32_to_buf(uint8_t *buf, uint32_t val)
|
||||
{
|
||||
return append_to_buf(buf, &val, sizeof(val));
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a sized buffer to |buf|. First appends |data_len| to |buf|, then
|
||||
* appends |data_len| bytes at |data| to |buf|. Performs no bounds checking.
|
||||
* Returns |buf| + sizeof(uint32_t) + |data_len|.
|
||||
*/
|
||||
static uint8_t *append_sized_buf_to_buf(uint8_t *buf, const uint8_t *data,
|
||||
uint32_t data_len)
|
||||
{
|
||||
buf = append_uint32_to_buf(buf, data_len);
|
||||
return append_to_buf(buf, data, data_len);
|
||||
}
|
||||
|
||||
int km_boot_params_serialize(const struct km_boot_params *params, uint8_t** out,
|
||||
uint32_t *out_size)
|
||||
{
|
||||
uint8_t *tmp;
|
||||
|
||||
if (!out || !params || !out_size) {
|
||||
return TRUSTY_ERR_INVALID_ARGS;
|
||||
}
|
||||
*out_size = (sizeof(params->os_version) + sizeof(params->os_patchlevel) +
|
||||
sizeof(params->device_locked) +
|
||||
sizeof(params->verified_boot_state) +
|
||||
sizeof(params->verified_boot_key_hash_size) +
|
||||
sizeof(params->verified_boot_hash_size) +
|
||||
params->verified_boot_key_hash_size +
|
||||
params->verified_boot_hash_size);
|
||||
*out = trusty_calloc(*out_size, 1);
|
||||
if (!*out) {
|
||||
return TRUSTY_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
tmp = append_uint32_to_buf(*out, params->os_version);
|
||||
tmp = append_uint32_to_buf(tmp, params->os_patchlevel);
|
||||
tmp = append_uint32_to_buf(tmp, params->device_locked);
|
||||
tmp = append_uint32_to_buf(tmp, params->verified_boot_state);
|
||||
tmp = append_sized_buf_to_buf(tmp, params->verified_boot_key_hash,
|
||||
params->verified_boot_key_hash_size);
|
||||
tmp = append_sized_buf_to_buf(tmp, params->verified_boot_hash,
|
||||
params->verified_boot_hash_size);
|
||||
|
||||
return TRUSTY_ERR_NONE;
|
||||
}
|
||||
|
||||
int km_attestation_data_serialize(const struct km_attestation_data *data,
|
||||
uint8_t** out, uint32_t *out_size)
|
||||
{
|
||||
uint8_t *tmp;
|
||||
|
||||
if (!out || !data || !out_size) {
|
||||
return TRUSTY_ERR_INVALID_ARGS;
|
||||
}
|
||||
*out_size = (sizeof(data->algorithm) + sizeof(data->data_size) +
|
||||
data->data_size);
|
||||
*out = trusty_calloc(*out_size, 1);
|
||||
if (!*out) {
|
||||
return TRUSTY_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
tmp = append_uint32_to_buf(*out, data->algorithm);
|
||||
tmp = append_sized_buf_to_buf(tmp, data->data, data->data_size);
|
||||
|
||||
return TRUSTY_ERR_NONE;
|
||||
}
|
||||
|
||||
int trusty_set_boot_params(uint32_t os_version, uint32_t os_patchlevel,
|
||||
keymaster_verified_boot_t verified_boot_state,
|
||||
bool device_locked,
|
||||
|
@ -299,7 +338,7 @@ int trusty_set_boot_params(uint32_t os_version, uint32_t os_patchlevel,
|
|||
trusty_error("failed (%d) to serialize request\n", rc);
|
||||
goto end;
|
||||
}
|
||||
rc = km_do_tipc(KM_SET_BOOT_PARAMS, req, req_size, false);
|
||||
rc = km_do_tipc(KM_SET_BOOT_PARAMS, false, req, req_size, NULL, NULL);
|
||||
|
||||
end:
|
||||
if (req) {
|
||||
|
@ -325,7 +364,31 @@ static int trusty_send_attestation_data(uint32_t cmd, const uint8_t *data,
|
|||
trusty_error("failed (%d) to serialize request\n", rc);
|
||||
goto end;
|
||||
}
|
||||
rc = km_do_tipc(cmd, req, req_size, true);
|
||||
rc = km_do_tipc(cmd, true, req, req_size, NULL, NULL);
|
||||
|
||||
end:
|
||||
if (req) {
|
||||
trusty_free(req);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int trusty_send_raw_buffer(uint32_t cmd, const uint8_t *req_data,
|
||||
uint32_t req_data_size, uint8_t *resp_data,
|
||||
uint32_t *resp_data_size)
|
||||
{
|
||||
struct km_raw_buffer buf = {
|
||||
.data_size = req_data_size,
|
||||
.data = req_data,
|
||||
};
|
||||
uint8_t *req = NULL;
|
||||
uint32_t req_size = 0;
|
||||
int rc = km_raw_buffer_serialize(&buf, &req, &req_size);
|
||||
if (rc < 0) {
|
||||
trusty_error("failed (%d) to serialize request\n", rc);
|
||||
goto end;
|
||||
}
|
||||
rc = km_do_tipc(cmd, false, req, req_size, resp_data, resp_data_size);
|
||||
|
||||
end:
|
||||
if (req) {
|
||||
|
@ -348,3 +411,53 @@ int trusty_append_attestation_cert_chain(const uint8_t *cert,
|
|||
return trusty_send_attestation_data(KM_APPEND_ATTESTATION_CERT_CHAIN,
|
||||
cert, cert_size, algorithm);
|
||||
}
|
||||
|
||||
int trusty_atap_get_ca_request(const uint8_t *operation_start,
|
||||
uint32_t operation_start_size,
|
||||
uint8_t **ca_request_p,
|
||||
uint32_t *ca_request_size_p)
|
||||
{
|
||||
*ca_request_p = trusty_calloc(1, max_ca_request_size);
|
||||
if (!*ca_request_p) {
|
||||
return TRUSTY_ERR_NO_MEMORY;
|
||||
}
|
||||
*ca_request_size_p = max_ca_request_size;
|
||||
int rc = trusty_send_raw_buffer(KM_ATAP_GET_CA_REQUEST, operation_start,
|
||||
operation_start_size, *ca_request_p,
|
||||
ca_request_size_p);
|
||||
if (rc != TRUSTY_ERR_NONE) {
|
||||
trusty_free(*ca_request_p);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int trusty_atap_set_ca_response(const uint8_t *ca_response,
|
||||
uint32_t ca_response_size)
|
||||
{
|
||||
struct km_set_ca_response_begin_req begin_req;
|
||||
int rc = TRUSTY_ERR_GENERIC;
|
||||
uint32_t bytes_sent = 0, send_size = 0;
|
||||
|
||||
/* Tell the Trusty Keymaster TA the size of CA Response message */
|
||||
begin_req.ca_response_size = ca_response_size;
|
||||
rc = km_do_tipc(KM_ATAP_SET_CA_RESPONSE_BEGIN, false, &begin_req,
|
||||
sizeof(begin_req), NULL, NULL);
|
||||
if (rc != TRUSTY_ERR_NONE) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Send the CA Response message in chunks */
|
||||
while (bytes_sent < ca_response_size) {
|
||||
send_size = MIN(max_send_size, ca_response_size - bytes_sent);
|
||||
rc = trusty_send_raw_buffer(KM_ATAP_SET_CA_RESPONSE_UPDATE,
|
||||
ca_response + bytes_sent, send_size,
|
||||
NULL, NULL);
|
||||
if (rc != TRUSTY_ERR_NONE) {
|
||||
return rc;
|
||||
}
|
||||
bytes_sent += send_size;
|
||||
}
|
||||
|
||||
/* Tell Trusty Keymaster to parse the CA Response message */
|
||||
return km_do_tipc(KM_ATAP_SET_CA_RESPONSE_FINISH, true, NULL, 0, NULL, NULL);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use, copy,
|
||||
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
* of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <trusty/keymaster_serializable.h>
|
||||
|
||||
uint8_t *append_to_buf(uint8_t *buf, const void *data, size_t data_len)
|
||||
{
|
||||
if (data && data_len) {
|
||||
trusty_memcpy(buf, data, data_len);
|
||||
}
|
||||
return buf + data_len;
|
||||
}
|
||||
|
||||
uint8_t *append_uint32_to_buf(uint8_t *buf, uint32_t val)
|
||||
{
|
||||
return append_to_buf(buf, &val, sizeof(val));
|
||||
}
|
||||
|
||||
uint8_t *append_sized_buf_to_buf(uint8_t *buf, const uint8_t *data,
|
||||
uint32_t data_len)
|
||||
{
|
||||
buf = append_uint32_to_buf(buf, data_len);
|
||||
return append_to_buf(buf, data, data_len);
|
||||
}
|
||||
|
||||
int km_boot_params_serialize(const struct km_boot_params *params, uint8_t** out,
|
||||
uint32_t *out_size)
|
||||
{
|
||||
uint8_t *tmp;
|
||||
|
||||
if (!out || !params || !out_size) {
|
||||
return TRUSTY_ERR_INVALID_ARGS;
|
||||
}
|
||||
*out_size = (sizeof(params->os_version) + sizeof(params->os_patchlevel) +
|
||||
sizeof(params->device_locked) +
|
||||
sizeof(params->verified_boot_state) +
|
||||
sizeof(params->verified_boot_key_hash_size) +
|
||||
sizeof(params->verified_boot_hash_size) +
|
||||
params->verified_boot_key_hash_size +
|
||||
params->verified_boot_hash_size);
|
||||
*out = trusty_calloc(*out_size, 1);
|
||||
if (!*out) {
|
||||
return TRUSTY_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
tmp = append_uint32_to_buf(*out, params->os_version);
|
||||
tmp = append_uint32_to_buf(tmp, params->os_patchlevel);
|
||||
tmp = append_uint32_to_buf(tmp, params->device_locked);
|
||||
tmp = append_uint32_to_buf(tmp, params->verified_boot_state);
|
||||
tmp = append_sized_buf_to_buf(tmp, params->verified_boot_key_hash,
|
||||
params->verified_boot_key_hash_size);
|
||||
tmp = append_sized_buf_to_buf(tmp, params->verified_boot_hash,
|
||||
params->verified_boot_hash_size);
|
||||
|
||||
return TRUSTY_ERR_NONE;
|
||||
}
|
||||
|
||||
int km_attestation_data_serialize(const struct km_attestation_data *data,
|
||||
uint8_t** out, uint32_t *out_size)
|
||||
{
|
||||
uint8_t *tmp;
|
||||
|
||||
if (!out || !data || !out_size) {
|
||||
return TRUSTY_ERR_INVALID_ARGS;
|
||||
}
|
||||
*out_size = (sizeof(data->algorithm) + sizeof(data->data_size) +
|
||||
data->data_size);
|
||||
*out = trusty_calloc(*out_size, 1);
|
||||
if (!*out) {
|
||||
return TRUSTY_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
tmp = append_uint32_to_buf(*out, data->algorithm);
|
||||
tmp = append_sized_buf_to_buf(tmp, data->data, data->data_size);
|
||||
|
||||
return TRUSTY_ERR_NONE;
|
||||
}
|
||||
|
||||
int km_raw_buffer_serialize(const struct km_raw_buffer *buf, uint8_t** out,
|
||||
uint32_t *out_size)
|
||||
{
|
||||
if (!out || !buf || !out_size) {
|
||||
return TRUSTY_ERR_INVALID_ARGS;
|
||||
}
|
||||
*out_size = sizeof(buf->data_size) + buf->data_size;
|
||||
*out = trusty_calloc(*out_size, 1);
|
||||
if (!*out) {
|
||||
return TRUSTY_ERR_NO_MEMORY;
|
||||
}
|
||||
append_sized_buf_to_buf(*out, buf->data, buf->data_size);
|
||||
|
||||
return TRUSTY_ERR_NONE;
|
||||
}
|
|
@ -27,7 +27,7 @@
|
|||
#include <trusty/util.h>
|
||||
#include <interface/storage/storage.h>
|
||||
|
||||
#define LOCAL_LOG 1
|
||||
#define LOCAL_LOG 0
|
||||
|
||||
static bool initialized;
|
||||
/* Address of rpmb device */
|
||||
|
@ -314,7 +314,7 @@ int rpmb_storage_proxy_init(struct trusty_ipc_dev *dev, void *rpmb_dev)
|
|||
int rpmb_storage_proxy_poll(void)
|
||||
{
|
||||
int rc = 0;
|
||||
while (rc != TRUSTY_EVENT_NONE) {
|
||||
while ((rc != TRUSTY_EVENT_NONE) && (proxy_chan.handle != INVALID_IPC_HANDLE)){
|
||||
/* Check for RPMB events */
|
||||
rc = trusty_ipc_poll_for_event(&proxy_chan);
|
||||
if (rc < 0) {
|
||||
|
@ -322,7 +322,7 @@ int rpmb_storage_proxy_poll(void)
|
|||
return rc;
|
||||
}
|
||||
}
|
||||
return TRUSTY_ERR_NONE;
|
||||
return (proxy_chan.handle)? TRUSTY_ERR_NONE : TRUSTY_ERR_CHANNEL_CLOSED;
|
||||
}
|
||||
|
||||
void rpmb_storage_proxy_shutdown(struct trusty_ipc_dev *dev)
|
||||
|
|
|
@ -30,14 +30,16 @@ TRUSTY_DIR = lib/trusty
|
|||
#ccflags-y += -I$(TRUSTY_DIR)/ql-tipc/include
|
||||
ccflags-y += -I$(TRUSTY_DIR)/interface/include
|
||||
|
||||
QL_TIPC = ../
|
||||
QL_TIPC = ..
|
||||
obj-y += \
|
||||
$(QL_TIPC)/avb.o \
|
||||
$(QL_TIPC)/keymaster.o \
|
||||
$(QL_TIPC)/keymaster_serializable.o \
|
||||
$(QL_TIPC)/ipc.o \
|
||||
$(QL_TIPC)/ipc_dev.o \
|
||||
$(QL_TIPC)/libtipc.o \
|
||||
$(QL_TIPC)/rpmb_proxy.o \
|
||||
$(QL_TIPC)/util.o \
|
||||
sysdeps_uboot.o \
|
||||
storage_ops_uboot.o
|
||||
|
||||
|
|
|
@ -59,12 +59,12 @@ void trusty_abort(void)
|
|||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
void trusty_printv(const char *message, ...)
|
||||
void trusty_printf(const char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, message);
|
||||
vprintf(message, ap);
|
||||
va_start(ap, format);
|
||||
vprintf(format, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use, copy,
|
||||
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
* of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <trusty/util.h>
|
||||
|
||||
const char* trusty_basename(const char* str) {
|
||||
int64_t n;
|
||||
size_t len;
|
||||
|
||||
len = trusty_strlen(str);
|
||||
if (len >= 2) {
|
||||
for (n = len - 2; n >= 0; n--) {
|
||||
if (str[n] == '/') {
|
||||
return str + n + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
Loading…
Reference in New Issue