]>
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 Pop single certificate from STACK_OF(X509).
230 If X509Stack, Cert, or CertSize is NULL, then return FALSE.
232 @param[in] X509Stack Pointer to a X509 stack object.
233 @param[out] Cert Pointer to a X509 certificate.
234 @param[out] CertSize Length of output X509 certificate in bytes.
236 @retval TRUE The X509 stack pop succeeded.
237 @retval FALSE The pop operation failed.
249 STACK_OF(X509
) *CertStack
;
257 if ((X509Stack
== NULL
) || (Cert
== NULL
) || (CertSize
== NULL
)) {
261 CertStack
= (STACK_OF(X509
) *) X509Stack
;
263 X509Cert
= sk_X509_pop (CertStack
);
265 if (X509Cert
== NULL
) {
271 CertBio
= BIO_new (BIO_s_mem ());
272 if (CertBio
== NULL
) {
276 Result
= i2d_X509_bio (CertBio
, X509Cert
);
281 Length
= ((BUF_MEM
*) CertBio
->ptr
)->length
;
286 Buffer
= malloc (Length
);
287 if (Buffer
== NULL
) {
291 Result
= BIO_read (CertBio
, Buffer
, Length
);
292 if (Result
!= Length
) {
305 if (!Status
&& (Buffer
!= NULL
)) {
313 Retrieve the subject bytes from one X.509 certificate.
315 @param[in] Cert Pointer to the DER-encoded X509 certificate.
316 @param[in] CertSize Size of the X509 certificate in bytes.
317 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
318 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
319 and the size of buffer returned CertSubject on output.
321 If Cert is NULL, then return FALSE.
322 If SubjectSize is NULL, then return FALSE.
324 @retval TRUE The certificate subject retrieved successfully.
325 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
326 The SubjectSize will be updated with the required size.
332 IN CONST UINT8
*Cert
,
334 OUT UINT8
*CertSubject
,
335 IN OUT UINTN
*SubjectSize
343 // Check input parameters.
345 if (Cert
== NULL
|| SubjectSize
== NULL
) {
352 // Read DER-encoded X509 Certificate and Construct X509 object.
354 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
355 if ((X509Cert
== NULL
) || (!Status
)) {
363 // Retrieve subject name from certificate object.
365 X509Name
= X509_get_subject_name (X509Cert
);
366 if (X509Name
== NULL
) {
370 if (*SubjectSize
< (UINTN
) X509Name
->bytes
->length
) {
371 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
374 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
375 if (CertSubject
!= NULL
) {
376 CopyMem (CertSubject
, (UINT8
*) X509Name
->bytes
->data
, *SubjectSize
);
382 // Release Resources.
384 if (X509Cert
!= NULL
) {
385 X509_free (X509Cert
);
392 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
394 @param[in] Cert Pointer to the DER-encoded X509 certificate.
395 @param[in] CertSize Size of the X509 certificate in bytes.
396 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
397 RSA public key component. Use RsaFree() function to free the
400 If Cert is NULL, then return FALSE.
401 If RsaContext is NULL, then return FALSE.
403 @retval TRUE RSA Public Key was retrieved successfully.
404 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
409 RsaGetPublicKeyFromX509 (
410 IN CONST UINT8
*Cert
,
412 OUT VOID
**RsaContext
420 // Check input parameters.
422 if (Cert
== NULL
|| RsaContext
== NULL
) {
430 // Read DER-encoded X509 Certificate and Construct X509 object.
432 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
433 if ((X509Cert
== NULL
) || (!Status
)) {
441 // Retrieve and check EVP_PKEY data from X509 Certificate.
443 Pkey
= X509_get_pubkey (X509Cert
);
444 if ((Pkey
== NULL
) || (Pkey
->type
!= EVP_PKEY_RSA
)) {
449 // Duplicate RSA Context from the retrieved EVP_PKEY.
451 if ((*RsaContext
= RSAPublicKey_dup (Pkey
->pkey
.rsa
)) != NULL
) {
457 // Release Resources.
459 if (X509Cert
!= NULL
) {
460 X509_free (X509Cert
);
464 EVP_PKEY_free (Pkey
);
471 Verify one X509 certificate was issued by the trusted CA.
473 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
474 @param[in] CertSize Size of the X509 certificate in bytes.
475 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
476 @param[in] CACertSize Size of the CA Certificate in bytes.
478 If Cert is NULL, then return FALSE.
479 If CACert is NULL, then return FALSE.
481 @retval TRUE The certificate was issued by the trusted CA.
482 @retval FALSE Invalid certificate or the certificate was not issued by the given
489 IN CONST UINT8
*Cert
,
491 IN CONST UINT8
*CACert
,
498 X509_STORE
*CertStore
;
499 X509_STORE_CTX CertCtx
;
502 // Check input parameters.
504 if (Cert
== NULL
|| CACert
== NULL
) {
514 // Register & Initialize necessary digest algorithms for certificate verification.
516 if (EVP_add_digest (EVP_md5 ()) == 0) {
519 if (EVP_add_digest (EVP_sha1 ()) == 0) {
522 if (EVP_add_digest (EVP_sha256 ()) == 0) {
527 // Read DER-encoded certificate to be verified and Construct X509 object.
529 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
530 if ((X509Cert
== NULL
) || (!Status
)) {
536 // Read DER-encoded root certificate and Construct X509 object.
538 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
539 if ((X509CACert
== NULL
) || (!Status
)) {
547 // Set up X509 Store for trusted certificate.
549 CertStore
= X509_STORE_new ();
550 if (CertStore
== NULL
) {
553 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
558 // Set up X509_STORE_CTX for the subsequent verification operation.
560 if (!X509_STORE_CTX_init (&CertCtx
, CertStore
, X509Cert
, NULL
)) {
565 // X509 Certificate Verification.
567 Status
= (BOOLEAN
) X509_verify_cert (&CertCtx
);
568 X509_STORE_CTX_cleanup (&CertCtx
);
572 // Release Resources.
574 if (X509Cert
!= NULL
) {
575 X509_free (X509Cert
);
578 if (X509CACert
!= NULL
) {
579 X509_free (X509CACert
);
582 if (CertStore
!= NULL
) {
583 X509_STORE_free (CertStore
);