mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 04:18:53 +01:00
Use the BouncyCastle provider so we can be sure we won't get
NoSuchAlgorithmExceptions. Key generation is *slow* - I guess that's a good sign. ;-)
This commit is contained in:
66
components/net/sf/briar/crypto/CryptoComponentImpl.java
Normal file
66
components/net/sf/briar/crypto/CryptoComponentImpl.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package net.sf.briar.crypto;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.Security;
|
||||
import java.security.Signature;
|
||||
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.crypto.KeyParser;
|
||||
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
|
||||
public class CryptoComponentImpl implements CryptoComponent {
|
||||
|
||||
private static final String PROVIDER = "BC";
|
||||
private static final String DIGEST_ALGO = "SHA-256";
|
||||
private static final String KEY_PAIR_ALGO = "RSA";
|
||||
private static final String SIGNATURE_ALGO = "SHA256withRSA";
|
||||
|
||||
private final KeyParser keyParser;
|
||||
private final KeyPairGenerator keyPairGenerator;
|
||||
|
||||
CryptoComponentImpl() {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
try {
|
||||
keyParser = new KeyParserImpl(KEY_PAIR_ALGO, PROVIDER);
|
||||
keyPairGenerator = KeyPairGenerator.getInstance(KEY_PAIR_ALGO,
|
||||
PROVIDER);
|
||||
} catch(NoSuchAlgorithmException impossible) {
|
||||
throw new RuntimeException(impossible);
|
||||
} catch(NoSuchProviderException impossible) {
|
||||
throw new RuntimeException(impossible);
|
||||
}
|
||||
}
|
||||
|
||||
public KeyPair generateKeyPair() {
|
||||
return keyPairGenerator.generateKeyPair();
|
||||
}
|
||||
|
||||
public KeyParser getKeyParser() {
|
||||
return keyParser;
|
||||
}
|
||||
|
||||
public MessageDigest getMessageDigest() {
|
||||
try {
|
||||
return MessageDigest.getInstance(DIGEST_ALGO, PROVIDER);
|
||||
} catch(NoSuchAlgorithmException impossible) {
|
||||
throw new RuntimeException(impossible);
|
||||
} catch(NoSuchProviderException impossible) {
|
||||
throw new RuntimeException(impossible);
|
||||
}
|
||||
}
|
||||
|
||||
public Signature getSignature() {
|
||||
try {
|
||||
return Signature.getInstance(SIGNATURE_ALGO, PROVIDER);
|
||||
} catch(NoSuchAlgorithmException impossible) {
|
||||
throw new RuntimeException(impossible);
|
||||
} catch(NoSuchProviderException impossible) {
|
||||
throw new RuntimeException(impossible);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,60 +1,14 @@
|
||||
package net.sf.briar.crypto;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Signature;
|
||||
|
||||
import net.sf.briar.api.crypto.KeyParser;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
public class CryptoModule extends AbstractModule {
|
||||
|
||||
private static final String DIGEST_ALGO = "SHA-256";
|
||||
private static final String KEY_PAIR_ALGO = "RSA";
|
||||
private static final String SIGNATURE_ALGO = "SHA256withRSA";
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
try {
|
||||
bind(KeyParser.class).toInstance(new KeyParserImpl(KEY_PAIR_ALGO));
|
||||
} catch(NoSuchAlgorithmException e) {
|
||||
// FIXME: Can modules throw?
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
MessageDigest getMessageDigest() {
|
||||
try {
|
||||
return MessageDigest.getInstance(DIGEST_ALGO);
|
||||
} catch(NoSuchAlgorithmException e) {
|
||||
// FIXME: Providers should not throw
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
Signature getSignature() {
|
||||
try {
|
||||
return Signature.getInstance(SIGNATURE_ALGO);
|
||||
} catch(NoSuchAlgorithmException e) {
|
||||
// FIXME: Providers should not throw
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
KeyPair generateKeyPair() {
|
||||
try {
|
||||
KeyPairGenerator gen = KeyPairGenerator.getInstance(KEY_PAIR_ALGO);
|
||||
return gen.generateKeyPair();
|
||||
} catch(NoSuchAlgorithmException e) {
|
||||
// FIXME: Providers should not throw
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
bind(CryptoComponent.class).to(CryptoComponentImpl.class).in(Singleton.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.sf.briar.crypto;
|
||||
|
||||
import java.security.KeyFactory;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.EncodedKeySpec;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
@@ -13,8 +14,9 @@ class KeyParserImpl implements KeyParser {
|
||||
|
||||
private final KeyFactory keyFactory;
|
||||
|
||||
KeyParserImpl(String algorithm) throws NoSuchAlgorithmException {
|
||||
keyFactory = KeyFactory.getInstance(algorithm);
|
||||
KeyParserImpl(String algorithm, String provider)
|
||||
throws NoSuchAlgorithmException, NoSuchProviderException {
|
||||
keyFactory = KeyFactory.getInstance(algorithm, provider);
|
||||
}
|
||||
|
||||
public PublicKey parsePublicKey(byte[] encodedKey)
|
||||
|
||||
@@ -3,6 +3,7 @@ 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;
|
||||
@@ -15,8 +16,8 @@ class GroupFactoryImpl implements GroupFactory {
|
||||
private final KeyParser keyParser;
|
||||
|
||||
@Inject
|
||||
GroupFactoryImpl(KeyParser keyParser) {
|
||||
this.keyParser = keyParser;
|
||||
GroupFactoryImpl(CryptoComponent crypto) {
|
||||
keyParser = crypto.getKeyParser();
|
||||
}
|
||||
|
||||
public Group createGroup(GroupId id, String name, boolean restricted,
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.security.KeyPair;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.Signature;
|
||||
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.Message;
|
||||
@@ -25,10 +26,9 @@ class MessageEncoderImpl implements MessageEncoder {
|
||||
private final WriterFactory writerFactory;
|
||||
|
||||
@Inject
|
||||
MessageEncoderImpl(Signature signature, MessageDigest messageDigest,
|
||||
WriterFactory writerFactory) {
|
||||
this.signature = signature;
|
||||
this.messageDigest = messageDigest;
|
||||
MessageEncoderImpl(CryptoComponent crypto, WriterFactory writerFactory) {
|
||||
signature = crypto.getSignature();
|
||||
messageDigest = crypto.getMessageDigest();
|
||||
this.writerFactory = writerFactory;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.sf.briar.protocol.writers;
|
||||
import java.io.OutputStream;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.protocol.writers.AckWriter;
|
||||
import net.sf.briar.api.protocol.writers.BatchWriter;
|
||||
import net.sf.briar.api.protocol.writers.PacketWriterFactory;
|
||||
@@ -18,9 +19,9 @@ class PacketWriterFactoryImpl implements PacketWriterFactory {
|
||||
private final WriterFactory writerFactory;
|
||||
|
||||
@Inject
|
||||
PacketWriterFactoryImpl(MessageDigest messageDigest,
|
||||
PacketWriterFactoryImpl(CryptoComponent crypto,
|
||||
WriterFactory writerFactory) {
|
||||
this.messageDigest = messageDigest;
|
||||
messageDigest = crypto.getMessageDigest();
|
||||
this.writerFactory = writerFactory;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user