provision_msg1.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "provision_msg.h"
  32. #include "sgx_trts.h"
  33. #include "cipher.h"
  34. #include "helper.h"
  35. #include "protocol.h"
  36. #include "ipp_wrapper.h"
  37. #include "sgx_tcrypto.h"
  38. #include "pve_hardcoded_tlv_data.h"
  39. #include "sgx_utils.h"
  40. #include "pve_qe_common.h"
  41. #include "pek_pub_key.h"
  42. #include "pce_cert.h"
  43. #include "arch.h"
  44. #include <string.h>
  45. #include <stdlib.h>
  46. /**
  47. * File: provision_msg1.cpp
  48. * Description: Provide the implementation of code to generate data for ProvMsg1
  49. *
  50. * Core Code of Provision Enclave
  51. */
  52. //generate data for ProvMsg1, the REPORT for PEK public key
  53. //The function return PVEC_SUCCESS on success or other error code to indicate error
  54. //@pce_target_info: target_info of PCE enclave
  55. //@xegb: extended epid group blob used
  56. //@pek: The PEK public key signed by PEKSK
  57. //@pek_report: output PvE REPORT of PEK public key so that PCE could verify it by using Local Attestation
  58. //@return PVEC_SUCCESS on success
  59. pve_status_t gen_prov_msg1_data(const sgx_target_info_t& pce_target_info,
  60. const extended_epid_group_blob_t& xegb,
  61. const signed_pek_t& pek,
  62. sgx_report_t& pek_report)
  63. {
  64. pve_status_t ret = PVEC_SUCCESS;
  65. sgx_status_t sgx_status = SGX_SUCCESS;
  66. uint8_t pek_result = SGX_EC_INVALID_SIGNATURE;
  67. sgx_report_data_t report_data = {0};
  68. extended_epid_group_blob_t local_xegb;
  69. sgx_sha_state_handle_t sha_handle = NULL;
  70. uint8_t crypto_suite = ALG_RSA_OAEP_2048;
  71. sgx_status = verify_xegb_with_default(xegb, &pek_result, local_xegb);
  72. if(SGX_SUCCESS != sgx_status){
  73. ret = sgx_error_to_pve_error(sgx_status);
  74. goto ret_point;
  75. }else if(pek_result != SGX_EC_VALID){
  76. ret = PVEC_XEGDSK_SIGN_ERROR;
  77. goto ret_point;
  78. }
  79. sgx_status = check_pek_signature(pek, (sgx_ec256_public_t*)local_xegb.pek_sk, &pek_result);
  80. if(SGX_SUCCESS != sgx_status){
  81. ret = sgx_error_to_pve_error(sgx_status);
  82. goto ret_point;
  83. }else if(pek_result != SGX_EC_VALID){
  84. ret = PVEC_PEK_SIGN_ERROR; //use a special error code to indicate PEK Signature error
  85. goto ret_point;
  86. }
  87. se_static_assert(sizeof(report_data)>=sizeof(sgx_sha256_hash_t)); /*hash size is too large to be hold by report*/
  88. //report_data = SHA256(crypto_suite||public_key)||0-padding
  89. do
  90. {
  91. sgx_status = sgx_sha256_init(&sha_handle);
  92. if (SGX_SUCCESS != sgx_status)
  93. break;
  94. sgx_status = sgx_sha256_update(&crypto_suite, sizeof(uint8_t),
  95. sha_handle);
  96. if (SGX_SUCCESS != sgx_status)
  97. break;
  98. //(MOD followed by e)
  99. sgx_status = sgx_sha256_update(pek.n, sizeof(pek.n),
  100. sha_handle);
  101. if (SGX_SUCCESS != sgx_status)
  102. break;
  103. sgx_status = sgx_sha256_update(pek.e, sizeof(pek.e),
  104. sha_handle);
  105. if (SGX_SUCCESS != sgx_status)
  106. break;
  107. sgx_status = sgx_sha256_get_hash(sha_handle, reinterpret_cast<sgx_sha256_hash_t *>(&report_data));
  108. } while (0);
  109. if (sha_handle != NULL)
  110. sgx_sha256_close(sha_handle);
  111. if(SGX_SUCCESS != sgx_status){
  112. ret = sgx_error_to_pve_error(sgx_status);
  113. goto ret_point;
  114. }
  115. if((pce_target_info.attributes.flags & SGX_FLAGS_PROVISION_KEY)!=SGX_FLAGS_PROVISION_KEY ||
  116. (pce_target_info.attributes.flags & SGX_FLAGS_DEBUG) != 0){
  117. //PCE must have access to provisioning key
  118. //Can't be debug PCE
  119. ret = PVEC_PARAMETER_ERROR;
  120. goto ret_point;
  121. }
  122. sgx_status = sgx_create_report(&pce_target_info, &report_data, &pek_report);
  123. if(SGX_SUCCESS != sgx_status){
  124. ret = sgx_error_to_pve_error(sgx_status);
  125. goto ret_point;
  126. }
  127. ret_point:
  128. if(PVEC_SUCCESS != ret){//clear critical output data on error
  129. (void)memset_s(&pek_report, sizeof(pek_report), 0, sizeof(pek_report));
  130. }
  131. return ret;
  132. }