]>
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 - 2012, 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
46 // Check input parameters.
48 if (Cert
== NULL
|| SingleX509Cert
== NULL
|| CertSize
> INT_MAX
) {
55 // Read DER-encoded X509 Certificate and Construct X509 object.
57 CertBio
= BIO_new (BIO_s_mem ());
58 BIO_write (CertBio
, Cert
, (int) CertSize
);
59 if (CertBio
== NULL
) {
62 X509Cert
= d2i_X509_bio (CertBio
, NULL
);
63 if (X509Cert
== NULL
) {
67 *SingleX509Cert
= (UINT8
*) X509Cert
;
80 Construct a X509 stack object from a list of DER-encoded certificate data.
82 If X509Stack is NULL, then return FALSE.
84 @param[in, out] X509Stack On input, pointer to an existing X509 stack object.
85 On output, pointer to the X509 stack object with new
86 inserted X509 certificate.
87 @param ... A list of DER-encoded single certificate data followed
88 by certificate size. A NULL terminates the list. The
89 pairs are the arguments to X509ConstructCertificate().
91 @retval TRUE The X509 stack construction succeeded.
92 @retval FALSE The construction operation failed.
97 X509ConstructCertificateStack (
98 IN OUT UINT8
**X509Stack
,
105 STACK_OF(X509
) *CertStack
;
111 // Check input parameters.
113 if (X509Stack
== NULL
) {
120 // Initialize X509 stack object.
122 CertStack
= (STACK_OF(X509
) *) (*X509Stack
);
123 if (CertStack
== NULL
) {
124 CertStack
= sk_X509_new_null ();
125 if (CertStack
== NULL
) {
130 VA_START (Args
, X509Stack
);
132 for (Index
= 0; ; Index
++) {
134 // If Cert is NULL, then it is the end of the list.
136 Cert
= VA_ARG (Args
, UINT8
*);
141 CertSize
= VA_ARG (Args
, UINTN
);
144 // Construct X509 Object from the given DER-encoded certificate data.
146 Status
= X509ConstructCertificate (
147 (CONST UINT8
*) Cert
,
152 X509_free (X509Cert
);
157 // Insert the new X509 object into X509 stack object.
159 sk_X509_push (CertStack
, X509Cert
);
165 sk_X509_pop_free (CertStack
, X509_free
);
167 *X509Stack
= (UINT8
*) CertStack
;
174 Release the specified X509 object.
176 If X509Cert is NULL, then return FALSE.
178 @param[in] X509Cert Pointer to the X509 object to be released.
188 // Check input parameters.
190 if (X509Cert
== NULL
) {
195 // Free OpenSSL X509 object.
197 X509_free ((X509
*) X509Cert
);
201 Release the specified X509 stack object.
203 If X509Stack is NULL, then return FALSE.
205 @param[in] X509Stack Pointer to the X509 stack object to be released.
215 // Check input parameters.
217 if (X509Stack
== NULL
) {
222 // Free OpenSSL X509 stack object.
224 sk_X509_pop_free ((STACK_OF(X509
) *) X509Stack
, X509_free
);
228 Retrieve the subject bytes from one X.509 certificate.
230 @param[in] Cert Pointer to the DER-encoded X509 certificate.
231 @param[in] CertSize Size of the X509 certificate in bytes.
232 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
233 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
234 and the size of buffer returned CertSubject on output.
236 If Cert is NULL, then return FALSE.
237 If SubjectSize is NULL, then return FALSE.
239 @retval TRUE The certificate subject retrieved successfully.
240 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
241 The SubjectSize will be updated with the required size.
247 IN CONST UINT8
*Cert
,
249 OUT UINT8
*CertSubject
,
250 IN OUT UINTN
*SubjectSize
258 // Check input parameters.
260 if (Cert
== NULL
|| SubjectSize
== NULL
) {
267 // Read DER-encoded X509 Certificate and Construct X509 object.
269 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
270 if ((X509Cert
== NULL
) || (!Status
)) {
278 // Retrieve subject name from certificate object.
280 X509Name
= X509_get_subject_name (X509Cert
);
281 if (X509Name
== NULL
) {
285 if (*SubjectSize
< (UINTN
) X509Name
->bytes
->length
) {
286 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
289 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
290 if (CertSubject
!= NULL
) {
291 CopyMem (CertSubject
, (UINT8
*) X509Name
->bytes
->data
, *SubjectSize
);
297 // Release Resources.
299 if (X509Cert
!= NULL
) {
300 X509_free (X509Cert
);
307 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
309 @param[in] Cert Pointer to the DER-encoded X509 certificate.
310 @param[in] CertSize Size of the X509 certificate in bytes.
311 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
312 RSA public key component. Use RsaFree() function to free the
315 If Cert is NULL, then return FALSE.
316 If RsaContext is NULL, then return FALSE.
318 @retval TRUE RSA Public Key was retrieved successfully.
319 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
324 RsaGetPublicKeyFromX509 (
325 IN CONST UINT8
*Cert
,
327 OUT VOID
**RsaContext
335 // Check input parameters.
337 if (Cert
== NULL
|| RsaContext
== NULL
) {
345 // Read DER-encoded X509 Certificate and Construct X509 object.
347 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
348 if ((X509Cert
== NULL
) || (!Status
)) {
356 // Retrieve and check EVP_PKEY data from X509 Certificate.
358 Pkey
= X509_get_pubkey (X509Cert
);
359 if ((Pkey
== NULL
) || (Pkey
->type
!= EVP_PKEY_RSA
)) {
364 // Duplicate RSA Context from the retrieved EVP_PKEY.
366 if ((*RsaContext
= RSAPublicKey_dup (Pkey
->pkey
.rsa
)) != NULL
) {
372 // Release Resources.
374 if (X509Cert
!= NULL
) {
375 X509_free (X509Cert
);
379 EVP_PKEY_free (Pkey
);
386 Verify one X509 certificate was issued by the trusted CA.
388 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
389 @param[in] CertSize Size of the X509 certificate in bytes.
390 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
391 @param[in] CACertSize Size of the CA Certificate in bytes.
393 If Cert is NULL, then return FALSE.
394 If CACert is NULL, then return FALSE.
396 @retval TRUE The certificate was issued by the trusted CA.
397 @retval FALSE Invalid certificate or the certificate was not issued by the given
404 IN CONST UINT8
*Cert
,
406 IN CONST UINT8
*CACert
,
413 X509_STORE
*CertStore
;
414 X509_STORE_CTX CertCtx
;
417 // Check input parameters.
419 if (Cert
== NULL
|| CACert
== NULL
) {
429 // Register & Initialize necessary digest algorithms for certificate verification.
431 if (EVP_add_digest (EVP_md5 ()) == 0) {
434 if (EVP_add_digest (EVP_sha1 ()) == 0) {
437 if (EVP_add_digest (EVP_sha256 ()) == 0) {
442 // Read DER-encoded certificate to be verified and Construct X509 object.
444 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
445 if ((X509Cert
== NULL
) || (!Status
)) {
451 // Read DER-encoded root certificate and Construct X509 object.
453 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
454 if ((X509CACert
== NULL
) || (!Status
)) {
462 // Set up X509 Store for trusted certificate.
464 CertStore
= X509_STORE_new ();
465 if (CertStore
== NULL
) {
468 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
473 // Set up X509_STORE_CTX for the subsequent verification operation.
475 if (!X509_STORE_CTX_init (&CertCtx
, CertStore
, X509Cert
, NULL
)) {
480 // X509 Certificate Verification.
482 Status
= (BOOLEAN
) X509_verify_cert (&CertCtx
);
483 X509_STORE_CTX_cleanup (&CertCtx
);
487 // Release Resources.
489 if (X509Cert
!= NULL
) {
490 X509_free (X509Cert
);
493 if (X509CACert
!= NULL
) {
494 X509_free (X509CACert
);
497 if (CertStore
!= NULL
) {
498 X509_STORE_free (CertStore
);