monotonic_counter_database_sqlite_check_error.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "monotonic_counter_database_sqlite_check_error.h"
  32. #include "monotonic_counter_database_sqlite_bin_hash_tree_utility.h"
  33. #include "monotonic_counter_database_sqlite_access_hw_mc.h"
  34. #include "pse_op_t.h"
  35. #include "sgx_tcrypto.h"
  36. #include "session_mgr.h"
  37. #include "sgx_sha256_128.h"
  38. #define CHECK_ERROR_CODE(ret, lable) if(OP_SUCCESS != ret) { ret = OP_ERROR_INTERNAL; goto lable; }
  39. /*******************************************************************
  40. ** Function name: verify_root_node
  41. ** Descrption: Update or verify the root node of the VMC hash tree by using specified
  42. ** MC value "mc".
  43. **
  44. *******************************************************************/
  45. static pse_op_error_t verify_root_node(pse_vmc_children_of_root_t* children)
  46. {
  47. sgx_status_t stat;
  48. pse_op_error_t ret;
  49. cal_root_hash_buf_t cal_root_hash_buf;
  50. uint8_t root_hash[ROOT_HASH_SIZE] = {0};
  51. uint8_t cached_root_hash[ROOT_HASH_SIZE] = {0};
  52. memset(&cal_root_hash_buf, 0, sizeof(cal_root_hash_buf_t));
  53. // children_hash[left_child_hash, right_child_hash], key_id, vmc_entry_nr, pairing_nonce
  54. memcpy(cal_root_hash_buf.children_hash, &children->left_child.internal, HASH_VALUE_SIZE);
  55. memcpy(cal_root_hash_buf.children_hash + HASH_VALUE_SIZE, &children->rigth_child.internal, HASH_VALUE_SIZE);
  56. // copy pairing nonce
  57. if (!copy_global_pairing_nonce(&cal_root_hash_buf.pairing_nonce[0]))
  58. return OP_ERROR_INTERNAL;
  59. ret = get_cached_rpepoch(&cal_root_hash_buf.rp_epoch);
  60. if(OP_SUCCESS != ret)
  61. {
  62. return OP_ERROR_INTERNAL;
  63. }
  64. ret = get_cached_roothash(cached_root_hash);
  65. if(OP_SUCCESS != ret)
  66. {
  67. return OP_ERROR_INTERNAL;
  68. }
  69. // calculate SHA256-128 for root node
  70. stat = sgx_sha256_128_msg((const uint8_t *)&cal_root_hash_buf, sizeof(cal_root_hash_buf_t), (sgx_sha256_128_hash_t*)root_hash);
  71. if (stat != SGX_SUCCESS)
  72. {
  73. return OP_ERROR_INTERNAL;
  74. }
  75. // compare root hash saved in PSDA's RPDATA and the calculated one
  76. if (0 != memcmp(cached_root_hash, root_hash, ROOT_HASH_SIZE) )
  77. {
  78. // root of the vmc database is invalid
  79. return OP_ERROR_INVALID_VMC_DB;
  80. }
  81. else
  82. {
  83. // root of the vmc database is valid
  84. return OP_SUCCESS;
  85. }
  86. }
  87. /*******************************************************************
  88. ** Function name: pse_vmc_database_check_error
  89. ** Descrption: check the integrity of the existed VMC Database, and try to fix fixable
  90. ** errors. The integrity check will only be taken on the root node of the VMC hash tree.
  91. **
  92. *******************************************************************/
  93. pse_op_error_t pse_vmc_database_check_error()
  94. {
  95. pse_vmc_children_of_root_t children;
  96. pse_op_error_t rc = OP_SUCCESS;
  97. memset(&children, 0, sizeof(pse_vmc_children_of_root_t));
  98. // Read children nodes of the vmc hash tree root node from SQLite DB via OCALL
  99. rc = get_db_children_of_root(&children);
  100. if(rc != OP_SUCCESS)
  101. {
  102. goto clean_up;
  103. }
  104. // Calculate the root hash by children nodes and then compare it with
  105. // the root hash read from DB and the root hash read from rpdata of PSDA.
  106. rc = verify_root_node(&children);
  107. if(OP_SUCCESS != rc)
  108. {
  109. if(OP_ERROR_INVALID_VMC_DB == rc)
  110. {
  111. // try to rollback
  112. rc = rollback_db_file();
  113. if(rc != OP_SUCCESS)
  114. {
  115. // If rollback recovery failed, should return theoriginal rc to upper layer, which will attempt to re-initialize the DB.
  116. rc = OP_ERROR_INVALID_VMC_DB;
  117. goto clean_up;
  118. }
  119. // get root and its chilren again
  120. memset(&children, 0, sizeof(pse_vmc_children_of_root_t));
  121. rc = get_db_children_of_root(&children);
  122. if(rc != OP_SUCCESS)
  123. {
  124. goto clean_up;
  125. }
  126. rc = verify_root_node(&children);
  127. if(rc != OP_SUCCESS)
  128. {
  129. goto clean_up;
  130. }
  131. }
  132. }
  133. clean_up:
  134. return rc;
  135. }