Start working on EncryptionBuilder API

This commit is contained in:
Paul Schaub 2018-06-04 19:45:18 +02:00
parent cbb3dd642a
commit 4844bf697c
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
5 changed files with 373 additions and 1 deletions

View File

@ -0,0 +1,17 @@
package de.vanitasvitae.crypto.pgpainless;
public class PublicKeyNotFoundException extends Exception {
private static final long serialVersionUID = 1L;
private final long keyId;
public PublicKeyNotFoundException(long keyId) {
super("No PGPPublicKey with id " + Long.toHexString(keyId) + " (" + keyId + ") found.");
this.keyId = keyId;
}
public long getKeyId() {
return keyId;
}
}

View File

@ -0,0 +1,17 @@
package de.vanitasvitae.crypto.pgpainless;
public class SecretKeyNotFoundException extends Exception {
private static final long serialVersionUID = 1L;
private final long keyId;
public SecretKeyNotFoundException(long keyId) {
super("No PGPSecretKey with id " + Long.toHexString(keyId) + " (" + keyId + ") found.");
this.keyId = keyId;
}
public long getKeyId() {
return keyId;
}
}

View File

@ -1,5 +1,197 @@
package de.vanitasvitae.crypto.pgpainless.encryption_signing;
public class EncryptionBuilder {
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import de.vanitasvitae.crypto.pgpainless.PublicKeyNotFoundException;
import de.vanitasvitae.crypto.pgpainless.SecretKeyNotFoundException;
import de.vanitasvitae.crypto.pgpainless.algorithm.CompressionAlgorithm;
import de.vanitasvitae.crypto.pgpainless.algorithm.HashAlgorithm;
import de.vanitasvitae.crypto.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
public class EncryptionBuilder implements EncryptionBuilderInterface {
private OutputStream outputStream;
private final Set<PGPPublicKey> encryptionKeys = new HashSet<>();
private final Set<PGPSecretKey> signingKeys = new HashSet<>();
private SymmetricKeyAlgorithm symmetricKeyAlgorithm;
private HashAlgorithm hashAlgorithm;
private CompressionAlgorithm compressionAlgorithm;
private boolean asciiArmor = false;
@Override
public ToRecipients onOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
return new ToRecipientsImpl();
}
class ToRecipientsImpl implements ToRecipients {
@Override
public WithAlgorithms toRecipient(PGPPublicKey key) {
EncryptionBuilder.this.encryptionKeys.add(key);
return new WithAlgorithmsImpl();
}
@Override
public WithAlgorithms toRecipients(Set<PGPPublicKey> keys) {
EncryptionBuilder.this.encryptionKeys.addAll(keys);
return new WithAlgorithmsImpl();
}
@Override
public WithAlgorithms toRecipients(Set<Long> keyIds, Set<PGPPublicKeyRing> keyRings)
throws PublicKeyNotFoundException {
Set<PGPPublicKey> keys = new HashSet<>();
for (Long id : keyIds) {
PGPPublicKey key = null;
for (PGPPublicKeyRing ring : keyRings) {
key = ring.getPublicKey(id);
if (key != null) {
break; // Found key. Break inner loop
}
}
if (key == null) {
throw new PublicKeyNotFoundException(id);
}
keys.add(key);
}
return toRecipients(keys);
}
@Override
public WithAlgorithms toRecipients(Set<Long> keyIds, PGPPublicKeyRingCollection keyRings)
throws PublicKeyNotFoundException {
Set<PGPPublicKeyRing> rings = new HashSet<>();
for (Iterator<PGPPublicKeyRing> i = keyRings.getKeyRings(); i.hasNext();) {
rings.add(i.next());
}
return toRecipients(keyIds, rings);
}
@Override
public SignWith doNotEncrypt() {
return new SignWithImpl();
}
}
class WithAlgorithmsImpl implements WithAlgorithms {
@Override
public WithAlgorithms andToSelf(Set<PGPPublicKey> keys) {
EncryptionBuilder.this.encryptionKeys.addAll(keys);
return this;
}
@Override
public SignWith usingAlgorithms(SymmetricKeyAlgorithm symmetricKeyAlgorithm,
HashAlgorithm hashAlgorithm,
CompressionAlgorithm compressionAlgorithm) {
EncryptionBuilder.this.symmetricKeyAlgorithm = symmetricKeyAlgorithm;
EncryptionBuilder.this.hashAlgorithm = hashAlgorithm;
EncryptionBuilder.this.compressionAlgorithm = compressionAlgorithm;
return new SignWithImpl();
}
}
class SignWithImpl implements SignWith {
@Override
public Armor signWith(PGPSecretKey key) {
EncryptionBuilder.this.signingKeys.add(key);
return new ArmorImpl();
}
@Override
public Armor signWith(Set<PGPSecretKey> keys) {
EncryptionBuilder.this.signingKeys.addAll(keys);
return new ArmorImpl();
}
@Override
public Armor signWith(Set<Long> keyIds, Set<PGPSecretKeyRing> keyRings)
throws SecretKeyNotFoundException {
Set<PGPSecretKey> keys = new HashSet<>();
for (Long id : keyIds) {
PGPSecretKey key = null;
for (PGPSecretKeyRing ring : keyRings) {
key = ring.getSecretKey(id);
if (key != null) {
break; // Found key. Break inner loop
}
}
if (key == null) {
throw new SecretKeyNotFoundException(id);
}
keys.add(key);
}
return signWith(keys);
}
@Override
public Armor signWith(Set<Long> keyIds, PGPSecretKeyRingCollection keys)
throws SecretKeyNotFoundException {
Set<PGPSecretKeyRing> rings = new HashSet<>();
for (Iterator<PGPSecretKeyRing> i = keys.getKeyRings(); i.hasNext();) {
rings.add(i.next());
}
return signWith(keyIds, rings);
}
@Override
public Armor doNotSign() {
return new ArmorImpl();
}
}
class ArmorImpl implements Armor {
@Override
public OutputStream asciiArmor() {
EncryptionBuilder.this.asciiArmor = true;
return build();
}
@Override
public OutputStream noArmor() {
EncryptionBuilder.this.asciiArmor = false;
return build();
}
private OutputStream build() {
return EncryptionStream.create(
EncryptionBuilder.this.outputStream,
EncryptionBuilder.this.encryptionKeys,
EncryptionBuilder.this.signingKeys,
EncryptionBuilder.this.symmetricKeyAlgorithm,
EncryptionBuilder.this.hashAlgorithm,
EncryptionBuilder.this.compressionAlgorithm,
EncryptionBuilder.this.asciiArmor);
}
}
}

View File

@ -0,0 +1,71 @@
package de.vanitasvitae.crypto.pgpainless.encryption_signing;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import de.vanitasvitae.crypto.pgpainless.PublicKeyNotFoundException;
import de.vanitasvitae.crypto.pgpainless.SecretKeyNotFoundException;
import de.vanitasvitae.crypto.pgpainless.algorithm.CompressionAlgorithm;
import de.vanitasvitae.crypto.pgpainless.algorithm.HashAlgorithm;
import de.vanitasvitae.crypto.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
public interface EncryptionBuilderInterface {
ToRecipients onOutputStream(OutputStream outputStream);
interface ToRecipients {
WithAlgorithms toRecipient(PGPPublicKey key);
WithAlgorithms toRecipients(Set<PGPPublicKey> keys);
WithAlgorithms toRecipients(Set<Long> keyIds, Set<PGPPublicKeyRing> keyRings)
throws PublicKeyNotFoundException;
WithAlgorithms toRecipients(Set<Long> keyIds, PGPPublicKeyRingCollection keys)
throws PublicKeyNotFoundException;
SignWith doNotEncrypt();
}
interface WithAlgorithms {
WithAlgorithms andToSelf(Set<PGPPublicKey> keys);
SignWith usingAlgorithms(SymmetricKeyAlgorithm symmetricKeyAlgorithm,
HashAlgorithm hashAlgorithm,
CompressionAlgorithm compressionAlgorithm);
}
interface SignWith {
Armor signWith(PGPSecretKey key);
Armor signWith(Set<PGPSecretKey> keys);
Armor signWith(Set<Long> keyIds, Set<PGPSecretKeyRing> keyRings) throws SecretKeyNotFoundException;
Armor signWith(Set<Long> keyIds, PGPSecretKeyRingCollection keys) throws SecretKeyNotFoundException;
Armor doNotSign();
}
interface Armor {
OutputStream asciiArmor();
OutputStream noArmor();
}
}

View File

@ -0,0 +1,75 @@
package de.vanitasvitae.crypto.pgpainless.encryption_signing;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import de.vanitasvitae.crypto.pgpainless.algorithm.CompressionAlgorithm;
import de.vanitasvitae.crypto.pgpainless.algorithm.HashAlgorithm;
import de.vanitasvitae.crypto.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
public class EncryptionStream extends OutputStream {
private final OutputStream outputStream;
private final Set<PGPPublicKey> encryptionKeys;
private final Set<PGPSecretKey> signingKeys;
private final SymmetricKeyAlgorithm symmetricKeyAlgorithm;
private final HashAlgorithm hashAlgorithm;
private final CompressionAlgorithm compressionAlgorithm;
private final boolean asciiArmor;
private EncryptionStream(OutputStream outputStream,
Set<PGPPublicKey> encryptionKeys,
Set<PGPSecretKey> signingKeys,
SymmetricKeyAlgorithm symmetricKeyAlgorithm,
HashAlgorithm hashAlgorithm,
CompressionAlgorithm compressionAlgorithm,
boolean asciiArmor) {
this.outputStream = outputStream;
this.encryptionKeys = encryptionKeys;
this.signingKeys = signingKeys;
this.symmetricKeyAlgorithm = symmetricKeyAlgorithm;
this.hashAlgorithm = hashAlgorithm;
this.compressionAlgorithm = compressionAlgorithm;
this.asciiArmor = asciiArmor;
}
public static EncryptionStream create(OutputStream outputStream,
Set<PGPPublicKey> encryptionKeys,
Set<PGPSecretKey> signingKeys,
SymmetricKeyAlgorithm symmetricKeyAlgorithm,
HashAlgorithm hashAlgorithm,
CompressionAlgorithm compressionAlgorithm,
boolean asciiArmor) {
requireNonNull(outputStream, "outputStream");
requireNonNull(encryptionKeys, "encryptionKeys");
requireNonNull(signingKeys, "signingKeys");
requireNonNull(symmetricKeyAlgorithm, "symmetricKeyAlgorithm");
requireNonNull(hashAlgorithm, "hashAlgorithm");
requireNonNull(compressionAlgorithm, "compressionAlgorithm");
return new EncryptionStream(outputStream,
encryptionKeys,
signingKeys,
symmetricKeyAlgorithm,
hashAlgorithm,
compressionAlgorithm,
asciiArmor);
}
@Override
public void write(int i) throws IOException {
}
private static void requireNonNull(Object o, String name) {
if (o == null) {
throw new IllegalArgumentException("Argument '" + name + "' MUST NOT be null.");
}
}
}