~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to commands/password_pbkdf2.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

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