2008-11-06 02:39:08 +08:00
|
|
|
/*
|
2024-03-20 20:07:54 +08:00
|
|
|
* Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved.
|
1999-12-05 08:40:59 +08:00
|
|
|
*
|
2018-12-06 20:50:13 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:24:46 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-12-05 08:40:59 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Simple PKCS#7 processing functions */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2023-10-05 03:32:00 +08:00
|
|
|
#include "crypto/x509.h"
|
1999-12-05 08:40:59 +08:00
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/x509v3.h>
|
2020-07-25 17:11:03 +08:00
|
|
|
#include "pk7_local.h"
|
1999-12-05 08:40:59 +08:00
|
|
|
|
2015-09-04 20:13:19 +08:00
|
|
|
#define BUFFERSIZE 4096
|
|
|
|
|
2019-12-20 06:30:24 +08:00
|
|
|
|
2006-05-19 07:44:44 +08:00
|
|
|
static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
PKCS7 *PKCS7_sign_ex(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
|
2020-10-15 17:55:50 +08:00
|
|
|
BIO *data, int flags, OSSL_LIB_CTX *libctx,
|
2020-09-24 17:42:23 +08:00
|
|
|
const char *propq)
|
1999-12-05 08:40:59 +08:00
|
|
|
{
|
|
|
|
PKCS7 *p7;
|
|
|
|
int i;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
|
1999-12-05 08:40:59 +08:00
|
|
|
return NULL;
|
2006-05-18 01:17:01 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
if (!PKCS7_set_type(p7, NID_pkcs7_signed))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
if (!PKCS7_content_new(p7, NID_pkcs7_data))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_ADD_SIGNER_ERROR);
|
2006-05-18 01:17:01 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
if (!(flags & PKCS7_NOCERTS)) {
|
|
|
|
for (i = 0; i < sk_X509_num(certs); i++) {
|
|
|
|
if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i)))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2006-05-18 01:17:01 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-07-09 20:02:08 +08:00
|
|
|
if (flags & PKCS7_DETACHED)
|
|
|
|
PKCS7_set_detached(p7, 1);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
if (flags & (PKCS7_STREAM | PKCS7_PARTIAL))
|
|
|
|
return p7;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
if (PKCS7_final(p7, data, flags))
|
|
|
|
return p7;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
err:
|
|
|
|
PKCS7_free(p7);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-07-25 17:11:03 +08:00
|
|
|
PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
|
|
|
|
BIO *data, int flags)
|
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return PKCS7_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
|
2020-07-25 17:11:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
int PKCS7_final(PKCS7 *p7, BIO *data, int flags)
|
|
|
|
{
|
|
|
|
BIO *p7bio;
|
2006-05-24 21:29:32 +08:00
|
|
|
int ret = 0;
|
2015-09-04 20:13:19 +08:00
|
|
|
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
|
2006-05-18 01:17:01 +08:00
|
|
|
return 0;
|
1999-12-05 08:40:59 +08:00
|
|
|
}
|
2006-05-18 01:17:01 +08:00
|
|
|
|
2022-07-26 20:17:06 +08:00
|
|
|
if (!SMIME_crlf_copy(data, p7bio, flags))
|
|
|
|
goto err;
|
2006-05-27 01:14:23 +08:00
|
|
|
|
2007-08-13 01:44:32 +08:00
|
|
|
(void)BIO_flush(p7bio);
|
2006-05-18 01:17:01 +08:00
|
|
|
|
|
|
|
if (!PKCS7_dataFinal(p7, p7bio)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_DATASIGN);
|
2006-05-18 01:17:01 +08:00
|
|
|
goto err;
|
2000-09-05 21:27:57 +08:00
|
|
|
}
|
2006-05-18 01:17:01 +08:00
|
|
|
ret = 1;
|
2020-07-25 17:11:03 +08:00
|
|
|
err:
|
2006-05-18 01:17:01 +08:00
|
|
|
BIO_free_all(p7bio);
|
|
|
|
|
|
|
|
return ret;
|
2003-02-15 08:50:55 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
/* Check to see if a cipher exists and if so add S/MIME capabilities */
|
2003-02-15 08:50:55 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
|
|
|
|
{
|
|
|
|
if (EVP_get_cipherbynid(nid))
|
|
|
|
return PKCS7_simple_smimecap(sk, nid, arg);
|
|
|
|
return 1;
|
|
|
|
}
|
1999-12-05 08:40:59 +08:00
|
|
|
|
2006-06-06 20:35:05 +08:00
|
|
|
static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
|
|
|
|
{
|
|
|
|
if (EVP_get_digestbynid(nid))
|
|
|
|
return PKCS7_simple_smimecap(sk, nid, arg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
|
|
|
|
EVP_PKEY *pkey, const EVP_MD *md,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
PKCS7_SIGNER_INFO *si = NULL;
|
|
|
|
STACK_OF(X509_ALGOR) *smcap = NULL;
|
2020-07-25 17:11:03 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
if (!X509_check_private_key(signcert, pkey)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7,
|
|
|
|
PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
|
2006-05-18 01:17:01 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((si = PKCS7_add_signature(p7, signcert, pkey, md)) == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
|
1999-12-05 08:40:59 +08:00
|
|
|
return NULL;
|
2006-05-18 01:17:01 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2021-02-19 15:29:29 +08:00
|
|
|
si->ctx = ossl_pkcs7_get0_ctx(p7);
|
2006-05-18 01:17:01 +08:00
|
|
|
if (!(flags & PKCS7_NOCERTS)) {
|
|
|
|
if (!PKCS7_add_certificate(p7, signcert))
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
if (!(flags & PKCS7_NOATTR)) {
|
2006-05-19 01:20:23 +08:00
|
|
|
if (!PKCS7_add_attrib_content_type(si, NULL))
|
2006-05-18 01:17:01 +08:00
|
|
|
goto err;
|
|
|
|
/* Add SMIMECapabilities */
|
|
|
|
if (!(flags & PKCS7_NOSMIMECAP)) {
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((smcap = sk_X509_ALGOR_new_null()) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
|
2006-05-18 01:17:01 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2006-06-06 20:35:05 +08:00
|
|
|
if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
|
2015-09-29 23:43:27 +08:00
|
|
|
|| !add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
|
|
|
|
|| !add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
|
2006-06-06 20:35:05 +08:00
|
|
|
|| !add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
|
|
|
|
|| !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
|
|
|
|
|| !add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
|
|
|
|
|| !add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
|
|
|
|
|| !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
|
2006-05-18 01:17:01 +08:00
|
|
|
|| !add_cipher_smcap(smcap, NID_rc2_cbc, 128)
|
|
|
|
|| !add_cipher_smcap(smcap, NID_rc2_cbc, 64)
|
|
|
|
|| !add_cipher_smcap(smcap, NID_des_cbc, -1)
|
|
|
|
|| !add_cipher_smcap(smcap, NID_rc2_cbc, 40)
|
|
|
|
|| !PKCS7_add_attrib_smimecap(si, smcap))
|
|
|
|
goto err;
|
|
|
|
sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
|
2006-05-19 07:44:44 +08:00
|
|
|
smcap = NULL;
|
|
|
|
}
|
|
|
|
if (flags & PKCS7_REUSE_DIGEST) {
|
|
|
|
if (!pkcs7_copy_existing_digest(p7, si))
|
|
|
|
goto err;
|
2020-07-25 17:11:03 +08:00
|
|
|
if (!(flags & PKCS7_PARTIAL)
|
|
|
|
&& !PKCS7_SIGNER_INFO_sign(si))
|
2006-05-19 07:44:44 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2006-05-18 01:17:01 +08:00
|
|
|
}
|
|
|
|
return si;
|
|
|
|
err:
|
2015-05-01 05:33:59 +08:00
|
|
|
sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
|
2006-05-18 01:17:01 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-19 07:44:44 +08:00
|
|
|
/*
|
|
|
|
* Search for a digest matching SignerInfo digest type and if found copy
|
|
|
|
* across.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
|
|
|
|
PKCS7_SIGNER_INFO *sitmp;
|
|
|
|
ASN1_OCTET_STRING *osdig = NULL;
|
|
|
|
sinfos = PKCS7_get_signer_info(p7);
|
|
|
|
for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
|
|
|
|
sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
|
|
|
|
if (si == sitmp)
|
|
|
|
break;
|
|
|
|
if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
|
|
|
|
continue;
|
|
|
|
if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
|
|
|
|
osdig = PKCS7_digest_from_attributes(sitmp->auth_attr);
|
|
|
|
break;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-19 07:44:44 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-07-25 17:11:03 +08:00
|
|
|
if (osdig != NULL)
|
2006-05-19 07:44:44 +08:00
|
|
|
return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
|
2006-05-19 07:44:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-31 16:27:42 +08:00
|
|
|
/* This strongly overlaps with CMS_verify(), partly with PKCS7_dataVerify() */
|
1999-12-05 08:40:59 +08:00
|
|
|
int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
|
|
|
|
BIO *indata, BIO *out, int flags)
|
|
|
|
{
|
|
|
|
STACK_OF(X509) *signers;
|
2023-10-05 03:32:00 +08:00
|
|
|
STACK_OF(X509) *included_certs;
|
|
|
|
STACK_OF(X509) *untrusted = NULL;
|
1999-12-05 08:40:59 +08:00
|
|
|
X509 *signer;
|
|
|
|
STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
|
|
|
|
PKCS7_SIGNER_INFO *si;
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX *cert_ctx = NULL;
|
2015-09-04 20:13:19 +08:00
|
|
|
char *buf = NULL;
|
2001-02-24 09:38:56 +08:00
|
|
|
int i, j = 0, k, ret = 0;
|
2015-08-28 11:07:07 +08:00
|
|
|
BIO *p7bio = NULL;
|
2021-09-13 02:54:43 +08:00
|
|
|
BIO *tmpout = NULL;
|
2020-07-25 17:11:03 +08:00
|
|
|
const PKCS7_CTX *p7_ctx;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (p7 == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
|
1999-12-12 04:04:06 +08:00
|
|
|
return 0;
|
1999-12-09 09:31:32 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-07 10:35:52 +08:00
|
|
|
if (!PKCS7_type_is_signed(p7)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
|
1999-12-05 08:40:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
/* Check for no data and no content: no data to verify signature */
|
2022-08-31 16:27:42 +08:00
|
|
|
if (PKCS7_get_detached(p7) && indata == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
|
1999-12-05 08:40:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-31 04:35:49 +08:00
|
|
|
|
2016-02-23 01:07:06 +08:00
|
|
|
if (flags & PKCS7_NO_DUAL_CONTENT) {
|
|
|
|
/*
|
|
|
|
* This was originally "#if 0" because we thought that only old broken
|
|
|
|
* Netscape did this. It turns out that Authenticode uses this kind
|
|
|
|
* of "extended" PKCS7 format, and things like UEFI secure boot and
|
|
|
|
* tools like osslsigncode need it. In Authenticode the verification
|
|
|
|
* process is different, but the existing PKCs7 verification works.
|
|
|
|
*/
|
2022-08-31 16:27:42 +08:00
|
|
|
if (!PKCS7_get_detached(p7) && indata != NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CONTENT_AND_DATA_PRESENT);
|
2016-02-23 01:07:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|
1999-12-05 08:40:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sinfos = PKCS7_get_signer_info(p7);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNATURES_ON_DATA);
|
1999-12-05 08:40:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2000-02-26 09:55:33 +08:00
|
|
|
signers = PKCS7_get0_signers(p7, certs, flags);
|
2020-07-25 17:11:03 +08:00
|
|
|
if (signers == NULL)
|
1999-12-07 10:35:52 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
/* Now verify the certificates */
|
2021-02-19 15:29:29 +08:00
|
|
|
p7_ctx = ossl_pkcs7_get0_ctx(p7);
|
|
|
|
cert_ctx = X509_STORE_CTX_new_ex(ossl_pkcs7_ctx_get0_libctx(p7_ctx),
|
|
|
|
ossl_pkcs7_ctx_get0_propq(p7_ctx));
|
2016-04-15 11:59:26 +08:00
|
|
|
if (cert_ctx == NULL)
|
|
|
|
goto err;
|
2023-10-05 03:32:00 +08:00
|
|
|
if ((flags & PKCS7_NOVERIFY) == 0) {
|
|
|
|
if (!ossl_x509_add_certs_new(&untrusted, certs, X509_ADD_FLAG_NO_DUP))
|
|
|
|
goto err;
|
|
|
|
included_certs = pkcs7_get0_certificates(p7);
|
|
|
|
if ((flags & PKCS7_NOCHAIN) == 0
|
|
|
|
&& !ossl_x509_add_certs_new(&untrusted, included_certs,
|
|
|
|
X509_ADD_FLAG_NO_DUP))
|
|
|
|
goto err;
|
|
|
|
|
2000-09-08 01:42:25 +08:00
|
|
|
for (k = 0; k < sk_X509_num(signers); k++) {
|
|
|
|
signer = sk_X509_value(signers, k);
|
2023-10-05 03:32:00 +08:00
|
|
|
if (!X509_STORE_CTX_init(cert_ctx, store, signer, untrusted)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
|
2015-08-28 11:07:07 +08:00
|
|
|
goto err;
|
2001-09-02 04:02:13 +08:00
|
|
|
}
|
2023-10-05 03:32:00 +08:00
|
|
|
if ((flags & PKCS7_NOCHAIN) == 0
|
|
|
|
&& !X509_STORE_CTX_set_default(cert_ctx, "smime_sign"))
|
|
|
|
goto err;
|
2004-03-28 06:49:28 +08:00
|
|
|
if (!(flags & PKCS7_NOCRL))
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl);
|
|
|
|
i = X509_verify_cert(cert_ctx);
|
1999-12-05 08:40:59 +08:00
|
|
|
if (i <= 0) {
|
2024-03-05 01:34:02 +08:00
|
|
|
j = X509_STORE_CTX_get_error(cert_ctx);
|
2020-11-04 23:14:00 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PKCS7, PKCS7_R_CERTIFICATE_VERIFY_ERROR,
|
|
|
|
"Verify error: %s",
|
|
|
|
X509_verify_cert_error_string(j));
|
2015-08-28 11:07:07 +08:00
|
|
|
goto err;
|
1999-12-05 08:40:59 +08:00
|
|
|
}
|
|
|
|
/* Check for revocation status here */
|
|
|
|
}
|
2023-10-05 03:32:00 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2021-09-13 02:54:43 +08:00
|
|
|
if ((p7bio = PKCS7_dataInit(p7, indata)) == NULL)
|
2007-02-03 17:55:42 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
if (flags & PKCS7_TEXT) {
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
|
1999-12-05 08:40:59 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2008-11-22 02:18:13 +08:00
|
|
|
BIO_set_mem_eof_return(tmpout, 0);
|
1999-12-05 08:40:59 +08:00
|
|
|
} else
|
|
|
|
tmpout = out;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
/* We now have to 'read' from p7bio to calculate digests etc. */
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL)
|
2015-09-04 20:13:19 +08:00
|
|
|
goto err;
|
1999-12-05 08:40:59 +08:00
|
|
|
for (;;) {
|
2015-09-04 20:13:19 +08:00
|
|
|
i = BIO_read(p7bio, buf, BUFFERSIZE);
|
1999-12-05 08:40:59 +08:00
|
|
|
if (i <= 0)
|
|
|
|
break;
|
|
|
|
if (tmpout)
|
|
|
|
BIO_write(tmpout, buf, i);
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
if (flags & PKCS7_TEXT) {
|
|
|
|
if (!SMIME_text(tmpout, out)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SMIME_TEXT_ERROR);
|
1999-12-05 08:40:59 +08:00
|
|
|
BIO_free(tmpout);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
BIO_free(tmpout);
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
/* Now Verify All Signatures */
|
|
|
|
if (!(flags & PKCS7_NOSIGS))
|
|
|
|
for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
|
|
|
|
si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
|
|
|
|
signer = sk_X509_value(signers, i);
|
|
|
|
j = PKCS7_signatureVerify(p7bio, p7, si, signer);
|
|
|
|
if (j <= 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
|
1999-12-05 08:40:59 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2001-02-24 09:38:56 +08:00
|
|
|
ret = 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
err:
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX_free(cert_ctx);
|
2015-09-04 20:13:19 +08:00
|
|
|
OPENSSL_free(buf);
|
2022-08-31 16:27:42 +08:00
|
|
|
if (indata != NULL)
|
2021-09-13 02:54:43 +08:00
|
|
|
BIO_pop(p7bio);
|
2005-05-17 13:52:24 +08:00
|
|
|
BIO_free_all(p7bio);
|
1999-12-05 08:40:59 +08:00
|
|
|
sk_X509_free(signers);
|
2023-10-05 03:32:00 +08:00
|
|
|
sk_X509_free(untrusted);
|
2001-02-24 09:38:56 +08:00
|
|
|
return ret;
|
1999-12-05 08:40:59 +08:00
|
|
|
}
|
|
|
|
|
2000-02-26 09:55:33 +08:00
|
|
|
STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
|
|
|
|
int flags)
|
1999-12-07 10:35:52 +08:00
|
|
|
{
|
2023-10-05 03:32:00 +08:00
|
|
|
STACK_OF(X509) *signers, *included_certs;
|
1999-12-07 10:35:52 +08:00
|
|
|
STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
|
|
|
|
PKCS7_SIGNER_INFO *si;
|
|
|
|
PKCS7_ISSUER_AND_SERIAL *ias;
|
|
|
|
X509 *signer;
|
|
|
|
int i;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (p7 == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
|
1999-12-09 09:31:32 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-07 10:35:52 +08:00
|
|
|
if (!PKCS7_type_is_signed(p7)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
|
1999-12-07 10:35:52 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-10-05 03:32:00 +08:00
|
|
|
included_certs = pkcs7_get0_certificates(p7);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-07 10:35:52 +08:00
|
|
|
/* Collect all the signers together */
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-07 10:35:52 +08:00
|
|
|
sinfos = PKCS7_get_signer_info(p7);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-07 10:35:52 +08:00
|
|
|
if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNERS);
|
1999-12-07 10:35:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((signers = sk_X509_new_null()) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
|
2005-02-15 05:53:24 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-07 10:35:52 +08:00
|
|
|
for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
|
|
|
|
si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
|
|
|
|
ias = si->issuer_and_serial;
|
|
|
|
signer = NULL;
|
|
|
|
/* If any certificates passed they take priority */
|
2023-10-05 03:32:00 +08:00
|
|
|
signer = X509_find_by_issuer_and_serial(certs,
|
|
|
|
ias->issuer, ias->serial);
|
|
|
|
if (signer == NULL && (flags & PKCS7_NOINTERN) == 0)
|
|
|
|
signer = X509_find_by_issuer_and_serial(included_certs,
|
1999-12-07 10:35:52 +08:00
|
|
|
ias->issuer, ias->serial);
|
2022-08-31 16:27:42 +08:00
|
|
|
if (signer == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
|
1999-12-07 10:35:52 +08:00
|
|
|
sk_X509_free(signers);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2007-02-03 17:55:42 +08:00
|
|
|
if (!sk_X509_push(signers, signer)) {
|
|
|
|
sk_X509_free(signers);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-12-07 10:35:52 +08:00
|
|
|
}
|
|
|
|
return signers;
|
|
|
|
}
|
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
/* Build a complete PKCS#7 enveloped data */
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
PKCS7 *PKCS7_encrypt_ex(STACK_OF(X509) *certs, BIO *in,
|
|
|
|
const EVP_CIPHER *cipher, int flags,
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx, const char *propq)
|
1999-12-05 08:40:59 +08:00
|
|
|
{
|
|
|
|
PKCS7 *p7;
|
|
|
|
BIO *p7bio = NULL;
|
|
|
|
int i;
|
|
|
|
X509 *x509;
|
2020-07-25 17:11:03 +08:00
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
|
1999-12-05 08:40:59 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2007-02-03 17:55:42 +08:00
|
|
|
if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
|
|
|
|
goto err;
|
|
|
|
if (!PKCS7_set_cipher(p7, cipher)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_SETTING_CIPHER);
|
1999-12-05 08:40:59 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
for (i = 0; i < sk_X509_num(certs); i++) {
|
|
|
|
x509 = sk_X509_value(certs, i);
|
|
|
|
if (!PKCS7_add_recipient(p7, x509)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_ADDING_RECIPIENT);
|
1999-12-05 08:40:59 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-12-25 00:22:56 +08:00
|
|
|
if (flags & PKCS7_STREAM)
|
|
|
|
return p7;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-18 01:17:01 +08:00
|
|
|
if (PKCS7_final(p7, in, flags))
|
|
|
|
return p7;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2007-02-03 17:55:42 +08:00
|
|
|
BIO_free_all(p7bio);
|
1999-12-05 08:40:59 +08:00
|
|
|
PKCS7_free(p7);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-25 17:11:03 +08:00
|
|
|
PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
|
|
|
|
int flags)
|
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return PKCS7_encrypt_ex(certs, in, cipher, flags, NULL, NULL);
|
2020-07-25 17:11:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
|
|
|
|
{
|
|
|
|
BIO *tmpmem;
|
2015-09-06 18:56:33 +08:00
|
|
|
int ret = 0, i;
|
2015-09-04 20:13:19 +08:00
|
|
|
char *buf = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (p7 == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
|
1999-12-09 09:31:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2023-01-19 01:07:55 +08:00
|
|
|
if (!PKCS7_type_is_enveloped(p7)
|
|
|
|
&& !PKCS7_type_is_signedAndEnveloped(p7)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
|
1999-12-05 08:40:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2005-08-05 06:15:22 +08:00
|
|
|
if (cert && !X509_check_private_key(cert, pkey)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7,
|
|
|
|
PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
|
1999-12-05 08:40:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DECRYPT_ERROR);
|
1999-12-05 08:40:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-12-05 08:40:59 +08:00
|
|
|
if (flags & PKCS7_TEXT) {
|
|
|
|
BIO *tmpbuf, *bread;
|
|
|
|
/* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
|
2007-02-03 17:55:42 +08:00
|
|
|
BIO_free_all(tmpmem);
|
1999-12-05 08:40:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
|
2007-02-03 17:55:42 +08:00
|
|
|
BIO_free_all(tmpbuf);
|
|
|
|
BIO_free_all(tmpmem);
|
1999-12-05 08:40:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = SMIME_text(bread, data);
|
2012-02-27 23:22:41 +08:00
|
|
|
if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
|
2022-05-29 00:07:04 +08:00
|
|
|
if (BIO_get_cipher_status(tmpmem) <= 0)
|
2012-02-27 23:22:41 +08:00
|
|
|
ret = 0;
|
|
|
|
}
|
1999-12-05 08:40:59 +08:00
|
|
|
BIO_free_all(bread);
|
|
|
|
return ret;
|
2015-09-04 20:13:19 +08:00
|
|
|
}
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL)
|
2015-09-04 20:13:19 +08:00
|
|
|
goto err;
|
|
|
|
for (;;) {
|
|
|
|
i = BIO_read(tmpmem, buf, BUFFERSIZE);
|
|
|
|
if (i <= 0) {
|
|
|
|
ret = 1;
|
|
|
|
if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
|
2022-05-29 00:07:04 +08:00
|
|
|
if (BIO_get_cipher_status(tmpmem) <= 0)
|
2015-09-04 20:13:19 +08:00
|
|
|
ret = 0;
|
2012-03-08 22:10:23 +08:00
|
|
|
}
|
2015-09-04 20:13:19 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (BIO_write(data, buf, i) != i) {
|
|
|
|
break;
|
1999-12-05 08:40:59 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 20:13:19 +08:00
|
|
|
err:
|
|
|
|
OPENSSL_free(buf);
|
|
|
|
BIO_free_all(tmpmem);
|
|
|
|
return ret;
|
1999-12-05 08:40:59 +08:00
|
|
|
}
|