]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Pk/CryptTs.c
CryptoPkg: Update PK Cipher Wrappers work with opaque objects.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptTs.c
CommitLineData
2ac68e8b
QL
1/** @file\r
2 RFC3161 Timestamp Countersignature Verification over OpenSSL.\r
3 The timestamp is generated by a TimeStamping Authority (TSA) and asserts that a\r
4 publisher's signature existed before the specified time. The timestamp extends\r
5 the lifetime of the signature when a signing certificate expires or is later\r
6 revoked.\r
7\r
f56b11d2 8Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.<BR>\r
2ac68e8b
QL
9This program and the accompanying materials\r
10are licensed and made available under the terms and conditions of the BSD License\r
11which accompanies this distribution. The full text of the license may be found at\r
12http://opensource.org/licenses/bsd-license.php\r
13\r
14THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18\r
19#include "InternalCryptLib.h"\r
20\r
21#include <openssl/asn1.h>\r
22#include <openssl/asn1t.h>\r
23#include <openssl/x509.h>\r
24#include <openssl/x509v3.h>\r
25#include <openssl/pkcs7.h>\r
26\r
27//\r
28// OID ASN.1 Value for SPC_RFC3161_OBJID ("1.3.6.1.4.1.311.3.3.1")\r
29//\r
30UINT8 mSpcRFC3161OidValue[] = {\r
31 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x03, 0x03, 0x01\r
32 };\r
33\r
34///\r
35/// The messageImprint field SHOULD contain the hash of the datum to be\r
36/// time-stamped. The hash is represented as an OCTET STRING. Its\r
37/// length MUST match the length of the hash value for that algorithm\r
38/// (e.g., 20 bytes for SHA-1 or 16 bytes for MD5).\r
39///\r
40/// MessageImprint ::= SEQUENCE {\r
41/// hashAlgorithm AlgorithmIdentifier,\r
42/// hashedMessage OCTET STRING }\r
43///\r
44typedef struct {\r
45 X509_ALGOR *HashAlgorithm;\r
46 ASN1_OCTET_STRING *HashedMessage;\r
47} TS_MESSAGE_IMPRINT;\r
48\r
49//\r
50// ASN.1 Functions for TS_MESSAGE_IMPRINT\r
51//\r
52DECLARE_ASN1_FUNCTIONS (TS_MESSAGE_IMPRINT)\r
53ASN1_SEQUENCE (TS_MESSAGE_IMPRINT) = {\r
54 ASN1_SIMPLE (TS_MESSAGE_IMPRINT, HashAlgorithm, X509_ALGOR),\r
55 ASN1_SIMPLE (TS_MESSAGE_IMPRINT, HashedMessage, ASN1_OCTET_STRING)\r
56} ASN1_SEQUENCE_END (TS_MESSAGE_IMPRINT)\r
57IMPLEMENT_ASN1_FUNCTIONS (TS_MESSAGE_IMPRINT)\r
58\r
59///\r
60/// Accuracy represents the time deviation around the UTC time contained\r
61/// in GeneralizedTime of time-stamp token.\r
62///\r
63/// Accuracy ::= SEQUENCE {\r
64/// seconds INTEGER OPTIONAL,\r
65/// millis [0] INTEGER (1..999) OPTIONAL,\r
66/// micros [1] INTEGER (1..999) OPTIONAL }\r
67///\r
68typedef struct {\r
69 ASN1_INTEGER *Seconds;\r
70 ASN1_INTEGER *Millis;\r
71 ASN1_INTEGER *Micros;\r
72} TS_ACCURACY;\r
73\r
74//\r
75// ASN.1 Functions for TS_ACCURACY\r
76//\r
77DECLARE_ASN1_FUNCTIONS (TS_ACCURACY)\r
78ASN1_SEQUENCE (TS_ACCURACY) = {\r
79 ASN1_OPT (TS_ACCURACY, Seconds, ASN1_INTEGER),\r
80 ASN1_IMP_OPT (TS_ACCURACY, Millis, ASN1_INTEGER, 0),\r
81 ASN1_IMP_OPT (TS_ACCURACY, Micros, ASN1_INTEGER, 1)\r
82} ASN1_SEQUENCE_END (TS_ACCURACY)\r
83IMPLEMENT_ASN1_FUNCTIONS (TS_ACCURACY)\r
84\r
85///\r
86/// The timestamp token info resulting from a successful timestamp request,\r
87/// as defined in RFC 3161.\r
88///\r
89/// TSTInfo ::= SEQUENCE {\r
90/// version INTEGER { v1(1) },\r
91/// policy TSAPolicyId,\r
92/// messageImprint MessageImprint,\r
93/// -- MUST have the same value as the similar field in\r
94/// -- TimeStampReq\r
95/// serialNumber INTEGER,\r
96/// -- Time-Stamping users MUST be ready to accommodate integers\r
97/// -- up to 160 bits.\r
98/// genTime GeneralizedTime,\r
99/// accuracy Accuracy OPTIONAL,\r
100/// ordering BOOLEAN DEFAULT FALSE,\r
101/// nonce INTEGER OPTIONAL,\r
102/// -- MUST be present if the similar field was present\r
103/// -- in TimeStampReq. In that case it MUST have the same value.\r
104/// tsa [0] GeneralName OPTIONAL,\r
105/// extensions [1] IMPLICIT Extensions OPTIONAL }\r
106///\r
107typedef struct {\r
108 ASN1_INTEGER *Version;\r
109 ASN1_OBJECT *Policy;\r
110 TS_MESSAGE_IMPRINT *MessageImprint;\r
111 ASN1_INTEGER *SerialNumber;\r
112 ASN1_GENERALIZEDTIME *GenTime;\r
113 TS_ACCURACY *Accuracy;\r
114 ASN1_BOOLEAN Ordering;\r
115 ASN1_INTEGER *Nonce;\r
116 GENERAL_NAME *Tsa;\r
117 STACK_OF(X509_EXTENSION) *Extensions;\r
118} TS_TST_INFO;\r
119\r
120//\r
121// ASN.1 Functions for TS_TST_INFO\r
122//\r
123DECLARE_ASN1_FUNCTIONS (TS_TST_INFO)\r
124ASN1_SEQUENCE (TS_TST_INFO) = {\r
125 ASN1_SIMPLE (TS_TST_INFO, Version, ASN1_INTEGER),\r
126 ASN1_SIMPLE (TS_TST_INFO, Policy, ASN1_OBJECT),\r
127 ASN1_SIMPLE (TS_TST_INFO, MessageImprint, TS_MESSAGE_IMPRINT),\r
128 ASN1_SIMPLE (TS_TST_INFO, SerialNumber, ASN1_INTEGER),\r
129 ASN1_SIMPLE (TS_TST_INFO, GenTime, ASN1_GENERALIZEDTIME),\r
130 ASN1_OPT (TS_TST_INFO, Accuracy, TS_ACCURACY),\r
131 ASN1_OPT (TS_TST_INFO, Ordering, ASN1_FBOOLEAN),\r
132 ASN1_OPT (TS_TST_INFO, Nonce, ASN1_INTEGER),\r
133 ASN1_EXP_OPT(TS_TST_INFO, Tsa, GENERAL_NAME, 0),\r
134 ASN1_IMP_SEQUENCE_OF_OPT (TS_TST_INFO, Extensions, X509_EXTENSION, 1)\r
135} ASN1_SEQUENCE_END (TS_TST_INFO)\r
136IMPLEMENT_ASN1_FUNCTIONS (TS_TST_INFO)\r
137\r
138\r
2ac68e8b
QL
139/**\r
140 Convert ASN.1 GeneralizedTime to EFI Time.\r
141\r
142 @param[in] Asn1Time Pointer to the ASN.1 GeneralizedTime to be converted.\r
143 @param[out] SigningTime Return the corresponding EFI Time.\r
144\r
145 @retval TRUE The time convertion succeeds.\r
146 @retval FALSE Invalid parameters.\r
147\r
148**/\r
149BOOLEAN\r
150EFIAPI\r
151ConvertAsn1TimeToEfiTime (\r
152 IN ASN1_TIME *Asn1Time,\r
153 OUT EFI_TIME *EfiTime\r
154 )\r
155{\r
156 CONST CHAR8 *Str;\r
157 UINTN Index;\r
158\r
159 if ((Asn1Time == NULL) || (EfiTime == NULL)) {\r
160 return FALSE;\r
161 }\r
162\r
163 Str = (CONST CHAR8*)Asn1Time->data;\r
164 SetMem (EfiTime, 0, sizeof (EFI_TIME));\r
165\r
166 Index = 0;\r
167 if (Asn1Time->type == V_ASN1_UTCTIME) { /* two digit year */\r
168 EfiTime->Year = (Str[Index++] - '0') * 10;\r
169 EfiTime->Year += (Str[Index++] - '0');\r
170 if (EfiTime->Year < 70) {\r
171 EfiTime->Year += 100;\r
172 }\r
173 } else if (Asn1Time->type == V_ASN1_GENERALIZEDTIME) { /* four digit year */\r
174 EfiTime->Year = (Str[Index++] - '0') * 1000;\r
175 EfiTime->Year += (Str[Index++] - '0') * 100;\r
176 EfiTime->Year += (Str[Index++] - '0') * 10;\r
177 EfiTime->Year += (Str[Index++] - '0');\r
178 if ((EfiTime->Year < 1900) || (EfiTime->Year > 9999)) {\r
179 return FALSE;\r
180 }\r
181 }\r
182\r
183 EfiTime->Month = (Str[Index++] - '0') * 10;\r
184 EfiTime->Month += (Str[Index++] - '0');\r
185 if ((EfiTime->Month < 1) || (EfiTime->Month > 12)) {\r
186 return FALSE;\r
187 }\r
188\r
189 EfiTime->Day = (Str[Index++] - '0') * 10;\r
190 EfiTime->Day += (Str[Index++] - '0');\r
191 if ((EfiTime->Day < 1) || (EfiTime->Day > 31)) {\r
192 return FALSE;\r
193 }\r
194\r
195 EfiTime->Hour = (Str[Index++] - '0') * 10;\r
196 EfiTime->Hour += (Str[Index++] - '0');\r
197 if (EfiTime->Hour > 23) {\r
198 return FALSE;\r
199 }\r
200\r
201 EfiTime->Minute = (Str[Index++] - '0') * 10;\r
202 EfiTime->Minute += (Str[Index++] - '0');\r
203 if (EfiTime->Minute > 59) {\r
204 return FALSE;\r
205 }\r
206\r
207 EfiTime->Second = (Str[Index++] - '0') * 10;\r
208 EfiTime->Second += (Str[Index++] - '0');\r
209 if (EfiTime->Second > 59) {\r
210 return FALSE;\r
211 }\r
212\r
213 /* Note: we did not adjust the time based on time zone information */\r
214\r
215 return TRUE;\r
216}\r
217\r
218/**\r
219\r
220 Check the validity of TimeStamp Token Information.\r
221\r
222 @param[in] TstInfo Pointer to the TS_TST_INFO structure.\r
223 @param[in] TimestampedData Pointer to the data to be time-stamped.\r
224 @param[in] DataSize Size of timestamped data in bytes.\r
225\r
226 @retval TRUE The TimeStamp Token Information is valid.\r
227 @retval FALSE Invalid TimeStamp Token Information.\r
228\r
229**/\r
230BOOLEAN\r
231EFIAPI\r
232CheckTSTInfo (\r
233 IN CONST TS_TST_INFO *TstInfo,\r
234 IN CONST UINT8 *TimestampedData,\r
235 IN UINTN DataSize\r
236 )\r
237{\r
238 BOOLEAN Status;\r
239 TS_MESSAGE_IMPRINT *Imprint;\r
240 X509_ALGOR *HashAlgo;\r
241 CONST EVP_MD *Md;\r
f56b11d2 242 EVP_MD_CTX *MdCtx;\r
2ac68e8b
QL
243 UINTN MdSize;\r
244 UINT8 *HashedMsg;\r
245\r
246 //\r
247 // Initialization\r
248 //\r
249 Status = FALSE;\r
250 HashAlgo = NULL;\r
251 HashedMsg = NULL;\r
f56b11d2 252 MdCtx = NULL;\r
2ac68e8b
QL
253\r
254 //\r
255 // -- Check version number of Timestamp:\r
256 // The version field (currently v1) describes the version of the time-stamp token.\r
257 // Conforming time-stamping servers MUST be able to provide version 1 time-stamp tokens.\r
258 //\r
259 if ((ASN1_INTEGER_get (TstInfo->Version)) != 1) {\r
260 return FALSE;\r
261 }\r
262\r
263 //\r
264 // -- Check Policies\r
265 // The policy field MUST indicate the TSA's policy under which the response was produced.\r
266 //\r
267 if (TstInfo->Policy == NULL) {\r
268 /// NOTE: Need to check if the requested and returned policies.\r
269 /// We have no information about the Requested TSA Policy.\r
270 return FALSE;\r
271 }\r
272\r
273 //\r
274 // -- Compute & Check Message Imprint\r
275 //\r
276 Imprint = TstInfo->MessageImprint;\r
277 HashAlgo = X509_ALGOR_dup (Imprint->HashAlgorithm);\r
278\r
279 Md = EVP_get_digestbyobj (HashAlgo->algorithm);\r
280 if (Md == NULL) {\r
281 goto _Exit;\r
282 }\r
283\r
284 MdSize = EVP_MD_size (Md);\r
285 HashedMsg = AllocateZeroPool (MdSize);\r
286 if (HashedMsg == NULL) {\r
287 goto _Exit;\r
288 }\r
f56b11d2
QL
289 MdCtx = EVP_MD_CTX_new ();\r
290 if (MdCtx == NULL) {\r
291 goto _Exit;\r
292 }\r
293 if ((EVP_DigestInit_ex (MdCtx, Md, NULL) != 1) ||\r
294 (EVP_DigestUpdate (MdCtx, TimestampedData, DataSize) != 1) ||\r
295 (EVP_DigestFinal (MdCtx, HashedMsg, NULL) != 1)) {\r
296 goto _Exit;\r
297 }\r
2ac68e8b 298 if ((MdSize == (UINTN)ASN1_STRING_length (Imprint->HashedMessage)) &&\r
f56b11d2 299 (CompareMem (HashedMsg, ASN1_STRING_get0_data (Imprint->HashedMessage), MdSize) != 0)) {\r
2ac68e8b
QL
300 goto _Exit;\r
301 }\r
302\r
303 //\r
304 // -- Check Nonces\r
305 //\r
306 if (TstInfo->Nonce != NULL) {\r
307 //\r
308 // Nonces is optional, No error if no nonce is returned;\r
309 //\r
310 }\r
311\r
312 //\r
313 // -- Check if the TSA name and signer certificate is matched.\r
314 //\r
315 if (TstInfo->Tsa != NULL) {\r
316 //\r
317 // Ignored the optional Tsa field checking.\r
318 //\r
319 }\r
320\r
321 Status = TRUE;\r
322\r
323_Exit:\r
324 X509_ALGOR_free (HashAlgo);\r
f56b11d2 325 EVP_MD_CTX_free (MdCtx);\r
2ac68e8b
QL
326 if (HashedMsg != NULL) {\r
327 FreePool (HashedMsg);\r
328 }\r
329\r
330 return Status;\r
331}\r
332\r
333/**\r
2998af86 334 Verifies the validity of a TimeStamp Token as described in RFC 3161 ("Internet\r
2ac68e8b
QL
335 X.509 Public Key Infrastructure Time-Stamp Protocol (TSP)").\r
336\r
337 If TSToken is NULL, then return FALSE.\r
338 If TimestampedData is NULL, then return FALSE.\r
339\r
340 @param[in] TSToken Pointer to the RFC3161 TimeStamp Token, which is generated\r
341 by a TSA and located in the software publisher's SignerInfo\r
342 structure.\r
343 @param[in] TokenSize Size of the TimeStamp Token in bytes.\r
344 @param[in] TsaCert Pointer to a trusted/root TSA certificate encoded in DER.\r
345 @param[in] CertSize Size of the trusted TSA certificate in bytes.\r
346 @param[in] TimestampedData Pointer to the data to be time-stamped.\r
347 @param[in] DataSize Size of timestamped data in bytes.\r
348 @param[out] SigningTime Return the time of timestamp generation time if the timestamp\r
349 signature is valid.\r
350\r
351 @retval TRUE The specified timestamp token is valid.\r
352 @retval FALSE Invalid timestamp token.\r
353\r
354**/\r
355BOOLEAN\r
356EFIAPI\r
357TimestampTokenVerify (\r
358 IN CONST UINT8 *TSToken,\r
359 IN UINTN TokenSize,\r
360 IN CONST UINT8 *TsaCert,\r
361 IN UINTN CertSize,\r
362 IN CONST UINT8 *TimestampedData,\r
363 IN UINTN DataSize,\r
364 OUT EFI_TIME *SigningTime\r
365 )\r
366{\r
367 BOOLEAN Status;\r
368 CONST UINT8 *TokenTemp;\r
369 PKCS7 *Pkcs7;\r
370 X509 *Cert;\r
1463ce18 371 CONST UINT8 *CertTemp;\r
2ac68e8b
QL
372 X509_STORE *CertStore;\r
373 BIO *OutBio;\r
374 UINT8 *TstData;\r
375 UINTN TstSize;\r
1463ce18 376 CONST UINT8 *TstTemp;\r
2ac68e8b
QL
377 TS_TST_INFO *TstInfo;\r
378\r
379 Status = FALSE;\r
380\r
381 //\r
382 // Check input parameters\r
383 //\r
384 if ((TSToken == NULL) || (TsaCert == NULL) || (TimestampedData == NULL) ||\r
385 (TokenSize > INT_MAX) || (CertSize > INT_MAX) || (DataSize > INT_MAX)) {\r
386 return FALSE;\r
387 }\r
388\r
389 //\r
390 // Initializations\r
391 //\r
392 if (SigningTime != NULL) {\r
393 SetMem (SigningTime, sizeof (EFI_TIME), 0);\r
394 }\r
395 Pkcs7 = NULL;\r
396 Cert = NULL;\r
397 CertStore = NULL;\r
398 OutBio = NULL;\r
399 TstData = NULL;\r
400 TstInfo = NULL;\r
401\r
402 //\r
403 // TimeStamp Token should contain one valid DER-encoded ASN.1 PKCS#7 structure.\r
404 //\r
405 TokenTemp = TSToken;\r
406 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &TokenTemp, (int) TokenSize);\r
407 if (Pkcs7 == NULL) {\r
408 goto _Exit;\r
409 }\r
410\r
411 //\r
412 // The timestamp signature (TSA's response) will be one PKCS#7 signed data.\r
413 //\r
414 if (!PKCS7_type_is_signed (Pkcs7)) {\r
415 goto _Exit;\r
416 }\r
417\r
418 //\r
419 // Read the trusted TSA certificate (DER-encoded), and Construct X509 Certificate.\r
420 //\r
1463ce18
QL
421 CertTemp = TsaCert;\r
422 Cert = d2i_X509 (NULL, &CertTemp, (long) CertSize);\r
2ac68e8b
QL
423 if (Cert == NULL) {\r
424 goto _Exit;\r
425 }\r
426\r
427 //\r
428 // Setup X509 Store for trusted certificate.\r
429 //\r
430 CertStore = X509_STORE_new ();\r
431 if ((CertStore == NULL) || !(X509_STORE_add_cert (CertStore, Cert))) {\r
432 goto _Exit;\r
433 }\r
434\r
435 //\r
68547181 436 // Allow partial certificate chains, terminated by a non-self-signed but\r
de0408be 437 // still trusted intermediate certificate. Also disable time checks.\r
2ac68e8b 438 //\r
de0408be
DW
439 X509_STORE_set_flags (CertStore,\r
440 X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME);\r
2ac68e8b
QL
441\r
442 X509_STORE_set_purpose (CertStore, X509_PURPOSE_ANY);\r
443\r
444 //\r
445 // Verifies the PKCS#7 signedData structure, and output the signed contents.\r
446 //\r
447 OutBio = BIO_new (BIO_s_mem ());\r
448 if (OutBio == NULL) {\r
449 goto _Exit;\r
450 }\r
451 if (!PKCS7_verify (Pkcs7, NULL, CertStore, NULL, OutBio, PKCS7_BINARY)) {\r
452 goto _Exit;\r
453 }\r
454\r
455 //\r
456 // Read the signed contents detached in timestamp signature.\r
457 //\r
458 TstData = AllocateZeroPool (2048);\r
459 if (TstData == NULL) {\r
460 goto _Exit;\r
461 }\r
462 TstSize = BIO_read (OutBio, (void *) TstData, 2048);\r
463\r
464 //\r
465 // Construct TS_TST_INFO structure from the signed contents.\r
466 //\r
467 TstTemp = TstData;\r
017c285e
LE
468 TstInfo = d2i_TS_TST_INFO (NULL, (const unsigned char **) &TstTemp,\r
469 (int)TstSize);\r
2ac68e8b
QL
470 if (TstInfo == NULL) {\r
471 goto _Exit;\r
472 }\r
473\r
474 //\r
475 // Check TS_TST_INFO structure.\r
476 //\r
477 Status = CheckTSTInfo (TstInfo, TimestampedData, DataSize);\r
478 if (!Status) {\r
479 goto _Exit;\r
480 }\r
481\r
482 //\r
483 // Retrieve the signing time from TS_TST_INFO structure.\r
484 //\r
485 if (SigningTime != NULL) {\r
486 SetMem (SigningTime, sizeof (EFI_TIME), 0);\r
487 Status = ConvertAsn1TimeToEfiTime (TstInfo->GenTime, SigningTime);\r
488 }\r
489\r
490_Exit:\r
491 //\r
492 // Release Resources\r
493 //\r
494 PKCS7_free (Pkcs7);\r
495 X509_free (Cert);\r
496 X509_STORE_free (CertStore);\r
497 BIO_free (OutBio);\r
498 TS_TST_INFO_free (TstInfo);\r
499\r
500 if (TstData != NULL) {\r
501 FreePool (TstData);\r
502 }\r
503\r
504 return Status;\r
505}\r
506\r
507/**\r
2998af86 508 Verifies the validity of a RFC3161 Timestamp CounterSignature embedded in PE/COFF Authenticode\r
2ac68e8b
QL
509 signature.\r
510\r
511 If AuthData is NULL, then return FALSE.\r
512\r
513 @param[in] AuthData Pointer to the Authenticode Signature retrieved from signed\r
514 PE/COFF image to be verified.\r
515 @param[in] DataSize Size of the Authenticode Signature in bytes.\r
516 @param[in] TsaCert Pointer to a trusted/root TSA certificate encoded in DER, which\r
517 is used for TSA certificate chain verification.\r
518 @param[in] CertSize Size of the trusted certificate in bytes.\r
519 @param[out] SigningTime Return the time of timestamp generation time if the timestamp\r
520 signature is valid.\r
521\r
522 @retval TRUE The specified Authenticode includes a valid RFC3161 Timestamp CounterSignature.\r
523 @retval FALSE No valid RFC3161 Timestamp CounterSignature in the specified Authenticode data.\r
524\r
525**/\r
526BOOLEAN\r
527EFIAPI\r
528ImageTimestampVerify (\r
529 IN CONST UINT8 *AuthData,\r
530 IN UINTN DataSize,\r
531 IN CONST UINT8 *TsaCert,\r
532 IN UINTN CertSize,\r
533 OUT EFI_TIME *SigningTime\r
534 )\r
535{\r
536 BOOLEAN Status;\r
537 PKCS7 *Pkcs7;\r
1463ce18 538 CONST UINT8 *Temp;\r
2ac68e8b
QL
539 STACK_OF(PKCS7_SIGNER_INFO) *SignerInfos;\r
540 PKCS7_SIGNER_INFO *SignInfo;\r
541 UINTN Index;\r
542 STACK_OF(X509_ATTRIBUTE) *Sk;\r
543 X509_ATTRIBUTE *Xa;\r
4ffe0fac 544 ASN1_OBJECT *XaObj;\r
2ac68e8b
QL
545 ASN1_TYPE *Asn1Type;\r
546 ASN1_OCTET_STRING *EncDigest;\r
547 UINT8 *TSToken;\r
548 UINTN TokenSize;\r
549\r
550 //\r
551 // Input Parameters Checking.\r
552 //\r
553 if ((AuthData == NULL) || (TsaCert == NULL)) {\r
554 return FALSE;\r
555 }\r
556\r
557 if ((DataSize > INT_MAX) || (CertSize > INT_MAX)) {\r
558 return FALSE;\r
559 }\r
560\r
561 //\r
562 // Register & Initialize necessary digest algorithms for PKCS#7 Handling.\r
563 //\r
564 if ((EVP_add_digest (EVP_md5 ()) == 0) || (EVP_add_digest (EVP_sha1 ()) == 0) ||\r
565 (EVP_add_digest (EVP_sha256 ()) == 0) || (EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA)) == 0) {\r
566 return FALSE;\r
567 }\r
568\r
569 //\r
570 // Initialization.\r
571 //\r
572 Status = FALSE;\r
573 Pkcs7 = NULL;\r
574 SignInfo = NULL;\r
575\r
576 //\r
577 // Decode ASN.1-encoded Authenticode data into PKCS7 structure.\r
578 //\r
1463ce18
QL
579 Temp = AuthData;\r
580 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) DataSize);\r
2ac68e8b
QL
581 if (Pkcs7 == NULL) {\r
582 goto _Exit;\r
583 }\r
584\r
585 //\r
586 // Check if there is one and only one signer.\r
587 //\r
588 SignerInfos = PKCS7_get_signer_info (Pkcs7);\r
589 if (!SignerInfos || (sk_PKCS7_SIGNER_INFO_num (SignerInfos) != 1)) {\r
590 goto _Exit;\r
591 }\r
592\r
593 //\r
594 // Locate the TimeStamp CounterSignature.\r
595 //\r
596 SignInfo = sk_PKCS7_SIGNER_INFO_value (SignerInfos, 0);\r
597 if (SignInfo == NULL) {\r
598 goto _Exit;\r
599 }\r
600\r
601 //\r
602 // Locate Message Digest which will be the data to be time-stamped.\r
603 //\r
604 EncDigest = SignInfo->enc_digest;\r
605 if (EncDigest == NULL) {\r
606 goto _Exit;\r
607 }\r
608\r
609 //\r
610 // The RFC3161 timestamp counterSignature is contained in unauthenticatedAttributes field\r
611 // of SignerInfo.\r
612 //\r
613 Sk = SignInfo->unauth_attr;\r
614 if (Sk == NULL) { // No timestamp counterSignature.\r
615 goto _Exit;\r
616 }\r
617\r
618 Asn1Type = NULL;\r
619 for (Index = 0; Index < (UINTN) sk_X509_ATTRIBUTE_num (Sk); Index++) {\r
620 //\r
621 // Search valid RFC3161 timestamp counterSignature based on OBJID.\r
622 //\r
623 Xa = sk_X509_ATTRIBUTE_value (Sk, (int)Index);\r
4ffe0fac 624 if (Xa == NULL) {\r
2ac68e8b
QL
625 continue;\r
626 }\r
4ffe0fac
DW
627 XaObj = X509_ATTRIBUTE_get0_object(Xa);\r
628 if (XaObj == NULL) {\r
629 continue;\r
630 }\r
338bfd97
DW
631 if ((OBJ_length(XaObj) != sizeof (mSpcRFC3161OidValue)) ||\r
632 (CompareMem (OBJ_get0_data(XaObj), mSpcRFC3161OidValue, sizeof (mSpcRFC3161OidValue)) != 0)) {\r
4ffe0fac
DW
633 continue;\r
634 }\r
635 Asn1Type = X509_ATTRIBUTE_get0_type(Xa, 0);\r
2ac68e8b
QL
636 }\r
637\r
638 if (Asn1Type == NULL) {\r
639 Status = FALSE;\r
640 goto _Exit;\r
641 }\r
642 TSToken = Asn1Type->value.octet_string->data;\r
643 TokenSize = Asn1Type->value.octet_string->length;\r
644\r
645 //\r
646 // TimeStamp counterSignature (Token) verification.\r
647 //\r
648 Status = TimestampTokenVerify (\r
649 TSToken,\r
650 TokenSize,\r
651 TsaCert,\r
652 CertSize,\r
653 EncDigest->data,\r
654 EncDigest->length,\r
655 SigningTime\r
656 );\r
657\r
658_Exit:\r
659 //\r
660 // Release Resources\r
661 //\r
662 PKCS7_free (Pkcs7);\r
663\r
664 return Status;\r
665}\r