enclave_framework.c 28 KB

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