1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-17 18:02:05 +01:00

Bump sop-java to 4.0.2 and improve exception handling

This commit is contained in:
Paul Schaub 2022-11-06 15:03:35 +01:00
parent 3000e496bc
commit e67c43a6f7
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 29 additions and 6 deletions

View file

@ -61,6 +61,11 @@ public class DecryptImpl implements Decrypt {
consumerOptions.addVerificationCerts(certs); 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) { } catch (PGPException e) {
throw new SOPGPException.BadData(e); throw new SOPGPException.BadData(e);
} }
@ -96,15 +101,23 @@ public class DecryptImpl implements Decrypt {
} }
@Override @Override
public DecryptImpl withKey(InputStream keyIn) throws SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo { public DecryptImpl withKey(InputStream keyIn) throws SOPGPException.BadData, IOException, SOPGPException.UnsupportedAsymmetricAlgo {
try { try {
PGPSecretKeyRingCollection secretKeyCollection = PGPainless.readKeyRing() PGPSecretKeyRingCollection secretKeyCollection = PGPainless.readKeyRing()
.secretKeyRingCollection(keyIn); .secretKeyRingCollection(keyIn);
if (secretKeyCollection.size() == 0) {
throw new SOPGPException.BadData("No key data found.");
}
for (PGPSecretKeyRing key : secretKeyCollection) { for (PGPSecretKeyRing key : secretKeyCollection) {
protector.addSecretKey(key); protector.addSecretKey(key);
consumerOptions.addDecryptionKey(key, protector); 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); throw new SOPGPException.BadData(e);
} }
return this; return this;
@ -132,7 +145,7 @@ public class DecryptImpl implements Decrypt {
.onInputStream(ciphertext) .onInputStream(ciphertext)
.withOptions(consumerOptions); .withOptions(consumerOptions);
} catch (MissingDecryptionMethodException e) { } catch (MissingDecryptionMethodException e) {
throw new SOPGPException.CannotDecrypt(); throw new SOPGPException.CannotDecrypt("No usable decryption key or password provided.", e);
} catch (WrongPassphraseException e) { } catch (WrongPassphraseException e) {
throw new SOPGPException.KeyIsProtected(); throw new SOPGPException.KeyIsProtected();
} catch (PGPException | IOException e) { } catch (PGPException | IOException e) {

View file

@ -100,8 +100,10 @@ public class EncryptImpl implements Encrypt {
public Encrypt withCert(InputStream cert) throws SOPGPException.CertCannotEncrypt, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.BadData { public Encrypt withCert(InputStream cert) throws SOPGPException.CertCannotEncrypt, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.BadData {
try { try {
PGPPublicKeyRingCollection certificates = PGPainless.readKeyRing() PGPPublicKeyRingCollection certificates = PGPainless.readKeyRing()
.keyRingCollection(cert, false) .publicKeyRingCollection(cert);
.getPgpPublicKeyRingCollection(); if (certificates.size() == 0) {
throw new SOPGPException.BadData("No certificate data found.");
}
encryptionOptions.addRecipients(certificates); encryptionOptions.addRecipients(certificates);
} catch (KeyException.UnacceptableEncryptionKeyException e) { } catch (KeyException.UnacceptableEncryptionKeyException e) {
throw new SOPGPException.CertCannotEncrypt(e.getMessage(), e); throw new SOPGPException.CertCannotEncrypt(e.getMessage(), e);

View file

@ -35,6 +35,11 @@ public class ExtractCertImpl implements ExtractCert {
PGPSecretKeyRingCollection keys; PGPSecretKeyRingCollection keys;
try { try {
keys = PGPainless.readKeyRing().secretKeyRingCollection(keyInputStream); 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) { } catch (PGPException e) {
throw new IOException("Cannot read keys.", e); throw new IOException("Cannot read keys.", e);
} }

View file

@ -19,6 +19,7 @@ import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream; import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.decryption_verification.SignatureVerification; import org.pgpainless.decryption_verification.SignatureVerification;
import org.pgpainless.exception.MissingDecryptionMethodException;
import sop.ReadyWithResult; import sop.ReadyWithResult;
import sop.Verification; import sop.Verification;
import sop.exception.SOPGPException; import sop.exception.SOPGPException;
@ -84,6 +85,8 @@ public class InlineVerifyImpl implements InlineVerify {
} }
return verificationList; return verificationList;
} catch (MissingDecryptionMethodException e) {
throw new SOPGPException.BadData("Cannot verify encrypted message.", e);
} catch (PGPException e) { } catch (PGPException e) {
throw new SOPGPException.BadData(e); throw new SOPGPException.BadData(e);
} }

View file

@ -16,6 +16,6 @@ allprojects {
logbackVersion = '1.2.11' logbackVersion = '1.2.11'
mockitoVersion = '4.5.1' mockitoVersion = '4.5.1'
slf4jVersion = '1.7.36' slf4jVersion = '1.7.36'
sopJavaVersion = '4.0.1' sopJavaVersion = '4.0.2'
} }
} }