There's something wrong with the V5 broadcast signature. I'm not quite shure what it is, so I made a test.

This commit is contained in:
Christian Basler 2015-06-10 00:49:44 +02:00
parent b4683bba68
commit 35996019a2
7 changed files with 39 additions and 17 deletions

View File

@ -67,14 +67,15 @@ public class Plaintext implements Streamable {
} }
public static Plaintext.Builder readWithoutSignature(Type type, InputStream in) throws IOException { public static Plaintext.Builder readWithoutSignature(Type type, InputStream in) throws IOException {
long version = Decode.varInt(in);
return new Builder(type) return new Builder(type)
.addressVersion(Decode.varInt(in)) .addressVersion(version)
.stream(Decode.varInt(in)) .stream(Decode.varInt(in))
.behaviorBitfield(Decode.int32(in)) .behaviorBitfield(Decode.int32(in))
.publicSigningKey(Decode.bytes(in, 64)) .publicSigningKey(Decode.bytes(in, 64))
.publicEncryptionKey(Decode.bytes(in, 64)) .publicEncryptionKey(Decode.bytes(in, 64))
.nonceTrialsPerByte(Decode.varInt(in)) .nonceTrialsPerByte(version >= 3 ? Decode.varInt(in) : 0)
.extraBytes(Decode.varInt(in)) .extraBytes(version >= 3 ? Decode.varInt(in) : 0)
.destinationRipe(type == Type.MSG ? Decode.bytes(in, 20) : null) .destinationRipe(type == Type.MSG ? Decode.bytes(in, 20) : null)
.encoding(Decode.varInt(in)) .encoding(Decode.varInt(in))
.message(Decode.varBytes(in)) .message(Decode.varBytes(in))
@ -136,8 +137,10 @@ public class Plaintext implements Streamable {
Encode.int32(from.getPubkey().getBehaviorBitfield(), out); Encode.int32(from.getPubkey().getBehaviorBitfield(), out);
out.write(from.getPubkey().getSigningKey(), 1, 64); out.write(from.getPubkey().getSigningKey(), 1, 64);
out.write(from.getPubkey().getEncryptionKey(), 1, 64); out.write(from.getPubkey().getEncryptionKey(), 1, 64);
if (from.getVersion() >= 3) {
Encode.varInt(from.getPubkey().getNonceTrialsPerByte(), out); Encode.varInt(from.getPubkey().getNonceTrialsPerByte(), out);
Encode.varInt(from.getPubkey().getExtraBytes(), out); Encode.varInt(from.getPubkey().getExtraBytes(), out);
}
if (type == Type.MSG) { if (type == Type.MSG) {
out.write(to.getRipe()); out.write(to.getRipe());
} }

View File

@ -47,6 +47,21 @@ public abstract class Broadcast extends ObjectPayload implements Encrypted, Plai
return address.getVersion() < 4 ? 4 : 5; return address.getVersion() < 4 ? 4 : 5;
} }
@Override
public boolean isSigned() {
return true;
}
@Override
public byte[] getSignature() {
return plaintext.getSignature();
}
@Override
public void setSignature(byte[] signature) {
plaintext.setSignature(signature);
}
@Override @Override
public long getStream() { public long getStream() {
return stream; return stream;

View File

@ -52,16 +52,6 @@ public class V4Broadcast extends Broadcast {
plaintext.write(out, false); plaintext.write(out, false);
} }
@Override
public byte[] getSignature() {
return plaintext.getSignature();
}
@Override
public void setSignature(byte[] signature) {
plaintext.setSignature(signature);
}
@Override @Override
public void write(OutputStream out) throws IOException { public void write(OutputStream out) throws IOException {
encrypted.write(out); encrypted.write(out);

View File

@ -27,21 +27,28 @@ import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class DecryptionTest { public class DecryptionTest {
@Test @Test
public void ensureV4BroadcastIsDecryptedCorrectly() throws IOException, DecryptionFailedException { public void ensureV4BroadcastIsDecryptedCorrectly() throws IOException, DecryptionFailedException {
BitmessageAddress address = new BitmessageAddress("BM-2D9Vc5rFxxR5vTi53T9gkLfemViHRMVLQZ");
TestUtils.loadPubkey(address);
ObjectMessage objectMessage = TestUtils.loadObjectMessage(5, "V4Broadcast.payload"); ObjectMessage objectMessage = TestUtils.loadObjectMessage(5, "V4Broadcast.payload");
V4Broadcast broadcast = (V4Broadcast) objectMessage.getPayload(); V4Broadcast broadcast = (V4Broadcast) objectMessage.getPayload();
broadcast.decrypt(new BitmessageAddress("BM-2D9Vc5rFxxR5vTi53T9gkLfemViHRMVLQZ")); broadcast.decrypt(address);
assertEquals("Test-Broadcast", broadcast.getPlaintext().getSubject()); assertEquals("Test-Broadcast", broadcast.getPlaintext().getSubject());
assertTrue(objectMessage.isSignatureValid(address.getPubkey()));
} }
@Test @Test
public void ensureV5BroadcastIsDecryptedCorrectly() throws IOException, DecryptionFailedException { public void ensureV5BroadcastIsDecryptedCorrectly() throws IOException, DecryptionFailedException {
BitmessageAddress address = new BitmessageAddress("BM-2cXxfcSetKnbHJX2Y85rSkaVpsdNUZ5q9h");
TestUtils.loadPubkey(address);
ObjectMessage objectMessage = TestUtils.loadObjectMessage(5, "V5Broadcast.payload"); ObjectMessage objectMessage = TestUtils.loadObjectMessage(5, "V5Broadcast.payload");
V5Broadcast broadcast = (V5Broadcast) objectMessage.getPayload(); V5Broadcast broadcast = (V5Broadcast) objectMessage.getPayload();
broadcast.decrypt(new BitmessageAddress("BM-2cXxfcSetKnbHJX2Y85rSkaVpsdNUZ5q9h")); broadcast.decrypt(address);
assertEquals("Test-Broadcast", broadcast.getPlaintext().getSubject()); assertEquals("Test-Broadcast", broadcast.getPlaintext().getSubject());
assertTrue(objectMessage.isSignatureValid(address.getPubkey()));
} }
} }

View File

@ -18,6 +18,7 @@ package ch.dissem.bitmessage.utils;
import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.ObjectMessage; import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.payload.Pubkey;
import ch.dissem.bitmessage.entity.payload.V4Pubkey; import ch.dissem.bitmessage.entity.payload.V4Pubkey;
import ch.dissem.bitmessage.entity.valueobject.PrivateKey; import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
import ch.dissem.bitmessage.exception.DecryptionFailedException; import ch.dissem.bitmessage.exception.DecryptionFailedException;
@ -76,4 +77,10 @@ public class TestUtils {
address.setPubkey((V4Pubkey) object.getPayload()); address.setPubkey((V4Pubkey) object.getPayload());
return address; return address;
} }
public static void loadPubkey(BitmessageAddress address) throws IOException {
byte[] bytes = getBytes(address.getAddress() + ".pubkey");
Pubkey pubkey = Factory.readPubkey(address.getVersion(), address.getStream(), new ByteArrayInputStream(bytes), bytes.length, false);
address.setPubkey(pubkey);
}
} }