2020-11-13 14:32:29 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2020 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.key.util;
|
|
|
|
|
2021-05-29 12:43:31 +02:00
|
|
|
import java.io.IOException;
|
2020-11-13 14:32:29 +01:00
|
|
|
import java.util.ArrayList;
|
2021-05-29 12:43:31 +02:00
|
|
|
import java.util.Arrays;
|
2020-11-13 14:32:29 +01:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2020-11-29 19:06:49 +01:00
|
|
|
import java.util.NoSuchElementException;
|
2021-05-29 12:43:31 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2020-11-29 16:04:15 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
2020-11-29 19:06:49 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPKeyRing;
|
2020-11-29 16:04:15 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPPrivateKey;
|
2020-11-13 14:32:29 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
2021-05-29 12:43:31 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
2020-11-29 16:04:15 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKey;
|
2020-11-13 14:32:29 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
2021-05-29 12:43:31 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
2020-11-29 16:04:15 +01:00
|
|
|
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
2021-05-14 13:18:34 +02:00
|
|
|
import org.pgpainless.key.protection.UnlockSecretKey;
|
2020-11-13 14:32:29 +01:00
|
|
|
|
2021-08-15 15:24:19 +02:00
|
|
|
public final class KeyRingUtils {
|
|
|
|
|
|
|
|
private KeyRingUtils() {
|
|
|
|
|
|
|
|
}
|
2020-11-13 14:32:29 +01:00
|
|
|
|
2020-12-03 22:46:59 +01:00
|
|
|
/**
|
|
|
|
* Return the primary {@link PGPSecretKey} from the provided {@link PGPSecretKeyRing}.
|
|
|
|
* If it has no primary secret key, throw a {@link NoSuchElementException}.
|
|
|
|
*
|
|
|
|
* @param secretKeys secret keys
|
|
|
|
* @return primary secret key
|
|
|
|
*/
|
2020-11-29 19:06:49 +01:00
|
|
|
public static PGPSecretKey requirePrimarySecretKeyFrom(PGPSecretKeyRing secretKeys) {
|
|
|
|
PGPSecretKey primarySecretKey = getPrimarySecretKeyFrom(secretKeys);
|
|
|
|
if (primarySecretKey == null) {
|
|
|
|
throw new NoSuchElementException("Provided PGPSecretKeyRing has no primary secret key.");
|
|
|
|
}
|
|
|
|
return primarySecretKey;
|
|
|
|
}
|
|
|
|
|
2020-12-03 22:46:59 +01:00
|
|
|
/**
|
|
|
|
* Return the primary {@link PGPSecretKey} from the provided {@link PGPSecretKeyRing} or null if it has none.
|
|
|
|
*
|
|
|
|
* @param secretKeys secret key ring
|
|
|
|
* @return primary secret key
|
|
|
|
*/
|
2020-11-29 19:06:49 +01:00
|
|
|
public static PGPSecretKey getPrimarySecretKeyFrom(PGPSecretKeyRing secretKeys) {
|
|
|
|
PGPSecretKey secretKey = secretKeys.getSecretKey();
|
|
|
|
if (secretKey.isMasterKey()) {
|
|
|
|
return secretKey;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-03 22:46:59 +01:00
|
|
|
/**
|
|
|
|
* Return the primary {@link PGPPublicKey} from the provided key ring.
|
|
|
|
* Throws a {@link NoSuchElementException} if the key ring has no primary public key.
|
|
|
|
*
|
|
|
|
* @param keyRing key ring
|
|
|
|
* @return primary public key
|
|
|
|
*/
|
2020-11-29 19:06:49 +01:00
|
|
|
public static PGPPublicKey requirePrimaryPublicKeyFrom(PGPKeyRing keyRing) {
|
|
|
|
PGPPublicKey primaryPublicKey = getPrimaryPublicKeyFrom(keyRing);
|
|
|
|
if (primaryPublicKey == null) {
|
|
|
|
throw new NoSuchElementException("Provided PGPKeyRing has no primary public key.");
|
|
|
|
}
|
|
|
|
return primaryPublicKey;
|
|
|
|
}
|
|
|
|
|
2020-12-03 22:46:59 +01:00
|
|
|
/**
|
|
|
|
* Return the primary {@link PGPPublicKey} from the provided key ring or null if it has none.
|
|
|
|
*
|
|
|
|
* @param keyRing key ring
|
|
|
|
* @return primary public key
|
|
|
|
*/
|
2020-11-29 19:06:49 +01:00
|
|
|
public static PGPPublicKey getPrimaryPublicKeyFrom(PGPKeyRing keyRing) {
|
|
|
|
PGPPublicKey primaryPublicKey = keyRing.getPublicKey();
|
|
|
|
if (primaryPublicKey.isMasterKey()) {
|
|
|
|
return primaryPublicKey;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-22 16:52:09 +01:00
|
|
|
public static PGPPublicKey getPublicKeyFrom(PGPKeyRing keyRing, long subKeyId) {
|
|
|
|
return keyRing.getPublicKey(subKeyId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PGPPublicKey requirePublicKeyFrom(PGPKeyRing keyRing, long subKeyId) {
|
|
|
|
PGPPublicKey publicKey = getPublicKeyFrom(keyRing, subKeyId);
|
|
|
|
if (publicKey == null) {
|
|
|
|
throw new IllegalArgumentException("KeyRing does not contain public key with keyID " + Long.toHexString(subKeyId));
|
|
|
|
}
|
|
|
|
return publicKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PGPSecretKey requireSecretKeyFrom(PGPSecretKeyRing keyRing, long subKeyId) {
|
|
|
|
PGPSecretKey secretKey = keyRing.getSecretKey(subKeyId);
|
|
|
|
if (secretKey == null) {
|
|
|
|
throw new IllegalArgumentException("KeyRing does not contain secret key with keyID " + Long.toHexString(subKeyId));
|
|
|
|
}
|
|
|
|
return secretKey;
|
|
|
|
}
|
|
|
|
|
2020-12-03 22:46:59 +01:00
|
|
|
/**
|
|
|
|
* Extract a {@link PGPPublicKeyRing} containing all public keys from the provided {@link PGPSecretKeyRing}.
|
|
|
|
*
|
|
|
|
* @param secretKeys secret key ring
|
|
|
|
* @return public key ring
|
|
|
|
*/
|
2020-11-13 14:32:29 +01:00
|
|
|
public static PGPPublicKeyRing publicKeyRingFrom(PGPSecretKeyRing secretKeys) {
|
|
|
|
List<PGPPublicKey> publicKeyList = new ArrayList<>();
|
|
|
|
Iterator<PGPPublicKey> publicKeyIterator = secretKeys.getPublicKeys();
|
|
|
|
while (publicKeyIterator.hasNext()) {
|
|
|
|
publicKeyList.add(publicKeyIterator.next());
|
|
|
|
}
|
|
|
|
PGPPublicKeyRing publicKeyRing = new PGPPublicKeyRing(publicKeyList);
|
|
|
|
return publicKeyRing;
|
|
|
|
}
|
2020-11-29 16:04:15 +01:00
|
|
|
|
2020-12-03 22:46:59 +01:00
|
|
|
/**
|
|
|
|
* Unlock a {@link PGPSecretKey} and return the resulting {@link PGPPrivateKey}.
|
|
|
|
*
|
|
|
|
* @param secretKey secret key
|
|
|
|
* @param protector protector to unlock the secret key
|
|
|
|
* @return private key
|
2020-12-05 00:01:12 +01:00
|
|
|
*
|
2020-12-03 22:46:59 +01:00
|
|
|
* @throws PGPException if something goes wrong (eg. wrong passphrase)
|
|
|
|
*/
|
2020-11-29 16:04:15 +01:00
|
|
|
public static PGPPrivateKey unlockSecretKey(PGPSecretKey secretKey, SecretKeyRingProtector protector) throws PGPException {
|
2021-05-14 13:18:34 +02:00
|
|
|
return UnlockSecretKey.unlockSecretKey(secretKey, protector);
|
2020-11-29 16:04:15 +01:00
|
|
|
}
|
2021-05-29 12:43:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
PGPXxxKeyRing -> PGPXxxKeyRingCollection
|
|
|
|
*/
|
|
|
|
public static PGPPublicKeyRingCollection keyRingsToKeyRingCollection(@Nonnull PGPPublicKeyRing... rings)
|
|
|
|
throws IOException, PGPException {
|
|
|
|
return new PGPPublicKeyRingCollection(Arrays.asList(rings));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PGPSecretKeyRingCollection keyRingsToKeyRingCollection(@Nonnull PGPSecretKeyRing... rings)
|
|
|
|
throws IOException, PGPException {
|
|
|
|
return new PGPSecretKeyRingCollection(Arrays.asList(rings));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean keyRingContainsKeyWithId(@Nonnull PGPPublicKeyRing ring,
|
|
|
|
long keyId) {
|
|
|
|
return ring.getPublicKey(keyId) != null;
|
|
|
|
}
|
2020-11-13 14:32:29 +01:00
|
|
|
}
|