blob: 5c88018f4a3d1316e82e5b4aa4c97470d380dc6c [file] [log] [blame]
Julian Hall700aa362021-05-13 15:30:39 +01001/*
2 * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stddef.h>
8#include <string.h>
9#include "iat_client.h"
10#include <common/tlv/tlv.h>
11#include <psa/initial_attestation.h>
12#include <protocols/service/attestation/packed-c/get_token.h>
13#include <protocols/service/attestation/packed-c/get_token_size.h>
14#include <protocols/service/attestation/packed-c/opcodes.h>
15#include <protocols/rpc/common/packed-c/status.h>
16
17/**
18 * @brief The singleton psa_iat_client instance
19 *
20 * The psa attestation C API assumes a single backend service provider. This
21 * structure defines the state used by the psa_iat_client that communicates
22 * with a remote provider using the provided rpc caller.
23 */
24static struct psa_iat_client
25{
26 struct rpc_caller *caller;
27 int rpc_status;
28} instance;
29
30
31psa_status_t psa_iat_client_init(struct rpc_caller *caller)
32{
33 instance.caller = caller;
34 instance.rpc_status = TS_RPC_CALL_ACCEPTED;
35
36 return PSA_SUCCESS;
37}
38
39void psa_iat_client_deinit(void)
40{
41 instance.caller = NULL;
42}
43
44int psa_iat_client_rpc_status(void)
45{
46 return instance.rpc_status;
47}
48
49psa_status_t psa_initial_attest_get_token(
50 const uint8_t *auth_challenge, size_t challenge_size,
51 uint8_t *token_buf, size_t token_buf_size, size_t *token_size)
52{
53 psa_status_t psa_status = PSA_ERROR_INVALID_ARGUMENT;
54 size_t req_len = tlv_required_space(challenge_size);
55
56 struct tlv_record challenge_record;
57 challenge_record.tag = TS_ATTESTATION_GET_TOKEN_IN_TAG_AUTH_CHALLENGE;
58 challenge_record.length = challenge_size;
59 challenge_record.value = auth_challenge;
60
61 rpc_call_handle call_handle;
62 uint8_t *req_buf;
63
64 *token_size = 0;
65
66 call_handle = rpc_caller_begin(instance.caller, &req_buf, req_len);
67
68 if (call_handle) {
69
70 uint8_t *resp_buf;
71 size_t resp_len;
72 int opstatus;
73 struct tlv_iterator req_iter;
74
75 tlv_iterator_begin(&req_iter, req_buf, req_len);
76 tlv_encode(&req_iter, &challenge_record);
77
78 instance.rpc_status = rpc_caller_invoke(instance.caller, call_handle,
79 TS_ATTESTATION_OPCODE_GET_TOKEN, &opstatus, &resp_buf, &resp_len);
80
81 if (instance.rpc_status == TS_RPC_CALL_ACCEPTED) {
82
83 psa_status = opstatus;
84
85 if (psa_status == PSA_SUCCESS) {
86
87 struct tlv_const_iterator resp_iter;
88 struct tlv_record decoded_record;
89 tlv_const_iterator_begin(&resp_iter, resp_buf, resp_len);
90
91 if (tlv_find_decode(&resp_iter,
92 TS_ATTESTATION_GET_TOKEN_OUT_TAG_TOKEN, &decoded_record)) {
93
94 if (decoded_record.length <= token_buf_size) {
95
96 memcpy(token_buf, decoded_record.value, decoded_record.length);
97 *token_size = decoded_record.length;
98 }
99 else {
100 /* Provided buffer is too small */
101 psa_status = PSA_ERROR_BUFFER_TOO_SMALL;
102 }
103 }
104 else {
105 /* Mandatory response parameter missing */
106 psa_status = PSA_ERROR_GENERIC_ERROR;
107 }
108 }
109 }
110
111 rpc_caller_end(instance.caller, call_handle);
112 }
113
114 return psa_status;
115}
116
117psa_status_t psa_initial_attest_get_token_size(
118 size_t challenge_size, size_t *token_size)
119{
120 psa_status_t psa_status = PSA_ERROR_INVALID_ARGUMENT;
121 struct ts_attestation_get_token_size_in req_msg;
122 size_t req_len = sizeof(struct ts_attestation_get_token_size_in);
123
124 *token_size = 0; /* For failure case */
125
126 req_msg.challenge_size = challenge_size;
127
128 rpc_call_handle call_handle;
129 uint8_t *req_buf;
130
131 call_handle = rpc_caller_begin(instance.caller, &req_buf, req_len);
132
133 if (call_handle) {
134
135 uint8_t *resp_buf;
136 size_t resp_len;
137 int opstatus;
138 struct tlv_iterator req_iter;
139
140 memcpy(req_buf, &req_msg, req_len);
141
142 instance.rpc_status = rpc_caller_invoke(instance.caller, call_handle,
143 TS_ATTESTATION_OPCODE_GET_TOKEN_SIZE, &opstatus, &resp_buf, &resp_len);
144
145 if (instance.rpc_status == TS_RPC_CALL_ACCEPTED) {
146
147 psa_status = opstatus;
148
149 if (psa_status == PSA_SUCCESS) {
150
151 if (resp_len >= sizeof(struct ts_attestation_get_token_size_out)) {
152
153 struct ts_attestation_get_token_size_out resp_msg;
154 memcpy(&resp_msg, resp_buf, sizeof(struct ts_attestation_get_token_size_out));
155 *token_size = resp_msg.token_size;
156 }
157 else {
158 /* Failed to decode response message */
159 psa_status = PSA_ERROR_GENERIC_ERROR;
160 }
161 }
162 }
163
164 rpc_caller_end(instance.caller, call_handle);
165 }
166
167 return psa_status;
168}