AEInitQuoteResponse.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <AEInitQuoteResponse.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. AEInitQuoteResponse::AEInitQuoteResponse() :
  36. mGIDLength(0),
  37. mTargetInfoLength(0),
  38. mTargetInfo(NULL),
  39. mGID(NULL)
  40. {
  41. }
  42. AEInitQuoteResponse::AEInitQuoteResponse(int errorCode, uint32_t gidLength, const uint8_t* gid,
  43. uint32_t targetInfoLength, const uint8_t* targetInfo) :
  44. mGIDLength(0),
  45. mTargetInfoLength(0),
  46. mTargetInfo(NULL),
  47. mGID(NULL)
  48. {
  49. CopyFields(errorCode, gidLength, gid, targetInfoLength, targetInfo);
  50. }
  51. AEInitQuoteResponse::AEInitQuoteResponse(const AEInitQuoteResponse& other) :
  52. mGIDLength(0),
  53. mTargetInfoLength(0),
  54. mTargetInfo(NULL),
  55. mGID(NULL)
  56. {
  57. CopyFields(other.mErrorCode, other.mGIDLength, other.mGID, other.mTargetInfoLength, other.mTargetInfo);
  58. }
  59. AEInitQuoteResponse::~AEInitQuoteResponse()
  60. {
  61. ReleaseMemory();
  62. }
  63. void AEInitQuoteResponse::ReleaseMemory()
  64. {
  65. if (mGID != NULL){
  66. delete [] mGID;
  67. mGID = NULL;
  68. mGIDLength = 0;
  69. }
  70. if (mTargetInfo != NULL)
  71. {
  72. delete [] mTargetInfo;
  73. mTargetInfo = NULL;
  74. mTargetInfoLength = 0;
  75. }
  76. mErrorCode = SGX_ERROR_UNEXPECTED;
  77. }
  78. void AEInitQuoteResponse::CopyFields(int errorCode, uint32_t gidLength, const uint8_t* gid,
  79. uint32_t targetInfoLength, const uint8_t* targetInfo)
  80. {
  81. uint32_t totalSize = gidLength + targetInfoLength;
  82. if(gidLength <= MAX_MEMORY_ALLOCATION && targetInfoLength <= MAX_MEMORY_ALLOCATION
  83. && totalSize <= MAX_MEMORY_ALLOCATION )
  84. {
  85. mValidSizeCheck = true;
  86. }
  87. else
  88. {
  89. mValidSizeCheck = false;
  90. return;
  91. }
  92. mErrorCode = errorCode;
  93. mGIDLength = gidLength;
  94. if (gid == NULL)
  95. mGID = NULL;
  96. else
  97. {
  98. mGID = new uint8_t[mGIDLength];
  99. memcpy(mGID, gid, mGIDLength);
  100. }
  101. mTargetInfoLength = targetInfoLength;
  102. mTargetInfo = new uint8_t[mTargetInfoLength];
  103. memcpy(mTargetInfo, targetInfo, mTargetInfoLength);
  104. }
  105. AEMessage* AEInitQuoteResponse::serialize(ISerializer* serializer)
  106. {
  107. return serializer->serialize(this);
  108. }
  109. bool AEInitQuoteResponse::inflateWithMessage(AEMessage* message, ISerializer *serializer)
  110. {
  111. return serializer->inflateResponse(message, this);
  112. }
  113. void AEInitQuoteResponse::inflateValues(int errorCode, uint32_t gidLength, const uint8_t* gid,
  114. uint32_t targetInfoLength, const uint8_t* targetInfo)
  115. {
  116. ReleaseMemory();
  117. CopyFields(errorCode, gidLength, gid, targetInfoLength, targetInfo);
  118. }
  119. bool AEInitQuoteResponse::operator==(const AEInitQuoteResponse &other) const
  120. {
  121. if (this == &other)
  122. return true;
  123. if (mTargetInfoLength != other.mTargetInfoLength ||
  124. mGIDLength != other.mGIDLength)
  125. return false;
  126. if (memcmp(mGID, other.mGID, mGIDLength) != 0)
  127. return false;
  128. if (memcmp(mTargetInfo, other.mTargetInfo, mTargetInfoLength) != 0)
  129. return false;
  130. return true;
  131. }
  132. AEInitQuoteResponse & AEInitQuoteResponse::operator=(const AEInitQuoteResponse &other)
  133. {
  134. if (this == &other)
  135. return *this;
  136. inflateValues(other.mErrorCode, other.mGIDLength, other.mGID, other.mTargetInfoLength, other.mTargetInfo);
  137. return *this;
  138. }
  139. bool AEInitQuoteResponse::check()
  140. {
  141. // no MAC to check at this point, but do some generic parameter check
  142. //first, fail if errorCode is not 0
  143. if (mErrorCode != SGX_SUCCESS)
  144. return false;
  145. if (mValidSizeCheck == false)
  146. return false;
  147. //check memory allocation fail in this object
  148. if (mTargetInfoLength > 0 && mTargetInfo == NULL)
  149. return false;
  150. if (mGIDLength > 0 && mGID == NULL)
  151. return false;
  152. return true;
  153. }
  154. void AEInitQuoteResponse::visit(IAEResponseVisitor& visitor)
  155. {
  156. visitor.visitInitQuoteResponse(*this);
  157. }