enclave_framework.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. bool sgx_is_within_enclave (const void * addr, uint64_t size)
  15. {
  16. return (addr >= enclave_base &&
  17. addr + size <= enclave_top) ? 1 : 0;
  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. int sgx_get_report (sgx_arch_hash_t * mrenclave,
  30. sgx_arch_attributes_t * attributes,
  31. void * enclave_data,
  32. sgx_arch_report_t * report)
  33. {
  34. sgx_arch_targetinfo_t targetinfo;
  35. memset(&targetinfo, 0, sizeof(sgx_arch_targetinfo_t));
  36. memcpy(targetinfo.mrenclave, mrenclave, sizeof(sgx_arch_hash_t));
  37. memcpy(&targetinfo.attributes, attributes, sizeof(sgx_arch_attributes_t));
  38. struct pal_enclave_state state;
  39. memcpy(&state, &pal_enclave_state, sizeof(struct pal_enclave_state));
  40. memcpy(&state.data, enclave_data, PAL_ATTESTATION_DATA_SIZE);
  41. int ret = sgx_report(&targetinfo, &state, report);
  42. if (ret)
  43. return -PAL_ERROR_DENIED;
  44. SGX_DBG(DBG_S, "Generated report:\n");
  45. SGX_DBG(DBG_S, " cpusvn: %08x %08x\n", report->cpusvn[0],
  46. report->cpusvn[1]);
  47. SGX_DBG(DBG_S, " mrenclave: %s\n", hex2str(report->mrenclave));
  48. SGX_DBG(DBG_S, " mrsigner: %s\n", hex2str(report->mrsigner));
  49. SGX_DBG(DBG_S, " attributes.flags: %016lx\n", report->attributes.flags);
  50. SGX_DBG(DBG_S, " sttributes.xfrm: %016lx\n", report->attributes.xfrm);
  51. SGX_DBG(DBG_S, " isvprodid: %02x\n", report->isvprodid);
  52. SGX_DBG(DBG_S, " isvsvn: %02x\n", report->isvsvn);
  53. SGX_DBG(DBG_S, " keyid: %s\n", hex2str(report->keyid));
  54. SGX_DBG(DBG_S, " mac: %s\n", hex2str(report->mac));
  55. return 0;
  56. }
  57. static sgx_arch_key128_t enclave_key;
  58. int sgx_verify_report (sgx_arch_report_t * report)
  59. {
  60. sgx_arch_keyrequest_t keyrequest;
  61. memset(&keyrequest, 0, sizeof(sgx_arch_keyrequest_t));
  62. keyrequest.keyname = REPORT_KEY;
  63. memcpy(keyrequest.keyid, report->keyid, sizeof(keyrequest.keyid));
  64. int ret = sgx_getkey(&keyrequest, &enclave_key);
  65. if (ret) {
  66. SGX_DBG(DBG_S, "Can't get report key\n");
  67. return -PAL_ERROR_DENIED;
  68. }
  69. SGX_DBG(DBG_S, "Get report key for verification: %s\n", hex2str(enclave_key));
  70. return 0;
  71. }
  72. int init_enclave_key (void)
  73. {
  74. sgx_arch_keyrequest_t keyrequest;
  75. memset(&keyrequest, 0, sizeof(sgx_arch_keyrequest_t));
  76. keyrequest.keyname = SEAL_KEY;
  77. int ret = sgx_getkey(&keyrequest, &enclave_key);
  78. if (ret) {
  79. SGX_DBG(DBG_S, "Can't get report key\n");
  80. return -PAL_ERROR_DENIED;
  81. }
  82. SGX_DBG(DBG_S, "Get sealing key: %s\n", hex2str(enclave_key));
  83. return 0;
  84. }
  85. DEFINE_LIST(trusted_file);
  86. struct trusted_file {
  87. LIST_TYPE(trusted_file) list;
  88. int64_t index;
  89. uint64_t size;
  90. int uri_len;
  91. char uri[URI_MAX];
  92. sgx_checksum_t checksum;
  93. sgx_stub_t * stubs;
  94. };
  95. DEFINE_LISTP(trusted_file);
  96. static LISTP_TYPE(trusted_file) trusted_file_list = LISTP_INIT;
  97. static struct spinlock trusted_file_lock = LOCK_INIT;
  98. static int trusted_file_indexes = 0;
  99. int load_trusted_file (PAL_HANDLE file, sgx_stub_t ** stubptr,
  100. uint64_t * sizeptr)
  101. {
  102. struct trusted_file * tf = NULL, * tmp;
  103. char uri[URI_MAX];
  104. char normpath[URI_MAX];
  105. int ret, fd = HANDLE_HDR(file)->fds[0], uri_len, len;
  106. if (!(HANDLE_HDR(file)->flags & RFD(0)))
  107. return -PAL_ERROR_DENIED;
  108. uri_len = _DkStreamGetName(file, uri, URI_MAX);
  109. if (uri_len < 0)
  110. return uri_len;
  111. /* Normalize the uri */
  112. if (!strpartcmp_static(uri, "file:")) {
  113. SGX_DBG(DBG_E, "Invalid URI [%s]: Trusted files must start with 'file:'\n", uri);;
  114. return -PAL_ERROR_INVAL;
  115. }
  116. normpath [0] = 'f';
  117. normpath [1] = 'i';
  118. normpath [2] = 'l';
  119. normpath [3] = 'e';
  120. normpath [4] = ':';
  121. len = get_norm_path(uri + 5, normpath + 5, 0, URI_MAX);
  122. uri_len = len + 5;
  123. _DkSpinLock(&trusted_file_lock);
  124. listp_for_each_entry(tmp, &trusted_file_list, list) {
  125. if (tmp->stubs) {
  126. /* trusted files: must be exactly the same URI */
  127. if (tmp->uri_len == uri_len && !memcmp(tmp->uri, normpath, uri_len + 1)) {
  128. tf = tmp;
  129. break;
  130. }
  131. } else {
  132. /* allowed files: must be a subfolder or file */
  133. if (tmp->uri_len <= uri_len &&
  134. !memcmp(tmp->uri, normpath, tmp->uri_len) &&
  135. (!normpath[tmp->uri_len] || normpath[tmp->uri_len] == '/')) {
  136. tf = tmp;
  137. break;
  138. }
  139. }
  140. }
  141. _DkSpinUnlock(&trusted_file_lock);
  142. if (!tf)
  143. return -PAL_ERROR_DENIED;
  144. if (tf->index < 0)
  145. return tf->index;
  146. #if CACHE_FILE_STUBS == 1
  147. if (tf->index && tf->stubs) {
  148. *stubptr = tf->stubs;
  149. *sizeptr = tf->size;
  150. return 0;
  151. }
  152. #endif
  153. if (!tf->index) {
  154. *stubptr = NULL;
  155. PAL_STREAM_ATTR attr;
  156. ret = _DkStreamAttributesQuery(normpath, &attr);
  157. if (!ret)
  158. *sizeptr = attr.pending_size;
  159. else
  160. *sizeptr = 0;
  161. return 0;
  162. }
  163. int nstubs = tf->size / TRUSTED_STUB_SIZE +
  164. (tf->size % TRUSTED_STUB_SIZE ? 1 : 0);
  165. sgx_stub_t * stubs = malloc(sizeof(sgx_stub_t) * nstubs);
  166. if (!stubs)
  167. return -PAL_ERROR_NOMEM;
  168. sgx_stub_t * s = stubs;
  169. uint64_t offset = 0;
  170. PAL_SHA256_CONTEXT sha;
  171. void * umem;
  172. ret = lib_SHA256Init(&sha);
  173. if (ret < 0)
  174. goto failed;
  175. for (; offset < tf->size ; offset += TRUSTED_STUB_SIZE, s++) {
  176. uint64_t mapping_size = tf->size - offset;
  177. if (mapping_size > TRUSTED_STUB_SIZE)
  178. mapping_size = TRUSTED_STUB_SIZE;
  179. ret = ocall_map_untrusted(fd, offset, mapping_size, PROT_READ, &umem);
  180. if (ret < 0)
  181. goto unmap;
  182. AES_CMAC((void *) &enclave_key, umem, mapping_size, (uint8_t *) s);
  183. /* update the file checksum */
  184. ret = lib_SHA256Update(&sha, umem, mapping_size);
  185. unmap:
  186. ocall_unmap_untrusted(umem, mapping_size);
  187. if (ret < 0)
  188. goto failed;
  189. }
  190. sgx_checksum_t hash;
  191. ret = lib_SHA256Final(&sha, (uint8_t *) hash.bytes);
  192. if (ret < 0)
  193. goto failed;
  194. if (memcmp(&hash, &tf->checksum, sizeof(sgx_checksum_t))) {
  195. ret = -PAL_ERROR_DENIED;
  196. goto failed;
  197. }
  198. _DkSpinLock(&trusted_file_lock);
  199. if (tf->stubs || tf->index == -PAL_ERROR_DENIED)
  200. free(tf->stubs);
  201. *stubptr = tf->stubs = stubs;
  202. *sizeptr = tf->size;
  203. ret = tf->index;
  204. _DkSpinUnlock(&trusted_file_lock);
  205. return ret;
  206. failed:
  207. free(stubs);
  208. _DkSpinLock(&trusted_file_lock);
  209. if (tf->stubs) {
  210. *stubptr = tf->stubs;
  211. *sizeptr = tf->size;
  212. ret = tf->index;
  213. } else {
  214. tf->index = -PAL_ERROR_DENIED;
  215. }
  216. _DkSpinUnlock(&trusted_file_lock);
  217. #if PRINT_ENCLAVE_STAT
  218. if (!ret) {
  219. sgx_stub_t * loaded_stub;
  220. uint64_t loaded_size;
  221. PAL_HANDLE handle = NULL;
  222. if (!_DkStreamOpen(&handle, normpath, PAL_ACCESS_RDONLY, 0, 0, 0))
  223. load_trusted_file (handle, &loaded_stub, &loaded_size);
  224. }
  225. #endif
  226. return ret;
  227. }
  228. int verify_trusted_file (const char * uri, void * mem,
  229. unsigned int offset, unsigned int size,
  230. sgx_stub_t * stubs,
  231. unsigned int total_size)
  232. {
  233. unsigned long checking = offset;
  234. sgx_stub_t * s = stubs + checking / TRUSTED_STUB_SIZE;
  235. int ret;
  236. for (; checking < offset + size ; checking += TRUSTED_STUB_SIZE, s++) {
  237. unsigned long checking_size = TRUSTED_STUB_SIZE;
  238. if (checking_size > total_size - checking)
  239. checking_size = total_size - checking;
  240. uint8_t hash[256/8]; // AES_CMAC hash size is 256 bits
  241. AES_CMAC((void *) &enclave_key, mem + checking - offset,
  242. checking_size, hash);
  243. if (memcmp(s, hash, sizeof(sgx_stub_t))) {
  244. SGX_DBG(DBG_E, "Accesing file:%s is denied. "
  245. "Does not match with its MAC.\n", uri);
  246. return -PAL_ERROR_DENIED;
  247. }
  248. }
  249. return 0;
  250. }
  251. static int register_trusted_file (const char * uri, const char * checksum_str)
  252. {
  253. struct trusted_file * tf = NULL, * new;
  254. int uri_len = strlen(uri);
  255. int ret;
  256. _DkSpinLock(&trusted_file_lock);
  257. listp_for_each_entry(tf, &trusted_file_list, list) {
  258. if (tf->uri_len == uri_len && !memcmp(tf->uri, uri, uri_len)) {
  259. _DkSpinUnlock(&trusted_file_lock);
  260. return 0;
  261. }
  262. }
  263. _DkSpinUnlock(&trusted_file_lock);
  264. new = malloc(sizeof(struct trusted_file));
  265. if (!new)
  266. return -PAL_ERROR_NOMEM;
  267. INIT_LIST_HEAD(new, list);
  268. new->uri_len = uri_len;
  269. memcpy(new->uri, uri, uri_len + 1);
  270. new->size = 0;
  271. new->stubs = NULL;
  272. if (checksum_str) {
  273. PAL_STREAM_ATTR attr;
  274. ret = _DkStreamAttributesQuery(uri, &attr);
  275. if (!ret)
  276. new->size = attr.pending_size;
  277. char checksum_text[sizeof(sgx_checksum_t) * 2 + 1] = "\0";
  278. int nbytes = 0;
  279. for (; nbytes < sizeof(sgx_checksum_t) ; nbytes++) {
  280. char byte1 = checksum_str[nbytes * 2];
  281. char byte2 = checksum_str[nbytes * 2 + 1];
  282. unsigned char val = 0;
  283. if (byte1 == 0 || byte2 == 0) {
  284. break;
  285. }
  286. if (!(byte1 >= '0' && byte1 <= '9') &&
  287. !(byte1 >= 'a' && byte1 <= 'f')) {
  288. break;
  289. }
  290. if (!(byte2 >= '0' && byte2 <= '9') &&
  291. !(byte2 >= 'a' && byte2 <= 'f')) {
  292. break;
  293. }
  294. if (byte1 >= '0' && byte1 <= '9')
  295. val = byte1 - '0';
  296. if (byte1 >= 'a' && byte1 <= 'f')
  297. val = byte1 - 'a' + 10;
  298. val *= 16;
  299. if (byte2 >= '0' && byte2 <= '9')
  300. val += byte2 - '0';
  301. if (byte2 >= 'a' && byte2 <= 'f')
  302. val += byte2 - 'a' + 10;
  303. new->checksum.bytes[nbytes] = val;
  304. snprintf(checksum_text + nbytes * 2, 3, "%02x", val);
  305. }
  306. if (nbytes < sizeof(sgx_checksum_t)) {
  307. free(new);
  308. return -PAL_ERROR_INVAL;
  309. }
  310. new->index = (++trusted_file_indexes);
  311. SGX_DBG(DBG_S, "trusted: [%d] %s %s\n", new->index,
  312. checksum_text, new->uri);
  313. } else {
  314. memset(&new->checksum, 0, sizeof(sgx_checksum_t));
  315. new->index = 0;
  316. SGX_DBG(DBG_S, "allowed: %s\n", new->uri);
  317. }
  318. _DkSpinLock(&trusted_file_lock);
  319. listp_for_each_entry(tf, &trusted_file_list, list) {
  320. if (tf->uri_len == uri_len && !memcmp(tf->uri, uri, uri_len)) {
  321. _DkSpinUnlock(&trusted_file_lock);
  322. free(new);
  323. return 0;
  324. }
  325. }
  326. listp_add_tail(new, &trusted_file_list, list);
  327. _DkSpinUnlock(&trusted_file_lock);
  328. return 0;
  329. }
  330. static int init_trusted_file (const char * key, const char * uri)
  331. {
  332. char cskey[URI_MAX], * tmp;
  333. char checksum[URI_MAX];
  334. char normpath[URI_MAX];
  335. tmp = strcpy_static(cskey, "sgx.trusted_checksum.", URI_MAX);
  336. memcpy(tmp, key, strlen(key) + 1);
  337. int len = get_config(pal_state.root_config, cskey, checksum, CONFIG_MAX);
  338. if (len < 0)
  339. return 0;
  340. /* Normalize the uri */
  341. if (!strpartcmp_static(uri, "file:")) {
  342. SGX_DBG(DBG_E, "Invalid URI [%s]: Trusted files must start with 'file:'\n", uri);
  343. return -PAL_ERROR_INVAL;
  344. }
  345. normpath [0] = 'f';
  346. normpath [1] = 'i';
  347. normpath [2] = 'l';
  348. normpath [3] = 'e';
  349. normpath [4] = ':';
  350. len = get_norm_path(uri + 5, normpath + 5, 0, URI_MAX);
  351. return register_trusted_file(normpath, checksum);
  352. }
  353. int init_trusted_files (void)
  354. {
  355. char cfgbuf[CONFIG_MAX];
  356. int ret;
  357. if (pal_sec.exec_fd != PAL_IDX_POISON) {
  358. ret = init_trusted_file("exec", pal_sec.exec_name);
  359. if (ret < 0)
  360. return ret;
  361. }
  362. int len = get_config(pal_state.root_config, "loader.preload",
  363. cfgbuf, CONFIG_MAX);
  364. if (len) {
  365. int npreload = 0;
  366. char key[10];
  367. const char * start, * end;
  368. for (start = cfgbuf ; start < cfgbuf + len ; start = end + 1) {
  369. for (end = start ; end < cfgbuf + len && *end && *end != ',' ; end++);
  370. if (end > start) {
  371. char uri[end - start + 1];
  372. memcpy(uri, start, end - start);
  373. uri[end - start] = 0;
  374. snprintf(key, 10, "preload%d", npreload++);
  375. ret = init_trusted_file(key, uri);
  376. if (ret < 0)
  377. return ret;
  378. }
  379. }
  380. }
  381. int nuris = get_config_entries(pal_state.root_config, "sgx.trusted_files",
  382. cfgbuf, CONFIG_MAX);
  383. if (nuris) {
  384. char key[CONFIG_MAX], uri[CONFIG_MAX];
  385. char * k = cfgbuf, * tmp;
  386. tmp = strcpy_static(key, "sgx.trusted_files.", CONFIG_MAX);
  387. for (int i = 0 ; i < nuris ; i++) {
  388. len = strlen(k);
  389. memcpy(tmp, k, len + 1);
  390. k += len + 1;
  391. len = get_config(pal_state.root_config, key, uri, CONFIG_MAX);
  392. if (len > 0) {
  393. ret = init_trusted_file(key + 18, uri);
  394. if (ret < 0)
  395. return ret;
  396. }
  397. }
  398. }
  399. nuris = get_config_entries(pal_state.root_config, "sgx.allowed_files",
  400. cfgbuf, CONFIG_MAX);
  401. if (nuris > 0) {
  402. char key[CONFIG_MAX], uri[CONFIG_MAX];
  403. char * k = cfgbuf, * tmp;
  404. tmp = strcpy_static(key, "sgx.allowed_files.", CONFIG_MAX);
  405. for (int i = 0 ; i < nuris ; i++) {
  406. len = strlen(k);
  407. memcpy(tmp, k, len + 1);
  408. k += len + 1;
  409. len = get_config(pal_state.root_config, key, uri, CONFIG_MAX);
  410. if (len > 0)
  411. register_trusted_file(uri, NULL);
  412. }
  413. }
  414. return 0;
  415. }
  416. int init_trusted_children (void)
  417. {
  418. char cfgbuf[CONFIG_MAX];
  419. char key[CONFIG_MAX], mrkey[CONFIG_MAX];
  420. char uri[CONFIG_MAX], mrenclave[CONFIG_MAX];
  421. char * tmp1 = strcpy_static(key, "sgx.trusted_children.", CONFIG_MAX);
  422. char * tmp2 = strcpy_static(mrkey, "sgx.trusted_mrenclave.", CONFIG_MAX);
  423. int nuris = get_config_entries(pal_state.root_config,
  424. "sgx.trusted_mrenclave", cfgbuf, CONFIG_MAX);
  425. if (nuris > 0) {
  426. char * k = cfgbuf;
  427. for (int i = 0 ; i < nuris ; i++) {
  428. int len = strlen(k);
  429. memcpy(tmp1, k, len + 1);
  430. memcpy(tmp2, k, len + 1);
  431. k += len + 1;
  432. int ret = get_config(pal_state.root_config, key, uri, CONFIG_MAX);
  433. if (ret < 0)
  434. continue;
  435. ret = get_config(pal_state.root_config, mrkey, mrenclave,
  436. CONFIG_MAX);
  437. if (ret > 0)
  438. register_trusted_child(uri, mrenclave);
  439. }
  440. }
  441. return 0;
  442. }
  443. #include "crypto/dh.h"
  444. static struct {
  445. uint8_t p[128], q[20], g[128];
  446. } dh_param = {
  447. {
  448. 0xfd, 0x7f, 0x53, 0x81, 0x1d, 0x75, 0x12, 0x29,
  449. 0x52, 0xdf, 0x4a, 0x9c, 0x2e, 0xec, 0xe4, 0xe7,
  450. 0xf6, 0x11, 0xb7, 0x52, 0x3c, 0xef, 0x44, 0x00,
  451. 0xc3, 0x1e, 0x3f, 0x80, 0xb6, 0x51, 0x26, 0x69,
  452. 0x45, 0x5d, 0x40, 0x22, 0x51, 0xfb, 0x59, 0x3d,
  453. 0x8d, 0x58, 0xfa, 0xbf, 0xc5, 0xf5, 0xba, 0x30,
  454. 0xf6, 0xcb, 0x9b, 0x55, 0x6c, 0xd7, 0x81, 0x3b,
  455. 0x80, 0x1d, 0x34, 0x6f, 0xf2, 0x66, 0x60, 0xb7,
  456. 0x6b, 0x99, 0x50, 0xa5, 0xa4, 0x9f, 0x9f, 0xe8,
  457. 0x04, 0x7b, 0x10, 0x22, 0xc2, 0x4f, 0xbb, 0xa9,
  458. 0xd7, 0xfe, 0xb7, 0xc6, 0x1b, 0xf8, 0x3b, 0x57,
  459. 0xe7, 0xc6, 0xa8, 0xa6, 0x15, 0x0f, 0x04, 0xfb,
  460. 0x83, 0xf6, 0xd3, 0xc5, 0x1e, 0xc3, 0x02, 0x35,
  461. 0x54, 0x13, 0x5a, 0x16, 0x91, 0x32, 0xf6, 0x75,
  462. 0xf3, 0xae, 0x2b, 0x61, 0xd7, 0x2a, 0xef, 0xf2,
  463. 0x22, 0x03, 0x19, 0x9d, 0xd1, 0x48, 0x01, 0xc7,
  464. },
  465. {
  466. 0x97, 0x60, 0x50, 0x8f, 0x15, 0x23, 0x0b, 0xcc,
  467. 0xb2, 0x92, 0xb9, 0x82, 0xa2, 0xeb, 0x84, 0x0b,
  468. 0xf0, 0x58, 0x1c, 0xf5,
  469. },
  470. {
  471. 0xf7, 0xe1, 0xa0, 0x85, 0xd6, 0x9b, 0x3d, 0xde,
  472. 0xcb, 0xbc, 0xab, 0x5c, 0x36, 0xb8, 0x57, 0xb9,
  473. 0x79, 0x94, 0xaf, 0xbb, 0xfa, 0x3a, 0xea, 0x82,
  474. 0xf9, 0x57, 0x4c, 0x0b, 0x3d, 0x07, 0x82, 0x67,
  475. 0x51, 0x59, 0x57, 0x8e, 0xba, 0xd4, 0x59, 0x4f,
  476. 0xe6, 0x71, 0x07, 0x10, 0x81, 0x80, 0xb4, 0x49,
  477. 0x16, 0x71, 0x23, 0xe8, 0x4c, 0x28, 0x16, 0x13,
  478. 0xb7, 0xcf, 0x09, 0x32, 0x8c, 0xc8, 0xa6, 0xe1,
  479. 0x3c, 0x16, 0x7a, 0x8b, 0x54, 0x7c, 0x8d, 0x28,
  480. 0xe0, 0xa3, 0xae, 0x1e, 0x2b, 0xb3, 0xa6, 0x75,
  481. 0x91, 0x6e, 0xa3, 0x7f, 0x0b, 0xfa, 0x21, 0x35,
  482. 0x62, 0xf1, 0xfb, 0x62, 0x7a, 0x01, 0x24, 0x3b,
  483. 0xcc, 0xa4, 0xf1, 0xbe, 0xa8, 0x51, 0x90, 0x89,
  484. 0xa8, 0x83, 0xdf, 0xe1, 0x5a, 0xe5, 0x9f, 0x06,
  485. 0x92, 0x8b, 0x66, 0x5e, 0x80, 0x7b, 0x55, 0x25,
  486. 0x64, 0x01, 0x4c, 0x3b, 0xfe, 0xcf, 0x49, 0x2a,
  487. },
  488. };
  489. void test_dh (void)
  490. {
  491. int ret;
  492. DhKey key1, key2;
  493. uint32_t privsz1, privsz2, pubsz1, pubsz2, agreesz1, agreesz2;
  494. unsigned char priv1[128], pub1[128], priv2[128], pub2[128], agree1[128],
  495. agree2[128];
  496. InitDhKey(&key1);
  497. InitDhKey(&key2);
  498. ret = DhSetKey(&key1, dh_param.p, sizeof(dh_param.p), dh_param.g,
  499. sizeof(dh_param.g));
  500. if (ret < 0) {
  501. SGX_DBG(DBG_S, "DhSetKey for key 1 failed: %d\n", ret);
  502. return;
  503. }
  504. ret = DhSetKey(&key2, dh_param.p, sizeof(dh_param.p), dh_param.g,
  505. sizeof(dh_param.g));
  506. if (ret < 0) {
  507. SGX_DBG(DBG_S, "DhSetKey for key 2 failed: %d\n", ret);
  508. return;
  509. }
  510. ret = DhGenerateKeyPair(&key1, priv1, &privsz1, pub1, &pubsz1);
  511. if (ret < 0) {
  512. SGX_DBG(DBG_S, "DhGenerateKeyPair for key 1 failed: %d\n", ret);
  513. return;
  514. }
  515. ret = DhGenerateKeyPair(&key2, priv2, &privsz2, pub2, &pubsz2);
  516. if (ret < 0) {
  517. SGX_DBG(DBG_S, "DhGenerateKeyPair for key 2 failed: %d\n", ret);
  518. return;
  519. }
  520. ret = DhAgree(&key1, agree1, &agreesz1, priv1, privsz1, pub2, pubsz2);
  521. if (ret < 0) {
  522. SGX_DBG(DBG_S, "DhAgree for key 1 failed: %d\n", ret);
  523. return;
  524. }
  525. ret = DhAgree(&key2, agree2, &agreesz2, priv2, privsz2, pub1, pubsz1);
  526. if (ret < 0) {
  527. SGX_DBG(DBG_S, "DhAgree for key 1 failed: %d\n", ret);
  528. return;
  529. }
  530. FreeDhKey(&key1);
  531. FreeDhKey(&key2);
  532. SGX_DBG(DBG_S, "key exchange(side A): %s (%d)\n", __hex2str(agree1, agreesz1),
  533. agreesz1);
  534. SGX_DBG(DBG_S, "key exchange(side B): %s (%d)\n", __hex2str(agree2, agreesz2),
  535. agreesz2);
  536. }
  537. #include "crypto/rsa.h"
  538. #define RSA_KEY_SIZE 2048
  539. #define RSA_E 3
  540. int init_enclave (void)
  541. {
  542. int ret;
  543. RSAKey *rsa = malloc(sizeof(RSAKey));
  544. InitRSAKey(rsa);
  545. ret = MakeRSAKey(rsa, RSA_KEY_SIZE, RSA_E);
  546. if (ret < 0) {
  547. SGX_DBG(DBG_S, "MakeRSAKey failed: %d\n", ret);
  548. return ret;
  549. }
  550. uint32_t nsz = RSA_KEY_SIZE / 8, esz = 1;
  551. uint8_t n[nsz], e[esz];
  552. ret = RSAFlattenPublicKey(rsa, e, &esz, n, &nsz);
  553. if (ret < 0) {
  554. SGX_DBG(DBG_S, "RSAFlattenPublicKey failed: %d\n", ret);
  555. goto out_free;
  556. }
  557. PAL_SHA256_CONTEXT sha256;
  558. ret = lib_SHA256Init(&sha256);
  559. if (ret < 0)
  560. goto out_free;
  561. ret = lib_SHA256Update(&sha256, n, nsz);
  562. if (ret < 0)
  563. goto out_free;
  564. ret = lib_SHA256Final(&sha256, (uint8_t *) pal_enclave_state.enclave_keyhash);
  565. if (ret < 0)
  566. goto out_free;
  567. pal_enclave_config.enclave_key = rsa;
  568. SGX_DBG(DBG_S, "enclave (software) key hash: %s\n",
  569. hex2str(pal_enclave_state.enclave_keyhash));
  570. return 0;
  571. out_free:
  572. FreeRSAKey(rsa);
  573. free(rsa);
  574. return ret;
  575. }
  576. int _DkStreamKeyExchange (PAL_HANDLE stream, PAL_SESSION_KEY * keyptr)
  577. {
  578. unsigned char session_key[32] __attribute__((aligned(32)));
  579. unsigned char priv[128] __attribute__((aligned(128))),
  580. pub[128] __attribute__((aligned(128))),
  581. agree[128] __attribute__((aligned(128)));
  582. uint32_t privsz, pubsz, agreesz;
  583. DhKey dh;
  584. int ret;
  585. InitDhKey(&dh);
  586. ret = DhSetKey(&dh, dh_param.p, sizeof(dh_param.p), dh_param.g,
  587. sizeof(dh_param.g));
  588. if (ret < 0) {
  589. SGX_DBG(DBG_S, "Key Exchange: DhSetKey failed: %d\n", ret);
  590. goto out;
  591. }
  592. ret = DhGenerateKeyPair(&dh, priv, &privsz, pub, &pubsz);
  593. if (ret < 0) {
  594. SGX_DBG(DBG_S, "Key Exchange: DhGenerateKeyPair failed: %d\n", ret);
  595. goto out;
  596. }
  597. ret = _DkStreamWrite(stream, 0, pubsz, pub, NULL, 0);
  598. if (ret < pubsz) {
  599. SGX_DBG(DBG_S, "Key Exchange: DkStreamWrite failed: %d\n", ret);
  600. goto out;
  601. }
  602. ret = _DkStreamRead(stream, 0, pubsz, pub, NULL, 0);
  603. if (ret < pubsz) {
  604. SGX_DBG(DBG_S, "Key Exchange: DkStreamRead failed: %d\n", ret);
  605. goto out;
  606. }
  607. ret = DhAgree(&dh, agree, &agreesz, priv, privsz, pub, pubsz);
  608. if (ret < 0) {
  609. SGX_DBG(DBG_S, "Key Exchange: DhAgree failed: %d\n", ret);
  610. goto out;
  611. }
  612. memset(session_key, 0, sizeof(session_key));
  613. for (int i = 0 ; i < agreesz ; i++)
  614. session_key[i % sizeof(session_key)] ^= agree[i];
  615. SGX_DBG(DBG_S, "key exchange: (%p) %s\n", session_key, hex2str(session_key));
  616. if (keyptr)
  617. memcpy(keyptr, session_key, sizeof(PAL_SESSION_KEY));
  618. ret = 0;
  619. out:
  620. FreeDhKey(&dh);
  621. return ret;
  622. }
  623. struct attestation_request {
  624. sgx_arch_hash_t mrenclave;
  625. sgx_arch_attributes_t attributes;
  626. };
  627. struct attestation {
  628. sgx_arch_hash_t mrenclave;
  629. sgx_arch_attributes_t attributes;
  630. sgx_arch_report_t report;
  631. };
  632. int _DkStreamAttestationRequest (PAL_HANDLE stream, void * data,
  633. int (*check_mrenclave) (sgx_arch_hash_t *,
  634. void *, void *),
  635. void * check_param)
  636. {
  637. struct attestation_request req;
  638. struct attestation att;
  639. int bytes, ret;
  640. memcpy(req.mrenclave, pal_sec.mrenclave, sizeof(sgx_arch_hash_t));
  641. memcpy(&req.attributes, &pal_sec.enclave_attributes,
  642. sizeof(sgx_arch_attributes_t));
  643. SGX_DBG(DBG_S, "Sending attestation request ... (mrenclave = %s)\n",\
  644. hex2str(req.mrenclave));
  645. for (bytes = 0, ret = 0 ; bytes < sizeof(req) ; bytes += ret) {
  646. ret = _DkStreamWrite(stream, 0, sizeof(req) - bytes,
  647. ((void *) &req) + bytes, NULL, 0);
  648. if (ret < 0) {
  649. SGX_DBG(DBG_S, "Attestation Request: DkStreamWrite failed: %d\n", ret);
  650. goto out;
  651. }
  652. }
  653. for (bytes = 0, ret = 0 ; bytes < sizeof(att) ; bytes += ret) {
  654. ret = _DkStreamRead(stream, 0, sizeof(att) - bytes,
  655. ((void *) &att) + bytes, NULL, 0);
  656. if (ret < 0) {
  657. SGX_DBG(DBG_S, "Attestation Request: DkStreamRead failed: %d\n", ret);
  658. goto out;
  659. }
  660. }
  661. SGX_DBG(DBG_S, "Received attestation (mrenclave = %s)\n",
  662. hex2str(att.mrenclave));
  663. ret = sgx_verify_report(&att.report);
  664. if (ret < 0) {
  665. SGX_DBG(DBG_S, "Attestation Request: sgx_verify_report failed: %d\n", ret);
  666. goto out;
  667. }
  668. if (ret == 1) {
  669. SGX_DBG(DBG_S, "Remote attestation not signed by SGX!\n");
  670. ret = -PAL_ERROR_DENIED;
  671. goto out;
  672. }
  673. ret = check_mrenclave(&att.report.mrenclave, &att.report.report_data,
  674. check_param);
  675. if (ret < 0) {
  676. SGX_DBG(DBG_S, "Attestation Request: check_mrenclave failed: %d\n", ret);
  677. goto out;
  678. }
  679. if (ret == 1) {
  680. SGX_DBG(DBG_S, "Not an allowed encalve (mrenclave = %s)\n",
  681. hex2str(att.mrenclave));
  682. ret = -PAL_ERROR_DENIED;
  683. goto out;
  684. }
  685. SGX_DBG(DBG_S, "Remote attestation succeed!\n");
  686. ret = sgx_get_report(&att.mrenclave, &att.attributes, data, &att.report);
  687. if (ret < 0) {
  688. SGX_DBG(DBG_S, "Attestation Request: sgx_get_report failed: %d\n", ret);
  689. goto out;
  690. }
  691. memcpy(att.mrenclave, pal_sec.mrenclave, sizeof(sgx_arch_hash_t));
  692. memcpy(&att.attributes, &pal_sec.enclave_attributes,
  693. sizeof(sgx_arch_attributes_t));
  694. SGX_DBG(DBG_S, "Sending attestation ... (mrenclave = %s)\n",
  695. hex2str(att.mrenclave));
  696. for (bytes = 0, ret = 0 ; bytes < sizeof(att) ; bytes += ret) {
  697. ret = _DkStreamWrite(stream, 0, sizeof(att) - bytes,
  698. ((void *) &att) + bytes, NULL, 0);
  699. if (ret < 0) {
  700. SGX_DBG(DBG_S, "Attestation Request: DkStreamWrite failed: %d\n", ret);
  701. goto out;
  702. }
  703. }
  704. return 0;
  705. out:
  706. DkStreamDelete(stream, 0);
  707. return ret;
  708. }
  709. int _DkStreamAttestationRespond (PAL_HANDLE stream, void * data,
  710. int (*check_mrenclave) (sgx_arch_hash_t *,
  711. void *, void *),
  712. void * check_param)
  713. {
  714. struct attestation_request req;
  715. struct attestation att;
  716. int bytes, ret;
  717. for (bytes = 0, ret = 0 ; bytes < sizeof(req) ; bytes += ret) {
  718. ret = _DkStreamRead(stream, 0, sizeof(req) - bytes,
  719. ((void *) &req) + bytes, NULL, 0);
  720. if (ret < 0) {
  721. SGX_DBG(DBG_S, "Attestation Respond: DkStreamRead failed: %d\n", ret);
  722. goto out;
  723. }
  724. }
  725. SGX_DBG(DBG_S, "Received attestation request ... (mrenclave = %s)\n",
  726. hex2str(req.mrenclave));
  727. ret = sgx_get_report(&req.mrenclave, &req.attributes, data, &att.report);
  728. if (ret < 0) {
  729. SGX_DBG(DBG_S, "Attestation Respond: sgx_get_report failed: %d\n", ret);
  730. goto out;
  731. }
  732. memcpy(att.mrenclave, pal_sec.mrenclave, sizeof(sgx_arch_hash_t));
  733. memcpy(&att.attributes, &pal_sec.enclave_attributes,
  734. sizeof(sgx_arch_attributes_t));
  735. SGX_DBG(DBG_S, "Sending attestation ... (mrenclave = %s)\n",
  736. hex2str(att.mrenclave));
  737. for (bytes = 0, ret = 0 ; bytes < sizeof(att) ; bytes += ret) {
  738. ret = _DkStreamWrite(stream, 0, sizeof(att) - bytes,
  739. ((void *) &att) + bytes, NULL, 0);
  740. if (ret < 0) {
  741. SGX_DBG(DBG_S, "Attestation Respond: DkStreamWrite failed: %d\n", ret);
  742. goto out;
  743. }
  744. }
  745. for (bytes = 0, ret = 0 ; bytes < sizeof(att) ; bytes += ret) {
  746. ret = _DkStreamRead(stream, 0, sizeof(att) - bytes,
  747. ((void *) &att) + bytes, NULL, 0);
  748. if (ret < 0) {
  749. SGX_DBG(DBG_S, "Attestation Respond: DkStreamRead failed: %d\n", ret);
  750. goto out;
  751. }
  752. }
  753. SGX_DBG(DBG_S, "Received attestation (mrenclave = %s)\n",
  754. hex2str(att.mrenclave));
  755. ret = sgx_verify_report(&att.report);
  756. if (ret < 0) {
  757. SGX_DBG(DBG_S, "Attestation Respond: sgx_verify_report failed: %d\n", ret);
  758. goto out;
  759. }
  760. if (ret == 1) {
  761. SGX_DBG(DBG_S, "Remote attestation not signed by SGX!\n");
  762. goto out;
  763. }
  764. ret = check_mrenclave(&att.report.mrenclave, &att.report.report_data,
  765. check_param);
  766. if (ret < 0) {
  767. SGX_DBG(DBG_S, "Attestation Request: check_mrenclave failed: %d\n", ret);
  768. goto out;
  769. }
  770. if (ret == 1) {
  771. SGX_DBG(DBG_S, "Not an allowed encalve (mrenclave = %s)\n",
  772. hex2str(att.mrenclave));
  773. ret = -PAL_ERROR_DENIED;
  774. goto out;
  775. }
  776. SGX_DBG(DBG_S, "Remote attestation succeed!\n");
  777. return 0;
  778. out:
  779. DkStreamDelete(stream, 0);
  780. return ret;
  781. }