mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-26 21:07:58 +01:00
Refactoring: move KeyRingEditor to SecretKeyRingEditor in prep for more editor classes
This commit is contained in:
parent
9f95e7925b
commit
5cdbb125b0
5 changed files with 73 additions and 53 deletions
|
@ -27,8 +27,8 @@ import org.pgpainless.decryption_verification.DecryptionStream;
|
|||
import org.pgpainless.encryption_signing.EncryptionBuilder;
|
||||
import org.pgpainless.encryption_signing.EncryptionStream;
|
||||
import org.pgpainless.key.generation.KeyRingBuilder;
|
||||
import org.pgpainless.key.modification.KeyRingEditor;
|
||||
import org.pgpainless.key.modification.KeyRingEditorInterface;
|
||||
import org.pgpainless.key.modification.secretkeyring.SecretKeyRingEditor;
|
||||
import org.pgpainless.key.modification.secretkeyring.SecretKeyRingEditorInterface;
|
||||
import org.pgpainless.key.parsing.KeyRingReader;
|
||||
import org.pgpainless.symmetric_encryption.SymmetricEncryptorDecryptor;
|
||||
import org.pgpainless.util.Passphrase;
|
||||
|
@ -67,8 +67,8 @@ public class PGPainless {
|
|||
return new DecryptionBuilder();
|
||||
}
|
||||
|
||||
public static KeyRingEditorInterface modifyKeyRing(PGPSecretKeyRing secretKeys) {
|
||||
return new KeyRingEditor(secretKeys);
|
||||
public static SecretKeyRingEditorInterface modifyKeyRing(PGPSecretKeyRing secretKeys) {
|
||||
return new SecretKeyRingEditor(secretKeys);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pgpainless.key.modification;
|
||||
package org.pgpainless.key.modification.secretkeyring;
|
||||
|
||||
import static org.pgpainless.key.util.KeyUtils.unlockSecretKey;
|
||||
|
||||
|
@ -61,7 +61,7 @@ import org.pgpainless.key.util.KeyRingUtils;
|
|||
import org.pgpainless.key.util.SignatureUtils;
|
||||
import org.pgpainless.util.Passphrase;
|
||||
|
||||
public class KeyRingEditor implements KeyRingEditorInterface {
|
||||
public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
|
||||
|
||||
// Default algorithm for calculating private key checksums
|
||||
// While I'd like to use something else, eg. SHA256, BC seems to lack support for
|
||||
|
@ -70,7 +70,7 @@ public class KeyRingEditor implements KeyRingEditorInterface {
|
|||
|
||||
private PGPSecretKeyRing secretKeyRing;
|
||||
|
||||
public KeyRingEditor(PGPSecretKeyRing secretKeyRing) {
|
||||
public SecretKeyRingEditor(PGPSecretKeyRing secretKeyRing) {
|
||||
if (secretKeyRing == null) {
|
||||
throw new NullPointerException("SecretKeyRing MUST NOT be null.");
|
||||
}
|
||||
|
@ -78,12 +78,12 @@ public class KeyRingEditor implements KeyRingEditorInterface {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface addUserId(String userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
public SecretKeyRingEditorInterface addUserId(String userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
return addUserId(secretKeyRing.getPublicKey().getKeyID(), userId, secretKeyRingProtector);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface addUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
public SecretKeyRingEditorInterface addUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
userId = sanitizeUserId(userId);
|
||||
|
||||
List<PGPSecretKey> secretKeyList = new ArrayList<>();
|
||||
|
@ -135,13 +135,13 @@ public class KeyRingEditor implements KeyRingEditorInterface {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector protector) {
|
||||
public SecretKeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector protector) {
|
||||
PGPPublicKey publicKey = secretKeyRing.getPublicKey();
|
||||
return deleteUserId(publicKey.getKeyID(), userId, protector);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface deleteUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector) {
|
||||
public SecretKeyRingEditorInterface deleteUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector) {
|
||||
List<PGPPublicKey> publicKeys = new ArrayList<>();
|
||||
Iterator<PGPPublicKey> publicKeyIterator = secretKeyRing.getPublicKeys();
|
||||
boolean foundKey = false;
|
||||
|
@ -175,9 +175,9 @@ public class KeyRingEditor implements KeyRingEditorInterface {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface addSubKey(@Nonnull KeySpec keySpec,
|
||||
@Nonnull Passphrase subKeyPassphrase,
|
||||
SecretKeyRingProtector secretKeyRingProtector)
|
||||
public SecretKeyRingEditorInterface addSubKey(@Nonnull KeySpec keySpec,
|
||||
@Nonnull Passphrase subKeyPassphrase,
|
||||
SecretKeyRingProtector secretKeyRingProtector)
|
||||
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
|
||||
|
||||
PGPSecretKey secretSubKey = generateSubKey(keySpec, subKeyPassphrase);
|
||||
|
@ -188,9 +188,9 @@ public class KeyRingEditor implements KeyRingEditorInterface {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface addSubKey(PGPSecretKey secretSubKey,
|
||||
SecretKeyRingProtector subKeyProtector,
|
||||
SecretKeyRingProtector keyRingProtector)
|
||||
public SecretKeyRingEditorInterface addSubKey(PGPSecretKey secretSubKey,
|
||||
SecretKeyRingProtector subKeyProtector,
|
||||
SecretKeyRingProtector keyRingProtector)
|
||||
throws PGPException {
|
||||
|
||||
PGPPublicKey primaryKey = secretKeyRing.getSecretKey().getPublicKey();
|
||||
|
@ -232,14 +232,14 @@ public class KeyRingEditor implements KeyRingEditorInterface {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface deleteSubKey(OpenPgpV4Fingerprint fingerprint,
|
||||
SecretKeyRingProtector protector) {
|
||||
public SecretKeyRingEditorInterface deleteSubKey(OpenPgpV4Fingerprint fingerprint,
|
||||
SecretKeyRingProtector protector) {
|
||||
return deleteSubKey(fingerprint.getKeyId(), protector);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface deleteSubKey(long subKeyId,
|
||||
SecretKeyRingProtector protector) {
|
||||
public SecretKeyRingEditorInterface deleteSubKey(long subKeyId,
|
||||
SecretKeyRingProtector protector) {
|
||||
if (secretKeyRing.getSecretKey().getKeyID() == subKeyId) {
|
||||
throw new IllegalArgumentException("You cannot delete the primary key of this key ring.");
|
||||
}
|
||||
|
@ -255,13 +255,13 @@ public class KeyRingEditor implements KeyRingEditorInterface {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface revokeSubKey(OpenPgpV4Fingerprint fingerprint, SecretKeyRingProtector protector)
|
||||
public SecretKeyRingEditorInterface revokeSubKey(OpenPgpV4Fingerprint fingerprint, SecretKeyRingProtector protector)
|
||||
throws PGPException {
|
||||
return revokeSubKey(fingerprint.getKeyId(), protector);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface revokeSubKey(long subKeyId, SecretKeyRingProtector protector) throws PGPException {
|
||||
public SecretKeyRingEditorInterface revokeSubKey(long subKeyId, SecretKeyRingProtector protector) throws PGPException {
|
||||
PGPPublicKey revokeeSubKey = secretKeyRing.getPublicKey(subKeyId);
|
||||
if (revokeeSubKey == null) {
|
||||
throw new NoSuchElementException("No subkey with id " + Long.toHexString(subKeyId) + " found.");
|
||||
|
@ -356,24 +356,24 @@ public class KeyRingEditor implements KeyRingEditorInterface {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface toNewPassphrase(Passphrase passphrase) throws PGPException {
|
||||
public SecretKeyRingEditorInterface toNewPassphrase(Passphrase passphrase) throws PGPException {
|
||||
SecretKeyRingProtector newProtector = new PasswordBasedSecretKeyRingProtector(
|
||||
newProtectionSettings, new SolitaryPassphraseProvider(passphrase));
|
||||
|
||||
PGPSecretKeyRing secretKeys = changePassphrase(keyId, KeyRingEditor.this.secretKeyRing, oldProtector, newProtector);
|
||||
KeyRingEditor.this.secretKeyRing = secretKeys;
|
||||
PGPSecretKeyRing secretKeys = changePassphrase(keyId, SecretKeyRingEditor.this.secretKeyRing, oldProtector, newProtector);
|
||||
SecretKeyRingEditor.this.secretKeyRing = secretKeys;
|
||||
|
||||
return KeyRingEditor.this;
|
||||
return SecretKeyRingEditor.this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyRingEditorInterface toNoPassphrase() throws PGPException {
|
||||
public SecretKeyRingEditorInterface toNoPassphrase() throws PGPException {
|
||||
SecretKeyRingProtector newProtector = new UnprotectedKeysProtector();
|
||||
|
||||
PGPSecretKeyRing secretKeys = changePassphrase(keyId, KeyRingEditor.this.secretKeyRing, oldProtector, newProtector);
|
||||
KeyRingEditor.this.secretKeyRing = secretKeys;
|
||||
PGPSecretKeyRing secretKeys = changePassphrase(keyId, SecretKeyRingEditor.this.secretKeyRing, oldProtector, newProtector);
|
||||
SecretKeyRingEditor.this.secretKeyRing = secretKeys;
|
||||
|
||||
return KeyRingEditor.this;
|
||||
return SecretKeyRingEditor.this;
|
||||
}
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pgpainless.key.modification;
|
||||
package org.pgpainless.key.modification.secretkeyring;
|
||||
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
@ -30,9 +30,9 @@ import org.pgpainless.key.protection.SecretKeyRingProtector;
|
|||
import org.pgpainless.key.util.UserId;
|
||||
import org.pgpainless.util.Passphrase;
|
||||
|
||||
public interface KeyRingEditorInterface {
|
||||
public interface SecretKeyRingEditorInterface {
|
||||
|
||||
default KeyRingEditorInterface addUserId(UserId userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
default SecretKeyRingEditorInterface addUserId(UserId userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
return addUserId(userId.toString(), secretKeyRingProtector);
|
||||
}
|
||||
|
||||
|
@ -42,21 +42,21 @@ public interface KeyRingEditorInterface {
|
|||
* @param userId user-id
|
||||
* @return the builder
|
||||
*/
|
||||
KeyRingEditorInterface addUserId(String userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException;
|
||||
SecretKeyRingEditorInterface addUserId(String userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException;
|
||||
|
||||
default KeyRingEditorInterface addUserId(OpenPgpV4Fingerprint fingerprint, UserId userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
default SecretKeyRingEditorInterface addUserId(OpenPgpV4Fingerprint fingerprint, UserId userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
return addUserId(fingerprint, userId.toString(), secretKeyRingProtector);
|
||||
}
|
||||
|
||||
default KeyRingEditorInterface addUserId(OpenPgpV4Fingerprint fingerprint, String userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
default SecretKeyRingEditorInterface addUserId(OpenPgpV4Fingerprint fingerprint, String userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
return addUserId(fingerprint.getKeyId(), userId, secretKeyRingProtector);
|
||||
}
|
||||
|
||||
default KeyRingEditorInterface addUserId(long keyId, UserId userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
default SecretKeyRingEditorInterface addUserId(long keyId, UserId userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException {
|
||||
return addUserId(keyId, userId.toString(), secretKeyRingProtector);
|
||||
}
|
||||
|
||||
KeyRingEditorInterface addUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException;
|
||||
SecretKeyRingEditorInterface addUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException;
|
||||
|
||||
/**
|
||||
* Remove a user-id from the primary key of the key ring.
|
||||
|
@ -64,13 +64,13 @@ public interface KeyRingEditorInterface {
|
|||
* @param userId exact user-id to be removed
|
||||
* @return the builder
|
||||
*/
|
||||
KeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector secretKeyRingProtector);
|
||||
SecretKeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector secretKeyRingProtector);
|
||||
|
||||
default KeyRingEditorInterface deleteUserId(OpenPgpV4Fingerprint fingerprint, String userId, SecretKeyRingProtector secretKeyRingProtector) {
|
||||
default SecretKeyRingEditorInterface deleteUserId(OpenPgpV4Fingerprint fingerprint, String userId, SecretKeyRingProtector secretKeyRingProtector) {
|
||||
return deleteUserId(fingerprint.getKeyId(), userId, secretKeyRingProtector);
|
||||
}
|
||||
|
||||
KeyRingEditorInterface deleteUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector);
|
||||
SecretKeyRingEditorInterface deleteUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector);
|
||||
|
||||
/**
|
||||
* Add a subkey to the key ring.
|
||||
|
@ -79,12 +79,12 @@ public interface KeyRingEditorInterface {
|
|||
* @param keySpec key specification
|
||||
* @return the builder
|
||||
*/
|
||||
KeyRingEditorInterface addSubKey(@Nonnull KeySpec keySpec,
|
||||
@Nonnull Passphrase subKeyPassphrase,
|
||||
SecretKeyRingProtector secretKeyRingProtector)
|
||||
SecretKeyRingEditorInterface addSubKey(@Nonnull KeySpec keySpec,
|
||||
@Nonnull Passphrase subKeyPassphrase,
|
||||
SecretKeyRingProtector secretKeyRingProtector)
|
||||
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException;
|
||||
|
||||
KeyRingEditorInterface addSubKey(PGPSecretKey subKey, SecretKeyRingProtector subKeyProtector, SecretKeyRingProtector keyRingProtector)
|
||||
SecretKeyRingEditorInterface addSubKey(PGPSecretKey subKey, SecretKeyRingProtector subKeyProtector, SecretKeyRingProtector keyRingProtector)
|
||||
throws PGPException;
|
||||
|
||||
/**
|
||||
|
@ -95,7 +95,7 @@ public interface KeyRingEditorInterface {
|
|||
* @param fingerprint fingerprint of the subkey to be removed
|
||||
* @return the builder
|
||||
*/
|
||||
KeyRingEditorInterface deleteSubKey(OpenPgpV4Fingerprint fingerprint, SecretKeyRingProtector secretKeyRingProtector);
|
||||
SecretKeyRingEditorInterface deleteSubKey(OpenPgpV4Fingerprint fingerprint, SecretKeyRingProtector secretKeyRingProtector);
|
||||
|
||||
/**
|
||||
* Delete a subkey from the key ring.
|
||||
|
@ -105,7 +105,7 @@ public interface KeyRingEditorInterface {
|
|||
* @param subKeyId id of the subkey
|
||||
* @return the builder
|
||||
*/
|
||||
KeyRingEditorInterface deleteSubKey(long subKeyId, SecretKeyRingProtector secretKeyRingProtector);
|
||||
SecretKeyRingEditorInterface deleteSubKey(long subKeyId, SecretKeyRingProtector secretKeyRingProtector);
|
||||
|
||||
/**
|
||||
* Revoke the subkey binding signature of a subkey.
|
||||
|
@ -115,7 +115,7 @@ public interface KeyRingEditorInterface {
|
|||
* @param fingerprint fingerprint of the subkey to be revoked
|
||||
* @return the builder
|
||||
*/
|
||||
KeyRingEditorInterface revokeSubKey(OpenPgpV4Fingerprint fingerprint, SecretKeyRingProtector secretKeyRingProtector) throws PGPException;
|
||||
SecretKeyRingEditorInterface revokeSubKey(OpenPgpV4Fingerprint fingerprint, SecretKeyRingProtector secretKeyRingProtector) throws PGPException;
|
||||
|
||||
/**
|
||||
* Revoke the subkey binding signature of a subkey.
|
||||
|
@ -125,7 +125,7 @@ public interface KeyRingEditorInterface {
|
|||
* @param subKeyId id of the subkey
|
||||
* @return the builder
|
||||
*/
|
||||
KeyRingEditorInterface revokeSubKey(long subKeyId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException;
|
||||
SecretKeyRingEditorInterface revokeSubKey(long subKeyId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException;
|
||||
|
||||
/**
|
||||
* Change the passphrase of the whole key ring.
|
||||
|
@ -187,14 +187,14 @@ public interface KeyRingEditorInterface {
|
|||
* @param passphrase passphrase
|
||||
* @return editor builder
|
||||
*/
|
||||
KeyRingEditorInterface toNewPassphrase(Passphrase passphrase) throws PGPException;
|
||||
SecretKeyRingEditorInterface toNewPassphrase(Passphrase passphrase) throws PGPException;
|
||||
|
||||
/**
|
||||
* Leave the key unprotected.
|
||||
*
|
||||
* @return editor builder
|
||||
*/
|
||||
KeyRingEditorInterface toNoPassphrase() throws PGPException;
|
||||
SecretKeyRingEditorInterface toNoPassphrase() throws PGPException;
|
||||
}
|
||||
|
||||
/**
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
/**
|
||||
* Classes that deal with modifications made to {@link org.bouncycastle.openpgp.PGPSecretKeyRing PGPSecretKeyRings}.
|
||||
*/
|
||||
package org.pgpainless.key.modification.secretkeyring;
|
|
@ -18,12 +18,13 @@ package org.pgpainless.key.modification;
|
|||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.key.modification.secretkeyring.SecretKeyRingEditor;
|
||||
|
||||
public class KeyRingEditorTest {
|
||||
|
||||
@Test
|
||||
public void testConstructorThrowsNpeForNull() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> new KeyRingEditor(null));
|
||||
() -> new SecretKeyRingEditor(null));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue