pse_provisioning_msg2.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "epid_utility.h"
  34. #include "tlv_common.h"
  35. #include "type_length_value.h"
  36. #define MSG2_NONCE_INDEX 0
  37. #define MSG2_SIGRL_INDEX 1
  38. #define MSG2_MAC_INDEX_NO_SIGRL 1
  39. #define MSG2_MAC_INDEX_WITH_SIGRL 2
  40. #define MSG2_FIELD_COUNT_WITHOUT_SIGRL 2
  41. #define MSG2_FIELD_COUNT_WITH_SIGRL 3
  42. //*********************************************************************************************************
  43. //* PSE_ProvMsg2
  44. //* Seq # Data Item
  45. //* ===== ============================================================================================
  46. //* 1 Response Header (Protocol, Version, TransactionID, Type)
  47. //* 2 Nonce TLV (TLV Type, Type, Version, Size, [Nonce])
  48. //* 3 EPID SigRL Nonce TLV (Optional) (TLV Type, Type, Version, Size, [Nonce])
  49. //* 4 Message Authentication Code TLV (TLV Type, Type, Version, Size, [MAC])
  50. //* MAC over 1, 2, and 3
  51. //*********************************************************************************************************
  52. ae_error_t CertificateProvisioningProtocol::msg2_process(const upse::Buffer& serializedMsg2, upse::Buffer& nonce, upse::Buffer& sigRL)
  53. {
  54. ae_error_t status = AE_FAILURE;
  55. tlv_status_t tlv_status;
  56. do
  57. {
  58. TLVsMsg tlvs;
  59. const provision_response_header_t& header = reinterpret_cast<const provision_response_header_t&>(*serializedMsg2.getData());
  60. status = check_response_header(header, TYPE_PSE_MSG2, serializedMsg2.getSize());
  61. BREAK_IF_FAILED_ERR(status, AESM_PSE_PR_BACKEND_MSG2_RESPONSE_HEADER_INTEGRITY);
  62. status = check_response_status(header);
  63. if (AE_FAILED(status))
  64. break;
  65. tlv_status= tlvs.init_from_buffer(serializedMsg2.getData() + PROVISION_RESPONSE_HEADER_SIZE,
  66. static_cast<uint32_t>(serializedMsg2.getSize() - PROVISION_RESPONSE_HEADER_SIZE));
  67. status = tlv_error_2_pve_error(tlv_status);
  68. if (AE_FAILED(status))
  69. break;
  70. status = msg2_check_integrity(tlvs);
  71. if (AE_FAILED(status))
  72. break;
  73. status = msg2_derive_ek2_and_retrieve_nonce(tlvs, EK2, nonce);
  74. if (AE_FAILED(status))
  75. break;
  76. status = Nonce.Clone(nonce);
  77. if (AE_FAILED(status))
  78. break;
  79. status = msg2_verify_mac_and_retrieve_sigrl(header, tlvs, EK2, sigRL);
  80. if (AE_FAILED(status))
  81. break;
  82. } while (0);
  83. return status;
  84. }
  85. ae_error_t CertificateProvisioningProtocol::msg2_check_integrity(const TLVsMsg& tlvs)
  86. {
  87. ae_error_t status = PVE_INTEGRITY_CHECK_ERROR;
  88. do
  89. {
  90. uint32_t tlv_count = tlvs.get_tlv_count();
  91. if (tlv_count < MSG2_FIELD_COUNT_WITHOUT_SIGRL ||
  92. tlv_count > MSG2_FIELD_COUNT_WITH_SIGRL)
  93. {
  94. break;
  95. }
  96. // NONCE TLV
  97. if (tlvs[MSG2_NONCE_INDEX].type != TLV_NONCE ||
  98. tlvs[MSG2_NONCE_INDEX].size != NONCE_SIZE ||
  99. tlvs[MSG2_NONCE_INDEX].version < TLV_VERSION_1)
  100. {
  101. break;
  102. }
  103. if (tlv_count == MSG2_FIELD_COUNT_WITH_SIGRL)
  104. {
  105. // EPID SIG RL TLV
  106. if (tlvs[MSG2_SIGRL_INDEX].type != TLV_EPID_SIG_RL || tlvs[MSG2_SIGRL_INDEX].version < TLV_VERSION_1)
  107. {
  108. break;
  109. }
  110. // MAC TLV
  111. if (tlvs[MSG2_MAC_INDEX_WITH_SIGRL].type != TLV_MESSAGE_AUTHENTICATION_CODE ||
  112. tlvs[MSG2_MAC_INDEX_WITH_SIGRL].size != MAC_SIZE ||
  113. tlvs[MSG2_MAC_INDEX_WITH_SIGRL].version < TLV_VERSION_1)
  114. {
  115. break;
  116. }
  117. }
  118. else
  119. {
  120. // MAC TLV
  121. if (tlvs[MSG2_MAC_INDEX_NO_SIGRL].type != TLV_MESSAGE_AUTHENTICATION_CODE ||
  122. tlvs[MSG2_MAC_INDEX_NO_SIGRL].size != MAC_SIZE ||
  123. tlvs[MSG2_MAC_INDEX_NO_SIGRL].version < TLV_VERSION_1)
  124. {
  125. break;
  126. }
  127. }
  128. status = AE_SUCCESS;
  129. } while (0);
  130. return status;
  131. }
  132. ae_error_t CertificateProvisioningProtocol::msg2_derive_ek2_and_retrieve_nonce(const TLVsMsg& tlvs, upse::Buffer& ek2, upse::Buffer& nonce)
  133. {
  134. ae_error_t status = AE_FAILURE;
  135. do
  136. {
  137. status = nonce.Alloc(NONCE_SIZE);
  138. if (AE_FAILED(status))
  139. break;
  140. status = upse::BufferWriter(nonce).writeRaw(tlvs[MSG2_NONCE_INDEX].payload, NONCE_SIZE);
  141. if (AE_FAILED(status))
  142. break;
  143. upse::Buffer message;
  144. status = message.Alloc(TransactionID.getSize() + nonce.getSize());
  145. if (AE_FAILED(status))
  146. break;
  147. upse::BufferWriter messageWriter(message);
  148. status = messageWriter.writeRaw(TransactionID.getData(), TransactionID.getSize());
  149. if (AE_FAILED(status))
  150. break;
  151. status = messageWriter.writeRaw(nonce.getData(), nonce.getSize());
  152. if (AE_FAILED(status))
  153. break;
  154. status = aesCMAC(M1SK, message, ek2);
  155. if (AE_FAILED(status))
  156. break;
  157. status = AE_SUCCESS;
  158. } while (0);
  159. return status;
  160. }
  161. ae_error_t CertificateProvisioningProtocol::msg2_verify_mac_and_retrieve_sigrl(const provision_response_header_t& header, const TLVsMsg& tlvs, const upse::Buffer& ek2, upse::Buffer& sigRL)
  162. {
  163. ae_error_t status = AE_FAILURE;
  164. tlv_status_t tlv_status;
  165. do
  166. {
  167. uint32_t tlv_count = tlvs.get_tlv_count();
  168. upse::Buffer m2IV;
  169. status = M1IV.Not(m2IV);
  170. if (AE_FAILED(status))
  171. break;
  172. upse::Buffer m2HeaderBuf;
  173. status = m2HeaderBuf.Alloc((const uint8_t*)&header, sizeof(header));
  174. if (AE_FAILED(status))
  175. break;
  176. TLVsMsg nonceTLV;
  177. tlv_status = nonceTLV.add_nonce(tlvs[MSG2_NONCE_INDEX].payload, NONCE_SIZE);
  178. status = tlv_error_2_pve_error(tlv_status);
  179. if (AE_FAILED(status))
  180. break;
  181. upse::Buffer nonceTlvBuf;
  182. status = nonceTlvBuf.Alloc(nonceTLV.get_tlv_msg_size());
  183. if (AE_FAILED(status))
  184. break;
  185. status = upse::BufferWriter(nonceTlvBuf).writeRaw(nonceTLV.get_tlv_msg(), nonceTLV.get_tlv_msg_size());
  186. if (AE_FAILED(status))
  187. break;
  188. upse::Buffer macBuf;
  189. const uint8_t* pSigRLTLV = NULL;
  190. uint32_t nSigRLTLV = 0;
  191. if (tlv_count == MSG2_FIELD_COUNT_WITH_SIGRL)
  192. {
  193. // Locate the SIG RL TLV within the serialized TLV message
  194. nSigRLTLV = tlvs[MSG2_SIGRL_INDEX].header_size + tlvs[MSG2_SIGRL_INDEX].size;
  195. pSigRLTLV = tlvs.get_tlv_msg();
  196. for (int i = 0; i < MSG2_SIGRL_INDEX; i++)
  197. {
  198. pSigRLTLV += (tlvs[i].header_size + tlvs[i].size);
  199. }
  200. // EPID SIG RL
  201. status = sigRL.Alloc(tlvs[MSG2_SIGRL_INDEX].size);
  202. if (AE_FAILED(status))
  203. break;
  204. status = upse::BufferWriter(sigRL).writeRaw(tlvs[MSG2_SIGRL_INDEX].payload, tlvs[MSG2_SIGRL_INDEX].size);
  205. if (AE_FAILED(status))
  206. break;
  207. // MAC TLV
  208. status = macBuf.Alloc(tlvs[MSG2_MAC_INDEX_WITH_SIGRL].size);
  209. if (AE_FAILED(status))
  210. break;
  211. status = upse::BufferWriter(macBuf).writeRaw(tlvs[MSG2_MAC_INDEX_WITH_SIGRL].payload, tlvs[MSG2_MAC_INDEX_WITH_SIGRL].size);
  212. if (AE_FAILED(status))
  213. break;
  214. }
  215. else
  216. {
  217. // MAC TLV
  218. status = macBuf.Alloc(tlvs[MSG2_MAC_INDEX_NO_SIGRL].size);
  219. if (AE_FAILED(status))
  220. break;
  221. status = upse::BufferWriter(macBuf).writeRaw(tlvs[MSG2_MAC_INDEX_NO_SIGRL].payload, tlvs[MSG2_MAC_INDEX_NO_SIGRL].size);
  222. if (AE_FAILED(status))
  223. break;
  224. }
  225. upse::Buffer aad;
  226. status = aad.Alloc(m2HeaderBuf.getSize() + nonceTlvBuf.getSize() + nSigRLTLV);
  227. if (AE_FAILED(status))
  228. break;
  229. upse::BufferWriter aadWriter(aad);
  230. status = aadWriter.writeRaw(m2HeaderBuf.getData(), m2HeaderBuf.getSize());
  231. if (AE_FAILED(status))
  232. break;
  233. status = aadWriter.writeRaw(nonceTlvBuf.getData(), nonceTlvBuf.getSize());
  234. if (AE_FAILED(status))
  235. break;
  236. status = aadWriter.writeRaw(pSigRLTLV, nSigRLTLV);
  237. if (AE_FAILED(status))
  238. break;
  239. upse::Buffer emptyCipherText;
  240. upse::Buffer plainText;
  241. status = aesGCMDecrypt(m2IV, ek2, emptyCipherText, aad, macBuf, plainText);
  242. if (AE_FAILED(status))
  243. break;
  244. status = AE_SUCCESS;
  245. } while (0);
  246. return status;
  247. }