-
Notifications
You must be signed in to change notification settings - Fork 202
/
ra_tls_attest.c
656 lines (542 loc) · 21.3 KB
/
ra_tls_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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2020 Intel Labs */
/*!
* \file
*
* This file contains the implementation of server-side attestation for TLS libraries. It contains
* functions to create a self-signed RA-TLS certificate with an SGX quote embedded in it. It works
* with both EPID-based (quote v2) and ECDSA-based (quote v3 or DCAP) SGX quotes (in fact, it is
* agnostic to the format of the SGX quote).
*
* This file is part of the RA-TLS attestation library which is typically linked into server
* applications. This library is *not* thread-safe.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <cbor.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/ecp.h>
#include <mbedtls/entropy.h>
#include <mbedtls/pk.h>
#include <mbedtls/sha256.h>
#include <mbedtls/x509_crt.h>
#include "ra_tls.h"
#include "ra_tls_common.h"
#define CERT_SUBJECT_NAME_VALUES "CN=RATLS,O=GramineDevelopers,C=US"
#define CERT_TIMESTAMP_NOT_BEFORE_DEFAULT "20010101000000"
#define CERT_TIMESTAMP_NOT_AFTER_DEFAULT "20301231235959"
static ssize_t rw_file(const char* path, uint8_t* buf, size_t len, bool do_write) {
ssize_t bytes = 0;
ssize_t ret = 0;
int fd = open(path, do_write ? O_WRONLY : O_RDONLY);
if (fd < 0)
return fd;
while ((ssize_t)len > bytes) {
if (do_write)
ret = write(fd, buf + bytes, len - bytes);
else
ret = read(fd, buf + bytes, len - bytes);
if (ret > 0) {
bytes += ret;
} else if (ret == 0) {
/* end of file */
break;
} else {
if (ret < 0 && (errno == EAGAIN || errno == EINTR))
continue;
break;
}
}
close(fd);
return ret < 0 ? ret : bytes;
}
/*! given public key \p pk, generate an RA-TLS certificate \p writecrt with \p quote (legacy format)
* and \p evidence (new standard format) embedded */
static int generate_x509(mbedtls_pk_context* pk, const uint8_t* quote, size_t quote_size,
const uint8_t* evidence, size_t evidence_size,
mbedtls_x509write_cert* writecrt) {
int ret;
char* cert_timestamp_not_before = NULL;
char* cert_timestamp_not_after = NULL;
mbedtls_mpi serial;
mbedtls_mpi_init(&serial);
mbedtls_x509write_crt_init(writecrt);
mbedtls_x509write_crt_set_md_alg(writecrt, MBEDTLS_MD_SHA256);
/* generated certificate is self-signed, so declares itself both a subject and an issuer */
mbedtls_x509write_crt_set_subject_key(writecrt, pk);
mbedtls_x509write_crt_set_issuer_key(writecrt, pk);
/* set (dummy) subject names for both subject and issuer */
ret = mbedtls_x509write_crt_set_subject_name(writecrt, CERT_SUBJECT_NAME_VALUES);
if (ret < 0)
goto out;
ret = mbedtls_x509write_crt_set_issuer_name(writecrt, CERT_SUBJECT_NAME_VALUES);
if (ret < 0)
goto out;
/* set a serial number (dummy "1") for the generated certificate */
ret = mbedtls_mpi_read_string(&serial, 10, "1");
if (ret < 0)
goto out;
ret = mbedtls_x509write_crt_set_serial(writecrt, &serial);
if (ret < 0)
goto out;
cert_timestamp_not_before = strdup(getenv(RA_TLS_CERT_TIMESTAMP_NOT_BEFORE) ? :
CERT_TIMESTAMP_NOT_BEFORE_DEFAULT);
if (!cert_timestamp_not_before) {
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
goto out;
}
cert_timestamp_not_after = strdup(getenv(RA_TLS_CERT_TIMESTAMP_NOT_AFTER) ? :
CERT_TIMESTAMP_NOT_AFTER_DEFAULT);
if (!cert_timestamp_not_after) {
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
goto out;
}
ret = mbedtls_x509write_crt_set_validity(writecrt, cert_timestamp_not_before,
cert_timestamp_not_after);
if (ret < 0)
goto out;
ret = mbedtls_x509write_crt_set_basic_constraints(writecrt, /*is_ca=*/0, /*max_pathlen=*/-1);
if (ret < 0)
goto out;
ret = mbedtls_x509write_crt_set_subject_key_identifier(writecrt);
if (ret < 0)
goto out;
ret = mbedtls_x509write_crt_set_authority_key_identifier(writecrt);
if (ret < 0)
goto out;
/* embed the SGX quote into the generated certificate (as X.509 extension) in two formats:
* - legacy non-standard "SGX quote" OID (used from Gramine v1.0)
* - new standard TCG DICE "tagged evidence" OID (2.23.133.5.4.9)
*/
ret = mbedtls_x509write_crt_set_extension(writecrt, (const char*)g_quote_oid,
sizeof(g_quote_oid), /*critical=*/0, quote,
quote_size);
if (ret < 0)
goto out;
ret = mbedtls_x509write_crt_set_extension(writecrt, (const char*)g_evidence_oid,
sizeof(g_evidence_oid), /*critical=*/0, evidence,
evidence_size);
if (ret < 0)
goto out;
ret = 0;
out:
free(cert_timestamp_not_before);
free(cert_timestamp_not_after);
mbedtls_mpi_free(&serial);
return ret;
}
/*! calculate sha256 over public key \p pk and copy it into \p sha */
static int sha256_over_pk(mbedtls_pk_context* pk, uint8_t* sha) {
uint8_t pk_der[PUB_KEY_SIZE_MAX] = {0};
/* below function writes data at the end of the buffer */
int pk_der_size_byte = mbedtls_pk_write_pubkey_der(pk, pk_der, sizeof(pk_der));
if (pk_der_size_byte < 0)
return pk_der_size_byte;
/* move the data to the beginning of the buffer, to avoid pointer arithmetic later */
memmove(pk_der, pk_der + PUB_KEY_SIZE_MAX - pk_der_size_byte, pk_der_size_byte);
return mbedtls_sha256(pk_der, pk_der_size_byte, sha, /*is224=*/0);
}
/*! generate SGX quote with user_report_data equal to SHA256 hash over \p pk (legacy format) */
static int generate_quote_with_pk_hash(mbedtls_pk_context* pk, uint8_t** out_quote,
size_t* out_quote_size) {
sgx_report_data_t user_report_data = {0};
int ret = sha256_over_pk(pk, user_report_data.d);
if (ret < 0)
return ret;
ssize_t written = rw_file("/dev/attestation/user_report_data", user_report_data.d,
sizeof(user_report_data.d), /*do_write=*/true);
if (written != sizeof(user_report_data))
return MBEDTLS_ERR_X509_FILE_IO_ERROR;
uint8_t* quote = malloc(SGX_QUOTE_MAX_SIZE);
if (!quote)
return MBEDTLS_ERR_X509_ALLOC_FAILED;
ssize_t quote_size = rw_file("/dev/attestation/quote", quote, SGX_QUOTE_MAX_SIZE,
/*do_write=*/false);
if (quote_size < 0) {
free(quote);
return MBEDTLS_ERR_X509_FILE_IO_ERROR;
}
*out_quote = quote;
*out_quote_size = (size_t)quote_size;
return 0;
}
/*! create CBOR bstr from SHA256 hash of public key \p pk and copy it into \p out_cbor_bstr */
static int cbor_bstr_from_pk_sha256(mbedtls_pk_context* pk, cbor_item_t** out_cbor_bstr) {
uint8_t sha256[SHA256_DIGEST_SIZE] = {0};
int ret = sha256_over_pk(pk, sha256);
if (ret < 0)
return ret;
cbor_item_t* cbor_bstr = cbor_build_bytestring(sha256, sizeof(sha256));
if (!cbor_bstr)
return MBEDTLS_ERR_X509_ALLOC_FAILED;
*out_cbor_bstr = cbor_bstr;
return 0;
}
/*! generate hash-entry -- CBOR array with [ hash-alg-id, hash-value -- hash of pubkey ] */
static int generate_serialized_pk_hash_entry(mbedtls_pk_context* pk, uint8_t** out_hash_entry_buf,
size_t* out_hash_entry_buf_size) {
/* the hash-entry array as defined in Concise Software Identification Tags (CoSWID) */
cbor_item_t* cbor_hash_entry = cbor_new_definite_array(2);
if (!cbor_hash_entry)
return MBEDTLS_ERR_X509_ALLOC_FAILED;
/* RA-TLS always generates SHA256 hash over pubkey */
cbor_item_t* cbor_hash_alg_id = cbor_build_uint8(IANA_NAMED_INFO_HASH_ALG_REGISTRY_SHA256);
if (!cbor_hash_alg_id) {
cbor_decref(&cbor_hash_entry);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
cbor_item_t* cbor_hash_value;
int ret = cbor_bstr_from_pk_sha256(pk, &cbor_hash_value);
if (ret < 0) {
cbor_decref(&cbor_hash_alg_id);
cbor_decref(&cbor_hash_entry);
return ret;
}
int bool_ret = cbor_array_push(cbor_hash_entry, cbor_hash_alg_id);
if (!bool_ret) {
cbor_decref(&cbor_hash_value);
cbor_decref(&cbor_hash_alg_id);
cbor_decref(&cbor_hash_entry);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
bool_ret = cbor_array_push(cbor_hash_entry, cbor_hash_value);
if (!bool_ret) {
cbor_decref(&cbor_hash_value);
cbor_decref(&cbor_hash_alg_id);
cbor_decref(&cbor_hash_entry);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
/* cbor_hash_entry took ownership of hash_alg_id and hash_value cbor items */
cbor_decref(&cbor_hash_alg_id);
cbor_decref(&cbor_hash_value);
uint8_t* hash_entry_buf;
size_t hash_entry_buf_size;
cbor_serialize_alloc(cbor_hash_entry, &hash_entry_buf, &hash_entry_buf_size);
cbor_decref(&cbor_hash_entry);
if (!hash_entry_buf)
return MBEDTLS_ERR_X509_ALLOC_FAILED;
*out_hash_entry_buf = hash_entry_buf;
*out_hash_entry_buf_size = hash_entry_buf_size;
return 0;
}
/*! generate claims -- CBOR map with { "pubkey-hash" = <serialized CBOR array hash-entry> } */
static int generate_serialized_claims(mbedtls_pk_context* pk, uint8_t** out_claims_buf,
size_t* out_claims_buf_size) {
/* TODO: currently, only claim "pubkey-hash" is implemented, but in the future there may be more
* (e.g. "nonce") */
cbor_item_t* cbor_claims = cbor_new_definite_map(1);
if (!cbor_claims)
return MBEDTLS_ERR_X509_ALLOC_FAILED;
cbor_item_t* cbor_pubkey_hash_key = cbor_build_string("pubkey-hash");
if (!cbor_pubkey_hash_key) {
cbor_decref(&cbor_claims);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
uint8_t* hash_entry_buf;
size_t hash_entry_buf_size;
int ret = generate_serialized_pk_hash_entry(pk, &hash_entry_buf, &hash_entry_buf_size);
if (ret < 0) {
cbor_decref(&cbor_pubkey_hash_key);
cbor_decref(&cbor_claims);
return ret;
}
cbor_item_t* cbor_pubkey_hash_val = cbor_build_bytestring(hash_entry_buf, hash_entry_buf_size);
free(hash_entry_buf);
if (!cbor_pubkey_hash_val) {
cbor_decref(&cbor_pubkey_hash_key);
cbor_decref(&cbor_claims);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
struct cbor_pair cbor_pubkey_hash_pair = { .key = cbor_pubkey_hash_key,
.value = cbor_pubkey_hash_val };
bool bool_ret = cbor_map_add(cbor_claims, cbor_pubkey_hash_pair);
if (!bool_ret) {
cbor_decref(&cbor_pubkey_hash_val);
cbor_decref(&cbor_pubkey_hash_key);
cbor_decref(&cbor_claims);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
uint8_t* claims_buf;
size_t claims_buf_size;
cbor_serialize_alloc(cbor_claims, &claims_buf, &claims_buf_size);
cbor_decref(&cbor_pubkey_hash_val);
cbor_decref(&cbor_pubkey_hash_key);
cbor_decref(&cbor_claims);
if (!claims_buf)
return MBEDTLS_ERR_X509_ALLOC_FAILED;
*out_claims_buf = claims_buf;
*out_claims_buf_size = claims_buf_size;
return 0;
}
/*! generate evidence -- CBOR tag with CBOR array of CBOR bstrs: [ quote, claims ] */
static int generate_serialized_evidence(uint8_t* quote, size_t quote_size, uint8_t* claims,
size_t claims_size, uint8_t** out_evidence_buf,
size_t* out_evidence_buf_size) {
cbor_item_t* cbor_evidence = cbor_new_definite_array(2);
if (!cbor_evidence)
return MBEDTLS_ERR_X509_ALLOC_FAILED;
cbor_item_t* cbor_quote = cbor_build_bytestring(quote, quote_size);
if (!cbor_quote) {
cbor_decref(&cbor_evidence);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
cbor_item_t* cbor_claims = cbor_build_bytestring(claims, claims_size);
if (!cbor_claims) {
cbor_decref(&cbor_quote);
cbor_decref(&cbor_evidence);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
int bool_ret = cbor_array_push(cbor_evidence, cbor_quote);
if (!bool_ret) {
cbor_decref(&cbor_claims);
cbor_decref(&cbor_quote);
cbor_decref(&cbor_evidence);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
bool_ret = cbor_array_push(cbor_evidence, cbor_claims);
if (!bool_ret) {
cbor_decref(&cbor_claims);
cbor_decref(&cbor_quote);
cbor_decref(&cbor_evidence);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
/* cbor_evidence took ownership of quote and claims cbor bstrs */
cbor_decref(&cbor_claims);
cbor_decref(&cbor_quote);
cbor_item_t* cbor_tagged_evidence = cbor_new_tag(TCG_DICE_TAGGED_EVIDENCE_TEE_QUOTE_CBOR_TAG);
if (!cbor_tagged_evidence) {
cbor_decref(&cbor_evidence);
return MBEDTLS_ERR_X509_ALLOC_FAILED;
}
cbor_tag_set_item(cbor_tagged_evidence, cbor_evidence);
uint8_t* evidence_buf;
size_t evidence_buf_size;
cbor_serialize_alloc(cbor_tagged_evidence, &evidence_buf, &evidence_buf_size);
cbor_decref(&cbor_evidence);
cbor_decref(&cbor_tagged_evidence);
if (!evidence_buf)
return MBEDTLS_ERR_X509_ALLOC_FAILED;
*out_evidence_buf = evidence_buf;
*out_evidence_buf_size = evidence_buf_size;
return 0;
}
/*! generate SGX-quote evidence with \p pk as one of the embedded claims (standard format) */
static int generate_evidence_with_claims(mbedtls_pk_context* pk, uint8_t** out_evidence,
size_t* out_evidence_size) {
/*
* SGX-quote evidence has the following serialized-CBOR format:
*
* CBOR object (major type 6, new CBOR tag for "ECDSA SGX Quotes") ->
* CBOR array ->
* [
* 0: CBOR bstr (SGX quote with user_report_data = hash(serialized-cbor-map of claims)),
* 1: CBOR bstr (serialized-cbor-map of claims)
* ]
*
* where "serialized-cbor-map of claims" is as follows:
*
* CBOR map ->
* {
* "pubkey-hash" (req) : CBOR bstr (serialized-cbor-array hash-entry),
* "nonce" (opt) : CBOR bstr (arbitrary-sized nonce for per-session freshness)
* }
*
* where "serialized-cbor-array hash-entry" is as follows:
*
* CBOR array ->
* [
* 0: CBOR uint (hash-alg-id),
* 1: CBOR bstr (hash of DER-formatted "SubjectPublicKeyInfo" field as CBOR bstr)
* ]
*
* For hash-alg-id values, see
* https://www.iana.org/assignments/named-information/named-information.xhtml
*/
uint8_t* claims = NULL;
uint8_t* quote = NULL;
uint8_t* evidence = NULL;
size_t claims_size;
int ret = generate_serialized_claims(pk, &claims, &claims_size);
if (ret < 0)
goto out;
sgx_report_data_t user_report_data = {0};
ret = mbedtls_sha256(claims, claims_size, user_report_data.d, /*is224=*/0);
if (ret < 0)
goto out;
ssize_t written = rw_file("/dev/attestation/user_report_data", user_report_data.d,
sizeof(user_report_data.d), /*do_write=*/true);
if (written != sizeof(user_report_data)) {
ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
goto out;
}
quote = malloc(SGX_QUOTE_MAX_SIZE);
if (!quote) {
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
goto out;
}
ssize_t quote_size = rw_file("/dev/attestation/quote", quote, SGX_QUOTE_MAX_SIZE,
/*do_write=*/false);
if (quote_size < 0) {
ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
goto out;
}
size_t evidence_size;
ret = generate_serialized_evidence(quote, quote_size, claims, claims_size, &evidence,
&evidence_size);
if (ret < 0)
goto out;
*out_evidence = evidence;
*out_evidence_size = (size_t)evidence_size;
ret = 0;
out:
free(quote);
free(claims);
return ret;
}
/*! given public key \p pk, generate an RA-TLS certificate \p writecrt */
static int create_x509(mbedtls_pk_context* pk, mbedtls_x509write_cert* writecrt) {
int ret;
/*
* We put both "legacy Gramine" OID with plain SGX quote as well as standardized TCG DICE "tagged
* evidence" OID with CBOR-formatted SGX quote into the RA-TLS X.509 cert. This is for keeping
* backward compatibility at the price of a larger size of the resulting cert. */
uint8_t* quote = NULL;
uint8_t* evidence = NULL;
/* TODO: this legacy OID with plain SGX quote should be removed at some point */
size_t quote_size;
ret = generate_quote_with_pk_hash(pk, "e, "e_size);
if (ret < 0)
goto out;
size_t evidence_size;
ret = generate_evidence_with_claims(pk, &evidence, &evidence_size);
if (ret < 0)
goto out;
/* TODO: currently, the Endorsement extension is not implemented (contains TCB info, CRL, etc.);
* should be added in the future */
ret = generate_x509(pk, quote, quote_size, evidence, evidence_size, writecrt);
out:
free(quote);
free(evidence);
return ret;
}
static int create_key_and_crt(mbedtls_pk_context* key, mbedtls_x509_crt* crt, uint8_t** crt_der,
size_t* crt_der_size) {
int ret;
if (!key || (!crt && !(crt_der && crt_der_size))) {
/* mbedTLS API (ra_tls_create_key_and_crt) and generic API (ra_tls_create_key_and_crt_der)
* both use `key`, but the former uses `crt` and the latter uses `crt_der` */
return MBEDTLS_ERR_X509_FATAL_ERROR;
}
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_entropy_context entropy;
mbedtls_entropy_init(&entropy);
mbedtls_x509write_cert writecrt;
mbedtls_x509write_crt_init(&writecrt);
uint8_t* crt_der_buf = NULL;
uint8_t* output_buf = NULL;
size_t output_buf_size = 16 * 1024; /* enough for any X.509 certificate */
output_buf = malloc(output_buf_size);
if (!output_buf) {
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
goto out;
}
ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, /*custom=*/NULL,
/*customlen=*/0);
if (ret < 0)
goto out;
ret = mbedtls_pk_setup(key, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
if (ret < 0)
goto out;
ret = mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP384R1, mbedtls_pk_ec(*key), mbedtls_ctr_drbg_random,
&ctr_drbg);
if (ret < 0)
goto out;
ret = create_x509(key, &writecrt);
if (ret < 0)
goto out;
int size = mbedtls_x509write_crt_der(&writecrt, output_buf, output_buf_size,
mbedtls_ctr_drbg_random, &ctr_drbg);
if (size < 0) {
ret = size;
goto out;
}
if (crt_der && crt_der_size) {
crt_der_buf = malloc(size);
if (!crt_der_buf) {
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
goto out;
}
/* note that mbedtls_x509write_crt_der() wrote data at the end of the output_buf */
memcpy(crt_der_buf, output_buf + output_buf_size - size, size);
*crt_der = crt_der_buf;
*crt_der_size = size;
}
if (crt) {
ret = mbedtls_x509_crt_parse_der(crt, output_buf + output_buf_size - size, size);
if (ret < 0)
goto out;
}
ret = 0;
out:
if (ret < 0) {
free(crt_der_buf);
}
mbedtls_x509write_crt_free(&writecrt);
mbedtls_entropy_free(&entropy);
mbedtls_ctr_drbg_free(&ctr_drbg);
free(output_buf);
return ret;
}
int ra_tls_create_key_and_crt(mbedtls_pk_context* key, mbedtls_x509_crt* crt) {
return create_key_and_crt(key, crt, NULL, NULL);
}
int ra_tls_create_key_and_crt_der(uint8_t** der_key, size_t* der_key_size, uint8_t** der_crt,
size_t* der_crt_size) {
int ret;
if (!der_key || !der_key_size || !der_crt || !der_crt_size)
return -EINVAL;
mbedtls_pk_context key;
mbedtls_pk_init(&key);
uint8_t* der_key_buf = NULL;
uint8_t* output_buf = NULL;
size_t output_buf_size = 4096; /* enough for any public key in DER format */
output_buf = malloc(output_buf_size);
if (!output_buf) {
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
goto out;
}
ret = create_key_and_crt(&key, NULL, der_crt, der_crt_size);
if (ret < 0) {
goto out;
}
/* populate der_key; note that der_crt was already populated */
int size = mbedtls_pk_write_key_der(&key, output_buf, output_buf_size);
if (size < 0) {
ret = size;
goto out;
}
der_key_buf = malloc(size);
if (!der_key_buf) {
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
goto out;
}
/* note that mbedtls_pk_write_key_der() wrote data at the end of the output_buf */
memcpy(der_key_buf, output_buf + output_buf_size - size, size);
*der_key = der_key_buf;
*der_key_size = size;
ret = 0;
out:
if (ret < 0) {
free(der_key_buf);
}
mbedtls_pk_free(&key);
free(output_buf);
return ret;
}