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