|
2 | 2 |
|
3 | 3 | import java.io.ByteArrayInputStream; |
4 | 4 | import java.io.IOException; |
| 5 | +import java.math.BigInteger; |
5 | 6 | import java.security.KeyPair; |
6 | 7 | import java.security.MessageDigest; |
| 8 | +import java.security.SecureRandom; |
7 | 9 | import java.security.Security; |
8 | 10 | import java.security.cert.CertificateException; |
| 11 | +import java.security.cert.CertificateFactory; |
9 | 12 | import java.security.cert.X509Certificate; |
10 | 13 | import java.util.ArrayList; |
11 | 14 | import java.util.Collection; |
| 15 | +import java.util.Date; |
12 | 16 | import java.util.HashSet; |
13 | 17 | import java.util.Iterator; |
14 | 18 | import java.util.List; |
|
27 | 31 | import org.bouncycastle.asn1.cms.SignerInfo; |
28 | 32 | import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; |
29 | 33 | import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
| 34 | +import org.bouncycastle.asn1.x500.X500NameBuilder; |
| 35 | +import org.bouncycastle.asn1.x500.style.RFC4519Style; |
30 | 36 | import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
31 | 37 | import org.bouncycastle.cert.X509CertificateHolder; |
| 38 | +import org.bouncycastle.cert.X509v1CertificateBuilder; |
| 39 | +import org.bouncycastle.cert.X509v3CertificateBuilder; |
| 40 | +import org.bouncycastle.cert.bc.BcX509v1CertificateBuilder; |
| 41 | +import org.bouncycastle.cert.bc.BcX509v3CertificateBuilder; |
32 | 42 | import org.bouncycastle.cert.jcajce.JcaCertStore; |
33 | 43 | import org.bouncycastle.cms.CMSException; |
34 | 44 | import org.bouncycastle.cms.CMSProcessableByteArray; |
|
40 | 50 | import org.bouncycastle.cms.SignerInformationStore; |
41 | 51 | import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder; |
42 | 52 | import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder; |
| 53 | +import org.bouncycastle.crypto.AsymmetricCipherKeyPair; |
| 54 | +import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator; |
| 55 | +import org.bouncycastle.crypto.generators.DSAKeyPairGenerator; |
| 56 | +import org.bouncycastle.crypto.generators.DSAParametersGenerator; |
| 57 | +import org.bouncycastle.crypto.params.AsymmetricKeyParameter; |
| 58 | +import org.bouncycastle.crypto.params.DSAKeyGenerationParameters; |
| 59 | +import org.bouncycastle.crypto.params.DSAParameters; |
| 60 | +import org.bouncycastle.crypto.util.PublicKeyFactory; |
43 | 61 | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 62 | +import org.bouncycastle.operator.ContentSigner; |
44 | 63 | import org.bouncycastle.operator.DigestCalculatorProvider; |
45 | 64 | import org.bouncycastle.operator.OperatorCreationException; |
| 65 | +import org.bouncycastle.operator.bc.BcDSAContentSignerBuilder; |
| 66 | +import org.bouncycastle.operator.bc.BcDSAContentVerifierProviderBuilder; |
| 67 | +import org.bouncycastle.operator.bc.BcHssLmsContentSignerBuilder; |
| 68 | +import org.bouncycastle.operator.bc.BcHssLmsContentVerifierProviderBuilder; |
46 | 69 | import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; |
47 | 70 | import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder; |
| 71 | +import org.bouncycastle.pqc.crypto.lms.HSSKeyGenerationParameters; |
| 72 | +import org.bouncycastle.pqc.crypto.lms.HSSKeyPairGenerator; |
| 73 | +import org.bouncycastle.pqc.crypto.lms.LMOtsParameters; |
| 74 | +import org.bouncycastle.pqc.crypto.lms.LMSKeyGenerationParameters; |
| 75 | +import org.bouncycastle.pqc.crypto.lms.LMSKeyPairGenerator; |
| 76 | +import org.bouncycastle.pqc.crypto.lms.LMSParameters; |
| 77 | +import org.bouncycastle.pqc.crypto.lms.LMSigParameters; |
48 | 78 | import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider; |
49 | 79 | import org.bouncycastle.util.Store; |
50 | 80 |
|
@@ -104,7 +134,7 @@ public static void main(String args[]) |
104 | 134 | throws Exception |
105 | 135 | { |
106 | 136 | init(); |
107 | | - |
| 137 | + //checkCreationHssLms(); |
108 | 138 | junit.textui.TestRunner.run(PQCSignedDataTest.class); |
109 | 139 | } |
110 | 140 |
|
@@ -321,7 +351,7 @@ public void testLmsEncapsulated() |
321 | 351 |
|
322 | 352 | assertTrue(digAlgIds.contains(new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256))); |
323 | 353 | assertTrue(digAlgIds.size() == 1); |
324 | | - |
| 354 | + |
325 | 355 | certs = s.getCertificates(); |
326 | 356 |
|
327 | 357 | SignerInformationStore signers = s.getSignerInfos(); |
@@ -352,6 +382,81 @@ public void testLmsEncapsulated() |
352 | 382 | } |
353 | 383 | } |
354 | 384 |
|
| 385 | + public void testCheckCreationHssLms() |
| 386 | + throws Exception |
| 387 | + { |
| 388 | + // |
| 389 | + // set up the keys |
| 390 | + // |
| 391 | + AsymmetricKeyParameter privKey; |
| 392 | + AsymmetricKeyParameter pubKey; |
| 393 | + |
| 394 | + AsymmetricCipherKeyPairGenerator kpg = new HSSKeyPairGenerator(); |
| 395 | + |
| 396 | + kpg.init(new HSSKeyGenerationParameters( |
| 397 | + new LMSParameters[]{new LMSParameters(LMSigParameters.lms_sha256_n32_h5, LMOtsParameters.sha256_n32_w4)}, new SecureRandom())); |
| 398 | + |
| 399 | + AsymmetricCipherKeyPair pair = kpg.generateKeyPair(); |
| 400 | + |
| 401 | + privKey = (AsymmetricKeyParameter)pair.getPrivate(); |
| 402 | + pubKey = (AsymmetricKeyParameter)pair.getPublic(); |
| 403 | + |
| 404 | + // |
| 405 | + // distinguished name table. |
| 406 | + // |
| 407 | + X500NameBuilder builder = new X500NameBuilder(RFC4519Style.INSTANCE); |
| 408 | + |
| 409 | + builder.addRDN(RFC4519Style.c, "AU"); |
| 410 | + builder.addRDN(RFC4519Style.o, "The Legion of the Bouncy Castle"); |
| 411 | + builder.addRDN(RFC4519Style.l, "Melbourne"); |
| 412 | + builder.addRDN(RFC4519Style.st, "Victoria"); |
| 413 | + builder. addRDN( PKCSObjectIdentifiers. pkcs_9_at_emailAddress, "[email protected]"); |
| 414 | + |
| 415 | + // |
| 416 | + // extensions |
| 417 | + // |
| 418 | + |
| 419 | + // |
| 420 | + // create the certificate - version 3 |
| 421 | + // |
| 422 | + |
| 423 | + |
| 424 | + ContentSigner sigGen = new BcHssLmsContentSignerBuilder().build(privKey); |
| 425 | + X509v3CertificateBuilder certGen = new BcX509v3CertificateBuilder(builder.build(), BigInteger.valueOf(1), new Date(System.currentTimeMillis() - 50000), new Date(System.currentTimeMillis() + 50000), builder.build(), pubKey); |
| 426 | + |
| 427 | + |
| 428 | + X509CertificateHolder cert = certGen.build(sigGen); |
| 429 | + |
| 430 | + assertTrue(cert.isValidOn(new Date())); |
| 431 | + |
| 432 | + assertTrue(cert.isSignatureValid(new BcHssLmsContentVerifierProviderBuilder().build(pubKey))); |
| 433 | + |
| 434 | + |
| 435 | + // |
| 436 | + // create the certificate - version 1 |
| 437 | + // |
| 438 | + |
| 439 | + sigGen = new BcHssLmsContentSignerBuilder().build(privKey); |
| 440 | + X509v1CertificateBuilder certGen1 = new BcX509v1CertificateBuilder(builder.build(), BigInteger.valueOf(1), new Date(System.currentTimeMillis() - 50000), new Date(System.currentTimeMillis() + 50000), builder.build(), pubKey); |
| 441 | + |
| 442 | + cert = certGen1.build(sigGen); |
| 443 | + |
| 444 | + assertTrue(cert.isValidOn(new Date())); |
| 445 | + |
| 446 | + assertTrue(cert.isSignatureValid(new BcHssLmsContentVerifierProviderBuilder().build(pubKey))); |
| 447 | + |
| 448 | + AsymmetricKeyParameter certPubKey = PublicKeyFactory.createKey(cert.getSubjectPublicKeyInfo()); |
| 449 | + |
| 450 | + assertTrue(cert.isSignatureValid(new BcHssLmsContentVerifierProviderBuilder().build(certPubKey))); |
| 451 | + |
| 452 | + ByteArrayInputStream bIn = new ByteArrayInputStream(cert.getEncoded()); |
| 453 | + CertificateFactory fact = CertificateFactory.getInstance("X.509"); |
| 454 | + |
| 455 | + X509Certificate x509cert = (X509Certificate)fact.generateCertificate(bIn); |
| 456 | + |
| 457 | + //System.out.println(cert); |
| 458 | + } |
| 459 | + |
355 | 460 | public void testTryLmsSettings() |
356 | 461 | throws Exception |
357 | 462 | { |
|
0 commit comments