mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-10 18:59:39 +02:00
Base PGPainlessCLI on new sop-java module
* Rename pgpainless-sop -> pgpainless-cli * Introduce sop-java (implementation-independent SOP API) * Introduce sop-java-picocli (CLI frontend for sop-java) * Introduce pgpainless-sop (implementation of sop-java using PGPainless) * Rework pgpainless-cli (plugs pgpainless-sop into sop-java-picocli)
This commit is contained in:
parent
2ba782c451
commit
8cf5347b52
112 changed files with 6146 additions and 1303 deletions
|
@ -16,6 +16,7 @@
|
|||
package org.pgpainless.decryption_verification;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
|
@ -76,8 +77,10 @@ public final class DecryptionStreamFactory {
|
|||
private final ConsumerOptions options;
|
||||
|
||||
private final OpenPgpMetadata.Builder resultBuilder = OpenPgpMetadata.getBuilder();
|
||||
private static final PGPContentVerifierBuilderProvider verifierBuilderProvider = ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider();
|
||||
private static final KeyFingerPrintCalculator keyFingerprintCalculator = ImplementationFactory.getInstance().getKeyFingerprintCalculator();
|
||||
private static final PGPContentVerifierBuilderProvider verifierBuilderProvider =
|
||||
ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider();
|
||||
private static final KeyFingerPrintCalculator keyFingerprintCalculator =
|
||||
ImplementationFactory.getInstance().getKeyFingerprintCalculator();
|
||||
private final Map<OpenPgpV4Fingerprint, OnePassSignature> verifiableOnePassSignatures = new HashMap<>();
|
||||
private final List<IntegrityProtectedInputStream> integrityProtectedStreams = new ArrayList<>();
|
||||
|
||||
|
@ -109,11 +112,23 @@ public final class DecryptionStreamFactory {
|
|||
try {
|
||||
// Parse OpenPGP message
|
||||
inputStream = factory.processPGPPackets(objectFactory, 1);
|
||||
} catch (MissingLiteralDataException e) {
|
||||
// Not an OpenPGP message. Reset the buffered stream to parse the message as arbitrary binary data
|
||||
} catch (EOFException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (MissingLiteralDataException e) {
|
||||
// Not an OpenPGP message.
|
||||
// Reset the buffered stream to parse the message as arbitrary binary data
|
||||
// to allow for detached signature verification.
|
||||
bufferedIn.reset();
|
||||
inputStream = bufferedIn;
|
||||
} catch (IOException e) {
|
||||
if (e.getMessage().contains("invalid armor")) {
|
||||
// We falsely assumed the data to be armored.
|
||||
bufferedIn.reset();
|
||||
inputStream = bufferedIn;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
return new DecryptionStream(inputStream, factory.resultBuilder, factory.integrityProtectedStreams);
|
||||
|
@ -146,7 +161,9 @@ public final class DecryptionStreamFactory {
|
|||
throws PGPException, IOException {
|
||||
LOGGER.log(LEVEL, "Encountered PGPEncryptedDataList");
|
||||
InputStream decryptedDataStream = decrypt(pgpEncryptedDataList);
|
||||
return processPGPPackets(new PGPObjectFactory(PGPUtil.getDecoderStream(decryptedDataStream), keyFingerprintCalculator), ++depth);
|
||||
InputStream decodedDataStream = PGPUtil.getDecoderStream(decryptedDataStream);
|
||||
PGPObjectFactory factory = new PGPObjectFactory(decodedDataStream, keyFingerprintCalculator);
|
||||
return processPGPPackets(factory, ++depth);
|
||||
}
|
||||
|
||||
private InputStream processPGPCompressedData(PGPCompressedData pgpCompressedData, int depth)
|
||||
|
@ -155,8 +172,9 @@ public final class DecryptionStreamFactory {
|
|||
LOGGER.log(LEVEL, "Encountered PGPCompressedData: " + compressionAlgorithm);
|
||||
resultBuilder.setCompressionAlgorithm(compressionAlgorithm);
|
||||
|
||||
InputStream dataStream = pgpCompressedData.getDataStream();
|
||||
PGPObjectFactory objectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(dataStream), keyFingerprintCalculator);
|
||||
InputStream inflatedDataStream = pgpCompressedData.getDataStream();
|
||||
InputStream decodedDataStream = PGPUtil.getDecoderStream(inflatedDataStream);
|
||||
PGPObjectFactory objectFactory = new PGPObjectFactory(decodedDataStream, keyFingerprintCalculator);
|
||||
|
||||
return processPGPPackets(objectFactory, ++depth);
|
||||
}
|
||||
|
@ -171,11 +189,10 @@ public final class DecryptionStreamFactory {
|
|||
private InputStream processPGPLiteralData(@Nonnull PGPObjectFactory objectFactory, PGPLiteralData pgpLiteralData) {
|
||||
LOGGER.log(LEVEL, "Found PGPLiteralData");
|
||||
InputStream literalDataInputStream = pgpLiteralData.getInputStream();
|
||||
OpenPgpMetadata.FileInfo fileInfo = new OpenPgpMetadata.FileInfo(
|
||||
pgpLiteralData.getFileName(),
|
||||
pgpLiteralData.getModificationTime(),
|
||||
StreamEncoding.fromCode(pgpLiteralData.getFormat()));
|
||||
resultBuilder.setFileInfo(fileInfo);
|
||||
|
||||
resultBuilder.setFileName(pgpLiteralData.getFileName())
|
||||
.setModificationDate(pgpLiteralData.getModificationTime())
|
||||
.setFileEncoding(StreamEncoding.fromCode(pgpLiteralData.getFormat()));
|
||||
|
||||
if (verifiableOnePassSignatures.isEmpty()) {
|
||||
LOGGER.log(LEVEL, "No OnePassSignatures found -> We are done");
|
||||
|
@ -226,9 +243,6 @@ public final class DecryptionStreamFactory {
|
|||
|
||||
// data is public key encrypted
|
||||
else if (encryptedData instanceof PGPPublicKeyEncryptedData) {
|
||||
if (options.getDecryptionKeys().isEmpty()) {
|
||||
|
||||
}
|
||||
PGPPublicKeyEncryptedData publicKeyEncryptedData = (PGPPublicKeyEncryptedData) encryptedData;
|
||||
long keyId = publicKeyEncryptedData.getKeyID();
|
||||
if (!options.getDecryptionKeys().isEmpty()) {
|
||||
|
|
|
@ -24,6 +24,8 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPLiteralData;
|
||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
|
@ -44,7 +46,9 @@ public class OpenPgpMetadata {
|
|||
private final List<DetachedSignature> detachedSignatures;
|
||||
private final SymmetricKeyAlgorithm symmetricKeyAlgorithm;
|
||||
private final CompressionAlgorithm compressionAlgorithm;
|
||||
private final FileInfo fileInfo;
|
||||
private final String fileName;
|
||||
private final Date modificationDate;
|
||||
private final StreamEncoding fileEncoding;
|
||||
|
||||
public OpenPgpMetadata(Set<Long> recipientKeyIds,
|
||||
SubkeyIdentifier decryptionKey,
|
||||
|
@ -52,7 +56,9 @@ public class OpenPgpMetadata {
|
|||
CompressionAlgorithm algorithm,
|
||||
List<OnePassSignature> onePassSignatures,
|
||||
List<DetachedSignature> detachedSignatures,
|
||||
FileInfo fileInfo) {
|
||||
String fileName,
|
||||
Date modificationDate,
|
||||
StreamEncoding fileEncoding) {
|
||||
|
||||
this.recipientKeyIds = Collections.unmodifiableSet(recipientKeyIds);
|
||||
this.decryptionKey = decryptionKey;
|
||||
|
@ -60,7 +66,9 @@ public class OpenPgpMetadata {
|
|||
this.compressionAlgorithm = algorithm;
|
||||
this.detachedSignatures = Collections.unmodifiableList(detachedSignatures);
|
||||
this.onePassSignatures = Collections.unmodifiableList(onePassSignatures);
|
||||
this.fileInfo = fileInfo;
|
||||
this.fileName = fileName;
|
||||
this.modificationDate = modificationDate;
|
||||
this.fileEncoding = fileEncoding;
|
||||
}
|
||||
|
||||
public Set<Long> getRecipientKeyIds() {
|
||||
|
@ -148,12 +156,59 @@ public class OpenPgpMetadata {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return information about the encrypted/signed file.
|
||||
*
|
||||
* @deprecated use {@link #getFileName()}, {@link #getModificationDate()} and {@link #getFileEncoding()} instead.
|
||||
* @return file info
|
||||
*/
|
||||
@Deprecated
|
||||
public FileInfo getFileInfo() {
|
||||
return fileInfo;
|
||||
return new FileInfo(
|
||||
getFileName(),
|
||||
getModificationDate(),
|
||||
getFileEncoding()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the encrypted / signed file.
|
||||
*
|
||||
* @return file name
|
||||
*/
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true, if the encrypted data is intended for your eyes only.
|
||||
*
|
||||
* @return true if for-your-eyes-only
|
||||
*/
|
||||
public boolean isForYourEyesOnly() {
|
||||
return PGPLiteralData.CONSOLE.equals(getFileName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the modification date of the encrypted / signed file.
|
||||
*
|
||||
* @return modification date
|
||||
*/
|
||||
public Date getModificationDate() {
|
||||
return modificationDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the encoding format of the encrypted / signed file.
|
||||
*
|
||||
* @return encoding
|
||||
*/
|
||||
public StreamEncoding getFileEncoding() {
|
||||
return fileEncoding;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static class FileInfo {
|
||||
public static final String FOR_YOUR_EYES_ONLY = PGPLiteralData.CONSOLE;
|
||||
|
||||
protected final String fileName;
|
||||
protected final Date modificationDate;
|
||||
|
@ -165,22 +220,10 @@ public class OpenPgpMetadata {
|
|||
this.streamEncoding = streamEncoding;
|
||||
}
|
||||
|
||||
public static FileInfo binaryStream() {
|
||||
return new FileInfo("", null, StreamEncoding.BINARY);
|
||||
}
|
||||
|
||||
public static FileInfo forYourEyesOnly() {
|
||||
return new FileInfo(FOR_YOUR_EYES_ONLY, null, StreamEncoding.BINARY);
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public boolean isForYourEyesOnly() {
|
||||
return FOR_YOUR_EYES_ONLY.equals(fileName);
|
||||
}
|
||||
|
||||
public Date getModificationDate() {
|
||||
return modificationDate;
|
||||
}
|
||||
|
@ -243,7 +286,9 @@ public class OpenPgpMetadata {
|
|||
private final List<OnePassSignature> onePassSignatures = new ArrayList<>();
|
||||
private SymmetricKeyAlgorithm symmetricKeyAlgorithm = SymmetricKeyAlgorithm.NULL;
|
||||
private CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.UNCOMPRESSED;
|
||||
private FileInfo fileInfo;
|
||||
private String fileName;
|
||||
private StreamEncoding fileEncoding;
|
||||
private Date modificationDate;
|
||||
|
||||
public Builder addRecipientKeyId(Long keyId) {
|
||||
this.recipientFingerprints.add(keyId);
|
||||
|
@ -269,6 +314,21 @@ public class OpenPgpMetadata {
|
|||
return this;
|
||||
}
|
||||
|
||||
public Builder setFileName(@Nonnull String fileName) {
|
||||
this.fileName = fileName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setModificationDate(Date modificationDate) {
|
||||
this.modificationDate = modificationDate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setFileEncoding(StreamEncoding encoding) {
|
||||
this.fileEncoding = encoding;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void addDetachedSignature(DetachedSignature signature) {
|
||||
this.detachedSignatures.add(signature);
|
||||
}
|
||||
|
@ -277,15 +337,10 @@ public class OpenPgpMetadata {
|
|||
this.onePassSignatures.add(onePassSignature);
|
||||
}
|
||||
|
||||
public Builder setFileInfo(FileInfo fileInfo) {
|
||||
this.fileInfo = fileInfo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OpenPgpMetadata build() {
|
||||
return new OpenPgpMetadata(recipientFingerprints, decryptionKey,
|
||||
symmetricKeyAlgorithm, compressionAlgorithm,
|
||||
onePassSignatures, detachedSignatures, fileInfo);
|
||||
onePassSignatures, detachedSignatures, fileName, modificationDate, fileEncoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,11 +16,16 @@
|
|||
package org.pgpainless.encryption_signing;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPLiteralData;
|
||||
import org.bouncycastle.openpgp.PGPSignature;
|
||||
import org.pgpainless.algorithm.CompressionAlgorithm;
|
||||
import org.pgpainless.algorithm.StreamEncoding;
|
||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||
import org.pgpainless.decryption_verification.OpenPgpMetadata;
|
||||
import org.pgpainless.key.SubkeyIdentifier;
|
||||
|
@ -33,18 +38,24 @@ public final class EncryptionResult {
|
|||
|
||||
private final MultiMap<SubkeyIdentifier, PGPSignature> detachedSignatures;
|
||||
private final Set<SubkeyIdentifier> recipients;
|
||||
private final OpenPgpMetadata.FileInfo fileInfo;
|
||||
private final String fileName;
|
||||
private final Date modificationDate;
|
||||
private final StreamEncoding fileEncoding;
|
||||
|
||||
private EncryptionResult(SymmetricKeyAlgorithm encryptionAlgorithm,
|
||||
CompressionAlgorithm compressionAlgorithm,
|
||||
MultiMap<SubkeyIdentifier, PGPSignature> detachedSignatures,
|
||||
Set<SubkeyIdentifier> recipients,
|
||||
OpenPgpMetadata.FileInfo fileInfo) {
|
||||
String fileName,
|
||||
Date modificationDate,
|
||||
StreamEncoding encoding) {
|
||||
this.encryptionAlgorithm = encryptionAlgorithm;
|
||||
this.compressionAlgorithm = compressionAlgorithm;
|
||||
this.detachedSignatures = detachedSignatures;
|
||||
this.recipients = Collections.unmodifiableSet(recipients);
|
||||
this.fileInfo = fileInfo;
|
||||
this.fileName = fileName;
|
||||
this.modificationDate = modificationDate;
|
||||
this.fileEncoding = encoding;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
|
@ -68,14 +79,37 @@ public final class EncryptionResult {
|
|||
return recipients;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return information about the encrypted / signed data.
|
||||
*
|
||||
* @deprecated use {@link #getFileName()}, {@link #getModificationDate()} and {@link #getFileEncoding()} instead.
|
||||
* @return info
|
||||
*/
|
||||
@Deprecated
|
||||
public OpenPgpMetadata.FileInfo getFileInfo() {
|
||||
return fileInfo;
|
||||
return new OpenPgpMetadata.FileInfo(getFileName(), getModificationDate(), getFileEncoding());
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public Date getModificationDate() {
|
||||
return modificationDate;
|
||||
}
|
||||
|
||||
public StreamEncoding getFileEncoding() {
|
||||
return fileEncoding;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public boolean isForYourEyesOnly() {
|
||||
return PGPLiteralData.CONSOLE.equals(getFileName());
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private SymmetricKeyAlgorithm encryptionAlgorithm;
|
||||
|
@ -83,7 +117,9 @@ public final class EncryptionResult {
|
|||
|
||||
private final MultiMap<SubkeyIdentifier, PGPSignature> detachedSignatures = new MultiMap<>();
|
||||
private Set<SubkeyIdentifier> recipients = new HashSet<>();
|
||||
private OpenPgpMetadata.FileInfo fileInfo;
|
||||
private String fileName = "";
|
||||
private Date modificationDate = new Date(0L); // NOW
|
||||
private StreamEncoding encoding = StreamEncoding.BINARY;
|
||||
|
||||
public Builder setEncryptionAlgorithm(SymmetricKeyAlgorithm encryptionAlgorithm) {
|
||||
this.encryptionAlgorithm = encryptionAlgorithm;
|
||||
|
@ -105,8 +141,18 @@ public final class EncryptionResult {
|
|||
return this;
|
||||
}
|
||||
|
||||
public Builder setFileInfo(OpenPgpMetadata.FileInfo fileInfo) {
|
||||
this.fileInfo = fileInfo;
|
||||
public Builder setFileName(@Nonnull String fileName) {
|
||||
this.fileName = fileName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setModificationDate(@Nonnull Date modificationDate) {
|
||||
this.modificationDate = modificationDate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setFileEncoding(StreamEncoding fileEncoding) {
|
||||
this.encoding = fileEncoding;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -117,11 +163,9 @@ public final class EncryptionResult {
|
|||
if (compressionAlgorithm == null) {
|
||||
throw new IllegalStateException("Compression algorithm not set.");
|
||||
}
|
||||
if (fileInfo == null) {
|
||||
throw new IllegalStateException("File info not set.");
|
||||
}
|
||||
|
||||
return new EncryptionResult(encryptionAlgorithm, compressionAlgorithm, detachedSignatures, recipients, fileInfo);
|
||||
return new EncryptionResult(encryptionAlgorithm, compressionAlgorithm, detachedSignatures, recipients,
|
||||
fileName, modificationDate, encoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,6 @@ import org.bouncycastle.openpgp.operator.PGPDataEncryptorBuilder;
|
|||
import org.bouncycastle.openpgp.operator.PGPKeyEncryptionMethodGenerator;
|
||||
import org.pgpainless.algorithm.CompressionAlgorithm;
|
||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||
import org.pgpainless.decryption_verification.OpenPgpMetadata;
|
||||
import org.pgpainless.implementation.ImplementationFactory;
|
||||
import org.pgpainless.key.SubkeyIdentifier;
|
||||
import org.pgpainless.util.ArmoredOutputStreamFactory;
|
||||
|
@ -154,8 +153,9 @@ public final class EncryptionStream extends OutputStream {
|
|||
new byte[BUFFER_SIZE]);
|
||||
outermostStream = literalDataStream;
|
||||
|
||||
resultBuilder.setFileInfo(new OpenPgpMetadata.FileInfo(
|
||||
options.getFileName(), options.getModificationDate(), options.getEncoding()));
|
||||
resultBuilder.setFileName(options.getFileName())
|
||||
.setModificationDate(options.getModificationDate())
|
||||
.setFileEncoding(options.getEncoding());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -256,4 +256,8 @@ public final class EncryptionStream extends OutputStream {
|
|||
}
|
||||
return resultBuilder.build();
|
||||
}
|
||||
|
||||
public boolean isClosed() {
|
||||
return closed;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,11 +33,14 @@ import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder;
|
|||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.algorithm.DocumentSignatureType;
|
||||
import org.pgpainless.algorithm.HashAlgorithm;
|
||||
import org.pgpainless.exception.KeyCannotSignException;
|
||||
import org.pgpainless.exception.KeyValidationException;
|
||||
import org.pgpainless.implementation.ImplementationFactory;
|
||||
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
||||
import org.pgpainless.key.SubkeyIdentifier;
|
||||
import org.pgpainless.key.info.KeyRingInfo;
|
||||
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
||||
import org.pgpainless.key.protection.UnlockSecretKey;
|
||||
import org.pgpainless.policy.Policy;
|
||||
|
||||
public final class SigningOptions {
|
||||
|
@ -161,12 +164,12 @@ public final class SigningOptions {
|
|||
|
||||
List<PGPPublicKey> signingPubKeys = keyRingInfo.getSigningSubkeys();
|
||||
if (signingPubKeys.isEmpty()) {
|
||||
throw new AssertionError("Key has no valid signing key.");
|
||||
throw new KeyCannotSignException("Key " + new OpenPgpV4Fingerprint(secretKey) + " has no valid signing key.");
|
||||
}
|
||||
|
||||
for (PGPPublicKey signingPubKey : signingPubKeys) {
|
||||
PGPSecretKey signingSecKey = secretKey.getSecretKey(signingPubKey.getKeyID());
|
||||
PGPPrivateKey signingSubkey = signingSecKey.extractPrivateKey(secretKeyDecryptor.getDecryptor(signingPubKey.getKeyID()));
|
||||
PGPPrivateKey signingSubkey = UnlockSecretKey.unlockSecretKey(signingSecKey, secretKeyDecryptor);
|
||||
Set<HashAlgorithm> hashAlgorithms = keyRingInfo.getPreferredHashAlgorithms(userId, signingPubKey.getKeyID());
|
||||
HashAlgorithm hashAlgorithm = negotiateHashAlgorithm(hashAlgorithms, PGPainless.getPolicy());
|
||||
addSigningMethod(secretKey, signingSubkey, hashAlgorithm, signatureType, false);
|
||||
|
@ -242,7 +245,7 @@ public final class SigningOptions {
|
|||
|
||||
List<PGPPublicKey> signingPubKeys = keyRingInfo.getSigningSubkeys();
|
||||
if (signingPubKeys.isEmpty()) {
|
||||
throw new AssertionError("Key has no valid signing key.");
|
||||
throw new KeyCannotSignException("Key has no valid signing key.");
|
||||
}
|
||||
|
||||
for (PGPPublicKey signingPubKey : signingPubKeys) {
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* Copyright 2021 Paul Schaub.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pgpainless.exception;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
|
||||
public class KeyCannotSignException extends PGPException {
|
||||
public KeyCannotSignException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -19,6 +19,7 @@ import java.io.ByteArrayInputStream;
|
|||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
@ -68,6 +69,23 @@ public class ArmorUtils {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
public static ArmoredOutputStream toAsciiArmoredStream(PGPKeyRing keyRing, OutputStream outputStream) {
|
||||
MultiMap<String, String> header = keyToHeader(keyRing);
|
||||
return toAsciiArmoredStream(outputStream, header);
|
||||
}
|
||||
|
||||
public static ArmoredOutputStream toAsciiArmoredStream(OutputStream outputStream, MultiMap<String, String> header) {
|
||||
ArmoredOutputStream armoredOutputStream = ArmoredOutputStreamFactory.get(outputStream);
|
||||
if (header != null) {
|
||||
for (String headerKey : header.keySet()) {
|
||||
for (String headerValue : header.get(headerKey)) {
|
||||
armoredOutputStream.addHeader(headerKey, headerValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
return armoredOutputStream;
|
||||
}
|
||||
|
||||
public static String toAsciiArmoredString(PGPPublicKeyRingCollection publicKeyRings) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Iterator<PGPPublicKeyRing> iterator = publicKeyRings.iterator(); iterator.hasNext(); ) {
|
||||
|
@ -124,20 +142,25 @@ public class ArmorUtils {
|
|||
|
||||
public static String toAsciiArmoredString(InputStream inputStream, MultiMap<String, String> additionalHeaderValues) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(out);
|
||||
if (additionalHeaderValues != null) {
|
||||
for (String header : additionalHeaderValues.keySet()) {
|
||||
for (String value : additionalHeaderValues.get(header)) {
|
||||
armor.addHeader(header, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
ArmoredOutputStream armor = toAsciiArmoredStream(out, additionalHeaderValues);
|
||||
Streams.pipeAll(inputStream, armor);
|
||||
armor.close();
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public static ArmoredOutputStream createArmoredOutputStreamFor(PGPKeyRing keyRing, OutputStream outputStream) {
|
||||
ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(outputStream);
|
||||
MultiMap<String, String> headerMap = keyToHeader(keyRing);
|
||||
for (String header : headerMap.keySet()) {
|
||||
for (String value : headerMap.get(header)) {
|
||||
armor.addHeader(header, value);
|
||||
}
|
||||
}
|
||||
|
||||
return armor;
|
||||
}
|
||||
|
||||
public static List<String> getCommendHeaderValues(ArmoredInputStream armor) {
|
||||
return getArmorHeaderValues(armor, HEADER_COMMENT);
|
||||
}
|
||||
|
|
|
@ -17,9 +17,17 @@ package org.junit;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.pgpainless.util.DateUtil;
|
||||
|
||||
public class JUtils {
|
||||
|
||||
public static void assertEquals(long a, long b, long delta) {
|
||||
assertTrue(a - delta <= b && a + delta >= b);
|
||||
}
|
||||
|
||||
public static void assertDateEquals(Date a, Date b) {
|
||||
org.junit.jupiter.api.Assertions.assertEquals(DateUtil.formatUTCDate(a), DateUtil.formatUTCDate(b));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,99 +0,0 @@
|
|||
/*
|
||||
* Copyright 2021 Paul Schaub.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pgpainless.encryption_signing;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.algorithm.StreamEncoding;
|
||||
import org.pgpainless.decryption_verification.ConsumerOptions;
|
||||
import org.pgpainless.decryption_verification.DecryptionStream;
|
||||
import org.pgpainless.decryption_verification.OpenPgpMetadata;
|
||||
import org.pgpainless.key.util.KeyRingUtils;
|
||||
|
||||
public class FileInfoTest {
|
||||
|
||||
@Test
|
||||
public void textFile() throws NoSuchAlgorithmException, PGPException, InvalidAlgorithmParameterException, IOException {
|
||||
OpenPgpMetadata.FileInfo fileInfo = new OpenPgpMetadata.FileInfo("message.txt", new Date(), StreamEncoding.TEXT);
|
||||
executeWith(fileInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void binaryStream() throws NoSuchAlgorithmException, PGPException, InvalidAlgorithmParameterException, IOException {
|
||||
OpenPgpMetadata.FileInfo fileInfo = OpenPgpMetadata.FileInfo.binaryStream();
|
||||
executeWith(fileInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forYourEyesOnly() throws NoSuchAlgorithmException, PGPException, InvalidAlgorithmParameterException, IOException {
|
||||
OpenPgpMetadata.FileInfo fileInfo = OpenPgpMetadata.FileInfo.forYourEyesOnly();
|
||||
executeWith(fileInfo);
|
||||
}
|
||||
|
||||
public void executeWith(OpenPgpMetadata.FileInfo fileInfo) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
|
||||
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit");
|
||||
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
|
||||
|
||||
String data = "Hello, World!";
|
||||
|
||||
ByteArrayInputStream dataIn = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
|
||||
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
|
||||
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
|
||||
.onOutputStream(dataOut)
|
||||
.withOptions(ProducerOptions.encrypt(
|
||||
EncryptionOptions
|
||||
.encryptCommunications()
|
||||
.addRecipient(publicKeys))
|
||||
.setEncoding(fileInfo.getStreamFormat())
|
||||
.setFileName(fileInfo.getFileName())
|
||||
.setModificationDate(fileInfo.getModificationDate())
|
||||
);
|
||||
|
||||
Streams.pipeAll(dataIn, encryptionStream);
|
||||
encryptionStream.close();
|
||||
|
||||
OpenPgpMetadata.FileInfo cryptInfo = encryptionStream.getResult().getFileInfo();
|
||||
assertEquals(fileInfo, cryptInfo);
|
||||
|
||||
ByteArrayInputStream cryptIn = new ByteArrayInputStream(dataOut.toByteArray());
|
||||
ByteArrayOutputStream plainOut = new ByteArrayOutputStream();
|
||||
|
||||
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
|
||||
.onInputStream(cryptIn)
|
||||
.withOptions(new ConsumerOptions()
|
||||
.addDecryptionKey(secretKeys));
|
||||
Streams.pipeAll(decryptionStream, plainOut);
|
||||
|
||||
decryptionStream.close();
|
||||
|
||||
OpenPgpMetadata.FileInfo decryptInfo = decryptionStream.getResult().getFileInfo();
|
||||
assertEquals(fileInfo, decryptInfo);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,181 @@
|
|||
/*
|
||||
* Copyright 2021 Paul Schaub.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pgpainless.encryption_signing;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPLiteralData;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
import org.junit.JUtils;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.algorithm.StreamEncoding;
|
||||
import org.pgpainless.decryption_verification.ConsumerOptions;
|
||||
import org.pgpainless.decryption_verification.DecryptionStream;
|
||||
import org.pgpainless.decryption_verification.OpenPgpMetadata;
|
||||
|
||||
public class FileInformationTest {
|
||||
|
||||
private static final String data = "Hello, World!\n";
|
||||
private static PGPSecretKeyRing secretKey;
|
||||
private static PGPPublicKeyRing certificate;
|
||||
|
||||
@BeforeAll
|
||||
public static void generateKey() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
|
||||
secretKey = PGPainless.generateKeyRing().modernKeyRing("alice@wonderland.lit", null);
|
||||
certificate = PGPainless.extractCertificate(secretKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTextFile() throws PGPException, IOException {
|
||||
String fileName = "message.txt";
|
||||
Date modificationDate = new Date();
|
||||
StreamEncoding encoding = StreamEncoding.TEXT;
|
||||
|
||||
ByteArrayInputStream dataIn = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
|
||||
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
|
||||
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
|
||||
.onOutputStream(dataOut)
|
||||
.withOptions(ProducerOptions.encrypt(
|
||||
EncryptionOptions
|
||||
.encryptCommunications()
|
||||
.addRecipient(certificate))
|
||||
.setFileName(fileName)
|
||||
.setModificationDate(modificationDate)
|
||||
.setEncoding(encoding)
|
||||
);
|
||||
|
||||
Streams.pipeAll(dataIn, encryptionStream);
|
||||
encryptionStream.close();
|
||||
|
||||
EncryptionResult encResult = encryptionStream.getResult();
|
||||
assertEquals(fileName, encResult.getFileName());
|
||||
JUtils.assertDateEquals(modificationDate, encResult.getModificationDate());
|
||||
assertEquals(encoding, encResult.getFileEncoding());
|
||||
|
||||
ByteArrayInputStream cryptIn = new ByteArrayInputStream(dataOut.toByteArray());
|
||||
ByteArrayOutputStream plainOut = new ByteArrayOutputStream();
|
||||
|
||||
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
|
||||
.onInputStream(cryptIn)
|
||||
.withOptions(new ConsumerOptions()
|
||||
.addDecryptionKey(secretKey));
|
||||
Streams.pipeAll(decryptionStream, plainOut);
|
||||
|
||||
decryptionStream.close();
|
||||
|
||||
OpenPgpMetadata decResult = decryptionStream.getResult();
|
||||
|
||||
assertEquals(fileName, decResult.getFileName());
|
||||
JUtils.assertDateEquals(modificationDate, decResult.getModificationDate());
|
||||
assertEquals(encoding, decResult.getFileEncoding());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaults() throws PGPException, IOException {
|
||||
ByteArrayInputStream dataIn = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
|
||||
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
|
||||
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
|
||||
.onOutputStream(dataOut)
|
||||
.withOptions(ProducerOptions.encrypt(
|
||||
EncryptionOptions
|
||||
.encryptCommunications()
|
||||
.addRecipient(certificate))
|
||||
);
|
||||
|
||||
Streams.pipeAll(dataIn, encryptionStream);
|
||||
encryptionStream.close();
|
||||
|
||||
EncryptionResult encResult = encryptionStream.getResult();
|
||||
assertEquals("", encResult.getFileName());
|
||||
JUtils.assertDateEquals(PGPLiteralData.NOW, encResult.getModificationDate());
|
||||
assertEquals(PGPLiteralData.BINARY, encResult.getFileEncoding().getCode());
|
||||
assertFalse(encResult.isForYourEyesOnly());
|
||||
|
||||
ByteArrayInputStream cryptIn = new ByteArrayInputStream(dataOut.toByteArray());
|
||||
ByteArrayOutputStream plainOut = new ByteArrayOutputStream();
|
||||
|
||||
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
|
||||
.onInputStream(cryptIn)
|
||||
.withOptions(new ConsumerOptions()
|
||||
.addDecryptionKey(secretKey));
|
||||
Streams.pipeAll(decryptionStream, plainOut);
|
||||
|
||||
decryptionStream.close();
|
||||
|
||||
OpenPgpMetadata decResult = decryptionStream.getResult();
|
||||
|
||||
assertEquals("", decResult.getFileName());
|
||||
JUtils.assertDateEquals(PGPLiteralData.NOW, decResult.getModificationDate());
|
||||
assertEquals(PGPLiteralData.BINARY, decResult.getFileEncoding().getCode());
|
||||
assertFalse(decResult.isForYourEyesOnly());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForYourEyesOnly() throws PGPException, IOException {
|
||||
ByteArrayInputStream dataIn = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
|
||||
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
|
||||
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
|
||||
.onOutputStream(dataOut)
|
||||
.withOptions(ProducerOptions.encrypt(
|
||||
EncryptionOptions
|
||||
.encryptCommunications()
|
||||
.addRecipient(certificate))
|
||||
.setForYourEyesOnly()
|
||||
);
|
||||
|
||||
Streams.pipeAll(dataIn, encryptionStream);
|
||||
encryptionStream.close();
|
||||
|
||||
EncryptionResult encResult = encryptionStream.getResult();
|
||||
assertEquals(PGPLiteralData.CONSOLE, encResult.getFileName());
|
||||
JUtils.assertDateEquals(PGPLiteralData.NOW, encResult.getModificationDate());
|
||||
assertEquals(PGPLiteralData.BINARY, encResult.getFileEncoding().getCode());
|
||||
assertTrue(encResult.isForYourEyesOnly());
|
||||
|
||||
ByteArrayInputStream cryptIn = new ByteArrayInputStream(dataOut.toByteArray());
|
||||
ByteArrayOutputStream plainOut = new ByteArrayOutputStream();
|
||||
|
||||
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
|
||||
.onInputStream(cryptIn)
|
||||
.withOptions(new ConsumerOptions()
|
||||
.addDecryptionKey(secretKey));
|
||||
Streams.pipeAll(decryptionStream, plainOut);
|
||||
|
||||
decryptionStream.close();
|
||||
|
||||
OpenPgpMetadata decResult = decryptionStream.getResult();
|
||||
|
||||
assertEquals(PGPLiteralData.CONSOLE, decResult.getFileName());
|
||||
JUtils.assertDateEquals(PGPLiteralData.NOW, decResult.getModificationDate());
|
||||
assertEquals(PGPLiteralData.BINARY, decResult.getFileEncoding().getCode());
|
||||
assertTrue(decResult.isForYourEyesOnly());
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue