GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid NPE in JdkSslClientContext when TrustManagerFactory returns null by daguimu · Pull Request #16690 · netty/netty

Conversation

@daguimu

@daguimu daguimu commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Problem

Since 4.1.114, building a JDK-backed client SslContext throws an opaque SSLException caused by NullPointerException whenever the configured TrustManagerFactory returns null from getTrustManagers():

javax.net.ssl.SSLException: failed to initialize the client-side SSL context
Caused by: java.lang.NullPointerException
    at io.netty.handler.ssl.JdkSslClientContext.wrapIfNeeded(JdkSslClientContext.java:322)
    at io.netty.handler.ssl.JdkSslClientContext.newSSLContext(JdkSslClientContext.java:301)

Returning null is valid per the TrustManagerFactory contract — SSLContext.init(..., null, ...) falls back to the JDK's default trust store, and that was the working behavior in 4.1.113 and earlier.

Root Cause

4.1.114 added ResumptionController wiring. newSSLContext now routes the trust managers through a helper:

ctx.init(keyManagerFactory == null ? null : keyManagerFactory.getKeyManagers(),
         trustManagerFactory == null ? null :
                 wrapIfNeeded(trustManagerFactory.getTrustManagers(), resumptionController),
         secureRandom);

and wrapIfNeeded unconditionally dereferences tms.length:

private static TrustManager[] wrapIfNeeded(TrustManager[] tms, ResumptionController resumptionController) {
    if (resumptionController != null) {
        for (int i = 0; i < tms.length; i++) {     // NPE when tms is null
            tms[i] = resumptionController.wrapIfNeeded(tms[i]);
        }
    }
    return tms;
}

There is no guard for tms == null, so any TrustManagerFactory whose getTrustManagers() legitimately returns null regresses with an NPE. Before 4.1.114, null flowed straight to SSLContext.init, which handled it natively.

Fix

Short-circuit wrapIfNeeded when either tms or the ResumptionController is null and return the input unchanged. null then reaches SSLContext.init exactly as it did pre-4.1.114.

private static TrustManager[] wrapIfNeeded(TrustManager[] tms, ResumptionController resumptionController) {
    if (tms == null || resumptionController == null) {
        return tms;
    }
    for (int i = 0; i < tms.length; i++) {
        tms[i] = resumptionController.wrapIfNeeded(tms[i]);
    }
    return tms;
}

Scope is intentionally limited to JdkSslClientContext.wrapIfNeeded, matching the reporter's observation. No other callers or formatting are touched.

Tests Added

Change point Test
wrapIfNeeded null-array guard JdkSslClientContextTest.testTrustManagerFactoryReturningNullDoesNotThrowNpe — installs a custom TrustManagerFactorySpi whose engineGetTrustManagers() returns null, builds the client SslContext via SslContextBuilder.forClient().sslProvider(JDK).trustManager(tmf), and asserts it is returned without throwing. Verified to reproduce the SSLException/NullPointerException on the pre-fix code and to pass after the fix.

All 55 tests in SslContextBuilderTest, SslContextTest, and JdkSslClientContextTest pass locally (0 failures, 0 errors, 1 pre-existing skip).

Impact

  • Restores pre-4.1.114 behavior: TrustManagerFactory implementations that return null from getTrustManagers() (i.e., "use the JDK defaults") work again with the JDK-backed SslContextBuilder.forClient().
  • Purely defensive change inside a private helper — public APIs, semantics when a non-null TrustManager[] is supplied, and the ResumptionController wrapping path are all unchanged.

Fixes #14488

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@daguimu
Avoid NPE in JdkSslClientContext when TrustManagerFactory returns null
6585499
Motivation:

Since 4.1.114 (commit that added ResumptionController wiring),
JdkSslClientContext.newSSLContext passes the TrustManager[] returned by
trustManagerFactory.getTrustManagers() through wrapIfNeeded before
handing it to SSLContext.init. wrapIfNeeded unconditionally dereferences
tms.length, so if the factory returns null — a valid return value that
asks SSLContext to fall back to the JDK default trust store, and which
worked in 4.1.113 — we now throw a NullPointerException wrapped in an
SSLException ("failed to initialize the client-side SSL context").

Modification:

Short-circuit wrapIfNeeded when either tms or the ResumptionController
is null, returning the input as-is. Null then flows into SSLContext.init
exactly as it did before 4.1.114.

Added JdkSslClientContextTest.testTrustManagerFactoryReturningNullDoesNotThrowNpe
which installs a custom TrustManagerFactorySpi that returns null from
engineGetTrustManagers and asserts the client SslContext builds without
throwing. The test fails against the pre-fix code with the NPE from the
issue and passes after the fix.

Result:

TrustManagerFactory implementations that return null from
getTrustManagers() can be used with JDK-backed SslContextBuilder again,
restoring the 4.1.113 behavior.

Fixes netty#14488
@daguimu
daguimu force-pushed the fix/jdk-ssl-client-null-trust-managers-14488 branch from 67ed129 to 6585499 Compare April 23, 2026 16:23
@daguimu daguimu mentioned this pull request Apr 23, 2026
Avoid NPE in JdkSslServerContext when TrustManagerFactory returns null #16691
Merged
@normanmaurer normanmaurer added this to the 4.1.133.Final milestone Apr 23, 2026
normanmaurer pushed a commit that referenced this pull request Apr 24, 2026
@daguimu
Avoid NPE in JdkSslServerContext when TrustManagerFactory returns null (
4a2a757
#16691)

## Problem

`JdkSslServerContext.newSSLContext` passes
`trustManagerFactory.getTrustManagers()` through
`wrapTrustManagerIfNeeded` before calling `SSLContext.init`. If a caller
supplies a `TrustManagerFactory` whose `getTrustManagers()` legitimately
returns `null` (a valid return value that asks `SSLContext.init` to fall
back to the JDK default trust store), building the server context fails
with:

```
javax.net.ssl.SSLException: failed to initialize the server-side SSL context
Caused by: java.lang.NullPointerException: Cannot read the array length because "trustManagers" is null
    at io.netty.handler.ssl.JdkSslServerContext.wrapTrustManagerIfNeeded(JdkSslServerContext.java:364)
    at io.netty.handler.ssl.JdkSslServerContext.newSSLContext(JdkSslServerContext.java:341)
```

This mirrors the client-side bug reported in #14488 / fixed in PR
#16690, but in a pre-existing server-side code path:
`JdkSslServerContext` has routed trust managers through
`wrapTrustManagerIfNeeded` since the `EnhancingX509ExtendedTrustManager`
wrapping was introduced, long before the `ResumptionController` wiring
was added — so the NPE has been latent on the server path for multiple
releases when a user supplies such a factory.

## Root Cause

```java
private static TrustManager[] wrapTrustManagerIfNeeded(
        TrustManager[] trustManagers, ResumptionController resumptionController) {
    if (WRAP_TRUST_MANAGER && PlatformDependent.javaVersion() >= 7) {
        for (int i = 0; i < trustManagers.length; i++) {     // NPE when trustManagers is null
            ...
        }
    }
    return trustManagers;
}
```

`WRAP_TRUST_MANAGER` is `true` on Java 7+ (guarded only by a FIPS
feature-check that passes on common JVMs), and `trustManagerFactory`
reaches this helper either as the user-supplied factory or — when the
user passes `null` — as a default-initialized `TrustManagerFactory`. The
user-supplied path is what fails: if the factory's `getTrustManagers()`
returns `null`, `trustManagers.length` dereferences `null`.

## Fix

Short-circuit `wrapTrustManagerIfNeeded` when `trustManagers` is `null`,
returning `null` so it flows into `SSLContext.init` exactly as it did
before any wrapping was applied. `SSLContext.init(km, null, sr)` is
explicitly documented to fall back to the default trust manager.

```java
if (trustManagers == null) {
    return null;
}
```

Scope is limited to this one helper. Public APIs, the
`EnhancingX509ExtendedTrustManager` wrapping path, and the
`ResumptionController` wrapping path are all unchanged.

## Tests Added

| Change point | Test |
|---|---|
| `wrapTrustManagerIfNeeded` null-array guard |
`JdkSslServerContextTest.testTrustManagerFactoryReturningNullDoesNotThrowNpe`
— installs a custom `TrustManagerFactorySpi` whose
`engineGetTrustManagers()` returns `null`, builds the server
`SslContext` via `SslContextBuilder.forServer(cert,
key).sslProvider(JDK).trustManager(tmf)`, and asserts it is returned
without throwing. Verified to reproduce the
`SSLException`/`NullPointerException` on the pre-fix code (`Cannot read
the array length because "trustManagers" is null`) and to pass after the
fix. |

All 36 tests in `JdkSslServerContextTest` pass locally (0 failures, 0
errors, 1 pre-existing skip).

## Impact

- `TrustManagerFactory` implementations that return `null` from
`getTrustManagers()` (i.e., "use the JDK defaults") now work with the
JDK-backed `SslContextBuilder.forServer()`.
- Purely defensive change inside a private helper. The fix pattern
parallels PR #16690 for `JdkSslClientContext.wrapIfNeeded`.

Paired with #16690 (same bug pattern on the client side).
@normanmaurer normanmaurer mentioned this pull request Apr 24, 2026
Avoid NPE in JdkSslServerContext when TrustManagerFactory returns nul… #16700
Merged
@normanmaurer normanmaurer added needs-cherry-pick-4.2 This PR should be cherry-picked to 4.2 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Apr 24, 2026
@normanmaurer
normanmaurer merged commit a96f448 into netty:4.1 Apr 24, 2026
19 of 21 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot

netty-project-bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #16701

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot netty-project-bot mentioned this pull request Apr 24, 2026
Auto-port 5.0: Avoid NPE in JdkSslClientContext when TrustManagerFactory returns null #16701
Merged
@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Apr 24, 2026
@netty-project-bot

netty-project-bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.2: #16702

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot removed the needs-cherry-pick-4.2 This PR should be cherry-picked to 4.2 once merged. label Apr 24, 2026
@netty-project-bot netty-project-bot mentioned this pull request Apr 24, 2026
Auto-port 4.2: Avoid NPE in JdkSslClientContext when TrustManagerFactory returns null #16702
Merged
normanmaurer pushed a commit that referenced this pull request Apr 24, 2026
@netty-project-bot @daguimu
Auto-port 5.0: Avoid NPE in JdkSslClientContext when TrustManagerFact…
31ab79a
…ory returns null (#16701)

Auto-port of #16690 to 5.0
Cherry-picked commit: a96f448

---
## Problem

Since 4.1.114, building a JDK-backed client `SslContext` throws an
opaque `SSLException` caused by `NullPointerException` whenever the
configured `TrustManagerFactory` returns `null` from
`getTrustManagers()`:

```
javax.net.ssl.SSLException: failed to initialize the client-side SSL context
Caused by: java.lang.NullPointerException
    at io.netty.handler.ssl.JdkSslClientContext.wrapIfNeeded(JdkSslClientContext.java:322)
    at io.netty.handler.ssl.JdkSslClientContext.newSSLContext(JdkSslClientContext.java:301)
```

Returning `null` is valid per the `TrustManagerFactory` contract —
`SSLContext.init(..., null, ...)` falls back to the JDK's default trust
store, and that was the working behavior in 4.1.113 and earlier.

## Root Cause

4.1.114 added `ResumptionController` wiring. `newSSLContext` now routes
the trust managers through a helper:

```java
ctx.init(keyManagerFactory == null ? null : keyManagerFactory.getKeyManagers(),
         trustManagerFactory == null ? null :
                 wrapIfNeeded(trustManagerFactory.getTrustManagers(), resumptionController),
         secureRandom);
```

and `wrapIfNeeded` unconditionally dereferences `tms.length`:

```java
private static TrustManager[] wrapIfNeeded(TrustManager[] tms, ResumptionController resumptionController) {
    if (resumptionController != null) {
        for (int i = 0; i < tms.length; i++) {     // NPE when tms is null
            tms[i] = resumptionController.wrapIfNeeded(tms[i]);
        }
    }
    return tms;
}
```

There is no guard for `tms == null`, so any `TrustManagerFactory` whose
`getTrustManagers()` legitimately returns `null` regresses with an NPE.
Before 4.1.114, `null` flowed straight to `SSLContext.init`, which
handled it natively.

## Fix

Short-circuit `wrapIfNeeded` when either `tms` or the
`ResumptionController` is `null` and return the input unchanged. `null`
then reaches `SSLContext.init` exactly as it did pre-4.1.114.

```java
private static TrustManager[] wrapIfNeeded(TrustManager[] tms, ResumptionController resumptionController) {
    if (tms == null || resumptionController == null) {
        return tms;
    }
    for (int i = 0; i < tms.length; i++) {
        tms[i] = resumptionController.wrapIfNeeded(tms[i]);
    }
    return tms;
}
```

Scope is intentionally limited to `JdkSslClientContext.wrapIfNeeded`,
matching the reporter's observation. No other callers or formatting are
touched.

## Tests Added

| Change point | Test |
|---|---|
| `wrapIfNeeded` null-array guard |
`JdkSslClientContextTest.testTrustManagerFactoryReturningNullDoesNotThrowNpe`
— installs a custom `TrustManagerFactorySpi` whose
`engineGetTrustManagers()` returns `null`, builds the client
`SslContext` via
`SslContextBuilder.forClient().sslProvider(JDK).trustManager(tmf)`, and
asserts it is returned without throwing. Verified to reproduce the
`SSLException`/`NullPointerException` on the pre-fix code and to pass
after the fix. |

All 55 tests in `SslContextBuilderTest`, `SslContextTest`, and
`JdkSslClientContextTest` pass locally (0 failures, 0 errors, 1
pre-existing skip).

## Impact

- Restores pre-4.1.114 behavior: `TrustManagerFactory` implementations
that return `null` from `getTrustManagers()` (i.e., "use the JDK
defaults") work again with the JDK-backed
`SslContextBuilder.forClient()`.
- Purely defensive change inside a private helper — public APIs,
semantics when a non-null `TrustManager[]` is supplied, and the
`ResumptionController` wrapping path are all unchanged.

Fixes #14488

Co-authored-by: Guimu <[email protected]>
normanmaurer pushed a commit that referenced this pull request Apr 24, 2026
@netty-project-bot @daguimu
Auto-port 4.2: Avoid NPE in JdkSslClientContext when TrustManagerFact…
386f9d3
…ory returns null (#16702)

Auto-port of #16690 to 4.2
Cherry-picked commit: a96f448

---
## Problem

Since 4.1.114, building a JDK-backed client `SslContext` throws an
opaque `SSLException` caused by `NullPointerException` whenever the
configured `TrustManagerFactory` returns `null` from
`getTrustManagers()`:

```
javax.net.ssl.SSLException: failed to initialize the client-side SSL context
Caused by: java.lang.NullPointerException
    at io.netty.handler.ssl.JdkSslClientContext.wrapIfNeeded(JdkSslClientContext.java:322)
    at io.netty.handler.ssl.JdkSslClientContext.newSSLContext(JdkSslClientContext.java:301)
```

Returning `null` is valid per the `TrustManagerFactory` contract —
`SSLContext.init(..., null, ...)` falls back to the JDK's default trust
store, and that was the working behavior in 4.1.113 and earlier.

## Root Cause

4.1.114 added `ResumptionController` wiring. `newSSLContext` now routes
the trust managers through a helper:

```java
ctx.init(keyManagerFactory == null ? null : keyManagerFactory.getKeyManagers(),
         trustManagerFactory == null ? null :
                 wrapIfNeeded(trustManagerFactory.getTrustManagers(), resumptionController),
         secureRandom);
```

and `wrapIfNeeded` unconditionally dereferences `tms.length`:

```java
private static TrustManager[] wrapIfNeeded(TrustManager[] tms, ResumptionController resumptionController) {
    if (resumptionController != null) {
        for (int i = 0; i < tms.length; i++) {     // NPE when tms is null
            tms[i] = resumptionController.wrapIfNeeded(tms[i]);
        }
    }
    return tms;
}
```

There is no guard for `tms == null`, so any `TrustManagerFactory` whose
`getTrustManagers()` legitimately returns `null` regresses with an NPE.
Before 4.1.114, `null` flowed straight to `SSLContext.init`, which
handled it natively.

## Fix

Short-circuit `wrapIfNeeded` when either `tms` or the
`ResumptionController` is `null` and return the input unchanged. `null`
then reaches `SSLContext.init` exactly as it did pre-4.1.114.

```java
private static TrustManager[] wrapIfNeeded(TrustManager[] tms, ResumptionController resumptionController) {
    if (tms == null || resumptionController == null) {
        return tms;
    }
    for (int i = 0; i < tms.length; i++) {
        tms[i] = resumptionController.wrapIfNeeded(tms[i]);
    }
    return tms;
}
```

Scope is intentionally limited to `JdkSslClientContext.wrapIfNeeded`,
matching the reporter's observation. No other callers or formatting are
touched.

## Tests Added

| Change point | Test |
|---|---|
| `wrapIfNeeded` null-array guard |
`JdkSslClientContextTest.testTrustManagerFactoryReturningNullDoesNotThrowNpe`
— installs a custom `TrustManagerFactorySpi` whose
`engineGetTrustManagers()` returns `null`, builds the client
`SslContext` via
`SslContextBuilder.forClient().sslProvider(JDK).trustManager(tmf)`, and
asserts it is returned without throwing. Verified to reproduce the
`SSLException`/`NullPointerException` on the pre-fix code and to pass
after the fix. |

All 55 tests in `SslContextBuilderTest`, `SslContextTest`, and
`JdkSslClientContextTest` pass locally (0 failures, 0 errors, 1
pre-existing skip).

## Impact

- Restores pre-4.1.114 behavior: `TrustManagerFactory` implementations
that return `null` from `getTrustManagers()` (i.e., "use the JDK
defaults") work again with the JDK-backed
`SslContextBuilder.forClient()`.
- Purely defensive change inside a private helper — public APIs,
semantics when a non-null `TrustManager[]` is supplied, and the
`ResumptionController` wrapping path are all unchanged.

Fixes #14488

Co-authored-by: Guimu <[email protected]>
normanmaurer added a commit that referenced this pull request Apr 24, 2026
@normanmaurer @daguimu
Avoid NPE in JdkSslServerContext when TrustManagerFactory returns nul… (
0fc37d3
#16700)

…l (#16691)

`JdkSslServerContext.newSSLContext` passes
`trustManagerFactory.getTrustManagers()` through
`wrapTrustManagerIfNeeded` before calling `SSLContext.init`. If a caller
supplies a `TrustManagerFactory` whose `getTrustManagers()` legitimately
returns `null` (a valid return value that asks `SSLContext.init` to fall
back to the JDK default trust store), building the server context fails
with:

```
javax.net.ssl.SSLException: failed to initialize the server-side SSL context
Caused by: java.lang.NullPointerException: Cannot read the array length because "trustManagers" is null
    at io.netty.handler.ssl.JdkSslServerContext.wrapTrustManagerIfNeeded(JdkSslServerContext.java:364)
    at io.netty.handler.ssl.JdkSslServerContext.newSSLContext(JdkSslServerContext.java:341)
```

This mirrors the client-side bug reported in #14488 / fixed in PR
`JdkSslServerContext` has routed trust managers through
`wrapTrustManagerIfNeeded` since the `EnhancingX509ExtendedTrustManager`
wrapping was introduced, long before the `ResumptionController` wiring
was added — so the NPE has been latent on the server path for multiple
releases when a user supplies such a factory.

```java
private static TrustManager[] wrapTrustManagerIfNeeded(
        TrustManager[] trustManagers, ResumptionController resumptionController) {
    if (WRAP_TRUST_MANAGER && PlatformDependent.javaVersion() >= 7) {
        for (int i = 0; i < trustManagers.length; i++) {     // NPE when trustManagers is null
            ...
        }
    }
    return trustManagers;
}
```

`WRAP_TRUST_MANAGER` is `true` on Java 7+ (guarded only by a FIPS
feature-check that passes on common JVMs), and `trustManagerFactory`
reaches this helper either as the user-supplied factory or — when the
user passes `null` — as a default-initialized `TrustManagerFactory`. The
user-supplied path is what fails: if the factory's `getTrustManagers()`
returns `null`, `trustManagers.length` dereferences `null`.

Short-circuit `wrapTrustManagerIfNeeded` when `trustManagers` is `null`,
returning `null` so it flows into `SSLContext.init` exactly as it did
before any wrapping was applied. `SSLContext.init(km, null, sr)` is
explicitly documented to fall back to the default trust manager.

```java
if (trustManagers == null) {
    return null;
}
```

Scope is limited to this one helper. Public APIs, the
`EnhancingX509ExtendedTrustManager` wrapping path, and the
`ResumptionController` wrapping path are all unchanged.

| Change point | Test |
|---|---|
| `wrapTrustManagerIfNeeded` null-array guard |

`JdkSslServerContextTest.testTrustManagerFactoryReturningNullDoesNotThrowNpe`
— installs a custom `TrustManagerFactorySpi` whose
`engineGetTrustManagers()` returns `null`, builds the server
`SslContext` via `SslContextBuilder.forServer(cert,
key).sslProvider(JDK).trustManager(tmf)`, and asserts it is returned
without throwing. Verified to reproduce the
`SSLException`/`NullPointerException` on the pre-fix code (`Cannot read
the array length because "trustManagers" is null`) and to pass after the
fix. |

All 36 tests in `JdkSslServerContextTest` pass locally (0 failures, 0
errors, 1 pre-existing skip).

- `TrustManagerFactory` implementations that return `null` from
`getTrustManagers()` (i.e., "use the JDK defaults") now work with the
JDK-backed `SslContextBuilder.forServer()`.
- Purely defensive change inside a private helper. The fix pattern
parallels PR #16690 for `JdkSslClientContext.wrapIfNeeded`.

Paired with #16690 (same bug pattern on the client side).

Co-authored-by: Guimu <[email protected]>
@netty-project-bot netty-project-bot mentioned this pull request Apr 24, 2026
Auto-port 5.0: Avoid NPE in JdkSslServerContext when TrustManagerFactory returns nul… #16704
Merged
normanmaurer added a commit that referenced this pull request Apr 25, 2026
@netty-project-bot @normanmaurer @daguimu
Auto-port 5.0: Avoid NPE in JdkSslServerContext when TrustManagerFact…
1dcd544
…ory returns nul… (#16704)

Auto-port of #16700 to 5.0
Cherry-picked commit: 0fc37d3

---
…l (#16691)

`JdkSslServerContext.newSSLContext` passes
`trustManagerFactory.getTrustManagers()` through
`wrapTrustManagerIfNeeded` before calling `SSLContext.init`. If a caller
supplies a `TrustManagerFactory` whose `getTrustManagers()` legitimately
returns `null` (a valid return value that asks `SSLContext.init` to fall
back to the JDK default trust store), building the server context fails
with:

```
javax.net.ssl.SSLException: failed to initialize the server-side SSL context
Caused by: java.lang.NullPointerException: Cannot read the array length because "trustManagers" is null
    at io.netty.handler.ssl.JdkSslServerContext.wrapTrustManagerIfNeeded(JdkSslServerContext.java:364)
    at io.netty.handler.ssl.JdkSslServerContext.newSSLContext(JdkSslServerContext.java:341)
```

This mirrors the client-side bug reported in #14488 / fixed in PR
`JdkSslServerContext` has routed trust managers through
`wrapTrustManagerIfNeeded` since the `EnhancingX509ExtendedTrustManager`
wrapping was introduced, long before the `ResumptionController` wiring
was added — so the NPE has been latent on the server path for multiple
releases when a user supplies such a factory.

```java
private static TrustManager[] wrapTrustManagerIfNeeded(
        TrustManager[] trustManagers, ResumptionController resumptionController) {
    if (WRAP_TRUST_MANAGER && PlatformDependent.javaVersion() >= 7) {
        for (int i = 0; i < trustManagers.length; i++) {     // NPE when trustManagers is null
            ...
        }
    }
    return trustManagers;
}
```

`WRAP_TRUST_MANAGER` is `true` on Java 7+ (guarded only by a FIPS
feature-check that passes on common JVMs), and `trustManagerFactory`
reaches this helper either as the user-supplied factory or — when the
user passes `null` — as a default-initialized `TrustManagerFactory`. The
user-supplied path is what fails: if the factory's `getTrustManagers()`
returns `null`, `trustManagers.length` dereferences `null`.

Short-circuit `wrapTrustManagerIfNeeded` when `trustManagers` is `null`,
returning `null` so it flows into `SSLContext.init` exactly as it did
before any wrapping was applied. `SSLContext.init(km, null, sr)` is
explicitly documented to fall back to the default trust manager.

```java
if (trustManagers == null) {
    return null;
}
```

Scope is limited to this one helper. Public APIs, the
`EnhancingX509ExtendedTrustManager` wrapping path, and the
`ResumptionController` wrapping path are all unchanged.

| Change point | Test |
|---|---|
| `wrapTrustManagerIfNeeded` null-array guard |

`JdkSslServerContextTest.testTrustManagerFactoryReturningNullDoesNotThrowNpe`
— installs a custom `TrustManagerFactorySpi` whose
`engineGetTrustManagers()` returns `null`, builds the server
`SslContext` via `SslContextBuilder.forServer(cert,
key).sslProvider(JDK).trustManager(tmf)`, and asserts it is returned
without throwing. Verified to reproduce the
`SSLException`/`NullPointerException` on the pre-fix code (`Cannot read
the array length because "trustManagers" is null`) and to pass after the
fix. |

All 36 tests in `JdkSslServerContextTest` pass locally (0 failures, 0
errors, 1 pre-existing skip).

- `TrustManagerFactory` implementations that return `null` from
`getTrustManagers()` (i.e., "use the JDK defaults") now work with the
JDK-backed `SslContextBuilder.forServer()`.
- Purely defensive change inside a private helper. The fix pattern
parallels PR #16690 for `JdkSslClientContext.wrapIfNeeded`.

Paired with #16690 (same bug pattern on the client side).

Co-authored-by: Norman Maurer <[email protected]>
Co-authored-by: Guimu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.1.133.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@daguimu @netty-project-bot @normanmaurer
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.