launch_checker.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2011-2016 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 <string.h>
  32. #include "se_memcpy.h"
  33. #include "se_error_internal.h"
  34. #include "uae_service_internal.h"
  35. #include "launch_checker.h"
  36. static unsigned int chk_launch_token(
  37. const enclave_css_t *css,
  38. const sgx_attributes_t *secs_attr,
  39. const sgx_launch_token_t *tok)
  40. {
  41. const token_t *launch = reinterpret_cast<const token_t *>(tok);
  42. // Is it a valid launch_token? 0 = Invalid, 1 = Valid.
  43. if(!launch->body.valid)
  44. {
  45. // For Non-Architectural Enclaves: HWVERSION = 0
  46. // Anyway, EINIT will return SE_INVALID_LAUNCH_TOKEN if the Enclave
  47. // is not signed by intel key.
  48. if(0 == css->header.hw_version)
  49. {
  50. // If the licese token is invalid, only intel key signed
  51. // enclave can be lauched.
  52. return (unsigned int)SE_ERROR_INVALID_LAUNCH_TOKEN;
  53. }
  54. else
  55. {
  56. // Don't need launch token for intel key signed enclave.
  57. return (unsigned int)SGX_SUCCESS;
  58. }
  59. }
  60. // The MRENCLAVE should match the one in SIGSTRUCT. We must check
  61. // it here, becaue EINIT will return SE_INVALID_MEASUREMENT for
  62. // this case.
  63. if(memcmp(&launch->body.mr_enclave, &css->body.enclave_hash, sizeof(sgx_measurement_t)))
  64. {
  65. return (unsigned int)SE_ERROR_INVALID_LAUNCH_TOKEN;
  66. }
  67. // The ATTRIBUTES should match the attributes in SECS.
  68. if(memcmp(&launch->body.attributes, secs_attr, sizeof(sgx_attributes_t)))
  69. {
  70. return (unsigned int)SE_ERROR_INVALID_LAUNCH_TOKEN;
  71. }
  72. // Other checks are done in get_secs_attr later.
  73. return SGX_SUCCESS;
  74. }
  75. SGXLaunchToken::SGXLaunchToken(
  76. const enclave_css_t *css,
  77. const sgx_attributes_t *secs_attr,
  78. const sgx_launch_token_t *launch)
  79. :m_css(css), m_secs_attr(secs_attr), m_launch_updated(false)
  80. {
  81. memcpy_s(m_launch, sizeof(m_launch), launch, sizeof(m_launch));
  82. }
  83. bool SGXLaunchToken::is_launch_updated() const
  84. {
  85. return m_launch_updated;
  86. }
  87. sgx_status_t SGXLaunchToken::get_launch_token(sgx_launch_token_t *tok) const
  88. {
  89. if (memcpy_s(tok, sizeof(m_launch), &m_launch, sizeof(m_launch)))
  90. return SGX_ERROR_UNEXPECTED;
  91. return SGX_SUCCESS;
  92. }
  93. sgx_status_t SGXLaunchToken::update_launch_token(
  94. bool force_update_tok)
  95. {
  96. sgx_status_t status = SGX_SUCCESS;
  97. if (force_update_tok ||
  98. SE_ERROR_INVALID_LAUNCH_TOKEN == chk_launch_token(m_css, m_secs_attr, &m_launch))
  99. {
  100. status = ::get_launch_token(m_css, m_secs_attr, &m_launch);
  101. if (status == SGX_SUCCESS)
  102. m_launch_updated = true;
  103. else
  104. return status;
  105. }
  106. return status;
  107. }