xref: /aosp_15_r20/external/kmod/libkmod/libkmod-signature.c (revision cc4ad7da8cefe208cb129ac2aa9a357c7c72deb2)
1 /*
2  * libkmod - module signature display
3  *
4  * Copyright (C) 2013 Michal Marek, SUSE
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <inttypes.h>
21 #ifdef ENABLE_OPENSSL
22 #include <openssl/pkcs7.h>
23 #include <openssl/ssl.h>
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include <shared/missing.h>
30 #include <shared/util.h>
31 
32 #include "libkmod-internal.h"
33 
34 /* These types and tables were copied from the 3.7 kernel sources.
35  * As this is just description of the signature format, it should not be
36  * considered derived work (so libkmod can use the LGPL license).
37  */
38 enum pkey_algo {
39 	PKEY_ALGO_DSA,
40 	PKEY_ALGO_RSA,
41 	PKEY_ALGO__LAST
42 };
43 
44 static const char *const pkey_algo[PKEY_ALGO__LAST] = {
45 	[PKEY_ALGO_DSA]		= "DSA",
46 	[PKEY_ALGO_RSA]		= "RSA",
47 };
48 
49 enum pkey_hash_algo {
50 	PKEY_HASH_MD4,
51 	PKEY_HASH_MD5,
52 	PKEY_HASH_SHA1,
53 	PKEY_HASH_RIPE_MD_160,
54 	PKEY_HASH_SHA256,
55 	PKEY_HASH_SHA384,
56 	PKEY_HASH_SHA512,
57 	PKEY_HASH_SHA224,
58 	PKEY_HASH_SM3,
59 	PKEY_HASH__LAST
60 };
61 
62 const char *const pkey_hash_algo[PKEY_HASH__LAST] = {
63 	[PKEY_HASH_MD4]		= "md4",
64 	[PKEY_HASH_MD5]		= "md5",
65 	[PKEY_HASH_SHA1]	= "sha1",
66 	[PKEY_HASH_RIPE_MD_160]	= "rmd160",
67 	[PKEY_HASH_SHA256]	= "sha256",
68 	[PKEY_HASH_SHA384]	= "sha384",
69 	[PKEY_HASH_SHA512]	= "sha512",
70 	[PKEY_HASH_SHA224]	= "sha224",
71 	[PKEY_HASH_SM3]		= "sm3",
72 };
73 
74 enum pkey_id_type {
75 	PKEY_ID_PGP,		/* OpenPGP generated key ID */
76 	PKEY_ID_X509,		/* X.509 arbitrary subjectKeyIdentifier */
77 	PKEY_ID_PKCS7,		/* Signature in PKCS#7 message */
78 	PKEY_ID_TYPE__LAST
79 };
80 
81 const char *const pkey_id_type[PKEY_ID_TYPE__LAST] = {
82 	[PKEY_ID_PGP]		= "PGP",
83 	[PKEY_ID_X509]		= "X509",
84 	[PKEY_ID_PKCS7]		= "PKCS#7",
85 };
86 
87 /*
88  * Module signature information block.
89  */
90 struct module_signature {
91 	uint8_t algo;        /* Public-key crypto algorithm [enum pkey_algo] */
92 	uint8_t hash;        /* Digest algorithm [enum pkey_hash_algo] */
93 	uint8_t id_type;     /* Key identifier type [enum pkey_id_type] */
94 	uint8_t signer_len;  /* Length of signer's name */
95 	uint8_t key_id_len;  /* Length of key identifier */
96 	uint8_t __pad[3];
97 	uint32_t sig_len;    /* Length of signature data (big endian) */
98 };
99 
fill_default(const char * mem,off_t size,const struct module_signature * modsig,size_t sig_len,struct kmod_signature_info * sig_info)100 static bool fill_default(const char *mem, off_t size,
101 			 const struct module_signature *modsig, size_t sig_len,
102 			 struct kmod_signature_info *sig_info)
103 {
104 	size -= sig_len;
105 	sig_info->sig = mem + size;
106 	sig_info->sig_len = sig_len;
107 
108 	size -= modsig->key_id_len;
109 	sig_info->key_id = mem + size;
110 	sig_info->key_id_len = modsig->key_id_len;
111 
112 	size -= modsig->signer_len;
113 	sig_info->signer = mem + size;
114 	sig_info->signer_len = modsig->signer_len;
115 
116 	sig_info->algo = pkey_algo[modsig->algo];
117 	sig_info->hash_algo = pkey_hash_algo[modsig->hash];
118 	sig_info->id_type = pkey_id_type[modsig->id_type];
119 
120 	return true;
121 }
122 
123 #ifdef ENABLE_OPENSSL
124 
125 struct pkcs7_private {
126 	PKCS7 *pkcs7;
127 	unsigned char *key_id;
128 	BIGNUM *sno;
129 	char *hash_algo;
130 };
131 
pkcs7_free(void * s)132 static void pkcs7_free(void *s)
133 {
134 	struct kmod_signature_info *si = s;
135 	struct pkcs7_private *pvt = si->private;
136 
137 	PKCS7_free(pvt->pkcs7);
138 	BN_free(pvt->sno);
139 	free(pvt->key_id);
140 	free(pvt->hash_algo);
141 	free(pvt);
142 	si->private = NULL;
143 }
144 
x509_name_to_str(X509_NAME * name)145 static const char *x509_name_to_str(X509_NAME *name)
146 {
147 	int i;
148 	X509_NAME_ENTRY *e;
149 	ASN1_STRING *d;
150 	ASN1_OBJECT *o;
151 	int nid = -1;
152 	const char *str;
153 
154 	for (i = 0; i < X509_NAME_entry_count(name); i++) {
155 		e = X509_NAME_get_entry(name, i);
156 		o = X509_NAME_ENTRY_get_object(e);
157 		nid = OBJ_obj2nid(o);
158 		if (nid == NID_commonName)
159 			break;
160 	}
161 	if (nid == -1)
162 		return NULL;
163 
164 	d = X509_NAME_ENTRY_get_data(e);
165 	str = (const char *)ASN1_STRING_get0_data(d);
166 
167 	return str;
168 }
169 
fill_pkcs7(const char * mem,off_t size,const struct module_signature * modsig,size_t sig_len,struct kmod_signature_info * sig_info)170 static bool fill_pkcs7(const char *mem, off_t size,
171 		       const struct module_signature *modsig, size_t sig_len,
172 		       struct kmod_signature_info *sig_info)
173 {
174 	const char *pkcs7_raw;
175 	PKCS7 *pkcs7;
176 	STACK_OF(PKCS7_SIGNER_INFO) *sis;
177 	PKCS7_SIGNER_INFO *si;
178 	PKCS7_ISSUER_AND_SERIAL *is;
179 	X509_NAME *issuer;
180 	ASN1_INTEGER *sno;
181 	ASN1_OCTET_STRING *sig;
182 	BIGNUM *sno_bn;
183 	X509_ALGOR *dig_alg;
184 	X509_ALGOR *sig_alg;
185 	const ASN1_OBJECT *o;
186 	BIO *in;
187 	int len;
188 	unsigned char *key_id_str;
189 	struct pkcs7_private *pvt;
190 	const char *issuer_str;
191 	char *hash_algo;
192 	int hash_algo_len;
193 
194 	size -= sig_len;
195 	pkcs7_raw = mem + size;
196 
197 	in = BIO_new_mem_buf(pkcs7_raw, sig_len);
198 
199 	pkcs7 = d2i_PKCS7_bio(in, NULL);
200 	if (pkcs7 == NULL) {
201 		BIO_free(in);
202 		return false;
203 	}
204 
205 	BIO_free(in);
206 
207 	sis = PKCS7_get_signer_info(pkcs7);
208 	if (sis == NULL)
209 		goto err;
210 
211 	si = sk_PKCS7_SIGNER_INFO_value(sis, 0);
212 	if (si == NULL)
213 		goto err;
214 
215 	is = si->issuer_and_serial;
216 	if (is == NULL)
217 		goto err;
218 	issuer = is->issuer;
219 	sno = is->serial;
220 
221 	sig = si->enc_digest;
222 	if (sig == NULL)
223 		goto err;
224 
225 	PKCS7_SIGNER_INFO_get0_algs(si, NULL, &dig_alg, &sig_alg);
226 
227 	sig_info->sig = (const char *)ASN1_STRING_get0_data(sig);
228 	sig_info->sig_len = ASN1_STRING_length(sig);
229 
230 	sno_bn = ASN1_INTEGER_to_BN(sno, NULL);
231 	if (sno_bn == NULL)
232 		goto err;
233 
234 	len = BN_num_bytes(sno_bn);
235 	key_id_str = malloc(len);
236 	if (key_id_str == NULL)
237 		goto err2;
238 	BN_bn2bin(sno_bn, key_id_str);
239 
240 	sig_info->key_id = (const char *)key_id_str;
241 	sig_info->key_id_len = len;
242 
243 	issuer_str = x509_name_to_str(issuer);
244 	if (issuer_str != NULL) {
245 		sig_info->signer = issuer_str;
246 		sig_info->signer_len = strlen(issuer_str);
247 	}
248 
249 	X509_ALGOR_get0(&o, NULL, NULL, dig_alg);
250 
251 	// Use OBJ_obj2txt to calculate string length
252 	hash_algo_len = OBJ_obj2txt(NULL, 0, o, 0);
253 	if (hash_algo_len < 0)
254 		goto err3;
255 	hash_algo = malloc(hash_algo_len + 1);
256 	if (hash_algo == NULL)
257 		goto err3;
258 	hash_algo_len = OBJ_obj2txt(hash_algo, hash_algo_len + 1, o, 0);
259 	if (hash_algo_len < 0)
260 		goto err4;
261 
262 	// Assign libcrypto hash algo string or number
263 	sig_info->hash_algo = hash_algo;
264 
265 	sig_info->id_type = pkey_id_type[modsig->id_type];
266 
267 	pvt = malloc(sizeof(*pvt));
268 	if (pvt == NULL)
269 		goto err4;
270 
271 	pvt->pkcs7 = pkcs7;
272 	pvt->key_id = key_id_str;
273 	pvt->sno = sno_bn;
274 	pvt->hash_algo = hash_algo;
275 	sig_info->private = pvt;
276 
277 	sig_info->free = pkcs7_free;
278 
279 	return true;
280 err4:
281 	free(hash_algo);
282 err3:
283 	free(key_id_str);
284 err2:
285 	BN_free(sno_bn);
286 err:
287 	PKCS7_free(pkcs7);
288 	return false;
289 }
290 
291 #else /* ENABLE OPENSSL */
292 
fill_pkcs7(const char * mem,off_t size,const struct module_signature * modsig,size_t sig_len,struct kmod_signature_info * sig_info)293 static bool fill_pkcs7(const char *mem, off_t size,
294 		       const struct module_signature *modsig, size_t sig_len,
295 		       struct kmod_signature_info *sig_info)
296 {
297 	sig_info->hash_algo = "unknown";
298 	sig_info->id_type = pkey_id_type[modsig->id_type];
299 	return true;
300 }
301 
302 #endif /* ENABLE OPENSSL */
303 
304 #define SIG_MAGIC "~Module signature appended~\n"
305 
306 /*
307  * A signed module has the following layout:
308  *
309  * [ module                  ]
310  * [ signer's name           ]
311  * [ key identifier          ]
312  * [ signature data          ]
313  * [ struct module_signature ]
314  * [ SIG_MAGIC               ]
315  */
316 
kmod_module_signature_info(const struct kmod_file * file,struct kmod_signature_info * sig_info)317 bool kmod_module_signature_info(const struct kmod_file *file, struct kmod_signature_info *sig_info)
318 {
319 	const char *mem;
320 	off_t size;
321 	const struct module_signature *modsig;
322 	size_t sig_len;
323 
324 	size = kmod_file_get_size(file);
325 	mem = kmod_file_get_contents(file);
326 	if (size < (off_t)strlen(SIG_MAGIC))
327 		return false;
328 	size -= strlen(SIG_MAGIC);
329 	if (memcmp(SIG_MAGIC, mem + size, strlen(SIG_MAGIC)) != 0)
330 		return false;
331 
332 	if (size < (off_t)sizeof(struct module_signature))
333 		return false;
334 	size -= sizeof(struct module_signature);
335 	modsig = (struct module_signature *)(mem + size);
336 	if (modsig->algo >= PKEY_ALGO__LAST ||
337 			modsig->hash >= PKEY_HASH__LAST ||
338 			modsig->id_type >= PKEY_ID_TYPE__LAST)
339 		return false;
340 	sig_len = be32toh(get_unaligned(&modsig->sig_len));
341 	if (sig_len == 0 ||
342 	    size < (int64_t)(modsig->signer_len + modsig->key_id_len + sig_len))
343 		return false;
344 
345 	switch (modsig->id_type) {
346 	case PKEY_ID_PKCS7:
347 		return fill_pkcs7(mem, size, modsig, sig_len, sig_info);
348 	default:
349 		return fill_default(mem, size, modsig, sig_len, sig_info);
350 	}
351 }
352 
kmod_module_signature_info_free(struct kmod_signature_info * sig_info)353 void kmod_module_signature_info_free(struct kmod_signature_info *sig_info)
354 {
355 	if (sig_info->free)
356 		sig_info->free(sig_info);
357 }
358