package org.bouncycastletest; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assume.assumeTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.math.BigInteger; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.security.interfaces.ECPublicKey; import java.security.spec.AlgorithmParameterSpec; import java.security.spec.ECPublicKeySpec; import java.util.Date; import org.apache.log4j.Logger; import org.bouncycastle.asn1.ASN1InputStream; import org.bouncycastle.asn1.ASN1Sequence; import org.bouncycastle.asn1.x500.X500Name; import org.bouncycastle.asn1.x500.X500NameBuilder; import org.bouncycastle.asn1.x500.style.BCStyle; import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier; import org.bouncycastle.asn1.x509.BasicConstraints; import org.bouncycastle.asn1.x509.Extension; import org.bouncycastle.asn1.x509.SubjectKeyIdentifier; import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; import org.bouncycastle.cert.X509v3CertificateBuilder; import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; import org.bouncycastle.jce.ECGOST3410NamedCurveTable; import org.bouncycastle.jce.X509KeyUsage; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.operator.BufferingContentSigner; import org.bouncycastle.operator.ContentSigner; import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; import org.bouncycastle.util.encoders.Base64; import org.cesecore.certificates.util.AlgorithmConstants; import org.cesecore.certificates.util.AlgorithmTools; import org.cesecore.config.CesecoreConfiguration; import org.cesecore.keys.util.KeyTools; import org.cesecore.util.CryptoProviderTools; import org.junit.BeforeClass; import org.junit.Test; /** * This test verifies that GOST3410 and DSTU4145 are working in BouncyCastle. * It doesn't actually test EJBCA. Note that these algorithms must be configured * in EJBCA. */ public class ExtraAlgorithmsPKCS12Test { private static final Logger log = Logger.getLogger(ExtraAlgorithmsPKCS12Test.class); @BeforeClass public static void beforeClass() throws Exception { // Install BouncyCastle provider CryptoProviderTools.installBCProviderIfNotAvailable(); } @Test public void testP12KeystoreGOST3410() throws Exception { log.debug("GOST3410 configured: "+(AlgorithmTools.isGost3410Enabled() ? "YES" : "NO")); assumeTrue(AlgorithmTools.isGost3410Enabled()); log.trace(">testP12KeystoreGOST3410()"); String keyspec = CesecoreConfiguration.getExtraAlgSubAlgName("gost3410", "B"); assertNotNull("curve B is not configued!", keyspec); AlgorithmParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(keyspec); assertNotNull(spec); testAlgorithm("ECGOST3410", AlgorithmConstants.KEYALGORITHM_ECGOST3410, AlgorithmConstants.SIGALG_GOST3411_WITH_ECGOST3410, spec); log.trace("testP12KeystoreDSTU4145()"); String keyspec = CesecoreConfiguration.getExtraAlgSubAlgName("dstu4145", "233"); assertNotNull("curve 233 is not configued!", keyspec); AlgorithmParameterSpec spec = KeyTools.dstuOidToAlgoParams(keyspec); assertNotNull(spec); testAlgorithm("DSTU4145", AlgorithmConstants.KEYALGORITHM_DSTU4145, AlgorithmConstants.SIGALG_GOST3411_WITH_DSTU4145, spec); log.trace("