v6-0003-cryptodisk-enable-the-backends-to-implement-detac.patch 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. From 7ebc9fc1076b2bf8dc728eca345db4a6b8ddff91 Mon Sep 17 00:00:00 2001
  2. From: John Lane <john@lane.uk.net>
  3. Date: Tue, 23 Jun 2015 11:16:30 +0100
  4. Subject: [PATCH v6 3/6] cryptodisk: enable the backends to implement detached
  5. headers
  6. Signed-off-by: John Lane <john@lane.uk.net>
  7. GNUtoo@cyberdimension.org: rebase, patch split, small fixes, commit message
  8. Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
  9. Reviewed-by: Patrick Steinhardt <ps@pks.im>
  10. ---
  11. grub-core/disk/cryptodisk.c | 24 ++++++++++++++++++++----
  12. grub-core/disk/geli.c | 15 +++++++++++++--
  13. grub-core/disk/luks.c | 14 +++++++++++---
  14. grub-core/disk/luks2.c | 15 ++++++++++++---
  15. include/grub/cryptodisk.h | 6 ++++--
  16. include/grub/file.h | 2 ++
  17. 6 files changed, 62 insertions(+), 14 deletions(-)
  18. diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
  19. index 1897acc4b..6ad2e486e 100644
  20. --- a/grub-core/disk/cryptodisk.c
  21. +++ b/grub-core/disk/cryptodisk.c
  22. @@ -41,6 +41,7 @@ static const struct grub_arg_option options[] =
  23. /* TRANSLATORS: It's still restricted to cryptodisks only. */
  24. {"all", 'a', 0, N_("Mount all."), 0, 0},
  25. {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0},
  26. + {"header", 'H', 0, N_("Read header from file"), 0, ARG_TYPE_STRING},
  27. {0, 0, 0, 0, 0, 0}
  28. };
  29. @@ -970,6 +971,7 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk)
  30. static int check_boot, have_it;
  31. static char *search_uuid;
  32. +static grub_file_t hdr;
  33. static void
  34. cryptodisk_close (grub_cryptodisk_t dev)
  35. @@ -994,13 +996,13 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source)
  36. FOR_CRYPTODISK_DEVS (cr)
  37. {
  38. - dev = cr->scan (source, search_uuid, check_boot);
  39. + dev = cr->scan (source, search_uuid, check_boot, hdr);
  40. if (grub_errno)
  41. return grub_errno;
  42. if (!dev)
  43. continue;
  44. - err = cr->recover_key (source, dev);
  45. + err = cr->recover_key (source, dev, hdr);
  46. if (err)
  47. {
  48. cryptodisk_close (dev);
  49. @@ -1041,7 +1043,7 @@ grub_cryptodisk_cheat_mount (const char *sourcedev, const char *cheat)
  50. FOR_CRYPTODISK_DEVS (cr)
  51. {
  52. - dev = cr->scan (source, search_uuid, check_boot);
  53. + dev = cr->scan (source, search_uuid, check_boot, NULL);
  54. if (grub_errno)
  55. return grub_errno;
  56. if (!dev)
  57. @@ -1095,6 +1097,20 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
  58. if (argc < 1 && !state[1].set && !state[2].set)
  59. return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
  60. + if (state[3].set) /* Detached header */
  61. + {
  62. + if (state[0].set)
  63. + return grub_error (GRUB_ERR_BAD_ARGUMENT,
  64. + N_("Cannot use UUID lookup with detached header"));
  65. +
  66. + hdr = grub_file_open (state[3].arg,
  67. + GRUB_FILE_TYPE_CRYPTODISK_DETACHED_HEADER);
  68. + if (!hdr)
  69. + return grub_errno;
  70. + }
  71. + else
  72. + hdr = NULL;
  73. +
  74. have_it = 0;
  75. if (state[0].set)
  76. {
  77. @@ -1302,7 +1318,7 @@ GRUB_MOD_INIT (cryptodisk)
  78. {
  79. grub_disk_dev_register (&grub_cryptodisk_dev);
  80. cmd = grub_register_extcmd ("cryptomount", grub_cmd_cryptomount, 0,
  81. - N_("SOURCE|-u UUID|-a|-b"),
  82. + N_("SOURCE|-u UUID|-a|-b|-H file"),
  83. N_("Mount a crypto device."), options);
  84. grub_procfs_register ("luks_script", &luks_script);
  85. }
  86. diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c
  87. index 581631c1d..acd09d874 100644
  88. --- a/grub-core/disk/geli.c
  89. +++ b/grub-core/disk/geli.c
  90. @@ -52,6 +52,7 @@
  91. #include <grub/dl.h>
  92. #include <grub/err.h>
  93. #include <grub/disk.h>
  94. +#include <grub/file.h>
  95. #include <grub/crypto.h>
  96. #include <grub/partition.h>
  97. #include <grub/i18n.h>
  98. @@ -121,6 +122,7 @@ enum
  99. /* FIXME: support version 0. */
  100. /* FIXME: support big-endian pre-version-4 volumes. */
  101. +/* FIXME: support for detached headers. */
  102. /* FIXME: support for keyfiles. */
  103. /* FIXME: support for HMAC. */
  104. const char *algorithms[] = {
  105. @@ -242,7 +244,8 @@ grub_util_get_geli_uuid (const char *dev)
  106. #endif
  107. static grub_cryptodisk_t
  108. -geli_scan (grub_disk_t disk, const char *check_uuid, int boot_only)
  109. +geli_scan (grub_disk_t disk, const char *check_uuid, int boot_only,
  110. + grub_file_t hdr)
  111. {
  112. grub_cryptodisk_t newdev;
  113. struct grub_geli_phdr header;
  114. @@ -254,6 +257,10 @@ geli_scan (grub_disk_t disk, const char *check_uuid, int boot_only)
  115. grub_disk_addr_t sector;
  116. grub_err_t err;
  117. + /* Detached headers are not implemented yet */
  118. + if (hdr)
  119. + return NULL;
  120. +
  121. if (2 * GRUB_MD_SHA256->mdlen + 1 > GRUB_CRYPTODISK_MAX_UUID_LENGTH)
  122. return NULL;
  123. @@ -397,7 +404,7 @@ geli_scan (grub_disk_t disk, const char *check_uuid, int boot_only)
  124. }
  125. static grub_err_t
  126. -geli_recover_key (grub_disk_t source, grub_cryptodisk_t dev)
  127. +geli_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr)
  128. {
  129. grub_size_t keysize;
  130. grub_uint8_t digest[GRUB_CRYPTO_MAX_MDLEN];
  131. @@ -413,6 +420,10 @@ geli_recover_key (grub_disk_t source, grub_cryptodisk_t dev)
  132. grub_disk_addr_t sector;
  133. grub_err_t err;
  134. + /* Detached headers are not implemented yet */
  135. + if (hdr)
  136. + return GRUB_ERR_NOT_IMPLEMENTED_YET;
  137. +
  138. if (dev->cipher->cipher->blocksize > GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE)
  139. return grub_error (GRUB_ERR_BUG, "cipher block is too long");
  140. diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c
  141. index b50f6fd02..685235565 100644
  142. --- a/grub-core/disk/luks.c
  143. +++ b/grub-core/disk/luks.c
  144. @@ -65,7 +65,8 @@ gcry_err_code_t AF_merge (const gcry_md_spec_t * hash, grub_uint8_t * src,
  145. grub_size_t blocknumbers);
  146. static grub_cryptodisk_t
  147. -luks_scan (grub_disk_t disk, const char *check_uuid, int check_boot)
  148. +luks_scan (grub_disk_t disk, const char *check_uuid, int check_boot,
  149. + grub_file_t hdr)
  150. {
  151. grub_cryptodisk_t newdev;
  152. const char *iptr;
  153. @@ -77,6 +78,10 @@ luks_scan (grub_disk_t disk, const char *check_uuid, int check_boot)
  154. char hashspec[sizeof (header.hashSpec) + 1];
  155. grub_err_t err;
  156. + /* Detached headers are not implemented yet */
  157. + if (hdr)
  158. + return NULL;
  159. +
  160. if (check_boot)
  161. return NULL;
  162. @@ -149,8 +154,7 @@ luks_scan (grub_disk_t disk, const char *check_uuid, int check_boot)
  163. }
  164. static grub_err_t
  165. -luks_recover_key (grub_disk_t source,
  166. - grub_cryptodisk_t dev)
  167. +luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr)
  168. {
  169. struct grub_luks_phdr header;
  170. grub_size_t keysize;
  171. @@ -163,6 +167,10 @@ luks_recover_key (grub_disk_t source,
  172. grub_size_t max_stripes = 1;
  173. char *tmp;
  174. + /* Detached headers are not implemented yet */
  175. + if (hdr)
  176. + return GRUB_ERR_NOT_IMPLEMENTED_YET;
  177. +
  178. err = grub_disk_read (source, 0, 0, sizeof (header), &header);
  179. if (err)
  180. return err;
  181. diff --git a/grub-core/disk/luks2.c b/grub-core/disk/luks2.c
  182. index e3ff7c83d..bc00e8bbc 100644
  183. --- a/grub-core/disk/luks2.c
  184. +++ b/grub-core/disk/luks2.c
  185. @@ -342,11 +342,16 @@ luks2_read_header (grub_disk_t disk, grub_luks2_header_t *outhdr)
  186. }
  187. static grub_cryptodisk_t
  188. -luks2_scan (grub_disk_t disk, const char *check_uuid, int check_boot)
  189. +luks2_scan (grub_disk_t disk, const char *check_uuid, int check_boot,
  190. + grub_file_t hdr_file)
  191. {
  192. grub_cryptodisk_t cryptodisk;
  193. grub_luks2_header_t header;
  194. + /* Detached headers are not implemented yet */
  195. + if (hdr_file)
  196. + return NULL;
  197. +
  198. if (check_boot)
  199. return NULL;
  200. @@ -523,8 +528,8 @@ luks2_decrypt_key (grub_uint8_t *out_key,
  201. }
  202. static grub_err_t
  203. -luks2_recover_key (grub_disk_t disk,
  204. - grub_cryptodisk_t crypt)
  205. +luks2_recover_key (grub_disk_t disk, grub_cryptodisk_t crypt,
  206. + grub_file_t hdr_file)
  207. {
  208. grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
  209. char passphrase[MAX_PASSPHRASE], cipher[32];
  210. @@ -538,6 +543,10 @@ luks2_recover_key (grub_disk_t disk,
  211. grub_json_t *json = NULL, keyslots;
  212. grub_err_t ret;
  213. + /* Detached headers are not implemented yet */
  214. + if (hdr_file)
  215. + return GRUB_ERR_NOT_IMPLEMENTED_YET;
  216. +
  217. ret = luks2_read_header (disk, &header);
  218. if (ret)
  219. return ret;
  220. diff --git a/include/grub/cryptodisk.h b/include/grub/cryptodisk.h
  221. index e1b21e785..e24b1b8cb 100644
  222. --- a/include/grub/cryptodisk.h
  223. +++ b/include/grub/cryptodisk.h
  224. @@ -20,6 +20,7 @@
  225. #define GRUB_CRYPTODISK_HEADER 1
  226. #include <grub/disk.h>
  227. +#include <grub/file.h>
  228. #include <grub/crypto.h>
  229. #include <grub/list.h>
  230. #ifdef GRUB_UTIL
  231. @@ -107,8 +108,9 @@ struct grub_cryptodisk_dev
  232. struct grub_cryptodisk_dev **prev;
  233. grub_cryptodisk_t (*scan) (grub_disk_t disk, const char *check_uuid,
  234. - int boot_only);
  235. - grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev);
  236. + int boot_only, grub_file_t hdr);
  237. + grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev,
  238. + grub_file_t hdr);
  239. };
  240. typedef struct grub_cryptodisk_dev *grub_cryptodisk_dev_t;
  241. diff --git a/include/grub/file.h b/include/grub/file.h
  242. index 31567483c..a7d7be853 100644
  243. --- a/include/grub/file.h
  244. +++ b/include/grub/file.h
  245. @@ -90,6 +90,8 @@ enum grub_file_type
  246. GRUB_FILE_TYPE_FONT,
  247. /* File holding encryption key for encrypted ZFS. */
  248. GRUB_FILE_TYPE_ZFS_ENCRYPTION_KEY,
  249. + /* File holiding the encryption metadata header */
  250. + GRUB_FILE_TYPE_CRYPTODISK_DETACHED_HEADER,
  251. /* File we open n grub-fstest. */
  252. GRUB_FILE_TYPE_FSTEST,
  253. /* File we open n grub-mount. */
  254. --
  255. 2.28.0