2009-12-23 19:31:12 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/extcmd.h>
|
|
|
|
#include <grub/file.h>
|
|
|
|
#include <grub/disk.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/crypto.h>
|
|
|
|
#include <grub/normal.h>
|
2010-05-01 18:28:07 +00:00
|
|
|
#include <grub/i18n.h>
|
2009-12-23 19:31:12 +00:00
|
|
|
|
2011-04-11 21:01:51 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2009-12-23 19:31:12 +00:00
|
|
|
static const struct grub_arg_option options[] = {
|
2010-05-01 18:28:07 +00:00
|
|
|
{"hash", 'h', 0, N_("Specify hash to use."), N_("HASH"), ARG_TYPE_STRING},
|
2012-02-08 18:26:01 +00:00
|
|
|
{"check", 'c', 0, N_("Check hashes of files with hash list FILE."),
|
|
|
|
N_("FILE"), ARG_TYPE_STRING},
|
2012-02-05 10:23:47 +00:00
|
|
|
{"prefix", 'p', 0, N_("Base directory for hash list."), N_("DIR"),
|
2009-12-23 19:31:12 +00:00
|
|
|
ARG_TYPE_STRING},
|
2010-05-01 18:28:07 +00:00
|
|
|
{"keep-going", 'k', 0, N_("Don't stop after first error."), 0, 0},
|
2010-09-05 14:48:54 +00:00
|
|
|
{"uncompress", 'u', 0, N_("Uncompress file before checksumming."), 0, 0},
|
2009-12-23 19:31:12 +00:00
|
|
|
{0, 0, 0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2011-03-23 11:05:13 +00:00
|
|
|
static struct { const char *name; const char *hashname; } aliases[] =
|
2009-12-23 19:31:12 +00:00
|
|
|
{
|
|
|
|
{"sha256sum", "sha256"},
|
|
|
|
{"sha512sum", "sha512"},
|
2010-09-21 09:56:16 +00:00
|
|
|
{"sha1sum", "sha1"},
|
2009-12-23 19:31:12 +00:00
|
|
|
{"md5sum", "md5"},
|
2010-09-19 23:40:58 +00:00
|
|
|
{"crc", "crc32"},
|
2009-12-23 19:31:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
hextoval (char c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return c - '0';
|
|
|
|
if (c >= 'a' && c <= 'f')
|
|
|
|
return c - 'a' + 10;
|
|
|
|
if (c >= 'A' && c <= 'F')
|
|
|
|
return c - 'A' + 10;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
hash_file (grub_file_t file, const gcry_md_spec_t *hash, void *result)
|
|
|
|
{
|
2013-11-11 23:52:54 +00:00
|
|
|
void *context;
|
|
|
|
grub_uint8_t *readbuf;
|
|
|
|
#define BUF_SIZE 4096
|
|
|
|
readbuf = grub_malloc (BUF_SIZE);
|
|
|
|
if (!readbuf)
|
|
|
|
return grub_errno;
|
|
|
|
context = grub_zalloc (hash->contextsize);
|
|
|
|
if (!readbuf || !context)
|
|
|
|
goto fail;
|
2009-12-23 19:31:12 +00:00
|
|
|
|
|
|
|
hash->init (context);
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
grub_ssize_t r;
|
2013-11-11 23:52:54 +00:00
|
|
|
r = grub_file_read (file, readbuf, BUF_SIZE);
|
2009-12-23 19:31:12 +00:00
|
|
|
if (r < 0)
|
2013-11-11 23:52:54 +00:00
|
|
|
goto fail;
|
2009-12-23 19:31:12 +00:00
|
|
|
if (r == 0)
|
|
|
|
break;
|
|
|
|
hash->write (context, readbuf, r);
|
|
|
|
}
|
|
|
|
hash->final (context);
|
|
|
|
grub_memcpy (result, hash->read (context), hash->mdlen);
|
|
|
|
|
2013-11-11 23:52:54 +00:00
|
|
|
grub_free (readbuf);
|
|
|
|
grub_free (context);
|
|
|
|
|
2009-12-23 19:31:12 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2013-11-11 23:52:54 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
grub_free (readbuf);
|
|
|
|
grub_free (context);
|
|
|
|
return grub_errno;
|
2009-12-23 19:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
check_list (const gcry_md_spec_t *hash, const char *hashfilename,
|
2010-09-05 14:48:54 +00:00
|
|
|
const char *prefix, int keep, int uncompress)
|
2009-12-23 19:31:12 +00:00
|
|
|
{
|
|
|
|
grub_file_t hashlist, file;
|
|
|
|
char *buf = NULL;
|
2013-11-11 23:52:54 +00:00
|
|
|
grub_uint8_t expected[GRUB_CRYPTO_MAX_MDLEN];
|
|
|
|
grub_uint8_t actual[GRUB_CRYPTO_MAX_MDLEN];
|
2009-12-23 19:31:12 +00:00
|
|
|
grub_err_t err;
|
|
|
|
unsigned i;
|
|
|
|
unsigned unread = 0, mismatch = 0;
|
|
|
|
|
2013-11-11 23:52:54 +00:00
|
|
|
if (hash->mdlen > GRUB_CRYPTO_MAX_MDLEN)
|
|
|
|
return grub_error (GRUB_ERR_BUG, "mdlen is too long");
|
|
|
|
|
2013-11-20 01:28:29 +00:00
|
|
|
hashlist = grub_file_open (hashfilename, GRUB_FILE_TYPE_HASHLIST);
|
2009-12-23 19:31:12 +00:00
|
|
|
if (!hashlist)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
while (grub_free (buf), (buf = grub_file_getline (hashlist)))
|
|
|
|
{
|
|
|
|
const char *p = buf;
|
2012-03-11 13:43:18 +00:00
|
|
|
while (grub_isspace (p[0]))
|
|
|
|
p++;
|
2009-12-23 19:31:12 +00:00
|
|
|
for (i = 0; i < hash->mdlen; i++)
|
|
|
|
{
|
|
|
|
int high, low;
|
|
|
|
high = hextoval (*p++);
|
|
|
|
low = hextoval (*p++);
|
|
|
|
if (high < 0 || low < 0)
|
|
|
|
return grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid hash list");
|
|
|
|
expected[i] = (high << 4) | low;
|
|
|
|
}
|
2012-03-11 13:43:18 +00:00
|
|
|
if ((p[0] != ' ' && p[0] != '\t') || (p[1] != ' ' && p[1] != '\t'))
|
2009-12-23 19:31:12 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid hash list");
|
2012-03-11 13:43:18 +00:00
|
|
|
p += 2;
|
2009-12-23 19:31:12 +00:00
|
|
|
if (prefix)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
|
2010-01-20 08:12:47 +00:00
|
|
|
filename = grub_xasprintf ("%s/%s", prefix, p);
|
2009-12-23 19:31:12 +00:00
|
|
|
if (!filename)
|
|
|
|
return grub_errno;
|
2013-11-20 01:28:29 +00:00
|
|
|
file = grub_file_open (filename, GRUB_FILE_TYPE_TO_HASH
|
|
|
|
| (!uncompress ? GRUB_FILE_TYPE_NO_DECOMPRESS
|
|
|
|
: GRUB_FILE_TYPE_NONE));
|
2009-12-23 19:31:12 +00:00
|
|
|
grub_free (filename);
|
|
|
|
}
|
|
|
|
else
|
2013-11-20 01:28:29 +00:00
|
|
|
file = grub_file_open (p, GRUB_FILE_TYPE_TO_HASH
|
|
|
|
| (!uncompress ? GRUB_FILE_TYPE_NO_DECOMPRESS
|
|
|
|
: GRUB_FILE_TYPE_NONE));
|
2009-12-23 19:31:12 +00:00
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
grub_file_close (hashlist);
|
|
|
|
grub_free (buf);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
err = hash_file (file, hash, actual);
|
|
|
|
grub_file_close (file);
|
|
|
|
if (err)
|
|
|
|
{
|
2011-11-11 23:34:14 +00:00
|
|
|
grub_printf_ (N_("%s: READ ERROR\n"), p);
|
2009-12-23 19:31:12 +00:00
|
|
|
if (!keep)
|
|
|
|
{
|
|
|
|
grub_file_close (hashlist);
|
|
|
|
grub_free (buf);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
grub_print_error ();
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
unread++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (grub_crypto_memcmp (expected, actual, hash->mdlen) != 0)
|
|
|
|
{
|
2011-11-11 23:34:14 +00:00
|
|
|
grub_printf_ (N_("%s: HASH MISMATCH\n"), p);
|
2009-12-23 19:31:12 +00:00
|
|
|
if (!keep)
|
|
|
|
{
|
|
|
|
grub_file_close (hashlist);
|
|
|
|
grub_free (buf);
|
|
|
|
return grub_error (GRUB_ERR_TEST_FAILURE,
|
|
|
|
"hash of '%s' mismatches", p);
|
|
|
|
}
|
|
|
|
mismatch++;
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-11 23:34:14 +00:00
|
|
|
grub_printf_ (N_("%s: OK\n"), p);
|
2009-12-23 19:31:12 +00:00
|
|
|
}
|
|
|
|
if (mismatch || unread)
|
|
|
|
return grub_error (GRUB_ERR_TEST_FAILURE,
|
|
|
|
"%d files couldn't be read and hash "
|
|
|
|
"of %d files mismatches", unread, mismatch);
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
2010-06-10 06:42:03 +00:00
|
|
|
grub_cmd_hashsum (struct grub_extcmd_context *ctxt,
|
2009-12-23 19:31:12 +00:00
|
|
|
int argc, char **args)
|
|
|
|
{
|
2010-06-10 06:42:03 +00:00
|
|
|
struct grub_arg_list *state = ctxt->state;
|
2009-12-23 19:31:12 +00:00
|
|
|
const char *hashname = NULL;
|
|
|
|
const char *prefix = NULL;
|
|
|
|
const gcry_md_spec_t *hash;
|
|
|
|
unsigned i;
|
|
|
|
int keep = state[3].set;
|
2010-09-05 14:48:54 +00:00
|
|
|
int uncompress = state[4].set;
|
2009-12-23 19:31:12 +00:00
|
|
|
unsigned unread = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE (aliases); i++)
|
2010-06-10 06:42:03 +00:00
|
|
|
if (grub_strcmp (ctxt->extcmd->cmd->name, aliases[i].name) == 0)
|
2009-12-23 19:31:12 +00:00
|
|
|
hashname = aliases[i].hashname;
|
|
|
|
if (state[0].set)
|
|
|
|
hashname = state[0].arg;
|
|
|
|
|
|
|
|
if (!hashname)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no hash specified");
|
|
|
|
|
|
|
|
hash = grub_crypto_lookup_md_by_name (hashname);
|
|
|
|
if (!hash)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown hash");
|
|
|
|
|
2013-11-11 23:52:54 +00:00
|
|
|
if (hash->mdlen > GRUB_CRYPTO_MAX_MDLEN)
|
|
|
|
return grub_error (GRUB_ERR_BUG, "mdlen is too long");
|
|
|
|
|
2009-12-23 19:31:12 +00:00
|
|
|
if (state[2].set)
|
|
|
|
prefix = state[2].arg;
|
|
|
|
|
|
|
|
if (state[1].set)
|
|
|
|
{
|
|
|
|
if (argc != 0)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
|
"--check is incompatible with file list");
|
2010-09-05 14:48:54 +00:00
|
|
|
return check_list (hash, state[1].arg, prefix, keep, uncompress);
|
2009-12-23 19:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < (unsigned) argc; i++)
|
|
|
|
{
|
2013-11-11 23:52:54 +00:00
|
|
|
GRUB_PROPERLY_ALIGNED_ARRAY (result, GRUB_CRYPTO_MAX_MDLEN);
|
2009-12-23 19:31:12 +00:00
|
|
|
grub_file_t file;
|
|
|
|
grub_err_t err;
|
|
|
|
unsigned j;
|
2013-11-20 01:28:29 +00:00
|
|
|
file = grub_file_open (args[i], GRUB_FILE_TYPE_TO_HASH
|
|
|
|
| (!uncompress ? GRUB_FILE_TYPE_NO_DECOMPRESS
|
|
|
|
: GRUB_FILE_TYPE_NONE));
|
2009-12-23 19:31:12 +00:00
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
if (!keep)
|
|
|
|
return grub_errno;
|
|
|
|
grub_print_error ();
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
unread++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
err = hash_file (file, hash, result);
|
|
|
|
grub_file_close (file);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
if (!keep)
|
|
|
|
return err;
|
|
|
|
grub_print_error ();
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
unread++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (j = 0; j < hash->mdlen; j++)
|
2012-05-08 12:22:38 +00:00
|
|
|
grub_printf ("%02x", ((grub_uint8_t *) result)[j]);
|
2009-12-23 19:31:12 +00:00
|
|
|
grub_printf (" %s\n", args[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unread)
|
2012-03-10 11:23:47 +00:00
|
|
|
return grub_error (GRUB_ERR_TEST_FAILURE, "%d files couldn't be read",
|
2009-12-23 19:31:12 +00:00
|
|
|
unread);
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-09-21 09:56:16 +00:00
|
|
|
static grub_extcmd_t cmd, cmd_md5, cmd_sha1, cmd_sha256, cmd_sha512, cmd_crc;
|
2009-12-23 19:31:12 +00:00
|
|
|
|
|
|
|
GRUB_MOD_INIT(hashsum)
|
|
|
|
{
|
2010-09-14 21:06:01 +00:00
|
|
|
cmd = grub_register_extcmd ("hashsum", grub_cmd_hashsum, 0,
|
2011-11-11 23:34:14 +00:00
|
|
|
N_("-h HASH [-c FILE [-p PREFIX]] "
|
|
|
|
"[FILE1 [FILE2 ...]]"),
|
2012-03-06 13:11:10 +00:00
|
|
|
/* TRANSLATORS: "hash checksum" is just to
|
|
|
|
be a bit more precise, you can treat it as
|
|
|
|
just "hash". */
|
2010-09-21 10:14:06 +00:00
|
|
|
N_("Compute or check hash checksum."),
|
2009-12-23 19:31:12 +00:00
|
|
|
options);
|
2010-09-14 21:06:01 +00:00
|
|
|
cmd_md5 = grub_register_extcmd ("md5sum", grub_cmd_hashsum, 0,
|
2010-05-01 18:28:07 +00:00
|
|
|
N_("[-c FILE [-p PREFIX]] "
|
|
|
|
"[FILE1 [FILE2 ...]]"),
|
|
|
|
N_("Compute or check hash checksum."),
|
2009-12-23 19:31:12 +00:00
|
|
|
options);
|
2010-09-21 09:56:16 +00:00
|
|
|
cmd_sha1 = grub_register_extcmd ("sha1sum", grub_cmd_hashsum, 0,
|
|
|
|
N_("[-c FILE [-p PREFIX]] "
|
|
|
|
"[FILE1 [FILE2 ...]]"),
|
2010-09-21 10:14:06 +00:00
|
|
|
N_("Compute or check hash checksum."),
|
2010-09-21 09:56:16 +00:00
|
|
|
options);
|
2010-09-14 21:06:01 +00:00
|
|
|
cmd_sha256 = grub_register_extcmd ("sha256sum", grub_cmd_hashsum, 0,
|
2010-05-01 18:28:07 +00:00
|
|
|
N_("[-c FILE [-p PREFIX]] "
|
|
|
|
"[FILE1 [FILE2 ...]]"),
|
2010-09-21 10:14:06 +00:00
|
|
|
N_("Compute or check hash checksum."),
|
2009-12-23 19:31:12 +00:00
|
|
|
options);
|
2010-09-14 21:06:01 +00:00
|
|
|
cmd_sha512 = grub_register_extcmd ("sha512sum", grub_cmd_hashsum, 0,
|
2010-05-01 18:28:07 +00:00
|
|
|
N_("[-c FILE [-p PREFIX]] "
|
|
|
|
"[FILE1 [FILE2 ...]]"),
|
|
|
|
N_("Compute or check hash checksum."),
|
2009-12-23 19:31:12 +00:00
|
|
|
options);
|
2010-09-19 23:40:58 +00:00
|
|
|
|
|
|
|
cmd_crc = grub_register_extcmd ("crc", grub_cmd_hashsum, 0,
|
|
|
|
N_("[-c FILE [-p PREFIX]] "
|
|
|
|
"[FILE1 [FILE2 ...]]"),
|
|
|
|
N_("Compute or check hash checksum."),
|
|
|
|
options);
|
2009-12-23 19:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GRUB_MOD_FINI(hashsum)
|
|
|
|
{
|
|
|
|
grub_unregister_extcmd (cmd);
|
|
|
|
grub_unregister_extcmd (cmd_md5);
|
2010-09-21 09:56:16 +00:00
|
|
|
grub_unregister_extcmd (cmd_sha1);
|
2009-12-23 19:31:12 +00:00
|
|
|
grub_unregister_extcmd (cmd_sha256);
|
|
|
|
grub_unregister_extcmd (cmd_sha512);
|
2010-09-19 23:40:58 +00:00
|
|
|
grub_unregister_extcmd (cmd_crc);
|
2009-12-23 19:31:12 +00:00
|
|
|
}
|