Skip to content

Commit f5a96c9

Browse files
Upstream changes from elsewhere into github. (#1404)
Includes the following changes: - Stop requiring testing method for HPKE duck typing - Stop throwing an extra null pointer exception in native_crypto - Simplify Conscrypt instance checks in `getApplicationProtocol`.
1 parent c9f1617 commit f5a96c9

File tree

4 files changed

+54
-106
lines changed

4 files changed

+54
-106
lines changed

common/src/jni/main/cpp/conscrypt/native_crypto.cc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11845,8 +11845,6 @@ static jboolean NativeCrypto_SSL_set1_ech_config_list(JNIEnv* env, jclass, jlong
1184511845
SSL* ssl = to_SSL(env, ssl_address, true);
1184611846
JNI_TRACE("ssl=%p NativeCrypto_SSL_set1_ech_config_list(%p)", ssl, configJavaBytes);
1184711847
if (ssl == nullptr) {
11848-
conscrypt::jniutil::throwNullPointerException(env, "Null pointer, ssl address");
11849-
ERR_clear_error();
1185011848
return JNI_FALSE;
1185111849
}
1185211850
ScopedByteArrayRO configBytes(env, configJavaBytes);
@@ -11951,23 +11949,20 @@ static jboolean NativeCrypto_SSL_ech_accepted(JNIEnv* env, jclass, jlong ssl_add
1195111949
JNI_TRACE("NativeCrypto_SSL_ech_accepted");
1195211950
CHECK_ERROR_QUEUE_ON_RETURN;
1195311951
SSL* ssl = to_SSL(env, ssl_address, true);
11954-
JNI_TRACE("ssl=%p NativeCrypto_SSL_ech_accepted", ssl);
1195511952
if (ssl == nullptr) {
11956-
conscrypt::jniutil::throwNullPointerException(env, "Null pointer, ssl address");
11957-
ERR_clear_error();
1195811953
return JNI_FALSE;
1195911954
}
11960-
jboolean accepted = SSL_ech_accepted(ssl);
11955+
JNI_TRACE("ssl=%p NativeCrypto_SSL_ech_accepted", ssl);
1196111956

11962-
if (!accepted) {
11957+
if (!SSL_ech_accepted(ssl)) {
1196311958
conscrypt::jniutil::throwParsingException(env, "Invalid ECH config list");
1196411959
ERR_clear_error();
1196511960
JNI_TRACE("ssl=%p NativeCrypto_SSL_ech_accepted => threw exception", ssl);
1196611961
return JNI_FALSE;
1196711962
}
1196811963

11969-
JNI_TRACE("ssl=%p NativeCrypto_SSL_ech_accepted => %d", ssl, accepted);
11970-
return accepted;
11964+
JNI_TRACE("ssl=%p NativeCrypto_SSL_ech_accepted => %d", ssl, JNI_TRUE);
11965+
return JNI_TRUE;
1197111966
}
1197211967

1197311968
static jboolean NativeCrypto_SSL_CTX_ech_enable_server(JNIEnv* env, jclass, jlong ssl_ctx_address,

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -457,17 +457,11 @@ public static String getApplicationProtocol(SSLSocket socket) {
457457
if (isConscrypt(socket)) {
458458
return toConscrypt(socket).getApplicationProtocol();
459459
}
460-
try {
461-
if (!Class.forName("com.android.org.conscrypt.AbstractConscryptSocket")
462-
.isInstance(socket)) {
463-
throw new IllegalArgumentException(
464-
"Not a conscrypt socket: " + socket.getClass().getName());
465-
}
466-
return invokeConscryptMethod(socket, "getApplicationProtocol");
467-
} catch (ClassNotFoundException e) {
460+
if (!socket.getClass().getName().contains("conscrypt")) {
468461
throw new IllegalArgumentException(
469-
"Not a conscrypt socket: " + socket.getClass().getName(), e);
462+
"Not a conscrypt socket: " + socket.getClass().getName());
470463
}
464+
return invokeConscryptMethod(socket, "getApplicationProtocol");
471465
}
472466

473467
/**
@@ -751,17 +745,11 @@ public static String getApplicationProtocol(SSLEngine engine) {
751745
if (isConscrypt(engine)) {
752746
return toConscrypt(engine).getApplicationProtocol();
753747
}
754-
try {
755-
if (!Class.forName("com.android.org.conscrypt.AbstractConscryptEngine")
756-
.isInstance(engine)) {
757-
throw new IllegalArgumentException(
758-
"Not a conscrypt engine: " + engine.getClass().getName());
759-
}
760-
return invokeConscryptMethod(engine, "getApplicationProtocol");
761-
} catch (ClassNotFoundException e) {
748+
if (!engine.getClass().getName().contains("conscrypt")) {
762749
throw new IllegalArgumentException(
763-
"Not a conscrypt engine: " + engine.getClass().getName(), e);
750+
"Not a conscrypt engine: " + engine.getClass().getName());
764751
}
752+
return invokeConscryptMethod(engine, "getApplicationProtocol");
765753
}
766754

767755
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ private DuckTypedHpkeSpi(Object delegate) throws NoSuchMethodException {
4444
if (targetMethod.isSynthetic()) {
4545
continue;
4646
}
47+
if (targetMethod.getName().equals("engineInitSenderForTesting")) {
48+
continue;
49+
}
4750

4851
Method sourceMethod =
4952
sourceClass.getMethod(targetMethod.getName(), targetMethod.getParameterTypes());
@@ -132,6 +135,10 @@ public void engineInitSender(
132135
@Override
133136
public void engineInitSenderForTesting(PublicKey recipientKey, byte[] info, PrivateKey senderKey,
134137
byte[] psk, byte[] pskId, byte[] sKe) throws InvalidKeyException {
138+
if (!methods.containsKey("engineInitSenderForTesting")) {
139+
throw new UnsupportedOperationException(
140+
"engineInitSenderForTesting is not supported by the delegate");
141+
}
135142
invokeWithPossibleInvalidKey("engineInitSenderForTesting",
136143
recipientKey, info, senderKey, psk, pskId, sKe);
137144
}

common/src/test/java/org/conscrypt/DuckTypedHpkeSpiTest.java

Lines changed: 37 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static org.conscrypt.HpkeFixture.DEFAULT_SUITE_NAME;
2828
import static org.conscrypt.HpkeFixture.createDefaultHpkeContextRecipient;
2929
import static org.conscrypt.HpkeFixture.createDefaultHpkeContextSender;
30-
import static org.conscrypt.HpkeTestVectorsTest.getHpkeEncryptionRecords;
3130
import static org.conscrypt.TestUtils.encodeHex;
3231
import static org.junit.Assert.assertArrayEquals;
3332
import static org.junit.Assert.assertEquals;
@@ -36,6 +35,13 @@
3635
import static org.junit.Assert.assertThrows;
3736
import static org.junit.Assert.assertTrue;
3837

38+
import org.conscrypt.java.security.DefaultKeys;
39+
import org.junit.After;
40+
import org.junit.Before;
41+
import org.junit.Test;
42+
import org.junit.runner.RunWith;
43+
import org.junit.runners.JUnit4;
44+
3945
import java.nio.charset.StandardCharsets;
4046
import java.security.GeneralSecurityException;
4147
import java.security.InvalidKeyException;
@@ -44,16 +50,6 @@
4450
import java.security.Provider;
4551
import java.security.PublicKey;
4652
import java.security.Security;
47-
import java.util.List;
48-
49-
import org.conscrypt.HpkeTestVectorsTest.HpkeData;
50-
import org.conscrypt.HpkeTestVectorsTest.HpkeEncryptionData;
51-
import org.conscrypt.java.security.DefaultKeys;
52-
import org.junit.After;
53-
import org.junit.Before;
54-
import org.junit.Test;
55-
import org.junit.runner.RunWith;
56-
import org.junit.runners.JUnit4;
5753

5854
/**
5955
* Tests for DuckTypedHpkeSpiTest. Essentially the same as the tests for HpkeContext but
@@ -134,14 +130,6 @@ public void export() throws Exception {
134130
assertNotEquals(encodeHex(export1), encodeHex(export2));
135131
}
136132

137-
@Test
138-
public void vectors() throws Exception {
139-
final List<HpkeData> records = getHpkeEncryptionRecords();
140-
for (HpkeData record : records) {
141-
testHpkeEncryption(record);
142-
}
143-
}
144-
145133
@Test
146134
public void initInvalidKeys() throws Exception {
147135
HpkeContextSender sender = HpkeContextSender.getInstance(DEFAULT_SUITE_NAME);
@@ -167,67 +155,37 @@ public void initInvalidKeys() throws Exception {
167155
() -> recipient.init(DEFAULT_ENC, invalidKey, DEFAULT_INFO));
168156
}
169157

170-
@Test
171-
public void testSeal_missingRequiredParameters_throwNullException() throws Exception {
172-
HpkeContextSender ctxSender = HpkeContextSender.getInstance(DEFAULT_SUITE_NAME);
173-
ctxSender.init(DEFAULT_PK, DEFAULT_INFO);
174-
assertThrows(NullPointerException.class,
175-
() -> ctxSender.seal(/* plaintext= */ null, DEFAULT_AAD));
176-
}
177-
178-
@Test
179-
public void testExport_lowerEdgeLength() throws Exception {
180-
final HpkeContextSender ctxSender = createDefaultHpkeContextSender();
181-
final byte[] enc = ctxSender.getEncapsulated();
182-
final byte[] export = ctxSender.export(/* length= */ 0, DEFAULT_EXPORTER_CONTEXT);
183-
assertNotNull(enc);
184-
assertNotNull(export);
185-
assertThrows(IllegalArgumentException.class,
186-
() -> ctxSender.export(/* length= */ -1, DEFAULT_EXPORTER_CONTEXT));
187-
}
188-
189-
190-
@Test
191-
public void testInitUnsupportedModes() throws Exception {
192-
HpkeContextSender sender = HpkeContextSender.getInstance(DEFAULT_SUITE_NAME);
193-
byte[] psk = "Shhh! Secret!".getBytes(StandardCharsets.UTF_8);
194-
byte[] pskId = "id".getBytes(StandardCharsets.UTF_8);
195-
196-
assertThrows(UnsupportedOperationException.class, () ->
197-
sender.init(DEFAULT_PK, DEFAULT_INFO, DEFAULT_SK));
198-
assertThrows(UnsupportedOperationException.class, () ->
199-
sender.init(DEFAULT_PK, DEFAULT_INFO, psk, pskId));
200-
assertThrows(UnsupportedOperationException.class, () ->
201-
sender.init(DEFAULT_PK, DEFAULT_INFO, DEFAULT_SK, psk, pskId));
202-
}
203-
204-
// Copied from HpkeTestVectorsTest but with extra checks to ensure we are operating on
205-
// duck typed instances.
206-
private void testHpkeEncryption(HpkeData record) throws Exception {
207-
final byte[] enc = record.pkEm;
208-
209-
// Encryption
210-
final HpkeContextSender contextSender =
211-
setupBaseForTesting(record.hpkeSuite, record.pkRm, record.info, record.skEm);
212-
assertForeign(contextSender);
213-
final byte[] encResult = contextSender.getEncapsulated();
214-
assertArrayEquals("Failed encryption 'enc' " + encodeHex(enc), enc, encResult);
215-
for (HpkeEncryptionData encryption : record.encryptions) {
216-
final byte[] ciphertext = contextSender.seal(encryption.pt, encryption.aad);
217-
assertArrayEquals("Failed encryption 'ciphertext' on data : " + encryption,
218-
encryption.ct, ciphertext);
219-
}
158+
@Test
159+
public void testSeal_missingRequiredParameters_throwNullException() throws Exception {
160+
HpkeContextSender ctxSender = HpkeContextSender.getInstance(DEFAULT_SUITE_NAME);
161+
ctxSender.init(DEFAULT_PK, DEFAULT_INFO);
162+
assertThrows(NullPointerException.class,
163+
() -> ctxSender.seal(/* plaintext= */ null, DEFAULT_AAD));
164+
}
220165

221-
// Decryption
222-
final HpkeContextRecipient contextRecipient =
223-
HpkeContextRecipient.getInstance(record.hpkeSuite.name());
224-
assertForeign(contextRecipient);
225-
contextRecipient.init(enc, record.skRm, record.info);
226-
for (HpkeEncryptionData encryption : record.encryptions) {
227-
final byte[] plaintext = contextRecipient.open(encryption.ct, encryption.aad);
228-
assertArrayEquals(
229-
"Failed decryption on data : " + encryption, encryption.pt, plaintext);
230-
}
166+
@Test
167+
public void testExport_lowerEdgeLength() throws Exception {
168+
final HpkeContextSender ctxSender = createDefaultHpkeContextSender();
169+
final byte[] enc = ctxSender.getEncapsulated();
170+
final byte[] export = ctxSender.export(/* length= */ 0, DEFAULT_EXPORTER_CONTEXT);
171+
assertNotNull(enc);
172+
assertNotNull(export);
173+
assertThrows(IllegalArgumentException.class,
174+
() -> ctxSender.export(/* length= */ -1, DEFAULT_EXPORTER_CONTEXT));
175+
}
176+
177+
@Test
178+
public void testInitUnsupportedModes() throws Exception {
179+
HpkeContextSender sender = HpkeContextSender.getInstance(DEFAULT_SUITE_NAME);
180+
byte[] psk = "Shhh! Secret!".getBytes(StandardCharsets.UTF_8);
181+
byte[] pskId = "id".getBytes(StandardCharsets.UTF_8);
182+
183+
assertThrows(UnsupportedOperationException.class,
184+
() -> sender.init(DEFAULT_PK, DEFAULT_INFO, DEFAULT_SK));
185+
assertThrows(UnsupportedOperationException.class,
186+
() -> sender.init(DEFAULT_PK, DEFAULT_INFO, psk, pskId));
187+
assertThrows(UnsupportedOperationException.class,
188+
() -> sender.init(DEFAULT_PK, DEFAULT_INFO, DEFAULT_SK, psk, pskId));
231189
}
232190

233191
private HpkeContextSender setupBaseForTesting(

0 commit comments

Comments
 (0)