From 3aab9a0fe4762c386396fa7c40f24b493796edfe Mon Sep 17 00:00:00 2001 From: "Flavio S. Glock" Date: Tue, 24 Nov 2026 18:35:49 +0100 Subject: [PATCH 1/2] fix(netssleay): bound RSA key generation sizes Reject RSA_generate_key sizes below the legacy 1024-bit floor and above 8192 bits before key generation. Retain 1024-bit generation for the bundled fast RSA round-trip test and existing OpenSSL-compatible callers, with a narrow CodeQL suppression documenting that exception. Generated with [Codex](https://openai.com/codex) Co-Authored-By: Codex --- docs/about/changelog.md | 1 + .../runtime/perlmodule/NetSSLeay.java | 31 ++++++++++++++- .../unit/netssleay_rsa_key_size_bounds.t | 39 +++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/unit/netssleay_rsa_key_size_bounds.t diff --git a/docs/about/changelog.md b/docs/about/changelog.md index 5f3d542ed4..d4f3bd89d9 100644 --- a/docs/about/changelog.md +++ b/docs/about/changelog.md @@ -6,6 +6,7 @@ priorities and future plans. ## Work in progress +- Bound Net::SSLeay RSA key generation to 1024–8192 bits, retaining 1024 bits for the bundled fast RSA round-trip test and legacy callers while rejecting weaker or unbounded sizes. - Honor overloaded logical negation, warn on distinct constant-sub redefinitions with equal values, and preserve reference-valued scalar slots through localized glob aliases (#2122, #2075, #1939). - Preserve the `Can't use a hash as a reference` diagnostic for `%$hashref->{key}` and hash slices (#2132). - Preserve file contents on `sysopen` without `O_TRUNC`, fail `open` on undefined paths, avoid duplicate `closedir` warnings, and match Socket lookup flags and return values (#2128, #2125, #2095). diff --git a/src/main/java/org/perlonjava/runtime/perlmodule/NetSSLeay.java b/src/main/java/org/perlonjava/runtime/perlmodule/NetSSLeay.java index 1cc0d9f282..ed2e9f73a4 100644 --- a/src/main/java/org/perlonjava/runtime/perlmodule/NetSSLeay.java +++ b/src/main/java/org/perlonjava/runtime/perlmodule/NetSSLeay.java @@ -366,6 +366,8 @@ private Map delegate() { private static final Map EVP_MD_CTX_HANDLES = new CurrentRuntimeMap<>(s -> s.evpMdContexts); private static final Map HMAC_CTX_HANDLES = new CurrentRuntimeMap<>(s -> s.hmacContexts); private static final Map RSA_HANDLES = new CurrentRuntimeMap<>(s -> s.rsaKeys); + private static final int MIN_RSA_KEY_BITS = 1024; + private static final int MAX_RSA_KEY_BITS = 8192; private static final Map ASN1_TIME_HANDLES = new CurrentRuntimeMap<>(s -> s.asn1Times); private static final Map CTX_HANDLES = new CurrentRuntimeMap<>(s -> s.sslContexts); private static final Map SSL_HANDLES = new CurrentRuntimeMap<>(s -> s.sslStates); @@ -3798,7 +3800,10 @@ public static RuntimeList BIO_eof(RuntimeArray args, int ctx) { public static RuntimeList RSA_generate_key(RuntimeArray args, int ctx) { // RSA_generate_key(bits, e, [cb], [cb_arg]) - int bits = args.size() > 0 ? (int) args.get(0).getLong() : 2048; + int bits = args.size() > 0 ? parseRsaKeySize(args.get(0)) : 2048; + if (bits < MIN_RSA_KEY_BITS || bits > MAX_RSA_KEY_BITS) { + return new RuntimeScalar().getList(); + } // e (public exponent) is typically 65537 — Java handles this internally // Handle callback argument @@ -3821,6 +3826,12 @@ public static RuntimeList RSA_generate_key(RuntimeArray args, int ctx) { try { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); + // The bundled netssleay_phase5_6.t uses 1024 bits to keep its RSA round-trip + // test fast. Preserve that OpenSSL-compatible legacy request; 1024-bit RSA + // is not suitable for new key material, which should use at least 2048 bits. + // Reject weaker and unbounded sizes above; suppress only this documented + // legacy-compatible generation call. + // codeql[java/insufficient-key-size] kpg.initialize(bits, SECURE_RANDOM); // Call callback during key generation if provided @@ -3854,6 +3865,24 @@ public static RuntimeList RSA_generate_key(RuntimeArray args, int ctx) { } } + private static int parseRsaKeySize(RuntimeScalar requestedBits) { + try { + int type = RuntimeScalarType.effectiveType(requestedBits); + if (type == RuntimeScalarType.INTEGER || type == RuntimeScalarType.DOUBLE) { + double number = requestedBits.getDouble(); + if (!Double.isFinite(number) || number != Math.rint(number) + || number < MIN_RSA_KEY_BITS || number > MAX_RSA_KEY_BITS) { + return -1; + } + return (int) number; + } + String text = requestedBits.toString().trim(); + return new java.math.BigDecimal(text).intValueExact(); + } catch (NumberFormatException | ArithmeticException e) { + return -1; + } + } + public static RuntimeList RSA_free(RuntimeArray args, int ctx) { long handleId = args.size() > 0 ? args.get(0).getLong() : 0; RSA_HANDLES.remove(handleId); diff --git a/src/test/resources/unit/netssleay_rsa_key_size_bounds.t b/src/test/resources/unit/netssleay_rsa_key_size_bounds.t new file mode 100644 index 0000000000..95b2a4c35b --- /dev/null +++ b/src/test/resources/unit/netssleay_rsa_key_size_bounds.t @@ -0,0 +1,39 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Config; +use Test::More; + +BEGIN { + Test::More::plan(skip_all => 'tests PerlOnJava Net::SSLeay implementation') + unless $Config{archname} =~ /^java-/; + eval { require Net::SSLeay; Net::SSLeay->import; 1 } + or do { + require Test::More; + Test::More->import; + Test::More::plan(skip_all => 'Net::SSLeay not available'); + }; +} + +plan tests => 7; + +for my $case ( + [ 0, 'zero-bit RSA key' ], + [ 512, '512-bit RSA key' ], + [ 1023, 'sub-1024-bit RSA key' ], + [ 8193, 'RSA key above the generation limit' ], + [ 'not-a-number', 'nonnumeric RSA key size' ], +) { + my ($bits, $description) = @$case; + my ($key, $completed); + $completed = eval { + $key = Net::SSLeay::RSA_generate_key($bits, 65537); + 1; + }; + ok($completed && !defined($key), "$description returns undef"); +} + +my $legacyKey = Net::SSLeay::RSA_generate_key(1024, 65537); +ok(defined($legacyKey), 'the bundled fast round-trip test size remains available'); +is(Net::SSLeay::RSA_size($legacyKey), 128, 'legacy 1024-bit RSA size is preserved'); +Net::SSLeay::RSA_free($legacyKey); From 13fe24e36db5299d28e5ab12daf53205c05cddc7 Mon Sep 17 00:00:00 2001 From: "Flavio S. Glock" Date: Tue, 24 Nov 2026 18:37:54 +0100 Subject: [PATCH 2/2] docs(netssleay): explain legacy RSA test size Document that the bundled RSA round-trip test requests 1024 bits to keep the test fast, and distinguish that compatibility exception from new-key guidance. Generated with [Codex](https://openai.com/codex) Co-Authored-By: Codex --- docs/reference/feature-matrix.md | 5 +++++ .../java/org/perlonjava/runtime/perlmodule/NetSSLeay.java | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/reference/feature-matrix.md b/docs/reference/feature-matrix.md index a08727fc5e..ada738aeda 100644 --- a/docs/reference/feature-matrix.md +++ b/docs/reference/feature-matrix.md @@ -941,6 +941,11 @@ The `:encoding()` layer supports all encodings provided by Java's `Charset.forNa - ✅ **YAML::Syck** compatibility module backed by bundled `YAML::PP`. - ✅ **IO::Socket::SSL** module backed by Java `javax.net.ssl` SSLEngine. - ✅ **Net::SSLeay** module backed by Java security APIs (2327 CPAN tests pass). + `RSA_generate_key` accepts 1024–8192-bit requests. The 1024-bit legacy floor + remains because `netssleay_phase5_6.t` uses it to keep the RSA round-trip test + fast; 1024-bit RSA is not suitable for new key material. Use at least 2048 + bits for new keys ([CodeQL guidance](https://codeql.github.com/codeql-query-help/java/java-insufficient-key-size/)); + 3072 bits provides 128-bit NIST security strength ([SP 800-57, Part 1 Rev. 5](https://csrc.nist.gov/pubs/sp/800/57/pt1/r5/final)). - ✅ **Plack::Handler::Netty** PSGI web server with HTTP/HTTPS, streaming, 32k+ req/sec. See [Web Server Guide](../../examples/http_server_plack/README.md). ### DBI module diff --git a/src/main/java/org/perlonjava/runtime/perlmodule/NetSSLeay.java b/src/main/java/org/perlonjava/runtime/perlmodule/NetSSLeay.java index ed2e9f73a4..a61f698616 100644 --- a/src/main/java/org/perlonjava/runtime/perlmodule/NetSSLeay.java +++ b/src/main/java/org/perlonjava/runtime/perlmodule/NetSSLeay.java @@ -3826,9 +3826,9 @@ public static RuntimeList RSA_generate_key(RuntimeArray args, int ctx) { try { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); - // The bundled netssleay_phase5_6.t uses 1024 bits to keep its RSA round-trip - // test fast. Preserve that OpenSSL-compatible legacy request; 1024-bit RSA - // is not suitable for new key material, which should use at least 2048 bits. + // src/test/resources/unit/netssleay_phase5_6.t uses 1024 bits to keep its + // RSA round-trip test fast. Preserve that OpenSSL-compatible legacy request. + // 1024-bit RSA is not suitable for new keys; use at least 2048 bits. // Reject weaker and unbounded sizes above; suppress only this documented // legacy-compatible generation call. // codeql[java/insufficient-key-size]