Skip to content

Commit 2b3228d

Browse files
olszomalmtrojnar
authored andcommitted
Changed error output to stderr instead of stdout
1 parent bad6e96 commit 2b3228d

File tree

11 files changed

+486
-476
lines changed

11 files changed

+486
-476
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- used native HTTP client with OpenSSL 3.0 or later, removed libcurl dependency
77
- added the "-ignore-crl" option to disable fetching and verifying
88
CRL Distribution Points
9+
- changed error output to stderr instead of stdout
910
- improved testing
1011

1112
### 2.8 (2024.03.03)

appx.c

Lines changed: 97 additions & 97 deletions
Large diffs are not rendered by default.

cab.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static u_char *cab_digest_calc(FILE_FORMAT_CTX *ctx, const EVP_MD *md)
206206
BIO *bhash = BIO_new(BIO_f_md());
207207

208208
if (!BIO_set_md(bhash, md)) {
209-
printf("Unable to set the message digest of BIO\n");
209+
fprintf(stderr, "Unable to set the message digest of BIO\n");
210210
BIO_free_all(bhash);
211211
return 0; /* FAILED */
212212
}
@@ -296,7 +296,7 @@ static u_char *cab_digest_calc(FILE_FORMAT_CTX *ctx, const EVP_MD *md)
296296
nfolders--;
297297
}
298298
if (idx != coffFiles) {
299-
printf("Corrupt coffFiles value: 0x%08X\n", coffFiles);
299+
fprintf(stderr, "Corrupt coffFiles value: 0x%08X\n", coffFiles);
300300
BIO_free_all(bhash);
301301
return 0; /* FAILED */
302302
}
@@ -307,7 +307,7 @@ static u_char *cab_digest_calc(FILE_FORMAT_CTX *ctx, const EVP_MD *md)
307307
}
308308
/* (variable) ab - the compressed data bytes */
309309
if (!bio_hash_data(bhash, ctx->options->indata, idx, fileend)) {
310-
printf("Unable to calculate digest\n");
310+
fprintf(stderr, "Unable to calculate digest\n");
311311
BIO_free_all(bhash);
312312
return 0; /* FAILED */
313313
}
@@ -343,17 +343,17 @@ static int cab_verify_digests(FILE_FORMAT_CTX *ctx, PKCS7 *p7)
343343
}
344344
}
345345
if (mdtype == -1) {
346-
printf("Failed to extract current message digest\n\n");
346+
fprintf(stderr, "Failed to extract current message digest\n\n");
347347
return 0; /* FAILED */
348348
}
349349
md = EVP_get_digestbynid(mdtype);
350350
cmdbuf = cab_digest_calc(ctx, md);
351351
if (!cmdbuf) {
352-
printf("Failed to calculate message digest\n\n");
352+
fprintf(stderr, "Failed to calculate message digest\n\n");
353353
return 0; /* FAILED */
354354
}
355355
if (!compare_digests(mdbuf, cmdbuf, mdtype)) {
356-
printf("Signature verification: failed\n\n");
356+
fprintf(stderr, "Signature verification: failed\n\n");
357357
OPENSSL_free(cmdbuf);
358358
return 0; /* FAILED */
359359
}
@@ -443,7 +443,7 @@ static int cab_remove_pkcs7(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
443443
BIO_write(outdata, ctx->options->indata + 32, 4);
444444
idx = cab_write_optional_names(outdata, ctx->options->indata, 60, flags);
445445
if (idx >= ctx->cab_ctx->fileend) {
446-
printf("Corrupt CAB file - too short\n");
446+
fprintf(stderr, "Corrupt CAB file - too short\n");
447447
OPENSSL_free(buf);
448448
return 0; /* FAILED */
449449
}
@@ -453,7 +453,7 @@ static int cab_remove_pkcs7(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
453453
*/
454454
nfolders = GET_UINT16_LE(ctx->options->indata + 26);
455455
if (nfolders * 8 >= ctx->cab_ctx->fileend - idx) {
456-
printf("Corrupt cFolders value: 0x%08X\n", nfolders);
456+
fprintf(stderr, "Corrupt cFolders value: 0x%08X\n", nfolders);
457457
OPENSSL_free(buf);
458458
return 0; /* FAILED */
459459
}
@@ -510,26 +510,26 @@ static PKCS7 *cab_pkcs7_signature_new(FILE_FORMAT_CTX *ctx, BIO *hash)
510510
PKCS7 *p7 = pkcs7_create(ctx);
511511

512512
if (!p7) {
513-
printf("Creating a new signature failed\n");
513+
fprintf(stderr, "Creating a new signature failed\n");
514514
return NULL; /* FAILED */
515515
}
516516
if (ctx->options->jp >= 0 && !cab_add_jp_attribute(p7, ctx->options->jp)) {
517-
printf("Adding jp attribute failed\n");
517+
fprintf(stderr, "Adding jp attribute failed\n");
518518
PKCS7_free(p7);
519519
return NULL; /* FAILED */
520520
}
521521
if (!add_indirect_data_object(p7)) {
522-
printf("Adding SPC_INDIRECT_DATA_OBJID failed\n");
522+
fprintf(stderr, "Adding SPC_INDIRECT_DATA_OBJID failed\n");
523523
PKCS7_free(p7);
524524
return NULL; /* FAILED */
525525
}
526526
content = spc_indirect_data_content_get(hash, ctx);
527527
if (!content) {
528-
printf("Failed to get spcIndirectDataContent\n");
528+
fprintf(stderr, "Failed to get spcIndirectDataContent\n");
529529
return NULL; /* FAILED */
530530
}
531531
if (!sign_spc_indirect_data_content(p7, content)) {
532-
printf("Failed to set signed content\n");
532+
fprintf(stderr, "Failed to set signed content\n");
533533
PKCS7_free(p7);
534534
ASN1_OCTET_STRING_free(content);
535535
return NULL; /* FAILED */
@@ -556,7 +556,7 @@ static int cab_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
556556

557557
if (((len = i2d_PKCS7(p7, NULL)) <= 0)
558558
|| (p = OPENSSL_malloc((size_t)len)) == NULL) {
559-
printf("i2d_PKCS memory allocation failed: %d\n", len);
559+
fprintf(stderr, "i2d_PKCS memory allocation failed: %d\n", len);
560560
return 1; /* FAILED */
561561
}
562562
i2d_PKCS7(p7, &p);
@@ -653,19 +653,19 @@ static CAB_CTX *cab_ctx_get(char *indata, uint32_t filesize)
653653
uint16_t flags;
654654

655655
if (filesize < 44) {
656-
printf("CAB file is too short\n");
656+
fprintf(stderr, "CAB file is too short\n");
657657
return NULL; /* FAILED */
658658
}
659659
reserved = GET_UINT32_LE(indata + 4);
660660
if (reserved) {
661-
printf("Reserved1: 0x%08X\n", reserved);
661+
fprintf(stderr, "Reserved1: 0x%08X\n", reserved);
662662
return NULL; /* FAILED */
663663
}
664664
/* flags specify bit-mapped values that indicate the presence of optional data */
665665
flags = GET_UINT16_LE(indata + 30);
666666
if (flags & FLAG_PREV_CABINET) {
667667
/* FLAG_NEXT_CABINET works */
668-
printf("Multivolume cabinet file is unsupported: flags 0x%04X\n", flags);
668+
fprintf(stderr, "Multivolume cabinet file is unsupported: flags 0x%04X\n", flags);
669669
return NULL; /* FAILED */
670670
}
671671
if (flags & FLAG_RESERVE_PRESENT) {
@@ -675,12 +675,12 @@ static CAB_CTX *cab_ctx_get(char *indata, uint32_t filesize)
675675
*/
676676
header_size = GET_UINT32_LE(indata + 36);
677677
if (header_size != 20) {
678-
printf("Additional header size: 0x%08X\n", header_size);
678+
fprintf(stderr, "Additional header size: 0x%08X\n", header_size);
679679
return NULL; /* FAILED */
680680
}
681681
reserved = GET_UINT32_LE(indata + 40);
682682
if (reserved != 0x00100000) {
683-
printf("abReserved: 0x%08X\n", reserved);
683+
fprintf(stderr, "abReserved: 0x%08X\n", reserved);
684684
return NULL; /* FAILED */
685685
}
686686
/*
@@ -695,13 +695,13 @@ static CAB_CTX *cab_ctx_get(char *indata, uint32_t filesize)
695695
sigpos = GET_UINT32_LE(indata + 44);
696696
siglen = GET_UINT32_LE(indata + 48);
697697
if ((sigpos < filesize && sigpos + siglen != filesize) || (sigpos >= filesize)) {
698-
printf("Additional data offset:\t%u bytes\nAdditional data size:\t%u bytes\n",
698+
fprintf(stderr, "Additional data offset:\t%u bytes\nAdditional data size:\t%u bytes\n",
699699
sigpos, siglen);
700-
printf("File size:\t\t%u bytes\n", filesize);
700+
fprintf(stderr, "File size:\t\t%u bytes\n", filesize);
701701
return NULL; /* FAILED */
702702
}
703703
if ((sigpos > 0 && siglen == 0) || (sigpos == 0 && siglen > 0)) {
704-
printf("Corrupt signature\n");
704+
fprintf(stderr, "Corrupt signature\n");
705705
return NULL; /* FAILED */
706706
}
707707
}
@@ -852,7 +852,7 @@ static int cab_modify_header(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
852852

853853
idx = cab_write_optional_names(outdata, ctx->options->indata, 60, flags);
854854
if (idx >= ctx->cab_ctx->fileend) {
855-
printf("Corrupt CAB file - too short\n");
855+
fprintf(stderr, "Corrupt CAB file - too short\n");
856856
return 0; /* FAILED */
857857
}
858858
/*
@@ -861,7 +861,7 @@ static int cab_modify_header(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
861861
*/
862862
nfolders = GET_UINT16_LE(ctx->options->indata + 26);
863863
if (nfolders * 8 >= ctx->cab_ctx->fileend - idx) {
864-
printf("Corrupt cFolders value: 0x%08X\n", nfolders);
864+
fprintf(stderr, "Corrupt cFolders value: 0x%08X\n", nfolders);
865865
return 0; /* FAILED */
866866
}
867867
while (nfolders) {
@@ -936,7 +936,7 @@ static int cab_add_header(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
936936

937937
idx = cab_write_optional_names(outdata, ctx->options->indata, 36, flags);
938938
if (idx >= ctx->cab_ctx->fileend) {
939-
printf("Corrupt CAB file - too short\n");
939+
fprintf(stderr, "Corrupt CAB file - too short\n");
940940
OPENSSL_free(buf);
941941
return 0; /* FAILED */
942942
}
@@ -946,7 +946,7 @@ static int cab_add_header(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
946946
*/
947947
nfolders = GET_UINT16_LE(ctx->options->indata + 26);
948948
if (nfolders * 8 >= ctx->cab_ctx->fileend - idx) {
949-
printf("Corrupt cFolders value: 0x%08X\n", nfolders);
949+
fprintf(stderr, "Corrupt cFolders value: 0x%08X\n", nfolders);
950950
OPENSSL_free(buf);
951951
return 0; /* FAILED */
952952
}
@@ -979,16 +979,16 @@ static int cab_add_header(FILE_FORMAT_CTX *ctx, BIO *hash, BIO *outdata)
979979
static int cab_check_file(FILE_FORMAT_CTX *ctx)
980980
{
981981
if (!ctx) {
982-
printf("Init error\n\n");
982+
fprintf(stderr, "Init error\n");
983983
return 0; /* FAILED */
984984
}
985985
if (ctx->cab_ctx->header_size != 20) {
986-
printf("No signature found\n\n");
986+
fprintf(stderr, "No signature found\n");
987987
return 0; /* FAILED */
988988
}
989989
if (ctx->cab_ctx->sigpos == 0 || ctx->cab_ctx->siglen == 0
990990
|| ctx->cab_ctx->sigpos > ctx->cab_ctx->fileend) {
991-
printf("No signature found\n\n");
991+
fprintf(stderr, "No signature found\n");
992992
return 0; /* FAILED */
993993
}
994994
return 1; /* OK */

cat.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static FILE_FORMAT_CTX *cat_ctx_new(GLOBAL_OPTIONS *options, BIO *hash, BIO *out
8282
uint32_t filesize;
8383

8484
if (options->cmd == CMD_REMOVE || options->cmd==CMD_ATTACH || options->cmd == CMD_EXTRACT_DATA) {
85-
printf("Unsupported command\n");
85+
fprintf(stderr, "Unsupported command\n");
8686
return NULL; /* FAILED */
8787
}
8888
filesize = get_file_size(options->infile);
@@ -158,21 +158,21 @@ static PKCS7 *cat_pkcs7_signature_new(FILE_FORMAT_CTX *ctx, BIO *hash)
158158

159159
p7 = pkcs7_create(ctx);
160160
if (!p7) {
161-
printf("Creating a new signature failed\n");
161+
fprintf(stderr, "Creating a new signature failed\n");
162162
return NULL; /* FAILED */
163163
}
164164
if (!cat_add_ms_ctl_object(p7)) {
165-
printf("Adding MS_CTL_OBJID failed\n");
165+
fprintf(stderr, "Adding MS_CTL_OBJID failed\n");
166166
PKCS7_free(p7);
167167
return NULL; /* FAILED */
168168
}
169169
if (!ctx->cat_ctx->p7 || !ctx->cat_ctx->p7->d.sign || !ctx->cat_ctx->p7->d.sign->contents) {
170-
printf("Failed to get content\n");
170+
fprintf(stderr, "Failed to get content\n");
171171
PKCS7_free(p7);
172172
return NULL; /* FAILED */
173173
}
174174
if (!cat_sign_ms_ctl_content(p7, ctx->cat_ctx->p7->d.sign->contents)) {
175-
printf("Failed to set signed content\n");
175+
fprintf(stderr, "Failed to set signed content\n");
176176
PKCS7_free(p7);
177177
return NULL; /* FAILED */
178178
}
@@ -287,7 +287,7 @@ static int cat_sign_ms_ctl_content(PKCS7 *p7, PKCS7 *contents)
287287

288288
if (!contents->d.other || !contents->d.other->value.sequence
289289
|| !contents->d.other->value.sequence->data) {
290-
printf("Failed to get content value\n");
290+
fprintf(stderr, "Failed to get content value\n");
291291
return 0; /* FAILED */
292292
}
293293
seqhdrlen = asn1_simple_hdr_len(contents->d.other->value.sequence->data,
@@ -296,11 +296,11 @@ static int cat_sign_ms_ctl_content(PKCS7 *p7, PKCS7 *contents)
296296
content_length = contents->d.other->value.sequence->length - seqhdrlen;
297297

298298
if (!pkcs7_sign_content(p7, content, content_length)) {
299-
printf("Failed to sign content\n");
299+
fprintf(stderr, "Failed to sign content\n");
300300
return 0; /* FAILED */
301301
}
302302
if (!PKCS7_set_content(p7, PKCS7_dup(contents))) {
303-
printf("PKCS7_set_content failed\n");
303+
fprintf(stderr, "PKCS7_set_content failed\n");
304304
return 0; /* FAILED */
305305
}
306306
return 1; /* OK */
@@ -318,7 +318,7 @@ static int cat_list_content(PKCS7 *p7)
318318

319319
ctlc = ms_ctl_content_get(p7);
320320
if (!ctlc) {
321-
printf("Failed to extract MS_CTL_OBJID data\n");
321+
fprintf(stderr, "Failed to extract MS_CTL_OBJID data\n");
322322
return 1; /* FAILED */
323323
}
324324
printf("\nCatalog members:\n");
@@ -353,7 +353,7 @@ static int cat_list_content(PKCS7 *p7)
353353
printf("\n");
354354
}
355355
MsCtlContent_free(ctlc);
356-
ERR_print_errors_fp(stdout);
356+
ERR_print_errors_fp(stderr);
357357
return 0; /* OK */
358358
}
359359

@@ -382,7 +382,7 @@ static int cat_print_content_member_digest(ASN1_TYPE *content)
382382
}
383383
SpcIndirectDataContent_free(idc);
384384
if (mdtype == -1) {
385-
printf("Failed to extract current message digest\n\n");
385+
fprintf(stderr, "Failed to extract current message digest\n\n");
386386
return 0; /* FAILED */
387387
}
388388
printf("\tHash algorithm: %s\n", OBJ_nid2sn(mdtype));
@@ -461,17 +461,17 @@ static int cat_check_file(FILE_FORMAT_CTX *ctx)
461461
PKCS7_SIGNER_INFO *si;
462462

463463
if (!ctx) {
464-
printf("Init error\n\n");
464+
fprintf(stderr, "Init error\n");
465465
return 0; /* FAILED */
466466
}
467467
signer_info = PKCS7_get_signer_info(ctx->cat_ctx->p7);
468468
if (!signer_info) {
469-
printf("Failed catalog file\n\n");
469+
fprintf(stderr, "Failed catalog file\n");
470470
return 0; /* FAILED */
471471
}
472472
si = sk_PKCS7_SIGNER_INFO_value(signer_info, 0);
473473
if (!si) {
474-
printf("No signature found\n\n");
474+
fprintf(stderr, "No signature found\n");
475475
return 0; /* FAILED */
476476
}
477477
if (ctx->options->verbose) {

0 commit comments

Comments
 (0)