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