mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-10 06:11:08 +01:00
Allow modification of keys with custom reference date
Also, bind subkeys using SubkeyBindingSignatureBuilder
This commit is contained in:
parent
3030de7f3f
commit
c3dc3c9d87
10 changed files with 207 additions and 170 deletions
|
|
@ -127,7 +127,11 @@ public final class PGPainless {
|
|||
* @return builder
|
||||
*/
|
||||
public static SecretKeyRingEditorInterface modifyKeyRing(PGPSecretKeyRing secretKeys) {
|
||||
return new SecretKeyRingEditor(secretKeys);
|
||||
return modifyKeyRing(secretKeys, null);
|
||||
}
|
||||
|
||||
public static SecretKeyRingEditorInterface modifyKeyRing(PGPSecretKeyRing secretKeys, Date referenceTime) {
|
||||
return new SecretKeyRingEditor(secretKeys, referenceTime);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -85,10 +85,10 @@ public class KeyRingInfo {
|
|||
* Evaluate the key ring at the provided validation date.
|
||||
*
|
||||
* @param keys key ring
|
||||
* @param validationDate date of validation
|
||||
* @param referenceDate date of validation
|
||||
*/
|
||||
public KeyRingInfo(PGPKeyRing keys, Date validationDate) {
|
||||
this(keys, PGPainless.getPolicy(), validationDate);
|
||||
public KeyRingInfo(PGPKeyRing keys, Date referenceDate) {
|
||||
this(keys, PGPainless.getPolicy(), referenceDate);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -96,12 +96,12 @@ public class KeyRingInfo {
|
|||
*
|
||||
* @param keys key ring
|
||||
* @param policy policy
|
||||
* @param validationDate validation date
|
||||
* @param referenceDate validation date
|
||||
*/
|
||||
public KeyRingInfo(PGPKeyRing keys, Policy policy, Date validationDate) {
|
||||
public KeyRingInfo(PGPKeyRing keys, Policy policy, Date referenceDate) {
|
||||
this.referenceDate = referenceDate != null ? referenceDate : new Date();
|
||||
this.keys = keys;
|
||||
this.signatures = new Signatures(keys, validationDate, policy);
|
||||
this.referenceDate = validationDate;
|
||||
this.signatures = new Signatures(keys, this.referenceDate, policy);
|
||||
this.primaryUserId = findPrimaryUserId();
|
||||
this.revocationState = findRevocationState();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,35 +4,17 @@
|
|||
|
||||
package org.pgpainless.key.modification.secretkeyring;
|
||||
|
||||
import static org.pgpainless.util.CollectionUtils.concat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.bouncycastle.bcpg.S2K;
|
||||
import org.bouncycastle.bcpg.SecretKeyPacket;
|
||||
import org.bouncycastle.bcpg.sig.KeyExpirationTime;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPKeyPair;
|
||||
import org.bouncycastle.openpgp.PGPKeyRingGenerator;
|
||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||
import org.bouncycastle.openpgp.PGPSecretKey;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSignature;
|
||||
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
|
||||
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
|
||||
import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.algorithm.AlgorithmSuite;
|
||||
import org.pgpainless.algorithm.CompressionAlgorithm;
|
||||
|
|
@ -57,26 +39,49 @@ import org.pgpainless.key.protection.passphrase_provider.SolitaryPassphraseProvi
|
|||
import org.pgpainless.key.util.KeyRingUtils;
|
||||
import org.pgpainless.key.util.RevocationAttributes;
|
||||
import org.pgpainless.signature.builder.DirectKeySelfSignatureBuilder;
|
||||
import org.pgpainless.signature.builder.PrimaryKeyBindingSignatureBuilder;
|
||||
import org.pgpainless.signature.builder.RevocationSignatureBuilder;
|
||||
import org.pgpainless.signature.builder.SelfSignatureBuilder;
|
||||
import org.pgpainless.signature.builder.SubkeyBindingSignatureBuilder;
|
||||
import org.pgpainless.signature.subpackets.RevocationSignatureSubpackets;
|
||||
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
|
||||
import org.pgpainless.signature.subpackets.SignatureSubpackets;
|
||||
import org.pgpainless.signature.subpackets.SignatureSubpacketsHelper;
|
||||
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil;
|
||||
import org.pgpainless.util.CollectionUtils;
|
||||
import org.pgpainless.util.Passphrase;
|
||||
import org.pgpainless.util.selection.userid.SelectUserId;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.IOException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.pgpainless.util.CollectionUtils.concat;
|
||||
|
||||
public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
||||
|
||||
private PGPSecretKeyRing secretKeyRing;
|
||||
private final Date referenceTime;
|
||||
|
||||
public SecretKeyRingEditor(PGPSecretKeyRing secretKeyRing) {
|
||||
this(secretKeyRing, null);
|
||||
}
|
||||
|
||||
public SecretKeyRingEditor(PGPSecretKeyRing secretKeyRing, Date referenceTime) {
|
||||
if (secretKeyRing == null) {
|
||||
throw new NullPointerException("SecretKeyRing MUST NOT be null.");
|
||||
}
|
||||
this.secretKeyRing = secretKeyRing;
|
||||
this.referenceTime = referenceTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -99,7 +104,7 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
PGPSecretKey primaryKey = secretKeyRing.getSecretKey();
|
||||
|
||||
// retain key flags from previous signature
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing);
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing, referenceTime);
|
||||
if (info.isHardRevoked(userId.toString())) {
|
||||
throw new IllegalArgumentException("User-ID " + userId + " is hard revoked and cannot be re-certified.");
|
||||
}
|
||||
|
|
@ -121,6 +126,9 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
}
|
||||
|
||||
SelfSignatureBuilder builder = new SelfSignatureBuilder(primaryKey, protector);
|
||||
if (referenceTime != null) {
|
||||
builder.getHashedSubpackets().setSignatureCreationTime(referenceTime);
|
||||
}
|
||||
builder.setSignatureType(SignatureType.POSITIVE_CERTIFICATION);
|
||||
|
||||
// Retain signature subpackets of previous signatures
|
||||
|
|
@ -145,7 +153,7 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
|
||||
// Determine previous key expiration date
|
||||
PGPPublicKey primaryKey = secretKeyRing.getSecretKey().getPublicKey();
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing);
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing, referenceTime);
|
||||
String primaryUserId = info.getPrimaryUserId();
|
||||
PGPSignature signature = primaryUserId == null ?
|
||||
info.getLatestDirectKeySelfSignature() : info.getLatestUserIdCertification(primaryUserId);
|
||||
|
|
@ -169,7 +177,7 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
protector);
|
||||
|
||||
// unmark previous primary user-ids to be non-primary
|
||||
info = PGPainless.inspectKeyRing(secretKeyRing);
|
||||
info = PGPainless.inspectKeyRing(secretKeyRing, referenceTime);
|
||||
for (String otherUserId : info.getValidAndExpiredUserIds()) {
|
||||
if (userId.toString().equals(otherUserId)) {
|
||||
continue;
|
||||
|
|
@ -227,7 +235,7 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
throw new IllegalArgumentException("New user-id cannot be empty.");
|
||||
}
|
||||
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing);
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing, referenceTime);
|
||||
if (!info.isUserIdValid(oldUID)) {
|
||||
throw new NoSuchElementException("Key does not carry user-id '" + oldUID + "', or it is not valid.");
|
||||
}
|
||||
|
|
@ -333,46 +341,34 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
}
|
||||
|
||||
PGPSecretKey primaryKey = secretKeyRing.getSecretKey();
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing);
|
||||
PublicKeyAlgorithm signingKeyAlgorithm = PublicKeyAlgorithm.requireFromId(primaryKey.getPublicKey().getAlgorithm());
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing, referenceTime);
|
||||
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()));
|
||||
PGPSecretKey secretSubkey = new PGPSecretKey(subkey.getPrivateKey(), subkey.getPublicKey(), ImplementationFactory.getInstance()
|
||||
.getV4FingerprintCalculator(), false, subkeyProtector.getEncryptor(subkey.getKeyID()));
|
||||
|
||||
SelfSignatureSubpackets hashedSubpackets = SignatureSubpackets.createHashedSubpackets(primaryKey.getPublicKey());
|
||||
SelfSignatureSubpackets unhashedSubpackets = SignatureSubpackets.createEmptySubpackets();
|
||||
hashedSubpackets.setKeyFlags(flags);
|
||||
SubkeyBindingSignatureBuilder skBindingBuilder = new SubkeyBindingSignatureBuilder(primaryKey, primaryKeyProtector, hashAlgorithm);
|
||||
if (referenceTime != null) {
|
||||
skBindingBuilder.getHashedSubpackets().setSignatureCreationTime(referenceTime);
|
||||
}
|
||||
skBindingBuilder.getHashedSubpackets().setKeyFlags(flags);
|
||||
|
||||
if (bindingSignatureCallback != null) {
|
||||
bindingSignatureCallback.modifyHashedSubpackets(hashedSubpackets);
|
||||
bindingSignatureCallback.modifyUnhashedSubpackets(unhashedSubpackets);
|
||||
if (subkeyAlgorithm.isSigningCapable()) {
|
||||
PrimaryKeyBindingSignatureBuilder pkBindingBuilder = new PrimaryKeyBindingSignatureBuilder(secretSubkey, subkeyProtector, hashAlgorithm);
|
||||
if (referenceTime != null) {
|
||||
pkBindingBuilder.getHashedSubpackets().setSignatureCreationTime(referenceTime);
|
||||
}
|
||||
PGPSignature pkBinding = pkBindingBuilder.build(primaryKey.getPublicKey());
|
||||
skBindingBuilder.getHashedSubpackets().addEmbeddedSignature(pkBinding);
|
||||
}
|
||||
|
||||
boolean isSigningKey = CollectionUtils.contains(flags, KeyFlag.SIGN_DATA) ||
|
||||
CollectionUtils.contains(flags, KeyFlag.CERTIFY_OTHER);
|
||||
PGPContentSignerBuilder primaryKeyBindingSigner = null;
|
||||
if (isSigningKey) {
|
||||
primaryKeyBindingSigner = ImplementationFactory.getInstance().getPGPContentSignerBuilder(subkeyAlgorithm, hashAlgorithm);
|
||||
}
|
||||
|
||||
ringGenerator.addSubKey(subkey,
|
||||
SignatureSubpacketsHelper.toVector((SignatureSubpackets) hashedSubpackets),
|
||||
SignatureSubpacketsHelper.toVector((SignatureSubpackets) unhashedSubpackets),
|
||||
primaryKeyBindingSigner);
|
||||
|
||||
secretKeyRing = ringGenerator.generateSecretKeyRing();
|
||||
skBindingBuilder.applyCallback(bindingSignatureCallback);
|
||||
PGPSignature skBinding = skBindingBuilder.build(secretSubkey.getPublicKey());
|
||||
|
||||
secretSubkey = KeyRingUtils.secretKeyPlusSignature(secretSubkey, skBinding);
|
||||
secretKeyRing = KeyRingUtils.keysPlusSecretKey(secretKeyRing, secretSubkey);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -558,6 +554,9 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
SignatureType.CERTIFICATION_REVOCATION,
|
||||
primarySecretKey,
|
||||
protector);
|
||||
if (referenceTime != null) {
|
||||
signatureBuilder.getHashedSubpackets().setSignatureCreationTime(referenceTime);
|
||||
}
|
||||
|
||||
signatureBuilder.applyCallback(callback);
|
||||
|
||||
|
|
@ -585,14 +584,14 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
}
|
||||
|
||||
// reissue primary user-id sig
|
||||
String primaryUserId = PGPainless.inspectKeyRing(secretKeyRing).getPossiblyExpiredPrimaryUserId();
|
||||
String primaryUserId = PGPainless.inspectKeyRing(secretKeyRing, referenceTime).getPossiblyExpiredPrimaryUserId();
|
||||
if (primaryUserId != null) {
|
||||
PGPSignature prevUserIdSig = getPreviousUserIdSignatures(primaryUserId);
|
||||
PGPSignature userIdSig = reissuePrimaryUserIdSig(expiration, secretKeyRingProtector, primaryUserId, prevUserIdSig);
|
||||
secretKeyRing = KeyRingUtils.injectCertification(secretKeyRing, primaryUserId, userIdSig);
|
||||
}
|
||||
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing);
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing, referenceTime);
|
||||
for (String userId : info.getValidUserIds()) {
|
||||
if (userId.equals(primaryUserId)) {
|
||||
continue;
|
||||
|
|
@ -618,6 +617,9 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
PGPSignature prevUserIdSig)
|
||||
throws PGPException {
|
||||
SelfSignatureBuilder builder = new SelfSignatureBuilder(secretKeyRing.getSecretKey(), secretKeyRingProtector, prevUserIdSig);
|
||||
if (referenceTime != null) {
|
||||
builder.getHashedSubpackets().setSignatureCreationTime(referenceTime);
|
||||
}
|
||||
builder.applyCallback(new SelfSignatureSubpackets.Callback() {
|
||||
@Override
|
||||
public void modifyHashedSubpackets(SelfSignatureSubpackets hashedSubpackets) {
|
||||
|
|
@ -638,6 +640,9 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
PGPPublicKey publicKey = primaryKey.getPublicKey();
|
||||
|
||||
SelfSignatureBuilder builder = new SelfSignatureBuilder(primaryKey, secretKeyRingProtector, prevUserIdSig);
|
||||
if (referenceTime != null) {
|
||||
builder.getHashedSubpackets().setSignatureCreationTime(referenceTime);
|
||||
}
|
||||
builder.applyCallback(new SelfSignatureSubpackets.Callback() {
|
||||
@Override
|
||||
public void modifyHashedSubpackets(SelfSignatureSubpackets hashedSubpackets) {
|
||||
|
|
@ -662,6 +667,9 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
final Date keyCreationTime = publicKey.getCreationTime();
|
||||
|
||||
DirectKeySelfSignatureBuilder builder = new DirectKeySelfSignatureBuilder(primaryKey, secretKeyRingProtector, prevDirectKeySig);
|
||||
if (referenceTime != null) {
|
||||
builder.getHashedSubpackets().setSignatureCreationTime(referenceTime);
|
||||
}
|
||||
builder.applyCallback(new SelfSignatureSubpackets.Callback() {
|
||||
@Override
|
||||
public void modifyHashedSubpackets(SelfSignatureSubpackets hashedSubpackets) {
|
||||
|
|
@ -677,12 +685,12 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
|||
}
|
||||
|
||||
private PGPSignature getPreviousDirectKeySignature() {
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing);
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing, referenceTime);
|
||||
return info.getLatestDirectKeySelfSignature();
|
||||
}
|
||||
|
||||
private PGPSignature getPreviousUserIdSignatures(String userId) {
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing);
|
||||
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeyRing, referenceTime);
|
||||
return info.getLatestUserIdCertification(userId);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,9 +10,11 @@ import org.bouncycastle.openpgp.PGPException;
|
|||
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||
import org.bouncycastle.openpgp.PGPSecretKey;
|
||||
import org.bouncycastle.openpgp.PGPSignature;
|
||||
import org.pgpainless.algorithm.HashAlgorithm;
|
||||
import org.pgpainless.algorithm.SignatureType;
|
||||
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
||||
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
|
||||
import org.pgpainless.signature.subpackets.SignatureSubpackets;
|
||||
|
||||
public class PrimaryKeyBindingSignatureBuilder extends AbstractSignatureBuilder<PrimaryKeyBindingSignatureBuilder> {
|
||||
|
||||
|
|
@ -21,6 +23,15 @@ public class PrimaryKeyBindingSignatureBuilder extends AbstractSignatureBuilder<
|
|||
super(SignatureType.PRIMARYKEY_BINDING, subkey, subkeyProtector);
|
||||
}
|
||||
|
||||
public PrimaryKeyBindingSignatureBuilder(PGPSecretKey secretSubKey,
|
||||
SecretKeyRingProtector subkeyProtector,
|
||||
HashAlgorithm hashAlgorithm)
|
||||
throws PGPException {
|
||||
super(SignatureType.PRIMARYKEY_BINDING, secretSubKey, subkeyProtector, hashAlgorithm,
|
||||
SignatureSubpackets.createHashedSubpackets(secretSubKey.getPublicKey()),
|
||||
SignatureSubpackets.createEmptySubpackets());
|
||||
}
|
||||
|
||||
public SelfSignatureSubpackets getHashedSubpackets() {
|
||||
return hashedSubpackets;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,9 +10,11 @@ import org.bouncycastle.openpgp.PGPException;
|
|||
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||
import org.bouncycastle.openpgp.PGPSecretKey;
|
||||
import org.bouncycastle.openpgp.PGPSignature;
|
||||
import org.pgpainless.algorithm.HashAlgorithm;
|
||||
import org.pgpainless.algorithm.SignatureType;
|
||||
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
||||
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
|
||||
import org.pgpainless.signature.subpackets.SignatureSubpackets;
|
||||
|
||||
public class SubkeyBindingSignatureBuilder extends AbstractSignatureBuilder<SubkeyBindingSignatureBuilder> {
|
||||
|
||||
|
|
@ -21,6 +23,13 @@ public class SubkeyBindingSignatureBuilder extends AbstractSignatureBuilder<Subk
|
|||
super(SignatureType.SUBKEY_BINDING, signingKey, protector);
|
||||
}
|
||||
|
||||
public SubkeyBindingSignatureBuilder(PGPSecretKey signingKey, SecretKeyRingProtector protector, HashAlgorithm hashAlgorithm)
|
||||
throws PGPException {
|
||||
super(SignatureType.SUBKEY_BINDING, signingKey, protector, hashAlgorithm,
|
||||
SignatureSubpackets.createHashedSubpackets(signingKey.getPublicKey()),
|
||||
SignatureSubpackets.createEmptySubpackets());
|
||||
}
|
||||
|
||||
public SubkeyBindingSignatureBuilder(
|
||||
PGPSecretKey signingKey,
|
||||
SecretKeyRingProtector protector,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue