]>
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 - 2011, 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 ASSERT().
23 If SingleX509Cert is NULL, then ASSERT().
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 // ASSERT if Cert is NULL or SingleX509Cert is NULL.
48 ASSERT (Cert
!= NULL
);
49 ASSERT (SingleX509Cert
!= NULL
);
51 if (CertSize
> INT_MAX
) {
58 // Read DER-encoded X509 Certificate and Construct X509 object.
60 CertBio
= BIO_new (BIO_s_mem ());
61 BIO_write (CertBio
, Cert
, (int) CertSize
);
62 if (CertBio
== NULL
) {
65 X509Cert
= d2i_X509_bio (CertBio
, NULL
);
66 if (X509Cert
== NULL
) {
70 *SingleX509Cert
= (UINT8
*) X509Cert
;
83 Construct a X509 stack object from a list of DER-encoded certificate data.
85 If X509Stack is NULL, then ASSERT().
87 @param[in, out] X509Stack On input, pointer to an existing X509 stack object.
88 On output, pointer to the X509 stack object with new
89 inserted X509 certificate.
90 @param ... A list of DER-encoded single certificate data followed
91 by certificate size. A NULL terminates the list. The
92 pairs are the arguments to X509ConstructCertificate().
94 @retval TRUE The X509 stack construction succeeded.
95 @retval FALSE The construction operation failed.
100 X509ConstructCertificateStack (
101 IN OUT UINT8
**X509Stack
,
108 STACK_OF(X509
) *CertStack
;
114 // ASSERT if input X509Stack is NULL.
116 ASSERT (X509Stack
!= NULL
);
121 // Initialize X509 stack object.
123 CertStack
= (STACK_OF(X509
) *) (*X509Stack
);
124 if (CertStack
== NULL
) {
125 CertStack
= sk_X509_new_null ();
126 if (CertStack
== NULL
) {
131 VA_START (Args
, X509Stack
);
133 for (Index
= 0; ; Index
++) {
135 // If Cert is NULL, then it is the end of the list.
137 Cert
= VA_ARG (Args
, UINT8
*);
142 CertSize
= VA_ARG (Args
, UINTN
);
145 // Construct X509 Object from the given DER-encoded certificate data.
147 Status
= X509ConstructCertificate (
148 (CONST UINT8
*) Cert
,
153 X509_free (X509Cert
);
158 // Insert the new X509 object into X509 stack object.
160 sk_X509_push (CertStack
, X509Cert
);
166 sk_X509_pop_free (CertStack
, X509_free
);
168 *X509Stack
= (UINT8
*) CertStack
;
175 Release the specified X509 object.
177 If X509Cert is NULL, then ASSERT().
179 @param[in] X509Cert Pointer to the X509 object to be released.
188 ASSERT (X509Cert
!= NULL
);
191 // Free OpenSSL X509 object.
193 X509_free ((X509
*) X509Cert
);
197 Release the specified X509 stack object.
199 If X509Stack is NULL, then ASSERT().
201 @param[in] X509Stack Pointer to the X509 stack object to be released.
210 ASSERT (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 ASSERT().
228 If SubjectSize is NULL, then ASSERT().
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 // ASSERT if Cert is NULL or SubjectSize is NULL.
251 ASSERT (Cert
!= NULL
);
252 ASSERT (SubjectSize
!= NULL
);
258 // Read DER-encoded X509 Certificate and Construct X509 object.
260 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
261 if ((X509Cert
== NULL
) || (!Status
)) {
266 // Retrieve subject name from certificate object.
268 X509Name
= X509_get_subject_name (X509Cert
);
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 X509_free (X509Cert
);
289 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
291 @param[in] Cert Pointer to the DER-encoded X509 certificate.
292 @param[in] CertSize Size of the X509 certificate in bytes.
293 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
294 RSA public key component. Use RsaFree() function to free the
297 If Cert is NULL, then ASSERT().
298 If RsaContext is NULL, then ASSERT().
300 @retval TRUE RSA Public Key was retrieved successfully.
301 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
306 RsaGetPublicKeyFromX509 (
307 IN CONST UINT8
*Cert
,
309 OUT VOID
**RsaContext
317 // ASSERT if Cert is NULL or RsaContext is NULL.
319 ASSERT (Cert
!= NULL
);
320 ASSERT (RsaContext
!= NULL
);
327 // Read DER-encoded X509 Certificate and Construct X509 object.
329 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
330 if ((X509Cert
== NULL
) || (!Status
)) {
335 // Retrieve and check EVP_PKEY data from X509 Certificate.
337 Pkey
= X509_get_pubkey (X509Cert
);
338 if ((Pkey
== NULL
) || (Pkey
->type
!= EVP_PKEY_RSA
)) {
343 // Duplicate RSA Context from the retrieved EVP_PKEY.
345 if ((*RsaContext
= RSAPublicKey_dup (Pkey
->pkey
.rsa
)) != NULL
) {
351 // Release Resources.
353 X509_free (X509Cert
);
354 EVP_PKEY_free (Pkey
);
360 Verify one X509 certificate was issued by the trusted CA.
362 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
363 @param[in] CertSize Size of the X509 certificate in bytes.
364 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
365 @param[in] CACertSize Size of the CA Certificate in bytes.
367 If Cert is NULL, then ASSERT().
368 If CACert is NULL, then ASSERT().
370 @retval TRUE The certificate was issued by the trusted CA.
371 @retval FALSE Invalid certificate or the certificate was not issued by the given
378 IN CONST UINT8
*Cert
,
380 IN CONST UINT8
*CACert
,
387 X509_STORE
*CertStore
;
388 X509_STORE_CTX CertCtx
;
391 // ASSERT if Cert is NULL or CACert is NULL.
393 ASSERT (Cert
!= NULL
);
394 ASSERT (CACert
!= NULL
);
402 // Register & Initialize necessary digest algorithms for certificate verification.
404 EVP_add_digest (EVP_md5());
405 EVP_add_digest (EVP_sha1());
406 EVP_add_digest (EVP_sha256());
409 // Read DER-encoded certificate to be verified and Construct X509 object.
411 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
412 if ((X509Cert
== NULL
) || (!Status
)) {
417 // Read DER-encoded root certificate and Construct X509 object.
419 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
420 if ((X509CACert
== NULL
) || (!Status
)) {
425 // Set up X509 Store for trusted certificate.
427 CertStore
= X509_STORE_new ();
428 if (CertStore
== NULL
) {
431 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
436 // Set up X509_STORE_CTX for the subsequent verification operation.
438 if (!X509_STORE_CTX_init (&CertCtx
, CertStore
, X509Cert
, NULL
)) {
443 // X509 Certificate Verification.
445 Status
= (BOOLEAN
) X509_verify_cert (&CertCtx
);
446 X509_STORE_CTX_cleanup (&CertCtx
);
450 // Release Resources.
452 X509_free (X509Cert
);
453 X509_free (X509CACert
);
454 X509_STORE_free (CertStore
);