enclave_framework.c 27 KB

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