wkd-java/wkd-java-cli/src/main/java/pgp/wkd/cli/CertificateParserImpl.java

40 lines
1.6 KiB
Java
Raw Normal View History

2022-03-10 16:56:46 +01:00
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.wkd.cli;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.pgpainless.PGPainless;
import org.pgpainless.certificate_store.CertificateFactory;
import org.pgpainless.key.info.KeyRingInfo;
import pgp.certificate_store.Certificate;
import pgp.wkd.CertificateAndUserIds;
2022-03-17 15:27:28 +01:00
import pgp.wkd.discovery.CertificateParser;
2022-03-10 16:56:46 +01:00
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
2022-03-17 15:27:28 +01:00
public class CertificateParserImpl implements CertificateParser {
2022-03-10 16:56:46 +01:00
@Override
public List<CertificateAndUserIds> read(InputStream inputStream) throws IOException {
List<CertificateAndUserIds> certificatesAndUserIds = new ArrayList<>();
try {
PGPPublicKeyRingCollection certificates = PGPainless.readKeyRing().publicKeyRingCollection(inputStream);
for (PGPPublicKeyRing certificate : certificates) {
KeyRingInfo info = PGPainless.inspectKeyRing(certificate);
Certificate parsedCert = CertificateFactory.certificateFromPublicKeyRing(certificate);
List<String> userIds = info.getValidAndExpiredUserIds();
certificatesAndUserIds.add(new CertificateAndUserIds(parsedCert, userIds));
}
return certificatesAndUserIds;
} catch (PGPException e) {
throw new IOException("Cannot parse certificates.", e);
}
}
}