Skip to content

Commit bc89863

Browse files
committed
Merge branch '2105-pgpainless-v6' into 'main'
Pual's thread for PGPainless new version release See merge request root/bc-java!110
2 parents 03dbe65 + 4877c9e commit bc89863

16 files changed

+310
-86
lines changed

pg/src/main/java/org/bouncycastle/bcpg/PublicKeyUtils.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,26 @@ public static boolean isSigningAlgorithm(int publicKeyAlgorithm)
3030
}
3131
}
3232

33-
// /**
34-
// * Return true, if the public key algorithm that corresponds to the given ID is capable of encryption.
35-
// * @param publicKeyAlgorithm public key algorithm id
36-
// * @return true if algorithm can encrypt
37-
// */
38-
// public static boolean isEncryptionAlgorithm(int publicKeyAlgorithm)
39-
// {
40-
// switch (publicKeyAlgorithm)
41-
// {
42-
// case PublicKeyAlgorithmTags.RSA_GENERAL:
43-
// case PublicKeyAlgorithmTags.RSA_ENCRYPT:
44-
// case PublicKeyAlgorithmTags.ELGAMAL_ENCRYPT:
45-
// case PublicKeyAlgorithmTags.ECDH:
46-
// case PublicKeyAlgorithmTags.ELGAMAL_GENERAL:
47-
// case PublicKeyAlgorithmTags.DIFFIE_HELLMAN:
48-
// case PublicKeyAlgorithmTags.X25519:
49-
// case PublicKeyAlgorithmTags.X448:
50-
// return true;
51-
// default:
52-
// return false;
53-
// }
54-
// }
33+
/**
34+
* Return true, if the public key algorithm that corresponds to the given ID is capable of encryption.
35+
* @param publicKeyAlgorithm public key algorithm id
36+
* @return true if algorithm can encrypt
37+
*/
38+
public static boolean isEncryptionAlgorithm(int publicKeyAlgorithm)
39+
{
40+
switch (publicKeyAlgorithm)
41+
{
42+
case PublicKeyAlgorithmTags.RSA_GENERAL:
43+
case PublicKeyAlgorithmTags.RSA_ENCRYPT:
44+
case PublicKeyAlgorithmTags.ELGAMAL_ENCRYPT:
45+
case PublicKeyAlgorithmTags.ECDH:
46+
case PublicKeyAlgorithmTags.ELGAMAL_GENERAL:
47+
case PublicKeyAlgorithmTags.DIFFIE_HELLMAN:
48+
case PublicKeyAlgorithmTags.X25519:
49+
case PublicKeyAlgorithmTags.X448:
50+
return true;
51+
default:
52+
return false;
53+
}
54+
}
5555
}

pg/src/main/java/org/bouncycastle/bcpg/SignaturePacket.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bouncycastle.bcpg.sig.IssuerFingerprint;
99
import org.bouncycastle.bcpg.sig.IssuerKeyID;
1010
import org.bouncycastle.bcpg.sig.SignatureCreationTime;
11+
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
1112
import org.bouncycastle.util.Arrays;
1213
import org.bouncycastle.util.Pack;
1314
import org.bouncycastle.util.io.Streams;
@@ -446,6 +447,40 @@ public SignaturePacket(
446447
}
447448
}
448449

450+
public static SignaturePacket copyOfWith(SignaturePacket packet, SignatureSubpacket[] unhashedSubpackets)
451+
{
452+
if (packet.getVersion() == SignaturePacket.VERSION_6)
453+
{
454+
return new SignaturePacket(
455+
packet.getVersion(),
456+
packet.getSignatureType(),
457+
packet.getKeyID(),
458+
packet.getKeyAlgorithm(),
459+
packet.getHashAlgorithm(),
460+
packet.getHashedSubPackets(),
461+
unhashedSubpackets,
462+
packet.getFingerPrint(),
463+
packet.getSignatureBytes(),
464+
packet.getSalt()
465+
);
466+
}
467+
else
468+
{
469+
return new SignaturePacket(
470+
packet.getVersion(),
471+
packet.hasNewPacketFormat(),
472+
packet.getSignatureType(),
473+
packet.getKeyID(),
474+
packet.getKeyAlgorithm(),
475+
packet.getHashAlgorithm(),
476+
packet.getHashedSubPackets(),
477+
unhashedSubpackets,
478+
packet.getFingerPrint(),
479+
packet.getSignature()
480+
);
481+
}
482+
}
483+
449484
/**
450485
* get the version number
451486
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ public PGPEncryptedData get(
154154
return (PGPEncryptedData)methods.get(index);
155155
}
156156

157+
public InputStreamPacket getEncryptedData()
158+
{
159+
return data;
160+
}
161+
157162
/**
158163
* Gets the number of encryption methods in this list.
159164
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ static void readUserIDs(
9393
}
9494
}
9595

96+
/**
97+
* Return the {@link KeyIdentifier} of this key rings primary key.
98+
*
99+
* @return primary key identifier
100+
*/
101+
public KeyIdentifier getKeyIdentifier()
102+
{
103+
return getPublicKey().getKeyIdentifier();
104+
}
105+
96106
/**
97107
* Return the first public key in the ring. In the case of a {@link PGPSecretKeyRing}
98108
* this is also the public key of the master key pair.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
import org.bouncycastle.bcpg.BCPGInputStream;
1212
import org.bouncycastle.bcpg.PacketTags;
13+
import org.bouncycastle.bcpg.TrustPacket;
1314
import org.bouncycastle.bcpg.UnknownPacket;
1415
import org.bouncycastle.bcpg.UnsupportedPacketVersionException;
1516
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
16-
import org.bouncycastle.util.Iterable;
1717

1818
/**
1919
* General class for reading a PGP object stream.
@@ -141,6 +141,8 @@ public Object nextObject()
141141
return new PGPCompressedData(in);
142142
case PacketTags.LITERAL_DATA:
143143
return new PGPLiteralData(in);
144+
case PacketTags.TRUST:
145+
return new PGPTrust(in);
144146
case PacketTags.PUBLIC_KEY_ENC_SESSION:
145147
case PacketTags.SYMMETRIC_KEY_ENC_SESSION:
146148
case PacketTags.SYMMETRIC_KEY_ENC:

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,14 @@ public Iterator<String> getUserIDs()
487487
{
488488
if (ids.get(i) instanceof UserIDPacket)
489489
{
490-
temp.add(((UserIDPacket)ids.get(i)).getID());
490+
try
491+
{
492+
temp.add(((UserIDPacket) ids.get(i)).getID());
493+
}
494+
catch (IllegalArgumentException e)
495+
{
496+
// Skip non-UTF8 user-ids
497+
}
491498
}
492499
}
493500

@@ -1157,7 +1164,7 @@ public static PGPPublicKey join(
11571164
}
11581165

11591166
// key signatures
1160-
joinPgpSignatureList(copy.keySigs, keySigs, true, true);
1167+
joinPgpSignatureList(copy.keySigs, keySigs, false, true);
11611168

11621169
// user-ids and id sigs
11631170
for (int idIdx = 0; idIdx < copy.ids.size(); idIdx++)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public PGPSecretKey(
293293
//
294294
// generate the certification
295295
//
296-
PGPSignatureGenerator sGen = new PGPSignatureGenerator(certificationSignerBuilder);
296+
PGPSignatureGenerator sGen = new PGPSignatureGenerator(certificationSignerBuilder, masterKeyPair.getPublicKey());
297297

298298
sGen.init(PGPSignature.SUBKEY_BINDING, masterKeyPair.getPrivateKey());
299299

@@ -302,7 +302,7 @@ public PGPSecretKey(
302302
{
303303
if (hashedPcks == null)
304304
{
305-
PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(certificationSignerBuilder);
305+
PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(certificationSignerBuilder, keyPair.getPublicKey());
306306

307307
signatureGenerator.init(PGPSignature.PRIMARYKEY_BINDING, keyPair.getPrivateKey());
308308

@@ -382,7 +382,7 @@ private static PGPPublicKey certifiedPublicKey(
382382

383383
try
384384
{
385-
sGen = new PGPSignatureGenerator(certificationSignerBuilder);
385+
sGen = new PGPSignatureGenerator(certificationSignerBuilder, keyPair.getPublicKey());
386386
}
387387
catch (Exception e)
388388
{

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,18 +1033,7 @@ public static PGPSignature join(PGPSignature sig1, PGPSignature sig2)
10331033

10341034
SignatureSubpacket[] unhashed = (SignatureSubpacket[])merged.toArray(new SignatureSubpacket[0]);
10351035
return new PGPSignature(
1036-
new SignaturePacket(
1037-
sig1.getVersion(),
1038-
sig1.sigPck.hasNewPacketFormat(),
1039-
sig1.getSignatureType(),
1040-
sig1.getKeyID(),
1041-
sig1.getKeyAlgorithm(),
1042-
sig1.getHashAlgorithm(),
1043-
sig1.getHashedSubPackets().packets,
1044-
unhashed,
1045-
sig1.getDigestPrefix(),
1046-
sig1.sigPck.getSignature()
1047-
)
1036+
SignaturePacket.copyOfWith(sig1.sigPck, unhashed)
10481037
);
10491038
}
10501039
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.bouncycastle.openpgp;
2+
3+
import org.bouncycastle.bcpg.BCPGInputStream;
4+
import org.bouncycastle.bcpg.TrustPacket;
5+
6+
import java.io.IOException;
7+
import java.util.Arrays;
8+
9+
public class PGPTrust
10+
{
11+
12+
private final TrustPacket packet;
13+
14+
public PGPTrust(TrustPacket packet)
15+
{
16+
this.packet = packet;
17+
}
18+
19+
public PGPTrust(BCPGInputStream inputStream)
20+
throws IOException
21+
{
22+
this((TrustPacket) inputStream.readPacket());
23+
}
24+
25+
public TrustPacket getPacket()
26+
{
27+
return packet;
28+
}
29+
30+
public byte[] getLevelAndTrust()
31+
{
32+
return Arrays.copyOf(
33+
packet.getLevelAndTrustAmount(),
34+
packet.getLevelAndTrustAmount().length);
35+
}
36+
}

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package org.bouncycastle.openpgp.api;
22

3+
import org.bouncycastle.bcpg.AEADEncDataPacket;
4+
import org.bouncycastle.bcpg.InputStreamPacket;
5+
import org.bouncycastle.bcpg.SymmetricEncDataPacket;
6+
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
7+
import org.bouncycastle.bcpg.UnsupportedPacketVersionException;
8+
import org.bouncycastle.openpgp.PGPEncryptedDataList;
9+
import org.bouncycastle.openpgp.PGPException;
10+
311
/**
412
* Encryption Mode.
513
*/
@@ -34,4 +42,58 @@ public enum EncryptedDataPacketType
3442
* Support for this feature is signalled using {@link org.bouncycastle.bcpg.sig.Features#FEATURE_AEAD_ENCRYPTED_DATA}.
3543
*/
3644
LIBREPGP_OED // "v5"
45+
;
46+
47+
/**
48+
* Detect the type of the PGPEncryptedDataList's encrypted data packet.
49+
*
50+
* @param encDataList encrypted data list
51+
* @return encrypted data packet type
52+
* @throws PGPException if an unexpected data packet is encountered.
53+
*/
54+
public static EncryptedDataPacketType of(PGPEncryptedDataList encDataList)
55+
throws PGPException
56+
{
57+
return of(encDataList.getEncryptedData());
58+
}
59+
60+
/**
61+
* Detect the type the provided encrypted data packet.
62+
*
63+
* @param encData encrypted data packet
64+
* @return encrypted data packet type
65+
* @throws PGPException if an unexpected data packet is encountered.
66+
*/
67+
public static EncryptedDataPacketType of(InputStreamPacket encData)
68+
throws PGPException
69+
{
70+
if (encData instanceof SymmetricEncIntegrityPacket)
71+
{
72+
SymmetricEncIntegrityPacket seipd = (SymmetricEncIntegrityPacket) encData;
73+
if (seipd.getVersion() == SymmetricEncIntegrityPacket.VERSION_1)
74+
{
75+
return SEIPDv1;
76+
}
77+
else if (seipd.getVersion() == SymmetricEncIntegrityPacket.VERSION_2)
78+
{
79+
return SEIPDv2;
80+
}
81+
else
82+
{
83+
throw new UnsupportedPacketVersionException("Symmetrically-Encrypted Integrity-Protected Data Packet of unknown version encountered: " + seipd.getVersion());
84+
}
85+
}
86+
else if (encData instanceof AEADEncDataPacket)
87+
{
88+
return LIBREPGP_OED;
89+
}
90+
else if (encData instanceof SymmetricEncDataPacket)
91+
{
92+
return SED;
93+
}
94+
else
95+
{
96+
throw new PGPException("Unexpected packet type: " + encData.getClass().getName());
97+
}
98+
}
3799
}

0 commit comments

Comments
 (0)