pgpainless/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionBuilder.java

210 lines
7.2 KiB
Java
Raw Normal View History

package de.vanitasvitae.crypto.pgpainless.encryption_signing;
2018-06-05 01:30:58 +02:00
import java.io.IOException;
2018-06-04 19:45:18 +02:00
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
2018-06-04 19:45:18 +02:00
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;
2018-06-05 01:30:58 +02:00
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPrivateKey;
2018-06-04 19:45:18 +02:00
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<>();
2018-06-05 01:30:58 +02:00
private SecretKeyRingDecryptor signingKeysDecryptor;
private SymmetricKeyAlgorithm symmetricKeyAlgorithm = SymmetricKeyAlgorithm.AES_128;
private HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256;
private CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.UNCOMPRESSED;
2018-06-04 19:45:18 +02:00
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
2018-06-05 01:30:58 +02:00
public Armor signWith(PGPSecretKey key, SecretKeyRingDecryptor decryptor) {
2018-06-04 19:45:18 +02:00
EncryptionBuilder.this.signingKeys.add(key);
2018-06-05 01:30:58 +02:00
EncryptionBuilder.this.signingKeysDecryptor = decryptor;
2018-06-04 19:45:18 +02:00
return new ArmorImpl();
}
@Override
2018-06-05 01:30:58 +02:00
public Armor signWith(Set<PGPSecretKey> keys, SecretKeyRingDecryptor decryptor) {
2018-06-04 19:45:18 +02:00
EncryptionBuilder.this.signingKeys.addAll(keys);
2018-06-05 01:30:58 +02:00
EncryptionBuilder.this.signingKeysDecryptor = decryptor;
2018-06-04 19:45:18 +02:00
return new ArmorImpl();
}
@Override
2018-06-05 01:30:58 +02:00
public Armor signWith(Set<Long> keyIds, Set<PGPSecretKeyRing> keyRings, SecretKeyRingDecryptor decryptor)
2018-06-04 19:45:18 +02:00
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);
}
2018-06-05 01:30:58 +02:00
return signWith(keys, decryptor);
2018-06-04 19:45:18 +02:00
}
@Override
2018-06-05 01:30:58 +02:00
public Armor signWith(Set<Long> keyIds, PGPSecretKeyRingCollection keys, SecretKeyRingDecryptor decryptor)
2018-06-04 19:45:18 +02:00
throws SecretKeyNotFoundException {
Set<PGPSecretKeyRing> rings = new HashSet<>();
for (Iterator<PGPSecretKeyRing> i = keys.getKeyRings(); i.hasNext();) {
rings.add(i.next());
}
2018-06-05 01:30:58 +02:00
return signWith(keyIds, rings, decryptor);
2018-06-04 19:45:18 +02:00
}
@Override
public Armor doNotSign() {
return new ArmorImpl();
}
}
class ArmorImpl implements Armor {
@Override
2018-06-05 01:30:58 +02:00
public OutputStream asciiArmor() throws IOException, PGPException {
2018-06-04 19:45:18 +02:00
EncryptionBuilder.this.asciiArmor = true;
return build();
}
@Override
2018-06-05 01:30:58 +02:00
public OutputStream noArmor() throws IOException, PGPException {
2018-06-04 19:45:18 +02:00
EncryptionBuilder.this.asciiArmor = false;
return build();
}
2018-06-05 01:30:58 +02:00
private OutputStream build() throws IOException, PGPException {
Set<PGPPrivateKey> privateKeys = new HashSet<>();
for (PGPSecretKey secretKey : signingKeys) {
privateKeys.add(secretKey.extractPrivateKey(signingKeysDecryptor.getDecryptor(secretKey.getKeyID())));
}
2018-06-04 19:45:18 +02:00
return EncryptionStream.create(
EncryptionBuilder.this.outputStream,
EncryptionBuilder.this.encryptionKeys,
2018-06-05 01:30:58 +02:00
privateKeys,
2018-06-04 19:45:18 +02:00
EncryptionBuilder.this.symmetricKeyAlgorithm,
EncryptionBuilder.this.hashAlgorithm,
EncryptionBuilder.this.compressionAlgorithm,
EncryptionBuilder.this.asciiArmor);
}
}
}