Skip to content

Commit 4aa4895

Browse files
author
gefeili
committed
Add OpenPGPV6KeyGeneratorTest
1 parent 6e8f4fd commit 4aa4895

23 files changed

+3069
-30
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.bouncycastle.bcpg;
2+
3+
/**
4+
* Utility methods related to OpenPGP public key algorithms.
5+
*/
6+
public class PublicKeyUtils
7+
{
8+
9+
/**
10+
* Return true, if the public key algorithm that corresponds to the given ID is capable of signing.
11+
*
12+
* @param publicKeyAlgorithm public key algorithm id
13+
* @return true if algorithm can sign
14+
*/
15+
public static boolean isSigningAlgorithm(int publicKeyAlgorithm)
16+
{
17+
switch (publicKeyAlgorithm)
18+
{
19+
case PublicKeyAlgorithmTags.RSA_GENERAL:
20+
case PublicKeyAlgorithmTags.RSA_SIGN:
21+
case PublicKeyAlgorithmTags.DSA:
22+
case PublicKeyAlgorithmTags.ECDSA:
23+
case PublicKeyAlgorithmTags.ELGAMAL_GENERAL:
24+
case PublicKeyAlgorithmTags.EDDSA_LEGACY:
25+
case PublicKeyAlgorithmTags.Ed25519:
26+
case PublicKeyAlgorithmTags.Ed448:
27+
return true;
28+
default:
29+
return false;
30+
}
31+
}
32+
33+
// /**
34+
// * Return true, if the public key algorithm that corresponds to the given ID is capable of encryption.
35+
// *
36+
// * @param publicKeyAlgorithm public key algorithm id
37+
// * @return true if algorithm can encrypt
38+
// */
39+
// public static boolean isEncryptionAlgorithm(int publicKeyAlgorithm)
40+
// {
41+
// switch (publicKeyAlgorithm)
42+
// {
43+
// case PublicKeyAlgorithmTags.RSA_GENERAL:
44+
// case PublicKeyAlgorithmTags.RSA_ENCRYPT:
45+
// case PublicKeyAlgorithmTags.ELGAMAL_ENCRYPT:
46+
// case PublicKeyAlgorithmTags.ECDH:
47+
// case PublicKeyAlgorithmTags.ELGAMAL_GENERAL:
48+
// case PublicKeyAlgorithmTags.DIFFIE_HELLMAN:
49+
// case PublicKeyAlgorithmTags.X25519:
50+
// case PublicKeyAlgorithmTags.X448:
51+
// return true;
52+
// default:
53+
// return false;
54+
// }
55+
// }
56+
}

pg/src/main/java/org/bouncycastle/openpgp/PGPKeyPair.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
package org.bouncycastle.openpgp;
22

33
import org.bouncycastle.bcpg.KeyIdentifier;
4+
import org.bouncycastle.bcpg.PublicSubkeyPacket;
5+
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
46

57
/**
68
* General class to handle JCA key pairs and convert them into OpenPGP ones.
79
* <p>
810
* A word for the unwary, the KeyID for a OpenPGP public key is calculated from
9-
* a hash that includes the time of creation, if you pass a different date to the
11+
* a hash that includes the time of creation, if you pass a different date to the
1012
* constructor below with the same public private key pair the KeyID will not be the
11-
* same as for previous generations of the key, so ideally you only want to do
13+
* same as for previous generations of the key, so ideally you only want to do
1214
* this once.
1315
*/
1416
public class PGPKeyPair
1517
{
16-
protected PGPPublicKey pub;
17-
protected PGPPrivateKey priv;
18+
protected PGPPublicKey pub;
19+
protected PGPPrivateKey priv;
1820

1921
/**
2022
* Create a key pair from a PGPPrivateKey and a PGPPublicKey.
21-
*
22-
* @param pub the public key
23+
*
24+
* @param pub the public key
2325
* @param priv the private key
2426
*/
2527
public PGPKeyPair(
26-
PGPPublicKey pub,
27-
PGPPrivateKey priv)
28+
PGPPublicKey pub,
29+
PGPPrivateKey priv)
2830
{
2931
this.pub = pub;
3032
this.priv = priv;
@@ -36,7 +38,7 @@ protected PGPKeyPair()
3638

3739
/**
3840
* Return the keyID associated with this key pair.
39-
*
41+
*
4042
* @return keyID
4143
*/
4244
public long getKeyID()
@@ -53,14 +55,32 @@ public KeyIdentifier getKeyIdentifier()
5355
{
5456
return getPublicKey().getKeyIdentifier();
5557
}
56-
58+
5759
public PGPPublicKey getPublicKey()
5860
{
5961
return pub;
6062
}
61-
63+
6264
public PGPPrivateKey getPrivateKey()
6365
{
6466
return priv;
6567
}
68+
69+
public PGPKeyPair asSubkey(KeyFingerPrintCalculator fingerPrintCalculator)
70+
throws PGPException
71+
{
72+
if (pub.getPublicKeyPacket() instanceof PublicSubkeyPacket)
73+
{
74+
return this; // is already subkey
75+
}
76+
77+
PublicSubkeyPacket pubSubPkt = new PublicSubkeyPacket(
78+
pub.getVersion(),
79+
pub.getAlgorithm(),
80+
pub.getCreationTime(),
81+
pub.getPublicKeyPacket().getKey());
82+
return new PGPKeyPair(
83+
new PGPPublicKey(pubSubPkt, fingerPrintCalculator),
84+
new PGPPrivateKey(pub.getKeyID(), pubSubPkt, priv.getPrivateKeyDataPacket()));
85+
}
6686
}

pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKeyRing.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,31 @@ public int size()
492492
return keys.size();
493493
}
494494

495+
/**
496+
* Return the OpenPGP certificate (Transferable Public Key) of this key.
497+
*
498+
* @return certificate
499+
*/
500+
public PGPPublicKeyRing toCertificate()
501+
{
502+
List<PGPPublicKey> pubKeys = new ArrayList<PGPPublicKey>();
503+
Iterator<PGPPublicKey> it = getPublicKeys();
504+
while (it.hasNext())
505+
{
506+
pubKeys.add(it.next());
507+
}
508+
return new PGPPublicKeyRing(pubKeys);
509+
}
510+
495511
public byte[] getEncoded()
496512
throws IOException
497513
{
498514
return getEncoded(PacketFormat.ROUNDTRIP);
499515
}
500516

501517
@Override
502-
public byte[] getEncoded(PacketFormat format) throws IOException
518+
public byte[] getEncoded(PacketFormat format)
519+
throws IOException
503520
{
504521
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
505522
BCPGOutputStream pOut = new BCPGOutputStream(bOut, format);

0 commit comments

Comments
 (0)