2022-11-09 22:01:20 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package org.pgpainless.sop;
|
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
2024-03-17 15:53:07 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPRuntimeOperationException;
|
2022-11-09 22:01:20 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
|
|
|
import org.pgpainless.PGPainless;
|
2024-03-17 15:53:07 +01:00
|
|
|
import org.pgpainless.key.collection.PGPKeyRingCollection;
|
2022-11-09 22:01:20 +01:00
|
|
|
import sop.exception.SOPGPException;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
2023-01-16 19:38:52 +01:00
|
|
|
/**
|
|
|
|
* Reader for OpenPGP keys and certificates with error matching according to the SOP spec.
|
|
|
|
*/
|
2022-11-09 22:01:20 +01:00
|
|
|
class KeyReader {
|
|
|
|
|
|
|
|
static PGPSecretKeyRingCollection readSecretKeys(InputStream keyInputStream, boolean requireContent)
|
|
|
|
throws IOException, SOPGPException.BadData {
|
|
|
|
PGPSecretKeyRingCollection keys;
|
|
|
|
try {
|
|
|
|
keys = PGPainless.readKeyRing().secretKeyRingCollection(keyInputStream);
|
|
|
|
} catch (IOException e) {
|
|
|
|
String message = e.getMessage();
|
|
|
|
if (message == null) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
if (message.startsWith("unknown object in stream:") ||
|
|
|
|
message.startsWith("invalid header encountered")) {
|
|
|
|
throw new SOPGPException.BadData(e);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2023-05-16 16:48:21 +02:00
|
|
|
if (requireContent && keys.size() == 0) {
|
2022-11-09 22:01:20 +01:00
|
|
|
throw new SOPGPException.BadData(new PGPException("No key data found."));
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2022-11-24 22:14:06 +01:00
|
|
|
static PGPPublicKeyRingCollection readPublicKeys(InputStream certIn, boolean requireContent)
|
|
|
|
throws IOException {
|
2024-03-17 15:53:07 +01:00
|
|
|
PGPKeyRingCollection certs;
|
2022-11-09 22:01:20 +01:00
|
|
|
try {
|
2024-03-17 15:53:07 +01:00
|
|
|
certs = PGPainless.readKeyRing().keyRingCollection(certIn, false);
|
2022-11-09 22:01:20 +01:00
|
|
|
} catch (IOException e) {
|
2023-08-01 14:27:49 +02:00
|
|
|
String msg = e.getMessage();
|
|
|
|
if (msg != null && (msg.startsWith("unknown object in stream:") || msg.startsWith("invalid header encountered"))) {
|
2022-11-09 22:01:20 +01:00
|
|
|
throw new SOPGPException.BadData(e);
|
|
|
|
}
|
|
|
|
throw e;
|
2024-03-17 15:53:07 +01:00
|
|
|
} catch (PGPRuntimeOperationException e) {
|
|
|
|
throw new SOPGPException.BadData(e);
|
2022-11-09 22:01:20 +01:00
|
|
|
}
|
2024-03-17 15:53:07 +01:00
|
|
|
if (certs.getPgpSecretKeyRingCollection().size() != 0) {
|
|
|
|
throw new SOPGPException.BadData("Secret key components encountered, while certificates were expected.");
|
|
|
|
}
|
|
|
|
if (requireContent && certs.getPgpPublicKeyRingCollection().size() == 0) {
|
2022-11-09 22:01:20 +01:00
|
|
|
throw new SOPGPException.BadData(new PGPException("No cert data found."));
|
|
|
|
}
|
2024-03-17 15:53:07 +01:00
|
|
|
return certs.getPgpPublicKeyRingCollection();
|
2022-11-09 22:01:20 +01:00
|
|
|
}
|
|
|
|
}
|