ias_ra.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "service_provider.h"
  32. #include "sample_libcrypto.h"
  33. #include "ecp.h"
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <stddef.h>
  37. #include <time.h>
  38. #include <string.h>
  39. #include "ias_ra.h"
  40. // @TODO: This whole file is used as simulation of the interfaces to be
  41. // delivered the IAS. Once the interface definitions are made available by the
  42. // IAS, this file should be changed accordingly.
  43. #define UNUSED(expr) do { (void)(expr); } while (0)
  44. #if !defined(SWAP_ENDIAN_DW)
  45. #define SWAP_ENDIAN_DW(dw) ((((dw) & 0x000000ff) << 24) \
  46. | (((dw) & 0x0000ff00) << 8) \
  47. | (((dw) & 0x00ff0000) >> 8) \
  48. | (((dw) & 0xff000000) >> 24))
  49. #endif
  50. #if !defined(SWAP_ENDIAN_32B)
  51. #define SWAP_ENDIAN_32B(ptr) \
  52. {\
  53. unsigned int temp = 0; \
  54. temp = SWAP_ENDIAN_DW(((unsigned int*)(ptr))[0]); \
  55. ((unsigned int*)(ptr))[0] = SWAP_ENDIAN_DW(((unsigned int*)(ptr))[7]); \
  56. ((unsigned int*)(ptr))[7] = temp; \
  57. temp = SWAP_ENDIAN_DW(((unsigned int*)(ptr))[1]); \
  58. ((unsigned int*)(ptr))[1] = SWAP_ENDIAN_DW(((unsigned int*)(ptr))[6]); \
  59. ((unsigned int*)(ptr))[6] = temp; \
  60. temp = SWAP_ENDIAN_DW(((unsigned int*)(ptr))[2]); \
  61. ((unsigned int*)(ptr))[2] = SWAP_ENDIAN_DW(((unsigned int*)(ptr))[5]); \
  62. ((unsigned int*)(ptr))[5] = temp; \
  63. temp = SWAP_ENDIAN_DW(((unsigned int*)(ptr))[3]); \
  64. ((unsigned int*)(ptr))[3] = SWAP_ENDIAN_DW(((unsigned int*)(ptr))[4]); \
  65. ((unsigned int*)(ptr))[4] = temp; \
  66. }
  67. #endif
  68. // This is the ECDSA NIST P-256 private key used to sign platform_info_blob.
  69. // This private
  70. // key and the public key in SDK untrusted KElibrary should be a temporary key
  71. // pair. For production parts the IAS will sign the platform_info_blob with the
  72. // production private key and the SDK untrusted KE library will have the public
  73. // key for verifcation.
  74. // @TODO: This key will will not be available when the production backend
  75. // is avaialbe. The remote attestation sample will need to change to use the
  76. // real backend. This will likely be an RSA2048 type of key.
  77. static const sample_ec256_private_t g_rk_priv_key =
  78. {{
  79. 0x63,0x2c,0xd4,0x02,0x7a,0xdc,0x56,0xa5,
  80. 0x59,0x6c,0x44,0x3e,0x43,0xca,0x4e,0x0b,
  81. 0x58,0xcd,0x78,0xcb,0x3c,0x7e,0xd5,0xb9,
  82. 0xf2,0x91,0x5b,0x39,0x0d,0xb3,0xb5,0xfb
  83. }};
  84. static sample_spid_t g_sim_spid = {"Service X"};
  85. // Simulates the IAS function for verifying the quote produce by
  86. // the ISV enclave. It doesn't decrypt or verify the quote in
  87. // the simulation. Just produces the attestaion verification
  88. // report with the platform info blob.
  89. //
  90. // @param p_isv_quote Pointer to the quote generated by the ISV
  91. // enclave.
  92. // @param pse_manifest Pointer to the PSE manifest if used.
  93. // @param p_attestation_verification_report Pointer the outputed
  94. // verification report.
  95. //
  96. // @return int
  97. int ias_verify_attestation_evidence(
  98. sample_quote_t *p_isv_quote,
  99. uint8_t* pse_manifest,
  100. ias_att_report_t* p_attestation_verification_report)
  101. {
  102. int ret = 0;
  103. sample_ecc_state_handle_t ecc_state = NULL;
  104. //unused parameters
  105. UNUSED(pse_manifest);
  106. if((NULL == p_isv_quote) ||
  107. (NULL == p_attestation_verification_report))
  108. {
  109. return -1;
  110. }
  111. //Decrypt the Quote signature and verify.
  112. p_attestation_verification_report->id = 0x12345678;
  113. p_attestation_verification_report->status = IAS_QUOTE_OK;
  114. p_attestation_verification_report->revocation_reason =
  115. IAS_REVOC_REASON_NONE;
  116. p_attestation_verification_report->info_blob.sample_epid_group_status =
  117. 0 << IAS_EPID_GROUP_STATUS_REVOKED_BIT_POS
  118. | 0 << IAS_EPID_GROUP_STATUS_REKEY_AVAILABLE_BIT_POS;
  119. p_attestation_verification_report->info_blob.sample_tcb_evaluation_status =
  120. 0 << IAS_TCB_EVAL_STATUS_CPUSVN_OUT_OF_DATE_BIT_POS
  121. | 0 << IAS_TCB_EVAL_STATUS_ISVSVN_OUT_OF_DATE_BIT_POS;
  122. p_attestation_verification_report->info_blob.pse_evaluation_status =
  123. 0 << IAS_PSE_EVAL_STATUS_ISVSVN_OUT_OF_DATE_BIT_POS
  124. | 0 << IAS_PSE_EVAL_STATUS_EPID_GROUP_REVOKED_BIT_POS
  125. | 0 << IAS_PSE_EVAL_STATUS_PSDASVN_OUT_OF_DATE_BIT_POS
  126. | 0 << IAS_PSE_EVAL_STATUS_SIGRL_OUT_OF_DATE_BIT_POS
  127. | 0 << IAS_PSE_EVAL_STATUS_PRIVRL_OUT_OF_DATE_BIT_POS;
  128. memset(p_attestation_verification_report->
  129. info_blob.latest_equivalent_tcb_psvn, 0, PSVN_SIZE);
  130. memset(p_attestation_verification_report->info_blob.latest_pse_isvsvn,
  131. 0, ISVSVN_SIZE);
  132. memset(p_attestation_verification_report->info_blob.latest_psda_svn,
  133. 0, PSDA_SVN_SIZE);
  134. memset(p_attestation_verification_report->info_blob.performance_rekey_gid,
  135. 0, GID_SIZE);
  136. // @TODO: Product signing algorithm still TBD. May be RSA2048 signing.
  137. // Generate the Service providers ECCDH key pair.
  138. do {
  139. ret = sample_ecc256_open_context(&ecc_state);
  140. if (SAMPLE_SUCCESS != ret) {
  141. fprintf(stderr, "\nError, cannot get ECC cotext in [%s].",
  142. __FUNCTION__);
  143. ret = -1;
  144. break;
  145. }
  146. // Sign
  147. ret = sample_ecdsa_sign(
  148. (uint8_t *)&p_attestation_verification_report->
  149. info_blob.sample_epid_group_status,
  150. sizeof(ias_platform_info_blob_t) - sizeof(sample_ec_sign256_t),
  151. (sample_ec256_private_t *)&g_rk_priv_key,
  152. (sample_ec256_signature_t *)&p_attestation_verification_report->
  153. info_blob.signature,
  154. ecc_state);
  155. if (SAMPLE_SUCCESS != ret) {
  156. fprintf(stderr, "\nError, sign ga_gb fail in [%s].", __FUNCTION__);
  157. ret = SP_INTERNAL_ERROR;
  158. break;
  159. }
  160. SWAP_ENDIAN_32B(p_attestation_verification_report->
  161. info_blob.signature.x);
  162. SWAP_ENDIAN_32B(p_attestation_verification_report->
  163. info_blob.signature.y);
  164. }while (0);
  165. if (ecc_state) {
  166. sample_ecc256_close_context(ecc_state);
  167. }
  168. p_attestation_verification_report->pse_status = IAS_PSE_OK;
  169. // For now, don't simulate the policy reports.
  170. p_attestation_verification_report->policy_report_size = 0;
  171. return(ret);
  172. }
  173. // Simulates retrieving the SIGRL for upon the SP request. Becaue the IAS
  174. // backend is not ready right now, we will return NULL.
  175. //
  176. // @param gid Group ID for the EPID key.
  177. // @param p_sig_rl_size Pointer to the output value of the full
  178. // SIGRL size in bytes. (including the
  179. // signature).
  180. // @param p_sig_rl Pointer to the output of the SIGRL.
  181. //
  182. // @return int
  183. int ias_get_sigrl(
  184. const sample_epid_group_id_t gid,
  185. uint32_t *p_sig_rl_size,
  186. uint8_t **p_sig_rl)
  187. {
  188. int ret = 0;
  189. UNUSED(gid);
  190. do {
  191. if (NULL == p_sig_rl || NULL == p_sig_rl_size) {
  192. ret = -1;
  193. break;
  194. }
  195. *p_sig_rl_size = 0;
  196. *p_sig_rl = NULL;
  197. // we should try to get sig_rl from IAS, but right now we will just
  198. // skip it until the IAS backend is ready.
  199. break;
  200. }while (0);
  201. return(ret);
  202. }
  203. // Used to simulate the enrollment function of the IAS. It only
  204. // gives back the SPID right now. In production, the enrollment
  205. // occurs out of context from an attestation attempt and only
  206. // occurs once.
  207. //
  208. //
  209. // @param sp_credentials
  210. // @param p_spid
  211. // @param p_authentication_token
  212. //
  213. // @return int
  214. int ias_enroll(
  215. int sp_credentials,
  216. sample_spid_t *p_spid,
  217. int *p_authentication_token)
  218. {
  219. UNUSED(sp_credentials);
  220. UNUSED(p_authentication_token);
  221. if (NULL != p_spid) {
  222. memcpy_s(p_spid, sizeof(sample_spid_t), &g_sim_spid,
  223. sizeof(sample_spid_t));
  224. } else {
  225. return(1);
  226. }
  227. return(0);
  228. }