Merge tag 'mvebu-fixes-4.3-2' of git://git.infradead.org/linux-mvebu into fixes
[linux-drm-fsl-dcu.git] / crypto / asymmetric_keys / pkcs7_verify.c
1 /* Verify the signature on a PKCS#7 message.
2  *
3  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11
12 #define pr_fmt(fmt) "PKCS7: "fmt
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/asn1.h>
18 #include <crypto/hash.h>
19 #include "public_key.h"
20 #include "pkcs7_parser.h"
21
22 /*
23  * Digest the relevant parts of the PKCS#7 data
24  */
25 static int pkcs7_digest(struct pkcs7_message *pkcs7,
26                         struct pkcs7_signed_info *sinfo)
27 {
28         struct crypto_shash *tfm;
29         struct shash_desc *desc;
30         size_t digest_size, desc_size;
31         void *digest;
32         int ret;
33
34         kenter(",%u,%u", sinfo->index, sinfo->sig.pkey_hash_algo);
35
36         if (sinfo->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
37             !hash_algo_name[sinfo->sig.pkey_hash_algo])
38                 return -ENOPKG;
39
40         /* Allocate the hashing algorithm we're going to need and find out how
41          * big the hash operational data will be.
42          */
43         tfm = crypto_alloc_shash(hash_algo_name[sinfo->sig.pkey_hash_algo],
44                                  0, 0);
45         if (IS_ERR(tfm))
46                 return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
47
48         desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
49         sinfo->sig.digest_size = digest_size = crypto_shash_digestsize(tfm);
50
51         ret = -ENOMEM;
52         digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
53         if (!digest)
54                 goto error_no_desc;
55
56         desc = digest + digest_size;
57         desc->tfm   = tfm;
58         desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
59
60         /* Digest the message [RFC2315 9.3] */
61         ret = crypto_shash_init(desc);
62         if (ret < 0)
63                 goto error;
64         ret = crypto_shash_finup(desc, pkcs7->data, pkcs7->data_len, digest);
65         if (ret < 0)
66                 goto error;
67         pr_devel("MsgDigest = [%*ph]\n", 8, digest);
68
69         /* However, if there are authenticated attributes, there must be a
70          * message digest attribute amongst them which corresponds to the
71          * digest we just calculated.
72          */
73         if (sinfo->authattrs) {
74                 u8 tag;
75
76                 if (!sinfo->msgdigest) {
77                         pr_warn("Sig %u: No messageDigest\n", sinfo->index);
78                         ret = -EKEYREJECTED;
79                         goto error;
80                 }
81
82                 if (sinfo->msgdigest_len != sinfo->sig.digest_size) {
83                         pr_debug("Sig %u: Invalid digest size (%u)\n",
84                                  sinfo->index, sinfo->msgdigest_len);
85                         ret = -EBADMSG;
86                         goto error;
87                 }
88
89                 if (memcmp(digest, sinfo->msgdigest, sinfo->msgdigest_len) != 0) {
90                         pr_debug("Sig %u: Message digest doesn't match\n",
91                                  sinfo->index);
92                         ret = -EKEYREJECTED;
93                         goto error;
94                 }
95
96                 /* We then calculate anew, using the authenticated attributes
97                  * as the contents of the digest instead.  Note that we need to
98                  * convert the attributes from a CONT.0 into a SET before we
99                  * hash it.
100                  */
101                 memset(digest, 0, sinfo->sig.digest_size);
102
103                 ret = crypto_shash_init(desc);
104                 if (ret < 0)
105                         goto error;
106                 tag = ASN1_CONS_BIT | ASN1_SET;
107                 ret = crypto_shash_update(desc, &tag, 1);
108                 if (ret < 0)
109                         goto error;
110                 ret = crypto_shash_finup(desc, sinfo->authattrs,
111                                          sinfo->authattrs_len, digest);
112                 if (ret < 0)
113                         goto error;
114                 pr_devel("AADigest = [%*ph]\n", 8, digest);
115         }
116
117         sinfo->sig.digest = digest;
118         digest = NULL;
119
120 error:
121         kfree(digest);
122 error_no_desc:
123         crypto_free_shash(tfm);
124         kleave(" = %d", ret);
125         return ret;
126 }
127
128 /*
129  * Find the key (X.509 certificate) to use to verify a PKCS#7 message.  PKCS#7
130  * uses the issuer's name and the issuing certificate serial number for
131  * matching purposes.  These must match the certificate issuer's name (not
132  * subject's name) and the certificate serial number [RFC 2315 6.7].
133  */
134 static int pkcs7_find_key(struct pkcs7_message *pkcs7,
135                           struct pkcs7_signed_info *sinfo)
136 {
137         struct x509_certificate *x509;
138         unsigned certix = 1;
139
140         kenter("%u", sinfo->index);
141
142         for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {
143                 /* I'm _assuming_ that the generator of the PKCS#7 message will
144                  * encode the fields from the X.509 cert in the same way in the
145                  * PKCS#7 message - but I can't be 100% sure of that.  It's
146                  * possible this will need element-by-element comparison.
147                  */
148                 if (!asymmetric_key_id_same(x509->id, sinfo->signing_cert_id))
149                         continue;
150                 pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
151                          sinfo->index, certix);
152
153                 if (x509->pub->pkey_algo != sinfo->sig.pkey_algo) {
154                         pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",
155                                 sinfo->index);
156                         continue;
157                 }
158
159                 sinfo->signer = x509;
160                 return 0;
161         }
162
163         /* The relevant X.509 cert isn't found here, but it might be found in
164          * the trust keyring.
165          */
166         pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
167                  sinfo->index,
168                  sinfo->signing_cert_id->len, sinfo->signing_cert_id->data);
169         return 0;
170 }
171
172 /*
173  * Verify the internal certificate chain as best we can.
174  */
175 static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
176                                   struct pkcs7_signed_info *sinfo)
177 {
178         struct x509_certificate *x509 = sinfo->signer, *p;
179         struct asymmetric_key_id *auth;
180         int ret;
181
182         kenter("");
183
184         for (p = pkcs7->certs; p; p = p->next)
185                 p->seen = false;
186
187         for (;;) {
188                 pr_debug("verify %s: %*phN\n",
189                          x509->subject,
190                          x509->raw_serial_size, x509->raw_serial);
191                 x509->seen = true;
192                 ret = x509_get_sig_params(x509);
193                 if (ret < 0)
194                         goto maybe_missing_crypto_in_x509;
195
196                 pr_debug("- issuer %s\n", x509->issuer);
197                 if (x509->akid_id)
198                         pr_debug("- authkeyid.id %*phN\n",
199                                  x509->akid_id->len, x509->akid_id->data);
200                 if (x509->akid_skid)
201                         pr_debug("- authkeyid.skid %*phN\n",
202                                  x509->akid_skid->len, x509->akid_skid->data);
203
204                 if ((!x509->akid_id && !x509->akid_skid) ||
205                     strcmp(x509->subject, x509->issuer) == 0) {
206                         /* If there's no authority certificate specified, then
207                          * the certificate must be self-signed and is the root
208                          * of the chain.  Likewise if the cert is its own
209                          * authority.
210                          */
211                         pr_debug("- no auth?\n");
212                         if (x509->raw_subject_size != x509->raw_issuer_size ||
213                             memcmp(x509->raw_subject, x509->raw_issuer,
214                                    x509->raw_issuer_size) != 0)
215                                 return 0;
216
217                         ret = x509_check_signature(x509->pub, x509);
218                         if (ret < 0)
219                                 goto maybe_missing_crypto_in_x509;
220                         x509->signer = x509;
221                         pr_debug("- self-signed\n");
222                         return 0;
223                 }
224
225                 /* Look through the X.509 certificates in the PKCS#7 message's
226                  * list to see if the next one is there.
227                  */
228                 auth = x509->akid_id;
229                 if (auth) {
230                         pr_debug("- want %*phN\n", auth->len, auth->data);
231                         for (p = pkcs7->certs; p; p = p->next) {
232                                 pr_debug("- cmp [%u] %*phN\n",
233                                          p->index, p->id->len, p->id->data);
234                                 if (asymmetric_key_id_same(p->id, auth))
235                                         goto found_issuer_check_skid;
236                         }
237                 } else {
238                         auth = x509->akid_skid;
239                         pr_debug("- want %*phN\n", auth->len, auth->data);
240                         for (p = pkcs7->certs; p; p = p->next) {
241                                 if (!p->skid)
242                                         continue;
243                                 pr_debug("- cmp [%u] %*phN\n",
244                                          p->index, p->skid->len, p->skid->data);
245                                 if (asymmetric_key_id_same(p->skid, auth))
246                                         goto found_issuer;
247                         }
248                 }
249
250                 /* We didn't find the root of this chain */
251                 pr_debug("- top\n");
252                 return 0;
253
254         found_issuer_check_skid:
255                 /* We matched issuer + serialNumber, but if there's an
256                  * authKeyId.keyId, that must match the CA subjKeyId also.
257                  */
258                 if (x509->akid_skid &&
259                     !asymmetric_key_id_same(p->skid, x509->akid_skid)) {
260                         pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
261                                 sinfo->index, x509->index, p->index);
262                         return -EKEYREJECTED;
263                 }
264         found_issuer:
265                 pr_debug("- subject %s\n", p->subject);
266                 if (p->seen) {
267                         pr_warn("Sig %u: X.509 chain contains loop\n",
268                                 sinfo->index);
269                         return 0;
270                 }
271                 ret = x509_check_signature(p->pub, x509);
272                 if (ret < 0)
273                         return ret;
274                 x509->signer = p;
275                 if (x509 == p) {
276                         pr_debug("- self-signed\n");
277                         return 0;
278                 }
279                 x509 = p;
280                 might_sleep();
281         }
282
283 maybe_missing_crypto_in_x509:
284         /* Just prune the certificate chain at this point if we lack some
285          * crypto module to go further.  Note, however, we don't want to set
286          * sinfo->missing_crypto as the signed info block may still be
287          * validatable against an X.509 cert lower in the chain that we have a
288          * trusted copy of.
289          */
290         if (ret == -ENOPKG)
291                 return 0;
292         return ret;
293 }
294
295 /*
296  * Verify one signed information block from a PKCS#7 message.
297  */
298 static int pkcs7_verify_one(struct pkcs7_message *pkcs7,
299                             struct pkcs7_signed_info *sinfo)
300 {
301         int ret;
302
303         kenter(",%u", sinfo->index);
304
305         /* First of all, digest the data in the PKCS#7 message and the
306          * signed information block
307          */
308         ret = pkcs7_digest(pkcs7, sinfo);
309         if (ret < 0)
310                 return ret;
311
312         /* Find the key for the signature if there is one */
313         ret = pkcs7_find_key(pkcs7, sinfo);
314         if (ret < 0)
315                 return ret;
316
317         if (!sinfo->signer)
318                 return 0;
319
320         pr_devel("Using X.509[%u] for sig %u\n",
321                  sinfo->signer->index, sinfo->index);
322
323         /* Check that the PKCS#7 signing time is valid according to the X.509
324          * certificate.  We can't, however, check against the system clock
325          * since that may not have been set yet and may be wrong.
326          */
327         if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) {
328                 if (sinfo->signing_time < sinfo->signer->valid_from ||
329                     sinfo->signing_time > sinfo->signer->valid_to) {
330                         pr_warn("Message signed outside of X.509 validity window\n");
331                         return -EKEYREJECTED;
332                 }
333         }
334
335         /* Verify the PKCS#7 binary against the key */
336         ret = public_key_verify_signature(sinfo->signer->pub, &sinfo->sig);
337         if (ret < 0)
338                 return ret;
339
340         pr_devel("Verified signature %u\n", sinfo->index);
341
342         /* Verify the internal certificate chain */
343         return pkcs7_verify_sig_chain(pkcs7, sinfo);
344 }
345
346 /**
347  * pkcs7_verify - Verify a PKCS#7 message
348  * @pkcs7: The PKCS#7 message to be verified
349  * @usage: The use to which the key is being put
350  *
351  * Verify a PKCS#7 message is internally consistent - that is, the data digest
352  * matches the digest in the AuthAttrs and any signature in the message or one
353  * of the X.509 certificates it carries that matches another X.509 cert in the
354  * message can be verified.
355  *
356  * This does not look to match the contents of the PKCS#7 message against any
357  * external public keys.
358  *
359  * Returns, in order of descending priority:
360  *
361  *  (*) -EKEYREJECTED if a key was selected that had a usage restriction at
362  *      odds with the specified usage, or:
363  *
364  *  (*) -EKEYREJECTED if a signature failed to match for which we found an
365  *      appropriate X.509 certificate, or:
366  *
367  *  (*) -EBADMSG if some part of the message was invalid, or:
368  *
369  *  (*) -ENOPKG if none of the signature chains are verifiable because suitable
370  *      crypto modules couldn't be found, or:
371  *
372  *  (*) 0 if all the signature chains that don't incur -ENOPKG can be verified
373  *      (note that a signature chain may be of zero length), or:
374  */
375 int pkcs7_verify(struct pkcs7_message *pkcs7,
376                  enum key_being_used_for usage)
377 {
378         struct pkcs7_signed_info *sinfo;
379         struct x509_certificate *x509;
380         int enopkg = -ENOPKG;
381         int ret, n;
382
383         kenter("");
384
385         switch (usage) {
386         case VERIFYING_MODULE_SIGNATURE:
387                 if (pkcs7->data_type != OID_data) {
388                         pr_warn("Invalid module sig (not pkcs7-data)\n");
389                         return -EKEYREJECTED;
390                 }
391                 if (pkcs7->have_authattrs) {
392                         pr_warn("Invalid module sig (has authattrs)\n");
393                         return -EKEYREJECTED;
394                 }
395                 break;
396         case VERIFYING_FIRMWARE_SIGNATURE:
397                 if (pkcs7->data_type != OID_data) {
398                         pr_warn("Invalid firmware sig (not pkcs7-data)\n");
399                         return -EKEYREJECTED;
400                 }
401                 if (!pkcs7->have_authattrs) {
402                         pr_warn("Invalid firmware sig (missing authattrs)\n");
403                         return -EKEYREJECTED;
404                 }
405                 break;
406         case VERIFYING_KEXEC_PE_SIGNATURE:
407                 if (pkcs7->data_type != OID_msIndirectData) {
408                         pr_warn("Invalid kexec sig (not Authenticode)\n");
409                         return -EKEYREJECTED;
410                 }
411                 /* Authattr presence checked in parser */
412                 break;
413         case VERIFYING_UNSPECIFIED_SIGNATURE:
414                 if (pkcs7->data_type != OID_data) {
415                         pr_warn("Invalid unspecified sig (not pkcs7-data)\n");
416                         return -EKEYREJECTED;
417                 }
418                 break;
419         default:
420                 return -EINVAL;
421         }
422
423         for (n = 0, x509 = pkcs7->certs; x509; x509 = x509->next, n++) {
424                 ret = x509_get_sig_params(x509);
425                 if (ret < 0)
426                         return ret;
427         }
428
429         for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
430                 ret = pkcs7_verify_one(pkcs7, sinfo);
431                 if (ret < 0) {
432                         if (ret == -ENOPKG) {
433                                 sinfo->unsupported_crypto = true;
434                                 continue;
435                         }
436                         kleave(" = %d", ret);
437                         return ret;
438                 }
439                 enopkg = 0;
440         }
441
442         kleave(" = %d", enopkg);
443         return enopkg;
444 }
445 EXPORT_SYMBOL_GPL(pkcs7_verify);
446
447 /**
448  * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
449  * @pkcs7: The PKCS#7 message
450  * @data: The data to be verified
451  * @datalen: The amount of data
452  *
453  * Supply the detached data needed to verify a PKCS#7 message.  Note that no
454  * attempt to retain/pin the data is made.  That is left to the caller.  The
455  * data will not be modified by pkcs7_verify() and will not be freed when the
456  * PKCS#7 message is freed.
457  *
458  * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
459  */
460 int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
461                                const void *data, size_t datalen)
462 {
463         if (pkcs7->data) {
464                 pr_debug("Data already supplied\n");
465                 return -EINVAL;
466         }
467         pkcs7->data = data;
468         pkcs7->data_len = datalen;
469         return 0;
470 }