-
Notifications
You must be signed in to change notification settings - Fork 0
/
sev-cert.c
191 lines (162 loc) · 4.24 KB
/
sev-cert.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
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "sev-cert.h"
static const char *curve_id_str[] = {
"invalid",
"P256",
"P384",
};
static const int curve_id_bytes[] = {
0, /* invalid */
32,
48
};
static void print_hex_dump(char *desc, void *addr, int len)
{
int i;
uint8_t *buf = (uint8_t *)addr;
if (desc)
printf("%s", desc);
for (i = 0; i < len; i++) {
if (i % 16 == 0)
printf("\n %04x ", i);
printf("%02hhx ", buf[i]);
}
printf("\n");
}
static void dump_pubkey_rsa(void *buf)
{
rsa_key_t *h = (rsa_key_t*) buf;
printf(" MODULES_SIZE : %d\n", h->modulus_sz);
print_hex_dump(" PUBEXP", h->pubexp, sizeof(h->pubexp));
print_hex_dump(" MODULUS", h->pubexp, sizeof(h->pubexp));
}
static void dump_pubkey_ecdh(void *buf)
{
ecdh_key_t *h = (ecdh_key_t*) buf;
printf(" CURVE : %s\n", curve_id_str[h->curve]);
print_hex_dump(" QX", h->qx, curve_id_bytes[h->curve]);
print_hex_dump(" QY", h->qy, curve_id_bytes[h->curve]);
}
static void dump_pubkey_ecdsa(void *buf)
{
ecdsa_key_t *h = (ecdsa_key_t*) buf;
printf(" CURVE : %s\n", curve_id_str[h->curve]);
print_hex_dump(" QX", h->qx, curve_id_bytes[h->curve]);
print_hex_dump(" QY", h->qy, curve_id_bytes[h->curve]);
}
static void dump_sig_rsa(void *buf)
{
rsa_sig_t *h = (rsa_sig_t*) buf;
print_hex_dump(" S", h->s, sizeof(h->s));
}
static void dump_sig_ecdsa(void *buf)
{
ecdsa_sig_t *h = (ecdsa_sig_t*) buf;
print_hex_dump(" R", h->r, sizeof(h->r));
print_hex_dump(" S", h->s, sizeof(h->s));
}
char* extract_cert(const char *buf, size_t len, pubkey_usage_t type)
{
int i;
cert_data_t *out;
for (i = 0; i < len; i+= sizeof(cert_data_t)) {
cert_data_t *h = (cert_data_t*) buf;
buf += sizeof(cert_data_t);
if (h->version != 0x1)
continue;
if (h->pubkey_usage != type)
continue;
out = calloc(sizeof(cert_data_t), 1);
if (!out)
return NULL;
memcpy(out, h, sizeof(cert_data_t));
return (char*)out;
}
return NULL;
}
void dump_cert_data(void *buf, int len)
{
int i;
for (i = 0; i < len; i+= sizeof(cert_data_t)) {
cert_data_t *h = (cert_data_t*) buf;
if (h->version != 0x1) {
fprintf(stderr, "invalid version expect 0x1 got 0x%x\n", h->version);
continue;
}
printf("VERSION : %04d\n", h->version);
printf("MAJOR : %02d\n", h->major);
printf("MINOR : %02d\n", h->minor);
switch(h->pubkey_usage) {
case PUBKEY_ARK: printf("PUBKEY : ARK\n"); break;
case PUBKEY_ASK: printf("PUBKEY : ASK\n"); break;
case PUBKEY_INVD: printf("PUBKEY : INVALID\n"); break;
case PUBKEY_OCA: printf("PUBKEY : OCA\n"); break;
case PUBKEY_PEK: printf("PUBKEY : PEK\n"); break;
case PUBKEY_PDH: printf("PUBKEY : PDH\n"); break;
case PUBKEY_CEK: printf("PUBKEY : CEK\n"); break;
}
printf("PUBKEY_ALGO :");
fflush(stdout);
switch(h->pubkey_algo) {
case PUBKEY_ALGO_RSA_SHA_256:
printf(" RSA with SHA-256\n");
dump_pubkey_rsa(h->pubkey);
break;
case PUBKEY_ALGO_ECDSA_SHA_256:
printf(" ECDSA with SHA-256\n");
dump_pubkey_ecdsa(h->pubkey);
break;
case PUBKEY_ALGO_ECDH_SHA_256:
printf(" ECDH with SHA-256\n");
dump_pubkey_ecdh(h->pubkey);
break;
case PUBKEY_ALGO_RSA_SHA_384:
printf(" RSA with SHA-384\n");
dump_pubkey_rsa(h->pubkey);
break;
case PUBKEY_ALGO_ECDSA_SHA_384:
printf(" ECDSA with SHA-384\n");
dump_pubkey_ecdsa(h->pubkey);
break;
case PUBKEY_ALGO_ECDH_SHA_384:
printf(" ECDH with SHA-384\n");
dump_pubkey_ecdh(h->pubkey);
break;
default: printf(" INVALID\n"); break;
}
if (h->sig1_usage != SIG_USAGE_NOT_PRESENT) {
printf("SIG1\n");
switch(h->sig1_algo) {
case PUBKEY_ALGO_RSA_SHA_256:
case PUBKEY_ALGO_RSA_SHA_384:
dump_sig_rsa(h->sig1);
break;
case PUBKEY_ALGO_ECDSA_SHA_256:
case PUBKEY_ALGO_ECDSA_SHA_384:
dump_sig_ecdsa(h->sig1);
break;
default: break;
}
}
if (len < offsetof(cert_data_t, sig2_usage))
continue;
if (h->sig2_usage != SIG_USAGE_NOT_PRESENT) {
printf("SIG2\n");
switch(h->sig2_algo) {
case PUBKEY_ALGO_RSA_SHA_256:
case PUBKEY_ALGO_RSA_SHA_384:
dump_sig_rsa(h->sig2);
break;
case PUBKEY_ALGO_ECDSA_SHA_256:
case PUBKEY_ALGO_ECDSA_SHA_384:
dump_sig_ecdsa(h->sig2);
break;
default: break;
}
}
buf += sizeof(cert_data_t);
}
}