isv_enclave.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2011-2016 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 "isv_enclave_t.h"
  32. #include "sgx_tkey_exchange.h"
  33. #include "sgx_tcrypto.h"
  34. #include "string.h"
  35. // This is the public EC key of the SP. The corresponding private EC key is
  36. // used by the SP to sign data used in the remote attestation SIGMA protocol
  37. // to sign channel binding data in MSG2. A successful verification of the
  38. // signature confirms the identity of the SP to the ISV app in remote
  39. // attestation secure channel binding. The public EC key should be hardcoded in
  40. // the enclave or delivered in a trustworthy manner. The use of a spoofed public
  41. // EC key in the remote attestation with secure channel binding session may lead
  42. // to a security compromise. Every different SP the enlcave communicates to
  43. // must have a unique SP public key. Delivery of the SP public key is
  44. // determined by the ISV. The TKE SIGMA protocl expects an Elliptical Curve key
  45. // based on NIST P-256
  46. static const sgx_ec256_public_t g_sp_pub_key = {
  47. {
  48. 0x72, 0x12, 0x8a, 0x7a, 0x17, 0x52, 0x6e, 0xbf,
  49. 0x85, 0xd0, 0x3a, 0x62, 0x37, 0x30, 0xae, 0xad,
  50. 0x3e, 0x3d, 0xaa, 0xee, 0x9c, 0x60, 0x73, 0x1d,
  51. 0xb0, 0x5b, 0xe8, 0x62, 0x1c, 0x4b, 0xeb, 0x38
  52. },
  53. {
  54. 0xd4, 0x81, 0x40, 0xd9, 0x50, 0xe2, 0x57, 0x7b,
  55. 0x26, 0xee, 0xb7, 0x41, 0xe7, 0xc6, 0x14, 0xe2,
  56. 0x24, 0xb7, 0xbd, 0xc9, 0x03, 0xf2, 0x9a, 0x28,
  57. 0xa8, 0x3c, 0xc8, 0x10, 0x11, 0x14, 0x5e, 0x06
  58. }
  59. };
  60. // Used to store the secret passed by the SP in the sample code. The
  61. // size is forced to be 8 bytes. Expected value is
  62. // 0x01,0x02,0x03,0x04,0x0x5,0x0x6,0x0x7
  63. uint8_t g_secret[8] = {0};
  64. // This ecall is a wrapper of sgx_ra_init to create the trusted
  65. // KE exchange key context needed for the remote attestation
  66. // SIGMA API's. Input pointers aren't checked since the trusted stubs
  67. // copy them into EPC memory.
  68. //
  69. // @param b_pse Indicates whether the ISV app is using the
  70. // platform services.
  71. // @param p_context Pointer to the location where the returned
  72. // key context is to be copied.
  73. //
  74. // @return Any error return from the create PSE session if b_pse
  75. // is true.
  76. // @return Any error returned from the trusted key exchange API
  77. // for creating a key context.
  78. sgx_status_t enclave_init_ra(
  79. int b_pse,
  80. sgx_ra_context_t *p_context)
  81. {
  82. // isv enclave call to trusted key exchange library.
  83. sgx_status_t ret;
  84. if(b_pse)
  85. {
  86. int busy_retry_times = 2;
  87. do{
  88. ret = sgx_create_pse_session();
  89. }while (ret == SGX_ERROR_BUSY && busy_retry_times--);
  90. if (ret != SGX_SUCCESS)
  91. return ret;
  92. }
  93. ret = sgx_ra_init(&g_sp_pub_key, b_pse, p_context);
  94. if(b_pse)
  95. {
  96. sgx_close_pse_session();
  97. return ret;
  98. }
  99. return ret;
  100. }
  101. // Closes the tKE key context used during the SIGMA key
  102. // exchange.
  103. //
  104. // @param context The trusted KE library key context.
  105. //
  106. // @return Return value from the key context close API
  107. sgx_status_t SGXAPI enclave_ra_close(
  108. sgx_ra_context_t context)
  109. {
  110. sgx_status_t ret;
  111. ret = sgx_ra_close(context);
  112. return ret;
  113. }
  114. // Verify the mac sent in att_result_msg from the SP using the
  115. // MK key. Input pointers aren't checked since the trusted stubs
  116. // copy them into EPC memory.
  117. //
  118. //
  119. // @param context The trusted KE library key context.
  120. // @param p_message Pointer to the message used to produce MAC
  121. // @param message_size Size in bytes of the message.
  122. // @param p_mac Pointer to the MAC to compare to.
  123. // @param mac_size Size in bytes of the MAC
  124. //
  125. // @return SGX_ERROR_INVALID_PARAMETER - MAC size is incorrect.
  126. // @return Any error produced by tKE API to get SK key.
  127. // @return Any error produced by the AESCMAC function.
  128. // @return SGX_ERROR_MAC_MISMATCH - MAC compare fails.
  129. sgx_status_t verify_att_result_mac(sgx_ra_context_t context,
  130. uint8_t* p_message,
  131. size_t message_size,
  132. uint8_t* p_mac,
  133. size_t mac_size)
  134. {
  135. sgx_status_t ret;
  136. sgx_ec_key_128bit_t mk_key;
  137. if(mac_size != sizeof(sgx_mac_t))
  138. {
  139. ret = SGX_ERROR_INVALID_PARAMETER;
  140. return ret;
  141. }
  142. if(message_size > UINT32_MAX)
  143. {
  144. ret = SGX_ERROR_INVALID_PARAMETER;
  145. return ret;
  146. }
  147. do {
  148. uint8_t mac[SGX_CMAC_MAC_SIZE] = {0};
  149. ret = sgx_ra_get_keys(context, SGX_RA_KEY_MK, &mk_key);
  150. if(SGX_SUCCESS != ret)
  151. {
  152. break;
  153. }
  154. ret = sgx_rijndael128_cmac_msg(&mk_key,
  155. p_message,
  156. (uint32_t)message_size,
  157. &mac);
  158. if(SGX_SUCCESS != ret)
  159. {
  160. break;
  161. }
  162. if(0 == consttime_memequal(p_mac, mac, sizeof(mac)))
  163. {
  164. ret = SGX_ERROR_MAC_MISMATCH;
  165. break;
  166. }
  167. }
  168. while(0);
  169. return ret;
  170. }
  171. // Generate a secret information for the SP encrypted with SK.
  172. // Input pointers aren't checked since the trusted stubs copy
  173. // them into EPC memory.
  174. //
  175. // @param context The trusted KE library key context.
  176. // @param p_secret Message containing the secret.
  177. // @param secret_size Size in bytes of the secret message.
  178. // @param p_gcm_mac The pointer the the AESGCM MAC for the
  179. // message.
  180. //
  181. // @return SGX_ERROR_INVALID_PARAMETER - secret size if
  182. // incorrect.
  183. // @return Any error produced by tKE API to get SK key.
  184. // @return Any error produced by the AESGCM function.
  185. // @return SGX_ERROR_UNEXPECTED - the secret doesn't match the
  186. // expected value.
  187. sgx_status_t put_secret_data(
  188. sgx_ra_context_t context,
  189. uint8_t *p_secret,
  190. uint32_t secret_size,
  191. uint8_t *p_gcm_mac)
  192. {
  193. sgx_status_t ret = SGX_SUCCESS;
  194. sgx_ec_key_128bit_t sk_key;
  195. do {
  196. if(secret_size != 8)
  197. {
  198. ret = SGX_ERROR_INVALID_PARAMETER;
  199. break;
  200. }
  201. ret = sgx_ra_get_keys(context, SGX_RA_KEY_SK, &sk_key);
  202. if(SGX_SUCCESS != ret)
  203. {
  204. break;
  205. }
  206. uint8_t aes_gcm_iv[12] = {0};
  207. ret = sgx_rijndael128GCM_decrypt(&sk_key,
  208. p_secret,
  209. secret_size,
  210. &g_secret[0],
  211. &aes_gcm_iv[0],
  212. 12,
  213. NULL,
  214. 0,
  215. (const sgx_aes_gcm_128bit_tag_t *)
  216. (p_gcm_mac));
  217. uint32_t i;
  218. bool secret_match = true;
  219. for(i=0;i<secret_size;i++)
  220. {
  221. if(g_secret[i] != i)
  222. {
  223. secret_match = false;
  224. }
  225. }
  226. if(!secret_match)
  227. {
  228. ret = SGX_ERROR_UNEXPECTED;
  229. }
  230. // Once the server has the shared secret, it should be sealed to
  231. // persistent storage for future use. This will prevents having to
  232. // perform remote attestation until the secret goes stale. Once the
  233. // enclave is created again, the secret can be unsealed.
  234. } while(0);
  235. return ret;
  236. }