Throw MissingArg for missing arguments

This commit is contained in:
Paul Schaub 2025-07-01 14:28:50 +02:00
parent ea8b1b6459
commit 80de3585d7
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 12 additions and 0 deletions

View file

@ -20,6 +20,7 @@ public class BCDetachedVerify
{ {
private final OpenPGPDetachedSignatureProcessor processor; private final OpenPGPDetachedSignatureProcessor processor;
private boolean hasCerts = false;
public BCDetachedVerify(OpenPGPApi api) public BCDetachedVerify(OpenPGPApi api)
{ {
@ -50,12 +51,16 @@ public class BCDetachedVerify
@Override @Override
public DetachedVerify cert(@NotNull InputStream inputStream) throws SOPGPException.BadData, IOException { public DetachedVerify cert(@NotNull InputStream inputStream) throws SOPGPException.BadData, IOException {
processor.addVerificationCertificate(parseCertificate(inputStream)); processor.addVerificationCertificate(parseCertificate(inputStream));
hasCerts = true;
return this; return this;
} }
@NotNull @NotNull
@Override @Override
public List<Verification> data(@NotNull InputStream inputStream) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData { public List<Verification> data(@NotNull InputStream inputStream) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData {
if (!hasCerts) {
throw new SOPGPException.MissingArg("No certificates provided for signature verification.");
}
List<OpenPGPSignature.OpenPGPDocumentSignature> signatures = processor.process(inputStream); List<OpenPGPSignature.OpenPGPDocumentSignature> signatures = processor.process(inputStream);
List<Verification> verifications = getVerifications(signatures); List<Verification> verifications = getVerifications(signatures);

View file

@ -31,6 +31,7 @@ public class BCEncrypt
private final OpenPGPMessageGenerator mGen; private final OpenPGPMessageGenerator mGen;
private final List<OpenPGPKey> signingKeys = new ArrayList<>(); private final List<OpenPGPKey> signingKeys = new ArrayList<>();
private int signatureMode = PGPSignature.BINARY_DOCUMENT; private int signatureMode = PGPSignature.BINARY_DOCUMENT;
private boolean hasEncryptionMethod = false;
public BCEncrypt(OpenPGPApi api) { public BCEncrypt(OpenPGPApi api) {
super(api); super(api);
@ -83,6 +84,7 @@ public class BCEncrypt
public Encrypt withPassword(@NotNull String s) public Encrypt withPassword(@NotNull String s)
throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption { throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
mGen.addEncryptionPassphrase(s.toCharArray()); mGen.addEncryptionPassphrase(s.toCharArray());
hasEncryptionMethod = true;
return this; return this;
} }
@ -92,6 +94,7 @@ public class BCEncrypt
throws SOPGPException.CertCannotEncrypt, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.BadData { throws SOPGPException.CertCannotEncrypt, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.BadData {
try { try {
mGen.addEncryptionCertificate(parseCertificate(inputStream)); mGen.addEncryptionCertificate(parseCertificate(inputStream));
hasEncryptionMethod = true;
} catch (InvalidEncryptionKeyException e) { } catch (InvalidEncryptionKeyException e) {
throw new SOPGPException.CertCannotEncrypt("Certificate cannot encrypt", e); throw new SOPGPException.CertCannotEncrypt("Certificate cannot encrypt", e);
} }
@ -109,6 +112,10 @@ public class BCEncrypt
@Override @Override
public ReadyWithResult<EncryptionResult> plaintext(@NotNull InputStream inputStream) public ReadyWithResult<EncryptionResult> plaintext(@NotNull InputStream inputStream)
throws SOPGPException.KeyIsProtected { throws SOPGPException.KeyIsProtected {
if (!hasEncryptionMethod) {
throw new SOPGPException.MissingArg("No encryption method provided.");
}
for (OpenPGPKey key : signingKeys) { for (OpenPGPKey key : signingKeys) {
try { try {
mGen.addSigningKey(key, new SignatureParameters.Callback() { mGen.addSigningKey(key, new SignatureParameters.Callback() {