mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-16 09:11:08 +01:00
Add SharedPGPCertificateDirectoryAdapter class
This commit is contained in:
parent
50243aa6b6
commit
3bc613f2ea
10 changed files with 236 additions and 38 deletions
8
pgp-cert-d-java/README.md
Normal file
8
pgp-cert-d-java/README.md
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<!--
|
||||
SPDX-FileCopyrightText: 2022 Paul Schaub <info@pgpainless.org>
|
||||
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
-->
|
||||
|
||||
# Shared PGP Certificate Directory for Java
|
||||
|
||||
|
|
@ -0,0 +1,136 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pgp.cert_d;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Iterator;
|
||||
|
||||
import pgp.cert_d.exception.BadDataException;
|
||||
import pgp.cert_d.exception.BadNameException;
|
||||
import pgp.certificate_store.Certificate;
|
||||
import pgp.certificate_store.CertificateStore;
|
||||
import pgp.certificate_store.MergeCallback;
|
||||
|
||||
/**
|
||||
* Adapter class used to adapt the {@link SharedPGPCertificateDirectory} for use with
|
||||
* {@link CertificateStore}.
|
||||
*/
|
||||
public class SharedPGPCertificateDirectoryAdapter
|
||||
implements CertificateStore {
|
||||
|
||||
private final SharedPGPCertificateDirectory directory;
|
||||
|
||||
/**
|
||||
* Create an adapter to use {@link SharedPGPCertificateDirectory} objects as {@link CertificateStore CertificateStores}.
|
||||
*
|
||||
* @param directory directory instance
|
||||
*/
|
||||
public SharedPGPCertificateDirectoryAdapter(SharedPGPCertificateDirectory directory) {
|
||||
this.directory = directory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate getCertificate(String identifier)
|
||||
throws IOException {
|
||||
SpecialName specialName = SpecialName.fromString(identifier);
|
||||
if (specialName != null) {
|
||||
try {
|
||||
return directory.get(specialName);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Unknown special name " + identifier, e);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return directory.get(identifier);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Invalid fingerprint or unknown special name " + identifier, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate getCertificateIfChanged(String identifier, String tag)
|
||||
throws IOException {
|
||||
SpecialName specialName = SpecialName.fromString(identifier);
|
||||
if (specialName != null) {
|
||||
try {
|
||||
return directory.getIfChanged(specialName, tag);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Unknown special name " + identifier, e);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return directory.getIfChanged(identifier, tag);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Invalid fingerprint or unknown special name " + identifier, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate insertCertificate(InputStream data, MergeCallback merge)
|
||||
throws IOException, InterruptedException {
|
||||
try {
|
||||
return directory.insert(data, merge);
|
||||
} catch (BadDataException e) {
|
||||
throw new IOException("Cannot insert certificate due to bad data", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate tryInsertCertificate(InputStream data, MergeCallback merge)
|
||||
throws IOException {
|
||||
try {
|
||||
return directory.tryInsert(data, merge);
|
||||
} catch (BadDataException e) {
|
||||
throw new IOException("Cannot insert certificate due to bad data", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate insertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
|
||||
throws IOException, InterruptedException {
|
||||
try {
|
||||
SpecialName specialNameEnum = SpecialName.fromString(specialName);
|
||||
if (specialNameEnum == null) {
|
||||
throw new IllegalArgumentException("Unknown special name " + specialName);
|
||||
}
|
||||
|
||||
return directory.insertSpecial(specialNameEnum, data, merge);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Unknown special name " + specialName);
|
||||
} catch (BadDataException e) {
|
||||
throw new IOException("Cannot insert certificate due to bad data", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate tryInsertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
|
||||
throws IOException {
|
||||
try {
|
||||
SpecialName specialNameEnum = SpecialName.fromString(specialName);
|
||||
if (specialNameEnum == null) {
|
||||
throw new IllegalArgumentException("Unknown special name " + specialName);
|
||||
}
|
||||
|
||||
return directory.tryInsertSpecial(specialNameEnum, data, merge);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Unknown special name " + specialName);
|
||||
} catch (BadDataException e) {
|
||||
throw new IOException("Cannot insert certificate due to bad data", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Certificate> getCertificates() {
|
||||
return directory.items();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<String> getFingerprints() {
|
||||
return directory.fingerprints();
|
||||
}
|
||||
}
|
||||
|
|
@ -21,7 +21,7 @@ import pgp.cert_d.exception.BadNameException;
|
|||
import pgp.cert_d.exception.NotAStoreException;
|
||||
import pgp.certificate_store.Certificate;
|
||||
import pgp.certificate_store.MergeCallback;
|
||||
import pgp.certificate_store.ParserBackend;
|
||||
import pgp.certificate_store.CertificateReaderBackend;
|
||||
|
||||
public class SharedPGPCertificateDirectoryImpl implements SharedPGPCertificateDirectory {
|
||||
|
||||
|
|
@ -29,16 +29,16 @@ public class SharedPGPCertificateDirectoryImpl implements SharedPGPCertificateDi
|
|||
private final Pattern openPgpV4FingerprintPattern = Pattern.compile("^[a-f0-9]{40}$");
|
||||
|
||||
private final LockingMechanism writeLock;
|
||||
private final ParserBackend parserBackend;
|
||||
private final CertificateReaderBackend certificateReaderBackend;
|
||||
|
||||
public SharedPGPCertificateDirectoryImpl(ParserBackend parserBackend)
|
||||
public SharedPGPCertificateDirectoryImpl(CertificateReaderBackend certificateReaderBackend)
|
||||
throws NotAStoreException {
|
||||
this(OSUtil.getDefaultBaseDir(), parserBackend);
|
||||
this(OSUtil.getDefaultBaseDir(), certificateReaderBackend);
|
||||
}
|
||||
|
||||
public SharedPGPCertificateDirectoryImpl(File baseDirectory, ParserBackend parserBackend)
|
||||
public SharedPGPCertificateDirectoryImpl(File baseDirectory, CertificateReaderBackend certificateReaderBackend)
|
||||
throws NotAStoreException {
|
||||
this.parserBackend = parserBackend;
|
||||
this.certificateReaderBackend = certificateReaderBackend;
|
||||
this.baseDirectory = baseDirectory;
|
||||
if (!baseDirectory.exists()) {
|
||||
if (!baseDirectory.mkdirs()) {
|
||||
|
|
@ -83,7 +83,7 @@ public class SharedPGPCertificateDirectoryImpl implements SharedPGPCertificateDi
|
|||
}
|
||||
FileInputStream fileIn = new FileInputStream(certFile);
|
||||
BufferedInputStream bufferedIn = new BufferedInputStream(fileIn);
|
||||
Certificate certificate = parserBackend.readCertificate(bufferedIn);
|
||||
Certificate certificate = certificateReaderBackend.readCertificate(bufferedIn);
|
||||
|
||||
if (!certificate.getFingerprint().equals(fingerprint)) {
|
||||
// TODO: Figure out more suitable exception
|
||||
|
|
@ -102,7 +102,7 @@ public class SharedPGPCertificateDirectoryImpl implements SharedPGPCertificateDi
|
|||
|
||||
FileInputStream fileIn = new FileInputStream(certFile);
|
||||
BufferedInputStream bufferedIn = new BufferedInputStream(fileIn);
|
||||
Certificate certificate = parserBackend.readCertificate(bufferedIn);
|
||||
Certificate certificate = certificateReaderBackend.readCertificate(bufferedIn);
|
||||
|
||||
return certificate;
|
||||
}
|
||||
|
|
@ -148,7 +148,7 @@ public class SharedPGPCertificateDirectoryImpl implements SharedPGPCertificateDi
|
|||
}
|
||||
|
||||
private Certificate _insert(InputStream data, MergeCallback merge) throws IOException, BadDataException {
|
||||
Certificate newCertificate = parserBackend.readCertificate(data);
|
||||
Certificate newCertificate = certificateReaderBackend.readCertificate(data);
|
||||
Certificate existingCertificate;
|
||||
File certFile;
|
||||
try {
|
||||
|
|
@ -209,7 +209,7 @@ public class SharedPGPCertificateDirectoryImpl implements SharedPGPCertificateDi
|
|||
}
|
||||
|
||||
private Certificate _insertSpecial(SpecialName specialName, InputStream data, MergeCallback merge) throws IOException, BadNameException, BadDataException {
|
||||
Certificate newCertificate = parserBackend.readCertificate(data);
|
||||
Certificate newCertificate = certificateReaderBackend.readCertificate(data);
|
||||
Certificate existingCertificate = get(specialName);
|
||||
File certFile = getCertFile(specialName);
|
||||
|
||||
|
|
@ -238,7 +238,7 @@ public class SharedPGPCertificateDirectoryImpl implements SharedPGPCertificateDi
|
|||
@Override
|
||||
Certificate get() {
|
||||
try {
|
||||
return parserBackend.readCertificate(new FileInputStream(certFile));
|
||||
return certificateReaderBackend.readCertificate(new FileInputStream(certFile));
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError("File got deleted.");
|
||||
}
|
||||
|
|
@ -267,7 +267,7 @@ public class SharedPGPCertificateDirectoryImpl implements SharedPGPCertificateDi
|
|||
@Override
|
||||
Certificate get() throws BadDataException {
|
||||
try {
|
||||
Certificate certificate = parserBackend.readCertificate(new FileInputStream(certFile));
|
||||
Certificate certificate = certificateReaderBackend.readCertificate(new FileInputStream(certFile));
|
||||
if (!(subdirectory.getName() + certFile.getName()).equals(certificate.getFingerprint())) {
|
||||
throw new BadDataException();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue