1 /* Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved. This
2  * program and the accompanying materials are licensed and made available
3  * under the terms and conditions of the 2-Clause BSD License which
4  * accompanies this distribution.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * This file is derived from the following files referred from edk2-staging[1] repo
29  * of tianocore
30  *
31  * MdePkg/Include/Guid/GlobalVariable.h
32  * MdePkg/Include/Guid/WinCertificate.h
33  * MdePkg/Include/Uefi/UefiMultiPhase.h
34  * MdePkg/Include/Uefi/UefiBaseType.h
35  * MdePkg/Include/Guid/ImageAuthentication.h
36  *
37  * [1] https://github.com/tianocore/edk2-staging
38  *
39  * Copyright 2020 IBM Corp.
40  */
41 
42 #ifndef __EDK2_H__
43 #define __EDK2_H__
44 
45 #include <compiler.h>
46 #include <ccan/short_types/short_types.h>
47 
48 #define UUID_SIZE 16
49 
50 typedef struct {
51         u8 b[UUID_SIZE];
52 } uuid_t;
53 
54 #define EFI_GLOBAL_VARIABLE_GUID (uuid_t){{0x61, 0xDF, 0xe4, 0x8b, 0xca, 0x93, 0xd2, 0x11, 0xaa, \
55 			 0x0d, 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c}}
56 
57 #define EFI_IMAGE_SECURITY_DATABASE_GUID (uuid_t){{0xcb, 0xb2, 0x19, 0xd7, 0x3a, 0x3d, 0x96, 0x45, \
58 					   0xa3, 0xbc, 0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f}}
59 
60 #define SECVAR_ATTRIBUTES	39
61 
62 ///
63 /// This identifies a signature based on an X.509 certificate. If the signature is an X.509
64 /// certificate then verification of the signature of an image should validate the public
65 /// key certificate in the image using certificate path verification, up to this X.509
66 /// certificate as a trusted root.  The SignatureHeader size shall always be 0. The
67 /// SignatureSize may vary but shall always be 16 (size of the SignatureOwner component) +
68 /// the size of the certificate itself.
69 /// Note: This means that each certificate will normally be in a separate EFI_SIGNATURE_LIST.
70 ///
71 
72 static const uuid_t EFI_CERT_TYPE_PKCS7_GUID = {{0x9d, 0xd2, 0xaf, 0x4a, 0xdf, 0x68, 0xee, 0x49, 0x8a, 0xa9, 0x34, 0x7d, 0x37, 0x56, 0x65, 0xa7}};
73 
74 static const uuid_t EFI_CERT_X509_GUID = {{ 0xa1, 0x59, 0xc0, 0xa5, 0xe4, 0x94, 0xa7, 0x4a, 0x87, 0xb5, 0xab, 0x15, 0x5c, 0x2b, 0xf0, 0x72 }};
75 
76 static const uuid_t EFI_CERT_SHA1_GUID = {{ 0x12, 0xa5, 0x6c, 0x82, 0x10, 0xcf, 0xc9, 0x4a, 0xb1, 0x87, 0xbe, 0x01, 0x49, 0x66, 0x31, 0xbd }};
77 
78 static const uuid_t EFI_CERT_SHA224_GUID = {{ 0x33, 0x52, 0x6e, 0x0b, 0x5c, 0xa6, 0xc9, 0x44, 0x94, 0x07, 0xd9, 0xab, 0x83, 0xbf, 0xc8, 0xbd }};
79 
80 static const uuid_t EFI_CERT_SHA256_GUID = {{ 0x26, 0x16, 0xc4, 0xc1, 0x4c, 0x50, 0x92, 0x40, 0xac, 0xa9, 0x41, 0xf9, 0x36, 0x93, 0x43, 0x28 }};
81 
82 static const uuid_t EFI_CERT_SHA384_GUID = {{ 0x07, 0x53, 0x3e, 0xff, 0xd0, 0x9f, 0xc9, 0x48, 0x85, 0xf1, 0x8a, 0xd5, 0x6c, 0x70, 0x1e, 0x01 }};
83 
84 static const uuid_t EFI_CERT_SHA512_GUID = {{ 0xae, 0x0f, 0x3e, 0x09, 0xc4, 0xa6, 0x50, 0x4f, 0x9f, 0x1b, 0xd4, 0x1e, 0x2b, 0x89, 0xc1, 0x9a }};
85 
86 static const uuid_t EFI_CERT_RSA2048_GUID = {{ 0xe8, 0x66, 0x57, 0x3c, 0x9c, 0x26, 0x34, 0x4e, 0xaa, 0x14, 0xed, 0x77, 0x6e, 0x85, 0xb3, 0xb6 }};
87 
88 #define EFI_VARIABLE_NON_VOLATILE				0x00000001
89 #define EFI_VARIABLE_BOOTSERVICE_ACCESS				0x00000002
90 #define EFI_VARIABLE_RUNTIME_ACCESS				0x00000004
91 
92 /*
93  * Attributes of Authenticated Variable
94  */
95 #define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS	0x00000020
96 #define EFI_VARIABLE_APPEND_WRITE				0x00000040
97 /*
98  * NOTE: EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated and should be
99  * considered reserved.
100  */
101 #define EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS			0x00000010
102 
103 /*
104  * win_certificate.w_certificate_type
105  */
106 #define WIN_CERT_TYPE_PKCS_SIGNED_DATA	0x0002
107 
108 #define SECURE_BOOT_MODE_ENABLE           1
109 #define SECURE_BOOT_MODE_DISABLE          0
110 ///
111 /// Depricated value definition for SetupMode variable
112 ///
113 #define SETUP_MODE                        1
114 #define USER_MODE                         0
115 
116 /*
117  * EFI Time Abstraction:
118  *   Year:       1900 - 9999
119  *   Month:      1 - 12
120  *   Day:        1 - 31
121  *   Hour:       0 - 23
122  *   Minute:     0 - 59
123  *   Second:     0 - 59
124  *   Nanosecond: 0 - 999,999,999
125  *   TimeZone:   -1440 to 1440 or 2047
126  */
127 struct efi_time {
128 	le16 year;
129 	u8 month;
130 	u8 day;
131 	u8 hour;
132 	u8 minute;
133 	u8 second;
134 	u8 pad1;
135 	le32 nanosecond;
136 	le16 timezone;
137 	u8 daylight;
138 	u8 pad2;
139 };
140 //***********************************************************************
141 // Signature Database
142 //***********************************************************************
143 ///
144 /// The format of a signature database.
145 ///
146 
147 typedef struct __packed {
148   ///
149   /// An identifier which identifies the agent which added the signature to the list.
150   ///
151   uuid_t SignatureOwner;
152   ///
153   /// The format of the signature is defined by the SignatureType.
154   ///
155   unsigned char SignatureData[0];
156 } EFI_SIGNATURE_DATA;
157 
158 typedef struct __packed {
159   ///
160   /// Type of the signature. GUID signature types are defined in below.
161   ///
162   uuid_t SignatureType;
163   ///
164   /// Total size of the signature list, including this header.
165   ///
166   leint32_t	SignatureListSize;
167   ///
168   /// Size of the signature header which precedes the array of signatures.
169   ///
170   leint32_t	SignatureHeaderSize;
171   ///
172   /// Size of each signature.
173   ///
174   leint32_t	SignatureSize;
175   ///
176   /// Header before the array of signatures. The format of this header is specified
177   /// by the SignatureType.
178   /// UINT8           SignatureHeader[SignatureHeaderSize];
179   ///
180   /// An array of signatures. Each signature is SignatureSize bytes in length.
181   /// EFI_SIGNATURE_DATA Signatures[][SignatureSize];
182   ///
183 } EFI_SIGNATURE_LIST;
184 
185 
186 /*
187  * The win_certificate structure is part of the PE/COFF specification.
188  */
189 struct win_certificate {
190 	/*
191 	 * The length of the entire certificate, including the length of the
192 	 * header, in bytes.
193 	 */
194 	le32  dw_length;
195 	/*
196 	 * The revision level of the WIN_CERTIFICATE structure. The current
197 	 * revision level is 0x0200.
198 	 */
199 	le16  w_revision;
200 	/*
201 	 * The certificate type. See WIN_CERT_TYPE_xxx for the UEFI certificate
202 	 * types. The UEFI specification reserves the range of certificate type
203 	 * values from 0x0EF0 to 0x0EFF.
204 	 */
205 	le16  w_certificate_type;
206 	/*
207 	 * The following is the actual certificate. The format of
208 	 * the certificate depends on wCertificateType.
209 	 */
210 	/// UINT8 bCertificate[ANYSIZE_ARRAY];
211 } __packed;
212 
213 /*
214  * Certificate which encapsulates a GUID-specific digital signature
215  */
216 struct win_certificate_uefi_guid {
217 	/*
218 	 * This is the standard win_certificate header, where w_certificate_type
219 	 * is set to WIN_CERT_TYPE_EFI_GUID.
220 	 */
221 	struct win_certificate hdr;
222 	/*
223 	 * This is the unique id which determines the format of the cert_data.
224 	 */
225 	uuid_t cert_type;
226 	/*
227 	 * The following is the certificate data. The format of the data is
228 	 * determined by the @cert_type. If @cert_type is
229 	 * EFI_CERT_TYPE_RSA2048_SHA256_GUID, the @cert_data will be
230 	 * EFI_CERT_BLOCK_RSA_2048_SHA256 structure.
231 	 */
232 	u8 cert_data[];
233 } __packed;
234 
235 /*
236  * When the attribute EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS is set,
237  * then the Data buffer shall begin with an instance of a complete (and
238  * serialized) EFI_VARIABLE_AUTHENTICATION_2 descriptor. The descriptor shall be
239  * followed by the new variable value and DataSize shall reflect the combined
240  * size of the descriptor and the new variable value. The authentication
241  * descriptor is not part of the variable data and is not returned by subsequent
242  * calls to GetVariable().
243  */
244 struct efi_variable_authentication_2 {
245 	/*
246 	 * For the TimeStamp value, components Pad1, Nanosecond, TimeZone, Daylight and
247 	 * Pad2 shall be set to 0. This means that the time shall always be expressed in GMT.
248 	 */
249 	struct efi_time timestamp;
250 	/*
251 	 * Only a CertType of  EFI_CERT_TYPE_PKCS7_GUID is accepted.
252 	 */
253 	struct win_certificate_uefi_guid auth_info;
254 } __packed;
255 
256 #endif
257