Skip to content

Commit 091a858

Browse files
miguelaranda0juergw
authored andcommitted
Project import generated by Copybara.
PiperOrigin-RevId: 907405902
1 parent eb7c3b1 commit 091a858

7 files changed

Lines changed: 146 additions & 17 deletions

File tree

android/src/main/java/org/conscrypt/Platform.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ private static void setSSLParametersOnImpl(SSLParameters params, SSLParametersIm
263263
}
264264
}
265265

266+
public static void setSSLParameters(SSLParameters params, SSLParametersImpl impl) {
267+
try {
268+
setSSLParametersOnImpl(params, impl);
269+
} catch (NoSuchMethodException | IllegalAccessException ignored) {
270+
// Ignored
271+
} catch (InvocationTargetException e) {
272+
throw new RuntimeException(e.getCause());
273+
}
274+
}
275+
266276
public static void setSSLParameters(SSLParameters params, SSLParametersImpl impl,
267277
AbstractConscryptSocket socket) {
268278
try {
@@ -274,9 +284,7 @@ public static void setSSLParameters(SSLParameters params, SSLParametersImpl impl
274284
socket.setHostname(sniHostname);
275285
}
276286
}
277-
} catch (NoSuchMethodException ignored) {
278-
// Ignored
279-
} catch (IllegalAccessException ignored) {
287+
} catch (NoSuchMethodException | IllegalAccessException ignored) {
280288
// Ignored
281289
} catch (InvocationTargetException e) {
282290
throw new RuntimeException(e.getCause());
@@ -294,9 +302,7 @@ public static void setSSLParameters(SSLParameters params, SSLParametersImpl impl
294302
engine.setHostname(sniHostname);
295303
}
296304
}
297-
} catch (NoSuchMethodException ignored) {
298-
// Ignored
299-
} catch (IllegalAccessException ignored) {
305+
} catch (NoSuchMethodException | IllegalAccessException ignored) {
300306
// Ignored
301307
} catch (InvocationTargetException e) {
302308
throw new RuntimeException(e.getCause());
@@ -340,6 +346,16 @@ private static void getSSLParametersFromImpl(SSLParameters params, SSLParameters
340346
}
341347
}
342348

349+
public static void getSSLParameters(SSLParameters params, SSLParametersImpl impl) {
350+
try {
351+
getSSLParametersFromImpl(params, impl);
352+
} catch (NoSuchMethodException | IllegalAccessException ignored) {
353+
// Ignored
354+
} catch (InvocationTargetException e) {
355+
throw new RuntimeException(e.getCause());
356+
}
357+
}
358+
343359
public static void getSSLParameters(SSLParameters params, SSLParametersImpl impl,
344360
AbstractConscryptSocket socket) {
345361
try {
@@ -348,9 +364,7 @@ public static void getSSLParameters(SSLParameters params, SSLParametersImpl impl
348364
if (Build.VERSION.SDK_INT >= 24) {
349365
setParametersSniHostname(params, impl, socket);
350366
}
351-
} catch (NoSuchMethodException ignored) {
352-
// Ignored
353-
} catch (IllegalAccessException ignored) {
367+
} catch (NoSuchMethodException | IllegalAccessException ignored) {
354368
// Ignored
355369
} catch (InvocationTargetException e) {
356370
throw new RuntimeException(e.getCause());
@@ -377,9 +391,7 @@ public static void getSSLParameters(SSLParameters params, SSLParametersImpl impl
377391
if (Build.VERSION.SDK_INT >= 24) {
378392
setParametersSniHostname(params, impl, engine);
379393
}
380-
} catch (NoSuchMethodException ignored) {
381-
// Ignored
382-
} catch (IllegalAccessException ignored) {
394+
} catch (NoSuchMethodException | IllegalAccessException ignored) {
383395
// Ignored
384396
} catch (InvocationTargetException e) {
385397
throw new RuntimeException(e.getCause());

common/src/main/java/org/conscrypt/ConscryptServerSocket.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.InetAddress;
2121
import java.net.Socket;
2222

23+
import javax.net.ssl.SSLParameters;
2324
import javax.net.ssl.SSLServerSocket;
2425

2526
/**
@@ -79,6 +80,19 @@ public String[] getSupportedProtocols() {
7980
return NativeCrypto.getSupportedProtocols();
8081
}
8182

83+
@Override
84+
public SSLParameters getSSLParameters() {
85+
SSLParameters params = super.getSSLParameters();
86+
Platform.getSSLParameters(params, sslParameters);
87+
return params;
88+
}
89+
90+
@Override
91+
public void setSSLParameters(SSLParameters params) {
92+
super.setSSLParameters(params);
93+
Platform.setSSLParameters(params, sslParameters);
94+
}
95+
8296
/**
8397
* The names of the protocols' versions that in use on this SSL connection.
8498
*

common/src/main/java/org/conscrypt/SSLParametersImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ private SSLParametersImpl(ClientSessionContext clientSessionContext,
240240
this.useSessionTickets = sslParams.useSessionTickets;
241241
this.useSni = sslParams.useSni;
242242
this.channelIdEnabled = sslParams.channelIdEnabled;
243+
this.namedGroups = (sslParams.namedGroups == null) ? null : sslParams.namedGroups.clone();
243244
}
244245

245246
/**

common/src/test/java/org/conscrypt/javax/net/ssl/SSLSocketTest.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,11 @@
7373
import javax.crypto.SecretKey;
7474
import javax.crypto.spec.SecretKeySpec;
7575
import javax.net.ssl.KeyManager;
76-
import javax.net.ssl.KeyManagerFactory;
77-
import javax.net.ssl.ManagerFactoryParameters;
7876
import javax.net.ssl.SSLContext;
7977
import javax.net.ssl.SSLEngine;
8078
import javax.net.ssl.SSLHandshakeException;
8179
import javax.net.ssl.SSLParameters;
8280
import javax.net.ssl.SSLProtocolException;
83-
import javax.net.ssl.SSLServerSocket;
8481
import javax.net.ssl.SSLSession;
8582
import javax.net.ssl.SSLSocket;
8683
import javax.net.ssl.SSLSocketFactory;
@@ -1118,6 +1115,65 @@ public void handshake_setsNamedGroups_usesFirstServerNamedGroupThatClientSupport
11181115
context.close();
11191116
}
11201117

1118+
@Test
1119+
public void handshake_setsNamedGroupsBeforeAccept_usesFirstServerNamedGroupThatClientSupports()
1120+
throws Exception {
1121+
TestSSLContext context = TestSSLContext.create();
1122+
final SSLSocket client = (SSLSocket) context.clientContext.getSocketFactory().createSocket(
1123+
context.host, context.port);
1124+
1125+
{
1126+
SSLParameters parameters = context.serverSocket.getSSLParameters();
1127+
setNamedGroups(parameters, new String[] {"P-384", "X25519"});
1128+
context.serverSocket.setSSLParameters(parameters);
1129+
1130+
if (sslParametersSupportsNamedGroups()) {
1131+
assertArrayEquals(new String[] {"P-384", "X25519"},
1132+
getNamedGroupsOrNull(context.serverSocket.getSSLParameters()));
1133+
} else {
1134+
assertArrayEquals(null,
1135+
getNamedGroupsOrNull(context.serverSocket.getSSLParameters()));
1136+
}
1137+
}
1138+
{
1139+
SSLParameters parameters = client.getSSLParameters();
1140+
setNamedGroups(parameters, new String[] {"P-521", "X25519", "P-384"});
1141+
client.setSSLParameters(parameters);
1142+
1143+
if (sslParametersSupportsNamedGroups()) {
1144+
assertArrayEquals(new String[] {"P-521", "X25519", "P-384"},
1145+
getNamedGroupsOrNull(client.getSSLParameters()));
1146+
} else {
1147+
assertArrayEquals(null, getNamedGroupsOrNull(client.getSSLParameters()));
1148+
}
1149+
}
1150+
1151+
final SSLSocket server = (SSLSocket) context.serverSocket.accept();
1152+
1153+
Future<Void> s = runAsync(() -> {
1154+
server.startHandshake();
1155+
return null;
1156+
});
1157+
Future<Void> c = runAsync(() -> {
1158+
client.startHandshake();
1159+
return null;
1160+
});
1161+
s.get();
1162+
c.get();
1163+
if (sslParametersSupportsNamedGroups()) {
1164+
// P-384 is the first named group in the server's list that both support.
1165+
assertEquals("P-384", getCurveName(client));
1166+
assertEquals("P-384", getCurveName(server));
1167+
} else {
1168+
// The defaults are used, and X25519 gets priority.
1169+
assertEquals("X25519", getCurveName(client));
1170+
assertEquals("X25519", getCurveName(server));
1171+
}
1172+
client.close();
1173+
server.close();
1174+
context.close();
1175+
}
1176+
11211177
@Test
11221178
public void handshake_withX25519MLKEM768_works() throws Exception {
11231179
TestSSLContext context = TestSSLContext.create();

openjdk/src/main/java/org/conscrypt/Java8PlatformUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ private static String getSniHostName(SSLParameters params) {
8383
return null;
8484
}
8585

86-
private static void setSSLParameters(SSLParameters params, SSLParametersImpl impl) {
86+
static void setSSLParameters(SSLParameters params, SSLParametersImpl impl) {
8787
impl.setEndpointIdentificationAlgorithm(params.getEndpointIdentificationAlgorithm());
8888
impl.setUseCipherSuitesOrder(params.getUseCipherSuitesOrder());
8989
impl.setSNIMatchers(params.getSNIMatchers());
9090
impl.setAlgorithmConstraints(params.getAlgorithmConstraints());
9191
}
9292

93-
private static void getSSLParameters(SSLParameters params, SSLParametersImpl impl) {
93+
static void getSSLParameters(SSLParameters params, SSLParametersImpl impl) {
9494
params.setEndpointIdentificationAlgorithm(impl.getEndpointIdentificationAlgorithm());
9595
params.setUseCipherSuitesOrder(impl.getUseCipherSuitesOrder());
9696
params.setSNIMatchers(impl.getSNIMatchers());

openjdk/src/main/java/org/conscrypt/Java9PlatformUtil.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ final class Java9PlatformUtil {
4545
SSL_PARAMETERS_SET_APPLICATION_PROTOCOLS_METHOD = setApplicationProtocolsMethod;
4646
}
4747

48+
static void setSSLParameters(SSLParameters src, SSLParametersImpl dest) {
49+
Java8PlatformUtil.setSSLParameters(src, dest);
50+
try {
51+
Method getNamedGroupsMethod = src.getClass().getMethod("getNamedGroups");
52+
dest.setNamedGroups((String[]) getNamedGroupsMethod.invoke(src));
53+
} catch (ReflectiveOperationException | SecurityException e) {
54+
// Method is not available. Ignore.
55+
}
56+
dest.setApplicationProtocols(getApplicationProtocols(src));
57+
}
58+
4859
static void setSSLParameters(SSLParameters src, SSLParametersImpl dest,
4960
AbstractConscryptSocket socket) {
5061
Java8PlatformUtil.setSSLParameters(src, dest, socket);
@@ -85,6 +96,21 @@ static void setSSLParameters(SSLParameters src, SSLParametersImpl dest,
8596
dest.setApplicationProtocols(getApplicationProtocols(src));
8697
}
8798

99+
static void getSSLParameters(SSLParameters dest, SSLParametersImpl src) {
100+
Java8PlatformUtil.getSSLParameters(dest, src);
101+
102+
try {
103+
String[] namedGroups = src.getNamedGroups();
104+
Method setNamedGroupsMethod =
105+
dest.getClass().getMethod("setNamedGroups", String[].class);
106+
setNamedGroupsMethod.invoke(dest, (Object) namedGroups);
107+
} catch (ReflectiveOperationException | SecurityException e) {
108+
// Method is not available. Ignore.
109+
}
110+
111+
setApplicationProtocols(dest, src.getApplicationProtocols());
112+
}
113+
88114
static void getSSLParameters(SSLParameters dest, SSLParametersImpl src,
89115
ConscryptEngine engine) {
90116
Java8PlatformUtil.getSSLParameters(dest, src, engine);

openjdk/src/main/java/org/conscrypt/Platform.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ static void setSocketWriteTimeout(@SuppressWarnings("unused") Socket s,
256256
// TODO: figure this out on the RI
257257
}
258258

259+
static void setSSLParameters(SSLParameters params, SSLParametersImpl impl) {
260+
if (JAVA_VERSION >= 9) {
261+
Java9PlatformUtil.setSSLParameters(params, impl);
262+
} else if (JAVA_VERSION >= 8) {
263+
Java8PlatformUtil.setSSLParameters(params, impl);
264+
} else {
265+
impl.setEndpointIdentificationAlgorithm(params.getEndpointIdentificationAlgorithm());
266+
}
267+
}
268+
259269
static void setSSLParameters(SSLParameters params, SSLParametersImpl impl,
260270
AbstractConscryptSocket socket) {
261271
if (JAVA_VERSION >= 9) {
@@ -267,6 +277,16 @@ static void setSSLParameters(SSLParameters params, SSLParametersImpl impl,
267277
}
268278
}
269279

280+
static void getSSLParameters(SSLParameters params, SSLParametersImpl impl) {
281+
if (JAVA_VERSION >= 9) {
282+
Java9PlatformUtil.getSSLParameters(params, impl);
283+
} else if (JAVA_VERSION >= 8) {
284+
Java8PlatformUtil.getSSLParameters(params, impl);
285+
} else {
286+
params.setEndpointIdentificationAlgorithm(impl.getEndpointIdentificationAlgorithm());
287+
}
288+
}
289+
270290
static void getSSLParameters(SSLParameters params, SSLParametersImpl impl,
271291
AbstractConscryptSocket socket) {
272292
if (JAVA_VERSION >= 9) {

0 commit comments

Comments
 (0)