This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathsp.cpp
1724 lines (1424 loc) · 46.8 KB
/
sp.cpp
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2019 Intel Corporation
This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you (License). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.
This software and the related documents are provided as is, with no
express or implied warranties, other than those that are expressly stated
in the License.
*/
#ifndef _WIN32
#include "config.h"
#endif
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/types.h>
#ifdef _WIN32
#include <intrin.h>
#include <openssl/applink.c>
#include "win32/getopt.h"
#else
#include <signal.h>
#include <getopt.h>
#include <unistd.h>
#endif
#include <sgx_key_exchange.h>
#include <sgx_report.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include "json.hpp"
#include "common.h"
#include "hexutil.h"
#include "fileio.h"
#include "crypto.h"
#include "byteorder.h"
#include "msgio.h"
#include "protocol.h"
#include "base64.h"
#include "iasrequest.h"
#include "logfile.h"
#include "settings.h"
#include "enclave_verify.h"
using namespace json;
using namespace std;
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
#ifdef _WIN32
#define strdup(x) _strdup(x)
#endif
static const unsigned char def_service_private_key[32] = {
0x90, 0xe7, 0x6c, 0xbb, 0x2d, 0x52, 0xa1, 0xce,
0x3b, 0x66, 0xde, 0x11, 0x43, 0x9c, 0x87, 0xec,
0x1f, 0x86, 0x6a, 0x3b, 0x65, 0xb6, 0xae, 0xea,
0xad, 0x57, 0x34, 0x53, 0xd1, 0x03, 0x8c, 0x01
};
typedef struct ra_session_struct {
unsigned char g_a[64];
unsigned char g_b[64];
unsigned char kdk[16];
unsigned char smk[16];
unsigned char sk[16];
unsigned char mk[16];
unsigned char vk[16];
} ra_session_t;
typedef struct config_struct {
sgx_spid_t spid;
unsigned char pri_subscription_key[IAS_SUBSCRIPTION_KEY_SIZE+1];
unsigned char sec_subscription_key[IAS_SUBSCRIPTION_KEY_SIZE+1];
uint16_t quote_type;
EVP_PKEY *service_private_key;
char *proxy_server;
char *ca_bundle;
char *user_agent;
unsigned int proxy_port;
unsigned char kdk[16];
X509_STORE *store;
X509 *signing_ca;
unsigned int apiver;
int strict_trust;
sgx_measurement_t req_mrsigner;
sgx_prod_id_t req_isv_product_id;
sgx_isv_svn_t min_isvsvn;
int allow_debug_enclave;
} config_t;
void usage();
#ifndef _WIN32
void cleanup_and_exit(int signo);
#endif
int derive_kdk(EVP_PKEY *Gb, unsigned char kdk[16], sgx_ec256_public_t g_a,
config_t *config);
int process_msg01 (MsgIO *msg, IAS_Connection *ias, sgx_ra_msg1_t *msg1,
sgx_ra_msg2_t *msg2, char **sigrl, config_t *config,
ra_session_t *session);
int process_msg3 (MsgIO *msg, IAS_Connection *ias, sgx_ra_msg1_t *msg1,
ra_msg4_t *msg4, config_t *config, ra_session_t *session);
int get_sigrl (IAS_Connection *ias, int version, sgx_epid_group_id_t gid,
char **sigrl, uint32_t *msg2);
int get_attestation_report(IAS_Connection *ias, int version,
const char *b64quote, sgx_ps_sec_prop_desc_t sec_prop, ra_msg4_t *msg4,
int strict_trust);
int get_proxy(char **server, unsigned int *port, const char *url);
char debug = 0;
char verbose = 0;
/* Need a global for the signal handler */
MsgIO *msgio = NULL;
int main(int argc, char *argv[])
{
char flag_spid = 0;
char flag_pubkey = 0;
char flag_api_key = 0;
char flag_ca = 0;
char flag_usage = 0;
char flag_noproxy= 0;
char flag_prod= 0;
char flag_stdio= 0;
char flag_isv_product_id= 0;
char flag_min_isvsvn= 0;
char flag_mrsigner= 0;
char *sigrl = NULL;
config_t config;
int oops;
IAS_Connection *ias= NULL;
char *port= NULL;
#ifndef _WIN32
struct sigaction sact;
#endif
/* Command line options */
static struct option long_opt[] =
{
{"ias-signing-cafile", required_argument, 0, 'A'},
{"ca-bundle", required_argument, 0, 'B'},
{"no-debug-enclave", no_argument, 0, 'D'},
{"list-agents", no_argument, 0, 'G'},
{"ias-pri-api-key-file", required_argument, 0, 'I'},
{"ias-sec-api-key-file", required_argument, 0, 'J'},
{"service-key-file", required_argument, 0, 'K'},
{"mrsigner", required_argument, 0, 'N'},
{"production", no_argument, 0, 'P'},
{"isv-product-id", required_argument, 0, 'R'},
{"spid-file", required_argument, 0, 'S'},
{"min-isv-svn", required_argument, 0, 'V'},
{"strict-trust-mode", no_argument, 0, 'X'},
{"debug", no_argument, 0, 'd'},
{"user-agent", required_argument, 0, 'g'},
{"help", no_argument, 0, 'h'},
{"ias-pri-api-key", required_argument, 0, 'i'},
{"ias-sec-api-key", required_argument, 0, 'j'},
{"key", required_argument, 0, 'k'},
{"linkable", no_argument, 0, 'l'},
{"proxy", required_argument, 0, 'p'},
{"api-version", required_argument, 0, 'r'},
{"spid", required_argument, 0, 's'},
{"verbose", no_argument, 0, 'v'},
{"no-proxy", no_argument, 0, 'x'},
{"stdio", no_argument, 0, 'z'},
{ 0, 0, 0, 0 }
};
/* Create a logfile to capture debug output and actual msg data */
fplog = create_logfile("sp.log");
fprintf(fplog, "Server log started\n");
/* Config defaults */
memset(&config, 0, sizeof(config));
config.apiver= IAS_API_DEF_VERSION;
/*
* For demo purposes only. A production/release enclave should
* never allow debug-mode enclaves to attest.
*/
config.allow_debug_enclave= 1;
/* Parse our options */
while (1) {
int c;
int opt_index = 0;
off_t offset = IAS_SUBSCRIPTION_KEY_SIZE;
int ret = 0;
char *eptr= NULL;
unsigned long val;
c = getopt_long(argc, argv,
"A:B:DGI:J:K:N:PR:S:V:X:dg:hk:lp:r:s:i:j:vxz",
long_opt, &opt_index);
if (c == -1) break;
switch (c) {
case 0:
break;
case 'A':
if (!cert_load_file(&config.signing_ca, optarg)) {
crypto_perror("cert_load_file");
eprintf("%s: could not load IAS Signing Cert CA\n", optarg);
return 1;
}
config.store = cert_init_ca(config.signing_ca);
if (config.store == NULL) {
eprintf("%s: could not initialize certificate store\n", optarg);
return 1;
}
++flag_ca;
break;
case 'B':
config.ca_bundle = strdup(optarg);
if (config.ca_bundle == NULL) {
perror("strdup");
return 1;
}
break;
case 'D':
config.allow_debug_enclave= 0;
break;
case 'G':
ias_list_agents(stdout);
return 1;
case 'I':
// Get Size of File, should be IAS_SUBSCRIPTION_KEY_SIZE + EOF
ret = from_file(NULL, optarg, &offset);
if ((offset != IAS_SUBSCRIPTION_KEY_SIZE+1) || (ret == 0)) {
eprintf("IAS Primary Subscription Key must be %d-byte hex string.\n",
IAS_SUBSCRIPTION_KEY_SIZE);
return 1;
}
// Remove the EOF
offset--;
// Read the contents of the file
if (!from_file((unsigned char *)&config.pri_subscription_key, optarg, &offset)) {
eprintf("IAS Primary Subscription Key must be %d-byte hex string.\n",
IAS_SUBSCRIPTION_KEY_SIZE);
return 1;
}
break;
case 'J':
// Get Size of File, should be IAS_SUBSCRIPTION_KEY_SIZE + EOF
ret = from_file(NULL, optarg, &offset);
if ((offset != IAS_SUBSCRIPTION_KEY_SIZE+1) || (ret == 0)) {
eprintf("IAS Secondary Subscription Key must be %d-byte hex string.\n",
IAS_SUBSCRIPTION_KEY_SIZE);
return 1;
}
// Remove the EOF
offset--;
// Read the contents of the file
if (!from_file((unsigned char *)&config.sec_subscription_key, optarg, &offset)) {
eprintf("IAS Secondary Subscription Key must be %d-byte hex string.\n",
IAS_SUBSCRIPTION_KEY_SIZE);
return 1;
}
break;
case 'K':
if (!key_load_file(&config.service_private_key, optarg, KEY_PRIVATE)) {
crypto_perror("key_load_file");
eprintf("%s: could not load EC private key\n", optarg);
return 1;
}
break;
case 'N':
if (!from_hexstring((unsigned char *)&config.req_mrsigner,
optarg, 32)) {
eprintf("MRSIGNER must be 64-byte hex string\n");
return 1;
}
++flag_mrsigner;
break;
case 'P':
flag_prod = 1;
break;
case 'R':
eptr= NULL;
val= strtoul(optarg, &eptr, 10);
if ( *eptr != '\0' || val > 0xFFFF ) {
eprintf("Product Id must be a positive integer <= 65535\n");
return 1;
}
config.req_isv_product_id= val;
++flag_isv_product_id;
break;
case 'S':
if (!from_hexstring_file((unsigned char *)&config.spid, optarg, 16)) {
eprintf("SPID must be 32-byte hex string\n");
return 1;
}
++flag_spid;
break;
case 'V':
eptr= NULL;
val= strtoul(optarg, &eptr, 10);
if ( *eptr != '\0' || val > (unsigned long) 0xFFFF ) {
eprintf("Minimum ISV SVN must be a positive integer <= 65535\n");
return 1;
}
config.min_isvsvn= val;
++flag_min_isvsvn;
break;
case 'X':
config.strict_trust= 1;
break;
case 'd':
debug = 1;
break;
case 'g':
config.user_agent= strdup(optarg);
if ( config.user_agent == NULL ) {
perror("malloc");
return 1;
}
break;
case 'i':
if (strlen(optarg) != IAS_SUBSCRIPTION_KEY_SIZE) {
eprintf("IAS Subscription Key must be %d-byte hex string\n",IAS_SUBSCRIPTION_KEY_SIZE);
return 1;
}
strncpy((char *) config.pri_subscription_key, optarg, IAS_SUBSCRIPTION_KEY_SIZE);
break;
case 'j':
if (strlen(optarg) != IAS_SUBSCRIPTION_KEY_SIZE) {
eprintf("IAS Secondary Subscription Key must be %d-byte hex string\n",
IAS_SUBSCRIPTION_KEY_SIZE);
return 1;
}
strncpy((char *) config.sec_subscription_key, optarg, IAS_SUBSCRIPTION_KEY_SIZE);
break;
case 'k':
if (!key_load(&config.service_private_key, optarg, KEY_PRIVATE)) {
crypto_perror("key_load");
eprintf("%s: could not load EC private key\n", optarg);
return 1;
}
break;
case 'l':
config.quote_type = SGX_LINKABLE_SIGNATURE;
break;
case 'p':
if ( flag_noproxy ) usage();
if (!get_proxy(&config.proxy_server, &config.proxy_port, optarg)) {
eprintf("%s: could not extract proxy info\n", optarg);
return 1;
}
// Break the URL into host and port. This is a simplistic algorithm.
break;
case 'r':
config.apiver= atoi(optarg);
if ( config.apiver < IAS_MIN_VERSION || config.apiver >
IAS_MAX_VERSION ) {
eprintf("version must be between %d and %d\n",
IAS_MIN_VERSION, IAS_MAX_VERSION);
return 1;
}
break;
case 's':
if (strlen(optarg) < 32) {
eprintf("SPID must be 32-byte hex string\n");
return 1;
}
if (!from_hexstring((unsigned char *)&config.spid, (unsigned char *)optarg, 16)) {
eprintf("SPID must be 32-byte hex string\n");
return 1;
}
++flag_spid;
break;
case 'v':
verbose = 1;
break;
case 'x':
if ( config.proxy_server != NULL ) usage();
flag_noproxy=1;
break;
case 'z':
flag_stdio= 1;
break;
case 'h':
case '?':
default:
usage();
}
}
/* We should have zero or one command-line argument remaining */
argc-= optind;
if ( argc > 1 ) usage();
/* The remaining argument, if present, is the port number. */
if ( flag_stdio && argc ) {
usage();
} else if ( argc ) {
port= argv[optind];
} else {
port= strdup(DEFAULT_PORT);
if ( port == NULL ) {
perror("strdup");
return 1;
}
}
if ( debug ) {
eprintf("+++ IAS Primary Subscription Key set to '%c%c%c%c........................%c%c%c%c'\n",
config.pri_subscription_key[0],
config.pri_subscription_key[1],
config.pri_subscription_key[2],
config.pri_subscription_key[3],
config.pri_subscription_key[IAS_SUBSCRIPTION_KEY_SIZE -4 ],
config.pri_subscription_key[IAS_SUBSCRIPTION_KEY_SIZE -3 ],
config.pri_subscription_key[IAS_SUBSCRIPTION_KEY_SIZE -2 ],
config.pri_subscription_key[IAS_SUBSCRIPTION_KEY_SIZE -1 ]
);
eprintf("+++ IAS Secondary Subscription Key set to '%c%c%c%c........................%c%c%c%c'\n",
config.sec_subscription_key[0],
config.sec_subscription_key[1],
config.sec_subscription_key[2],
config.sec_subscription_key[3],
config.sec_subscription_key[IAS_SUBSCRIPTION_KEY_SIZE -4 ],
config.sec_subscription_key[IAS_SUBSCRIPTION_KEY_SIZE -3 ],
config.sec_subscription_key[IAS_SUBSCRIPTION_KEY_SIZE -2 ],
config.sec_subscription_key[IAS_SUBSCRIPTION_KEY_SIZE -1 ]
);
}
/* Use the default CA bundle unless one is provided */
if ( config.ca_bundle == NULL ) {
config.ca_bundle= strdup(DEFAULT_CA_BUNDLE);
if ( config.ca_bundle == NULL ) {
perror("strdup");
return 1;
}
if ( debug ) eprintf("+++ Using default CA bundle %s\n",
config.ca_bundle);
}
/*
* Use the hardcoded default key unless one is provided on the
* command line. Most real-world services would hardcode the
* key since the public half is also hardcoded into the enclave.
*/
if (config.service_private_key == NULL) {
if (debug) {
eprintf("Using default private key\n");
}
config.service_private_key = key_private_from_bytes(def_service_private_key);
if (config.service_private_key == NULL) {
crypto_perror("key_private_from_bytes");
return 1;
}
}
if (debug) {
eprintf("+++ using private key:\n");
PEM_write_PrivateKey(stderr, config.service_private_key, NULL,
NULL, 0, 0, NULL);
PEM_write_PrivateKey(fplog, config.service_private_key, NULL,
NULL, 0, 0, NULL);
}
if (!flag_spid) {
eprintf("--spid or --spid-file is required\n");
flag_usage = 1;
}
if (!flag_ca) {
eprintf("--ias-signing-cafile is required\n");
flag_usage = 1;
}
if ( ! flag_isv_product_id ) {
eprintf("--isv-product-id is required\n");
flag_usage = 1;
}
if ( ! flag_min_isvsvn ) {
eprintf("--min-isvsvn is required\n");
flag_usage = 1;
}
if ( ! flag_mrsigner ) {
eprintf("--mrsigner is required\n");
flag_usage = 1;
}
if (flag_usage) usage();
/* Initialize out support libraries */
crypto_init();
/* Initialize our IAS request object */
try {
ias = new IAS_Connection(
(flag_prod) ? IAS_SERVER_PRODUCTION : IAS_SERVER_DEVELOPMENT,
0,
(char *)(config.pri_subscription_key),
(char *)(config.sec_subscription_key)
);
}
catch (...) {
oops = 1;
eprintf("exception while creating IAS request object\n");
return 1;
}
if ( flag_noproxy ) ias->proxy_mode(IAS_PROXY_NONE);
else if (config.proxy_server != NULL) {
ias->proxy_mode(IAS_PROXY_FORCE);
ias->proxy(config.proxy_server, config.proxy_port);
}
if ( config.user_agent != NULL ) {
if ( ! ias->agent(config.user_agent) ) {
eprintf("%s: unknown user agent\n", config.user_agent);
return 0;
}
}
/*
* Set the cert store for this connection. This is used for verifying
* the IAS signing certificate, not the TLS connection with IAS (the
* latter is handled using config.ca_bundle).
*/
ias->cert_store(config.store);
/*
* Set the CA bundle for verifying the IAS server certificate used
* for the TLS session. If this isn't set, then the user agent
* will fall back to it's default.
*/
if ( strlen(config.ca_bundle) ) ias->ca_bundle(config.ca_bundle);
/* Get our message IO object. */
if ( flag_stdio ) {
msgio= new MsgIO();
} else {
try {
msgio= new MsgIO(NULL, (port == NULL) ? DEFAULT_PORT : port);
}
catch(...) {
return 1;
}
}
#ifndef _WIN32
/*
* Install some rudimentary signal handlers. We just want to make
* sure we gracefully shutdown the listen socket before we exit
* to avoid "address already in use" errors on startup.
*/
sigemptyset(&sact.sa_mask);
sact.sa_flags= 0;
sact.sa_handler= &cleanup_and_exit;
if ( sigaction(SIGHUP, &sact, NULL) == -1 ) perror("sigaction: SIGHUP");
if ( sigaction(SIGINT, &sact, NULL) == -1 ) perror("sigaction: SIGHUP");
if ( sigaction(SIGTERM, &sact, NULL) == -1 ) perror("sigaction: SIGHUP");
if ( sigaction(SIGQUIT, &sact, NULL) == -1 ) perror("sigaction: SIGHUP");
#endif
/* If we're running in server mode, we'll block here. */
while ( msgio->server_loop() ) {
ra_session_t session;
sgx_ra_msg1_t msg1;
sgx_ra_msg2_t msg2;
ra_msg4_t msg4;
memset(&session, 0, sizeof(ra_session_t));
/* Read message 0 and 1, then generate message 2 */
if ( ! process_msg01(msgio, ias, &msg1, &msg2, &sigrl, &config,
&session) ) {
eprintf("error processing msg1\n");
goto disconnect;
}
/* Send message 2 */
/*
* sgx_ra_msg2_t is a struct with a flexible array member at the
* end (defined as uint8_t sig_rl[]). We could go to all the
* trouble of building a byte array large enough to hold the
* entire struct and then cast it as (sgx_ra_msg2_t) but that's
* a lot of work for no gain when we can just send the fixed
* portion and the array portion by hand.
*/
dividerWithText(stderr, "Copy/Paste Msg2 Below to Client");
dividerWithText(fplog, "Msg2 (send to Client)");
msgio->send_partial((void *) &msg2, sizeof(sgx_ra_msg2_t));
fsend_msg_partial(fplog, (void *) &msg2, sizeof(sgx_ra_msg2_t));
msgio->send(sigrl, msg2.sig_rl_size);
fsend_msg(fplog, sigrl, msg2.sig_rl_size);
edivider();
/* Read message 3, and generate message 4 */
if ( ! process_msg3(msgio, ias, &msg1, &msg4, &config, &session) ) {
eprintf("error processing msg3\n");
goto disconnect;
}
disconnect:
msgio->disconnect();
}
crypto_destroy();
return 0;
}
int process_msg3 (MsgIO *msgio, IAS_Connection *ias, sgx_ra_msg1_t *msg1,
ra_msg4_t *msg4, config_t *config, ra_session_t *session)
{
sgx_ra_msg3_t *msg3;
size_t blen= 0;
size_t sz;
int rv;
uint32_t quote_sz;
char *buffer= NULL;
char *b64quote;
sgx_mac_t vrfymac;
sgx_quote_t *q;
/*
* Read our incoming message. We're using base16 encoding/hex strings
* so we should end up with sizeof(msg)*2 bytes.
*/
fprintf(stderr, "Waiting for msg3\n");
/*
* Read message 3
*
* CMACsmk(M) || M
*
* where
*
* M = ga || PS_SECURITY_PROPERTY || QUOTE
*
*/
rv= msgio->read((void **) &msg3, &sz);
if ( rv == -1 ) {
eprintf("system error reading msg3\n");
return 0;
} else if ( rv == 0 ) {
eprintf("protocol error reading msg3\n");
return 0;
}
if ( debug ) {
eprintf("+++ read %lu bytes\n", sz);
}
/*
* The quote size will be the total msg3 size - sizeof(sgx_ra_msg3_t)
* since msg3.quote is a flexible array member.
*
* Total message size is sz/2 since the income message is in base16.
*/
quote_sz = (uint32_t)((sz / 2) - sizeof(sgx_ra_msg3_t));
if ( debug ) {
eprintf("+++ quote_sz= %lu bytes\n", quote_sz);
}
/* Make sure Ga matches msg1 */
if ( debug ) {
eprintf("+++ Verifying msg3.g_a matches msg1.g_a\n");
eprintf("msg1.g_a.gx = %s\n",
hexstring(&msg1->g_a.gx, sizeof(msg1->g_a.gx)));
eprintf("msg1.g_a.gy = %s\n",
hexstring(&msg1->g_a.gy, sizeof(msg1->g_a.gy)));
eprintf("msg3.g_a.gx = %s\n",
hexstring(&msg3->g_a.gx, sizeof(msg3->g_a.gx)));
eprintf("msg3.g_a.gy = %s\n",
hexstring(&msg3->g_a.gy, sizeof(msg3->g_a.gy)));
}
if ( CRYPTO_memcmp(&msg3->g_a, &msg1->g_a, sizeof(sgx_ec256_public_t)) ) {
eprintf("msg1.g_a and mgs3.g_a keys don't match\n");
free(msg3);
return 0;
}
/* Validate the MAC of M */
cmac128(session->smk, (unsigned char *) &msg3->g_a,
sizeof(sgx_ra_msg3_t)-sizeof(sgx_mac_t)+quote_sz,
(unsigned char *) vrfymac);
if ( debug ) {
eprintf("+++ Validating MACsmk(M)\n");
eprintf("msg3.mac = %s\n", hexstring(msg3->mac, sizeof(sgx_mac_t)));
eprintf("calculated = %s\n", hexstring(vrfymac, sizeof(sgx_mac_t)));
}
if ( CRYPTO_memcmp(msg3->mac, vrfymac, sizeof(sgx_mac_t)) ) {
eprintf("Failed to verify msg3 MAC\n");
free(msg3);
return 0;
}
/* Encode the report body as base64 */
b64quote= base64_encode((char *) &msg3->quote, quote_sz);
if ( b64quote == NULL ) {
eprintf("Could not base64 encode the quote\n");
free(msg3);
return 0;
}
q= (sgx_quote_t *) msg3->quote;
if ( verbose ) {
edividerWithText("Msg3 Details (from Client)");
eprintf("msg3.mac = %s\n",
hexstring(&msg3->mac, sizeof(msg3->mac)));
eprintf("msg3.g_a.gx = %s\n",
hexstring(msg3->g_a.gx, sizeof(msg3->g_a.gx)));
eprintf("msg3.g_a.gy = %s\n",
hexstring(&msg3->g_a.gy, sizeof(msg3->g_a.gy)));
eprintf("msg3.ps_sec_prop = %s\n",
hexstring(&msg3->ps_sec_prop, sizeof(msg3->ps_sec_prop)));
eprintf("msg3.quote.version = %s\n",
hexstring(&q->version, sizeof(uint16_t)));
eprintf("msg3.quote.sign_type = %s\n",
hexstring(&q->sign_type, sizeof(uint16_t)));
eprintf("msg3.quote.epid_group_id = %s\n",
hexstring(&q->epid_group_id, sizeof(sgx_epid_group_id_t)));
eprintf("msg3.quote.qe_svn = %s\n",
hexstring(&q->qe_svn, sizeof(sgx_isv_svn_t)));
eprintf("msg3.quote.pce_svn = %s\n",
hexstring(&q->pce_svn, sizeof(sgx_isv_svn_t)));
eprintf("msg3.quote.xeid = %s\n",
hexstring(&q->xeid, sizeof(uint32_t)));
eprintf("msg3.quote.basename = %s\n",
hexstring(&q->basename, sizeof(sgx_basename_t)));
eprintf("msg3.quote.report_body = %s\n",
hexstring(&q->report_body, sizeof(sgx_report_body_t)));
eprintf("msg3.quote.signature_len = %s\n",
hexstring(&q->signature_len, sizeof(uint32_t)));
eprintf("msg3.quote.signature = %s\n",
hexstring(&q->signature, q->signature_len));
edividerWithText("Enclave Quote (base64) ==> Send to IAS");
eputs(b64quote);
eprintf("\n");
edivider();
}
/* Verify that the EPID group ID in the quote matches the one from msg1 */
if ( debug ) {
eprintf("+++ Validating quote's epid_group_id against msg1\n");
eprintf("msg1.egid = %s\n",
hexstring(msg1->gid, sizeof(sgx_epid_group_id_t)));
eprintf("msg3.quote.epid_group_id = %s\n",
hexstring(&q->epid_group_id, sizeof(sgx_epid_group_id_t)));
}
if ( memcmp(msg1->gid, &q->epid_group_id, sizeof(sgx_epid_group_id_t)) ) {
eprintf("EPID GID mismatch. Attestation failed.\n");
free(b64quote);
free(msg3);
return 0;
}
if ( get_attestation_report(ias, config->apiver, b64quote,
msg3->ps_sec_prop, msg4, config->strict_trust) ) {
unsigned char vfy_rdata[64];
unsigned char msg_rdata[144]; /* for Ga || Gb || VK */
sgx_report_body_t *r= (sgx_report_body_t *) &q->report_body;
memset(vfy_rdata, 0, 64);
/*
* Verify that the first 64 bytes of the report data (inside
* the quote) are SHA256(Ga||Gb||VK) || 0x00[32]
*
* VK = CMACkdk( 0x01 || "VK" || 0x00 || 0x80 || 0x00 )
*
* where || denotes concatenation.
*/
/* Derive VK */
cmac128(session->kdk, (unsigned char *)("\x01VK\x00\x80\x00"),
6, session->vk);
/* Build our plaintext */
memcpy(msg_rdata, session->g_a, 64);
memcpy(&msg_rdata[64], session->g_b, 64);
memcpy(&msg_rdata[128], session->vk, 16);
/* SHA-256 hash */
sha256_digest(msg_rdata, 144, vfy_rdata);
if ( verbose ) {
edividerWithText("Enclave Report Verification");
if ( debug ) {
eprintf("VK = %s\n",
hexstring(session->vk, 16));
}
eprintf("SHA256(Ga||Gb||VK) = %s\n",
hexstring(vfy_rdata, 32));
eprintf("report_data[64] = %s\n",
hexstring(&r->report_data, 64));
}
if ( CRYPTO_memcmp((void *) vfy_rdata, (void *) &r->report_data,
64) ) {
eprintf("Report verification failed.\n");
free(b64quote);
free(msg3);
return 0;
}
/*
* The service provider must validate that the enclave
* report is from an enclave that they recognize. Namely,
* that the MRSIGNER matches our signing key, and the MRENCLAVE
* hash matches an enclave that we compiled.
*
* Other policy decisions might include examining ISV_SVN to
* prevent outdated/deprecated software from successfully
* attesting, and ensuring the TCB is not out of date.
*
* A real-world service provider might allow multiple ISV_SVN
* values, but for this sample we only allow the enclave that
* is compiled.
*/
#ifndef _WIN32
/* Windows implementation is not available yet */
if ( ! verify_enclave_identity(config->req_mrsigner,
config->req_isv_product_id, config->min_isvsvn,
config->allow_debug_enclave, r) ) {
eprintf("Invalid enclave.\n");
msg4->status= NotTrusted;
}
#endif
if ( verbose ) {
edivider();
// The enclave report is valid so we can trust the report
// data.
edividerWithText("Enclave Report Details");
eprintf("cpu_svn = %s\n",
hexstring(&r->cpu_svn, sizeof(sgx_cpu_svn_t)));
eprintf("misc_select = %s\n",
hexstring(&r->misc_select, sizeof(sgx_misc_select_t)));
eprintf("attributes = %s\n",
hexstring(&r->attributes, sizeof(sgx_attributes_t)));
eprintf("mr_enclave = %s\n",
hexstring(&r->mr_enclave, sizeof(sgx_measurement_t)));
eprintf("mr_signer = %s\n",
hexstring(&r->mr_signer, sizeof(sgx_measurement_t)));
eprintf("isv_prod_id = %04hX\n", r->isv_prod_id);
eprintf("isv_svn = %04hX\n", r->isv_svn);
eprintf("report_data = %s\n",
hexstring(&r->report_data, sizeof(sgx_report_data_t)));
}
edividerWithText("Copy/Paste Msg4 Below to Client");
/* Serialize the members of the Msg4 structure independently */
/* vs. the entire structure as one send_msg() */
msgio->send_partial(&msg4->status, sizeof(msg4->status));
msgio->send(&msg4->platformInfoBlob, sizeof(msg4->platformInfoBlob));
fsend_msg_partial(fplog, &msg4->status, sizeof(msg4->status));
fsend_msg(fplog, &msg4->platformInfoBlob,
sizeof(msg4->platformInfoBlob));
edivider();
/*
* If the enclave is trusted, derive the MK and SK. Also get
* SHA256 hashes of these so we can verify there's a shared
* secret between us and the client.
*/
if ( msg4->status == Trusted ) {
unsigned char hashmk[32], hashsk[32];
if ( debug ) eprintf("+++ Deriving the MK and SK\n");
cmac128(session->kdk, (unsigned char *)("\x01MK\x00\x80\x00"),
6, session->mk);
cmac128(session->kdk, (unsigned char *)("\x01SK\x00\x80\x00"),
6, session->sk);
sha256_digest(session->mk, 16, hashmk);
sha256_digest(session->sk, 16, hashsk);
if ( verbose ) {
if ( debug ) {
eprintf("MK = %s\n", hexstring(session->mk, 16));
eprintf("SK = %s\n", hexstring(session->sk, 16));
}
eprintf("SHA256(MK) = %s\n", hexstring(hashmk, 32));
eprintf("SHA256(SK) = %s\n", hexstring(hashsk, 32));