Skip to content

Commit 7f95854

Browse files
committed
added use of TestResourceFinder.
1 parent c518429 commit 7f95854

File tree

3 files changed

+180
-159
lines changed

3 files changed

+180
-159
lines changed

pkix/src/test/java/org/bouncycastle/cert/test/CertTest.java

+41-40
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.io.ByteArrayInputStream;
44
import java.io.ByteArrayOutputStream;
5-
import java.io.InputStreamReader;
65
import java.io.IOException;
6+
import java.io.InputStreamReader;
77
import java.io.ObjectInputStream;
88
import java.io.ObjectOutputStream;
99
import java.io.Serializable;
@@ -137,6 +137,7 @@
137137
import org.bouncycastle.pqc.jcajce.spec.SPHINCSPlusParameterSpec;
138138
import org.bouncycastle.pqc.jcajce.spec.XMSSMTParameterSpec;
139139
import org.bouncycastle.pqc.jcajce.spec.XMSSParameterSpec;
140+
import org.bouncycastle.test.TestResourceFinder;
140141
import org.bouncycastle.util.Encodable;
141142
import org.bouncycastle.util.Strings;
142143
import org.bouncycastle.util.encoders.Base64;
@@ -2965,7 +2966,7 @@ public void checkCRLCompositeCreation()
29652966
{
29662967
isTrue(e.getMessage().equals("no matching key found"));
29672968
}
2968-
2969+
29692970
// single key test
29702971
crl.verify(ecPub, BC);
29712972

@@ -4034,7 +4035,7 @@ public void checkCreationECDSA()
40344035
}
40354036

40364037
public void checkCreationPicnic()
4037-
throws Exception
4038+
throws Exception
40384039
{
40394040
if (Security.getProvider("BCPQC") == null)
40404041
{
@@ -4060,12 +4061,12 @@ public void checkCreationPicnic()
40604061
//
40614062
ContentSigner sigGen = new JcaContentSignerBuilder("PICNIC").setProvider("BCPQC").build(privKey);
40624063
X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(builder.build(), BigInteger.valueOf(1), new Date(System.currentTimeMillis() - 50000), new Date(System.currentTimeMillis() + 50000), builder.build(), pubKey)
4063-
.addExtension(new ASN1ObjectIdentifier("2.5.29.15"), true,
4064-
new X509KeyUsage(X509KeyUsage.encipherOnly))
4065-
.addExtension(new ASN1ObjectIdentifier("2.5.29.37"), true,
4066-
new DERSequence(KeyPurposeId.anyExtendedKeyUsage))
4067-
.addExtension(new ASN1ObjectIdentifier("2.5.29.17"), true,
4068-
new GeneralNames(new GeneralName(GeneralName.rfc822Name, "[email protected]")));
4064+
.addExtension(new ASN1ObjectIdentifier("2.5.29.15"), true,
4065+
new X509KeyUsage(X509KeyUsage.encipherOnly))
4066+
.addExtension(new ASN1ObjectIdentifier("2.5.29.37"), true,
4067+
new DERSequence(KeyPurposeId.anyExtendedKeyUsage))
4068+
.addExtension(new ASN1ObjectIdentifier("2.5.29.17"), true,
4069+
new GeneralNames(new GeneralName(GeneralName.rfc822Name, "[email protected]")));
40694070

40704071
X509Certificate baseCert = new JcaX509CertificateConverter().setProvider(BC).getCertificate(certGen.build(sigGen));
40714072

@@ -4077,8 +4078,8 @@ public void checkCreationPicnic()
40774078
//
40784079

40794080
certGen = new JcaX509v3CertificateBuilder(builder.build(), BigInteger.valueOf(1), new Date(System.currentTimeMillis() - 50000), new Date(System.currentTimeMillis() + 50000), builder.build(), pubKey)
4080-
.copyAndAddExtension(new ASN1ObjectIdentifier("2.5.29.15"), true, baseCert)
4081-
.copyAndAddExtension(new ASN1ObjectIdentifier("2.5.29.37"), false, baseCert);
4081+
.copyAndAddExtension(new ASN1ObjectIdentifier("2.5.29.15"), true, baseCert)
4082+
.copyAndAddExtension(new ASN1ObjectIdentifier("2.5.29.37"), false, baseCert);
40824083

40834084
X509Certificate cert = new JcaX509CertificateConverter().setProvider(BC).getCertificate(certGen.build(sigGen));
40844085

@@ -4122,8 +4123,8 @@ public void checkCreationPicnic()
41224123
KeyPair nhKp = kpGen.generateKeyPair();
41234124

41244125
certGen = new JcaX509v3CertificateBuilder(builder.build(), BigInteger.valueOf(1), new Date(System.currentTimeMillis() - 50000), new Date(System.currentTimeMillis() + 50000), builder.build(), nhKp.getPublic())
4125-
.copyAndAddExtension(new ASN1ObjectIdentifier("2.5.29.15"), true, baseCert)
4126-
.copyAndAddExtension(new ASN1ObjectIdentifier("2.5.29.37"), false, baseCert);
4126+
.copyAndAddExtension(new ASN1ObjectIdentifier("2.5.29.15"), true, baseCert)
4127+
.copyAndAddExtension(new ASN1ObjectIdentifier("2.5.29.37"), false, baseCert);
41274128

41284129
cert = new JcaX509CertificateConverter().setProvider(BC).getCertificate(certGen.build(sigGen));
41294130

@@ -4620,7 +4621,7 @@ public void checkCreationComposite()
46204621
if (System.getProperty("java.version").indexOf("1.5.") < 0)
46214622
{
46224623
cert.verify(ecPub, new BouncyCastleProvider()); // ec key only
4623-
4624+
46244625
cert.verify(lmsPub, new BouncyCastlePQCProvider()); // lms key only
46254626
}
46264627

@@ -5425,23 +5426,23 @@ private void checkSerialisation()
54255426

54265427
// TESTS REGARDING COMPOSITES https://www.ietf.org/archive/id/draft-ounsworth-pq-composite-sigs-13.html
54275428
private static String[] compositeSignaturesOIDs = {
5428-
"2.16.840.1.114027.80.8.1.1", //id-MLDSA44-RSA2048-PSS-SHA256
5429-
"2.16.840.1.114027.80.8.1.2", //id-MLDSA44-RSA2048-PKCS15-SHA256
5430-
"2.16.840.1.114027.80.8.1.3", //id-MLDSA44-Ed25519-SHA512
5431-
"2.16.840.1.114027.80.8.1.4", //id-MLDSA44-ECDSA-P256-SHA256
5432-
"2.16.840.1.114027.80.8.1.5", //id-MLDSA44-ECDSA-brainpoolP256r1-SHA256
5433-
"2.16.840.1.114027.80.8.1.6", //id-MLDSA65-RSA3072-PSS-SHA512
5434-
"2.16.840.1.114027.80.8.1.7", //id-MLDSA65-RSA3072-PKCS15-SHA512
5435-
"2.16.840.1.114027.80.8.1.8", //id-MLDSA65-ECDSA-P256-SHA512
5436-
"2.16.840.1.114027.80.8.1.9", //id-MLDSA65-ECDSA-brainpoolP256r1-SHA512
5437-
"2.16.840.1.114027.80.8.1.10", //id-MLDSA65-Ed25519-SHA512
5438-
"2.16.840.1.114027.80.8.1.11", //id-MLDSA87-ECDSA-P384-SHA512
5439-
"2.16.840.1.114027.80.8.1.12", //id-MLDSA87-ECDSA-brainpoolP384r1-SHA512
5440-
"2.16.840.1.114027.80.8.1.13", //id-MLDSA87-Ed448-SHA512
5441-
// Falcon composites below were excluded from the draft. See MiscObjectIdentifiers for details.
5442-
"2.16.840.1.114027.80.8.1.14", //id-Falcon512-ECDSA-P256-SHA256
5443-
"2.16.840.1.114027.80.8.1.15", //id-Falcon512-ECDSA-brainpoolP256r1-SHA256
5444-
"2.16.840.1.114027.80.8.1.16", //id-Falcon512-Ed25519-SHA512
5429+
"2.16.840.1.114027.80.8.1.1", //id-MLDSA44-RSA2048-PSS-SHA256
5430+
"2.16.840.1.114027.80.8.1.2", //id-MLDSA44-RSA2048-PKCS15-SHA256
5431+
"2.16.840.1.114027.80.8.1.3", //id-MLDSA44-Ed25519-SHA512
5432+
"2.16.840.1.114027.80.8.1.4", //id-MLDSA44-ECDSA-P256-SHA256
5433+
"2.16.840.1.114027.80.8.1.5", //id-MLDSA44-ECDSA-brainpoolP256r1-SHA256
5434+
"2.16.840.1.114027.80.8.1.6", //id-MLDSA65-RSA3072-PSS-SHA512
5435+
"2.16.840.1.114027.80.8.1.7", //id-MLDSA65-RSA3072-PKCS15-SHA512
5436+
"2.16.840.1.114027.80.8.1.8", //id-MLDSA65-ECDSA-P256-SHA512
5437+
"2.16.840.1.114027.80.8.1.9", //id-MLDSA65-ECDSA-brainpoolP256r1-SHA512
5438+
"2.16.840.1.114027.80.8.1.10", //id-MLDSA65-Ed25519-SHA512
5439+
"2.16.840.1.114027.80.8.1.11", //id-MLDSA87-ECDSA-P384-SHA512
5440+
"2.16.840.1.114027.80.8.1.12", //id-MLDSA87-ECDSA-brainpoolP384r1-SHA512
5441+
"2.16.840.1.114027.80.8.1.13", //id-MLDSA87-Ed448-SHA512
5442+
// Falcon composites below were excluded from the draft. See MiscObjectIdentifiers for details.
5443+
"2.16.840.1.114027.80.8.1.14", //id-Falcon512-ECDSA-P256-SHA256
5444+
"2.16.840.1.114027.80.8.1.15", //id-Falcon512-ECDSA-brainpoolP256r1-SHA256
5445+
"2.16.840.1.114027.80.8.1.16", //id-Falcon512-Ed25519-SHA512
54455446
};
54465447

54475448
private void checkCompositeSignatureCertificateCreation()
@@ -5465,7 +5466,7 @@ private void checkCompositeSignatureCertificateCreation()
54655466
X509Certificate cert = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certHolder);
54665467

54675468
isEquals(oid, cert.getSigAlgOID());
5468-
CompositePublicKey compositePublicKey = (CompositePublicKey) cert.getPublicKey();
5469+
CompositePublicKey compositePublicKey = (CompositePublicKey)cert.getPublicKey();
54695470
isEquals(CompositeSignaturesConstants.ASN1IdentifierAlgorithmNameMap.get(new ASN1ObjectIdentifier(oid)), compositePublicKey.getAlgorithm());
54705471

54715472
isEquals(subjectName, cert.getSubjectX500Principal().getName());
@@ -5475,7 +5476,7 @@ private void checkCompositeSignatureCertificateCreation()
54755476
}
54765477
}
54775478
catch (NoSuchAlgorithmException | NoSuchProviderException | CertificateException | OperatorCreationException |
5478-
SignatureException | InvalidKeyException | TestFailedException e)
5479+
SignatureException | InvalidKeyException | TestFailedException e)
54795480
{
54805481
fail("checkCompositeSignatureCertificateCreation failed: " + e.getMessage());
54815482
}
@@ -5486,8 +5487,8 @@ private void checkParseCompositePublicKey()
54865487
try
54875488
{
54885489
//compositePublicKeyExampleRFC.pem contains the sample public key from https://www.ietf.org/archive/id/draft-ounsworth-pq-composite-sigs-13.html
5489-
PEMParser pemParser = new PEMParser(new InputStreamReader(this.getClass().getResourceAsStream("compositePublicKeyExampleRFC.pem")));
5490-
SubjectPublicKeyInfo subjectPublicKeyInfo = (SubjectPublicKeyInfo) pemParser.readObject();
5490+
PEMParser pemParser = new PEMParser(new InputStreamReader(TestResourceFinder.findTestResource("pqc/composite", "compositePublicKeyExampleRFC.pem")));
5491+
SubjectPublicKeyInfo subjectPublicKeyInfo = (SubjectPublicKeyInfo)pemParser.readObject();
54915492
isEquals(subjectPublicKeyInfo.getAlgorithm().getAlgorithm(), MiscObjectIdentifiers.id_MLDSA44_ECDSA_P256_SHA256);
54925493

54935494
CompositePublicKey compositePublicKey = new CompositePublicKey(subjectPublicKeyInfo);
@@ -5508,8 +5509,8 @@ private void checkParseCompositePrivateKey()
55085509
//compositePrivateKeyExample.pem does NOT contain the sample private key from https://www.ietf.org/archive/id/draft-ounsworth-pq-composite-sigs-13.html
55095510
//because the at this moment, the Dilithium private key formats don't match.
55105511
//this sample was generated from this BC implementation
5511-
PEMParser pemParser = new PEMParser(new InputStreamReader(this.getClass().getResourceAsStream("compositePrivateKeyExample.pem")));
5512-
PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) pemParser.readObject();
5512+
PEMParser pemParser = new PEMParser(new InputStreamReader(TestResourceFinder.findTestResource("pqc/composite", "compositePrivateKeyExample.pem")));
5513+
PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo)pemParser.readObject();
55135514

55145515
isEquals(privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm(), MiscObjectIdentifiers.id_MLDSA44_ECDSA_P256_SHA256);
55155516

@@ -5529,14 +5530,14 @@ private void checkParseAndVerifyCompositeCertificate()
55295530
try
55305531
{
55315532
//compositeCertificateExampleRFC.pem contains the sample certificate from https://www.ietf.org/archive/id/draft-ounsworth-pq-composite-sigs-13.html
5532-
PEMParser pemParser = new PEMParser(new InputStreamReader(this.getClass().getResourceAsStream("compositeCertificateExampleRFC.pem")));
5533-
X509CertificateHolder certificateHolder = (X509CertificateHolder) pemParser.readObject();
5533+
PEMParser pemParser = new PEMParser(new InputStreamReader(TestResourceFinder.findTestResource("pqc/composite", "compositeCertificateExampleRFC.pem")));
5534+
X509CertificateHolder certificateHolder = (X509CertificateHolder)pemParser.readObject();
55345535
JcaX509CertificateConverter x509Converter = new JcaX509CertificateConverter().setProvider("BC");
55355536
X509Certificate certificate = x509Converter.getCertificate(certificateHolder);
55365537

55375538
isEquals(certificate.getSigAlgOID(), MiscObjectIdentifiers.id_MLDSA44_ECDSA_P256_SHA256.toString());
55385539

5539-
CompositePublicKey compositePublicKey = (CompositePublicKey) certificate.getPublicKey();
5540+
CompositePublicKey compositePublicKey = (CompositePublicKey)certificate.getPublicKey();
55405541

55415542
isEquals(compositePublicKey.getPublicKeys().get(0).getAlgorithm(), "DILITHIUM2");
55425543
isEquals(compositePublicKey.getPublicKeys().get(1).getAlgorithm(), "ECDSA");

prov/src/test/java/org/bouncycastle/jcajce/provider/test/AllTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public static Test suite()
2929
suite.addTestSuite(RandomTest.class);
3030
suite.addTestSuite(RFC3211WrapTest.class);
3131
suite.addTestSuite(SP80038GTest.class);
32+
suite.addTestSuite(CompositeKeyTest.class);
33+
suite.addTestSuite(CompositeSignaturesTest.class);
3234
suite.addTestSuite(BouncyCastleProviderTest.class);
3335

3436
return new BCTestSetup(suite);

0 commit comments

Comments
 (0)