fapi-get-intl-cert.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2018, Fraunhofer SIT sponsored by Infineon Technologies AG
  4. * All rights reserved.
  5. ******************************************************************************/
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif
  9. #include <stdarg.h>
  10. #include <inttypes.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <json-c/json_util.h>
  15. #include <json-c/json_tokener.h>
  16. #include <openssl/evp.h>
  17. #include <setjmp.h>
  18. #include <cmocka.h>
  19. #include "tss2_fapi.h"
  20. #include "fapi_int.h"
  21. #include "ifapi_get_intl_cert.h"
  22. #include "util/aux_util.h"
  23. #define LOGMODULE tests
  24. #include "util/log.h"
  25. /*
  26. * The unit tests will simulate error codes which can be returned by the
  27. * functions which are used to retrieve the INTEL certificates.
  28. */
  29. /* Mock data for the certificate buffer. and the public data of the EK */
  30. char* valid_json_cert = "{ \"certificate\": \"ZG15Cg==\" }"; /**< dmy base64 encoded */
  31. char* invalid_json_cert1 = "{ \"certificate\": 1 }";
  32. char* invalid_json_cert2 = "{ }";
  33. char* mock_json_cert;
  34. TPM2B_PUBLIC eccPublic = {
  35. .size = 0,
  36. .publicArea = {
  37. .type = TPM2_ALG_ECC,
  38. .nameAlg = TPM2_ALG_SHA256,
  39. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  40. TPMA_OBJECT_RESTRICTED |
  41. TPMA_OBJECT_SIGN_ENCRYPT |
  42. TPMA_OBJECT_FIXEDTPM |
  43. TPMA_OBJECT_FIXEDPARENT |
  44. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  45. .authPolicy = {
  46. .size = 0,
  47. },
  48. .parameters.eccDetail = {
  49. .symmetric = {
  50. .algorithm = TPM2_ALG_NULL,
  51. .keyBits.aes = 128,
  52. .mode.aes = TPM2_ALG_CFB,
  53. },
  54. .scheme = {
  55. .scheme = TPM2_ALG_ECDSA,
  56. .details = {
  57. .ecdsa = {.hashAlg = TPM2_ALG_SHA256}},
  58. },
  59. .curveID = TPM2_ECC_NIST_P256,
  60. .kdf = {
  61. .scheme = TPM2_ALG_NULL,
  62. .details = {}}
  63. },
  64. .unique.ecc = {
  65. .x = {.size = 2, .buffer = { 1, 2 }},
  66. .y = {.size = 2, .buffer = { 3, 4 }},
  67. },
  68. },
  69. };
  70. TPM2B_PUBLIC rsaPublic = {
  71. .size = 0,
  72. .publicArea = {
  73. .type = TPM2_ALG_RSA,
  74. .nameAlg = TPM2_ALG_SHA1,
  75. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  76. TPMA_OBJECT_SIGN_ENCRYPT |
  77. TPMA_OBJECT_FIXEDTPM |
  78. TPMA_OBJECT_FIXEDPARENT |
  79. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  80. .authPolicy = {
  81. .size = 0,
  82. },
  83. .parameters.rsaDetail = {
  84. .symmetric = {
  85. .algorithm = TPM2_ALG_NULL,
  86. .keyBits.aes = 128,
  87. .mode.aes = TPM2_ALG_CFB},
  88. .scheme = {
  89. .scheme = TPM2_ALG_RSAPSS,
  90. .details = {
  91. .rsapss = { .hashAlg = TPM2_ALG_SHA1 }
  92. }
  93. },
  94. .keyBits = 2048,
  95. .exponent = 0,
  96. },
  97. .unique.rsa = {
  98. .size = 2,
  99. .buffer = { 1, 2 },
  100. },
  101. },
  102. };
  103. /*
  104. * Wrapper function for reading the certificate buffer.
  105. */
  106. int
  107. __real_ifapi_get_curl_buffer(unsigned char * url, unsigned char ** buffer,
  108. size_t *buffer_size);
  109. int
  110. __wrap_ifapi_get_curl_buffer(unsigned char * url, unsigned char ** buffer,
  111. size_t *buffer_size)
  112. {
  113. UNUSED(url);
  114. *buffer = (unsigned char *)strdup(mock_json_cert);
  115. *buffer_size = strlen(mock_json_cert) + 1;
  116. return 0;
  117. }
  118. /*
  119. * Wrapper function for updating the hash of EK public data.
  120. */
  121. size_t wrap_EVP_DigestUpdate_test = 0;
  122. int
  123. __real_EVP_DigestUpdate(EVP_MD_CTX *c, const void *data, size_t len);
  124. int
  125. __wrap_EVP_DigestUpdate(EVP_MD_CTX *c, const void *data, size_t len)
  126. {
  127. if (!wrap_EVP_DigestUpdate_test) {
  128. return __real_EVP_DigestUpdate(c, data, len);
  129. } else if (wrap_EVP_DigestUpdate_test == 1) {
  130. wrap_EVP_DigestUpdate_test = 0;
  131. return mock_type(int);
  132. } else {
  133. wrap_EVP_DigestUpdate_test--;
  134. return __real_EVP_DigestUpdate(c, data, len);
  135. }
  136. }
  137. static int
  138. setup (void **state)
  139. {
  140. *state = calloc(1, sizeof(FAPI_CONTEXT)); //Fapi_Initialize
  141. return 0;
  142. }
  143. static int
  144. teardown (void **state)
  145. {
  146. SAFE_FREE(*state);
  147. return 0;
  148. }
  149. /*
  150. * Check receiving of valid JSON data for the certificate.
  151. */
  152. static void
  153. check_get_intl_cert_ok(void **state) {
  154. FAPI_CONTEXT *ctx = *state;
  155. unsigned char *cert_buf = NULL;
  156. size_t cert_size;
  157. TSS2_RC r;
  158. mock_json_cert = valid_json_cert;
  159. r = ifapi_get_intl_ek_certificate(ctx, &eccPublic, &cert_buf, &cert_size);
  160. assert_int_equal(r, TSS2_RC_SUCCESS);
  161. SAFE_FREE(cert_buf);
  162. r = ifapi_get_intl_ek_certificate(ctx, &rsaPublic, &cert_buf, &cert_size);
  163. SAFE_FREE(cert_buf);
  164. assert_int_equal(r, TSS2_RC_SUCCESS);
  165. }
  166. /*
  167. * Check receiving of invalid JSON data for the certificate.
  168. */
  169. static void
  170. check_get_intl_cert_invalid_json(void **state) {
  171. FAPI_CONTEXT *ctx = *state;
  172. unsigned char *cert_buf = NULL;
  173. size_t cert_size;
  174. TSS2_RC r;
  175. mock_json_cert = invalid_json_cert1;
  176. r = ifapi_get_intl_ek_certificate(ctx, &eccPublic, &cert_buf, &cert_size);
  177. assert_int_equal(r, TSS2_FAPI_RC_NO_CERT);
  178. mock_json_cert = invalid_json_cert2;
  179. r = ifapi_get_intl_ek_certificate(ctx, &rsaPublic, &cert_buf, &cert_size);
  180. assert_int_equal(r, TSS2_FAPI_RC_NO_CERT);
  181. }
  182. /*
  183. * Simulate error during hash update for the EK public data.
  184. */
  185. static void
  186. check_get_intl_cert_sha_error(void **state) {
  187. FAPI_CONTEXT *ctx = *state;
  188. unsigned char *cert_buf = NULL;
  189. size_t cert_size;
  190. TSS2_RC r;
  191. will_return_always(__wrap_EVP_DigestUpdate, 0);
  192. mock_json_cert = valid_json_cert;
  193. wrap_EVP_DigestUpdate_test = 1;
  194. r = ifapi_get_intl_ek_certificate(ctx, &eccPublic, &cert_buf, &cert_size);
  195. assert_int_equal(r,TSS2_FAPI_RC_NO_CERT);
  196. wrap_EVP_DigestUpdate_test = 1;
  197. r = ifapi_get_intl_ek_certificate(ctx, &rsaPublic, &cert_buf, &cert_size);
  198. assert_int_equal(r,TSS2_FAPI_RC_NO_CERT);
  199. wrap_EVP_DigestUpdate_test = 2;
  200. r = ifapi_get_intl_ek_certificate(ctx, &eccPublic, &cert_buf, &cert_size);
  201. assert_int_equal(r,TSS2_FAPI_RC_NO_CERT);
  202. wrap_EVP_DigestUpdate_test = 2;
  203. r = ifapi_get_intl_ek_certificate(ctx, &rsaPublic, &cert_buf, &cert_size);
  204. assert_int_equal(r,TSS2_FAPI_RC_NO_CERT);
  205. }
  206. int
  207. main(int argc, char *argv[])
  208. {
  209. const struct CMUnitTest tests[] = {
  210. cmocka_unit_test_setup_teardown(check_get_intl_cert_ok, setup, teardown),
  211. cmocka_unit_test_setup_teardown(check_get_intl_cert_invalid_json, setup, teardown),
  212. cmocka_unit_test_setup_teardown(check_get_intl_cert_sha_error, setup, teardown),
  213. };
  214. return cmocka_run_group_tests(tests, NULL, NULL);
  215. }