]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
CryptoPkg: Apply uncrustify changes
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptX509.c
... / ...
CommitLineData
1/** @file\r
2 X.509 Certificate Handler Wrapper Implementation over OpenSSL.\r
3\r
4Copyright (c) 2010 - 2020, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include "InternalCryptLib.h"\r
10#include <openssl/x509.h>\r
11#include <openssl/rsa.h>\r
12\r
13/**\r
14 Construct a X509 object from DER-encoded certificate data.\r
15\r
16 If Cert is NULL, then return FALSE.\r
17 If SingleX509Cert is NULL, then return FALSE.\r
18\r
19 @param[in] Cert Pointer to the DER-encoded certificate data.\r
20 @param[in] CertSize The size of certificate data in bytes.\r
21 @param[out] SingleX509Cert The generated X509 object.\r
22\r
23 @retval TRUE The X509 object generation succeeded.\r
24 @retval FALSE The operation failed.\r
25\r
26**/\r
27BOOLEAN\r
28EFIAPI\r
29X509ConstructCertificate (\r
30 IN CONST UINT8 *Cert,\r
31 IN UINTN CertSize,\r
32 OUT UINT8 **SingleX509Cert\r
33 )\r
34{\r
35 X509 *X509Cert;\r
36 CONST UINT8 *Temp;\r
37\r
38 //\r
39 // Check input parameters.\r
40 //\r
41 if ((Cert == NULL) || (SingleX509Cert == NULL) || (CertSize > INT_MAX)) {\r
42 return FALSE;\r
43 }\r
44\r
45 //\r
46 // Read DER-encoded X509 Certificate and Construct X509 object.\r
47 //\r
48 Temp = Cert;\r
49 X509Cert = d2i_X509 (NULL, &Temp, (long)CertSize);\r
50 if (X509Cert == NULL) {\r
51 return FALSE;\r
52 }\r
53\r
54 *SingleX509Cert = (UINT8 *)X509Cert;\r
55\r
56 return TRUE;\r
57}\r
58\r
59/**\r
60 Construct a X509 stack object from a list of DER-encoded certificate data.\r
61\r
62 If X509Stack is NULL, then return FALSE.\r
63 If this interface is not supported, then return FALSE.\r
64\r
65 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.\r
66 On output, pointer to the X509 stack object with new\r
67 inserted X509 certificate.\r
68 @param[in] Args VA_LIST marker for the variable argument list.\r
69 A list of DER-encoded single certificate data followed\r
70 by certificate size. A NULL terminates the list. The\r
71 pairs are the arguments to X509ConstructCertificate().\r
72\r
73 @retval TRUE The X509 stack construction succeeded.\r
74 @retval FALSE The construction operation failed.\r
75 @retval FALSE This interface is not supported.\r
76\r
77**/\r
78BOOLEAN\r
79EFIAPI\r
80X509ConstructCertificateStackV (\r
81 IN OUT UINT8 **X509Stack,\r
82 IN VA_LIST Args\r
83 )\r
84{\r
85 UINT8 *Cert;\r
86 UINTN CertSize;\r
87 X509 *X509Cert;\r
88\r
89 STACK_OF (X509) *CertStack;\r
90 BOOLEAN Status;\r
91 UINTN Index;\r
92\r
93 //\r
94 // Check input parameters.\r
95 //\r
96 if (X509Stack == NULL) {\r
97 return FALSE;\r
98 }\r
99\r
100 Status = FALSE;\r
101\r
102 //\r
103 // Initialize X509 stack object.\r
104 //\r
105 CertStack = (STACK_OF (X509) *)(*X509Stack);\r
106 if (CertStack == NULL) {\r
107 CertStack = sk_X509_new_null ();\r
108 if (CertStack == NULL) {\r
109 return Status;\r
110 }\r
111 }\r
112\r
113 for (Index = 0; ; Index++) {\r
114 //\r
115 // If Cert is NULL, then it is the end of the list.\r
116 //\r
117 Cert = VA_ARG (Args, UINT8 *);\r
118 if (Cert == NULL) {\r
119 break;\r
120 }\r
121\r
122 CertSize = VA_ARG (Args, UINTN);\r
123 if (CertSize == 0) {\r
124 break;\r
125 }\r
126\r
127 //\r
128 // Construct X509 Object from the given DER-encoded certificate data.\r
129 //\r
130 X509Cert = NULL;\r
131 Status = X509ConstructCertificate (\r
132 (CONST UINT8 *)Cert,\r
133 CertSize,\r
134 (UINT8 **)&X509Cert\r
135 );\r
136 if (!Status) {\r
137 if (X509Cert != NULL) {\r
138 X509_free (X509Cert);\r
139 }\r
140\r
141 break;\r
142 }\r
143\r
144 //\r
145 // Insert the new X509 object into X509 stack object.\r
146 //\r
147 sk_X509_push (CertStack, X509Cert);\r
148 }\r
149\r
150 if (!Status) {\r
151 sk_X509_pop_free (CertStack, X509_free);\r
152 } else {\r
153 *X509Stack = (UINT8 *)CertStack;\r
154 }\r
155\r
156 return Status;\r
157}\r
158\r
159/**\r
160 Construct a X509 stack object from a list of DER-encoded certificate data.\r
161\r
162 If X509Stack is NULL, then return FALSE.\r
163\r
164 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.\r
165 On output, pointer to the X509 stack object with new\r
166 inserted X509 certificate.\r
167 @param ... A list of DER-encoded single certificate data followed\r
168 by certificate size. A NULL terminates the list. The\r
169 pairs are the arguments to X509ConstructCertificate().\r
170\r
171 @retval TRUE The X509 stack construction succeeded.\r
172 @retval FALSE The construction operation failed.\r
173\r
174**/\r
175BOOLEAN\r
176EFIAPI\r
177X509ConstructCertificateStack (\r
178 IN OUT UINT8 **X509Stack,\r
179 ...\r
180 )\r
181{\r
182 VA_LIST Args;\r
183 BOOLEAN Result;\r
184\r
185 VA_START (Args, X509Stack);\r
186 Result = X509ConstructCertificateStackV (X509Stack, Args);\r
187 VA_END (Args);\r
188 return Result;\r
189}\r
190\r
191/**\r
192 Release the specified X509 object.\r
193\r
194 If X509Cert is NULL, then return FALSE.\r
195\r
196 @param[in] X509Cert Pointer to the X509 object to be released.\r
197\r
198**/\r
199VOID\r
200EFIAPI\r
201X509Free (\r
202 IN VOID *X509Cert\r
203 )\r
204{\r
205 //\r
206 // Check input parameters.\r
207 //\r
208 if (X509Cert == NULL) {\r
209 return;\r
210 }\r
211\r
212 //\r
213 // Free OpenSSL X509 object.\r
214 //\r
215 X509_free ((X509 *)X509Cert);\r
216}\r
217\r
218/**\r
219 Release the specified X509 stack object.\r
220\r
221 If X509Stack is NULL, then return FALSE.\r
222\r
223 @param[in] X509Stack Pointer to the X509 stack object to be released.\r
224\r
225**/\r
226VOID\r
227EFIAPI\r
228X509StackFree (\r
229 IN VOID *X509Stack\r
230 )\r
231{\r
232 //\r
233 // Check input parameters.\r
234 //\r
235 if (X509Stack == NULL) {\r
236 return;\r
237 }\r
238\r
239 //\r
240 // Free OpenSSL X509 stack object.\r
241 //\r
242 sk_X509_pop_free ((STACK_OF (X509) *) X509Stack, X509_free);\r
243}\r
244\r
245/**\r
246 Retrieve the subject bytes from one X.509 certificate.\r
247\r
248 @param[in] Cert Pointer to the DER-encoded X509 certificate.\r
249 @param[in] CertSize Size of the X509 certificate in bytes.\r
250 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.\r
251 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,\r
252 and the size of buffer returned CertSubject on output.\r
253\r
254 If Cert is NULL, then return FALSE.\r
255 If SubjectSize is NULL, then return FALSE.\r
256\r
257 @retval TRUE The certificate subject retrieved successfully.\r
258 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.\r
259 The SubjectSize will be updated with the required size.\r
260\r
261**/\r
262BOOLEAN\r
263EFIAPI\r
264X509GetSubjectName (\r
265 IN CONST UINT8 *Cert,\r
266 IN UINTN CertSize,\r
267 OUT UINT8 *CertSubject,\r
268 IN OUT UINTN *SubjectSize\r
269 )\r
270{\r
271 BOOLEAN Status;\r
272 X509 *X509Cert;\r
273 X509_NAME *X509Name;\r
274 UINTN X509NameSize;\r
275\r
276 //\r
277 // Check input parameters.\r
278 //\r
279 if ((Cert == NULL) || (SubjectSize == NULL)) {\r
280 return FALSE;\r
281 }\r
282\r
283 X509Cert = NULL;\r
284\r
285 //\r
286 // Read DER-encoded X509 Certificate and Construct X509 object.\r
287 //\r
288 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);\r
289 if ((X509Cert == NULL) || (!Status)) {\r
290 Status = FALSE;\r
291 goto _Exit;\r
292 }\r
293\r
294 Status = FALSE;\r
295\r
296 //\r
297 // Retrieve subject name from certificate object.\r
298 //\r
299 X509Name = X509_get_subject_name (X509Cert);\r
300 if (X509Name == NULL) {\r
301 goto _Exit;\r
302 }\r
303\r
304 X509NameSize = i2d_X509_NAME (X509Name, NULL);\r
305 if (*SubjectSize < X509NameSize) {\r
306 *SubjectSize = X509NameSize;\r
307 goto _Exit;\r
308 }\r
309\r
310 *SubjectSize = X509NameSize;\r
311 if (CertSubject != NULL) {\r
312 i2d_X509_NAME (X509Name, &CertSubject);\r
313 Status = TRUE;\r
314 }\r
315\r
316_Exit:\r
317 //\r
318 // Release Resources.\r
319 //\r
320 if (X509Cert != NULL) {\r
321 X509_free (X509Cert);\r
322 }\r
323\r
324 return Status;\r
325}\r
326\r
327/**\r
328 Retrieve a string from one X.509 certificate base on the Request_NID.\r
329\r
330 @param[in] Cert Pointer to the DER-encoded X509 certificate.\r
331 @param[in] CertSize Size of the X509 certificate in bytes.\r
332 @param[in] Request_NID NID of string to obtain\r
333 @param[out] CommonName Buffer to contain the retrieved certificate common\r
334 name string (UTF8). At most CommonNameSize bytes will be\r
335 written and the string will be null terminated. May be\r
336 NULL in order to determine the size buffer needed.\r
337 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,\r
338 and the size of buffer returned CommonName on output.\r
339 If CommonName is NULL then the amount of space needed\r
340 in buffer (including the final null) is returned.\r
341\r
342 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.\r
343 @retval RETURN_INVALID_PARAMETER If Cert is NULL.\r
344 If CommonNameSize is NULL.\r
345 If CommonName is not NULL and *CommonNameSize is 0.\r
346 If Certificate is invalid.\r
347 @retval RETURN_NOT_FOUND If no NID Name entry exists.\r
348 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size\r
349 (including the final null) is returned in the\r
350 CommonNameSize parameter.\r
351 @retval RETURN_UNSUPPORTED The operation is not supported.\r
352\r
353**/\r
354STATIC\r
355RETURN_STATUS\r
356InternalX509GetNIDName (\r
357 IN CONST UINT8 *Cert,\r
358 IN UINTN CertSize,\r
359 IN INT32 Request_NID,\r
360 OUT CHAR8 *CommonName OPTIONAL,\r
361 IN OUT UINTN *CommonNameSize\r
362 )\r
363{\r
364 RETURN_STATUS ReturnStatus;\r
365 BOOLEAN Status;\r
366 X509 *X509Cert;\r
367 X509_NAME *X509Name;\r
368 INT32 Index;\r
369 INTN Length;\r
370 X509_NAME_ENTRY *Entry;\r
371 ASN1_STRING *EntryData;\r
372 UINT8 *UTF8Name;\r
373\r
374 ReturnStatus = RETURN_INVALID_PARAMETER;\r
375 UTF8Name = NULL;\r
376\r
377 //\r
378 // Check input parameters.\r
379 //\r
380 if ((Cert == NULL) || (CertSize > INT_MAX) || (CommonNameSize == NULL)) {\r
381 return ReturnStatus;\r
382 }\r
383\r
384 if ((CommonName != NULL) && (*CommonNameSize == 0)) {\r
385 return ReturnStatus;\r
386 }\r
387\r
388 X509Cert = NULL;\r
389 //\r
390 // Read DER-encoded X509 Certificate and Construct X509 object.\r
391 //\r
392 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);\r
393 if ((X509Cert == NULL) || (!Status)) {\r
394 //\r
395 // Invalid X.509 Certificate\r
396 //\r
397 goto _Exit;\r
398 }\r
399\r
400 Status = FALSE;\r
401\r
402 //\r
403 // Retrieve subject name from certificate object.\r
404 //\r
405 X509Name = X509_get_subject_name (X509Cert);\r
406 if (X509Name == NULL) {\r
407 //\r
408 // Fail to retrieve subject name content\r
409 //\r
410 goto _Exit;\r
411 }\r
412\r
413 //\r
414 // Retrive the string from X.509 Subject base on the Request_NID\r
415 //\r
416 Index = X509_NAME_get_index_by_NID (X509Name, Request_NID, -1);\r
417 if (Index < 0) {\r
418 //\r
419 // No Request_NID name entry exists in X509_NAME object\r
420 //\r
421 *CommonNameSize = 0;\r
422 ReturnStatus = RETURN_NOT_FOUND;\r
423 goto _Exit;\r
424 }\r
425\r
426 Entry = X509_NAME_get_entry (X509Name, Index);\r
427 if (Entry == NULL) {\r
428 //\r
429 // Fail to retrieve name entry data\r
430 //\r
431 *CommonNameSize = 0;\r
432 ReturnStatus = RETURN_NOT_FOUND;\r
433 goto _Exit;\r
434 }\r
435\r
436 EntryData = X509_NAME_ENTRY_get_data (Entry);\r
437\r
438 Length = ASN1_STRING_to_UTF8 (&UTF8Name, EntryData);\r
439 if (Length < 0) {\r
440 //\r
441 // Fail to convert the Name string\r
442 //\r
443 *CommonNameSize = 0;\r
444 ReturnStatus = RETURN_INVALID_PARAMETER;\r
445 goto _Exit;\r
446 }\r
447\r
448 if (CommonName == NULL) {\r
449 *CommonNameSize = Length + 1;\r
450 ReturnStatus = RETURN_BUFFER_TOO_SMALL;\r
451 } else {\r
452 *CommonNameSize = MIN ((UINTN)Length, *CommonNameSize - 1) + 1;\r
453 CopyMem (CommonName, UTF8Name, *CommonNameSize - 1);\r
454 CommonName[*CommonNameSize - 1] = '\0';\r
455 ReturnStatus = RETURN_SUCCESS;\r
456 }\r
457\r
458_Exit:\r
459 //\r
460 // Release Resources.\r
461 //\r
462 if (X509Cert != NULL) {\r
463 X509_free (X509Cert);\r
464 }\r
465\r
466 if (UTF8Name != NULL) {\r
467 OPENSSL_free (UTF8Name);\r
468 }\r
469\r
470 return ReturnStatus;\r
471}\r
472\r
473/**\r
474 Retrieve the common name (CN) string from one X.509 certificate.\r
475\r
476 @param[in] Cert Pointer to the DER-encoded X509 certificate.\r
477 @param[in] CertSize Size of the X509 certificate in bytes.\r
478 @param[out] CommonName Buffer to contain the retrieved certificate common\r
479 name string. At most CommonNameSize bytes will be\r
480 written and the string will be null terminated. May be\r
481 NULL in order to determine the size buffer needed.\r
482 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,\r
483 and the size of buffer returned CommonName on output.\r
484 If CommonName is NULL then the amount of space needed\r
485 in buffer (including the final null) is returned.\r
486\r
487 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.\r
488 @retval RETURN_INVALID_PARAMETER If Cert is NULL.\r
489 If CommonNameSize is NULL.\r
490 If CommonName is not NULL and *CommonNameSize is 0.\r
491 If Certificate is invalid.\r
492 @retval RETURN_NOT_FOUND If no CommonName entry exists.\r
493 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size\r
494 (including the final null) is returned in the\r
495 CommonNameSize parameter.\r
496 @retval RETURN_UNSUPPORTED The operation is not supported.\r
497\r
498**/\r
499RETURN_STATUS\r
500EFIAPI\r
501X509GetCommonName (\r
502 IN CONST UINT8 *Cert,\r
503 IN UINTN CertSize,\r
504 OUT CHAR8 *CommonName OPTIONAL,\r
505 IN OUT UINTN *CommonNameSize\r
506 )\r
507{\r
508 return InternalX509GetNIDName (Cert, CertSize, NID_commonName, CommonName, CommonNameSize);\r
509}\r
510\r
511/**\r
512 Retrieve the organization name (O) string from one X.509 certificate.\r
513\r
514 @param[in] Cert Pointer to the DER-encoded X509 certificate.\r
515 @param[in] CertSize Size of the X509 certificate in bytes.\r
516 @param[out] NameBuffer Buffer to contain the retrieved certificate organization\r
517 name string. At most NameBufferSize bytes will be\r
518 written and the string will be null terminated. May be\r
519 NULL in order to determine the size buffer needed.\r
520 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,\r
521 and the size of buffer returned Name on output.\r
522 If NameBuffer is NULL then the amount of space needed\r
523 in buffer (including the final null) is returned.\r
524\r
525 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.\r
526 @retval RETURN_INVALID_PARAMETER If Cert is NULL.\r
527 If NameBufferSize is NULL.\r
528 If NameBuffer is not NULL and *CommonNameSize is 0.\r
529 If Certificate is invalid.\r
530 @retval RETURN_NOT_FOUND If no Organization Name entry exists.\r
531 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size\r
532 (including the final null) is returned in the\r
533 CommonNameSize parameter.\r
534 @retval RETURN_UNSUPPORTED The operation is not supported.\r
535\r
536**/\r
537RETURN_STATUS\r
538EFIAPI\r
539X509GetOrganizationName (\r
540 IN CONST UINT8 *Cert,\r
541 IN UINTN CertSize,\r
542 OUT CHAR8 *NameBuffer OPTIONAL,\r
543 IN OUT UINTN *NameBufferSize\r
544 )\r
545{\r
546 return InternalX509GetNIDName (Cert, CertSize, NID_organizationName, NameBuffer, NameBufferSize);\r
547}\r
548\r
549/**\r
550 Retrieve the RSA Public Key from one DER-encoded X509 certificate.\r
551\r
552 @param[in] Cert Pointer to the DER-encoded X509 certificate.\r
553 @param[in] CertSize Size of the X509 certificate in bytes.\r
554 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved\r
555 RSA public key component. Use RsaFree() function to free the\r
556 resource.\r
557\r
558 If Cert is NULL, then return FALSE.\r
559 If RsaContext is NULL, then return FALSE.\r
560\r
561 @retval TRUE RSA Public Key was retrieved successfully.\r
562 @retval FALSE Fail to retrieve RSA public key from X509 certificate.\r
563\r
564**/\r
565BOOLEAN\r
566EFIAPI\r
567RsaGetPublicKeyFromX509 (\r
568 IN CONST UINT8 *Cert,\r
569 IN UINTN CertSize,\r
570 OUT VOID **RsaContext\r
571 )\r
572{\r
573 BOOLEAN Status;\r
574 EVP_PKEY *Pkey;\r
575 X509 *X509Cert;\r
576\r
577 //\r
578 // Check input parameters.\r
579 //\r
580 if ((Cert == NULL) || (RsaContext == NULL)) {\r
581 return FALSE;\r
582 }\r
583\r
584 Pkey = NULL;\r
585 X509Cert = NULL;\r
586\r
587 //\r
588 // Read DER-encoded X509 Certificate and Construct X509 object.\r
589 //\r
590 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);\r
591 if ((X509Cert == NULL) || (!Status)) {\r
592 Status = FALSE;\r
593 goto _Exit;\r
594 }\r
595\r
596 Status = FALSE;\r
597\r
598 //\r
599 // Retrieve and check EVP_PKEY data from X509 Certificate.\r
600 //\r
601 Pkey = X509_get_pubkey (X509Cert);\r
602 if ((Pkey == NULL) || (EVP_PKEY_id (Pkey) != EVP_PKEY_RSA)) {\r
603 goto _Exit;\r
604 }\r
605\r
606 //\r
607 // Duplicate RSA Context from the retrieved EVP_PKEY.\r
608 //\r
609 if ((*RsaContext = RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey))) != NULL) {\r
610 Status = TRUE;\r
611 }\r
612\r
613_Exit:\r
614 //\r
615 // Release Resources.\r
616 //\r
617 if (X509Cert != NULL) {\r
618 X509_free (X509Cert);\r
619 }\r
620\r
621 if (Pkey != NULL) {\r
622 EVP_PKEY_free (Pkey);\r
623 }\r
624\r
625 return Status;\r
626}\r
627\r
628/**\r
629 Verify one X509 certificate was issued by the trusted CA.\r
630\r
631 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.\r
632 @param[in] CertSize Size of the X509 certificate in bytes.\r
633 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.\r
634 @param[in] CACertSize Size of the CA Certificate in bytes.\r
635\r
636 If Cert is NULL, then return FALSE.\r
637 If CACert is NULL, then return FALSE.\r
638\r
639 @retval TRUE The certificate was issued by the trusted CA.\r
640 @retval FALSE Invalid certificate or the certificate was not issued by the given\r
641 trusted CA.\r
642\r
643**/\r
644BOOLEAN\r
645EFIAPI\r
646X509VerifyCert (\r
647 IN CONST UINT8 *Cert,\r
648 IN UINTN CertSize,\r
649 IN CONST UINT8 *CACert,\r
650 IN UINTN CACertSize\r
651 )\r
652{\r
653 BOOLEAN Status;\r
654 X509 *X509Cert;\r
655 X509 *X509CACert;\r
656 X509_STORE *CertStore;\r
657 X509_STORE_CTX *CertCtx;\r
658\r
659 //\r
660 // Check input parameters.\r
661 //\r
662 if ((Cert == NULL) || (CACert == NULL)) {\r
663 return FALSE;\r
664 }\r
665\r
666 Status = FALSE;\r
667 X509Cert = NULL;\r
668 X509CACert = NULL;\r
669 CertStore = NULL;\r
670 CertCtx = NULL;\r
671\r
672 //\r
673 // Register & Initialize necessary digest algorithms for certificate verification.\r
674 //\r
675 if (EVP_add_digest (EVP_md5 ()) == 0) {\r
676 goto _Exit;\r
677 }\r
678\r
679 if (EVP_add_digest (EVP_sha1 ()) == 0) {\r
680 goto _Exit;\r
681 }\r
682\r
683 if (EVP_add_digest (EVP_sha256 ()) == 0) {\r
684 goto _Exit;\r
685 }\r
686\r
687 //\r
688 // Read DER-encoded certificate to be verified and Construct X509 object.\r
689 //\r
690 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);\r
691 if ((X509Cert == NULL) || (!Status)) {\r
692 Status = FALSE;\r
693 goto _Exit;\r
694 }\r
695\r
696 //\r
697 // Read DER-encoded root certificate and Construct X509 object.\r
698 //\r
699 Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **)&X509CACert);\r
700 if ((X509CACert == NULL) || (!Status)) {\r
701 Status = FALSE;\r
702 goto _Exit;\r
703 }\r
704\r
705 Status = FALSE;\r
706\r
707 //\r
708 // Set up X509 Store for trusted certificate.\r
709 //\r
710 CertStore = X509_STORE_new ();\r
711 if (CertStore == NULL) {\r
712 goto _Exit;\r
713 }\r
714\r
715 if (!(X509_STORE_add_cert (CertStore, X509CACert))) {\r
716 goto _Exit;\r
717 }\r
718\r
719 //\r
720 // Allow partial certificate chains, terminated by a non-self-signed but\r
721 // still trusted intermediate certificate. Also disable time checks.\r
722 //\r
723 X509_STORE_set_flags (\r
724 CertStore,\r
725 X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME\r
726 );\r
727\r
728 //\r
729 // Set up X509_STORE_CTX for the subsequent verification operation.\r
730 //\r
731 CertCtx = X509_STORE_CTX_new ();\r
732 if (CertCtx == NULL) {\r
733 goto _Exit;\r
734 }\r
735\r
736 if (!X509_STORE_CTX_init (CertCtx, CertStore, X509Cert, NULL)) {\r
737 goto _Exit;\r
738 }\r
739\r
740 //\r
741 // X509 Certificate Verification.\r
742 //\r
743 Status = (BOOLEAN)X509_verify_cert (CertCtx);\r
744 X509_STORE_CTX_cleanup (CertCtx);\r
745\r
746_Exit:\r
747 //\r
748 // Release Resources.\r
749 //\r
750 if (X509Cert != NULL) {\r
751 X509_free (X509Cert);\r
752 }\r
753\r
754 if (X509CACert != NULL) {\r
755 X509_free (X509CACert);\r
756 }\r
757\r
758 if (CertStore != NULL) {\r
759 X509_STORE_free (CertStore);\r
760 }\r
761\r
762 X509_STORE_CTX_free (CertCtx);\r
763\r
764 return Status;\r
765}\r
766\r
767/**\r
768 Retrieve the TBSCertificate from one given X.509 certificate.\r
769\r
770 @param[in] Cert Pointer to the given DER-encoded X509 certificate.\r
771 @param[in] CertSize Size of the X509 certificate in bytes.\r
772 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.\r
773 @param[out] TBSCertSize Size of the TBS certificate in bytes.\r
774\r
775 If Cert is NULL, then return FALSE.\r
776 If TBSCert is NULL, then return FALSE.\r
777 If TBSCertSize is NULL, then return FALSE.\r
778\r
779 @retval TRUE The TBSCertificate was retrieved successfully.\r
780 @retval FALSE Invalid X.509 certificate.\r
781\r
782**/\r
783BOOLEAN\r
784EFIAPI\r
785X509GetTBSCert (\r
786 IN CONST UINT8 *Cert,\r
787 IN UINTN CertSize,\r
788 OUT UINT8 **TBSCert,\r
789 OUT UINTN *TBSCertSize\r
790 )\r
791{\r
792 CONST UINT8 *Temp;\r
793 UINT32 Asn1Tag;\r
794 UINT32 ObjClass;\r
795 UINTN Length;\r
796\r
797 //\r
798 // Check input parameters.\r
799 //\r
800 if ((Cert == NULL) || (TBSCert == NULL) ||\r
801 (TBSCertSize == NULL) || (CertSize > INT_MAX))\r
802 {\r
803 return FALSE;\r
804 }\r
805\r
806 //\r
807 // An X.509 Certificate is: (defined in RFC3280)\r
808 // Certificate ::= SEQUENCE {\r
809 // tbsCertificate TBSCertificate,\r
810 // signatureAlgorithm AlgorithmIdentifier,\r
811 // signature BIT STRING }\r
812 //\r
813 // and\r
814 //\r
815 // TBSCertificate ::= SEQUENCE {\r
816 // version [0] Version DEFAULT v1,\r
817 // ...\r
818 // }\r
819 //\r
820 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip\r
821 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.\r
822 //\r
823 Temp = Cert;\r
824 Length = 0;\r
825 ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)CertSize);\r
826\r
827 if (Asn1Tag != V_ASN1_SEQUENCE) {\r
828 return FALSE;\r
829 }\r
830\r
831 *TBSCert = (UINT8 *)Temp;\r
832\r
833 ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)Length);\r
834 //\r
835 // Verify the parsed TBSCertificate is one correct SEQUENCE data.\r
836 //\r
837 if (Asn1Tag != V_ASN1_SEQUENCE) {\r
838 return FALSE;\r
839 }\r
840\r
841 *TBSCertSize = Length + (Temp - *TBSCert);\r
842\r
843 return TRUE;\r
844}\r