]>
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, 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>
19 Retrieve the subject bytes from one X.509 certificate.
21 @param[in] Cert Pointer to the DER-encoded X509 certificate.
22 @param[in] CertSize Size of the X509 certificate in bytes.
23 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
24 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
25 and the size of buffer returned CertSubject on output.
27 If Cert is NULL, then ASSERT().
28 If SubjectSize is NULL, then ASSERT().
30 @retval TRUE The certificate subject retrieved successfully.
31 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
32 The SubjectSize will be updated with the required size.
40 OUT UINT8
*CertSubject
,
41 IN OUT UINTN
*SubjectSize
50 // ASSERT if Cert is NULL or SubjectSize is NULL.
52 ASSERT (Cert
!= NULL
);
53 ASSERT (SubjectSize
!= NULL
);
59 // Read DER-encoded X509 Certificate and Construct X509 object.
61 CertBio
= BIO_new (BIO_s_mem ());
62 BIO_write (CertBio
, Cert
, (int)CertSize
);
63 if (CertBio
== NULL
) {
66 X509Cert
= d2i_X509_bio (CertBio
, NULL
);
72 // Retrieve subject name from certificate object.
74 X509Name
= X509_get_subject_name (X509Cert
);
75 if (*SubjectSize
< (UINTN
) X509Name
->bytes
->length
) {
76 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
79 *SubjectSize
= (UINTN
) X509Name
->bytes
->length
;
80 if (CertSubject
!= NULL
) {
81 CopyMem (CertSubject
, (UINT8
*)X509Name
->bytes
->data
, *SubjectSize
);
96 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
98 @param[in] Cert Pointer to the DER-encoded X509 certificate.
99 @param[in] CertSize Size of the X509 certificate in bytes.
100 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
101 RSA public key component. Use RsaFree() function to free the
104 If Cert is NULL, then ASSERT().
105 If RsaContext is NULL, then ASSERT().
107 @retval TRUE RSA Public Key was retrieved successfully.
108 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
113 RsaGetPublicKeyFromX509 (
114 IN CONST UINT8
*Cert
,
116 OUT VOID
**RsaContext
125 // ASSERT if Cert is NULL or RsaContext is NULL.
127 ASSERT (Cert
!= NULL
);
128 ASSERT (RsaContext
!= NULL
);
136 // Read DER-encoded X509 Certificate and Construct X509 object.
138 CertBio
= BIO_new (BIO_s_mem ());
139 BIO_write (CertBio
, Cert
, (int)CertSize
);
140 if (CertBio
== NULL
) {
143 X509Cert
= d2i_X509_bio (CertBio
, NULL
);
144 if (X509Cert
== NULL
) {
149 // Retrieve and check EVP_PKEY data from X509 Certificate.
151 Pkey
= X509_get_pubkey (X509Cert
);
152 if ((Pkey
== NULL
) || (Pkey
->type
!= EVP_PKEY_RSA
)) {
157 // Duplicate RSA Context from the retrieved EVP_PKEY.
159 if ((*RsaContext
= RSAPublicKey_dup (Pkey
->pkey
.rsa
)) != NULL
) {
165 // Release Resources.
168 X509_free (X509Cert
);
169 EVP_PKEY_free (Pkey
);
175 Verify one X509 certificate was issued by the trusted CA.
177 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
178 @param[in] CertSize Size of the X509 certificate in bytes.
179 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
180 @param[in] CACertSize Size of the CA Certificate in bytes.
182 If Cert is NULL, then ASSERT().
183 If CACert is NULL, then ASSERT().
185 @retval TRUE The certificate was issued by the trusted CA.
186 @retval FALSE Invalid certificate or the certificate was not issued by the given
193 IN CONST UINT8
*Cert
,
195 IN CONST UINT8
*CACert
,
204 X509_STORE
*CertStore
;
205 X509_STORE_CTX CertCtx
;
208 // ASSERT if Cert is NULL or CACert is NULL.
210 ASSERT (Cert
!= NULL
);
211 ASSERT (CACert
!= NULL
);
221 // Register & Initialize necessary digest algorithms for certificate verification.
223 EVP_add_digest (EVP_md5());
224 EVP_add_digest (EVP_sha1());
225 EVP_add_digest (EVP_sha256());
228 // Read DER-encoded certificate to be verified and Construct X509 object.
230 BioCert
= BIO_new (BIO_s_mem ());
231 BIO_write (BioCert
, Cert
, (int)CertSize
);
232 if (BioCert
== NULL
) {
235 X509Cert
= d2i_X509_bio (BioCert
, NULL
);
236 if (X509Cert
== NULL
) {
241 // Read DER-encoded root certificate and Construct X509 object.
243 BioCACert
= BIO_new (BIO_s_mem());
244 BIO_write (BioCACert
, CACert
, (int)CACertSize
);
245 if (BioCert
== NULL
) {
248 X509CACert
= d2i_X509_bio (BioCACert
, NULL
);
249 if (CACert
== NULL
) {
254 // Set up X509 Store for trusted certificate.
256 CertStore
= X509_STORE_new ();
257 if (CertStore
== NULL
) {
260 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
265 // Set up X509_STORE_CTX for the subsequent verification operation.
267 if (!X509_STORE_CTX_init (&CertCtx
, CertStore
, X509Cert
, NULL
)) {
272 // X509 Certificate Verification.
274 Status
= (BOOLEAN
) X509_verify_cert (&CertCtx
);
278 // Release Resources.
281 BIO_free (BioCACert
);
282 X509_free (X509Cert
);
283 X509_free (X509CACert
);
284 X509_STORE_free (CertStore
);
285 X509_STORE_CTX_cleanup (&CertCtx
);