1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-10 14:21:09 +01:00

Fix tests

This commit is contained in:
Paul Schaub 2021-05-20 13:42:52 +02:00
parent d70ee86468
commit a30767eb91
9 changed files with 78 additions and 33 deletions

View file

@ -128,12 +128,12 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
}
@Override
public AdditionalSignWith signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRing... keyRings) throws KeyValidationException {
public AdditionalSignWith signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRing... keyRings) throws KeyValidationException, PGPException {
return new SignWithImpl().signWith(decryptor, keyRings);
}
@Override
public AdditionalSignWith signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRingCollection keyRings) {
public AdditionalSignWith signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRingCollection keyRings) throws PGPException {
return new SignWithImpl().signWith(decryptor, keyRings);
}
@ -161,7 +161,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
@Override
public AdditionalSignWith signWith(@Nonnull SecretKeyRingProtector decryptor,
@Nonnull PGPSecretKeyRing... keyRings)
throws KeyValidationException {
throws KeyValidationException, PGPException {
for (PGPSecretKeyRing secretKeyRing : keyRings) {
signingOptions.addInlineSignature(decryptor, secretKeyRing, DocumentSignatureType.BINARY_DOCUMENT);
}
@ -170,7 +170,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
@Override
public AdditionalSignWith signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRingCollection keyRings)
throws KeyValidationException {
throws KeyValidationException, PGPException {
for (PGPSecretKeyRing key : keyRings) {
signingOptions.addInlineSignature(decryptor, key, DocumentSignatureType.BINARY_DOCUMENT);
}
@ -193,7 +193,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
String userId,
DocumentSignatureType signatureType)
throws PGPException, KeyValidationException {
signingOptions.addInlineSignature(secretKeyDecryptor, signingKey, userId, signatureType);
signingOptions.addDetachedSignature(secretKeyDecryptor, signingKey, userId, signatureType);
return new AdditionalSignWithImpl();
}
}
@ -269,7 +269,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
// TODO: Negotiation
return PGPainless.getPolicy().getSymmetricKeyAlgorithmPolicy().getDefaultSymmetricKeyAlgorithm();
return PGPainless.getPolicy().getSymmetricKeyEncryptionAlgorithmPolicy().getDefaultSymmetricKeyAlgorithm();
}
/**

View file

@ -188,7 +188,7 @@ public interface EncryptionBuilderInterface {
* @return api handle
*/
@Deprecated
AdditionalSignWith signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRing... keyRings) throws KeyValidationException;
AdditionalSignWith signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRing... keyRings) throws KeyValidationException, PGPException;
/**
* Sign inline using the passed in secret keys.
@ -199,7 +199,7 @@ public interface EncryptionBuilderInterface {
* @return api handle
*/
@Deprecated
AdditionalSignWith signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRingCollection keyRings) throws KeyValidationException;
AdditionalSignWith signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRingCollection keyRings) throws KeyValidationException, PGPException;
/**
* Create an inline signature using the provided secret key.

View file

@ -41,10 +41,22 @@ public class EncryptionOptions {
private SymmetricKeyAlgorithm encryptionAlgorithmOverride = null;
public EncryptionOptions() {
this(EncryptionStream.Purpose.STORAGE_AND_COMMUNICATIONS);
}
public EncryptionOptions(EncryptionStream.Purpose purpose) {
this.purpose = purpose;
}
public static EncryptionOptions encryptCommunications() {
return new EncryptionOptions(EncryptionStream.Purpose.COMMUNICATIONS);
}
public static EncryptionOptions encryptDataAtRest() {
return new EncryptionOptions(EncryptionStream.Purpose.STORAGE);
}
/**
* Add a recipient by providing a key and recipient user-id.
* The user-id is used to determine the recipients preferences (algorithms etc.).
@ -71,7 +83,7 @@ public class EncryptionOptions {
KeyRingInfo info = new KeyRingInfo(key, new Date());
PGPPublicKey encryptionSubkey = info.getEncryptionSubkey(purpose);
if (encryptionSubkey == null) {
throw new AssertionError("Key has no encryption subkey.");
throw new IllegalArgumentException("Key has no encryption subkey.");
}
addRecipientKey(key, encryptionSubkey);
}

View file

@ -70,8 +70,8 @@ public final class SigningOptions {
public void addInlineSignature(SecretKeyRingProtector secretKeyDecryptor,
PGPSecretKeyRing secretKey,
DocumentSignatureType signatureType)
throws KeyValidationException {
throws KeyValidationException, PGPException {
addInlineSignature(secretKeyDecryptor, secretKey, null, signatureType);
}
public void addInlineSignature(SecretKeyRingProtector secretKeyDecryptor,
@ -96,6 +96,35 @@ public final class SigningOptions {
addSigningMethod(secretKey, signingSubkey, hashAlgorithms.get(0), signatureType, false);
}
public void addDetachedSignature(SecretKeyRingProtector secretKeyDecryptor,
PGPSecretKeyRing secretKey,
DocumentSignatureType signatureType)
throws PGPException {
addDetachedSignature(secretKeyDecryptor, secretKey, null, signatureType);
}
public void addDetachedSignature(SecretKeyRingProtector secretKeyDecryptor,
PGPSecretKeyRing secretKey,
String userId,
DocumentSignatureType signatureType)
throws PGPException {
KeyRingInfo keyRingInfo = new KeyRingInfo(secretKey, new Date());
if (userId != null) {
if (!keyRingInfo.isUserIdValid(userId)) {
throw new KeyValidationException(userId, keyRingInfo.getCurrentUserIdCertification(userId), keyRingInfo.getUserIdRevocation(userId));
}
}
PGPPublicKey signingPubKey = keyRingInfo.getSigningSubkey();
if (signingPubKey == null) {
throw new AssertionError("Key has no valid signing key.");
}
PGPSecretKey signingSecKey = secretKey.getSecretKey(signingPubKey.getKeyID());
PGPPrivateKey signingSubkey = signingSecKey.extractPrivateKey(secretKeyDecryptor.getDecryptor(signingPubKey.getKeyID()));
List<HashAlgorithm> hashAlgorithms = keyRingInfo.getPreferredHashAlgorithms(userId, signingPubKey.getKeyID());
addSigningMethod(secretKey, signingSubkey, hashAlgorithms.get(0), signatureType, true);
}
private void addSigningMethod(PGPSecretKeyRing secretKey,
PGPPrivateKey signingSubkey,
HashAlgorithm hashAlgorithm,

View file

@ -597,10 +597,6 @@ public class KeyRingInfo {
continue;
}
if (!subKey.isEncryptionKey()) {
continue;
}
List<KeyFlag> keyFlags = getKeyFlagsOf(subKey.getKeyID());
if (keyFlags.contains(KeyFlag.SIGN_DATA)) {
return subKey;