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).
This commit is contained in:
akwizgran
2011-07-25 20:11:32 +01:00
parent b1f27757df
commit 586d1739ae
8 changed files with 34 additions and 102 deletions

View File

@@ -40,8 +40,7 @@ abstract class JdbcDatabase implements Database<Connection> {
"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<Connection> {
+ " (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<Connection> {
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<Connection> {
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<Connection> {
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<Connection> {
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<Connection> {
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<Connection> {
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();

View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -28,11 +28,12 @@ class GroupReader implements ObjectReader<Group> {
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);
}
}