tpm2_identity_util.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <tss2/tss2_mu.h>
  6. #include <openssl/rand.h>
  7. #include "log.h"
  8. #include "tpm2_alg_util.h"
  9. #include "tpm2_identity_util.h"
  10. #include "tpm2_kdfa.h"
  11. #include "tpm2_kdfe.h"
  12. #include "tpm2_openssl.h"
  13. // Identity-related functionality that the TPM normally does, but using OpenSSL
  14. #if defined(LIBRESSL_VERSION_NUMBER)
  15. static int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
  16. const unsigned char *from, int flen, const unsigned char *param, int plen,
  17. const EVP_MD *md, const EVP_MD *mgf1md) {
  18. int ret = 0;
  19. int i, emlen = tlen - 1;
  20. unsigned char *db, *seed;
  21. unsigned char *dbmask, seedmask[EVP_MAX_MD_SIZE];
  22. int mdlen;
  23. if (md == NULL)
  24. md = EVP_sha1();
  25. if (mgf1md == NULL)
  26. mgf1md = md;
  27. mdlen = EVP_MD_size(md);
  28. if (flen > emlen - 2 * mdlen - 1) {
  29. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
  30. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  31. return 0;
  32. }
  33. if (emlen < 2 * mdlen + 1) {
  34. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
  35. RSA_R_KEY_SIZE_TOO_SMALL);
  36. return 0;
  37. }
  38. to[0] = 0;
  39. seed = to + 1;
  40. db = to + mdlen + 1;
  41. if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
  42. return 0;
  43. memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
  44. db[emlen - flen - mdlen - 1] = 0x01;
  45. memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
  46. if (RAND_bytes(seed, mdlen) <= 0)
  47. return 0;
  48. dbmask = OPENSSL_malloc(emlen - mdlen);
  49. if (dbmask == NULL) {
  50. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
  51. return 0;
  52. }
  53. if (PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md) < 0)
  54. goto err;
  55. for (i = 0; i < emlen - mdlen; i++)
  56. db[i] ^= dbmask[i];
  57. if (PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md) < 0)
  58. goto err;
  59. for (i = 0; i < mdlen; i++)
  60. seed[i] ^= seedmask[i];
  61. ret = 1;
  62. err:
  63. OPENSSL_free(dbmask);
  64. return ret;
  65. }
  66. #endif
  67. static TPM2_KEY_BITS get_pub_asym_key_bits(TPM2B_PUBLIC *public) {
  68. TPMU_PUBLIC_PARMS *p = &public->publicArea.parameters;
  69. switch (public->publicArea.type) {
  70. case TPM2_ALG_ECC:
  71. /* fall-thru */
  72. case TPM2_ALG_RSA:
  73. return p->asymDetail.symmetric.keyBits.sym;
  74. /* no default */
  75. }
  76. return 0;
  77. }
  78. static bool share_secret_with_tpm2_rsa_public_key(TPM2B_DIGEST *protection_seed,
  79. TPM2B_PUBLIC *parent_pub, const unsigned char *label, int label_len,
  80. TPM2B_ENCRYPTED_SECRET *encrypted_protection_seed) {
  81. bool rval = false;
  82. RSA *rsa = NULL;
  83. // Public modulus (RSA-only!)
  84. TPMI_RSA_KEY_BITS mod_size_bits =
  85. parent_pub->publicArea.parameters.rsaDetail.keyBits;
  86. UINT16 mod_size = mod_size_bits / 8;
  87. TPM2B *pub_key_val = (TPM2B *) &parent_pub->publicArea.unique.rsa;
  88. unsigned char *pub_modulus = malloc(mod_size);
  89. if (pub_modulus == NULL) {
  90. LOG_ERR("Failed to allocate memory to store public key's modulus.");
  91. return false;
  92. }
  93. memcpy(pub_modulus, pub_key_val->buffer, mod_size);
  94. TPMI_ALG_HASH parent_name_alg = parent_pub->publicArea.nameAlg;
  95. /*
  96. * RSA Secret Sharing uses a randomly generated seed (Part 1, B.10.3).
  97. */
  98. protection_seed->size = tpm2_alg_util_get_hash_size(parent_name_alg);
  99. int return_code = RAND_bytes(protection_seed->buffer, protection_seed->size);
  100. if (return_code != 1) {
  101. LOG_ERR("Failed to get random bytes");
  102. goto error;
  103. }
  104. /*
  105. * This is the biggest buffer value, so it should always be sufficient.
  106. */
  107. unsigned char encoded[TPM2_MAX_DIGEST_BUFFER];
  108. return_code = RSA_padding_add_PKCS1_OAEP_mgf1(encoded, mod_size,
  109. protection_seed->buffer, protection_seed->size, label, label_len,
  110. tpm2_openssl_halg_from_tpmhalg(parent_name_alg), NULL);
  111. if (return_code != 1) {
  112. LOG_ERR("Failed RSA_padding_add_PKCS1_OAEP_mgf1\n");
  113. goto error;
  114. }
  115. BIGNUM* bne = BN_new();
  116. if (!bne) {
  117. LOG_ERR("BN_new for bne failed\n");
  118. goto error;
  119. }
  120. return_code = BN_set_word(bne, RSA_F4);
  121. if (return_code != 1) {
  122. LOG_ERR("BN_set_word failed\n");
  123. BN_free(bne);
  124. goto error;
  125. }
  126. rsa = RSA_new();
  127. if (!rsa) {
  128. LOG_ERR("RSA_new failed\n");
  129. BN_free(bne);
  130. goto error;
  131. }
  132. return_code = RSA_generate_key_ex(rsa, mod_size_bits, bne, NULL);
  133. BN_free(bne);
  134. if (return_code != 1) {
  135. LOG_ERR("RSA_generate_key_ex failed\n");
  136. goto error;
  137. }
  138. BIGNUM *n = BN_bin2bn(pub_modulus, mod_size, NULL);
  139. if (n == NULL) {
  140. LOG_ERR("BN_bin2bn failed\n");
  141. goto error;
  142. }
  143. if (!RSA_set0_key(rsa, n, NULL, NULL)) {
  144. LOG_ERR("RSA_set0_key failed\n");
  145. BN_free(n);
  146. goto error;
  147. }
  148. // Encrypting
  149. encrypted_protection_seed->size = mod_size;
  150. return_code = RSA_public_encrypt(mod_size, encoded,
  151. encrypted_protection_seed->secret, rsa, RSA_NO_PADDING);
  152. if (return_code < 0) {
  153. LOG_ERR("Failed RSA_public_encrypt\n");
  154. goto error;
  155. }
  156. rval = true;
  157. error:
  158. free(pub_modulus);
  159. RSA_free(rsa);
  160. return rval;
  161. }
  162. bool tpm2_identity_util_calc_outer_integrity_hmac_key_and_dupsensitive_enc_key(
  163. TPM2B_PUBLIC *parent_pub, TPM2B_NAME *pubname,
  164. TPM2B_DIGEST *protection_seed, TPM2B_MAX_BUFFER *protection_hmac_key,
  165. TPM2B_MAX_BUFFER *protection_enc_key) {
  166. TPM2B null_2b = { .size = 0 };
  167. TPMI_ALG_HASH parent_alg = parent_pub->publicArea.nameAlg;
  168. UINT16 parent_hash_size = tpm2_alg_util_get_hash_size(parent_alg);
  169. TSS2_RC rval = tpm2_kdfa(parent_alg, (TPM2B *) protection_seed, "INTEGRITY",
  170. &null_2b, &null_2b, parent_hash_size * 8, protection_hmac_key);
  171. if (rval != TPM2_RC_SUCCESS) {
  172. return false;
  173. }
  174. TPM2_KEY_BITS pub_key_bits = get_pub_asym_key_bits(parent_pub);
  175. rval = tpm2_kdfa(parent_alg, (TPM2B *) protection_seed, "STORAGE",
  176. (TPM2B *) pubname, &null_2b, pub_key_bits, protection_enc_key);
  177. if (rval != TPM2_RC_SUCCESS) {
  178. return false;
  179. }
  180. return true;
  181. }
  182. bool tpm2_identity_util_share_secret_with_public_key(
  183. TPM2B_DIGEST *protection_seed, TPM2B_PUBLIC *parent_pub,
  184. const unsigned char *label, int label_len,
  185. TPM2B_ENCRYPTED_SECRET *encrypted_protection_seed) {
  186. bool result = false;
  187. TPMI_ALG_PUBLIC alg = parent_pub->publicArea.type;
  188. switch (alg) {
  189. case TPM2_ALG_RSA:
  190. result = share_secret_with_tpm2_rsa_public_key(protection_seed,
  191. parent_pub, label, label_len, encrypted_protection_seed);
  192. break;
  193. case TPM2_ALG_ECC:
  194. result = ecdh_derive_seed_and_encrypted_seed(parent_pub,
  195. label, label_len,
  196. protection_seed, encrypted_protection_seed);
  197. break;
  198. default:
  199. LOG_ERR("Cannot handle algorithm, got: %s",
  200. tpm2_alg_util_algtostr(alg, tpm2_alg_util_flags_any));
  201. return false;
  202. }
  203. return result;
  204. }
  205. static const EVP_CIPHER *tpm_alg_to_ossl(TPMT_SYM_DEF_OBJECT *sym) {
  206. switch (sym->algorithm) {
  207. case TPM2_ALG_AES: {
  208. switch (sym->keyBits.aes) {
  209. case 128:
  210. return EVP_aes_128_cfb();
  211. case 256:
  212. return EVP_aes_256_cfb();
  213. /* no default */
  214. }
  215. }
  216. /* no default */
  217. }
  218. LOG_ERR("Unsupported parent key symmetric parameters");
  219. return NULL;
  220. }
  221. static bool aes_encrypt_buffers(TPMT_SYM_DEF_OBJECT *sym,
  222. uint8_t *encryption_key, uint8_t *buf1, size_t buf1_len, uint8_t *buf2,
  223. size_t buf2_len, TPM2B_MAX_BUFFER *cipher_text) {
  224. bool result = false;
  225. unsigned offset = 0;
  226. size_t total_len = buf1_len + buf2_len;
  227. if (total_len > sizeof(cipher_text->buffer)) {
  228. LOG_ERR("Plaintext too big, got %zu, expected less then %zu", total_len,
  229. sizeof(cipher_text->buffer));
  230. return false;
  231. }
  232. const EVP_CIPHER *cipher = tpm_alg_to_ossl(sym);
  233. if (!cipher) {
  234. return false;
  235. }
  236. const unsigned char iv[512] = { 0 };
  237. if (((unsigned long) EVP_CIPHER_iv_length(cipher)) > sizeof(iv)) {
  238. LOG_ERR("IV size is bigger then IV buffer size");
  239. return false;
  240. }
  241. EVP_CIPHER_CTX *ctx = tpm2_openssl_cipher_new();
  242. int rc = EVP_EncryptInit_ex(ctx, cipher, NULL, encryption_key, iv);
  243. if (!rc) {
  244. return false;
  245. }
  246. EVP_CIPHER_CTX_set_padding(ctx, 0);
  247. uint8_t *bufs[2] = { buf1, buf2 };
  248. size_t lens[ARRAY_LEN(bufs)] = { buf1_len, buf2_len };
  249. unsigned i;
  250. for (i = 0; i < ARRAY_LEN(bufs); i++) {
  251. uint8_t *b = bufs[i];
  252. size_t l = lens[i];
  253. if (!b) {
  254. continue;
  255. }
  256. int output_len = total_len - offset;
  257. rc = EVP_EncryptUpdate(ctx, &cipher_text->buffer[offset], &output_len,
  258. b, l);
  259. if (!rc) {
  260. LOG_ERR("Encrypt failed");
  261. goto out;
  262. }
  263. offset += l;
  264. }
  265. int tmp_len = 0;
  266. rc = EVP_EncryptFinal_ex(ctx, NULL, &tmp_len);
  267. if (!rc) {
  268. LOG_ERR("Encrypt failed");
  269. goto out;
  270. }
  271. cipher_text->size = total_len;
  272. result = true;
  273. out:
  274. tpm2_openssl_cipher_free(ctx);
  275. return result;
  276. }
  277. static void hmac_outer_integrity(TPMI_ALG_HASH parent_name_alg,
  278. uint8_t *buffer1, uint16_t buffer1_size, uint8_t *buffer2,
  279. uint16_t buffer2_size, uint8_t *hmac_key,
  280. TPM2B_DIGEST *outer_integrity_hmac) {
  281. uint8_t to_hmac_buffer[TPM2_MAX_DIGEST_BUFFER];
  282. memcpy(to_hmac_buffer, buffer1, buffer1_size);
  283. memcpy(to_hmac_buffer + buffer1_size, buffer2, buffer2_size);
  284. uint32_t size = 0;
  285. UINT16 hash_size = tpm2_alg_util_get_hash_size(parent_name_alg);
  286. HMAC(tpm2_openssl_halg_from_tpmhalg(parent_name_alg), hmac_key, hash_size,
  287. to_hmac_buffer, buffer1_size + buffer2_size,
  288. outer_integrity_hmac->buffer, &size);
  289. outer_integrity_hmac->size = size;
  290. }
  291. bool tpm2_identity_util_calculate_inner_integrity(TPMI_ALG_HASH name_alg,
  292. TPM2B_SENSITIVE *sensitive, TPM2B_NAME *pubname,
  293. TPM2B_DATA *enc_sensitive_key, TPMT_SYM_DEF_OBJECT *sym_alg,
  294. TPM2B_MAX_BUFFER *encrypted_inner_integrity) {
  295. TSS2_RC rval;
  296. //Marshal sensitive area
  297. uint8_t buffer_marshalled_sensitiveArea[TPM2_MAX_DIGEST_BUFFER] = { 0 };
  298. size_t marshalled_sensitive_size = 0;
  299. rval = Tss2_MU_TPMT_SENSITIVE_Marshal(&sensitive->sensitiveArea,
  300. buffer_marshalled_sensitiveArea + sizeof(uint16_t),
  301. TPM2_MAX_DIGEST_BUFFER, &marshalled_sensitive_size);
  302. if (rval != TPM2_RC_SUCCESS)
  303. {
  304. LOG_ERR("Error serializing the sensitive data");
  305. return false;
  306. }
  307. size_t marshalled_sensitive_size_info = 0;
  308. rval = Tss2_MU_UINT16_Marshal(marshalled_sensitive_size,
  309. buffer_marshalled_sensitiveArea, sizeof(uint16_t),
  310. &marshalled_sensitive_size_info);
  311. if (rval != TPM2_RC_SUCCESS)
  312. {
  313. LOG_ERR("Error serializing the sensitive size");
  314. return false;
  315. }
  316. //concatenate NAME
  317. memcpy(buffer_marshalled_sensitiveArea + marshalled_sensitive_size +
  318. marshalled_sensitive_size_info, pubname->name, pubname->size);
  319. //Digest marshalled-sensitive || name
  320. uint8_t *marshalled_sensitive_and_name_digest =
  321. buffer_marshalled_sensitiveArea + marshalled_sensitive_size
  322. + marshalled_sensitive_size_info + pubname->size;
  323. size_t digest_size_info = 0;
  324. UINT16 hash_size = tpm2_alg_util_get_hash_size(name_alg);
  325. rval = Tss2_MU_UINT16_Marshal(hash_size, marshalled_sensitive_and_name_digest,
  326. sizeof(uint16_t), &digest_size_info);
  327. if (rval != TPM2_RC_SUCCESS)
  328. {
  329. LOG_ERR("Error serializing the name size");
  330. return false;
  331. }
  332. digester d = tpm2_openssl_halg_to_digester(name_alg);
  333. d(buffer_marshalled_sensitiveArea,
  334. marshalled_sensitive_size_info + marshalled_sensitive_size
  335. + pubname->size,
  336. marshalled_sensitive_and_name_digest + digest_size_info);
  337. //Inner integrity
  338. encrypted_inner_integrity->size = marshalled_sensitive_size_info
  339. + marshalled_sensitive_size + pubname->size;
  340. return aes_encrypt_buffers(sym_alg, enc_sensitive_key->buffer,
  341. marshalled_sensitive_and_name_digest, hash_size + digest_size_info,
  342. buffer_marshalled_sensitiveArea,
  343. marshalled_sensitive_size_info + marshalled_sensitive_size,
  344. encrypted_inner_integrity);
  345. }
  346. void tpm2_identity_util_calculate_outer_integrity(TPMI_ALG_HASH parent_name_alg,
  347. TPM2B_NAME *pubname, TPM2B_MAX_BUFFER *marshalled_sensitive,
  348. TPM2B_MAX_BUFFER *protection_hmac_key,
  349. TPM2B_MAX_BUFFER *protection_enc_key, TPMT_SYM_DEF_OBJECT *sym_alg,
  350. TPM2B_MAX_BUFFER *encrypted_duplicate_sensitive,
  351. TPM2B_DIGEST *outer_hmac) {
  352. //Calculate dupSensitive
  353. encrypted_duplicate_sensitive->size = marshalled_sensitive->size;
  354. aes_encrypt_buffers(sym_alg, protection_enc_key->buffer,
  355. marshalled_sensitive->buffer, marshalled_sensitive->size,
  356. NULL, 0, encrypted_duplicate_sensitive);
  357. //Calculate outerHMAC
  358. hmac_outer_integrity(parent_name_alg, encrypted_duplicate_sensitive->buffer,
  359. encrypted_duplicate_sensitive->size, pubname->name, pubname->size,
  360. protection_hmac_key->buffer, outer_hmac);
  361. }
  362. bool tpm2_identity_create_name(TPM2B_PUBLIC *public, TPM2B_NAME *pubname) {
  363. /*
  364. * A TPM2B_NAME is the name of the algorithm, followed by the hash.
  365. * Calculate the name by:
  366. * 1. Marshaling the name algorithm
  367. * 2. Marshaling the TPMT_PUBLIC past the name algorithm from step 1.
  368. * 3. Hash the TPMT_PUBLIC portion in marshaled data.
  369. */
  370. TSS2_RC rval;
  371. TPMI_ALG_HASH name_alg = public->publicArea.nameAlg;
  372. // Step 1 - set beginning of name to hash alg
  373. size_t hash_offset = 0;
  374. rval = Tss2_MU_UINT16_Marshal(name_alg, pubname->name, pubname->size,
  375. &hash_offset);
  376. if (rval != TPM2_RC_SUCCESS)
  377. {
  378. LOG_ERR("Error serializing the name size");
  379. return false;
  380. }
  381. // Step 2 - marshal TPMTP
  382. TPMT_PUBLIC marshaled_tpmt;
  383. size_t tpmt_marshalled_size = 0;
  384. rval = Tss2_MU_TPMT_PUBLIC_Marshal(&public->publicArea,
  385. (uint8_t *) &marshaled_tpmt, sizeof(public->publicArea),
  386. &tpmt_marshalled_size);
  387. if (rval != TPM2_RC_SUCCESS)
  388. {
  389. LOG_ERR("Error serializing the public area");
  390. return false;
  391. }
  392. // Step 3 - Hash the data into name just past the alg type.
  393. digester d = tpm2_openssl_halg_to_digester(name_alg);
  394. if (!d) {
  395. return false;
  396. }
  397. d((const unsigned char *) &marshaled_tpmt, tpmt_marshalled_size,
  398. pubname->name + hash_offset);
  399. //Set the name size, UINT16 followed by HASH
  400. UINT16 hash_size = tpm2_alg_util_get_hash_size(name_alg);
  401. pubname->size = hash_size + hash_offset;
  402. return true;
  403. }