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 18 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
11 changes: 10 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,11 @@ 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) {
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
2 changes: 2 additions & 0 deletions crypto/cms/cms_err.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ static const ERR_STRING_DATA CMS_str_functs[] = {
{ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_UNCOMPRESS, 0), "CMS_uncompress"},
{ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_VERIFY, 0), "CMS_verify"},
{ERR_PACK(ERR_LIB_CMS, CMS_F_KEK_UNWRAP_KEY, 0), "kek_unwrap_key"},
{ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_SIGNING_CERT_V2, 0),
"CMS_add1_signing_cert_v2"},
{0, NULL}
};

Expand Down
60 changes: 60 additions & 0 deletions crypto/cms/cms_ess.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include <openssl/cms.h>
#include <openssl/ess.h>
#include "cms_lcl.h"

IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
Expand Down Expand Up @@ -335,3 +336,62 @@ ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
CMS_ReceiptRequest_free(rr);
return os;
}

/*
* Add signer certificate's 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);
pp = NULL;
if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
V_ASN1_SEQUENCE, seq, -1))
goto err;
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;
opensignature marked this conversation as resolved.
Show resolved Hide resolved
}
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
3 changes: 3 additions & 0 deletions include/openssl/cms.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ DECLARE_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
# define CMS_DEBUG_DECRYPT 0x20000
# define CMS_KEY_PARAM 0x40000
# define CMS_ASCIICRLF 0x80000
# define CMS_CADES 0x100000

const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms);

Expand Down Expand Up @@ -283,6 +284,8 @@ int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si,
const void *bytes, int len);
void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
int lastpos, int type);
CMS_SignerInfo *CMS_add1_signing_cert_v2(CMS_SignerInfo *si, X509 *signer,
const EVP_MD *sign_md);

# ifdef HEADER_X509V3_H

Expand Down
1 change: 1 addition & 0 deletions include/openssl/cmserr.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ int ERR_load_CMS_strings(void);
# define CMS_F_CMS_STREAM 155
# define CMS_F_CMS_UNCOMPRESS 156
# define CMS_F_CMS_VERIFY 157
# define CMS_F_CMS_ADD1_SIGNING_CERT_V2 158
# define CMS_F_KEK_UNWRAP_KEY 180

/*
Expand Down
110 changes: 110 additions & 0 deletions include/openssl/ess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2018-2018 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
opensignature marked this conversation as resolved.
Show resolved Hide resolved
* 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
*/

# include <openssl/ossl_typ.h>
opensignature marked this conversation as resolved.
Show resolved Hide resolved

ESS_ISSUER_SERIAL *ESS_ISSUER_SERIAL_new(void);
void ESS_ISSUER_SERIAL_free(ESS_ISSUER_SERIAL *a);
int i2d_ESS_ISSUER_SERIAL(const ESS_ISSUER_SERIAL *a, unsigned char **pp);
ESS_ISSUER_SERIAL *d2i_ESS_ISSUER_SERIAL(ESS_ISSUER_SERIAL **a,
const unsigned char **pp,
long length);
ESS_ISSUER_SERIAL *ESS_ISSUER_SERIAL_dup(ESS_ISSUER_SERIAL *a);

ESS_CERT_ID *ESS_CERT_ID_new(void);
void ESS_CERT_ID_free(ESS_CERT_ID *a);
int i2d_ESS_CERT_ID(const ESS_CERT_ID *a, unsigned char **pp);
ESS_CERT_ID *d2i_ESS_CERT_ID(ESS_CERT_ID **a, const unsigned char **pp,
long length);
ESS_CERT_ID *ESS_CERT_ID_dup(ESS_CERT_ID *a);

ESS_SIGNING_CERT *ESS_SIGNING_CERT_new(void);
void ESS_SIGNING_CERT_free(ESS_SIGNING_CERT *a);
int i2d_ESS_SIGNING_CERT(const ESS_SIGNING_CERT *a, unsigned char **pp);
ESS_SIGNING_CERT *d2i_ESS_SIGNING_CERT(ESS_SIGNING_CERT **a,
const unsigned char **pp, long length);
ESS_SIGNING_CERT *ESS_SIGNING_CERT_dup(ESS_SIGNING_CERT *a);

ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new(void);
void ESS_CERT_ID_V2_free(ESS_CERT_ID_V2 *a);
int i2d_ESS_CERT_ID_V2(const ESS_CERT_ID_V2 *a, unsigned char **pp);
ESS_CERT_ID_V2 *d2i_ESS_CERT_ID_V2(ESS_CERT_ID_V2 **a,
const unsigned char **pp, long length);
ESS_CERT_ID_V2 *ESS_CERT_ID_V2_dup(ESS_CERT_ID_V2 *a);

ESS_SIGNING_CERT_V2 *ESS_SIGNING_CERT_V2_new(void);
void ESS_SIGNING_CERT_V2_free(ESS_SIGNING_CERT_V2 *a);
int i2d_ESS_SIGNING_CERT_V2(const ESS_SIGNING_CERT_V2 *a, unsigned char **pp);
ESS_SIGNING_CERT_V2 *d2i_ESS_SIGNING_CERT_V2(ESS_SIGNING_CERT_V2 **a,
const unsigned char **pp,
long length);
ESS_SIGNING_CERT_V2 *ESS_SIGNING_CERT_V2_dup(ESS_SIGNING_CERT_V2 *a);

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

struct ESS_issuer_serial {
opensignature marked this conversation as resolved.
Show resolved Hide resolved
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;
};
12 changes: 12 additions & 0 deletions include/openssl/ossl_typ.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# define HEADER_OPENSSL_TYPES_H

#include <limits.h>
#include <openssl/safestack.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -177,6 +178,17 @@ typedef struct ct_policy_eval_ctx_st CT_POLICY_EVAL_CTX;
typedef struct ossl_store_info_st OSSL_STORE_INFO;
typedef struct ossl_store_search_st OSSL_STORE_SEARCH;

typedef struct ESS_issuer_serial ESS_ISSUER_SERIAL;
typedef struct ESS_cert_id ESS_CERT_ID;
typedef struct ESS_signing_cert ESS_SIGNING_CERT;

DEFINE_STACK_OF(ESS_CERT_ID)
opensignature marked this conversation as resolved.
Show resolved Hide resolved

typedef struct ESS_signing_cert_v2_st ESS_SIGNING_CERT_V2;
typedef struct ESS_cert_id_v2_st ESS_CERT_ID_V2;

DEFINE_STACK_OF(ESS_CERT_ID_V2)

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \
defined(INTMAX_MAX) && defined(UINTMAX_MAX)
typedef intmax_t ossl_intmax_t;
Expand Down
Loading