pkcs7_trust.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Validate the trust chain of a PKCS#7 message.
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define pr_fmt(fmt) "PKCS7: "fmt
  8. #include <linux/kernel.h>
  9. #include <linux/export.h>
  10. #include <linux/slab.h>
  11. #include <linux/err.h>
  12. #include <linux/asn1.h>
  13. #include <linux/key.h>
  14. #include <keys/asymmetric-type.h>
  15. #include <crypto/public_key.h>
  16. #include "pkcs7_parser.h"
  17. /**
  18. * Check the trust on one PKCS#7 SignedInfo block.
  19. */
  20. static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
  21. struct pkcs7_signed_info *sinfo,
  22. struct key *trust_keyring)
  23. {
  24. struct public_key_signature *sig = sinfo->sig;
  25. struct x509_certificate *x509, *last = NULL, *p;
  26. struct key *key;
  27. int ret;
  28. kenter(",%u,", sinfo->index);
  29. if (sinfo->unsupported_crypto) {
  30. kleave(" = -ENOPKG [cached]");
  31. return -ENOPKG;
  32. }
  33. for (x509 = sinfo->signer; x509; x509 = x509->signer) {
  34. if (x509->seen) {
  35. if (x509->verified)
  36. goto verified;
  37. kleave(" = -ENOKEY [cached]");
  38. return -ENOKEY;
  39. }
  40. x509->seen = true;
  41. /* Look to see if this certificate is present in the trusted
  42. * keys.
  43. */
  44. key = find_asymmetric_key(trust_keyring,
  45. x509->id, x509->skid, false);
  46. if (!IS_ERR(key)) {
  47. /* One of the X.509 certificates in the PKCS#7 message
  48. * is apparently the same as one we already trust.
  49. * Verify that the trusted variant can also validate
  50. * the signature on the descendant.
  51. */
  52. pr_devel("sinfo %u: Cert %u as key %x\n",
  53. sinfo->index, x509->index, key_serial(key));
  54. goto matched;
  55. }
  56. if (key == ERR_PTR(-ENOMEM))
  57. return -ENOMEM;
  58. /* Self-signed certificates form roots of their own, and if we
  59. * don't know them, then we can't accept them.
  60. */
  61. if (x509->signer == x509) {
  62. kleave(" = -ENOKEY [unknown self-signed]");
  63. return -ENOKEY;
  64. }
  65. might_sleep();
  66. last = x509;
  67. sig = last->sig;
  68. }
  69. /* No match - see if the root certificate has a signer amongst the
  70. * trusted keys.
  71. */
  72. if (last && (last->sig->auth_ids[0] || last->sig->auth_ids[1])) {
  73. key = find_asymmetric_key(trust_keyring,
  74. last->sig->auth_ids[0],
  75. last->sig->auth_ids[1],
  76. false);
  77. if (!IS_ERR(key)) {
  78. x509 = last;
  79. pr_devel("sinfo %u: Root cert %u signer is key %x\n",
  80. sinfo->index, x509->index, key_serial(key));
  81. goto matched;
  82. }
  83. if (PTR_ERR(key) != -ENOKEY)
  84. return PTR_ERR(key);
  85. }
  86. /* As a last resort, see if we have a trusted public key that matches
  87. * the signed info directly.
  88. */
  89. key = find_asymmetric_key(trust_keyring,
  90. sinfo->sig->auth_ids[0], NULL, false);
  91. if (!IS_ERR(key)) {
  92. pr_devel("sinfo %u: Direct signer is key %x\n",
  93. sinfo->index, key_serial(key));
  94. x509 = NULL;
  95. sig = sinfo->sig;
  96. goto matched;
  97. }
  98. if (PTR_ERR(key) != -ENOKEY)
  99. return PTR_ERR(key);
  100. kleave(" = -ENOKEY [no backref]");
  101. return -ENOKEY;
  102. matched:
  103. ret = verify_signature(key, sig);
  104. key_put(key);
  105. if (ret < 0) {
  106. if (ret == -ENOMEM)
  107. return ret;
  108. kleave(" = -EKEYREJECTED [verify %d]", ret);
  109. return -EKEYREJECTED;
  110. }
  111. verified:
  112. if (x509) {
  113. x509->verified = true;
  114. for (p = sinfo->signer; p != x509; p = p->signer)
  115. p->verified = true;
  116. }
  117. kleave(" = 0");
  118. return 0;
  119. }
  120. /**
  121. * pkcs7_validate_trust - Validate PKCS#7 trust chain
  122. * @pkcs7: The PKCS#7 certificate to validate
  123. * @trust_keyring: Signing certificates to use as starting points
  124. *
  125. * Validate that the certificate chain inside the PKCS#7 message intersects
  126. * keys we already know and trust.
  127. *
  128. * Returns, in order of descending priority:
  129. *
  130. * (*) -EKEYREJECTED if a signature failed to match for which we have a valid
  131. * key, or:
  132. *
  133. * (*) 0 if at least one signature chain intersects with the keys in the trust
  134. * keyring, or:
  135. *
  136. * (*) -ENOPKG if a suitable crypto module couldn't be found for a check on a
  137. * chain.
  138. *
  139. * (*) -ENOKEY if we couldn't find a match for any of the signature chains in
  140. * the message.
  141. *
  142. * May also return -ENOMEM.
  143. */
  144. int pkcs7_validate_trust(struct pkcs7_message *pkcs7,
  145. struct key *trust_keyring)
  146. {
  147. struct pkcs7_signed_info *sinfo;
  148. struct x509_certificate *p;
  149. int cached_ret = -ENOKEY;
  150. int ret;
  151. for (p = pkcs7->certs; p; p = p->next)
  152. p->seen = false;
  153. for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
  154. ret = pkcs7_validate_trust_one(pkcs7, sinfo, trust_keyring);
  155. switch (ret) {
  156. case -ENOKEY:
  157. continue;
  158. case -ENOPKG:
  159. if (cached_ret == -ENOKEY)
  160. cached_ret = -ENOPKG;
  161. continue;
  162. case 0:
  163. cached_ret = 0;
  164. continue;
  165. default:
  166. return ret;
  167. }
  168. }
  169. return cached_ret;
  170. }
  171. EXPORT_SYMBOL_GPL(pkcs7_validate_trust);