App.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. # include <unistd.h>
  35. # include <pwd.h>
  36. # define MAX_PATH FILENAME_MAX
  37. #include "sgx_urts.h"
  38. #include "sgx_uae_service.h"
  39. #include "App.h"
  40. #include "Enclave_u.h"
  41. /* Global EID shared by multiple threads */
  42. sgx_enclave_id_t global_eid = 0;
  43. typedef struct _sgx_errlist_t {
  44. sgx_status_t err;
  45. const char *msg;
  46. const char *sug; /* Suggestion */
  47. } sgx_errlist_t;
  48. /* Error code returned by sgx_create_enclave */
  49. static sgx_errlist_t sgx_errlist[] = {
  50. {
  51. SGX_ERROR_UNEXPECTED,
  52. "Unexpected error occurred.",
  53. NULL
  54. },
  55. {
  56. SGX_ERROR_INVALID_PARAMETER,
  57. "Invalid parameter.",
  58. NULL
  59. },
  60. {
  61. SGX_ERROR_OUT_OF_MEMORY,
  62. "Out of memory.",
  63. NULL
  64. },
  65. {
  66. SGX_ERROR_ENCLAVE_LOST,
  67. "Power transition occurred.",
  68. "Please refer to the sample \"PowerTransition\" for details."
  69. },
  70. {
  71. SGX_ERROR_INVALID_ENCLAVE,
  72. "Invalid enclave image.",
  73. NULL
  74. },
  75. {
  76. SGX_ERROR_INVALID_ENCLAVE_ID,
  77. "Invalid enclave identification.",
  78. NULL
  79. },
  80. {
  81. SGX_ERROR_INVALID_SIGNATURE,
  82. "Invalid enclave signature.",
  83. NULL
  84. },
  85. {
  86. SGX_ERROR_OUT_OF_EPC,
  87. "Out of EPC memory.",
  88. NULL
  89. },
  90. {
  91. SGX_ERROR_NO_DEVICE,
  92. "Invalid SGX device.",
  93. "Please make sure SGX module is enabled in the BIOS, and install SGX driver afterwards."
  94. },
  95. {
  96. SGX_ERROR_MEMORY_MAP_CONFLICT,
  97. "Memory map conflicted.",
  98. NULL
  99. },
  100. {
  101. SGX_ERROR_INVALID_METADATA,
  102. "Invalid enclave metadata.",
  103. NULL
  104. },
  105. {
  106. SGX_ERROR_DEVICE_BUSY,
  107. "SGX device was busy.",
  108. NULL
  109. },
  110. {
  111. SGX_ERROR_INVALID_VERSION,
  112. "Enclave version was invalid.",
  113. NULL
  114. },
  115. {
  116. SGX_ERROR_INVALID_ATTRIBUTE,
  117. "Enclave was not authorized.",
  118. NULL
  119. },
  120. {
  121. SGX_ERROR_ENCLAVE_FILE_ACCESS,
  122. "Can't open enclave file.",
  123. NULL
  124. },
  125. {
  126. SGX_ERROR_NDEBUG_ENCLAVE,
  127. "The enclave is signed as product enclave, and can not be created as debuggable enclave.",
  128. NULL
  129. },
  130. };
  131. /* Check error conditions for loading enclave */
  132. void print_error_message(sgx_status_t ret)
  133. {
  134. size_t idx = 0;
  135. size_t ttl = sizeof sgx_errlist/sizeof sgx_errlist[0];
  136. for (idx = 0; idx < ttl; idx++) {
  137. if(ret == sgx_errlist[idx].err) {
  138. if(NULL != sgx_errlist[idx].sug)
  139. printf("Info: %s\n", sgx_errlist[idx].sug);
  140. printf("Error: %s\n", sgx_errlist[idx].msg);
  141. break;
  142. }
  143. }
  144. if (idx == ttl)
  145. printf("Error: Unexpected error occurred.\n");
  146. }
  147. /* Initialize the enclave:
  148. * Step 1: try to retrieve the launch token saved by last transaction
  149. * Step 2: call sgx_create_enclave to initialize an enclave instance
  150. * Step 3: save the launch token if it is updated
  151. */
  152. int initialize_enclave(void)
  153. {
  154. char token_path[MAX_PATH] = {'\0'};
  155. sgx_launch_token_t token = {0};
  156. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  157. int updated = 0;
  158. /* Step 1: try to retrieve the launch token saved by last transaction
  159. * if there is no token, then create a new one.
  160. */
  161. /* try to get the token saved in $HOME */
  162. const char *home_dir = getpwuid(getuid())->pw_dir;
  163. if (home_dir != NULL &&
  164. (strlen(home_dir)+strlen("/")+sizeof(TOKEN_FILENAME)+1) <= MAX_PATH) {
  165. /* compose the token path */
  166. strncpy(token_path, home_dir, strlen(home_dir));
  167. strncat(token_path, "/", strlen("/"));
  168. strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME)+1);
  169. } else {
  170. /* if token path is too long or $HOME is NULL */
  171. strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME));
  172. }
  173. FILE *fp = fopen(token_path, "rb");
  174. if (fp == NULL && (fp = fopen(token_path, "wb")) == NULL) {
  175. printf("Warning: Failed to create/open the launch token file \"%s\".\n", token_path);
  176. }
  177. if (fp != NULL) {
  178. /* read the token from saved file */
  179. size_t read_num = fread(token, 1, sizeof(sgx_launch_token_t), fp);
  180. if (read_num != 0 && read_num != sizeof(sgx_launch_token_t)) {
  181. /* if token is invalid, clear the buffer */
  182. memset(&token, 0x0, sizeof(sgx_launch_token_t));
  183. printf("Warning: Invalid launch token read from \"%s\".\n", token_path);
  184. }
  185. }
  186. /* Step 2: call sgx_create_enclave to initialize an enclave instance */
  187. /* Debug Support: set 2nd parameter to 1 */
  188. ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, &token, &updated, &global_eid, NULL);
  189. if (ret != SGX_SUCCESS) {
  190. print_error_message(ret);
  191. if (fp != NULL) fclose(fp);
  192. return -1;
  193. }
  194. /* Step 3: save the launch token if it is updated */
  195. if (updated == FALSE || fp == NULL) {
  196. /* if the token is not updated, or file handler is invalid, do not perform saving */
  197. if (fp != NULL) fclose(fp);
  198. return 0;
  199. }
  200. /* reopen the file with write capablity */
  201. fp = freopen(token_path, "wb", fp);
  202. if (fp == NULL) return 0;
  203. size_t write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp);
  204. if (write_num != sizeof(sgx_launch_token_t))
  205. printf("Warning: Failed to save launch token to \"%s\".\n", token_path);
  206. fclose(fp);
  207. return 0;
  208. }
  209. /* OCall functions */
  210. void ocall_print_string(const char *str)
  211. {
  212. /* Proxy/Bridge will check the length and null-terminate
  213. * the input string to prevent buffer overflow.
  214. */
  215. printf("%s", str);
  216. }
  217. /* Application entry */
  218. int SGX_CDECL main(int argc, char *argv[])
  219. {
  220. (void)(argc);
  221. (void)(argv);
  222. /* Initialize the enclave */
  223. if(initialize_enclave() < 0){
  224. printf("Enter a character before exit ...\n");
  225. getchar();
  226. return -1;
  227. }
  228. /* Utilize trusted libraries */
  229. ecall_libcxx_functions();
  230. /* Destroy the enclave */
  231. sgx_destroy_enclave(global_eid);
  232. printf("Info: Cxx11DemoEnclave successfully returned.\n");
  233. //printf("Enter a character before exit ...\n");
  234. //getchar();
  235. return 0;
  236. }