password_pbkdf2.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. buf = grub_malloc (pass->buflen);
  44. if (!buf)
  45. return grub_crypto_gcry_error (GPG_ERR_OUT_OF_MEMORY);
  46. err = grub_crypto_pbkdf2 (GRUB_MD_SHA512, (grub_uint8_t *) entered,
  47. grub_strlen (entered),
  48. pass->salt, pass->saltlen, pass->c,
  49. buf, pass->buflen);
  50. if (err)
  51. {
  52. grub_free (buf);
  53. return grub_crypto_gcry_error (err);
  54. }
  55. if (grub_crypto_memcmp (buf, pass->expected, pass->buflen) != 0)
  56. return GRUB_ACCESS_DENIED;
  57. grub_auth_authenticate (user);
  58. return GRUB_ERR_NONE;
  59. }
  60. static inline int
  61. hex2val (char hex)
  62. {
  63. if ('0' <= hex && hex <= '9')
  64. return hex - '0';
  65. if ('a' <= hex && hex <= 'f')
  66. return hex - 'a' + 10;
  67. if ('A' <= hex && hex <= 'F')
  68. return hex - 'A' + 10;
  69. return -1;
  70. }
  71. static grub_err_t
  72. grub_cmd_password (grub_command_t cmd __attribute__ ((unused)),
  73. int argc, char **args)
  74. {
  75. grub_err_t err;
  76. char *ptr, *ptr2;
  77. grub_uint8_t *ptro;
  78. struct pbkdf2_password *pass;
  79. if (argc != 2)
  80. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Two arguments expected.");
  81. if (grub_memcmp (args[1], "grub.pbkdf2.sha512.",
  82. sizeof ("grub.pbkdf2.sha512.") - 1) != 0)
  83. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Incorrect PBKDF2 password.");
  84. ptr = args[1] + sizeof ("grub.pbkdf2.sha512.") - 1;
  85. pass = grub_malloc (sizeof (*pass));
  86. if (!pass)
  87. return grub_errno;
  88. pass->c = grub_strtoul (ptr, &ptr, 0);
  89. if (*ptr != '.')
  90. {
  91. grub_free (pass);
  92. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Incorrect PBKDF2 password.");
  93. }
  94. ptr++;
  95. ptr2 = grub_strchr (ptr, '.');
  96. if (!ptr2 || ((ptr2 - ptr) & 1) || grub_strlen (ptr2 + 1) & 1)
  97. {
  98. grub_free (pass);
  99. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Incorrect PBKDF2 password.");
  100. }
  101. pass->saltlen = (ptr2 - ptr) >> 1;
  102. pass->buflen = grub_strlen (ptr2 + 1) >> 1;
  103. ptro = pass->salt = grub_malloc (pass->saltlen);
  104. if (!ptro)
  105. {
  106. grub_free (pass);
  107. return grub_errno;
  108. }
  109. while (ptr < ptr2)
  110. {
  111. int hex1, hex2;
  112. hex1 = hex2val (*ptr);
  113. ptr++;
  114. hex2 = hex2val (*ptr);
  115. ptr++;
  116. if (hex1 < 0 || hex2 < 0)
  117. {
  118. grub_free (pass->salt);
  119. grub_free (pass);
  120. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  121. "Incorrect PBKDF2 password.");
  122. }
  123. *ptro = (hex1 << 4) | hex2;
  124. ptro++;
  125. }
  126. ptro = pass->expected = grub_malloc (pass->buflen);
  127. if (!ptro)
  128. {
  129. grub_free (pass->salt);
  130. grub_free (pass);
  131. return grub_errno;
  132. }
  133. ptr = ptr2 + 1;
  134. ptr2 += grub_strlen (ptr2);
  135. while (ptr < ptr2)
  136. {
  137. int hex1, hex2;
  138. hex1 = hex2val (*ptr);
  139. ptr++;
  140. hex2 = hex2val (*ptr);
  141. ptr++;
  142. if (hex1 < 0 || hex2 < 0)
  143. {
  144. grub_free (pass->expected);
  145. grub_free (pass->salt);
  146. grub_free (pass);
  147. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  148. "Incorrect PBKDF2 password.");
  149. }
  150. *ptro = (hex1 << 4) | hex2;
  151. ptro++;
  152. }
  153. err = grub_auth_register_authentication (args[0], check_password, pass);
  154. if (err)
  155. {
  156. grub_free (pass);
  157. return err;
  158. }
  159. grub_dl_ref (my_mod);
  160. return GRUB_ERR_NONE;
  161. }
  162. static grub_command_t cmd;
  163. GRUB_MOD_INIT(password_pbkdf2)
  164. {
  165. my_mod = mod;
  166. cmd = grub_register_command ("password_pbkdf2", grub_cmd_password,
  167. N_("USER PBKDF2_PASSWORD"),
  168. N_("Set user password (PBKDF2). "));
  169. }
  170. GRUB_MOD_FINI(password_pbkdf2)
  171. {
  172. grub_unregister_command (cmd);
  173. }