Convert certain IOExceptions into BadDataException

This commit is contained in:
Paul Schaub 2022-08-25 12:56:10 +02:00
parent 0cc7c64412
commit 5e5e3dd760
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 57 additions and 1 deletions

View file

@ -19,7 +19,17 @@ public class KeyMaterialReader implements KeyMaterialReaderBackend {
@Override @Override
public KeyMaterial read(InputStream data, Long tag) throws IOException, BadDataException { public KeyMaterial read(InputStream data, Long tag) throws IOException, BadDataException {
PGPKeyRing keyMaterial = PGPainless.readKeyRing().keyRing(data); PGPKeyRing keyMaterial;
try {
keyMaterial = PGPainless.readKeyRing().keyRing(data);
} catch (IOException e) {
if (e.getMessage().contains("unknown object in stream") ||
e.getMessage().contains("unexpected end of file in armored stream.")) {
throw new BadDataException();
} else {
throw e;
}
}
if (keyMaterial instanceof PGPSecretKeyRing) { if (keyMaterial instanceof PGPSecretKeyRing) {
return KeyFactory.keyFromSecretKeyRing((PGPSecretKeyRing) keyMaterial, tag); return KeyFactory.keyFromSecretKeyRing((PGPSecretKeyRing) keyMaterial, tag);
} else if (keyMaterial instanceof PGPPublicKeyRing) { } else if (keyMaterial instanceof PGPPublicKeyRing) {

View file

@ -0,0 +1,46 @@
package org.pgpainless.certificate_store;
import org.junit.jupiter.api.Test;
public class CertificateAndKeyFactoryTest {
private static final String KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
"Comment: B21A ABBF 15DF 0FDA 3742 4DE9 AD00 8384 AD0A 064C\n" +
"Comment: Volodymyr Zelenskyy <zelenskyy@gov.ua>\n" +
"\n" +
"xVgEYwdKchYJKwYBBAHaRw8BAQdARSvx9BDpV0AoNYTmN/wrZXQAB7VzOV0rKEQc\n" +
"OkhbP5wAAP0QE5FCIOvzea7wu3Yw3LDMmOOgMaWXngYp0948VPP2+xM7wsARBB8W\n" +
"CgCDBYJjB0pyBYkFn6YAAwsJBwkQrQCDhK0KBkxHFAAAAAAAHgAgc2FsdEBub3Rh\n" +
"dGlvbnMuc2V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