summaryrefslogtreecommitdiff
path: root/libkmod/libkmod-signature.c
blob: 4e8748c26c1c8b1b440b9fa48b1420ffa452a5fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * libkmod - module signature display
 *
 * Copyright (C) 2013 Michal Marek, SUSE
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include <endian.h>
#include <inttypes.h>
#ifdef ENABLE_OPENSSL
#include <openssl/pkcs7.h>
#include <openssl/ssl.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <shared/missing.h>
#include <shared/util.h>

#include "libkmod-internal.h"

/* These types and tables were copied from the 3.7 kernel sources.
 * As this is just description of the signature format, it should not be
 * considered derived work (so libkmod can use the LGPL license).
 */
enum pkey_algo {
	PKEY_ALGO_DSA,
	PKEY_ALGO_RSA,
	PKEY_ALGO__LAST
};

static const char *const pkey_algo[PKEY_ALGO__LAST] = {
	[PKEY_ALGO_DSA]		= "DSA",
	[PKEY_ALGO_RSA]		= "RSA",
};

enum pkey_hash_algo {
	PKEY_HASH_MD4,
	PKEY_HASH_MD5,
	PKEY_HASH_SHA1,
	PKEY_HASH_RIPE_MD_160,
	PKEY_HASH_SHA256,
	PKEY_HASH_SHA384,
	PKEY_HASH_SHA512,
	PKEY_HASH_SHA224,
	PKEY_HASH__LAST
};

const char *const pkey_hash_algo[PKEY_HASH__LAST] = {
	[PKEY_HASH_MD4]		= "md4",
	[PKEY_HASH_MD5]		= "md5",
	[PKEY_HASH_SHA1]	= "sha1",
	[PKEY_HASH_RIPE_MD_160]	= "rmd160",
	[PKEY_HASH_SHA256]	= "sha256",
	[PKEY_HASH_SHA384]	= "sha384",
	[PKEY_HASH_SHA512]	= "sha512",
	[PKEY_HASH_SHA224]	= "sha224",
};

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 */
	PKEY_ID_TYPE__LAST
};

const char *const pkey_id_type[PKEY_ID_TYPE__LAST] = {
	[PKEY_ID_PGP]		= "PGP",
	[PKEY_ID_X509]		= "X509",
	[PKEY_ID_PKCS7]		= "PKCS#7",
};

/*
 * Module signature information block.
 */
struct module_signature {
	uint8_t algo;        /* Public-key crypto algorithm [enum pkey_algo] */
	uint8_t hash;        /* Digest algorithm [enum pkey_hash_algo] */
	uint8_t id_type;     /* Key identifier type [enum pkey_id_type] */
	uint8_t signer_len;  /* Length of signer's name */
	uint8_t key_id_len;  /* Length of key identifier */
	uint8_t __pad[3];
	uint32_t sig_len;    /* Length of signature data (big endian) */
};

static bool fill_default(const char *mem, off_t size,
			 const struct module_signature *modsig, size_t sig_len,
			 struct kmod_signature_info *sig_info)
{
	size -= sig_len;
	sig_info->sig = mem + size;
	sig_info->sig_len = sig_len;

	size -= modsig->key_id_len;
	sig_info->key_id = mem + size;
	sig_info->key_id_len = modsig->key_id_len;

	size -= modsig->signer_len;
	sig_info->signer = mem + size;
	sig_info->signer_len = modsig->signer_len;

	sig_info->algo = pkey_algo[modsig->algo];
	sig_info->hash_algo = pkey_hash_algo[modsig->hash];
	sig_info->id_type = pkey_id_type[modsig->id_type];

	return true;
}

#ifdef ENABLE_OPENSSL

struct pkcs7_private {
	PKCS7 *pkcs7;
	unsigned char *key_id;
	BIGNUM *sno;
};

static void pkcs7_free(void *s)
{
	struct kmod_signature_info *si = s;
	struct pkcs7_private *pvt = si->private;

	PKCS7_free(pvt->pkcs7);
	BN_free(pvt->sno);
	free(pvt->key_id);
	free(pvt);
	si->private = NULL;
}

static int obj_to_hash_algo(const ASN1_OBJECT *o)
{
	int nid;

	nid = OBJ_obj2nid(o);
	switch (nid) {
	case NID_md4:
		return PKEY_HASH_MD4;
	case NID_md5:
		return PKEY_HASH_MD5;
	case NID_sha1:
		return PKEY_HASH_SHA1;
	case NID_ripemd160:
		return PKEY_HASH_RIPE_MD_160;
	case NID_sha256:
		return PKEY_HASH_SHA256;
	case NID_sha384:
		return PKEY_HASH_SHA384;
	case NID_sha512:
		return PKEY_HASH_SHA512;
	case NID_sha224:
		return PKEY_HASH_SHA224;
	default:
		return -1;
	}
	return -1;
}

static const char *x509_name_to_str(X509_NAME *name)
{
	int i;
	X509_NAME_ENTRY *e;
	ASN1_STRING *d;
	ASN1_OBJECT *o;
	int nid = -1;
	const char *str;

	for (i = 0; i < X509_NAME_entry_count(name); i++) {
		e = X509_NAME_get_entry(name, i);
		o = X509_NAME_ENTRY_get_object(e);
		nid = OBJ_obj2nid(o);
		if (nid == NID_commonName)
			break;
	}
	if (nid == -1)
		return NULL;

	d = X509_NAME_ENTRY_get_data(e);
	str = (const char *)ASN1_STRING_get0_data(d);

	return str;
}

static bool fill_pkcs7(const char *mem, off_t size,
		       const struct module_signature *modsig, size_t sig_len,
		       struct kmod_signature_info *sig_info)
{
	const char *pkcs7_raw;
	PKCS7 *pkcs7;
	STACK_OF(PKCS7_SIGNER_INFO) *sis;
	PKCS7_SIGNER_INFO *si;
	PKCS7_ISSUER_AND_SERIAL *is;
	X509_NAME *issuer;
	ASN1_INTEGER *sno;
	ASN1_OCTET_STRING *sig;
	BIGNUM *sno_bn;
	X509_ALGOR *dig_alg;
	X509_ALGOR *sig_alg;
	const ASN1_OBJECT *o;
	BIO *in;
	int len;
	unsigned char *key_id_str;
	struct pkcs7_private *pvt;
	const char *issuer_str;

	size -= sig_len;
	pkcs7_raw = mem + size;

	in = BIO_new_mem_buf(pkcs7_raw, sig_len);

	pkcs7 = d2i_PKCS7_bio(in, NULL);
	if (pkcs7 == NULL) {
		BIO_free(in);
		return false;
	}

	BIO_free(in);

	sis = PKCS7_get_signer_info(pkcs7);
	if (sis == NULL)
		goto err;

	si = sk_PKCS7_SIGNER_INFO_value(sis, 0);
	if (si == NULL)
		goto err;

	is = si->issuer_and_serial;
	if (is == NULL)
		goto err;
	issuer = is->issuer;
	sno = is->serial;

	sig = si->enc_digest;
	if (sig == NULL)
		goto err;

	PKCS7_SIGNER_INFO_get0_algs(si, NULL, &dig_alg, &sig_alg);

	sig_info->sig = (const char *)ASN1_STRING_get0_data(sig);
	sig_info->sig_len = ASN1_STRING_length(sig);

	sno_bn = ASN1_INTEGER_to_BN(sno, NULL);
	if (sno_bn == NULL)
		goto err;

	len = BN_num_bytes(sno_bn);
	key_id_str = malloc(len);
	if (key_id_str == NULL)
		goto err2;
	BN_bn2bin(sno_bn, key_id_str);

	sig_info->key_id = (const char *)key_id_str;
	sig_info->key_id_len = len;

	issuer_str = x509_name_to_str(issuer);
	if (issuer_str != NULL) {
		sig_info->signer = issuer_str;
		sig_info->signer_len = strlen(issuer_str);
	}

	X509_ALGOR_get0(&o, NULL, NULL, dig_alg);

	sig_info->hash_algo = pkey_hash_algo[obj_to_hash_algo(o)];
	sig_info->id_type = pkey_id_type[modsig->id_type];

	pvt = malloc(sizeof(*pvt));
	if (pvt == NULL)
		goto err3;

	pvt->pkcs7 = pkcs7;
	pvt->key_id = key_id_str;
	pvt->sno = sno_bn;
	sig_info->private = pvt;

	sig_info->free = pkcs7_free;

	return true;
err3:
	free(key_id_str);
err2:
	BN_free(sno_bn);
err:
	PKCS7_free(pkcs7);
	return false;
}

#else /* ENABLE OPENSSL */

static bool fill_pkcs7(const char *mem, off_t size,
		       const struct module_signature *modsig, size_t sig_len,
		       struct kmod_signature_info *sig_info)
{
	sig_info->hash_algo = "unknown";
	sig_info->id_type = pkey_id_type[modsig->id_type];
	return true;
}

#endif /* ENABLE OPENSSL */

#define SIG_MAGIC "~Module signature appended~\n"

/*
 * A signed module has the following layout:
 *
 * [ module                  ]
 * [ signer's name           ]
 * [ key identifier          ]
 * [ signature data          ]
 * [ struct module_signature ]
 * [ SIG_MAGIC               ]
 */

bool kmod_module_signature_info(const struct kmod_file *file, struct kmod_signature_info *sig_info)
{
	const char *mem;
	off_t size;
	const struct module_signature *modsig;
	size_t sig_len;

	size = kmod_file_get_size(file);
	mem = kmod_file_get_contents(file);
	if (size < (off_t)strlen(SIG_MAGIC))
		return false;
	size -= strlen(SIG_MAGIC);
	if (memcmp(SIG_MAGIC, mem + size, strlen(SIG_MAGIC)) != 0)
		return false;

	if (size < (off_t)sizeof(struct module_signature))
		return false;
	size -= sizeof(struct module_signature);
	modsig = (struct module_signature *)(mem + size);
	if (modsig->algo >= PKEY_ALGO__LAST ||
			modsig->hash >= PKEY_HASH__LAST ||
			modsig->id_type >= PKEY_ID_TYPE__LAST)
		return false;
	sig_len = be32toh(get_unaligned(&modsig->sig_len));
	if (sig_len == 0 ||
	    size < (int64_t)(modsig->signer_len + modsig->key_id_len + sig_len))
		return false;

	switch (modsig->id_type) {
	case PKEY_ID_PKCS7:
		return fill_pkcs7(mem, size, modsig, sig_len, sig_info);
	default:
		return fill_default(mem, size, modsig, sig_len, sig_info);
	}
}

void kmod_module_signature_info_free(struct kmod_signature_info *sig_info)
{
	if (sig_info->free)
		sig_info->free(sig_info);
}