1 | /** @file
|
---|
2 |
|
---|
3 | A hook-in library for NetworkPkg/TlsAuthConfigDxe, in order to set volatile
|
---|
4 | variables related to TLS configuration, before TlsAuthConfigDxe or HttpDxe
|
---|
5 | (which is a UEFI_DRIVER) consume them.
|
---|
6 |
|
---|
7 | Copyright (C) 2013, 2015, 2018, Red Hat, Inc.
|
---|
8 | Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>
|
---|
9 |
|
---|
10 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
11 |
|
---|
12 | **/
|
---|
13 |
|
---|
14 | #include <Uefi/UefiBaseType.h>
|
---|
15 | #include <Uefi/UefiSpec.h>
|
---|
16 |
|
---|
17 | #include <Guid/HttpTlsCipherList.h>
|
---|
18 | #include <Guid/TlsAuthentication.h>
|
---|
19 |
|
---|
20 | #include <Library/BaseLib.h>
|
---|
21 | #include <Library/DebugLib.h>
|
---|
22 | #include <Library/MemoryAllocationLib.h>
|
---|
23 | #include <Library/QemuFwCfgLib.h>
|
---|
24 | #include <Library/UefiRuntimeServicesTableLib.h>
|
---|
25 |
|
---|
26 | /**
|
---|
27 | Read the list of trusted CA certificates from the fw_cfg file
|
---|
28 | "etc/edk2/https/cacerts", and store it to
|
---|
29 | gEfiTlsCaCertificateGuid:EFI_TLS_CA_CERTIFICATE_VARIABLE.
|
---|
30 |
|
---|
31 | The contents are validated (for well-formedness) by NetworkPkg/HttpDxe.
|
---|
32 | **/
|
---|
33 | STATIC
|
---|
34 | VOID
|
---|
35 | SetCaCerts (
|
---|
36 | VOID
|
---|
37 | )
|
---|
38 | {
|
---|
39 | EFI_STATUS Status;
|
---|
40 | FIRMWARE_CONFIG_ITEM HttpsCaCertsItem;
|
---|
41 | UINTN HttpsCaCertsSize;
|
---|
42 | VOID *HttpsCaCerts;
|
---|
43 |
|
---|
44 | Status = QemuFwCfgFindFile ("etc/edk2/https/cacerts", &HttpsCaCertsItem,
|
---|
45 | &HttpsCaCertsSize);
|
---|
46 | if (EFI_ERROR (Status)) {
|
---|
47 | DEBUG ((DEBUG_VERBOSE, "%a:%a: not touching CA cert list\n",
|
---|
48 | gEfiCallerBaseName, __FUNCTION__));
|
---|
49 | return;
|
---|
50 | }
|
---|
51 |
|
---|
52 | //
|
---|
53 | // Delete the current EFI_TLS_CA_CERTIFICATE_VARIABLE if it exists. This
|
---|
54 | // serves two purposes:
|
---|
55 | //
|
---|
56 | // (a) If the variable exists with EFI_VARIABLE_NON_VOLATILE attribute, we
|
---|
57 | // cannot make it volatile without deleting it first.
|
---|
58 | //
|
---|
59 | // (b) If we fail to recreate the variable later, deleting the current one is
|
---|
60 | // still justified if the fw_cfg file exists. Emptying the set of trusted
|
---|
61 | // CA certificates will fail HTTPS boot, which is better than trusting
|
---|
62 | // any certificate that's possibly missing from the fw_cfg file.
|
---|
63 | //
|
---|
64 | Status = gRT->SetVariable (
|
---|
65 | EFI_TLS_CA_CERTIFICATE_VARIABLE, // VariableName
|
---|
66 | &gEfiTlsCaCertificateGuid, // VendorGuid
|
---|
67 | 0, // Attributes
|
---|
68 | 0, // DataSize
|
---|
69 | NULL // Data
|
---|
70 | );
|
---|
71 | if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
|
---|
72 | //
|
---|
73 | // This is fatal.
|
---|
74 | //
|
---|
75 | DEBUG ((DEBUG_ERROR, "%a:%a: failed to delete %g:\"%s\"\n",
|
---|
76 | gEfiCallerBaseName, __FUNCTION__, &gEfiTlsCaCertificateGuid,
|
---|
77 | EFI_TLS_CA_CERTIFICATE_VARIABLE));
|
---|
78 | ASSERT_EFI_ERROR (Status);
|
---|
79 | CpuDeadLoop ();
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (HttpsCaCertsSize == 0) {
|
---|
83 | DEBUG ((DEBUG_VERBOSE, "%a:%a: applied empty CA cert list\n",
|
---|
84 | gEfiCallerBaseName, __FUNCTION__));
|
---|
85 | return;
|
---|
86 | }
|
---|
87 |
|
---|
88 | HttpsCaCerts = AllocatePool (HttpsCaCertsSize);
|
---|
89 | if (HttpsCaCerts == NULL) {
|
---|
90 | DEBUG ((DEBUG_ERROR, "%a:%a: failed to allocate HttpsCaCerts\n",
|
---|
91 | gEfiCallerBaseName, __FUNCTION__));
|
---|
92 | return;
|
---|
93 | }
|
---|
94 |
|
---|
95 | QemuFwCfgSelectItem (HttpsCaCertsItem);
|
---|
96 | QemuFwCfgReadBytes (HttpsCaCertsSize, HttpsCaCerts);
|
---|
97 |
|
---|
98 | Status = gRT->SetVariable (
|
---|
99 | EFI_TLS_CA_CERTIFICATE_VARIABLE, // VariableName
|
---|
100 | &gEfiTlsCaCertificateGuid, // VendorGuid
|
---|
101 | EFI_VARIABLE_BOOTSERVICE_ACCESS, // Attributes
|
---|
102 | HttpsCaCertsSize, // DataSize
|
---|
103 | HttpsCaCerts // Data
|
---|
104 | );
|
---|
105 | if (EFI_ERROR (Status)) {
|
---|
106 | DEBUG ((DEBUG_ERROR, "%a:%a: failed to set %g:\"%s\": %r\n",
|
---|
107 | gEfiCallerBaseName, __FUNCTION__, &gEfiTlsCaCertificateGuid,
|
---|
108 | EFI_TLS_CA_CERTIFICATE_VARIABLE, Status));
|
---|
109 | goto FreeHttpsCaCerts;
|
---|
110 | }
|
---|
111 |
|
---|
112 | DEBUG ((DEBUG_VERBOSE, "%a:%a: stored CA cert list (%Lu byte(s))\n",
|
---|
113 | gEfiCallerBaseName, __FUNCTION__, (UINT64)HttpsCaCertsSize));
|
---|
114 |
|
---|
115 | FreeHttpsCaCerts:
|
---|
116 | FreePool (HttpsCaCerts);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | Read the list of trusted cipher suites from the fw_cfg file
|
---|
121 | "etc/edk2/https/ciphers", and store it to
|
---|
122 | gEdkiiHttpTlsCipherListGuid:EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE.
|
---|
123 |
|
---|
124 | The contents are propagated by NetworkPkg/HttpDxe to NetworkPkg/TlsDxe; the
|
---|
125 | list is processed by the latter.
|
---|
126 | **/
|
---|
127 | STATIC
|
---|
128 | VOID
|
---|
129 | SetCipherSuites (
|
---|
130 | VOID
|
---|
131 | )
|
---|
132 | {
|
---|
133 | EFI_STATUS Status;
|
---|
134 | FIRMWARE_CONFIG_ITEM HttpsCiphersItem;
|
---|
135 | UINTN HttpsCiphersSize;
|
---|
136 | VOID *HttpsCiphers;
|
---|
137 |
|
---|
138 | Status = QemuFwCfgFindFile ("etc/edk2/https/ciphers", &HttpsCiphersItem,
|
---|
139 | &HttpsCiphersSize);
|
---|
140 | if (EFI_ERROR (Status)) {
|
---|
141 | DEBUG ((DEBUG_VERBOSE, "%a:%a: not touching cipher suites\n",
|
---|
142 | gEfiCallerBaseName, __FUNCTION__));
|
---|
143 | return;
|
---|
144 | }
|
---|
145 | //
|
---|
146 | // From this point on, any failure is fatal. An ordered cipher preference
|
---|
147 | // list is available from QEMU, thus we cannot let the firmware attempt HTTPS
|
---|
148 | // boot with either pre-existent or non-existent preferences. An empty set of
|
---|
149 | // cipher suites does not fail HTTPS boot automatically; the default cipher
|
---|
150 | // suite preferences would take effect, and we must prevent that.
|
---|
151 | //
|
---|
152 | // Delete the current EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE if it exists. If
|
---|
153 | // the variable exists with EFI_VARIABLE_NON_VOLATILE attribute, we cannot
|
---|
154 | // make it volatile without deleting it first.
|
---|
155 | //
|
---|
156 | Status = gRT->SetVariable (
|
---|
157 | EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE, // VariableName
|
---|
158 | &gEdkiiHttpTlsCipherListGuid, // VendorGuid
|
---|
159 | 0, // Attributes
|
---|
160 | 0, // DataSize
|
---|
161 | NULL // Data
|
---|
162 | );
|
---|
163 | if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
|
---|
164 | DEBUG ((DEBUG_ERROR, "%a:%a: failed to delete %g:\"%s\"\n",
|
---|
165 | gEfiCallerBaseName, __FUNCTION__, &gEdkiiHttpTlsCipherListGuid,
|
---|
166 | EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE));
|
---|
167 | goto Done;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (HttpsCiphersSize == 0) {
|
---|
171 | DEBUG ((DEBUG_ERROR, "%a:%a: list of cipher suites must not be empty\n",
|
---|
172 | gEfiCallerBaseName, __FUNCTION__));
|
---|
173 | Status = EFI_INVALID_PARAMETER;
|
---|
174 | goto Done;
|
---|
175 | }
|
---|
176 |
|
---|
177 | HttpsCiphers = AllocatePool (HttpsCiphersSize);
|
---|
178 | if (HttpsCiphers == NULL) {
|
---|
179 | DEBUG ((DEBUG_ERROR, "%a:%a: failed to allocate HttpsCiphers\n",
|
---|
180 | gEfiCallerBaseName, __FUNCTION__));
|
---|
181 | Status = EFI_OUT_OF_RESOURCES;
|
---|
182 | goto Done;
|
---|
183 | }
|
---|
184 |
|
---|
185 | QemuFwCfgSelectItem (HttpsCiphersItem);
|
---|
186 | QemuFwCfgReadBytes (HttpsCiphersSize, HttpsCiphers);
|
---|
187 |
|
---|
188 | Status = gRT->SetVariable (
|
---|
189 | EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE, // VariableName
|
---|
190 | &gEdkiiHttpTlsCipherListGuid, // VendorGuid
|
---|
191 | EFI_VARIABLE_BOOTSERVICE_ACCESS, // Attributes
|
---|
192 | HttpsCiphersSize, // DataSize
|
---|
193 | HttpsCiphers // Data
|
---|
194 | );
|
---|
195 | if (EFI_ERROR (Status)) {
|
---|
196 | DEBUG ((DEBUG_ERROR, "%a:%a: failed to set %g:\"%s\"\n",
|
---|
197 | gEfiCallerBaseName, __FUNCTION__, &gEdkiiHttpTlsCipherListGuid,
|
---|
198 | EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE));
|
---|
199 | goto FreeHttpsCiphers;
|
---|
200 | }
|
---|
201 |
|
---|
202 | DEBUG ((DEBUG_VERBOSE, "%a:%a: stored list of cipher suites (%Lu byte(s))\n",
|
---|
203 | gEfiCallerBaseName, __FUNCTION__, (UINT64)HttpsCiphersSize));
|
---|
204 |
|
---|
205 | FreeHttpsCiphers:
|
---|
206 | FreePool (HttpsCiphers);
|
---|
207 |
|
---|
208 | Done:
|
---|
209 | if (EFI_ERROR (Status)) {
|
---|
210 | ASSERT_EFI_ERROR (Status);
|
---|
211 | CpuDeadLoop ();
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | RETURN_STATUS
|
---|
216 | EFIAPI
|
---|
217 | TlsAuthConfigInit (
|
---|
218 | VOID
|
---|
219 | )
|
---|
220 | {
|
---|
221 | SetCaCerts ();
|
---|
222 | SetCipherSuites ();
|
---|
223 |
|
---|
224 | return RETURN_SUCCESS;
|
---|
225 | }
|
---|