password_pbkdf2.c 4.6 KB

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