forked from cloud-security-research/sgx-ra-tls
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wolfssl-ra-challenger.c
385 lines (318 loc) · 12.8 KB
/
wolfssl-ra-challenger.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
/**
* wolfSSL-based implementation of the RA-TLS challenger API
* (cf. ra-challenger.h).
*/
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <wolfssl/options.h>
#include <wolfssl/ssl.h>
#include <wolfssl/wolfcrypt/asn.h>
#include <wolfssl/wolfcrypt/asn_public.h>
#include <wolfssl/wolfcrypt/coding.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/signature.h>
#ifdef RATLS_ECDSA
#include <SgxEcdsaAttestation/QuoteVerification.h>
#endif
#include "ra.h"
#include "wolfssl-ra.h"
#include "ra-challenger.h"
#include "ra-challenger_private.h"
#ifdef RATLS_ECDSA
#include "ecdsa-sample-data/real/sample_data.h"
#endif
extern unsigned char ias_sign_ca_cert_der[];
extern unsigned int ias_sign_ca_cert_der_len;
void get_quote_from_cert
(
const uint8_t* der_crt,
uint32_t der_crt_len,
sgx_quote_t* q
)
{
DecodedCert crt;
int ret;
InitDecodedCert(&crt, (byte*) der_crt, der_crt_len, NULL);
InitSignatureCtx(&crt.sigCtx, NULL, INVALID_DEVID);
ret = ParseCertRelative(&crt, CERT_TYPE, NO_VERIFY, 0);
assert(ret == 0);
get_quote_from_extension(crt.extensions, crt.extensionsSz, q);
FreeDecodedCert(&crt);
}
void get_quote_from_report
(
const uint8_t* report /* in */,
const int report_len /* in */,
sgx_quote_t* quote
)
{
// Move report into \0 terminated buffer such that we can work
// with str* functions.
char buf[report_len + 1];
memcpy(buf, report, report_len);
buf[report_len] = '\0';
const char* json_string = "\"isvEnclaveQuoteBody\":\"";
char* p_begin = strstr(buf, json_string);
assert(p_begin != NULL);
p_begin += strlen(json_string);
const char* p_end = strchr(p_begin, '"');
assert(p_end != NULL);
const int quote_base64_len = p_end - p_begin;
uint8_t* quote_bin = malloc(quote_base64_len);
uint32_t quote_bin_len = quote_base64_len;
Base64_Decode((const byte*) p_begin, quote_base64_len,
quote_bin, "e_bin_len);
assert(quote_bin_len <= sizeof(sgx_quote_t));
memset(quote, 0, sizeof(sgx_quote_t));
memcpy(quote, quote_bin, quote_bin_len);
free(quote_bin);
}
static
int verify_report_data_against_server_cert
(
DecodedCert* crt,
sgx_quote_t* quote
)
{
/* crt->publicKey seems to be the DER encoded public key. The
OpenSSL DER formatted version of the public key obtained with
openssl rsa -in ./server-key.pem -pubout -outform DER -out
server-pubkey.der has an additional 24 bytes
prefix/header. d->pubKeySize is 270 and the server-pubkey.der
file has 294 bytes. That's to be expected according to [1] */
/* [1] https://crypto.stackexchange.com/questions/14491/why-is-a-2048-bit-public-rsa-key-represented-by-540-hexadecimal-characters-in */
/* 2017-12-06, Thomas Knauth, A hard-coded offset into the
DER-encoded public key only works for specific key sizes. The
24 byte offset is specific to 2048 bit RSA keys. For example, a
1024 bit RSA key only has an offset of 22.
*/
RsaKey rsaKey;
unsigned int idx = 0;
int ret;
wc_InitRsaKey(&rsaKey, NULL);
ret = wc_RsaPublicKeyDecode(crt->publicKey, &idx, &rsaKey, crt->pubKeySize);
assert(ret == 0);
byte shaSum[SHA256_DIGEST_SIZE] = {0, };
sha256_rsa_pubkey(shaSum, &rsaKey);
wc_FreeRsaKey(&rsaKey);
#ifdef DEBUG
fprintf(stderr, "SHA256 of server's public key:\n");
for (int i=0; i < SHA256_DIGEST_SIZE; ++i) fprintf(stderr, "%02x", shaSum[i]);
fprintf(stderr, "\n");
fprintf(stderr, "Quote's report data:\n");
for (int i=0; i < SGX_REPORT_DATA_SIZE; ++i) fprintf(stderr, "%02x", quote->report_body.report_data.d[i]);
fprintf(stderr, "\n");
#endif
assert(SHA256_DIGEST_SIZE <= SGX_REPORT_DATA_SIZE);
ret = memcmp(quote->report_body.report_data.d, shaSum, SHA256_DIGEST_SIZE);
assert(ret == 0);
return ret;
}
static
int verify_ias_report_signature
(
attestation_verification_report_t* attn_report
)
{
DecodedCert crt;
int ret;
uint8_t der[4096];
int der_len;
der_len = wolfSSL_CertPemToDer(attn_report->ias_sign_cert, attn_report->ias_sign_cert_len,
der, sizeof(der),
CERT_TYPE);
assert(der_len > 0);
InitDecodedCert(&crt, der, der_len, NULL);
InitSignatureCtx(&crt.sigCtx, NULL, INVALID_DEVID);
ret = ParseCertRelative(&crt, CERT_TYPE, NO_VERIFY, 0);
assert(ret == 0);
RsaKey rsaKey;
unsigned int idx = 0;
ret = wc_InitRsaKey(&rsaKey, NULL);
assert(ret == 0);
ret = wc_RsaPublicKeyDecode(crt.publicKey, &idx, &rsaKey, crt.pubKeySize);
assert(ret == 0);
ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256,
/* This is required such that signature
matches what OpenSSL produces. OpenSSL
embeds the hash in an ASN.1 structure
before signing it. */
WC_SIGNATURE_TYPE_RSA_W_ENC,
attn_report->ias_report, attn_report->ias_report_len,
attn_report->ias_report_signature, attn_report->ias_report_signature_len,
&rsaKey, sizeof(rsaKey));
FreeDecodedCert(&crt);
wc_FreeRsaKey(&rsaKey);
return ret;
}
static
int verify_ias_certificate_chain(attestation_verification_report_t* attn_report) {
WOLFSSL_CERT_MANAGER* cm;
cm = wolfSSL_CertManagerNew();
assert(cm != NULL);
/* like load verify locations, 1 for success, < 0 for error */
int ret = wolfSSL_CertManagerLoadCABuffer(cm, ias_sign_ca_cert_der,
ias_sign_ca_cert_der_len,
SSL_FILETYPE_ASN1);
assert(ret == 1);
ret = wolfSSL_CertManagerVerifyBuffer(cm, attn_report->ias_sign_cert,
attn_report->ias_sign_cert_len,
SSL_FILETYPE_PEM);
assert(ret == SSL_SUCCESS);
wolfSSL_CertManagerFree(cm);
cm = NULL;
return 0;
}
/**
* Check if isvEnclaveQuoteStatus is "OK"
* (cf. https://software.intel.com/sites/default/files/managed/7e/3b/ias-api-spec.pdf,
* pg. 24).
*
* @return 0 if verified successfully, 1 otherwise.
*/
static
int verify_enclave_quote_status
(
const char* ias_report,
int ias_report_len
)
{
// Move ias_report into \0 terminated buffer such that we can work
// with str* functions.
char buf[ias_report_len + 1];
memcpy(buf, ias_report, ias_report_len);
buf[ias_report_len] = '\0';
const char* json_string = "\"isvEnclaveQuoteStatus\":\"";
char* p_begin = strstr(buf, json_string);
assert(p_begin != NULL);
p_begin += strlen(json_string);
const char* status_OK = "OK\"";
if (0 == strncmp(p_begin, status_OK, strlen(status_OK))) return 0;
#ifdef SGX_GROUP_OUT_OF_DATE
const char* status_outdated = "GROUP_OUT_OF_DATE\"";
if (0 == strncmp(p_begin, status_outdated, strlen(status_outdated))) {
printf("WARNING: isvEnclaveQuoteStatus is GROUP_OUT_OF_DATE\n");
return 0;
}
#endif
return 1;
}
static
int epid_verify_sgx_cert_extensions
(
uint8_t* der_crt,
uint32_t der_crt_len
)
{
attestation_verification_report_t attn_report;
DecodedCert crt;
int ret;
InitDecodedCert(&crt, der_crt, der_crt_len, NULL);
InitSignatureCtx(&crt.sigCtx, NULL, INVALID_DEVID);
ret = ParseCertRelative(&crt, CERT_TYPE, NO_VERIFY, 0);
assert(ret == 0);
extract_x509_extensions(crt.extensions, crt.extensionsSz, &attn_report);
/* Base64 decode attestation report signature. */
uint8_t sig_base64[sizeof(attn_report.ias_report_signature)];
memcpy(sig_base64, attn_report.ias_report_signature, attn_report.ias_report_signature_len);
int rc = Base64_Decode(sig_base64, attn_report.ias_report_signature_len,
attn_report.ias_report_signature, &attn_report.ias_report_signature_len);
assert(0 == rc);
ret = verify_ias_certificate_chain(&attn_report);
assert(ret == 0);
ret = verify_ias_report_signature(&attn_report);
assert(ret == 0);
ret = verify_enclave_quote_status((const char*) attn_report.ias_report,
attn_report.ias_report_len);
assert(ret == 0);
sgx_quote_t quote = {0, };
get_quote_from_report(attn_report.ias_report,
attn_report.ias_report_len,
"e);
ret = verify_report_data_against_server_cert(&crt, "e);
assert(ret == 0);
FreeDecodedCert(&crt);
return 0;
}
#ifdef RATLS_ECDSA
static
int ecdsa_verify_sgx_cert_extensions
(
uint8_t* der_crt,
uint32_t der_crt_len
)
{
DecodedCert crt;
int ret;
InitDecodedCert(&crt, der_crt, der_crt_len, NULL);
InitSignatureCtx(&crt.sigCtx, NULL, INVALID_DEVID);
ret = ParseCertRelative(&crt, CERT_TYPE, NO_VERIFY, 0);
assert(ret == 0);
ecdsa_attestation_evidence_t evidence;
ecdsa_extract_x509_extensions(crt.extensions,
crt.extensionsSz,
&evidence);
FreeDecodedCert(&crt);
/* pem_cert_chain := pck cert + pck CA cert + root CA cert */
/* crls := root ca crl + pck crl */
/* pem_trusted_root_ca_cert */
size_t pck_cert_chain_len = evidence.pck_sign_chain_len + evidence.pck_crt_len + 1;
char pck_cert_chain[pck_cert_chain_len];
memcpy(pck_cert_chain, evidence.pck_crt, evidence.pck_crt_len);
memcpy(pck_cert_chain + evidence.pck_crt_len, evidence.pck_sign_chain, evidence.pck_sign_chain_len);
pck_cert_chain[sizeof(pck_cert_chain) - 1] = '\0';
assert(evidence.root_ca_crl_len < sizeof(evidence.root_ca_crl));
evidence.root_ca_crl[evidence.root_ca_crl_len] = '\0';
assert(evidence.pck_crl_len < sizeof(evidence.pck_crl));
evidence.pck_crl[evidence.pck_crl_len] = '\0';
const char* const crls[] = {(char*) evidence.root_ca_crl,
(char*) evidence.pck_crl};
/* SGXDataCenterAttestationPrimitives/QuoteVerification/Src/ThirdParty/CMakeLists.txt depends on openssl-1.1.0i.tar.gz" */
/* libQuoteVerification.so seems to link in OpenSSL dependencies statically?! */
/* QVL expects zero terminated strings as input! */
int pck_crt_status = sgxAttestationVerifyPCKCertificate(pck_cert_chain, crls,
(char*) ecdsa_sample_data_real_trustedRootCaCert_pem);
assert(evidence.tcb_info_len < sizeof(evidence.tcb_info));
evidence.tcb_info[evidence.tcb_info_len] = '\0';
assert(evidence.tcb_sign_chain_len < sizeof(evidence.tcb_sign_chain));
evidence.tcb_sign_chain[evidence.tcb_sign_chain_len] = '\0';
int tcb_info_status = sgxAttestationVerifyTCBInfo((char*) evidence.tcb_info,
(char*) evidence.tcb_sign_chain,
(char*) evidence.root_ca_crl,
(char*) ecdsa_sample_data_real_trustedRootCaCert_pem);
assert(evidence.qe_identity_len < sizeof(evidence.qe_identity));
evidence.qe_identity[evidence.qe_identity_len] = '\0';
int qe_identity_status = sgxAttestationVerifyQEIdentity((char*) evidence.qe_identity,
(char*) evidence.tcb_sign_chain,
(char*) evidence.root_ca_crl,
(char*) ecdsa_sample_data_real_trustedRootCaCert_pem);
int quote_status = sgxAttestationVerifyQuote(evidence.quote, evidence.quote_len,
(char*) evidence.pck_crt,
(char*) evidence.pck_crl,
(char*) evidence.tcb_info,
(char*) evidence.qe_identity);
int report_user_data_status = verify_report_data_against_server_cert(&crt, (sgx_quote_t*) evidence.quote);
return pck_crt_status || tcb_info_status || qe_identity_status || quote_status || report_user_data_status;
}
#endif
int verify_sgx_cert_extensions
(
uint8_t* der_crt,
uint32_t der_crt_len
)
{
if (is_epid_ratls_cert(der_crt, der_crt_len)) {
return epid_verify_sgx_cert_extensions(der_crt, der_crt_len);
} else {
#ifdef RATLS_ECDSA
return ecdsa_verify_sgx_cert_extensions(der_crt, der_crt_len);
#endif
}
assert(0);
// Avoid compiler error: control reaches end of non-void function
// [-Werror=return-type]
return -1;
}