App.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2011-2017 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 "App.h"
  39. #include "Enclave_u.h"
  40. /* Global EID shared by multiple threads */
  41. sgx_enclave_id_t global_eid = 0;
  42. typedef struct _sgx_errlist_t {
  43. sgx_status_t err;
  44. const char *msg;
  45. const char *sug; /* Suggestion */
  46. } sgx_errlist_t;
  47. /* Error code returned by sgx_create_enclave */
  48. static sgx_errlist_t sgx_errlist[] = {
  49. {
  50. SGX_ERROR_UNEXPECTED,
  51. "Unexpected error occurred.",
  52. NULL
  53. },
  54. {
  55. SGX_ERROR_INVALID_PARAMETER,
  56. "Invalid parameter.",
  57. NULL
  58. },
  59. {
  60. SGX_ERROR_OUT_OF_MEMORY,
  61. "Out of memory.",
  62. NULL
  63. },
  64. {
  65. SGX_ERROR_ENCLAVE_LOST,
  66. "Power transition occurred.",
  67. "Please refer to the sample \"PowerTransition\" for details."
  68. },
  69. {
  70. SGX_ERROR_INVALID_ENCLAVE,
  71. "Invalid enclave image.",
  72. NULL
  73. },
  74. {
  75. SGX_ERROR_INVALID_ENCLAVE_ID,
  76. "Invalid enclave identification.",
  77. NULL
  78. },
  79. {
  80. SGX_ERROR_INVALID_SIGNATURE,
  81. "Invalid enclave signature.",
  82. NULL
  83. },
  84. {
  85. SGX_ERROR_OUT_OF_EPC,
  86. "Out of EPC memory.",
  87. NULL
  88. },
  89. {
  90. SGX_ERROR_NO_DEVICE,
  91. "Invalid SGX device.",
  92. "Please make sure SGX module is enabled in the BIOS, and install SGX driver afterwards."
  93. },
  94. {
  95. SGX_ERROR_MEMORY_MAP_CONFLICT,
  96. "Memory map conflicted.",
  97. NULL
  98. },
  99. {
  100. SGX_ERROR_INVALID_METADATA,
  101. "Invalid enclave metadata.",
  102. NULL
  103. },
  104. {
  105. SGX_ERROR_DEVICE_BUSY,
  106. "SGX device was busy.",
  107. NULL
  108. },
  109. {
  110. SGX_ERROR_INVALID_VERSION,
  111. "Enclave version was invalid.",
  112. NULL
  113. },
  114. {
  115. SGX_ERROR_INVALID_ATTRIBUTE,
  116. "Enclave was not authorized.",
  117. NULL
  118. },
  119. {
  120. SGX_ERROR_ENCLAVE_FILE_ACCESS,
  121. "Can't open enclave file.",
  122. NULL
  123. },
  124. };
  125. /* Check error conditions for loading enclave */
  126. void print_error_message(sgx_status_t ret)
  127. {
  128. size_t idx = 0;
  129. size_t ttl = sizeof sgx_errlist/sizeof sgx_errlist[0];
  130. for (idx = 0; idx < ttl; idx++) {
  131. if(ret == sgx_errlist[idx].err) {
  132. if(NULL != sgx_errlist[idx].sug)
  133. printf("Info: %s\n", sgx_errlist[idx].sug);
  134. printf("Error: %s\n", sgx_errlist[idx].msg);
  135. break;
  136. }
  137. }
  138. if (idx == ttl)
  139. printf("Error: Unexpected error occurred.\n");
  140. }
  141. /* Initialize the enclave:
  142. * Step 1: try to retrieve the launch token saved by last transaction
  143. * Step 2: call sgx_create_enclave to initialize an enclave instance
  144. * Step 3: save the launch token if it is updated
  145. */
  146. int initialize_enclave(void)
  147. {
  148. char token_path[MAX_PATH] = {'\0'};
  149. sgx_launch_token_t token = {0};
  150. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  151. int updated = 0;
  152. /* Step 1: try to retrieve the launch token saved by last transaction
  153. * if there is no token, then create a new one.
  154. */
  155. /* try to get the token saved in $HOME */
  156. const char *home_dir = getpwuid(getuid())->pw_dir;
  157. if (home_dir != NULL &&
  158. (strlen(home_dir)+strlen("/")+sizeof(TOKEN_FILENAME)+1) <= MAX_PATH) {
  159. /* compose the token path */
  160. strncpy(token_path, home_dir, strlen(home_dir));
  161. strncat(token_path, "/", strlen("/"));
  162. strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME)+1);
  163. } else {
  164. /* if token path is too long or $HOME is NULL */
  165. strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME));
  166. }
  167. FILE *fp = fopen(token_path, "rb");
  168. if (fp == NULL && (fp = fopen(token_path, "wb")) == NULL) {
  169. printf("Warning: Failed to create/open the launch token file \"%s\".\n", token_path);
  170. }
  171. if (fp != NULL) {
  172. /* read the token from saved file */
  173. size_t read_num = fread(token, 1, sizeof(sgx_launch_token_t), fp);
  174. if (read_num != 0 && read_num != sizeof(sgx_launch_token_t)) {
  175. /* if token is invalid, clear the buffer */
  176. memset(&token, 0x0, sizeof(sgx_launch_token_t));
  177. printf("Warning: Invalid launch token read from \"%s\".\n", token_path);
  178. }
  179. }
  180. /* Step 2: call sgx_create_enclave to initialize an enclave instance */
  181. /* Debug Support: set 2nd parameter to 1 */
  182. ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, &token, &updated, &global_eid, NULL);
  183. if (ret != SGX_SUCCESS) {
  184. print_error_message(ret);
  185. if (fp != NULL) fclose(fp);
  186. return -1;
  187. }
  188. /* Step 3: save the launch token if it is updated */
  189. if (updated == FALSE || fp == NULL) {
  190. /* if the token is not updated, or file handler is invalid, do not perform saving */
  191. if (fp != NULL) fclose(fp);
  192. return 0;
  193. }
  194. /* reopen the file with write capablity */
  195. fp = freopen(token_path, "wb", fp);
  196. if (fp == NULL) return 0;
  197. size_t write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp);
  198. if (write_num != sizeof(sgx_launch_token_t))
  199. printf("Warning: Failed to save launch token to \"%s\".\n", token_path);
  200. fclose(fp);
  201. return 0;
  202. }
  203. /* OCall functions */
  204. void ocall_print_string(const char *str)
  205. {
  206. /* Proxy/Bridge will check the length and null-terminate
  207. * the input string to prevent buffer overflow.
  208. */
  209. printf("%s", str);
  210. }
  211. /* Application entry */
  212. int SGX_CDECL main(int argc, char *argv[])
  213. {
  214. (void)(argc);
  215. (void)(argv);
  216. /* Initialize the enclave */
  217. if(initialize_enclave() < 0){
  218. printf("Enter a character before exit ...\n");
  219. getchar();
  220. return -1;
  221. }
  222. /* Utilize edger8r attributes */
  223. edger8r_array_attributes();
  224. edger8r_pointer_attributes();
  225. edger8r_type_attributes();
  226. edger8r_function_attributes();
  227. /* Utilize trusted libraries */
  228. ecall_libc_functions();
  229. ecall_libcxx_functions();
  230. ecall_thread_functions();
  231. /* Destroy the enclave */
  232. sgx_destroy_enclave(global_eid);
  233. printf("Info: SampleEnclave successfully returned.\n");
  234. printf("Enter a character before exit ...\n");
  235. getchar();
  236. return 0;
  237. }