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