monotonic_counter.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.h"
  32. #include "monotonic_counter_database_sqlite_rpdb.h"
  33. #include <assert.h>
  34. static pse_op_error_t handle_vmc_errors(const pse_op_error_t op_error, pse_service_resp_status_t* status)
  35. {
  36. switch(op_error)
  37. {
  38. case OP_SUCCESS:
  39. /* SUCCESS */
  40. *status = PSE_SUCCESS;
  41. return OP_SUCCESS;
  42. case OP_ERROR_INVALID_COUNTER:
  43. /* No VMC entry matches the counter ID */
  44. *status = PSE_ERROR_MC_NOT_FOUND;
  45. return OP_SUCCESS;
  46. case OP_ERROR_INVALID_OWNER:
  47. *status = PSE_ERROR_MC_NO_ACCESS_RIGHT;
  48. return OP_SUCCESS;
  49. case OP_ERROR_CAP_NOT_AVAILABLE:
  50. *status = PSE_ERROR_CAP_NOT_AVAILABLE;
  51. return OP_SUCCESS;
  52. case OP_ERROR_DATABASE_FULL:
  53. *status = PSE_ERROR_MC_USED_UP;
  54. return OP_SUCCESS;
  55. case OP_ERROR_DATABASE_OVER_QUOTA:
  56. *status = PSE_ERROR_MC_OVER_QUOTA;
  57. return OP_SUCCESS;
  58. case OP_ERROR_INVALID_POLICY:
  59. *status = PSE_ERROR_INVALID_POLICY;
  60. return OP_SUCCESS;
  61. case OP_ERROR_PSDA_BUSY:
  62. *status = PSE_ERROR_BUSY;
  63. return OP_SUCCESS;
  64. // --- errors cannot be translated to status code
  65. case OP_ERROR_INVALID_EPH_SESSION:
  66. case OP_ERROR_PSDA_SESSION_LOST:
  67. return op_error; // should not be translated to status code
  68. // --- end
  69. default:
  70. // OP_ERROR_INTERNAL
  71. // OP_ERROR_INVALID_PARAMETER
  72. // OP_ERROR_MALLOC
  73. // OP_ERROR_SQLITE_INTERNAL
  74. // OP_ERROR_UNKNOWN_REQUEST
  75. // OP_ERROR_COPY_PREBUILD_DB
  76. *status = PSE_ERROR_INTERNAL;
  77. return OP_SUCCESS;
  78. }
  79. }
  80. pse_op_error_t pse_mc_create(
  81. const isv_attributes_t &owner_attributes,
  82. const uint8_t* req,
  83. uint8_t* resp)
  84. {
  85. assert(req != NULL && resp != NULL);
  86. pse_op_error_t op_ret = OP_SUCCESS;
  87. const pse_mc_create_req_t* create_req = (const pse_mc_create_req_t*)req;
  88. pse_mc_create_resp_t* create_resp = (pse_mc_create_resp_t*)resp;
  89. //initial create_resp
  90. memset(create_resp->counter_id, 0xFF, UUID_ENTRY_INDEX_SIZE);
  91. memset(create_resp->nonce, 0x0, UUID_NONCE_SIZE);
  92. mc_rpdb_uuid_t uuid;
  93. vmc_data_blob_t data;
  94. memset(&uuid, 0xff, sizeof(uuid));
  95. memset(&data, 0, sizeof(data));
  96. memcpy(data.owner_attr_mask, create_req->attr_mask, sizeof(data.owner_attr_mask));
  97. data.owner_policy = create_req->policy;
  98. if(0 == (create_req->policy & (MC_POLICY_SIGNER | MC_POLICY_ENCLAVE))) // Invalid policy
  99. {
  100. return OP_ERROR_INVALID_POLICY;
  101. }
  102. op_ret = create_vmc(owner_attributes, data, uuid);
  103. if(OP_SUCCESS == op_ret)
  104. {
  105. memcpy(create_resp->counter_id, uuid.entry_index, UUID_ENTRY_INDEX_SIZE);
  106. memcpy(create_resp->nonce, uuid.nonce, UUID_NONCE_SIZE);
  107. }
  108. return handle_vmc_errors(op_ret, &create_resp->resp_hdr.status);
  109. }
  110. pse_op_error_t pse_mc_read(
  111. const isv_attributes_t &owner_attributes,
  112. const uint8_t* req,
  113. uint8_t* resp)
  114. {
  115. assert(req != NULL && resp != NULL);
  116. pse_op_error_t op_ret = OP_SUCCESS;
  117. const pse_mc_read_req_t* read_req = (const pse_mc_read_req_t*)req;
  118. pse_mc_read_resp_t* read_resp = (pse_mc_read_resp_t*)resp;
  119. vmc_data_blob_t vmc;
  120. mc_rpdb_uuid_t uuid;
  121. memset(&vmc, 0, sizeof(vmc));
  122. //initial read_resp
  123. read_resp->counter_value = 0;
  124. memcpy(uuid.entry_index, read_req->counter_id, UUID_ENTRY_INDEX_SIZE);
  125. memcpy(uuid.nonce, read_req->nonce, UUID_NONCE_SIZE);
  126. op_ret = read_vmc(owner_attributes, uuid, vmc);
  127. if(OP_SUCCESS == op_ret)
  128. {
  129. read_resp->counter_value = vmc.value;
  130. }
  131. return handle_vmc_errors(op_ret, &read_resp->resp_hdr.status);;
  132. }
  133. pse_op_error_t pse_mc_inc(
  134. const isv_attributes_t &owner_attributes,
  135. const uint8_t* req,
  136. uint8_t* resp)
  137. {
  138. assert(req != NULL && resp != NULL);
  139. pse_op_error_t op_ret = OP_SUCCESS;
  140. const pse_mc_inc_req_t* inc_req = (const pse_mc_inc_req_t*)req;
  141. pse_mc_inc_resp_t* inc_resp = (pse_mc_inc_resp_t*)resp;
  142. //initial inc_resp
  143. inc_resp->counter_value = 0;
  144. vmc_data_blob_t vmc;
  145. mc_rpdb_uuid_t uuid;
  146. memset(&vmc, 0, sizeof(vmc));
  147. memcpy(uuid.entry_index, inc_req->counter_id, UUID_ENTRY_INDEX_SIZE);
  148. memcpy(uuid.nonce, inc_req->nonce, UUID_NONCE_SIZE);
  149. op_ret = inc_vmc(owner_attributes, uuid, vmc);
  150. if(op_ret == OP_SUCCESS)
  151. {
  152. inc_resp->counter_value = vmc.value;
  153. }
  154. return handle_vmc_errors(op_ret, &inc_resp->resp_hdr.status);;
  155. }
  156. pse_op_error_t pse_mc_del(
  157. const isv_attributes_t &owner_attributes,
  158. const uint8_t* req,
  159. uint8_t* resp)
  160. {
  161. assert(req != NULL && resp != NULL);
  162. pse_op_error_t op_ret;
  163. const pse_mc_del_req_t* del_req = (const pse_mc_del_req_t*)req;
  164. mc_rpdb_uuid_t uuid;
  165. memcpy(uuid.entry_index, del_req->counter_id, UUID_ENTRY_INDEX_SIZE);
  166. memcpy(uuid.nonce, del_req->nonce, UUID_NONCE_SIZE);
  167. op_ret = delete_vmc(owner_attributes, uuid);
  168. return handle_vmc_errors(op_ret, &((pse_mc_inc_resp_t*)resp)->resp_hdr.status);;
  169. }