fapi-key-create-policy-signed.int.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 <stdio.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/rsa.h>
  17. #include <openssl/engine.h>
  18. #include <openssl/pem.h>
  19. #include "tss2_fapi.h"
  20. #include "test-fapi.h"
  21. #define LOGMODULE test
  22. #include "util/log.h"
  23. #include "util/aux_util.h"
  24. #ifdef TEST_ECC
  25. const char *priv_pem =
  26. "-----BEGIN EC PRIVATE KEY-----\n"
  27. "MHcCAQEEILE8jic/6w/lKoFTVblkNQ4Ls5IYibQNQ4Dk5B9R09ONoAoGCCqGSM49\n"
  28. "AwEHoUQDQgAEoJTa3zftdAzHC96IjpqQ/dnLm+p7pEiLMi03Jd0oP0aYnnXFjolz\n"
  29. "IB/dBZ/t+BLh0PwLM5aAM/jugeLkHgpIyQ==\n"
  30. "-----END EC PRIVATE KEY-----\n";
  31. const char *pub_pem =
  32. "-----BEGIN PUBLIC KEY-----\n"
  33. "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEoJTa3zftdAzHC96IjpqQ/dnLm+p7\n"
  34. "pEiLMi03Jd0oP0aYnnXFjolzIB/dBZ/t+BLh0PwLM5aAM/jugeLkHgpIyQ==\n"
  35. "-----END PUBLIC KEY-----\n";
  36. #else
  37. const char *priv_pem =
  38. "-----BEGIN PRIVATE KEY-----\n"
  39. "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCgYvoisJIDOeYg\n"
  40. "jMF6ywiZbu085TLvy5ZMhq5vfYqdgefvwpemutnfKSnpYOs5B4yO/gAD7XiYluDv\n"
  41. "tqlVhfISeQV04xrWGzNImenpwm/HsgueAu8VTHNtWSL96G+BLedGTrs2NqX6cxN7\n"
  42. "yGl7dQpB5X8iP4XSvpjP3Vb7gs+adwCJR6xFkt60jYFmwrAdhEzOeakhimi5rU21\n"
  43. "LxCkRdEyaxS57X15L9dEA+aYJ+dvkFfZOfTqIKmTrA75F8yj161xflwtIC4hgRBg\n"
  44. "K9Xb/RdN8TDrTu+20E3RjngutU4qejW9Fd3mzHJGV8HRYvjYXhUblN9wmjm7Veru\n"
  45. "T2b0rnvzAgMBAAECggEBAIwHvoJ5DRJ6A50Zp3dROxHTEphfOEi6xF/OGxBGWLbK\n"
  46. "C7l+eS9d5gj8BJa5QsXI/IR/6X2EYQ1AdeV04oVD7CUKuqPiALU8jFrv3pV0aGm+\n"
  47. "3nu37gv3crPe5jkvLeNoM4tkA/oCXom63SDuyoG6nxkHiSdatLlaJUse4em3vRAL\n"
  48. "QivziZIMyswcleMe0xAoMi7LO+nUFFxBS8/xGya0vsU0dsMQEl1SRITv1VCXmPQD\n"
  49. "T4dEI4+1cufv6Ax0EDbFKmnjyiGTjOeQKrGIqETUSQolbg5PgL1XZehaaxM822OY\n"
  50. "Qpnp5T0XhUEmVrOb2Wrboj+dC/2tgAN/fWXjAAxnm2ECgYEA02UTZuZ+QnD6tqo3\n"
  51. "/y3n5kaM9uA2mdOIqgECI9psGF1IBIC/iP2diKyuvmQL8hzymComb5YzZl3TOAga\n"
  52. "WHQYbIeU3JhnYTG75/Dv5Zh32H4NjkIJHT2/8LUM25Ove9u6QAniVgIQpBZ47LjX\n"
  53. "9jHjTYCW5n79qNSfu0egYJUvypECgYEAwjqWzzEINqnX/xIVCoB4XpuDuSdkM0JW\n"
  54. "MZDIH9xHjZPp07/5XYEoITylk6Zwbh+djvWDNP4gzPtuK26VsqrNxoWMsFZeXn6U\n"
  55. "xSOYL2UNCZiOgchdZCOr+6r8LRUuo8xHjbawVoJVK1+tZ2WsR3ilt3Gw34O8Z5ep\n"
  56. "f4v7GOXw+EMCgYAUHjFrgJIRhqkFi0uK+HZyXtJ5iDsKBqyh6Tin6tiQtQfujcYs\n"
  57. "pl5ArJZwvhq47vJTcud3hSbdHh7E3ViMhHfylDChkct833vPhgl+ozT8oHpvyG8P\n"
  58. "nlnO8ZwIpZR0yCOAhrBImSe2RgE6HhlHb9X/ATbbNsizMZEGBLoJlwkWUQKBgQCy\n"
  59. "4U7fh2LvJUF+82JZh7RUPZn1Pmg0JVZI0/TcEv37UEy77kR1b2xMIBTGhTVq1sc/\n"
  60. "ULIEbkA7SR1P9sr7//8AZSMLjJ/hG2dcoMmabNCzE8O7l5MblRbh87nIs4d+57bG\n"
  61. "t4h0RBi4l6eWYLdoI59L8fNaB3PPXIiIpZ0eczeZDQKBgQC2vuFYpUZqDb9CaJsn\n"
  62. "Luee6P6n5v3ZBTAT4E+GG1kWS28BiebcCuLKNAY4ZtLo08ozaTWcMxooOTeka2ux\n"
  63. "fQDE4M/LTNpam8QOJ2hqECF5a0uBYNcbmaGtfA9KwIgwCZZYuwb5IDq/DRPuR690\n"
  64. "i8Kp6jR2wY0suObmZHKvbCB1Dw==\n"
  65. "-----END PRIVATE KEY-----\n";
  66. const char *pub_pem =
  67. "-----BEGIN PUBLIC KEY-----\n"
  68. "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoGL6IrCSAznmIIzBessI\n"
  69. "mW7tPOUy78uWTIaub32KnYHn78KXprrZ3ykp6WDrOQeMjv4AA+14mJbg77apVYXy\n"
  70. "EnkFdOMa1hszSJnp6cJvx7ILngLvFUxzbVki/ehvgS3nRk67Njal+nMTe8hpe3UK\n"
  71. "QeV/Ij+F0r6Yz91W+4LPmncAiUesRZLetI2BZsKwHYRMznmpIYpoua1NtS8QpEXR\n"
  72. "MmsUue19eS/XRAPmmCfnb5BX2Tn06iCpk6wO+RfMo9etcX5cLSAuIYEQYCvV2/0X\n"
  73. "TfEw607vttBN0Y54LrVOKno1vRXd5sxyRlfB0WL42F4VG5TfcJo5u1Xq7k9m9K57\n"
  74. "8wIDAQAB\n"
  75. "-----END PUBLIC KEY-----\n";
  76. #endif /* TEST_ECC */
  77. #define RSA_SIG_SCHEME RSA_PKCS1_PSS_PADDING
  78. char *userDataTest = "test";
  79. #define chknull(X) if (!X) { LOG_ERROR(str(X) "should not be null"); \
  80. r = TSS2_FAPI_RC_GENERAL_FAILURE; \
  81. goto error_cleanup; }
  82. static uint8_t *global_signature = NULL;
  83. static TSS2_RC
  84. signatureCallback(
  85. char const *objectPath,
  86. char const *description,
  87. char const *publicKey,
  88. char const *publicKeyHint,
  89. uint32_t hashAlg,
  90. uint8_t const *dataToSign,
  91. size_t dataToSignSize,
  92. uint8_t const **signature,
  93. size_t *signatureSize,
  94. void *userData)
  95. {
  96. uint8_t *aux_signature = NULL;
  97. UNUSED(description);
  98. UNUSED(publicKey);
  99. UNUSED(publicKeyHint);
  100. if (!objectPath) {
  101. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  102. }
  103. if (userData != userDataTest) {
  104. LOG_ERROR("userData is not correct, %p != %p", userData, userDataTest);
  105. return TSS2_FAPI_RC_GENERAL_FAILURE;
  106. }
  107. if (hashAlg != TPM2_ALG_SHA256) {
  108. LOG_ERROR("hashAlg is not correct, %u != %u", hashAlg, TPM2_ALG_SHA256);
  109. return TSS2_FAPI_RC_GENERAL_FAILURE;
  110. }
  111. TSS2_RC r = TSS2_RC_SUCCESS;
  112. EVP_PKEY *priv_key = NULL;
  113. BIO *bufio = NULL;
  114. EVP_MD_CTX *mdctx = NULL;
  115. EVP_PKEY_CTX *pctx = NULL;
  116. const EVP_MD *ossl_hash = EVP_sha256();
  117. chknull(ossl_hash);
  118. LOGBLOB_DEBUG(dataToSign, dataToSignSize, "Data to be signed");
  119. bufio = BIO_new_mem_buf((void *)priv_pem, strlen(priv_pem));
  120. priv_key = PEM_read_bio_PrivateKey(bufio, NULL, NULL, NULL);
  121. chknull(priv_key);
  122. mdctx = EVP_MD_CTX_create();
  123. chknull(mdctx);
  124. if (1 != EVP_DigestSignInit(mdctx, &pctx, ossl_hash, NULL, priv_key)) {
  125. goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign init.",
  126. error_cleanup);
  127. }
  128. if (EVP_PKEY_base_id(priv_key) == EVP_PKEY_RSA) {
  129. int signing_scheme = RSA_SIG_SCHEME;
  130. if (1 != EVP_PKEY_CTX_set_rsa_padding(pctx, signing_scheme)) {
  131. goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL set RSA padding.",
  132. error_cleanup);
  133. }
  134. }
  135. if (1 != EVP_DigestSignUpdate(mdctx, dataToSign, dataToSignSize)) {
  136. goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign update.",
  137. error_cleanup);
  138. }
  139. if (1 != EVP_DigestSignFinal(mdctx, NULL, signatureSize)) {
  140. goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign final.",
  141. error_cleanup);
  142. }
  143. aux_signature = malloc(*signatureSize);
  144. global_signature = aux_signature;
  145. chknull(aux_signature);
  146. if (1 != EVP_DigestSignFinal(mdctx, aux_signature, signatureSize)) {
  147. goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign final.",
  148. error_cleanup);
  149. }
  150. *signature = aux_signature;
  151. error_cleanup:
  152. if (priv_key)
  153. EVP_PKEY_free(priv_key);
  154. if (bufio)
  155. BIO_free(bufio);
  156. if (mdctx)
  157. EVP_MD_CTX_destroy(mdctx);
  158. return r;
  159. }
  160. #define PASSWORD NULL
  161. #define SIGN_TEMPLATE "sign,noDa"
  162. /** Test the FAPI functions for key creation and usage with a PolicySigned.
  163. *
  164. * Tested FAPI commands:
  165. * - Fapi_Provision()
  166. * - Fapi_Import()
  167. * - Fapi_CreateKey()
  168. * - Fapi_SetSignCB()
  169. * - Fapi_Sign()
  170. * - Fapi_Delete()
  171. * - Fapi_List()
  172. *
  173. * Tested Policies:
  174. * - PolicySigned
  175. *
  176. * @param[in,out] context The FAPI_CONTEXT.
  177. * @retval EXIT_FAILURE
  178. * @retval EXIT_SUCCESS
  179. */
  180. int
  181. test_fapi_key_create_policy_signed(FAPI_CONTEXT *context)
  182. {
  183. TSS2_RC r;
  184. #ifdef TEST_ECC
  185. char *policy_name = "/policy/pol_signed_ecc";
  186. char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_signed_ecc.json";
  187. #else
  188. char *policy_name = "/policy/pol_signed";
  189. char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_signed.json";
  190. #endif
  191. FILE *stream = NULL;
  192. char *json_policy = NULL;
  193. long policy_size;
  194. uint8_t *signature = NULL;
  195. char *publicKey = NULL;
  196. char *certificate = NULL;
  197. char *pathList = NULL;
  198. r = Fapi_Provision(context, NULL, NULL, NULL);
  199. goto_if_error(r, "Error Fapi_Provision", error);
  200. r = pcr_reset(context, 16);
  201. goto_if_error(r, "Error pcr_reset", error);
  202. stream = fopen(policy_file, "r");
  203. if (!stream) {
  204. LOG_ERROR("File %s does not exist", policy_file);
  205. goto error;
  206. }
  207. fseek(stream, 0L, SEEK_END);
  208. policy_size = ftell(stream);
  209. fclose(stream);
  210. json_policy = malloc(policy_size + 1);
  211. goto_if_null(json_policy,
  212. "Could not allocate memory for the JSON policy",
  213. TSS2_FAPI_RC_MEMORY, error);
  214. stream = fopen(policy_file, "r");
  215. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  216. if (ret != policy_size) {
  217. LOG_ERROR("IO error %s.", policy_file);
  218. goto error;
  219. }
  220. json_policy[policy_size] = '\0';
  221. r = Fapi_Import(context, policy_name, json_policy);
  222. goto_if_error(r, "Error Fapi_Import", error);
  223. r = Fapi_CreateKey(context, "/HS/SRK/mySignKey", SIGN_TEMPLATE,
  224. policy_name, PASSWORD);
  225. goto_if_error(r, "Error Fapi_CreateKey", error);
  226. r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", "-----BEGIN "\
  227. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  228. goto_if_error(r, "Error Fapi_CreateKey", error);
  229. size_t signatureSize = 0;
  230. TPM2B_DIGEST digest = {
  231. .size = 20,
  232. .buffer = {
  233. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  234. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
  235. }
  236. };
  237. r = Fapi_SetSignCB(context, signatureCallback, userDataTest);
  238. goto_if_error(r, "Error SetPolicySignatureCallback", error);
  239. r = Fapi_Sign(context, "/HS/SRK/mySignKey", NULL,
  240. &digest.buffer[0], digest.size, &signature, &signatureSize,
  241. &publicKey, &certificate);
  242. goto_if_error(r, "Error Fapi_Sign", error);
  243. ASSERT(signature != NULL);
  244. ASSERT(publicKey != NULL);
  245. ASSERT(certificate != NULL);
  246. ASSERT(strstr(publicKey, "BEGIN PUBLIC KEY"));
  247. ASSERT(strstr(certificate, "BEGIN CERTIFICATE"));
  248. r = Fapi_List(context, "/", &pathList);
  249. goto_if_error(r, "Error Fapi_List", error);
  250. ASSERT(pathList != NULL);
  251. ASSERT(strlen(pathList) > ASSERT_SIZE);
  252. fprintf(stderr, "\n%s\n", pathList);
  253. r = Fapi_Delete(context, "/");
  254. goto_if_error(r, "Error Fapi_Delete", error);
  255. fclose(stream);
  256. SAFE_FREE(json_policy);
  257. SAFE_FREE(signature);
  258. SAFE_FREE(publicKey);
  259. SAFE_FREE(certificate);
  260. SAFE_FREE(pathList);
  261. SAFE_FREE(global_signature);
  262. return EXIT_SUCCESS;
  263. error:
  264. Fapi_Delete(context, "/");
  265. SAFE_FREE(json_policy);
  266. SAFE_FREE(signature);
  267. SAFE_FREE(publicKey);
  268. SAFE_FREE(certificate);
  269. SAFE_FREE(pathList);
  270. SAFE_FREE(global_signature);
  271. return EXIT_FAILURE;
  272. }
  273. int
  274. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  275. {
  276. return test_fapi_key_create_policy_signed(fapi_context);
  277. }