1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-09-27 10:09:33 +02:00
pgpainless/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/OpenPgpMetadata.java

255 lines
8.7 KiB
Java
Raw Normal View History

/*
* 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.
*/
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;
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;
import java.util.Map;
2018-06-02 21:21:35 +02:00
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
2018-06-02 21:21:35 +02:00
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPLiteralData;
2018-06-10 17:12:44 +02:00
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.StreamEncoding;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.key.OpenPgpV4Fingerprint;
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
public class OpenPgpMetadata {
2018-06-06 18:46:41 +02:00
private final Set<Long> recipientKeyIds;
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;
private final String fileName;
private final Date modificationDate;
private final StreamEncoding fileEncoding;
2018-06-06 18:46:41 +02:00
public OpenPgpMetadata(Set<Long> recipientKeyIds,
SubkeyIdentifier decryptionKey,
SymmetricKeyAlgorithm symmetricKeyAlgorithm,
CompressionAlgorithm algorithm,
2020-08-24 14:55:06 +02:00
List<OnePassSignature> onePassSignatures,
List<DetachedSignature> detachedSignatures,
String fileName,
Date modificationDate,
StreamEncoding fileEncoding) {
2018-06-06 18:46:41 +02:00
this.recipientKeyIds = Collections.unmodifiableSet(recipientKeyIds);
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);
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() {
return symmetricKeyAlgorithm != SymmetricKeyAlgorithm.NULL && !getRecipientKeyIds().isEmpty();
2018-06-10 17:12:44 +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;
}
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());
}
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
}
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()) {
verifiedSignatures.put(detachedSignature.getSigningKeyIdentifier(), detachedSignature.getSignature());
2020-08-24 14:55:06 +02:00
}
}
for (OnePassSignature onePassSignature : onePassSignatures) {
if (onePassSignature.isVerified()) {
verifiedSignatures.put(onePassSignature.getSigningKey(), onePassSignature.getSignature());
2020-08-24 14:55:06 +02: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
}
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);
if (containsVerifiedSignatureFrom(fingerprint)) {
2018-06-10 17:12:44 +02:00
return true;
}
}
return false;
}
public boolean containsVerifiedSignatureFrom(OpenPgpV4Fingerprint fingerprint) {
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
}
/**
* 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;
}
public static Builder getBuilder() {
2018-06-06 18:46:41 +02:00
return new Builder();
}
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<>();
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;
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;
}
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;
}
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);
}
public OpenPgpMetadata build() {
return new OpenPgpMetadata(recipientFingerprints, decryptionKey,
symmetricKeyAlgorithm, compressionAlgorithm,
onePassSignatures, detachedSignatures, fileName, modificationDate, fileEncoding);
2018-06-06 18:46:41 +02:00
}
}
2018-06-02 21:21:35 +02:00
}