From e67c43a6f7376f26cb519d7f1c042b7d2a59cb05 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sun, 6 Nov 2022 15:03:35 +0100 Subject: [PATCH] Bump sop-java to 4.0.2 and improve exception handling --- .../java/org/pgpainless/sop/DecryptImpl.java | 19 ++++++++++++++++--- .../java/org/pgpainless/sop/EncryptImpl.java | 6 ++++-- .../org/pgpainless/sop/ExtractCertImpl.java | 5 +++++ .../org/pgpainless/sop/InlineVerifyImpl.java | 3 +++ version.gradle | 2 +- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/DecryptImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/DecryptImpl.java index 4957f748..f18ed732 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/DecryptImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/DecryptImpl.java @@ -61,6 +61,11 @@ public class DecryptImpl implements Decrypt { consumerOptions.addVerificationCerts(certs); + } catch (IOException e) { + if (e.getMessage() != null && e.getMessage().startsWith("unknown object in stream:")) { + throw new SOPGPException.BadData(e); + } + throw e; } catch (PGPException e) { throw new SOPGPException.BadData(e); } @@ -96,15 +101,23 @@ public class DecryptImpl implements Decrypt { } @Override - public DecryptImpl withKey(InputStream keyIn) throws SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo { + public DecryptImpl withKey(InputStream keyIn) throws SOPGPException.BadData, IOException, SOPGPException.UnsupportedAsymmetricAlgo { try { PGPSecretKeyRingCollection secretKeyCollection = PGPainless.readKeyRing() .secretKeyRingCollection(keyIn); + if (secretKeyCollection.size() == 0) { + throw new SOPGPException.BadData("No key data found."); + } for (PGPSecretKeyRing key : secretKeyCollection) { protector.addSecretKey(key); consumerOptions.addDecryptionKey(key, protector); } - } catch (IOException | PGPException e) { + } catch (IOException e) { + if (e.getMessage() != null && e.getMessage().startsWith("unknown object in stream:")) { + throw new SOPGPException.BadData(e); + } + throw e; + } catch (PGPException e) { throw new SOPGPException.BadData(e); } return this; @@ -132,7 +145,7 @@ public class DecryptImpl implements Decrypt { .onInputStream(ciphertext) .withOptions(consumerOptions); } catch (MissingDecryptionMethodException e) { - throw new SOPGPException.CannotDecrypt(); + throw new SOPGPException.CannotDecrypt("No usable decryption key or password provided.", e); } catch (WrongPassphraseException e) { throw new SOPGPException.KeyIsProtected(); } catch (PGPException | IOException e) { diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/EncryptImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/EncryptImpl.java index 1b95d87c..9658bd17 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/EncryptImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/EncryptImpl.java @@ -100,8 +100,10 @@ public class EncryptImpl implements Encrypt { public Encrypt withCert(InputStream cert) throws SOPGPException.CertCannotEncrypt, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.BadData { try { PGPPublicKeyRingCollection certificates = PGPainless.readKeyRing() - .keyRingCollection(cert, false) - .getPgpPublicKeyRingCollection(); + .publicKeyRingCollection(cert); + if (certificates.size() == 0) { + throw new SOPGPException.BadData("No certificate data found."); + } encryptionOptions.addRecipients(certificates); } catch (KeyException.UnacceptableEncryptionKeyException e) { throw new SOPGPException.CertCannotEncrypt(e.getMessage(), e); diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/ExtractCertImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/ExtractCertImpl.java index 5f694208..16848383 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/ExtractCertImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/ExtractCertImpl.java @@ -35,6 +35,11 @@ public class ExtractCertImpl implements ExtractCert { PGPSecretKeyRingCollection keys; try { keys = PGPainless.readKeyRing().secretKeyRingCollection(keyInputStream); + } catch (IOException e) { + if (e.getMessage() != null && e.getMessage().startsWith("unknown object in stream:")) { + throw new SOPGPException.BadData(e); + } + throw e; } catch (PGPException e) { throw new IOException("Cannot read keys.", e); } diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/InlineVerifyImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/InlineVerifyImpl.java index 1e8c4fee..e9994f38 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/InlineVerifyImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/InlineVerifyImpl.java @@ -19,6 +19,7 @@ import org.pgpainless.decryption_verification.ConsumerOptions; import org.pgpainless.decryption_verification.DecryptionStream; import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.decryption_verification.SignatureVerification; +import org.pgpainless.exception.MissingDecryptionMethodException; import sop.ReadyWithResult; import sop.Verification; import sop.exception.SOPGPException; @@ -84,6 +85,8 @@ public class InlineVerifyImpl implements InlineVerify { } return verificationList; + } catch (MissingDecryptionMethodException e) { + throw new SOPGPException.BadData("Cannot verify encrypted message.", e); } catch (PGPException e) { throw new SOPGPException.BadData(e); } diff --git a/version.gradle b/version.gradle index 680b9d45..36822069 100644 --- a/version.gradle +++ b/version.gradle @@ -16,6 +16,6 @@ allprojects { logbackVersion = '1.2.11' mockitoVersion = '4.5.1' slf4jVersion = '1.7.36' - sopJavaVersion = '4.0.1' + sopJavaVersion = '4.0.2' } }