iPXE
rootcert.c
Go to the documentation of this file.
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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdlib.h>
27 #include <ipxe/crypto.h>
28 #include <ipxe/sha256.h>
29 #include <ipxe/x509.h>
30 #include <ipxe/settings.h>
31 #include <ipxe/dhcp.h>
32 #include <ipxe/init.h>
33 #include <ipxe/rootcert.h>
34 
35 /** @file
36  *
37  * Root certificate store
38  *
39  */
40 
41 /** Length of a root certificate fingerprint */
42 #define FINGERPRINT_LEN SHA256_DIGEST_SIZE
43 
44 /* Allow trusted certificates to be overridden if not explicitly specified */
45 #ifndef ALLOW_TRUST_OVERRIDE
46  #ifdef TRUSTED
47  #define ALLOW_TRUST_OVERRIDE 0
48  #else
49  #define ALLOW_TRUST_OVERRIDE 1
50  #endif
51 #endif
52 
53 /* Use iPXE root CA if no trusted certificates are explicitly specified */
54 #ifndef TRUSTED
55 #define TRUSTED \
56  /* iPXE root CA */ \
57  0x9f, 0xaf, 0x71, 0x7b, 0x7f, 0x8c, 0xa2, 0xf9, 0x3c, 0x25, \
58  0x6c, 0x79, 0xf8, 0xac, 0x55, 0x91, 0x89, 0x5d, 0x66, 0xd1, \
59  0xff, 0x3b, 0xee, 0x63, 0x97, 0xa7, 0x0d, 0x29, 0xc6, 0x5e, \
60  0xed, 0x1a,
61 #endif
62 
63 /** Flag indicating if root of trust may be overridden at runtime */
65 
66 /** Root certificate fingerprints */
67 static const uint8_t fingerprints[] = { TRUSTED };
68 
69 /** Root certificate fingerprint setting */
70 static struct setting trust_setting __setting ( SETTING_CRYPTO, trust ) = {
71  .name = "trust",
72  .description = "Trusted root certificate fingerprints",
73  .tag = DHCP_EB_TRUST,
74  .type = &setting_type_hex,
75 };
76 
77 /** Root certificates */
80  .digest = &sha256_algorithm,
81  .count = ( sizeof ( fingerprints ) / FINGERPRINT_LEN ),
82  .fingerprints = fingerprints,
83 };
84 
85 /**
86  * Initialise root certificate
87  *
88  * The list of trusted root certificates can be specified at build
89  * time using the TRUST= build parameter. If no certificates are
90  * specified, then the default iPXE root CA certificate is trusted.
91  *
92  * If no certificates were explicitly specified, then we allow the
93  * list of trusted root certificate fingerprints to be overridden
94  * using the "trust" setting, but only at the point of iPXE
95  * initialisation. This prevents untrusted sources of settings
96  * (e.g. DHCP) from subverting the chain of trust, while allowing
97  * trustworthy sources (e.g. VMware GuestInfo or non-volatile stored
98  * options) to specify the trusted root certificate without requiring
99  * a rebuild.
100  */
101 static void rootcert_init ( void ) {
102  static int initialised;
103  void *external = NULL;
104  int len;
105 
106  /* Allow trusted root certificates to be overridden only if
107  * not explicitly specified at build time.
108  */
109  if ( ALLOW_TRUST_OVERRIDE && ( ! initialised ) ) {
110 
111  /* Fetch copy of "trust" setting, if it exists. This
112  * memory will never be freed.
113  */
114  if ( ( len = fetch_raw_setting_copy ( NULL, &trust_setting,
115  &external ) ) >= 0 ) {
116  root_certificates.fingerprints = external;
118  }
119 
120  /* Prevent subsequent modifications */
121  initialised = 1;
122  }
123 
124  DBGC ( &root_certificates, "ROOTCERT using %d %s certificate(s):\n",
125  root_certificates.count, ( external ? "external" : "built-in" ));
128 }
129 
130 /** Root certificate initialiser */
131 struct startup_fn rootcert_startup_fn __startup_fn ( STARTUP_LATE ) = {
132  .name = "rootcert",
133  .startup = rootcert_init,
134 };
Dynamic Host Configuration Protocol.
#define TRUSTED
Definition: rootcert.c:55
static struct setting trust_setting __setting(SETTING_CRYPTO, trust)
Root certificate fingerprint setting.
const int allow_trust_override
Flag indicating if root of trust may be overridden at runtime.
Definition: rootcert.c:64
int fetch_raw_setting_copy(struct settings *settings, const struct setting *setting, void **data)
Fetch value of setting.
Definition: settings.c:821
struct refcnt refcnt
Reference count.
Definition: x509.h:376
static void rootcert_init(void)
Initialise root certificate.
Definition: rootcert.c:101
struct x509_root root_certificates
Root certificates.
Definition: rootcert.c:78
#define DBGC(...)
Definition: compiler.h:505
#define DHCP_EB_TRUST
Trusted root certficate fingerprints.
Definition: dhcp.h:416
Cryptographic API.
const char * name
Definition: init.h:43
#define STARTUP_LATE
Late startup.
Definition: init.h:65
const char * name
Name.
Definition: settings.h:28
A startup/shutdown function.
Definition: init.h:42
#define DBGC_HDA(...)
Definition: compiler.h:506
ring len
Length.
Definition: dwmac.h:231
#define ALLOW_TRUST_OVERRIDE
Definition: rootcert.c:49
struct startup_fn rootcert_startup_fn __startup_fn(STARTUP_LATE)
Root certificate initialiser.
Configuration settings.
#define FINGERPRINT_LEN
Length of a root certificate fingerprint.
Definition: rootcert.c:42
unsigned char uint8_t
Definition: stdint.h:10
X.509 certificates.
A setting.
Definition: settings.h:23
An X.509 root certificate list.
Definition: x509.h:374
struct digest_algorithm sha256_algorithm
SHA-256 algorithm.
Definition: sha256.c:264
unsigned int count
Number of certificates.
Definition: x509.h:380
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition: refcnt.h:77
Root certificate store.
#define SETTING_CRYPTO
Cryptography settings.
Definition: settings.h:79
const void * fingerprints
Certificate fingerprints.
Definition: x509.h:382
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static const uint8_t fingerprints[]
Root certificate fingerprints.
Definition: rootcert.c:67
SHA-256 algorithm.
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition: refcnt.c:101
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321