TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptTs.c
Go to the documentation of this file.
1
10#include "InternalCryptLib.h"
11#include <mbedtls/asn1.h>
12
13//
14// OID ASN.1 Value for SPC_RFC3161_OBJID ("1.3.6.1.4.1.311.3.3.1")
15//
16GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 mSpcRFC3161OidValue[] = {
17 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x03, 0x03, 0x01
18};
19
31BOOLEAN
33 IN UINT8 *Ptr,
34 OUT EFI_TIME *EfiTime
35 )
36{
37 CONST CHAR8 *Str;
38 UINTN Index;
39
40 if ((Ptr == NULL) || (EfiTime == NULL)) {
41 return FALSE;
42 }
43
44 Str = (CONST CHAR8 *)Ptr;
45 SetMem (EfiTime, sizeof (EFI_TIME), 0);
46
47 Index = 0;
48
49 /* four digit year */
50 EfiTime->Year = (Str[Index++] - '0') * 1000;
51 EfiTime->Year += (Str[Index++] - '0') * 100;
52 EfiTime->Year += (Str[Index++] - '0') * 10;
53 EfiTime->Year += (Str[Index++] - '0');
54 if ((EfiTime->Year < 1900) || (EfiTime->Year > 9999)) {
55 return FALSE;
56 }
57
58 EfiTime->Month = (Str[Index++] - '0') * 10;
59 EfiTime->Month += (Str[Index++] - '0');
60 if ((EfiTime->Month < 1) || (EfiTime->Month > 12)) {
61 return FALSE;
62 }
63
64 EfiTime->Day = (Str[Index++] - '0') * 10;
65 EfiTime->Day += (Str[Index++] - '0');
66 if ((EfiTime->Day < 1) || (EfiTime->Day > 31)) {
67 return FALSE;
68 }
69
70 EfiTime->Hour = (Str[Index++] - '0') * 10;
71 EfiTime->Hour += (Str[Index++] - '0');
72 if (EfiTime->Hour > 23) {
73 return FALSE;
74 }
75
76 EfiTime->Minute = (Str[Index++] - '0') * 10;
77 EfiTime->Minute += (Str[Index++] - '0');
78 if (EfiTime->Minute > 59) {
79 return FALSE;
80 }
81
82 EfiTime->Second = (Str[Index++] - '0') * 10;
83 EfiTime->Second += (Str[Index++] - '0');
84 if (EfiTime->Second > 59) {
85 return FALSE;
86 }
87
88 /* Note: we did not adjust the time based on time zone information */
89
90 return TRUE;
91}
92
111BOOLEAN
112EFIAPI
114 IN CONST UINT8 *AuthData,
115 IN UINTN DataSize,
116 IN CONST UINT8 *TsaCert,
117 IN UINTN CertSize,
118 OUT EFI_TIME *SigningTime
119 )
120{
121 UINT8 *Ptr;
122 UINT8 *End;
123 INT32 Len;
124 UINTN ObjLen;
125 UINT8 *TempPtr;
126
127 //
128 // Initializations
129 //
130 if (SigningTime != NULL) {
131 SetMem (SigningTime, sizeof (EFI_TIME), 0);
132 }
133
134 //
135 // Input Parameters Checking.
136 //
137 if ((AuthData == NULL) || (TsaCert == NULL)) {
138 return FALSE;
139 }
140
141 if ((DataSize > INT_MAX) || (CertSize > INT_MAX)) {
142 return FALSE;
143 }
144
145 Ptr = (UINT8 *)(UINTN)AuthData;
146 Len = (UINT32)DataSize;
147 End = Ptr + Len;
148
149 // ContentInfo
150 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
151 return FALSE;
152 }
153
154 // ContentType
155 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_OID) != 0) {
156 return FALSE;
157 }
158
159 Ptr += ObjLen;
160 // content
161 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC) != 0) {
162 return FALSE;
163 }
164
165 End = Ptr + ObjLen;
166 // signedData
167 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
168 return FALSE;
169 }
170
171 // version
172 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_INTEGER) != 0) {
173 return FALSE;
174 }
175
176 Ptr += ObjLen;
177 // digestAlgo
178 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET) != 0) {
179 return FALSE;
180 }
181
182 Ptr += ObjLen;
183
184 // encapContentInfo
185 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
186 return FALSE;
187 }
188
189 Ptr += ObjLen;
190
191 // cert
192 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC) != 0) {
193 return FALSE;
194 }
195
196 Ptr += ObjLen;
197
198 TempPtr = Ptr;
199 // OPTIONAL CRLs
200 if (mbedtls_asn1_get_tag (&TempPtr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC) == 0) {
201 Ptr = TempPtr + ObjLen;
202 }
203
204 // signerInfo
205 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET) != 0) {
206 return FALSE;
207 }
208
209 // sub parse
210 // signerInfo
211 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
212 return FALSE;
213 }
214
215 End = Ptr + ObjLen;
216
217 // version
218 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_INTEGER) != 0) {
219 return FALSE;
220 }
221
222 Ptr += ObjLen;
223
224 // sid
225 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
226 return FALSE;
227 }
228
229 Ptr += ObjLen;
230
231 // digestalgo
232 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
233 return FALSE;
234 }
235
236 Ptr += ObjLen;
237
238 // OPTIONAL AuthenticatedAttributes
239 TempPtr = Ptr;
240 if (mbedtls_asn1_get_tag (&TempPtr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC) == 0) {
241 Ptr = TempPtr + ObjLen;
242 }
243
244 // signaturealgo
245 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
246 return FALSE;
247 }
248
249 Ptr += ObjLen;
250
251 // signature
252 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_OCTET_STRING) != 0) {
253 return FALSE;
254 }
255
256 Ptr += ObjLen;
257
258 // OPTIONAL UnauthenticatedAttributes
259 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, 0xA1) != 0) {
260 return FALSE;
261 }
262
263 // Attribute
264 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
265 return FALSE;
266 }
267
268 // type
269 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_OID) != 0) {
270 return FALSE;
271 }
272
273 if (CompareMem (Ptr, mSpcRFC3161OidValue, sizeof (mSpcRFC3161OidValue)) != 0) {
274 return FALSE;
275 }
276
277 Ptr += ObjLen;
278
279 // values
280 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET) != 0) {
281 return FALSE;
282 }
283
284 // values
285 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
286 return FALSE;
287 }
288
289 // signedData OID
290 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_OID) != 0) {
291 return FALSE;
292 }
293
294 Ptr += ObjLen;
295
296 // [0]
297 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC) != 0) {
298 return FALSE;
299 }
300
301 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
302 return FALSE;
303 }
304
305 // integer
306 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_INTEGER) != 0) {
307 return FALSE;
308 }
309
310 Ptr += ObjLen;
311 // SET
312 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET) != 0) {
313 return FALSE;
314 }
315
316 Ptr += ObjLen;
317
318 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
319 return FALSE;
320 }
321
322 // tST OID
323 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_OID) != 0) {
324 return FALSE;
325 }
326
327 Ptr += ObjLen;
328
329 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC) != 0) {
330 return FALSE;
331 }
332
333 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_OCTET_STRING) != 0) {
334 return FALSE;
335 }
336
337 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
338 return FALSE;
339 }
340
341 // Integer
342 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_INTEGER) != 0) {
343 return FALSE;
344 }
345
346 Ptr += ObjLen;
347 // policy OID
348 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_OID) != 0) {
349 return FALSE;
350 }
351
352 Ptr += ObjLen;
353 // sequence
354 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0) {
355 return FALSE;
356 }
357
358 Ptr += ObjLen;
359 // Integer
360 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_INTEGER) != 0) {
361 return FALSE;
362 }
363
364 Ptr += ObjLen;
365
366 // GeneralizedTime
367 if (mbedtls_asn1_get_tag (&Ptr, End, &ObjLen, MBEDTLS_ASN1_GENERALIZED_TIME) != 0) {
368 return FALSE;
369 }
370
371 //
372 // Retrieve the signing time from TS_TST_INFO structure.
373 //
374 if (SigningTime != NULL) {
375 SetMem (SigningTime, sizeof (EFI_TIME), 0);
376 return ConvertAsn1TimeToEfiTime (Ptr, SigningTime);
377 }
378
379 return TRUE;
380}
UINT64 UINTN
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI SetMem(OUT VOID *Buffer, IN UINTN Length, IN UINT8 Value)
Definition: SetMemWrapper.c:38
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define GLOBAL_REMOVE_IF_UNREFERENCED
Definition: Base.h:48
STATIC BOOLEAN ConvertAsn1TimeToEfiTime(IN ASN1_TIME *Asn1Time, OUT EFI_TIME *EfiTime)
Definition: CryptTs.c:162
BOOLEAN EFIAPI ImageTimestampVerify(IN CONST UINT8 *AuthData, IN UINTN DataSize, IN CONST UINT8 *TsaCert, IN UINTN CertSize, OUT EFI_TIME *SigningTime)
Definition: CryptTs.c:555