Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/about/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
5 changes: 5 additions & 0 deletions docs/reference/feature-matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/org/perlonjava/runtime/perlmodule/NetSSLeay.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ private Map<K, V> delegate() {
private static final Map<Long, EvpMdCtx> EVP_MD_CTX_HANDLES = new CurrentRuntimeMap<>(s -> s.evpMdContexts);
private static final Map<Long, HmacCtx> HMAC_CTX_HANDLES = new CurrentRuntimeMap<>(s -> s.hmacContexts);
private static final Map<Long, KeyPair> 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<Long, Long> ASN1_TIME_HANDLES = new CurrentRuntimeMap<>(s -> s.asn1Times);
private static final Map<Long, SslCtxState> CTX_HANDLES = new CurrentRuntimeMap<>(s -> s.sslContexts);
private static final Map<Long, SslState> SSL_HANDLES = new CurrentRuntimeMap<>(s -> s.sslStates);
Expand Down Expand Up @@ -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
Expand All @@ -3821,6 +3826,12 @@ public static RuntimeList RSA_generate_key(RuntimeArray args, int ctx) {

try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
// 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]
kpg.initialize(bits, SECURE_RANDOM);

// Call callback during key generation if provided
Expand Down Expand Up @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions src/test/resources/unit/netssleay_rsa_key_size_bounds.t
Original file line number Diff line number Diff line change
@@ -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);
Loading