/
main.c
789 lines (661 loc) · 19.9 KB
/
main.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <openenclave/internal/elf.h>
#include <openenclave/internal/mem.h>
#include <openenclave/internal/properties.h>
#include <openenclave/internal/raise.h>
#include <openenclave/internal/sgxcreate.h>
#include <openenclave/internal/sgxsign.h>
#include <openenclave/internal/str.h>
#include <stdarg.h>
#include <sys/stat.h>
#include "../host/enclave.h"
static const char* arg0;
int oedump(const char*);
int oesign(const char*, const char*, const char*);
OE_PRINTF_FORMAT(1, 2)
void Err(const char* format, ...)
{
fprintf(stderr, "%s: ", arg0);
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fprintf(stderr, "\n");
}
// Replace .so-extension with .signed.so. If there is no .so extension,
// append .signed.so.
static char* _make_signed_lib_name(const char* path)
{
const char* p;
mem_t buf = MEM_DYNAMIC_INIT;
if ((!(p = strrchr(path, '.'))) || (strcmp(p, ".so") != 0))
p = path + strlen(path);
mem_append(&buf, path, p - path);
mem_append(&buf, ".signed.so", 11);
return (char*)mem_steal(&buf);
}
static int _update_and_write_shared_lib(
const char* path,
const oe_sgx_enclave_properties_t* properties)
{
int rc = -1;
oe_enclave_image_t oeimage;
FILE* os = NULL;
/* Open ELF file */
if (oe_load_enclave_image(path, &oeimage) != OE_OK)
{
Err("cannot load ELF file: %s", path);
goto done;
}
// Update or create a new .oeinfo section.
if (oe_sgx_update_enclave_properties(
&oeimage, OE_INFO_SECTION_NAME, properties) != OE_OK)
{
{
Err("section doesn't exist: %s", OE_INFO_SECTION_NAME);
goto done;
}
}
/* Write new shared shared library */
{
char* p = _make_signed_lib_name(path);
if (!p)
{
Err("bad shared library name: %s", path);
goto done;
}
if (!(os = fopen(p, "wb")))
{
Err("failed to open: %s", p);
goto done;
}
if (fwrite(oeimage.u.elf.elf.data, 1, oeimage.u.elf.elf.size, os) !=
oeimage.u.elf.elf.size)
{
Err("failed to write: %s", p);
goto done;
}
fclose(os);
os = NULL;
printf("Created %s\n", p);
free(p);
}
rc = 0;
done:
if (os)
fclose(os);
oeimage.unload(&oeimage);
return rc;
}
// Options loaded from .conf file. Uninitialized fields contain the maximum
// integer value for the corresponding type.
typedef struct _config_file_options
{
bool debug;
uint64_t num_heap_pages;
uint64_t num_stack_pages;
uint64_t num_tcs;
uint16_t product_id;
uint16_t security_version;
} ConfigFileOptions;
#define CONFIG_FILE_OPTIONS_INITIALIZER \
{ \
.debug = false, .num_heap_pages = OE_UINT64_MAX, \
.num_stack_pages = OE_UINT64_MAX, .num_tcs = OE_UINT64_MAX, \
.product_id = OE_UINT16_MAX, .security_version = OE_UINT16_MAX, \
}
/* Check whether the .conf file is missing required options */
static int _check_for_missing_options(const ConfigFileOptions* options)
{
int ret = 0;
if (options->num_heap_pages == OE_UINT64_MAX)
{
Err("%s: missing option: NumHeapPages", arg0);
ret = -1;
}
if (options->num_stack_pages == OE_UINT64_MAX)
{
Err("%s: missing option: NumStackPages", arg0);
ret = -1;
}
if (options->num_tcs == OE_UINT64_MAX)
{
Err("%s: missing option: NumTCS", arg0);
ret = -1;
}
if (options->product_id == OE_UINT16_MAX)
{
Err("%s: missing option: ProductID", arg0);
ret = -1;
}
if (options->security_version == OE_UINT16_MAX)
{
Err("%s: missing option: SecurityVersion", arg0);
ret = -1;
}
return ret;
}
static int _load_config_file(const char* path, ConfigFileOptions* options)
{
int rc = -1;
FILE* is = NULL;
int r;
str_t str = STR_NULL_INIT;
str_t lhs = STR_NULL_INIT;
str_t rhs = STR_NULL_INIT;
size_t line = 1;
if (!(is = fopen(path, "rb")))
goto done;
if (str_dynamic(&str, NULL, 0) != 0)
goto done;
if (str_dynamic(&lhs, NULL, 0) != 0)
goto done;
if (str_dynamic(&rhs, NULL, 0) != 0)
goto done;
for (; (r = str_fgets(&str, is)) == 0; line++)
{
/* Remove leading and trailing whitespace */
str_ltrim(&str, " \t");
str_rtrim(&str, " \t\n\r");
/* Skip comments and empty lines */
if (str_ptr(&str)[0] == '#' || str_len(&str) == 0)
continue;
/* Split string about '=' character */
if (str_split(&str, " \t=", &lhs, &rhs) != 0 || str_len(&lhs) == 0 ||
str_len(&rhs) == 0)
{
Err("%s(%zu): syntax error", path, line);
goto done;
}
/* Handle each setting */
if (strcmp(str_ptr(&lhs), "Debug") == 0)
{
uint64_t value;
// Debug must be 0 or 1
if (str_u64(&rhs, &value) != 0 || (value > 1))
{
Err("%s(%zu): bad value for 'Debug'", path, line);
goto done;
}
options->debug = (bool)value;
}
else if (strcmp(str_ptr(&lhs), "NumHeapPages") == 0)
{
uint64_t n;
if (str_u64(&rhs, &n) != 0 || !oe_sgx_is_valid_num_heap_pages(n))
{
Err("%s(%zu): bad value for 'NumHeapPages'", path, line);
goto done;
}
options->num_heap_pages = n;
}
else if (strcmp(str_ptr(&lhs), "NumStackPages") == 0)
{
uint64_t n;
if (str_u64(&rhs, &n) != 0 || !oe_sgx_is_valid_num_stack_pages(n))
{
Err("%s(%zu): bad value for 'NumStackPages'", path, line);
goto done;
}
options->num_stack_pages = n;
}
else if (strcmp(str_ptr(&lhs), "NumTCS") == 0)
{
uint64_t n;
if (str_u64(&rhs, &n) != 0 || !oe_sgx_is_valid_num_tcs(n))
{
Err("%s(%zu): bad value for 'NumTCS'", path, line);
goto done;
}
options->num_tcs = n;
}
else if (strcmp(str_ptr(&lhs), "ProductID") == 0)
{
uint16_t n;
if (str_u16(&rhs, &n) != 0 || !oe_sgx_is_valid_product_id(n))
{
Err("%s(%zu): bad value for 'ProductID'", path, line);
goto done;
}
options->product_id = n;
}
else if (strcmp(str_ptr(&lhs), "SecurityVersion") == 0)
{
uint16_t n;
if (str_u16(&rhs, &n) != 0 || !oe_sgx_is_valid_security_version(n))
{
Err("%s(%zu): bad value for 'SecurityVersion'", path, line);
goto done;
}
options->security_version = n;
}
else
{
Err("%s(%zu): unknown setting: %s", path, line, str_ptr(&rhs));
goto done;
}
}
rc = 0;
done:
str_free(&str);
str_free(&lhs);
str_free(&rhs);
if (is)
fclose(is);
return rc;
}
static int _load_pem_file(const char* path, void** data, size_t* size)
{
int rc = -1;
FILE* is = NULL;
if (data)
*data = NULL;
if (size)
*size = 0;
/* Check parameters */
if (!path || !data || !size)
goto done;
/* Get size of this file */
{
struct stat st;
if (stat(path, &st) != 0)
goto done;
*size = st.st_size;
}
/* Allocate memory. We add 1 to null terimate the file since the crypto
* libraries require null terminated PEM data. */
if (*size == SIZE_MAX)
goto done;
if (!(*data = (uint8_t*)malloc(*size + 1)))
goto done;
/* Open the file */
if (!(is = fopen(path, "rb")))
goto done;
/* Read file into memory */
if (fread(*data, 1, *size, is) != *size)
goto done;
/* Zero terminate the PEM data. */
{
uint8_t* data_tmp = (uint8_t*)*data;
data_tmp[*size] = 0;
*size += 1;
}
rc = 0;
done:
if (rc != 0)
{
if (data && *data)
{
free(*data);
*data = NULL;
}
if (size)
*size = 0;
}
if (is)
fclose(is);
return rc;
}
// Load the SGX enclave properties from an enclave's .oeinfo section.
static oe_result_t _sgx_load_enclave_properties(
const char* path,
oe_sgx_enclave_properties_t* properties)
{
oe_result_t result = OE_UNEXPECTED;
oe_enclave_image_t oeimage;
/* clear ELF magic */
oeimage.u.elf.elf.magic = 0;
if (properties)
memset(properties, 0, sizeof(oe_sgx_enclave_properties_t));
/* Check parameters */
if (!path || !properties)
OE_RAISE(OE_INVALID_PARAMETER);
/* Load the ELF image */
OE_CHECK(oe_load_enclave_image(path, &oeimage));
/* Load the SGX enclave properties */
OE_CHECK(
oe_sgx_load_enclave_properties(
&oeimage, OE_INFO_SECTION_NAME, properties));
result = OE_OK;
done:
if (oeimage.u.elf.elf.magic == ELF_MAGIC)
oeimage.unload(&oeimage);
return result;
}
/* Merge configuration file options into enclave properties */
void _merge_config_file_options(
oe_sgx_enclave_properties_t* properties,
const char* path,
const ConfigFileOptions* options)
{
bool initialized = false;
/* Determine whether the properties are already initialized */
if (properties->header.size == sizeof(oe_sgx_enclave_properties_t))
initialized = true;
/* Initialize properties if not already initialized */
if (!initialized)
{
properties->header.size = sizeof(oe_sgx_enclave_properties_t);
properties->header.enclave_type = OE_ENCLAVE_TYPE_SGX;
properties->config.attributes = SGX_FLAGS_MODE64BIT;
}
/* Debug option is present */
if (options->debug)
properties->config.attributes |= SGX_FLAGS_DEBUG;
/* If ProductID option is present */
if (options->product_id != OE_UINT16_MAX)
properties->config.product_id = options->product_id;
/* If SecurityVersion option is present */
if (options->security_version != OE_UINT16_MAX)
properties->config.security_version = options->security_version;
/* If NumHeapPages option is present */
if (options->num_heap_pages != OE_UINT64_MAX)
properties->header.size_settings.num_heap_pages =
options->num_heap_pages;
/* If NumStackPages option is present */
if (options->num_stack_pages != OE_UINT64_MAX)
properties->header.size_settings.num_stack_pages =
options->num_stack_pages;
/* If NumTCS option is present */
if (options->num_tcs != OE_UINT64_MAX)
properties->header.size_settings.num_tcs = options->num_tcs;
}
static const char _usage_gen[] =
"Usage: %s <command> [options]\n"
"\n"
"Commands:\n"
" sign - Sign the specified enclave.\n"
" dump - Print out the Open Enclave metadata for the specified "
"enclave.\n"
"\n"
"For help with a specific command, enter \"%s <command> -?\"\n";
static const char _usage_sign[] =
"Usage: %s enclave_image sign config_file key_file\n"
"\n"
"Where:\n"
" enclave_image -- path of an enclave image file\n"
" config_file -- configuration file containing enclave properties\n"
" key_file -- private key file used to digitally sign the image\n"
"\n"
"Description:\n"
" This utility (1) injects runtime properties into an enclave image "
"and\n"
" (2) digitally signs that image.\n"
"\n"
" The properties are read from the <ConfigFile>. They override any\n"
" properties that were already defined inside the enclave image "
"through\n"
" use of the OE_SET_ENCLAVE_SGX macro. These properties include:\n"
"\n"
" Debug - whether enclave debug mode should be enabled (1) or not "
"(0)\n"
" ProductID - the product identified number\n"
" SecurityVersion - the security version number\n"
" NumHeapPages - the number of heap pages for this enclave\n"
" NumStackPages - the number of stack pages for this enclave\n"
" NumTCS - the number of thread control structures for this "
"enclave\n"
"\n"
" The configuration file contains simple NAME=VALUE entries. For "
"example:\n"
"\n"
" Debug=1\n"
" NumHeapPages=1024\n"
"\n"
" The key is read from <KeyFile> and contains a private RSA key in PEM\n"
" format. The keyfile must contain the following header.\n"
"\n"
" -----BEGIN RSA PRIVATE KEY-----\n"
"\n"
" The resulting image is written to <EnclaveImage>.signed.so.\n"
"\n";
static const char _usage_dump[] =
"\n"
"Usage: %s dump enclave_image\n"
"\n"
"Where:\n"
" enclave_image -- path of an enclave image file\n"
"\n"
"Description:\n"
" This option dumps the oeinfo and signature information of an "
"enclave\n";
int oesign(const char* enclave, const char* conffile, const char* keyfile)
{
int ret = 1;
oe_result_t result;
oe_enclave_t enc;
void* pem_data = NULL;
size_t pem_size;
ConfigFileOptions options = CONFIG_FILE_OPTIONS_INITIALIZER;
oe_sgx_enclave_properties_t props;
oe_sgx_load_context_t context;
/* Load the configuration file */
if (_load_config_file(conffile, &options) != 0)
{
Err("failed to load configuration file: %s", conffile);
goto done;
}
/* Load the enclave properties from the enclave */
{
result = _sgx_load_enclave_properties(enclave, &props);
if (result != OE_OK && result != OE_NOT_FOUND)
{
Err("failed to load enclave: %s: result=%s (%u)",
enclave,
oe_result_str(result),
result);
goto done;
}
/* If enclave properties not found, then options must be complete */
if (result == OE_NOT_FOUND)
{
if (_check_for_missing_options(&options) != 0)
goto done;
}
}
/* Merge the configuration file options into the enclave properties */
_merge_config_file_options(&props, conffile, &options);
/* Check whether enclave properties are valid */
{
const char* field_name;
if (oe_sgx_validate_enclave_properties(&props, &field_name) != OE_OK)
{
Err("invalid enclave property value: %s", field_name);
goto done;
}
}
/* Initialize the context parameters for measurement only */
if (oe_sgx_initialize_load_context(
&context, OE_SGX_LOAD_TYPE_MEASURE, props.config.attributes) !=
OE_OK)
{
Err("oe_sgx_initialize_load_context() failed");
goto done;
}
/* Build an enclave to obtain the MRENCLAVE measurement */
if ((result = oe_sgx_build_enclave(&context, enclave, &props, &enc)) !=
OE_OK)
{
Err("oe_sgx_build_enclave(): result=%s (%u)",
oe_result_str(result),
result);
goto done;
}
/* Load private key into memory */
if (_load_pem_file(keyfile, &pem_data, &pem_size) != 0)
{
Err("Failed to load file: %s", keyfile);
goto done;
}
/* Initialize the SigStruct object */
if ((result = oe_sgx_sign_enclave(
&enc.hash,
props.config.attributes,
props.config.product_id,
props.config.security_version,
pem_data,
pem_size,
(sgx_sigstruct_t*)props.sigstruct)) != OE_OK)
{
Err("oe_sgx_sign_enclave() failed: result=%s (%u)",
oe_result_str(result),
result);
goto done;
}
/* Create signature section and write out new file */
if ((result = _update_and_write_shared_lib(enclave, &props)) != OE_OK)
{
Err("_update_and_write_shared_lib(): result=%s (%u)",
oe_result_str(result),
result);
goto done;
}
ret = 0;
done:
if (pem_data)
free(pem_data);
oe_sgx_cleanup_load_context(&context);
return ret;
}
int dump_parser(const char* argv[])
{
int ret = 1;
const char* enclave;
if (strcmp(argv[2], "-?") == 0)
{
fprintf(stderr, _usage_dump, argv[0]);
exit(1);
}
else
{
if (strstr(argv[2], "_enc") != NULL)
{
enclave = argv[2];
/* dump oeinfo and signature information */
ret = oedump(enclave);
}
else
{
fprintf(stderr, _usage_gen, argv[0], argv[0]);
exit(1);
}
}
return ret;
}
int sign_parser(int argc, const char* argv[])
{
int ret = 1;
const char* enclave;
const char* conffile;
const char* keyfile;
if (strcmp(argv[2], "-?") == 0)
{
fprintf(stderr, _usage_sign, argv[0]);
exit(1);
}
else if (argc == 5)
{
if (strstr(argv[2], "enc") != NULL)
{
enclave = argv[2];
if (strstr(argv[3], "conf") != NULL)
{
/* Collect arguments for signing*/
conffile = argv[3];
keyfile = argv[4];
}
else if (strstr(argv[3], "pem") != NULL)
{
/* Collect arguments for signing*/
keyfile = argv[3];
conffile = argv[4];
}
else
{
fprintf(stderr, _usage_gen, argv[0], argv[0]);
exit(1);
}
}
else if (strstr(argv[3], "enc") != NULL)
{
enclave = argv[3];
if (strstr(argv[2], "conf") != NULL)
{
/* Collect arguments for signing*/
conffile = argv[2];
keyfile = argv[4];
}
else if (strstr(argv[2], "pem") != NULL)
{
/* Collect arguments for signing*/
keyfile = argv[2];
conffile = argv[4];
}
else
{
fprintf(stderr, _usage_gen, argv[0], argv[0]);
exit(1);
}
}
else if (strstr(argv[4], "enc") != NULL)
{
enclave = argv[4];
if (strstr(argv[2], "conf") != NULL)
{
/* Collect arguments for signing*/
conffile = argv[2];
keyfile = argv[3];
}
else if (strstr(argv[2], "pem") != NULL)
{
/* Collect arguments for signing*/
keyfile = argv[2];
conffile = argv[3];
}
else
{
fprintf(stderr, _usage_gen, argv[0], argv[0]);
exit(1);
}
}
else
{
fprintf(stderr, _usage_gen, argv[0], argv[0]);
exit(1);
}
}
else
{
fprintf(stderr, _usage_gen, argv[0], argv[0]);
exit(1);
}
ret = oesign(enclave, conffile, keyfile);
return ret;
}
int arg_handler(int argc, const char* argv[])
{
int ret = 1;
if ((strcmp(argv[1], "dump") == 0))
ret = dump_parser(argv);
else if ((strcmp(argv[1], "sign") == 0))
ret = sign_parser(argc, argv);
else
{
fprintf(stderr, _usage_gen, argv[0], argv[0]);
exit(1);
}
return ret;
}
int main(int argc, const char* argv[])
{
arg0 = argv[0];
int ret = 1;
if (argc <= 2)
{
fprintf(stderr, _usage_gen, argv[0], argv[0]);
exit(1);
}
ret = arg_handler(argc, argv);
return ret;
}