pse_provisioning_msg4.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "epid_utility.h"
  33. #include "tlv_common.h"
  34. #include "type_length_value.h"
  35. #define MSG4_FIELD_COUNT_MINIMUM 3
  36. //*********************************************************************************************************
  37. //* PSE_ProvMsg4
  38. //* Seq # Data Item
  39. //* ===== ============================================================================================
  40. //* 1 Request Header (Protocol, Version, TransactionID, Type)
  41. //* 2 X509 Certificate TLV (TLV Type, Type, Version, Size, [X509 Certificate]) - signed certificate issued by CA
  42. //* 3 X509 Certificate TLV (TLV Type, Type, Version, Size, [X509 Certificate]) - first certificate in CA's certificate chain
  43. //* X,* (Optional) X509 Certificate TLV --- a subsequent certificate in the CA's certificate chain (only present if chain has more than two elements)
  44. //* N (Optional) X509 Certificate TLV --- the last certificate in the CA's certificate chain (only present if chain has more than one element )
  45. //* N+1 (Optional) Platform Info Blob TLV (TLV Type, Type, Version, Size, [PlatformInfoBlob])
  46. //* N+2 Message Authentication Code TLV (TLV Type, Type, Version, Size, [MAC])
  47. //* MAC over 1, 2, 3, and 4:[X, N]
  48. //*********************************************************************************************************
  49. ae_error_t CertificateProvisioningProtocol::msg4_process(const upse::Buffer& serializedMsg4, std::list< upse::Buffer >& certificateChainList, platform_info_blob_wrapper_t& piBlobWrapper)
  50. {
  51. ae_error_t status = AE_FAILURE;
  52. tlv_status_t tlv_status;
  53. do
  54. {
  55. TLVsMsg tlvs;
  56. const provision_response_header_t& header = reinterpret_cast<const provision_response_header_t&>(*serializedMsg4.getData());
  57. status = check_response_header(header, TYPE_PSE_MSG4, serializedMsg4.getSize());
  58. BREAK_IF_FAILED_ERR(status, AESM_PSE_PR_BACKEND_MSG4_RESPONSE_HEADER_INTEGRITY);
  59. status = check_response_status(header);
  60. if (AE_FAILED(status))
  61. break;
  62. tlv_status= tlvs.init_from_buffer(serializedMsg4.getData() + PROVISION_RESPONSE_HEADER_SIZE, static_cast<uint32_t>(serializedMsg4.getSize() - PROVISION_RESPONSE_HEADER_SIZE));
  63. status = tlv_error_2_pve_error(tlv_status);
  64. if (AE_FAILED(status))
  65. break;
  66. status = msg4_validate_tlvs(tlvs);
  67. if (AE_FAILED(status))
  68. break;
  69. status = msg4_verify_mac(header, tlvs);
  70. if (AE_FAILED(status))
  71. break;
  72. status = msg4_get_certificates(tlvs, certificateChainList, piBlobWrapper);
  73. if (AE_FAILED(status))
  74. break;
  75. } while (0);
  76. return status;
  77. }
  78. ae_error_t CertificateProvisioningProtocol::msg4_validate_tlvs(const TLVsMsg& tlvs)
  79. {
  80. ae_error_t status = AESM_PSE_PR_BACKEND_MSG4_TLV_INTEGRITY;
  81. do
  82. {
  83. uint32_t tlv_count = tlvs.get_tlv_count();
  84. if (tlv_count < MSG4_FIELD_COUNT_MINIMUM)
  85. break;
  86. uint32_t mac_tlv_index = tlv_count - 1;
  87. // MAC TLV
  88. if (tlvs[mac_tlv_index].type != TLV_MESSAGE_AUTHENTICATION_CODE ||
  89. tlvs[mac_tlv_index].size != MAC_SIZE ||
  90. tlvs[mac_tlv_index].version < TLV_VERSION_1)
  91. {
  92. break;
  93. }
  94. uint32_t i;
  95. for (i = 0; i < mac_tlv_index; i++)
  96. {
  97. if (tlvs[i].type != TLV_X509_CERT_TLV || tlvs[i].version < TLV_VERSION_1)
  98. break;
  99. }
  100. if (i < mac_tlv_index - 1)
  101. break;
  102. if (i < mac_tlv_index)
  103. {
  104. // If we stopped before reaching MAC TLV, then TLV directly prior to MAC must be Platform Info Blob
  105. if (tlvs[i].type != TLV_PLATFORM_INFO_BLOB || tlvs[i].version < TLV_VERSION_1)
  106. break;
  107. }
  108. status = AE_SUCCESS;
  109. } while (0);
  110. return status;
  111. }
  112. ae_error_t CertificateProvisioningProtocol::msg4_verify_mac(const provision_response_header_t& header, const TLVsMsg& tlvs)
  113. {
  114. ae_error_t status = AE_FAILURE;
  115. do
  116. {
  117. // The MAC consists of the header and all TLVs, prior to the MAC TLV
  118. uint32_t bytes_to_mac = tlvs.get_tlv_msg_size() - MAC_TLV_SIZE(MAC_SIZE);
  119. uint32_t aadSize = static_cast<uint32_t>(sizeof(header)) + bytes_to_mac;
  120. uint32_t mac_tlv_index = tlvs.get_tlv_count() - 1;
  121. upse::Buffer aad;
  122. status = aad.Alloc(aadSize);
  123. if (AE_FAILED(status))
  124. break;
  125. upse::BufferWriter aadWriter(aad);
  126. status = aadWriter.writeRaw((const uint8_t*)&header, sizeof(header));
  127. if (AE_FAILED(status))
  128. break;
  129. status = aadWriter.writeRaw(const_cast<uint8_t*>(tlvs.get_tlv_msg()), bytes_to_mac);
  130. if (AE_FAILED(status))
  131. break;
  132. upse::Buffer mac;
  133. status = mac.Alloc(tlvs[mac_tlv_index].size);
  134. if (AE_FAILED(status))
  135. break;
  136. status = upse::BufferWriter(mac).writeRaw(tlvs[mac_tlv_index].payload, tlvs[mac_tlv_index].size);
  137. if (AE_FAILED(status))
  138. break;
  139. upse::Buffer emptyCipherText;
  140. upse::Buffer msg4_iv;
  141. status = M3IV.Not(msg4_iv);
  142. if (AE_FAILED(status))
  143. break;
  144. upse::Buffer plainText;
  145. status = aesGCMDecrypt(msg4_iv, EK2, emptyCipherText, aad, mac, plainText);
  146. if (AE_FAILED(status))
  147. break;
  148. status = AE_SUCCESS;
  149. } while (0);
  150. return status;
  151. }
  152. ae_error_t CertificateProvisioningProtocol::msg4_get_certificates(const TLVsMsg& tlvs, std::list< upse::Buffer >& certificateChainList, platform_info_blob_wrapper_t& piBlobWrapper)
  153. {
  154. // NOTE: With Backend Server 1.1.105.0, the order of TLV_X509_CERT_TLV was [LeafCertificate, CA CHAIN].
  155. // This was out of spec. It's fixed now and this comment is the only reminder of what was.
  156. ae_error_t status = AE_FAILURE;
  157. certificateChainList.clear();
  158. upse::Buffer leafCertificate;
  159. memset(&piBlobWrapper, 0, sizeof(piBlobWrapper));
  160. do
  161. {
  162. uint32_t mac_tlv_index = tlvs.get_tlv_count() - 1;
  163. uint32_t i;
  164. for (i = 0; i < mac_tlv_index; i++)
  165. {
  166. upse::Buffer b;
  167. status = b.Alloc(tlvs[i].size);
  168. if (AE_FAILED(status))
  169. break;
  170. status = upse::BufferWriter(b).writeRaw(tlvs[i].payload, tlvs[i].size);
  171. if (AE_FAILED(status))
  172. break;
  173. if (tlvs[i].type == TLV_X509_CERT_TLV)
  174. {
  175. if (i == 0)
  176. {
  177. //
  178. // this is sort of a reminder of the incorrect cert order in the poc
  179. // but leaving it here makes it easy to do checks here on the leaf cert
  180. //
  181. status = leafCertificate.Clone(b);
  182. if (AE_FAILED(status))
  183. break;
  184. }
  185. certificateChainList.push_back(b);
  186. }
  187. else if (tlvs[i].type == TLV_PLATFORM_INFO_BLOB)
  188. {
  189. if (sizeof(piBlobWrapper.platform_info_blob) > b.getSize())
  190. {
  191. status = AESM_PSE_PR_BACKEND_MSG4_PLATFORM_INFO_BLOB_SIZE;
  192. break;
  193. }
  194. // Ignore PIB during PSE cert provisioning.
  195. }
  196. else
  197. {
  198. status = AESM_PSE_PR_BACKEND_MSG4_UNEXPECTED_TLV_TYPE;
  199. break;
  200. }
  201. }
  202. if (i < mac_tlv_index)
  203. break;
  204. BREAK_IF_FALSE((leafCertificate.getSize() > 0), status, AESM_PSE_PR_BACKEND_MSG4_LEAF_CERTIFICATE_SIZE);
  205. status = AE_SUCCESS;
  206. } while (0);
  207. return status;
  208. }