launch_checker.cpp 4.4 KB

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