enclave_framework.c 27 KB

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