fapi-data-crypt.int.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <unistd.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/rsa.h>
  15. #include <openssl/engine.h>
  16. #include <openssl/pem.h>
  17. #include "tss2_fapi.h"
  18. #include "test-fapi.h"
  19. #define LOGMODULE test
  20. #include "util/log.h"
  21. #include "util/aux_util.h"
  22. #define SIZE 128
  23. const char *priv_pem =
  24. "-----BEGIN PRIVATE KEY-----\n"
  25. "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCgYvoisJIDOeYg\n"
  26. "jMF6ywiZbu085TLvy5ZMhq5vfYqdgefvwpemutnfKSnpYOs5B4yO/gAD7XiYluDv\n"
  27. "tqlVhfISeQV04xrWGzNImenpwm/HsgueAu8VTHNtWSL96G+BLedGTrs2NqX6cxN7\n"
  28. "yGl7dQpB5X8iP4XSvpjP3Vb7gs+adwCJR6xFkt60jYFmwrAdhEzOeakhimi5rU21\n"
  29. "LxCkRdEyaxS57X15L9dEA+aYJ+dvkFfZOfTqIKmTrA75F8yj161xflwtIC4hgRBg\n"
  30. "K9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  45. "4U7fh2LvJUF+82JZh7RUPZn1Pmg0JVZI0/TcEv37UEy77kR1b2xMIBTGhTVq1sc/\n"
  46. "ULIEbkA7SR1P9sr7//8AZSMLjJ/hG2dcoMmabNCzE8O7l5MblRbh87nIs4d+57bG\n"
  47. "t4h0RBi4l6eWYLdoI59L8fNaB3PPXIiIpZ0eczeZDQKBgQC2vuFYpUZqDb9CaJsn\n"
  48. "Luee6P6n5v3ZBTAT4E+GG1kWS28BiebcCuLKNAY4ZtLo08ozaTWcMxooOTeka2ux\n"
  49. "fQDE4M/LTNpam8QOJ2hqECF5a0uBYNcbmaGtfA9KwIgwCZZYuwb5IDq/DRPuR690\n"
  50. "i8Kp6jR2wY0suObmZHKvbCB1Dw==\n"
  51. "-----END PRIVATE KEY-----\n";
  52. const char *pub_pem =
  53. "-----BEGIN PUBLIC KEY-----\n"
  54. "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoGL6IrCSAznmIIzBessI\n"
  55. "mW7tPOUy78uWTIaub32KnYHn78KXprrZ3ykp6WDrOQeMjv4AA+14mJbg77apVYXy\n"
  56. "EnkFdOMa1hszSJnp6cJvx7ILngLvFUxzbVki/ehvgS3nRk67Njal+nMTe8hpe3UK\n"
  57. "QeV/Ij+F0r6Yz91W+4LPmncAiUesRZLetI2BZsKwHYRMznmpIYpoua1NtS8QpEXR\n"
  58. "MmsUue19eS/XRAPmmCfnb5BX2Tn06iCpk6wO+RfMo9etcX5cLSAuIYEQYCvV2/0X\n"
  59. "TfEw607vttBN0Y54LrVOKno1vRXd5sxyRlfB0WL42F4VG5TfcJo5u1Xq7k9m9K57\n"
  60. "8wIDAQAB\n"
  61. "-----END PUBLIC KEY-----\n";
  62. #define RSA_SIG_SCHEME RSA_PKCS1_PSS_PADDING
  63. char *userDataTest = "test";
  64. #define chknull(X) if (!X) { LOG_ERROR(str(X) "should not be null"); \
  65. r = TSS2_FAPI_RC_GENERAL_FAILURE; \
  66. goto error_cleanup; }
  67. static uint8_t *global_signature = NULL;
  68. static TSS2_RC
  69. signatureCallback(
  70. char const *objectPath,
  71. char const *description,
  72. char const *publicKey,
  73. char const *publicKeyHint,
  74. uint32_t hashAlg,
  75. uint8_t const *dataToSign,
  76. size_t dataToSignSize,
  77. uint8_t const **signature,
  78. size_t *signatureSize,
  79. void *userData)
  80. {
  81. UNUSED(description);
  82. UNUSED(publicKey);
  83. UNUSED(publicKeyHint);
  84. uint8_t *aux_signature = NULL;
  85. if (strcmp(objectPath, "P_RSA/HS/SRK/myRsaCryptKey") != 0) {
  86. return_error(TSS2_FAPI_RC_BAD_VALUE, "Unexpected path");
  87. }
  88. if (userData != userDataTest) {
  89. LOG_ERROR("userData is not correct, %p != %p", userData, userDataTest);
  90. return TSS2_FAPI_RC_GENERAL_FAILURE;
  91. }
  92. if (hashAlg != TPM2_ALG_SHA256) {
  93. LOG_ERROR("hashAlg is not correct, %u != %u", hashAlg, TPM2_ALG_SHA256);
  94. return TSS2_FAPI_RC_GENERAL_FAILURE;
  95. }
  96. TSS2_RC r = TSS2_RC_SUCCESS;
  97. EVP_PKEY *priv_key = NULL;
  98. BIO *bufio = NULL;
  99. EVP_MD_CTX *mdctx =NULL;
  100. EVP_PKEY_CTX *pctx = NULL;
  101. const EVP_MD *ossl_hash = EVP_sha256();
  102. chknull(ossl_hash);
  103. LOGBLOB_DEBUG(dataToSign, dataToSignSize, "Data to be signed");
  104. bufio = BIO_new_mem_buf((void *)priv_pem, strlen(priv_pem));
  105. priv_key = PEM_read_bio_PrivateKey(bufio, NULL, NULL, NULL);
  106. chknull(priv_key);
  107. mdctx = EVP_MD_CTX_create();
  108. chknull(mdctx);
  109. if (1 != EVP_DigestSignInit(mdctx, &pctx, ossl_hash, NULL, priv_key)) {
  110. goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign init.",
  111. error_cleanup);
  112. }
  113. if (EVP_PKEY_base_id(priv_key) == EVP_PKEY_RSA) {
  114. int signing_scheme = RSA_SIG_SCHEME;
  115. if (1 != EVP_PKEY_CTX_set_rsa_padding(pctx, signing_scheme)) {
  116. goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL set RSA padding.",
  117. error_cleanup);
  118. }
  119. }
  120. if (1 != EVP_DigestSignUpdate(mdctx, dataToSign, dataToSignSize)) {
  121. goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign update.",
  122. error_cleanup);
  123. }
  124. if (1 != EVP_DigestSignFinal(mdctx, NULL, signatureSize)) {
  125. goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign final.",
  126. error_cleanup);
  127. }
  128. aux_signature = malloc(*signatureSize);
  129. global_signature = aux_signature;
  130. chknull(aux_signature);
  131. if (1 != EVP_DigestSignFinal(mdctx, aux_signature, signatureSize)) {
  132. goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign final.",
  133. error_cleanup);
  134. }
  135. *signature = aux_signature;
  136. if (priv_key)
  137. EVP_PKEY_free(priv_key);
  138. if (mdctx)
  139. EVP_MD_CTX_destroy(mdctx);
  140. if (bufio)
  141. BIO_free(bufio);
  142. return r;
  143. error_cleanup:
  144. if (priv_key)
  145. EVP_PKEY_free(priv_key);
  146. if (mdctx)
  147. EVP_MD_CTX_destroy(mdctx);
  148. if (bufio)
  149. BIO_free(bufio);
  150. return r;
  151. }
  152. /** Test the FAPI functions for encryption and decryption.
  153. *
  154. * Tested FAPI commands:
  155. * - Fapi_Provision()
  156. * - Fapi_Import()
  157. * - Fapi_CreateKey()
  158. * - Fapi_SetSignCB()
  159. * - Fapi_Encrypt()
  160. * - Fapi_Decrypt()
  161. * - Fapi_Free()
  162. *
  163. * Tested Policies:
  164. * - PolicySigned
  165. *
  166. * @param[in,out] context The FAPI_CONTEXT.
  167. * @retval EXIT_FAILURE
  168. * @retval EXIT_SUCCESS
  169. */
  170. int
  171. test_fapi_data_crypt(FAPI_CONTEXT *context)
  172. {
  173. TSS2_RC r;
  174. long policy_size;
  175. const char *policy_name = "/policy/pol_signed";
  176. const char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_signed.json";
  177. FILE *stream = NULL;
  178. char *json_policy = NULL;
  179. uint8_t *cipherText = NULL;
  180. size_t cipherTextSize;
  181. r = Fapi_Provision(context, NULL, NULL, NULL);
  182. goto_if_error(r, "Error Fapi_Provision", error);
  183. r = pcr_reset(context, 16);
  184. goto_if_error(r, "Error pcr_reset", error);
  185. stream = fopen(policy_file, "r");
  186. if (!stream) {
  187. LOG_ERROR("File %s does not exist", policy_file);
  188. goto error;
  189. }
  190. fseek(stream, 0L, SEEK_END);
  191. policy_size = ftell(stream);
  192. fclose(stream);
  193. json_policy = malloc(policy_size + 1);
  194. goto_if_null(json_policy,
  195. "Could not allocate memory for the JSON policy",
  196. TSS2_FAPI_RC_MEMORY, error);
  197. stream = fopen(policy_file, "r");
  198. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  199. if (ret != policy_size) {
  200. LOG_ERROR("IO error %s.", policy_file);
  201. goto error;
  202. }
  203. json_policy[policy_size] = '\0';
  204. r = Fapi_Import(context, policy_name, json_policy);
  205. goto_if_error(r, "Error Fapi_Import", error);
  206. #ifdef PERSISTENT
  207. r = Fapi_CreateKey(context, "HS/SRK/myRsaCryptKey", "decrypt,0x81000004",
  208. policy_name, NULL);
  209. #else
  210. r = Fapi_CreateKey(context, "HS/SRK/myRsaCryptKey", "decrypt",
  211. policy_name, NULL);
  212. #endif
  213. goto_if_error(r, "Error Fapi_CreateKey", error);
  214. uint8_t plainText[SIZE];
  215. int i;
  216. for (i = 0; i < SIZE; i++)
  217. plainText[i] = i % 256;
  218. r = Fapi_SetSignCB(context, signatureCallback, userDataTest);
  219. goto_if_error(r, "Error SetPolicySignatureCallback", error);
  220. r = Fapi_Encrypt(context, "HS/SRK/myRsaCryptKey", &plainText[0],
  221. SIZE, &cipherText, &cipherTextSize);
  222. if (r == TSS2_FAPI_RC_NOT_IMPLEMENTED) {
  223. goto skip;
  224. }
  225. goto_if_error(r, "Error Fapi_Encrypt", error);
  226. uint8_t *plainText2 = NULL;
  227. size_t plainText2_size = 0;
  228. r = Fapi_Decrypt(context, "HS/SRK/myRsaCryptKey", cipherText, cipherTextSize,
  229. &plainText2, &plainText2_size);
  230. goto_if_error(r, "Error Fapi_Encrypt", error);
  231. if (plainText2_size != SIZE ||
  232. memcmp(plainText, plainText2, plainText2_size) != 0) {
  233. LOG_ERROR("Error: decrypted text not equal to origin");
  234. goto error;
  235. }
  236. Fapi_Free(cipherText);
  237. Fapi_Free(plainText2);
  238. Fapi_Free(json_policy);
  239. Fapi_Delete(context, "/");
  240. SAFE_FREE(global_signature);
  241. return EXIT_SUCCESS;
  242. error:
  243. Fapi_Delete(context, "/");
  244. Fapi_Free(cipherText);
  245. Fapi_Free(json_policy);
  246. Fapi_Delete(context, "/");
  247. SAFE_FREE(global_signature);
  248. return EXIT_FAILURE;
  249. skip:
  250. Fapi_Delete(context, "/");
  251. Fapi_Free(json_policy);
  252. Fapi_Delete(context, "/");
  253. SAFE_FREE(global_signature);
  254. return EXIT_SKIP;
  255. }
  256. int
  257. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  258. {
  259. return test_fapi_data_crypt(fapi_context);
  260. }