Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / rootcert.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdlib.h>
23 #include <ipxe/crypto.h>
24 #include <ipxe/sha256.h>
25 #include <ipxe/x509.h>
26 #include <ipxe/settings.h>
27 #include <ipxe/dhcp.h>
28 #include <ipxe/init.h>
29 #include <ipxe/rootcert.h>
30
31 /** @file
32  *
33  * Root certificate store
34  *
35  */
36
37 /** Length of a root certificate fingerprint */
38 #define FINGERPRINT_LEN SHA256_DIGEST_SIZE
39
40 /* Allow trusted certificates to be overridden if not explicitly specified */
41 #ifdef TRUSTED
42 #define ALLOW_TRUST_OVERRIDE 0
43 #else
44 #define ALLOW_TRUST_OVERRIDE 1
45 #endif
46
47 /* Use iPXE root CA if no trusted certificates are explicitly specified */
48 #ifndef TRUSTED
49 #define TRUSTED                                                         \
50         /* iPXE root CA */                                              \
51         0x9f, 0xaf, 0x71, 0x7b, 0x7f, 0x8c, 0xa2, 0xf9, 0x3c, 0x25,     \
52         0x6c, 0x79, 0xf8, 0xac, 0x55, 0x91, 0x89, 0x5d, 0x66, 0xd1,     \
53         0xff, 0x3b, 0xee, 0x63, 0x97, 0xa7, 0x0d, 0x29, 0xc6, 0x5e,     \
54         0xed, 0x1a,
55 #endif
56
57 /** Root certificate fingerprints */
58 static const uint8_t fingerprints[] = { TRUSTED };
59
60 /** Root certificate fingerprint setting */
61 static struct setting trust_setting __setting ( SETTING_CRYPTO, trust ) = {
62         .name = "trust",
63         .description = "Trusted root certificate fingerprints",
64         .tag = DHCP_EB_TRUST,
65         .type = &setting_type_hex,
66 };
67
68 /** Root certificates */
69 struct x509_root root_certificates = {
70         .digest = &sha256_algorithm,
71         .count = ( sizeof ( fingerprints ) / FINGERPRINT_LEN ),
72         .fingerprints = fingerprints,
73 };
74
75 /**
76  * Initialise root certificate
77  *
78  * The list of trusted root certificates can be specified at build
79  * time using the TRUST= build parameter.  If no certificates are
80  * specified, then the default iPXE root CA certificate is trusted.
81  *
82  * If no certificates were explicitly specified, then we allow the
83  * list of trusted root certificate fingerprints to be overridden
84  * using the "trust" setting, but only at the point of iPXE
85  * initialisation.  This prevents untrusted sources of settings
86  * (e.g. DHCP) from subverting the chain of trust, while allowing
87  * trustworthy sources (e.g. VMware GuestInfo or non-volatile stored
88  * options) to specify the trusted root certificate without requiring
89  * a rebuild.
90  */
91 static void rootcert_init ( void ) {
92         void *external = NULL;
93         int len;
94
95         /* Allow trusted root certificates to be overridden only if
96          * not explicitly specified at build time.
97          */
98         if ( ALLOW_TRUST_OVERRIDE ) {
99
100                 /* Fetch copy of "trust" setting, if it exists.  This
101                  * memory will never be freed.
102                  */
103                 if ( ( len = fetch_raw_setting_copy ( NULL, &trust_setting,
104                                                       &external ) ) >= 0 ) {
105                         root_certificates.fingerprints = external;
106                         root_certificates.count = ( len / FINGERPRINT_LEN );
107                 }
108         }
109
110         DBGC ( &root_certificates, "ROOTCERT using %d %s certificate(s):\n",
111                root_certificates.count, ( external ? "external" : "built-in" ));
112         DBGC_HDA ( &root_certificates, 0, root_certificates.fingerprints,
113                    ( root_certificates.count * FINGERPRINT_LEN ) );
114 }
115
116 /** Root certificate initialiser */
117 struct init_fn rootcert_init_fn __init_fn ( INIT_LATE ) = {
118         .initialise = rootcert_init,
119 };