password_pbkdf2.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/auth.h>
  19. #include <grub/crypto.h>
  20. #include <grub/list.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/env.h>
  24. #include <grub/normal.h>
  25. #include <grub/dl.h>
  26. #include <grub/i18n.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. static grub_dl_t my_mod;
  29. struct pbkdf2_password
  30. {
  31. grub_uint8_t *salt;
  32. grub_size_t saltlen;
  33. unsigned int c;
  34. grub_uint8_t *expected;
  35. grub_size_t buflen;
  36. };
  37. static grub_err_t
  38. check_password (const char *user, const char *entered, void *pin)
  39. {
  40. grub_uint8_t *buf;
  41. struct pbkdf2_password *pass = pin;
  42. gcry_err_code_t err;
  43. grub_err_t ret;
  44. buf = grub_malloc (pass->buflen);
  45. if (!buf)
  46. return grub_crypto_gcry_error (GPG_ERR_OUT_OF_MEMORY);
  47. err = grub_crypto_pbkdf2 (GRUB_MD_SHA512, (grub_uint8_t *) entered,
  48. grub_strlen (entered),
  49. pass->salt, pass->saltlen, pass->c,
  50. buf, pass->buflen);
  51. if (err)
  52. ret = grub_crypto_gcry_error (err);
  53. else if (grub_crypto_memcmp (buf, pass->expected, pass->buflen) != 0)
  54. ret = GRUB_ACCESS_DENIED;
  55. else
  56. {
  57. grub_auth_authenticate (user);
  58. ret = GRUB_ERR_NONE;
  59. }
  60. grub_free (buf);
  61. return ret;
  62. }
  63. static inline int
  64. hex2val (char hex)
  65. {
  66. if ('0' <= hex && hex <= '9')
  67. return hex - '0';
  68. if ('a' <= hex && hex <= 'f')
  69. return hex - 'a' + 10;
  70. if ('A' <= hex && hex <= 'F')
  71. return hex - 'A' + 10;
  72. return -1;
  73. }
  74. static grub_err_t
  75. grub_cmd_password (grub_command_t cmd __attribute__ ((unused)),
  76. int argc, char **args)
  77. {
  78. grub_err_t err;
  79. const char *ptr, *ptr2;
  80. grub_uint8_t *ptro;
  81. struct pbkdf2_password *pass;
  82. if (argc != 2)
  83. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
  84. if (grub_memcmp (args[1], "grub.pbkdf2.sha512.",
  85. sizeof ("grub.pbkdf2.sha512.") - 1) != 0)
  86. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid PBKDF2 password"));
  87. ptr = args[1] + sizeof ("grub.pbkdf2.sha512.") - 1;
  88. pass = grub_malloc (sizeof (*pass));
  89. if (!pass)
  90. return grub_errno;
  91. pass->c = grub_strtoul (ptr, &ptr, 0);
  92. if (grub_errno)
  93. {
  94. grub_free (pass);
  95. return grub_errno;
  96. }
  97. if (*ptr != '.')
  98. {
  99. grub_free (pass);
  100. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid PBKDF2 password"));
  101. }
  102. ptr++;
  103. ptr2 = grub_strchr (ptr, '.');
  104. if (!ptr2 || ((ptr2 - ptr) & 1) || grub_strlen (ptr2 + 1) & 1)
  105. {
  106. grub_free (pass);
  107. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid PBKDF2 password"));
  108. }
  109. pass->saltlen = (ptr2 - ptr) >> 1;
  110. pass->buflen = grub_strlen (ptr2 + 1) >> 1;
  111. ptro = pass->salt = grub_malloc (pass->saltlen);
  112. if (!ptro)
  113. {
  114. grub_free (pass);
  115. return grub_errno;
  116. }
  117. while (ptr < ptr2)
  118. {
  119. int hex1, hex2;
  120. hex1 = hex2val (*ptr);
  121. ptr++;
  122. hex2 = hex2val (*ptr);
  123. ptr++;
  124. if (hex1 < 0 || hex2 < 0)
  125. {
  126. grub_free (pass->salt);
  127. grub_free (pass);
  128. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  129. /* TRANSLATORS: it means that the string which
  130. was supposed to be a password hash doesn't
  131. have a correct format, not to password
  132. mismatch. */
  133. N_("invalid PBKDF2 password"));
  134. }
  135. *ptro = (hex1 << 4) | hex2;
  136. ptro++;
  137. }
  138. ptro = pass->expected = grub_malloc (pass->buflen);
  139. if (!ptro)
  140. {
  141. grub_free (pass->salt);
  142. grub_free (pass);
  143. return grub_errno;
  144. }
  145. ptr = ptr2 + 1;
  146. ptr2 += grub_strlen (ptr2);
  147. while (ptr < ptr2)
  148. {
  149. int hex1, hex2;
  150. hex1 = hex2val (*ptr);
  151. ptr++;
  152. hex2 = hex2val (*ptr);
  153. ptr++;
  154. if (hex1 < 0 || hex2 < 0)
  155. {
  156. grub_free (pass->expected);
  157. grub_free (pass->salt);
  158. grub_free (pass);
  159. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  160. N_("invalid PBKDF2 password"));
  161. }
  162. *ptro = (hex1 << 4) | hex2;
  163. ptro++;
  164. }
  165. err = grub_auth_register_authentication (args[0], check_password, pass);
  166. if (err)
  167. {
  168. grub_free (pass);
  169. return err;
  170. }
  171. grub_dl_ref (my_mod);
  172. return GRUB_ERR_NONE;
  173. }
  174. static grub_command_t cmd;
  175. GRUB_MOD_INIT(password_pbkdf2)
  176. {
  177. my_mod = mod;
  178. cmd = grub_register_command ("password_pbkdf2", grub_cmd_password,
  179. N_("USER PBKDF2_PASSWORD"),
  180. N_("Set user password (PBKDF2). "));
  181. }
  182. GRUB_MOD_FINI(password_pbkdf2)
  183. {
  184. grub_unregister_command (cmd);
  185. }