mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Message parser and encoder.
This commit is contained in:
63
components/net/sf/briar/protocol/MessageEncoderImpl.java
Normal file
63
components/net/sf/briar/protocol/MessageEncoderImpl.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyPair;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.Signature;
|
||||
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageEncoder;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.serial.Writer;
|
||||
import net.sf.briar.api.serial.WriterFactory;
|
||||
|
||||
class MessageEncoderImpl implements MessageEncoder {
|
||||
|
||||
private final Signature signature;
|
||||
private final MessageDigest messageDigest;
|
||||
private final WriterFactory writerFactory;
|
||||
|
||||
MessageEncoderImpl(Signature signature, MessageDigest messageDigest,
|
||||
WriterFactory writerFactory) {
|
||||
this.signature = signature;
|
||||
this.messageDigest = messageDigest;
|
||||
this.writerFactory = writerFactory;
|
||||
}
|
||||
|
||||
public Message encodeMessage(MessageId parent, GroupId group, String nick,
|
||||
KeyPair keyPair, byte[] body) throws IOException,
|
||||
GeneralSecurityException {
|
||||
long timestamp = System.currentTimeMillis();
|
||||
byte[] encodedKey = keyPair.getPublic().getEncoded();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeRaw(parent);
|
||||
w.writeRaw(group);
|
||||
w.writeInt64(timestamp);
|
||||
w.writeUtf8(nick);
|
||||
w.writeRaw(encodedKey);
|
||||
w.writeRaw(body);
|
||||
byte[] signable = out.toByteArray();
|
||||
signature.initSign(keyPair.getPrivate());
|
||||
signature.update(signable);
|
||||
byte[] sig = signature.sign();
|
||||
w.writeRaw(sig);
|
||||
byte[] raw = out.toByteArray();
|
||||
w.close();
|
||||
// The message ID is the hash of the entire message
|
||||
messageDigest.reset();
|
||||
messageDigest.update(raw);
|
||||
MessageId id = new MessageId(messageDigest.digest());
|
||||
// The author ID is the hash of the author's nick and public key
|
||||
messageDigest.reset();
|
||||
messageDigest.update(nick.getBytes("UTF-8"));
|
||||
messageDigest.update((byte) 0); // Null separator
|
||||
messageDigest.update(encodedKey);
|
||||
AuthorId author = new AuthorId(messageDigest.digest());
|
||||
return new MessageImpl(id, parent, group, author, timestamp, raw);
|
||||
}
|
||||
}
|
||||
@@ -12,16 +12,16 @@ public class MessageImpl implements Message {
|
||||
private final GroupId group;
|
||||
private final AuthorId author;
|
||||
private final long timestamp;
|
||||
private final byte[] body;
|
||||
private final byte[] raw;
|
||||
|
||||
public MessageImpl(MessageId id, MessageId parent, GroupId group,
|
||||
AuthorId author, long timestamp, byte[] body) {
|
||||
AuthorId author, long timestamp, byte[] raw) {
|
||||
this.id = id;
|
||||
this.parent = parent;
|
||||
this.group = group;
|
||||
this.author = author;
|
||||
this.timestamp = timestamp;
|
||||
this.body = body;
|
||||
this.raw = raw;
|
||||
}
|
||||
|
||||
public MessageId getId() {
|
||||
@@ -45,11 +45,11 @@ public class MessageImpl implements Message {
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return body.length;
|
||||
return raw.length;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return body;
|
||||
return raw;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
84
components/net/sf/briar/protocol/MessageParserImpl.java
Normal file
84
components/net/sf/briar/protocol/MessageParserImpl.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.PublicKey;
|
||||
import java.security.Signature;
|
||||
import java.security.SignatureException;
|
||||
|
||||
import net.sf.briar.api.crypto.KeyParser;
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.MessageParser;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.FormatException;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
import net.sf.briar.api.serial.ReaderFactory;
|
||||
|
||||
class MessageParserImpl implements MessageParser {
|
||||
|
||||
private final KeyParser keyParser;
|
||||
private final Signature signature;
|
||||
private final MessageDigest messageDigest;
|
||||
private final ReaderFactory readerFactory;
|
||||
|
||||
MessageParserImpl(KeyParser keyParser, Signature signature,
|
||||
MessageDigest messageDigest, ReaderFactory readerFactory) {
|
||||
this.keyParser = keyParser;
|
||||
this.signature = signature;
|
||||
this.messageDigest = messageDigest;
|
||||
this.readerFactory = readerFactory;
|
||||
}
|
||||
|
||||
public Message parseMessage(byte[] raw) throws IOException,
|
||||
GeneralSecurityException {
|
||||
if(raw.length > Message.MAX_SIZE) throw new FormatException();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(raw);
|
||||
Reader r = readerFactory.createReader(in);
|
||||
// Read the parent message ID
|
||||
byte[] idBytes = r.readRaw();
|
||||
if(idBytes.length != UniqueId.LENGTH) throw new FormatException();
|
||||
MessageId parent = new MessageId(idBytes);
|
||||
// Read the group ID
|
||||
idBytes = r.readRaw();
|
||||
if(idBytes.length != UniqueId.LENGTH) throw new FormatException();
|
||||
GroupId group = new GroupId(idBytes);
|
||||
// Read the timestamp
|
||||
long timestamp = r.readInt64();
|
||||
// Hash the author's nick and public key to get the author ID
|
||||
String nick = r.readUtf8();
|
||||
byte[] encodedKey = r.readRaw();
|
||||
PublicKey publicKey = keyParser.parsePublicKey(encodedKey);
|
||||
messageDigest.reset();
|
||||
messageDigest.update(nick.getBytes("UTF-8"));
|
||||
messageDigest.update((byte) 0); // Null separator
|
||||
messageDigest.update(encodedKey);
|
||||
AuthorId author = new AuthorId(messageDigest.digest());
|
||||
// Skip the message body
|
||||
r.readRaw();
|
||||
// Read the signature and work out how long the signed message is
|
||||
byte[] sig = r.readRaw();
|
||||
int length = raw.length - sig.length - bytesToEncode(sig.length);
|
||||
// Verify the signature
|
||||
signature.initVerify(publicKey);
|
||||
signature.update(raw, 0, length);
|
||||
if(!signature.verify(sig)) throw new SignatureException();
|
||||
// Hash the message, including the signature, to get the message ID
|
||||
messageDigest.reset();
|
||||
messageDigest.update(raw);
|
||||
MessageId id = new MessageId(messageDigest.digest());
|
||||
return new MessageImpl(id, parent, group, author, timestamp, raw);
|
||||
}
|
||||
|
||||
// FIXME: Work out a better way of doing this
|
||||
private int bytesToEncode(int i) {
|
||||
if(i >= 0 && i <= Byte.MAX_VALUE) return 2;
|
||||
if(i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) return 3;
|
||||
if(i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) return 4;
|
||||
return 6;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user