var-service-pkcs7.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0-or-later
  3. *
  4. * uefi vars device - pkcs7 verification
  5. */
  6. #include "qemu/osdep.h"
  7. #include "qemu/error-report.h"
  8. #include "system/dma.h"
  9. #include <gnutls/gnutls.h>
  10. #include <gnutls/pkcs7.h>
  11. #include <gnutls/crypto.h>
  12. #include "hw/uefi/var-service.h"
  13. #define AUTHVAR_DIGEST_ALGO GNUTLS_DIG_SHA256
  14. #define AUTHVAR_DIGEST_SIZE 32
  15. /*
  16. * Replicate the signed data for signature verification.
  17. */
  18. static gnutls_datum_t *build_signed_data(mm_variable_access *va, void *data)
  19. {
  20. variable_auth_2 *auth = data;
  21. uint64_t data_offset = sizeof(efi_time) + auth->hdr_length;
  22. uint16_t *name = (void *)va + sizeof(mm_variable_access);
  23. gnutls_datum_t *sdata;
  24. uint64_t pos = 0;
  25. sdata = g_new(gnutls_datum_t, 1);
  26. sdata->size = (va->name_size - 2
  27. + sizeof(QemuUUID)
  28. + sizeof(va->attributes)
  29. + sizeof(auth->timestamp)
  30. + va->data_size - data_offset);
  31. sdata->data = g_malloc(sdata->size);
  32. /* Variable Name (without terminating \0) */
  33. memcpy(sdata->data + pos, name, va->name_size - 2);
  34. pos += va->name_size - 2;
  35. /* Variable Namespace Guid */
  36. memcpy(sdata->data + pos, &va->guid, sizeof(va->guid));
  37. pos += sizeof(va->guid);
  38. /* Attributes */
  39. memcpy(sdata->data + pos, &va->attributes, sizeof(va->attributes));
  40. pos += sizeof(va->attributes);
  41. /* TimeStamp */
  42. memcpy(sdata->data + pos, &auth->timestamp, sizeof(auth->timestamp));
  43. pos += sizeof(auth->timestamp);
  44. /* Variable Content */
  45. memcpy(sdata->data + pos, data + data_offset, va->data_size - data_offset);
  46. pos += va->data_size - data_offset;
  47. assert(pos == sdata->size);
  48. return sdata;
  49. }
  50. /*
  51. * See WrapPkcs7Data() in edk2.
  52. *
  53. * UEFI spec allows pkcs7 signatures being used without the envelope which
  54. * identifies them as pkcs7 signatures. openssl and gnutls will not parse them
  55. * without the envelope though. So add it if needed.
  56. */
  57. static void wrap_pkcs7(gnutls_datum_t *pkcs7)
  58. {
  59. static uint8_t signed_data_oid[9] = {
  60. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02
  61. };
  62. gnutls_datum_t wrap;
  63. if (pkcs7->data[4] == 0x06 &&
  64. pkcs7->data[5] == 0x09 &&
  65. memcmp(pkcs7->data + 6, signed_data_oid, sizeof(signed_data_oid)) == 0 &&
  66. pkcs7->data[15] == 0x0a &&
  67. pkcs7->data[16] == 0x82) {
  68. return;
  69. }
  70. wrap.size = pkcs7->size + 19;
  71. wrap.data = g_malloc(wrap.size);
  72. wrap.data[0] = 0x30;
  73. wrap.data[1] = 0x82;
  74. wrap.data[2] = (wrap.size - 4) >> 8;
  75. wrap.data[3] = (wrap.size - 4) & 0xff;
  76. wrap.data[4] = 0x06;
  77. wrap.data[5] = 0x09;
  78. memcpy(wrap.data + 6, signed_data_oid, sizeof(signed_data_oid));
  79. wrap.data[15] = 0xa0;
  80. wrap.data[16] = 0x82;
  81. wrap.data[17] = pkcs7->size >> 8;
  82. wrap.data[18] = pkcs7->size & 0xff;
  83. memcpy(wrap.data + 19, pkcs7->data, pkcs7->size);
  84. g_free(pkcs7->data);
  85. *pkcs7 = wrap;
  86. }
  87. static gnutls_datum_t *build_pkcs7(void *data)
  88. {
  89. variable_auth_2 *auth = data;
  90. gnutls_datum_t *pkcs7;
  91. pkcs7 = g_new(gnutls_datum_t, 1);
  92. pkcs7->size = auth->hdr_length - 24;
  93. pkcs7->data = g_malloc(pkcs7->size);
  94. memcpy(pkcs7->data, data + 16 + 24, pkcs7->size);
  95. wrap_pkcs7(pkcs7);
  96. return pkcs7;
  97. }
  98. /*
  99. * Read UEFI signature database, store x509 all certificates found in
  100. * gnutls_x509_trust_list_t.
  101. */
  102. static gnutls_x509_trust_list_t build_trust_list_sb(uefi_variable *var)
  103. {
  104. gnutls_x509_trust_list_t tlist;
  105. gnutls_datum_t cert_data;
  106. gnutls_x509_crt_t cert;
  107. uefi_vars_siglist siglist;
  108. uefi_vars_cert *c;
  109. int rc;
  110. rc = gnutls_x509_trust_list_init(&tlist, 0);
  111. if (rc < 0) {
  112. warn_report("gnutls_x509_trust_list_init error: %s",
  113. gnutls_strerror(rc));
  114. return NULL;
  115. }
  116. uefi_vars_siglist_init(&siglist);
  117. uefi_vars_siglist_parse(&siglist, var->data, var->data_size);
  118. QTAILQ_FOREACH(c, &siglist.x509, next) {
  119. cert_data.size = c->size;
  120. cert_data.data = c->data;
  121. rc = gnutls_x509_crt_init(&cert);
  122. if (rc < 0) {
  123. warn_report("gnutls_x509_crt_init error: %s", gnutls_strerror(rc));
  124. break;
  125. }
  126. rc = gnutls_x509_crt_import(cert, &cert_data, GNUTLS_X509_FMT_DER);
  127. if (rc < 0) {
  128. warn_report("gnutls_x509_crt_import error: %s",
  129. gnutls_strerror(rc));
  130. gnutls_x509_crt_deinit(cert);
  131. break;
  132. }
  133. rc = gnutls_x509_trust_list_add_cas(tlist, &cert, 1, 0);
  134. if (rc < 0) {
  135. warn_report("gnutls_x509_crt_import error: %s",
  136. gnutls_strerror(rc));
  137. gnutls_x509_crt_deinit(cert);
  138. break;
  139. }
  140. }
  141. uefi_vars_siglist_free(&siglist);
  142. return tlist;
  143. }
  144. static int build_digest_authvar(gnutls_x509_crt_t signer,
  145. gnutls_x509_crt_t root,
  146. uint8_t *hash_digest)
  147. {
  148. char *cn;
  149. size_t cn_size = 0;
  150. uint8_t fp[AUTHVAR_DIGEST_SIZE];
  151. size_t fp_size = sizeof(fp);
  152. gnutls_hash_hd_t hash;
  153. int rc;
  154. /* get signer CN */
  155. rc = gnutls_x509_crt_get_dn_by_oid(signer, GNUTLS_OID_X520_COMMON_NAME,
  156. 0, 0, NULL, &cn_size);
  157. if (rc != GNUTLS_E_SHORT_MEMORY_BUFFER) {
  158. warn_report("gnutls_x509_crt_get_dn_by_oid error #1: %s",
  159. gnutls_strerror(rc));
  160. return rc;
  161. }
  162. cn = g_malloc(cn_size);
  163. rc = gnutls_x509_crt_get_dn_by_oid(signer, GNUTLS_OID_X520_COMMON_NAME,
  164. 0, 0, cn, &cn_size);
  165. if (rc < 0) {
  166. warn_report("gnutls_x509_crt_get_dn_by_oid error #2: %s",
  167. gnutls_strerror(rc));
  168. goto err;
  169. }
  170. /* get root certificate fingerprint */
  171. rc = gnutls_x509_crt_get_fingerprint(root, AUTHVAR_DIGEST_ALGO,
  172. fp, &fp_size);
  173. if (rc < 0) {
  174. warn_report("gnutls_x509_crt_get_fingerprint error: %s",
  175. gnutls_strerror(rc));
  176. goto err;
  177. }
  178. /* digest both items */
  179. rc = gnutls_hash_init(&hash, AUTHVAR_DIGEST_ALGO);
  180. if (rc < 0) {
  181. warn_report("gnutls_hash_init error: %s",
  182. gnutls_strerror(rc));
  183. goto err;
  184. }
  185. rc = gnutls_hash(hash, cn, cn_size);
  186. if (rc < 0) {
  187. warn_report("gnutls_hash error: %s",
  188. gnutls_strerror(rc));
  189. goto err;
  190. }
  191. rc = gnutls_hash(hash, fp, fp_size);
  192. if (rc < 0) {
  193. warn_report("gnutls_hash error: %s",
  194. gnutls_strerror(rc));
  195. goto err;
  196. }
  197. gnutls_hash_deinit(hash, hash_digest);
  198. return 0;
  199. err:
  200. g_free(cn);
  201. return rc;
  202. }
  203. /*
  204. * uefi spec 2.9, section 8.2.2
  205. *
  206. * For EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS variables which are
  207. * NOT secure boot variables we should track the root certificate of the trust
  208. * chain, and the subject CN of the signer certificate.
  209. *
  210. * So we'll go store a digest of these two items so we can verify this. Also
  211. * create a gnutls_x509_trust_list_t with the root certificate, so
  212. * gnutls_pkcs7_verify() will pass (assuming the signature is otherwise
  213. * correct).
  214. */
  215. static gnutls_x509_trust_list_t build_trust_list_authvar(gnutls_pkcs7_t pkcs7,
  216. uint8_t *hash_digest)
  217. {
  218. gnutls_datum_t signer_data = { 0 };
  219. gnutls_datum_t root_data = { 0 };
  220. gnutls_x509_crt_t signer = NULL;
  221. gnutls_x509_crt_t root = NULL;
  222. gnutls_x509_trust_list_t tlist = NULL;
  223. int n, rc;
  224. n = gnutls_pkcs7_get_crt_count(pkcs7);
  225. /* first is signer certificate */
  226. rc = gnutls_pkcs7_get_crt_raw2(pkcs7, 0, &signer_data);
  227. if (rc < 0) {
  228. warn_report("gnutls_pkcs7_get_crt_raw2(0) error: %s",
  229. gnutls_strerror(rc));
  230. goto done;
  231. }
  232. rc = gnutls_x509_crt_init(&signer);
  233. if (rc < 0) {
  234. warn_report("gnutls_x509_crt_init error: %s", gnutls_strerror(rc));
  235. goto done;
  236. }
  237. rc = gnutls_x509_crt_import(signer, &signer_data, GNUTLS_X509_FMT_DER);
  238. if (rc < 0) {
  239. warn_report("gnutls_x509_crt_import error: %s",
  240. gnutls_strerror(rc));
  241. gnutls_x509_crt_deinit(signer);
  242. goto done;
  243. }
  244. /* last is root-of-trust certificate (can be identical to signer) */
  245. rc = gnutls_pkcs7_get_crt_raw2(pkcs7, n - 1, &root_data);
  246. if (rc < 0) {
  247. warn_report("gnutls_pkcs7_get_crt_raw2(%d) error: %s",
  248. n - 1, gnutls_strerror(rc));
  249. goto done;
  250. }
  251. rc = gnutls_x509_crt_init(&root);
  252. if (rc < 0) {
  253. warn_report("gnutls_x509_crt_init error: %s", gnutls_strerror(rc));
  254. goto done;
  255. }
  256. rc = gnutls_x509_crt_import(root, &root_data, GNUTLS_X509_FMT_DER);
  257. if (rc < 0) {
  258. warn_report("gnutls_x509_crt_import error: %s",
  259. gnutls_strerror(rc));
  260. goto done;
  261. }
  262. /* calc digest for signer CN + root cert */
  263. rc = build_digest_authvar(signer, root, hash_digest);
  264. if (rc < 0) {
  265. goto done;
  266. }
  267. /* add root to trust list */
  268. rc = gnutls_x509_trust_list_init(&tlist, 0);
  269. if (rc < 0) {
  270. warn_report("gnutls_x509_trust_list_init error: %s",
  271. gnutls_strerror(rc));
  272. goto done;
  273. }
  274. rc = gnutls_x509_trust_list_add_cas(tlist, &root, 1, 0);
  275. if (rc < 0) {
  276. warn_report("gnutls_x509_crt_import error: %s",
  277. gnutls_strerror(rc));
  278. gnutls_x509_trust_list_deinit(tlist, 1);
  279. tlist = NULL;
  280. goto done;
  281. } else {
  282. /* ownership passed to tlist */
  283. root = NULL;
  284. }
  285. done:
  286. if (signer_data.data) {
  287. gnutls_free(signer_data.data);
  288. }
  289. if (root_data.data) {
  290. gnutls_free(root_data.data);
  291. }
  292. if (signer) {
  293. gnutls_x509_crt_deinit(signer);
  294. }
  295. if (root) {
  296. gnutls_x509_crt_deinit(root);
  297. }
  298. return tlist;
  299. }
  300. static void free_datum(gnutls_datum_t *ptr)
  301. {
  302. if (!ptr) {
  303. return;
  304. }
  305. g_free(ptr->data);
  306. g_free(ptr);
  307. }
  308. static void gnutls_log_stderr(int level, const char *msg)
  309. {
  310. if (strncmp(msg, "ASSERT:", 7) == 0) {
  311. return;
  312. }
  313. fprintf(stderr, " %d: %s", level, msg);
  314. }
  315. /*
  316. * pkcs7 signature verification (EFI_VARIABLE_AUTHENTICATION_2).
  317. */
  318. efi_status uefi_vars_check_pkcs7_2(uefi_variable *siglist,
  319. void **digest, uint32_t *digest_size,
  320. mm_variable_access *va, void *data)
  321. {
  322. gnutls_x509_trust_list_t tlist = NULL;
  323. gnutls_datum_t *signed_data = NULL;
  324. gnutls_datum_t *pkcs7_data = NULL;
  325. gnutls_pkcs7_t pkcs7 = NULL;
  326. efi_status status = EFI_SECURITY_VIOLATION;
  327. int rc;
  328. if (0) {
  329. /* gnutls debug logging */
  330. static bool first = true;
  331. if (first) {
  332. first = false;
  333. gnutls_global_set_log_function(gnutls_log_stderr);
  334. gnutls_global_set_log_level(99);
  335. }
  336. }
  337. signed_data = build_signed_data(va, data);
  338. pkcs7_data = build_pkcs7(data);
  339. rc = gnutls_pkcs7_init(&pkcs7);
  340. if (rc < 0) {
  341. warn_report("gnutls_pkcs7_init error: %s", gnutls_strerror(rc));
  342. goto out;
  343. }
  344. rc = gnutls_pkcs7_import(pkcs7, pkcs7_data, GNUTLS_X509_FMT_DER);
  345. if (rc < 0) {
  346. warn_report("gnutls_pkcs7_import error: %s", gnutls_strerror(rc));
  347. goto out;
  348. }
  349. if (siglist) {
  350. /* secure boot variables */
  351. tlist = build_trust_list_sb(siglist);
  352. } else if (digest && digest_size) {
  353. /* other authenticated variables */
  354. *digest_size = AUTHVAR_DIGEST_SIZE;
  355. *digest = g_malloc(*digest_size);
  356. tlist = build_trust_list_authvar(pkcs7, *digest);
  357. } else {
  358. /* should not happen */
  359. goto out;
  360. }
  361. rc = gnutls_pkcs7_verify(pkcs7, tlist,
  362. NULL, 0,
  363. 0, signed_data,
  364. GNUTLS_VERIFY_DISABLE_TIME_CHECKS |
  365. GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS);
  366. if (rc < 0) {
  367. warn_report("gnutls_pkcs7_verify error: %s", gnutls_strerror(rc));
  368. goto out;
  369. }
  370. /* check passed */
  371. status = EFI_SUCCESS;
  372. out:
  373. free_datum(signed_data);
  374. free_datum(pkcs7_data);
  375. if (tlist) {
  376. gnutls_x509_trust_list_deinit(tlist, 1);
  377. }
  378. if (pkcs7) {
  379. gnutls_pkcs7_deinit(pkcs7);
  380. }
  381. return status;
  382. }