system_keyring.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "module-internal.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 u8 system_certificate_list_end[];
  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),
  34. KEY_ALLOC_NOT_IN_QUOTA, NULL);
  35. if (IS_ERR(system_trusted_keyring))
  36. panic("Can't allocate system trusted keyring\n");
  37. return 0;
  38. }
  39. /*
  40. * Must be initialised before we try and load the keys into the keyring.
  41. */
  42. device_initcall(system_trusted_keyring_init);
  43. /*
  44. * Load the compiled-in list of X.509 certificates.
  45. */
  46. static __init int load_system_certificate_list(void)
  47. {
  48. key_ref_t key;
  49. const u8 *p, *end;
  50. size_t plen;
  51. pr_notice("Loading compiled-in X.509 certificates\n");
  52. end = system_certificate_list_end;
  53. p = system_certificate_list;
  54. while (p < end) {
  55. /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
  56. * than 256 bytes in size.
  57. */
  58. if (end - p < 4)
  59. goto dodgy_cert;
  60. if (p[0] != 0x30 &&
  61. p[1] != 0x82)
  62. goto dodgy_cert;
  63. plen = (p[2] << 8) | p[3];
  64. plen += 4;
  65. if (plen > end - p)
  66. goto dodgy_cert;
  67. key = key_create_or_update(make_key_ref(system_trusted_keyring, 1),
  68. "asymmetric",
  69. NULL,
  70. p,
  71. plen,
  72. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  73. KEY_USR_VIEW,
  74. KEY_ALLOC_NOT_IN_QUOTA);
  75. if (IS_ERR(key)) {
  76. pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
  77. PTR_ERR(key));
  78. } else {
  79. pr_notice("Loaded X.509 cert '%s'\n",
  80. key_ref_to_ptr(key)->description);
  81. key_ref_put(key);
  82. }
  83. p += plen;
  84. }
  85. return 0;
  86. dodgy_cert:
  87. pr_err("Problem parsing in-kernel X.509 certificate list\n");
  88. return 0;
  89. }
  90. late_initcall(load_system_certificate_list);