]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
2 X.509 Certificate Handler Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2015, 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
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.
15 #include "InternalCryptLib.h"
16 #include <openssl/x509.h>
20 Construct a X509 object from DER-encoded certificate data.
22 If Cert is NULL, then return FALSE.
23 If SingleX509Cert is NULL, then return FALSE.
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.
29 @retval TRUE The X509 object generation succeeded.
30 @retval FALSE The operation failed.
35 X509ConstructCertificate (
38 OUT UINT8
**SingleX509Cert
45 // Check input parameters.
47 if (Cert
== NULL
|| SingleX509Cert
== NULL
|| CertSize
> INT_MAX
) {
52 // Read DER-encoded X509 Certificate and Construct X509 object.
55 X509Cert
= d2i_X509 (NULL
, &Temp
, (long) CertSize
);
56 if (X509Cert
== NULL
) {
60 *SingleX509Cert
= (UINT8
*) X509Cert
;
66 Construct a X509 stack object from a list of DER-encoded certificate data.
68 If X509Stack is NULL, then return FALSE.
70 @param[in, out] X509Stack On input, pointer to an existing 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().
77 @retval TRUE The X509 stack construction succeeded.
78 @retval FALSE The construction operation failed.
83 X509ConstructCertificateStack (
84 IN OUT UINT8
**X509Stack
,
91 STACK_OF(X509
) *CertStack
;
97 // Check input parameters.
99 if (X509Stack
== NULL
) {
106 // Initialize X509 stack object.
108 CertStack
= (STACK_OF(X509
) *) (*X509Stack
);
109 if (CertStack
== NULL
) {
110 CertStack
= sk_X509_new_null ();
111 if (CertStack
== NULL
) {
116 VA_START (Args
, X509Stack
);
118 for (Index
= 0; ; Index
++) {
120 // If Cert is NULL, then it is the end of the list.
122 Cert
= VA_ARG (Args
, UINT8
*);
127 CertSize
= VA_ARG (Args
, UINTN
);
133 // Construct X509 Object from the given DER-encoded certificate data.
135 Status
= X509ConstructCertificate (
136 (CONST UINT8
*) Cert
,
141 if (X509Cert
!= NULL
) {
142 X509_free (X509Cert
);
148 // Insert the new X509 object into X509 stack object.
150 sk_X509_push (CertStack
, X509Cert
);
156 sk_X509_pop_free (CertStack
, X509_free
);
158 *X509Stack
= (UINT8
*) CertStack
;
165 Release the specified X509 object.
167 If X509Cert is NULL, then return FALSE.
169 @param[in] X509Cert Pointer to the X509 object to be released.
179 // Check input parameters.
181 if (X509Cert
== NULL
) {
186 // Free OpenSSL X509 object.
188 X509_free ((X509
*) X509Cert
);
192 Release the specified X509 stack object.
194 If X509Stack is NULL, then return FALSE.
196 @param[in] X509Stack Pointer to the X509 stack object to be released.
206 // Check input parameters.
208 if (X509Stack
== NULL
) {
213 // Free OpenSSL X509 stack object.
215 sk_X509_pop_free ((STACK_OF(X509
) *) X509Stack
, X509_free
);
219 Retrieve the subject bytes from one X.509 certificate.
221 @param[in] Cert Pointer to the DER-encoded X509 certificate.
222 @param[in] CertSize Size of the X509 certificate in bytes.
223 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
224 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
225 and the size of buffer returned CertSubject on output.
227 If Cert is NULL, then return FALSE.
228 If SubjectSize is NULL, then return FALSE.
230 @retval TRUE The certificate subject retrieved successfully.
231 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
232 The SubjectSize will be updated with the required size.
238 IN CONST UINT8
*Cert
,
240 OUT UINT8
*CertSubject
,
241 IN OUT UINTN
*SubjectSize
249 // Check input parameters.
251 if (Cert
== NULL
|| SubjectSize
== NULL
) {
258 // Read DER-encoded X509 Certificate and Construct X509 object.
260 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
261 if ((X509Cert
== NULL
) || (!Status
)) {
269 // Retrieve subject name from certificate object.
271 X509Name
= X509_get_subject_name (X509Cert
);
272 if (X509Name
== NULL
) {
276 if (*SubjectSize
< (UINTN
) X509Name
->bytes
->length
) {
277 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
280 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
281 if (CertSubject
!= NULL
) {
282 CopyMem (CertSubject
, (UINT8
*) X509Name
->bytes
->data
, *SubjectSize
);
288 // Release Resources.
290 if (X509Cert
!= NULL
) {
291 X509_free (X509Cert
);
298 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
300 @param[in] Cert Pointer to the DER-encoded X509 certificate.
301 @param[in] CertSize Size of the X509 certificate in bytes.
302 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
303 RSA public key component. Use RsaFree() function to free the
306 If Cert is NULL, then return FALSE.
307 If RsaContext is NULL, then return FALSE.
309 @retval TRUE RSA Public Key was retrieved successfully.
310 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
315 RsaGetPublicKeyFromX509 (
316 IN CONST UINT8
*Cert
,
318 OUT VOID
**RsaContext
326 // Check input parameters.
328 if (Cert
== NULL
|| RsaContext
== NULL
) {
336 // Read DER-encoded X509 Certificate and Construct X509 object.
338 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
339 if ((X509Cert
== NULL
) || (!Status
)) {
347 // Retrieve and check EVP_PKEY data from X509 Certificate.
349 Pkey
= X509_get_pubkey (X509Cert
);
350 if ((Pkey
== NULL
) || (Pkey
->type
!= EVP_PKEY_RSA
)) {
355 // Duplicate RSA Context from the retrieved EVP_PKEY.
357 if ((*RsaContext
= RSAPublicKey_dup (Pkey
->pkey
.rsa
)) != NULL
) {
363 // Release Resources.
365 if (X509Cert
!= NULL
) {
366 X509_free (X509Cert
);
370 EVP_PKEY_free (Pkey
);
377 Verify one X509 certificate was issued by the trusted CA.
379 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
380 @param[in] CertSize Size of the X509 certificate in bytes.
381 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
382 @param[in] CACertSize Size of the CA Certificate in bytes.
384 If Cert is NULL, then return FALSE.
385 If CACert is NULL, then return FALSE.
387 @retval TRUE The certificate was issued by the trusted CA.
388 @retval FALSE Invalid certificate or the certificate was not issued by the given
395 IN CONST UINT8
*Cert
,
397 IN CONST UINT8
*CACert
,
404 X509_STORE
*CertStore
;
405 X509_STORE_CTX CertCtx
;
408 // Check input parameters.
410 if (Cert
== NULL
|| CACert
== NULL
) {
420 // Register & Initialize necessary digest algorithms for certificate verification.
422 if (EVP_add_digest (EVP_md5 ()) == 0) {
425 if (EVP_add_digest (EVP_sha1 ()) == 0) {
428 if (EVP_add_digest (EVP_sha256 ()) == 0) {
433 // Read DER-encoded certificate to be verified and Construct X509 object.
435 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
436 if ((X509Cert
== NULL
) || (!Status
)) {
442 // Read DER-encoded root certificate and Construct X509 object.
444 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
445 if ((X509CACert
== NULL
) || (!Status
)) {
453 // Set up X509 Store for trusted certificate.
455 CertStore
= X509_STORE_new ();
456 if (CertStore
== NULL
) {
459 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
464 // Set up X509_STORE_CTX for the subsequent verification operation.
466 if (!X509_STORE_CTX_init (&CertCtx
, CertStore
, X509Cert
, NULL
)) {
471 // X509 Certificate Verification.
473 Status
= (BOOLEAN
) X509_verify_cert (&CertCtx
);
474 X509_STORE_CTX_cleanup (&CertCtx
);
478 // Release Resources.
480 if (X509Cert
!= NULL
) {
481 X509_free (X509Cert
);
484 if (X509CACert
!= NULL
) {
485 X509_free (X509CACert
);
488 if (CertStore
!= NULL
) {
489 X509_STORE_free (CertStore
);
496 Retrieve the TBSCertificate from one given X.509 certificate.
498 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
499 @param[in] CertSize Size of the X509 certificate in bytes.
500 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
501 @param[out] TBSCertSize Size of the TBS certificate in bytes.
503 If Cert is NULL, then return FALSE.
504 If TBSCert is NULL, then return FALSE.
505 If TBSCertSize is NULL, then return FALSE.
507 @retval TRUE The TBSCertificate was retrieved successfully.
508 @retval FALSE Invalid X.509 certificate.
514 IN CONST UINT8
*Cert
,
517 OUT UINTN
*TBSCertSize
526 // Check input parameters.
528 if ((Cert
== NULL
) || (TBSCert
== NULL
) ||
529 (TBSCertSize
== NULL
) || (CertSize
> INT_MAX
)) {
534 // An X.509 Certificate is: (defined in RFC3280)
535 // Certificate ::= SEQUENCE {
536 // tbsCertificate TBSCertificate,
537 // signatureAlgorithm AlgorithmIdentifier,
538 // signature BIT STRING }
542 // TBSCertificate ::= SEQUENCE {
543 // version [0] Version DEFAULT v1,
547 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip
548 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.
551 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)CertSize
);
553 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
557 *TBSCert
= (UINT8
*)Temp
;
559 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)Length
);
561 // Verify the parsed TBSCertificate is one correct SEQUENCE data.
563 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
567 *TBSCertSize
= Length
+ (Temp
- *TBSCert
);