0002-Cryptomount-support-key-files.patch 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. From 3b7ef4a5fd57b042201fbefb92a217070b944d67 Mon Sep 17 00:00:00 2001
  2. From: John Lane <john@lane.uk.net>
  3. Date: Fri, 26 Jun 2015 13:37:10 +0100
  4. Subject: [PATCH 03/10] Cryptomount support key files
  5. ---
  6. grub-core/disk/cryptodisk.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-
  7. grub-core/disk/geli.c | 4 +++-
  8. grub-core/disk/luks.c | 44 +++++++++++++++++++++++++++++--------------
  9. include/grub/cryptodisk.h | 5 ++++-
  10. 4 files changed, 82 insertions(+), 17 deletions(-)
  11. diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
  12. index dd8870d..0e7ced8 100644
  13. --- a/grub-core/disk/cryptodisk.c
  14. +++ b/grub-core/disk/cryptodisk.c
  15. @@ -42,6 +42,9 @@ static const struct grub_arg_option options[] =
  16. {"all", 'a', 0, N_("Mount all."), 0, 0},
  17. {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0},
  18. {"header", 'H', 0, N_("Read LUKS header from file"), 0, ARG_TYPE_STRING},
  19. + {"keyfile", 'k', 0, N_("Key file"), 0, ARG_TYPE_STRING},
  20. + {"keyfile-offset", 'O', 0, N_("Key file offset (bytes)"), 0, ARG_TYPE_INT},
  21. + {"keyfile-size", 'S', 0, N_("Key file data size (bytes)"), 0, ARG_TYPE_INT},
  22. {0, 0, 0, 0, 0, 0}
  23. };
  24. @@ -810,6 +813,8 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk)
  25. static int check_boot, have_it;
  26. static char *search_uuid;
  27. static grub_file_t hdr;
  28. +static grub_uint8_t *key, keyfile_buffer[GRUB_CRYPTODISK_MAX_KEYFILE_SIZE];
  29. +static grub_size_t keyfile_size;
  30. static void
  31. cryptodisk_close (grub_cryptodisk_t dev)
  32. @@ -840,7 +845,7 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source)
  33. if (!dev)
  34. continue;
  35. - err = cr->recover_key (source, dev, hdr);
  36. + err = cr->recover_key (source, dev, hdr, key, keyfile_size);
  37. if (err)
  38. {
  39. cryptodisk_close (dev);
  40. @@ -948,6 +953,45 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
  41. hdr = NULL;
  42. have_it = 0;
  43. + key = NULL;
  44. +
  45. + if (state[4].set) /* Key file; fails back to passphrase entry */
  46. + {
  47. + grub_file_t keyfile;
  48. + int keyfile_offset;
  49. + grub_size_t requested_keyfile_size;
  50. +
  51. + requested_keyfile_size = state[6].set ? grub_strtoul(state[6].arg, 0, 0) : 0;
  52. +
  53. + if (requested_keyfile_size > GRUB_CRYPTODISK_MAX_KEYFILE_SIZE)
  54. + grub_printf (N_("Key file size exceeds maximum (%llu)\n"), \
  55. + (unsigned long long) GRUB_CRYPTODISK_MAX_KEYFILE_SIZE);
  56. + else
  57. + {
  58. + keyfile_offset = state[5].set ? grub_strtoul (state[5].arg, 0, 0) : 0;
  59. + keyfile_size = requested_keyfile_size ? requested_keyfile_size : \
  60. + GRUB_CRYPTODISK_MAX_KEYFILE_SIZE;
  61. +
  62. + keyfile = grub_file_open (state[4].arg);
  63. + if (!keyfile)
  64. + grub_printf (N_("Unable to open key file %s\n"), state[4].arg);
  65. + else if (grub_file_seek (keyfile, keyfile_offset) == (grub_off_t)-1)
  66. + grub_printf (N_("Unable to seek to offset %d in key file\n"), keyfile_offset);
  67. + else
  68. + {
  69. + keyfile_size = grub_file_read (keyfile, keyfile_buffer, keyfile_size);
  70. + if (keyfile_size == (grub_size_t)-1)
  71. + grub_printf (N_("Error reading key file\n"));
  72. + else if (requested_keyfile_size && (keyfile_size != requested_keyfile_size))
  73. + grub_printf (N_("Cannot read %llu bytes for key file (read %llu bytes)\n"),
  74. + (unsigned long long) requested_keyfile_size,
  75. + (unsigned long long) keyfile_size);
  76. + else
  77. + key = keyfile_buffer;
  78. + }
  79. + }
  80. + }
  81. +
  82. if (state[0].set)
  83. {
  84. grub_cryptodisk_t dev;
  85. diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c
  86. index f4394eb..da6aa6a 100644
  87. --- a/grub-core/disk/geli.c
  88. +++ b/grub-core/disk/geli.c
  89. @@ -401,7 +401,9 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
  90. static grub_err_t
  91. recover_key (grub_disk_t source, grub_cryptodisk_t dev,
  92. - grub_file_t hdr __attribute__ ((unused)) )
  93. + grub_file_t hdr __attribute__ ((unused)),
  94. + grub_uint8_t *key __attribute__ ((unused)),
  95. + grub_size_t keyfile_size __attribute__ ((unused)) )
  96. {
  97. grub_size_t keysize;
  98. grub_uint8_t digest[GRUB_CRYPTO_MAX_MDLEN];
  99. diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c
  100. index 66e64c0..5882368 100644
  101. --- a/grub-core/disk/luks.c
  102. +++ b/grub-core/disk/luks.c
  103. @@ -322,12 +322,16 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
  104. static grub_err_t
  105. luks_recover_key (grub_disk_t source,
  106. grub_cryptodisk_t dev,
  107. - grub_file_t hdr)
  108. + grub_file_t hdr,
  109. + grub_uint8_t *keyfile_bytes,
  110. + grub_size_t keyfile_bytes_size)
  111. {
  112. struct grub_luks_phdr header;
  113. grub_size_t keysize;
  114. grub_uint8_t *split_key = NULL;
  115. - char passphrase[MAX_PASSPHRASE] = "";
  116. + char interactive_passphrase[MAX_PASSPHRASE] = "";
  117. + grub_uint8_t *passphrase;
  118. + grub_size_t passphrase_length;
  119. grub_uint8_t candidate_digest[sizeof (header.mkDigest)];
  120. unsigned i;
  121. grub_size_t length;
  122. @@ -364,18 +368,30 @@ luks_recover_key (grub_disk_t source,
  123. if (!split_key)
  124. return grub_errno;
  125. - /* Get the passphrase from the user. */
  126. - tmp = NULL;
  127. - if (source->partition)
  128. - tmp = grub_partition_get_name (source->partition);
  129. - grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
  130. - source->partition ? "," : "", tmp ? : "",
  131. - dev->uuid);
  132. - grub_free (tmp);
  133. - if (!grub_password_get (passphrase, MAX_PASSPHRASE))
  134. + if (keyfile_bytes)
  135. {
  136. - grub_free (split_key);
  137. - return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied");
  138. + /* Use bytestring from key file as passphrase */
  139. + passphrase = keyfile_bytes;
  140. + passphrase_length = keyfile_bytes_size;
  141. + }
  142. + else
  143. + {
  144. + /* Get the passphrase from the user. */
  145. + tmp = NULL;
  146. + if (source->partition)
  147. + tmp = grub_partition_get_name (source->partition);
  148. + grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
  149. + source->partition ? "," : "", tmp ? : "", dev->uuid);
  150. + grub_free (tmp);
  151. + if (!grub_password_get (interactive_passphrase, MAX_PASSPHRASE))
  152. + {
  153. + grub_free (split_key);
  154. + return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied");
  155. + }
  156. +
  157. + passphrase = (grub_uint8_t *)interactive_passphrase;
  158. + passphrase_length = grub_strlen (interactive_passphrase);
  159. +
  160. }
  161. /* Try to recover master key from each active keyslot. */
  162. @@ -393,7 +409,7 @@ luks_recover_key (grub_disk_t source,
  163. /* Calculate the PBKDF2 of the user supplied passphrase. */
  164. gcry_err = grub_crypto_pbkdf2 (dev->hash, (grub_uint8_t *) passphrase,
  165. - grub_strlen (passphrase),
  166. + passphrase_length,
  167. header.keyblock[i].passwordSalt,
  168. sizeof (header.keyblock[i].passwordSalt),
  169. grub_be_to_cpu32 (header.keyblock[i].
  170. diff --git a/include/grub/cryptodisk.h b/include/grub/cryptodisk.h
  171. index 4e6e89a..67f6b0b 100644
  172. --- a/include/grub/cryptodisk.h
  173. +++ b/include/grub/cryptodisk.h
  174. @@ -55,6 +55,8 @@ typedef enum
  175. #define GRUB_CRYPTODISK_GF_BYTES (1U << GRUB_CRYPTODISK_GF_LOG_BYTES)
  176. #define GRUB_CRYPTODISK_MAX_KEYLEN 128
  177. +#define GRUB_CRYPTODISK_MAX_KEYFILE_SIZE 8192
  178. +
  179. struct grub_cryptodisk;
  180. typedef gcry_err_code_t
  181. @@ -109,7 +111,8 @@ struct grub_cryptodisk_dev
  182. grub_cryptodisk_t (*scan) (grub_disk_t disk, const char *check_uuid,
  183. int boot_only, grub_file_t hdr);
  184. - grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev, grub_file_t hdr);
  185. + grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev,
  186. + grub_file_t hdr, grub_uint8_t *key, grub_size_t keyfile_size);
  187. };
  188. typedef struct grub_cryptodisk_dev *grub_cryptodisk_dev_t;
  189. --
  190. 1.9.1