Skip to content

Commit

Permalink
apps/{cms.c,smime.c}: improve code layout according to coding guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
DDvO committed Oct 4, 2023
1 parent 456e6ca commit fff9574
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
47 changes: 29 additions & 18 deletions apps/cms.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,9 @@ 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;
Expand Down Expand Up @@ -927,8 +928,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);
Expand Down Expand Up @@ -976,6 +977,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)
Expand All @@ -984,7 +986,7 @@ int cms_main(int argc, char **argv)
CMS_RecipientInfo *ri;
cms_key_param *kparam;
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;
Expand Down Expand Up @@ -1012,8 +1014,8 @@ int cms_main(int argc, char **argv)

if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
&& wrap_cipher != NULL) {
EVP_CIPHER_CTX *wctx;
wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
EVP_CIPHER_CTX *wctx = CMS_RecipientInfo_kari_get0_ctx(ri);

if (EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL) != 1)
goto end;
}
Expand Down Expand Up @@ -1044,14 +1046,14 @@ int cms_main(int argc, char **argv)
goto end;
}
} 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);
Expand All @@ -1062,6 +1064,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.
Expand All @@ -1073,7 +1076,8 @@ int cms_main(int argc, char **argv)
flags |= CMS_STREAM;
}
flags |= CMS_PARTIAL;
cms = CMS_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
cms = CMS_sign_ex(NULL, NULL, other, in, flags,
libctx, app_get0_propq());
if (cms == NULL)
goto end;
if (econtent_type != NULL)
Expand All @@ -1092,6 +1096,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);

Expand All @@ -1116,8 +1121,8 @@ int cms_main(int argc, char **argv)
if (si == NULL)
goto end;
if (kparam != NULL) {
EVP_PKEY_CTX *pctx;
pctx = CMS_SignerInfo_get0_pkey_ctx(si);
EVP_PKEY_CTX *pctx = CMS_SignerInfo_get0_pkey_ctx(si);

if (!cms_set_pkey_param(pctx, kparam->param))
goto end;
}
Expand Down Expand Up @@ -1230,6 +1235,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 */
Expand Down Expand Up @@ -1283,6 +1289,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);
Expand Down Expand Up @@ -1313,6 +1320,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");
Expand Down Expand Up @@ -1364,14 +1372,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);
Expand All @@ -1384,6 +1392,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");
Expand Down Expand Up @@ -1411,14 +1420,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;
Expand Down Expand Up @@ -1470,6 +1480,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++) {
Expand Down
35 changes: 17 additions & 18 deletions apps/smime.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,13 @@ int smime_main(int argc, char **argv)
goto opthelp;
}
if (!opt_cipher_any(ciphername, &cipher))
goto opthelp;
goto opthelp;
if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
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;
}

Expand Down Expand Up @@ -490,11 +489,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)) {
Expand Down Expand Up @@ -575,9 +573,11 @@ int smime_main(int argc, char **argv)
if (operation == SMIME_ENCRYPT) {
if (indef)
flags |= PKCS7_STREAM;
p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, app_get0_propq());
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.
Expand All @@ -590,15 +590,13 @@ int smime_main(int argc, char **argv)
flags |= PKCS7_STREAM;
}
flags |= PKCS7_PARTIAL;
p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
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;
}
Expand Down Expand Up @@ -639,9 +637,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;
}
Expand Down

0 comments on commit fff9574

Please sign in to comment.