2022-08-23 15:19:01 +02:00
|
|
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2022-08-24 13:04:28 +02:00
|
|
|
package pgp.cert_d.dummy;
|
2022-08-23 15:19:01 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.bouncycastle.openpgp.PGPKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPUtil;
|
|
|
|
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
|
|
|
|
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
|
|
|
|
import org.bouncycastle.util.encoders.Hex;
|
|
|
|
import org.bouncycastle.util.io.Streams;
|
|
|
|
import pgp.certificate_store.certificate.Certificate;
|
|
|
|
import pgp.certificate_store.certificate.Key;
|
|
|
|
import pgp.certificate_store.certificate.KeyMaterial;
|
|
|
|
import pgp.certificate_store.certificate.KeyMaterialReaderBackend;
|
|
|
|
import pgp.certificate_store.exception.BadDataException;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class TestKeyMaterialReaderBackend implements KeyMaterialReaderBackend {
|
|
|
|
|
|
|
|
KeyFingerPrintCalculator fpCalc = new BcKeyFingerprintCalculator();
|
|
|
|
|
|
|
|
@Override
|
2022-08-24 13:04:28 +02:00
|
|
|
public KeyMaterial read(InputStream data, Long tag) throws IOException, BadDataException {
|
2022-08-23 15:19:01 +02:00
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
Streams.pipeAll(data, out);
|
|
|
|
|
|
|
|
try {
|
2022-08-24 13:04:28 +02:00
|
|
|
return readKey(new ByteArrayInputStream(out.toByteArray()), tag);
|
2022-08-23 15:19:01 +02:00
|
|
|
} catch (IOException | PGPException e) {
|
|
|
|
try {
|
2022-08-24 13:04:28 +02:00
|
|
|
return readCertificate(new ByteArrayInputStream(out.toByteArray()), tag);
|
2022-08-23 15:19:01 +02:00
|
|
|
} catch (IOException e1) {
|
|
|
|
throw new BadDataException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:04:28 +02:00
|
|
|
private Key readKey(InputStream inputStream, Long tag) throws IOException, PGPException {
|
2022-08-23 15:19:01 +02:00
|
|
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
|
Streams.pipeAll(inputStream, buffer);
|
|
|
|
inputStream.close();
|
|
|
|
|
|
|
|
InputStream decoderStream = PGPUtil.getDecoderStream(new ByteArrayInputStream(buffer.toByteArray()));
|
|
|
|
|
|
|
|
PGPSecretKeyRing secretKeys = new PGPSecretKeyRing(decoderStream, fpCalc);
|
|
|
|
PGPPublicKeyRing cert = extractCert(secretKeys);
|
|
|
|
ByteArrayInputStream encoded = new ByteArrayInputStream(cert.getEncoded());
|
2022-08-24 13:04:28 +02:00
|
|
|
Certificate certificate = readCertificate(encoded, tag);
|
2022-08-23 15:19:01 +02:00
|
|
|
|
2022-08-24 13:04:28 +02:00
|
|
|
return new Key(buffer.toByteArray(), certificate, tag);
|
2022-08-23 15:19:01 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 13:04:28 +02:00
|
|
|
private Certificate readCertificate(InputStream inputStream, Long tag) throws IOException {
|
2022-08-23 15:19:01 +02:00
|
|
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
|
Streams.pipeAll(inputStream, buffer);
|
|
|
|
ByteArrayInputStream in = new ByteArrayInputStream(buffer.toByteArray());
|
|
|
|
InputStream decoderStream = PGPUtil.getDecoderStream(in);
|
|
|
|
|
|
|
|
PGPPublicKeyRing cert = new PGPPublicKeyRing(decoderStream, fpCalc);
|
2022-08-24 13:04:28 +02:00
|
|
|
String fingerprint = Hex.toHexString(cert.getPublicKey().getFingerprint()).toLowerCase();
|
|
|
|
List<Long> subKeyIds = getSubkeyIds(cert);
|
|
|
|
return new Certificate(buffer.toByteArray(), fingerprint, subKeyIds, tag);
|
2022-08-23 15:19:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private PGPPublicKeyRing extractCert(PGPSecretKeyRing secretKeys) {
|
|
|
|
List<PGPPublicKey> publicKeyList = new ArrayList<>();
|
|
|
|
Iterator<PGPPublicKey> publicKeyIterator = secretKeys.getPublicKeys();
|
|
|
|
while (publicKeyIterator.hasNext()) {
|
|
|
|
publicKeyList.add(publicKeyIterator.next());
|
|
|
|
}
|
2022-08-24 13:04:28 +02:00
|
|
|
return new PGPPublicKeyRing(publicKeyList);
|
2022-08-23 15:19:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static List<Long> getSubkeyIds(PGPKeyRing keyRing) {
|
|
|
|
List<Long> keyIds = new ArrayList<>();
|
|
|
|
Iterator<PGPPublicKey> keys = keyRing.getPublicKeys();
|
|
|
|
while (keys.hasNext()) {
|
|
|
|
keyIds.add(keys.next().getKeyID());
|
|
|
|
}
|
|
|
|
return keyIds;
|
|
|
|
}
|
|
|
|
}
|