system_keyring.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* System trusted keyring for trusted public keys
  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. #include <linux/export.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/cred.h>
  15. #include <linux/err.h>
  16. #include <keys/asymmetric-type.h>
  17. #include <keys/system_keyring.h>
  18. #include <crypto/pkcs7.h>
  19. struct key *system_trusted_keyring;
  20. EXPORT_SYMBOL_GPL(system_trusted_keyring);
  21. extern __initconst const u8 system_certificate_list[];
  22. extern __initconst const unsigned long system_certificate_list_size;
  23. /*
  24. * Load the compiled-in keys
  25. */
  26. static __init int system_trusted_keyring_init(void)
  27. {
  28. pr_notice("Initialise system trusted keyring\n");
  29. system_trusted_keyring =
  30. keyring_alloc(".system_keyring",
  31. KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
  32. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  33. KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
  34. KEY_ALLOC_NOT_IN_QUOTA, NULL);
  35. if (IS_ERR(system_trusted_keyring))
  36. panic("Can't allocate system trusted keyring\n");
  37. set_bit(KEY_FLAG_TRUSTED_ONLY, &system_trusted_keyring->flags);
  38. return 0;
  39. }
  40. /*
  41. * Must be initialised before we try and load the keys into the keyring.
  42. */
  43. device_initcall(system_trusted_keyring_init);
  44. /*
  45. * Load the compiled-in list of X.509 certificates.
  46. */
  47. static __init int load_system_certificate_list(void)
  48. {
  49. key_ref_t key;
  50. const u8 *p, *end;
  51. size_t plen;
  52. pr_notice("Loading compiled-in X.509 certificates\n");
  53. p = system_certificate_list;
  54. end = p + system_certificate_list_size;
  55. while (p < end) {
  56. /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
  57. * than 256 bytes in size.
  58. */
  59. if (end - p < 4)
  60. goto dodgy_cert;
  61. if (p[0] != 0x30 &&
  62. p[1] != 0x82)
  63. goto dodgy_cert;
  64. plen = (p[2] << 8) | p[3];
  65. plen += 4;
  66. if (plen > end - p)
  67. goto dodgy_cert;
  68. key = key_create_or_update(make_key_ref(system_trusted_keyring, 1),
  69. "asymmetric",
  70. NULL,
  71. p,
  72. plen,
  73. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  74. KEY_USR_VIEW | KEY_USR_READ),
  75. KEY_ALLOC_NOT_IN_QUOTA |
  76. KEY_ALLOC_TRUSTED);
  77. if (IS_ERR(key)) {
  78. pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
  79. PTR_ERR(key));
  80. } else {
  81. set_bit(KEY_FLAG_BUILTIN, &key_ref_to_ptr(key)->flags);
  82. pr_notice("Loaded X.509 cert '%s'\n",
  83. key_ref_to_ptr(key)->description);
  84. key_ref_put(key);
  85. }
  86. p += plen;
  87. }
  88. return 0;
  89. dodgy_cert:
  90. pr_err("Problem parsing in-kernel X.509 certificate list\n");
  91. return 0;
  92. }
  93. late_initcall(load_system_certificate_list);
  94. #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
  95. /**
  96. * Verify a PKCS#7-based signature on system data.
  97. * @data: The data to be verified.
  98. * @len: Size of @data.
  99. * @raw_pkcs7: The PKCS#7 message that is the signature.
  100. * @pkcs7_len: The size of @raw_pkcs7.
  101. * @usage: The use to which the key is being put.
  102. */
  103. int system_verify_data(const void *data, unsigned long len,
  104. const void *raw_pkcs7, size_t pkcs7_len,
  105. enum key_being_used_for usage)
  106. {
  107. struct pkcs7_message *pkcs7;
  108. bool trusted;
  109. int ret;
  110. pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
  111. if (IS_ERR(pkcs7))
  112. return PTR_ERR(pkcs7);
  113. /* The data should be detached - so we need to supply it. */
  114. if (pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
  115. pr_err("PKCS#7 signature with non-detached data\n");
  116. ret = -EBADMSG;
  117. goto error;
  118. }
  119. ret = pkcs7_verify(pkcs7, usage);
  120. if (ret < 0)
  121. goto error;
  122. ret = pkcs7_validate_trust(pkcs7, system_trusted_keyring, &trusted);
  123. if (ret < 0)
  124. goto error;
  125. if (!trusted) {
  126. pr_err("PKCS#7 signature not signed with a trusted key\n");
  127. ret = -ENOKEY;
  128. }
  129. error:
  130. pkcs7_free_message(pkcs7);
  131. pr_devel("<==%s() = %d\n", __func__, ret);
  132. return ret;
  133. }
  134. EXPORT_SYMBOL_GPL(system_verify_data);
  135. #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */