2 PKCS#7 SignedData Verification Wrapper Implementation over OpenSSL.
4 Copyright (c) 2009 - 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"
17 #include <openssl/objects.h>
18 #include <openssl/x509.h>
19 #include <openssl/pkcs7.h>
23 Verification callback function to override any existing callbacks in OpenSSL
24 for intermediate certificate supports.
26 @param[in] Status Original status before calling this callback.
27 @param[in] Context X509 store context.
29 @retval 1 Current X509 certificate is verified successfully.
30 @retval 0 Verification failed.
36 IN X509_STORE_CTX
*Context
45 Error
= (INTN
) X509_STORE_CTX_get_error (Context
);
48 // X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT and X509_V_ERR_UNABLE_TO_GET_ISSUER_
49 // CERT_LOCALLY mean a X509 certificate is not self signed and its issuer
50 // can not be found in X509_verify_cert of X509_vfy.c.
51 // In order to support intermediate certificate node, we override the
52 // errors if the certification is obtained from X509 store, i.e. it is
53 // a trusted ceritifcate node that is enrolled by user.
54 // Besides,X509_V_ERR_CERT_UNTRUSTED and X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
55 // are also ignored to enable such feature.
57 if ((Error
== X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
) ||
58 (Error
== X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
)) {
59 Obj
= (X509_OBJECT
*) OPENSSL_malloc (sizeof (X509_OBJECT
));
64 Obj
->type
= X509_LU_X509
;
65 Obj
->data
.x509
= Context
->current_cert
;
67 CRYPTO_w_lock (CRYPTO_LOCK_X509_STORE
);
69 if (X509_OBJECT_retrieve_match (Context
->ctx
->objs
, Obj
)) {
73 // If any certificate in the chain is enrolled as trusted certificate,
74 // pass the certificate verification.
76 if (Error
== X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
) {
77 Count
= (INTN
) sk_X509_num (Context
->chain
);
78 for (Index
= 0; Index
< Count
; Index
++) {
79 Obj
->data
.x509
= sk_X509_value (Context
->chain
, (int) Index
);
80 if (X509_OBJECT_retrieve_match (Context
->ctx
->objs
, Obj
)) {
88 CRYPTO_w_unlock (CRYPTO_LOCK_X509_STORE
);
91 if ((Error
== X509_V_ERR_CERT_UNTRUSTED
) ||
92 (Error
== X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
)) {
104 Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message
105 Syntax Standard, version 1.5". This interface is only intended to be used for
106 application to perform PKCS#7 functionality validation.
108 @param[in] PrivateKey Pointer to the PEM-formatted private key data for
110 @param[in] PrivateKeySize Size of the PEM private key data in bytes.
111 @param[in] KeyPassword NULL-terminated passphrase used for encrypted PEM
113 @param[in] InData Pointer to the content to be signed.
114 @param[in] InDataSize Size of InData in bytes.
115 @param[in] SignCert Pointer to signer's DER-encoded certificate to sign with.
116 @param[in] OtherCerts Pointer to an optional additional set of certificates to
117 include in the PKCS#7 signedData (e.g. any intermediate
119 @param[out] SignedData Pointer to output PKCS#7 signedData.
120 @param[out] SignedDataSize Size of SignedData in bytes.
122 @retval TRUE PKCS#7 data signing succeeded.
123 @retval FALSE PKCS#7 data signing failed.
129 IN CONST UINT8
*PrivateKey
,
130 IN UINTN PrivateKeySize
,
131 IN CONST UINT8
*KeyPassword
,
135 IN UINT8
*OtherCerts OPTIONAL
,
136 OUT UINT8
**SignedData
,
137 OUT UINTN
*SignedDataSize
148 // Check input parameters.
150 if ((PrivateKey
== NULL
) || (KeyPassword
== NULL
) || (InData
== NULL
)) {
154 if ((SignCert
== NULL
) || (SignedData
== NULL
) || (SignedDataSize
== NULL
)) {
165 // Retrieve RSA private key from PEM data.
167 Status
= RsaGetPrivateKeyFromPem (
170 (CONST CHAR8
*) KeyPassword
,
171 (VOID
**) &RsaContext
178 // Register & Initialize necessary digest algorithms and PRNG for PKCS#7 Handling
180 EVP_add_digest (EVP_md5());
181 EVP_add_digest (EVP_sha1());
182 EVP_add_digest (EVP_sha256());
183 RandomSeed (NULL
, 0);
186 // Construct OpenSSL EVP_PKEY for private key.
188 Key
= EVP_PKEY_new ();
192 Key
->save_type
= EVP_PKEY_RSA
;
193 Key
->type
= EVP_PKEY_type (EVP_PKEY_RSA
);
194 Key
->pkey
.rsa
= (RSA
*) RsaContext
;
197 // Convert the data to be signed to BIO format.
199 DataBio
= BIO_new (BIO_s_mem ());
200 BIO_write (DataBio
, InData
, (int) InDataSize
);
203 // Create the PKCS#7 signedData structure.
208 (STACK_OF(X509
) *) OtherCerts
,
210 PKCS7_BINARY
| PKCS7_NOATTR
| PKCS7_DETACHED
217 // Convert PKCS#7 signedData structure into DER-encoded buffer.
219 *SignedDataSize
= i2d_PKCS7 (Pkcs7
, NULL
);
220 if (*SignedDataSize
== 0) {
223 *SignedData
= OPENSSL_malloc (*SignedDataSize
);
224 P7Data
= *SignedData
;
225 *SignedDataSize
= i2d_PKCS7 (Pkcs7
, (unsigned char **) &P7Data
);
233 if (RsaContext
!= NULL
) {
234 RsaFree (RsaContext
);
236 Key
->pkey
.rsa
= NULL
;
244 if (DataBio
!= NULL
) {
256 Verifies the validility of a PKCS#7 signed data as described in "PKCS #7: Cryptographic
257 Message Syntax Standard".
259 If P7Data is NULL, then ASSERT().
261 @param[in] P7Data Pointer to the PKCS#7 message to verify.
262 @param[in] P7Length Length of the PKCS#7 message in bytes.
263 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
264 is used for certificate chain verification.
265 @param[in] CertLength Length of the trusted certificate in bytes.
266 @param[in] InData Pointer to the content to be verified.
267 @param[in] DataLength Length of InData in bytes.
269 @retval TRUE The specified PKCS#7 signed data is valid.
270 @retval FALSE Invalid PKCS#7 signed data.
276 IN CONST UINT8
*P7Data
,
278 IN CONST UINT8
*TrustedCert
,
280 IN CONST UINT8
*InData
,
289 X509_STORE
*CertStore
;
292 // ASSERT if P7Data is NULL
294 ASSERT (P7Data
!= NULL
);
304 // Register & Initialize necessary digest algorithms for PKCS#7 Handling
306 EVP_add_digest (EVP_md5());
307 EVP_add_digest (EVP_sha1());
308 EVP_add_digest_alias (SN_sha1WithRSAEncryption
, SN_sha1WithRSA
);
309 EVP_add_digest (EVP_sha256());
312 // Retrieve PKCS#7 Data (DER encoding)
314 Pkcs7
= d2i_PKCS7 (NULL
, &P7Data
, (int)P7Length
);
320 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
322 if (!PKCS7_type_is_signed (Pkcs7
)) {
327 // Read DER-encoded root certificate and Construct X509 Certificate
329 CertBio
= BIO_new (BIO_s_mem ());
330 BIO_write (CertBio
, TrustedCert
, (int)CertLength
);
331 if (CertBio
== NULL
) {
334 Cert
= d2i_X509_bio (CertBio
, NULL
);
340 // Setup X509 Store for trusted certificate
342 CertStore
= X509_STORE_new ();
343 if (CertStore
== NULL
) {
346 if (!(X509_STORE_add_cert (CertStore
, Cert
))) {
351 // Register customized X509 verification callback function to support
352 // trusted intermediate certificate anchor.
354 CertStore
->verify_cb
= X509VerifyCb
;
357 // For generic PKCS#7 handling, InData may be NULL if the content is present
358 // in PKCS#7 structure. So ignore NULL checking here.
360 DataBio
= BIO_new (BIO_s_mem ());
361 BIO_write (DataBio
, InData
, (int)DataLength
);
364 // Verifies the PKCS#7 signedData structure
366 Status
= (BOOLEAN
) PKCS7_verify (Pkcs7
, NULL
, CertStore
, DataBio
, NULL
, PKCS7_BINARY
);
375 X509_STORE_free (CertStore
);