Skip to content

Commit 70e68f1

Browse files
committed
OpenPGPCertificate: Fix getCertification(date) accidentally returning non-self-sig
1 parent 727e864 commit 70e68f1

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,14 +1227,16 @@ public OpenPGPSignatureChains getSignatureChains()
12271227
}
12281228

12291229
/**
1230-
* Return the (at evaluation time) latest certification signature binding this component.
1230+
* Return the (at evaluation time) latest self certification signature binding this component.
12311231
*
12321232
* @param evaluationTime reference time
12331233
* @return latest component certification signature
12341234
*/
12351235
public OpenPGPComponentSignature getCertification(Date evaluationTime)
12361236
{
1237-
OpenPGPSignatureChain certification = getSignatureChains().getCertificationAt(evaluationTime);
1237+
OpenPGPSignatureChain certification = getSignatureChains()
1238+
.fromOrigin(getCertificate().getPrimaryKey())
1239+
.getCertificationAt(evaluationTime);
12381240
if (certification != null)
12391241
{
12401242
return certification.getSignature();
@@ -1243,14 +1245,16 @@ public OpenPGPComponentSignature getCertification(Date evaluationTime)
12431245
}
12441246

12451247
/**
1246-
* Return the (at evaluation time) latest revocation signature revoking this component.
1248+
* Return the (at evaluation time) latest self revocation signature revoking this component.
12471249
*
12481250
* @param evaluationTime reference time
12491251
* @return latest component revocation signature
12501252
*/
12511253
public OpenPGPComponentSignature getRevocation(Date evaluationTime)
12521254
{
1253-
OpenPGPSignatureChain revocation = getSignatureChains().getRevocationAt(evaluationTime);
1255+
OpenPGPSignatureChain revocation = getSignatureChains()
1256+
.fromOrigin(getCertificate().getPrimaryKey())
1257+
.getRevocationAt(evaluationTime);
12541258
if (revocation != null)
12551259
{
12561260
return revocation.getSignature();
@@ -2113,7 +2117,9 @@ public Date getCreationTime()
21132117
@Override
21142118
public OpenPGPComponentSignature getLatestSelfSignature(Date evaluationTime)
21152119
{
2116-
OpenPGPSignatureChain currentDKChain = getSignatureChains().getChainAt(evaluationTime);
2120+
OpenPGPSignatureChain currentDKChain = getSignatureChains()
2121+
.fromOrigin(getCertificate().getPrimaryKey())
2122+
.getChainAt(evaluationTime);
21172123
if (currentDKChain != null && !currentDKChain.chainLinks.isEmpty())
21182124
{
21192125
return currentDKChain.getSignature();
@@ -2299,7 +2305,9 @@ public OpenPGPComponentSignature getLatestDirectKeySelfSignature()
22992305
*/
23002306
public OpenPGPComponentSignature getLatestDirectKeySelfSignature(Date evaluationTime)
23012307
{
2302-
OpenPGPSignatureChain currentDKChain = getCertificate().getAllSignatureChainsFor(this)
2308+
OpenPGPSignatureChain currentDKChain = getCertificate()
2309+
.getAllSignatureChainsFor(this)
2310+
.fromOrigin(this)
23032311
.getCertificationAt(evaluationTime);
23042312
if (currentDKChain != null && !currentDKChain.chainLinks.isEmpty())
23052313
{
@@ -2327,7 +2335,9 @@ public OpenPGPComponentSignature getLatestKeyRevocationSelfSignature()
23272335
*/
23282336
public OpenPGPComponentSignature getLatestKeyRevocationSelfSignature(Date evaluationTime)
23292337
{
2330-
OpenPGPSignatureChain currentRevocationChain = getCertificate().getAllSignatureChainsFor(this)
2338+
OpenPGPSignatureChain currentRevocationChain = getCertificate()
2339+
.getAllSignatureChainsFor(this)
2340+
.fromOrigin(this)
23312341
.getRevocationAt(evaluationTime);
23322342
if (currentRevocationChain != null && !currentRevocationChain.chainLinks.isEmpty())
23332343
{
@@ -2676,7 +2686,9 @@ public OpenPGPPrimaryKey getPrimaryKey()
26762686
@Override
26772687
public OpenPGPComponentSignature getLatestSelfSignature(Date evaluationTime)
26782688
{
2679-
OpenPGPSignatureChain currentChain = getSignatureChains().getChainAt(evaluationTime);
2689+
OpenPGPSignatureChain currentChain = getSignatureChains()
2690+
.fromOrigin(getPrimaryKey())
2691+
.getChainAt(evaluationTime);
26802692
if (currentChain != null && !currentChain.chainLinks.isEmpty())
26812693
{
26822694
return currentChain.getSignature();

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.bouncycastle.openpgp.PGPException;
1616
import org.bouncycastle.openpgp.PGPObjectFactory;
1717
import org.bouncycastle.openpgp.PGPSignature;
18+
import org.bouncycastle.openpgp.PGPSignatureException;
1819
import org.bouncycastle.openpgp.PGPSignatureList;
1920
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
2021
import org.bouncycastle.openpgp.api.OpenPGPApi;
@@ -49,6 +50,8 @@ protected void performTestWith(OpenPGPApi api)
4950
testSKSignsPKRevokedNoSubpacket(api);
5051
testPKSignsPKRevocationSuperseded(api);
5152
testGetPrimaryUserId(api);
53+
54+
testIgnoreThirdPartySigsForSelfSigs(api);
5255
}
5356

5457
private void testOpenPGPv6Key(OpenPGPApi api)
@@ -879,6 +882,35 @@ public String getMsg()
879882
}
880883
}
881884

885+
private void testIgnoreThirdPartySigsForSelfSigs(OpenPGPApi api)
886+
throws IOException, PGPSignatureException
887+
{
888+
String certWith3rdPartyUIDSig = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
889+
"Comment: 2933 CBF1 9C19 5FEC C3D8 F6BB 7875 DF0D 34D8 0659\n" +
890+
"Comment: Alice\n" +
891+
"\n" +
892+
"mCYEaUKQbBvoc5joeGZFjSjl2LoEuEfTn4dzNPkF68PUTROte/Yn2LQFQWxpY2XC\n" +
893+
"cQQTGwoAHRahBCkzy/GcGV/sw9j2u3h13w002AZZBYJpQpBsAAoJEHh13w002AZZ\n" +
894+
"XM1wwAo+gEchltvtokJUM2alG9z/iCOzBVs7WONrPo5rDJb+RRXXhVz+Mw1lYGWo\n" +
895+
"USe86sZiTnjThA+Ech7JZdoHwnUEEBYKACcFgmlCnn0JEG2VRHjfrsFLFqEE2gJf\n" +
896+
"vjCRGba1de0nbZVEeN+uwUsAAM3RAP0fEo5u5CdRg849xsNYAPv1oHT03el6LyGc\n" +
897+
"Bk44oz7INgD/cFTufapwXJJB5IRX+lJA84w++6Xg0SS9h9TBmQBMiw24JgRpQpBs\n" +
898+
"GyB6+bOfuk3Xaqlv2y9W08EiasmbznRLVaPhlLYTdNzCwsAnBBgbCgCTFqEEKTPL\n" +
899+
"8ZwZX+zD2Pa7eHXfDTTYBlkFgmlCkGwCmwJyoAQZGwoAHRahBB7oLGA9/n/GLv02\n" +
900+
"vM2YyJHfn7e+BYJpQpBsAAoJEM2YyJHfn7e+b0/C2Cv/ujgLxz3TOGi5rTFW7LQ+\n" +
901+
"8vxC25T7ryBmnXaBdZvv0dBvOXy7MpSzRIrgxJQQWpoDNLHFZKosEGYCCUwKAAoJ\n" +
902+
"EHh13w002AZZLI0VnHaOFQRwf+6BCOD/+0d9JhYAOh6nP24pAc0kTeZ7UHZusysk\n" +
903+
"SfhI5KGG2gFUEJlItnagBCsIzxV0GwFoLSwAuCYEaUKQbBnAZbXB6dCd6LT+HeS6\n" +
904+
"1Js5qhp7S+GPhFW4MfGeCBU/F8J0BBgbCgAgFqEEKTPL8ZwZX+zD2Pa7eHXfDTTY\n" +
905+
"BlkFgmlCkGwCmwwACgkQeHXfDTTYBllHw49G2YdupzV1pu1qk4KXgDtsVQumEthi\n" +
906+
"fOXKC8sGfUZASw5bPNFMcWfT/nFrzmuvi01DD+pfUo9a8GoRAZ6qSQ0=\n" +
907+
"=oG2x\n" +
908+
"-----END PGP PUBLIC KEY BLOCK-----";
909+
910+
OpenPGPCertificate cert = api.readKeyOrCertificate().parseCertificate(certWith3rdPartyUIDSig);
911+
cert.getUserId("Alice").getCertification(new Date()).verify(api.getImplementation());
912+
}
913+
882914
public static void main(String[] args)
883915
{
884916
runTest(new OpenPGPCertificateTest());

0 commit comments

Comments
 (0)