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
);
54 // Read DER-encoded X509 Certificate and Construct X509 object.
56 CertBio
= BIO_new (BIO_s_mem ());
57 BIO_write (CertBio
, Cert
, (int) CertSize
);
58 if (CertBio
== NULL
) {
61 X509Cert
= d2i_X509_bio (CertBio
, NULL
);
62 if (X509Cert
== NULL
) {
66 *SingleX509Cert
= (UINT8
*) X509Cert
;
79 Construct a X509 stack object from a list of DER-encoded certificate data.
81 If X509Stack is NULL, then ASSERT().
83 @param[in, out] X509Stack On input, pointer to an existing X509 stack object.
84 On output, pointer to the X509 stack object with new
85 inserted X509 certificate.
86 @param ... A list of DER-encoded single certificate data followed
87 by certificate size. A NULL terminates the list. The
88 pairs are the arguments to X509ConstructCertificate().
90 @retval TRUE The X509 stack construction succeeded.
91 @retval FALSE The construction operation failed.
96 X509ConstructCertificateStack (
97 IN OUT UINT8
**X509Stack
,
104 STACK_OF(X509
) *CertStack
;
110 // ASSERT if input X509Stack is NULL.
112 ASSERT (X509Stack
!= NULL
);
117 // Initialize X509 stack object.
119 CertStack
= (STACK_OF(X509
) *) (*X509Stack
);
120 if (CertStack
== NULL
) {
121 CertStack
= sk_X509_new_null ();
122 if (CertStack
== NULL
) {
127 VA_START (Args
, X509Stack
);
129 for (Index
= 0; ; Index
++) {
131 // If Cert is NULL, then it is the end of the list.
133 Cert
= VA_ARG (Args
, UINT8
*);
138 CertSize
= VA_ARG (Args
, UINTN
);
141 // Construct X509 Object from the given DER-encoded certificate data.
143 Status
= X509ConstructCertificate (
144 (CONST UINT8
*) Cert
,
149 X509_free (X509Cert
);
154 // Insert the new X509 object into X509 stack object.
156 sk_X509_push (CertStack
, X509Cert
);
162 sk_X509_pop_free (CertStack
, X509_free
);
164 *X509Stack
= (UINT8
*) CertStack
;
171 Release the specified X509 object.
173 If X509Cert is NULL, then ASSERT().
175 @param[in] X509Cert Pointer to the X509 object to be released.
184 ASSERT (X509Cert
!= NULL
);
187 // Free OpenSSL X509 object.
189 X509_free ((X509
*) X509Cert
);
193 Release the specified X509 stack object.
195 If X509Stack is NULL, then ASSERT().
197 @param[in] X509Stack Pointer to the X509 stack object to be released.
206 ASSERT (X509Stack
!= NULL
);
209 // Free OpenSSL X509 stack object.
211 sk_X509_pop_free ((STACK_OF(X509
) *) X509Stack
, X509_free
);
215 Retrieve the subject bytes from one X.509 certificate.
217 @param[in] Cert Pointer to the DER-encoded X509 certificate.
218 @param[in] CertSize Size of the X509 certificate in bytes.
219 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
220 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
221 and the size of buffer returned CertSubject on output.
223 If Cert is NULL, then ASSERT().
224 If SubjectSize is NULL, then ASSERT().
226 @retval TRUE The certificate subject retrieved successfully.
227 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
228 The SubjectSize will be updated with the required size.
234 IN CONST UINT8
*Cert
,
236 OUT UINT8
*CertSubject
,
237 IN OUT UINTN
*SubjectSize
245 // ASSERT if Cert is NULL or SubjectSize is NULL.
247 ASSERT (Cert
!= NULL
);
248 ASSERT (SubjectSize
!= NULL
);
254 // Read DER-encoded X509 Certificate and Construct X509 object.
256 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
257 if ((X509Cert
== NULL
) || (!Status
)) {
262 // Retrieve subject name from certificate object.
264 X509Name
= X509_get_subject_name (X509Cert
);
265 if (*SubjectSize
< (UINTN
) X509Name
->bytes
->length
) {
266 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
269 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
270 if (CertSubject
!= NULL
) {
271 CopyMem (CertSubject
, (UINT8
*)X509Name
->bytes
->data
, *SubjectSize
);
277 // Release Resources.
279 X509_free (X509Cert
);
285 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
287 @param[in] Cert Pointer to the DER-encoded X509 certificate.
288 @param[in] CertSize Size of the X509 certificate in bytes.
289 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
290 RSA public key component. Use RsaFree() function to free the
293 If Cert is NULL, then ASSERT().
294 If RsaContext is NULL, then ASSERT().
296 @retval TRUE RSA Public Key was retrieved successfully.
297 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
302 RsaGetPublicKeyFromX509 (
303 IN CONST UINT8
*Cert
,
305 OUT VOID
**RsaContext
313 // ASSERT if Cert is NULL or RsaContext is NULL.
315 ASSERT (Cert
!= NULL
);
316 ASSERT (RsaContext
!= NULL
);
323 // Read DER-encoded X509 Certificate and Construct X509 object.
325 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
326 if ((X509Cert
== NULL
) || (!Status
)) {
331 // Retrieve and check EVP_PKEY data from X509 Certificate.
333 Pkey
= X509_get_pubkey (X509Cert
);
334 if ((Pkey
== NULL
) || (Pkey
->type
!= EVP_PKEY_RSA
)) {
339 // Duplicate RSA Context from the retrieved EVP_PKEY.
341 if ((*RsaContext
= RSAPublicKey_dup (Pkey
->pkey
.rsa
)) != NULL
) {
347 // Release Resources.
349 X509_free (X509Cert
);
350 EVP_PKEY_free (Pkey
);
356 Verify one X509 certificate was issued by the trusted CA.
358 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
359 @param[in] CertSize Size of the X509 certificate in bytes.
360 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
361 @param[in] CACertSize Size of the CA Certificate in bytes.
363 If Cert is NULL, then ASSERT().
364 If CACert is NULL, then ASSERT().
366 @retval TRUE The certificate was issued by the trusted CA.
367 @retval FALSE Invalid certificate or the certificate was not issued by the given
374 IN CONST UINT8
*Cert
,
376 IN CONST UINT8
*CACert
,
383 X509_STORE
*CertStore
;
384 X509_STORE_CTX CertCtx
;
387 // ASSERT if Cert is NULL or CACert is NULL.
389 ASSERT (Cert
!= NULL
);
390 ASSERT (CACert
!= NULL
);
398 // Register & Initialize necessary digest algorithms for certificate verification.
400 EVP_add_digest (EVP_md5());
401 EVP_add_digest (EVP_sha1());
402 EVP_add_digest (EVP_sha256());
405 // Read DER-encoded certificate to be verified and Construct X509 object.
407 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
408 if ((X509Cert
== NULL
) || (!Status
)) {
413 // Read DER-encoded root certificate and Construct X509 object.
415 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
416 if ((X509CACert
== NULL
) || (!Status
)) {
421 // Set up X509 Store for trusted certificate.
423 CertStore
= X509_STORE_new ();
424 if (CertStore
== NULL
) {
427 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
432 // Set up X509_STORE_CTX for the subsequent verification operation.
434 if (!X509_STORE_CTX_init (&CertCtx
, CertStore
, X509Cert
, NULL
)) {
439 // X509 Certificate Verification.
441 Status
= (BOOLEAN
) X509_verify_cert (&CertCtx
);
445 // Release Resources.
447 X509_free (X509Cert
);
448 X509_free (X509CACert
);
449 X509_STORE_free (CertStore
);
450 X509_STORE_CTX_cleanup (&CertCtx
);