]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
153e7106171eeaa83cce35e4814d6c054645deb6
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
) {
268 // Read DER-encoded X509 Certificate and Construct X509 object.
270 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
271 if ((X509Cert
== NULL
) || (!Status
)) {
276 // Retrieve subject name from certificate object.
278 X509Name
= X509_get_subject_name (X509Cert
);
279 if (*SubjectSize
< (UINTN
) X509Name
->bytes
->length
) {
280 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
283 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
284 if (CertSubject
!= NULL
) {
285 CopyMem (CertSubject
, (UINT8
*)X509Name
->bytes
->data
, *SubjectSize
);
291 // Release Resources.
293 X509_free (X509Cert
);
299 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
301 @param[in] Cert Pointer to the DER-encoded X509 certificate.
302 @param[in] CertSize Size of the X509 certificate in bytes.
303 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
304 RSA public key component. Use RsaFree() function to free the
307 If Cert is NULL, then return FALSE.
308 If RsaContext is NULL, then return FALSE.
310 @retval TRUE RSA Public Key was retrieved successfully.
311 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
316 RsaGetPublicKeyFromX509 (
317 IN CONST UINT8
*Cert
,
319 OUT VOID
**RsaContext
327 // Check input parameters.
329 if (Cert
== NULL
|| RsaContext
== NULL
) {
338 // Read DER-encoded X509 Certificate and Construct X509 object.
340 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
341 if ((X509Cert
== NULL
) || (!Status
)) {
346 // Retrieve and check EVP_PKEY data from X509 Certificate.
348 Pkey
= X509_get_pubkey (X509Cert
);
349 if ((Pkey
== NULL
) || (Pkey
->type
!= EVP_PKEY_RSA
)) {
354 // Duplicate RSA Context from the retrieved EVP_PKEY.
356 if ((*RsaContext
= RSAPublicKey_dup (Pkey
->pkey
.rsa
)) != NULL
) {
362 // Release Resources.
364 X509_free (X509Cert
);
365 EVP_PKEY_free (Pkey
);
371 Verify one X509 certificate was issued by the trusted CA.
373 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
374 @param[in] CertSize Size of the X509 certificate in bytes.
375 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
376 @param[in] CACertSize Size of the CA Certificate in bytes.
378 If Cert is NULL, then return FALSE.
379 If CACert is NULL, then return FALSE.
381 @retval TRUE The certificate was issued by the trusted CA.
382 @retval FALSE Invalid certificate or the certificate was not issued by the given
389 IN CONST UINT8
*Cert
,
391 IN CONST UINT8
*CACert
,
398 X509_STORE
*CertStore
;
399 X509_STORE_CTX CertCtx
;
402 // Check input parameters.
404 if (Cert
== NULL
|| CACert
== NULL
) {
414 // Register & Initialize necessary digest algorithms for certificate verification.
416 EVP_add_digest (EVP_md5());
417 EVP_add_digest (EVP_sha1());
418 EVP_add_digest (EVP_sha256());
421 // Read DER-encoded certificate to be verified and Construct X509 object.
423 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
424 if ((X509Cert
== NULL
) || (!Status
)) {
429 // Read DER-encoded root certificate and Construct X509 object.
431 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
432 if ((X509CACert
== NULL
) || (!Status
)) {
437 // Set up X509 Store for trusted certificate.
439 CertStore
= X509_STORE_new ();
440 if (CertStore
== NULL
) {
443 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
448 // Set up X509_STORE_CTX for the subsequent verification operation.
450 if (!X509_STORE_CTX_init (&CertCtx
, CertStore
, X509Cert
, NULL
)) {
455 // X509 Certificate Verification.
457 Status
= (BOOLEAN
) X509_verify_cert (&CertCtx
);
458 X509_STORE_CTX_cleanup (&CertCtx
);
462 // Release Resources.
464 X509_free (X509Cert
);
465 X509_free (X509CACert
);
466 X509_STORE_free (CertStore
);