tpm_tss_quote_info.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (C) 2016 Andreas Steffen
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <tpm_tss_quote_info.h>
  16. #include <bio/bio_writer.h>
  17. #ifndef TPM_TAG_QUOTE_INFO2
  18. #define TPM_TAG_QUOTE_INFO2 0x0036
  19. #endif
  20. #ifndef TPM_LOC_ZERO
  21. #define TPM_LOC_ZERO 0x01
  22. #endif
  23. typedef struct private_tpm_tss_quote_info_t private_tpm_tss_quote_info_t;
  24. /**
  25. * Private data of an tpm_tss_quote_info_t object.
  26. */
  27. struct private_tpm_tss_quote_info_t {
  28. /**
  29. * Public tpm_tss_quote_info_t interface.
  30. */
  31. tpm_tss_quote_info_t public;
  32. /**
  33. * TPM Quote Mode
  34. */
  35. tpm_quote_mode_t quote_mode;
  36. /**
  37. * TPM Qualified Signer
  38. */
  39. chunk_t qualified_signer;
  40. /**
  41. * TPM Clock Info
  42. */
  43. chunk_t clock_info;
  44. /**
  45. * TPM Version Info
  46. */
  47. chunk_t version_info;
  48. /**
  49. * TPM PCR Selection
  50. */
  51. chunk_t pcr_select;
  52. /**
  53. * TPM PCR Composite Hash
  54. */
  55. chunk_t pcr_digest;
  56. /**
  57. * TPM PCR Composite Hash algorithm
  58. */
  59. hash_algorithm_t pcr_digest_alg;
  60. /**
  61. * Reference count
  62. */
  63. refcount_t ref;
  64. };
  65. METHOD(tpm_tss_quote_info_t, get_quote_mode, tpm_quote_mode_t,
  66. private_tpm_tss_quote_info_t *this)
  67. {
  68. return this->quote_mode;
  69. }
  70. METHOD(tpm_tss_quote_info_t, get_pcr_digest_alg, hash_algorithm_t,
  71. private_tpm_tss_quote_info_t *this)
  72. {
  73. return this->pcr_digest_alg;
  74. }
  75. METHOD(tpm_tss_quote_info_t, get_pcr_digest, chunk_t,
  76. private_tpm_tss_quote_info_t *this)
  77. {
  78. return this->pcr_digest;
  79. }
  80. METHOD(tpm_tss_quote_info_t, get_quote, bool,
  81. private_tpm_tss_quote_info_t *this, chunk_t nonce,
  82. tpm_tss_pcr_composite_t *composite, chunk_t *quoted)
  83. {
  84. chunk_t pcr_composite, pcr_digest;
  85. bio_writer_t *writer;
  86. hasher_t *hasher;
  87. bool equal_digests;
  88. /* Construct PCR Composite */
  89. writer = bio_writer_create(32);
  90. switch (this->quote_mode)
  91. {
  92. case TPM_QUOTE:
  93. case TPM_QUOTE2:
  94. case TPM_QUOTE2_VERSION_INFO:
  95. writer->write_data16(writer, composite->pcr_select);
  96. writer->write_data32(writer, composite->pcr_composite);
  97. break;
  98. case TPM_QUOTE_TPM2:
  99. writer->write_data(writer, composite->pcr_composite);
  100. break;
  101. case TPM_QUOTE_NONE:
  102. break;
  103. }
  104. pcr_composite = writer->extract_buf(writer);
  105. writer->destroy(writer);
  106. DBG2(DBG_PTS, "constructed PCR Composite: %B", &pcr_composite);
  107. /* Compute PCR Composite Hash */
  108. hasher = lib->crypto->create_hasher(lib->crypto, this->pcr_digest_alg);
  109. if (!hasher || !hasher->allocate_hash(hasher, pcr_composite, &pcr_digest))
  110. {
  111. DESTROY_IF(hasher);
  112. chunk_free(&pcr_composite);
  113. return FALSE;
  114. }
  115. hasher->destroy(hasher);
  116. chunk_free(&pcr_composite);
  117. DBG2(DBG_PTS, "constructed PCR Composite digest: %B", &pcr_digest);
  118. equal_digests = chunk_equals(pcr_digest, this->pcr_digest);
  119. /* Construct Quote Info */
  120. writer = bio_writer_create(32);
  121. switch (this->quote_mode)
  122. {
  123. case TPM_QUOTE:
  124. /* Version number */
  125. writer->write_data(writer, chunk_from_chars(1, 1, 0, 0));
  126. /* Magic QUOT value */
  127. writer->write_data(writer, chunk_from_str("QUOT"));
  128. /* PCR Composite Hash */
  129. writer->write_data(writer, pcr_digest);
  130. /* Secret assessment value 20 bytes (nonce) */
  131. writer->write_data(writer, nonce);
  132. break;
  133. case TPM_QUOTE2:
  134. case TPM_QUOTE2_VERSION_INFO:
  135. /* TPM Structure Tag */
  136. writer->write_uint16(writer, TPM_TAG_QUOTE_INFO2);
  137. /* Magic QUT2 value */
  138. writer->write_data(writer, chunk_from_str("QUT2"));
  139. /* Secret assessment value 20 bytes (nonce) */
  140. writer->write_data(writer, nonce);
  141. /* PCR selection */
  142. writer->write_data16(writer, composite->pcr_select);
  143. /* TPM Locality Selection */
  144. writer->write_uint8(writer, TPM_LOC_ZERO);
  145. /* PCR Composite Hash */
  146. writer->write_data(writer, pcr_digest);
  147. if (this->quote_mode == TPM_QUOTE2_VERSION_INFO)
  148. {
  149. /* TPM version Info */
  150. writer->write_data(writer, this->version_info);
  151. }
  152. break;
  153. case TPM_QUOTE_TPM2:
  154. /* Magic */
  155. writer->write_data(writer, chunk_from_chars(0xff,0x54,0x43,0x47));
  156. /* Type */
  157. writer->write_uint16(writer, 0x8018);
  158. /* Qualified Signer */
  159. writer->write_data16(writer, this->qualified_signer);
  160. /* Extra Data */
  161. writer->write_data16(writer, nonce);
  162. /* Clock Info */
  163. writer->write_data(writer, this->clock_info);
  164. /* Firmware Version */
  165. writer->write_data(writer, this->version_info);
  166. /* PCR Selection */
  167. writer->write_data(writer, this->pcr_select);
  168. /* PCR Composite Hash */
  169. writer->write_data16(writer, pcr_digest);
  170. break;
  171. case TPM_QUOTE_NONE:
  172. break;
  173. }
  174. chunk_free(&pcr_digest);
  175. *quoted = writer->extract_buf(writer);
  176. writer->destroy(writer);
  177. DBG2(DBG_PTS, "constructed TPM Quote Info: %B", quoted);
  178. if (!equal_digests)
  179. {
  180. DBG1(DBG_IMV, "received PCR Composite digest does not match "
  181. "constructed one");
  182. chunk_free(quoted);
  183. }
  184. return equal_digests;
  185. }
  186. METHOD(tpm_tss_quote_info_t, set_version_info, void,
  187. private_tpm_tss_quote_info_t *this, chunk_t version_info)
  188. {
  189. chunk_free(&this->version_info);
  190. this->version_info = chunk_clone(version_info);
  191. }
  192. METHOD(tpm_tss_quote_info_t, get_version_info, chunk_t,
  193. private_tpm_tss_quote_info_t *this)
  194. {
  195. return this->version_info;
  196. }
  197. METHOD(tpm_tss_quote_info_t, set_tpm2_info, void,
  198. private_tpm_tss_quote_info_t *this, chunk_t qualified_signer,
  199. chunk_t clock_info, chunk_t pcr_select)
  200. {
  201. chunk_free(&this->qualified_signer);
  202. this->qualified_signer = chunk_clone(qualified_signer);
  203. chunk_free(&this->clock_info);
  204. this->clock_info = chunk_clone(clock_info);
  205. chunk_free(&this->pcr_select);
  206. this->pcr_select = chunk_clone(pcr_select);
  207. }
  208. METHOD(tpm_tss_quote_info_t, get_tpm2_info, void,
  209. private_tpm_tss_quote_info_t *this, chunk_t *qualified_signer,
  210. chunk_t *clock_info, chunk_t *pcr_select)
  211. {
  212. if (qualified_signer)
  213. {
  214. *qualified_signer = this->qualified_signer;
  215. }
  216. if (clock_info)
  217. {
  218. *clock_info = this->clock_info;
  219. }
  220. if (pcr_select)
  221. {
  222. *pcr_select = this->pcr_select;
  223. }
  224. }
  225. METHOD(tpm_tss_quote_info_t, get_ref, tpm_tss_quote_info_t*,
  226. private_tpm_tss_quote_info_t *this)
  227. {
  228. ref_get(&this->ref);
  229. return &this->public;
  230. }
  231. METHOD(tpm_tss_quote_info_t, destroy, void,
  232. private_tpm_tss_quote_info_t *this)
  233. {
  234. if (ref_put(&this->ref))
  235. {
  236. chunk_free(&this->qualified_signer);
  237. chunk_free(&this->clock_info);
  238. chunk_free(&this->version_info);
  239. chunk_free(&this->pcr_select);
  240. chunk_free(&this->pcr_digest);
  241. free(this);
  242. }
  243. }
  244. /**
  245. * See header
  246. */
  247. tpm_tss_quote_info_t *tpm_tss_quote_info_create(tpm_quote_mode_t quote_mode,
  248. hash_algorithm_t pcr_digest_alg, chunk_t pcr_digest)
  249. {
  250. private_tpm_tss_quote_info_t *this;
  251. INIT(this,
  252. .public = {
  253. .get_quote_mode = _get_quote_mode,
  254. .get_pcr_digest_alg = _get_pcr_digest_alg,
  255. .get_pcr_digest = _get_pcr_digest,
  256. .get_quote = _get_quote,
  257. .set_version_info = _set_version_info,
  258. .get_version_info = _get_version_info,
  259. .set_tpm2_info = _set_tpm2_info,
  260. .get_tpm2_info = _get_tpm2_info,
  261. .get_ref = _get_ref,
  262. .destroy = _destroy,
  263. },
  264. .quote_mode = quote_mode,
  265. .pcr_digest_alg = pcr_digest_alg,
  266. .pcr_digest = chunk_clone(pcr_digest),
  267. .ref = 1,
  268. );
  269. return &this->public;
  270. }