2021-05-06 00:04:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Paul Schaub.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.pgpainless.encryption_signing;
|
|
|
|
|
2021-05-24 16:10:26 +02:00
|
|
|
import java.util.Collections;
|
2021-05-06 00:04:03 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedHashSet;
|
2021-05-24 16:10:26 +02:00
|
|
|
import java.util.List;
|
2021-05-06 00:04:03 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.operator.PBEKeyEncryptionMethodGenerator;
|
|
|
|
import org.bouncycastle.openpgp.operator.PGPKeyEncryptionMethodGenerator;
|
|
|
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
|
|
|
import org.pgpainless.implementation.ImplementationFactory;
|
|
|
|
import org.pgpainless.key.SubkeyIdentifier;
|
|
|
|
import org.pgpainless.key.info.KeyRingInfo;
|
2021-05-25 13:52:45 +02:00
|
|
|
import org.pgpainless.key.info.KeyAccessor;
|
2021-05-06 00:04:03 +02:00
|
|
|
import org.pgpainless.util.Passphrase;
|
|
|
|
|
2021-05-24 16:10:26 +02:00
|
|
|
/**
|
|
|
|
* Options for the encryption process.
|
|
|
|
* This class can be used to set encryption parameters, like encryption keys and passphrases, algorithms etc.
|
|
|
|
*
|
|
|
|
* A typical use might look like follows:
|
|
|
|
* <pre>
|
|
|
|
* {@code
|
|
|
|
* EncryptionOptions opt = new EncryptionOptions();
|
|
|
|
* opt.addRecipient(aliceKey, "Alice <alice@wonderland.lit>");
|
|
|
|
* opt.addPassphrase(Passphrase.fromPassword("AdditionalDecryptionPassphrase123"));
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* To use a custom symmetric encryption algorithm, use {@link #overrideEncryptionAlgorithm(SymmetricKeyAlgorithm)}.
|
|
|
|
* This will cause PGPainless to use the provided algorithm for message encryption, instead of negotiating an algorithm
|
|
|
|
* by inspecting the provided recipient keys.
|
|
|
|
*
|
|
|
|
* By default, PGPainless will only encrypt to a single encryption capable subkey per recipient key.
|
|
|
|
* This behavior can be changed, eg. by calling
|
|
|
|
* <pre>
|
|
|
|
* {@code
|
|
|
|
* opt.addRecipient(aliceKey, EncryptionOptions.encryptToAllCapableSubkeys());
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
* when adding the recipient key.
|
|
|
|
*/
|
2021-05-06 00:04:03 +02:00
|
|
|
public class EncryptionOptions {
|
|
|
|
|
|
|
|
private final EncryptionStream.Purpose purpose;
|
|
|
|
private final Set<PGPKeyEncryptionMethodGenerator> encryptionMethods = new LinkedHashSet<>();
|
|
|
|
private final Set<SubkeyIdentifier> encryptionKeys = new LinkedHashSet<>();
|
|
|
|
private final Map<SubkeyIdentifier, KeyRingInfo> keyRingInfo = new HashMap<>();
|
2021-05-25 13:52:45 +02:00
|
|
|
private final Map<SubkeyIdentifier, KeyAccessor> keyViews = new HashMap<>();
|
2021-05-24 16:10:26 +02:00
|
|
|
private final EncryptionKeySelector encryptionKeySelector = encryptToFirstSubkey();
|
2021-05-06 00:04:03 +02:00
|
|
|
|
|
|
|
private SymmetricKeyAlgorithm encryptionAlgorithmOverride = null;
|
|
|
|
|
2021-05-24 16:10:26 +02:00
|
|
|
/**
|
|
|
|
* Encrypt to keys both carrying the key flag {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_COMMS}
|
|
|
|
* or {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_STORAGE}.
|
|
|
|
*/
|
2021-05-20 13:42:52 +02:00
|
|
|
public EncryptionOptions() {
|
|
|
|
this(EncryptionStream.Purpose.STORAGE_AND_COMMUNICATIONS);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:04:03 +02:00
|
|
|
public EncryptionOptions(EncryptionStream.Purpose purpose) {
|
|
|
|
this.purpose = purpose;
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:10:26 +02:00
|
|
|
/**
|
|
|
|
* Factory method to create an {@link EncryptionOptions} object which will encrypt for keys
|
|
|
|
* which carry the flag {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_COMMS}.
|
|
|
|
*
|
|
|
|
* @return encryption options
|
|
|
|
*/
|
2021-05-20 13:42:52 +02:00
|
|
|
public static EncryptionOptions encryptCommunications() {
|
|
|
|
return new EncryptionOptions(EncryptionStream.Purpose.COMMUNICATIONS);
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:10:26 +02:00
|
|
|
/**
|
|
|
|
* Factory method to create an {@link EncryptionOptions} object which will encrypt for keys
|
|
|
|
* which carry the flag {@link org.pgpainless.algorithm.KeyFlag#ENCRYPT_STORAGE}.
|
|
|
|
*
|
|
|
|
* @return encryption options
|
|
|
|
*/
|
2021-05-20 13:42:52 +02:00
|
|
|
public static EncryptionOptions encryptDataAtRest() {
|
|
|
|
return new EncryptionOptions(EncryptionStream.Purpose.STORAGE);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:04:03 +02:00
|
|
|
/**
|
|
|
|
* Add a recipient by providing a key and recipient user-id.
|
|
|
|
* The user-id is used to determine the recipients preferences (algorithms etc.).
|
|
|
|
*
|
|
|
|
* @param key key ring
|
|
|
|
* @param userId user id
|
2021-05-27 13:46:40 +02:00
|
|
|
* @return this
|
2021-05-06 00:04:03 +02:00
|
|
|
*/
|
2021-05-24 16:10:26 +02:00
|
|
|
public EncryptionOptions addRecipient(PGPPublicKeyRing key, String userId) {
|
|
|
|
return addRecipient(key, userId, encryptionKeySelector);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a recipient by providing a key and recipient user-id, as well as a strategy for selecting one or multiple
|
|
|
|
* encryption capable subkeys from the key.
|
|
|
|
*
|
|
|
|
* @param key key
|
|
|
|
* @param userId user-id
|
|
|
|
* @param encryptionKeySelectionStrategy strategy to select one or more encryption subkeys to encrypt to
|
2021-05-27 13:46:40 +02:00
|
|
|
* @return this
|
2021-05-24 16:10:26 +02:00
|
|
|
*/
|
|
|
|
public EncryptionOptions addRecipient(PGPPublicKeyRing key, String userId, EncryptionKeySelector encryptionKeySelectionStrategy) {
|
2021-05-06 00:04:03 +02:00
|
|
|
KeyRingInfo info = new KeyRingInfo(key, new Date());
|
|
|
|
|
2021-05-24 16:10:26 +02:00
|
|
|
List<PGPPublicKey> encryptionSubkeys = encryptionKeySelectionStrategy
|
|
|
|
.selectEncryptionSubkeys(info.getEncryptionSubkeys(userId, purpose));
|
|
|
|
if (encryptionSubkeys.isEmpty()) {
|
|
|
|
throw new AssertionError("Key has no suitable encryption subkeys.");
|
2021-05-06 00:04:03 +02:00
|
|
|
}
|
2021-05-24 16:10:26 +02:00
|
|
|
|
|
|
|
for (PGPPublicKey encryptionSubkey : encryptionSubkeys) {
|
2021-05-24 18:51:37 +02:00
|
|
|
SubkeyIdentifier keyId = new SubkeyIdentifier(key, encryptionSubkey.getKeyID());
|
|
|
|
keyRingInfo.put(keyId, info);
|
2021-05-25 13:52:45 +02:00
|
|
|
keyViews.put(keyId, new KeyAccessor.ViaUserId(info, keyId, userId));
|
2021-05-24 16:10:26 +02:00
|
|
|
addRecipientKey(key, encryptionSubkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
2021-05-06 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a recipient by providing a key.
|
|
|
|
*
|
|
|
|
* @param key key ring
|
2021-05-27 13:46:40 +02:00
|
|
|
* @return this
|
2021-05-06 00:04:03 +02:00
|
|
|
*/
|
2021-05-24 16:10:26 +02:00
|
|
|
public EncryptionOptions addRecipient(PGPPublicKeyRing key) {
|
|
|
|
return addRecipient(key, encryptionKeySelector);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a recipient by providing a key and an encryption key selection strategy.
|
|
|
|
*
|
|
|
|
* @param key key ring
|
|
|
|
* @param encryptionKeySelectionStrategy strategy used to select one or multiple encryption subkeys.
|
2021-05-27 13:46:40 +02:00
|
|
|
* @return this
|
2021-05-24 16:10:26 +02:00
|
|
|
*/
|
|
|
|
public EncryptionOptions addRecipient(PGPPublicKeyRing key, EncryptionKeySelector encryptionKeySelectionStrategy) {
|
2021-05-06 00:04:03 +02:00
|
|
|
KeyRingInfo info = new KeyRingInfo(key, new Date());
|
2021-05-24 16:10:26 +02:00
|
|
|
|
|
|
|
List<PGPPublicKey> encryptionSubkeys = encryptionKeySelectionStrategy
|
|
|
|
.selectEncryptionSubkeys(info.getEncryptionSubkeys(purpose));
|
|
|
|
if (encryptionSubkeys.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("Key has no suitable encryption subkeys.");
|
2021-05-06 00:04:03 +02:00
|
|
|
}
|
2021-05-24 16:10:26 +02:00
|
|
|
|
|
|
|
for (PGPPublicKey encryptionSubkey : encryptionSubkeys) {
|
2021-05-24 18:51:37 +02:00
|
|
|
SubkeyIdentifier keyId = new SubkeyIdentifier(key, encryptionSubkey.getKeyID());
|
|
|
|
keyRingInfo.put(keyId, info);
|
2021-05-25 13:52:45 +02:00
|
|
|
keyViews.put(keyId, new KeyAccessor.ViaKeyId(info, keyId));
|
2021-05-24 16:10:26 +02:00
|
|
|
addRecipientKey(key, encryptionSubkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
2021-05-06 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addRecipientKey(PGPPublicKeyRing keyRing, PGPPublicKey key) {
|
|
|
|
encryptionKeys.add(new SubkeyIdentifier(keyRing, key.getKeyID()));
|
|
|
|
PGPKeyEncryptionMethodGenerator encryptionMethod = ImplementationFactory
|
|
|
|
.getInstance().getPublicKeyKeyEncryptionMethodGenerator(key);
|
|
|
|
addEncryptionMethod(encryptionMethod);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a symmetric passphrase which the message will be encrypted to.
|
|
|
|
*
|
|
|
|
* @param passphrase passphrase
|
2021-05-27 13:46:40 +02:00
|
|
|
* @return this
|
2021-05-06 00:04:03 +02:00
|
|
|
*/
|
2021-05-24 16:10:26 +02:00
|
|
|
public EncryptionOptions addPassphrase(Passphrase passphrase) {
|
2021-05-06 00:04:03 +02:00
|
|
|
if (passphrase.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("Passphrase must not be empty.");
|
|
|
|
}
|
|
|
|
PBEKeyEncryptionMethodGenerator encryptionMethod = ImplementationFactory
|
|
|
|
.getInstance().getPBEKeyEncryptionMethodGenerator(passphrase);
|
2021-05-24 16:10:26 +02:00
|
|
|
return addEncryptionMethod(encryptionMethod);
|
2021-05-06 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an {@link PGPKeyEncryptionMethodGenerator} which will be used to encrypt the message.
|
|
|
|
* Method generators are either {@link PBEKeyEncryptionMethodGenerator} (passphrase)
|
|
|
|
* or {@link PGPKeyEncryptionMethodGenerator} (public key).
|
|
|
|
*
|
|
|
|
* This method is intended for advanced users to allow encryption for specific subkeys.
|
|
|
|
* This can come in handy for example if data needs to be encrypted to a subkey that's ignored by PGPainless.
|
|
|
|
*
|
|
|
|
* @param encryptionMethod encryption method
|
2021-05-27 13:46:40 +02:00
|
|
|
* @return this
|
2021-05-06 00:04:03 +02:00
|
|
|
*/
|
2021-05-24 16:10:26 +02:00
|
|
|
public EncryptionOptions addEncryptionMethod(PGPKeyEncryptionMethodGenerator encryptionMethod) {
|
2021-05-06 00:04:03 +02:00
|
|
|
encryptionMethods.add(encryptionMethod);
|
2021-05-24 16:10:26 +02:00
|
|
|
return this;
|
2021-05-06 00:04:03 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 18:51:37 +02:00
|
|
|
Set<PGPKeyEncryptionMethodGenerator> getEncryptionMethods() {
|
2021-05-06 00:04:03 +02:00
|
|
|
return new HashSet<>(encryptionMethods);
|
|
|
|
}
|
|
|
|
|
2021-05-24 18:51:37 +02:00
|
|
|
Map<SubkeyIdentifier, KeyRingInfo> getKeyRingInfo() {
|
|
|
|
return new HashMap<>(keyRingInfo);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:04:03 +02:00
|
|
|
public Set<SubkeyIdentifier> getEncryptionKeyIdentifiers() {
|
|
|
|
return new HashSet<>(encryptionKeys);
|
|
|
|
}
|
|
|
|
|
2021-05-25 13:52:45 +02:00
|
|
|
public Map<SubkeyIdentifier, KeyAccessor> getKeyViews() {
|
2021-05-24 18:51:37 +02:00
|
|
|
return new HashMap<>(keyViews);
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:04:03 +02:00
|
|
|
public SymmetricKeyAlgorithm getEncryptionAlgorithmOverride() {
|
|
|
|
return encryptionAlgorithmOverride;
|
|
|
|
}
|
|
|
|
|
2021-05-27 13:46:40 +02:00
|
|
|
/**
|
|
|
|
* Override the used symmetric encryption algorithm.
|
|
|
|
* The symmetric encryption algorithm is used to encrypt the message itself,
|
|
|
|
* while the used symmetric key will be encrypted to all recipients using public key
|
|
|
|
* cryptography.
|
|
|
|
*
|
|
|
|
* If the algorithm is not overridden, a suitable algorithm will be negotiated.
|
|
|
|
*
|
|
|
|
* @param encryptionAlgorithm encryption algorithm override
|
|
|
|
*/
|
2021-05-06 00:04:03 +02:00
|
|
|
public void overrideEncryptionAlgorithm(SymmetricKeyAlgorithm encryptionAlgorithm) {
|
2021-05-20 12:40:38 +02:00
|
|
|
if (encryptionAlgorithm == SymmetricKeyAlgorithm.NULL) {
|
|
|
|
throw new IllegalArgumentException("Plaintext encryption can only be used to denote unencrypted secret keys.");
|
|
|
|
}
|
2021-05-06 00:04:03 +02:00
|
|
|
this.encryptionAlgorithmOverride = encryptionAlgorithm;
|
|
|
|
}
|
2021-05-24 16:10:26 +02:00
|
|
|
|
|
|
|
public interface EncryptionKeySelector {
|
|
|
|
List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only encrypt to the first valid encryption capable subkey we stumble upon.
|
|
|
|
*
|
|
|
|
* @return encryption key selector
|
|
|
|
*/
|
|
|
|
public static EncryptionKeySelector encryptToFirstSubkey() {
|
|
|
|
return new EncryptionKeySelector() {
|
|
|
|
@Override
|
|
|
|
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) {
|
|
|
|
return encryptionCapableKeys.isEmpty() ? Collections.emptyList() : Collections.singletonList(encryptionCapableKeys.get(0));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypt to any valid, encryption capable subkey on the key ring.
|
|
|
|
*
|
|
|
|
* @return encryption key selector
|
|
|
|
*/
|
|
|
|
public static EncryptionKeySelector encryptToAllCapableSubkeys() {
|
|
|
|
return new EncryptionKeySelector() {
|
|
|
|
@Override
|
|
|
|
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) {
|
|
|
|
return encryptionCapableKeys;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Create encryptToBestSubkey() method
|
2021-05-06 00:04:03 +02:00
|
|
|
}
|