Commit 75776eb7 authored by akwizgran's avatar akwizgran

Generate handshake keys when creating local author.

parent f0a3130b
......@@ -18,14 +18,7 @@ public interface AuthorFactory {
/**
* Creates a local author with the current format version and the given
* name and keys.
* name.
*/
LocalAuthor createLocalAuthor(String name, byte[] publicKey,
byte[] privateKey);
/**
* Creates a local author with the given format version, name and keys.
*/
LocalAuthor createLocalAuthor(int formatVersion, String name,
byte[] publicKey, byte[] privateKey);
LocalAuthor createLocalAuthor(String name, boolean handshakeKeys);
}
package org.briarproject.bramble.identity;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.KeyPair;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.identity.AuthorId;
......@@ -43,17 +44,21 @@ class AuthorFactoryImpl implements AuthorFactory {
}
@Override
public LocalAuthor createLocalAuthor(String name, byte[] publicKey,
byte[] privateKey) {
return createLocalAuthor(FORMAT_VERSION, name, publicKey, privateKey);
}
@Override
public LocalAuthor createLocalAuthor(int formatVersion, String name,
byte[] publicKey, byte[] privateKey) {
AuthorId id = getId(formatVersion, name, publicKey);
return new LocalAuthor(id, formatVersion, name, publicKey, privateKey,
clock.currentTimeMillis());
public LocalAuthor createLocalAuthor(String name, boolean handshakeKeys) {
KeyPair signatureKeyPair = crypto.generateSignatureKeyPair();
byte[] sigPub = signatureKeyPair.getPublic().getEncoded();
byte[] sigPriv = signatureKeyPair.getPrivate().getEncoded();
AuthorId id = getId(FORMAT_VERSION, name, sigPub);
if (handshakeKeys) {
KeyPair handshakeKeyPair = crypto.generateAgreementKeyPair();
byte[] handPub = handshakeKeyPair.getPublic().getEncoded();
byte[] handPriv = handshakeKeyPair.getPrivate().getEncoded();
return new LocalAuthor(id, FORMAT_VERSION, name, sigPub, sigPriv,
handPub, handPriv, clock.currentTimeMillis());
} else {
return new LocalAuthor(id, FORMAT_VERSION, name, sigPub, sigPriv,
clock.currentTimeMillis());
}
}
private AuthorId getId(int formatVersion, String name, byte[] publicKey) {
......
package org.briarproject.bramble.identity;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.KeyPair;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
......@@ -27,7 +25,6 @@ class IdentityManagerImpl implements IdentityManager {
Logger.getLogger(IdentityManagerImpl.class.getName());
private final DatabaseComponent db;
private final CryptoComponent crypto;
private final AuthorFactory authorFactory;
// The local author is immutable so we can cache it
......@@ -35,21 +32,15 @@ class IdentityManagerImpl implements IdentityManager {
private volatile LocalAuthor cachedAuthor;
@Inject
IdentityManagerImpl(DatabaseComponent db, CryptoComponent crypto,
AuthorFactory authorFactory) {
IdentityManagerImpl(DatabaseComponent db, AuthorFactory authorFactory) {
this.db = db;
this.crypto = crypto;
this.authorFactory = authorFactory;
}
@Override
public LocalAuthor createLocalAuthor(String name) {
long start = now();
KeyPair keyPair = crypto.generateSignatureKeyPair();
byte[] publicKey = keyPair.getPublic().getEncoded();
byte[] privateKey = keyPair.getPrivate().getEncoded();
LocalAuthor localAuthor = authorFactory.createLocalAuthor(name,
publicKey, privateKey);
LocalAuthor localAuthor = authorFactory.createLocalAuthor(name, true);
logDuration(LOG, "Creating local author", start);
return localAuthor;
}
......
package org.briarproject.bramble.identity;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.KeyPair;
import org.briarproject.bramble.api.crypto.PrivateKey;
import org.briarproject.bramble.api.crypto.PublicKey;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
......@@ -12,56 +8,30 @@ import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.DbExpectations;
import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import static java.util.Collections.singletonList;
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
import static org.junit.Assert.assertEquals;
public class IdentityManagerImplTest extends BrambleMockTestCase {
private final DatabaseComponent db = context.mock(DatabaseComponent.class);
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
private final AuthorFactory authorFactory =
context.mock(AuthorFactory.class);
private final PublicKey publicKey = context.mock(PublicKey.class);
private final PrivateKey privateKey = context.mock(PrivateKey.class);
private final Transaction txn = new Transaction(null, false);
private final LocalAuthor localAuthor = getLocalAuthor();
private final Collection<LocalAuthor> localAuthors =
Collections.singletonList(localAuthor);
private final String authorName = localAuthor.getName();
private final KeyPair keyPair = new KeyPair(publicKey, privateKey);
private final byte[] publicKeyBytes = localAuthor.getPublicKey();
private final byte[] privateKeyBytes = localAuthor.getPrivateKey();
singletonList(localAuthor);
private IdentityManager identityManager;
@Before
public void setUp() {
identityManager = new IdentityManagerImpl(db, crypto, authorFactory);
}
@Test
public void testCreateLocalAuthor() {
context.checking(new Expectations() {{
oneOf(crypto).generateSignatureKeyPair();
will(returnValue(keyPair));
oneOf(publicKey).getEncoded();
will(returnValue(publicKeyBytes));
oneOf(privateKey).getEncoded();
will(returnValue(privateKeyBytes));
oneOf(authorFactory).createLocalAuthor(authorName,
publicKeyBytes, privateKeyBytes);
will(returnValue(localAuthor));
}});
assertEquals(localAuthor,
identityManager.createLocalAuthor(authorName));
identityManager = new IdentityManagerImpl(db, authorFactory);
}
@Test
......
......@@ -4,8 +4,6 @@ import com.rometools.rome.feed.synd.SyndFeed;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.KeyPair;
import org.briarproject.bramble.api.data.BdfDictionary;
import org.briarproject.bramble.api.data.BdfEntry;
import org.briarproject.bramble.api.data.BdfList;
......@@ -32,17 +30,14 @@ import static org.briarproject.briar.api.feed.FeedConstants.KEY_FEED_URL;
class FeedFactoryImpl implements FeedFactory {
private final CryptoComponent cryptoComponent;
private final AuthorFactory authorFactory;
private final BlogFactory blogFactory;
private final ClientHelper clientHelper;
private final Clock clock;
@Inject
FeedFactoryImpl(CryptoComponent cryptoComponent,
AuthorFactory authorFactory, BlogFactory blogFactory,
FeedFactoryImpl(AuthorFactory authorFactory, BlogFactory blogFactory,
ClientHelper clientHelper, Clock clock) {
this.cryptoComponent = cryptoComponent;
this.authorFactory = authorFactory;
this.blogFactory = blogFactory;
this.clientHelper = clientHelper;
......@@ -55,10 +50,7 @@ class FeedFactoryImpl implements FeedFactory {
if (title == null) title = "RSS";
else title = StringUtils.truncateUtf8(title, MAX_AUTHOR_NAME_LENGTH);
KeyPair keyPair = cryptoComponent.generateSignatureKeyPair();
LocalAuthor localAuthor = authorFactory.createLocalAuthor(title,
keyPair.getPublic().getEncoded(),
keyPair.getPrivate().getEncoded());
LocalAuthor localAuthor = authorFactory.createLocalAuthor(title, false);
Blog blog = blogFactory.createFeedBlog(localAuthor);
long added = clock.currentTimeMillis();
......@@ -80,9 +72,9 @@ class FeedFactoryImpl implements FeedFactory {
BdfList authorList = d.getList(KEY_FEED_AUTHOR);
byte[] privateKey = d.getRaw(KEY_FEED_PRIVATE_KEY);
Author author = clientHelper.parseAndValidateAuthor(authorList);
LocalAuthor localAuthor = authorFactory.createLocalAuthor(
LocalAuthor localAuthor = new LocalAuthor(author.getId(),
author.getFormatVersion(), author.getName(),
author.getPublicKey(), privateKey);
author.getPublicKey(), privateKey, 0);
Blog blog = blogFactory.createFeedBlog(localAuthor);
String desc = d.getOptionalString(KEY_FEED_DESC);
......
......@@ -5,8 +5,6 @@ import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.contact.Contact;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.KeyPair;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.data.BdfDictionary;
import org.briarproject.bramble.api.db.DatabaseComponent;
......@@ -74,7 +72,6 @@ public class TestDataCreatorImpl implements TestDataCreator {
private final ClientHelper clientHelper;
private final MessageTracker messageTracker;
private final BlogPostFactory blogPostFactory;
private final CryptoComponent cryptoComponent;
private final DatabaseComponent db;
private final IdentityManager identityManager;
......@@ -94,9 +91,8 @@ public class TestDataCreatorImpl implements TestDataCreator {
TestDataCreatorImpl(AuthorFactory authorFactory, Clock clock,
PrivateMessageFactory privateMessageFactory,
ClientHelper clientHelper, MessageTracker messageTracker,
BlogPostFactory blogPostFactory, CryptoComponent cryptoComponent,
DatabaseComponent db, IdentityManager identityManager,
ContactManager contactManager,
BlogPostFactory blogPostFactory, DatabaseComponent db,
IdentityManager identityManager, ContactManager contactManager,
TransportPropertyManager transportPropertyManager,
MessagingManager messagingManager, BlogManager blogManager,
ForumManager forumManager, @IoExecutor Executor ioExecutor) {
......@@ -106,7 +102,6 @@ public class TestDataCreatorImpl implements TestDataCreator {
this.clientHelper = clientHelper;
this.messageTracker = messageTracker;
this.blogPostFactory = blogPostFactory;
this.cryptoComponent = cryptoComponent;
this.db = db;
this.identityManager = identityManager;
this.contactManager = contactManager;
......@@ -150,14 +145,14 @@ public class TestDataCreatorImpl implements TestDataCreator {
List<Contact> contacts = new ArrayList<>(numContacts);
LocalAuthor localAuthor = identityManager.getLocalAuthor();
for (int i = 0; i < numContacts; i++) {
LocalAuthor author = getRandomAuthor();
Contact contact = addContact(localAuthor.getId(), author);
LocalAuthor remote = getRandomAuthor();
Contact contact = addContact(localAuthor.getId(), remote);
contacts.add(contact);
}
return contacts;
}
private Contact addContact(AuthorId localAuthorId, LocalAuthor author)
private Contact addContact(AuthorId localAuthorId, LocalAuthor remote)
throws DbException {
// prepare to add contact
......@@ -173,7 +168,7 @@ public class TestDataCreatorImpl implements TestDataCreator {
Transaction txn = db.startTransaction(false);
try {
ContactId contactId = contactManager
.addContact(txn, author, localAuthorId, secretKey,
.addContact(txn, remote, localAuthorId, secretKey,
timestamp, true, verified, true);
if (random.nextBoolean()) {
contactManager
......@@ -187,24 +182,18 @@ public class TestDataCreatorImpl implements TestDataCreator {
}
if (LOG.isLoggable(INFO)) {
LOG.info("Added contact " + author.getName());
LOG.info("Added contact " + remote.getName());
LOG.info("with transport properties: " + props.toString());
}
localAuthors.put(contact, author);
localAuthors.put(contact, remote);
return contact;
}
@Override
public Contact addContact(String name) throws DbException {
LocalAuthor localAuthor = identityManager.getLocalAuthor();
return addContact(localAuthor.getId(), getAuthor(name));
}
private LocalAuthor getAuthor(String name) {
KeyPair keyPair = cryptoComponent.generateSignatureKeyPair();
byte[] publicKey = keyPair.getPublic().getEncoded();
byte[] privateKey = keyPair.getPrivate().getEncoded();
return authorFactory.createLocalAuthor(name, publicKey, privateKey);
LocalAuthor remote = authorFactory.createLocalAuthor(name, false);
return addContact(localAuthor.getId(), remote);
}
private String getRandomAuthorName() {
......@@ -213,7 +202,7 @@ public class TestDataCreatorImpl implements TestDataCreator {
}
private LocalAuthor getRandomAuthor() {
return getAuthor(getRandomAuthorName());
return authorFactory.createLocalAuthor(getRandomAuthorName(), false);
}
private SecretKey getSecretKey() {
......
......@@ -21,9 +21,7 @@ import java.util.Iterator;
import static junit.framework.Assert.assertNotNull;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorInfo.Status.NONE;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.blog.MessageType.COMMENT;
import static org.briarproject.briar.api.blog.MessageType.POST;
......@@ -51,9 +49,7 @@ public class BlogManagerIntegrationTest
author0 = identityManager0.getLocalAuthor();
author1 = identityManager1.getLocalAuthor();
rssAuthor = c0.getAuthorFactory().createLocalAuthor(
getRandomString(MAX_AUTHOR_NAME_LENGTH),
getRandomBytes(MAX_PUBLIC_KEY_LENGTH),
getRandomBytes(123));
getRandomString(MAX_AUTHOR_NAME_LENGTH), false);
blogManager0 = c0.getBlogManager();
blogManager1 = c1.getBlogManager();
......
......@@ -58,10 +58,8 @@ public class IntroductionCryptoIntegrationTest extends BrambleTestCase {
crypto = new IntroductionCryptoImpl(cryptoComponent, clientHelper);
introducer = getRealAuthor(authorFactory);
LocalAuthor introducee1 =
getRealLocalAuthor(cryptoComponent, authorFactory);
LocalAuthor introducee2 =
getRealLocalAuthor(cryptoComponent, authorFactory);
LocalAuthor introducee1 = getRealLocalAuthor(authorFactory);
LocalAuthor introducee2 = getRealLocalAuthor(authorFactory);
boolean isAlice =
crypto.isAlice(introducee1.getId(), introducee2.getId());
alice = isAlice ? introducee1 : introducee2;
......
......@@ -2,7 +2,6 @@ package org.briarproject.briar.messaging;
import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.PrivateKey;
import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.sync.GroupId;
......@@ -70,12 +69,8 @@ public class MessageSizeIntegrationTest extends BriarTestCase {
@Test
public void testForumPostFitsIntoRecord() throws Exception {
// Create a maximum-length author
int formatVersion = Integer.MAX_VALUE;
String authorName = getRandomString(MAX_AUTHOR_NAME_LENGTH);
byte[] authorPublic = new byte[MAX_PUBLIC_KEY_LENGTH];
PrivateKey privateKey = crypto.generateSignatureKeyPair().getPrivate();
LocalAuthor author = authorFactory.createLocalAuthor(formatVersion,
authorName, authorPublic, privateKey.getEncoded());
LocalAuthor author = authorFactory.createLocalAuthor(authorName, false);
// Create a maximum-length forum post
GroupId groupId = new GroupId(getRandomId());
long timestamp = Long.MAX_VALUE;
......
package org.briarproject.briar.test;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.KeyPair;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorFactory;
......@@ -10,6 +8,7 @@ import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.briar.api.client.MessageTracker;
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
......@@ -27,23 +26,21 @@ public class BriarTestUtils {
}
public static void assertGroupCount(MessageTracker tracker, GroupId g,
long msgCount, long unreadCount) throws DbException {
long msgCount, long unreadCount) throws DbException {
GroupCount c1 = tracker.getGroupCount(g);
assertEquals(msgCount, c1.getMsgCount());
assertEquals(unreadCount, c1.getUnreadCount());
}
public static Author getRealAuthor(AuthorFactory authorFactory) {
return authorFactory.createAuthor(getRandomString(5),
getRandomBytes(MAX_PUBLIC_KEY_LENGTH));
String name = getRandomString(MAX_AUTHOR_NAME_LENGTH);
byte[] publicKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
return authorFactory.createAuthor(name, publicKey);
}
public static LocalAuthor getRealLocalAuthor(
CryptoComponent cryptoComponent, AuthorFactory authorFactory) {
KeyPair keyPair = cryptoComponent.generateSignatureKeyPair();
return authorFactory.createLocalAuthor(getRandomString(5),
keyPair.getPublic().getEncoded(),
keyPair.getPrivate().getEncoded());
public static LocalAuthor getRealLocalAuthor(AuthorFactory authorFactory) {
String name = getRandomString(MAX_AUTHOR_NAME_LENGTH);
return authorFactory.createLocalAuthor(name, false);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment