enclave_framework.c 27 KB

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