Commit 5928f99e9c for openssl.org

commit 5928f99e9cee5203f52081a040b64da6c4616662
Author: SebastianKeltz <Sebastiankeltz@icloud.com>
Date:   Mon Nov 23 06:05:07 2026 -0500

    Reject conflicting operation options in openssl cms

    Check for conflicts during argument parsing so a second operation
    cannot silently replace the first.  Preserve repeated uses of the same
    operation, document the restriction, and cover both argument orders
    in the CMS test suite.

    Resolves: https://github.com/openssl/openssl/issues/33914
    Assisted-by: Codex:GPT-6
    Reviewed-by: Tomas Mraz <tomas@openssl.foundation>
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    Merge-date: Tue Nov 24 17:05:05 2026
    Merged-from: https://github.com/openssl/openssl/pull/33922

diff --git a/apps/cms.c b/apps/cms.c
index ee59b9faa0..8a94fd824a 100644
--- a/apps/cms.c
+++ b/apps/cms.c
@@ -407,6 +407,7 @@ int cms_main(int argc, char **argv)
     int recipidx = -1;
     long ltmp;
     const char *mime_eol = "\n";
+    const char *operation_flag = NULL;
     OPTION_CHOICE o;
     OSSL_LIB_CTX *libctx = app_get0_libctx();

@@ -416,6 +417,8 @@ int cms_main(int argc, char **argv)
     opt_set_unknown_name("cipher");
     prog = opt_init(argc, argv, cms_options);
     while ((o = opt_next()) != OPT_EOF) {
+        int selected_operation = 0;
+
         switch (o) {
         case OPT_EOF:
         case OPT_ERR:
@@ -439,25 +442,25 @@ int cms_main(int argc, char **argv)
             break;

         case OPT_ENCRYPT:
-            operation = SMIME_ENCRYPT;
+            selected_operation = SMIME_ENCRYPT;
             break;
         case OPT_DECRYPT:
-            operation = SMIME_DECRYPT;
+            selected_operation = SMIME_DECRYPT;
             break;
         case OPT_SIGN:
-            operation = SMIME_SIGN;
+            selected_operation = SMIME_SIGN;
             break;
         case OPT_VERIFY:
-            operation = SMIME_VERIFY;
+            selected_operation = SMIME_VERIFY;
             break;
         case OPT_RESIGN:
-            operation = SMIME_RESIGN;
+            selected_operation = SMIME_RESIGN;
             break;
         case OPT_SIGN_RECEIPT:
-            operation = SMIME_SIGN_RECEIPT;
+            selected_operation = SMIME_SIGN_RECEIPT;
             break;
         case OPT_VERIFY_RECEIPT:
-            operation = SMIME_VERIFY_RECEIPT;
+            selected_operation = SMIME_VERIFY_RECEIPT;
             rctfile = opt_arg();
             break;
         case OPT_VERIFY_PARTIAL:
@@ -467,34 +470,34 @@ int cms_main(int argc, char **argv)
             verify_retcode = 1;
             break;
         case OPT_DIGEST_CREATE:
-            operation = SMIME_DIGEST_CREATE;
+            selected_operation = SMIME_DIGEST_CREATE;
             break;
         case OPT_DIGEST:
             digesthex = opt_arg();
             break;
         case OPT_DIGEST_VERIFY:
-            operation = SMIME_DIGEST_VERIFY;
+            selected_operation = SMIME_DIGEST_VERIFY;
             break;
         case OPT_COMPRESS:
-            operation = SMIME_COMPRESS;
+            selected_operation = SMIME_COMPRESS;
             break;
         case OPT_UNCOMPRESS:
-            operation = SMIME_UNCOMPRESS;
+            selected_operation = SMIME_UNCOMPRESS;
             break;
         case OPT_ED_ENCRYPT:
-            operation = SMIME_ENCRYPTED_ENCRYPT;
+            selected_operation = SMIME_ENCRYPTED_ENCRYPT;
             break;
         case OPT_ED_DECRYPT:
-            operation = SMIME_ENCRYPTED_DECRYPT;
+            selected_operation = SMIME_ENCRYPTED_DECRYPT;
             break;
         case OPT_DATA_CREATE:
-            operation = SMIME_DATA_CREATE;
+            selected_operation = SMIME_DATA_CREATE;
             break;
         case OPT_DATA_OUT:
-            operation = SMIME_DATA_OUT;
+            selected_operation = SMIME_DATA_OUT;
             break;
         case OPT_CMSOUT:
-            operation = SMIME_CMSOUT;
+            selected_operation = SMIME_CMSOUT;
             break;

         case OPT_DEBUG_DECRYPT:
@@ -850,6 +853,16 @@ int cms_main(int argc, char **argv)
             wrapname = opt_flag() + 1;
             break;
         }
+
+        if (selected_operation != 0) {
+            if (operation != 0 && operation != selected_operation) {
+                BIO_printf(bio_err, "%s: Cannot use %s together with %s\n",
+                    prog, opt_flag(), operation_flag);
+                goto opthelp;
+            }
+            operation = selected_operation;
+            operation_flag = opt_flag();
+        }
     }
     if (!app_RAND_load())
         goto end;
diff --git a/doc/man1/openssl-cms.pod.in b/doc/man1/openssl-cms.pod.in
index 3226f49f4f..168acbb413 100644
--- a/doc/man1/openssl-cms.pod.in
+++ b/doc/man1/openssl-cms.pod.in
@@ -140,6 +140,7 @@ There are a number of operation options that set the type of operation to be
 performed: encrypt, decrypt, sign, verify, resign, sign_receipt, verify_receipt,
 digest_create, digest_verify, compress, uncompress,
 EncryptedData_encrypt, EncryptedData_decrypt, data_create, data_out, or cmsout.
+These operation options are mutually exclusive.
 The relevance of the other options depends on the operation type
 and their meaning may vary according to it.

diff --git a/test/recipes/80-test_cms.t b/test/recipes/80-test_cms.t
index 2f5af7d871..ccaf1ef5a5 100644
--- a/test/recipes/80-test_cms.t
+++ b/test/recipes/80-test_cms.t
@@ -13,7 +13,7 @@ use warnings;
 use POSIX;
 use File::Spec::Functions qw/catfile/;
 use File::Compare qw/compare_text compare/;
-use OpenSSL::Test qw/:DEFAULT srctop_dir srctop_file bldtop_dir bldtop_file with data_file slurp_file/;
+use OpenSSL::Test qw/:DEFAULT srctop_dir srctop_file bldtop_dir bldtop_file with data_file slurp_file app_fails/;

 use OpenSSL::Test::Utils;

@@ -56,7 +56,22 @@ my ($no_des, $no_dh, $no_dsa, $no_ec, $no_ec2m, $no_rc2, $no_zlib)

 $no_rc2 = 1 if disabled("legacy");

-plan tests => 43;
+plan tests => 44;
+
+subtest "CMS operation option validation" => sub {
+    plan tests => 5;
+
+    app_fails("cms", "Reject conflicting operation options",
+        qr/Cannot use -digest_create together with -data_create/,
+        "-data_create", "-digest_create", "-in", $smcont, "-outform", "PEM");
+    app_fails("cms", "Reject conflicting operation options in reverse order",
+        qr/Cannot use -data_create together with -digest_create/,
+        "-digest_create", "-data_create", "-in", $smcont, "-outform", "PEM");
+
+    ok(run(app(["openssl", "cms", "-data_create", "-data_create",
+                "-in", $smcont, "-outform", "PEM"], stdout => 0)),
+        "Accept a repeated operation option");
+};

 ok(run(test(["pkcs7_test", srctop_file("test", "certs", "servercert.pem"),
              srctop_file("test", "certs", "serverkey.pem")])), "test pkcs7");