Skip to content

Commit abbbfab

Browse files
olszomalmtrojnar
authored andcommitted
Move some functions from common to main code
1 parent 2a4b758 commit abbbfab

File tree

3 files changed

+78
-80
lines changed

3 files changed

+78
-80
lines changed

helpers.c

+1-79
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
/* Prototypes */
1212
static int pkcs7_set_content_blob(PKCS7 *sig, PKCS7 *cursig);
1313
static SpcSpOpusInfo *spc_sp_opus_info_create(FILE_FORMAT_CTX *ctx);
14-
static int X509_attribute_chain_append_signature(STACK_OF(X509_ATTRIBUTE) **unauth_attr, u_char *p, int len);
1514
static int spc_indirect_data_content_get(u_char **blob, int *len, FILE_FORMAT_CTX *ctx);
1615
static int pkcs7_set_spc_indirect_data_content(PKCS7 *p7, BIO *hash, u_char *buf, int len);
1716
static int pkcs7_signer_info_add_spc_sp_opus_info(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx);
1817
static int pkcs7_signer_info_add_purpose(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx);
19-
static int pkcs7_signer_info_add_signing_time(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx);
2018

2119
/*
2220
* Common functions
@@ -174,7 +172,7 @@ static int pkcs7_signer_info_add_purpose(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX
174172
* [in] ctx: structure holds input and output data
175173
* [returns] 0 on error or 1 on success
176174
*/
177-
static int pkcs7_signer_info_add_signing_time(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx)
175+
int pkcs7_signer_info_add_signing_time(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx)
178176
{
179177
if (ctx->options->time == INVALID_TIME) /* -time option was not specified */
180178
return 1; /* SUCCESS */
@@ -380,44 +378,6 @@ static int pkcs7_set_content_blob(PKCS7 *sig, PKCS7 *cursig)
380378
return 1; /* OK */
381379
}
382380

383-
/*
384-
* Add the new signature to the current signature as a nested signature:
385-
* new unauthorized SPC_NESTED_SIGNATURE_OBJID attribute
386-
* [out] cursig: current PKCS#7 signature
387-
* [in] p7: new PKCS#7 signature
388-
* [in] ctx: structure holds input and output data
389-
* [returns] 0 on error or 1 on success
390-
*/
391-
int cursig_set_nested(PKCS7 *cursig, PKCS7 *p7, FILE_FORMAT_CTX *ctx)
392-
{
393-
u_char *p = NULL;
394-
int len = 0;
395-
PKCS7_SIGNER_INFO *si;
396-
STACK_OF(PKCS7_SIGNER_INFO) *signer_info;
397-
398-
if (!cursig)
399-
return 0; /* FAILED */
400-
signer_info = PKCS7_get_signer_info(cursig);
401-
if (!signer_info)
402-
return 0; /* FAILED */
403-
si = sk_PKCS7_SIGNER_INFO_value(signer_info, 0);
404-
if (!si)
405-
return 0; /* FAILED */
406-
if (((len = i2d_PKCS7(p7, NULL)) <= 0) ||
407-
(p = OPENSSL_malloc((size_t)len)) == NULL)
408-
return 0; /* FAILED */
409-
i2d_PKCS7(p7, &p);
410-
p -= len;
411-
412-
pkcs7_signer_info_add_signing_time(si, ctx);
413-
if (!X509_attribute_chain_append_signature(&(si->unauth_attr), p, len)) {
414-
OPENSSL_free(p);
415-
return 0; /* FAILED */
416-
}
417-
OPENSSL_free(p);
418-
return 1; /* OK */
419-
}
420-
421381
/* Return the header length (tag and length octets) of the ASN.1 type
422382
* [in] p: ASN.1 data
423383
* [in] len: ASN.1 data length
@@ -603,44 +563,6 @@ static SpcSpOpusInfo *spc_sp_opus_info_create(FILE_FORMAT_CTX *ctx)
603563
return info;
604564
}
605565

606-
/*
607-
* [in, out] unauth_attr: unauthorized attributes list
608-
* [in] p: PKCS#7 data
609-
* [in] len: PKCS#7 data length
610-
* [returns] 0 on error or 1 on success
611-
*/
612-
static int X509_attribute_chain_append_signature(STACK_OF(X509_ATTRIBUTE) **unauth_attr, u_char *p, int len)
613-
{
614-
X509_ATTRIBUTE *attr = NULL;
615-
int nid = OBJ_txt2nid(SPC_NESTED_SIGNATURE_OBJID);
616-
617-
if (*unauth_attr == NULL) {
618-
if ((*unauth_attr = sk_X509_ATTRIBUTE_new_null()) == NULL)
619-
return 0; /* FAILED */
620-
} else {
621-
/* try to find SPC_NESTED_SIGNATURE_OBJID attribute */
622-
int i;
623-
for (i = 0; i < sk_X509_ATTRIBUTE_num(*unauth_attr); i++) {
624-
attr = sk_X509_ATTRIBUTE_value(*unauth_attr, i);
625-
if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
626-
/* append p to the V_ASN1_SEQUENCE */
627-
if (!X509_ATTRIBUTE_set1_data(attr, V_ASN1_SEQUENCE, p, len))
628-
return 0; /* FAILED */
629-
return 1; /* OK */
630-
}
631-
}
632-
}
633-
/* create new unauthorized SPC_NESTED_SIGNATURE_OBJID attribute */
634-
attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, V_ASN1_SEQUENCE, p, len);
635-
if (!attr)
636-
return 0; /* FAILED */
637-
if (!sk_X509_ATTRIBUTE_push(*unauth_attr, attr)) {
638-
X509_ATTRIBUTE_free(attr);
639-
return 0; /* FAILED */
640-
}
641-
return 1; /* OK */
642-
}
643-
644566
/*
645567
* [out] blob: SpcIndirectDataContent data
646568
* [out] len: SpcIndirectDataContent data length

helpers.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
uint32_t get_file_size(const char *infile);
1010
char *map_file(const char *infile, const size_t size);
1111
void unmap_file(char *indata, const size_t size);
12+
int pkcs7_signer_info_add_signing_time(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx);
1213
PKCS7 *pkcs7_get_sigfile(FILE_FORMAT_CTX *ctx);
1314
PKCS7 *pkcs7_create(FILE_FORMAT_CTX *ctx);
1415
void add_content_type(PKCS7 *p7);
1516
int add_indirect_data_object(PKCS7 *p7, BIO *hash, FILE_FORMAT_CTX *ctx);
1617
int add_ms_ctl_object(PKCS7 *p7, PKCS7 *cursig);
17-
int cursig_set_nested(PKCS7 *cursig, PKCS7 *p7, FILE_FORMAT_CTX *ctx);
1818
int asn1_simple_hdr_len(const u_char *p, int len);
1919
int bio_hash_data(BIO *hash, char *indata, size_t idx, size_t fileend);
2020
void print_hash(const char *descript1, const char *descript2, const u_char *hashbuf, int length);

osslsigncode.c

+76
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,82 @@ static int add_timestamp_and_blob(PKCS7 *p7, FILE_FORMAT_CTX *ctx)
809809
return 0; /* OK */
810810
}
811811

812+
/*
813+
* [in, out] unauth_attr: unauthorized attributes list
814+
* [in] p: PKCS#7 data
815+
* [in] len: PKCS#7 data length
816+
* [returns] 0 on error or 1 on success
817+
*/
818+
static int X509_attribute_chain_append_signature(STACK_OF(X509_ATTRIBUTE) **unauth_attr, u_char *p, int len)
819+
{
820+
X509_ATTRIBUTE *attr = NULL;
821+
int nid = OBJ_txt2nid(SPC_NESTED_SIGNATURE_OBJID);
822+
823+
if (*unauth_attr == NULL) {
824+
if ((*unauth_attr = sk_X509_ATTRIBUTE_new_null()) == NULL)
825+
return 0; /* FAILED */
826+
} else {
827+
/* try to find SPC_NESTED_SIGNATURE_OBJID attribute */
828+
int i;
829+
for (i = 0; i < sk_X509_ATTRIBUTE_num(*unauth_attr); i++) {
830+
attr = sk_X509_ATTRIBUTE_value(*unauth_attr, i);
831+
if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
832+
/* append p to the V_ASN1_SEQUENCE */
833+
if (!X509_ATTRIBUTE_set1_data(attr, V_ASN1_SEQUENCE, p, len))
834+
return 0; /* FAILED */
835+
return 1; /* OK */
836+
}
837+
}
838+
}
839+
/* create new unauthorized SPC_NESTED_SIGNATURE_OBJID attribute */
840+
attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, V_ASN1_SEQUENCE, p, len);
841+
if (!attr)
842+
return 0; /* FAILED */
843+
if (!sk_X509_ATTRIBUTE_push(*unauth_attr, attr)) {
844+
X509_ATTRIBUTE_free(attr);
845+
return 0; /* FAILED */
846+
}
847+
return 1; /* OK */
848+
}
849+
850+
/*
851+
* Add the new signature to the current signature as a nested signature:
852+
* new unauthorized SPC_NESTED_SIGNATURE_OBJID attribute
853+
* [out] cursig: current PKCS#7 signature
854+
* [in] p7: new PKCS#7 signature
855+
* [in] ctx: structure holds input and output data
856+
* [returns] 0 on error or 1 on success
857+
*/
858+
static int cursig_set_nested(PKCS7 *cursig, PKCS7 *p7, FILE_FORMAT_CTX *ctx)
859+
{
860+
u_char *p = NULL;
861+
int len = 0;
862+
PKCS7_SIGNER_INFO *si;
863+
STACK_OF(PKCS7_SIGNER_INFO) *signer_info;
864+
865+
if (!cursig)
866+
return 0; /* FAILED */
867+
signer_info = PKCS7_get_signer_info(cursig);
868+
if (!signer_info)
869+
return 0; /* FAILED */
870+
si = sk_PKCS7_SIGNER_INFO_value(signer_info, 0);
871+
if (!si)
872+
return 0; /* FAILED */
873+
if (((len = i2d_PKCS7(p7, NULL)) <= 0) ||
874+
(p = OPENSSL_malloc((size_t)len)) == NULL)
875+
return 0; /* FAILED */
876+
i2d_PKCS7(p7, &p);
877+
p -= len;
878+
879+
pkcs7_signer_info_add_signing_time(si, ctx);
880+
if (!X509_attribute_chain_append_signature(&(si->unauth_attr), p, len)) {
881+
OPENSSL_free(p);
882+
return 0; /* FAILED */
883+
}
884+
OPENSSL_free(p);
885+
return 1; /* OK */
886+
}
887+
812888
/*
813889
* [in, out] store: structure for holding information about X.509 certificates and CRLs
814890
* [in] time: time_t to set

0 commit comments

Comments
 (0)