From d57f0249cdd83386a2091e18e2c948ee195b43f7 Mon Sep 17 00:00:00 2001 From: "Dr. David von Oheimb" Date: Wed, 4 Oct 2023 21:19:48 +0200 Subject: [PATCH] apps/{cms.c,smime.c}: improve code layout according to coding guidelines --- apps/cms.c | 36 ++++++++++++++++++++++-------------- apps/smime.c | 27 ++++++++++++--------------- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/apps/cms.c b/apps/cms.c index df13876b231f8..19daaf31a459f 100644 --- a/apps/cms.c +++ b/apps/cms.c @@ -795,8 +795,8 @@ int cms_main(int argc, char **argv) goto opthelp; } if (key_param == NULL || key_param->idx != keyidx) { - cms_key_param *nparam; - nparam = app_malloc(sizeof(*nparam), "key param buffer"); + cms_key_param *nparam = app_malloc(sizeof(*nparam), "key param buffer"); + if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL) { OPENSSL_free(nparam); goto end; @@ -1061,8 +1061,8 @@ int cms_main(int argc, char **argv) } } if (certsoutfile != NULL) { - STACK_OF(X509) *allcerts; - allcerts = CMS_get1_certs(cms); + STACK_OF(X509) *allcerts = CMS_get1_certs(cms); + if (!save_certs(certsoutfile, allcerts)) { BIO_printf(bio_err, "Error writing certs to %s\n", certsoutfile); @@ -1111,6 +1111,7 @@ int cms_main(int argc, char **argv) cms = CMS_compress(in, -1, flags); } else if (operation == SMIME_ENCRYPT) { int i; + flags |= CMS_PARTIAL; cms = CMS_encrypt_ex(NULL, in, cipher, flags, libctx, app_get0_propq()); if (cms == NULL) @@ -1121,7 +1122,7 @@ int cms_main(int argc, char **argv) cms_key_param *kparam; cms_recip_opt *ropt; int tflags = flags | CMS_KEY_PARAM; - /* This flag enforces allocating the EVP_PKEY_CTX for the recipient here */ + /* This flag enforces allocating EVP_PKEY_CTX for the recipient */ EVP_PKEY_CTX *pctx; X509 *x = sk_X509_value(encerts, i); int res; @@ -1220,14 +1221,13 @@ int cms_main(int argc, char **argv) } } } else if (operation == SMIME_ENCRYPTED_ENCRYPT) { - cms = CMS_EncryptedData_encrypt_ex(in, cipher, secret_key, - secret_keylen, flags, libctx, app_get0_propq()); - + cms = CMS_EncryptedData_encrypt_ex(in, cipher, secret_key, secret_keylen, + flags, libctx, app_get0_propq()); } else if (operation == SMIME_SIGN_RECEIPT) { CMS_ContentInfo *srcms = NULL; - STACK_OF(CMS_SignerInfo) *sis; CMS_SignerInfo *si; - sis = CMS_get0_SignerInfos(cms); + STACK_OF(CMS_SignerInfo) *sis = CMS_get0_SignerInfos(cms); + if (sis == NULL) goto end; si = sk_CMS_SignerInfo_value(sis, 0); @@ -1238,6 +1238,7 @@ int cms_main(int argc, char **argv) cms = srcms; } else if (operation & SMIME_SIGNERS) { int i; + /* * If detached data content and not signing pre-computed digest, we * enable streaming if S/MIME output format. @@ -1270,6 +1271,7 @@ int cms_main(int argc, char **argv) CMS_SignerInfo *si; cms_key_param *kparam; int tflags = flags; + signerfile = sk_OPENSSL_STRING_value(sksigners, i); keyfile = sk_OPENSSL_STRING_value(skkeys, i); @@ -1417,6 +1419,7 @@ int cms_main(int argc, char **argv) if (noout) { if (print) { ASN1_PCTX *pctx = NULL; + if (get_nameopt() != XN_FLAG_ONELINE) { pctx = ASN1_PCTX_new(); if (pctx != NULL) { /* Print anyway if malloc failed */ @@ -1472,6 +1475,7 @@ int cms_main(int argc, char **argv) sk_OPENSSL_STRING_free(rr_from); for (key_param = key_first; key_param;) { cms_key_param *tparam; + sk_OPENSSL_STRING_free(key_param->param); tparam = key_param->next; OPENSSL_free(key_param); @@ -1511,6 +1515,7 @@ static int save_certs(char *signerfile, STACK_OF(X509) *signers) { int i; BIO *tmp; + if (signerfile == NULL) return 1; tmp = BIO_new_file(signerfile, "w"); @@ -1561,14 +1566,14 @@ static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns) static void receipt_request_print(CMS_ContentInfo *cms) { - STACK_OF(CMS_SignerInfo) *sis; CMS_SignerInfo *si; CMS_ReceiptRequest *rr; int allorfirst; STACK_OF(GENERAL_NAMES) *rto, *rlist; ASN1_STRING *scid; int i, rv; - sis = CMS_get0_SignerInfos(cms); + STACK_OF(CMS_SignerInfo) *sis = CMS_get0_SignerInfos(cms); + for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) { si = sk_CMS_SignerInfo_value(sis, i); rv = CMS_get1_ReceiptRequest(si, &rr); @@ -1581,6 +1586,7 @@ static void receipt_request_print(CMS_ContentInfo *cms) } else { const char *id; int idlen; + CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst, &rlist, &rto); BIO_puts(bio_err, " Signed Content ID:\n"); @@ -1608,14 +1614,15 @@ static void receipt_request_print(CMS_ContentInfo *cms) static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns) { int i; - STACK_OF(GENERAL_NAMES) *ret; GENERAL_NAMES *gens = NULL; GENERAL_NAME *gen = NULL; - ret = sk_GENERAL_NAMES_new_null(); + STACK_OF(GENERAL_NAMES) *ret = sk_GENERAL_NAMES_new_null(); + if (ret == NULL) goto err; for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) { char *str = sk_OPENSSL_STRING_value(ns, i); + gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0); if (gen == NULL) goto err; @@ -1671,6 +1678,7 @@ static int cms_set_pkey_param(EVP_PKEY_CTX *pctx, { char *keyopt; int i; + if (sk_OPENSSL_STRING_num(param) <= 0) return 1; for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) { diff --git a/apps/smime.c b/apps/smime.c index 458056c04dc48..99107552cabcb 100644 --- a/apps/smime.c +++ b/apps/smime.c @@ -438,8 +438,7 @@ int smime_main(int argc, char **argv) goto opthelp; } if (!operation) { - BIO_puts(bio_err, - "No operation (-encrypt|-sign|...) specified\n"); + BIO_puts(bio_err, "No operation (-encrypt|-sign|...) specified\n"); goto opthelp; } @@ -519,11 +518,10 @@ int smime_main(int argc, char **argv) } } - if (certfile != NULL) { - if (!load_certs(certfile, 0, &other, NULL, "certificates")) { - ERR_print_errors(bio_err); - goto end; - } + if (certfile != NULL + && !load_certs(certfile, 0, &other, NULL, "certificates")) { + ERR_print_errors(bio_err); + goto end; } if (recipfile != NULL && (operation == SMIME_DECRYPT)) { @@ -609,6 +607,7 @@ int smime_main(int argc, char **argv) p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, app_get0_propq()); } else if (operation & SMIME_SIGNERS) { int i; + /* * If detached data content we only enable streaming if S/MIME output * format. @@ -624,12 +623,9 @@ int smime_main(int argc, char **argv) p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq()); if (p7 == NULL) goto end; - if (flags & PKCS7_NOCERTS) { - for (i = 0; i < sk_X509_num(other); i++) { - X509 *x = sk_X509_value(other, i); - PKCS7_add_certificate(p7, x); - } - } + if ((flags & PKCS7_NOCERTS) != 0) + for (i = 0; i < sk_X509_num(other); i++) + PKCS7_add_certificate(p7, sk_X509_value(other, i)); } else { flags |= PKCS7_REUSE_DIGEST; } @@ -670,9 +666,10 @@ int smime_main(int argc, char **argv) } } else if (operation == SMIME_VERIFY) { STACK_OF(X509) *signers; - if (PKCS7_verify(p7, other, store, indata, out, flags)) + + if (PKCS7_verify(p7, other, store, indata, out, flags)) { BIO_printf(bio_err, "Verification successful\n"); - else { + } else { BIO_printf(bio_err, "Verification failure\n"); goto end; }