Working native image

This commit is contained in:
Paul Schaub 2024-12-12 19:22:46 +01:00
parent cc4870219a
commit 0123b0e5fe
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
12 changed files with 254 additions and 41 deletions

View file

@ -23,10 +23,10 @@ public abstract class AbstractBCOperation
return new SessionKey((byte) sessionKey.getAlgorithm(), sessionKey.getKey());
}
protected List<Verification> getVerifications(OpenPGPMessageInputStream.Result result)
protected List<Verification> getVerifications(List<OpenPGPSignature.OpenPGPDocumentSignature> signatures)
{
List<Verification> verifications = new ArrayList<>();
for (OpenPGPSignature.OpenPGPDocumentSignature sig : result.getSignatures())
for (OpenPGPSignature.OpenPGPDocumentSignature sig : signatures)
{
if (sig.isValid())
{

View file

@ -4,7 +4,6 @@ import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.util.io.Streams;
import org.jetbrains.annotations.NotNull;
import sop.Ready;
import sop.enums.ArmorLabel;
import sop.exception.SOPGPException;
import sop.operation.Armor;
@ -30,10 +29,4 @@ public class BCArmor
}
};
}
@NotNull
@Override
public Armor label(@NotNull ArmorLabel armorLabel) throws SOPGPException.UnsupportedOption {
throw new SOPGPException.UnsupportedOption("Custom labels not supported.");
}
}

View file

@ -26,8 +26,7 @@ public class BCDecrypt
private Date notBefore = new Date(Long.MAX_VALUE); // end of time
private Date notAfter = new Date(); // now
private final List<PGPSecretKeyRing> encryptionKeys = new ArrayList<>();
private final List<String> encryptionKeyPassphrases = new ArrayList<>();
private char[] keyPassword;
private final OpenPGPMessageProcessor processor = new OpenPGPMessageProcessor();
@ -35,7 +34,7 @@ public class BCDecrypt
@NotNull
@Override
public ReadyWithResult<DecryptionResult> ciphertext(@NotNull InputStream inputStream) throws SOPGPException.BadData, SOPGPException.MissingArg, SOPGPException.CannotDecrypt, SOPGPException.KeyIsProtected, IOException {
return new ReadyWithResult<DecryptionResult>() {
return new ReadyWithResult<>() {
@Override
public DecryptionResult writeTo(@NotNull OutputStream outputStream) throws IOException, SOPGPException {
try {
@ -45,7 +44,7 @@ public class BCDecrypt
OpenPGPMessageInputStream.Result result = mIn.getResult();
return new DecryptionResult(
getSessionKey(result),
getVerifications(result));
getVerifications(result.getSignatures()));
} catch (PGPException e) {
throw new RuntimeException(e);
}
@ -93,7 +92,7 @@ public class BCDecrypt
@Override
public Decrypt withKey(@NotNull InputStream inputStream) throws SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException {
OpenPGPKey key = OpenPGPKey.fromInputStream(inputStream);
processor.addDecryptionKey(key);
processor.addDecryptionKey(key, keyPassword);
return this;
}
@ -101,7 +100,7 @@ public class BCDecrypt
@Override
public Decrypt withKeyPassword(@NotNull byte[] bytes) throws SOPGPException.UnsupportedOption, SOPGPException.PasswordNotHumanReadable {
String passphrase = new String(bytes);
this.encryptionKeyPassphrases.add(passphrase);
this.keyPassword = passphrase.toCharArray();
return this;
}
}

View file

@ -26,6 +26,7 @@ public class BCDetachedSign
private final OpenPGPDetachedSignatureGenerator sigGen = new OpenPGPDetachedSignatureGenerator();
private boolean armored = true;
private char[] keyPassword = null;
@NotNull
@Override
@ -96,7 +97,7 @@ public class BCDetachedSign
public DetachedSign key(@NotNull InputStream inputStream) throws SOPGPException.KeyCannotSign, SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException {
try
{
sigGen.addSigningKey(OpenPGPKey.fromInputStream(inputStream), null);
sigGen.addSigningKey(OpenPGPKey.fromInputStream(inputStream), keyPassword);
}
catch (InvalidSigningKeyException e)
{
@ -111,6 +112,7 @@ public class BCDetachedSign
@Override
public DetachedSign withKeyPassword(@NotNull byte[] bytes) throws SOPGPException.UnsupportedOption, SOPGPException.PasswordNotHumanReadable {
keyPassword = new String(bytes).toCharArray();
return this;
}
}

View file

@ -56,16 +56,10 @@ public class BCDetachedVerify
public List<Verification> data(@NotNull InputStream inputStream) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData {
List<OpenPGPSignature.OpenPGPDocumentSignature> signatures = processor.verify(inputStream);
List<Verification> verifications = new ArrayList<>();
for (OpenPGPSignature.OpenPGPDocumentSignature signature : signatures)
List<Verification> verifications = getVerifications(signatures);
if (verifications.isEmpty())
{
if (signature.isValidAt(signature.getCreationTime()))
{
verifications.add(new Verification(
signature.getCreationTime(),
Hex.toHexString(signature.getIssuer().getKeyIdentifier().getFingerprint()),
Hex.toHexString(signature.getIssuerCertificate().getFingerprint())));
}
throw new SOPGPException.NoSignature();
}
return verifications;
}

View file

@ -2,6 +2,7 @@ package org.pgpainless.bouncycastle.sop.operation;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.openpgp.api.OpenPGPMessageGenerator;
import org.bouncycastle.openpgp.api.OpenPGPMessageOutputStream;
import org.bouncycastle.util.io.Streams;
@ -22,6 +23,7 @@ public class BCEncrypt
implements Encrypt {
private final OpenPGPMessageGenerator mGen;
private char[] keyPassword;
public BCEncrypt()
{
@ -45,12 +47,15 @@ public class BCEncrypt
@NotNull
@Override
public Encrypt signWith(@NotNull InputStream inputStream) throws SOPGPException.KeyCannotSign, SOPGPException.UnsupportedAsymmetricAlgo, SOPGPException.BadData, IOException {
OpenPGPKey key = OpenPGPKey.fromInputStream(inputStream);
mGen.addSigningKey(key, k -> keyPassword);
return this;
}
@NotNull
@Override
public Encrypt withKeyPassword(@NotNull byte[] bytes) throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
this.keyPassword = new String(bytes).toCharArray();
return this;
}

View file

@ -33,23 +33,33 @@ public class BCGenerateKey
public void writeTo(@NotNull OutputStream outputStream) throws IOException
{
OpenPGPV6KeyGenerator generator = new BcOpenPGPV6KeyGenerator(new Date());
OpenPGPKey key;
try
{
PGPSecretKeyRing keyRing = generator.ed25519x25519Key(userId, passphrase);
OpenPGPKey key = new OpenPGPKey(keyRing);
if (armor)
if (signOnly)
{
outputStream.write(key.toAsciiArmoredString().getBytes(StandardCharsets.UTF_8));
PGPSecretKeyRing keyRing = generator.signOnlyKey(passphrase);
key = new OpenPGPKey(keyRing);
}
else
{
keyRing.encode(outputStream);
PGPSecretKeyRing keyRing = generator.ed25519x25519Key(userId, passphrase);
key = new OpenPGPKey(keyRing);
}
}
catch (PGPException e)
{
throw new RuntimeException(e);
}
if (armor)
{
outputStream.write(key.toAsciiArmoredString().getBytes(StandardCharsets.UTF_8));
}
else
{
key.getPGPKeyRing().encode(outputStream);
}
}
};
}

View file

@ -17,9 +17,11 @@ import java.io.OutputStream;
public class BCInlineSign
extends AbstractBCOperation
implements InlineSign {
implements InlineSign
{
private final OpenPGPMessageGenerator mGen = new OpenPGPMessageGenerator();
private char[] keyPassword;
@NotNull
@Override
@ -52,12 +54,13 @@ public class BCInlineSign
@Override
public InlineSign key(@NotNull InputStream inputStream) throws SOPGPException.KeyCannotSign, SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException {
mGen.addSigningKey(OpenPGPKey.fromInputStream(inputStream));
mGen.addSigningKey(OpenPGPKey.fromInputStream(inputStream), k -> keyPassword);
return this;
}
@Override
public InlineSign withKeyPassword(@NotNull byte[] bytes) throws SOPGPException.UnsupportedOption, SOPGPException.PasswordNotHumanReadable {
this.keyPassword = new String(bytes).toCharArray();
return this;
}
}

View file

@ -26,7 +26,7 @@ public class BCInlineVerify
@NotNull
@Override
public ReadyWithResult<List<Verification>> data(@NotNull InputStream inputStream) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData {
return new ReadyWithResult<List<Verification>>() {
return new ReadyWithResult<>() {
@Override
public List<Verification> writeTo(@NotNull OutputStream outputStream) throws IOException, SOPGPException {
try {
@ -34,7 +34,12 @@ public class BCInlineVerify
Streams.pipeAll(mIn, outputStream);
mIn.close();
OpenPGPMessageInputStream.Result result = mIn.getResult();
return getVerifications(result);
List<Verification> verifications = getVerifications(result.getSignatures());
if (verifications.isEmpty())
{
throw new SOPGPException.NoSignature();
}
return verifications;
} catch (PGPException e) {
throw new RuntimeException(e);
}