2018-06-13 17:26:48 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2018 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.
|
|
|
|
*/
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless.decryption_verification;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
2020-08-24 14:55:06 +02:00
|
|
|
import java.util.ArrayList;
|
2018-06-06 18:46:41 +02:00
|
|
|
import java.util.Collections;
|
2021-04-25 00:28:48 +02:00
|
|
|
import java.util.Date;
|
2018-06-06 18:46:41 +02:00
|
|
|
import java.util.HashSet;
|
2020-08-24 14:55:06 +02:00
|
|
|
import java.util.List;
|
2020-01-10 15:12:04 +01:00
|
|
|
import java.util.Map;
|
2018-06-02 21:21:35 +02:00
|
|
|
import java.util.Set;
|
2020-01-10 15:12:04 +01:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
2021-07-15 16:55:13 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2021-04-25 00:28:48 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPLiteralData;
|
2018-06-10 17:12:44 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
2020-01-10 15:12:04 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPSignature;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.algorithm.CompressionAlgorithm;
|
2021-04-25 00:28:48 +02:00
|
|
|
import org.pgpainless.algorithm.StreamEncoding;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
|
|
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
2021-07-04 13:08:24 +02:00
|
|
|
import org.pgpainless.key.SubkeyIdentifier;
|
2021-04-26 13:38:12 +02:00
|
|
|
import org.pgpainless.signature.DetachedSignature;
|
|
|
|
import org.pgpainless.signature.OnePassSignature;
|
2018-06-06 18:46:41 +02:00
|
|
|
|
2018-07-23 16:23:23 +02:00
|
|
|
public class OpenPgpMetadata {
|
2018-06-06 18:46:41 +02:00
|
|
|
|
|
|
|
private final Set<Long> recipientKeyIds;
|
2021-07-04 13:08:24 +02:00
|
|
|
private final SubkeyIdentifier decryptionKey;
|
2020-08-24 14:55:06 +02:00
|
|
|
private final List<OnePassSignature> onePassSignatures;
|
|
|
|
private final List<DetachedSignature> detachedSignatures;
|
2018-06-06 18:46:41 +02:00
|
|
|
private final SymmetricKeyAlgorithm symmetricKeyAlgorithm;
|
|
|
|
private final CompressionAlgorithm compressionAlgorithm;
|
2021-07-15 16:55:13 +02:00
|
|
|
private final String fileName;
|
|
|
|
private final Date modificationDate;
|
|
|
|
private final StreamEncoding fileEncoding;
|
2018-06-06 18:46:41 +02:00
|
|
|
|
2018-07-23 16:23:23 +02:00
|
|
|
public OpenPgpMetadata(Set<Long> recipientKeyIds,
|
2021-07-04 13:08:24 +02:00
|
|
|
SubkeyIdentifier decryptionKey,
|
2018-07-23 16:23:23 +02:00
|
|
|
SymmetricKeyAlgorithm symmetricKeyAlgorithm,
|
|
|
|
CompressionAlgorithm algorithm,
|
2020-08-24 14:55:06 +02:00
|
|
|
List<OnePassSignature> onePassSignatures,
|
2021-04-25 00:28:48 +02:00
|
|
|
List<DetachedSignature> detachedSignatures,
|
2021-07-15 16:55:13 +02:00
|
|
|
String fileName,
|
|
|
|
Date modificationDate,
|
|
|
|
StreamEncoding fileEncoding) {
|
2018-06-06 18:46:41 +02:00
|
|
|
|
|
|
|
this.recipientKeyIds = Collections.unmodifiableSet(recipientKeyIds);
|
2021-07-04 13:08:24 +02:00
|
|
|
this.decryptionKey = decryptionKey;
|
2018-06-06 18:46:41 +02:00
|
|
|
this.symmetricKeyAlgorithm = symmetricKeyAlgorithm;
|
|
|
|
this.compressionAlgorithm = algorithm;
|
2020-08-24 14:55:06 +02:00
|
|
|
this.detachedSignatures = Collections.unmodifiableList(detachedSignatures);
|
|
|
|
this.onePassSignatures = Collections.unmodifiableList(onePassSignatures);
|
2021-07-15 16:55:13 +02:00
|
|
|
this.fileName = fileName;
|
|
|
|
this.modificationDate = modificationDate;
|
|
|
|
this.fileEncoding = fileEncoding;
|
2018-06-06 18:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Set<Long> getRecipientKeyIds() {
|
|
|
|
return recipientKeyIds;
|
|
|
|
}
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
public boolean isEncrypted() {
|
2021-02-17 20:07:54 +01:00
|
|
|
return symmetricKeyAlgorithm != SymmetricKeyAlgorithm.NULL && !getRecipientKeyIds().isEmpty();
|
2018-06-10 17:12:44 +02:00
|
|
|
}
|
|
|
|
|
2021-07-04 13:08:24 +02:00
|
|
|
public SubkeyIdentifier getDecryptionKey() {
|
|
|
|
return decryptionKey;
|
2018-06-06 18:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public SymmetricKeyAlgorithm getSymmetricKeyAlgorithm() {
|
|
|
|
return symmetricKeyAlgorithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CompressionAlgorithm getCompressionAlgorithm() {
|
|
|
|
return compressionAlgorithm;
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:12:04 +01:00
|
|
|
public Set<PGPSignature> getSignatures() {
|
2020-08-24 14:55:06 +02:00
|
|
|
Set<PGPSignature> signatures = new HashSet<>();
|
|
|
|
for (DetachedSignature detachedSignature : detachedSignatures) {
|
|
|
|
signatures.add(detachedSignature.getSignature());
|
|
|
|
}
|
|
|
|
for (OnePassSignature onePassSignature : onePassSignatures) {
|
|
|
|
signatures.add(onePassSignature.getSignature());
|
|
|
|
}
|
2020-01-10 15:12:04 +01:00
|
|
|
return signatures;
|
|
|
|
}
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
public boolean isSigned() {
|
2020-08-24 14:55:06 +02:00
|
|
|
return !getSignatures().isEmpty();
|
2018-06-10 17:12:44 +02:00
|
|
|
}
|
|
|
|
|
2021-07-04 13:08:24 +02:00
|
|
|
public Map<SubkeyIdentifier, PGPSignature> getVerifiedSignatures() {
|
|
|
|
Map<SubkeyIdentifier, PGPSignature> verifiedSignatures = new ConcurrentHashMap<>();
|
2020-08-24 14:55:06 +02:00
|
|
|
for (DetachedSignature detachedSignature : detachedSignatures) {
|
|
|
|
if (detachedSignature.isVerified()) {
|
2021-07-04 13:08:24 +02:00
|
|
|
verifiedSignatures.put(detachedSignature.getSigningKeyIdentifier(), detachedSignature.getSignature());
|
2020-08-24 14:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (OnePassSignature onePassSignature : onePassSignatures) {
|
|
|
|
if (onePassSignature.isVerified()) {
|
2021-07-04 13:08:24 +02:00
|
|
|
verifiedSignatures.put(onePassSignature.getSigningKey(), onePassSignature.getSignature());
|
2020-08-24 14:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:12:04 +01:00
|
|
|
return verifiedSignatures;
|
|
|
|
}
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
public boolean isVerified() {
|
2020-08-24 14:55:06 +02:00
|
|
|
return !getVerifiedSignatures().isEmpty();
|
2018-06-10 17:12:44 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 16:40:02 +02:00
|
|
|
public boolean containsVerifiedSignatureFrom(PGPPublicKeyRing publicKeys) {
|
2018-06-10 17:12:44 +02:00
|
|
|
for (PGPPublicKey key : publicKeys) {
|
2018-07-08 19:31:53 +02:00
|
|
|
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(key);
|
2018-07-26 16:40:26 +02:00
|
|
|
if (containsVerifiedSignatureFrom(fingerprint)) {
|
2018-06-10 17:12:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:40:26 +02:00
|
|
|
public boolean containsVerifiedSignatureFrom(OpenPgpV4Fingerprint fingerprint) {
|
2021-07-04 13:08:24 +02:00
|
|
|
for (SubkeyIdentifier verifiedSigningKey : getVerifiedSignatures().keySet()) {
|
|
|
|
if (verifiedSigningKey.getPrimaryKeyFingerprint().equals(fingerprint) ||
|
|
|
|
verifiedSigningKey.getSubkeyFingerprint().equals(fingerprint)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2020-08-24 14:55:06 +02:00
|
|
|
}
|
|
|
|
|
2021-07-15 16:55:13 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2021-04-25 00:28:48 +02:00
|
|
|
}
|
|
|
|
|
2021-07-15 16:55:13 +02:00
|
|
|
/**
|
|
|
|
* Return the encoding format of the encrypted / signed file.
|
|
|
|
*
|
|
|
|
* @return encoding
|
|
|
|
*/
|
|
|
|
public StreamEncoding getFileEncoding() {
|
|
|
|
return fileEncoding;
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:12:04 +01:00
|
|
|
public static Builder getBuilder() {
|
2018-06-06 18:46:41 +02:00
|
|
|
return new Builder();
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:12:04 +01:00
|
|
|
public static class Builder {
|
2018-06-06 18:46:41 +02:00
|
|
|
|
2018-07-08 19:31:53 +02:00
|
|
|
private final Set<Long> recipientFingerprints = new HashSet<>();
|
2021-07-04 13:08:24 +02:00
|
|
|
private SubkeyIdentifier decryptionKey;
|
2020-08-24 14:55:06 +02:00
|
|
|
private final List<DetachedSignature> detachedSignatures = new ArrayList<>();
|
|
|
|
private final List<OnePassSignature> onePassSignatures = new ArrayList<>();
|
2018-06-06 18:46:41 +02:00
|
|
|
private SymmetricKeyAlgorithm symmetricKeyAlgorithm = SymmetricKeyAlgorithm.NULL;
|
|
|
|
private CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.UNCOMPRESSED;
|
2021-07-15 16:55:13 +02:00
|
|
|
private String fileName;
|
|
|
|
private StreamEncoding fileEncoding;
|
|
|
|
private Date modificationDate;
|
2018-06-06 18:46:41 +02:00
|
|
|
|
2018-07-08 19:31:53 +02:00
|
|
|
public Builder addRecipientKeyId(Long keyId) {
|
|
|
|
this.recipientFingerprints.add(keyId);
|
2018-06-06 18:46:41 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-07-04 13:08:24 +02:00
|
|
|
public Builder setDecryptionKey(SubkeyIdentifier decryptionKey) {
|
|
|
|
this.decryptionKey = decryptionKey;
|
2018-06-06 18:46:41 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder setCompressionAlgorithm(CompressionAlgorithm algorithm) {
|
|
|
|
this.compressionAlgorithm = algorithm;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-08-24 14:55:06 +02:00
|
|
|
public List<DetachedSignature> getDetachedSignatures() {
|
|
|
|
return detachedSignatures;
|
2018-06-06 18:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Builder setSymmetricKeyAlgorithm(SymmetricKeyAlgorithm symmetricKeyAlgorithm) {
|
|
|
|
this.symmetricKeyAlgorithm = symmetricKeyAlgorithm;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:55:13 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-24 14:55:06 +02:00
|
|
|
public void addDetachedSignature(DetachedSignature signature) {
|
|
|
|
this.detachedSignatures.add(signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addOnePassSignature(OnePassSignature onePassSignature) {
|
|
|
|
this.onePassSignatures.add(onePassSignature);
|
|
|
|
}
|
|
|
|
|
2018-07-23 16:23:23 +02:00
|
|
|
public OpenPgpMetadata build() {
|
2021-07-04 13:08:24 +02:00
|
|
|
return new OpenPgpMetadata(recipientFingerprints, decryptionKey,
|
2021-04-27 12:27:25 +02:00
|
|
|
symmetricKeyAlgorithm, compressionAlgorithm,
|
2021-07-15 16:55:13 +02:00
|
|
|
onePassSignatures, detachedSignatures, fileName, modificationDate, fileEncoding);
|
2018-06-06 18:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|