AEGetLaunchTokenRequest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <AEGetLaunchTokenRequest.h>
  32. #include <AEGetLaunchTokenResponse.h>
  33. #include <IAESMLogic.h>
  34. #include <stdlib.h>
  35. #include <limits.h>
  36. #include <IAEMessage.h>
  37. AEGetLaunchTokenRequest::AEGetLaunchTokenRequest(const aesm::message::Request::GetLaunchTokenRequest& request) :
  38. m_request(NULL)
  39. {
  40. m_request = new aesm::message::Request::GetLaunchTokenRequest();
  41. m_request->CopyFrom(request);
  42. }
  43. AEGetLaunchTokenRequest::AEGetLaunchTokenRequest(uint32_t measurementLength, const uint8_t* measurement,
  44. uint32_t pubkeyLength, const uint8_t* pubkey,
  45. uint32_t attributesLength, const uint8_t* attributes,
  46. uint32_t timeout) :
  47. m_request(NULL)
  48. {
  49. m_request = new aesm::message::Request::GetLaunchTokenRequest();
  50. if (measurementLength != 0 && measurement != NULL)
  51. m_request->set_mr_enclave(measurement, measurementLength);
  52. if (pubkeyLength!= 0 && pubkey != NULL)
  53. m_request->set_mr_signer(pubkey, pubkeyLength);
  54. if (attributesLength != 0 && attributes != NULL)
  55. m_request->set_se_attributes(attributes, attributesLength);
  56. m_request->set_timeout(timeout);
  57. }
  58. AEGetLaunchTokenRequest::AEGetLaunchTokenRequest(const AEGetLaunchTokenRequest& other) :
  59. m_request(NULL)
  60. {
  61. if (other.m_request != NULL)
  62. m_request = new aesm::message::Request::GetLaunchTokenRequest(*other.m_request);
  63. }
  64. AEGetLaunchTokenRequest::~AEGetLaunchTokenRequest()
  65. {
  66. if (m_request != NULL)
  67. delete m_request;
  68. }
  69. AEMessage* AEGetLaunchTokenRequest::serialize()
  70. {
  71. AEMessage *ae_msg = NULL;
  72. aesm::message::Request msg;
  73. if (check())
  74. {
  75. aesm::message::Request::GetLaunchTokenRequest* mutableReq = msg.mutable_getlictokenreq();
  76. mutableReq->CopyFrom(*m_request);
  77. if (msg.ByteSize() <= INT_MAX) {
  78. ae_msg = new AEMessage;
  79. ae_msg->size = (unsigned int)msg.ByteSize();
  80. ae_msg->data = new char[ae_msg->size];
  81. msg.SerializeToArray(ae_msg->data, ae_msg->size);
  82. }
  83. }
  84. return ae_msg;
  85. }
  86. bool AEGetLaunchTokenRequest::check()
  87. {
  88. if (m_request == NULL)
  89. return false;
  90. return m_request->IsInitialized();
  91. }
  92. AEGetLaunchTokenRequest& AEGetLaunchTokenRequest::operator=(const AEGetLaunchTokenRequest& other)
  93. {
  94. if (this == &other)
  95. return *this;
  96. if (m_request != NULL)
  97. {
  98. delete m_request;
  99. m_request = NULL;
  100. }
  101. if (other.m_request != NULL)
  102. m_request = new aesm::message::Request::GetLaunchTokenRequest(*other.m_request);
  103. return *this;
  104. }
  105. IAERequest::RequestClass AEGetLaunchTokenRequest::getRequestClass() {
  106. return LAUNCH_CLASS;
  107. }
  108. IAEResponse* AEGetLaunchTokenRequest::execute(IAESMLogic* aesmLogic) {
  109. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  110. uint8_t* token = NULL;
  111. uint32_t tokenSize = 0;
  112. if (check())
  113. {
  114. uint32_t mr_enclave_length = 0;
  115. uint8_t* mr_enclave = NULL;
  116. uint32_t mr_signer_length = 0;
  117. uint8_t* mr_signer = NULL;
  118. uint32_t se_attributes_length = 0;
  119. uint8_t* se_attributes = NULL;
  120. if (m_request->has_mr_enclave())
  121. {
  122. mr_enclave_length = (unsigned int)m_request->mr_enclave().size();
  123. mr_enclave = (uint8_t*)const_cast<char *>(m_request->mr_enclave().data());
  124. }
  125. if (m_request->has_mr_signer())
  126. {
  127. mr_signer_length = (unsigned int)m_request->mr_signer().size();
  128. mr_signer = (uint8_t*)const_cast<char *>(m_request->mr_signer().data());
  129. }
  130. if (m_request->has_se_attributes())
  131. {
  132. se_attributes_length = (unsigned int)m_request->se_attributes().size();
  133. se_attributes = (uint8_t*)const_cast<char *>(m_request->se_attributes().data());
  134. }
  135. result = aesmLogic->getLaunchToken(mr_enclave, mr_enclave_length,
  136. mr_signer, mr_signer_length,
  137. se_attributes, se_attributes_length,
  138. &token, &tokenSize);
  139. }
  140. IAEResponse* response = new AEGetLaunchTokenResponse(result, tokenSize, token);
  141. //free the buffer before send
  142. if (token)
  143. delete [] token;
  144. return response;
  145. }