fapi-key-create-null-key-sign.int.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2017-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 <stdlib.h>
  10. #include "tss2_fapi.h"
  11. #include "test-fapi.h"
  12. #include "fapi_util.h"
  13. #include "fapi_int.h"
  14. #include "esys_iutil.h"
  15. #define LOGMODULE test
  16. #include "util/log.h"
  17. #include "util/aux_util.h"
  18. #define PASSWORD "abc"
  19. #define SIGN_TEMPLATE "sign,noDa"
  20. json_object *
  21. get_json_hex_string(const uint8_t *buffer, size_t size)
  22. {
  23. char hex_string[size * 2 + 1];
  24. for (size_t i = 0, off = 0; i < size; i++, off += 2) {
  25. sprintf(&hex_string[off], "%02x", buffer[i]);
  26. }
  27. hex_string[(size) * 2] = '\0';
  28. json_object *jso = json_object_new_string(hex_string);
  29. return jso;
  30. }
  31. static TSS2_RC
  32. auth_callback(
  33. char const *objectPath,
  34. char const *description,
  35. const char **auth,
  36. void *userData)
  37. {
  38. UNUSED(description);
  39. UNUSED(userData);
  40. if (!objectPath) {
  41. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  42. }
  43. *auth = PASSWORD;
  44. return TSS2_RC_SUCCESS;
  45. }
  46. /** Test creation of a primary in the NULL hiearchy and directly it the hierarchy.
  47. *
  48. * Tested FAPI commands:
  49. * - Fapi_Provision()
  50. * - Fapi_SetAuthCB()
  51. * - Fapi_CreateKey()
  52. * - Fapi_Sign()
  53. * - Fapi_VerifySignature()
  54. * - Fapi_Delete()
  55. *
  56. * @param[in,out] context The FAPI_CONTEXT.
  57. * @retval EXIT_FAILURE
  58. * @retval EXIT_SUCCESS
  59. */
  60. int
  61. test_fapi_key_create_null_sign(FAPI_CONTEXT *context)
  62. {
  63. TSS2_RC r;
  64. char *sigscheme = NULL;
  65. uint8_t *signature = NULL;
  66. char *publicKey = NULL;
  67. char *path_list = NULL;
  68. if (strcmp("P_ECC", fapi_profile) != 0)
  69. sigscheme = "RSA_PSS";
  70. r = Fapi_Provision(context, NULL, NULL, NULL);
  71. goto_if_error(r, "Error Fapi_Provision", error);
  72. r = Fapi_SetAuthCB(context, auth_callback, NULL);
  73. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  74. r = Fapi_CreateKey(context, "HN/myNullPrimary", "noDa,0x81000004", "",
  75. PASSWORD);
  76. if (r == TSS2_RC_SUCCESS) {
  77. goto_if_error(r, "Persistent handle not allowed.", error);
  78. }
  79. if (r != TSS2_FAPI_RC_BAD_VALUE) {
  80. goto_if_error(r, "Wrong check persistent.", error);
  81. }
  82. r = Fapi_CreateKey(context, "HN/myNullPrimary", "restricted,decrypt,noDa", "",
  83. NULL);
  84. goto_if_error(r, "Error Fapi_CreateKey", error);
  85. r = Fapi_CreateKey(context, "HN/myNullPrimary/myNullSignKey", SIGN_TEMPLATE ",0x81000004", "",
  86. PASSWORD);
  87. if (r == TSS2_RC_SUCCESS) {
  88. goto_if_error(r, "Wrong authentication.", error);
  89. }
  90. if (r != TSS2_FAPI_RC_BAD_VALUE) {
  91. goto_if_error(r, "Wrong check persistent.", error);
  92. }
  93. r = Fapi_CreateKey(context, "HN/myNullPrimary/myNullSignKey", SIGN_TEMPLATE, "",
  94. PASSWORD);
  95. goto_if_error(r, "Error Fapi_CreateKey", error);
  96. size_t signatureSize = 0;
  97. TPM2B_DIGEST digest = {
  98. .size = 32,
  99. .buffer = {
  100. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  101. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  102. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  103. 0x67, 0x68
  104. }
  105. };
  106. r = Fapi_Sign(context, "HN/myNullPrimary/myNullSignKey", sigscheme,
  107. &digest.buffer[0], digest.size, &signature, &signatureSize,
  108. &publicKey, NULL);
  109. goto_if_error(r, "Error Fapi_Sign", error);
  110. r = Fapi_VerifySignature(context, "HN/myNullPrimary/myNullSignKey",
  111. &digest.buffer[0], digest.size, signature, signatureSize);
  112. goto_if_error(r, "Error Fapi_VerifySignature", error);
  113. Fapi_Finalize(&context);
  114. int rc = init_fapi("P_ECC", &context);
  115. if (rc)
  116. goto error;
  117. /* Test the creation of a primary in the storage hierarchy. */
  118. r = Fapi_CreateKey(context, "HS/myPrimary", "noDa", "",
  119. PASSWORD);
  120. goto_if_error(r, "Error Fapi_CreateKey", error);
  121. r = Fapi_Delete(context, "HS/myPrimary");
  122. goto_if_error(r, "Error Fapi_Delete", error);
  123. /* Test the creation of a primary in the storage hierarchy with a policy. */
  124. char *policy_name = "/policy/pol_pcr16_0";
  125. const char *json_policy =
  126. "{" \
  127. "\"description\":\"Description pol_16_0\"," \
  128. "\"policy\":[" \
  129. "{" \
  130. "\"type\":\"POLICYPCR\"," \
  131. "\"pcrs\":[" \
  132. "{" \
  133. "\"pcr\":16," \
  134. "\"hashAlg\":\"TPM2_ALG_SHA256\"," \
  135. "\"digest\":\"00000000000000000000000000000000000000000000000000000000000000000\"" \
  136. "}" \
  137. "]" \
  138. "}" \
  139. "]" \
  140. "}";
  141. r = Fapi_Import(context, policy_name, json_policy);
  142. goto_if_error(r, "Error Fapi_Import", error);
  143. r = Fapi_CreateKey(context, "HS/myPrimary", "noDa", policy_name,
  144. NULL);
  145. goto_if_error(r, "Error Fapi_CreateKey", error);
  146. r = Fapi_Delete(context, "HS/myPrimary");
  147. goto_if_error(r, "Error Fapi_Delete", error);
  148. /* Test the creation of a primary in the endorsement hierarchy. */
  149. r = Fapi_CreateKey(context, "HE/myPrimary", "noDa", "",
  150. PASSWORD);
  151. goto_if_error(r, "Error Fapi_CreateKey", error);
  152. r = Fapi_Delete(context, "HE/myPrimary");
  153. goto_if_error(r, "Error Fapi_Delete", error);
  154. r = Fapi_Delete(context, "/");
  155. goto_if_error(r, "Error Fapi_Delete", error);
  156. SAFE_FREE(path_list);
  157. SAFE_FREE(publicKey);
  158. SAFE_FREE(signature);
  159. return EXIT_SUCCESS;
  160. error:
  161. Fapi_Delete(context, "/");
  162. SAFE_FREE(path_list);
  163. SAFE_FREE(publicKey);
  164. SAFE_FREE(signature);
  165. return EXIT_FAILURE;
  166. }
  167. int
  168. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  169. {
  170. return test_fapi_key_create_null_sign(fapi_context);
  171. }