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;
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
|
|
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
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;
|
2018-07-08 19:31:53 +02:00
|
|
|
private final OpenPgpV4Fingerprint decryptionFingerprint;
|
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 boolean integrityProtected;
|
|
|
|
|
2018-07-23 16:23:23 +02:00
|
|
|
public OpenPgpMetadata(Set<Long> recipientKeyIds,
|
|
|
|
OpenPgpV4Fingerprint decryptionFingerprint,
|
|
|
|
SymmetricKeyAlgorithm symmetricKeyAlgorithm,
|
|
|
|
CompressionAlgorithm algorithm,
|
|
|
|
boolean integrityProtected,
|
2020-08-24 14:55:06 +02:00
|
|
|
List<OnePassSignature> onePassSignatures,
|
|
|
|
List<DetachedSignature> detachedSignatures) {
|
2018-06-06 18:46:41 +02:00
|
|
|
|
|
|
|
this.recipientKeyIds = Collections.unmodifiableSet(recipientKeyIds);
|
2018-07-08 19:31:53 +02:00
|
|
|
this.decryptionFingerprint = decryptionFingerprint;
|
2018-06-06 18:46:41 +02:00
|
|
|
this.symmetricKeyAlgorithm = symmetricKeyAlgorithm;
|
|
|
|
this.compressionAlgorithm = algorithm;
|
|
|
|
this.integrityProtected = integrityProtected;
|
2020-08-24 14:55:06 +02:00
|
|
|
this.detachedSignatures = Collections.unmodifiableList(detachedSignatures);
|
|
|
|
this.onePassSignatures = Collections.unmodifiableList(onePassSignatures);
|
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
|
|
|
}
|
|
|
|
|
2018-07-08 19:31:53 +02:00
|
|
|
public OpenPgpV4Fingerprint getDecryptionFingerprint() {
|
|
|
|
return decryptionFingerprint;
|
2018-06-06 18:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public SymmetricKeyAlgorithm getSymmetricKeyAlgorithm() {
|
|
|
|
return symmetricKeyAlgorithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CompressionAlgorithm getCompressionAlgorithm() {
|
|
|
|
return compressionAlgorithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isIntegrityProtected() {
|
|
|
|
return integrityProtected;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-10 15:12:04 +01:00
|
|
|
public Map<OpenPgpV4Fingerprint, PGPSignature> getVerifiedSignatures() {
|
2020-08-24 14:55:06 +02:00
|
|
|
Map<OpenPgpV4Fingerprint, PGPSignature> verifiedSignatures = new ConcurrentHashMap<>();
|
|
|
|
for (DetachedSignature detachedSignature : detachedSignatures) {
|
|
|
|
if (detachedSignature.isVerified()) {
|
|
|
|
verifiedSignatures.put(detachedSignature.getFingerprint(), detachedSignature.getSignature());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (OnePassSignature onePassSignature : onePassSignatures) {
|
|
|
|
if (onePassSignature.isVerified()) {
|
|
|
|
verifiedSignatures.put(onePassSignature.getFingerprint(), onePassSignature.getSignature());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:12:04 +01:00
|
|
|
return verifiedSignatures;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<OpenPgpV4Fingerprint> getVerifiedSignatureKeyFingerprints() {
|
2020-08-24 14:55:06 +02:00
|
|
|
return getVerifiedSignatures().keySet();
|
2018-06-06 18:46:41 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-08-24 14:55:06 +02:00
|
|
|
return getVerifiedSignatureKeyFingerprints().contains(fingerprint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Signature {
|
|
|
|
protected final PGPSignature signature;
|
|
|
|
protected final OpenPgpV4Fingerprint fingerprint;
|
|
|
|
|
|
|
|
public Signature(PGPSignature signature, OpenPgpV4Fingerprint fingerprint) {
|
|
|
|
this.signature = signature;
|
|
|
|
this.fingerprint = fingerprint;
|
|
|
|
}
|
2018-07-26 16:40:26 +02:00
|
|
|
}
|
|
|
|
|
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<>();
|
|
|
|
private OpenPgpV4Fingerprint decryptionFingerprint;
|
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 boolean integrityProtected = false;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-08 19:31:53 +02:00
|
|
|
public Builder setDecryptionFingerprint(OpenPgpV4Fingerprint fingerprint) {
|
|
|
|
this.decryptionFingerprint = fingerprint;
|
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 setIntegrityProtected(boolean integrityProtected) {
|
|
|
|
this.integrityProtected = integrityProtected;
|
|
|
|
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() {
|
2020-01-10 15:12:04 +01:00
|
|
|
return new OpenPgpMetadata(recipientFingerprints, decryptionFingerprint,
|
|
|
|
symmetricKeyAlgorithm, compressionAlgorithm, integrityProtected,
|
2020-08-24 14:55:06 +02:00
|
|
|
onePassSignatures, detachedSignatures);
|
2018-06-06 18:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|