Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TorIdentity: Add onion address encoding from key #1189

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions network/tor/tor/src/main/java/bisq/tor/TorIdentity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,61 @@

import lombok.Getter;
import lombok.ToString;
import org.bouncycastle.crypto.digests.SHA3Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.math.ec.rfc8032.Ed25519;
import org.bouncycastle.util.encoders.Base32;

import java.nio.ByteBuffer;
import java.security.SecureRandom;

@Getter
@ToString
public class TorIdentity {

@ToString.Exclude
private final String privateKey;
private final byte[] privateKey;
private final int port;

public TorIdentity(String privateKey, int port) {
public TorIdentity(byte[] privateKey, int port) {
this.privateKey = privateKey;
this.port = port;
}

public static TorIdentity generate(int port) {
// Key Format definition:
// https://gitlab.torproject.org/tpo/core/torspec/-/blob/main/control-spec.txt

byte[] privateKey = new byte[32];
Ed25519.generatePrivateKey(new SecureRandom(), privateKey);
return new TorIdentity(privateKey, port);
}

public String getTorOnionKey() {
// Key Format definition:
// https://gitlab.torproject.org/tpo/core/torspec/-/blob/main/control-spec.txt

byte[] secretScalar = generateSecretScalar(privateKey);
String base64EncodedSecretScalar = java.util.Base64.getEncoder()
.encodeToString(secretScalar);

String torOnionKey = "-----BEGIN OPENSSH PRIVATE KEY-----\n" +
return "-----BEGIN OPENSSH PRIVATE KEY-----\n" +
base64EncodedSecretScalar + "\n" +
"-----END OPENSSH PRIVATE KEY-----\n";
}

public String getOnionAddress() {
byte[] publicKey = new byte[32];
Ed25519.generatePublicKey(privateKey, 0, publicKey, 0);

return new TorIdentity(torOnionKey, port);
byte[] checksumForAddress = computeOnionAddressChecksum(publicKey);

ByteBuffer byteBuffer = ByteBuffer.allocate(32 + 2 + 1);
byteBuffer.put(publicKey); // 32 bytes
byteBuffer.put(checksumForAddress); // 2 bytes
byteBuffer.put((byte) 3); // 1 byte

byte[] byteArray = byteBuffer.array();
String base32String = Base32.toBase32String(byteArray);

return base32String.toLowerCase() + ".onion";
}

private static byte[] generateSecretScalar(byte[] privateKey) {
Expand All @@ -70,4 +91,24 @@ private static byte[] generateSecretScalar(byte[] privateKey) {

return secretScalar;
}

private byte[] computeOnionAddressChecksum(byte[] publicKey) {
ByteBuffer byteBuffer = ByteBuffer.allocate(15 + 32 + 1);
byteBuffer.put(".onion checksum".getBytes()); // 15 bytes
byteBuffer.put(publicKey); // 32 bytes
byteBuffer.put((byte) 3); // 1 byte

byte[] byteArray = byteBuffer.array();

SHA3Digest sha3_256Digest = new SHA3Digest();
sha3_256Digest.update(byteArray, 0, byteArray.length);

byte[] hashedByteArray = new byte[64];
sha3_256Digest.doFinal(hashedByteArray, 0);

return new byte[]{
hashedByteArray[0],
hashedByteArray[1]
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public synchronized CompletableFuture<OnionAddress> publish(String nodeId, int o

public synchronized CompletableFuture<Void> publish(TorIdentity torIdentity, int localPort) {
try {
Optional<String> privateKey = Optional.of(torIdentity.getPrivateKey());
Optional<String> privateKey = Optional.of(torIdentity.getTorOnionKey());
nativeTorController.createHiddenService(torIdentity.getPort(), localPort, privateKey);
return CompletableFuture.completedFuture(null);

Expand Down