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__LAST
59 };
60
61 const char *const pkey_hash_algo[PKEY_HASH__LAST] = {
62 [PKEY_HASH_MD4] = "md4",
63 [PKEY_HASH_MD5] = "md5",
64 [PKEY_HASH_SHA1] = "sha1",
65 [PKEY_HASH_RIPE_MD_160] = "rmd160",
66 [PKEY_HASH_SHA256] = "sha256",
67 [PKEY_HASH_SHA384] = "sha384",
68 [PKEY_HASH_SHA512] = "sha512",
69 [PKEY_HASH_SHA224] = "sha224",
70 };
71
72 enum pkey_id_type {
73 PKEY_ID_PGP, /* OpenPGP generated key ID */
74 PKEY_ID_X509, /* X.509 arbitrary subjectKeyIdentifier */
75 PKEY_ID_PKCS7, /* Signature in PKCS#7 message */
76 PKEY_ID_TYPE__LAST
77 };
78
79 const char *const pkey_id_type[PKEY_ID_TYPE__LAST] = {
80 [PKEY_ID_PGP] = "PGP",
81 [PKEY_ID_X509] = "X509",
82 [PKEY_ID_PKCS7] = "PKCS#7",
83 };
84
85 /*
86 * Module signature information block.
87 */
88 struct module_signature {
89 uint8_t algo; /* Public-key crypto algorithm [enum pkey_algo] */
90 uint8_t hash; /* Digest algorithm [enum pkey_hash_algo] */
91 uint8_t id_type; /* Key identifier type [enum pkey_id_type] */
92 uint8_t signer_len; /* Length of signer's name */
93 uint8_t key_id_len; /* Length of key identifier */
94 uint8_t __pad[3];
95 uint32_t sig_len; /* Length of signature data (big endian) */
96 };
97
fill_default(const char * mem,off_t size,const struct module_signature * modsig,size_t sig_len,struct kmod_signature_info * sig_info)98 static bool fill_default(const char *mem, off_t size,
99 const struct module_signature *modsig, size_t sig_len,
100 struct kmod_signature_info *sig_info)
101 {
102 size -= sig_len;
103 sig_info->sig = mem + size;
104 sig_info->sig_len = sig_len;
105
106 size -= modsig->key_id_len;
107 sig_info->key_id = mem + size;
108 sig_info->key_id_len = modsig->key_id_len;
109
110 size -= modsig->signer_len;
111 sig_info->signer = mem + size;
112 sig_info->signer_len = modsig->signer_len;
113
114 sig_info->algo = pkey_algo[modsig->algo];
115 sig_info->hash_algo = pkey_hash_algo[modsig->hash];
116 sig_info->id_type = pkey_id_type[modsig->id_type];
117
118 return true;
119 }
120
121 #ifdef ENABLE_OPENSSL
122
123 struct pkcs7_private {
124 PKCS7 *pkcs7;
125 unsigned char *key_id;
126 BIGNUM *sno;
127 };
128
pkcs7_free(void * s)129 static void pkcs7_free(void *s)
130 {
131 struct kmod_signature_info *si = s;
132 struct pkcs7_private *pvt = si->private;
133
134 PKCS7_free(pvt->pkcs7);
135 BN_free(pvt->sno);
136 free(pvt->key_id);
137 free(pvt);
138 si->private = NULL;
139 }
140
obj_to_hash_algo(const ASN1_OBJECT * o)141 static int obj_to_hash_algo(const ASN1_OBJECT *o)
142 {
143 int nid;
144
145 nid = OBJ_obj2nid(o);
146 switch (nid) {
147 case NID_md4:
148 return PKEY_HASH_MD4;
149 case NID_md5:
150 return PKEY_HASH_MD5;
151 case NID_sha1:
152 return PKEY_HASH_SHA1;
153 case NID_ripemd160:
154 return PKEY_HASH_RIPE_MD_160;
155 case NID_sha256:
156 return PKEY_HASH_SHA256;
157 case NID_sha384:
158 return PKEY_HASH_SHA384;
159 case NID_sha512:
160 return PKEY_HASH_SHA512;
161 case NID_sha224:
162 return PKEY_HASH_SHA224;
163 default:
164 return -1;
165 }
166 return -1;
167 }
168
x509_name_to_str(X509_NAME * name)169 static const char *x509_name_to_str(X509_NAME *name)
170 {
171 int i;
172 X509_NAME_ENTRY *e;
173 ASN1_STRING *d;
174 ASN1_OBJECT *o;
175 int nid = -1;
176 const char *str;
177
178 for (i = 0; i < X509_NAME_entry_count(name); i++) {
179 e = X509_NAME_get_entry(name, i);
180 o = X509_NAME_ENTRY_get_object(e);
181 nid = OBJ_obj2nid(o);
182 if (nid == NID_commonName)
183 break;
184 }
185 if (nid == -1)
186 return NULL;
187
188 d = X509_NAME_ENTRY_get_data(e);
189 str = (const char *)ASN1_STRING_get0_data(d);
190
191 return str;
192 }
193
fill_pkcs7(const char * mem,off_t size,const struct module_signature * modsig,size_t sig_len,struct kmod_signature_info * sig_info)194 static bool fill_pkcs7(const char *mem, off_t size,
195 const struct module_signature *modsig, size_t sig_len,
196 struct kmod_signature_info *sig_info)
197 {
198 const char *pkcs7_raw;
199 PKCS7 *pkcs7;
200 STACK_OF(PKCS7_SIGNER_INFO) *sis;
201 PKCS7_SIGNER_INFO *si;
202 PKCS7_ISSUER_AND_SERIAL *is;
203 X509_NAME *issuer;
204 ASN1_INTEGER *sno;
205 ASN1_OCTET_STRING *sig;
206 BIGNUM *sno_bn;
207 X509_ALGOR *dig_alg;
208 X509_ALGOR *sig_alg;
209 const ASN1_OBJECT *o;
210 BIO *in;
211 int len;
212 unsigned char *key_id_str;
213 struct pkcs7_private *pvt;
214 const char *issuer_str;
215
216 size -= sig_len;
217 pkcs7_raw = mem + size;
218
219 in = BIO_new_mem_buf(pkcs7_raw, sig_len);
220
221 pkcs7 = d2i_PKCS7_bio(in, NULL);
222 if (pkcs7 == NULL) {
223 BIO_free(in);
224 return false;
225 }
226
227 BIO_free(in);
228
229 sis = PKCS7_get_signer_info(pkcs7);
230 if (sis == NULL)
231 goto err;
232
233 si = sk_PKCS7_SIGNER_INFO_value(sis, 0);
234 if (si == NULL)
235 goto err;
236
237 is = si->issuer_and_serial;
238 if (is == NULL)
239 goto err;
240 issuer = is->issuer;
241 sno = is->serial;
242
243 sig = si->enc_digest;
244 if (sig == NULL)
245 goto err;
246
247 PKCS7_SIGNER_INFO_get0_algs(si, NULL, &dig_alg, &sig_alg);
248
249 sig_info->sig = (const char *)ASN1_STRING_get0_data(sig);
250 sig_info->sig_len = ASN1_STRING_length(sig);
251
252 sno_bn = ASN1_INTEGER_to_BN(sno, NULL);
253 if (sno_bn == NULL)
254 goto err;
255
256 len = BN_num_bytes(sno_bn);
257 key_id_str = malloc(len);
258 if (key_id_str == NULL)
259 goto err2;
260 BN_bn2bin(sno_bn, key_id_str);
261
262 sig_info->key_id = (const char *)key_id_str;
263 sig_info->key_id_len = len;
264
265 issuer_str = x509_name_to_str(issuer);
266 if (issuer_str != NULL) {
267 sig_info->signer = issuer_str;
268 sig_info->signer_len = strlen(issuer_str);
269 }
270
271 X509_ALGOR_get0(&o, NULL, NULL, dig_alg);
272
273 sig_info->hash_algo = pkey_hash_algo[obj_to_hash_algo(o)];
274 sig_info->id_type = pkey_id_type[modsig->id_type];
275
276 pvt = malloc(sizeof(*pvt));
277 if (pvt == NULL)
278 goto err3;
279
280 pvt->pkcs7 = pkcs7;
281 pvt->key_id = key_id_str;
282 pvt->sno = sno_bn;
283 sig_info->private = pvt;
284
285 sig_info->free = pkcs7_free;
286
287 return true;
288 err3:
289 free(key_id_str);
290 err2:
291 BN_free(sno_bn);
292 err:
293 PKCS7_free(pkcs7);
294 return false;
295 }
296
297 #else /* ENABLE OPENSSL */
298
fill_pkcs7(const char * mem,off_t size,const struct module_signature * modsig,size_t sig_len,struct kmod_signature_info * sig_info)299 static bool fill_pkcs7(const char *mem, off_t size,
300 const struct module_signature *modsig, size_t sig_len,
301 struct kmod_signature_info *sig_info)
302 {
303 sig_info->hash_algo = "unknown";
304 sig_info->id_type = pkey_id_type[modsig->id_type];
305 return true;
306 }
307
308 #endif /* ENABLE OPENSSL */
309
310 #define SIG_MAGIC "~Module signature appended~\n"
311
312 /*
313 * A signed module has the following layout:
314 *
315 * [ module ]
316 * [ signer's name ]
317 * [ key identifier ]
318 * [ signature data ]
319 * [ struct module_signature ]
320 * [ SIG_MAGIC ]
321 */
322
kmod_module_signature_info(const struct kmod_file * file,struct kmod_signature_info * sig_info)323 bool kmod_module_signature_info(const struct kmod_file *file, struct kmod_signature_info *sig_info)
324 {
325 const char *mem;
326 off_t size;
327 const struct module_signature *modsig;
328 size_t sig_len;
329
330 size = kmod_file_get_size(file);
331 mem = kmod_file_get_contents(file);
332 if (size < (off_t)strlen(SIG_MAGIC))
333 return false;
334 size -= strlen(SIG_MAGIC);
335 if (memcmp(SIG_MAGIC, mem + size, strlen(SIG_MAGIC)) != 0)
336 return false;
337
338 if (size < (off_t)sizeof(struct module_signature))
339 return false;
340 size -= sizeof(struct module_signature);
341 modsig = (struct module_signature *)(mem + size);
342 if (modsig->algo >= PKEY_ALGO__LAST ||
343 modsig->hash >= PKEY_HASH__LAST ||
344 modsig->id_type >= PKEY_ID_TYPE__LAST)
345 return false;
346 sig_len = be32toh(get_unaligned(&modsig->sig_len));
347 if (sig_len == 0 ||
348 size < (int64_t)(modsig->signer_len + modsig->key_id_len + sig_len))
349 return false;
350
351 switch (modsig->id_type) {
352 case PKEY_ID_PKCS7:
353 return fill_pkcs7(mem, size, modsig, sig_len, sig_info);
354 default:
355 return fill_default(mem, size, modsig, sig_len, sig_info);
356 }
357 }
358
kmod_module_signature_info_free(struct kmod_signature_info * sig_info)359 void kmod_module_signature_info_free(struct kmod_signature_info *sig_info)
360 {
361 if (sig_info->free)
362 sig_info->free(sig_info);
363 }
364