|
1 | 1 | using System; |
2 | 2 | using System.IO; |
3 | 3 | using System.Text; |
| 4 | +using System.Threading; |
4 | 5 |
|
5 | 6 | using NUnit.Framework; |
6 | 7 |
|
7 | 8 | using Org.BouncyCastle.Bcpg.Attr; |
| 9 | +using Org.BouncyCastle.Bcpg.Sig; |
8 | 10 | using Org.BouncyCastle.Crypto; |
9 | 11 | using Org.BouncyCastle.Crypto.Parameters; |
10 | 12 | using Org.BouncyCastle.Math; |
@@ -538,6 +540,64 @@ private void EmbeddedJpegTest() |
538 | 540 | } |
539 | 541 | } |
540 | 542 |
|
| 543 | + private void RemovedExpiryTest() |
| 544 | + { |
| 545 | + SecureRandom random = new SecureRandom(); |
| 546 | + |
| 547 | + // RFC 4880 5.2.4.1: a more recent self-signature without a Key Expiration |
| 548 | + // Time subpacket cancels an earlier self-signature's expiration. |
| 549 | + char[] passPhrase = "test".ToCharArray(); |
| 550 | + string identity = "TEST <test@test.org>"; |
| 551 | + DateTime date = DateTime.UtcNow; |
| 552 | + |
| 553 | + var kpg = GeneratorUtilities.GetKeyPairGenerator("RSA"); |
| 554 | + kpg.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(0x10001), random, 1024, 25)); |
| 555 | + |
| 556 | + var kpSgn = kpg.GenerateKeyPair(); |
| 557 | + |
| 558 | + PgpKeyPair sgnKeyPair = new PgpKeyPair(PublicKeyAlgorithmTag.RsaSign, kpSgn, date); |
| 559 | + |
| 560 | + PgpSignatureSubpacketGenerator svg = new PgpSignatureSubpacketGenerator(); |
| 561 | + svg.SetKeyExpirationTime(isCritical: true, 86400L * 366 * 2); |
| 562 | + svg.SetKeyFlags(isCritical: true, KeyFlags.CertifyOther | KeyFlags.SignData); |
| 563 | + PgpSignatureSubpacketVector hashedPcks = svg.Generate(); |
| 564 | + |
| 565 | + PgpKeyRingGenerator keyRingGen = new PgpKeyRingGenerator(PgpSignature.PositiveCertification, sgnKeyPair, |
| 566 | + identity, SymmetricKeyAlgorithmTag.Aes256, passPhrase, useSha1: true, hashedPcks, null, random); |
| 567 | + |
| 568 | + PgpPublicKeyRing keyRing = keyRingGen.GeneratePublicKeyRing(); |
| 569 | + |
| 570 | + // Encode/decode |
| 571 | + keyRing = new PgpPublicKeyRing(keyRing.GetEncoded()); |
| 572 | + |
| 573 | + PgpPublicKey pKey = keyRing.GetPublicKey(); |
| 574 | + |
| 575 | + if (pKey.GetValidSeconds() != 86400L * 366 * 2) |
| 576 | + { |
| 577 | + Fail("initial key expiration time wrong"); |
| 578 | + } |
| 579 | + |
| 580 | + // Add a newer self-cert that omits KeyExpireTime (intent: remove the expiry). |
| 581 | + Thread.Sleep(millisecondsTimeout: 1100); // ensure later creation time at one-second granularity |
| 582 | + |
| 583 | + // TODO[pgp] Add constructor that accepts also sgnKeyPair.PublicKey |
| 584 | + PgpSignatureGenerator keySigGen = new PgpSignatureGenerator(PublicKeyAlgorithmTag.RsaGeneral, |
| 585 | + HashAlgorithmTag.Sha1); |
| 586 | + keySigGen.InitSign(PgpSignature.PositiveCertification, sgnKeyPair.PrivateKey); |
| 587 | + |
| 588 | + PgpSignatureSubpacketGenerator noExpiry = new PgpSignatureSubpacketGenerator(); |
| 589 | + noExpiry.SetKeyFlags(isCritical: true, KeyFlags.CertifyOther | KeyFlags.SignData); |
| 590 | + keySigGen.SetHashedSubpackets(noExpiry.Generate()); |
| 591 | + |
| 592 | + pKey = PgpPublicKey.AddCertification(pKey, keySigGen.GenerateCertification(identity, pKey)); |
| 593 | + |
| 594 | + if (pKey.GetValidSeconds() != 0) |
| 595 | + { |
| 596 | + Fail("expected getValidSeconds() == 0 after newer self-sig without KEY_EXPIRE_TIME, got " |
| 597 | + + pKey.GetValidSeconds()); |
| 598 | + } |
| 599 | + } |
| 600 | + |
541 | 601 | public override void PerformTest() |
542 | 602 | { |
543 | 603 | // |
@@ -897,8 +957,7 @@ public override void PerformTest() |
897 | 957 | // |
898 | 958 | // use of PgpKeyPair |
899 | 959 | // |
900 | | - PgpKeyPair pgpKp = new PgpKeyPair(PublicKeyAlgorithmTag.RsaGeneral, |
901 | | - kp.Public, kp.Private, DateTime.UtcNow); |
| 960 | + PgpKeyPair pgpKp = new PgpKeyPair(PublicKeyAlgorithmTag.RsaGeneral, kp, DateTime.UtcNow); |
902 | 961 |
|
903 | 962 | PgpPublicKey k1 = pgpKp.PublicKey; |
904 | 963 | PgpPrivateKey k2 = pgpKp.PrivateKey; |
@@ -1125,6 +1184,7 @@ public override void PerformTest() |
1125 | 1184 | FingerPrintTest(); |
1126 | 1185 | ExistingEmbeddedJpegTest(); |
1127 | 1186 | EmbeddedJpegTest(); |
| 1187 | + RemovedExpiryTest(); |
1128 | 1188 | } |
1129 | 1189 |
|
1130 | 1190 | private void PerformTestSig( |
|
0 commit comments