2021-10-07 15:48:52 +02:00
|
|
|
// Copyright 2021 Paul Schaub.
|
|
|
|
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-05-14 13:18:34 +02:00
|
|
|
package org.pgpainless.key.protection;
|
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.bouncycastle.openpgp.PGPPrivateKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKey;
|
|
|
|
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
|
|
|
|
import org.pgpainless.exception.WrongPassphraseException;
|
2021-05-31 15:38:47 +02:00
|
|
|
import org.pgpainless.key.info.KeyInfo;
|
2021-05-14 13:18:34 +02:00
|
|
|
import org.pgpainless.util.Passphrase;
|
|
|
|
|
2021-08-15 15:24:19 +02:00
|
|
|
public final class UnlockSecretKey {
|
|
|
|
|
|
|
|
private UnlockSecretKey() {
|
|
|
|
|
|
|
|
}
|
2021-05-14 13:18:34 +02:00
|
|
|
|
|
|
|
public static PGPPrivateKey unlockSecretKey(PGPSecretKey secretKey, SecretKeyRingProtector protector)
|
|
|
|
throws WrongPassphraseException {
|
|
|
|
try {
|
2021-05-31 15:38:47 +02:00
|
|
|
PBESecretKeyDecryptor decryptor = null;
|
|
|
|
if (KeyInfo.isEncrypted(secretKey)) {
|
|
|
|
decryptor = protector.getDecryptor(secretKey.getKeyID());
|
|
|
|
}
|
2021-05-14 13:18:34 +02:00
|
|
|
return secretKey.extractPrivateKey(decryptor);
|
|
|
|
} catch (PGPException e) {
|
|
|
|
throw new WrongPassphraseException(secretKey.getKeyID(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PGPPrivateKey unlockSecretKey(PGPSecretKey secretKey, PBESecretKeyDecryptor decryptor) throws WrongPassphraseException {
|
|
|
|
try {
|
|
|
|
return secretKey.extractPrivateKey(decryptor);
|
|
|
|
} catch (PGPException e) {
|
|
|
|
throw new WrongPassphraseException(secretKey.getKeyID(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static PGPPrivateKey unlockSecretKey(PGPSecretKey secretKey, Passphrase passphrase) throws WrongPassphraseException {
|
|
|
|
return unlockSecretKey(secretKey, SecretKeyRingProtector.unlockSingleKeyWith(passphrase, secretKey));
|
|
|
|
}
|
|
|
|
}
|