-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathtdx_attest.c
511 lines (463 loc) · 15.8 KB
/
tdx_attest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
* Copyright (C) 2011-2021 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/socket.h>
#include <linux/vm_sockets.h>
#include "tdx_attest.h"
#include "qgs_msg_lib.h"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/types.h>
#include <linux/ioctl.h>
#include <sys/ioctl.h>
#include <fcntl.h>
// For strtoul
#include <limits.h>
#include <errno.h>
#include <syslog.h>
#include <assert.h>
#define TDX_ATTEST_DEV_PATH "/dev/tdx-attest"
#define CFG_FILE_PATH "/etc/tdx-attest.conf"
//TODO: Should include kernel header
#define TDX_CMD_GET_TDREPORT _IOWR('T', 0x01, __u64)
#define TDX_CMD_EXTEND_RTMR _IOR('T', 0x04, __u64)
#define TDX_CMD_GET_EXTEND_SIZE _IOR('T', 0x05, __u64)
#define TDX_CMD_GEN_QUOTE _IOR('T', 0x02, __u64)
#ifdef DEBUG
#define TDX_TRACE \
do { \
fprintf(stderr, "\n[%s:%d] ", __FILE__, __LINE__); \
perror(NULL); \
}while(0)
#else
#define TDX_TRACE
#endif
#pragma pack(push, 1)
// It's a 4*4K byte structure
typedef struct _get_quote_blob_t
{
// Following fields are used for transport layer, LE
uint64_t version;
uint64_t status;
uint32_t in_len;
uint32_t out_len;
// Following fields are consumed by QGS and this library
uint8_t trans_len[4]; // BE
uint8_t p_buf[4 * 4 * 1024 - 28];
} get_quote_blob_t;
typedef struct _get_quote_ioctl_arg_t
{
void *p_blob;
size_t len;
} get_quote_ioctl_arg_t;
#pragma pack(pop)
static const unsigned HEADER_SIZE = 4;
static const tdx_uuid_t g_intel_tdqe_uuid = {TDX_SGX_ECDSA_ATTESTATION_ID};
static unsigned int get_vsock_port(void)
{
FILE *p_config_fd = NULL;
char *p_line = NULL;
char *p = NULL;
size_t line_len = 0;
long long_num = 0;
unsigned int port = 0;
p_config_fd = fopen(CFG_FILE_PATH, "r");
if (NULL == p_config_fd) {
TDX_TRACE;
return 0;
}
while(-1 != getline(&p_line, &line_len, p_config_fd)) {
char temp[11] = {0};
int number = 0;
int ret = sscanf(p_line, " %10[#]", temp);
if (ret == 1) {
continue;
}
/* leading or trailing white space are ignored, white space around '='
are also ignored. The number should no longer than 10 characters.
Trailing non-whitespace are not allowed. */
ret = sscanf(p_line, " port = %10[0-9] %n", temp, &number);
/* Make sure number is positive then make the cast. It's not likely to
have a negtive value, just a defense-in-depth. The cast is used to
suppress the -Wsign-compare warning. */
if (ret == 1 && number > 0 && ((size_t)number < line_len)
&& !p_line[number]) {
errno = 0;
long_num = strtol(temp, &p, 10);
if (p == temp) {
TDX_TRACE;
port = 0;
break;
}
// make sure that no range error occurred
if (errno == ERANGE || long_num > UINT_MAX) {
TDX_TRACE;
port = 0;
break;
}
// range is ok, so we can convert to int
port = (unsigned int)long_num & 0xFFFFFFFF;
#ifdef DEBUG
fprintf(stdout, "\nGet the vsock port number [%u]\n", port);
#endif
break;
}
}
/* p_line is allocated by sscanf */
free(p_line);
fclose(p_config_fd);
return port;
}
static tdx_attest_error_t get_tdx_report(
int devfd,
const tdx_report_data_t *p_tdx_report_data,
tdx_report_t *p_tdx_report)
{
if (-1 == devfd) {
return TDX_ATTEST_ERROR_UNEXPECTED;
}
if (!p_tdx_report) {
fprintf(stderr, "\nNeed to input TDX report.");
return TDX_ATTEST_ERROR_INVALID_PARAMETER;
}
uint8_t tdx_report[TDX_REPORT_SIZE] = {0};
if (p_tdx_report_data) {
memcpy(tdx_report, p_tdx_report_data->d, sizeof(p_tdx_report_data->d));
}
if (-1 == ioctl(devfd, TDX_CMD_GET_TDREPORT, tdx_report)) {
TDX_TRACE;
return TDX_ATTEST_ERROR_REPORT_FAILURE;
}
memcpy(p_tdx_report->d, tdx_report, sizeof(p_tdx_report->d));
return TDX_ATTEST_SUCCESS;
}
tdx_attest_error_t tdx_att_get_quote(
const tdx_report_data_t *p_tdx_report_data,
const tdx_uuid_t *p_att_key_id_list,
uint32_t list_size,
tdx_uuid_t *p_att_key_id,
uint8_t **pp_quote,
uint32_t *p_quote_size,
uint32_t flags)
{
int s = -1;
int devfd = -1;
int use_tdvmcall = 1;
uint32_t quote_size = 0;
uint32_t recieved_bytes = 0;
uint32_t in_msg_size = 0;
unsigned int vsock_port = 0;
tdx_attest_error_t ret = TDX_ATTEST_ERROR_UNEXPECTED;
get_quote_blob_t *p_get_quote_blob = NULL;
tdx_report_t tdx_report;
uint32_t msg_size = 0;
qgs_msg_error_t qgs_msg_ret = QGS_MSG_SUCCESS;
qgs_msg_header_t *p_header = NULL;
uint8_t *p_req = NULL;
const uint8_t *p_quote = NULL;
const uint8_t *p_selected_id = NULL;
uint32_t id_size = 0;
if ((!p_att_key_id_list && list_size) ||
(p_att_key_id_list && !list_size)) {
ret = TDX_ATTEST_ERROR_INVALID_PARAMETER;
goto ret_point;
}
if (!pp_quote) {
ret = TDX_ATTEST_ERROR_INVALID_PARAMETER;
goto ret_point;
}
if (flags) {
//TODO: I think we need to have a runtime version to make this flag usable.
ret = TDX_ATTEST_ERROR_INVALID_PARAMETER;
goto ret_point;
}
// Currently only intel TDQE are supported
if (1 < list_size) {
ret = TDX_ATTEST_ERROR_INVALID_PARAMETER;
}
if (p_att_key_id_list && memcmp(p_att_key_id_list, &g_intel_tdqe_uuid,
sizeof(g_intel_tdqe_uuid))) {
ret = TDX_ATTEST_ERROR_UNSUPPORTED_ATT_KEY_ID;
}
*pp_quote = NULL;
memset(&tdx_report, 0, sizeof(tdx_report));
p_get_quote_blob = (get_quote_blob_t *)malloc(sizeof(get_quote_blob_t));
if (!p_get_quote_blob) {
ret = TDX_ATTEST_ERROR_OUT_OF_MEMORY;
goto ret_point;
}
devfd = open(TDX_ATTEST_DEV_PATH, O_RDWR | O_SYNC);
if (-1 == devfd) {
TDX_TRACE;
ret = TDX_ATTEST_ERROR_DEVICE_FAILURE;
goto ret_point;
}
ret = get_tdx_report(devfd, p_tdx_report_data, &tdx_report);
if (TDX_ATTEST_SUCCESS != ret) {
goto ret_point;
}
qgs_msg_ret = qgs_msg_gen_get_quote_req(tdx_report.d, sizeof(tdx_report.d),
NULL, 0, &p_req, &msg_size);
if (QGS_MSG_SUCCESS != qgs_msg_ret) {
#ifdef DEBUG
fprintf(stdout, "\nqgs_msg_gen_get_quote_req return 0x%x\n", qgs_msg_ret);
#endif
ret = TDX_ATTEST_ERROR_UNEXPECTED;
goto ret_point;
}
if (msg_size > sizeof(p_get_quote_blob->p_buf)) {
#ifdef DEBUG
fprintf(stdout, "\nqmsg_size[%d] is too big\n", msg_size);
#endif
ret = TDX_ATTEST_ERROR_NOT_SUPPORTED;
goto ret_point;
}
p_get_quote_blob->trans_len[0] = (uint8_t)((msg_size >> 24) & 0xFF);
p_get_quote_blob->trans_len[1] = (uint8_t)((msg_size >> 16) & 0xFF);
p_get_quote_blob->trans_len[2] = (uint8_t)((msg_size >> 8) & 0xFF);
p_get_quote_blob->trans_len[3] = (uint8_t)(msg_size & 0xFF);
memcpy(p_get_quote_blob->p_buf, p_req, msg_size);
do {
vsock_port = get_vsock_port();
if (!vsock_port) {
syslog(LOG_INFO, "libtdx_attest: fallback to tdvmcall mode.");
break;
}
s = socket(AF_VSOCK, SOCK_STREAM, 0);
if (-1 == s) {
syslog(LOG_INFO, "libtdx_attest: fallback to tdvmcall mode.");
break;
}
struct sockaddr_vm vm_addr;
memset(&vm_addr, 0, sizeof(vm_addr));
vm_addr.svm_family = AF_VSOCK;
vm_addr.svm_reserved1 = 0;
vm_addr.svm_port = vsock_port;
vm_addr.svm_cid = VMADDR_CID_HOST;
if (connect(s, (struct sockaddr *)&vm_addr, sizeof(vm_addr))) {
syslog(LOG_INFO, "libtdx_attest: fallback to tdvmcall mode.");
break;
}
// Write to socket
if (HEADER_SIZE + msg_size != send(s, p_get_quote_blob->trans_len,
HEADER_SIZE + msg_size, 0)) {
TDX_TRACE;
ret = TDX_ATTEST_ERROR_VSOCK_FAILURE;
goto ret_point;
}
// Read the response size header
if (HEADER_SIZE != recv(s, p_get_quote_blob->trans_len,
HEADER_SIZE, 0)) {
TDX_TRACE;
ret = TDX_ATTEST_ERROR_VSOCK_FAILURE;
goto ret_point;
}
// decode the size
for (unsigned i = 0; i < HEADER_SIZE; ++i) {
in_msg_size = in_msg_size * 256
+ ((p_get_quote_blob->trans_len[i]) & 0xFF);
}
// prepare the buffer and read the reply body
#ifdef DEBUG
fprintf(stdout, "\nReply message body is %u bytes", in_msg_size);
#endif
if (sizeof(p_get_quote_blob->p_buf) < in_msg_size)
{
#ifdef DEBUG
fprintf(stdout, "\nReply message body is too big");
#endif
ret = TDX_ATTEST_ERROR_UNEXPECTED;
goto ret_point;
}
while( recieved_bytes < in_msg_size) {
int recv_ret = (int)recv(s, p_get_quote_blob->p_buf + recieved_bytes,
in_msg_size - recieved_bytes, 0);
if (recv_ret < 0) {
ret = TDX_ATTEST_ERROR_VSOCK_FAILURE;
goto ret_point;
}
recieved_bytes += (uint32_t)recv_ret;
}
#ifdef DEBUG
fprintf(stdout, "\nGet %u bytes response from vsock", recieved_bytes);
#endif
use_tdvmcall = 0;
} while (0);
if (use_tdvmcall) {
int ioctl_ret = 0;
get_quote_ioctl_arg_t arg;
p_get_quote_blob->version = 1;
p_get_quote_blob->status = 0;
p_get_quote_blob->in_len = HEADER_SIZE + msg_size;
p_get_quote_blob->out_len = (uint32_t)(sizeof(*p_get_quote_blob) - 24);
arg.p_blob = p_get_quote_blob;
arg.len = sizeof(*p_get_quote_blob);
ioctl_ret = ioctl(devfd, TDX_CMD_GEN_QUOTE, &arg);
if (EBUSY == ioctl_ret) {
TDX_TRACE;
ret = TDX_ATTEST_ERROR_BUSY;
goto ret_point;
} else if (ioctl_ret) {
TDX_TRACE;
ret = TDX_ATTEST_ERROR_QUOTE_FAILURE;
goto ret_point;
}
if (p_get_quote_blob->status
|| p_get_quote_blob->out_len <= HEADER_SIZE) {
TDX_TRACE;
ret = TDX_ATTEST_ERROR_UNEXPECTED;
goto ret_point;
}
//in_msg_size is the size of serialized response, remove 4bytes header
//TODO: Decode the HEAD and compare it with out_len as defense-in-depth
in_msg_size = p_get_quote_blob->out_len - HEADER_SIZE;
#ifdef DEBUG
fprintf(stdout, "\nGet %u bytes response from tdvmcall", in_msg_size);
#endif
}
qgs_msg_ret = qgs_msg_inflate_get_quote_resp(
p_get_quote_blob->p_buf, in_msg_size,
&p_selected_id, &id_size,
&p_quote, "e_size);
if (QGS_MSG_SUCCESS != qgs_msg_ret) {
#ifdef DEBUG
fprintf(stdout, "\nqgs_msg_inflate_get_quote_resp return 0x%x", qgs_msg_ret);
#endif
ret = TDX_ATTEST_ERROR_UNEXPECTED;
goto ret_point;
}
// We've called qgs_msg_inflate_get_quote_resp, the message type should be GET_QUOTE_RESP
p_header = (qgs_msg_header_t *)p_get_quote_blob->p_buf;
if (p_header->error_code != 0) {
#ifdef DEBUG
fprintf(stdout, "\nerror code in resp msg is 0x%x", p_header->error_code);
#endif
ret = TDX_ATTEST_ERROR_UNEXPECTED;
goto ret_point;
}
*pp_quote = malloc(quote_size);
if (!*pp_quote) {
ret = TDX_ATTEST_ERROR_OUT_OF_MEMORY;
goto ret_point;
}
memcpy(*pp_quote, p_quote, quote_size);
if (p_quote_size) {
*p_quote_size = quote_size;
}
if (p_att_key_id) {
*p_att_key_id = g_intel_tdqe_uuid;
}
ret = TDX_ATTEST_SUCCESS;
ret_point:
if (s >= 0) {
close(s);
}
if (-1 != devfd) {
close(devfd);
}
qgs_msg_free(p_req);
free(p_get_quote_blob);
return ret;
}
tdx_attest_error_t tdx_att_free_quote(
uint8_t *p_quote)
{
free(p_quote);
return TDX_ATTEST_SUCCESS;
}
tdx_attest_error_t tdx_att_get_report(
const tdx_report_data_t *p_tdx_report_data,
tdx_report_t *p_tdx_report)
{
int devfd;
tdx_attest_error_t ret = TDX_ATTEST_SUCCESS;
devfd = open(TDX_ATTEST_DEV_PATH, O_RDWR | O_SYNC);
if (-1 == devfd) {
TDX_TRACE;
return TDX_ATTEST_ERROR_DEVICE_FAILURE;
}
ret = get_tdx_report(devfd, p_tdx_report_data, p_tdx_report);
close(devfd);
return ret;
}
tdx_attest_error_t tdx_att_get_supported_att_key_ids(
tdx_uuid_t *p_att_key_id_list,
uint32_t *p_list_size)
{
if (!p_list_size) {
return TDX_ATTEST_ERROR_INVALID_PARAMETER;
}
if (p_att_key_id_list && !*p_list_size) {
return TDX_ATTEST_ERROR_INVALID_PARAMETER;
}
if (!p_att_key_id_list && *p_list_size) {
return TDX_ATTEST_ERROR_INVALID_PARAMETER;
}
if (p_att_key_id_list) {
p_att_key_id_list[0] = g_intel_tdqe_uuid;
}
*p_list_size = 1;
return TDX_ATTEST_SUCCESS;
}
tdx_attest_error_t tdx_att_extend(
const tdx_rtmr_event_t *p_rtmr_event)
{
int devfd = -1;
uint64_t extend_data_size = 0;
if (!p_rtmr_event || p_rtmr_event->version != 1) {
return TDX_ATTEST_ERROR_INVALID_PARAMETER;
}
if (p_rtmr_event->event_data_size) {
return TDX_ATTEST_ERROR_NOT_SUPPORTED;
}
devfd = open(TDX_ATTEST_DEV_PATH, O_RDWR | O_SYNC);
if (-1 == devfd) {
TDX_TRACE;
return TDX_ATTEST_ERROR_DEVICE_FAILURE;
}
if (-1 == ioctl(devfd, TDX_CMD_GET_EXTEND_SIZE, &extend_data_size)) {
TDX_TRACE;
close(devfd);
return TDX_ATTEST_ERROR_EXTEND_FAILURE;
}
assert(extend_data_size == sizeof(p_rtmr_event->extend_data));
if (-1 == ioctl(devfd, TDX_CMD_EXTEND_RTMR, &p_rtmr_event->rtmr_index)) {
TDX_TRACE;
close(devfd);
if (EINVAL == errno) {
return TDX_ATTEST_ERROR_INVALID_RTMR_INDEX;
}
return TDX_ATTEST_ERROR_EXTEND_FAILURE;
}
close(devfd);
return TDX_ATTEST_SUCCESS;
}