Commit cef5956599 for openssl.org

commit cef5956599323989348c1bc304be25ae5c053c03
Author: rudy-07 <kanhaiffco2007@gmail.com>
Date:   Thu Nov 12 01:34:03 2026 +0530

    apps/x509: Fix DER output for -x509toreq

    When using "openssl x509 -x509toreq -outform DER", the utility previously
    invoked X509_REQ_print_ex() on the output BIO, polluting the binary stream
    with human-readable text. It then called i2d_X509_bio() with the input
    certificate x instead of serializing the generated certificate request rq.
    Consequently, the command produced corrupt output containing text followed
    by an unverified certificate rather than a valid DER-encoded PKCS#10 request.

    Fix this by:
    - Writing the request via i2d_X509_REQ_bio() when outformat is FORMAT_ASN1.
    - Removing the unwanted X509_REQ_print_ex() call on the binary output path.
    - Reporting an error if an unsupported output format is provided.
    - Adding regression test coverage for -x509toreq with -outform DER in
      test/recipes/25-test_x509.t.

    Resolves: https://github.com/openssl/openssl/issues/33495
    Fixes: b24cfd6bf4d6 "apps/x509.c: Major code, user guidance, and documentation cleanup"
    Assisted-by: Antigravity:gemini-3.8-flash
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    Reviewed-by: Norbert Pocs <norbertp@openssl.org>
    Merge-date: Fri Nov 20 15:20:17 2026
    Merged-from: https://github.com/openssl/openssl/pull/33674

diff --git a/apps/x509.c b/apps/x509.c
index cfad10b96c..19704c0617 100644
--- a/apps/x509.c
+++ b/apps/x509.c
@@ -1051,10 +1051,12 @@ cert_loop:
             goto err;
         if (!noout) {
             if (outformat == FORMAT_ASN1) {
-                X509_REQ_print_ex(out, rq, get_nameopt(), X509_FLAG_COMPAT);
-                i = i2d_X509_bio(out, x);
-            } else {
+                i = i2d_X509_REQ_bio(out, rq);
+            } else if (outformat == FORMAT_PEM) {
                 i = PEM_write_bio_X509_REQ(out, rq);
+            } else {
+                BIO_puts(bio_err, "Bad output format specified for outfile\n");
+                goto err;
             }
             if (!i) {
                 BIO_puts(bio_err,
diff --git a/test/recipes/25-test_x509.t b/test/recipes/25-test_x509.t
index f6ac1ea683..7ad11a4b66 100644
--- a/test/recipes/25-test_x509.t
+++ b/test/recipes/25-test_x509.t
@@ -607,12 +607,13 @@ ok(!run(app(["openssl", "x509", "-req", "-in", $in_csr, "-signkey", $in_key,
             "-extfile", $invextfile])));

 subtest "signing output detection with -key" => sub {
-    plan tests => 5;
+    plan tests => 7;

     my $v1_cert = srctop_file("test", "testx509.pem");
     my $with_days_cert = "x509-resigned-with-days.pem";
     my $default_ext_cert = "x509-resigned-with-default-ext.pem";
     my $request = "x509-to-request-with-ext.pem";
+    my $request_der = "x509-to-request.der";

     ok(!run(app(["openssl", "x509", "-in", $v1_cert, "-noout",
                  "-days", "1"])),
@@ -628,6 +629,11 @@ subtest "signing output detection with -key" => sub {
                 "-key", $in_key, "-extfile", $cnf,
                 "-extensions", "v3_req", "-out", $request])),
        "accept extension options when signing a request with -key");
+    ok(run(app(["openssl", "x509", "-in", $v1_cert, "-x509toreq",
+                "-key", $in_key, "-outform", "DER", "-out", $request_der])),
+       "generate DER-encoded certificate request with -x509toreq");
+    ok(run(app(["openssl", "req", "-in", $request_der, "-inform", "DER", "-noout"])),
+       "verify generated DER certificate request");
 };

 # Tests for issue #16080 (fixed in 1.1.1o)