-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.c
More file actions
282 lines (226 loc) · 5.61 KB
/
command.c
File metadata and controls
282 lines (226 loc) · 5.61 KB
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
#include "config.h"
#include <stdio.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "command.h"
extern int verbose;
static struct cmd_def {
char *name;
size_t size;
} cmd_buf;
static struct cmd_def cmd_def[] = {
{ "reset", 0 },
{ "status", sizeof(struct sev_user_data_status) },
{ "pek_gen", 0 },
{ "pek_csr", sizeof(struct sev_user_data_pek_csr) },
{ "pdh_gen", 0 },
{ "pdh_export", sizeof(struct sev_user_data_pdh_cert_export) },
{ "pek_import", sizeof(struct sev_user_data_pek_cert_import) },
{ "get_id", sizeof(struct sev_user_data_get_id) },
{ "get_id2", sizeof(struct sev_user_data_get_id) },
{ "snp_status", sizeof(struct sev_user_data_snp_status) },
{ "snp_set_config", sizeof(struct sev_user_data_ext_snp_config) },
{ "snp_get_config", sizeof(struct sev_user_data_ext_snp_config) },
{ NULL, 0 }
};
static void print_hex_dump(char *desc, void *addr, int len)
{
int i;
unsigned int *buf = (unsigned int*)addr;
if (desc)
printf("%s", desc);
for (i = 0; i < len / sizeof(unsigned int); i++) {
if (i % 16 == 0)
printf("\n %04x ", i);
printf("%04x ", *buf);
buf += 1;
}
printf("\n");
}
static int sev_ioctl(int cmd, void *data)
{
int fd, r;
struct sev_issue_cmd arg = { };
fd = open("/dev/sev", O_RDWR);
if (fd < 0) {
printf("open() failed '%s'\n", strerror(errno));
return -1;
}
arg.cmd = cmd;
arg.data = (unsigned long)data;
if (verbose) {
printf("(in) ");
print_hex_dump(cmd_def[cmd].name, data, cmd_def[cmd].size);
}
r = ioctl(fd, SEV_ISSUE_CMD, &arg);
if (verbose) {
printf("(out) ");
print_hex_dump(cmd_def[cmd].name, data, cmd_def[cmd].size);
}
close(fd);
if (arg.error)
return arg.error;
return r;
}
int get_status(struct sev_user_data_status *status)
{
return sev_ioctl(SEV_PLATFORM_STATUS, status);
}
int get_snp_status(struct sev_user_data_snp_status *status)
{
return sev_ioctl(SNP_PLATFORM_STATUS, status);
}
int pek_gen(void)
{
return sev_ioctl(SEV_PEK_GEN, NULL);
}
int pdh_gen(void)
{
return sev_ioctl(SEV_PDH_GEN, NULL);
}
int factory_reset(void)
{
return sev_ioctl(SEV_FACTORY_RESET, NULL);
}
int get_pek_csr_length(void)
{
int ret;
struct sev_user_data_pek_csr csr = { };
ret = sev_ioctl(SEV_PEK_CSR, &csr);
if (ret == SEV_RET_INVALID_LEN)
return csr.length;
return -1;
}
int pek_csr(const char* data, unsigned int len)
{
struct sev_user_data_pek_csr csr;
if (!data || !len)
return 1;
csr.address = (unsigned long)data;
csr.length = len;
return sev_ioctl(SEV_PEK_CSR, &csr);
}
int get_oca_cert_length(void)
{
int r;
struct sev_user_data_pek_cert_import data = { };
r = sev_ioctl(SEV_PEK_CERT_IMPORT, &data);
if (r == SEV_RET_INVALID_LEN)
return data.oca_cert_len;
else
return -1;
}
int get_pek_cert_length(void)
{
int r;
struct sev_user_data_pek_cert_import data = { };
r = sev_ioctl(SEV_PEK_CERT_IMPORT, &data);
if (r == SEV_RET_INVALID_LEN)
return data.pek_cert_len;
else
return r;
}
int get_pdh_cert_length(void)
{
int r;
struct sev_user_data_pdh_cert_export data = { };
r = sev_ioctl(SEV_PDH_CERT_EXPORT, &data);
if (r == SEV_RET_INVALID_LEN)
return data.pdh_cert_len;
else
return -1;
}
int get_cert_chain_length(void)
{
int r;
struct sev_user_data_pdh_cert_export data = { };
r = sev_ioctl(SEV_PDH_CERT_EXPORT, &data);
if (r == SEV_RET_INVALID_LEN)
return data.cert_chain_len;
else
return -1;
}
int pek_cert_import(const unsigned char* pek_cert, unsigned int pek_cert_len,
const unsigned char* oca_cert, unsigned int oca_cert_len)
{
struct sev_user_data_pek_cert_import data = { };
if (!pek_cert || !pek_cert_len || !oca_cert || !oca_cert_len)
return 1;
data.pek_cert_address = (unsigned long)pek_cert;
data.pek_cert_len = pek_cert_len;
data.oca_cert_address = (unsigned long)oca_cert;
data.oca_cert_len = oca_cert_len;
return sev_ioctl(SEV_PEK_CERT_IMPORT, &data);
}
int pdh_cert_export(unsigned char* pdh, unsigned int pdh_len,
unsigned char* cert_chain, unsigned int cert_chain_len)
{
struct sev_user_data_pdh_cert_export data = { };
if (!pdh || !pdh_len || !cert_chain || !cert_chain_len)
return 1;
data.pdh_cert_address = (unsigned long)pdh;
data.pdh_cert_len = pdh_len;
data.cert_chain_address = (unsigned long)cert_chain;
data.cert_chain_len = cert_chain_len;
return sev_ioctl(SEV_PDH_CERT_EXPORT, &data);
}
int set_ext_snp_config(unsigned long reported_tcb, char *certs, int certs_len)
{
struct sev_user_data_snp_config config = {};
struct sev_user_data_ext_snp_config data = {};
if (certs && certs_len) {
data.certs_address = (unsigned long)certs;
data.certs_len = certs_len;
}
if (reported_tcb) {
config.reported_tcb = reported_tcb;
data.config_address = (unsigned long)&config;
}
return sev_ioctl(SNP_SET_EXT_CONFIG, &data);
}
int get_ext_snp_config(struct sev_user_data_ext_snp_config *data)
{
return sev_ioctl(SNP_GET_EXT_CONFIG, data);
}
#if HAVE_DECL_SEV_GET_ID2
int get_id(unsigned char **id, unsigned int *len)
{
int r;
unsigned char *buf;
unsigned int sz;
struct sev_user_data_get_id2 data = { };
r = sev_ioctl(SEV_GET_ID2, &data);
if (r == SEV_RET_INVALID_LEN) {
sz = data.length;
} else {
fprintf(stderr, "Error: expected %d got %d\n",
SEV_RET_INVALID_LEN, r);
return 1;
}
buf = calloc(sizeof(char), sz);
if (!buf) {
perror("malloc()");
return 1;
}
data.address = (unsigned long)buf;
data.length = sz;
r = sev_ioctl(SEV_GET_ID2, &data);
if (r) {
free(buf);
return r;
}
*id = buf;
*len = sz;
return 0;
}
#else
int get_id(unsigned char **id, unsigned int *len)
{
fprintf(stderr, "%s 'not supported'\n", __func__);
return 1; /* not supported */
}
#endif