Debian Codegraph @

stubdom/vtpmmgr/vtpm_cmd_handler.c

raw
1 /*
2 * Copyright (c) 2010-2012 United States Government, as represented by
3 * the Secretary of Defense. All rights reserved.
4 *
5 * based off of the original tools/vtpm_manager code base which is:
6 * Copyright (c) 2005, Intel Corp.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * * Neither the name of Intel Corporation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include <inttypes.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <mini-os/console.h>
41 #include <mini-os/lib.h>
42 #include <polarssl/sha1.h>
43 #include <polarssl/sha2.h>
44
45 #include "marshal.h"
46 #include "log.h"
47 #include "vtpm_disk.h"
48 #include "vtpmmgr.h"
49 #include "tpm.h"
50 #include "tpm2.h"
51 #include "tpmrsa.h"
52 #include "tcg.h"
53 #include "mgmt_authority.h"
54 #include "disk_crypto.h"
55
56 static void gen_random_uuid(uuid_t uuid)
57 {
58 do_random(uuid, 16);
59 // make the 128-bit random number a valid UUID (122 bits remain)
60 uuid[6] = 0x40 | (uuid[6] & 0x0F);
61 uuid[8] = 0x80 | (uuid[8] & 0x3F);
62 }
63
64 /*
65 * Instead of using a kernel hash, which requires a trusted domain builder to
66 * report, use the XSM label as a substitute.
67 */
68 static TPM_RESULT find_vtpm_khash(int domid, struct tpm_opaque *opq)
69 {
70 char buf[128];
71 int i, rv;
72 buf[127] = 0;
73 rv = tpmback_get_peercontext(opq->domid, opq->handle, buf, sizeof(buf) - 1);
74 if (rv < 0)
75 return TPM_FAIL;
76
77 sha1((void*)buf, strlen(buf), opq->kern_hash);
78
79 /*
80 * As a hack to support the use of the XSM user field as an optional
81 * wildcard, check the hash against the group here. If it fails, replace
82 * the user field with a "*" and return the hash of that value.
83 */
84 for(i=0; i < be32_native(opq->group->seal_bits.nr_kerns); i++) {
85 if (!memcmp(opq->group->seal_bits.kernels[i].bits, opq->kern_hash, 20)) {
86 return TPM_SUCCESS;
87 }
88 }
89
90 char* upos = strchr(buf, ':');
91 if (upos == NULL || upos == buf)
92 return TPM_SUCCESS;
93
94 upos--;
95 upos[0] = '*';
96
97 sha1((void*)upos, strlen(upos), opq->kern_hash);
98 return TPM_SUCCESS;
99 }
100
101 static TPM_RESULT find_vtpm_verified(int domid, struct tpm_opaque *opq)
102 {
103 TPM_RESULT rc;
104 int i;
105 if (opq->vtpm)
106 return TPM_SUCCESS;
107
108 rc = find_vtpm(&opq->group, &opq->vtpm, *opq->uuid);
109 if (rc)
110 return TPM_BAD_PARAMETER;
111
112 if (opq->vtpm->flags & VTPM_FLAG_OPEN) {
113 printk("Attempted to open vTPM twice!\n");
114 opq->vtpm = NULL;
115 opq->group = NULL;
116 return TPM_DISABLED;
117 }
118
119 rc = find_vtpm_khash(domid, opq);
120 if (rc)
121 return rc;
122
123 for(i=0; i < be32_native(opq->group->seal_bits.nr_kerns); i++) {
124 if (!memcmp(opq->group->seal_bits.kernels[i].bits, opq->kern_hash, 20)) {
125 opq->vtpm->flags |= VTPM_FLAG_OPEN;
126 return TPM_SUCCESS;
127 }
128 }
129 printk("Unauthorized vTPM kernel image used!\n");
130 return TPM_DISABLED;
131 }
132
133 static TPM_RESULT vtpmmgr_SaveHashKey(struct tpm_opaque *opq, tpmcmd_t* tpmcmd)
134 {
135 TPM_RESULT status = TPM_SUCCESS;
136 int rc = 0;
137
138 size_t bufsize = tpmcmd->req_len - VTPM_COMMAND_HEADER_SIZE;
139 const void *buf = tpmcmd->req + VTPM_COMMAND_HEADER_SIZE;
140
141 if (bufsize < 52) {
142 vtpmlogerror(VTPM_LOG_VTPM, "VTPM_ORD_SAVEHASHKEY hashkey too short!\n");
143 status = TPM_BAD_PARAMETER;
144 goto abort_egress;
145 }
146 if (bufsize > 64) {
147 vtpmlogerror(VTPM_LOG_VTPM, "VTPM_ORD_SAVEHASHKEY hashkey too long!\n");
148 status = TPM_BAD_PARAMETER;
149 goto abort_egress;
150 }
151
152 vtpmloginfo(VTPM_LOG_VTPM, "vtpmmgr_SaveHashKey\n");
153 status = find_vtpm_verified(tpmcmd->domid, opq);
154
155 // auto-create vTPMs in group0 when saving a new UUID
156 // TODO restrict to certain UUIDs (such as all-zero)
157 // this is not done yet to simplify use of the TPM Manager
158 if (status == TPM_BAD_PARAMETER) {
159 opq->group = g_mgr->groups[0].v;
160 rc = create_vtpm(opq->group, &opq->vtpm, *opq->uuid);
161 if (rc) {
162 status = TPM_BAD_PARAMETER;
163 goto abort_egress;
164 }
165 if (opq->group->nr_vtpms == 1)
166 opq->vtpm->flags = VTPM_FLAG_ADMIN;
167 printk("SaveHashKey with unknown UUID="UUID_FMT" - creating in auth0 (f=%d)\n",
168 UUID_BYTES((*opq->uuid)), opq->vtpm->flags);
169 status = TPM_SUCCESS;
170 }
171 if (status)
172 goto abort_egress;
173
174 memcpy(opq->vtpm->data, buf, bufsize);
175 memset(opq->vtpm->data + bufsize, 0, 64 - bufsize);
176
177 vtpm_sync(opq->group, opq->vtpm);
178
179 abort_egress:
180 pack_TPM_RSP_HEADER(tpmcmd->resp, VTPM_TAG_RSP, VTPM_COMMAND_HEADER_SIZE, status);
181 tpmcmd->resp_len = VTPM_COMMAND_HEADER_SIZE;
182
183 return status;
184 }
185
186 static TPM_RESULT vtpmmgr_LoadHashKey(struct tpm_opaque *opq, tpmcmd_t* tpmcmd)
187 {
188 TPM_RESULT status = TPM_SUCCESS;
189 int i;
190 uint8_t *buf = tpmcmd->resp + VTPM_COMMAND_HEADER_SIZE;
191
192 vtpmloginfo(VTPM_LOG_VTPM, "vtpmmgr_LoadHashKey\n");
193 tpmcmd->resp_len = VTPM_COMMAND_HEADER_SIZE;
194
195 status = find_vtpm_verified(tpmcmd->domid, opq);
196 if (status)
197 goto abort_egress;
198
199 memcpy(buf, opq->vtpm->data, 64);
200
201 for(i=52; i < 64; i++) {
202 if (buf[i]) {
203 tpmcmd->resp_len += 64;
204 goto abort_egress;
205 }
206 }
207 tpmcmd->resp_len += 52;
208
209 abort_egress:
210 pack_TPM_RSP_HEADER(tpmcmd->resp, VTPM_TAG_RSP, tpmcmd->resp_len, status);
211
212 return status;
213 }
214
215 #define CMD_BEGIN \
216 TPM_RESULT status = TPM_SUCCESS; \
217 uint32_t in_pos = VTPM_COMMAND_HEADER_SIZE; \
218 tpmcmd->resp_len = VTPM_COMMAND_HEADER_SIZE; \
219 vtpmloginfo(VTPM_LOG_TPM, "%s\n", __func__);
220
221 #define CMD_END \
222 abort_egress: \
223 if (status) \
224 tpmcmd->resp_len = VTPM_COMMAND_HEADER_SIZE; \
225 pack_TPM_RSP_HEADER(tpmcmd->resp, VTPM_TAG_RSP, tpmcmd->resp_len, status); \
226 return status
227
228 #define UNPACK_IN(type, item...) do { \
229 status = unpack3_ ## type (tpmcmd->req, &in_pos, tpmcmd->req_len, item); \
230 if (status) { \
231 status = TPM_BAD_PARAMETER; \
232 goto abort_egress; \
233 } \
234 } while (0)
235
236 #define UNPACK_GROUP(group) do { \
237 uint32_t group_idx; \
238 UNPACK_IN(UINT32, &group_idx); \
239 if (group_idx >= g_mgr->nr_groups) { \
240 status = TPM_BADINDEX; \
241 goto abort_egress; \
242 } \
243 group = g_mgr->groups[group_idx].v; \
244 if (!group) { \
245 status = TPM_AUTHFAIL; \
246 goto abort_egress; \
247 } \
248 } while (0)
249
250 #define UNPACK_DONE() do { \
251 if (in_pos != tpmcmd->req_len) { \
252 status = TPM_BAD_PARAMETER; \
253 goto abort_egress; \
254 } \
255 } while (0)
256
257 #define PACK_OUT(type, item...) do { \
258 UINT32 isize = sizeof_ ## type(item); \
259 if (isize + tpmcmd->resp_len > TCPA_MAX_BUFFER_LENGTH) { \
260 status = TPM_SIZE; \
261 goto abort_egress; \
262 } \
263 pack_ ## type (tpmcmd->resp + tpmcmd->resp_len, item); \
264 tpmcmd->resp_len += isize; \
265 } while (0)
266
267 #define PACK_BUF ((void*)(tpmcmd->resp + tpmcmd->resp_len))
268
269 static TPM_RESULT vtpmmgr_GetBootHash(struct tpm_opaque *opq, tpmcmd_t* tpmcmd)
270 {
271 CMD_BEGIN;
272 UNPACK_DONE();
273
274 PACK_OUT(BUFFER, opq->kern_hash, 20);
275
276 CMD_END;
277 }
278
279 static TPM_RESULT vtpmmgr_GetQuote(struct tpm_opaque *opq, tpmcmd_t* tpmcmd)
280 {
281 CMD_BEGIN;
282 int i;
283 void *ibuf;
284 uint32_t pcr_size;
285 TPM_PCR_SELECTION sel;
286 uint32_t extra_info_flags;
287
288 UNPACK_IN(VPTR, &ibuf, 20, UNPACK_ALIAS);
289 UNPACK_IN(TPM_PCR_SELECTION, &sel, UNPACK_ALIAS);
290 UNPACK_IN(TPM_DEEP_QUOTE_INFO, &extra_info_flags);
291 UNPACK_DONE();
292
293 if (!opq->vtpm) {
294 status = TPM_BAD_PARAMETER;
295 goto abort_egress;
296 }
297
298 printk("ibuf: ");
299 for (i=0; i < 20; i++)
300 printk("%02x", ((uint8_t*)ibuf)[i]);
301 printk("\n");
302
303 status = vtpm_do_quote(opq->group, *opq->uuid, opq->kern_hash, ibuf, &sel, extra_info_flags, PACK_BUF + 256, &pcr_size, PACK_BUF);
304 if (status)
305 goto abort_egress;
306 tpmcmd->resp_len += 256 + pcr_size;
307
308 CMD_END;
309 }
310
311 static TPM_RESULT vtpmmgr_GroupList(tpmcmd_t* tpmcmd)
312 {
313 CMD_BEGIN;
314 UNPACK_DONE();
315 PACK_OUT(UINT32, g_mgr->nr_groups);
316 CMD_END;
317 }
318
319 static TPM_RESULT vtpmmgr_GroupNew(tpmcmd_t* tpmcmd)
320 {
321 void *privCADigest;
322 BYTE *pubkey;
323 struct mem_group *group;
324 uint32_t group_idx;
325 CMD_BEGIN;
326
327 UNPACK_IN(VPTR, &privCADigest, 20, UNPACK_ALIAS);
328 UNPACK_IN(PTR, &pubkey, 256, UNPACK_ALIAS);
329 UNPACK_DONE();
330
331 group = vtpm_new_group(privCADigest);
332 if (!group) {
333 status = TPM_FAIL;
334 goto abort_egress;
335 }
336
337 memcpy(group->id_data.saa_pubkey, pubkey, 256);
338
339 PACK_OUT(BUFFER, group->id_data.uuid, 16);
340 PACK_OUT(BUFFER, group->id_data.tpm_aik_public, 256);
341 PACK_OUT(BUFFER, group->details.recovery_data, 256);
342
343 memset(group->details.recovery_data, 0, 256);
344
345 group->details.sequence = native_be64(g_mgr->sequence);
346
347 if (group != g_mgr->groups[0].v) {
348 group_idx = g_mgr->nr_groups;
349 g_mgr->nr_groups++;
350 g_mgr->groups = realloc(g_mgr->groups, g_mgr->nr_groups*sizeof(struct mem_group_hdr));
351 memset(&g_mgr->groups[group_idx], 0, sizeof(g_mgr->groups[0]));
352 g_mgr->groups[group_idx].v = group;
353 }
354
355 vtpm_sync_group(group, SEQ_UPDATE);
356 CMD_END;
357 }
358
359 static TPM_RESULT vtpmmgr_GroupDel(tpmcmd_t* tpmcmd)
360 {
361 CMD_BEGIN;
362 struct mem_group *group;
363 uint32_t group_idx, nr_mov;
364
365 UNPACK_IN(UINT32, &group_idx);
366 UNPACK_DONE();
367
368 if (group_idx > g_mgr->nr_groups) {
369 status = TPM_BADINDEX;
370 goto abort_egress;
371 }
372 group = g_mgr->groups[group_idx].v;
373
374 if (group) {
375 int i, j;
376 for (i = 0; i < group->nr_pages; i++) {
377 for (j = 0; j < group->data[i].size; j++) {
378 if (group->data[i].vtpms[j]->flags & VTPM_FLAG_OPEN) {
379 status = TPM_FAIL;
380 goto abort_egress;
381 }
382 }
383 }
384
385 for (i = 0; i < group->nr_pages; i++) {
386 for (j = 0; j < group->data[i].size; j++) {
387 free(group->data[i].vtpms[j]);
388 }
389 }
390 free(group->data);
391 free(group->seals);
392 free(group);
393 }
394
395 g_mgr->nr_groups--;
396 nr_mov = g_mgr->nr_groups - group_idx;
397 memmove(&g_mgr->groups[group_idx], &g_mgr->groups[group_idx + 1], nr_mov * sizeof(g_mgr->groups[0]));
398
399 vtpm_sync_disk(g_mgr, CTR_UPDATE);
400
401 CMD_END;
402 }
403
404 static int pack_cfg_list(void* buf, struct mem_group *group)
405 {
406 int i;
407 void *bstart = buf;
408 memcpy(buf, &group->details.cfg_seq, 8); buf += 8;
409 buf = pack_UINT32(buf, group->nr_seals);
410 for(i=0; i < group->nr_seals; i++) {
411 memcpy(buf, &group->seals[i].digest_release, 20);
412 buf += 20;
413 }
414 memcpy(buf, &group->seal_bits.nr_kerns, 4); buf += 4;
415 memcpy(buf, &group->seal_bits.kernels, 20 * be32_native(group->seal_bits.nr_kerns));
416 return buf - bstart + 20 * be32_native(group->seal_bits.nr_kerns);
417 }
418
419 static TPM_RESULT vtpmmgr_GroupShow(tpmcmd_t* tpmcmd)
420 {
421 CMD_BEGIN;
422 struct mem_group *group;
423
424 UNPACK_GROUP(group);
425 UNPACK_DONE();
426
427 // TODO show is read-only access, need to hit disk if group is NULL
428
429 PACK_OUT(BUFFER, group->id_data.uuid, 16);
430 PACK_OUT(BUFFER, group->id_data.saa_pubkey, 256);
431 tpmcmd->resp_len += pack_cfg_list(PACK_BUF, group);
432
433 CMD_END;
434 }
435
436 static TPM_RESULT vtpmmgr_GroupActivate(tpmcmd_t* tpmcmd)
437 {
438 CMD_BEGIN;
439 struct mem_group *group;
440 uint32_t blobSize;
441 void *blob;
442
443 UNPACK_GROUP(group);
444 UNPACK_IN(UINT32, &blobSize);
445 UNPACK_IN(VPTR, &blob, blobSize, UNPACK_ALIAS);
446 UNPACK_DONE();
447
448 status = group_do_activate(group, blob, blobSize, tpmcmd->resp, &tpmcmd->resp_len);
449
450 CMD_END;
451 }
452
453 /* 2048-bit MODP Group from RFC3526:
454 * 2^2048 - 2^1984 - 1 + 2^64 * { [2^1918 pi] + 124476 }
455 */
456 static uint8_t P_bytes[256] = {
457 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
458 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
459 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
460 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
461 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
462 0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
463 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
464 0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
465 0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11,
466 0x7C, 0x4B, 0x1F, 0xE6, 0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B, 0x3D,
467 0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF, 0x05, 0x98, 0xDA, 0x48, 0x36,
468 0x1C, 0x55, 0xD3, 0x9A, 0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF, 0x5F,
469 0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD, 0x96, 0x1C, 0x62, 0xF3, 0x56,
470 0x20, 0x85, 0x52, 0xBB, 0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D,
471 0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04, 0xF1, 0x74, 0x6C, 0x08,
472 0xCA, 0x18, 0x21, 0x7C, 0x32, 0x90, 0x5E, 0x46, 0x2E, 0x36, 0xCE, 0x3B,
473 0xE3, 0x9E, 0x77, 0x2C, 0x18, 0x0E, 0x86, 0x03, 0x9B, 0x27, 0x83, 0xA2,
474 0xEC, 0x07, 0xA2, 0x8F, 0xB5, 0xC5, 0x5D, 0xF0, 0x6F, 0x4C, 0x52, 0xC9,
475 0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17, 0x18, 0x39, 0x95, 0x49, 0x7C,
476 0xEA, 0x95, 0x6A, 0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10,
477 0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAC, 0xAA, 0x68, 0xFF, 0xFF, 0xFF, 0xFF,
478 0xFF, 0xFF, 0xFF, 0xFF,
479 };
480 static t_uint Gp[] = { 2 };
481
482 static void tm_dhkx_gen(void* dhkx1, void* dhkx2, void* out)
483 {
484 mpi GX = { 0 }, GY = { 0 }, K = { 0 }, RP = { 0 };
485 mpi P = { 0 };
486
487 t_uint Xp[sizeof(P_bytes) / sizeof(t_uint)];
488 mpi X = {
489 .s = 1,
490 .n = ARRAY_SIZE(Xp),
491 .p = Xp
492 };
493 mpi G = {
494 .s = 1,
495 .n = 1,
496 .p = Gp,
497 };
498 mpi_read_binary(&P, P_bytes, sizeof(P_bytes));
499
500 do_random(Xp, sizeof(Xp));
501
502 mpi_exp_mod(&GX, &G, &X, &P, &RP);
503 mpi_write_binary(&GX, dhkx2, 256);
504 mpi_free(&GX);
505
506 mpi_read_binary(&GY, dhkx1, 256);
507 mpi_exp_mod(&K, &GY, &X, &P, &RP);
508 mpi_free(&P);
509 mpi_free(&RP);
510 mpi_free(&GY);
511
512 mpi_write_binary(&K, (void*)Xp, 256);
513 mpi_free(&K);
514 sha2((void*)Xp, 256, out, 0);
515 }
516
517 static void xor2_256b(void *xv, const void* yv)
518 {
519 int i;
520 uint64_t *x = xv;
521 const uint64_t *y = yv;
522 for(i=0; i < 4; i++)
523 x[i] ^= y[i];
524 }
525
526 static TPM_RESULT vtpmmgr_GroupRegister(tpmcmd_t* tpmcmd)
527 {
528 CMD_BEGIN;
529 struct mem_group *group = NULL;
530 tpmrsa_context saa_rsa = TPMRSA_CTX_INIT;
531 struct tpm_authdata digest;
532 sha1_context ctx;
533 TPM_PCR_SELECTION sel;
534 void *dhkx1, *dhkx2, *gk, *sig;
535 uint32_t extra_info_flags = 0;
536
537 UNPACK_GROUP(group);
538 UNPACK_IN(VPTR, &dhkx1, 256, UNPACK_ALIAS);
539 UNPACK_IN(VPTR, &sig, 256, UNPACK_ALIAS);
540 UNPACK_IN(TPM_PCR_SELECTION, &sel, UNPACK_ALIAS);
541 UNPACK_DONE();
542
543 /* Only generating this quote during the same boot that this group was
544 * created in allows the quote to prove that the group key has never
545 * been available outside a configuration approved by its SAA.
546 */
547 if (!(group->flags & MEM_GROUP_FLAG_FIRSTBOOT)) {
548 status = TPM_FAIL;
549 goto abort_egress;
550 }
551
552 sha1(dhkx1, 256, digest.bits);
553 tpmrsa_set_pubkey(&saa_rsa, group->id_data.saa_pubkey, 256, 0, 0);
554 if (tpmrsa_sigcheck(&saa_rsa, sig, digest.bits))
555 status = TPM_FAIL;
556 tpmrsa_free(&saa_rsa);
557 if (status)
558 goto abort_egress;
559
560 dhkx2 = PACK_BUF;
561 tpmcmd->resp_len += 256;
562 gk = PACK_BUF;
563 tpmcmd->resp_len += 32;
564
565 tm_dhkx_gen(dhkx1, dhkx2, gk);
566 xor2_256b(gk, &group->group_key);
567
568 sha1_starts(&ctx);
569 sha1_update(&ctx, (void*)"REGR", 4);
570 sha1_update(&ctx, dhkx1, 256);
571 sha1_update(&ctx, dhkx2, 256 + 32);
572 sha1_finish(&ctx, digest.bits);
573
574 status = vtpm_do_quote(group, NULL, NULL, &digest, &sel, extra_info_flags,NULL, NULL, PACK_BUF);
575 tpmcmd->resp_len += 256;
576
577 CMD_END;
578 }
579
580 static TPM_RESULT vtpmmgr_GroupUpdate(tpmcmd_t* tpmcmd)
581 {
582 CMD_BEGIN;
583 struct mem_group *group;
584 int i;
585 int hstart;
586 uint32_t nr_kerns, nr_seals;
587 uint64_t old_seq, new_seq;
588 struct mem_seal *seals = NULL;
589 tpmrsa_context saa_rsa = TPMRSA_CTX_INIT;
590 unsigned char digest[20];
591 TPM_RESULT rc;
592 void *sig, *seal_bits, *kern_bits;
593
594 UNPACK_GROUP(group);
595 UNPACK_IN(VPTR, &sig, 256, UNPACK_ALIAS);
596
597 // Hash starts here
598 hstart = in_pos;
599
600 new_seq = be64_native(*(be64_t*)(tpmcmd->req + in_pos));
601 old_seq = be64_native(group->details.cfg_seq);
602 in_pos += 8;
603 if (old_seq > new_seq) {
604 status = TPM_FAIL;
605 goto abort_egress;
606 }
607
608 UNPACK_IN(UINT32, &nr_seals);
609 UNPACK_IN(VPTR, &seal_bits, nr_seals * 20, UNPACK_ALIAS);
610
611 UNPACK_IN(UINT32, &nr_kerns);
612 UNPACK_IN(VPTR, &kern_bits, nr_kerns * 20, UNPACK_ALIAS);
613
614 // TODO handle saving larger lists on disk
615 if (nr_seals > NR_SEALS_PER_GROUP) {
616 status = TPM_SIZE;
617 goto abort_egress;
618 }
619
620 if (nr_kerns > NR_KERNS_PER_GROUP) {
621 status = TPM_SIZE;
622 goto abort_egress;
623 }
624
625 sha1(tpmcmd->req + hstart, in_pos - hstart, digest);
626
627 seals = calloc(nr_seals, sizeof(seals[0]));
628
629 for(i=0; i < nr_seals; i++) {
630 TPM_PCR_SELECTION sel;
631 UNPACK_IN(TPM_PCR_SELECTION, &sel, UNPACK_ALIAS);
632 memcpy(&seals[i].digest_release, seal_bits, 20);
633 seal_bits += 20;
634 if (sel.sizeOfSelect > 4) {
635 status = TPM_BAD_PARAMETER;
636 goto abort_egress;
637 }
638 seals[i].pcr_selection = native_le32(0);
639 memcpy(&seals[i].pcr_selection, sel.pcrSelect, sel.sizeOfSelect);
640 }
641
642 UNPACK_DONE();
643
644 tpmrsa_set_pubkey(&saa_rsa, group->id_data.saa_pubkey, 256, 0, 0);
645 rc = tpmrsa_sigcheck(&saa_rsa, sig, digest);
646 tpmrsa_free(&saa_rsa);
647 if (rc) {
648 printk("sigcheck failed: %d\n", rc);
649 status = rc;
650 goto abort_egress;
651 }
652
653 // Commit
654 free(group->seals);
655
656 memcpy(&group->seal_bits.kernels, kern_bits, 20 * nr_kerns);
657 group->details.cfg_seq = native_be64(new_seq);
658 group->nr_seals = nr_seals;
659 group->seals = seals;
660 group->seal_bits.nr_kerns = native_be32(nr_kerns);
661
662 seals = NULL;
663
664 group->flags &= ~MEM_GROUP_FLAG_SEAL_VALID;
665 if (group == g_mgr->groups[0].v)
666 g_mgr->root_seals_valid = 0;
667
668 // TODO use GROUP_KEY_UPDATE or MGR_KEY_UPDATE here?
669 // only required if this update was to address a potential key leak
670 vtpm_sync_group(group, SEQ_UPDATE);
671
672 abort_egress:
673 free(seals);
674
675 pack_TPM_RSP_HEADER(tpmcmd->resp, VTPM_TAG_RSP, tpmcmd->resp_len, status);
676 return status;
677 }
678
679 static TPM_RESULT vtpmmgr_VtpmList(tpmcmd_t* tpmcmd)
680 {
681 CMD_BEGIN;
682 struct mem_group *group;
683 uint32_t vtpm_offset;
684 int i, j;
685
686 UNPACK_GROUP(group);
687 UNPACK_IN(UINT32, &vtpm_offset);
688
689 PACK_OUT(UINT32, group->nr_vtpms);
690 if (vtpm_offset > group->nr_vtpms)
691 goto egress;
692
693 for(i=0; i < group->nr_pages; i++) {
694 struct mem_vtpm_page *pg = &group->data[i];
695 for(j=0; j < pg->size; j++) {
696 if (vtpm_offset) {
697 // TODO a proper seek would be far faster
698 vtpm_offset--;
699 continue;
700 }
701 memcpy(PACK_BUF, pg->vtpms[j]->uuid, 16);
702 tpmcmd->resp_len += 16;
703 if (tpmcmd->resp_len + 16 > TCPA_MAX_BUFFER_LENGTH)
704 goto egress;
705 }
706 }
707
708 egress:
709 CMD_END;
710 }
711
712 static TPM_RESULT vtpmmgr_VtpmNew(tpmcmd_t* tpmcmd)
713 {
714 CMD_BEGIN;
715 struct mem_group *group;
716 struct mem_vtpm *vtpm;
717 uuid_t newuuid;
718 int rc;
719
720 UNPACK_GROUP(group);
721
722 // XXX allow non-random UUIDs for testing
723 if (tpmcmd->req_len == 14 + 16)
724 UNPACK_IN(BUFFER, newuuid, 16);
725 else
726 gen_random_uuid(newuuid);
727 UNPACK_DONE();
728
729 rc = create_vtpm(group, &vtpm, newuuid);
730 if (rc) {
731 status = TPM_FAIL;
732 goto abort_egress;
733 }
734 memset(vtpm->data, 0, 64);
735 vtpm_sync(group, vtpm);
736
737 PACK_OUT(BUFFER, newuuid, 16);
738 CMD_END;
739 }
740
741 static TPM_RESULT vtpmmgr_VtpmDel(tpmcmd_t* tpmcmd)
742 {
743 CMD_BEGIN;
744 uuid_t uuid;
745 struct mem_group *group;
746 struct mem_vtpm *vtpm;
747 int rc;
748
749 UNPACK_IN(BUFFER, uuid, 16);
750 UNPACK_DONE();
751 rc = find_vtpm(&group, &vtpm, uuid);
752 if (rc) {
753 status = TPM_FAIL;
754 goto abort_egress;
755 }
756
757 if (vtpm->flags & VTPM_FLAG_OPEN) {
758 status = TPM_FAIL;
759 goto abort_egress;
760 }
761
762 delete_vtpm(group, vtpm);
763
764 CMD_END;
765 }
766
767 static int vtpmmgr_permcheck(struct tpm_opaque *opq)
768 {
769 if (!opq->vtpm)
770 return 1;
771 if (opq->vtpm->flags & VTPM_FLAG_ADMIN)
772 return 0;
773 return 1;
774 }
775
776 TPM_RESULT vtpmmgr_handle_getrandom(struct tpm_opaque *opaque,
777 tpmcmd_t* tpmcmd)
778 {
779 TPM_RESULT status = TPM_SUCCESS;
780 TPM_TAG tag;
781 UINT32 size;
782 const int max_rand_size = TCPA_MAX_BUFFER_LENGTH -
783 sizeof_TPM_RQU_GetRandom(tpmcmd->req);
784 UINT32 rand_offset;
785 UINT32 rand_size;
786 TPM_COMMAND_CODE ord;
787 BYTE *p;
788
789 if (tpmcmd->req_len != sizeof_TPM_RQU_GetRandom(tpmcmd->req)) {
790 status = TPM_BAD_PARAMETER;
791 tag = TPM_TAG_RQU_COMMAND;
792 goto abort_egress;
793 }
794
795 p = unpack_TPM_RQU_HEADER(tpmcmd->req, &tag, &size, &ord);
796
797 if (!hw_is_tpm2()) {
798 size = TCPA_MAX_BUFFER_LENGTH;
799 TPMTRYRETURN(TPM_TransmitData(tpmcmd->req, tpmcmd->req_len,
800 tpmcmd->resp, &size));
801 tpmcmd->resp_len = size;
802
803 return TPM_SUCCESS;
804 }
805
806 /* TPM_GetRandom req: <header><uint32 num bytes> */
807 unpack_UINT32(p, &rand_size);
808
809 /* Returning fewer bytes is acceptable per the spec. */
810 if (rand_size > max_rand_size)
811 rand_size = max_rand_size;
812
813 /* Call TPM2_GetRandom but return a TPM_GetRandom response. */
814 /* TPM_GetRandom resp: <header><uint32 num bytes><num random bytes> */
815 rand_offset = sizeof_TPM_RSP_HEADER(tpmcmd->resp) +
816 sizeof_UINT32(tpmcmd->resp);
817
818 TPMTRYRETURN(TPM2_GetRandom(&rand_size, tpmcmd->resp + rand_offset));
819
820 p = pack_TPM_RSP_HEADER(tpmcmd->resp, TPM_TAG_RSP_COMMAND,
821 rand_offset + rand_size, status);
822 p = pack_UINT32(p, rand_size);
823 tpmcmd->resp_len = rand_offset + rand_size;
824
825 return status;
826
827 abort_egress:
828 tpmcmd->resp_len = VTPM_COMMAND_HEADER_SIZE;
829 pack_TPM_RSP_HEADER(tpmcmd->resp, tag + 3, tpmcmd->resp_len, status);
830
831 return status;
832 }
833
834 TPM_RESULT vtpmmgr_handle_cmd(
835 struct tpm_opaque *opaque,
836 tpmcmd_t* tpmcmd)
837 {
838 TPM_RESULT status = TPM_SUCCESS;
839 TPM_TAG tag;
840 UINT32 size;
841 TPM_COMMAND_CODE ord;
842
843 if (tpmcmd->req_len < sizeof_TPM_RQU_HEADER(tpmcmd->req)) {
844 status = TPM_BAD_PARAMETER;
845 tag = TPM_TAG_RQU_COMMAND;
846 goto abort_egress;
847 }
848
849 unpack_TPM_RQU_HEADER(tpmcmd->req,
850 &tag, &size, &ord);
851
852 /* Handle the command now */
853 switch(tag) {
854 case VTPM_TAG_REQ:
855 // This is a vTPM command
856 switch(ord) {
857 case VTPM_ORD_SAVEHASHKEY:
858 return vtpmmgr_SaveHashKey(opaque, tpmcmd);
859 case VTPM_ORD_LOADHASHKEY:
860 return vtpmmgr_LoadHashKey(opaque, tpmcmd);
861 case VTPM_ORD_GET_BOOT_HASH:
862 return vtpmmgr_GetBootHash(opaque, tpmcmd);
863 case VTPM_ORD_GET_QUOTE:
864 return vtpmmgr_GetQuote(opaque, tpmcmd);
865 default:
866 vtpmlogerror(VTPM_LOG_VTPM, "Invalid vTPM Ordinal %" PRIu32 "\n", ord);
867 status = TPM_BAD_ORDINAL;
868 }
869 break;
870 case VTPM_TAG_REQ2:
871 // This is a management command
872 if (vtpmmgr_permcheck(opaque)) {
873 status = TPM_AUTHFAIL;
874 vtpmlogerror(VTPM_LOG_VTPM, "Rejected attempt to use management command from client\n");
875 break;
876 }
877 switch (ord) {
878 case VTPM_ORD_GROUP_LIST:
879 return vtpmmgr_GroupList(tpmcmd);
880 case VTPM_ORD_GROUP_NEW:
881 return vtpmmgr_GroupNew(tpmcmd);
882 case VTPM_ORD_GROUP_DEL:
883 return vtpmmgr_GroupDel(tpmcmd);
884 case VTPM_ORD_GROUP_ACTIVATE:
885 return vtpmmgr_GroupActivate(tpmcmd);
886 case VTPM_ORD_GROUP_REGISTER:
887 return vtpmmgr_GroupRegister(tpmcmd);
888 case VTPM_ORD_GROUP_UPDATE:
889 return vtpmmgr_GroupUpdate(tpmcmd);
890 case VTPM_ORD_GROUP_SHOW:
891 return vtpmmgr_GroupShow(tpmcmd);
892 case VTPM_ORD_VTPM_LIST:
893 return vtpmmgr_VtpmList(tpmcmd);
894 case VTPM_ORD_VTPM_NEW:
895 return vtpmmgr_VtpmNew(tpmcmd);
896 case VTPM_ORD_VTPM_DEL:
897 return vtpmmgr_VtpmDel(tpmcmd);
898 default:
899 vtpmlogerror(VTPM_LOG_VTPM, "Invalid TM Ordinal %" PRIu32 "\n", ord);
900 status = TPM_BAD_ORDINAL;
901 }
902 break;
903 case TPM_TAG_RQU_COMMAND:
904 case TPM_TAG_RQU_AUTH1_COMMAND:
905 case TPM_TAG_RQU_AUTH2_COMMAND:
906 //This is a TPM passthrough command
907 switch(ord) {
908 case TPM_ORD_GetRandom:
909 vtpmloginfo(VTPM_LOG_VTPM, "Passthrough: TPM_GetRandom\n");
910 return vtpmmgr_handle_getrandom(opaque, tpmcmd);
911 case TPM_ORD_PcrRead:
912 vtpmloginfo(VTPM_LOG_VTPM, "Passthrough: TPM_PcrRead\n");
913 // Quotes also need to be restricted to hide PCR values
914 break;
915 case TPM_ORD_Extend:
916 // TODO allow to certain clients? A malicious client
917 // could scramble PCRs and make future quotes invalid.
918 if (vtpmmgr_permcheck(opaque)) {
919 vtpmlogerror(VTPM_LOG_VTPM, "Disallowed TPM_Extend\n");
920 status = TPM_DISABLED_CMD;
921 goto abort_egress;
922 } else {
923 vtpmloginfo(VTPM_LOG_VTPM, "Passthrough: TPM_Extend\n");
924 }
925 break;
926 default:
927 vtpmlogerror(VTPM_LOG_VTPM, "TPM Disallowed Passthrough ord=%" PRIu32 "\n", ord);
928 status = TPM_DISABLED_CMD;
929 goto abort_egress;
930 }
931
932 size = TCPA_MAX_BUFFER_LENGTH;
933 TPMTRYRETURN(TPM_TransmitData(tpmcmd->req, tpmcmd->req_len, tpmcmd->resp, &size));
934 tpmcmd->resp_len = size;
935
936 return TPM_SUCCESS;
937 default:
938 vtpmlogerror(VTPM_LOG_VTPM, "Invalid tag=%" PRIu16 "\n", tag);
939 status = TPM_BADTAG;
940 }
941
942 abort_egress:
943 tpmcmd->resp_len = VTPM_COMMAND_HEADER_SIZE;
944 pack_TPM_RSP_HEADER(tpmcmd->resp, tag + 3, tpmcmd->resp_len, status);
945
946 return status;
947 }