module_signing.c 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* Module signature checker
 *
 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public Licence
 * as published by the Free Software Foundation; either version
 * 2 of the Licence, or (at your option) any later version.
 */

#include <linux/kernel.h>
13
#include <linux/errno.h>
14
#include <linux/string.h>
15
#include <linux/verification.h>
16
#include <crypto/public_key.h>
17 18
#include "module-internal.h"

19 20 21 22 23 24
enum pkey_id_type {
	PKEY_ID_PGP,		/* OpenPGP generated key ID */
	PKEY_ID_X509,		/* X.509 arbitrary subjectKeyIdentifier */
	PKEY_ID_PKCS7,		/* Signature in PKCS#7 message */
};

25 26 27 28 29 30 31 32 33 34 35
/*
 * Module signature information block.
 *
 * The constituents of the signature section are, in order:
 *
 *	- Signer's name
 *	- Key identifier
 *	- Signature data
 *	- Information block
 */
struct module_signature {
36 37 38 39 40
	u8	algo;		/* Public-key crypto algorithm [0] */
	u8	hash;		/* Digest algorithm [0] */
	u8	id_type;	/* Key identifier type [PKEY_ID_PKCS7] */
	u8	signer_len;	/* Length of signer's name [0] */
	u8	key_id_len;	/* Length of key identifier [0] */
41 42
	u8	__pad[3];
	__be32	sig_len;	/* Length of signature data */
43 44
};

45 46 47
/*
 * Verify the signature on a module.
 */
48
int mod_verify_sig(const void *mod, unsigned long *_modlen)
49
{
50
	struct module_signature ms;
51
	size_t modlen = *_modlen, sig_len;
52

53
	pr_devel("==>%s(,%zu)\n", __func__, modlen);
54

55
	if (modlen <= sizeof(ms))
56 57
		return -EBADMSG;

58 59
	memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms));
	modlen -= sizeof(ms);
60 61

	sig_len = be32_to_cpu(ms.sig_len);
62
	if (sig_len >= modlen)
63
		return -EBADMSG;
64 65
	modlen -= sig_len;
	*_modlen = modlen;
66

67 68
	if (ms.id_type != PKEY_ID_PKCS7) {
		pr_err("Module is not signed with expected PKCS#7 message\n");
69 70 71
		return -ENOPKG;
	}

72 73 74 75 76 77 78 79 80 81
	if (ms.algo != 0 ||
	    ms.hash != 0 ||
	    ms.signer_len != 0 ||
	    ms.key_id_len != 0 ||
	    ms.__pad[0] != 0 ||
	    ms.__pad[1] != 0 ||
	    ms.__pad[2] != 0) {
		pr_err("PKCS#7 signature info has unexpected non-zero params\n");
		return -EBADMSG;
	}
82

83
	return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len,
84
				      NULL, VERIFYING_MODULE_SIGNATURE,
85
				      NULL, NULL);
86
}