blob: 404f89a0f85252d5d2f3606382f6b16a8c7fc5b1 [file] [log] [blame]
David Howells9f0d3312014-07-01 16:40:19 +01001/* 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 */
25static 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->msgdigest) {
74 u8 tag;
75
76 if (sinfo->msgdigest_len != sinfo->sig.digest_size) {
77 pr_debug("Sig %u: Invalid digest size (%u)\n",
78 sinfo->index, sinfo->msgdigest_len);
79 ret = -EBADMSG;
80 goto error;
81 }
82
83 if (memcmp(digest, sinfo->msgdigest, sinfo->msgdigest_len) != 0) {
84 pr_debug("Sig %u: Message digest doesn't match\n",
85 sinfo->index);
86 ret = -EKEYREJECTED;
87 goto error;
88 }
89
90 /* We then calculate anew, using the authenticated attributes
91 * as the contents of the digest instead. Note that we need to
92 * convert the attributes from a CONT.0 into a SET before we
93 * hash it.
94 */
95 memset(digest, 0, sinfo->sig.digest_size);
96
97 ret = crypto_shash_init(desc);
98 if (ret < 0)
99 goto error;
100 tag = ASN1_CONS_BIT | ASN1_SET;
101 ret = crypto_shash_update(desc, &tag, 1);
102 if (ret < 0)
103 goto error;
104 ret = crypto_shash_finup(desc, sinfo->authattrs,
105 sinfo->authattrs_len, digest);
106 if (ret < 0)
107 goto error;
108 pr_devel("AADigest = [%*ph]\n", 8, digest);
109 }
110
111 sinfo->sig.digest = digest;
112 digest = NULL;
113
114error:
115 kfree(digest);
116error_no_desc:
117 crypto_free_shash(tfm);
118 kleave(" = %d", ret);
119 return ret;
120}
121
122/*
David Howellsa4730352014-07-01 16:40:19 +0100123 * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7
124 * uses the issuer's name and the issuing certificate serial number for
125 * matching purposes. These must match the certificate issuer's name (not
126 * subject's name) and the certificate serial number [RFC 2315 6.7].
127 */
128static int pkcs7_find_key(struct pkcs7_message *pkcs7,
129 struct pkcs7_signed_info *sinfo)
130{
131 struct x509_certificate *x509;
132 unsigned certix = 1;
133
David Howells46963b72014-09-16 17:36:13 +0100134 kenter("%u", sinfo->index);
David Howellsa4730352014-07-01 16:40:19 +0100135
136 for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {
137 /* I'm _assuming_ that the generator of the PKCS#7 message will
138 * encode the fields from the X.509 cert in the same way in the
139 * PKCS#7 message - but I can't be 100% sure of that. It's
140 * possible this will need element-by-element comparison.
141 */
David Howells46963b72014-09-16 17:36:13 +0100142 if (!asymmetric_key_id_same(x509->id, sinfo->signing_cert_id))
David Howellsa4730352014-07-01 16:40:19 +0100143 continue;
144 pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
145 sinfo->index, certix);
146
David Howellsa4730352014-07-01 16:40:19 +0100147 if (x509->pub->pkey_algo != sinfo->sig.pkey_algo) {
148 pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",
149 sinfo->index);
150 continue;
151 }
152
153 sinfo->signer = x509;
154 return 0;
155 }
David Howells46963b72014-09-16 17:36:13 +0100156
David Howells757932e2014-09-16 17:36:17 +0100157 /* The relevant X.509 cert isn't found here, but it might be found in
158 * the trust keyring.
159 */
160 pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
161 sinfo->index,
162 sinfo->signing_cert_id->len, sinfo->signing_cert_id->data);
163 return 0;
David Howellsa4730352014-07-01 16:40:19 +0100164}
165
David Howells9f0d3312014-07-01 16:40:19 +0100166/*
David Howells8c76d792014-07-01 16:40:19 +0100167 * Verify the internal certificate chain as best we can.
168 */
169static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
170 struct pkcs7_signed_info *sinfo)
171{
172 struct x509_certificate *x509 = sinfo->signer, *p;
David Howells4573b642015-07-20 21:16:26 +0100173 struct asymmetric_key_id *auth;
David Howells8c76d792014-07-01 16:40:19 +0100174 int ret;
175
176 kenter("");
177
178 for (p = pkcs7->certs; p; p = p->next)
179 p->seen = false;
180
181 for (;;) {
David Howells46963b72014-09-16 17:36:13 +0100182 pr_debug("verify %s: %*phN\n",
183 x509->subject,
184 x509->raw_serial_size, x509->raw_serial);
David Howells8c76d792014-07-01 16:40:19 +0100185 x509->seen = true;
186 ret = x509_get_sig_params(x509);
187 if (ret < 0)
David Howells41559422014-09-16 17:36:15 +0100188 goto maybe_missing_crypto_in_x509;
David Howells8c76d792014-07-01 16:40:19 +0100189
David Howells412eccb2014-07-31 14:46:44 +0100190 pr_debug("- issuer %s\n", x509->issuer);
David Howells4573b642015-07-20 21:16:26 +0100191 if (x509->akid_id)
192 pr_debug("- authkeyid.id %*phN\n",
193 x509->akid_id->len, x509->akid_id->data);
David Howellsb92e6572015-07-20 21:16:26 +0100194 if (x509->akid_skid)
David Howells4573b642015-07-20 21:16:26 +0100195 pr_debug("- authkeyid.skid %*phN\n",
David Howellsb92e6572015-07-20 21:16:26 +0100196 x509->akid_skid->len, x509->akid_skid->data);
David Howells8c76d792014-07-01 16:40:19 +0100197
David Howells4573b642015-07-20 21:16:26 +0100198 if ((!x509->akid_id && !x509->akid_skid) ||
David Howells412eccb2014-07-31 14:46:44 +0100199 strcmp(x509->subject, x509->issuer) == 0) {
David Howells8c76d792014-07-01 16:40:19 +0100200 /* If there's no authority certificate specified, then
201 * the certificate must be self-signed and is the root
202 * of the chain. Likewise if the cert is its own
203 * authority.
204 */
205 pr_debug("- no auth?\n");
206 if (x509->raw_subject_size != x509->raw_issuer_size ||
207 memcmp(x509->raw_subject, x509->raw_issuer,
208 x509->raw_issuer_size) != 0)
209 return 0;
210
211 ret = x509_check_signature(x509->pub, x509);
212 if (ret < 0)
David Howells41559422014-09-16 17:36:15 +0100213 goto maybe_missing_crypto_in_x509;
David Howells8c76d792014-07-01 16:40:19 +0100214 x509->signer = x509;
215 pr_debug("- self-signed\n");
216 return 0;
217 }
218
219 /* Look through the X.509 certificates in the PKCS#7 message's
220 * list to see if the next one is there.
221 */
David Howells4573b642015-07-20 21:16:26 +0100222 auth = x509->akid_id;
223 if (auth) {
224 pr_debug("- want %*phN\n", auth->len, auth->data);
225 for (p = pkcs7->certs; p; p = p->next) {
226 pr_debug("- cmp [%u] %*phN\n",
227 p->index, p->id->len, p->id->data);
228 if (asymmetric_key_id_same(p->id, auth))
229 goto found_issuer_check_skid;
230 }
231 } else {
232 auth = x509->akid_skid;
233 pr_debug("- want %*phN\n", auth->len, auth->data);
234 for (p = pkcs7->certs; p; p = p->next) {
235 if (!p->skid)
236 continue;
237 pr_debug("- cmp [%u] %*phN\n",
238 p->index, p->skid->len, p->skid->data);
239 if (asymmetric_key_id_same(p->skid, auth))
240 goto found_issuer;
241 }
David Howells8c76d792014-07-01 16:40:19 +0100242 }
243
244 /* We didn't find the root of this chain */
245 pr_debug("- top\n");
246 return 0;
247
David Howells4573b642015-07-20 21:16:26 +0100248 found_issuer_check_skid:
249 /* We matched issuer + serialNumber, but if there's an
250 * authKeyId.keyId, that must match the CA subjKeyId also.
251 */
252 if (x509->akid_skid &&
253 !asymmetric_key_id_same(p->skid, x509->akid_skid)) {
254 pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
255 sinfo->index, x509->index, p->index);
256 return -EKEYREJECTED;
257 }
David Howells8c76d792014-07-01 16:40:19 +0100258 found_issuer:
David Howells46963b72014-09-16 17:36:13 +0100259 pr_debug("- subject %s\n", p->subject);
David Howells8c76d792014-07-01 16:40:19 +0100260 if (p->seen) {
261 pr_warn("Sig %u: X.509 chain contains loop\n",
262 sinfo->index);
263 return 0;
264 }
265 ret = x509_check_signature(p->pub, x509);
266 if (ret < 0)
267 return ret;
268 x509->signer = p;
269 if (x509 == p) {
270 pr_debug("- self-signed\n");
271 return 0;
272 }
273 x509 = p;
274 might_sleep();
275 }
David Howells41559422014-09-16 17:36:15 +0100276
277maybe_missing_crypto_in_x509:
278 /* Just prune the certificate chain at this point if we lack some
279 * crypto module to go further. Note, however, we don't want to set
280 * sinfo->missing_crypto as the signed info block may still be
281 * validatable against an X.509 cert lower in the chain that we have a
282 * trusted copy of.
283 */
284 if (ret == -ENOPKG)
285 return 0;
286 return ret;
David Howells8c76d792014-07-01 16:40:19 +0100287}
288
289/*
David Howells9f0d3312014-07-01 16:40:19 +0100290 * Verify one signed information block from a PKCS#7 message.
291 */
292static int pkcs7_verify_one(struct pkcs7_message *pkcs7,
293 struct pkcs7_signed_info *sinfo)
294{
295 int ret;
296
297 kenter(",%u", sinfo->index);
298
299 /* First of all, digest the data in the PKCS#7 message and the
300 * signed information block
301 */
302 ret = pkcs7_digest(pkcs7, sinfo);
303 if (ret < 0)
304 return ret;
305
David Howells757932e2014-09-16 17:36:17 +0100306 /* Find the key for the signature if there is one */
David Howellsa4730352014-07-01 16:40:19 +0100307 ret = pkcs7_find_key(pkcs7, sinfo);
308 if (ret < 0)
309 return ret;
310
David Howells757932e2014-09-16 17:36:17 +0100311 if (!sinfo->signer)
312 return 0;
313
David Howellsa4730352014-07-01 16:40:19 +0100314 pr_devel("Using X.509[%u] for sig %u\n",
315 sinfo->signer->index, sinfo->index);
316
317 /* Verify the PKCS#7 binary against the key */
318 ret = public_key_verify_signature(sinfo->signer->pub, &sinfo->sig);
319 if (ret < 0)
320 return ret;
321
322 pr_devel("Verified signature %u\n", sinfo->index);
323
David Howells8c76d792014-07-01 16:40:19 +0100324 /* Verify the internal certificate chain */
325 return pkcs7_verify_sig_chain(pkcs7, sinfo);
David Howells9f0d3312014-07-01 16:40:19 +0100326}
327
328/**
329 * pkcs7_verify - Verify a PKCS#7 message
330 * @pkcs7: The PKCS#7 message to be verified
David Howells41559422014-09-16 17:36:15 +0100331 *
332 * Verify a PKCS#7 message is internally consistent - that is, the data digest
333 * matches the digest in the AuthAttrs and any signature in the message or one
334 * of the X.509 certificates it carries that matches another X.509 cert in the
335 * message can be verified.
336 *
337 * This does not look to match the contents of the PKCS#7 message against any
338 * external public keys.
339 *
340 * Returns, in order of descending priority:
341 *
342 * (*) -EKEYREJECTED if a signature failed to match for which we found an
343 * appropriate X.509 certificate, or:
344 *
345 * (*) -EBADMSG if some part of the message was invalid, or:
346 *
347 * (*) -ENOPKG if none of the signature chains are verifiable because suitable
348 * crypto modules couldn't be found, or:
349 *
350 * (*) 0 if all the signature chains that don't incur -ENOPKG can be verified
351 * (note that a signature chain may be of zero length), or:
David Howells9f0d3312014-07-01 16:40:19 +0100352 */
353int pkcs7_verify(struct pkcs7_message *pkcs7)
354{
355 struct pkcs7_signed_info *sinfo;
356 struct x509_certificate *x509;
David Howells41559422014-09-16 17:36:15 +0100357 int enopkg = -ENOPKG;
David Howells9f0d3312014-07-01 16:40:19 +0100358 int ret, n;
359
360 kenter("");
361
362 for (n = 0, x509 = pkcs7->certs; x509; x509 = x509->next, n++) {
363 ret = x509_get_sig_params(x509);
364 if (ret < 0)
365 return ret;
David Howells9f0d3312014-07-01 16:40:19 +0100366 }
367
368 for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
369 ret = pkcs7_verify_one(pkcs7, sinfo);
370 if (ret < 0) {
David Howells41559422014-09-16 17:36:15 +0100371 if (ret == -ENOPKG) {
372 sinfo->unsupported_crypto = true;
373 continue;
374 }
David Howells9f0d3312014-07-01 16:40:19 +0100375 kleave(" = %d", ret);
376 return ret;
377 }
David Howells41559422014-09-16 17:36:15 +0100378 enopkg = 0;
David Howells9f0d3312014-07-01 16:40:19 +0100379 }
380
David Howells41559422014-09-16 17:36:15 +0100381 kleave(" = %d", enopkg);
382 return enopkg;
David Howells9f0d3312014-07-01 16:40:19 +0100383}
384EXPORT_SYMBOL_GPL(pkcs7_verify);
David Howells4ebdb76f2015-07-20 21:16:26 +0100385
386/**
387 * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
388 * @pkcs7: The PKCS#7 message
389 * @data: The data to be verified
390 * @datalen: The amount of data
391 *
392 * Supply the detached data needed to verify a PKCS#7 message. Note that no
393 * attempt to retain/pin the data is made. That is left to the caller. The
394 * data will not be modified by pkcs7_verify() and will not be freed when the
395 * PKCS#7 message is freed.
396 *
397 * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
398 */
399int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
400 const void *data, size_t datalen)
401{
402 if (pkcs7->data) {
403 pr_debug("Data already supplied\n");
404 return -EINVAL;
405 }
406 pkcs7->data = data;
407 pkcs7->data_len = datalen;
408 return 0;
409}