lib/digsig: additional sanity checks against badly formated key payload
[linux-flexiantxendom0-3.2.10.git] / lib / digsig.c
1 /*
2  * Copyright (C) 2011 Nokia Corporation
3  * Copyright (C) 2011 Intel Corporation
4  *
5  * Author:
6  * Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
7  *                 <dmitry.kasatkin@intel.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, version 2 of the License.
12  *
13  * File: sign.c
14  *      implements signature (RSA) verification
15  *      pkcs decoding is based on LibTomCrypt code
16  */
17
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <linux/err.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/key.h>
24 #include <linux/crypto.h>
25 #include <crypto/hash.h>
26 #include <crypto/sha.h>
27 #include <keys/user-type.h>
28 #include <linux/mpi.h>
29 #include <linux/digsig.h>
30
31 static struct crypto_shash *shash;
32
33 static int pkcs_1_v1_5_decode_emsa(const unsigned char *msg,
34                         unsigned long  msglen,
35                         unsigned long  modulus_bitlen,
36                         unsigned char *out,
37                         unsigned long *outlen,
38                         int *is_valid)
39 {
40         unsigned long modulus_len, ps_len, i;
41         int result;
42
43         /* default to invalid packet */
44         *is_valid = 0;
45
46         modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
47
48         /* test message size */
49         if ((msglen > modulus_len) || (modulus_len < 11))
50                 return -EINVAL;
51
52         /* separate encoded message */
53         if ((msg[0] != 0x00) || (msg[1] != (unsigned char)1)) {
54                 result = -EINVAL;
55                 goto bail;
56         }
57
58         for (i = 2; i < modulus_len - 1; i++)
59                 if (msg[i] != 0xFF)
60                         break;
61
62         /* separator check */
63         if (msg[i] != 0) {
64                 /* There was no octet with hexadecimal value 0x00
65                 to separate ps from m. */
66                 result = -EINVAL;
67                 goto bail;
68         }
69
70         ps_len = i - 2;
71
72         if (*outlen < (msglen - (2 + ps_len + 1))) {
73                 *outlen = msglen - (2 + ps_len + 1);
74                 result = -EOVERFLOW;
75                 goto bail;
76         }
77
78         *outlen = (msglen - (2 + ps_len + 1));
79         memcpy(out, &msg[2 + ps_len + 1], *outlen);
80
81         /* valid packet */
82         *is_valid = 1;
83         result    = 0;
84 bail:
85         return result;
86 }
87
88 /*
89  * RSA Signature verification with public key
90  */
91 static int digsig_verify_rsa(struct key *key,
92                     const char *sig, int siglen,
93                        const char *h, int hlen)
94 {
95         int err = -EINVAL;
96         unsigned long len;
97         unsigned long mlen, mblen;
98         unsigned nret, l;
99         int valid, head, i;
100         unsigned char *out1 = NULL, *out2 = NULL;
101         MPI in = NULL, res = NULL, pkey[2];
102         uint8_t *p, *datap, *endp;
103         struct user_key_payload *ukp;
104         struct pubkey_hdr *pkh;
105
106         down_read(&key->sem);
107         ukp = key->payload.data;
108
109         if (ukp->datalen < sizeof(*pkh))
110                 goto err1;
111
112         pkh = (struct pubkey_hdr *)ukp->data;
113
114         if (pkh->version != 1)
115                 goto err1;
116
117         if (pkh->algo != PUBKEY_ALGO_RSA)
118                 goto err1;
119
120         if (pkh->nmpi != 2)
121                 goto err1;
122
123         datap = pkh->mpi;
124         endp = ukp->data + ukp->datalen;
125
126         for (i = 0; i < pkh->nmpi; i++) {
127                 unsigned int remaining = endp - datap;
128                 pkey[i] = mpi_read_from_buffer(datap, &remaining);
129                 datap += remaining;
130         }
131
132         mblen = mpi_get_nbits(pkey[0]);
133         mlen = (mblen + 7)/8;
134
135         if (mlen == 0)
136                 goto err;
137
138         out1 = kzalloc(mlen, GFP_KERNEL);
139         if (!out1)
140                 goto err;
141
142         out2 = kzalloc(mlen, GFP_KERNEL);
143         if (!out2)
144                 goto err;
145
146         nret = siglen;
147         in = mpi_read_from_buffer(sig, &nret);
148         if (!in)
149                 goto err;
150
151         res = mpi_alloc(mpi_get_nlimbs(in) * 2);
152         if (!res)
153                 goto err;
154
155         err = mpi_powm(res, in, pkey[1], pkey[0]);
156         if (err)
157                 goto err;
158
159         if (mpi_get_nlimbs(res) * BYTES_PER_MPI_LIMB > mlen) {
160                 err = -EINVAL;
161                 goto err;
162         }
163
164         p = mpi_get_buffer(res, &l, NULL);
165         if (!p) {
166                 err = -EINVAL;
167                 goto err;
168         }
169
170         len = mlen;
171         head = len - l;
172         memset(out1, 0, head);
173         memcpy(out1 + head, p, l);
174
175         err = -EINVAL;
176         pkcs_1_v1_5_decode_emsa(out1, len, mblen, out2, &len, &valid);
177
178         if (valid && len == hlen)
179                 err = memcmp(out2, h, hlen);
180
181 err:
182         mpi_free(in);
183         mpi_free(res);
184         kfree(out1);
185         kfree(out2);
186         mpi_free(pkey[0]);
187         mpi_free(pkey[1]);
188 err1:
189         up_read(&key->sem);
190
191         return err;
192 }
193
194 /**
195  * digsig_verify() - digital signature verification with public key
196  * @keyring:    keyring to search key in
197  * @sig:        digital signature
198  * @sigen:      length of the signature
199  * @data:       data
200  * @datalen:    length of the data
201  * @return:     0 on success, -EINVAL otherwise
202  *
203  * Verifies data integrity against digital signature.
204  * Currently only RSA is supported.
205  * Normally hash of the content is used as a data for this function.
206  *
207  */
208 int digsig_verify(struct key *keyring, const char *sig, int siglen,
209                                                 const char *data, int datalen)
210 {
211         int err = -ENOMEM;
212         struct signature_hdr *sh = (struct signature_hdr *)sig;
213         struct shash_desc *desc = NULL;
214         unsigned char hash[SHA1_DIGEST_SIZE];
215         struct key *key;
216         char name[20];
217
218         if (siglen < sizeof(*sh) + 2)
219                 return -EINVAL;
220
221         if (sh->algo != PUBKEY_ALGO_RSA)
222                 return -ENOTSUPP;
223
224         sprintf(name, "%llX", __be64_to_cpup((uint64_t *)sh->keyid));
225
226         if (keyring) {
227                 /* search in specific keyring */
228                 key_ref_t kref;
229                 kref = keyring_search(make_key_ref(keyring, 1UL),
230                                                 &key_type_user, name);
231                 if (IS_ERR(kref))
232                         key = ERR_PTR(PTR_ERR(kref));
233                 else
234                         key = key_ref_to_ptr(kref);
235         } else {
236                 key = request_key(&key_type_user, name, NULL);
237         }
238         if (IS_ERR(key)) {
239                 pr_err("key not found, id: %s\n", name);
240                 return PTR_ERR(key);
241         }
242
243         desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(shash),
244                        GFP_KERNEL);
245         if (!desc)
246                 goto err;
247
248         desc->tfm = shash;
249         desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
250
251         crypto_shash_init(desc);
252         crypto_shash_update(desc, data, datalen);
253         crypto_shash_update(desc, sig, sizeof(*sh));
254         crypto_shash_final(desc, hash);
255
256         kfree(desc);
257
258         /* pass signature mpis address */
259         err = digsig_verify_rsa(key, sig + sizeof(*sh), siglen - sizeof(*sh),
260                              hash, sizeof(hash));
261
262 err:
263         key_put(key);
264
265         return err ? -EINVAL : 0;
266 }
267 EXPORT_SYMBOL_GPL(digsig_verify);
268
269 static int __init digsig_init(void)
270 {
271         shash = crypto_alloc_shash("sha1", 0, 0);
272         if (IS_ERR(shash)) {
273                 pr_err("shash allocation failed\n");
274                 return  PTR_ERR(shash);
275         }
276
277         return 0;
278
279 }
280
281 static void __exit digsig_cleanup(void)
282 {
283         crypto_free_shash(shash);
284 }
285
286 module_init(digsig_init);
287 module_exit(digsig_cleanup);
288
289 MODULE_LICENSE("GPL");