Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This patch adds cades support for openssl #7611

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
6509be5
Add files via upload
opensignature Nov 10, 2018
3610180
Delete cms.c
opensignature Nov 10, 2018
732536a
Add files via upload
opensignature Nov 10, 2018
acd1d2d
Add files via upload
opensignature Nov 17, 2018
142b848
Add files via upload
opensignature Nov 17, 2018
78d88e7
Add files via upload
opensignature Nov 17, 2018
fc2318e
Add files via upload
opensignature Nov 18, 2018
4fe6a48
Add files via upload
opensignature Nov 18, 2018
856c968
Add files via upload
opensignature Nov 18, 2018
97b65e2
Add files via upload
opensignature Nov 18, 2018
41ea6cf
Add files via upload
opensignature Nov 18, 2018
edb085b
Add files via upload
opensignature Nov 19, 2018
9b26f3a
Fixed cms.h and cmserr.h
opensignature Nov 19, 2018
58eb937
Moved ESS code to cms_ess.c
opensignature Nov 19, 2018
aaf4726
Move ESS (Enhanced Security Services - RFC 5035) definitions from
opensignature Nov 27, 2018
eeee142
Add <openssl/x509.h> to include/openssl/ess.h
opensignature Nov 27, 2018
b430193
Deleted ESS definitions moved to new include/openssl/ess.h
opensignature Nov 28, 2018
257715a
Moved typedefs from ess.h into ossl_typ.h
opensignature Nov 29, 2018
0f39264
Moved structures from include/openssl/ess.h to new crypto/include/int…
opensignature Nov 30, 2018
4d3154b
According to RFC5126 section 5.7.3 added ESS signing-certificate attr…
opensignature Nov 30, 2018
d94c783
Draft of documentation for CAdES
opensignature Nov 30, 2018
e806f87
Merge remote-tracking branch 'upstream/master'
opensignature Dec 1, 2018
2a2a54e
Bug fixed
opensignature Dec 1, 2018
d827142
Test cms with -cades flag
opensignature Dec 1, 2018
e70bf2f
Test -cades flag with SHA256 md
opensignature Dec 2, 2018
04a3338
Release block of memory
opensignature Dec 2, 2018
3545cdf
Release block of memory
opensignature Dec 2, 2018
6359a22
Merge branch 'master' of https://github.com/opensignature/openssl
opensignature Dec 2, 2018
0ae477d
Release block of memory
opensignature Dec 2, 2018
724043e
Draft documentation for CMS_add1_signing_cert ad CMS_add1_signing_cer…
opensignature Dec 3, 2018
da3637f
Fixed libcrypto.num diff
opensignature Dec 3, 2018
48c9519
Add CMS_add1_signing_cert and CMS_add1_signing_cert_v2 to
opensignature Dec 3, 2018
75c7cf5
Documentation bug fixed
opensignature Dec 3, 2018
62e4a9b
Documentation bug fixed and better arranged ess.h and ossl_typ.h
opensignature Dec 4, 2018
6a48bd9
of ess.h needs TS and CMS_add1_signing_cert and CMS1_add1_signing_cer…
opensignature Dec 4, 2018
0b2ac37
ess.h needs to TS and CMS_add1_signing_cert and CMS_add1_signing_cert_v2
opensignature Dec 4, 2018
e8f939c
Change FUNCTION:TS to FUNCTION:CMS in libcrypto.num
opensignature Dec 4, 2018
ad4c5f0
Bug on libcrypto.num fixed
opensignature Dec 4, 2018
8cdd628
Add memory cleanup
opensignature Dec 4, 2018
a06fdda
Memory bug fixed
opensignature Dec 4, 2018
ae46c5a
Add issuer stuff
opensignature Dec 7, 2018
49be137
Conflict with upstream fixed
opensignature Dec 9, 2018
5418ae0
Re-licensing and ifndef changed
opensignature Dec 9, 2018
66537c2
Merge remote-tracking branch 'upstream/master'
opensignature Dec 13, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion apps/cms.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct cms_key_param_st {
typedef enum OPTION_choice {
OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENCRYPT,
OPT_DECRYPT, OPT_SIGN, OPT_SIGN_RECEIPT, OPT_RESIGN,
OPT_DECRYPT, OPT_SIGN, OPT_CADES, OPT_SIGN_RECEIPT, OPT_RESIGN,
OPT_VERIFY, OPT_VERIFY_RETCODE, OPT_VERIFY_RECEIPT,
OPT_CMSOUT, OPT_DATA_OUT, OPT_DATA_CREATE, OPT_DIGEST_VERIFY,
OPT_DIGEST_CREATE, OPT_COMPRESS, OPT_UNCOMPRESS,
Expand Down Expand Up @@ -102,6 +102,7 @@ const OPTIONS cms_options[] = {
{"sign", OPT_SIGN, '-', "Sign message"},
{"sign_receipt", OPT_SIGN_RECEIPT, '-', "Generate a signed receipt for the message"},
{"resign", OPT_RESIGN, '-', "Resign a signed message"},
{"cades", OPT_CADES, '-', "Include signer certificate digest"},
{"verify", OPT_VERIFY, '-', "Verify signed message"},
{"verify_retcode", OPT_VERIFY_RETCODE, '-'},
{"verify_receipt", OPT_VERIFY_RECEIPT, '<'},
Expand Down Expand Up @@ -326,6 +327,9 @@ int cms_main(int argc, char **argv)
case OPT_BINARY:
flags |= CMS_BINARY;
break;
case OPT_CADES:
flags |= CMS_CADES;
break;
case OPT_KEYID:
flags |= CMS_USE_KEYID;
break;
Expand Down Expand Up @@ -940,6 +944,14 @@ int cms_main(int argc, char **argv)
si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
if (si == NULL)
goto end;
if (flags & CMS_CADES) {
if (sign_md == EVP_sha1() || sign_md == NULL)
si = CMS_add1_signing_cert(si, signer);
else
si = CMS_add1_signing_cert_v2(si, signer, sign_md);
if (si == NULL)
goto end;
}
if (kparam != NULL) {
EVP_PKEY_CTX *pctx;
pctx = CMS_SignerInfo_get0_pkey_ctx(si);
Expand Down
4 changes: 4 additions & 0 deletions crypto/cms/cms_err.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ static const ERR_STRING_DATA CMS_str_functs[] = {
{ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_SIGNER, 0), "CMS_add1_signer"},
{ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_SIGNINGTIME, 0),
"cms_add1_signingTime"},
{ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_SIGNING_CERT, 0),
"CMS_add1_signing_cert"},
{ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_SIGNING_CERT_V2, 0),
"CMS_add1_signing_cert_v2"},
{ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COMPRESS, 0), "CMS_compress"},
{ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COMPRESSEDDATA_CREATE, 0),
"cms_CompressedData_create"},
Expand Down
114 changes: 114 additions & 0 deletions crypto/cms/cms_ess.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include <openssl/cms.h>
#include <openssl/ess.h>
#include "cms_lcl.h"
#include "internal/ess_int.h"

IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)

Expand Down Expand Up @@ -335,3 +337,115 @@ ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
CMS_ReceiptRequest_free(rr);
return os;
}

/*
* Add signer certificate's V2 digest to a SignerInfo
* structure
*/

opensignature marked this conversation as resolved.
Show resolved Hide resolved
CMS_SignerInfo *CMS_add1_signing_cert_v2(CMS_SignerInfo *si, X509 *signer,
const EVP_MD *sign_md)
{
ASN1_STRING *seq = NULL;
unsigned char *p, *pp = NULL;
ESS_SIGNING_CERT_V2 *sc = NULL;
ESS_CERT_ID_V2 * cid;
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hash_len = sizeof (hash);
X509_ALGOR *alg = NULL;
int len;

memset(hash, 0, sizeof (hash));

/* Create the SigningCertificateV2 attribute
* and adding the signing certificate id.
*/

if ((sc = ESS_SIGNING_CERT_V2_new()) == NULL
|| (cid = ESS_CERT_ID_V2_new()) == NULL
|| (alg = X509_ALGOR_new()) == NULL)
goto err;

X509_ALGOR_set_md(alg, sign_md);
if (alg->algorithm == NULL)
goto err;
cid->hash_alg = alg;
alg = NULL;
if (!X509_digest(signer, sign_md, hash, &hash_len))
opensignature marked this conversation as resolved.
Show resolved Hide resolved
goto err;
if (!ASN1_OCTET_STRING_set(cid->hash, hash, hash_len))
goto err;
if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
goto err;
/* Add SigningCertificateV2 signed attribute to the signer info. */
len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
if ((pp = OPENSSL_malloc(len)) == NULL)
goto err;
p = pp;
i2d_ESS_SIGNING_CERT_V2(sc, &p);
if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
goto err;
OPENSSL_free(pp);
ESS_SIGNING_CERT_V2_free(sc);
X509_ALGOR_free(alg);
pp = NULL;
if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
V_ASN1_SEQUENCE, seq, -1))
goto err;
ASN1_STRING_free(seq);
return si;

err:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memory cleanup is missing ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I reproduce this error?
../test/recipes/80-test_cms.t line 462.
file=crypto/cms/cms_ess.c, line=382
53 bytes leaked in 1 chunks

CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT_V2, ERR_R_MALLOC_FAILURE);
return NULL;
}

/*
* Add signer certificate's digest to a SignerInfo
* structure
*/

CMS_SignerInfo *CMS_add1_signing_cert(CMS_SignerInfo *si, X509 *signer)
{
ASN1_STRING *seq = NULL;
unsigned char *p, *pp = NULL;
ESS_SIGNING_CERT *sc = NULL;
ESS_CERT_ID * cid;
unsigned char hash[SHA_DIGEST_LENGTH];
int len;

/* Create the SigningCertificate attribute
* and adding the signing certificate id.
*/

if ((sc = ESS_SIGNING_CERT_new()) == NULL
|| (cid = ESS_CERT_ID_new()) == NULL)
goto err;

if (!X509_digest(signer, EVP_sha1(), hash, NULL))
goto err;
if (!ASN1_OCTET_STRING_set(cid->hash, hash, SHA_DIGEST_LENGTH))
goto err;
if (!sk_ESS_CERT_ID_push(sc->cert_ids, cid))
goto err;
/* Add SigningCertificate signed attribute to the signer info. */
len = i2d_ESS_SIGNING_CERT(sc, NULL);
if ((pp = OPENSSL_malloc(len)) == NULL)
goto err;
p = pp;
i2d_ESS_SIGNING_CERT(sc, &p);
if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
goto err;
OPENSSL_free(pp);
ESS_SIGNING_CERT_free(sc);
pp = NULL;
if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
V_ASN1_SEQUENCE, seq, -1))
goto err;
ASN1_STRING_free(seq);
return si;

err:
CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memory cleanup is missing ;)

return NULL;
opensignature marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions crypto/err/openssl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ CMS_F_CMS_ADD1_RECEIPTREQUEST:158:CMS_add1_ReceiptRequest
CMS_F_CMS_ADD1_RECIPIENT_CERT:101:CMS_add1_recipient_cert
CMS_F_CMS_ADD1_SIGNER:102:CMS_add1_signer
CMS_F_CMS_ADD1_SIGNINGTIME:103:cms_add1_signingTime
CMS_F_CMS_ADD1_SIGNING_CERT:181:CMS_add1_signing_cert
CMS_F_CMS_ADD1_SIGNING_CERT_V2:182:CMS_add1_signing_cert_v2
CMS_F_CMS_COMPRESS:104:CMS_compress
CMS_F_CMS_COMPRESSEDDATA_CREATE:105:cms_CompressedData_create
CMS_F_CMS_COMPRESSEDDATA_INIT_BIO:106:cms_CompressedData_init_bio
Expand Down
73 changes: 73 additions & 0 deletions crypto/include/internal/ess_int.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* 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
*/

/* ESS related stuff */

/*-
* IssuerSerial ::= SEQUENCE {
* issuer GeneralNames,
* serialNumber CertificateSerialNumber
* }
*/

struct ESS_issuer_serial {
STACK_OF(GENERAL_NAME) *issuer;
ASN1_INTEGER *serial;
};

/*-
* ESSCertID ::= SEQUENCE {
* certHash Hash,
* issuerSerial IssuerSerial OPTIONAL
* }
*/

struct ESS_cert_id {
ASN1_OCTET_STRING *hash; /* Always SHA-1 digest. */
ESS_ISSUER_SERIAL *issuer_serial;
};

/*-
* SigningCertificate ::= SEQUENCE {
* certs SEQUENCE OF ESSCertID,
* policies SEQUENCE OF PolicyInformation OPTIONAL
* }
*/

struct ESS_signing_cert {
STACK_OF(ESS_CERT_ID) *cert_ids;
STACK_OF(POLICYINFO) *policy_info;
};

/*-
* ESSCertIDv2 ::= SEQUENCE {
* hashAlgorithm AlgorithmIdentifier
* DEFAULT {algorithm id-sha256},
* certHash Hash,
* issuerSerial IssuerSerial OPTIONAL
* }
*/

struct ESS_cert_id_v2_st {
X509_ALGOR *hash_alg; /* Default: SHA-256 */
ASN1_OCTET_STRING *hash;
ESS_ISSUER_SERIAL *issuer_serial;
};

/*-
* SigningCertificateV2 ::= SEQUENCE {
* certs SEQUENCE OF ESSCertIDv2,
* policies SEQUENCE OF PolicyInformation OPTIONAL
* }
*/

struct ESS_signing_cert_v2_st {
STACK_OF(ESS_CERT_ID_V2) *cert_ids;
STACK_OF(POLICYINFO) *policy_info;
};
1 change: 1 addition & 0 deletions crypto/ts/ts_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <openssl/err.h>
#include <openssl/asn1t.h>
#include "ts_lcl.h"
#include "internal/ess_int.h"

ASN1_SEQUENCE(TS_MSG_IMPRINT) = {
ASN1_SIMPLE(TS_MSG_IMPRINT, hash_algo, X509_ALGOR),
Expand Down
61 changes: 0 additions & 61 deletions crypto/ts/ts_lcl.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,67 +98,6 @@ struct TS_status_info_st {
ASN1_BIT_STRING *failure_info;
};

/*-
* IssuerSerial ::= SEQUENCE {
* issuer GeneralNames,
* serialNumber CertificateSerialNumber
* }
*/
struct ESS_issuer_serial {
STACK_OF(GENERAL_NAME) *issuer;
ASN1_INTEGER *serial;
};

/*-
* ESSCertID ::= SEQUENCE {
* certHash Hash,
* issuerSerial IssuerSerial OPTIONAL
* }
*/
struct ESS_cert_id {
ASN1_OCTET_STRING *hash; /* Always SHA-1 digest. */
ESS_ISSUER_SERIAL *issuer_serial;
};

/*-
* SigningCertificate ::= SEQUENCE {
* certs SEQUENCE OF ESSCertID,
* policies SEQUENCE OF PolicyInformation OPTIONAL
* }
*/
struct ESS_signing_cert {
STACK_OF(ESS_CERT_ID) *cert_ids;
STACK_OF(POLICYINFO) *policy_info;
};

/*-
* ESSCertIDv2 ::= SEQUENCE {
* hashAlgorithm AlgorithmIdentifier
* DEFAULT {algorithm id-sha256},
* certHash Hash,
* issuerSerial IssuerSerial OPTIONAL
* }
*/

struct ESS_cert_id_v2_st {
X509_ALGOR *hash_alg; /* Default: SHA-256 */
ASN1_OCTET_STRING *hash;
ESS_ISSUER_SERIAL *issuer_serial;
};

/*-
* SigningCertificateV2 ::= SEQUENCE {
* certs SEQUENCE OF ESSCertIDv2,
* policies SEQUENCE OF PolicyInformation OPTIONAL
* }
*/

struct ESS_signing_cert_v2_st {
STACK_OF(ESS_CERT_ID_V2) *cert_ids;
STACK_OF(POLICYINFO) *policy_info;
};


struct TS_resp_ctx {
X509 *signer_cert;
EVP_PKEY *signer_key;
Expand Down
1 change: 1 addition & 0 deletions crypto/ts/ts_rsp_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <openssl/pkcs7.h>
#include <openssl/crypto.h>
#include "ts_lcl.h"
#include "internal/ess_int.h"

static ASN1_INTEGER *def_serial_cb(struct TS_resp_ctx *, void *);
static int def_time_cb(struct TS_resp_ctx *, void *, long *sec, long *usec);
Expand Down
1 change: 1 addition & 0 deletions crypto/ts/ts_rsp_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <openssl/ts.h>
#include <openssl/pkcs7.h>
#include "ts_lcl.h"
#include "internal/ess_int.h"

static int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,
X509 *signer, STACK_OF(X509) **chain);
Expand Down
Loading