tpm2_getekcertificate.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <curl/curl.h>
  7. #include <openssl/buffer.h>
  8. #include <openssl/evp.h>
  9. #include <openssl/sha.h>
  10. #include "files.h"
  11. #include "log.h"
  12. #include "object.h"
  13. #include "tpm2.h"
  14. #include "tpm2_alg_util.h"
  15. #include "tpm2_auth_util.h"
  16. #include "tpm2_capability.h"
  17. #include "tpm2_nv_util.h"
  18. #include "tpm2_tool.h"
  19. typedef struct tpm_getekcertificate_ctx tpm_getekcertificate_ctx;
  20. struct tpm_getekcertificate_ctx {
  21. // TPM Device properties
  22. bool is_tpm2_device_active;
  23. bool is_cert_on_nv;
  24. bool is_intc_cert;
  25. bool is_rsa_ek_cert_nv_location_defined;
  26. bool is_ecc_ek_cert_nv_location_defined;
  27. bool is_tpmgeneratedeps;
  28. // Certficate data handling
  29. uint8_t cert_count;
  30. char *ec_cert_path_1;
  31. FILE *ec_cert_file_handle_1;
  32. char *ec_cert_path_2;
  33. FILE *ec_cert_file_handle_2;
  34. unsigned char *rsa_cert_buffer;
  35. uint16_t rsa_cert_buffer_size;
  36. unsigned char *ecc_cert_buffer;
  37. uint16_t ecc_cert_buffer_size;
  38. bool is_cert_raw;
  39. // EK certificate hosting particulars
  40. char *ek_server_addr;
  41. unsigned int SSL_NO_VERIFY;
  42. char *ek_path;
  43. bool verbose;
  44. TPM2B_PUBLIC *out_public;
  45. };
  46. static tpm_getekcertificate_ctx ctx = {
  47. .is_tpm2_device_active = true,
  48. .ek_server_addr = "https://ekop.intel.com/ekcertservice/",
  49. .is_cert_on_nv = true,
  50. .cert_count = 0,
  51. };
  52. static unsigned char *hash_ek_public(void) {
  53. unsigned char *hash = (unsigned char*) malloc(SHA256_DIGEST_LENGTH);
  54. if (!hash) {
  55. LOG_ERR("OOM");
  56. return NULL;
  57. }
  58. SHA256_CTX sha256;
  59. int is_success = SHA256_Init(&sha256);
  60. if (!is_success) {
  61. LOG_ERR("SHA256_Init failed");
  62. goto err;
  63. }
  64. switch (ctx.out_public->publicArea.type) {
  65. case TPM2_ALG_RSA:
  66. is_success = SHA256_Update(&sha256,
  67. ctx.out_public->publicArea.unique.rsa.buffer,
  68. ctx.out_public->publicArea.unique.rsa.size);
  69. if (!is_success) {
  70. LOG_ERR("SHA256_Update failed");
  71. goto err;
  72. }
  73. if (ctx.out_public->publicArea.parameters.rsaDetail.exponent != 0) {
  74. LOG_ERR("non-default exponents unsupported");
  75. goto err;
  76. }
  77. BYTE buf[3] = { 0x1, 0x00, 0x01 }; // Exponent
  78. is_success = SHA256_Update(&sha256, buf, sizeof(buf));
  79. if (!is_success) {
  80. LOG_ERR("SHA256_Update failed");
  81. goto err;
  82. }
  83. break;
  84. case TPM2_ALG_ECC:
  85. is_success = SHA256_Update(&sha256,
  86. ctx.out_public->publicArea.unique.ecc.x.buffer,
  87. ctx.out_public->publicArea.unique.ecc.x.size);
  88. if (!is_success) {
  89. LOG_ERR("SHA256_Update failed");
  90. goto err;
  91. }
  92. is_success = SHA256_Update(&sha256,
  93. ctx.out_public->publicArea.unique.ecc.y.buffer,
  94. ctx.out_public->publicArea.unique.ecc.y.size);
  95. if (!is_success) {
  96. LOG_ERR("SHA256_Update failed");
  97. goto err;
  98. }
  99. break;
  100. default:
  101. LOG_ERR("unsupported EK algorithm");
  102. goto err;
  103. }
  104. is_success = SHA256_Final(hash, &sha256);
  105. if (!is_success) {
  106. LOG_ERR("SHA256_Final failed");
  107. goto err;
  108. }
  109. if (ctx.verbose) {
  110. tpm2_tool_output("public-key-hash:\n");
  111. tpm2_tool_output(" sha256: ");
  112. unsigned i;
  113. for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
  114. tpm2_tool_output("%02X", hash[i]);
  115. }
  116. tpm2_tool_output("\n");
  117. }
  118. return hash;
  119. err:
  120. free(hash);
  121. return NULL;
  122. }
  123. static char *base64_encode(const unsigned char* buffer)
  124. {
  125. BIO *bio, *b64;
  126. BUF_MEM *buffer_pointer;
  127. LOG_INFO("Calculating the base64_encode of the hash of the Endorsement"
  128. "Public Key:");
  129. if (buffer == NULL) {
  130. LOG_ERR("hash_ek_public returned null");
  131. return NULL;
  132. }
  133. b64 = BIO_new(BIO_f_base64());
  134. bio = BIO_new(BIO_s_mem());
  135. bio = BIO_push(b64, bio);
  136. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  137. BIO_write(bio, buffer, SHA256_DIGEST_LENGTH);
  138. UNUSED(BIO_flush(bio));
  139. BIO_get_mem_ptr(bio, &buffer_pointer);
  140. /* these are not NULL terminated */
  141. char *b64text = buffer_pointer->data;
  142. size_t len = buffer_pointer->length;
  143. size_t i;
  144. for (i = 0; i < len; i++) {
  145. if (b64text[i] == '+') {
  146. b64text[i] = '-';
  147. }
  148. if (b64text[i] == '/') {
  149. b64text[i] = '_';
  150. }
  151. }
  152. char *final_string = NULL;
  153. CURL *curl = curl_easy_init();
  154. if (curl) {
  155. char *output = curl_easy_escape(curl, b64text, len);
  156. if (output) {
  157. final_string = strdup(output);
  158. curl_free(output);
  159. }
  160. }
  161. curl_easy_cleanup(curl);
  162. curl_global_cleanup();
  163. BIO_free_all(bio);
  164. /* format to a proper NULL terminated string */
  165. return final_string;
  166. }
  167. static size_t writecallback(char *contents, size_t size, size_t nitems,
  168. void *CERT_BUFFER) {
  169. strncpy(CERT_BUFFER, (const char *)contents, nitems * size);
  170. ctx.rsa_cert_buffer_size = nitems * size;
  171. return ctx.rsa_cert_buffer_size;
  172. }
  173. static bool retrieve_web_endorsement_certificate(char *b64h) {
  174. #define NULL_TERM_LEN 1 // '\0'
  175. #define PATH_JOIN_CHAR_LEN 1 // '/'
  176. size_t len = strlen(ctx.ek_server_addr) + strlen(b64h) + NULL_TERM_LEN +
  177. PATH_JOIN_CHAR_LEN;
  178. char *weblink = (char *) malloc(len);
  179. if (!weblink) {
  180. LOG_ERR("oom");
  181. return false;
  182. }
  183. bool ret = true;
  184. CURLcode rc = curl_global_init(CURL_GLOBAL_DEFAULT);
  185. if (rc != CURLE_OK) {
  186. LOG_ERR("curl_global_init failed: %s", curl_easy_strerror(rc));
  187. ret = false;
  188. goto out_memory;
  189. }
  190. CURL *curl = curl_easy_init();
  191. if (!curl) {
  192. LOG_ERR("curl_easy_init failed");
  193. ret = false;
  194. goto out_global_cleanup;
  195. }
  196. /*
  197. * should not be used - Used only on platforms with older CA certificates.
  198. */
  199. if (ctx.SSL_NO_VERIFY) {
  200. rc = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  201. if (rc != CURLE_OK) {
  202. LOG_ERR("curl_easy_setopt for CURLOPT_SSL_VERIFYPEER failed: %s",
  203. curl_easy_strerror(rc));
  204. ret = false;
  205. goto out_easy_cleanup;
  206. }
  207. }
  208. snprintf(weblink, len, "%s%s%s", ctx.ek_server_addr, "/", b64h);
  209. rc = curl_easy_setopt(curl, CURLOPT_URL, weblink);
  210. if (rc != CURLE_OK) {
  211. LOG_ERR("curl_easy_setopt for CURLOPT_URL failed: %s",
  212. curl_easy_strerror(rc));
  213. ret = false;
  214. goto out_easy_cleanup;
  215. }
  216. /*
  217. * If verbose is set, add in diagnostic information for debugging connections.
  218. * https://curl.haxx.se/libcurl/c/CURLOPT_VERBOSE.html
  219. */
  220. rc = curl_easy_setopt(curl, CURLOPT_VERBOSE, (long )ctx.verbose);
  221. if (rc != CURLE_OK) {
  222. LOG_ERR("curl_easy_setopt for CURLOPT_VERBOSE failed: %s",
  223. curl_easy_strerror(rc));
  224. ret = false;
  225. goto out_easy_cleanup;
  226. }
  227. rc = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecallback);
  228. if (rc != CURLE_OK) {
  229. LOG_ERR("curl_easy_setopt for CURLOPT_WRITEFUNCTION failed: %s",
  230. curl_easy_strerror(rc));
  231. ret = false;
  232. goto out_easy_cleanup;
  233. }
  234. /*
  235. * As only one cert is downloaded at a time, we can simply use
  236. * rsa_cert_buffer for either RSA EK cert or ECC EK cert.
  237. */
  238. ctx.rsa_cert_buffer = malloc(CURL_MAX_WRITE_SIZE);
  239. rc = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)ctx.rsa_cert_buffer);
  240. if (rc != CURLE_OK) {
  241. LOG_ERR("curl_easy_setopt for CURLOPT_WRITEDATA failed: %s",
  242. curl_easy_strerror(rc));
  243. ret = false;
  244. goto out_easy_cleanup;
  245. }
  246. rc = curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
  247. if (rc != CURLE_OK) {
  248. LOG_ERR("curl_easy_setopt for CURLOPT_FAILONERROR failed: %s",
  249. curl_easy_strerror(rc));
  250. goto out_easy_cleanup;
  251. }
  252. rc = curl_easy_perform(curl);
  253. if (rc != CURLE_OK) {
  254. LOG_ERR("curl_easy_perform() failed: %s", curl_easy_strerror(rc));
  255. ret = false;
  256. goto out_easy_cleanup;
  257. }
  258. out_easy_cleanup:
  259. curl_easy_cleanup(curl);
  260. out_global_cleanup:
  261. curl_global_cleanup();
  262. out_memory:
  263. free(weblink);
  264. return ret;
  265. }
  266. static bool get_web_ek_certificate(void) {
  267. if (ctx.SSL_NO_VERIFY) {
  268. LOG_WARN("TLS communication with the said TPM manufacturer server setup"
  269. " with SSL_NO_VERIFY!");
  270. }
  271. bool ret = true;
  272. unsigned char *hash = hash_ek_public();
  273. char *b64 = base64_encode(hash);
  274. if (!b64) {
  275. LOG_ERR("base64_encode returned null");
  276. ret = false;
  277. goto out;
  278. }
  279. LOG_INFO("%s", b64);
  280. ret = retrieve_web_endorsement_certificate(b64);
  281. free(b64);
  282. out:
  283. free(hash);
  284. return ret;
  285. }
  286. #define INTC 0x494E5443
  287. #define IBM 0x49424D20
  288. #define RSA_EK_CERT_NV_INDEX 0x01C00002
  289. #define ECC_EK_CERT_NV_INDEX 0x01C0000A
  290. tool_rc get_tpm_properties(ESYS_CONTEXT *ectx) {
  291. TPMI_YES_NO more_data;
  292. TPMS_CAPABILITY_DATA *capability_data;
  293. tool_rc rc = tool_rc_success;
  294. rc = tpm2_getcap(ectx, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_MANUFACTURER,
  295. 1, &more_data, &capability_data);
  296. if (rc != tool_rc_success) {
  297. LOG_ERR("TPM property read failure.");
  298. goto get_tpm_properties_out;
  299. }
  300. if (capability_data->data.tpmProperties.tpmProperty[0].value == IBM) {
  301. LOG_WARN("The TPM device is a simulator —— Inspect the certficate chain and root certificate");
  302. }
  303. if (capability_data->data.tpmProperties.tpmProperty[0].value == INTC) {
  304. ctx.is_intc_cert = true;
  305. }
  306. free(capability_data);
  307. rc = tpm2_getcap(ectx, TPM2_CAP_TPM_PROPERTIES, TPM2_PT_PERMANENT,
  308. 1, &more_data, &capability_data);
  309. if (rc != tool_rc_success) {
  310. LOG_ERR("TPM property read failure.");
  311. goto get_tpm_properties_out;
  312. }
  313. if (capability_data->data.tpmProperties.tpmProperty[0].value &
  314. TPMA_PERMANENT_TPMGENERATEDEPS) {
  315. ctx.is_tpmgeneratedeps = true;
  316. }
  317. free(capability_data);
  318. rc = tpm2_getcap(ectx, TPM2_CAP_HANDLES,
  319. tpm2_util_hton_32(TPM2_HT_NV_INDEX), TPM2_PT_NV_INDEX_MAX, NULL,
  320. &capability_data);
  321. if (rc != tool_rc_success) {
  322. LOG_ERR("Failed to read capability data for NV indices.");
  323. ctx.is_cert_on_nv = false;
  324. goto get_tpm_properties_out;
  325. }
  326. if (capability_data->data.handles.count == 0) {
  327. ctx.is_cert_on_nv = false;
  328. goto get_tpm_properties_out;
  329. }
  330. UINT32 i;
  331. for (i = 0; i < capability_data->data.handles.count; i++) {
  332. TPMI_RH_NV_INDEX index = capability_data->data.handles.handle[i];
  333. if (index == RSA_EK_CERT_NV_INDEX) {
  334. ctx.is_rsa_ek_cert_nv_location_defined = true;
  335. }
  336. if (index == ECC_EK_CERT_NV_INDEX) {
  337. ctx.is_ecc_ek_cert_nv_location_defined = true;
  338. }
  339. }
  340. if (!ctx.is_rsa_ek_cert_nv_location_defined &&
  341. !ctx.is_ecc_ek_cert_nv_location_defined) {
  342. ctx.is_cert_on_nv = false;
  343. }
  344. get_tpm_properties_out:
  345. free(capability_data);
  346. return rc;
  347. }
  348. static tool_rc nv_read(ESYS_CONTEXT *ectx, TPMI_RH_NV_INDEX nv_index) {
  349. /*
  350. * Typical NV Index holding EK certificate has an empty auth
  351. * with attributes:
  352. * ppwrite|ppread|ownerread|authread|no_da|written|platformcreate
  353. */
  354. char index_string[11];
  355. if (nv_index == RSA_EK_CERT_NV_INDEX) {
  356. strcpy(index_string, "0x01C00002");
  357. } else {
  358. strcpy(index_string, "0x01C0000A");
  359. }
  360. tpm2_loaded_object object;
  361. tool_rc tmp_rc = tool_rc_success;
  362. tool_rc rc = tpm2_util_object_load_auth(ectx, index_string, NULL, &object,
  363. false, TPM2_HANDLE_FLAGS_NV);
  364. if (rc != tool_rc_success) {
  365. goto nv_read_out;
  366. }
  367. rc = nv_index == RSA_EK_CERT_NV_INDEX ?
  368. tpm2_util_nv_read(ectx, nv_index, 0, 0, &object, &ctx.rsa_cert_buffer,
  369. &ctx.rsa_cert_buffer_size, 0) :
  370. tpm2_util_nv_read(ectx, nv_index, 0, 0, &object, &ctx.ecc_cert_buffer,
  371. &ctx.ecc_cert_buffer_size, 0);
  372. nv_read_out:
  373. tmp_rc = tpm2_session_close(&object.session);
  374. if (rc != tool_rc_success) {
  375. return tmp_rc;
  376. }
  377. return rc;
  378. }
  379. static tool_rc get_nv_ek_certificate(ESYS_CONTEXT *ectx) {
  380. if (!ctx.is_cert_on_nv) {
  381. LOG_ERR("TCG specified location for EK certs aren't defined.");
  382. return tool_rc_general_error;
  383. }
  384. if (ctx.SSL_NO_VERIFY) {
  385. LOG_WARN("Ignoring -X or --allow-unverified if EK certificate found on NV");
  386. }
  387. if (ctx.ek_path) {
  388. LOG_WARN("Ignoring -u or --ek-public option if EK certificate found on NV");
  389. return tool_rc_option_error;
  390. }
  391. if (ctx.is_rsa_ek_cert_nv_location_defined &&
  392. ctx.is_ecc_ek_cert_nv_location_defined && ctx.cert_count == 1) {
  393. LOG_WARN("Found 2 certficates on NV. Add another -o to save the ECC cert");
  394. }
  395. if ((!ctx.is_rsa_ek_cert_nv_location_defined ||
  396. !ctx.is_ecc_ek_cert_nv_location_defined) && ctx.cert_count == 2) {
  397. LOG_WARN("Ignoring the additional output file since only 1 cert found on NV");
  398. }
  399. tool_rc rc = tool_rc_success;
  400. if (ctx.is_rsa_ek_cert_nv_location_defined) {
  401. rc = nv_read(ectx, RSA_EK_CERT_NV_INDEX);
  402. if (rc != tool_rc_success) {
  403. return rc;
  404. }
  405. }
  406. if (ctx.is_ecc_ek_cert_nv_location_defined) {
  407. rc = nv_read(ectx, ECC_EK_CERT_NV_INDEX);
  408. }
  409. return rc;
  410. }
  411. static tool_rc print_intel_ek_certificate_warning(void) {
  412. if (ctx.is_intc_cert && ctx.is_tpmgeneratedeps && !ctx.is_cert_on_nv) {
  413. LOG_ERR("Cannot proceed. For further information please refer to: "
  414. "https://www.intel.com/content/www/us/en/security-center/"
  415. "advisory/intel-sa-00086.html. Recovery tools are located here:"
  416. "https://github.com/intel/INTEL-SA-00086-Linux-Recovery-Tools");
  417. return tool_rc_general_error;
  418. }
  419. return tool_rc_success;
  420. }
  421. static tool_rc get_ek_certificates(ESYS_CONTEXT *ectx) {
  422. tool_rc rc = tool_rc_success;
  423. if (ctx.is_cert_on_nv) {
  424. rc = get_nv_ek_certificate(ectx);
  425. if (rc == tool_rc_success) {
  426. return rc;
  427. } else {
  428. LOG_WARN("EK certificate not found on NV");
  429. ctx.is_cert_on_nv = false;
  430. }
  431. }
  432. /*
  433. * Following is everything applicable to ctx.is_cert_on_nv = false.
  434. */
  435. rc = print_intel_ek_certificate_warning();
  436. if (rc != tool_rc_success) {
  437. return rc;
  438. }
  439. if (!ctx.ek_path) {
  440. LOG_ERR("Must specify the EK public key path");
  441. return tool_rc_option_error;
  442. }
  443. if (ctx.cert_count > 1) {
  444. LOG_ERR("Specify one output path for EK cert file per EK public key");
  445. return tool_rc_option_error;
  446. }
  447. bool retval = get_web_ek_certificate();
  448. if (!retval) {
  449. return tool_rc_general_error;
  450. }
  451. return tool_rc_success;
  452. }
  453. static tool_rc process_input(ESYS_CONTEXT *ectx) {
  454. if (ctx.ek_path) {
  455. ctx.out_public = malloc(sizeof(*ctx.out_public));
  456. ctx.out_public->size = 0;
  457. bool res = files_load_public(ctx.ek_path, ctx.out_public);
  458. if (!res) {
  459. LOG_ERR("Could not load EK public from file");
  460. return tool_rc_general_error;
  461. }
  462. }
  463. tool_rc rc = tool_rc_success;
  464. if (ctx.is_tpm2_device_active) {
  465. rc = get_tpm_properties(ectx);
  466. if (rc != tool_rc_success) {
  467. return rc;
  468. }
  469. }
  470. return print_intel_ek_certificate_warning();
  471. }
  472. static char *base64_decode(char **split, unsigned int cert_length) {
  473. *split += strlen("certficate\" : ");
  474. char *final_string = NULL;
  475. int outlen;
  476. CURL *curl = curl_easy_init();
  477. if (curl) {
  478. char *output = curl_easy_unescape(curl, *split, cert_length, &outlen);
  479. if (output) {
  480. final_string = strdup(output);
  481. curl_free(output);
  482. }
  483. }
  484. curl_easy_cleanup(curl);
  485. curl_global_cleanup();
  486. if(final_string) {
  487. size_t i;
  488. for (i = 0; i < strlen(final_string); i++) {
  489. final_string[i] = final_string[i] == '-' ? '+' : final_string[i];
  490. final_string[i] = final_string[i] == '_' ? '/' : final_string[i];
  491. final_string[i] = final_string[i] == '"' ? '\0' : final_string[i];
  492. final_string[i] = final_string[i] == '}' ? '\0' : final_string[i];
  493. }
  494. }
  495. return final_string;
  496. }
  497. #define PEM_BEGIN_CERT_LINE "\n-----BEGIN CERTIFICATE-----\n"
  498. #define PEM_END_CERT_LINE "\n-----END CERTIFICATE-----\n"
  499. static tool_rc process_output(void) {
  500. /*
  501. * Check if the cert is from INTC based on certificated data containing
  502. * the EK public hash in addition to the certificate data.
  503. * If so set the flag.
  504. */
  505. if (ctx.rsa_cert_buffer) {
  506. ctx.is_intc_cert = ctx.is_intc_cert ? ctx.is_intc_cert :
  507. !(strncmp((const char *)ctx.rsa_cert_buffer,
  508. "{\"pubhash", strlen("{\"pubhash")));
  509. }
  510. if (ctx.ecc_cert_buffer) {
  511. ctx.is_intc_cert = ctx.is_intc_cert ? ctx.is_intc_cert :
  512. !(strncmp((const char *)ctx.ecc_cert_buffer,
  513. "{\"pubhash", strlen("{\"pubhash")));
  514. }
  515. /*
  516. * Convert Intel EK certificates as received in the URL safe variant of
  517. * Base 64: https://tools.ietf.org/html/rfc4648#section-5 to PEM
  518. */
  519. if (ctx.rsa_cert_buffer && ctx.is_intc_cert && !ctx.is_cert_raw) {
  520. char *split = strstr((const char *)ctx.rsa_cert_buffer, "certificate");
  521. char *copy_buffer = base64_decode(&split, ctx.rsa_cert_buffer_size);
  522. ctx.rsa_cert_buffer_size = strlen(PEM_BEGIN_CERT_LINE) +
  523. strlen(copy_buffer) + strlen(PEM_END_CERT_LINE);
  524. strcpy((char *)ctx.rsa_cert_buffer, PEM_BEGIN_CERT_LINE);
  525. strcpy((char *)ctx.rsa_cert_buffer + strlen(PEM_BEGIN_CERT_LINE),
  526. copy_buffer);
  527. strcpy((char *)ctx.rsa_cert_buffer + strlen(PEM_BEGIN_CERT_LINE) +
  528. strlen(copy_buffer), PEM_END_CERT_LINE);
  529. free(copy_buffer);
  530. }
  531. if (ctx.ecc_cert_buffer && ctx.is_intc_cert && !ctx.is_cert_raw) {
  532. char *split = strstr((const char *)ctx.ecc_cert_buffer, "certificate");
  533. char *copy_buffer = base64_decode(&split, ctx.ecc_cert_buffer_size);
  534. ctx.ecc_cert_buffer_size = strlen(PEM_BEGIN_CERT_LINE) +
  535. strlen(copy_buffer) + strlen(PEM_END_CERT_LINE);
  536. strcpy((char *)ctx.ecc_cert_buffer, PEM_BEGIN_CERT_LINE);
  537. strcpy((char *)ctx.ecc_cert_buffer + strlen(PEM_BEGIN_CERT_LINE),
  538. copy_buffer);
  539. strcpy((char *)ctx.ecc_cert_buffer + strlen(PEM_BEGIN_CERT_LINE) +
  540. strlen(copy_buffer), PEM_END_CERT_LINE);
  541. free(copy_buffer);
  542. }
  543. bool retval = true;
  544. if (ctx.rsa_cert_buffer) {
  545. retval = files_write_bytes(
  546. ctx.ec_cert_file_handle_1 ? ctx.ec_cert_file_handle_1 : stdout,
  547. ctx.rsa_cert_buffer, ctx.rsa_cert_buffer_size);
  548. if (!retval) {
  549. return tool_rc_general_error;
  550. }
  551. }
  552. if (ctx.ecc_cert_buffer) {
  553. retval = files_write_bytes(
  554. ctx.ec_cert_file_handle_2 ? ctx.ec_cert_file_handle_2 :
  555. ctx.rsa_cert_buffer ? stdout : ctx.ec_cert_file_handle_1,
  556. ctx.ecc_cert_buffer, ctx.ecc_cert_buffer_size);
  557. if (!retval) {
  558. return tool_rc_general_error;
  559. }
  560. }
  561. return tool_rc_success;
  562. }
  563. static tool_rc check_input_options(void) {
  564. if (!ctx.ek_path && !ctx.is_cert_on_nv) {
  565. LOG_ERR("Must specify the EK public key path");
  566. return tool_rc_option_error;
  567. }
  568. if (!ctx.ek_server_addr && !ctx.is_cert_on_nv) {
  569. LOG_ERR("Must specify a valid remote server url!");
  570. return tool_rc_option_error;
  571. }
  572. if (ctx.ec_cert_path_1) {
  573. ctx.ec_cert_file_handle_1 = fopen(ctx.ec_cert_path_1, "wb");
  574. if (!ctx.ec_cert_file_handle_1) {
  575. LOG_ERR("Could not open file for writing: \"%s\"",
  576. ctx.ec_cert_path_1);
  577. return tool_rc_general_error;
  578. }
  579. }
  580. if (ctx.ec_cert_path_2) {
  581. ctx.ec_cert_file_handle_2 = fopen(ctx.ec_cert_path_2, "wb");
  582. if (!ctx.ec_cert_file_handle_2) {
  583. LOG_ERR("Could not open file for writing: \"%s\"",
  584. ctx.ec_cert_path_2);
  585. return tool_rc_general_error;
  586. }
  587. }
  588. return tool_rc_success;
  589. }
  590. static bool on_args(int argc, char **argv) {
  591. if (argc > 1) {
  592. LOG_ERR("Only supports one remote server url, got: %d", argc);
  593. return false;
  594. }
  595. ctx.ek_server_addr = argv[0];
  596. ctx.is_cert_on_nv = false;
  597. return true;
  598. }
  599. static bool on_option(char key, char *value) {
  600. switch (key) {
  601. case 'o':
  602. if (ctx.cert_count < 2) {
  603. ctx.cert_count++;
  604. } else {
  605. LOG_ERR("Specify only 2 outputs for RSA/ ECC certificates");
  606. return false;
  607. }
  608. if (ctx.cert_count == 1) {
  609. ctx.ec_cert_path_1 = value;
  610. }
  611. if (ctx.cert_count == 2) {
  612. ctx.ec_cert_path_2 = value;
  613. }
  614. break;
  615. case 'X':
  616. ctx.SSL_NO_VERIFY = 1;
  617. break;
  618. case 'u':
  619. ctx.ek_path = value;
  620. break;
  621. case 'x':
  622. ctx.is_tpm2_device_active = false;
  623. ctx.is_cert_on_nv = false;
  624. break;
  625. case 0:
  626. ctx.is_cert_raw = true;
  627. break;
  628. }
  629. return true;
  630. }
  631. static bool tpm2_tool_onstart(tpm2_options **opts) {
  632. const struct option topts[] =
  633. {
  634. { "ek-certificate", required_argument, NULL, 'o' },
  635. { "allow-unverified", no_argument, NULL, 'X' },
  636. { "ek-public", required_argument, NULL, 'u' },
  637. { "offline", no_argument, NULL, 'x' },
  638. { "raw", no_argument, NULL, 0 },
  639. };
  640. *opts = tpm2_options_new("o:u:Xx", ARRAY_LEN(topts), topts, on_option,
  641. on_args, 0);
  642. return *opts != NULL;
  643. }
  644. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  645. UNUSED(ectx);
  646. tool_rc rc = check_input_options();
  647. if (rc != tool_rc_success) {
  648. return rc;
  649. }
  650. rc = process_input(ectx);
  651. if (rc != tool_rc_success) {
  652. return rc;
  653. }
  654. ctx.verbose = flags.verbose;
  655. rc = get_ek_certificates(ectx);
  656. if (rc != tool_rc_success) {
  657. return rc;
  658. }
  659. return process_output();
  660. }
  661. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  662. UNUSED(ectx);
  663. if (ctx.ec_cert_file_handle_1) {
  664. fclose(ctx.ec_cert_file_handle_1);
  665. }
  666. if (ctx.ec_cert_file_handle_2) {
  667. fclose(ctx.ec_cert_file_handle_2);
  668. }
  669. if (ctx.rsa_cert_buffer) {
  670. free(ctx.rsa_cert_buffer);
  671. }
  672. if (ctx.ecc_cert_buffer) {
  673. free(ctx.ecc_cert_buffer);
  674. }
  675. return tool_rc_success;
  676. }
  677. static void tpm2_tool_onexit(void) {
  678. if (ctx.out_public) {
  679. free(ctx.out_public);
  680. }
  681. }
  682. // Register this tool with tpm2_tool.c
  683. TPM2_TOOL_REGISTER("getekcertificate", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, tpm2_tool_onexit)