AEGetQuoteRequest.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <ISerializer.h>
  32. #include <AEGetQuoteRequest.h>
  33. #include <AEGetQuoteResponse.h>
  34. #include "IAESMLogic.h"
  35. #include <stdlib.h>
  36. #include <sgx_report.h>
  37. AEGetQuoteRequest::AEGetQuoteRequest()
  38. :mReportLength(0), mReport(NULL), mQuoteType(0), mSPIDLength(0), mSPID(NULL),
  39. mNonceLength(0), mNonce(NULL), mSigRLLength(0), mSigRL(NULL), mBufferSize(0), mQEReport(false)
  40. {
  41. mValidSizeCheck = false;
  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. :mReportLength(0), mReport(NULL), mQuoteType(0), mSPIDLength(0), mSPID(NULL),
  52. mNonceLength(0), mNonce(NULL), mSigRLLength(0), mSigRL(NULL), mBufferSize(0), mQEReport(false)
  53. {
  54. CopyFields(reportLength, report, quoteType, spidLength, spid,
  55. nonceLength, nonce, sig_rlLength, sig_rl, bufferSize, qe_report, timeout);
  56. }
  57. AEGetQuoteRequest::AEGetQuoteRequest(const AEGetQuoteRequest& other)
  58. : IAERequest(other)
  59. {
  60. CopyFields(other.mReportLength, other.mReport,
  61. other.mQuoteType,
  62. other.mSPIDLength, other.mSPID,
  63. other.mNonceLength, other.mNonce,
  64. other.mSigRLLength, other.mSigRL,
  65. other.mBufferSize,
  66. other.mQEReport,
  67. other.mTimeout);
  68. }
  69. AEGetQuoteRequest::~AEGetQuoteRequest()
  70. {
  71. ReleaseMemory();
  72. }
  73. AEMessage* AEGetQuoteRequest::serialize(ISerializer* serializer)
  74. {
  75. return serializer->serialize(this);
  76. }
  77. void AEGetQuoteRequest::CopyFields(uint32_t reportLength, const uint8_t* report,
  78. uint32_t quoteType,
  79. uint32_t spidLength, const uint8_t* spid,
  80. uint32_t nonceLength, const uint8_t* nonce,
  81. uint32_t sig_rlLength, const uint8_t* sig_rl,
  82. uint32_t bufferSize,
  83. bool qe_report,
  84. uint32_t timeout)
  85. {
  86. uint32_t totalAllocation = reportLength + spidLength + nonceLength + sig_rlLength;
  87. if(reportLength <= MAX_MEMORY_ALLOCATION && spidLength <= MAX_MEMORY_ALLOCATION &&
  88. nonceLength <= MAX_MEMORY_ALLOCATION && sig_rlLength <= MAX_MEMORY_ALLOCATION &&
  89. totalAllocation <= MAX_MEMORY_ALLOCATION)
  90. {
  91. mValidSizeCheck = true;
  92. }
  93. else
  94. {
  95. mReport = NULL;
  96. mReportLength = 0;
  97. mSigRL = NULL;
  98. mSigRLLength = 0;
  99. mNonce = NULL;
  100. mNonceLength = 0;
  101. mSPID = NULL;
  102. mSPIDLength = 0;
  103. mValidSizeCheck = false;
  104. return;
  105. }
  106. mReportLength = reportLength;
  107. if (reportLength > 0 && report != NULL)
  108. {
  109. mReport = new uint8_t[reportLength];
  110. memcpy(mReport, report, reportLength);
  111. }
  112. else
  113. mReport = NULL;
  114. mSigRLLength = sig_rlLength;
  115. if (sig_rl != NULL && sig_rlLength > 0)
  116. {
  117. mSigRL = new uint8_t[sig_rlLength];
  118. memcpy(mSigRL, sig_rl, sig_rlLength);
  119. }
  120. else
  121. mSigRL = NULL;
  122. mNonceLength = nonceLength;
  123. if (nonce != NULL && nonceLength > 0)
  124. {
  125. mNonce = new uint8_t[nonceLength];
  126. memcpy(mNonce, nonce, nonceLength);
  127. }
  128. else
  129. mNonce = NULL;
  130. mSPIDLength = spidLength;
  131. if (spid != NULL && spidLength > 0)
  132. {
  133. mSPID = new uint8_t[spidLength];
  134. memcpy(mSPID, spid, spidLength);
  135. }
  136. else
  137. mSPID = NULL;
  138. mQuoteType = quoteType;
  139. mBufferSize = bufferSize;
  140. mQEReport = qe_report;
  141. mTimeout = timeout;
  142. }
  143. void AEGetQuoteRequest::inflateValues(uint32_t reportLength, const uint8_t* report,
  144. uint32_t quoteType,
  145. uint32_t spidLength, const uint8_t* spid,
  146. uint32_t nonceLength, const uint8_t* nonce,
  147. uint32_t sig_rlLength, const uint8_t* sig_rl,
  148. uint32_t bufferSize,
  149. bool qe_report,
  150. uint32_t timeout)
  151. {
  152. ReleaseMemory();
  153. CopyFields(reportLength, report, quoteType, spidLength, spid,
  154. nonceLength, nonce, sig_rlLength, sig_rl, bufferSize, qe_report, timeout);
  155. }
  156. void AEGetQuoteRequest::ReleaseMemory()
  157. {
  158. if (mReport != NULL)
  159. {
  160. if (mReportLength > 0)
  161. memset(mReport,0,mReportLength);
  162. delete [] mReport;
  163. mReport = NULL;
  164. }
  165. if (mSigRL != NULL)
  166. {
  167. if (mSigRLLength > 0)
  168. memset(mSigRL, 0, mSigRLLength);
  169. delete [] mSigRL;
  170. mSigRL = NULL;
  171. }
  172. if (mNonce != NULL)
  173. {
  174. if (mNonceLength > 0)
  175. memset(mNonce, 0, mNonceLength);
  176. delete [] mNonce;
  177. mNonce = NULL;
  178. }
  179. if (mSPID != NULL)
  180. {
  181. if (mSPIDLength > 0)
  182. memset(mSPID, 0, mSPIDLength);
  183. delete [] mSPID;
  184. mSPID = NULL;
  185. }
  186. mReportLength = 0;
  187. mSigRLLength = 0;
  188. mNonceLength = 0;
  189. mSPIDLength = 0;
  190. mBufferSize = 0;
  191. mQuoteType = 0;
  192. mQEReport = false;
  193. mTimeout = 0;
  194. }
  195. bool AEGetQuoteRequest::operator==(const AEGetQuoteRequest& other) const
  196. {
  197. if (&other == this)
  198. return true;
  199. if (mReportLength != other.mReportLength ||
  200. mSigRLLength != other.mSigRLLength ||
  201. mNonceLength != other.mNonceLength ||
  202. mSPIDLength != other.mSPIDLength ||
  203. mQuoteType != other.mQuoteType ||
  204. mBufferSize != other.mBufferSize ||
  205. mQEReport != other.mQEReport ||
  206. mTimeout != other.mTimeout)
  207. return false;
  208. if ((mReport != other.mReport) &&
  209. (mReport == NULL || other.mReport == NULL))
  210. return false;
  211. if ((mSigRL != other.mSigRL) &&
  212. (mSigRL == NULL || other.mSigRL == NULL))
  213. return false;
  214. if ((mNonce != other.mNonce) &&
  215. (mNonce == NULL || other.mNonce == NULL))
  216. return false;
  217. if ((mSPID != other.mSPID) &&
  218. (mSPID == NULL || other.mSPID == NULL))
  219. return false;
  220. if ((mReport != NULL && other.mReport != NULL && memcmp(mReport, other.mReport, mReportLength) != 0) ||
  221. (mSigRL != NULL && other.mSigRL != NULL && memcmp(mSigRL, other.mSigRL, mSigRLLength) != 0) ||
  222. (mNonce != NULL && other.mNonce != NULL && memcmp(mNonce, other.mNonce, mNonceLength) != 0) ||
  223. (mSPID != NULL && other.mSPID != NULL && memcmp(mSPID, other.mSPID, mSPIDLength) !=0 ))
  224. return false;
  225. return true;
  226. }
  227. AEGetQuoteRequest& AEGetQuoteRequest::operator=(const AEGetQuoteRequest& other)
  228. {
  229. if (this == &other)
  230. return *this;
  231. inflateValues(other.mReportLength, other.mReport,
  232. other.mQuoteType,
  233. other.mSPIDLength, other.mSPID,
  234. other.mNonceLength, other.mNonce,
  235. other.mSigRLLength, other.mSigRL,
  236. other.mBufferSize,
  237. other.mQEReport,
  238. other.mTimeout);
  239. return *this;
  240. }
  241. bool AEGetQuoteRequest::check()
  242. {
  243. //maybe TODO - add stronger checks
  244. if(mValidSizeCheck == false)
  245. return false;
  246. //allocations - only non optional fields
  247. if (mReport == NULL || mSPID == NULL)
  248. return false;
  249. return true;
  250. }
  251. IAERequest::RequestClass AEGetQuoteRequest::getRequestClass() {
  252. return QUOTING_CLASS;
  253. }
  254. void AEGetQuoteRequest::visit(IAERequestVisitor& visitor)
  255. {
  256. visitor.visitGetQuoteRequest(*this);
  257. }
  258. IAEResponse* AEGetQuoteRequest::execute(IAESMLogic* aesmLogic) {
  259. aesm_error_t result;
  260. int32_t qe_report_length = 0;
  261. uint8_t* qe_report = NULL;
  262. if (mQEReport == true)
  263. {
  264. qe_report_length = sizeof(sgx_report_t);
  265. qe_report = new uint8_t[sizeof(sgx_report_t)];
  266. }
  267. uint8_t* quote = new uint8_t[mBufferSize];
  268. result = aesmLogic->getQuote(mReportLength,mReport,
  269. mQuoteType,
  270. mSPIDLength, mSPID,
  271. mNonceLength, mNonce,
  272. mSigRLLength, mSigRL,
  273. mBufferSize, quote,
  274. qe_report_length, qe_report);
  275. AEGetQuoteResponse* response = new AEGetQuoteResponse(result, mBufferSize, quote, qe_report_length, qe_report);
  276. //free memory
  277. delete [] quote;
  278. delete [] qe_report;
  279. return response;
  280. }