2012-04-23 10:14:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
|
|
|
*/
|
2012-05-14 07:46:58 +00:00
|
|
|
#define _GNU_SOURCE
|
2012-04-23 09:18:34 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-05-14 07:46:58 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
|
2012-05-13 04:21:20 +00:00
|
|
|
#include <ccan/talloc/talloc.h>
|
|
|
|
|
2012-04-23 09:18:34 +00:00
|
|
|
#include "image.h"
|
2012-05-13 04:21:20 +00:00
|
|
|
#include "idc.h"
|
2012-04-23 09:18:34 +00:00
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/pkcs7.h>
|
2012-05-14 07:46:58 +00:00
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/x509v3.h>
|
2012-04-23 09:18:34 +00:00
|
|
|
|
2012-05-23 03:52:22 +00:00
|
|
|
static const char *toolname = "sbverify";
|
|
|
|
|
2012-05-13 04:21:20 +00:00
|
|
|
enum verify_status {
|
|
|
|
VERIFY_FAIL = 0,
|
|
|
|
VERIFY_OK = 1,
|
|
|
|
};
|
2012-04-23 09:18:34 +00:00
|
|
|
|
2012-05-14 07:46:58 +00:00
|
|
|
static struct option options[] = {
|
|
|
|
{ "cert", required_argument, NULL, 'c' },
|
|
|
|
{ "no-verify", no_argument, NULL, 'n' },
|
2012-05-23 03:52:22 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
2012-05-14 07:46:58 +00:00
|
|
|
{ NULL, 0, NULL, 0 },
|
|
|
|
};
|
|
|
|
|
2012-05-23 03:52:22 +00:00
|
|
|
static void usage(void)
|
2012-05-14 07:46:58 +00:00
|
|
|
{
|
2012-05-23 03:52:22 +00:00
|
|
|
printf("Usage: %s [options] --cert <certfile> <efi-boot-image>\n"
|
|
|
|
"Verify a UEFI secure boot image.\n\n"
|
|
|
|
"Options:\n"
|
2012-05-14 07:46:58 +00:00
|
|
|
"\t--cert <certfile> certificate (x509 certificate)\n"
|
|
|
|
"\t--no-verify don't perform certificate verification\n",
|
2012-05-23 03:52:22 +00:00
|
|
|
toolname);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void version(void)
|
|
|
|
{
|
|
|
|
printf("%s %s\n", toolname, VERSION);
|
2012-05-14 07:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int load_cert(X509_STORE *certs, const char *filename)
|
|
|
|
{
|
|
|
|
X509 *cert;
|
|
|
|
BIO *bio;
|
|
|
|
|
|
|
|
bio = NULL;
|
|
|
|
cert = NULL;
|
|
|
|
|
|
|
|
bio = BIO_new_file(filename, "r");
|
|
|
|
if (!bio) {
|
|
|
|
fprintf(stderr, "Couldn't open file %s\n", filename);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
|
|
|
|
if (!cert) {
|
|
|
|
fprintf(stderr, "Couldn't read certificate file %s\n",
|
|
|
|
filename);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
X509_STORE_add_cert(certs, cert);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
if (cert)
|
|
|
|
X509_free(cert);
|
|
|
|
if (bio)
|
|
|
|
BIO_free(bio);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int x509_verify_cb(int status, X509_STORE_CTX *ctx)
|
|
|
|
{
|
|
|
|
int err = X509_STORE_CTX_get_error(ctx);
|
|
|
|
|
|
|
|
/* also accept code-signing keys */
|
|
|
|
if (err == X509_V_ERR_INVALID_PURPOSE
|
|
|
|
&& ctx->cert->ex_xkusage == XKU_CODE_SIGN)
|
|
|
|
status = 1;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-04-23 09:18:34 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct cert_table_header *header;
|
2012-05-13 04:21:20 +00:00
|
|
|
enum verify_status status;
|
2012-05-14 07:46:58 +00:00
|
|
|
int rc, c, flags, verify;
|
2012-04-23 09:18:34 +00:00
|
|
|
struct image *image;
|
|
|
|
const uint8_t *buf;
|
2012-05-14 07:46:58 +00:00
|
|
|
X509_STORE *certs;
|
2012-05-13 06:11:46 +00:00
|
|
|
struct idc *idc;
|
2012-04-23 09:18:34 +00:00
|
|
|
BIO *idcbio;
|
|
|
|
PKCS7 *p7;
|
|
|
|
|
2012-05-13 04:21:20 +00:00
|
|
|
status = VERIFY_FAIL;
|
2012-05-14 07:46:58 +00:00
|
|
|
certs = X509_STORE_new();
|
|
|
|
verify = 1;
|
2012-05-13 04:21:20 +00:00
|
|
|
|
2012-05-14 07:53:26 +00:00
|
|
|
OpenSSL_add_all_digests();
|
2012-05-14 07:46:58 +00:00
|
|
|
ERR_load_crypto_strings();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int idx;
|
|
|
|
c = getopt_long(argc, argv, "c:n", options, &idx);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
rc = load_cert(certs, optarg);
|
|
|
|
if (rc)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
verify = 0;
|
|
|
|
break;
|
2012-05-23 03:52:22 +00:00
|
|
|
case 'V':
|
|
|
|
version();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
return EXIT_SUCCESS;
|
2012-05-14 07:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc != optind + 1) {
|
2012-05-23 03:52:22 +00:00
|
|
|
usage();
|
2012-04-23 09:18:34 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-05-14 07:46:58 +00:00
|
|
|
image = image_load(argv[optind]);
|
2012-04-23 09:18:34 +00:00
|
|
|
image_pecoff_parse(image);
|
2012-05-13 04:21:20 +00:00
|
|
|
image_find_regions(image);
|
2012-04-23 09:18:34 +00:00
|
|
|
|
2012-05-12 17:47:21 +00:00
|
|
|
if (!image->data_dir_sigtable->addr
|
|
|
|
|| !image->data_dir_sigtable->size) {
|
|
|
|
fprintf(stderr, "No signature table present\n");
|
2012-05-13 04:21:20 +00:00
|
|
|
goto out;
|
2012-05-12 17:47:21 +00:00
|
|
|
}
|
|
|
|
|
2012-05-14 07:52:03 +00:00
|
|
|
header = image->buf + image->data_dir_sigtable->addr;
|
2012-04-23 09:18:34 +00:00
|
|
|
buf = (void *)(header + 1);
|
|
|
|
p7 = d2i_PKCS7(NULL, &buf, header->size);
|
2012-05-14 07:52:03 +00:00
|
|
|
if (!p7) {
|
|
|
|
fprintf(stderr, "Unable to parse signature data\n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-04-23 09:18:34 +00:00
|
|
|
|
2012-05-13 06:11:46 +00:00
|
|
|
idcbio = BIO_new(BIO_s_mem());
|
|
|
|
idc = IDC_get(p7, idcbio);
|
|
|
|
if (!idc)
|
2012-05-13 04:21:20 +00:00
|
|
|
goto out;
|
|
|
|
|
2012-05-13 06:11:46 +00:00
|
|
|
rc = IDC_check_hash(idc, image);
|
|
|
|
if (rc)
|
|
|
|
goto out;
|
2012-04-23 09:18:34 +00:00
|
|
|
|
2012-05-14 07:46:58 +00:00
|
|
|
flags = PKCS7_BINARY;
|
|
|
|
if (!verify)
|
|
|
|
flags |= PKCS7_NOVERIFY;
|
|
|
|
|
|
|
|
X509_STORE_set_verify_cb_func(certs, x509_verify_cb);
|
|
|
|
rc = PKCS7_verify(p7, NULL, certs, idcbio, NULL, flags);
|
2012-04-23 09:18:34 +00:00
|
|
|
if (!rc) {
|
|
|
|
printf("PKCS7 verification failed\n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
2012-05-13 04:21:20 +00:00
|
|
|
goto out;
|
2012-04-23 09:18:34 +00:00
|
|
|
}
|
|
|
|
|
2012-05-13 04:21:20 +00:00
|
|
|
status = VERIFY_OK;
|
|
|
|
|
|
|
|
out:
|
|
|
|
talloc_free(image);
|
|
|
|
if (status == VERIFY_OK)
|
|
|
|
printf("Signature verification OK\n");
|
|
|
|
else
|
|
|
|
printf("Signature verification failed\n");
|
|
|
|
|
|
|
|
return status == VERIFY_OK ? EXIT_SUCCESS : EXIT_FAILURE;
|
2012-04-23 09:18:34 +00:00
|
|
|
}
|