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 — Choose server key types from client's signature algorithms for TLS 1.… · netty/netty@84c167e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • OpenSslKeyMaterialManager.java
      • ReferenceCountedOpenSslEngine.java
    • test/java/io/netty/handler/ssl
      • PkiTestingTlsTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/OpenSslKeyMaterialManager.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OpenSslKeyMaterialManager.java
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.netty.handler.ssl;
1717

18+
import javax.net.ssl.ExtendedSSLSession;
1819
import javax.net.ssl.SSLException;
1920
import javax.net.ssl.SSLHandshakeException;
2021
import javax.net.ssl.X509ExtendedKeyManager;
@@ -42,12 +43,14 @@ final class OpenSslKeyMaterialManager {
4243
static final String KEY_TYPE_EC = "EC";
4344
static final String KEY_TYPE_EC_EC = "EC_EC";
4445
static final String KEY_TYPE_EC_RSA = "EC_RSA";
46+
static final String KEY_TYPE_EDDSA = "EdDSA";
4547

4648
private static final int TYPE_RSA = 1; // 00001
4749
private static final int TYPE_DH_RSA = 1 << 1; // 00010
4850
private static final int TYPE_EC = 1 << 2; // 00100
4951
private static final int TYPE_EC_EC = 1 << 3; // 01000
5052
private static final int TYPE_EC_RSA = 1 << 4; // 10000
53+
private static final int TYPE_EDDSA = 1 << 5; // 100000
5154

5255
private final OpenSslKeyMaterialProvider provider;
5356
private final boolean hasTmpDhKeys;
@@ -58,6 +61,29 @@ final class OpenSslKeyMaterialManager {
5861
}
5962

6063
void setKeyMaterialServerSide(ReferenceCountedOpenSslEngine engine) throws SSLException {
64+
// TLS 1.3 cipher suites don't carry an authentication method, so choose the key type from the peer's
65+
// signature algorithms instead. This is consistent with what OpenJDK does:
66+
// https://github.com/openjdk/jdk/blob/jdk-26-ga/
67+
// src/java.base/share/classes/sun/security/ssl/CertificateMessage.java#L995-L1029
68+
if (engine.isTLSv13() && engine.getHandshakeSession() instanceof ExtendedSSLSession) {
69+
ExtendedSSLSession session = (ExtendedSSLSession) engine.getHandshakeSession();
70+
int seenTypes = 0;
71+
for (String algorithm : session.getPeerSupportedSignatureAlgorithms()) {
72+
int typeBit = resolveSignatureAlgorithmKeyTypeBit(algorithm);
73+
if (typeBit == 0 || (seenTypes & typeBit) != 0) {
74+
continue;
75+
}
76+
77+
seenTypes |= typeBit; // mark as seen
78+
79+
String alias = chooseServerAlias(engine, keyTypeString(typeBit));
80+
if (alias != null) {
81+
setKeyMaterial(engine, alias);
82+
return;
83+
}
84+
}
85+
}
86+
6187
String[] authMethods = engine.authMethods();
6288
if (authMethods.length == 0) {
6389
throw new SSLHandshakeException("Unable to find key material");
@@ -92,6 +118,19 @@ void setKeyMaterialServerSide(ReferenceCountedOpenSslEngine engine) throws SSLEx
92118
+ Arrays.toString(authMethods));
93119
}
94120

121+
private static int resolveSignatureAlgorithmKeyTypeBit(String algorithm) {
122+
if ("Ed25519".equals(algorithm) || "Ed448".equals(algorithm)) {
123+
return TYPE_EDDSA;
124+
}
125+
if (algorithm.endsWith("withECDSA")) {
126+
return TYPE_EC;
127+
}
128+
if (algorithm.endsWith("withRSA")) {
129+
return TYPE_RSA;
130+
}
131+
return 0;
132+
}
133+
95134
private static int resolveKeyTypeBit(String authMethod) {
96135
switch (authMethod) {
97136
case "RSA":
@@ -118,6 +157,7 @@ private static String keyTypeString(int typeBit) {
118157
case TYPE_EC: return KEY_TYPE_EC;
119158
case TYPE_EC_EC: return KEY_TYPE_EC_EC;
120159
case TYPE_EC_RSA: return KEY_TYPE_EC_RSA;
160+
case TYPE_EDDSA: return KEY_TYPE_EDDSA;
121161
default: return null;
122162
}
123163
}
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ public class ReferenceCountedOpenSslEngine extends SSLEngine implements Referenc
130130
private static final SSLEngineResult NEED_WRAP_CLOSED = new SSLEngineResult(CLOSED, NEED_WRAP, 0, 0);
131131
private static final SSLEngineResult CLOSED_NOT_HANDSHAKING = new SSLEngineResult(CLOSED, NOT_HANDSHAKING, 0, 0);
132132

133+
// See https://www.rfc-editor.org/rfc/rfc8446#section-4.2.1
134+
private static final int TLS1_3_VERSION = 0x0304;
135+
133136
// OpenSSL state
134137
private long ssl;
135138
private long networkBIO;
@@ -446,6 +449,10 @@ final synchronized String[] authMethods() {
446449
return SSL.authenticationMethods(ssl);
447450
}
448451

452+
final synchronized boolean isTLSv13() {
453+
return !destroyed && SSL.getVersionInt(ssl) == TLS1_3_VERSION;
454+
}
455+
449456
final void setKeyMaterial(OpenSslKeyMaterial keyMaterial) throws Exception {
450457
synchronized (this) {
451458
if (destroyed) {
Collapse file

‎handler/src/test/java/io/netty/handler/ssl/PkiTestingTlsTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/PkiTestingTlsTest.java
+155-20Lines changed: 155 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.netty.pkitesting.X509Bundle;
3131
import io.netty.util.ReferenceCountUtil;
3232
import io.netty.util.concurrent.Promise;
33+
import io.netty.util.internal.EmptyArrays;
3334
import org.junit.jupiter.api.Test;
3435
import org.junit.jupiter.api.condition.EnabledForJreRange;
3536
import org.junit.jupiter.api.condition.EnabledIf;
@@ -40,6 +41,7 @@
4041
import org.junit.jupiter.params.provider.ValueSource;
4142

4243
import java.net.Socket;
44+
import java.security.KeyStore;
4345
import java.security.Principal;
4446
import java.security.PrivateKey;
4547
import java.security.cert.X509Certificate;
@@ -49,27 +51,22 @@
4951
import java.util.concurrent.atomic.AtomicReference;
5052
import java.util.stream.Stream;
5153
import javax.net.ssl.ExtendedSSLSession;
54+
import javax.net.ssl.KeyManagerFactory;
5255
import javax.net.ssl.SNIHostName;
5356
import javax.net.ssl.SSLEngine;
57+
import javax.net.ssl.SSLException;
5458
import javax.net.ssl.SSLParameters;
5559
import javax.net.ssl.SSLSession;
5660
import javax.net.ssl.X509ExtendedKeyManager;
5761

5862
import static org.assertj.core.api.Assertions.assertThat;
63+
import static org.junit.jupiter.api.Assertions.assertThrows;
5964

6065
public class PkiTestingTlsTest {
6166

6267
static List<Arguments> classicalAlgorithms() {
63-
List<SslProvider> providers = new ArrayList<>();
64-
if (SslProvider.isTlsv13Supported(SslProvider.JDK)) {
65-
providers.add(SslProvider.JDK);
66-
}
67-
if (OpenSsl.isAvailable() && OpenSsl.supportsKeyManagerFactory() && OpenSsl.isTlsv13Supported()) {
68-
providers.add(SslProvider.OPENSSL);
69-
}
70-
7168
List<Arguments> args = new ArrayList<>();
72-
for (SslProvider provider : providers) {
69+
for (SslProvider provider : tlsv13Providers()) {
7370
List<CertificateBuilder.Algorithm> algs = new ArrayList<>();
7471
algs.add(CertificateBuilder.Algorithm.rsa2048);
7572
algs.add(CertificateBuilder.Algorithm.ecp256);
@@ -81,6 +78,17 @@ static List<Arguments> classicalAlgorithms() {
8178
return args;
8279
}
8380

81+
static List<SslProvider> tlsv13Providers() {
82+
List<SslProvider> providers = new ArrayList<>();
83+
if (SslProvider.isTlsv13Supported(SslProvider.JDK)) {
84+
providers.add(SslProvider.JDK);
85+
}
86+
if (OpenSsl.isAvailable() && OpenSsl.supportsKeyManagerFactory() && OpenSsl.isTlsv13Supported()) {
87+
providers.add(SslProvider.OPENSSL);
88+
}
89+
return providers;
90+
}
91+
8492
static Stream<Arguments> interoperabilityParams() {
8593
Stream.Builder<Arguments> builder = Stream.builder();
8694
for (boolean enableOnClient : new boolean[] {true, false}) {
@@ -118,32 +126,137 @@ public void connectWithClassicalAlgorithms(SslProvider provider, CertificateBuil
118126
testTlsConnection(serverContext, clientContext, null);
119127
}
120128

129+
static List<SslProvider> ed25519Providers() {
130+
List<SslProvider> providers = tlsv13Providers();
131+
if (!OpenSsl.isBoringSSL()) {
132+
// The OPENSSL provider doesn't include `ed25519` in its peer signature algorithms
133+
// https://github.com/netty/netty-tcnative/pull/1019
134+
providers.remove(SslProvider.OPENSSL);
135+
}
136+
return providers;
137+
}
138+
139+
static List<Arguments> ed25519Params() {
140+
List<Arguments> args = new ArrayList<>();
141+
for (SslProvider provider : ed25519Providers()) {
142+
for (boolean useKeyManagerFactory : new boolean[] {true, false}) {
143+
args.add(Arguments.of(provider, useKeyManagerFactory));
144+
}
145+
}
146+
return args;
147+
}
148+
121149
@EnabledForJreRange(min = JRE.JAVA_15)
122-
@Test
123-
public void connectWithEd25519()
150+
@ParameterizedTest
151+
@MethodSource("ed25519Params")
152+
public void connectWithEd25519(SslProvider provider, boolean useKeyManagerFactory)
124153
throws Exception {
125154
X509Bundle cert = new CertificateBuilder()
126155
.algorithm(CertificateBuilder.Algorithm.ed25519)
127156
.setIsCertificateAuthority(true)
128157
.subject("CN=localhost")
129158
.buildSelfSigned();
130159

131-
// We currently don't support Ed25519 or Ed448 with the OPENSSL provider,
132-
// so to use those algorithms we have to use the JDK provider.
133-
SslProvider provider = SslProvider.JDK;
160+
SslContextBuilder serverBuilder = useKeyManagerFactory ?
161+
SslContextBuilder.forServer(cert.toKeyManagerFactory()) :
162+
SslContextBuilder.forServer(cert.getKeyPair().getPrivate(), cert.getCertificate());
163+
final SslContext serverContext = serverBuilder
164+
.sslProvider(provider)
165+
.build();
166+
167+
final SslContext clientContext = SslContextBuilder.forClient()
168+
.trustManager(cert.toTrustManagerFactory())
169+
.sslProvider(SslProvider.JDK)
170+
.serverName(new SNIHostName("localhost"))
171+
.protocols("TLSv1.3")
172+
.build();
173+
174+
SSLSession session = testTlsConnection(serverContext, clientContext, null, null);
175+
assertThat(session.getPeerCertificates()[0]).isEqualTo(cert.getCertificate());
176+
}
177+
178+
static List<Arguments> ed25519AndRsaParams() {
179+
List<Arguments> args = new ArrayList<>();
180+
for (SslProvider provider : ed25519Providers()) {
181+
// The JDK client's default signature schemes list ECDSA first, then Ed25519, then RSA.
182+
args.add(Arguments.of(provider, null, "Ed25519"));
183+
args.add(Arguments.of(provider, new String[] {"rsa_pss_rsae_sha256"}, "RSA"));
184+
args.add(Arguments.of(provider, new String[] {"rsa_pss_rsae_sha256", "ed25519"}, "RSA"));
185+
args.add(Arguments.of(provider, new String[] {"ed25519", "rsa_pss_rsae_sha256"}, "Ed25519"));
186+
}
187+
return args;
188+
}
189+
190+
/**
191+
* Requires Java 19 for {@code SSLParameters#setSignatureSchemes(String[])}.
192+
*/
193+
@EnabledForJreRange(min = JRE.JAVA_19)
194+
@ParameterizedTest
195+
@MethodSource("ed25519AndRsaParams")
196+
public void connectWithEd25519AndRsa(SslProvider provider, String[] clientSignatureSchemes,
197+
String expectedAlgorithm) throws Exception {
198+
X509Bundle ed25519 = new CertificateBuilder()
199+
.algorithm(CertificateBuilder.Algorithm.ed25519)
200+
.setIsCertificateAuthority(true)
201+
.subject("CN=localhost")
202+
.buildSelfSigned();
203+
X509Bundle rsa = new CertificateBuilder()
204+
.algorithm(CertificateBuilder.Algorithm.rsa2048)
205+
.setIsCertificateAuthority(true)
206+
.subject("CN=localhost")
207+
.buildSelfSigned();
208+
209+
KeyStore keyStore = KeyStore.getInstance("PKCS12");
210+
keyStore.load(null, null);
211+
keyStore.setKeyEntry("ed25519", ed25519.getKeyPair().getPrivate(), EmptyArrays.EMPTY_CHARS,
212+
ed25519.getCertificatePath());
213+
keyStore.setKeyEntry("rsa", rsa.getKeyPair().getPrivate(), EmptyArrays.EMPTY_CHARS,
214+
rsa.getCertificatePath());
215+
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
216+
kmf.init(keyStore, EmptyArrays.EMPTY_CHARS);
217+
218+
final SslContext serverContext = SslContextBuilder.forServer(kmf)
219+
.sslProvider(provider)
220+
.build();
221+
222+
final SslContext clientContext = SslContextBuilder.forClient()
223+
.trustManager(ed25519.getCertificate(), rsa.getCertificate())
224+
.sslProvider(SslProvider.JDK)
225+
.serverName(new SNIHostName("localhost"))
226+
.protocols("TLSv1.3")
227+
.build();
228+
229+
SSLSession session = testTlsConnection(serverContext, clientContext, null, clientSignatureSchemes);
230+
X509Bundle expected = "RSA".equals(expectedAlgorithm) ? rsa : ed25519;
231+
assertThat(session.getPeerCertificates()[0]).isEqualTo(expected.getCertificate());
232+
}
233+
234+
/**
235+
* Requires Java 19 for {@code SSLParameters#setSignatureSchemes(String[])}.
236+
*/
237+
@EnabledForJreRange(min = JRE.JAVA_19)
238+
@ParameterizedTest
239+
@MethodSource("tlsv13Providers")
240+
public void connectWithEd25519FailsWithoutCommonSignatureAlgorithm(SslProvider provider) throws Exception {
241+
X509Bundle cert = new CertificateBuilder()
242+
.algorithm(CertificateBuilder.Algorithm.ed25519)
243+
.setIsCertificateAuthority(true)
244+
.subject("CN=localhost")
245+
.buildSelfSigned();
134246

135247
final SslContext serverContext = SslContextBuilder.forServer(cert.toKeyManagerFactory())
136248
.sslProvider(provider)
137249
.build();
138250

139251
final SslContext clientContext = SslContextBuilder.forClient()
140252
.trustManager(cert.toTrustManagerFactory())
141-
.sslProvider(provider)
253+
.sslProvider(SslProvider.JDK)
142254
.serverName(new SNIHostName("localhost"))
143255
.protocols("TLSv1.3")
144256
.build();
145257

146-
testTlsConnection(serverContext, clientContext, null);
258+
assertThrows(SSLException.class, () -> testTlsConnection(
259+
serverContext, clientContext, null, new String[] {"ecdsa_secp256r1_sha256"}));
147260
}
148261

149262
/**
@@ -321,7 +434,16 @@ void x25519MLKEM768Interoperability(boolean enabledOnClient, String[] protocols)
321434
}
322435

323436
private void testTlsConnection(SslContext serverContext, SslContext clientContext, String[] groups)
324-
throws InterruptedException {
437+
throws Exception {
438+
testTlsConnection(serverContext, clientContext, groups, null);
439+
}
440+
441+
/**
442+
* @param clientSignatureSchemes sets the preferred client signature schemes. Requires JRE 19+
443+
* @return the client's session.
444+
*/
445+
private SSLSession testTlsConnection(SslContext serverContext, SslContext clientContext, String[] groups,
446+
String[] clientSignatureSchemes) throws Exception {
325447
MultiThreadIoEventLoopGroup group = new MultiThreadIoEventLoopGroup(1, LocalIoHandler.newFactory());
326448
LocalAddress serverAddress = new LocalAddress(getClass());
327449

@@ -345,7 +467,7 @@ protected void initChannel(Channel ch) throws Exception {
345467
.group(group)
346468
.bind(serverAddress).sync().channel();
347469

348-
Promise<SslHandshakeCompletionEvent> promise = group.next().newPromise();
470+
Promise<SSLSession> promise = group.next().newPromise();
349471

350472
clientChannel = new Bootstrap()
351473
.channel(LocalChannel.class)
@@ -359,6 +481,11 @@ protected void initChannel(Channel ch) throws Exception {
359481
OpenSslParametersUtil.setNamesGroups(parameters, groups);
360482
handler.engine().setSSLParameters(parameters);
361483
}
484+
if (clientSignatureSchemes != null) {
485+
SSLParameters parameters = handler.engine().getSSLParameters();
486+
setSignatureSchemes(parameters, clientSignatureSchemes);
487+
handler.engine().setSSLParameters(parameters);
488+
}
362489
ch.pipeline()
363490
.addLast(handler)
364491
.addLast(new ChannelInboundHandlerAdapter() {
@@ -374,7 +501,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
374501
.getSession()).getNamedGroup();
375502
assertThat(OpenSsl.NAMED_GROUPS).contains(namedGroup);
376503
}
377-
promise.setSuccess(shce);
504+
promise.setSuccess(session);
378505
} else {
379506
promise.setFailure(shce.cause());
380507
}
@@ -396,7 +523,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
396523
.sync()
397524
.channel();
398525

399-
promise.sync();
526+
return promise.sync().getNow();
400527
} finally {
401528
if (clientChannel != null) {
402529
clientChannel.close();
@@ -417,4 +544,12 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
417544
ReferenceCountUtil.release(serverContext);
418545
}
419546
}
547+
548+
/**
549+
* Uses reflection to invoke the Java 19+ {@code SSLParameters#setSignatureSchemes(String[])}
550+
*/
551+
private static void setSignatureSchemes(SSLParameters parameters, String[] signatureSchemes) throws Exception {
552+
SSLParameters.class.getMethod("setSignatureSchemes", String[].class)
553+
.invoke(parameters, (Object) signatureSchemes);
554+
}
420555
}

0 commit comments

Comments
0 (0)
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.