2021-10-07 15:48:52 +02:00
|
|
|
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-11-19 17:51:57 +01:00
|
|
|
package org.pgpainless.key.modification.secretkeyring;
|
2020-10-22 01:20:43 +02:00
|
|
|
|
2021-11-08 21:25:02 +01:00
|
|
|
import static org.pgpainless.util.CollectionUtils.concat;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2020-11-10 17:25:35 +01:00
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2020-10-23 16:44:21 +02:00
|
|
|
import java.util.ArrayList;
|
2020-10-25 20:43:09 +01:00
|
|
|
import java.util.Collections;
|
2020-11-27 13:00:06 +01:00
|
|
|
import java.util.Date;
|
2020-10-23 16:44:21 +02:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2020-10-25 20:43:09 +01:00
|
|
|
import java.util.Map;
|
2020-11-03 19:29:15 +01:00
|
|
|
import java.util.NoSuchElementException;
|
2020-10-23 16:44:21 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2020-10-22 01:20:43 +02:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2021-07-19 14:52:59 +02:00
|
|
|
import org.bouncycastle.bcpg.S2K;
|
2021-09-10 20:14:12 +02:00
|
|
|
import org.bouncycastle.bcpg.SecretKeyPacket;
|
2020-10-23 16:44:21 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
2020-11-10 17:25:35 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPKeyPair;
|
|
|
|
import org.bouncycastle.openpgp.PGPKeyRingGenerator;
|
2020-10-23 16:44:21 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPrivateKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKey;
|
2020-10-22 01:20:43 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
2020-10-23 16:44:21 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSignature;
|
2020-10-25 19:54:03 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPSignatureGenerator;
|
2020-11-20 12:01:39 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
|
|
|
|
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
|
2020-10-23 16:44:21 +02:00
|
|
|
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
|
2020-10-25 20:43:09 +01:00
|
|
|
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
|
2020-11-10 17:25:35 +01:00
|
|
|
import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder;
|
2021-11-12 15:07:09 +01:00
|
|
|
import org.pgpainless.PGPainless;
|
2021-11-20 16:07:27 +01:00
|
|
|
import org.pgpainless.algorithm.HashAlgorithm;
|
2021-11-08 21:25:02 +01:00
|
|
|
import org.pgpainless.algorithm.KeyFlag;
|
|
|
|
import org.pgpainless.algorithm.PublicKeyAlgorithm;
|
2020-10-23 16:44:21 +02:00
|
|
|
import org.pgpainless.algorithm.SignatureType;
|
2021-11-20 16:07:27 +01:00
|
|
|
import org.pgpainless.algorithm.negotiation.HashAlgorithmNegotiator;
|
2020-12-27 01:56:18 +01:00
|
|
|
import org.pgpainless.implementation.ImplementationFactory;
|
2021-10-27 17:38:25 +02:00
|
|
|
import org.pgpainless.key.OpenPgpFingerprint;
|
2020-11-10 17:25:35 +01:00
|
|
|
import org.pgpainless.key.generation.KeyRingBuilder;
|
2020-10-22 01:20:43 +02:00
|
|
|
import org.pgpainless.key.generation.KeySpec;
|
2021-11-12 15:07:09 +01:00
|
|
|
import org.pgpainless.key.info.KeyRingInfo;
|
2021-05-14 18:55:26 +02:00
|
|
|
import org.pgpainless.key.protection.CachingSecretKeyRingProtector;
|
2021-05-28 23:14:20 +02:00
|
|
|
import org.pgpainless.key.protection.KeyRingProtectionSettings;
|
2020-10-25 20:43:09 +01:00
|
|
|
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
|
2020-10-23 16:44:21 +02:00
|
|
|
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
2021-05-14 13:18:34 +02:00
|
|
|
import org.pgpainless.key.protection.UnlockSecretKey;
|
2020-10-25 20:43:09 +01:00
|
|
|
import org.pgpainless.key.protection.UnprotectedKeysProtector;
|
2021-09-10 20:14:12 +02:00
|
|
|
import org.pgpainless.key.protection.fixes.S2KUsageFix;
|
2020-10-25 20:43:09 +01:00
|
|
|
import org.pgpainless.key.protection.passphrase_provider.SolitaryPassphraseProvider;
|
2020-11-13 14:32:29 +01:00
|
|
|
import org.pgpainless.key.util.KeyRingUtils;
|
2020-11-20 12:01:39 +01:00
|
|
|
import org.pgpainless.key.util.RevocationAttributes;
|
2021-04-26 13:38:12 +02:00
|
|
|
import org.pgpainless.signature.SignatureUtils;
|
2021-11-16 15:18:51 +01:00
|
|
|
import org.pgpainless.signature.builder.RevocationSignatureBuilder;
|
2021-11-08 15:57:35 +01:00
|
|
|
import org.pgpainless.signature.builder.SelfSignatureBuilder;
|
2021-11-16 15:18:51 +01:00
|
|
|
import org.pgpainless.signature.subpackets.RevocationSignatureSubpackets;
|
2021-11-08 15:57:35 +01:00
|
|
|
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
|
2021-04-26 13:38:12 +02:00
|
|
|
import org.pgpainless.signature.subpackets.SignatureSubpacketGeneratorUtil;
|
2021-11-20 16:07:27 +01:00
|
|
|
import org.pgpainless.signature.subpackets.SignatureSubpackets;
|
|
|
|
import org.pgpainless.signature.subpackets.SignatureSubpacketsHelper;
|
2021-11-08 21:25:02 +01:00
|
|
|
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil;
|
|
|
|
import org.pgpainless.util.CollectionUtils;
|
2021-05-28 23:14:20 +02:00
|
|
|
import org.pgpainless.util.Passphrase;
|
2020-10-22 01:20:43 +02:00
|
|
|
|
2020-11-19 17:51:57 +01:00
|
|
|
public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
2020-10-22 01:20:43 +02:00
|
|
|
|
|
|
|
private PGPSecretKeyRing secretKeyRing;
|
|
|
|
|
2020-11-19 17:51:57 +01:00
|
|
|
public SecretKeyRingEditor(PGPSecretKeyRing secretKeyRing) {
|
2020-10-22 01:20:43 +02:00
|
|
|
if (secretKeyRing == null) {
|
|
|
|
throw new NullPointerException("SecretKeyRing MUST NOT be null.");
|
|
|
|
}
|
|
|
|
this.secretKeyRing = secretKeyRing;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-16 15:18:51 +01:00
|
|
|
public SecretKeyRingEditorInterface addUserId(
|
|
|
|
String userId,
|
|
|
|
SecretKeyRingProtector secretKeyRingProtector)
|
|
|
|
throws PGPException {
|
2021-11-08 15:57:35 +01:00
|
|
|
return addUserId(userId, null, secretKeyRingProtector);
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:18:51 +01:00
|
|
|
@Override
|
2021-11-08 15:57:35 +01:00
|
|
|
public SecretKeyRingEditorInterface addUserId(
|
|
|
|
String userId,
|
|
|
|
@Nullable SelfSignatureSubpackets.Callback signatureSubpacketCallback,
|
|
|
|
SecretKeyRingProtector protector) throws PGPException {
|
2020-10-23 16:44:21 +02:00
|
|
|
userId = sanitizeUserId(userId);
|
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
// user-id certifications live on the primary key
|
|
|
|
PGPSecretKey primaryKey = secretKeyRing.getSecretKey();
|
2020-11-03 19:29:15 +01:00
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
// retain key flags from previous signature
|
2021-11-12 15:07:09 +01:00
|
|
|
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing);
|
|
|
|
List<KeyFlag> keyFlags = info.getKeyFlagsOf(info.getKeyId());
|
2021-11-20 16:07:27 +01:00
|
|
|
|
2021-11-20 20:19:22 +01:00
|
|
|
SelfSignatureBuilder builder = new SelfSignatureBuilder(primaryKey, protector);
|
2021-11-08 15:57:35 +01:00
|
|
|
builder.setSignatureType(SignatureType.POSITIVE_CERTIFICATION);
|
2021-11-20 20:19:22 +01:00
|
|
|
builder.getHashedSubpackets().setKeyFlags(keyFlags);
|
|
|
|
builder.applyCallback(signatureSubpacketCallback);
|
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
PGPSignature signature = builder.build(primaryKey.getPublicKey(), userId);
|
|
|
|
secretKeyRing = KeyRingUtils.injectCertification(secretKeyRing, userId, signature);
|
2020-10-25 19:54:03 +01:00
|
|
|
|
2020-11-03 19:29:15 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-10-25 20:43:09 +01:00
|
|
|
// TODO: Move to utility class?
|
2020-10-23 16:44:21 +02:00
|
|
|
private String sanitizeUserId(String userId) {
|
|
|
|
userId = userId.trim();
|
|
|
|
// TODO: Further research how to sanitize user IDs.
|
|
|
|
// eg. what about newlines?
|
|
|
|
return userId;
|
|
|
|
}
|
|
|
|
|
2020-10-22 01:20:43 +02:00
|
|
|
@Override
|
2020-11-19 17:51:57 +01:00
|
|
|
public SecretKeyRingEditorInterface addSubKey(@Nonnull KeySpec keySpec,
|
|
|
|
@Nonnull Passphrase subKeyPassphrase,
|
|
|
|
SecretKeyRingProtector secretKeyRingProtector)
|
2021-11-20 16:07:27 +01:00
|
|
|
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
|
2020-11-10 17:25:35 +01:00
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
PGPKeyPair keyPair = KeyRingBuilder.generateKeyPair(keySpec);
|
2020-11-10 17:25:35 +01:00
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
SecretKeyRingProtector subKeyProtector = PasswordBasedSecretKeyRingProtector
|
|
|
|
.forKeyId(keyPair.getKeyID(), subKeyPassphrase);
|
2020-11-10 17:25:35 +01:00
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
SelfSignatureSubpackets.Callback callback = new SelfSignatureSubpackets.Callback() {
|
|
|
|
@Override
|
|
|
|
public void modifyHashedSubpackets(SelfSignatureSubpackets hashedSubpackets) {
|
|
|
|
SignatureSubpacketsHelper.applyFrom(keySpec.getSubpackets(), (SignatureSubpackets) hashedSubpackets);
|
|
|
|
}
|
|
|
|
};
|
2020-11-10 17:25:35 +01:00
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
List<KeyFlag> keyFlags = KeyFlag.fromBitmask(keySpec.getSubpackets().getKeyFlags());
|
|
|
|
KeyFlag firstFlag = keyFlags.remove(0);
|
|
|
|
KeyFlag[] otherFlags = keyFlags.toArray(new KeyFlag[0]);
|
2020-11-10 17:25:35 +01:00
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
return addSubKey(keyPair, callback, subKeyProtector, secretKeyRingProtector, firstFlag, otherFlags);
|
2020-11-10 17:25:35 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 21:25:02 +01:00
|
|
|
@Override
|
2021-11-20 16:07:27 +01:00
|
|
|
public SecretKeyRingEditorInterface addSubKey(PGPKeyPair subkey,
|
2021-11-08 21:25:02 +01:00
|
|
|
@Nullable SelfSignatureSubpackets.Callback bindingSignatureCallback,
|
|
|
|
SecretKeyRingProtector subkeyProtector,
|
|
|
|
SecretKeyRingProtector primaryKeyProtector,
|
|
|
|
KeyFlag keyFlag,
|
2021-11-16 15:18:51 +01:00
|
|
|
KeyFlag... additionalKeyFlags)
|
|
|
|
throws PGPException, IOException {
|
2021-11-08 21:25:02 +01:00
|
|
|
KeyFlag[] flags = concat(keyFlag, additionalKeyFlags);
|
2021-11-08 22:45:08 +01:00
|
|
|
PublicKeyAlgorithm subkeyAlgorithm = PublicKeyAlgorithm.fromId(subkey.getPublicKey().getAlgorithm());
|
|
|
|
SignatureSubpacketsUtil.assureKeyCanCarryFlags(subkeyAlgorithm);
|
2021-11-08 21:25:02 +01:00
|
|
|
|
|
|
|
PGPSecretKey primaryKey = secretKeyRing.getSecretKey();
|
2021-11-20 16:07:27 +01:00
|
|
|
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing);
|
|
|
|
PublicKeyAlgorithm signingKeyAlgorithm = PublicKeyAlgorithm.fromId(primaryKey.getPublicKey().getAlgorithm());
|
|
|
|
HashAlgorithm hashAlgorithm = HashAlgorithmNegotiator
|
|
|
|
.negotiateSignatureHashAlgorithm(PGPainless.getPolicy())
|
|
|
|
.negotiateHashAlgorithm(info.getPreferredHashAlgorithms());
|
|
|
|
|
|
|
|
// While we'd like to rely on our own BindingSignatureBuilder implementation,
|
|
|
|
// unfortunately we have to use BCs PGPKeyRingGenerator class since there is no public constructor
|
|
|
|
// for subkeys. See https://github.com/bcgit/bc-java/pull/1063
|
|
|
|
PGPKeyRingGenerator ringGenerator = new PGPKeyRingGenerator(
|
|
|
|
secretKeyRing,
|
|
|
|
primaryKeyProtector.getDecryptor(primaryKey.getKeyID()),
|
|
|
|
ImplementationFactory.getInstance().getV4FingerprintCalculator(),
|
|
|
|
ImplementationFactory.getInstance().getPGPContentSignerBuilder(
|
|
|
|
signingKeyAlgorithm, hashAlgorithm),
|
|
|
|
subkeyProtector.getEncryptor(subkey.getKeyID()));
|
|
|
|
|
|
|
|
SelfSignatureSubpackets hashedSubpackets = SignatureSubpackets.createHashedSubpackets(primaryKey.getPublicKey());
|
|
|
|
SelfSignatureSubpackets unhashedSubpackets = SignatureSubpackets.createEmptySubpackets();
|
|
|
|
hashedSubpackets.setKeyFlags(flags);
|
|
|
|
|
|
|
|
if (bindingSignatureCallback != null) {
|
|
|
|
bindingSignatureCallback.modifyHashedSubpackets(hashedSubpackets);
|
|
|
|
bindingSignatureCallback.modifyUnhashedSubpackets(unhashedSubpackets);
|
|
|
|
}
|
2021-11-08 21:25:02 +01:00
|
|
|
|
|
|
|
boolean isSigningKey = CollectionUtils.contains(flags, KeyFlag.SIGN_DATA) ||
|
|
|
|
CollectionUtils.contains(flags, KeyFlag.CERTIFY_OTHER);
|
2021-11-20 16:07:27 +01:00
|
|
|
PGPContentSignerBuilder primaryKeyBindingSigner = null;
|
2021-11-08 21:25:02 +01:00
|
|
|
if (isSigningKey) {
|
2021-11-20 16:07:27 +01:00
|
|
|
primaryKeyBindingSigner = ImplementationFactory.getInstance().getPGPContentSignerBuilder(subkeyAlgorithm, hashAlgorithm);
|
2021-11-08 21:25:02 +01:00
|
|
|
}
|
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
ringGenerator.addSubKey(subkey,
|
|
|
|
SignatureSubpacketsHelper.toVector((SignatureSubpackets) hashedSubpackets),
|
|
|
|
SignatureSubpacketsHelper.toVector((SignatureSubpackets) unhashedSubpackets),
|
|
|
|
primaryKeyBindingSigner);
|
2021-11-08 21:25:02 +01:00
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
secretKeyRing = ringGenerator.generateSecretKeyRing();
|
2021-11-08 21:25:02 +01:00
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
return this;
|
2020-11-10 17:25:35 +01:00
|
|
|
}
|
|
|
|
|
2020-11-22 21:25:52 +01:00
|
|
|
@Override
|
|
|
|
public SecretKeyRingEditorInterface revoke(SecretKeyRingProtector secretKeyRingProtector,
|
2021-11-16 15:18:51 +01:00
|
|
|
@Nullable RevocationAttributes revocationAttributes)
|
2020-11-22 21:25:52 +01:00
|
|
|
throws PGPException {
|
2021-11-16 15:18:51 +01:00
|
|
|
RevocationSignatureSubpackets.Callback callback = callbackFromRevocationAttributes(revocationAttributes);
|
|
|
|
return revoke(secretKeyRingProtector, callback);
|
2020-11-22 21:25:52 +01:00
|
|
|
}
|
|
|
|
|
2020-10-22 01:20:43 +02:00
|
|
|
@Override
|
2021-11-16 15:18:51 +01:00
|
|
|
public SecretKeyRingEditorInterface revoke(SecretKeyRingProtector secretKeyRingProtector,
|
|
|
|
@Nullable RevocationSignatureSubpackets.Callback subpacketsCallback)
|
2020-11-13 14:32:29 +01:00
|
|
|
throws PGPException {
|
2021-11-16 15:18:51 +01:00
|
|
|
return revokeSubKey(secretKeyRing.getSecretKey().getKeyID(), secretKeyRingProtector, subpacketsCallback);
|
2020-11-13 16:59:55 +01:00
|
|
|
}
|
2020-11-13 14:32:29 +01:00
|
|
|
|
2020-11-13 16:59:55 +01:00
|
|
|
@Override
|
2020-11-20 12:19:45 +01:00
|
|
|
public SecretKeyRingEditorInterface revokeSubKey(long subKeyId,
|
|
|
|
SecretKeyRingProtector protector,
|
|
|
|
RevocationAttributes revocationAttributes)
|
|
|
|
throws PGPException {
|
2021-11-16 15:18:51 +01:00
|
|
|
RevocationSignatureSubpackets.Callback callback = callbackFromRevocationAttributes(revocationAttributes);
|
|
|
|
return revokeSubKey(subKeyId, protector, callback);
|
|
|
|
}
|
2020-11-13 14:32:29 +01:00
|
|
|
|
2021-11-16 15:18:51 +01:00
|
|
|
@Override
|
|
|
|
public SecretKeyRingEditorInterface revokeSubKey(long keyID,
|
|
|
|
SecretKeyRingProtector secretKeyRingProtector,
|
|
|
|
@Nullable RevocationSignatureSubpackets.Callback subpacketsCallback)
|
|
|
|
throws PGPException {
|
2021-11-20 16:07:27 +01:00
|
|
|
// retrieve subkey to be revoked
|
2021-11-16 15:18:51 +01:00
|
|
|
PGPPublicKey revokeeSubKey = KeyRingUtils.requirePublicKeyFrom(secretKeyRing, keyID);
|
2021-11-20 16:07:27 +01:00
|
|
|
// create revocation
|
2021-11-16 15:18:51 +01:00
|
|
|
PGPSignature subKeyRevocation = generateRevocation(secretKeyRingProtector, revokeeSubKey,
|
|
|
|
subpacketsCallback);
|
2021-11-20 16:07:27 +01:00
|
|
|
// inject revocation sig into key ring
|
|
|
|
secretKeyRing = KeyRingUtils.injectCertification(secretKeyRing, revokeeSubKey, subKeyRevocation);
|
2020-11-13 16:59:55 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:18:51 +01:00
|
|
|
@Override
|
|
|
|
public PGPSignature createRevocationCertificate(SecretKeyRingProtector secretKeyRingProtector,
|
|
|
|
RevocationAttributes revocationAttributes)
|
|
|
|
throws PGPException {
|
|
|
|
PGPPublicKey revokeeSubKey = secretKeyRing.getPublicKey();
|
|
|
|
PGPSignature revocationCertificate = generateRevocation(
|
|
|
|
secretKeyRingProtector, revokeeSubKey, callbackFromRevocationAttributes(revocationAttributes));
|
|
|
|
return revocationCertificate;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public PGPSignature createRevocationCertificate(
|
|
|
|
long subkeyId,
|
|
|
|
SecretKeyRingProtector secretKeyRingProtector,
|
|
|
|
RevocationAttributes revocationAttributes)
|
|
|
|
throws PGPException {
|
|
|
|
PGPPublicKey revokeeSubkey = KeyRingUtils.requirePublicKeyFrom(secretKeyRing, subkeyId);
|
|
|
|
RevocationSignatureSubpackets.Callback callback = callbackFromRevocationAttributes(revocationAttributes);
|
|
|
|
return generateRevocation(secretKeyRingProtector, revokeeSubkey, callback);
|
|
|
|
}
|
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
@Override
|
|
|
|
public PGPSignature createRevocationCertificate(
|
|
|
|
long subkeyId,
|
|
|
|
SecretKeyRingProtector secretKeyRingProtector,
|
|
|
|
@Nullable RevocationSignatureSubpackets.Callback certificateSubpacketsCallback)
|
|
|
|
throws PGPException {
|
|
|
|
PGPPublicKey revokeeSubkey = KeyRingUtils.requirePublicKeyFrom(secretKeyRing, subkeyId);
|
|
|
|
return generateRevocation(secretKeyRingProtector, revokeeSubkey, certificateSubpacketsCallback);
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:18:51 +01:00
|
|
|
private PGPSignature generateRevocation(SecretKeyRingProtector protector,
|
|
|
|
PGPPublicKey revokeeSubKey,
|
|
|
|
@Nullable RevocationSignatureSubpackets.Callback callback)
|
|
|
|
throws PGPException {
|
|
|
|
PGPSecretKey primaryKey = secretKeyRing.getSecretKey();
|
|
|
|
SignatureType signatureType = revokeeSubKey.isMasterKey() ?
|
|
|
|
SignatureType.KEY_REVOCATION : SignatureType.SUBKEY_REVOCATION;
|
|
|
|
|
|
|
|
RevocationSignatureBuilder signatureBuilder =
|
|
|
|
new RevocationSignatureBuilder(signatureType, primaryKey, protector);
|
|
|
|
signatureBuilder.applyCallback(callback);
|
|
|
|
PGPSignature revocation = signatureBuilder.build(revokeeSubKey);
|
|
|
|
return revocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static RevocationSignatureSubpackets.Callback callbackFromRevocationAttributes(
|
|
|
|
RevocationAttributes attributes) {
|
|
|
|
return new RevocationSignatureSubpackets.Callback() {
|
|
|
|
@Override
|
|
|
|
public void modifyHashedSubpackets(RevocationSignatureSubpackets hashedSubpackets) {
|
|
|
|
if (attributes != null) {
|
|
|
|
hashedSubpackets.setRevocationReason(attributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:28:48 +01:00
|
|
|
@Override
|
|
|
|
public SecretKeyRingEditorInterface revokeUserId(String userId,
|
|
|
|
SecretKeyRingProtector secretKeyRingProtector,
|
2021-11-16 15:35:17 +01:00
|
|
|
@Nullable RevocationAttributes revocationAttributes)
|
|
|
|
throws PGPException {
|
|
|
|
if (revocationAttributes != null) {
|
|
|
|
RevocationAttributes.Reason reason = revocationAttributes.getReason();
|
|
|
|
if (reason != RevocationAttributes.Reason.NO_REASON
|
|
|
|
&& reason != RevocationAttributes.Reason.USER_ID_NO_LONGER_VALID) {
|
|
|
|
throw new IllegalArgumentException("Revocation reason must either be NO_REASON or USER_ID_NO_LONGER_VALID");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RevocationSignatureSubpackets.Callback callback = new RevocationSignatureSubpackets.Callback() {
|
|
|
|
@Override
|
|
|
|
public void modifyHashedSubpackets(RevocationSignatureSubpackets hashedSubpackets) {
|
|
|
|
if (revocationAttributes != null) {
|
|
|
|
hashedSubpackets.setRevocationReason(false, revocationAttributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return revokeUserId(userId, secretKeyRingProtector, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SecretKeyRingEditorInterface revokeUserId(
|
|
|
|
String userId,
|
|
|
|
SecretKeyRingProtector secretKeyRingProtector,
|
|
|
|
@Nullable RevocationSignatureSubpackets.Callback subpacketCallback)
|
2021-01-22 18:28:48 +01:00
|
|
|
throws PGPException {
|
2021-05-28 23:14:20 +02:00
|
|
|
Iterator<String> userIds = secretKeyRing.getPublicKey().getUserIDs();
|
|
|
|
boolean found = false;
|
|
|
|
while (userIds.hasNext()) {
|
|
|
|
if (userId.equals(userIds.next())) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-22 18:28:48 +01:00
|
|
|
}
|
2021-05-28 23:14:20 +02:00
|
|
|
if (!found) {
|
|
|
|
throw new NoSuchElementException("No user-id '" + userId + "' found on the key.");
|
2021-01-22 18:28:48 +01:00
|
|
|
}
|
2021-11-16 15:35:17 +01:00
|
|
|
return doRevokeUserId(userId, secretKeyRingProtector, subpacketCallback);
|
2021-01-22 18:28:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private SecretKeyRingEditorInterface doRevokeUserId(String userId,
|
|
|
|
SecretKeyRingProtector protector,
|
2021-11-16 15:35:17 +01:00
|
|
|
@Nullable RevocationSignatureSubpackets.Callback callback)
|
2021-11-16 15:18:51 +01:00
|
|
|
throws PGPException {
|
2021-05-28 23:14:20 +02:00
|
|
|
PGPSecretKey primarySecretKey = secretKeyRing.getSecretKey();
|
|
|
|
PGPPublicKey primaryPublicKey = primarySecretKey.getPublicKey();
|
2021-11-16 15:35:17 +01:00
|
|
|
RevocationSignatureBuilder signatureBuilder = new RevocationSignatureBuilder(
|
|
|
|
SignatureType.CERTIFICATION_REVOCATION,
|
|
|
|
primarySecretKey,
|
|
|
|
protector);
|
2021-01-22 18:28:48 +01:00
|
|
|
|
2021-11-16 15:35:17 +01:00
|
|
|
signatureBuilder.applyCallback(callback);
|
2021-01-22 18:28:48 +01:00
|
|
|
|
2021-11-16 15:35:17 +01:00
|
|
|
PGPSignature revocationSignature = signatureBuilder.build(userId);
|
2021-11-20 16:07:27 +01:00
|
|
|
secretKeyRing = KeyRingUtils.injectCertification(secretKeyRing, userId, revocationSignature);
|
2021-01-22 18:28:48 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
@Override
|
|
|
|
public SecretKeyRingEditorInterface setExpirationDate(Date expiration,
|
|
|
|
SecretKeyRingProtector secretKeyRingProtector)
|
|
|
|
throws PGPException {
|
2021-10-27 17:38:25 +02:00
|
|
|
return setExpirationDate(OpenPgpFingerprint.of(secretKeyRing), expiration, secretKeyRingProtector);
|
2021-01-18 17:09:57 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 13:00:06 +01:00
|
|
|
@Override
|
2021-10-27 17:38:25 +02:00
|
|
|
public SecretKeyRingEditorInterface setExpirationDate(OpenPgpFingerprint fingerprint,
|
2020-11-27 13:00:06 +01:00
|
|
|
Date expiration,
|
|
|
|
SecretKeyRingProtector secretKeyRingProtector)
|
|
|
|
throws PGPException {
|
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
List<PGPSecretKey> secretKeyList = new ArrayList<>();
|
|
|
|
PGPSecretKey primaryKey = secretKeyRing.getSecretKey();
|
|
|
|
if (!primaryKey.isMasterKey()) {
|
|
|
|
throw new IllegalArgumentException("Key Ring does not appear to contain a primary secret key.");
|
2020-11-27 13:00:06 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
boolean found = false;
|
2021-06-10 12:42:48 +02:00
|
|
|
for (PGPSecretKey secretKey : secretKeyRing) {
|
2021-01-18 17:09:57 +01:00
|
|
|
// Skip over unaffected subkeys
|
|
|
|
if (secretKey.getKeyID() != fingerprint.getKeyId()) {
|
|
|
|
secretKeyList.add(secretKey);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// We found the target subkey
|
|
|
|
found = true;
|
|
|
|
secretKey = setExpirationDate(primaryKey, secretKey, expiration, secretKeyRingProtector);
|
|
|
|
secretKeyList.add(secretKey);
|
|
|
|
}
|
2020-11-27 13:00:06 +01:00
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
if (!found) {
|
|
|
|
throw new IllegalArgumentException("Key Ring does not contain secret key with fingerprint " + fingerprint);
|
2020-11-27 13:00:06 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
secretKeyRing = new PGPSecretKeyRing(secretKeyList);
|
2020-11-27 13:00:06 +01:00
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
return this;
|
|
|
|
}
|
2020-11-27 13:00:06 +01:00
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
private PGPSecretKey setExpirationDate(PGPSecretKey primaryKey,
|
|
|
|
PGPSecretKey subjectKey,
|
|
|
|
Date expiration,
|
|
|
|
SecretKeyRingProtector secretKeyRingProtector)
|
|
|
|
throws PGPException {
|
|
|
|
|
|
|
|
if (expiration != null && expiration.before(subjectKey.getPublicKey().getCreationTime())) {
|
|
|
|
throw new IllegalArgumentException("Expiration date cannot be before creation date.");
|
2020-11-27 13:00:06 +01:00
|
|
|
}
|
|
|
|
|
2021-05-14 13:18:34 +02:00
|
|
|
PGPPrivateKey privateKey = UnlockSecretKey.unlockSecretKey(primaryKey, secretKeyRingProtector);
|
2021-01-18 17:09:57 +01:00
|
|
|
PGPPublicKey subjectPubKey = subjectKey.getPublicKey();
|
|
|
|
|
|
|
|
PGPSignature oldSignature = getPreviousSignature(primaryKey, subjectPubKey);
|
|
|
|
|
|
|
|
PGPSignatureSubpacketVector oldSubpackets = oldSignature.getHashedSubPackets();
|
|
|
|
PGPSignatureSubpacketGenerator subpacketGenerator = new PGPSignatureSubpacketGenerator(oldSubpackets);
|
|
|
|
SignatureSubpacketGeneratorUtil.setSignatureCreationTimeInSubpacketGenerator(new Date(), subpacketGenerator);
|
2021-11-16 15:18:51 +01:00
|
|
|
SignatureSubpacketGeneratorUtil.setKeyExpirationDateInSubpacketGenerator(
|
|
|
|
expiration, subjectPubKey.getCreationTime(), subpacketGenerator);
|
2021-01-18 17:09:57 +01:00
|
|
|
|
|
|
|
PGPSignatureGenerator signatureGenerator = SignatureUtils.getSignatureGeneratorFor(primaryKey);
|
|
|
|
signatureGenerator.setHashedSubpackets(subpacketGenerator.generate());
|
|
|
|
|
|
|
|
if (primaryKey.getKeyID() == subjectKey.getKeyID()) {
|
|
|
|
signatureGenerator.init(PGPSignature.POSITIVE_CERTIFICATION, privateKey);
|
2020-11-27 13:00:06 +01:00
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
for (Iterator<String> it = subjectKey.getUserIDs(); it.hasNext(); ) {
|
|
|
|
String userId = it.next();
|
|
|
|
PGPSignature signature = signatureGenerator.generateCertification(userId, subjectPubKey);
|
|
|
|
subjectPubKey = PGPPublicKey.addCertification(subjectPubKey, userId, signature);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
signatureGenerator.init(PGPSignature.SUBKEY_BINDING, privateKey);
|
2020-11-27 13:00:06 +01:00
|
|
|
|
2021-11-16 15:18:51 +01:00
|
|
|
PGPSignature signature = signatureGenerator.generateCertification(
|
|
|
|
primaryKey.getPublicKey(), subjectPubKey);
|
2021-01-18 17:09:57 +01:00
|
|
|
subjectPubKey = PGPPublicKey.addCertification(subjectPubKey, signature);
|
2020-11-27 13:00:06 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
subjectKey = PGPSecretKey.replacePublicKey(subjectKey, subjectPubKey);
|
|
|
|
return subjectKey;
|
|
|
|
}
|
2020-11-27 13:00:06 +01:00
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
private PGPSignature getPreviousSignature(PGPSecretKey primaryKey, PGPPublicKey subjectPubKey) {
|
|
|
|
PGPSignature oldSignature = null;
|
|
|
|
if (primaryKey.getKeyID() == subjectPubKey.getKeyID()) {
|
|
|
|
Iterator<PGPSignature> keySignatures = subjectPubKey.getSignaturesForKeyID(primaryKey.getKeyID());
|
|
|
|
while (keySignatures.hasNext()) {
|
|
|
|
PGPSignature next = keySignatures.next();
|
2021-09-08 11:59:28 +02:00
|
|
|
SignatureType type = SignatureType.valueOf(next.getSignatureType());
|
|
|
|
if (type == SignatureType.POSITIVE_CERTIFICATION ||
|
|
|
|
type == SignatureType.CASUAL_CERTIFICATION ||
|
|
|
|
type == SignatureType.GENERIC_CERTIFICATION) {
|
2021-01-18 17:09:57 +01:00
|
|
|
oldSignature = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (oldSignature == null) {
|
2021-11-16 15:18:51 +01:00
|
|
|
throw new IllegalStateException("Key " + OpenPgpFingerprint.of(subjectPubKey) +
|
|
|
|
" does not have a previous positive/casual/generic certification signature.");
|
2021-01-18 17:09:57 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-16 15:18:51 +01:00
|
|
|
Iterator<PGPSignature> bindingSignatures = subjectPubKey.getSignaturesOfType(
|
|
|
|
SignatureType.SUBKEY_BINDING.getCode());
|
2021-01-18 17:09:57 +01:00
|
|
|
while (bindingSignatures.hasNext()) {
|
2021-06-10 12:41:12 +02:00
|
|
|
oldSignature = bindingSignatures.next();
|
2021-01-18 17:09:57 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-27 13:00:06 +01:00
|
|
|
|
2021-01-18 17:09:57 +01:00
|
|
|
if (oldSignature == null) {
|
2021-11-16 15:18:51 +01:00
|
|
|
throw new IllegalStateException("Key " + OpenPgpFingerprint.of(subjectPubKey) +
|
|
|
|
" does not have a previous subkey binding signature.");
|
2021-01-18 17:09:57 +01:00
|
|
|
}
|
|
|
|
return oldSignature;
|
2020-11-27 13:00:06 +01:00
|
|
|
}
|
|
|
|
|
2020-11-20 12:01:39 +01:00
|
|
|
@Override
|
2021-11-16 15:18:51 +01:00
|
|
|
public WithKeyRingEncryptionSettings changePassphraseFromOldPassphrase(
|
|
|
|
@Nullable Passphrase oldPassphrase,
|
|
|
|
@Nonnull KeyRingProtectionSettings oldProtectionSettings) {
|
2020-10-25 20:43:09 +01:00
|
|
|
SecretKeyRingProtector protector = new PasswordBasedSecretKeyRingProtector(
|
|
|
|
oldProtectionSettings,
|
|
|
|
new SolitaryPassphraseProvider(oldPassphrase));
|
|
|
|
|
|
|
|
return new WithKeyRingEncryptionSettingsImpl(null, protector);
|
2020-10-23 16:44:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-16 15:18:51 +01:00
|
|
|
public WithKeyRingEncryptionSettings changeSubKeyPassphraseFromOldPassphrase(
|
|
|
|
@Nonnull Long keyId,
|
|
|
|
@Nullable Passphrase oldPassphrase,
|
|
|
|
@Nonnull KeyRingProtectionSettings oldProtectionSettings) {
|
2020-10-25 20:43:09 +01:00
|
|
|
Map<Long, Passphrase> passphraseMap = Collections.singletonMap(keyId, oldPassphrase);
|
2021-05-14 18:55:26 +02:00
|
|
|
SecretKeyRingProtector protector = new CachingSecretKeyRingProtector(
|
2020-10-25 20:43:09 +01:00
|
|
|
passphraseMap, oldProtectionSettings, null);
|
|
|
|
|
|
|
|
return new WithKeyRingEncryptionSettingsImpl(keyId, protector);
|
2020-10-23 16:44:21 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 01:20:43 +02:00
|
|
|
@Override
|
|
|
|
public PGPSecretKeyRing done() {
|
|
|
|
return secretKeyRing;
|
|
|
|
}
|
2020-10-23 16:44:21 +02:00
|
|
|
|
2020-10-25 20:43:09 +01:00
|
|
|
private final class WithKeyRingEncryptionSettingsImpl implements WithKeyRingEncryptionSettings {
|
|
|
|
|
|
|
|
private final Long keyId;
|
|
|
|
// Protector to unlock the key with the old passphrase
|
|
|
|
private final SecretKeyRingProtector oldProtector;
|
|
|
|
|
2020-10-29 15:15:13 +01:00
|
|
|
/**
|
|
|
|
* Builder for selecting protection settings.
|
|
|
|
*
|
|
|
|
* If the keyId is null, the whole keyRing will get the same new passphrase.
|
|
|
|
*
|
|
|
|
* @param keyId id of the subkey whose passphrase will be changed, or null.
|
|
|
|
* @param oldProtector protector do unlock the key/ring.
|
|
|
|
*/
|
2020-10-25 20:43:09 +01:00
|
|
|
private WithKeyRingEncryptionSettingsImpl(Long keyId, SecretKeyRingProtector oldProtector) {
|
|
|
|
this.keyId = keyId;
|
|
|
|
this.oldProtector = oldProtector;
|
|
|
|
}
|
2020-10-23 16:44:21 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public WithPassphrase withSecureDefaultSettings() {
|
|
|
|
return withCustomSettings(KeyRingProtectionSettings.secureDefaultSettings());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public WithPassphrase withCustomSettings(KeyRingProtectionSettings settings) {
|
2020-10-25 20:43:09 +01:00
|
|
|
return new WithPassphraseImpl(keyId, oldProtector, settings);
|
2020-10-23 16:44:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 20:43:09 +01:00
|
|
|
private final class WithPassphraseImpl implements WithPassphrase {
|
|
|
|
|
|
|
|
private final SecretKeyRingProtector oldProtector;
|
|
|
|
private final KeyRingProtectionSettings newProtectionSettings;
|
|
|
|
private final Long keyId;
|
|
|
|
|
2021-11-16 15:18:51 +01:00
|
|
|
private WithPassphraseImpl(
|
|
|
|
Long keyId,
|
|
|
|
SecretKeyRingProtector oldProtector,
|
|
|
|
KeyRingProtectionSettings newProtectionSettings) {
|
2020-10-25 20:43:09 +01:00
|
|
|
this.keyId = keyId;
|
|
|
|
this.oldProtector = oldProtector;
|
|
|
|
this.newProtectionSettings = newProtectionSettings;
|
|
|
|
}
|
2020-10-23 16:44:21 +02:00
|
|
|
|
|
|
|
@Override
|
2021-11-16 15:18:51 +01:00
|
|
|
public SecretKeyRingEditorInterface toNewPassphrase(Passphrase passphrase)
|
|
|
|
throws PGPException {
|
2020-10-25 20:43:09 +01:00
|
|
|
SecretKeyRingProtector newProtector = new PasswordBasedSecretKeyRingProtector(
|
|
|
|
newProtectionSettings, new SolitaryPassphraseProvider(passphrase));
|
|
|
|
|
2021-11-16 15:18:51 +01:00
|
|
|
PGPSecretKeyRing secretKeys = changePassphrase(
|
|
|
|
keyId, SecretKeyRingEditor.this.secretKeyRing, oldProtector, newProtector);
|
2020-11-19 17:51:57 +01:00
|
|
|
SecretKeyRingEditor.this.secretKeyRing = secretKeys;
|
2020-10-25 20:43:09 +01:00
|
|
|
|
2020-11-19 17:51:57 +01:00
|
|
|
return SecretKeyRingEditor.this;
|
2020-10-23 16:44:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-16 15:18:51 +01:00
|
|
|
public SecretKeyRingEditorInterface toNoPassphrase()
|
|
|
|
throws PGPException {
|
2020-10-25 20:43:09 +01:00
|
|
|
SecretKeyRingProtector newProtector = new UnprotectedKeysProtector();
|
|
|
|
|
2021-11-16 15:18:51 +01:00
|
|
|
PGPSecretKeyRing secretKeys = changePassphrase(
|
|
|
|
keyId, SecretKeyRingEditor.this.secretKeyRing, oldProtector, newProtector);
|
2020-11-19 17:51:57 +01:00
|
|
|
SecretKeyRingEditor.this.secretKeyRing = secretKeys;
|
2020-10-25 20:43:09 +01:00
|
|
|
|
2020-11-19 17:51:57 +01:00
|
|
|
return SecretKeyRingEditor.this;
|
2020-10-23 16:44:21 +02:00
|
|
|
}
|
2020-11-10 17:25:35 +01:00
|
|
|
}
|
2020-10-25 20:43:09 +01:00
|
|
|
|
2020-11-10 17:25:35 +01:00
|
|
|
private PGPSecretKeyRing changePassphrase(Long keyId,
|
|
|
|
PGPSecretKeyRing secretKeys,
|
|
|
|
SecretKeyRingProtector oldProtector,
|
2021-11-16 15:18:51 +01:00
|
|
|
SecretKeyRingProtector newProtector)
|
|
|
|
throws PGPException {
|
2021-09-10 20:14:12 +02:00
|
|
|
List<PGPSecretKey> secretKeyList = new ArrayList<>();
|
2020-11-10 17:25:35 +01:00
|
|
|
if (keyId == null) {
|
|
|
|
// change passphrase of whole key ring
|
|
|
|
Iterator<PGPSecretKey> secretKeyIterator = secretKeys.getSecretKeys();
|
|
|
|
while (secretKeyIterator.hasNext()) {
|
|
|
|
PGPSecretKey secretKey = secretKeyIterator.next();
|
2021-07-19 14:52:59 +02:00
|
|
|
secretKey = reencryptPrivateKey(secretKey, oldProtector, newProtector);
|
2021-09-10 20:14:12 +02:00
|
|
|
secretKeyList.add(secretKey);
|
2020-11-10 17:25:35 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// change passphrase of selected subkey only
|
|
|
|
Iterator<PGPSecretKey> secretKeyIterator = secretKeys.getSecretKeys();
|
|
|
|
while (secretKeyIterator.hasNext()) {
|
|
|
|
PGPSecretKey secretKey = secretKeyIterator.next();
|
|
|
|
if (secretKey.getPublicKey().getKeyID() == keyId) {
|
|
|
|
// Re-encrypt only the selected subkey
|
2021-07-19 14:52:59 +02:00
|
|
|
secretKey = reencryptPrivateKey(secretKey, oldProtector, newProtector);
|
2020-10-25 20:43:09 +01:00
|
|
|
}
|
2020-11-10 17:25:35 +01:00
|
|
|
secretKeyList.add(secretKey);
|
2020-10-25 20:43:09 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-10 20:14:12 +02:00
|
|
|
|
|
|
|
PGPSecretKeyRing newRing = new PGPSecretKeyRing(secretKeyList);
|
|
|
|
newRing = s2kUsageFixIfNecessary(newRing, newProtector);
|
|
|
|
return newRing;
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:18:51 +01:00
|
|
|
private PGPSecretKeyRing s2kUsageFixIfNecessary(PGPSecretKeyRing secretKeys, SecretKeyRingProtector protector)
|
|
|
|
throws PGPException {
|
2021-09-10 20:14:12 +02:00
|
|
|
boolean hasS2KUsageChecksum = false;
|
|
|
|
for (PGPSecretKey secKey : secretKeys) {
|
|
|
|
if (secKey.getS2KUsage() == SecretKeyPacket.USAGE_CHECKSUM) {
|
|
|
|
hasS2KUsageChecksum = true;
|
2021-09-10 21:04:36 +02:00
|
|
|
break;
|
2021-09-10 20:14:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasS2KUsageChecksum) {
|
2021-11-16 15:18:51 +01:00
|
|
|
secretKeys = S2KUsageFix.replaceUsageChecksumWithUsageSha1(
|
|
|
|
secretKeys, protector, true);
|
2021-09-10 20:14:12 +02:00
|
|
|
}
|
|
|
|
return secretKeys;
|
2020-11-10 17:25:35 +01:00
|
|
|
}
|
2020-10-25 20:43:09 +01:00
|
|
|
|
2021-11-16 15:18:51 +01:00
|
|
|
private static PGPSecretKey reencryptPrivateKey(
|
|
|
|
PGPSecretKey secretKey,
|
|
|
|
SecretKeyRingProtector oldProtector,
|
|
|
|
SecretKeyRingProtector newProtector)
|
|
|
|
throws PGPException {
|
2021-07-19 14:52:59 +02:00
|
|
|
S2K s2k = secretKey.getS2K();
|
|
|
|
// If the key uses GNU_DUMMY_S2K, we leave it as is and skip this block
|
|
|
|
if (s2k == null || s2k.getType() != S2K.GNU_DUMMY_S2K) {
|
|
|
|
long secretKeyId = secretKey.getKeyID();
|
|
|
|
PBESecretKeyDecryptor decryptor = oldProtector.getDecryptor(secretKeyId);
|
|
|
|
PBESecretKeyEncryptor encryptor = newProtector.getEncryptor(secretKeyId);
|
|
|
|
secretKey = PGPSecretKey.copyWithNewPassword(secretKey, decryptor, encryptor);
|
|
|
|
}
|
2020-11-10 17:25:35 +01:00
|
|
|
return secretKey;
|
2020-10-23 16:44:21 +02:00
|
|
|
}
|
2020-10-22 01:20:43 +02:00
|
|
|
}
|