cms encrypt, better OBJ_nid2obj() return check
authorJames Muir <james@openssl.org>
Sat, 14 Oct 2023 23:36:57 +0000 (19:36 -0400)
committerTomas Mraz <tomas@openssl.org>
Wed, 18 Oct 2023 14:51:00 +0000 (16:51 +0200)
Fixes #22225

In OBJ_nid2obj(), if the NID does not have an OID, then a pointer to
the special "undefined" ASN1_OBJECT is returned.  Check for the
undefined-ASN1_OBJECT and return an error.  Also, add a test for this
in 80-test_cms.t.

Testing:

  #!/bin/bash -x

  shopt -s expand_aliases

  alias openssl="LD_LIBRARY_PATH=~/git/openssl ~/git/openssl/apps/openssl"

  echo "This is a confidential message.  It should be encrypted." > msg.txt

  ## this should fail b/c there is no OID for aes-256-ctr
  openssl cms -encrypt -in msg.txt -aes-256-ctr -out msg.txt.cms -recip demos/cms/signer.pem
  echo $?

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22392)

(cherry picked from commit bd160912dcc5e39bcdc925d9aa6538f20e37ad16)

crypto/cms/cms_enc.c
test/recipes/80-test_cms.t

index f7007c12319e66d3af452b36a4fef306d68f8fb3..914343398680fa6120da135749e58c575fa562cf 100644 (file)
@@ -81,7 +81,7 @@ BIO *ossl_cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
 
     if (enc) {
         calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx));
-        if (calg->algorithm == NULL) {
+        if (calg->algorithm == NULL || calg->algorithm->nid == NID_undef) {
             ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_ENCRYPTION_ALGORITHM);
             goto err;
         }
index 9fa2daae8a417894057ee17e247167d8b117fed5..75d6f523898badd26cdccb0a8f99026ecf7a4238 100644 (file)
@@ -50,7 +50,7 @@ my ($no_des, $no_dh, $no_dsa, $no_ec, $no_ec2m, $no_rc2, $no_zlib)
 
 $no_rc2 = 1 if disabled("legacy");
 
-plan tests => 17;
+plan tests => 18;
 
 ok(run(test(["pkcs7_test"])), "test pkcs7");
 
@@ -1060,3 +1060,14 @@ with({ exit_checker => sub { return shift == 3; } },
             "issue#21986");
         }
     });
+
+# Test for problem reported in #22225
+with({ exit_checker => sub { return shift == 3; } },
+    sub {
+       ok(run(app(['openssl', 'cms', '-encrypt',
+                   '-in', srctop_file("test", "smcont.txt"),
+                   '-aes-256-ctr', '-recip',
+                   catfile($smdir, "smec1.pem"),
+                  ])),
+          "Check for failure when cipher does not have an assigned OID (issue#22225)");
+     });