blob: 0c4215ffaaf72cecb3a3a2c14d71fc45693b7bbd [file] [log] [blame]
Jamie Foxab30e712023-03-30 17:48:36 +01001/*
2 * Copyright (c) 2023, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "dpe_cmd_decode.h"
9
10#include <string.h>
11
12#include "dpe_client.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +010013#include "dpe_context_mngr.h"
Jamie Foxab30e712023-03-30 17:48:36 +010014#include "dpe_impl.h"
15#include "qcbor/qcbor_encode.h"
16#include "qcbor/qcbor_decode.h"
17#include "qcbor/qcbor_spiffy_decode.h"
18
19static dpe_error_t decode_dice_inputs(QCBORDecodeContext *decode_ctx,
20 DiceInputValues *input)
21{
22 QCBORError qcbor_err;
23 UsefulBufC out = { NULL, 0 };
24 int64_t out_int;
25
26 /* The DICE inputs are encoded as a map wrapped into a byte string */
27 QCBORDecode_EnterBstrWrappedFromMapN(decode_ctx,
28 DPE_DERIVE_CHILD_INPUT_DATA,
29 QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
30 QCBORDecode_EnterMap(decode_ctx, NULL);
31
32 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CODE_HASH, &out);
33 if (out.len != sizeof(input->code_hash)) {
34 return DPE_INVALID_COMMAND;
35 }
36 memcpy(input->code_hash, out.ptr, out.len);
37
38 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CODE_DESCRIPTOR, &out);
39 input->code_descriptor = out.ptr;
40 input->code_descriptor_size = out.len;
41
42 QCBORDecode_GetInt64InMapN(decode_ctx, DICE_CONFIG_TYPE, &out_int);
43
44 /* Check error state before interpreting config type */
45 qcbor_err = QCBORDecode_GetError(decode_ctx);
46 if (qcbor_err != QCBOR_SUCCESS) {
47 return DPE_INVALID_COMMAND;
48 }
49
50 if (out_int < kDiceConfigTypeInline ||
51 out_int > kDiceConfigTypeDescriptor) {
52 return DPE_INVALID_COMMAND;
53 }
54 input->config_type = (DiceConfigType)out_int;
55
56 /* Only one of config value or config descriptor needs to be provided */
57 if (input->config_type == kDiceConfigTypeInline) {
58 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CONFIG_VALUE, &out);
59 if (out.len != sizeof(input->config_value)) {
60 return DPE_INVALID_COMMAND;
61 }
62 memcpy(input->config_value, out.ptr, out.len);
63
64 /* Config descriptor is not provided */
65 input->config_descriptor = NULL;
66 input->config_descriptor_size = 0;
67 } else {
68 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_CONFIG_DESCRIPTOR,
69 &out);
70 input->config_descriptor = out.ptr;
71 input->config_descriptor_size = out.len;
72
73 /* Config value is not provided */
74 memset(input->config_value, 0, sizeof(input->config_value));
75 }
76
77 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_AUTHORITY_HASH, &out);
78 if (out.len != sizeof(input->authority_hash)) {
79 return DPE_INVALID_COMMAND;
80 }
81 memcpy(input->authority_hash, out.ptr, out.len);
82
83 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_AUTHORITY_DESCRIPTOR,
84 &out);
85 input->authority_descriptor = out.ptr;
86 input->authority_descriptor_size = out.len;
87
88 QCBORDecode_GetInt64InMapN(decode_ctx, DICE_MODE, &out_int);
89 if (out_int < kDiceModeNotInitialized || out_int > kDiceModeMaintenance) {
90 return DPE_INVALID_COMMAND;
91 }
92 input->mode = (DiceMode)out_int;
93
94 QCBORDecode_GetByteStringInMapN(decode_ctx, DICE_HIDDEN, &out);
95 if (out.len != sizeof(input->hidden)) {
96 return DPE_INVALID_COMMAND;
97 }
98 memcpy(input->hidden, out.ptr, out.len);
99
100 QCBORDecode_ExitMap(decode_ctx);
101 QCBORDecode_ExitBstrWrapped(decode_ctx);
102
103 return DPE_NO_ERROR;
104}
105
106static dpe_error_t decode_derive_child(QCBORDecodeContext *decode_ctx,
Maulik Patelad2f3db2023-05-17 15:41:36 +0100107 QCBOREncodeContext *encode_ctx,
108 int32_t client_id)
Jamie Foxab30e712023-03-30 17:48:36 +0100109{
110 dpe_error_t dpe_err;
111 QCBORError qcbor_err;
112 UsefulBufC out;
113 int context_handle;
114 bool retain_parent_context;
115 bool allow_child_to_derive;
116 bool create_certificate;
117 DiceInputValues dice_inputs;
118 int new_child_context_handle;
119 int new_parent_context_handle;
120
121 /* Decode DeriveChild command */
122 QCBORDecode_EnterMap(decode_ctx, NULL);
123
124 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_DERIVE_CHILD_CONTEXT_HANDLE,
125 &out);
126 if (out.len != sizeof(context_handle)) {
127 return DPE_INVALID_COMMAND;
128 }
129 memcpy(&context_handle, out.ptr, out.len);
130
131 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CHILD_RETAIN_PARENT_CONTEXT,
132 &retain_parent_context);
133
134 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CHILD_ALLOW_CHILD_TO_DERIVE,
135 &allow_child_to_derive);
136
137 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_DERIVE_CHILD_CREATE_CERTIFICATE,
138 &create_certificate);
139
140 dpe_err = decode_dice_inputs(decode_ctx, &dice_inputs);
141 if (dpe_err != DPE_NO_ERROR) {
142 return dpe_err;
143 }
144
145 QCBORDecode_ExitMap(decode_ctx);
146
147 /* Exit top level array */
148 QCBORDecode_ExitArray(decode_ctx);
149
150 /* Finish and check for errors before using decoded values */
151 qcbor_err = QCBORDecode_Finish(decode_ctx);
152 if (qcbor_err != QCBOR_SUCCESS) {
153 return DPE_INVALID_COMMAND;
154 }
155
Maulik Patelad2f3db2023-05-17 15:41:36 +0100156 dpe_err = derive_child_request(context_handle, retain_parent_context,
157 allow_child_to_derive, create_certificate,
158 &dice_inputs, client_id,
159 &new_child_context_handle,
160 &new_parent_context_handle);
Jamie Foxab30e712023-03-30 17:48:36 +0100161 if (dpe_err != DPE_NO_ERROR) {
162 return dpe_err;
163 }
164
165 /* Encode response */
166 QCBOREncode_OpenArray(encode_ctx);
167 QCBOREncode_AddInt64(encode_ctx, DPE_NO_ERROR);
168
169 QCBOREncode_OpenMap(encode_ctx);
170 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_DERIVE_CHILD_NEW_CONTEXT_HANDLE,
171 (UsefulBufC){ &new_child_context_handle,
172 sizeof(new_child_context_handle) });
173 QCBOREncode_AddBytesToMapN(encode_ctx,
174 DPE_DERIVE_CHILD_PARENT_CONTEXT_HANDLE,
175 (UsefulBufC){ &new_parent_context_handle,
176 sizeof(new_parent_context_handle) });
177 QCBOREncode_CloseMap(encode_ctx);
178
179 QCBOREncode_CloseArray(encode_ctx);
180
181 return DPE_NO_ERROR;
182}
183
184static dpe_error_t decode_certify_key(QCBORDecodeContext *decode_ctx,
185 QCBOREncodeContext *encode_ctx)
186{
187 QCBORError qcbor_err;
188 UsefulBufC out;
189 dpe_error_t dpe_err;
190 int context_handle;
191 bool retain_context;
192 const uint8_t *public_key;
193 size_t public_key_size;
194 const uint8_t *label;
195 size_t label_size;
196 uint8_t certificate_chain_buf[DPE_CERTIFICATE_CHAIN_MAX_SIZE];
197 size_t certificate_chain_actual_size;
198 uint8_t derived_public_key_buf[DPE_PUBLIC_KEY_MAX_SIZE];
199 size_t derived_public_key_actual_size;
200 int new_context_handle;
201
202 /* Decode CertifyKey command */
203 QCBORDecode_EnterMap(decode_ctx, NULL);
204
205 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_CERTIFY_KEY_CONTEXT_HANDLE,
206 &out);
207 if (out.len != sizeof(context_handle)) {
208 return DPE_INVALID_COMMAND;
209 }
210 memcpy(&context_handle, out.ptr, out.len);
211
212 QCBORDecode_GetBoolInMapN(decode_ctx, DPE_CERTIFY_KEY_RETAIN_CONTEXT,
213 &retain_context);
214
215 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_CERTIFY_KEY_PUBLIC_KEY,
216 &out);
217 public_key = out.ptr;
218 public_key_size = out.len;
219
220 QCBORDecode_GetByteStringInMapN(decode_ctx, DPE_CERTIFY_KEY_LABEL, &out);
221 label = out.ptr;
222 label_size = out.len;
223
224 QCBORDecode_ExitMap(decode_ctx);
225
226 /* Exit top level array */
227 QCBORDecode_ExitArray(decode_ctx);
228
229 /* Finish and check for errors before using decoded values */
230 qcbor_err = QCBORDecode_Finish(decode_ctx);
231 if (qcbor_err != QCBOR_SUCCESS) {
232 return DPE_INVALID_COMMAND;
233 }
234
235 dpe_err = dpe_certify_key_impl(context_handle, retain_context, public_key,
236 public_key_size, label, label_size,
237 certificate_chain_buf,
238 sizeof(certificate_chain_buf),
239 &certificate_chain_actual_size,
240 derived_public_key_buf,
241 sizeof(derived_public_key_buf),
242 &derived_public_key_actual_size,
243 &new_context_handle);
244 if (dpe_err != DPE_NO_ERROR) {
245 return dpe_err;
246 }
247
248 /* Encode response */
249 QCBOREncode_OpenArray(encode_ctx);
250 QCBOREncode_AddInt64(encode_ctx, DPE_NO_ERROR);
251
252 QCBOREncode_OpenMap(encode_ctx);
253
254 /* The certificate chain is already encoded into a CBOR array by the certify
255 * key implementation. Add it as a byte string so that its decoding can be
256 * skipped and the CBOR returned to the caller.
257 */
258 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_CERTIFY_KEY_CERTIFICATE_CHAIN,
259 (UsefulBufC){ certificate_chain_buf,
260 certificate_chain_actual_size });
261
262 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_CERTIFY_KEY_DERIVED_PUBLIC_KEY,
263 (UsefulBufC){ derived_public_key_buf,
264 derived_public_key_actual_size });
265 QCBOREncode_AddBytesToMapN(encode_ctx, DPE_CERTIFY_KEY_NEW_CONTEXT_HANDLE,
266 (UsefulBufC){ &new_context_handle,
267 sizeof(new_context_handle) });
268
269 QCBOREncode_CloseMap(encode_ctx);
270
271 QCBOREncode_CloseArray(encode_ctx);
272
273 return DPE_NO_ERROR;
274}
275
276static void encode_error_only(QCBOREncodeContext *encode_ctx,
277 dpe_error_t dpe_err)
278{
279 QCBOREncode_OpenArray(encode_ctx);
280 QCBOREncode_AddInt64(encode_ctx, dpe_err);
281 QCBOREncode_CloseArray(encode_ctx);
282}
283
284int32_t dpe_command_decode(int32_t client_id,
285 const char *cmd_input, size_t cmd_input_size,
286 char *cmd_output, size_t *cmd_output_size)
287{
288 dpe_error_t dpe_err;
289 QCBORError qcbor_err;
290 QCBORDecodeContext decode_ctx;
291 QCBOREncodeContext encode_ctx;
292 UsefulBufC out;
293 uint64_t command_id;
294
295 QCBORDecode_Init(&decode_ctx, (UsefulBufC){ cmd_input, cmd_input_size },
296 QCBOR_DECODE_MODE_NORMAL);
297 QCBOREncode_Init(&encode_ctx, (UsefulBuf){ cmd_output, *cmd_output_size });
298
299 /* Enter top level array */
300 QCBORDecode_EnterArray(&decode_ctx, NULL);
301
302 /* Get the command ID */
303 QCBORDecode_GetUInt64(&decode_ctx, &command_id);
304
305 /* Check for errors before interpreting the decoded command ID */
306 qcbor_err = QCBORDecode_GetError(&decode_ctx);
307
308 if (qcbor_err == QCBOR_SUCCESS) {
309 switch (command_id) {
310 case DPE_DERIVE_CHILD:
Maulik Patelad2f3db2023-05-17 15:41:36 +0100311 dpe_err = decode_derive_child(&decode_ctx, &encode_ctx, client_id);
Jamie Foxab30e712023-03-30 17:48:36 +0100312 break;
313 case DPE_CERTIFY_KEY:
314 dpe_err = decode_certify_key(&decode_ctx, &encode_ctx);
315 break;
316 default:
317 dpe_err = DPE_INVALID_COMMAND;
318 break;
319 }
320 } else {
321 dpe_err = DPE_INVALID_COMMAND;
322 }
323
324 /* If an unhandled DPE error was returned, then encode it into a response */
325 if (dpe_err != DPE_NO_ERROR) {
326 encode_error_only(&encode_ctx, dpe_err);
327 }
328
329 qcbor_err = QCBOREncode_Finish(&encode_ctx, &out);
330 if (qcbor_err != QCBOR_SUCCESS) {
331 return -1;
332 }
333
334 *cmd_output_size = out.len;
335
336 return 0;
337}