blob: 0a90df0e963d2dbc1754cc1f6576a64a93ba464d [file] [log] [blame]
Vadim Bendebury627afc22016-06-19 12:13:18 -07001/*
2 * Copyright 2016 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7#include <commonlib/endian.h>
8#include <console/console.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "tpm2_marshaling.h"
13
14static uint16_t tpm_tag; /* Depends on the command type. */
15
16/*
17 * Each unmarshaling function receives a pointer to the buffer pointer and a
18 * pointer to the size of data still in the buffer. The function extracts data
19 * from the buffer and adjusts both buffer pointer and remaining data size.
20 *
21 * Should there be not enough data in the buffer to unmarshal the required
22 * object, the remaining data size is set to -1 to indicate the error. The
23 * remaining data size is expected to be set to zero once the last data item
Vadim Bendeburybc927102016-07-07 10:52:46 -070024 * has been extracted from the receive buffer.
Vadim Bendebury627afc22016-06-19 12:13:18 -070025 */
26static uint16_t unmarshal_u16(void **buffer, int *buffer_space)
27{
28 uint16_t value;
29
Vadim Bendeburybc927102016-07-07 10:52:46 -070030 if (*buffer_space < 0)
31 return 0;
32
Vadim Bendebury627afc22016-06-19 12:13:18 -070033 if (*buffer_space < sizeof(value)) {
34 *buffer_space = -1; /* Indicate a failure. */
35 return 0;
36 }
37
38 value = read_be16(*buffer);
39 *buffer = (void *) ((uintptr_t) (*buffer) + sizeof(value));
40 *buffer_space -= sizeof(value);
41
42 return value;
43}
44
45static uint16_t unmarshal_u32(void **buffer, int *buffer_space)
46{
47 uint32_t value;
48
Vadim Bendeburybc927102016-07-07 10:52:46 -070049 if (*buffer_space < 0)
50 return 0;
51
Vadim Bendebury627afc22016-06-19 12:13:18 -070052 if (*buffer_space < sizeof(value)) {
53 *buffer_space = -1; /* Indicate a failure. */
54 return 0;
55 }
56
57 value = read_be32(*buffer);
58 *buffer = (void *) ((uintptr_t) (*buffer) + sizeof(value));
59 *buffer_space -= sizeof(value);
60
61 return value;
62}
63
64static uint8_t unmarshal_u8(void **buffer, int *buffer_space)
65{
66 uint8_t value;
67
Vadim Bendeburybc927102016-07-07 10:52:46 -070068 if (*buffer_space < 0)
69 return 0;
70
Vadim Bendebury627afc22016-06-19 12:13:18 -070071 if (*buffer_space < sizeof(value)) {
72 *buffer_space = -1; /* Indicate a failure. */
73 return 0;
74 }
75
76 value = ((uint8_t *)(*buffer))[0];
77 *buffer = (void *) ((uintptr_t) (*buffer) + sizeof(value));
78 *buffer_space -= sizeof(value);
79
80 return value;
81}
82
83#define unmarshal_TPM_CAP(a, b) unmarshal_u32(a, b)
84#define unmarshal_TPM_CC(a, b) unmarshal_u32(a, b)
85#define unmarshal_TPM_PT(a, b) unmarshal_u32(a, b)
86#define unmarshal_TPM_HANDLE(a, b) unmarshal_u32(a, b)
87
88/*
89 * Each marshaling function receives a pointer to the buffer to marshal into,
90 * a pointer to the data item to be marshaled, and a pointer to the remaining
91 * room in the buffer.
92 */
93
94 /*
95 * Marshaling an arbitrary blob requires its size in addition to common
96 * parameter set.
97 */
98static void marshal_blob(void **buffer, void *blob,
Vadim Bendeburybc927102016-07-07 10:52:46 -070099 size_t blob_size, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700100{
101 if (*buffer_space < blob_size) {
102 *buffer_space = -1;
103 return;
104 }
105
106 memcpy(*buffer, blob, blob_size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700107 *buffer_space -= blob_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700108 *buffer = (void *)((uintptr_t)(*buffer) + blob_size);
109}
110
Vadim Bendeburybc927102016-07-07 10:52:46 -0700111static void marshal_u8(void **buffer, uint8_t value, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700112{
113 uint8_t *bp = *buffer;
114
115 if (*buffer_space < sizeof(value)) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700116 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700117 return;
118 }
119
120 *bp++ = value;
121 *buffer = bp;
122 *buffer_space -= sizeof(value);
123}
124
Vadim Bendeburybc927102016-07-07 10:52:46 -0700125static void marshal_u16(void **buffer, uint16_t value, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700126{
127 if (*buffer_space < sizeof(value)) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700128 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700129 return;
130 }
131 write_be16(*buffer, value);
132 *buffer = (void *)((uintptr_t)(*buffer) + sizeof(value));
133 *buffer_space -= sizeof(value);
134}
135
Vadim Bendeburybc927102016-07-07 10:52:46 -0700136static void marshal_u32(void **buffer, uint32_t value, size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700137{
138 if (*buffer_space < sizeof(value)) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700139 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700140 return;
141 }
142
143 write_be32(*buffer, value);
144 *buffer = (void *)((uintptr_t)(*buffer) + sizeof(value));
145 *buffer_space -= sizeof(value);
146}
147
148#define marshal_TPM_HANDLE(a, b, c) marshal_u32(a, b, c)
149#define marshal_TPMI_RH_NV_AUTH(a, b, c) marshal_TPM_HANDLE(a, b, c)
150#define marshal_TPMI_RH_NV_INDEX(a, b, c) marshal_TPM_HANDLE(a, b, c)
151#define marshal_TPMI_SH_AUTH_SESSION(a, b, c) marshal_TPM_HANDLE(a, b, c)
152#define marshal_TPMI_ALG_HASH(a, b, c) marshal_u16(a, b, c)
153
154static void marshal_startup(void **buffer,
155 struct tpm2_startup *cmd_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700156 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700157{
158 marshal_u16(buffer, cmd_body->startup_type, buffer_space);
159}
160
161static void marshal_get_capability(void **buffer,
162 struct tpm2_get_capability *cmd_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700163 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700164{
165 marshal_u32(buffer, cmd_body->capability, buffer_space);
166 marshal_u32(buffer, cmd_body->property, buffer_space);
167 marshal_u32(buffer, cmd_body->propertyCount, buffer_space);
168}
169
170static void marshal_TPM2B(void **buffer,
171 TPM2B *data,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700172 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700173{
174 size_t total_size = data->size + sizeof(data->size);
175
176 if (total_size > *buffer_space) {
Vadim Bendeburybc927102016-07-07 10:52:46 -0700177 *buffer_space = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700178 return;
179 }
180 marshal_u16(buffer, data->size, buffer_space);
181 memcpy(*buffer, data->buffer, data->size);
182 *buffer = ((uint8_t *)(*buffer)) + data->size;
183 *buffer_space -= data->size;
184}
185
186static void marshal_TPMA_NV(void **buffer,
187 TPMA_NV *nv,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700188 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700189{
190 marshal_u32(buffer, *((uint32_t *)nv), buffer_space);
191}
192
193static void marshal_TPMS_NV_PUBLIC(void **buffer,
194 TPMS_NV_PUBLIC *nvpub,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700195 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700196{
197 marshal_TPM_HANDLE(buffer, nvpub->nvIndex, buffer_space);
198 marshal_TPMI_ALG_HASH(buffer, nvpub->nameAlg, buffer_space);
199 marshal_TPMA_NV(buffer, &nvpub->attributes, buffer_space);
200 marshal_TPM2B(buffer, &nvpub->authPolicy.b, buffer_space);
201 marshal_u16(buffer, nvpub->dataSize, buffer_space);
202}
203
204static void marshal_session_header(void **buffer,
205 struct tpm2_session_header *session_header,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700206 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700207{
Vadim Bendeburybc927102016-07-07 10:52:46 -0700208 size_t base_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700209 void *size_location = *buffer;
210
211 /* Skip room for the session header size. */
Vadim Bendeburybc927102016-07-07 10:52:46 -0700212 if (*buffer_space < sizeof(uint32_t)) {
213 *buffer_space = 0;
214 return;
215 }
216
Vadim Bendebury627afc22016-06-19 12:13:18 -0700217 *buffer_space -= sizeof(uint32_t);
218 *buffer = (void *)(((uintptr_t) *buffer) + sizeof(uint32_t));
219
220 base_size = *buffer_space;
221
222 marshal_u32(buffer, session_header->session_handle, buffer_space);
223 marshal_u16(buffer, session_header->nonce_size, buffer_space);
224 marshal_blob(buffer, session_header->nonce,
225 session_header->nonce_size, buffer_space);
226 marshal_u8(buffer, session_header->session_attrs, buffer_space);
227 marshal_u16(buffer, session_header->auth_size, buffer_space);
228 marshal_blob(buffer, session_header->auth,
229 session_header->auth_size, buffer_space);
230
Vadim Bendeburybc927102016-07-07 10:52:46 -0700231 if (!*buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700232 return; /* The structure did not fit. */
233
234 /* Paste in the session size. */
235 marshal_u32(&size_location, base_size - *buffer_space, &base_size);
236}
237
238static void marshal_nv_define_space(void **buffer,
239 struct tpm2_nv_define_space_cmd *nvd_in,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700240 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700241{
242 void *size_location;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700243 size_t base_size;
244 size_t sizeof_nv_public_size = sizeof(uint16_t);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700245 struct tpm2_session_header session_header;
246
247 marshal_TPM_HANDLE(buffer, TPM_RH_PLATFORM, buffer_space);
248 memset(&session_header, 0, sizeof(session_header));
249 session_header.session_handle = TPM_RS_PW;
250 marshal_session_header(buffer, &session_header, buffer_space);
251 tpm_tag = TPM_ST_SESSIONS;
252
253 marshal_TPM2B(buffer, &nvd_in->auth.b, buffer_space);
254
255 /* This is where the TPMS_NV_PUBLIC size will be stored. */
256 size_location = *buffer;
257
258 /* Allocate room for the size. */
259 *buffer = ((uint8_t *)(*buffer)) + sizeof(uint16_t);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700260
261 if (*buffer_space < sizeof_nv_public_size) {
262 *buffer_space = 0;
263 return;
264 }
265 *buffer_space -= sizeof_nv_public_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700266 base_size = *buffer_space;
267
268 marshal_TPMS_NV_PUBLIC(buffer, &nvd_in->publicInfo, buffer_space);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700269 if (!*buffer_space)
270 return;
271
Vadim Bendebury627afc22016-06-19 12:13:18 -0700272 base_size = base_size - *buffer_space;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700273 marshal_u16(&size_location, base_size, &sizeof_nv_public_size);
Vadim Bendebury627afc22016-06-19 12:13:18 -0700274}
275
276static void marshal_nv_write(void **buffer,
277 struct tpm2_nv_write_cmd *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700278 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700279{
280 struct tpm2_session_header session_header;
281
282 marshal_TPM_HANDLE(buffer, TPM_RH_PLATFORM, buffer_space);
283 marshal_TPM_HANDLE(buffer, command_body->nvIndex, buffer_space);
284 memset(&session_header, 0, sizeof(session_header));
285 session_header.session_handle = TPM_RS_PW;
286 marshal_session_header(buffer, &session_header, buffer_space);
287 tpm_tag = TPM_ST_SESSIONS;
288
289 marshal_TPM2B(buffer, &command_body->data.b, buffer_space);
290 marshal_u16(buffer, command_body->offset, buffer_space);
291}
292
293static void marshal_nv_read(void **buffer,
294 struct tpm2_nv_read_cmd *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700295 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700296{
297 struct tpm2_session_header session_header;
298
299 marshal_TPM_HANDLE(buffer, TPM_RH_PLATFORM, buffer_space);
300 marshal_TPM_HANDLE(buffer, command_body->nvIndex, buffer_space);
301 memset(&session_header, 0, sizeof(session_header));
302 session_header.session_handle = TPM_RS_PW;
303 marshal_session_header(buffer, &session_header, buffer_space);
304 tpm_tag = TPM_ST_SESSIONS;
305 marshal_u16(buffer, command_body->size, buffer_space);
306 marshal_u16(buffer, command_body->offset, buffer_space);
307}
308
309static void marshal_selftest(void **buffer,
310 struct tpm2_self_test *command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700311 size_t *buffer_space)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700312{
313 marshal_u8(buffer, command_body->yes_no, buffer_space);
314}
315
316int tpm_marshal_command(TPM_CC command, void *tpm_command_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700317 void *buffer, size_t buffer_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700318{
319 void *cmd_body = (uint8_t *)buffer + sizeof(struct tpm_header);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700320 size_t max_body_size = buffer_size - sizeof(struct tpm_header);
321 size_t body_size = max_body_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700322
323 /* Will be modified when marshaling some commands. */
324 tpm_tag = TPM_ST_NO_SESSIONS;
325
326 switch (command) {
327 case TPM2_Startup:
328 marshal_startup(&cmd_body, tpm_command_body, &body_size);
329 break;
330
331 case TPM2_GetCapability:
332 marshal_get_capability(&cmd_body, tpm_command_body,
333 &body_size);
334 break;
335
336 case TPM2_NV_Read:
337 marshal_nv_read(&cmd_body, tpm_command_body, &body_size);
338 break;
339
340 case TPM2_NV_DefineSpace:
341 marshal_nv_define_space(&cmd_body,
342 tpm_command_body, &body_size);
343 break;
344
345 case TPM2_NV_Write:
346 marshal_nv_write(&cmd_body, tpm_command_body, &body_size);
347 break;
348
349 case TPM2_SelfTest:
350 marshal_selftest(&cmd_body, tpm_command_body, &body_size);
351 break;
352
353 default:
Vadim Bendeburybc927102016-07-07 10:52:46 -0700354 body_size = 0;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700355 printk(BIOS_INFO, "%s:%d:Request to marshal unsupported command %#x\n",
356 __FILE__, __LINE__, command);
357 }
358
359 if (body_size > 0) {
360
361 /* See how much room was taken by marshaling. */
362 body_size = max_body_size - body_size;
363
364 body_size += sizeof(struct tpm_header);
365
366 marshal_u16(&buffer, tpm_tag, &max_body_size);
367 marshal_u32(&buffer, body_size, &max_body_size);
368 marshal_u32(&buffer, command, &max_body_size);
369 }
370
371 return body_size;
372}
373
374static void unmarshal_get_capability(void **buffer, int *size,
375 struct get_cap_response *gcr)
376{
377 int i;
378
379 gcr->more_data = unmarshal_u8(buffer, size);
380
381 gcr->cd.capability = unmarshal_TPM_CAP(buffer, size);
382 if (*size < 0)
383 return;
384
385 switch (gcr->cd.capability) {
386 case TPM_CAP_TPM_PROPERTIES:
387 gcr->cd.data.tpmProperties.count =
388 unmarshal_u32(buffer, size);
389 if (*size < 0)
390 return;
391 if (gcr->cd.data.tpmProperties.count > ARRAY_SIZE
392 (gcr->cd.data.tpmProperties.tpmProperty)) {
393 printk(BIOS_INFO, "%s:%s:%d - %d - too many properties\n",
394 __FILE__, __func__, __LINE__,
395 gcr->cd.data.tpmProperties.count);
396 *size = -1;
397 return;
398 }
399 for (i = 0; i < gcr->cd.data.tpmProperties.count; i++) {
400 TPMS_TAGGED_PROPERTY *pp;
401
402 pp = gcr->cd.data.tpmProperties.tpmProperty + i;
403 pp->property = unmarshal_TPM_PT(buffer, size);
404 pp->value = unmarshal_u32(buffer, size);
405 }
406 break;
407 default:
408 printk(BIOS_ERR,
409 "%s:%d - unable to unmarshal capability response",
410 __func__, __LINE__);
411 printk(BIOS_ERR, " for %d\n", gcr->cd.capability);
412 *size = -1;
413 break;
414 }
415}
416
417static void unmarshal_TPM2B_MAX_NV_BUFFER(void **buffer,
418 int *size,
419 TPM2B_MAX_NV_BUFFER *nv_buffer)
420{
421 nv_buffer->t.size = unmarshal_u16(buffer, size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700422 if ((*size < 0) || (nv_buffer->t.size > *size)) {
Vadim Bendebury627afc22016-06-19 12:13:18 -0700423 printk(BIOS_ERR, "%s:%d - "
424 "size mismatch: expected %d, remaining %d\n",
425 __func__, __LINE__, nv_buffer->t.size, *size);
Vadim Bendeburybc927102016-07-07 10:52:46 -0700426 *size = -1;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700427 return;
428 }
429
430 nv_buffer->t.buffer = *buffer;
431
432 *buffer = ((uint8_t *)(*buffer)) + nv_buffer->t.size;
433 *size -= nv_buffer->t.size;
434}
435
436static void unmarshal_nv_read(void **buffer, int *size,
437 struct nv_read_response *nvr)
438{
439 /* Total size of the parameter field. */
440 nvr->params_size = unmarshal_u32(buffer, size);
441 unmarshal_TPM2B_MAX_NV_BUFFER(buffer, size, &nvr->buffer);
442
443 if (nvr->params_size !=
444 (nvr->buffer.t.size + sizeof(nvr->buffer.t.size))) {
445 printk(BIOS_ERR,
446 "%s:%d - parameter/buffer %d/%d size mismatch",
447 __func__, __LINE__, nvr->params_size,
448 nvr->buffer.t.size);
449 return;
450 }
451
452 if (*size < 0)
453 return;
454 /*
455 * Let's ignore the authorisation section. It should be 5 bytes total,
456 * just confirm that this is the case and report any discrepancy.
457 */
458 if (*size != 5)
459 printk(BIOS_ERR,
460 "%s:%d - unexpected authorisation seciton size %d\n",
461 __func__, __LINE__, *size);
462
463 *buffer = ((uint8_t *)(*buffer)) + *size;
464 *size = 0;
465}
466
467struct tpm2_response *tpm_unmarshal_response(TPM_CC command,
468 void *response_body,
Vadim Bendeburybc927102016-07-07 10:52:46 -0700469 size_t in_size)
Vadim Bendebury627afc22016-06-19 12:13:18 -0700470{
471 static struct tpm2_response tpm2_resp;
Vadim Bendeburybc927102016-07-07 10:52:46 -0700472 /*
473 * Should be 0 when done, positive and negaitive values indicate
474 * unmarshaling errors.
475 */
476 int cr_size = in_size;
Vadim Bendebury627afc22016-06-19 12:13:18 -0700477
Vadim Bendeburybc927102016-07-07 10:52:46 -0700478 if ((cr_size < 0) || (in_size < sizeof(struct tpm_header)))
Vadim Bendebury627afc22016-06-19 12:13:18 -0700479 return NULL;
480
481 tpm2_resp.hdr.tpm_tag = unmarshal_u16(&response_body, &cr_size);
482 tpm2_resp.hdr.tpm_size = unmarshal_u32(&response_body, &cr_size);
483 tpm2_resp.hdr.tpm_code = unmarshal_TPM_CC(&response_body, &cr_size);
484
485 if (!cr_size) {
486 if (tpm2_resp.hdr.tpm_size != sizeof(tpm2_resp.hdr))
487 printk(BIOS_ERR,
488 "%s: size mismatch in response to command %#x\n",
489 __func__, command);
490 return &tpm2_resp;
491 }
492
493 switch (command) {
494 case TPM2_Startup:
495 break;
496
497 case TPM2_GetCapability:
498 unmarshal_get_capability(&response_body, &cr_size,
499 &tpm2_resp.gc);
500 break;
501
502 case TPM2_NV_Read:
503 unmarshal_nv_read(&response_body, &cr_size,
504 &tpm2_resp.nvr);
505 break;
506
507 case TPM2_NV_DefineSpace:
508 case TPM2_NV_Write:
509 /* Session data included in response can be safely ignored. */
510 cr_size = 0;
511 break;
512
513 default:
514 {
515 int i;
516
517 printk(BIOS_INFO, "%s:%d:"
518 "Request to unmarshal unexpected command %#x,"
519 " code %#x",
520 __func__, __LINE__, command,
521 tpm2_resp.hdr.tpm_code);
522
523 for (i = 0; i < cr_size; i++) {
524 if (!(i % 16))
525 printk(BIOS_INFO, "\n");
526 printk(BIOS_INFO, "%2.2x ",
527 ((uint8_t *)response_body)[i]);
528 }
529 }
530 printk(BIOS_INFO, "\n");
531 return NULL;
532 }
533
534 if (cr_size) {
535 printk(BIOS_INFO,
536 "%s:%d got %d bytes back in response to %#x,"
537 " failed to parse (%d)\n",
538 __func__, __LINE__, tpm2_resp.hdr.tpm_size,
539 command, cr_size);
540 return NULL;
541 }
542
543 /* The entire message have been parsed. */
544 return &tpm2_resp;
545}