2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2021-01-28 20:54:57 +08:00
|
|
|
* Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2008-03-13 05:14:28 +08:00
|
|
|
*
|
2018-12-06 20:32:50 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +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
|
2008-03-13 05:14:28 +08:00
|
|
|
*/
|
|
|
|
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2008-03-13 05:14:28 +08:00
|
|
|
#include <openssl/asn1t.h>
|
|
|
|
#include <openssl/pem.h>
|
2013-07-17 21:36:39 +08:00
|
|
|
#include <openssl/x509.h>
|
2008-03-13 05:14:28 +08:00
|
|
|
#include <openssl/x509v3.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/cms.h>
|
2019-07-31 17:14:12 +08:00
|
|
|
#include <openssl/ess.h>
|
2021-03-10 19:58:53 +08:00
|
|
|
#include "internal/sizes.h"
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/asn1.h"
|
|
|
|
#include "crypto/evp.h"
|
|
|
|
#include "crypto/ess.h"
|
2021-02-17 19:29:39 +08:00
|
|
|
#include "crypto/x509.h" /* for ossl_x509_add_cert_new() */
|
2021-03-10 19:58:53 +08:00
|
|
|
#include "cms_local.h"
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
/* CMS SignedData Utilities */
|
|
|
|
|
|
|
|
static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return cms->d.signedData;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (cms->d.other == NULL) {
|
|
|
|
cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
|
|
|
|
if (!cms->d.signedData) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cms->d.signedData->version = 1;
|
|
|
|
cms->d.signedData->encapContentInfo->eContentType =
|
|
|
|
OBJ_nid2obj(NID_pkcs7_data);
|
|
|
|
cms->d.signedData->encapContentInfo->partial = 1;
|
|
|
|
ASN1_OBJECT_free(cms->contentType);
|
|
|
|
cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
|
|
|
|
return cms->d.signedData;
|
|
|
|
}
|
|
|
|
return cms_get0_signed(cms);
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2016-02-06 04:23:54 +08:00
|
|
|
/* Just initialise SignedData e.g. for certs only structure */
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
int CMS_SignedData_init(CMS_ContentInfo *cms)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (cms_signed_data_init(cms))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2008-03-13 05:14:28 +08:00
|
|
|
/* Check structures and fixup version numbers (if necessary) */
|
|
|
|
|
|
|
|
static void cms_sd_set_version(CMS_SignedData *sd)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
CMS_CertificateChoices *cch;
|
|
|
|
CMS_RevocationInfoChoice *rch;
|
|
|
|
CMS_SignerInfo *si;
|
|
|
|
|
|
|
|
for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
|
|
|
|
cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
|
|
|
|
if (cch->type == CMS_CERTCHOICE_OTHER) {
|
|
|
|
if (sd->version < 5)
|
|
|
|
sd->version = 5;
|
|
|
|
} else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
|
|
|
|
if (sd->version < 4)
|
|
|
|
sd->version = 4;
|
|
|
|
} else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
|
|
|
|
if (sd->version < 3)
|
|
|
|
sd->version = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
|
|
|
|
rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
|
|
|
|
if (rch->type == CMS_REVCHOICE_OTHER) {
|
|
|
|
if (sd->version < 5)
|
|
|
|
sd->version = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
|
|
|
|
&& (sd->version < 3))
|
|
|
|
sd->version = 3;
|
|
|
|
|
|
|
|
for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
|
|
|
|
si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
|
|
|
|
if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
|
|
|
|
if (si->version < 3)
|
|
|
|
si->version = 3;
|
|
|
|
if (sd->version < 3)
|
|
|
|
sd->version = 3;
|
|
|
|
} else if (si->version < 1)
|
|
|
|
si->version = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sd->version < 1)
|
|
|
|
sd->version = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-03 13:19:48 +08:00
|
|
|
/*
|
|
|
|
* RFC 5652 Section 11.1 Content Type
|
|
|
|
* The content-type attribute within signed-data MUST
|
|
|
|
* 1) be present if there are signed attributes
|
|
|
|
* 2) match the content type in the signed-data,
|
|
|
|
* 3) be a signed attribute.
|
|
|
|
* 4) not have more than one copy of the attribute.
|
|
|
|
*
|
|
|
|
* Note that since the CMS_SignerInfo_sign() always adds the "signing time"
|
|
|
|
* attribute, the content type attribute MUST be added also.
|
|
|
|
* Assumptions: This assumes that the attribute does not already exist.
|
|
|
|
*/
|
|
|
|
static int cms_set_si_contentType_attr(CMS_ContentInfo *cms, CMS_SignerInfo *si)
|
|
|
|
{
|
|
|
|
ASN1_OBJECT *ctype = cms->d.signedData->encapContentInfo->eContentType;
|
|
|
|
|
|
|
|
/* Add the contentType attribute */
|
|
|
|
return CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
|
|
|
|
V_ASN1_OBJECT, ctype, -1) > 0;
|
|
|
|
}
|
|
|
|
|
2008-03-13 05:14:28 +08:00
|
|
|
/* Copy an existing messageDigest value */
|
|
|
|
|
|
|
|
static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
STACK_OF(CMS_SignerInfo) *sinfos;
|
|
|
|
CMS_SignerInfo *sitmp;
|
|
|
|
int i;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
sinfos = CMS_get0_SignerInfos(cms);
|
|
|
|
for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
|
|
|
|
ASN1_OCTET_STRING *messageDigest;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
sitmp = sk_CMS_SignerInfo_value(sinfos, i);
|
|
|
|
if (sitmp == si)
|
|
|
|
continue;
|
|
|
|
if (CMS_signed_get_attr_count(sitmp) < 0)
|
|
|
|
continue;
|
|
|
|
if (OBJ_cmp(si->digestAlgorithm->algorithm,
|
|
|
|
sitmp->digestAlgorithm->algorithm))
|
|
|
|
continue;
|
|
|
|
messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
|
|
|
|
OBJ_nid2obj
|
|
|
|
(NID_pkcs9_messageDigest),
|
|
|
|
-3, V_ASN1_OCTET_STRING);
|
|
|
|
if (!messageDigest) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
|
|
|
|
V_ASN1_OCTET_STRING,
|
|
|
|
messageDigest, -1))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2021-02-18 12:03:25 +08:00
|
|
|
int ossl_cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
|
|
|
|
int type, const CMS_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case CMS_SIGNERINFO_ISSUER_SERIAL:
|
2021-02-18 12:03:25 +08:00
|
|
|
if (!ossl_cms_set1_ias(&sid->d.issuerAndSerialNumber, cert))
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
break;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
case CMS_SIGNERINFO_KEYIDENTIFIER:
|
2021-02-18 12:03:25 +08:00
|
|
|
if (!ossl_cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert))
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
break;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
default:
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_ID);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
sid->type = type;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2021-02-18 12:03:25 +08:00
|
|
|
int ossl_cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
|
|
|
|
ASN1_OCTET_STRING **keyid,
|
|
|
|
X509_NAME **issuer,
|
|
|
|
ASN1_INTEGER **sno)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
|
|
|
|
if (issuer)
|
|
|
|
*issuer = sid->d.issuerAndSerialNumber->issuer;
|
|
|
|
if (sno)
|
|
|
|
*sno = sid->d.issuerAndSerialNumber->serialNumber;
|
|
|
|
} else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
|
|
|
|
if (keyid)
|
|
|
|
*keyid = sid->d.subjectKeyIdentifier;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2021-02-18 12:03:25 +08:00
|
|
|
int ossl_cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
|
2021-02-18 12:03:25 +08:00
|
|
|
return ossl_cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
|
2015-01-22 11:40:55 +08:00
|
|
|
else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
|
2021-02-18 12:03:25 +08:00
|
|
|
return ossl_cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
|
2015-01-22 11:40:55 +08:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2013-06-20 01:17:14 +08:00
|
|
|
static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
EVP_PKEY *pkey = si->pkey;
|
|
|
|
int i;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2020-10-07 21:45:22 +08:00
|
|
|
if (EVP_PKEY_is_a(pkey, "DSA") || EVP_PKEY_is_a(pkey, "EC"))
|
2021-02-18 12:03:25 +08:00
|
|
|
return ossl_cms_ecdsa_dsa_sign(si, cmd);
|
2020-12-03 01:27:03 +08:00
|
|
|
else if (EVP_PKEY_is_a(pkey, "RSA") || EVP_PKEY_is_a(pkey, "RSA-PSS"))
|
2021-02-18 12:03:25 +08:00
|
|
|
return ossl_cms_rsa_sign(si, cmd);
|
2020-10-07 21:45:22 +08:00
|
|
|
|
|
|
|
/* Something else? We'll give engines etc a chance to handle this */
|
2019-09-17 03:28:57 +08:00
|
|
|
if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
|
|
|
|
if (i == -2) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (i <= 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-20 01:17:14 +08:00
|
|
|
|
2021-03-16 23:41:52 +08:00
|
|
|
/* Add SigningCertificate signed attribute to the signer info. */
|
|
|
|
static int ossl_cms_add1_signing_cert(CMS_SignerInfo *si,
|
|
|
|
const ESS_SIGNING_CERT *sc)
|
|
|
|
{
|
|
|
|
ASN1_STRING *seq = NULL;
|
|
|
|
unsigned char *p, *pp = NULL;
|
|
|
|
int ret, len = i2d_ESS_SIGNING_CERT(sc, NULL);
|
|
|
|
|
|
|
|
if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = pp;
|
|
|
|
i2d_ESS_SIGNING_CERT(sc, &p);
|
|
|
|
if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len)) {
|
|
|
|
ASN1_STRING_free(seq);
|
|
|
|
OPENSSL_free(pp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
OPENSSL_free(pp);
|
|
|
|
ret = CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
|
|
|
|
V_ASN1_SEQUENCE, seq, -1);
|
|
|
|
ASN1_STRING_free(seq);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add SigningCertificateV2 signed attribute to the signer info. */
|
|
|
|
static int ossl_cms_add1_signing_cert_v2(CMS_SignerInfo *si,
|
|
|
|
const ESS_SIGNING_CERT_V2 *sc)
|
|
|
|
{
|
|
|
|
ASN1_STRING *seq = NULL;
|
|
|
|
unsigned char *p, *pp = NULL;
|
|
|
|
int ret, len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
|
|
|
|
|
|
|
|
if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = pp;
|
|
|
|
i2d_ESS_SIGNING_CERT_V2(sc, &p);
|
|
|
|
if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len)) {
|
|
|
|
ASN1_STRING_free(seq);
|
|
|
|
OPENSSL_free(pp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
OPENSSL_free(pp);
|
|
|
|
ret = CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
|
|
|
|
V_ASN1_SEQUENCE, seq, -1);
|
|
|
|
ASN1_STRING_free(seq);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-13 05:14:28 +08:00
|
|
|
CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
|
2015-01-22 11:40:55 +08:00
|
|
|
X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
CMS_SignedData *sd;
|
|
|
|
CMS_SignerInfo *si = NULL;
|
|
|
|
X509_ALGOR *alg;
|
|
|
|
int i, type;
|
2021-02-18 12:03:25 +08:00
|
|
|
const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!X509_check_private_key(signer, pk)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sd = cms_signed_data_init(cms);
|
|
|
|
if (!sd)
|
|
|
|
goto err;
|
|
|
|
si = M_ASN1_new_of(CMS_SignerInfo);
|
|
|
|
if (!si)
|
|
|
|
goto merr;
|
2016-01-28 10:54:09 +08:00
|
|
|
/* Call for side-effect of computing hash and caching extensions */
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_check_purpose(signer, -1, -1);
|
|
|
|
|
2015-09-01 03:29:57 +08:00
|
|
|
X509_up_ref(signer);
|
2016-02-26 20:21:15 +08:00
|
|
|
EVP_PKEY_up_ref(pk);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-07-25 16:04:55 +08:00
|
|
|
si->cms_ctx = ctx;
|
2015-01-22 11:40:55 +08:00
|
|
|
si->pkey = pk;
|
|
|
|
si->signer = signer;
|
2015-12-02 07:49:35 +08:00
|
|
|
si->mctx = EVP_MD_CTX_new();
|
2015-01-22 11:40:55 +08:00
|
|
|
si->pctx = NULL;
|
|
|
|
|
2015-11-27 21:02:12 +08:00
|
|
|
if (si->mctx == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
|
2015-11-27 21:02:12 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (flags & CMS_USE_KEYID) {
|
|
|
|
si->version = 3;
|
|
|
|
if (sd->version < 3)
|
|
|
|
sd->version = 3;
|
|
|
|
type = CMS_SIGNERINFO_KEYIDENTIFIER;
|
|
|
|
} else {
|
|
|
|
type = CMS_SIGNERINFO_ISSUER_SERIAL;
|
|
|
|
si->version = 1;
|
|
|
|
}
|
|
|
|
|
2021-02-18 12:03:25 +08:00
|
|
|
if (!ossl_cms_set1_SignerIdentifier(si->sid, signer, type, ctx))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (md == NULL) {
|
|
|
|
int def_nid;
|
|
|
|
if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
|
|
|
|
goto err;
|
|
|
|
md = EVP_get_digestbynid(def_nid);
|
|
|
|
if (md == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_NO_DEFAULT_DIGEST);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!md) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_NO_DIGEST_SET);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:04:55 +08:00
|
|
|
if (md == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_NO_DIGEST_SET);
|
2020-07-25 16:04:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_ALGOR_set_md(si->digestAlgorithm, md);
|
|
|
|
|
|
|
|
/* See if digest is present in digestAlgorithms */
|
|
|
|
for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
|
2016-08-17 03:18:04 +08:00
|
|
|
const ASN1_OBJECT *aoid;
|
2021-03-10 19:58:53 +08:00
|
|
|
char name[OSSL_MAX_NAME_SIZE];
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
|
|
|
|
X509_ALGOR_get0(&aoid, NULL, NULL, alg);
|
2021-03-10 19:58:53 +08:00
|
|
|
OBJ_obj2txt(name, sizeof(name), aoid, 0);
|
|
|
|
if (EVP_MD_is_a(md, name))
|
2015-01-22 11:40:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
|
|
|
|
alg = X509_ALGOR_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (alg == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto merr;
|
|
|
|
X509_ALGOR_set_md(alg, md);
|
|
|
|
if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
|
|
|
|
X509_ALGOR_free(alg);
|
|
|
|
goto merr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0))
|
|
|
|
goto err;
|
|
|
|
if (!(flags & CMS_NOATTR)) {
|
|
|
|
/*
|
2016-02-06 04:23:54 +08:00
|
|
|
* Initialize signed attributes structure so other attributes
|
2015-01-22 11:40:55 +08:00
|
|
|
* such as signing time etc are added later even if we add none here.
|
|
|
|
*/
|
|
|
|
if (!si->signedAttrs) {
|
|
|
|
si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
|
|
|
|
if (!si->signedAttrs)
|
|
|
|
goto merr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & CMS_NOSMIMECAP)) {
|
|
|
|
STACK_OF(X509_ALGOR) *smcap = NULL;
|
|
|
|
i = CMS_add_standard_smimecap(&smcap);
|
|
|
|
if (i)
|
|
|
|
i = CMS_add_smimecap(si, smcap);
|
|
|
|
sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
|
|
|
|
if (!i)
|
|
|
|
goto merr;
|
|
|
|
}
|
2018-12-13 06:08:49 +08:00
|
|
|
if (flags & CMS_CADES) {
|
|
|
|
ESS_SIGNING_CERT *sc = NULL;
|
|
|
|
ESS_SIGNING_CERT_V2 *sc2 = NULL;
|
|
|
|
int add_sc;
|
|
|
|
|
2021-03-05 06:22:56 +08:00
|
|
|
if (md == NULL || EVP_MD_is_a(md, SN_sha1)) {
|
2021-03-16 03:24:40 +08:00
|
|
|
if ((sc = OSSL_ESS_signing_cert_new_init(signer,
|
2021-02-18 12:03:25 +08:00
|
|
|
NULL, 1)) == NULL)
|
2018-12-13 06:08:49 +08:00
|
|
|
goto err;
|
2021-02-18 12:03:25 +08:00
|
|
|
add_sc = ossl_cms_add1_signing_cert(si, sc);
|
2018-12-13 06:08:49 +08:00
|
|
|
ESS_SIGNING_CERT_free(sc);
|
|
|
|
} else {
|
2021-03-16 03:24:40 +08:00
|
|
|
if ((sc2 = OSSL_ESS_signing_cert_v2_new_init(md, signer,
|
2021-02-18 12:03:25 +08:00
|
|
|
NULL, 1)) == NULL)
|
2018-12-13 06:08:49 +08:00
|
|
|
goto err;
|
2021-02-18 12:03:25 +08:00
|
|
|
add_sc = ossl_cms_add1_signing_cert_v2(si, sc2);
|
2018-12-13 06:08:49 +08:00
|
|
|
ESS_SIGNING_CERT_V2_free(sc2);
|
|
|
|
}
|
|
|
|
if (!add_sc)
|
|
|
|
goto err;
|
|
|
|
}
|
2019-01-30 06:54:30 +08:00
|
|
|
if (flags & CMS_REUSE_DIGEST) {
|
|
|
|
if (!cms_copy_messageDigest(cms, si))
|
|
|
|
goto err;
|
2019-06-03 13:19:48 +08:00
|
|
|
if (!cms_set_si_contentType_attr(cms, si))
|
|
|
|
goto err;
|
2019-01-30 06:54:30 +08:00
|
|
|
if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
|
|
|
|
!CMS_SignerInfo_sign(si))
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & CMS_NOCERTS)) {
|
|
|
|
/* NB ignore -1 return for duplicate cert */
|
|
|
|
if (!CMS_add1_cert(cms, signer))
|
|
|
|
goto merr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & CMS_KEY_PARAM) {
|
|
|
|
if (flags & CMS_NOATTR) {
|
2021-02-18 12:03:25 +08:00
|
|
|
si->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
|
2020-12-11 17:19:37 +08:00
|
|
|
si->pkey,
|
2021-02-18 12:03:25 +08:00
|
|
|
ossl_cms_ctx_get0_propq(ctx));
|
2015-10-30 19:12:26 +08:00
|
|
|
if (si->pctx == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (EVP_PKEY_sign_init(si->pctx) <= 0)
|
|
|
|
goto err;
|
|
|
|
if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
|
|
|
|
goto err;
|
2020-09-24 17:42:23 +08:00
|
|
|
} else if (EVP_DigestSignInit_ex(si->mctx, &si->pctx, EVP_MD_name(md),
|
2021-02-18 12:03:25 +08:00
|
|
|
ossl_cms_ctx_get0_libctx(ctx),
|
|
|
|
ossl_cms_ctx_get0_propq(ctx),
|
2021-03-02 18:21:00 +08:00
|
|
|
pk, NULL) <= 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2020-07-25 16:04:55 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!sd->signerInfos)
|
|
|
|
sd->signerInfos = sk_CMS_SignerInfo_new_null();
|
|
|
|
if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
|
|
|
|
goto merr;
|
|
|
|
|
|
|
|
return si;
|
|
|
|
|
|
|
|
merr:
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2015-04-30 23:30:03 +08:00
|
|
|
M_ASN1_free_of(si, CMS_SignerInfo);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2021-02-18 12:03:25 +08:00
|
|
|
void ossl_cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms)
|
2020-07-25 16:04:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
CMS_SignerInfo *si;
|
2021-05-04 03:58:02 +08:00
|
|
|
STACK_OF(CMS_SignerInfo) *sinfos;
|
2021-02-18 12:03:25 +08:00
|
|
|
const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2021-05-04 03:58:02 +08:00
|
|
|
ERR_set_mark();
|
|
|
|
sinfos = CMS_get0_SignerInfos(cms);
|
|
|
|
ERR_pop_to_mark(); /* removes error in case sinfos == NULL */
|
|
|
|
|
2020-07-25 16:04:55 +08:00
|
|
|
for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
|
|
|
|
si = sk_CMS_SignerInfo_value(sinfos, i);
|
|
|
|
if (si != NULL)
|
|
|
|
si->cms_ctx = ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-17 05:05:46 +08:00
|
|
|
static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
ASN1_TIME *tt;
|
|
|
|
int r = 0;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
|
|
|
if (t != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
tt = t;
|
|
|
|
else
|
|
|
|
tt = X509_gmtime_adj(NULL, 0);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2020-07-25 16:04:55 +08:00
|
|
|
if (tt == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto merr;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
|
|
|
|
tt->type, tt, -1) <= 0)
|
|
|
|
goto merr;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
r = 1;
|
|
|
|
merr:
|
2020-07-25 16:04:55 +08:00
|
|
|
if (t == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1_TIME_free(tt);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!r)
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return r;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2013-06-20 01:17:14 +08:00
|
|
|
EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return si->pctx;
|
|
|
|
}
|
2013-06-20 01:17:14 +08:00
|
|
|
|
|
|
|
EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-11-27 21:02:12 +08:00
|
|
|
return si->mctx;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2013-06-20 01:17:14 +08:00
|
|
|
|
2008-03-13 05:14:28 +08:00
|
|
|
STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2020-07-25 16:04:55 +08:00
|
|
|
CMS_SignedData *sd = cms_get0_signed(cms);
|
|
|
|
|
|
|
|
return sd != NULL ? sd->signerInfos : NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
STACK_OF(X509) *signers = NULL;
|
|
|
|
STACK_OF(CMS_SignerInfo) *sinfos;
|
|
|
|
CMS_SignerInfo *si;
|
|
|
|
int i;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
sinfos = CMS_get0_SignerInfos(cms);
|
|
|
|
for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
|
|
|
|
si = sk_CMS_SignerInfo_value(sinfos, i);
|
2020-07-25 16:04:55 +08:00
|
|
|
if (si->signer != NULL) {
|
2021-02-17 19:29:39 +08:00
|
|
|
if (!ossl_x509_add_cert_new(&signers, si->signer,
|
|
|
|
X509_ADD_FLAG_DEFAULT)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
sk_X509_free(signers);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return signers;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2020-07-25 16:04:55 +08:00
|
|
|
if (signer != NULL) {
|
2015-09-01 03:29:57 +08:00
|
|
|
X509_up_ref(signer);
|
2015-03-28 22:54:15 +08:00
|
|
|
EVP_PKEY_free(si->pkey);
|
2015-01-22 11:40:55 +08:00
|
|
|
si->pkey = X509_get_pubkey(signer);
|
|
|
|
}
|
2015-05-01 05:33:59 +08:00
|
|
|
X509_free(si->signer);
|
2015-01-22 11:40:55 +08:00
|
|
|
si->signer = signer;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1_OCTET_STRING **keyid,
|
|
|
|
X509_NAME **issuer, ASN1_INTEGER **sno)
|
|
|
|
{
|
2021-02-18 12:03:25 +08:00
|
|
|
return ossl_cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2021-02-18 12:03:25 +08:00
|
|
|
return ossl_cms_SignerIdentifier_cert_cmp(si->sid, cert);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
CMS_SignedData *sd;
|
|
|
|
CMS_SignerInfo *si;
|
|
|
|
CMS_CertificateChoices *cch;
|
|
|
|
STACK_OF(CMS_CertificateChoices) *certs;
|
|
|
|
X509 *x;
|
|
|
|
int i, j;
|
|
|
|
int ret = 0;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
sd = cms_get0_signed(cms);
|
2020-07-25 16:04:55 +08:00
|
|
|
if (sd == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
|
|
|
certs = sd->certificates;
|
|
|
|
for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
|
|
|
|
si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
|
2020-07-25 16:04:55 +08:00
|
|
|
if (si->signer != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
for (j = 0; j < sk_X509_num(scerts); j++) {
|
|
|
|
x = sk_X509_value(scerts, j);
|
|
|
|
if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
|
|
|
|
CMS_SignerInfo_set1_signer_cert(si, x);
|
|
|
|
ret++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:04:55 +08:00
|
|
|
if (si->signer != NULL || (flags & CMS_NOINTERN))
|
2015-01-22 11:40:55 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
|
|
|
|
cch = sk_CMS_CertificateChoices_value(certs, j);
|
|
|
|
if (cch->type != 0)
|
|
|
|
continue;
|
|
|
|
x = cch->d.certificate;
|
|
|
|
if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
|
|
|
|
CMS_SignerInfo_set1_signer_cert(si, x);
|
|
|
|
ret++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
|
|
|
|
X509 **signer, X509_ALGOR **pdig,
|
|
|
|
X509_ALGOR **psig)
|
|
|
|
{
|
2020-07-25 16:04:55 +08:00
|
|
|
if (pk != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
*pk = si->pkey;
|
2020-07-25 16:04:55 +08:00
|
|
|
if (signer != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
*signer = si->signer;
|
2020-07-25 16:04:55 +08:00
|
|
|
if (pdig != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
*pdig = si->digestAlgorithm;
|
2020-07-25 16:04:55 +08:00
|
|
|
if (psig != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
*psig = si->signatureAlgorithm;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2013-11-07 11:55:49 +08:00
|
|
|
ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return si->signature;
|
|
|
|
}
|
2013-11-07 11:55:49 +08:00
|
|
|
|
2008-04-07 00:29:47 +08:00
|
|
|
static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
|
2015-01-22 11:40:55 +08:00
|
|
|
CMS_SignerInfo *si, BIO *chain)
|
|
|
|
{
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
|
2015-01-22 11:40:55 +08:00
|
|
|
int r = 0;
|
|
|
|
EVP_PKEY_CTX *pctx = NULL;
|
2021-02-18 12:03:25 +08:00
|
|
|
const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
|
2015-11-27 21:02:12 +08:00
|
|
|
|
|
|
|
if (mctx == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
|
2015-11-27 21:02:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-07-25 16:04:55 +08:00
|
|
|
if (si->pkey == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
|
2016-04-27 20:22:29 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2021-02-18 12:03:25 +08:00
|
|
|
if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-02-06 04:23:54 +08:00
|
|
|
/* Set SignerInfo algorithm details if we used custom parameter */
|
2015-01-22 11:40:55 +08:00
|
|
|
if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If any signed attributes calculate and add messageDigest attribute
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (CMS_signed_get_attr_count(si) >= 0) {
|
|
|
|
unsigned char md[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int mdlen;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-11-27 21:02:12 +08:00
|
|
|
if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
|
|
|
|
V_ASN1_OCTET_STRING, md, mdlen))
|
|
|
|
goto err;
|
|
|
|
/* Copy content type across */
|
2019-06-03 13:19:48 +08:00
|
|
|
if (!cms_set_si_contentType_attr(cms, si))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2019-06-03 13:19:48 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!CMS_SignerInfo_sign(si))
|
|
|
|
goto err;
|
|
|
|
} else if (si->pctx) {
|
|
|
|
unsigned char *sig;
|
|
|
|
size_t siglen;
|
|
|
|
unsigned char md[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int mdlen;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
pctx = si->pctx;
|
2015-11-27 21:02:12 +08:00
|
|
|
if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
siglen = EVP_PKEY_size(si->pkey);
|
|
|
|
sig = OPENSSL_malloc(siglen);
|
2015-10-30 19:12:26 +08:00
|
|
|
if (sig == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
|
|
|
|
OPENSSL_free(sig);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ASN1_STRING_set0(si->signature, sig, siglen);
|
|
|
|
} else {
|
|
|
|
unsigned char *sig;
|
|
|
|
unsigned int siglen;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
|
2015-10-30 19:12:26 +08:00
|
|
|
if (sig == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2020-12-11 17:19:37 +08:00
|
|
|
if (!EVP_SignFinal_ex(mctx, sig, &siglen, si->pkey,
|
2021-02-18 12:03:25 +08:00
|
|
|
ossl_cms_ctx_get0_libctx(ctx),
|
|
|
|
ossl_cms_ctx_get0_propq(ctx))) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_SIGNFINAL_ERROR);
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(sig);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ASN1_STRING_set0(si->signature, sig, siglen);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = 1;
|
|
|
|
|
|
|
|
err:
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(mctx);
|
2015-03-28 22:54:15 +08:00
|
|
|
EVP_PKEY_CTX_free(pctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
return r;
|
|
|
|
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2021-02-18 12:03:25 +08:00
|
|
|
int ossl_cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
STACK_OF(CMS_SignerInfo) *sinfos;
|
|
|
|
CMS_SignerInfo *si;
|
|
|
|
int i;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
sinfos = CMS_get0_SignerInfos(cms);
|
|
|
|
for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
|
|
|
|
si = sk_CMS_SignerInfo_value(sinfos, i);
|
|
|
|
if (!cms_SignerInfo_content_sign(cms, si, chain))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
cms->d.signedData->encapContentInfo->partial = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
int CMS_SignerInfo_sign(CMS_SignerInfo *si)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-11-27 21:02:12 +08:00
|
|
|
EVP_MD_CTX *mctx = si->mctx;
|
2017-10-11 02:02:56 +08:00
|
|
|
EVP_PKEY_CTX *pctx = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char *abuf = NULL;
|
|
|
|
int alen;
|
|
|
|
size_t siglen;
|
2020-07-25 16:04:55 +08:00
|
|
|
const CMS_CTX *ctx = si->cms_ctx;
|
2021-03-10 19:58:53 +08:00
|
|
|
char md_name[OSSL_MAX_NAME_SIZE];
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2021-03-10 19:58:53 +08:00
|
|
|
if (!OBJ_obj2txt(md_name, sizeof(md_name),
|
|
|
|
si->digestAlgorithm->algorithm, 0))
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
|
|
|
|
if (!cms_add1_signingTime(si, NULL))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-03-09 12:49:27 +08:00
|
|
|
if (!ossl_cms_si_check_attributes(si))
|
2019-06-03 13:19:48 +08:00
|
|
|
goto err;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (si->pctx)
|
|
|
|
pctx = si->pctx;
|
|
|
|
else {
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_reset(mctx);
|
2021-02-18 12:03:25 +08:00
|
|
|
if (EVP_DigestSignInit_ex(mctx, &pctx, md_name,
|
|
|
|
ossl_cms_ctx_get0_libctx(ctx),
|
2021-03-02 18:21:00 +08:00
|
|
|
ossl_cms_ctx_get0_propq(ctx), si->pkey,
|
|
|
|
NULL) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-12-06 22:19:41 +08:00
|
|
|
si->pctx = pctx;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
|
|
|
|
ASN1_ITEM_rptr(CMS_Attributes_Sign));
|
|
|
|
if (!abuf)
|
|
|
|
goto err;
|
|
|
|
if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
|
|
|
|
goto err;
|
|
|
|
if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
|
|
|
|
goto err;
|
|
|
|
OPENSSL_free(abuf);
|
|
|
|
abuf = OPENSSL_malloc(siglen);
|
2015-10-30 19:12:26 +08:00
|
|
|
if (abuf == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
|
|
|
|
goto err;
|
|
|
|
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_reset(mctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
ASN1_STRING_set0(si->signature, abuf, siglen);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
2015-05-01 22:02:07 +08:00
|
|
|
OPENSSL_free(abuf);
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_reset(mctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
int CMS_SignerInfo_verify(CMS_SignerInfo *si)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-11-27 21:02:12 +08:00
|
|
|
EVP_MD_CTX *mctx = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char *abuf = NULL;
|
|
|
|
int alen, r = -1;
|
2021-03-10 19:58:53 +08:00
|
|
|
char name[OSSL_MAX_NAME_SIZE];
|
2020-08-20 11:28:11 +08:00
|
|
|
const EVP_MD *md;
|
|
|
|
EVP_MD *fetched_md = NULL;
|
2020-07-25 16:04:55 +08:00
|
|
|
const CMS_CTX *ctx = si->cms_ctx;
|
2021-02-18 12:03:25 +08:00
|
|
|
OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
|
|
|
|
const char *propq = ossl_cms_ctx_get0_propq(ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-07-25 16:04:55 +08:00
|
|
|
if (si->pkey == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_NO_PUBLIC_KEY);
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-03-09 12:49:27 +08:00
|
|
|
if (!ossl_cms_si_check_attributes(si))
|
2019-06-03 13:19:48 +08:00
|
|
|
return -1;
|
|
|
|
|
2021-03-10 19:58:53 +08:00
|
|
|
OBJ_obj2txt(name, sizeof(name), si->digestAlgorithm->algorithm, 0);
|
2020-08-20 11:28:11 +08:00
|
|
|
|
|
|
|
(void)ERR_set_mark();
|
2020-12-11 17:19:37 +08:00
|
|
|
fetched_md = EVP_MD_fetch(libctx, name, propq);
|
2020-08-20 11:28:11 +08:00
|
|
|
|
|
|
|
if (fetched_md != NULL)
|
|
|
|
md = fetched_md;
|
|
|
|
else
|
|
|
|
md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
|
|
|
|
if (md == NULL) {
|
|
|
|
(void)ERR_clear_last_mark();
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
2020-08-20 11:28:11 +08:00
|
|
|
}
|
|
|
|
(void)ERR_pop_to_mark();
|
|
|
|
|
2017-11-28 03:11:36 +08:00
|
|
|
if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
|
2020-07-25 16:04:55 +08:00
|
|
|
goto err;
|
2017-11-28 03:11:36 +08:00
|
|
|
}
|
2015-11-27 21:02:12 +08:00
|
|
|
mctx = si->mctx;
|
2020-12-11 17:19:37 +08:00
|
|
|
if (EVP_DigestVerifyInit_ex(mctx, &si->pctx, EVP_MD_name(md), libctx,
|
2021-03-02 18:21:00 +08:00
|
|
|
propq, si->pkey, NULL) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!cms_sd_asn1_ctrl(si, 1))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
|
|
|
|
ASN1_ITEM_rptr(CMS_Attributes_Verify));
|
2020-09-21 09:42:41 +08:00
|
|
|
if (abuf == NULL || alen < 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
|
|
|
|
OPENSSL_free(abuf);
|
|
|
|
if (r <= 0) {
|
|
|
|
r = -1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
r = EVP_DigestVerifyFinal(mctx,
|
|
|
|
si->signature->data, si->signature->length);
|
|
|
|
if (r <= 0)
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2020-08-20 11:28:11 +08:00
|
|
|
EVP_MD_free(fetched_md);
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_reset(mctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
return r;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
/* Create a chain of digest BIOs from a CMS ContentInfo */
|
|
|
|
|
2021-02-18 12:03:25 +08:00
|
|
|
BIO *ossl_cms_SignedData_init_bio(CMS_ContentInfo *cms)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
CMS_SignedData *sd;
|
|
|
|
BIO *chain = NULL;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
sd = cms_get0_signed(cms);
|
2020-07-25 16:04:55 +08:00
|
|
|
if (sd == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
if (cms->d.signedData->encapContentInfo->partial)
|
|
|
|
cms_sd_set_version(sd);
|
|
|
|
for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
|
|
|
|
X509_ALGOR *digestAlgorithm;
|
|
|
|
BIO *mdbio;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
|
2021-02-18 12:03:25 +08:00
|
|
|
mdbio = ossl_cms_DigestAlgorithm_init_bio(digestAlgorithm,
|
|
|
|
ossl_cms_get0_cmsctx(cms));
|
2020-07-25 16:04:55 +08:00
|
|
|
if (mdbio == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2020-07-25 16:04:55 +08:00
|
|
|
if (chain != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
BIO_push(chain, mdbio);
|
|
|
|
else
|
|
|
|
chain = mdbio;
|
|
|
|
}
|
|
|
|
return chain;
|
|
|
|
err:
|
2015-03-25 23:31:18 +08:00
|
|
|
BIO_free_all(chain);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
ASN1_OCTET_STRING *os = NULL;
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
|
2015-01-22 11:40:55 +08:00
|
|
|
EVP_PKEY_CTX *pkctx = NULL;
|
|
|
|
int r = -1;
|
|
|
|
unsigned char mval[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int mlen;
|
2015-11-27 21:02:12 +08:00
|
|
|
|
|
|
|
if (mctx == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
|
2015-11-27 21:02:12 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
/* If we have any signed attributes look for messageDigest value */
|
|
|
|
if (CMS_signed_get_attr_count(si) >= 0) {
|
|
|
|
os = CMS_signed_get0_data_by_OBJ(si,
|
|
|
|
OBJ_nid2obj(NID_pkcs9_messageDigest),
|
|
|
|
-3, V_ASN1_OCTET_STRING);
|
2020-07-25 16:04:55 +08:00
|
|
|
if (os == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 12:03:25 +08:00
|
|
|
if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
2015-11-27 21:02:12 +08:00
|
|
|
if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If messageDigest found compare it */
|
|
|
|
|
2020-07-25 16:04:55 +08:00
|
|
|
if (os != NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
if (mlen != (unsigned int)os->length) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(mval, os->data, mlen)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
r = 0;
|
|
|
|
} else
|
|
|
|
r = 1;
|
|
|
|
} else {
|
Add "origin" field to EVP_CIPHER, EVP_MD
Add a "where did this EVP_{CIPHER,MD} come from" flag: global, via fetch,
or via EVP_{CIPHER,MD}_meth_new. Update EVP_{CIPHER,MD}_free to handle all
three origins. The flag is deliberately right before some function pointers,
so that compile-time failures (int/pointer) will occur, as opposed to
taking a bit in the existing "flags" field. The "global variable" flag
is non-zero, so the default case of using OPENSSL_zalloc (for provider
ciphers), will do the right thing. Ref-counting is a no-op for
Make up_ref no-op for global MD and CIPHER objects
Deprecate EVP_MD_CTX_md(). Added EVP_MD_CTX_get0_md() (same semantics as
the deprecated function) and EVP_MD_CTX_get1_md(). Likewise, deprecate
EVP_CIPHER_CTX_cipher() in favor of EVP_CIPHER_CTX_get0_cipher(), and add
EVP_CIPHER_CTX_get1_CIPHER().
Refactor EVP_MD_free() and EVP_MD_meth_free() to call new common
evp_md_free_int() function.
Refactor EVP_CIPHER_free() and EVP_CIPHER_meth_free() to call new common
evp_cipher_free_int() function.
Also change some flags tests to explicit test == or != zero. E.g.,
if (flags & x) --> if ((flags & x) != 0)
if (!(flags & x)) --> if ((flags & x) == 0)
Only done for those lines where "get0_cipher" calls were made.
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14193)
2021-02-17 06:51:56 +08:00
|
|
|
const EVP_MD *md = EVP_MD_CTX_get0_md(mctx);
|
2020-07-25 16:04:55 +08:00
|
|
|
const CMS_CTX *ctx = si->cms_ctx;
|
|
|
|
|
2021-02-18 12:03:25 +08:00
|
|
|
pkctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
|
|
|
|
si->pkey,
|
|
|
|
ossl_cms_ctx_get0_propq(ctx));
|
2015-10-30 19:12:26 +08:00
|
|
|
if (pkctx == NULL)
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (EVP_PKEY_verify_init(pkctx) <= 0)
|
|
|
|
goto err;
|
|
|
|
if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
|
|
|
|
goto err;
|
|
|
|
si->pctx = pkctx;
|
|
|
|
if (!cms_sd_asn1_ctrl(si, 1))
|
|
|
|
goto err;
|
|
|
|
r = EVP_PKEY_verify(pkctx, si->signature->data,
|
|
|
|
si->signature->length, mval, mlen);
|
|
|
|
if (r <= 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
r = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
2015-03-28 22:54:15 +08:00
|
|
|
EVP_PKEY_CTX_free(pkctx);
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(mctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
return r;
|
|
|
|
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
unsigned char *smder = NULL;
|
|
|
|
int smderlen, r;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
smderlen = i2d_X509_ALGORS(algs, &smder);
|
|
|
|
if (smderlen <= 0)
|
|
|
|
return 0;
|
|
|
|
r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
|
|
|
|
V_ASN1_SEQUENCE, smder, smderlen);
|
|
|
|
OPENSSL_free(smder);
|
|
|
|
return r;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
|
2015-01-22 11:40:55 +08:00
|
|
|
int algnid, int keysize)
|
|
|
|
{
|
|
|
|
X509_ALGOR *alg;
|
|
|
|
ASN1_INTEGER *key = NULL;
|
2020-07-25 16:04:55 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (keysize > 0) {
|
|
|
|
key = ASN1_INTEGER_new();
|
2020-04-27 07:26:48 +08:00
|
|
|
if (key == NULL || !ASN1_INTEGER_set(key, keysize)) {
|
|
|
|
ASN1_INTEGER_free(key);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
2020-04-27 07:26:48 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
alg = X509_ALGOR_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (alg == NULL) {
|
2015-04-30 23:30:03 +08:00
|
|
|
ASN1_INTEGER_free(key);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
|
|
|
|
key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
|
2015-10-30 19:12:26 +08:00
|
|
|
if (*algs == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
*algs = sk_X509_ALGOR_new_null();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_ALGOR_free(alg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
/* Check to see if a cipher exists and if so add S/MIME capabilities */
|
|
|
|
|
|
|
|
static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (EVP_get_cipherbynid(nid))
|
|
|
|
return CMS_add_simple_smimecap(sk, nid, arg);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (EVP_get_digestbynid(nid))
|
|
|
|
return CMS_add_simple_smimecap(sk, nid, arg);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
|
2015-09-29 23:43:27 +08:00
|
|
|
|| !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
|
|
|
|
|| !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
|
2015-01-22 11:40:55 +08:00
|
|
|
|| !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
|
|
|
|
|| !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
|
|
|
|
|| !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
|
|
|
|
|| !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
|
|
|
|
|| !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
|
|
|
|
|| !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
|
|
|
|
|| !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
|
|
|
|
|| !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
|
|
|
|
|| !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|