AEGetQuoteRequest.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <AEGetQuoteRequest.h>
  32. #include <AEGetQuoteResponse.h>
  33. #include "IAESMLogic.h"
  34. #include <stdlib.h>
  35. #include <limits.h>
  36. #include <IAEMessage.h>
  37. AEGetQuoteRequest::AEGetQuoteRequest(const aesm::message::Request::GetQuoteRequest& request) :
  38. m_request(NULL)
  39. {
  40. m_request = new aesm::message::Request::GetQuoteRequest();
  41. m_request->CopyFrom(request);
  42. }
  43. AEGetQuoteRequest::AEGetQuoteRequest(uint32_t reportLength, const uint8_t* report,
  44. uint32_t quoteType,
  45. uint32_t spidLength, const uint8_t* spid,
  46. uint32_t nonceLength, const uint8_t* nonce,
  47. uint32_t sig_rlLength, const uint8_t* sig_rl,
  48. uint32_t bufferSize,
  49. bool qe_report,
  50. uint32_t timeout)
  51. : m_request(NULL)
  52. {
  53. m_request = new aesm::message::Request::GetQuoteRequest();
  54. if (reportLength !=0 && report != NULL)
  55. m_request->set_report(report, reportLength);
  56. if (spidLength != 0 && spid != NULL)
  57. m_request->set_spid(spid, spidLength);
  58. if (nonceLength != 0 && nonce != NULL)
  59. m_request->set_nonce(nonce, nonceLength);
  60. if (sig_rlLength != 0 && sig_rl != NULL)
  61. m_request->set_sig_rl(sig_rl, sig_rlLength);
  62. m_request->set_quote_type(quoteType);
  63. m_request->set_buf_size(bufferSize);
  64. m_request->set_qe_report(qe_report);
  65. m_request->set_timeout(timeout);
  66. }
  67. AEGetQuoteRequest::AEGetQuoteRequest(const AEGetQuoteRequest& other)
  68. : m_request(NULL)
  69. {
  70. if (other.m_request != NULL)
  71. m_request = new aesm::message::Request::GetQuoteRequest(*other.m_request);
  72. }
  73. AEGetQuoteRequest::~AEGetQuoteRequest()
  74. {
  75. if (m_request != NULL)
  76. delete m_request;}
  77. AEMessage* AEGetQuoteRequest::serialize()
  78. {
  79. AEMessage *ae_msg = NULL;
  80. aesm::message::Request msg;
  81. if (check())
  82. {
  83. aesm::message::Request::GetQuoteRequest* mutableReq = msg.mutable_getquotereq();
  84. mutableReq->CopyFrom(*m_request);
  85. if (msg.ByteSize() <= INT_MAX) {
  86. ae_msg = new AEMessage;
  87. ae_msg->size = (unsigned int)msg.ByteSize();
  88. ae_msg->data = new char[ae_msg->size];
  89. msg.SerializeToArray(ae_msg->data, ae_msg->size);
  90. }
  91. }
  92. return ae_msg;
  93. }
  94. AEGetQuoteRequest& AEGetQuoteRequest::operator=(const AEGetQuoteRequest& other)
  95. {
  96. if (this == &other)
  97. return *this;
  98. if (m_request != NULL)
  99. {
  100. delete m_request;
  101. m_request = NULL;
  102. }
  103. if (other.m_request != NULL)
  104. m_request = new aesm::message::Request::GetQuoteRequest(*other.m_request);
  105. return *this;
  106. }
  107. bool AEGetQuoteRequest::check()
  108. {
  109. if (m_request == NULL)
  110. return false;
  111. return m_request->IsInitialized();
  112. }
  113. IAERequest::RequestClass AEGetQuoteRequest::getRequestClass() {
  114. return QUOTING_CLASS;
  115. }
  116. IAEResponse* AEGetQuoteRequest::execute(IAESMLogic* aesmLogic) {
  117. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  118. uint32_t qe_report_length = 0;
  119. uint8_t* qe_report = NULL;
  120. uint32_t quote_length = 0;
  121. uint8_t* quote = NULL;
  122. if (check())
  123. {
  124. uint32_t report_length = 0;
  125. uint8_t* report = NULL;
  126. uint32_t spid_length = 0;
  127. uint8_t* spid = NULL;
  128. uint32_t nonce_length = 0;
  129. uint8_t* nonce = NULL;
  130. uint32_t sig_rl_length = 0;
  131. uint8_t* sig_rl = NULL;
  132. if (m_request->has_report())
  133. {
  134. report_length = (unsigned int)m_request->report().size();
  135. report = (uint8_t*)const_cast<char *>(m_request->report().data());
  136. }
  137. if (m_request->has_spid())
  138. {
  139. spid_length = (unsigned int)m_request->spid().size();
  140. spid = (uint8_t*)const_cast<char *>(m_request->spid().data());
  141. }
  142. if (m_request->has_nonce())
  143. {
  144. nonce_length = (unsigned int)m_request->nonce().size();
  145. nonce = (uint8_t*)const_cast<char *>(m_request->nonce().data());
  146. }
  147. if (m_request->has_sig_rl())
  148. {
  149. sig_rl_length = (unsigned int)m_request->sig_rl().size();
  150. sig_rl = (uint8_t*)const_cast<char *>(m_request->sig_rl().data());
  151. }
  152. quote_length = (uint32_t)m_request->buf_size();
  153. result = aesmLogic->getQuote(report_length, report,
  154. (uint32_t)m_request->quote_type(),
  155. spid_length, spid,
  156. nonce_length, nonce,
  157. sig_rl_length, sig_rl,
  158. quote_length, &quote,
  159. m_request->qe_report(), &qe_report_length, &qe_report);
  160. }
  161. AEGetQuoteResponse* response = new AEGetQuoteResponse(result, quote_length, quote, qe_report_length, qe_report);
  162. //free the buffer before send
  163. if (quote)
  164. delete[] quote;
  165. if (qe_report)
  166. delete[] qe_report;
  167. return response;
  168. }