iPXE
rootcert.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdlib.h>
00027 #include <ipxe/crypto.h>
00028 #include <ipxe/sha256.h>
00029 #include <ipxe/x509.h>
00030 #include <ipxe/settings.h>
00031 #include <ipxe/dhcp.h>
00032 #include <ipxe/init.h>
00033 #include <ipxe/rootcert.h>
00034 
00035 /** @file
00036  *
00037  * Root certificate store
00038  *
00039  */
00040 
00041 /** Length of a root certificate fingerprint */
00042 #define FINGERPRINT_LEN SHA256_DIGEST_SIZE
00043 
00044 /* Allow trusted certificates to be overridden if not explicitly specified */
00045 #ifdef TRUSTED
00046 #define ALLOW_TRUST_OVERRIDE 0
00047 #else
00048 #define ALLOW_TRUST_OVERRIDE 1
00049 #endif
00050 
00051 /* Use iPXE root CA if no trusted certificates are explicitly specified */
00052 #ifndef TRUSTED
00053 #define TRUSTED                                                         \
00054         /* iPXE root CA */                                              \
00055         0x9f, 0xaf, 0x71, 0x7b, 0x7f, 0x8c, 0xa2, 0xf9, 0x3c, 0x25,     \
00056         0x6c, 0x79, 0xf8, 0xac, 0x55, 0x91, 0x89, 0x5d, 0x66, 0xd1,     \
00057         0xff, 0x3b, 0xee, 0x63, 0x97, 0xa7, 0x0d, 0x29, 0xc6, 0x5e,     \
00058         0xed, 0x1a,
00059 #endif
00060 
00061 /** Root certificate fingerprints */
00062 static const uint8_t fingerprints[] = { TRUSTED };
00063 
00064 /** Root certificate fingerprint setting */
00065 static struct setting trust_setting __setting ( SETTING_CRYPTO, trust ) = {
00066         .name = "trust",
00067         .description = "Trusted root certificate fingerprints",
00068         .tag = DHCP_EB_TRUST,
00069         .type = &setting_type_hex,
00070 };
00071 
00072 /** Root certificates */
00073 struct x509_root root_certificates = {
00074         .digest = &sha256_algorithm,
00075         .count = ( sizeof ( fingerprints ) / FINGERPRINT_LEN ),
00076         .fingerprints = fingerprints,
00077 };
00078 
00079 /**
00080  * Initialise root certificate
00081  *
00082  * The list of trusted root certificates can be specified at build
00083  * time using the TRUST= build parameter.  If no certificates are
00084  * specified, then the default iPXE root CA certificate is trusted.
00085  *
00086  * If no certificates were explicitly specified, then we allow the
00087  * list of trusted root certificate fingerprints to be overridden
00088  * using the "trust" setting, but only at the point of iPXE
00089  * initialisation.  This prevents untrusted sources of settings
00090  * (e.g. DHCP) from subverting the chain of trust, while allowing
00091  * trustworthy sources (e.g. VMware GuestInfo or non-volatile stored
00092  * options) to specify the trusted root certificate without requiring
00093  * a rebuild.
00094  */
00095 static void rootcert_init ( void ) {
00096         static int initialised;
00097         void *external = NULL;
00098         int len;
00099 
00100         /* Allow trusted root certificates to be overridden only if
00101          * not explicitly specified at build time.
00102          */
00103         if ( ALLOW_TRUST_OVERRIDE && ( ! initialised ) ) {
00104 
00105                 /* Fetch copy of "trust" setting, if it exists.  This
00106                  * memory will never be freed.
00107                  */
00108                 if ( ( len = fetch_raw_setting_copy ( NULL, &trust_setting,
00109                                                       &external ) ) >= 0 ) {
00110                         root_certificates.fingerprints = external;
00111                         root_certificates.count = ( len / FINGERPRINT_LEN );
00112                 }
00113 
00114                 /* Prevent subsequent modifications */
00115                 initialised = 1;
00116         }
00117 
00118         DBGC ( &root_certificates, "ROOTCERT using %d %s certificate(s):\n",
00119                root_certificates.count, ( external ? "external" : "built-in" ));
00120         DBGC_HDA ( &root_certificates, 0, root_certificates.fingerprints,
00121                    ( root_certificates.count * FINGERPRINT_LEN ) );
00122 }
00123 
00124 /** Root certificate initialiser */
00125 struct startup_fn rootcert_startup_fn __startup_fn ( STARTUP_LATE ) = {
00126         .startup = rootcert_init,
00127 };