Generate handshake keys when creating local author.

This commit is contained in:
akwizgran
2019-04-18 18:09:09 +01:00
parent f0a3130bf3
commit 75776eb7de
10 changed files with 50 additions and 124 deletions

View File

@@ -1,6 +1,7 @@
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) {

View File

@@ -1,7 +1,5 @@
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;
}

View File

@@ -1,9 +1,5 @@
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