enclave_framework.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <pal_linux.h>
  4. #include <pal_internal.h>
  5. #include <pal_debug.h>
  6. #include <pal_security.h>
  7. #include <pal_crypto.h>
  8. #include <api.h>
  9. #include <list.h>
  10. #include "enclave_pages.h"
  11. struct pal_enclave_state pal_enclave_state;
  12. void * enclave_base, * enclave_top;
  13. struct pal_enclave_config pal_enclave_config;
  14. static int register_trusted_file (const char * uri, const char * checksum_str);
  15. bool sgx_is_within_enclave (const void * addr, uint64_t size)
  16. {
  17. return enclave_base <= addr && addr + size <= enclave_top;
  18. }
  19. void * sgx_ocalloc (uint64_t size)
  20. {
  21. void * ustack = GET_ENCLAVE_TLS(ustack) - size;
  22. SET_ENCLAVE_TLS(ustack, ustack);
  23. return ustack;
  24. }
  25. void sgx_ocfree (void)
  26. {
  27. SET_ENCLAVE_TLS(ustack, GET_ENCLAVE_TLS(ustack_top));
  28. }
  29. #define HASHBUF_SIZE ((sizeof(sgx_arch_hash_t)*2)+1)
  30. int sgx_get_report (sgx_arch_hash_t * mrenclave,
  31. sgx_arch_attributes_t * attributes,
  32. void * enclave_data,
  33. sgx_arch_report_t * report)
  34. {
  35. sgx_arch_targetinfo_t targetinfo;
  36. memset(&targetinfo, 0, sizeof(sgx_arch_targetinfo_t));
  37. memcpy(targetinfo.mrenclave, mrenclave, sizeof(sgx_arch_hash_t));
  38. memcpy(&targetinfo.attributes, attributes, sizeof(sgx_arch_attributes_t));
  39. struct pal_enclave_state state;
  40. memcpy(&state, &pal_enclave_state, sizeof(struct pal_enclave_state));
  41. memcpy(&state.data, enclave_data, PAL_ATTESTATION_DATA_SIZE);
  42. int ret = sgx_report(&targetinfo, &state, report);
  43. if (ret)
  44. return -PAL_ERROR_DENIED;
  45. char hash_buf[HASHBUF_SIZE];
  46. char mac_buf[MACBUF_SIZE];
  47. SGX_DBG(DBG_S, "Generated report:\n");
  48. SGX_DBG(DBG_S, " cpusvn: %08x %08x\n", report->cpusvn[0],
  49. report->cpusvn[1]);
  50. SGX_DBG(DBG_S, " mrenclave: %s\n", bytes2hexstr(report->mrenclave, hash_buf, HASHBUF_SIZE));
  51. SGX_DBG(DBG_S, " mrsigner: %s\n", bytes2hexstr(report->mrsigner, hash_buf, HASHBUF_SIZE));
  52. SGX_DBG(DBG_S, " attributes.flags: %016lx\n", report->attributes.flags);
  53. SGX_DBG(DBG_S, " sttributes.xfrm: %016lx\n", report->attributes.xfrm);
  54. SGX_DBG(DBG_S, " isvprodid: %02x\n", report->isvprodid);
  55. SGX_DBG(DBG_S, " isvsvn: %02x\n", report->isvsvn);
  56. SGX_DBG(DBG_S, " keyid: %s\n", bytes2hexstr(report->keyid, hash_buf, HASHBUF_SIZE));
  57. SGX_DBG(DBG_S, " mac: %s\n", bytes2hexstr(report->mac, mac_buf, MACBUF_SIZE));
  58. return 0;
  59. }
  60. static sgx_arch_key128_t enclave_key;
  61. #define KEYBUF_SIZE ((sizeof(sgx_arch_key128_t) * 2) + 1)
  62. int sgx_verify_report (sgx_arch_report_t * report)
  63. {
  64. sgx_arch_keyrequest_t keyrequest;
  65. memset(&keyrequest, 0, sizeof(sgx_arch_keyrequest_t));
  66. keyrequest.keyname = REPORT_KEY;
  67. memcpy(keyrequest.keyid, report->keyid, sizeof(keyrequest.keyid));
  68. int ret = sgx_getkey(&keyrequest, &enclave_key);
  69. if (ret) {
  70. SGX_DBG(DBG_S, "Can't get report key\n");
  71. return -PAL_ERROR_DENIED;
  72. }
  73. char key_buf[KEYBUF_SIZE];
  74. SGX_DBG(DBG_S, "Get report key for verification: %s\n", bytes2hexstr(enclave_key, key_buf, KEYBUF_SIZE));
  75. return 0;
  76. }
  77. int init_enclave_key (void)
  78. {
  79. sgx_arch_keyrequest_t keyrequest;
  80. memset(&keyrequest, 0, sizeof(sgx_arch_keyrequest_t));
  81. keyrequest.keyname = SEAL_KEY;
  82. int ret = sgx_getkey(&keyrequest, &enclave_key);
  83. if (ret) {
  84. SGX_DBG(DBG_S, "Can't get report key\n");
  85. return -PAL_ERROR_DENIED;
  86. }
  87. char key_buf[KEYBUF_SIZE];
  88. SGX_DBG(DBG_S, "Get sealing key: %s\n", bytes2hexstr(enclave_key, key_buf, KEYBUF_SIZE));
  89. return 0;
  90. }
  91. /*
  92. * The file integrity check is designed as follow:
  93. *
  94. * For each file that requires authentication (specified in the manifest
  95. * as "sgx.trusted_files.xxx"), a SHA256 checksum is generated and stored
  96. * in the manifest, signed and verified as part of the enclave's crypto
  97. * measurement. When user requests for opening the file, Graphene loads
  98. * the whole file, generate the SHA256 checksum, and check with the known
  99. * checksums listed in the manifest. If the checksum does not match, and
  100. * neither does the file is allowed for unauthenticated access, the file
  101. * access will be rejected.
  102. *
  103. * During the generation of the SHA256 checksum, a 128-bit hash is also
  104. * generated for each chunk in the file. The per-chunk hashes are used
  105. * for partial verification in future reads, to avoid re-verifying the
  106. * whole file again or the need of caching file contents. The per-chunk
  107. * hashes are stored as "stubs" for each file. For a performance reason,
  108. * each per-chunk hash is a 128-bit AES-CMAC hash value, using a secret
  109. * key generated at the beginning of the enclave.
  110. */
  111. DEFINE_LIST(trusted_file);
  112. struct trusted_file {
  113. LIST_TYPE(trusted_file) list;
  114. int64_t index;
  115. uint64_t size;
  116. int uri_len;
  117. char uri[URI_MAX];
  118. sgx_checksum_t checksum;
  119. sgx_stub_t * stubs;
  120. };
  121. DEFINE_LISTP(trusted_file);
  122. static LISTP_TYPE(trusted_file) trusted_file_list = LISTP_INIT;
  123. static struct spinlock trusted_file_lock = LOCK_INIT;
  124. static int trusted_file_indexes = 0;
  125. static int allow_file_creation = 0;
  126. /*
  127. * 'load_trusted_file' checks if the file to be opened is trusted
  128. * or allowed for unauthenticated access, according to the manifest.
  129. *
  130. * file: file handle to be opened
  131. * stubptr: buffer for catching matched file stub.
  132. * sizeptr: size pointer
  133. * create: this file is newly created or not
  134. *
  135. * return: 0 succeed
  136. */
  137. int load_trusted_file (PAL_HANDLE file, sgx_stub_t ** stubptr,
  138. uint64_t * sizeptr, int create)
  139. {
  140. struct trusted_file * tf = NULL, * tmp;
  141. char uri[URI_MAX];
  142. char normpath[URI_MAX];
  143. int ret, fd = file->file.fd, uri_len, len;
  144. if (!(HANDLE_HDR(file)->flags & RFD(0)))
  145. return -PAL_ERROR_DENIED;
  146. uri_len = _DkStreamGetName(file, uri, URI_MAX);
  147. if (uri_len < 0)
  148. return uri_len;
  149. /* Allow to create the file when allow_file_creation is turned on;
  150. The created file is added to allowed_file list for later access */
  151. if (create && allow_file_creation) {
  152. register_trusted_file(uri, NULL);
  153. *sizeptr = 0;
  154. return 0;
  155. }
  156. /* Normalize the uri */
  157. if (!strpartcmp_static(uri, "file:")) {
  158. SGX_DBG(DBG_E, "Invalid URI [%s]: Trusted files must start with 'file:'\n", uri);;
  159. return -PAL_ERROR_INVAL;
  160. }
  161. normpath [0] = 'f';
  162. normpath [1] = 'i';
  163. normpath [2] = 'l';
  164. normpath [3] = 'e';
  165. normpath [4] = ':';
  166. len = get_norm_path(uri + 5, normpath + 5, 0, URI_MAX);
  167. uri_len = len + 5;
  168. _DkSpinLock(&trusted_file_lock);
  169. listp_for_each_entry(tmp, &trusted_file_list, list) {
  170. if (tmp->stubs) {
  171. /* trusted files: must be exactly the same URI */
  172. if (tmp->uri_len == uri_len && !memcmp(tmp->uri, normpath, uri_len + 1)) {
  173. tf = tmp;
  174. break;
  175. }
  176. } else {
  177. /* allowed files: must be a subfolder or file */
  178. if (tmp->uri_len <= uri_len &&
  179. !memcmp(tmp->uri, normpath, tmp->uri_len) &&
  180. (!normpath[tmp->uri_len] || normpath[tmp->uri_len] == '/')) {
  181. tf = tmp;
  182. break;
  183. }
  184. }
  185. }
  186. _DkSpinUnlock(&trusted_file_lock);
  187. if (!tf)
  188. return -PAL_ERROR_DENIED;
  189. if (tf->index < 0)
  190. return tf->index;
  191. #if CACHE_FILE_STUBS == 1
  192. if (tf->index && tf->stubs) {
  193. *stubptr = tf->stubs;
  194. *sizeptr = tf->size;
  195. return 0;
  196. }
  197. #endif
  198. if (!tf->index) {
  199. *stubptr = NULL;
  200. PAL_STREAM_ATTR attr;
  201. ret = _DkStreamAttributesQuery(normpath, &attr);
  202. if (!ret)
  203. *sizeptr = attr.pending_size;
  204. else
  205. *sizeptr = 0;
  206. return 0;
  207. }
  208. int nstubs = tf->size / TRUSTED_STUB_SIZE +
  209. (tf->size % TRUSTED_STUB_SIZE ? 1 : 0);
  210. sgx_stub_t * stubs = malloc(sizeof(sgx_stub_t) * nstubs);
  211. if (!stubs)
  212. return -PAL_ERROR_NOMEM;
  213. sgx_stub_t * s = stubs; /* stubs is an array of 128bit values */
  214. uint64_t offset = 0;
  215. LIB_SHA256_CONTEXT sha;
  216. void * umem;
  217. ret = lib_SHA256Init(&sha);
  218. if (ret < 0)
  219. goto failed;
  220. for (; offset < tf->size ; offset += TRUSTED_STUB_SIZE, s++) {
  221. /* For each stub, generate a 128bit hash of a file chunk with
  222. * AES-CMAC, and then update the SHA256 digest. */
  223. uint64_t mapping_size = MIN(tf->size - offset, TRUSTED_STUB_SIZE);
  224. LIB_AESCMAC_CONTEXT aes_cmac;
  225. ret = lib_AESCMACInit(&aes_cmac, (uint8_t *) &enclave_key,
  226. AES_CMAC_KEY_LEN);
  227. if (ret < 0)
  228. goto failed;
  229. ret = ocall_map_untrusted(fd, offset, mapping_size, PROT_READ, &umem);
  230. if (ret < 0)
  231. goto unmap;
  232. /*
  233. * To prevent TOCTOU attack when generating the file checksum, we
  234. * need to copy the file content into the enclave before hashing.
  235. * For optimization, we use a relatively small buffer (1024 byte) to
  236. * store the data for checksum generation.
  237. */
  238. #define FILE_CHUNK_SIZE 1024
  239. uint8_t small_chunk[FILE_CHUNK_SIZE]; /* Buffer for hashing */
  240. int chunk_offset = 0;
  241. for (; chunk_offset < mapping_size; chunk_offset += FILE_CHUNK_SIZE) {
  242. uint64_t chunk_size = MIN(mapping_size - chunk_offset, FILE_CHUNK_SIZE);
  243. /* Any file content needs to be copied into the enclave before
  244. * checking and re-hashing */
  245. memcpy(small_chunk, umem + chunk_offset, chunk_size);
  246. /* Update the file checksum */
  247. ret = lib_SHA256Update(&sha, small_chunk, chunk_size);
  248. if (ret < 0)
  249. goto unmap;
  250. /* Update the checksum for the file chunk */
  251. ret = lib_AESCMACUpdate(&aes_cmac, small_chunk, chunk_size);
  252. if (ret < 0)
  253. goto unmap;
  254. }
  255. /* Store the checksum for one file chunk for checking */
  256. ret = lib_AESCMACFinish(&aes_cmac, (uint8_t *) s, sizeof *s);
  257. unmap:
  258. ocall_unmap_untrusted(umem, mapping_size);
  259. if (ret < 0)
  260. goto failed;
  261. }
  262. sgx_checksum_t hash;
  263. /* Finalize and checking if the checksum of the whole file matches
  264. * with record given in the manifest. */
  265. ret = lib_SHA256Final(&sha, (uint8_t *) hash.bytes);
  266. if (ret < 0)
  267. goto failed;
  268. if (memcmp(&hash, &tf->checksum, sizeof(sgx_checksum_t))) {
  269. ret = -PAL_ERROR_DENIED;
  270. goto failed;
  271. }
  272. _DkSpinLock(&trusted_file_lock);
  273. if (tf->stubs || tf->index == -PAL_ERROR_DENIED)
  274. free(tf->stubs);
  275. *stubptr = tf->stubs = stubs;
  276. *sizeptr = tf->size;
  277. ret = tf->index;
  278. _DkSpinUnlock(&trusted_file_lock);
  279. return ret;
  280. failed:
  281. free(stubs);
  282. _DkSpinLock(&trusted_file_lock);
  283. if (tf->stubs) {
  284. *stubptr = tf->stubs;
  285. *sizeptr = tf->size;
  286. ret = tf->index;
  287. } else {
  288. tf->index = -PAL_ERROR_DENIED;
  289. }
  290. _DkSpinUnlock(&trusted_file_lock);
  291. #if PRINT_ENCLAVE_STAT
  292. if (!ret) {
  293. sgx_stub_t * loaded_stub;
  294. uint64_t loaded_size;
  295. PAL_HANDLE handle = NULL;
  296. if (!_DkStreamOpen(&handle, normpath, PAL_ACCESS_RDONLY, 0, 0, 0))
  297. load_trusted_file (handle, &loaded_stub, &loaded_size);
  298. }
  299. #endif
  300. return ret;
  301. }
  302. /*
  303. * A common helper function for copying and checking the file contents
  304. * from a buffer mapped outside the enclaves into an in-enclave buffer.
  305. * If needed, regions at either the beginning or the end of the copied regions
  306. * are copied into a scratch buffer to avoid a TOCTTOU race.
  307. *
  308. * * Note that it must be done this way to avoid the following TOCTTOU race
  309. * * condition with the untrusted host as an adversary:
  310. * * Adversary: put good contents in buffer
  311. * * Enclave: buffer check passes
  312. * * Adversary: put bad contents in buffer
  313. * * Enclave: copies in bad buffer contents
  314. *
  315. * * For optimization, we verify the memory in place, as the application code
  316. * should not use the memory before return. There can be subtle interactions
  317. * at the edges of a region with ELF loading. Namely, the ELF loader will
  318. * want to map several file chunks that are not aligned to TRUSTED_STUB_SIZE
  319. * next to each other, sometimes overlapping. There is probably room to
  320. * improve load time with more smarts around ELF loading, but for now, just
  321. * make things work.
  322. *
  323. * 'umem' is the untrusted file memory mapped outside the enclave (should
  324. * already be mapped up by the caller). 'umem_start' and 'umem_end' are
  325. * the offset _within the file_ of 'umem'. 'umem_start' should be aligned
  326. * to the file checking chunk size (TRUSTED_STUB_SIZE). 'umem_end' can be
  327. * either aligned, or equal to 'total_size'. 'buffer' is the in-enclave
  328. * buffer for copying the file content. 'offset' is the offset within the file
  329. * for copying into the buffer. 'size' is the size of the in-enclave buffer.
  330. * 'stubs' contain the checksums of all the chunks in a file.
  331. */
  332. int copy_and_verify_trusted_file (const char * path, const void * umem,
  333. uint64_t umem_start, uint64_t umem_end,
  334. void * buffer, uint64_t offset, uint64_t size,
  335. sgx_stub_t * stubs, uint64_t total_size)
  336. {
  337. /* Check that the untrusted mapping is aligned to TRUSTED_STUB_SIZE
  338. * and includes the range for copying into the buffer */
  339. assert(umem_start % TRUSTED_STUB_SIZE == 0);
  340. assert(offset >= umem_start && offset + size <= umem_end);
  341. /* Start copying and checking at umem_start. The checked content may or
  342. * may not be copied into the file content, depending on the offset of
  343. * the content within the file. */
  344. uint64_t checking = umem_start;
  345. /* The stubs is an array of 128-bit hash values of the file chunks.
  346. * from the beginning of the file. 's' points to the stub that needs to
  347. * be checked for the current offset. */
  348. sgx_stub_t * s = stubs + checking / TRUSTED_STUB_SIZE;
  349. int ret = 0;
  350. for (; checking < umem_end ; checking += TRUSTED_STUB_SIZE, s++) {
  351. /* Check one chunk at a time. */
  352. uint64_t checking_size = MIN(total_size - checking, TRUSTED_STUB_SIZE);
  353. uint64_t checking_end = checking + checking_size;
  354. uint8_t hash[AES_CMAC_DIGEST_LEN];
  355. if (checking >= offset && checking_end <= offset + size) {
  356. /* If the checking chunk completely overlaps with the region
  357. * needed for copying into the buffer, simplying use the buffer
  358. * for checking */
  359. memcpy(buffer + checking - offset, umem + checking - umem_start,
  360. checking_size);
  361. /* Storing the checksum (using AES-CMAC) inside hash. */
  362. ret = lib_AESCMAC((uint8_t *) &enclave_key,
  363. AES_CMAC_KEY_LEN,
  364. buffer + checking - offset, checking_size,
  365. hash, sizeof(hash));
  366. } else {
  367. /* If the checking chunk only partially overlaps with the region,
  368. * read the file content in smaller chunks and only copy the part
  369. * needed by the caller. */
  370. LIB_AESCMAC_CONTEXT aes_cmac;
  371. ret = lib_AESCMACInit(&aes_cmac, (uint8_t *) &enclave_key,
  372. AES_CMAC_KEY_LEN);
  373. if (ret < 0)
  374. goto failed;
  375. uint8_t small_chunk[FILE_CHUNK_SIZE]; /* A small buffer */
  376. uint64_t chunk_offset = checking;
  377. for (; chunk_offset < checking_end
  378. ; chunk_offset += FILE_CHUNK_SIZE) {
  379. uint64_t chunk_size = MIN(checking_end - chunk_offset,
  380. FILE_CHUNK_SIZE);
  381. /* Copy into the small buffer before hashing the content */
  382. memcpy(small_chunk, umem + (chunk_offset - umem_start),
  383. chunk_size);
  384. /* Update the hash for the current chunk */
  385. ret = lib_AESCMACUpdate(&aes_cmac, small_chunk, chunk_size);
  386. if (ret < 0)
  387. goto failed;
  388. /* Determine if the part just copied and checked is needed
  389. * by the caller. If so, copy it into the user buffer. */
  390. uint64_t copy_start = chunk_offset;
  391. uint64_t copy_end = copy_start + chunk_size;
  392. if (copy_start < offset)
  393. copy_start = offset;
  394. if (copy_end > offset + size)
  395. copy_end = offset + size;
  396. if (copy_end > copy_start)
  397. memcpy(buffer + (copy_start - offset),
  398. small_chunk + (copy_start - chunk_offset),
  399. copy_end - copy_start);
  400. }
  401. /* Storing the checksum (using AES-CMAC) inside hash. */
  402. ret = lib_AESCMACFinish(&aes_cmac, hash, sizeof(hash));
  403. }
  404. if (ret < 0)
  405. goto failed;
  406. /*
  407. * Check if the hash matches with the checksum of current chunk.
  408. * If not, return with access denied. Note: some file content may
  409. * still be in the buffer (including the corrupted part).
  410. * We assume the user won't use the content if this function
  411. * returns with failures.
  412. *
  413. * XXX: Maybe we should zero the buffer after denying the access?
  414. */
  415. if (memcmp(s, hash, sizeof(sgx_stub_t))) {
  416. SGX_DBG(DBG_E, "Accesing file:%s is denied. Does not match with MAC"
  417. " at chunk starting at %llu-%llu.\n",
  418. path, checking, checking_end);
  419. return -PAL_ERROR_DENIED;
  420. }
  421. }
  422. return 0;
  423. failed:
  424. return -PAL_ERROR_DENIED;
  425. }
  426. static int register_trusted_file (const char * uri, const char * checksum_str)
  427. {
  428. struct trusted_file * tf = NULL, * new;
  429. int uri_len = strlen(uri);
  430. int ret;
  431. _DkSpinLock(&trusted_file_lock);
  432. listp_for_each_entry(tf, &trusted_file_list, list) {
  433. if (tf->uri_len == uri_len && !memcmp(tf->uri, uri, uri_len)) {
  434. _DkSpinUnlock(&trusted_file_lock);
  435. return 0;
  436. }
  437. }
  438. _DkSpinUnlock(&trusted_file_lock);
  439. new = malloc(sizeof(struct trusted_file));
  440. if (!new)
  441. return -PAL_ERROR_NOMEM;
  442. INIT_LIST_HEAD(new, list);
  443. new->uri_len = uri_len;
  444. memcpy(new->uri, uri, uri_len + 1);
  445. new->size = 0;
  446. new->stubs = NULL;
  447. if (checksum_str) {
  448. PAL_STREAM_ATTR attr;
  449. ret = _DkStreamAttributesQuery(uri, &attr);
  450. if (!ret)
  451. new->size = attr.pending_size;
  452. char checksum_text[sizeof(sgx_checksum_t) * 2 + 1] = "\0";
  453. int nbytes = 0;
  454. for (; nbytes < sizeof(sgx_checksum_t) ; nbytes++) {
  455. char byte1 = checksum_str[nbytes * 2];
  456. char byte2 = checksum_str[nbytes * 2 + 1];
  457. unsigned char val = 0;
  458. if (byte1 == 0 || byte2 == 0) {
  459. break;
  460. }
  461. if (!(byte1 >= '0' && byte1 <= '9') &&
  462. !(byte1 >= 'a' && byte1 <= 'f')) {
  463. break;
  464. }
  465. if (!(byte2 >= '0' && byte2 <= '9') &&
  466. !(byte2 >= 'a' && byte2 <= 'f')) {
  467. break;
  468. }
  469. if (byte1 >= '0' && byte1 <= '9')
  470. val = byte1 - '0';
  471. if (byte1 >= 'a' && byte1 <= 'f')
  472. val = byte1 - 'a' + 10;
  473. val *= 16;
  474. if (byte2 >= '0' && byte2 <= '9')
  475. val += byte2 - '0';
  476. if (byte2 >= 'a' && byte2 <= 'f')
  477. val += byte2 - 'a' + 10;
  478. new->checksum.bytes[nbytes] = val;
  479. snprintf(checksum_text + nbytes * 2, 3, "%02x", val);
  480. }
  481. if (nbytes < sizeof(sgx_checksum_t)) {
  482. free(new);
  483. return -PAL_ERROR_INVAL;
  484. }
  485. new->index = (++trusted_file_indexes);
  486. SGX_DBG(DBG_S, "trusted: [%d] %s %s\n", new->index,
  487. checksum_text, new->uri);
  488. } else {
  489. memset(&new->checksum, 0, sizeof(sgx_checksum_t));
  490. new->index = 0;
  491. SGX_DBG(DBG_S, "allowed: %s\n", new->uri);
  492. }
  493. _DkSpinLock(&trusted_file_lock);
  494. listp_for_each_entry(tf, &trusted_file_list, list) {
  495. if (tf->uri_len == uri_len && !memcmp(tf->uri, uri, uri_len)) {
  496. _DkSpinUnlock(&trusted_file_lock);
  497. free(new);
  498. return 0;
  499. }
  500. }
  501. listp_add_tail(new, &trusted_file_list, list);
  502. _DkSpinUnlock(&trusted_file_lock);
  503. return 0;
  504. }
  505. static int init_trusted_file (const char * key, const char * uri)
  506. {
  507. char cskey[URI_MAX], * tmp;
  508. char checksum[URI_MAX];
  509. char normpath[URI_MAX];
  510. tmp = strcpy_static(cskey, "sgx.trusted_checksum.", URI_MAX);
  511. memcpy(tmp, key, strlen(key) + 1);
  512. ssize_t len = get_config(pal_state.root_config, cskey, checksum, CONFIG_MAX);
  513. if (len < 0)
  514. return 0;
  515. /* Normalize the uri */
  516. if (!strpartcmp_static(uri, "file:")) {
  517. SGX_DBG(DBG_E, "Invalid URI [%s]: Trusted files must start with 'file:'\n", uri);
  518. return -PAL_ERROR_INVAL;
  519. }
  520. normpath [0] = 'f';
  521. normpath [1] = 'i';
  522. normpath [2] = 'l';
  523. normpath [3] = 'e';
  524. normpath [4] = ':';
  525. len = get_norm_path(uri + 5, normpath + 5, 0, URI_MAX);
  526. return register_trusted_file(normpath, checksum);
  527. }
  528. int init_trusted_files (void)
  529. {
  530. struct config_store * store = pal_state.root_config;
  531. char * cfgbuf = NULL;
  532. ssize_t cfgsize;
  533. int nuris, ret;
  534. if (pal_sec.exec_fd != PAL_IDX_POISON) {
  535. ret = init_trusted_file("exec", pal_sec.exec_name);
  536. if (ret < 0)
  537. goto out;
  538. }
  539. cfgbuf = malloc(CONFIG_MAX);
  540. if (!cfgbuf) {
  541. ret = -PAL_ERROR_NOMEM;
  542. goto out;
  543. }
  544. ssize_t len = get_config(store, "loader.preload", cfgbuf, CONFIG_MAX);
  545. if (len > 0) {
  546. int npreload = 0;
  547. char key[10];
  548. const char * start, * end;
  549. for (start = cfgbuf ; start < cfgbuf + len ; start = end + 1) {
  550. for (end = start ; end < cfgbuf + len && *end && *end != ',' ; end++);
  551. if (end > start) {
  552. char uri[end - start + 1];
  553. memcpy(uri, start, end - start);
  554. uri[end - start] = 0;
  555. snprintf(key, 10, "preload%d", npreload++);
  556. ret = init_trusted_file(key, uri);
  557. if (ret < 0)
  558. goto out;
  559. }
  560. }
  561. }
  562. cfgsize = get_config_entries_size(store, "sgx.trusted_files");
  563. if (cfgsize <= 0)
  564. goto no_trusted;
  565. free(cfgbuf);
  566. cfgbuf = malloc(cfgsize);
  567. if (!cfgbuf) {
  568. ret = -PAL_ERROR_NOMEM;
  569. goto out;
  570. }
  571. nuris = get_config_entries(store, "sgx.trusted_files", cfgbuf, cfgsize);
  572. if (nuris <= 0)
  573. goto no_trusted;
  574. {
  575. char key[CONFIG_MAX], uri[CONFIG_MAX];
  576. char * k = cfgbuf, * tmp;
  577. tmp = strcpy_static(key, "sgx.trusted_files.", CONFIG_MAX);
  578. for (int i = 0 ; i < nuris ; i++) {
  579. len = strlen(k);
  580. memcpy(tmp, k, len + 1);
  581. k += len + 1;
  582. len = get_config(store, key, uri, CONFIG_MAX);
  583. if (len > 0) {
  584. ret = init_trusted_file(key + 18, uri);
  585. if (ret < 0)
  586. goto out;
  587. }
  588. }
  589. }
  590. no_trusted:
  591. cfgsize = get_config_entries_size(store, "sgx.allowed_files");
  592. if (cfgsize <= 0)
  593. goto no_allowed;
  594. free(cfgbuf);
  595. cfgbuf = malloc(cfgsize);
  596. if (!cfgbuf) {
  597. ret = -PAL_ERROR_NOMEM;
  598. goto out;
  599. }
  600. nuris = get_config_entries(store, "sgx.allowed_files", cfgbuf, cfgsize);
  601. if (nuris <= 0)
  602. goto no_allowed;
  603. {
  604. char key[CONFIG_MAX], uri[CONFIG_MAX];
  605. char * k = cfgbuf, * tmp;
  606. tmp = strcpy_static(key, "sgx.allowed_files.", CONFIG_MAX);
  607. for (int i = 0 ; i < nuris ; i++) {
  608. len = strlen(k);
  609. memcpy(tmp, k, len + 1);
  610. k += len + 1;
  611. len = get_config(store, key, uri, CONFIG_MAX);
  612. if (len > 0)
  613. register_trusted_file(uri, NULL);
  614. }
  615. }
  616. no_allowed:
  617. ret = 0;
  618. if (get_config(store, "sgx.allow_file_creation", cfgbuf, CONFIG_MAX) <= 0) {
  619. allow_file_creation = 0;
  620. } else
  621. allow_file_creation = 1;
  622. out:
  623. free(cfgbuf);
  624. return ret;
  625. }
  626. int init_trusted_children (void)
  627. {
  628. struct config_store * store = pal_state.root_config;
  629. char key[CONFIG_MAX], mrkey[CONFIG_MAX];
  630. char uri[CONFIG_MAX], mrenclave[CONFIG_MAX];
  631. char * tmp1 = strcpy_static(key, "sgx.trusted_children.", CONFIG_MAX);
  632. char * tmp2 = strcpy_static(mrkey, "sgx.trusted_mrenclave.", CONFIG_MAX);
  633. ssize_t cfgsize = get_config_entries_size(store, "sgx.trusted_mrenclave");
  634. if (cfgsize <= 0)
  635. return 0;
  636. char * cfgbuf = malloc(cfgsize);
  637. if (!cfgbuf)
  638. return -PAL_ERROR_NOMEM;
  639. int nuris = get_config_entries(store, "sgx.trusted_mrenclave",
  640. cfgbuf, cfgsize);
  641. if (nuris > 0) {
  642. char * k = cfgbuf;
  643. for (int i = 0 ; i < nuris ; i++) {
  644. int len = strlen(k);
  645. memcpy(tmp1, k, len + 1);
  646. memcpy(tmp2, k, len + 1);
  647. k += len + 1;
  648. ssize_t ret = get_config(store, key, uri, CONFIG_MAX);
  649. if (ret < 0)
  650. continue;
  651. ret = get_config(store, mrkey, mrenclave, CONFIG_MAX);
  652. if (ret > 0)
  653. register_trusted_child(uri, mrenclave);
  654. }
  655. }
  656. free(cfgbuf);
  657. return 0;
  658. }
  659. #if 0
  660. void test_dh (void)
  661. {
  662. int ret;
  663. DhKey key1, key2;
  664. uint32_t privsz1, privsz2, pubsz1, pubsz2, agreesz1, agreesz2;
  665. unsigned char priv1[128], pub1[128], priv2[128], pub2[128], agree1[128],
  666. agree2[128], scratch[257];
  667. InitDhKey(&key1);
  668. InitDhKey(&key2);
  669. ret = DhSetKey(&key1, dh_param.p, sizeof(dh_param.p), dh_param.g,
  670. sizeof(dh_param.g));
  671. if (ret < 0) {
  672. SGX_DBG(DBG_S, "DhSetKey for key 1 failed: %d\n", ret);
  673. return;
  674. }
  675. ret = DhSetKey(&key2, dh_param.p, sizeof(dh_param.p), dh_param.g,
  676. sizeof(dh_param.g));
  677. if (ret < 0) {
  678. SGX_DBG(DBG_S, "DhSetKey for key 2 failed: %d\n", ret);
  679. return;
  680. }
  681. ret = DhGenerateKeyPair(&key1, priv1, &privsz1, pub1, &pubsz1);
  682. if (ret < 0) {
  683. SGX_DBG(DBG_S, "DhGenerateKeyPair for key 1 failed: %d\n", ret);
  684. return;
  685. }
  686. ret = DhGenerateKeyPair(&key2, priv2, &privsz2, pub2, &pubsz2);
  687. if (ret < 0) {
  688. SGX_DBG(DBG_S, "DhGenerateKeyPair for key 2 failed: %d\n", ret);
  689. return;
  690. }
  691. ret = DhAgree(&key1, agree1, &agreesz1, priv1, privsz1, pub2, pubsz2);
  692. if (ret < 0) {
  693. SGX_DBG(DBG_S, "DhAgree for key 1 failed: %d\n", ret);
  694. return;
  695. }
  696. ret = DhAgree(&key2, agree2, &agreesz2, priv2, privsz2, pub1, pubsz1);
  697. if (ret < 0) {
  698. SGX_DBG(DBG_S, "DhAgree for key 1 failed: %d\n", ret);
  699. return;
  700. }
  701. FreeDhKey(&key1);
  702. FreeDhKey(&key2);
  703. SGX_DBG(DBG_S, "key exchange(side A): %s (%d)\n", __bytes2hexstr(agree1, agreesz1, scratch, (agreesz1 * 2) + 1),
  704. agreesz1);
  705. SGX_DBG(DBG_S, "key exchange(side B): %s (%d)\n", __bytes2hexstr(agree2, agreesz2, scratch, (agreesz2 * 2) + 1),
  706. agreesz2);
  707. }
  708. #endif
  709. #define RSA_KEY_SIZE 2048
  710. #define RSA_E 3
  711. int init_enclave (void)
  712. {
  713. int ret;
  714. LIB_RSA_KEY *rsa = malloc(sizeof(LIB_RSA_KEY));
  715. lib_RSAInitKey(rsa);
  716. ret = lib_RSAGenerateKey(rsa, RSA_KEY_SIZE, RSA_E);
  717. if (ret != 0) {
  718. SGX_DBG(DBG_S, "lib_RSAGenerateKey failed: %d\n", ret);
  719. return ret;
  720. }
  721. PAL_NUM nsz = RSA_KEY_SIZE / 8, esz = 1;
  722. uint8_t n[nsz], e[esz];
  723. ret = lib_RSAExportPublicKey(rsa, e, &esz, n, &nsz);
  724. if (ret != 0) {
  725. SGX_DBG(DBG_S, "lib_RSAExtractPublicKey failed: %d\n", ret);
  726. goto out_free;
  727. }
  728. LIB_SHA256_CONTEXT sha256;
  729. ret = lib_SHA256Init(&sha256);
  730. if (ret < 0)
  731. goto out_free;
  732. ret = lib_SHA256Update(&sha256, n, nsz);
  733. if (ret < 0)
  734. goto out_free;
  735. ret = lib_SHA256Final(&sha256, (uint8_t *) pal_enclave_state.enclave_keyhash);
  736. if (ret < 0)
  737. goto out_free;
  738. pal_enclave_config.enclave_key = rsa;
  739. char hash_buf[HASHBUF_SIZE];
  740. SGX_DBG(DBG_S, "enclave (software) key hash: %s\n",
  741. bytes2hexstr(pal_enclave_state.enclave_keyhash, hash_buf, HASHBUF_SIZE));
  742. return 0;
  743. out_free:
  744. lib_RSAFreeKey(rsa);
  745. free(rsa);
  746. return ret;
  747. }
  748. int _DkStreamKeyExchange (PAL_HANDLE stream, PAL_SESSION_KEY * keyptr)
  749. {
  750. unsigned char session_key[32] __attribute__((aligned(32)));
  751. uint8_t pub[DH_SIZE] __attribute__((aligned(DH_SIZE)));
  752. uint8_t agree[DH_SIZE] __attribute__((aligned(DH_SIZE)));
  753. PAL_NUM pubsz, agreesz;
  754. LIB_DH_CONTEXT context;
  755. int ret;
  756. ret = lib_DhInit(&context);
  757. if (ret < 0) {
  758. SGX_DBG(DBG_S, "Key Exchange: DH Init failed: %d\n", ret);
  759. goto out_no_final;
  760. }
  761. pubsz = sizeof pub;
  762. ret = lib_DhCreatePublic(&context, pub, &pubsz);
  763. if (ret < 0) {
  764. SGX_DBG(DBG_S, "Key Exchange: DH CreatePublic failed: %d\n", ret);
  765. goto out;
  766. }
  767. assert(pubsz > 0 && pubsz <= DH_SIZE);
  768. if (pubsz < DH_SIZE) {
  769. /* Insert leading zero bytes if necessary. These values are big-
  770. * endian, so we either need to know the length of the bignum or
  771. * zero-pad at the beginning instead of the end. This code chooses
  772. * to do the latter. */
  773. memmove(pub + (DH_SIZE - pubsz), pub, pubsz);
  774. memset(pub, 0, DH_SIZE - pubsz);
  775. }
  776. ret = _DkStreamWrite(stream, 0, DH_SIZE, pub, NULL, 0);
  777. if (ret != DH_SIZE) {
  778. SGX_DBG(DBG_S, "Key Exchange: DkStreamWrite failed: %d\n", ret);
  779. goto out;
  780. }
  781. ret = _DkStreamRead(stream, 0, DH_SIZE, pub, NULL, 0);
  782. if (ret != DH_SIZE) {
  783. SGX_DBG(DBG_S, "Key Exchange: DkStreamRead failed: %d\n", ret);
  784. goto out;
  785. }
  786. agreesz = sizeof agree;
  787. ret = lib_DhCalcSecret(&context, pub, DH_SIZE, agree, &agreesz);
  788. if (ret < 0) {
  789. SGX_DBG(DBG_S, "Key Exchange: DH CalcSecret failed: %d\n", ret);
  790. goto out;
  791. }
  792. assert(agreesz > 0 && agreesz <= sizeof agree);
  793. // TODO(security): use a real KDF
  794. memset(session_key, 0, sizeof(session_key));
  795. for (int i = 0 ; i < agreesz ; i++)
  796. session_key[i % sizeof(session_key)] ^= agree[i];
  797. char key_buf[KEYBUF_SIZE];
  798. SGX_DBG(DBG_S, "key exchange: (%p) %s\n", session_key,
  799. bytes2hexstr(session_key, key_buf, KEYBUF_SIZE));
  800. if (keyptr)
  801. memcpy(keyptr, session_key, sizeof(PAL_SESSION_KEY));
  802. ret = 0;
  803. out:
  804. lib_DhFinal(&context);
  805. out_no_final:
  806. return ret;
  807. }
  808. struct attestation_request {
  809. sgx_arch_hash_t mrenclave;
  810. sgx_arch_attributes_t attributes;
  811. };
  812. struct attestation {
  813. sgx_arch_hash_t mrenclave;
  814. sgx_arch_attributes_t attributes;
  815. sgx_arch_report_t report;
  816. };
  817. int _DkStreamAttestationRequest (PAL_HANDLE stream, void * data,
  818. int (*check_mrenclave) (sgx_arch_hash_t *,
  819. void *, void *),
  820. void * check_param)
  821. {
  822. struct attestation_request req;
  823. struct attestation att;
  824. int bytes, ret;
  825. memcpy(req.mrenclave, pal_sec.mrenclave, sizeof(sgx_arch_hash_t));
  826. memcpy(&req.attributes, &pal_sec.enclave_attributes,
  827. sizeof(sgx_arch_attributes_t));
  828. char hash_buf[HASHBUF_SIZE];
  829. SGX_DBG(DBG_S, "Sending attestation request ... (mrenclave = %s)\n",\
  830. bytes2hexstr(req.mrenclave, hash_buf, HASHBUF_SIZE));
  831. for (bytes = 0, ret = 0 ; bytes < sizeof(req) ; bytes += ret) {
  832. ret = _DkStreamWrite(stream, 0, sizeof(req) - bytes,
  833. ((void *) &req) + bytes, NULL, 0);
  834. if (ret < 0) {
  835. SGX_DBG(DBG_S, "Attestation Request: DkStreamWrite failed: %d\n", ret);
  836. goto out;
  837. }
  838. }
  839. for (bytes = 0, ret = 0 ; bytes < sizeof(att) ; bytes += ret) {
  840. ret = _DkStreamRead(stream, 0, sizeof(att) - bytes,
  841. ((void *) &att) + bytes, NULL, 0);
  842. if (ret < 0) {
  843. SGX_DBG(DBG_S, "Attestation Request: DkStreamRead failed: %d\n", ret);
  844. goto out;
  845. }
  846. }
  847. SGX_DBG(DBG_S, "Received attestation (mrenclave = %s)\n",
  848. bytes2hexstr(att.mrenclave, hash_buf, HASHBUF_SIZE));
  849. ret = sgx_verify_report(&att.report);
  850. if (ret < 0) {
  851. SGX_DBG(DBG_S, "Attestation Request: sgx_verify_report failed: %d\n", ret);
  852. goto out;
  853. }
  854. if (ret == 1) {
  855. SGX_DBG(DBG_S, "Remote attestation not signed by SGX!\n");
  856. ret = -PAL_ERROR_DENIED;
  857. goto out;
  858. }
  859. ret = check_mrenclave(&att.report.mrenclave, &att.report.report_data,
  860. check_param);
  861. if (ret < 0) {
  862. SGX_DBG(DBG_S, "Attestation Request: check_mrenclave failed: %d\n", ret);
  863. goto out;
  864. }
  865. if (ret == 1) {
  866. SGX_DBG(DBG_S, "Not an allowed encalve (mrenclave = %s)\n",
  867. bytes2hexstr(att.mrenclave, hash_buf, HASHBUF_SIZE));
  868. ret = -PAL_ERROR_DENIED;
  869. goto out;
  870. }
  871. SGX_DBG(DBG_S, "Remote attestation succeed!\n");
  872. ret = sgx_get_report(&att.mrenclave, &att.attributes, data, &att.report);
  873. if (ret < 0) {
  874. SGX_DBG(DBG_S, "Attestation Request: sgx_get_report failed: %d\n", ret);
  875. goto out;
  876. }
  877. memcpy(att.mrenclave, pal_sec.mrenclave, sizeof(sgx_arch_hash_t));
  878. memcpy(&att.attributes, &pal_sec.enclave_attributes,
  879. sizeof(sgx_arch_attributes_t));
  880. SGX_DBG(DBG_S, "Sending attestation ... (mrenclave = %s)\n",
  881. bytes2hexstr(att.mrenclave, hash_buf, HASHBUF_SIZE));
  882. for (bytes = 0, ret = 0 ; bytes < sizeof(att) ; bytes += ret) {
  883. ret = _DkStreamWrite(stream, 0, sizeof(att) - bytes,
  884. ((void *) &att) + bytes, NULL, 0);
  885. if (ret < 0) {
  886. SGX_DBG(DBG_S, "Attestation Request: DkStreamWrite failed: %d\n", ret);
  887. goto out;
  888. }
  889. }
  890. return 0;
  891. out:
  892. DkStreamDelete(stream, 0);
  893. return ret;
  894. }
  895. int _DkStreamAttestationRespond (PAL_HANDLE stream, void * data,
  896. int (*check_mrenclave) (sgx_arch_hash_t *,
  897. void *, void *),
  898. void * check_param)
  899. {
  900. struct attestation_request req;
  901. struct attestation att;
  902. int bytes, ret;
  903. for (bytes = 0, ret = 0 ; bytes < sizeof(req) ; bytes += ret) {
  904. ret = _DkStreamRead(stream, 0, sizeof(req) - bytes,
  905. ((void *) &req) + bytes, NULL, 0);
  906. if (ret < 0) {
  907. SGX_DBG(DBG_S, "Attestation Respond: DkStreamRead failed: %d\n", ret);
  908. goto out;
  909. }
  910. }
  911. char hash_buf[HASHBUF_SIZE];
  912. SGX_DBG(DBG_S, "Received attestation request ... (mrenclave = %s)\n",
  913. bytes2hexstr(req.mrenclave, hash_buf, HASHBUF_SIZE));
  914. ret = sgx_get_report(&req.mrenclave, &req.attributes, data, &att.report);
  915. if (ret < 0) {
  916. SGX_DBG(DBG_S, "Attestation Respond: sgx_get_report failed: %d\n", ret);
  917. goto out;
  918. }
  919. memcpy(att.mrenclave, pal_sec.mrenclave, sizeof(sgx_arch_hash_t));
  920. memcpy(&att.attributes, &pal_sec.enclave_attributes,
  921. sizeof(sgx_arch_attributes_t));
  922. SGX_DBG(DBG_S, "Sending attestation ... (mrenclave = %s)\n",
  923. bytes2hexstr(att.mrenclave, hash_buf, HASHBUF_SIZE));
  924. for (bytes = 0, ret = 0 ; bytes < sizeof(att) ; bytes += ret) {
  925. ret = _DkStreamWrite(stream, 0, sizeof(att) - bytes,
  926. ((void *) &att) + bytes, NULL, 0);
  927. if (ret < 0) {
  928. SGX_DBG(DBG_S, "Attestation Respond: DkStreamWrite failed: %d\n", ret);
  929. goto out;
  930. }
  931. }
  932. for (bytes = 0, ret = 0 ; bytes < sizeof(att) ; bytes += ret) {
  933. ret = _DkStreamRead(stream, 0, sizeof(att) - bytes,
  934. ((void *) &att) + bytes, NULL, 0);
  935. if (ret < 0) {
  936. SGX_DBG(DBG_S, "Attestation Respond: DkStreamRead failed: %d\n", ret);
  937. goto out;
  938. }
  939. }
  940. SGX_DBG(DBG_S, "Received attestation (mrenclave = %s)\n",
  941. bytes2hexstr(att.mrenclave, hash_buf, HASHBUF_SIZE));
  942. ret = sgx_verify_report(&att.report);
  943. if (ret < 0) {
  944. SGX_DBG(DBG_S, "Attestation Respond: sgx_verify_report failed: %d\n", ret);
  945. goto out;
  946. }
  947. if (ret == 1) {
  948. SGX_DBG(DBG_S, "Remote attestation not signed by SGX!\n");
  949. goto out;
  950. }
  951. ret = check_mrenclave(&att.report.mrenclave, &att.report.report_data,
  952. check_param);
  953. if (ret < 0) {
  954. SGX_DBG(DBG_S, "Attestation Request: check_mrenclave failed: %d\n", ret);
  955. goto out;
  956. }
  957. if (ret == 1) {
  958. SGX_DBG(DBG_S, "Not an allowed enclave (mrenclave = %s)\n",
  959. bytes2hexstr(att.mrenclave, hash_buf, HASHBUF_SIZE));
  960. ret = -PAL_ERROR_DENIED;
  961. goto out;
  962. }
  963. SGX_DBG(DBG_S, "Remote attestation succeeded!\n");
  964. return 0;
  965. out:
  966. DkStreamDelete(stream, 0);
  967. return ret;
  968. }