]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
29efc42b021a60782f54dfd53572f0ef977241fc
2 X.509 Certificate Handler Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2014, 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
44 // Check input parameters.
46 if (Cert
== NULL
|| SingleX509Cert
== NULL
|| CertSize
> INT_MAX
) {
51 // Read DER-encoded X509 Certificate and Construct X509 object.
53 X509Cert
= d2i_X509 (NULL
, &Cert
, (long) CertSize
);
54 if (X509Cert
== NULL
) {
58 *SingleX509Cert
= (UINT8
*) X509Cert
;
64 Construct a X509 stack object from a list of DER-encoded certificate data.
66 If X509Stack is NULL, then return FALSE.
68 @param[in, out] X509Stack On input, pointer to an existing X509 stack object.
69 On output, pointer to the X509 stack object with new
70 inserted X509 certificate.
71 @param ... A list of DER-encoded single certificate data followed
72 by certificate size. A NULL terminates the list. The
73 pairs are the arguments to X509ConstructCertificate().
75 @retval TRUE The X509 stack construction succeeded.
76 @retval FALSE The construction operation failed.
81 X509ConstructCertificateStack (
82 IN OUT UINT8
**X509Stack
,
89 STACK_OF(X509
) *CertStack
;
95 // Check input parameters.
97 if (X509Stack
== NULL
) {
104 // Initialize X509 stack object.
106 CertStack
= (STACK_OF(X509
) *) (*X509Stack
);
107 if (CertStack
== NULL
) {
108 CertStack
= sk_X509_new_null ();
109 if (CertStack
== NULL
) {
114 VA_START (Args
, X509Stack
);
116 for (Index
= 0; ; Index
++) {
118 // If Cert is NULL, then it is the end of the list.
120 Cert
= VA_ARG (Args
, UINT8
*);
125 CertSize
= VA_ARG (Args
, UINTN
);
128 // Construct X509 Object from the given DER-encoded certificate data.
130 Status
= X509ConstructCertificate (
131 (CONST UINT8
*) Cert
,
136 X509_free (X509Cert
);
141 // Insert the new X509 object into X509 stack object.
143 sk_X509_push (CertStack
, X509Cert
);
149 sk_X509_pop_free (CertStack
, X509_free
);
151 *X509Stack
= (UINT8
*) CertStack
;
158 Release the specified X509 object.
160 If X509Cert is NULL, then return FALSE.
162 @param[in] X509Cert Pointer to the X509 object to be released.
172 // Check input parameters.
174 if (X509Cert
== NULL
) {
179 // Free OpenSSL X509 object.
181 X509_free ((X509
*) X509Cert
);
185 Release the specified X509 stack object.
187 If X509Stack is NULL, then return FALSE.
189 @param[in] X509Stack Pointer to the X509 stack object to be released.
199 // Check input parameters.
201 if (X509Stack
== NULL
) {
206 // Free OpenSSL X509 stack object.
208 sk_X509_pop_free ((STACK_OF(X509
) *) X509Stack
, X509_free
);
212 Retrieve the subject bytes from one X.509 certificate.
214 @param[in] Cert Pointer to the DER-encoded X509 certificate.
215 @param[in] CertSize Size of the X509 certificate in bytes.
216 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
217 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
218 and the size of buffer returned CertSubject on output.
220 If Cert is NULL, then return FALSE.
221 If SubjectSize is NULL, then return FALSE.
223 @retval TRUE The certificate subject retrieved successfully.
224 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
225 The SubjectSize will be updated with the required size.
231 IN CONST UINT8
*Cert
,
233 OUT UINT8
*CertSubject
,
234 IN OUT UINTN
*SubjectSize
242 // Check input parameters.
244 if (Cert
== NULL
|| SubjectSize
== NULL
) {
251 // Read DER-encoded X509 Certificate and Construct X509 object.
253 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
254 if ((X509Cert
== NULL
) || (!Status
)) {
262 // Retrieve subject name from certificate object.
264 X509Name
= X509_get_subject_name (X509Cert
);
265 if (X509Name
== NULL
) {
269 if (*SubjectSize
< (UINTN
) X509Name
->bytes
->length
) {
270 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
273 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
274 if (CertSubject
!= NULL
) {
275 CopyMem (CertSubject
, (UINT8
*) X509Name
->bytes
->data
, *SubjectSize
);
281 // Release Resources.
283 if (X509Cert
!= NULL
) {
284 X509_free (X509Cert
);
291 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
293 @param[in] Cert Pointer to the DER-encoded X509 certificate.
294 @param[in] CertSize Size of the X509 certificate in bytes.
295 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
296 RSA public key component. Use RsaFree() function to free the
299 If Cert is NULL, then return FALSE.
300 If RsaContext is NULL, then return FALSE.
302 @retval TRUE RSA Public Key was retrieved successfully.
303 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
308 RsaGetPublicKeyFromX509 (
309 IN CONST UINT8
*Cert
,
311 OUT VOID
**RsaContext
319 // Check input parameters.
321 if (Cert
== NULL
|| RsaContext
== NULL
) {
329 // Read DER-encoded X509 Certificate and Construct X509 object.
331 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
332 if ((X509Cert
== NULL
) || (!Status
)) {
340 // Retrieve and check EVP_PKEY data from X509 Certificate.
342 Pkey
= X509_get_pubkey (X509Cert
);
343 if ((Pkey
== NULL
) || (Pkey
->type
!= EVP_PKEY_RSA
)) {
348 // Duplicate RSA Context from the retrieved EVP_PKEY.
350 if ((*RsaContext
= RSAPublicKey_dup (Pkey
->pkey
.rsa
)) != NULL
) {
356 // Release Resources.
358 if (X509Cert
!= NULL
) {
359 X509_free (X509Cert
);
363 EVP_PKEY_free (Pkey
);
370 Verify one X509 certificate was issued by the trusted CA.
372 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
373 @param[in] CertSize Size of the X509 certificate in bytes.
374 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
375 @param[in] CACertSize Size of the CA Certificate in bytes.
377 If Cert is NULL, then return FALSE.
378 If CACert is NULL, then return FALSE.
380 @retval TRUE The certificate was issued by the trusted CA.
381 @retval FALSE Invalid certificate or the certificate was not issued by the given
388 IN CONST UINT8
*Cert
,
390 IN CONST UINT8
*CACert
,
397 X509_STORE
*CertStore
;
398 X509_STORE_CTX CertCtx
;
401 // Check input parameters.
403 if (Cert
== NULL
|| CACert
== NULL
) {
413 // Register & Initialize necessary digest algorithms for certificate verification.
415 if (EVP_add_digest (EVP_md5 ()) == 0) {
418 if (EVP_add_digest (EVP_sha1 ()) == 0) {
421 if (EVP_add_digest (EVP_sha256 ()) == 0) {
426 // Read DER-encoded certificate to be verified and Construct X509 object.
428 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
429 if ((X509Cert
== NULL
) || (!Status
)) {
435 // Read DER-encoded root certificate and Construct X509 object.
437 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
438 if ((X509CACert
== NULL
) || (!Status
)) {
446 // Set up X509 Store for trusted certificate.
448 CertStore
= X509_STORE_new ();
449 if (CertStore
== NULL
) {
452 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
457 // Set up X509_STORE_CTX for the subsequent verification operation.
459 if (!X509_STORE_CTX_init (&CertCtx
, CertStore
, X509Cert
, NULL
)) {
464 // X509 Certificate Verification.
466 Status
= (BOOLEAN
) X509_verify_cert (&CertCtx
);
467 X509_STORE_CTX_cleanup (&CertCtx
);
471 // Release Resources.
473 if (X509Cert
!= NULL
) {
474 X509_free (X509Cert
);
477 if (X509CACert
!= NULL
) {
478 X509_free (X509CACert
);
481 if (CertStore
!= NULL
) {
482 X509_STORE_free (CertStore
);
489 Retrieve the TBSCertificate from one given X.509 certificate.
491 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
492 @param[in] CertSize Size of the X509 certificate in bytes.
493 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
494 @param[out] TBSCertSize Size of the TBS certificate in bytes.
496 If Cert is NULL, then return FALSE.
497 If TBSCert is NULL, then return FALSE.
498 If TBSCertSize is NULL, then return FALSE.
500 @retval TRUE The TBSCertificate was retrieved successfully.
501 @retval FALSE Invalid X.509 certificate.
507 IN CONST UINT8
*Cert
,
510 OUT UINTN
*TBSCertSize
519 // Check input parameters.
521 if ((Cert
== NULL
) || (TBSCert
== NULL
) || (TBSCertSize
== NULL
)) {
526 // An X.509 Certificate is: (defined in RFC3280)
527 // Certificate ::= SEQUENCE {
528 // tbsCertificate TBSCertificate,
529 // signatureAlgorithm AlgorithmIdentifier,
530 // signature BIT STRING }
534 // TBSCertificate ::= SEQUENCE {
535 // version [0] Version DEFAULT v1,
539 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip
540 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.
543 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)CertSize
);
545 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
549 *TBSCert
= (UINT8
*)Temp
;
551 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)Length
);
553 // Verify the parsed TBSCertificate is one correct SEQUENCE data.
555 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
559 *TBSCertSize
= Length
+ (Temp
- *TBSCert
);