From ce6739a9fd4b9b48020dc41d24d9f37b35cb3048 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Tue, 30 May 2023 22:00:24 +0100 Subject: [PATCH] Use US locale for lowercasing onion hostname. --- .../bramble/crypto/CryptoComponentImpl.java | 3 +- .../bramble/crypto/OnionEncodingTest.java | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 bramble-core/src/test/java/org/briarproject/bramble/crypto/OnionEncodingTest.java diff --git a/bramble-core/src/main/java/org/briarproject/bramble/crypto/CryptoComponentImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/crypto/CryptoComponentImpl.java index 1bb7fe9f8..5dd24a5f7 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/crypto/CryptoComponentImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/crypto/CryptoComponentImpl.java @@ -34,6 +34,7 @@ import java.security.NoSuchAlgorithmException; import java.security.Provider; import java.security.SecureRandom; import java.security.Security; +import java.util.Locale; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -500,7 +501,7 @@ class CryptoComponentImpl implements CryptoComponent { arraycopy(publicKey, 0, address, 0, publicKey.length); arraycopy(checksum, 0, address, publicKey.length, ONION_CHECKSUM_BYTES); address[address.length - 1] = ONION_HS_PROTOCOL_VERSION; - return Base32.encode(address).toLowerCase(); + return Base32.encode(address).toLowerCase(Locale.US); } } diff --git a/bramble-core/src/test/java/org/briarproject/bramble/crypto/OnionEncodingTest.java b/bramble-core/src/test/java/org/briarproject/bramble/crypto/OnionEncodingTest.java new file mode 100644 index 000000000..2574b7f53 --- /dev/null +++ b/bramble-core/src/test/java/org/briarproject/bramble/crypto/OnionEncodingTest.java @@ -0,0 +1,30 @@ +package org.briarproject.bramble.crypto; + +import org.briarproject.bramble.api.crypto.CryptoComponent; +import org.briarproject.bramble.test.BrambleTestCase; +import org.briarproject.bramble.test.TestSecureRandomProvider; +import org.junit.Test; + +import java.security.SecureRandom; +import java.util.regex.Pattern; + +import static org.junit.Assert.assertTrue; + +public class OnionEncodingTest extends BrambleTestCase { + + private static final Pattern ONION_V3 = Pattern.compile("[a-z2-7]{56}"); + + private final CryptoComponent crypto = + new CryptoComponentImpl(new TestSecureRandomProvider(), null); + private final SecureRandom secureRandom = new SecureRandom(); + + @Test + public void testHostnameIsValid() { + byte[] publicKey = new byte[32]; + for (int i = 0; i < 100; i++) { + secureRandom.nextBytes(publicKey); + String onion = crypto.encodeOnion(publicKey); + assertTrue(onion, ONION_V3.matcher(onion).matches()); + } + } +}