You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rootcert.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <ipxe/crypto.h>
  26. #include <ipxe/sha256.h>
  27. #include <ipxe/x509.h>
  28. #include <ipxe/settings.h>
  29. #include <ipxe/dhcp.h>
  30. #include <ipxe/init.h>
  31. #include <ipxe/rootcert.h>
  32. /** @file
  33. *
  34. * Root certificate store
  35. *
  36. */
  37. /** Length of a root certificate fingerprint */
  38. #define FINGERPRINT_LEN SHA256_DIGEST_SIZE
  39. /* Allow trusted certificates to be overridden if not explicitly specified */
  40. #ifdef TRUSTED
  41. #define ALLOW_TRUST_OVERRIDE 0
  42. #else
  43. #define ALLOW_TRUST_OVERRIDE 1
  44. #endif
  45. /* Use iPXE root CA if no trusted certificates are explicitly specified */
  46. #ifndef TRUSTED
  47. #define TRUSTED \
  48. /* iPXE root CA */ \
  49. 0x9f, 0xaf, 0x71, 0x7b, 0x7f, 0x8c, 0xa2, 0xf9, 0x3c, 0x25, \
  50. 0x6c, 0x79, 0xf8, 0xac, 0x55, 0x91, 0x89, 0x5d, 0x66, 0xd1, \
  51. 0xff, 0x3b, 0xee, 0x63, 0x97, 0xa7, 0x0d, 0x29, 0xc6, 0x5e, \
  52. 0xed, 0x1a,
  53. #endif
  54. /** Root certificate fingerprints */
  55. static const uint8_t fingerprints[] = { TRUSTED };
  56. /** Root certificate fingerprint setting */
  57. static struct setting trust_setting __setting ( SETTING_CRYPTO, trust ) = {
  58. .name = "trust",
  59. .description = "Trusted root certificate fingerprints",
  60. .tag = DHCP_EB_TRUST,
  61. .type = &setting_type_hex,
  62. };
  63. /** Root certificates */
  64. struct x509_root root_certificates = {
  65. .digest = &sha256_algorithm,
  66. .count = ( sizeof ( fingerprints ) / FINGERPRINT_LEN ),
  67. .fingerprints = fingerprints,
  68. };
  69. /**
  70. * Initialise root certificate
  71. *
  72. * The list of trusted root certificates can be specified at build
  73. * time using the TRUST= build parameter. If no certificates are
  74. * specified, then the default iPXE root CA certificate is trusted.
  75. *
  76. * If no certificates were explicitly specified, then we allow the
  77. * list of trusted root certificate fingerprints to be overridden
  78. * using the "trust" setting, but only at the point of iPXE
  79. * initialisation. This prevents untrusted sources of settings
  80. * (e.g. DHCP) from subverting the chain of trust, while allowing
  81. * trustworthy sources (e.g. VMware GuestInfo or non-volatile stored
  82. * options) to specify the trusted root certificate without requiring
  83. * a rebuild.
  84. */
  85. static void rootcert_init ( void ) {
  86. static int initialised;
  87. void *external = NULL;
  88. int len;
  89. /* Allow trusted root certificates to be overridden only if
  90. * not explicitly specified at build time.
  91. */
  92. if ( ALLOW_TRUST_OVERRIDE && ( ! initialised ) ) {
  93. /* Fetch copy of "trust" setting, if it exists. This
  94. * memory will never be freed.
  95. */
  96. if ( ( len = fetch_raw_setting_copy ( NULL, &trust_setting,
  97. &external ) ) >= 0 ) {
  98. root_certificates.fingerprints = external;
  99. root_certificates.count = ( len / FINGERPRINT_LEN );
  100. }
  101. /* Prevent subsequent modifications */
  102. initialised = 1;
  103. }
  104. DBGC ( &root_certificates, "ROOTCERT using %d %s certificate(s):\n",
  105. root_certificates.count, ( external ? "external" : "built-in" ));
  106. DBGC_HDA ( &root_certificates, 0, root_certificates.fingerprints,
  107. ( root_certificates.count * FINGERPRINT_LEN ) );
  108. }
  109. /** Root certificate initialiser */
  110. struct startup_fn rootcert_startup_fn __startup_fn ( STARTUP_LATE ) = {
  111. .name = "rootcert",
  112. .startup = rootcert_init,
  113. };