v6-0005-cryptodisk-enable-the-backends-to-implement-key-f.patch 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. From f17a54035876808331c7ab753a0706de4bf46a7a 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 5/6] cryptodisk: enable the backends to implement key files
  5. Signed-off-by: John Lane <john@lane.uk.net>
  6. GNUtoo@cyberdimension.org: rebase, patch split, small fixes, commit message
  7. Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
  8. Reviewed-by: Patrick Steinhardt <ps@pks.im>
  9. ---
  10. grub-core/disk/cryptodisk.c | 87 ++++++++++++++++++++++++++++++++++++-
  11. grub-core/disk/geli.c | 7 +--
  12. grub-core/disk/luks.c | 7 ++-
  13. grub-core/disk/luks2.c | 7 +--
  14. include/grub/cryptodisk.h | 5 ++-
  15. include/grub/file.h | 2 +
  16. 6 files changed, 106 insertions(+), 9 deletions(-)
  17. diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
  18. index 6ad2e486e..dd94736d3 100644
  19. --- a/grub-core/disk/cryptodisk.c
  20. +++ b/grub-core/disk/cryptodisk.c
  21. @@ -42,6 +42,9 @@ static const struct grub_arg_option options[] =
  22. {"all", 'a', 0, N_("Mount all."), 0, 0},
  23. {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0},
  24. {"header", 'H', 0, N_("Read header from file"), 0, ARG_TYPE_STRING},
  25. + {"keyfile", 'k', 0, N_("Key file"), 0, ARG_TYPE_STRING},
  26. + {"keyfile-offset", 'O', 0, N_("Key file offset (bytes)"), 0, ARG_TYPE_INT},
  27. + {"keyfile-size", 'S', 0, N_("Key file data size (bytes)"), 0, ARG_TYPE_INT},
  28. {0, 0, 0, 0, 0, 0}
  29. };
  30. @@ -972,6 +975,8 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk)
  31. static int check_boot, have_it;
  32. static char *search_uuid;
  33. static grub_file_t hdr;
  34. +static grub_uint8_t *key, keyfile_buffer[GRUB_CRYPTODISK_MAX_KEYFILE_SIZE];
  35. +static grub_ssize_t key_size;
  36. static void
  37. cryptodisk_close (grub_cryptodisk_t dev)
  38. @@ -1002,7 +1007,7 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source)
  39. if (!dev)
  40. continue;
  41. - err = cr->recover_key (source, dev, hdr);
  42. + err = cr->recover_key (source, dev, hdr, key, key_size);
  43. if (err)
  44. {
  45. cryptodisk_close (dev);
  46. @@ -1112,6 +1117,86 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
  47. hdr = NULL;
  48. have_it = 0;
  49. + key = NULL;
  50. +
  51. + if (state[4].set) /* keyfile */
  52. + {
  53. + const char *p = NULL;
  54. + grub_file_t keyfile;
  55. + int keyfile_offset;
  56. + grub_size_t requested_keyfile_size = 0;
  57. +
  58. +
  59. + if (state[5].set) /* keyfile-offset */
  60. + {
  61. + keyfile_offset = grub_strtoul (state[5].arg, &p, 0);
  62. +
  63. + if (grub_errno != GRUB_ERR_NONE)
  64. + return grub_errno;
  65. +
  66. + if (*p != '\0')
  67. + return grub_error (GRUB_ERR_BAD_ARGUMENT,
  68. + N_("unrecognized number"));
  69. + }
  70. + else
  71. + {
  72. + keyfile_offset = 0;
  73. + }
  74. +
  75. + if (state[6].set) /* keyfile-size */
  76. + {
  77. + requested_keyfile_size = grub_strtoul (state[6].arg, &p, 0);
  78. +
  79. + if (*p != '\0')
  80. + return grub_error (GRUB_ERR_BAD_ARGUMENT,
  81. + N_("unrecognized number"));
  82. +
  83. + if (grub_errno != GRUB_ERR_NONE)
  84. + return grub_errno;
  85. +
  86. + if (requested_keyfile_size > GRUB_CRYPTODISK_MAX_KEYFILE_SIZE)
  87. + return grub_error (GRUB_ERR_OUT_OF_RANGE,
  88. + N_("Key file size exceeds maximum (%"
  89. + PRIuGRUB_SIZE ")\n"),
  90. + GRUB_CRYPTODISK_MAX_KEYFILE_SIZE);
  91. +
  92. + if (requested_keyfile_size == 0)
  93. + return grub_error (GRUB_ERR_OUT_OF_RANGE,
  94. + N_("Key file size is 0\n"));
  95. + }
  96. +
  97. + keyfile = grub_file_open (state[4].arg,
  98. + GRUB_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY);
  99. + if (!keyfile)
  100. + return grub_errno;
  101. +
  102. + if (grub_file_seek (keyfile, keyfile_offset) == (grub_off_t)-1)
  103. + return grub_errno;
  104. +
  105. + if (requested_keyfile_size)
  106. + {
  107. + if (requested_keyfile_size > (keyfile->size - keyfile_offset))
  108. + return grub_error (GRUB_ERR_FILE_READ_ERROR,
  109. + N_("Keyfile is too small: "
  110. + "requested %" PRIuGRUB_SIZE " bytes, "
  111. + "but the file only has %" PRIuGRUB_SIZE
  112. + " bytes.\n"),
  113. + requested_keyfile_size,
  114. + keyfile->size);
  115. +
  116. + key_size = requested_keyfile_size;
  117. + }
  118. + else
  119. + {
  120. + key_size = keyfile->size - keyfile_offset;
  121. + }
  122. +
  123. + if (grub_file_read (keyfile, keyfile_buffer, key_size) != key_size)
  124. + return grub_error (GRUB_ERR_FILE_READ_ERROR,
  125. + (N_("Error reading key file\n")));
  126. + key = keyfile_buffer;
  127. + }
  128. +
  129. if (state[0].set)
  130. {
  131. grub_cryptodisk_t dev;
  132. diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c
  133. index acd09d874..159ac0f96 100644
  134. --- a/grub-core/disk/geli.c
  135. +++ b/grub-core/disk/geli.c
  136. @@ -404,7 +404,8 @@ geli_scan (grub_disk_t disk, const char *check_uuid, int boot_only,
  137. }
  138. static grub_err_t
  139. -geli_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr)
  140. +geli_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr,
  141. + grub_uint8_t *key, grub_size_t keyfile_size)
  142. {
  143. grub_size_t keysize;
  144. grub_uint8_t digest[GRUB_CRYPTO_MAX_MDLEN];
  145. @@ -420,8 +421,8 @@ geli_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr)
  146. grub_disk_addr_t sector;
  147. grub_err_t err;
  148. - /* Detached headers are not implemented yet */
  149. - if (hdr)
  150. + /* Detached headers and keyfiles are not implemented yet */
  151. + if (hdr || key || keyfile_size)
  152. return GRUB_ERR_NOT_IMPLEMENTED_YET;
  153. if (dev->cipher->cipher->blocksize > GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE)
  154. diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c
  155. index 6286302e7..0dd33b2af 100644
  156. --- a/grub-core/disk/luks.c
  157. +++ b/grub-core/disk/luks.c
  158. @@ -161,7 +161,8 @@ luks_scan (grub_disk_t disk, const char *check_uuid, int check_boot,
  159. }
  160. static grub_err_t
  161. -luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr)
  162. +luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr,
  163. + grub_uint8_t *keyfile_bytes, grub_size_t keyfile_bytes_size)
  164. {
  165. struct grub_luks_phdr header;
  166. grub_size_t keysize;
  167. @@ -175,6 +176,10 @@ luks_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_file_t hdr)
  168. char *tmp;
  169. grub_uint32_t sector;
  170. + /* Keyfiles are not implemented yet */
  171. + if (keyfile_bytes || keyfile_bytes_size)
  172. + return GRUB_ERR_NOT_IMPLEMENTED_YET;
  173. +
  174. if (hdr)
  175. {
  176. if (grub_file_seek (hdr, 0) == (grub_off_t) -1)
  177. diff --git a/grub-core/disk/luks2.c b/grub-core/disk/luks2.c
  178. index bc00e8bbc..6a38a1f4d 100644
  179. --- a/grub-core/disk/luks2.c
  180. +++ b/grub-core/disk/luks2.c
  181. @@ -529,7 +529,8 @@ luks2_decrypt_key (grub_uint8_t *out_key,
  182. static grub_err_t
  183. luks2_recover_key (grub_disk_t disk, grub_cryptodisk_t crypt,
  184. - grub_file_t hdr_file)
  185. + grub_file_t hdr_file, grub_uint8_t *key,
  186. + grub_size_t keyfile_size)
  187. {
  188. grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
  189. char passphrase[MAX_PASSPHRASE], cipher[32];
  190. @@ -543,8 +544,8 @@ luks2_recover_key (grub_disk_t disk, grub_cryptodisk_t crypt,
  191. grub_json_t *json = NULL, keyslots;
  192. grub_err_t ret;
  193. - /* Detached headers are not implemented yet */
  194. - if (hdr_file)
  195. + /* Detached headers and keyfiles are not implemented yet */
  196. + if (hdr_file || key || keyfile_size)
  197. return GRUB_ERR_NOT_IMPLEMENTED_YET;
  198. ret = luks2_read_header (disk, &header);
  199. diff --git a/include/grub/cryptodisk.h b/include/grub/cryptodisk.h
  200. index e24b1b8cb..6d2610f93 100644
  201. --- a/include/grub/cryptodisk.h
  202. +++ b/include/grub/cryptodisk.h
  203. @@ -55,6 +55,8 @@ typedef enum
  204. #define GRUB_CRYPTODISK_GF_BYTES (1U << GRUB_CRYPTODISK_GF_LOG_BYTES)
  205. #define GRUB_CRYPTODISK_MAX_KEYLEN 128
  206. +#define GRUB_CRYPTODISK_MAX_KEYFILE_SIZE 8192
  207. +
  208. struct grub_cryptodisk;
  209. typedef gcry_err_code_t
  210. @@ -110,7 +112,8 @@ struct grub_cryptodisk_dev
  211. grub_cryptodisk_t (*scan) (grub_disk_t disk, const char *check_uuid,
  212. int boot_only, grub_file_t hdr);
  213. grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev,
  214. - grub_file_t hdr);
  215. + grub_file_t hdr, grub_uint8_t *key,
  216. + grub_size_t keyfile_size);
  217. };
  218. typedef struct grub_cryptodisk_dev *grub_cryptodisk_dev_t;
  219. diff --git a/include/grub/file.h b/include/grub/file.h
  220. index a7d7be853..97678aa45 100644
  221. --- a/include/grub/file.h
  222. +++ b/include/grub/file.h
  223. @@ -92,6 +92,8 @@ enum grub_file_type
  224. GRUB_FILE_TYPE_ZFS_ENCRYPTION_KEY,
  225. /* File holiding the encryption metadata header */
  226. GRUB_FILE_TYPE_CRYPTODISK_DETACHED_HEADER,
  227. + /* File holiding the encryption key */
  228. + GRUB_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY,
  229. /* File we open n grub-fstest. */
  230. GRUB_FILE_TYPE_FSTEST,
  231. /* File we open n grub-mount. */
  232. --
  233. 2.28.0