Skip to content

Commit 139f642

Browse files
author
Dmitrii Kuvaiskii
committed
[tools/RA-TLS] Copy quote from X.509 cert into a separate object
Previously, `extract_quote_and_verify_pubkey()` returned a pointer to the SGX quote located inside the X.509 certificate. This is a confusing pattern, so this commit introduces a copy operation, to copy the SGX quote into a newly allocated object whose ownership is passed to the callers of this func. Signed-off-by: Dmitrii Kuvaiskii <[email protected]>
1 parent 72668bb commit 139f642

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

tools/sgx/ra-tls/ra_tls_attest.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ static int generate_x509(mbedtls_pk_context* pk, const uint8_t* quote, size_t qu
133133
goto out;
134134

135135
/* finally, embed the quote into the generated certificate (as X.509 extension) */
136-
ret = mbedtls_x509write_crt_set_extension(writecrt, (const char*)g_quote_oid, g_quote_oid_size,
136+
ret = mbedtls_x509write_crt_set_extension(writecrt, (const char*)g_quote_oid,
137+
sizeof(g_quote_oid),
137138
/*critical=*/0, quote, quote_size);
138139
if (ret < 0)
139140
goto out;

tools/sgx/ra-tls/ra_tls_common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#define OID(N) \
2525
{ 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF8, 0x4D, 0x8A, 0x39, (N) }
2626
static const uint8_t g_quote_oid[] = OID(0x06);
27-
static const size_t g_quote_oid_size = sizeof(g_quote_oid);
2827

2928
bool getenv_allow_outdated_tcb(void);
3029
bool getenv_allow_hw_config_needed(void);

tools/sgx/ra-tls/ra_tls_verify_common.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ int extract_quote_and_verify_pubkey(mbedtls_x509_crt* crt, sgx_quote_t** out_quo
227227
sgx_quote_t* quote;
228228
size_t quote_size;
229229
int ret = find_oid_in_cert_extensions(crt->v3_ext.p, crt->v3_ext.len, g_quote_oid,
230-
g_quote_oid_size, (uint8_t**)&quote, &quote_size);
230+
sizeof(g_quote_oid), (uint8_t**)&quote, &quote_size);
231231
if (ret < 0)
232232
return ret;
233233

@@ -239,7 +239,14 @@ int extract_quote_and_verify_pubkey(mbedtls_x509_crt* crt, sgx_quote_t** out_quo
239239
if (ret < 0)
240240
return ret;
241241

242-
*out_quote = quote;
242+
/* quote returned by find_oid_in_cert_extensions() is a pointer somewhere inside of the X.509
243+
* cert object; let's copy it into a newly allocated object to make tracing ownership easier */
244+
sgx_quote_t* allocated_quote = malloc(quote_size);
245+
if (!allocated_quote)
246+
return MBEDTLS_ERR_X509_ALLOC_FAILED;
247+
memcpy(allocated_quote, quote, quote_size);
248+
249+
*out_quote = allocated_quote;
243250
*out_quote_size = quote_size;
244251
return 0;
245252
}

tools/sgx/ra-tls/ra_tls_verify_dcap.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ int ra_tls_verify_callback(void* data, mbedtls_x509_crt* crt, int depth, uint32_
9999
struct ra_tls_verify_callback_results* results = (struct ra_tls_verify_callback_results*)data;
100100

101101
int ret;
102+
sgx_quote_t* quote = NULL;
102103

103104
uint8_t* supplemental_data = NULL;
104105
uint32_t supplemental_data_size = 0;
@@ -124,7 +125,6 @@ int ra_tls_verify_callback(void* data, mbedtls_x509_crt* crt, int depth, uint32_
124125
results->err_loc = AT_EXTRACT_QUOTE;
125126

126127
/* extract SGX quote from "quote" OID extension from crt */
127-
sgx_quote_t* quote;
128128
size_t quote_size;
129129
ret = extract_quote_and_verify_pubkey(crt, &quote, &quote_size);
130130
if (ret < 0) {
@@ -263,6 +263,7 @@ int ra_tls_verify_callback(void* data, mbedtls_x509_crt* crt, int depth, uint32_
263263
results->err_loc = AT_NONE;
264264
ret = 0;
265265
out:
266+
free(quote);
266267
free(supplemental_data);
267268
return ret;
268269
}

tools/sgx/ra-tls/ra_tls_verify_epid.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ int ra_tls_verify_callback(void* data, mbedtls_x509_crt* crt, int depth, uint32_
121121
struct ra_tls_verify_callback_results* results = (struct ra_tls_verify_callback_results*)data;
122122

123123
int ret;
124+
sgx_quote_t* quote = NULL;
125+
124126
struct ias_context_t* ias = NULL;
125127
char* ias_pub_key_pem = NULL;
126128

@@ -168,7 +170,6 @@ int ra_tls_verify_callback(void* data, mbedtls_x509_crt* crt, int depth, uint32_
168170
results->err_loc = AT_EXTRACT_QUOTE;
169171

170172
/* extract SGX quote from "quote" OID extension from crt */
171-
sgx_quote_t* quote;
172173
size_t quote_size;
173174
ret = extract_quote_and_verify_pubkey(crt, &quote, &quote_size);
174175
if (ret < 0) {
@@ -281,6 +282,7 @@ int ra_tls_verify_callback(void* data, mbedtls_x509_crt* crt, int depth, uint32_
281282
if (ias)
282283
ias_cleanup(ias);
283284

285+
free(quote);
284286
free(ias_pub_key_pem);
285287
free(quote_from_ias);
286288
free(report_data);

0 commit comments

Comments
 (0)