Require a label for signing

This adds a sign() and a verify() method to the CryptoComponent
that take a mandatory label argument to ensure that signatures can't be
repurposed.
This commit is contained in:
Torsten Grote
2016-11-17 16:36:51 -02:00
parent 9b09b64ad3
commit c86d971166
20 changed files with 158 additions and 107 deletions

View File

@@ -38,6 +38,8 @@ import static org.briarproject.api.blogs.BlogConstants.KEY_ORIGINAL_PARENT_MSG_I
import static org.briarproject.api.blogs.BlogConstants.KEY_PARENT_MSG_ID;
import static org.briarproject.api.blogs.BlogConstants.KEY_PUBLIC_KEY;
import static org.briarproject.api.blogs.BlogConstants.KEY_READ;
import static org.briarproject.api.blogs.BlogPostFactory.SIGNING_LABEL_COMMENT;
import static org.briarproject.api.blogs.BlogPostFactory.SIGNING_LABEL_POST;
import static org.briarproject.api.blogs.MessageType.COMMENT;
import static org.briarproject.api.blogs.MessageType.POST;
import static org.briarproject.api.blogs.MessageType.WRAPPED_COMMENT;
@@ -101,7 +103,7 @@ public class BlogPostValidatorTest extends BriarTestCase {
BdfList signed =
BdfList.of(blog.getId(), message.getTimestamp(), body);
expectCrypto(signed, sigBytes);
expectCrypto(SIGNING_LABEL_POST, signed, sigBytes);
final BdfDictionary result =
validator.validateMessage(message, group, m).getDictionary();
@@ -143,7 +145,7 @@ public class BlogPostValidatorTest extends BriarTestCase {
BdfList signed =
BdfList.of(blog.getId(), message.getTimestamp(), comment,
pOriginalId, currentId);
expectCrypto(signed, sigBytes);
expectCrypto(SIGNING_LABEL_COMMENT, signed, sigBytes);
final BdfDictionary result =
validator.validateMessage(message, group, m).getDictionary();
@@ -170,7 +172,7 @@ public class BlogPostValidatorTest extends BriarTestCase {
BdfList signed =
BdfList.of(blog.getId(), message.getTimestamp(), null,
originalId, currentId);
expectCrypto(signed, sigBytes);
expectCrypto(SIGNING_LABEL_COMMENT, signed, sigBytes);
final BdfDictionary result =
validator.validateMessage(message, group, m).getDictionary();
@@ -189,7 +191,7 @@ public class BlogPostValidatorTest extends BriarTestCase {
BdfList signed =
BdfList.of(blog.getId(), message.getTimestamp(), body);
expectCrypto(signed, sigBytes);
expectCrypto(SIGNING_LABEL_POST, signed, sigBytes);
final BdfList originalList = BdfList.of(POST.getInt(), body, sigBytes);
final byte[] originalBody = TestUtils.getRandomBytes(42);
@@ -228,7 +230,7 @@ public class BlogPostValidatorTest extends BriarTestCase {
BdfList signed = BdfList.of(blog.getId(), message.getTimestamp(),
comment, originalId, oldId);
expectCrypto(signed, sigBytes);
expectCrypto(SIGNING_LABEL_COMMENT, signed, sigBytes);
final BdfList originalList = BdfList.of(COMMENT.getInt(), comment,
originalId, oldId, sigBytes);
@@ -256,13 +258,13 @@ public class BlogPostValidatorTest extends BriarTestCase {
context.assertIsSatisfied();
}
private void expectCrypto(final BdfList signed, final byte[] sig)
throws IOException, GeneralSecurityException {
private void expectCrypto(final String label, final BdfList signed,
final byte[] sig) throws IOException, GeneralSecurityException {
context.checking(new Expectations() {{
oneOf(blogFactory).parseBlog(group);
will(returnValue(blog));
oneOf(clientHelper)
.verifySignature(sig, author.getPublicKey(), signed);
.verifySignature(label, sig, author.getPublicKey(), signed);
}});
}

View File

@@ -1,6 +1,7 @@
package org.briarproject.clients;
import org.briarproject.BriarTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.crypto.CryptoComponent;
@@ -70,6 +71,7 @@ public class ClientHelperImplTest extends BriarTestCase {
new Message(messageId, groupId, timestamp, rawMessage);
private final Metadata metadata = new Metadata();
private final BdfList list = BdfList.of("Sign this!", getRandomBytes(42));
private final String label = TestUtils.getRandomString(5);
public ClientHelperImplTest() {
clientHelper =
@@ -290,19 +292,16 @@ public class ClientHelperImplTest extends BriarTestCase {
final byte[] bytes = expectToByteArray(list);
context.checking(new Expectations() {{
oneOf(cryptoComponent).getSignature();
will(returnValue(signature));
oneOf(cryptoComponent).getSignatureKeyParser();
will(returnValue(keyParser));
oneOf(keyParser).parsePrivateKey(privateKeyBytes);
will(returnValue(privateKey));
oneOf(signature).initSign(privateKey);
oneOf(signature).update(bytes);
oneOf(signature).sign();
oneOf(cryptoComponent).sign(label, bytes, privateKey);
will(returnValue(signed));
}});
assertArrayEquals(signed, clientHelper.sign(list, privateKeyBytes));
assertArrayEquals(signed,
clientHelper.sign(label, list, privateKeyBytes));
context.assertIsSatisfied();
}
@@ -317,15 +316,11 @@ public class ClientHelperImplTest extends BriarTestCase {
will(returnValue(keyParser));
oneOf(keyParser).parsePublicKey(publicKeyBytes);
will(returnValue(publicKey));
oneOf(cryptoComponent).getSignature();
will(returnValue(signature));
oneOf(signature).initVerify(publicKey);
oneOf(signature).update(bytes);
oneOf(signature).verify(rawMessage);
oneOf(cryptoComponent).verify(label, bytes, publicKey, rawMessage);
will(returnValue(true));
}});
clientHelper.verifySignature(rawMessage, publicKeyBytes, list);
clientHelper.verifySignature(label, rawMessage, publicKeyBytes, list);
context.assertIsSatisfied();
}
@@ -340,16 +335,13 @@ public class ClientHelperImplTest extends BriarTestCase {
will(returnValue(keyParser));
oneOf(keyParser).parsePublicKey(publicKeyBytes);
will(returnValue(publicKey));
oneOf(cryptoComponent).getSignature();
will(returnValue(signature));
oneOf(signature).initVerify(publicKey);
oneOf(signature).update(bytes);
oneOf(signature).verify(rawMessage);
oneOf(cryptoComponent).verify(label, bytes, publicKey, rawMessage);
will(returnValue(false));
}});
try {
clientHelper.verifySignature(rawMessage, publicKeyBytes, list);
clientHelper
.verifySignature(label, rawMessage, publicKeyBytes, list);
fail();
} catch (GeneralSecurityException e) {
// expected

View File

@@ -19,6 +19,7 @@ import java.security.GeneralSecurityException;
import java.util.Collection;
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENGTH;
import static org.briarproject.api.forum.ForumPostFactory.SIGNING_LABEL_POST;
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;
@@ -70,8 +71,8 @@ public class ForumPostValidatorTest extends ValidatorTestCase {
context.checking(new Expectations() {{
oneOf(authorFactory).createAuthor(authorName, authorPublicKey);
will(returnValue(author));
oneOf(clientHelper).verifySignature(signature, authorPublicKey,
signedWithoutParent);
oneOf(clientHelper).verifySignature(SIGNING_LABEL_POST, signature,
authorPublicKey, signedWithoutParent);
}});
ForumPostValidator v = new ForumPostValidator(authorFactory,
@@ -179,8 +180,8 @@ public class ForumPostValidatorTest extends ValidatorTestCase {
context.checking(new Expectations() {{
oneOf(authorFactory).createAuthor(shortAuthorName, authorPublicKey);
will(returnValue(shortNameAuthor));
oneOf(clientHelper).verifySignature(signature, authorPublicKey,
signedWithShortNameAuthor);
oneOf(clientHelper).verifySignature(SIGNING_LABEL_POST, signature,
authorPublicKey, signedWithShortNameAuthor);
}});
ForumPostValidator v = new ForumPostValidator(authorFactory,
@@ -267,8 +268,8 @@ public class ForumPostValidatorTest extends ValidatorTestCase {
context.checking(new Expectations() {{
oneOf(authorFactory).createAuthor(authorName, authorPublicKey);
will(returnValue(author));
oneOf(clientHelper).verifySignature(signature, authorPublicKey,
signedWithShortContent);
oneOf(clientHelper).verifySignature(SIGNING_LABEL_POST, signature,
authorPublicKey, signedWithShortContent);
}});
ForumPostValidator v = new ForumPostValidator(authorFactory,
@@ -342,8 +343,8 @@ public class ForumPostValidatorTest extends ValidatorTestCase {
context.checking(new Expectations() {{
oneOf(authorFactory).createAuthor(authorName, authorPublicKey);
will(returnValue(author));
oneOf(clientHelper).verifySignature(signature, authorPublicKey,
signedWithParent);
oneOf(clientHelper).verifySignature(SIGNING_LABEL_POST, signature,
authorPublicKey, signedWithParent);
will(throwException(new FormatException()));
}});
@@ -359,8 +360,8 @@ public class ForumPostValidatorTest extends ValidatorTestCase {
context.checking(new Expectations() {{
oneOf(authorFactory).createAuthor(authorName, authorPublicKey);
will(returnValue(author));
oneOf(clientHelper).verifySignature(signature, authorPublicKey,
signedWithParent);
oneOf(clientHelper).verifySignature(SIGNING_LABEL_POST, signature,
authorPublicKey, signedWithParent);
will(throwException(new GeneralSecurityException()));
}});