2021-10-07 15:48:52 +02:00
|
|
|
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless.encryption_signing;
|
2018-06-04 14:50:09 +02:00
|
|
|
|
2018-06-05 01:30:58 +02:00
|
|
|
import java.io.IOException;
|
2018-06-04 19:45:18 +02:00
|
|
|
import java.io.OutputStream;
|
2021-05-24 18:51:37 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2021-05-25 13:52:45 +02:00
|
|
|
import java.util.Set;
|
2020-08-24 16:26:29 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2018-06-04 14:50:09 +02:00
|
|
|
|
2018-06-05 01:30:58 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
2021-04-26 13:38:12 +02:00
|
|
|
import org.pgpainless.PGPainless;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.algorithm.CompressionAlgorithm;
|
|
|
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
2021-05-25 13:52:45 +02:00
|
|
|
import org.pgpainless.algorithm.negotiation.SymmetricKeyAlgorithmNegotiator;
|
2021-05-24 18:51:37 +02:00
|
|
|
import org.pgpainless.key.SubkeyIdentifier;
|
2021-08-23 13:29:57 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-06-04 19:45:18 +02:00
|
|
|
|
|
|
|
public class EncryptionBuilder implements EncryptionBuilderInterface {
|
|
|
|
|
2021-08-23 13:29:57 +02:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(EncryptionBuilder.class);
|
|
|
|
|
2018-06-04 19:45:18 +02:00
|
|
|
private OutputStream outputStream;
|
|
|
|
|
|
|
|
@Override
|
2021-07-10 12:22:47 +02:00
|
|
|
public WithOptions onOutputStream(@Nonnull OutputStream outputStream) {
|
2018-06-04 19:45:18 +02:00
|
|
|
this.outputStream = outputStream;
|
2021-06-29 16:43:37 +02:00
|
|
|
return new WithOptionsImpl();
|
2018-06-04 19:45:18 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 16:43:37 +02:00
|
|
|
class WithOptionsImpl implements WithOptions {
|
2018-06-07 18:12:13 +02:00
|
|
|
@Override
|
2021-05-06 00:04:03 +02:00
|
|
|
public EncryptionStream withOptions(ProducerOptions options) throws PGPException, IOException {
|
|
|
|
if (options == null) {
|
|
|
|
throw new NullPointerException("ProducerOptions cannot be null.");
|
2018-06-07 18:12:13 +02:00
|
|
|
}
|
2021-07-10 12:22:47 +02:00
|
|
|
return new EncryptionStream(outputStream, options);
|
2018-06-04 19:45:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2021-05-06 00:04:03 +02:00
|
|
|
/**
|
|
|
|
* Negotiate the {@link SymmetricKeyAlgorithm} used for message encryption.
|
|
|
|
*
|
|
|
|
* @param encryptionOptions encryption options
|
|
|
|
* @return negotiated symmetric key algorithm
|
|
|
|
*/
|
|
|
|
public static SymmetricKeyAlgorithm negotiateSymmetricEncryptionAlgorithm(EncryptionOptions encryptionOptions) {
|
2021-05-25 13:52:45 +02:00
|
|
|
List<Set<SymmetricKeyAlgorithm>> preferences = new ArrayList<>();
|
2021-05-24 18:51:37 +02:00
|
|
|
for (SubkeyIdentifier key : encryptionOptions.getKeyViews().keySet()) {
|
2021-05-25 13:52:45 +02:00
|
|
|
preferences.add(encryptionOptions.getKeyViews().get(key).getPreferredSymmetricKeyAlgorithms());
|
2021-05-24 18:51:37 +02:00
|
|
|
}
|
2021-05-06 00:04:03 +02:00
|
|
|
|
2021-08-23 13:29:57 +02:00
|
|
|
SymmetricKeyAlgorithm algorithm = SymmetricKeyAlgorithmNegotiator
|
2021-05-25 13:52:45 +02:00
|
|
|
.byPopularity()
|
|
|
|
.negotiate(
|
|
|
|
PGPainless.getPolicy().getSymmetricKeyEncryptionAlgorithmPolicy(),
|
|
|
|
encryptionOptions.getEncryptionAlgorithmOverride(),
|
|
|
|
preferences);
|
2021-08-23 13:29:57 +02:00
|
|
|
LOGGER.debug("Negotiation resulted in {} being the symmetric encryption algorithm of choice.", algorithm);
|
|
|
|
return algorithm;
|
2021-01-09 20:55:19 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 00:04:03 +02:00
|
|
|
public static CompressionAlgorithm negotiateCompressionAlgorithm(ProducerOptions producerOptions) {
|
|
|
|
CompressionAlgorithm compressionAlgorithmOverride = producerOptions.getCompressionAlgorithmOverride();
|
|
|
|
if (compressionAlgorithmOverride != null) {
|
|
|
|
return compressionAlgorithmOverride;
|
2021-01-09 20:30:34 +01:00
|
|
|
}
|
2021-05-06 00:04:03 +02:00
|
|
|
|
|
|
|
// TODO: Negotiation
|
|
|
|
|
|
|
|
return PGPainless.getPolicy().getCompressionAlgorithmPolicy().defaultCompressionAlgorithm();
|
2018-06-11 01:33:49 +02:00
|
|
|
}
|
2018-06-04 14:50:09 +02:00
|
|
|
}
|