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
/*
Copyright 2018 Intel Corporation
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
typedef int bool;
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#include "sgx_stub.h"
#include "sgx_detect.h"
#include <sgx_edger8r.h>
#include <sgx_uae_service.h>
#include <sgx_urts.h>
typedef void* (*func)();
static const char *dlerr= NULL;
static void _undefined_symbol (const char *symbol);
static void *_load_symbol(void *handle, const char *symbol, int *status);
#include <sys/types.h>
void sgx_oc_cpuidex(int cpuinfo[4], int leaf, int subleaf);
int sgx_thread_wait_untrusted_event_ocall(const void *self);
int sgx_thread_set_untrusted_event_ocall(const void *waiter);
int sgx_thread_setwait_untrusted_events_ocall(const void *waiter, const void *self);
int sgx_thread_set_multiple_untrusted_events_ocall(const void **waiters, size_t total);
;
static void *_load_libsgx_uae_service();
static void *_load_libsgx_urts();
static void *h_libsgx_uae_service= NULL;
static void *h_libsgx_urts= NULL;
static int l_libsgx_uae_service= 0;
static int l_libsgx_urts= 0;
static func p_sgx_thread_wait_untrusted_event_ocall= NULL;
static func p_sgx_report_attestation_status= NULL;
static func p_sgx_destroy_enclave= NULL;
static func p_sgx_create_encrypted_enclave= NULL;
static func p_sgx_thread_setwait_untrusted_events_ocall= NULL;
static func p_sgx_register_wl_cert_chain= NULL;
static func p_sgx_thread_set_multiple_untrusted_events_ocall= NULL;
static func p_sgx_ocall= NULL;
static func p_sgx_get_quote= NULL;
static func p_sgx_create_enclave= NULL;
static func p_sgx_init_quote= NULL;
static func p_sgx_get_whitelist_size= NULL;
static func p_sgx_thread_set_untrusted_event_ocall= NULL;
static func p_sgx_get_ps_cap= NULL;
static func p_sgx_get_whitelist= NULL;
static func p_sgx_oc_cpuidex= NULL;
static func p_sgx_get_extended_epid_group_id= NULL;
static func p_sgx_calc_quote_size= NULL;
static func p_sgx_ecall= NULL;
static func p_sgx_get_quote_size= NULL;
static int l_sgx_thread_wait_untrusted_event_ocall= 0;
static int l_sgx_report_attestation_status= 0;
static int l_sgx_destroy_enclave= 0;
static int l_sgx_create_encrypted_enclave= 0;
static int l_sgx_thread_setwait_untrusted_events_ocall= 0;
static int l_sgx_register_wl_cert_chain= 0;
static int l_sgx_thread_set_multiple_untrusted_events_ocall= 0;
static int l_sgx_ocall= 0;
static int l_sgx_get_quote= 0;
static int l_sgx_create_enclave= 0;
static int l_sgx_init_quote= 0;
static int l_sgx_get_whitelist_size= 0;
static int l_sgx_thread_set_untrusted_event_ocall= 0;
static int l_sgx_get_ps_cap= 0;
static int l_sgx_get_whitelist= 0;
static int l_sgx_oc_cpuidex= 0;
static int l_sgx_get_extended_epid_group_id= 0;
static int l_sgx_calc_quote_size= 0;
static int l_sgx_ecall= 0;
static int l_sgx_get_quote_size= 0;
#ifdef SGX_HW_SIM
#define UAE_SERVICE_LIB "libsgx_uae_service_sim.so"
#define URTS_LIB "libsgx_urts_sim.so"
#else
#define UAE_SERVICE_LIB "libsgx_uae_service.so"
#define URTS_LIB "libsgx_urts.so"
#endif
static void _undefined_symbol (const char *symbol)
{
fprintf(stderr, "%s: %s\n", symbol, dlerr);
exit(1);
}
static void *_load_symbol(void *handle, const char *symbol, int *status)
{
void *hsym;
dlerr= dlerror();
hsym= dlsym(handle, symbol);
dlerr= dlerror();
*status= ( dlerr == NULL ) ? 1 : -1;
return hsym;
}
static void *_load_libsgx_uae_service()
{
if ( l_libsgx_uae_service == 0 ) {
dlerror();
h_libsgx_uae_service= dlopen(UAE_SERVICE_LIB, RTLD_GLOBAL|RTLD_NOW);
if ( h_libsgx_uae_service == NULL ) {
fprintf(stderr, "%s: %s\n", UAE_SERVICE_LIB, dlerror());
l_libsgx_uae_service= -1;
} else l_libsgx_uae_service= 1;
}
return h_libsgx_uae_service;
}
static void *_load_libsgx_urts()
{
if ( l_libsgx_urts == 0 ) {
h_libsgx_urts= dlopen(URTS_LIB, RTLD_GLOBAL|RTLD_NOW);
if ( h_libsgx_urts == NULL ) {
fprintf(stderr, "%s: %s\n", URTS_LIB, dlerror());
l_libsgx_urts= -1;
} else l_libsgx_urts= 1;
}
return h_libsgx_urts;
}
int have_sgx_psw()
{
return (
_load_libsgx_uae_service() == NULL ||
_load_libsgx_urts() == NULL
) ? 0 : 1;
}
int sgx_thread_wait_untrusted_event_ocall(const void *self)
{
if ( l_sgx_thread_wait_untrusted_event_ocall == 0 ) {
if ( h_libsgx_urts == 0 ) _load_libsgx_urts();
*(void **)(&p_sgx_thread_wait_untrusted_event_ocall)= _load_symbol(h_libsgx_urts, "sgx_thread_wait_untrusted_event_ocall", &l_sgx_thread_wait_untrusted_event_ocall);
}
if ( l_sgx_thread_wait_untrusted_event_ocall == -1 ) _undefined_symbol("sgx_thread_wait_untrusted_event_ocall");
return (int) p_sgx_thread_wait_untrusted_event_ocall(self);
}
sgx_status_t sgx_report_attestation_status(const sgx_platform_info_t *p_platform_info, int attestation_status, sgx_update_info_bit_t *p_update_info)
{
if ( l_sgx_report_attestation_status == 0 ) {
if ( h_libsgx_uae_service == 0 ) _load_libsgx_uae_service();
*(void **)(&p_sgx_report_attestation_status)= _load_symbol(h_libsgx_uae_service, "sgx_report_attestation_status", &l_sgx_report_attestation_status);
}
if ( l_sgx_report_attestation_status == -1 ) _undefined_symbol("sgx_report_attestation_status");
return (sgx_status_t) p_sgx_report_attestation_status(p_platform_info, attestation_status, p_update_info);
}
sgx_status_t sgx_destroy_enclave(const sgx_enclave_id_t enclave_id)
{
if ( l_sgx_destroy_enclave == 0 ) {
if ( h_libsgx_urts == 0 ) _load_libsgx_urts();
*(void **)(&p_sgx_destroy_enclave)= _load_symbol(h_libsgx_urts, "sgx_destroy_enclave", &l_sgx_destroy_enclave);
}
if ( l_sgx_destroy_enclave == -1 ) _undefined_symbol("sgx_destroy_enclave");
return (sgx_status_t) p_sgx_destroy_enclave(enclave_id);
}
sgx_status_t sgx_create_encrypted_enclave(const char *file_name, const int debug, sgx_launch_token_t *launch_token, int *launch_token_updated, sgx_enclave_id_t *enclave_id, sgx_misc_attribute_t *misc_attr, uint8_t *sealed_key)
{
if ( l_sgx_create_encrypted_enclave == 0 ) {
if ( h_libsgx_urts == 0 ) _load_libsgx_urts();
*(void **)(&p_sgx_create_encrypted_enclave)= _load_symbol(h_libsgx_urts, "sgx_create_encrypted_enclave", &l_sgx_create_encrypted_enclave);
}
if ( l_sgx_create_encrypted_enclave == -1 ) _undefined_symbol("sgx_create_encrypted_enclave");
return (sgx_status_t) p_sgx_create_encrypted_enclave(file_name, debug, launch_token, launch_token_updated, enclave_id, misc_attr, sealed_key);
}
int sgx_thread_setwait_untrusted_events_ocall(const void *waiter, const void *self)
{
if ( l_sgx_thread_setwait_untrusted_events_ocall == 0 ) {
if ( h_libsgx_urts == 0 ) _load_libsgx_urts();
*(void **)(&p_sgx_thread_setwait_untrusted_events_ocall)= _load_symbol(h_libsgx_urts, "sgx_thread_setwait_untrusted_events_ocall", &l_sgx_thread_setwait_untrusted_events_ocall);
}
if ( l_sgx_thread_setwait_untrusted_events_ocall == -1 ) _undefined_symbol("sgx_thread_setwait_untrusted_events_ocall");
return (int) p_sgx_thread_setwait_untrusted_events_ocall(waiter, self);
}
sgx_status_t sgx_register_wl_cert_chain(uint8_t *p_wl_cert_chain, uint32_t wl_cert_chain_size)
{
if ( l_sgx_register_wl_cert_chain == 0 ) {
if ( h_libsgx_uae_service == 0 ) _load_libsgx_uae_service();
*(void **)(&p_sgx_register_wl_cert_chain)= _load_symbol(h_libsgx_uae_service, "sgx_register_wl_cert_chain", &l_sgx_register_wl_cert_chain);
}
if ( l_sgx_register_wl_cert_chain == -1 ) _undefined_symbol("sgx_register_wl_cert_chain");
return (sgx_status_t) p_sgx_register_wl_cert_chain(p_wl_cert_chain, wl_cert_chain_size);
}
int sgx_thread_set_multiple_untrusted_events_ocall(const void **waiters, size_t total)
{
if ( l_sgx_thread_set_multiple_untrusted_events_ocall == 0 ) {
if ( h_libsgx_urts == 0 ) _load_libsgx_urts();
*(void **)(&p_sgx_thread_set_multiple_untrusted_events_ocall)= _load_symbol(h_libsgx_urts, "sgx_thread_set_multiple_untrusted_events_ocall", &l_sgx_thread_set_multiple_untrusted_events_ocall);
}
if ( l_sgx_thread_set_multiple_untrusted_events_ocall == -1 ) _undefined_symbol("sgx_thread_set_multiple_untrusted_events_ocall");
return (int) p_sgx_thread_set_multiple_untrusted_events_ocall(waiters, total);
}
sgx_status_t sgx_ocall(const unsigned int index, void *ms)
{
if ( l_sgx_ocall == 0 ) {
if ( h_libsgx_urts == 0 ) _load_libsgx_urts();
*(void **)(&p_sgx_ocall)= _load_symbol(h_libsgx_urts, "sgx_ocall", &l_sgx_ocall);
}
if ( l_sgx_ocall == -1 ) _undefined_symbol("sgx_ocall");
return (sgx_status_t) p_sgx_ocall(index, ms);
}
sgx_status_t sgx_get_quote(const sgx_report_t *p_report, sgx_quote_sign_type_t quote_type, const sgx_spid_t *p_spid, const sgx_quote_nonce_t *p_nonce, const uint8_t *p_sig_rl, uint32_t sig_rl_size, sgx_report_t *p_qe_report, sgx_quote_t *p_quote, uint32_t quote_size)
{
if ( l_sgx_get_quote == 0 ) {
if ( h_libsgx_uae_service == 0 ) _load_libsgx_uae_service();
*(void **)(&p_sgx_get_quote)= _load_symbol(h_libsgx_uae_service, "sgx_get_quote", &l_sgx_get_quote);
}
if ( l_sgx_get_quote == -1 ) _undefined_symbol("sgx_get_quote");
return (sgx_status_t) p_sgx_get_quote(p_report, quote_type, p_spid, p_nonce, p_sig_rl, sig_rl_size, p_qe_report, p_quote, quote_size);
}
sgx_status_t sgx_create_enclave(const char *file_name, const int debug, sgx_launch_token_t *launch_token, int *launch_token_updated, sgx_enclave_id_t *enclave_id, sgx_misc_attribute_t *misc_attr)
{
if ( l_sgx_create_enclave == 0 ) {
if ( h_libsgx_urts == 0 ) _load_libsgx_urts();
*(void **)(&p_sgx_create_enclave)= _load_symbol(h_libsgx_urts, "sgx_create_enclave", &l_sgx_create_enclave);
}
if ( l_sgx_create_enclave == -1 ) _undefined_symbol("sgx_create_enclave");
return (sgx_status_t) p_sgx_create_enclave(file_name, debug, launch_token, launch_token_updated, enclave_id, misc_attr);
}
sgx_status_t sgx_init_quote(sgx_target_info_t *p_target_info, sgx_epid_group_id_t *p_gid)
{
if ( l_sgx_init_quote == 0 ) {
if ( h_libsgx_uae_service == 0 ) _load_libsgx_uae_service();
*(void **)(&p_sgx_init_quote)= _load_symbol(h_libsgx_uae_service, "sgx_init_quote", &l_sgx_init_quote);
}
if ( l_sgx_init_quote == -1 ) _undefined_symbol("sgx_init_quote");
return (sgx_status_t) p_sgx_init_quote(p_target_info, p_gid);
}
sgx_status_t sgx_get_whitelist_size(uint32_t *p_whitelist_size)
{
if ( l_sgx_get_whitelist_size == 0 ) {
if ( h_libsgx_uae_service == 0 ) _load_libsgx_uae_service();
*(void **)(&p_sgx_get_whitelist_size)= _load_symbol(h_libsgx_uae_service, "sgx_get_whitelist_size", &l_sgx_get_whitelist_size);
}
if ( l_sgx_get_whitelist_size == -1 ) _undefined_symbol("sgx_get_whitelist_size");
return (sgx_status_t) p_sgx_get_whitelist_size(p_whitelist_size);
}
int sgx_thread_set_untrusted_event_ocall(const void *waiter)
{
if ( l_sgx_thread_set_untrusted_event_ocall == 0 ) {
if ( h_libsgx_urts == 0 ) _load_libsgx_urts();
*(void **)(&p_sgx_thread_set_untrusted_event_ocall)= _load_symbol(h_libsgx_urts, "sgx_thread_set_untrusted_event_ocall", &l_sgx_thread_set_untrusted_event_ocall);
}
if ( l_sgx_thread_set_untrusted_event_ocall == -1 ) _undefined_symbol("sgx_thread_set_untrusted_event_ocall");
return (int) p_sgx_thread_set_untrusted_event_ocall(waiter);
}
sgx_status_t sgx_get_ps_cap(sgx_ps_cap_t *p_sgx_ps_cap)
{
if ( l_sgx_get_ps_cap == 0 ) {
if ( h_libsgx_uae_service == 0 ) _load_libsgx_uae_service();
*(void **)(&p_sgx_get_ps_cap)= _load_symbol(h_libsgx_uae_service, "sgx_get_ps_cap", &l_sgx_get_ps_cap);
}
if ( l_sgx_get_ps_cap == -1 ) _undefined_symbol("sgx_get_ps_cap");
return (sgx_status_t) p_sgx_get_ps_cap(p_sgx_ps_cap);
}
sgx_status_t sgx_get_whitelist(uint8_t *p_whitelist, uint32_t whitelist_size)
{
if ( l_sgx_get_whitelist == 0 ) {
if ( h_libsgx_uae_service == 0 ) _load_libsgx_uae_service();
*(void **)(&p_sgx_get_whitelist)= _load_symbol(h_libsgx_uae_service, "sgx_get_whitelist", &l_sgx_get_whitelist);
}
if ( l_sgx_get_whitelist == -1 ) _undefined_symbol("sgx_get_whitelist");
return (sgx_status_t) p_sgx_get_whitelist(p_whitelist, whitelist_size);
}
void sgx_oc_cpuidex(int *cpuinfo, int leaf, int subleaf)
{
if ( l_sgx_oc_cpuidex == 0 ) {
if ( h_libsgx_urts == 0 ) _load_libsgx_urts();
*(void **)(&p_sgx_oc_cpuidex)= _load_symbol(h_libsgx_urts, "sgx_oc_cpuidex", &l_sgx_oc_cpuidex);
}
if ( l_sgx_oc_cpuidex == -1 ) _undefined_symbol("sgx_oc_cpuidex");
return (void) p_sgx_oc_cpuidex(cpuinfo, leaf, subleaf);
}
sgx_status_t sgx_get_extended_epid_group_id(uint32_t *p_extended_epid_group_id)
{
if ( l_sgx_get_extended_epid_group_id == 0 ) {
if ( h_libsgx_uae_service == 0 ) _load_libsgx_uae_service();
*(void **)(&p_sgx_get_extended_epid_group_id)= _load_symbol(h_libsgx_uae_service, "sgx_get_extended_epid_group_id", &l_sgx_get_extended_epid_group_id);
}
if ( l_sgx_get_extended_epid_group_id == -1 ) _undefined_symbol("sgx_get_extended_epid_group_id");
return (sgx_status_t) p_sgx_get_extended_epid_group_id(p_extended_epid_group_id);
}
sgx_status_t sgx_calc_quote_size(const uint8_t *p_sig_rl, uint32_t sig_rl_size, uint32_t *p_quote_size)
{
if ( l_sgx_calc_quote_size == 0 ) {
if ( h_libsgx_uae_service == 0 ) _load_libsgx_uae_service();
*(void **)(&p_sgx_calc_quote_size)= _load_symbol(h_libsgx_uae_service, "sgx_calc_quote_size", &l_sgx_calc_quote_size);
}
if ( l_sgx_calc_quote_size == -1 ) _undefined_symbol("sgx_calc_quote_size");
return (sgx_status_t) p_sgx_calc_quote_size(p_sig_rl, sig_rl_size, p_quote_size);
}
sgx_status_t sgx_ecall(const sgx_enclave_id_t eid, const int index, const void *ocall_table, void *ms)
{
if ( l_sgx_ecall == 0 ) {
if ( h_libsgx_urts == 0 ) _load_libsgx_urts();
*(void **)(&p_sgx_ecall)= _load_symbol(h_libsgx_urts, "sgx_ecall", &l_sgx_ecall);
}
if ( l_sgx_ecall == -1 ) _undefined_symbol("sgx_ecall");
return (sgx_status_t) p_sgx_ecall(eid, index, ocall_table, ms);
}
sgx_status_t sgx_get_quote_size(const uint8_t *p_sig_rl, uint32_t *p_quote_size)
{
if ( l_sgx_get_quote_size == 0 ) {
if ( h_libsgx_uae_service == 0 ) _load_libsgx_uae_service();
*(void **)(&p_sgx_get_quote_size)= _load_symbol(h_libsgx_uae_service, "sgx_get_quote_size", &l_sgx_get_quote_size);
}
if ( l_sgx_get_quote_size == -1 ) _undefined_symbol("sgx_get_quote_size");
return (sgx_status_t) p_sgx_get_quote_size(p_sig_rl, p_quote_size);
}