From 586d1739ae11710564105e3a19fb7f56f8e44ee7 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Mon, 25 Jul 2011 20:11:32 +0100 Subject: [PATCH] Removed salt from unrestricted groups: two unrestricted groups with the same name will now be treated as the same group (this seems more intuitive than the alternative). --- api/net/sf/briar/api/protocol/Group.java | 17 +-------- .../sf/briar/api/protocol/GroupFactory.java | 3 +- components/net/sf/briar/db/JdbcDatabase.java | 37 +++++++------------ .../sf/briar/protocol/GroupFactoryImpl.java | 26 +------------ .../net/sf/briar/protocol/GroupImpl.java | 29 +++------------ .../net/sf/briar/protocol/GroupReader.java | 7 ++-- test/net/sf/briar/db/H2DatabaseTest.java | 9 ++--- .../sf/briar/protocol/FileReadWriteTest.java | 8 ++-- 8 files changed, 34 insertions(+), 102 deletions(-) diff --git a/api/net/sf/briar/api/protocol/Group.java b/api/net/sf/briar/api/protocol/Group.java index 13e63a2b3..d25b69eed 100644 --- a/api/net/sf/briar/api/protocol/Group.java +++ b/api/net/sf/briar/api/protocol/Group.java @@ -1,7 +1,5 @@ package net.sf.briar.api.protocol; -import java.security.PublicKey; - import net.sf.briar.api.serial.Writable; /** A group to which users may subscribe. */ @@ -13,22 +11,9 @@ public interface Group extends Writable { /** Returns the group's name. */ String getName(); - /** - * Returns true if messages sent to the group must be signed with a - * particular private key. - */ - boolean isRestricted(); - - /** - * If the group is restricted, returns null. Otherwise returns a salt - * value that is combined with the group's name to generate its unique - * identifier. - */ - byte[] getSalt(); - /** * If the group is restricted, returns the public key that is used to * authorise all messages sent to the group. Otherwise returns null. */ - PublicKey getPublicKey(); + byte[] getPublicKey(); } diff --git a/api/net/sf/briar/api/protocol/GroupFactory.java b/api/net/sf/briar/api/protocol/GroupFactory.java index d10ab913a..cd6f711e5 100644 --- a/api/net/sf/briar/api/protocol/GroupFactory.java +++ b/api/net/sf/briar/api/protocol/GroupFactory.java @@ -2,6 +2,5 @@ package net.sf.briar.api.protocol; public interface GroupFactory { - Group createGroup(GroupId id, String name, boolean restricted, - byte[] saltOrKey); + Group createGroup(GroupId id, String name, byte[] publicKey); } diff --git a/components/net/sf/briar/db/JdbcDatabase.java b/components/net/sf/briar/db/JdbcDatabase.java index 9e251e201..e7bf13d0b 100644 --- a/components/net/sf/briar/db/JdbcDatabase.java +++ b/components/net/sf/briar/db/JdbcDatabase.java @@ -40,8 +40,7 @@ abstract class JdbcDatabase implements Database { "CREATE TABLE localSubscriptions" + " (groupId HASH NOT NULL," + " groupName VARCHAR NOT NULL," - + " restricted BOOLEAN NOT NULL," - + " groupKey BINARY NOT NULL," + + " groupKey BINARY," + " PRIMARY KEY (groupId))"; private static final String CREATE_MESSAGES = @@ -90,8 +89,7 @@ abstract class JdbcDatabase implements Database { + " (contactId INT NOT NULL," + " groupId HASH NOT NULL," + " groupName VARCHAR NOT NULL," - + " restricted BOOLEAN NOT NULL," - + " groupKey BINARY NOT NULL," + + " groupKey BINARY," + " PRIMARY KEY (contactId, groupId)," + " FOREIGN KEY (contactId) REFERENCES contacts (contactId)" + " ON DELETE CASCADE)"; @@ -530,14 +528,12 @@ abstract class JdbcDatabase implements Database { PreparedStatement ps = null; try { String sql = "INSERT INTO localSubscriptions" - + " (groupId, groupName, restricted, groupKey)" - + " VALUES (?, ?, ?, ?)"; + + " (groupId, groupName, groupKey)" + + " VALUES (?, ?, ?)"; ps = txn.prepareStatement(sql); ps.setBytes(1, g.getId().getBytes()); ps.setString(2, g.getName()); - ps.setBoolean(3, g.isRestricted()); - if(g.isRestricted()) ps.setBytes(4, g.getPublicKey().getEncoded()); - else ps.setBytes(4, g.getSalt()); + ps.setBytes(3, g.getPublicKey()); int rowsAffected = ps.executeUpdate(); assert rowsAffected == 1; ps.close(); @@ -989,7 +985,7 @@ abstract class JdbcDatabase implements Database { PreparedStatement ps = null; ResultSet rs = null; try { - String sql = "SELECT groupId, groupName, restricted, groupKey" + String sql = "SELECT groupId, groupName, groupKey" + " FROM localSubscriptions"; ps = txn.prepareStatement(sql); rs = ps.executeQuery(); @@ -997,9 +993,8 @@ abstract class JdbcDatabase implements Database { while(rs.next()) { GroupId id = new GroupId(rs.getBytes(1)); String name = rs.getString(2); - boolean restricted = rs.getBoolean(3); - byte[] key = rs.getBytes(4); - subs.add(groupFactory.createGroup(id, name, restricted, key)); + byte[] publicKey = rs.getBytes(3); + subs.add(groupFactory.createGroup(id, name, publicKey)); } rs.close(); ps.close(); @@ -1017,7 +1012,7 @@ abstract class JdbcDatabase implements Database { PreparedStatement ps = null; ResultSet rs = null; try { - String sql = "SELECT groupId, groupName, restricted, groupKey" + String sql = "SELECT groupId, groupName, groupKey" + " FROM contactSubscriptions" + " WHERE contactId = ?"; ps = txn.prepareStatement(sql); @@ -1027,9 +1022,8 @@ abstract class JdbcDatabase implements Database { while(rs.next()) { GroupId id = new GroupId(rs.getBytes(1)); String name = rs.getString(2); - boolean restricted = rs.getBoolean(3); - byte[] key = rs.getBytes(4); - subs.add(groupFactory.createGroup(id, name, restricted, key)); + byte[] publicKey = rs.getBytes(3); + subs.add(groupFactory.createGroup(id, name, publicKey)); } rs.close(); ps.close(); @@ -1389,17 +1383,14 @@ abstract class JdbcDatabase implements Database { ps.close(); // Store the new subscriptions sql = "INSERT INTO contactSubscriptions" - + "(contactId, groupId, groupName, restricted, groupKey)" - + " VALUES (?, ?, ?, ?, ?)"; + + " (contactId, groupId, groupName, groupKey)" + + " VALUES (?, ?, ?, ?)"; ps = txn.prepareStatement(sql); ps.setInt(1, c.getInt()); for(Group g : subs) { ps.setBytes(2, g.getId().getBytes()); ps.setString(3, g.getName()); - ps.setBoolean(4, g.isRestricted()); - if(g.isRestricted()) - ps.setBytes(5, g.getPublicKey().getEncoded()); - else ps.setBytes(5, g.getSalt()); + ps.setBytes(4, g.getPublicKey()); ps.addBatch(); } int[] rowsAffectedArray = ps.executeBatch(); diff --git a/components/net/sf/briar/protocol/GroupFactoryImpl.java b/components/net/sf/briar/protocol/GroupFactoryImpl.java index f704a31e3..f1d85b7fa 100644 --- a/components/net/sf/briar/protocol/GroupFactoryImpl.java +++ b/components/net/sf/briar/protocol/GroupFactoryImpl.java @@ -1,34 +1,12 @@ package net.sf.briar.protocol; -import java.security.PublicKey; -import java.security.spec.InvalidKeySpecException; - -import net.sf.briar.api.crypto.CryptoComponent; -import net.sf.briar.api.crypto.KeyParser; import net.sf.briar.api.protocol.Group; import net.sf.briar.api.protocol.GroupFactory; import net.sf.briar.api.protocol.GroupId; -import com.google.inject.Inject; - class GroupFactoryImpl implements GroupFactory { - private final KeyParser keyParser; - - @Inject - GroupFactoryImpl(CryptoComponent crypto) { - keyParser = crypto.getKeyParser(); - } - - public Group createGroup(GroupId id, String name, boolean restricted, - byte[] saltOrKey) { - if(restricted) { - try { - PublicKey key = keyParser.parsePublicKey(saltOrKey); - return new GroupImpl(id, name, key); - } catch(InvalidKeySpecException e) { - throw new IllegalArgumentException(e); - } - } else return new GroupImpl(id, name, saltOrKey); + public Group createGroup(GroupId id, String name, byte[] publicKey) { + return new GroupImpl(id, name, publicKey); } } diff --git a/components/net/sf/briar/protocol/GroupImpl.java b/components/net/sf/briar/protocol/GroupImpl.java index 79afd7993..34fe24e6d 100644 --- a/components/net/sf/briar/protocol/GroupImpl.java +++ b/components/net/sf/briar/protocol/GroupImpl.java @@ -1,7 +1,6 @@ package net.sf.briar.protocol; import java.io.IOException; -import java.security.PublicKey; import net.sf.briar.api.protocol.Group; import net.sf.briar.api.protocol.GroupId; @@ -12,21 +11,12 @@ class GroupImpl implements Group { private final GroupId id; private final String name; - private final byte[] salt; - private final PublicKey publicKey; + private final byte[] publicKey; - GroupImpl(GroupId id, String name, byte[] salt) { - this.id = id; - this.name = name; - this.salt = salt; - publicKey = null; - } - - GroupImpl(GroupId id, String name, PublicKey publicKey) { + GroupImpl(GroupId id, String name, byte[] publicKey) { this.id = id; this.name = name; this.publicKey = publicKey; - salt = null; } public GroupId getId() { @@ -37,24 +27,15 @@ class GroupImpl implements Group { return name; } - public boolean isRestricted() { - return salt == null; - } - - public byte[] getSalt() { - return salt; - } - - public PublicKey getPublicKey() { + public byte[] getPublicKey() { return publicKey; } public void writeTo(Writer w) throws IOException { w.writeUserDefinedTag(Tags.GROUP); w.writeString(name); - w.writeBoolean(isRestricted()); - if(salt == null) w.writeBytes(publicKey.getEncoded()); - else w.writeBytes(salt); + if(publicKey == null) w.writeNull(); + else w.writeBytes(publicKey); } @Override diff --git a/components/net/sf/briar/protocol/GroupReader.java b/components/net/sf/briar/protocol/GroupReader.java index 8bf54f897..61b4721ef 100644 --- a/components/net/sf/briar/protocol/GroupReader.java +++ b/components/net/sf/briar/protocol/GroupReader.java @@ -28,11 +28,12 @@ class GroupReader implements ObjectReader { r.addConsumer(digesting); r.readUserDefinedTag(Tags.GROUP); String name = r.readString(); - boolean restricted = r.readBoolean(); - byte[] saltOrKey = r.readBytes(); + byte[] publicKey = null; + if(r.hasNull()) r.readNull(); + else publicKey = r.readBytes(); r.removeConsumer(digesting); // Build and return the group GroupId id = new GroupId(messageDigest.digest()); - return groupFactory.createGroup(id, name, restricted, saltOrKey); + return groupFactory.createGroup(id, name, publicKey); } } diff --git a/test/net/sf/briar/db/H2DatabaseTest.java b/test/net/sf/briar/db/H2DatabaseTest.java index 25fa730d4..2706af2b7 100644 --- a/test/net/sf/briar/db/H2DatabaseTest.java +++ b/test/net/sf/briar/db/H2DatabaseTest.java @@ -77,8 +77,7 @@ public class H2DatabaseTest extends TestCase { random.nextBytes(raw); message = new TestMessage(messageId, MessageId.NONE, groupId, authorId, timestamp, raw); - group = groupFactory.createGroup(groupId, "Group name", false, - TestUtils.getRandomId()); + group = groupFactory.createGroup(groupId, "Group name", null); } @Before @@ -534,7 +533,7 @@ public class H2DatabaseTest extends TestCase { MessageId childId3 = new MessageId(TestUtils.getRandomId()); GroupId groupId1 = new GroupId(TestUtils.getRandomId()); Group group1 = groupFactory.createGroup(groupId1, "Another group name", - false, TestUtils.getRandomId()); + null); Message child1 = new TestMessage(childId1, messageId, groupId, authorId, timestamp, raw); Message child2 = new TestMessage(childId2, messageId, groupId, @@ -759,7 +758,7 @@ public class H2DatabaseTest extends TestCase { public void testUpdateSubscriptions() throws DbException { GroupId groupId1 = new GroupId(TestUtils.getRandomId()); Group group1 = groupFactory.createGroup(groupId1, "Another group name", - false, TestUtils.getRandomId()); + null); Database db = open(false); Connection txn = db.startTransaction(); @@ -784,7 +783,7 @@ public class H2DatabaseTest extends TestCase { throws DbException { GroupId groupId1 = new GroupId(TestUtils.getRandomId()); Group group1 = groupFactory.createGroup(groupId1, "Another group name", - false, TestUtils.getRandomId()); + null); Database db = open(false); Connection txn = db.startTransaction(); diff --git a/test/net/sf/briar/protocol/FileReadWriteTest.java b/test/net/sf/briar/protocol/FileReadWriteTest.java index 62ad02a7f..e06f1156f 100644 --- a/test/net/sf/briar/protocol/FileReadWriteTest.java +++ b/test/net/sf/briar/protocol/FileReadWriteTest.java @@ -64,7 +64,6 @@ public class FileReadWriteTest extends TestCase { private final ReaderFactory readerFactory; private final WriterFactory writerFactory; private final PacketWriterFactory packetWriterFactory; - private final CryptoComponent crypto; private final Signature signature; private final MessageDigest messageDigest, batchDigest; private final KeyParser keyParser; @@ -79,7 +78,7 @@ public class FileReadWriteTest extends TestCase { readerFactory = i.getInstance(ReaderFactory.class); writerFactory = i.getInstance(WriterFactory.class); packetWriterFactory = i.getInstance(PacketWriterFactory.class); - crypto = i.getInstance(CryptoComponent.class); + CryptoComponent crypto = i.getInstance(CryptoComponent.class); keyParser = crypto.getKeyParser(); signature = crypto.getSignature(); messageDigest = crypto.getMessageDigest(); @@ -94,8 +93,7 @@ public class FileReadWriteTest extends TestCase { // Create a test group, then write and read it to calculate its ID GroupFactory groupFactory = i.getInstance(GroupFactory.class); Group noId = groupFactory.createGroup( - new GroupId(new byte[UniqueId.LENGTH]), "Group name", false, - TestUtils.getRandomId()); + new GroupId(new byte[UniqueId.LENGTH]), "Group name", null); ByteArrayOutputStream out = new ByteArrayOutputStream(); Writer w = writerFactory.createWriter(out); noId.writeTo(w); @@ -147,7 +145,7 @@ public class FileReadWriteTest extends TestCase { ObjectReader batchReader = new BatchReader(batchDigest, messageReader, new BatchFactoryImpl()); ObjectReader groupReader = new GroupReader(batchDigest, - new GroupFactoryImpl(crypto)); + new GroupFactoryImpl()); ObjectReader subscriptionReader = new SubscriptionReader(groupReader, new SubscriptionFactoryImpl()); ObjectReader transportReader =