Skip to content

Commit 94b98de

Browse files
ligefeiBouncycastledghgit
authored andcommitted
Openpgp openpgpkeyconstructor
1 parent 84ba827 commit 94b98de

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPKey.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.Collection;
57
import java.util.Date;
68
import java.util.Iterator;
79
import java.util.LinkedHashMap;
@@ -61,6 +63,27 @@ public OpenPGPKey(PGPSecretKeyRing keyRing, OpenPGPImplementation implementation
6163
this(keyRing, implementation, implementation.policy());
6264
}
6365

66+
public OpenPGPKey(Collection<OpenPGPSecretKey> secretKeys, OpenPGPImplementation implementation)
67+
{
68+
this(secretKeys, implementation, implementation.policy());
69+
}
70+
71+
public OpenPGPKey(Collection<OpenPGPSecretKey> secretKeys, OpenPGPImplementation implementation, OpenPGPPolicy policy)
72+
{
73+
this(fromSecretKeys(secretKeys), implementation, policy);
74+
}
75+
76+
private static PGPSecretKeyRing fromSecretKeys(Collection<OpenPGPSecretKey> secretKeys)
77+
{
78+
List pgpSecretKeys = new ArrayList();
79+
for (Iterator it = secretKeys.iterator(); it.hasNext(); )
80+
{
81+
OpenPGPSecretKey secretKey = (OpenPGPSecretKey)it.next();
82+
pgpSecretKeys.add(secretKey.getPGPSecretKey());
83+
}
84+
return new PGPSecretKeyRing(pgpSecretKeys);
85+
}
86+
6487
/**
6588
* Create an {@link OpenPGPKey} instance based on a {@link PGPSecretKeyRing},
6689
* a provided {@link OpenPGPImplementation} and {@link OpenPGPPolicy}.

pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPKeyEditor.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ public OpenPGPKeyEditor(OpenPGPKey key,
4949
throws PGPException
5050
{
5151
this.key = key;
52-
this.primaryKey = key.getPrimarySecretKey().unlock(passphraseProvider);
52+
OpenPGPKey.OpenPGPSecretKey primarySecretKey = key.getPrimarySecretKey();
53+
if (primarySecretKey.isLocked())
54+
{
55+
this.primaryKey = primarySecretKey.unlock(passphraseProvider);
56+
}
57+
else
58+
{
59+
this.primaryKey = primarySecretKey.unlock();
60+
}
5361
this.implementation = implementation;
5462
this.policy = policy;
5563
}
@@ -429,6 +437,37 @@ public OpenPGPKeyEditor changePassphrase(KeyIdentifier componentKeyIdentifier,
429437
return this;
430438
}
431439

440+
/**
441+
* Change the passphrase of the given component key.
442+
*
443+
* @param componentKeyIdentifier identifier of the component key, whose passphrase shall be changed
444+
* @param passphraseProvider provider for the old key passphrase
445+
* @param newPassphrase new passphrase (or null)
446+
* @param useAEAD whether to use AEAD
447+
* @return this
448+
* @throws OpenPGPKeyException if the secret component of the component key is missing
449+
* @throws PGPException if the key passphrase cannot be changed
450+
*/
451+
public OpenPGPKeyEditor changePassphrase(KeyIdentifier componentKeyIdentifier,
452+
KeyPassphraseProvider passphraseProvider,
453+
char[] newPassphrase,
454+
boolean useAEAD)
455+
throws OpenPGPKeyException, PGPException
456+
{
457+
OpenPGPKey.OpenPGPSecretKey secretKey = key.getSecretKey(componentKeyIdentifier);
458+
if (secretKey == null)
459+
{
460+
throw new OpenPGPKeyException(key, "Secret component key " + componentKeyIdentifier +
461+
" is missing from the key.");
462+
}
463+
464+
return changePassphrase(
465+
componentKeyIdentifier,
466+
passphraseProvider.getKeyPassword(secretKey),
467+
newPassphrase,
468+
useAEAD);
469+
}
470+
432471
/**
433472
* Return the modified {@link OpenPGPKey}.
434473
*

pg/src/test/java/org/bouncycastle/openpgp/api/test/OpenPGPKeyEditorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ private void changePassphraseUnprotectedToCFBTest(OpenPGPApi api)
248248
isFalse(key.getPrimarySecretKey().isLocked());
249249

250250
key = api.editKey(key)
251-
.changePassphrase(key.getPrimaryKey().getKeyIdentifier(), null, "sw0rdf1sh".toCharArray(), false)
251+
.changePassphrase(key.getPrimaryKey().getKeyIdentifier(), (char[]) null, "sw0rdf1sh".toCharArray(), false)
252252
.done();
253253
isTrue("Expect key to be locked", key.getPrimarySecretKey().isLocked());
254254
isTrue("Expect sw0rdf1sh to be the correct passphrase",
@@ -264,7 +264,7 @@ private void changePassphraseUnprotectedToAEADTest(OpenPGPApi api)
264264
isFalse("Expect key to be unprotected", key.getPrimarySecretKey().isLocked());
265265

266266
key = api.editKey(key)
267-
.changePassphrase(key.getPrimaryKey().getKeyIdentifier(), null, "sw0rdf1sh".toCharArray(), true)
267+
.changePassphrase(key.getPrimaryKey().getKeyIdentifier(), (char[]) null, "sw0rdf1sh".toCharArray(), true)
268268
.done();
269269
isTrue("Expect key to be locked after changing passphrase",
270270
key.getPrimarySecretKey().isLocked());

0 commit comments

Comments
 (0)