Remove new member announcement and add signature to invitation

This commit is contained in:
Torsten Grote
2016-11-03 12:24:56 -02:00
parent 4bad7076e7
commit 7125248677
11 changed files with 257 additions and 251 deletions

View File

@@ -41,6 +41,13 @@ class ContactGroupFactoryImpl implements ContactGroupFactory {
return groupFactory.createGroup(clientId, descriptor);
}
@Override
public Group createContactGroup(ClientId clientId, AuthorId authorId1,
AuthorId authorId2) {
byte[] descriptor = createGroupDescriptor(authorId1, authorId2);
return groupFactory.createGroup(clientId, descriptor);
}
private byte[] createGroupDescriptor(AuthorId local, AuthorId remote) {
try {
if (Bytes.COMPARATOR.compare(local, remote) < 0)

View File

@@ -3,7 +3,6 @@ package org.briarproject.privategroup;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.nullsafety.NotNullByDefault;
import org.briarproject.api.privategroup.GroupMessage;
@@ -18,7 +17,6 @@ import java.security.GeneralSecurityException;
import javax.inject.Inject;
import static org.briarproject.api.privategroup.MessageType.JOIN;
import static org.briarproject.api.privategroup.MessageType.NEW_MEMBER;
import static org.briarproject.api.privategroup.MessageType.POST;
@NotNullByDefault
@@ -32,45 +30,34 @@ class GroupMessageFactoryImpl implements GroupMessageFactory {
}
@Override
public GroupMessage createNewMemberMessage(GroupId groupId, long timestamp,
LocalAuthor creator, Author member) {
try {
// Generate the signature
int type = NEW_MEMBER.getInt();
BdfList toSign = BdfList.of(groupId, timestamp, type,
member.getName(), member.getPublicKey());
byte[] signature =
clientHelper.sign(toSign, creator.getPrivateKey());
public GroupMessage createJoinMessage(GroupId groupId, long timestamp,
LocalAuthor creator) {
// Compose the message
BdfList body =
BdfList.of(type, member.getName(),
member.getPublicKey(), signature);
Message m = clientHelper.createMessage(groupId, timestamp, body);
return new GroupMessage(m, null, member);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (FormatException e) {
throw new RuntimeException(e);
}
return createJoinMessage(groupId, timestamp, creator, null);
}
@Override
public GroupMessage createJoinMessage(GroupId groupId, long timestamp,
LocalAuthor member, MessageId newMemberId) {
LocalAuthor member, long inviteTimestamp, byte[] creatorSignature) {
BdfList invite = BdfList.of(inviteTimestamp, creatorSignature);
return createJoinMessage(groupId, timestamp, member, invite);
}
private GroupMessage createJoinMessage(GroupId groupId, long timestamp,
LocalAuthor member, @Nullable BdfList invite) {
try {
// Generate the signature
int type = JOIN.getInt();
BdfList toSign = BdfList.of(groupId, timestamp, type,
member.getName(), member.getPublicKey(), newMemberId);
byte[] signature =
member.getName(), member.getPublicKey(), invite);
byte[] memberSignature =
clientHelper.sign(toSign, member.getPrivateKey());
// Compose the message
BdfList body =
BdfList.of(type, member.getName(),
member.getPublicKey(), newMemberId, signature);
member.getPublicKey(), invite, memberSignature);
Message m = clientHelper.createMessage(groupId, timestamp, body);
return new GroupMessage(m, null, member);

View File

@@ -3,6 +3,7 @@ package org.briarproject.privategroup;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.BdfMessageContext;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.ContactGroupFactory;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder;
@@ -11,6 +12,7 @@ import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.privategroup.MessageType;
import org.briarproject.api.privategroup.PrivateGroup;
import org.briarproject.api.privategroup.PrivateGroupFactory;
import org.briarproject.api.privategroup.invitation.GroupInvitationManager;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.InvalidMessageException;
import org.briarproject.api.sync.Message;
@@ -21,19 +23,16 @@ import org.briarproject.clients.BdfMessageValidator;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.api.privategroup.MessageType.JOIN;
import static org.briarproject.api.privategroup.MessageType.NEW_MEMBER;
import static org.briarproject.api.privategroup.MessageType.POST;
import static org.briarproject.api.privategroup.PrivateGroupConstants.MAX_GROUP_POST_BODY_LENGTH;
import static org.briarproject.privategroup.Constants.KEY_MEMBER_ID;
import static org.briarproject.privategroup.Constants.KEY_MEMBER_NAME;
import static org.briarproject.privategroup.Constants.KEY_MEMBER_PUBLIC_KEY;
import static org.briarproject.privategroup.Constants.KEY_NEW_MEMBER_MSG_ID;
import static org.briarproject.privategroup.Constants.KEY_PARENT_MSG_ID;
import static org.briarproject.privategroup.Constants.KEY_PREVIOUS_MSG_ID;
import static org.briarproject.privategroup.Constants.KEY_READ;
@@ -42,52 +41,50 @@ import static org.briarproject.privategroup.Constants.KEY_TYPE;
class GroupMessageValidator extends BdfMessageValidator {
private final ContactGroupFactory contactGroupFactory;
private final PrivateGroupFactory groupFactory;
private final AuthorFactory authorFactory;
private final GroupInvitationManager groupInvitationManager; // TODO remove
GroupMessageValidator(PrivateGroupFactory groupFactory,
GroupMessageValidator(ContactGroupFactory contactGroupFactory,
PrivateGroupFactory groupFactory,
ClientHelper clientHelper, MetadataEncoder metadataEncoder,
Clock clock, AuthorFactory authorFactory) {
Clock clock, AuthorFactory authorFactory,
GroupInvitationManager groupInvitationManager) {
super(clientHelper, metadataEncoder, clock);
this.contactGroupFactory = contactGroupFactory;
this.groupFactory = groupFactory;
this.authorFactory = authorFactory;
this.groupInvitationManager = groupInvitationManager;
}
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList body) throws InvalidMessageException, FormatException {
checkSize(body, 4, 7);
checkSize(body, 5, 7);
// message type (int)
int type = body.getLong(0).intValue();
body.removeElementAt(0);
// member_name (string)
String memberName = body.getString(0);
String memberName = body.getString(1);
checkLength(memberName, 1, MAX_AUTHOR_NAME_LENGTH);
// member_public_key (raw)
byte[] memberPublicKey = body.getRaw(1);
byte[] memberPublicKey = body.getRaw(2);
checkLength(memberPublicKey, 1, MAX_PUBLIC_KEY_LENGTH);
Author member = authorFactory.createAuthor(memberName, memberPublicKey);
BdfMessageContext c;
switch (MessageType.valueOf(type)) {
case NEW_MEMBER:
c = validateNewMember(m, g, body, memberName,
memberPublicKey);
addMessageMetadata(c, memberName, memberPublicKey,
m.getTimestamp());
break;
case JOIN:
c = validateJoin(m, g, body, memberName, memberPublicKey);
addMessageMetadata(c, memberName, memberPublicKey,
m.getTimestamp());
c = validateJoin(m, g, body, member);
addMessageMetadata(c, member, m.getTimestamp());
break;
case POST:
c = validatePost(m, g, body, memberName, memberPublicKey);
addMessageMetadata(c, memberName, memberPublicKey,
m.getTimestamp());
c = validatePost(m, g, body, member);
addMessageMetadata(c, member, m.getTimestamp());
break;
default:
throw new InvalidMessageException("Unknown Message Type");
@@ -96,26 +93,64 @@ class GroupMessageValidator extends BdfMessageValidator {
return c;
}
private BdfMessageContext validateNewMember(Message m, Group g,
BdfList body, String memberName, byte[] memberPublicKey)
private BdfMessageContext validateJoin(Message m, Group g, BdfList body,
Author member)
throws InvalidMessageException, FormatException {
// The content is a BDF list with three elements
checkSize(body, 3);
// The content is a BDF list with five elements
checkSize(body, 5);
PrivateGroup pg = groupFactory.parsePrivateGroup(g);
// signature (raw)
// signature with the creator's private key over a list with 4 elements
byte[] signature = body.getRaw(2);
checkLength(signature, 1, MAX_SIGNATURE_LENGTH);
// invite is null if the member is the creator of the private group
BdfList invite = body.getList(3, null);
if (invite == null) {
if (!member.equals(pg.getAuthor()))
throw new InvalidMessageException();
} else {
// Otherwise invite is a list with two elements
checkSize(invite, 2);
// invite_timestamp (int)
// join_timestamp must be greater than invite_timestamp
long inviteTimestamp = invite.getLong(0);
if (m.getTimestamp() <= inviteTimestamp)
throw new InvalidMessageException();
// creator_signature (raw)
byte[] creatorSignature = invite.getRaw(1);
checkLength(creatorSignature, 1, MAX_SIGNATURE_LENGTH);
// derive invitation group
Group invitationGroup = contactGroupFactory
.createContactGroup(groupInvitationManager.getClientId(),
pg.getAuthor().getId(), member.getId());
// signature with the creator's private key
// over a list with four elements:
// invite_type (int), invite_timestamp (int),
// invitation_group_id (raw), and private_group_id (raw)
BdfList signed =
BdfList.of(0, inviteTimestamp, invitationGroup.getId(),
g.getId());
try {
clientHelper.verifySignature(creatorSignature,
pg.getAuthor().getPublicKey(), signed);
} catch (GeneralSecurityException e) {
throw new InvalidMessageException(e);
}
}
// member_signature (raw)
// a signature with the member's private key over a list with 6 elements
byte[] memberSignature = body.getRaw(4);
checkLength(memberSignature, 1, MAX_SIGNATURE_LENGTH);
// Verify Signature
BdfList signed =
BdfList.of(g.getId(), m.getTimestamp(), NEW_MEMBER.getInt(),
memberName, memberPublicKey);
PrivateGroup group = groupFactory.parsePrivateGroup(g);
byte[] creatorPublicKey = group.getAuthor().getPublicKey();
BdfList signed = BdfList.of(g.getId(), m.getTimestamp(), JOIN.getInt(),
member.getName(), member.getPublicKey(), invite);
try {
clientHelper.verifySignature(signature, creatorPublicKey, signed);
clientHelper.verifySignature(memberSignature, member.getPublicKey(),
signed);
} catch (GeneralSecurityException e) {
throw new InvalidMessageException(e);
}
@@ -125,75 +160,39 @@ class GroupMessageValidator extends BdfMessageValidator {
return new BdfMessageContext(meta);
}
private BdfMessageContext validateJoin(Message m, Group g, BdfList body,
String memberName, byte[] memberPublicKey)
throws InvalidMessageException, FormatException {
// The content is a BDF list with four elements
checkSize(body, 4);
// new_member_id (raw)
// the identifier of a new member message
// with the same member_name and member_public_key
byte[] newMemberId = body.getRaw(2);
checkLength(newMemberId, MessageId.LENGTH);
// signature (raw)
// a signature with the member's private key over a list with 5 elements
byte[] signature = body.getRaw(3);
checkLength(signature, 1, MAX_SIGNATURE_LENGTH);
// Verify Signature
BdfList signed = BdfList.of(g.getId(), m.getTimestamp(), JOIN.getInt(),
memberName, memberPublicKey, newMemberId);
try {
clientHelper.verifySignature(signature, memberPublicKey, signed);
} catch (GeneralSecurityException e) {
throw new InvalidMessageException(e);
}
// The new member message is a dependency
Collection<MessageId> dependencies =
Collections.singleton(new MessageId(newMemberId));
// Return the metadata and dependencies
BdfDictionary meta = new BdfDictionary();
meta.put(KEY_NEW_MEMBER_MSG_ID, newMemberId);
return new BdfMessageContext(meta, dependencies);
}
private BdfMessageContext validatePost(Message m, Group g, BdfList body,
String memberName, byte[] memberPublicKey)
Author member)
throws InvalidMessageException, FormatException {
// The content is a BDF list with six elements
checkSize(body, 6);
checkSize(body, 7);
// parent_id (raw or null)
// the identifier of the post to which this is a reply, if any
byte[] parentId = body.getOptionalRaw(2);
byte[] parentId = body.getOptionalRaw(3);
checkLength(parentId, MessageId.LENGTH);
// previous_message_id (raw)
// the identifier of the member's previous post or join message
byte[] previousMessageId = body.getRaw(3);
byte[] previousMessageId = body.getRaw(4);
checkLength(previousMessageId, MessageId.LENGTH);
// content (string)
String content = body.getString(4);
String content = body.getString(5);
checkLength(content, 0, MAX_GROUP_POST_BODY_LENGTH);
// signature (raw)
// a signature with the member's private key over a list with 7 elements
byte[] signature = body.getRaw(5);
byte[] signature = body.getRaw(6);
checkLength(signature, 1, MAX_SIGNATURE_LENGTH);
// Verify Signature
BdfList signed = BdfList.of(g.getId(), m.getTimestamp(), POST.getInt(),
memberName, memberPublicKey, parentId, previousMessageId,
content);
member.getName(), member.getPublicKey(), parentId,
previousMessageId, content);
try {
clientHelper.verifySignature(signature, memberPublicKey, signed);
clientHelper
.verifySignature(signature, member.getPublicKey(), signed);
} catch (GeneralSecurityException e) {
throw new InvalidMessageException(e);
}
@@ -211,14 +210,13 @@ class GroupMessageValidator extends BdfMessageValidator {
return new BdfMessageContext(meta, dependencies);
}
private void addMessageMetadata(BdfMessageContext c, String authorName,
byte[] pubKey, long time) {
private void addMessageMetadata(BdfMessageContext c, Author member,
long time) {
c.getDictionary().put(KEY_TIMESTAMP, time);
c.getDictionary().put(KEY_READ, false);
Author a = authorFactory.createAuthor(authorName, pubKey);
c.getDictionary().put(KEY_MEMBER_ID, a.getId());
c.getDictionary().put(KEY_MEMBER_NAME, authorName);
c.getDictionary().put(KEY_MEMBER_PUBLIC_KEY, pubKey);
c.getDictionary().put(KEY_MEMBER_ID, member.getId());
c.getDictionary().put(KEY_MEMBER_NAME, member.getName());
c.getDictionary().put(KEY_MEMBER_PUBLIC_KEY, member.getPublicKey());
}
}

View File

@@ -49,14 +49,12 @@ import static org.briarproject.api.identity.Author.Status.OURSELVES;
import static org.briarproject.api.identity.Author.Status.UNVERIFIED;
import static org.briarproject.api.identity.Author.Status.VERIFIED;
import static org.briarproject.api.privategroup.MessageType.JOIN;
import static org.briarproject.api.privategroup.MessageType.NEW_MEMBER;
import static org.briarproject.api.privategroup.MessageType.POST;
import static org.briarproject.privategroup.Constants.KEY_DISSOLVED;
import static org.briarproject.privategroup.Constants.KEY_MEMBERS;
import static org.briarproject.privategroup.Constants.KEY_MEMBER_ID;
import static org.briarproject.privategroup.Constants.KEY_MEMBER_NAME;
import static org.briarproject.privategroup.Constants.KEY_MEMBER_PUBLIC_KEY;
import static org.briarproject.privategroup.Constants.KEY_NEW_MEMBER_MSG_ID;
import static org.briarproject.privategroup.Constants.KEY_PARENT_MSG_ID;
import static org.briarproject.privategroup.Constants.KEY_PREVIOUS_MSG_ID;
import static org.briarproject.privategroup.Constants.KEY_READ;
@@ -95,8 +93,7 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
}
@Override
public void addPrivateGroup(PrivateGroup group,
GroupMessage newMemberMsg, GroupMessage joinMsg)
public void addPrivateGroup(PrivateGroup group, GroupMessage joinMsg)
throws DbException {
Transaction txn = db.startTransaction(false);
try {
@@ -106,7 +103,6 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
new BdfEntry(KEY_DISSOLVED, false)
);
clientHelper.mergeGroupMetadata(txn, group.getId(), meta);
announceNewMember(txn, newMemberMsg);
joinPrivateGroup(txn, joinMsg);
db.commitTransaction(txn);
} catch (FormatException e) {
@@ -116,14 +112,6 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
}
}
private void announceNewMember(Transaction txn, GroupMessage m)
throws DbException, FormatException {
BdfDictionary meta = new BdfDictionary();
meta.put(KEY_TYPE, NEW_MEMBER.getInt());
addMessageMetadata(meta, m, true);
clientHelper.addLocalMessage(txn, m.getMessage(), meta, true);
}
private void joinPrivateGroup(Transaction txn, GroupMessage m)
throws DbException, FormatException {
BdfDictionary meta = new BdfDictionary();
@@ -315,8 +303,6 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
// get all authors we need to get the status for
Set<AuthorId> authors = new HashSet<AuthorId>();
for (BdfDictionary meta : metadata.values()) {
if (meta.getLong(KEY_TYPE) == NEW_MEMBER.getInt())
continue;
byte[] idBytes = meta.getRaw(KEY_MEMBER_ID);
authors.add(new AuthorId(idBytes));
}
@@ -328,8 +314,6 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
// Parse the metadata
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
BdfDictionary meta = entry.getValue();
if (meta.getLong(KEY_TYPE) == NEW_MEMBER.getInt())
continue;
headers.add(getGroupMessageHeader(txn, g, entry.getKey(), meta,
statuses));
}
@@ -434,36 +418,7 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
MessageType type =
MessageType.valueOf(meta.getLong(KEY_TYPE).intValue());
switch (type) {
case NEW_MEMBER:
// don't track incoming message, because it won't show in the UI
return true;
case JOIN:
// new_member_id must be the identifier of a NEW_MEMBER message
byte[] newMemberIdBytes =
meta.getOptionalRaw(KEY_NEW_MEMBER_MSG_ID);
MessageId newMemberId = new MessageId(newMemberIdBytes);
BdfDictionary newMemberMeta = clientHelper
.getMessageMetadataAsDictionary(txn, newMemberId);
MessageType newMemberType = MessageType
.valueOf(newMemberMeta.getLong(KEY_TYPE).intValue());
if (newMemberType != NEW_MEMBER) {
// FIXME throw new InvalidMessageException() (#643)
db.deleteMessage(txn, m.getId());
return false;
}
// timestamp must be equal to timestamp of NEW_MEMBER message
if (timestamp != newMemberMeta.getLong(KEY_TIMESTAMP)) {
// FIXME throw new InvalidMessageException() (#643)
db.deleteMessage(txn, m.getId());
return false;
}
// NEW_MEMBER must have same member_name and member_public_key
if (!Arrays.equals(meta.getRaw(KEY_MEMBER_ID),
newMemberMeta.getRaw(KEY_MEMBER_ID))) {
// FIXME throw new InvalidMessageException() (#643)
db.deleteMessage(txn, m.getId());
return false;
}
addMember(txn, m.getGroupId(), getAuthor(meta));
trackIncomingMessage(txn, m);
return true;

View File

@@ -1,6 +1,7 @@
package org.briarproject.privategroup;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.ContactGroupFactory;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.identity.AuthorFactory;
@@ -58,14 +59,16 @@ public class PrivateGroupModule {
@Provides
@Singleton
GroupMessageValidator provideGroupMessageValidator(
ContactGroupFactory contactGroupFactory,
PrivateGroupFactory groupFactory,
ValidationManager validationManager, ClientHelper clientHelper,
MetadataEncoder metadataEncoder, Clock clock,
AuthorFactory authorFactory) {
AuthorFactory authorFactory,
GroupInvitationManager groupInvitationManager) {
GroupMessageValidator validator = new GroupMessageValidator(
groupFactory, clientHelper, metadataEncoder, clock,
authorFactory);
contactGroupFactory, groupFactory, clientHelper,
metadataEncoder, clock, authorFactory, groupInvitationManager);
validationManager.registerMessageValidator(
PrivateGroupManagerImpl.CLIENT_ID, validator);