provision_msg1.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "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_3072;
  71. static_assert(sizeof(pek.n) == 384, "pek.n should be 384 bytes");
  72. sgx_status = verify_xegb_with_default(xegb, &pek_result, local_xegb);
  73. if(SGX_SUCCESS != sgx_status){
  74. ret = sgx_error_to_pve_error(sgx_status);
  75. goto ret_point;
  76. }else if(pek_result != SGX_EC_VALID){
  77. ret = PVEC_XEGDSK_SIGN_ERROR;
  78. goto ret_point;
  79. }
  80. sgx_status = check_pek_signature(pek, (sgx_ec256_public_t*)local_xegb.pek_sk, &pek_result);
  81. if(SGX_SUCCESS != sgx_status){
  82. ret = sgx_error_to_pve_error(sgx_status);
  83. goto ret_point;
  84. }else if(pek_result != SGX_EC_VALID){
  85. ret = PVEC_PEK_SIGN_ERROR; //use a special error code to indicate PEK Signature error
  86. goto ret_point;
  87. }
  88. se_static_assert(sizeof(report_data)>=sizeof(sgx_sha256_hash_t)); /*hash size is too large to be hold by report*/
  89. //report_data = SHA256(crypto_suite||public_key)||0-padding
  90. do
  91. {
  92. sgx_status = sgx_sha256_init(&sha_handle);
  93. if (SGX_SUCCESS != sgx_status)
  94. break;
  95. sgx_status = sgx_sha256_update(&crypto_suite, sizeof(uint8_t),
  96. sha_handle);
  97. if (SGX_SUCCESS != sgx_status)
  98. break;
  99. //(MOD followed by e)
  100. sgx_status = sgx_sha256_update(pek.n, sizeof(pek.n),
  101. sha_handle);
  102. if (SGX_SUCCESS != sgx_status)
  103. break;
  104. sgx_status = sgx_sha256_update(pek.e, sizeof(pek.e),
  105. sha_handle);
  106. if (SGX_SUCCESS != sgx_status)
  107. break;
  108. sgx_status = sgx_sha256_get_hash(sha_handle, reinterpret_cast<sgx_sha256_hash_t *>(&report_data));
  109. } while (0);
  110. if (sha_handle != NULL)
  111. sgx_sha256_close(sha_handle);
  112. if(SGX_SUCCESS != sgx_status){
  113. ret = sgx_error_to_pve_error(sgx_status);
  114. goto ret_point;
  115. }
  116. if((pce_target_info.attributes.flags & SGX_FLAGS_PROVISION_KEY)!=SGX_FLAGS_PROVISION_KEY ||
  117. (pce_target_info.attributes.flags & SGX_FLAGS_DEBUG) != 0){
  118. //PCE must have access to provisioning key
  119. //Can't be debug PCE
  120. ret = PVEC_PARAMETER_ERROR;
  121. goto ret_point;
  122. }
  123. sgx_status = sgx_create_report(&pce_target_info, &report_data, &pek_report);
  124. if(SGX_SUCCESS != sgx_status){
  125. ret = sgx_error_to_pve_error(sgx_status);
  126. goto ret_point;
  127. }
  128. ret_point:
  129. if(PVEC_SUCCESS != ret){//clear critical output data on error
  130. (void)memset_s(&pek_report, sizeof(pek_report), 0, sizeof(pek_report));
  131. }
  132. return ret;
  133. }