sgx_verify_report.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /**
  32. * File: sgx_verify_report.cpp
  33. * Description:
  34. * API for report verification
  35. */
  36. #include "sgx_utils.h"
  37. #include "util.h"
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "se_memcpy.h"
  41. #include "sgx_trts.h"
  42. #include "sgx_tcrypto.h"
  43. #include "se_cdefs.h"
  44. // add a version to tservice.
  45. SGX_ACCESS_VERSION(tservice, 3)
  46. sgx_status_t sgx_verify_report(const sgx_report_t *report)
  47. {
  48. sgx_mac_t mac;
  49. sgx_key_request_t key_request;
  50. sgx_key_128bit_t key;
  51. sgx_status_t err = SGX_ERROR_UNEXPECTED;
  52. //check parameter
  53. if(!report||!sgx_is_within_enclave(report, sizeof(*report)))
  54. {
  55. return SGX_ERROR_INVALID_PARAMETER;
  56. }
  57. memset(&mac, 0, sizeof(sgx_mac_t));
  58. memset(&key_request, 0, sizeof(sgx_key_request_t));
  59. memset(&key, 0, sizeof(sgx_key_128bit_t));
  60. //prepare the key_request
  61. key_request.key_name = SGX_KEYSELECT_REPORT;
  62. memcpy_s(&key_request.key_id, sizeof(key_request.key_id), &report->key_id, sizeof(report->key_id));
  63. //get the report key
  64. // Since the key_request is not an input parameter by caller,
  65. // we suppose sgx_get_key would never return the following error code:
  66. // SGX_ERROR_INVALID_PARAMETER
  67. // SGX_ERROR_INVALID_ATTRIBUTE
  68. // SGX_ERROR_INVALID_CPUSVN
  69. // SGX_ERROR_INVALID_ISVSVN
  70. // SGX_ERROR_INVALID_KEYNAME
  71. err = sgx_get_key(&key_request, &key);
  72. if(err != SGX_SUCCESS)
  73. {
  74. return err; // err must be SGX_ERROR_OUT_OF_MEMORY or SGX_ERROR_UNEXPECTED
  75. }
  76. //get the report mac
  77. err = sgx_rijndael128_cmac_msg((sgx_cmac_128bit_key_t*)&key, (const uint8_t *)(&report->body), sizeof(sgx_report_body_t), &mac);
  78. memset_s (&key, sizeof(sgx_key_128bit_t), 0, sizeof(sgx_key_128bit_t));
  79. if (SGX_SUCCESS != err)
  80. {
  81. if(err != SGX_ERROR_OUT_OF_MEMORY)
  82. err = SGX_ERROR_UNEXPECTED;
  83. return err;
  84. }
  85. if(consttime_memequal(mac, report->mac, sizeof(sgx_mac_t)) == 0)
  86. {
  87. return SGX_ERROR_MAC_MISMATCH;
  88. }
  89. else
  90. {
  91. return SGX_SUCCESS;
  92. }
  93. }