CertificateProvisioningProtocol.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2011-2017 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 "CertificateProvisioningProtocol.h"
  32. #include <cstddef>
  33. #include "oal/oal.h"
  34. #include "network_encoding_wrapper.h"
  35. CertificateProvisioningProtocol::CertificateProvisioningProtocol() :
  36. m_is_initialized(false),
  37. m_url(""),
  38. m_nextState(msg_next_state_M1),
  39. generalResponseStatus(GRS_OK),
  40. protocolResponseStatus(PSE_PRS_OK)
  41. {
  42. }
  43. CertificateProvisioningProtocol::~CertificateProvisioningProtocol(void)
  44. {
  45. }
  46. extern public_key_t s_public_key;
  47. ae_error_t CertificateProvisioningProtocol::init(const char* szURL, const signed_pek_t& pek)
  48. {
  49. if (NULL == szURL)
  50. return AESM_PSE_PR_BACKEND_INVALID_URL;
  51. m_url = szURL;
  52. memset(&m_publicKey, 0, sizeof(m_publicKey));
  53. // Swap n
  54. for (unsigned i = 0, j = static_cast<uint32_t>(sizeof(pek.n)-1); i < sizeof(pek.n); i++, j--)
  55. {
  56. m_publicKey.n[i] = pek.n[j];
  57. }
  58. // Swap e
  59. for (unsigned i = 0, j = static_cast<uint32_t>(sizeof(pek.e)-1); i < sizeof(pek.e); i++, j--)
  60. {
  61. ((uint8_t*)&m_publicKey.e)[i] = pek.e[j];
  62. }
  63. m_is_initialized = true;
  64. return AE_SUCCESS;
  65. }
  66. ae_error_t CertificateProvisioningProtocol::SendM1_ReceiveM2
  67. ( /*in */ const uint32_t gid,
  68. /*out*/ upse::Buffer& nonce,
  69. /*out*/ upse::Buffer& sigRLBuffer
  70. )
  71. {
  72. ae_error_t status = AE_FAILURE;
  73. upse::Buffer serializedMsg1;
  74. upse::Buffer serializedMsg2;
  75. do
  76. {
  77. BREAK_IF_FALSE((m_is_initialized), status, AESM_PSE_PR_BACKEND_NOT_INITIALIZED);
  78. BREAK_IF_FALSE( (msg_next_state_M1 == m_nextState), status, AESM_PSE_PR_CALL_ORDER_ERROR);
  79. status = msg1_generate(*(const GroupId*)&gid, serializedMsg1);
  80. BREAK_IF_FAILED_ERR(status, AESM_PSE_PR_BACKEND_MSG1_GENERATE);
  81. // BREAK_IF_FAILED(status);
  82. status = sendReceive(serializedMsg1, serializedMsg2);
  83. if (AE_FAILED(status))
  84. break;
  85. status = msg2_process(serializedMsg2, nonce, sigRLBuffer);
  86. if (AE_FAILED(status))
  87. break;
  88. m_nextState = msg_next_state_M3;
  89. } while (0);
  90. return status;
  91. }
  92. ae_error_t CertificateProvisioningProtocol::SendM3_ReceiveM4
  93. ( /*in */ const upse::Buffer& csrBuffer,
  94. /*in */ const upse::Buffer& quoteBuffer,
  95. /*out*/ std::list< upse::Buffer >& certificateChainList,
  96. /*out*/ platform_info_blob_wrapper_t& piBlobWrapper
  97. )
  98. {
  99. ae_error_t status = AE_FAILURE;
  100. upse::Buffer serializedMsg3;
  101. upse::Buffer serializedMsg4;
  102. AESM_DBG_TRACE("start to send M3");
  103. do
  104. {
  105. BREAK_IF_FALSE((m_is_initialized), status, AESM_PSE_PR_BACKEND_NOT_INITIALIZED);
  106. BREAK_IF_FALSE( (msg_next_state_M3 == m_nextState), status, AESM_PSE_PR_CALL_ORDER_ERROR);
  107. status = msg3_generate(csrBuffer, quoteBuffer, serializedMsg3);
  108. BREAK_IF_FAILED_ERR(status, AESM_PSE_PR_BACKEND_MSG3_GENERATE);
  109. // BREAK_IF_FAILED(status);
  110. AESM_DBG_TRACE("M3 generated");
  111. status = sendReceive(serializedMsg3, serializedMsg4);
  112. BREAK_IF_FAILED(status);
  113. AESM_DBG_TRACE("start to process M4");
  114. status = msg4_process(serializedMsg4, certificateChainList, piBlobWrapper);
  115. BREAK_IF_FAILED(status);
  116. AESM_DBG_TRACE("finished M4");
  117. } while (0);
  118. m_nextState = msg_next_state_init;
  119. return status;
  120. }
  121. //***************************************************************************************************************************************
  122. //***************************************************************************************************************************************
  123. //***************************************************************************************************************************************
  124. ae_error_t CertificateProvisioningProtocol::check_response_header(const provision_response_header_t& header, uint8_t msg_type, uint32_t msg_size)
  125. {
  126. ae_error_t status = AE_FAILURE;
  127. do
  128. {
  129. if (sizeof(provision_request_header_t) > msg_size)
  130. break;
  131. if (header.protocol != PSE_PROVISIONING || header.type != msg_type || header.version < TLV_VERSION_1)
  132. break;
  133. const uint32_t* temp = reinterpret_cast<const uint32_t*>(header.size);
  134. uint32_t totalSize = _ntohl(*temp);
  135. if (totalSize + PROVISION_RESPONSE_HEADER_SIZE != msg_size)
  136. break;
  137. if (sizeof(header.xid) != TransactionID.getSize())
  138. break;
  139. if (0 != memcmp(header.xid, TransactionID.getData(), sizeof(header.xid)))
  140. break;
  141. status = AE_SUCCESS;
  142. } while (0);
  143. return status;
  144. }
  145. ae_error_t CertificateProvisioningProtocol::check_response_status(const provision_response_header_t& msg2_header)
  146. {
  147. ae_error_t status = PVE_SERVER_REPORTED_ERROR;
  148. do
  149. {
  150. generalResponseStatus = static_cast<general_response_status_t>(lv_ntohs(msg2_header.gstatus));
  151. protocolResponseStatus = static_cast<pse_protocol_response_status_t>(lv_ntohs(msg2_header.pstatus));
  152. // gstatus: GRS_OK, GRS_SERVER_BUSY, GRS_INTEGRITY_CHECK_FAIL, GRS_INCORRECT_SYNTAX,
  153. // GRS_INCOMPATIBLE_VERSION, GRS_TRANSACTION_STATE_LOST, GRS_PROTOCOL_ERROR, GRS_INTERNAL_ERROR
  154. // pstatus: PSE_PRS_OK, PSE_PRS_INVALID_GID, PSE_PRS_GID_REVOKED, PSE_PRS_INVALID_QUOTE, PSE_PRS_INVALID_REQUEST
  155. if (protocolResponseStatus != PSE_PRS_OK || generalResponseStatus != GRS_OK)
  156. break;
  157. status = AE_SUCCESS;
  158. } while (0);
  159. return status;
  160. }
  161. ae_error_t CertificateProvisioningProtocol::sendReceive(const upse::Buffer& sendSerialized, upse::Buffer& recvSerialized)
  162. {
  163. ae_error_t status = AE_FAILURE;
  164. uint8_t* recv = NULL;
  165. uint32_t recv_size = 0;
  166. do
  167. {
  168. AESM_DBG_INFO("start send msg");
  169. status = AESMNetworkEncoding::aesm_send_recv_msg_encoding(m_url.c_str(), const_cast<uint8_t *>(sendSerialized.getData()), sendSerialized.getSize(), recv, recv_size);
  170. if (AE_FAILED(status))
  171. break;
  172. AESM_DBG_INFO("msg received with size %u", recv_size);
  173. status = recvSerialized.Alloc(recv_size);
  174. if (AE_FAILED(status))
  175. break;
  176. AESM_DBG_INFO("buffer alloced");
  177. status = upse::BufferWriter(recvSerialized).writeRaw(recv, recv_size);
  178. if (AE_FAILED(status))
  179. break;
  180. AESM_DBG_INFO("buffer written");
  181. status = AE_SUCCESS;
  182. } while (0);
  183. if (NULL != recv)
  184. AESMNetworkEncoding::aesm_free_response_msg(recv);
  185. return status;
  186. }