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