2022-06-07 08:55:10 +02:00
|
|
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package org.pgpainless.sop;
|
|
|
|
|
2022-06-09 00:44:09 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
|
|
|
import org.bouncycastle.util.io.Streams;
|
|
|
|
import org.pgpainless.PGPainless;
|
|
|
|
import org.pgpainless.algorithm.DocumentSignatureType;
|
|
|
|
import org.pgpainless.encryption_signing.EncryptionStream;
|
|
|
|
import org.pgpainless.encryption_signing.ProducerOptions;
|
|
|
|
import org.pgpainless.encryption_signing.SigningOptions;
|
|
|
|
import org.pgpainless.exception.KeyException;
|
2022-06-10 17:43:51 +02:00
|
|
|
import org.pgpainless.key.OpenPgpFingerprint;
|
|
|
|
import org.pgpainless.key.info.KeyRingInfo;
|
2022-06-09 00:44:09 +02:00
|
|
|
import org.pgpainless.util.Passphrase;
|
2022-06-10 17:43:51 +02:00
|
|
|
import sop.Ready;
|
2022-06-07 08:55:10 +02:00
|
|
|
import sop.enums.InlineSignAs;
|
|
|
|
import sop.exception.SOPGPException;
|
|
|
|
import sop.operation.InlineSign;
|
2023-11-15 13:39:26 +01:00
|
|
|
import sop.util.UTF8Util;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
2022-06-07 08:55:10 +02:00
|
|
|
|
2023-01-16 19:38:52 +01:00
|
|
|
/**
|
|
|
|
* Implementation of the <pre>inline-sign</pre> operation using PGPainless.
|
|
|
|
*/
|
2022-06-07 08:55:10 +02:00
|
|
|
public class InlineSignImpl implements InlineSign {
|
2022-06-09 00:44:09 +02:00
|
|
|
|
|
|
|
private boolean armor = true;
|
2022-11-11 13:46:05 +01:00
|
|
|
private InlineSignAs mode = InlineSignAs.binary;
|
2022-06-09 00:44:09 +02:00
|
|
|
private final SigningOptions signingOptions = new SigningOptions();
|
|
|
|
private final MatchMakingSecretKeyRingProtector protector = new MatchMakingSecretKeyRingProtector();
|
2022-06-10 17:43:51 +02:00
|
|
|
private final List<PGPSecretKeyRing> signingKeys = new ArrayList<>();
|
2022-06-09 00:44:09 +02:00
|
|
|
|
2022-06-07 08:55:10 +02:00
|
|
|
@Override
|
2023-11-15 13:39:26 +01:00
|
|
|
@Nonnull
|
|
|
|
public InlineSign mode(@Nonnull InlineSignAs mode) throws SOPGPException.UnsupportedOption {
|
2022-06-09 00:44:09 +02:00
|
|
|
this.mode = mode;
|
|
|
|
return this;
|
2022-06-07 08:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-11-15 13:39:26 +01:00
|
|
|
@Nonnull
|
2022-06-09 00:44:09 +02:00
|
|
|
public InlineSign noArmor() {
|
|
|
|
this.armor = false;
|
|
|
|
return this;
|
2022-06-07 08:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-11-15 13:39:26 +01:00
|
|
|
@Nonnull
|
|
|
|
public InlineSign key(@Nonnull InputStream keyIn) throws SOPGPException.KeyCannotSign, SOPGPException.BadData, IOException {
|
2022-11-09 22:01:20 +01:00
|
|
|
PGPSecretKeyRingCollection keys = KeyReader.readSecretKeys(keyIn, true);
|
|
|
|
for (PGPSecretKeyRing key : keys) {
|
|
|
|
KeyRingInfo info = PGPainless.inspectKeyRing(key);
|
|
|
|
if (!info.isUsableForSigning()) {
|
|
|
|
throw new SOPGPException.KeyCannotSign("Key " + info.getFingerprint() + " does not have valid, signing capable subkeys.");
|
2022-06-09 00:44:09 +02:00
|
|
|
}
|
2022-11-09 22:01:20 +01:00
|
|
|
protector.addSecretKey(key);
|
|
|
|
signingKeys.add(key);
|
2022-06-09 00:44:09 +02:00
|
|
|
}
|
|
|
|
return this;
|
2022-06-07 08:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-11-15 13:39:26 +01:00
|
|
|
@Nonnull
|
|
|
|
public InlineSign withKeyPassword(@Nonnull byte[] password) {
|
|
|
|
String string = new String(password, UTF8Util.UTF8);
|
2022-06-09 00:44:09 +02:00
|
|
|
protector.addPassphrase(Passphrase.fromPassword(string));
|
|
|
|
return this;
|
2022-06-07 08:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-11-15 13:39:26 +01:00
|
|
|
@Nonnull
|
|
|
|
public Ready data(@Nonnull InputStream data) throws SOPGPException.KeyIsProtected, SOPGPException.ExpectedText {
|
2022-06-10 17:43:51 +02:00
|
|
|
for (PGPSecretKeyRing key : signingKeys) {
|
|
|
|
try {
|
2022-11-11 13:46:05 +01:00
|
|
|
if (mode == InlineSignAs.clearsigned) {
|
2022-12-13 16:42:51 +01:00
|
|
|
signingOptions.addDetachedSignature(protector, key, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT);
|
2022-06-10 17:43:51 +02:00
|
|
|
} else {
|
|
|
|
signingOptions.addInlineSignature(protector, key, modeToSigType(mode));
|
|
|
|
}
|
|
|
|
} catch (KeyException.UnacceptableSigningKeyException | KeyException.MissingSecretKeyException e) {
|
|
|
|
throw new SOPGPException.KeyCannotSign("Key " + OpenPgpFingerprint.of(key) + " cannot sign.", e);
|
|
|
|
} catch (PGPException e) {
|
|
|
|
throw new SOPGPException.KeyIsProtected("Key " + OpenPgpFingerprint.of(key) + " cannot be unlocked.", e);
|
|
|
|
}
|
|
|
|
}
|
2022-06-09 00:44:09 +02:00
|
|
|
|
|
|
|
ProducerOptions producerOptions = ProducerOptions.sign(signingOptions);
|
2022-11-11 13:46:05 +01:00
|
|
|
if (mode == InlineSignAs.clearsigned) {
|
2022-06-09 00:44:09 +02:00
|
|
|
producerOptions.setCleartextSigned();
|
|
|
|
producerOptions.setAsciiArmor(true);
|
|
|
|
} else {
|
|
|
|
producerOptions.setAsciiArmor(armor);
|
|
|
|
}
|
|
|
|
|
2022-06-10 17:43:51 +02:00
|
|
|
return new Ready() {
|
2022-06-09 00:44:09 +02:00
|
|
|
@Override
|
2023-11-15 13:39:26 +01:00
|
|
|
public void writeTo(@Nonnull OutputStream outputStream) throws IOException, SOPGPException.NoSignature {
|
2022-06-09 00:44:09 +02:00
|
|
|
try {
|
|
|
|
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
|
|
|
|
.onOutputStream(outputStream)
|
|
|
|
.withOptions(producerOptions);
|
|
|
|
|
|
|
|
if (signingStream.isClosed()) {
|
|
|
|
throw new IllegalStateException("EncryptionStream is already closed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Streams.pipeAll(data, signingStream);
|
|
|
|
signingStream.close();
|
|
|
|
|
|
|
|
// forget passphrases
|
|
|
|
protector.clear();
|
|
|
|
} catch (PGPException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private static DocumentSignatureType modeToSigType(InlineSignAs mode) {
|
2022-11-11 13:46:05 +01:00
|
|
|
return mode == InlineSignAs.binary ? DocumentSignatureType.BINARY_DOCUMENT
|
2022-06-09 00:44:09 +02:00
|
|
|
: DocumentSignatureType.CANONICAL_TEXT_DOCUMENT;
|
2022-06-07 08:55:10 +02:00
|
|
|
}
|
|
|
|
}
|