test_tpm2_auth_util.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdarg.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <setjmp.h>
  7. #include <cmocka.h>
  8. #include "tpm2_auth_util.h"
  9. #include "tpm2_auth_util.c" /* we want to test the static function parse_pcr */
  10. #include "esys_stubs.h"
  11. #include "test_session_common.h"
  12. TSS2_RC __wrap_Esys_TR_SetAuth(ESYS_CONTEXT *esysContext, ESYS_TR handle,
  13. TPM2B_AUTH const *authValue) {
  14. UNUSED(esysContext);
  15. UNUSED(handle);
  16. UNUSED(authValue);
  17. return TPM2_RC_SUCCESS;
  18. }
  19. static void test_tpm2_auth_util_from_optarg_raw_noprefix(void **state) {
  20. (void) state;
  21. tpm2_session *session;
  22. tool_rc rc = tpm2_auth_util_from_optarg(NULL, "abcd", &session, true);
  23. assert_int_equal(rc, tool_rc_success);
  24. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(session);
  25. assert_int_equal(auth->size, 4);
  26. assert_memory_equal(auth->buffer, "abcd", 4);
  27. tpm2_session_close(&session);
  28. }
  29. static void test_tpm2_auth_util_from_optarg_str_prefix(void **state) {
  30. (void) state;
  31. tpm2_session *session;
  32. tool_rc rc = tpm2_auth_util_from_optarg(NULL, "str:abcd", &session, true);
  33. assert_int_equal(rc, tool_rc_success);
  34. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(session);
  35. assert_int_equal(auth->size, 4);
  36. assert_memory_equal(auth->buffer, "abcd", 4);
  37. tpm2_session_close(&session);
  38. }
  39. static void test_tpm2_auth_util_from_optarg_hex_prefix(void **state) {
  40. (void) state;
  41. tpm2_session *session;
  42. BYTE expected[] = { 0x12, 0x34, 0xab, 0xcd };
  43. tool_rc rc = tpm2_auth_util_from_optarg(NULL, "hex:1234abcd", &session,
  44. true);
  45. assert_int_equal(rc, tool_rc_success);
  46. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(session);
  47. assert_int_equal(auth->size, sizeof(expected));
  48. assert_memory_equal(auth->buffer, expected, sizeof(expected));
  49. tpm2_session_close(&session);
  50. }
  51. static void test_tpm2_auth_util_from_optarg_str_escaped_hex_prefix(
  52. void **state) {
  53. (void) state;
  54. tpm2_session *session;
  55. tool_rc rc = tpm2_auth_util_from_optarg(NULL, "str:hex:1234abcd", &session,
  56. true);
  57. assert_int_equal(rc, tool_rc_success);
  58. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(session);
  59. assert_int_equal(auth->size, 12);
  60. assert_memory_equal(auth->buffer, "hex:1234abcd", 12);
  61. tpm2_session_close(&session);
  62. }
  63. FILE mocked_file_stream;
  64. const char *mocked_file_data = "sekretpasswrd";
  65. FILE * __real_fopen(const char *path, const char *mode);
  66. FILE * __wrap_fopen(const char *path, const char *mode) {
  67. if (strcmp(path, "test_tpm2_auth_util_foobar")) {
  68. return __real_fopen(path, mode);
  69. }
  70. return mock_ptr_type(FILE*);
  71. }
  72. size_t __real_fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
  73. size_t __wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  74. if (stream != &mocked_file_stream) {
  75. return __real_fread(ptr, size, nmemb, stream);
  76. }
  77. strncpy(ptr, mocked_file_data, size * nmemb);
  78. return mock_type(size_t);
  79. }
  80. long __real_ftell(FILE *stream);
  81. long __wrap_ftell(FILE *stream) {
  82. if (stream != &mocked_file_stream) {
  83. return __real_ftell(stream);
  84. }
  85. return mock_type(long);
  86. }
  87. int __real_fseek(FILE *stream, long offset, int whence);
  88. int __wrap_fseek(FILE *stream, long offset, int whence) {
  89. if (stream != &mocked_file_stream) {
  90. return __real_fseek(stream, offset, whence);
  91. }
  92. return 0;
  93. }
  94. int __real_feof(FILE *stream);
  95. int __wrap_feof(FILE *stream) {
  96. if (stream != &mocked_file_stream) {
  97. return __real_feof(stream);
  98. }
  99. return 0;
  100. }
  101. int __real_fclose(FILE *stream);
  102. int __wrap_fclose(FILE *stream) {
  103. if (stream != &mocked_file_stream) {
  104. return __real_fclose(stream);
  105. }
  106. return 0;
  107. }
  108. static void test_tpm2_auth_util_from_optarg_file(void **state) {
  109. UNUSED(state);
  110. tpm2_session *session;
  111. will_return(__wrap_fopen, &mocked_file_stream);
  112. will_return(__wrap_fread, (size_t) strlen(mocked_file_data));
  113. will_return(__wrap_ftell, (long) strlen(mocked_file_data));
  114. will_return(__wrap_ftell, (long) strlen(mocked_file_data));
  115. tool_rc rc = tpm2_auth_util_from_optarg(NULL, "file:test_tpm2_auth_util_foobar",
  116. &session, true);
  117. assert_int_equal(rc, tool_rc_success);
  118. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(session);
  119. assert_int_equal(auth->size, strlen(mocked_file_data));
  120. assert_memory_equal(auth->buffer, mocked_file_data, strlen(mocked_file_data));
  121. tpm2_session_close(&session);
  122. }
  123. #define PCR_SPECIFICATION "sha256:0,1,2,3+sha1:0,1,2,3"
  124. #define PCR_FILE "raw-pcr-file"
  125. static void test_parse_pcr_no_raw_file(void **state) {
  126. UNUSED(state);
  127. const char *policy = "pcr:" PCR_SPECIFICATION;
  128. char *pcr_str, *raw_file;
  129. bool ret = parse_pcr(policy, &pcr_str, &raw_file);
  130. assert_true(ret);
  131. assert_string_equal(pcr_str, PCR_SPECIFICATION);
  132. assert_null(raw_file);
  133. free(pcr_str);
  134. }
  135. static void test_parse_pcr_with_raw_file(void **state) {
  136. UNUSED(state);
  137. const char *policy = "pcr:" PCR_SPECIFICATION "=" PCR_FILE;
  138. char *pcr_str, *raw_file;
  139. bool ret = parse_pcr(policy, &pcr_str, &raw_file);
  140. assert_true(ret);
  141. assert_string_equal(pcr_str, PCR_SPECIFICATION);
  142. assert_string_equal(raw_file, PCR_FILE);
  143. free(pcr_str);
  144. }
  145. static void test_tpm2_auth_util_from_optarg_raw_overlength(void **state) {
  146. (void) state;
  147. tpm2_session *session = NULL;
  148. char *overlength =
  149. "this_password_is_over_64_characters_in_length_and_should_fail_XXX";
  150. tool_rc rc = tpm2_auth_util_from_optarg(NULL, overlength, &session, true);
  151. assert_int_equal(rc, tool_rc_general_error);
  152. assert_null(session);
  153. }
  154. static void test_tpm2_auth_util_from_optarg_hex_overlength(void **state) {
  155. (void) state;
  156. tpm2_session *session = NULL;
  157. /* 65 hex chars generated via: echo \"`xxd -p -c256 -l65 /dev/urandom`\"\; */
  158. char *overlength =
  159. "hex:ae6f6fa01589aa7b227bb6a34c7a8e0c273adbcf14195ce12391a5cc12a5c271f62088"
  160. "dbfcf1914fdf120da183ec3ad6cc78a2ffd91db40a560169961e3a6d26bf";
  161. tool_rc rc = tpm2_auth_util_from_optarg(NULL, overlength, &session, false);
  162. assert_int_equal(rc, tool_rc_general_error);
  163. assert_null(session);
  164. }
  165. static void test_tpm2_auth_util_from_optarg_empty_str(void **state) {
  166. (void) state;
  167. tpm2_session *session;
  168. tool_rc rc = tpm2_auth_util_from_optarg(NULL, "", &session, true);
  169. assert_int_equal(rc, tool_rc_success);
  170. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(session);
  171. assert_int_equal(auth->size, 0);
  172. tpm2_session_close(&session);
  173. }
  174. static void test_tpm2_auth_util_from_optarg_empty_str_str_prefix(
  175. void **state) {
  176. (void) state;
  177. tpm2_session *session;
  178. tool_rc rc = tpm2_auth_util_from_optarg(NULL, "str:", &session, true);
  179. assert_int_equal(rc, tool_rc_success);
  180. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(session);
  181. assert_int_equal(auth->size, 0);
  182. tpm2_session_close(&session);
  183. }
  184. static void test_tpm2_auth_util_from_optarg_empty_str_hex_prefix(
  185. void **state) {
  186. (void) state;
  187. tpm2_session *session;
  188. tool_rc rc = tpm2_auth_util_from_optarg(NULL, "hex:", &session, true);
  189. assert_int_equal(rc, tool_rc_success);
  190. const TPM2B_AUTH *auth = tpm2_session_get_auth_value(session);
  191. assert_int_equal(auth->size, 0);
  192. tpm2_session_close(&session);
  193. }
  194. static void test_parse_pcr_empty(void **state) {
  195. UNUSED(state);
  196. const char *policy = "pcr:";
  197. char *pcr_str, *raw_file;
  198. bool ret = parse_pcr(policy, &pcr_str, &raw_file);
  199. assert_false(ret);
  200. }
  201. static void test_parse_pcr_empty_pcr_specification(void **state) {
  202. UNUSED(state);
  203. const char *policy = "pcr:=" PCR_FILE;
  204. char *pcr_str, *raw_file;
  205. bool ret = parse_pcr(policy, &pcr_str, &raw_file);
  206. assert_false(ret);
  207. }
  208. static void test_parse_pcr_empty_pcr_file(void **state) {
  209. UNUSED(state);
  210. const char *policy = "pcr:" PCR_SPECIFICATION "=";
  211. char *pcr_str, *raw_file;
  212. bool ret = parse_pcr(policy, &pcr_str, &raw_file);
  213. assert_false(ret);
  214. free(pcr_str);
  215. }
  216. static int setup(void **state) {
  217. TSS2_RC rc;
  218. ESYS_CONTEXT *ectx;
  219. size_t size = sizeof(TSS2_TCTI_CONTEXT_FAKE);
  220. TSS2_TCTI_CONTEXT *tcti = malloc(size);
  221. rc = tcti_fake_initialize(tcti, &size);
  222. if (rc) {
  223. return (int)rc;
  224. }
  225. rc = Esys_Initialize(&ectx, tcti, NULL);
  226. *state = (void *)ectx;
  227. return (int)rc;
  228. }
  229. static int teardown(void **state) {
  230. TSS2_TCTI_CONTEXT *tcti;
  231. ESYS_CONTEXT *ectx = (ESYS_CONTEXT *)*state;
  232. Esys_GetTcti(ectx, &tcti);
  233. Esys_Finalize(&ectx);
  234. free(tcti);
  235. return 0;
  236. }
  237. static void test_tpm2_auth_util_get_pw_shandle(void **state) {
  238. ESYS_CONTEXT *ectx = (ESYS_CONTEXT *)*state;
  239. ESYS_TR auth_handle = ESYS_TR_NONE;
  240. ESYS_TR shandle;
  241. tpm2_session *s;
  242. tool_rc rc = tpm2_auth_util_from_optarg(NULL, "fakepass",
  243. &s, true);
  244. assert_int_equal(rc, tool_rc_success);
  245. assert_non_null(s);
  246. rc = tpm2_auth_util_get_shandle(ectx, auth_handle, s, &shandle);
  247. assert_int_equal(rc, tool_rc_success);
  248. assert_true(shandle == ESYS_TR_PASSWORD);
  249. tpm2_session_close(&s);
  250. assert_null(s);
  251. set_expected_defaults(TPM2_SE_POLICY, SESSION_HANDLE, TPM2_RC_SUCCESS);
  252. tpm2_session_data *d = tpm2_session_data_new(TPM2_SE_POLICY);
  253. assert_non_null(d);
  254. rc = tpm2_session_open(ectx, d, &s);
  255. assert_int_equal(rc, tool_rc_success);
  256. assert_non_null(s);
  257. rc = tpm2_auth_util_get_shandle(ectx, auth_handle, s, &shandle);
  258. assert_int_equal(rc, tool_rc_success);
  259. assert_int_equal(SESSION_HANDLE, shandle);
  260. tpm2_session_close(&s);
  261. assert_null(s);
  262. }
  263. /* link required symbol, but tpm2_tool.c declares it AND main, which
  264. * we have a main below for cmocka tests.
  265. */
  266. bool output_enabled = true;
  267. int main(int argc, char* argv[]) {
  268. (void) argc;
  269. (void) argv;
  270. const struct CMUnitTest tests[] = {
  271. cmocka_unit_test(test_tpm2_auth_util_from_optarg_raw_noprefix),
  272. cmocka_unit_test(test_tpm2_auth_util_from_optarg_str_prefix),
  273. cmocka_unit_test(test_tpm2_auth_util_from_optarg_hex_prefix),
  274. cmocka_unit_test(test_tpm2_auth_util_from_optarg_str_escaped_hex_prefix),
  275. cmocka_unit_test_setup_teardown(test_tpm2_auth_util_get_pw_shandle,
  276. setup, teardown),
  277. cmocka_unit_test(test_tpm2_auth_util_from_optarg_file),
  278. cmocka_unit_test(test_parse_pcr_no_raw_file),
  279. cmocka_unit_test(test_parse_pcr_with_raw_file),
  280. /* negative testing */
  281. cmocka_unit_test(test_tpm2_auth_util_from_optarg_raw_overlength),
  282. cmocka_unit_test(test_tpm2_auth_util_from_optarg_hex_overlength),
  283. cmocka_unit_test(test_tpm2_auth_util_from_optarg_empty_str),
  284. cmocka_unit_test(test_tpm2_auth_util_from_optarg_empty_str_str_prefix),
  285. cmocka_unit_test(test_tpm2_auth_util_from_optarg_empty_str_hex_prefix),
  286. cmocka_unit_test(test_parse_pcr_empty),
  287. cmocka_unit_test(test_parse_pcr_empty_pcr_specification),
  288. cmocka_unit_test(test_parse_pcr_empty_pcr_file),
  289. };
  290. return cmocka_run_group_tests(tests, NULL, NULL);
  291. }