Commit 60eefbf3 authored by akwizgran's avatar akwizgran

Use named constants.

parent ba19716e
Pipeline #3502 passed with stage
in 8 minutes and 20 seconds
...@@ -76,6 +76,7 @@ import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_ONLY_WHE ...@@ -76,6 +76,7 @@ import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_ONLY_WHE
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_PORT; import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_PORT;
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V2; import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V2;
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V3; import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V3;
import static org.briarproject.bramble.plugin.tor.TorRendezvousCrypto.SEED_BYTES;
import static org.briarproject.bramble.util.IoUtils.copyAndClose; import static org.briarproject.bramble.util.IoUtils.copyAndClose;
import static org.briarproject.bramble.util.IoUtils.tryToClose; import static org.briarproject.bramble.util.IoUtils.tryToClose;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.LogUtils.logException;
...@@ -613,15 +614,15 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener { ...@@ -613,15 +614,15 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
@Override @Override
public RendezvousEndpoint createRendezvousEndpoint(KeyMaterialSource k, public RendezvousEndpoint createRendezvousEndpoint(KeyMaterialSource k,
boolean alice, ConnectionHandler incoming) { boolean alice, ConnectionHandler incoming) {
byte[] aliceSeed = k.getKeyMaterial(32); byte[] aliceSeed = k.getKeyMaterial(SEED_BYTES);
byte[] bobSeed = k.getKeyMaterial(32); byte[] bobSeed = k.getKeyMaterial(SEED_BYTES);
byte[] localSeed = alice ? aliceSeed : bobSeed; byte[] localSeed = alice ? aliceSeed : bobSeed;
byte[] remoteSeed = alice ? bobSeed : aliceSeed; byte[] remoteSeed = alice ? bobSeed : aliceSeed;
String blob = torRendezvousCrypto.getPrivateKeyBlob(localSeed); String blob = torRendezvousCrypto.getPrivateKeyBlob(localSeed);
String localOnion = torRendezvousCrypto.getOnionAddress(localSeed); String localOnion = torRendezvousCrypto.getOnionAddress(localSeed);
String remoteOnion = torRendezvousCrypto.getOnionAddress(remoteSeed); String remoteOnion = torRendezvousCrypto.getOnionAddress(remoteSeed);
TransportProperties remote = new TransportProperties(); TransportProperties remoteProperties = new TransportProperties();
remote.put(PROP_ONION_V3, remoteOnion); remoteProperties.put(PROP_ONION_V3, remoteOnion);
try { try {
ServerSocket ss = new ServerSocket(); ServerSocket ss = new ServerSocket();
ss.bind(new InetSocketAddress("127.0.0.1", 0)); ss.bind(new InetSocketAddress("127.0.0.1", 0));
...@@ -646,7 +647,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener { ...@@ -646,7 +647,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
@Override @Override
public TransportProperties getRemoteTransportProperties() { public TransportProperties getRemoteTransportProperties() {
return remote; return remoteProperties;
} }
@Override @Override
......
...@@ -2,6 +2,8 @@ package org.briarproject.bramble.plugin.tor; ...@@ -2,6 +2,8 @@ package org.briarproject.bramble.plugin.tor;
interface TorRendezvousCrypto { interface TorRendezvousCrypto {
static final int SEED_BYTES = 32;
String getOnionAddress(byte[] seed); String getOnionAddress(byte[] seed);
String getPrivateKeyBlob(byte[] seed); String getPrivateKeyBlob(byte[] seed);
......
...@@ -18,6 +18,9 @@ public class TorRendezvousCryptoImpl implements TorRendezvousCrypto { ...@@ -18,6 +18,9 @@ public class TorRendezvousCryptoImpl implements TorRendezvousCrypto {
private static final EdDSANamedCurveSpec CURVE_SPEC = private static final EdDSANamedCurveSpec CURVE_SPEC =
EdDSANamedCurveTable.getByName("Ed25519"); EdDSANamedCurveTable.getByName("Ed25519");
private static final byte HS_PROTOCOL_VERSION = 3;
private static final int CHECKSUM_BYTES = 2;
@Override @Override
public String getOnionAddress(byte[] seed) { public String getOnionAddress(byte[] seed) {
EdDSAPrivateKeySpec spec = new EdDSAPrivateKeySpec(seed, CURVE_SPEC); EdDSAPrivateKeySpec spec = new EdDSAPrivateKeySpec(seed, CURVE_SPEC);
...@@ -26,13 +29,13 @@ public class TorRendezvousCryptoImpl implements TorRendezvousCrypto { ...@@ -26,13 +29,13 @@ public class TorRendezvousCryptoImpl implements TorRendezvousCrypto {
byte[] label = ".onion checksum".getBytes(Charset.forName("US-ASCII")); byte[] label = ".onion checksum".getBytes(Charset.forName("US-ASCII"));
digest.update(label, 0, label.length); digest.update(label, 0, label.length);
digest.update(publicKey, 0, publicKey.length); digest.update(publicKey, 0, publicKey.length);
digest.update((byte) 3); digest.update(HS_PROTOCOL_VERSION);
byte[] checksum = new byte[digest.getDigestSize()]; byte[] checksum = new byte[digest.getDigestSize()];
digest.doFinal(checksum, 0); digest.doFinal(checksum, 0);
byte[] address = new byte[publicKey.length + 3]; byte[] address = new byte[publicKey.length + CHECKSUM_BYTES + 1];
arraycopy(publicKey, 0, address, 0, publicKey.length); arraycopy(publicKey, 0, address, 0, publicKey.length);
arraycopy(checksum, 0, address, publicKey.length, 2); arraycopy(checksum, 0, address, publicKey.length, CHECKSUM_BYTES);
address[address.length - 1] = 3; address[address.length - 1] = HS_PROTOCOL_VERSION;
return Base32.encode(address).toLowerCase(); return Base32.encode(address).toLowerCase();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment