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-06-28 18:25:11 +02:00
|
|
|
package org.pgpainless.pgpainless.util;
|
2018-06-10 17:12:44 +02:00
|
|
|
|
2018-06-26 15:18:01 +02:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
2018-06-10 17:12:44 +02:00
|
|
|
import java.io.IOException;
|
2018-06-26 15:18:01 +02:00
|
|
|
import java.io.InputStream;
|
2018-06-11 01:33:49 +02:00
|
|
|
import java.util.ArrayList;
|
2018-06-10 17:12:44 +02:00
|
|
|
import java.util.Arrays;
|
2018-06-21 15:18:19 +02:00
|
|
|
import java.util.HashSet;
|
2018-06-10 17:12:44 +02:00
|
|
|
import java.util.Iterator;
|
2018-06-11 01:33:49 +02:00
|
|
|
import java.util.List;
|
2018-06-21 15:18:19 +02:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2018-06-10 17:12:44 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
2018-07-06 14:09:36 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPKeyRing;
|
2018-06-10 17:12:44 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
2018-06-21 15:18:19 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKey;
|
2018-06-10 17:12:44 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
2018-06-21 15:18:19 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSignature;
|
|
|
|
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
|
2018-06-26 15:18:01 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPUtil;
|
|
|
|
import org.bouncycastle.util.io.Streams;
|
2018-06-28 18:25:11 +02:00
|
|
|
import org.pgpainless.pgpainless.algorithm.KeyFlag;
|
2018-07-02 20:09:35 +02:00
|
|
|
import org.pgpainless.pgpainless.key.selection.key.PublicKeySelectionStrategy;
|
|
|
|
import org.pgpainless.pgpainless.key.selection.key.impl.And;
|
|
|
|
import org.pgpainless.pgpainless.key.selection.key.impl.NoRevocation;
|
|
|
|
import org.pgpainless.pgpainless.key.selection.key.impl.SignedByMasterKey;
|
2018-06-10 17:12:44 +02:00
|
|
|
|
|
|
|
public class BCUtil {
|
|
|
|
|
2018-06-21 15:18:19 +02:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(BCUtil.class.getName());
|
|
|
|
|
|
|
|
/*
|
|
|
|
PGPXxxKeyRing -> PGPXxxKeyRingCollection
|
|
|
|
*/
|
2018-06-10 17:12:44 +02:00
|
|
|
public static PGPPublicKeyRingCollection keyRingsToKeyRingCollection(PGPPublicKeyRing... rings)
|
|
|
|
throws IOException, PGPException {
|
|
|
|
return new PGPPublicKeyRingCollection(Arrays.asList(rings));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PGPSecretKeyRingCollection keyRingsToKeyRingCollection(PGPSecretKeyRing... rings)
|
|
|
|
throws IOException, PGPException {
|
|
|
|
return new PGPSecretKeyRingCollection(Arrays.asList(rings));
|
|
|
|
}
|
|
|
|
|
2018-06-21 15:18:19 +02:00
|
|
|
/*
|
|
|
|
PGPSecretKeyRing -> PGPPublicKeyRing
|
|
|
|
*/
|
|
|
|
|
2018-06-11 01:33:49 +02:00
|
|
|
public static PGPPublicKeyRing publicKeyRingFromSecretKeyRing(PGPSecretKeyRing secring) {
|
|
|
|
List<PGPPublicKey> list = new ArrayList<>();
|
|
|
|
for (Iterator<PGPPublicKey> i = secring.getPublicKeys(); i.hasNext(); ) {
|
2018-06-10 17:12:44 +02:00
|
|
|
PGPPublicKey k = i.next();
|
2018-06-11 01:33:49 +02:00
|
|
|
list.add(k);
|
|
|
|
}
|
|
|
|
|
2018-07-09 10:47:41 +02:00
|
|
|
return new PGPPublicKeyRing(list);
|
2018-06-10 17:12:44 +02:00
|
|
|
}
|
2018-06-21 15:18:19 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
PGPXxxKeyRingCollection -> PGPXxxKeyRing
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static PGPSecretKeyRing getKeyRingFromCollection(PGPSecretKeyRingCollection collection, Long id)
|
|
|
|
throws PGPException {
|
|
|
|
PGPSecretKeyRing uncleanedRing = collection.getSecretKeyRing(id);
|
|
|
|
PGPPublicKeyRing publicKeys = publicKeyRingFromSecretKeyRing(uncleanedRing);
|
|
|
|
|
|
|
|
// Determine ids of signed keys
|
|
|
|
Set<Long> signedKeyIds = new HashSet<>();
|
|
|
|
signedKeyIds.add(id); // Add the signing key itself
|
|
|
|
Iterator<PGPPublicKey> signedPubKeys = publicKeys.getKeysWithSignaturesBy(id);
|
|
|
|
while (signedPubKeys.hasNext()) {
|
|
|
|
signedKeyIds.add(signedPubKeys.next().getKeyID());
|
|
|
|
}
|
|
|
|
|
|
|
|
PGPSecretKeyRing cleanedRing = uncleanedRing;
|
|
|
|
Iterator<PGPSecretKey> secretKeys = uncleanedRing.getSecretKeys();
|
|
|
|
while (secretKeys.hasNext()) {
|
|
|
|
PGPSecretKey secretKey = secretKeys.next();
|
|
|
|
if (!signedKeyIds.contains(secretKey.getKeyID())) {
|
|
|
|
cleanedRing = PGPSecretKeyRing.removeSecretKey(cleanedRing, secretKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cleanedRing;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PGPPublicKeyRing getKeyRingFromCollection(PGPPublicKeyRingCollection collection, Long id)
|
|
|
|
throws PGPException {
|
2018-07-11 14:17:04 +02:00
|
|
|
PGPPublicKey key = collection.getPublicKey(id);
|
|
|
|
return removeUnassociatedKeysFromKeyRing(collection.getPublicKeyRing(id), key);
|
2018-06-21 15:18:19 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:18:01 +02:00
|
|
|
public static InputStream getPgpDecoderInputStream(byte[] bytes) throws IOException {
|
|
|
|
return getPgpDecoderInputStream(new ByteArrayInputStream(bytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static InputStream getPgpDecoderInputStream(InputStream inputStream) throws IOException {
|
|
|
|
return PGPUtil.getDecoderStream(inputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] getDecodedBytes(byte[] bytes) throws IOException {
|
|
|
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
|
Streams.pipeAll(getPgpDecoderInputStream(bytes), buffer);
|
|
|
|
return buffer.toByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] getDecodedBytes(InputStream inputStream) throws IOException {
|
|
|
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
|
Streams.pipeAll(inputStream, buffer);
|
|
|
|
return getDecodedBytes(buffer.toByteArray());
|
|
|
|
}
|
|
|
|
|
2018-07-02 20:09:35 +02:00
|
|
|
/**
|
|
|
|
* Remove all keys from the key ring, are either not having a subkey signature from the master key
|
|
|
|
* (identified by {@code masterKeyId}), or are revoked ("normal" key revocation, as well as subkey revocation).
|
|
|
|
*
|
|
|
|
* @param ring key ring
|
2018-07-11 14:17:04 +02:00
|
|
|
* @param masterKey master key
|
2018-07-02 20:09:35 +02:00
|
|
|
* @return "cleaned" key ring
|
|
|
|
*/
|
2018-07-11 14:17:04 +02:00
|
|
|
public static PGPPublicKeyRing removeUnassociatedKeysFromKeyRing(PGPPublicKeyRing ring, PGPPublicKey masterKey) {
|
|
|
|
if (!masterKey.isMasterKey()) {
|
|
|
|
throw new IllegalArgumentException("Given key is not a master key.");
|
|
|
|
}
|
2018-07-02 20:09:35 +02:00
|
|
|
// Only select keys which are signed by the master key and not revoked.
|
2018-07-11 14:17:04 +02:00
|
|
|
PublicKeySelectionStrategy<PGPPublicKey> selector = new And.PubKeySelectionStrategy<>(
|
2018-07-02 20:09:35 +02:00
|
|
|
new SignedByMasterKey.PubkeySelectionStrategy(),
|
|
|
|
new NoRevocation.PubKeySelectionStrategy<>());
|
2018-06-21 15:18:19 +02:00
|
|
|
|
|
|
|
PGPPublicKeyRing cleaned = ring;
|
|
|
|
|
|
|
|
Iterator<PGPPublicKey> publicKeys = ring.getPublicKeys();
|
|
|
|
while (publicKeys.hasNext()) {
|
|
|
|
PGPPublicKey publicKey = publicKeys.next();
|
2018-07-11 14:17:04 +02:00
|
|
|
if (!selector.accept(masterKey, publicKey)) {
|
2018-06-21 15:18:19 +02:00
|
|
|
cleaned = PGPPublicKeyRing.removePublicKey(cleaned, publicKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleaned;
|
|
|
|
}
|
|
|
|
|
2018-07-02 20:09:35 +02:00
|
|
|
/**
|
|
|
|
* Remove all keys from the key ring, are either not having a subkey signature from the master key
|
|
|
|
* (identified by {@code masterKeyId}), or are revoked ("normal" key revocation, as well as subkey revocation).
|
|
|
|
*
|
|
|
|
* @param ring key ring
|
2018-07-11 14:17:04 +02:00
|
|
|
* @param masterKey master key
|
2018-07-02 20:09:35 +02:00
|
|
|
* @return "cleaned" key ring
|
|
|
|
*/
|
2018-07-11 14:17:04 +02:00
|
|
|
public static PGPSecretKeyRing removeUnassociatedKeysFromKeyRing(PGPSecretKeyRing ring, PGPPublicKey masterKey) {
|
|
|
|
if (!masterKey.isMasterKey()) {
|
|
|
|
throw new IllegalArgumentException("Given key is not a master key.");
|
|
|
|
}
|
2018-07-02 20:09:35 +02:00
|
|
|
// Only select keys which are signed by the master key and not revoked.
|
2018-07-11 14:17:04 +02:00
|
|
|
PublicKeySelectionStrategy<PGPPublicKey> selector = new And.PubKeySelectionStrategy<>(
|
2018-07-02 20:09:35 +02:00
|
|
|
new SignedByMasterKey.PubkeySelectionStrategy(),
|
|
|
|
new NoRevocation.PubKeySelectionStrategy<>());
|
2018-06-28 15:45:30 +02:00
|
|
|
|
|
|
|
PGPSecretKeyRing cleaned = ring;
|
|
|
|
|
|
|
|
Iterator<PGPSecretKey> secretKeys = ring.getSecretKeys();
|
|
|
|
while (secretKeys.hasNext()) {
|
|
|
|
PGPSecretKey secretKey = secretKeys.next();
|
2018-07-11 14:17:04 +02:00
|
|
|
if (!selector.accept(masterKey, secretKey.getPublicKey())) {
|
2018-06-28 15:45:30 +02:00
|
|
|
cleaned = PGPSecretKeyRing.removeSecretKey(cleaned, secretKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleaned;
|
|
|
|
}
|
|
|
|
|
2018-07-02 20:09:35 +02:00
|
|
|
/**
|
|
|
|
* Return the {@link PGPPublicKey} which is the master key of the key ring.
|
|
|
|
*
|
|
|
|
* @param ring key ring
|
|
|
|
* @return master key
|
|
|
|
*/
|
2018-06-21 15:18:19 +02:00
|
|
|
public static PGPPublicKey getMasterKeyFrom(PGPPublicKeyRing ring) {
|
|
|
|
Iterator<PGPPublicKey> it = ring.getPublicKeys();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
PGPPublicKey k = it.next();
|
|
|
|
if (k.isMasterKey()) {
|
2018-07-02 20:09:35 +02:00
|
|
|
// There can only be one master key, so we can immediately return
|
2018-06-21 15:18:19 +02:00
|
|
|
return k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-06 14:09:36 +02:00
|
|
|
public static PGPPublicKey getMasterKeyFrom(PGPKeyRing ring) {
|
|
|
|
Iterator<PGPPublicKey> it = ring.getPublicKeys();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
PGPPublicKey k = it.next();
|
|
|
|
if (k.isMasterKey()) {
|
|
|
|
// There can only be one master key, so we can immediately return
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-06-21 15:18:19 +02:00
|
|
|
public static Set<Long> signingKeyIds(PGPSecretKeyRing ring) {
|
|
|
|
Set<Long> ids = new HashSet<>();
|
|
|
|
Iterator<PGPPublicKey> it = ring.getPublicKeys();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
PGPPublicKey k = it.next();
|
|
|
|
|
|
|
|
boolean signingKey = false;
|
|
|
|
|
|
|
|
Iterator sit = k.getSignatures();
|
|
|
|
while (sit.hasNext()) {
|
|
|
|
Object n = sit.next();
|
|
|
|
if (!(n instanceof PGPSignature)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PGPSignature s = (PGPSignature) n;
|
|
|
|
if (!s.hasSubpackets()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
s.verifyCertification(ring.getPublicKey(s.getKeyID()));
|
|
|
|
} catch (PGPException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Could not verify signature on " + Long.toHexString(k.getKeyID()) + " made by " + Long.toHexString(s.getKeyID()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PGPSignatureSubpacketVector hashed = s.getHashedSubPackets();
|
|
|
|
if (KeyFlag.fromInteger(hashed.getKeyFlags()).contains(KeyFlag.SIGN_DATA)) {
|
|
|
|
signingKey = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signingKey) {
|
|
|
|
ids.add(k.getKeyID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
}
|
2018-06-27 15:09:39 +02:00
|
|
|
|
|
|
|
public static boolean keyRingContainsKeyWithId(PGPPublicKeyRing ring, long keyId) {
|
2018-07-02 20:09:35 +02:00
|
|
|
return ring.getPublicKey(keyId) != null;
|
2018-06-27 15:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean keyRingContainsKeyWithId(PGPSecretKeyRing ring, long keyId) {
|
2018-07-02 20:09:35 +02:00
|
|
|
return ring.getSecretKey(keyId) != null;
|
2018-06-27 15:09:39 +02:00
|
|
|
}
|
2018-07-06 14:09:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
public static PGPKeyRing merge(PGPKeyRing one, PGPKeyRing other) {
|
|
|
|
|
|
|
|
PGPPublicKey masterOne = getMasterKeyFrom(one);
|
|
|
|
if (masterOne == null) {
|
|
|
|
throw new IllegalArgumentException("First KeyRing has no master key");
|
|
|
|
}
|
|
|
|
|
|
|
|
PGPPublicKey masterOther = getMasterKeyFrom(other);
|
|
|
|
if (masterOther == null) {
|
|
|
|
throw new IllegalArgumentException("Other KeyRing has no master key");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (masterOne.getKeyID() != masterOther.getKeyID() ||
|
|
|
|
Arrays.equals(masterOne.getFingerprint(), masterOther.getFingerprint())) {
|
|
|
|
throw new IllegalArgumentException("Keys are not the same.");
|
|
|
|
}
|
|
|
|
|
|
|
|
PGPKeyRing merged = one;
|
|
|
|
|
|
|
|
boolean mergedIsSecret = (merged instanceof PGPSecretKeyRing);
|
|
|
|
boolean otherIsSecret = (other instanceof PGPSecretKeyRing);
|
|
|
|
|
|
|
|
for (Iterator it = other.getPublicKeys(); it.hasNext(); ) {
|
|
|
|
|
|
|
|
PGPPublicKey nextPublicKey = (PGPPublicKey) it.next();
|
|
|
|
PGPPublicKey pendant = merged.getPublicKey(nextPublicKey.getKeyID());
|
|
|
|
|
|
|
|
if (pendant == null) {
|
|
|
|
if (mergedIsSecret && otherIsSecret) {
|
|
|
|
// Add secret key
|
|
|
|
PGPSecretKey secretKey = ((PGPSecretKeyRing) other).getSecretKey(nextPublicKey.getKeyID());
|
|
|
|
merged = PGPSecretKeyRing.insertSecretKey((PGPSecretKeyRing) merged, secretKey);
|
|
|
|
} else {
|
|
|
|
if (mergedIsSecret) {
|
|
|
|
PGPSecretKeyRing mergedAsSecret = (PGPSecretKeyRing) merged;
|
|
|
|
PGPSecretKey secretKey = mergedAsSecret.getSecretKey(nextPublicKey.getKeyID());
|
|
|
|
if (secretKey == null) {
|
|
|
|
PGPPublicKeyRing mergedAsPublic = publicKeyRingFromSecretKeyRing((PGPSecretKeyRing) merged);
|
|
|
|
mergedAsPublic = PGPPublicKeyRing.insertPublicKey(mergedAsPublic, nextPublicKey);
|
|
|
|
mergedAsSecret = PGPSecretKeyRing.replacePublicKeys(mergedAsSecret, mergedAsPublic);
|
|
|
|
merged = mergedAsSecret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
}
|