use of java.security.cert.PKIXBuilderParameters in project robovm by robovm.
the class PKIXBuilderParametersTest method testPKIXBuilderParametersKeyStoreCertSelector04.
/**
* Test #4 for <code>PKIXBuilderParameters(KeyStore, CertSelector)</code>
* constructor<br>
* Assertion: <code>NullPointerException</code> -
* if the <code>keystore</code> is <code>null</code>
*/
public final void testPKIXBuilderParametersKeyStoreCertSelector04() throws Exception {
KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
keyTest.load(null, null);
ByteArrayInputStream certArray = new ByteArrayInputStream(certificate.getBytes());
ByteArrayInputStream certArray2 = new ByteArrayInputStream(certificate2.getBytes());
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate[] cert = new X509Certificate[2];
cert[0] = (X509Certificate) cf.generateCertificate(certArray);
cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
keyTest.setCertificateEntry("alias1", cert[0]);
keyTest.setCertificateEntry("alias2", cert[0]);
keyTest.setCertificateEntry("alias3", cert[1]);
try {
PKIXBuilderParameters p = new PKIXBuilderParameters(keyTest, new X509CertSelector());
assertEquals(3, p.getTrustAnchors().size());
assertEquals(5, p.getMaxPathLength());
} catch (Exception e) {
fail("Unexpected exception " + e.getMessage());
}
}
use of java.security.cert.PKIXBuilderParameters in project robovm by robovm.
the class PKIXBuilderParametersTest method testPKIXBuilderParametersSetCertSelector02.
/**
* Test #2 for <code>PKIXBuilderParameters(Set, CertSelector)</code>
* constructor<br>
* Assertion: creates an instance of <code>PKIXBuilderParameters</code>
* @throws InvalidAlgorithmParameterException
*/
public final void testPKIXBuilderParametersSetCertSelector02() throws InvalidAlgorithmParameterException {
Set<TrustAnchor> taSet = TestUtils.getTrustAnchorSet();
if (taSet == null) {
fail(getName() + ": not performed (could not create test TrustAnchor set)");
}
// both parameters are valid but CertSelector is null
PKIXParameters p = new PKIXBuilderParameters(taSet, null);
assertTrue("instanceOf", p instanceof PKIXBuilderParameters);
assertNull("certSelector", p.getTargetCertConstraints());
}
use of java.security.cert.PKIXBuilderParameters in project robovm by robovm.
the class PKIXBuilderParametersTest method testGetMaxPathLength.
/**
* Test for <code>getMaxPathLength()</code>
*/
public final void testGetMaxPathLength() throws Exception {
KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
keyTest.load(null, null);
ByteArrayInputStream certArray = new ByteArrayInputStream(certificate.getBytes());
ByteArrayInputStream certArray2 = new ByteArrayInputStream(certificate2.getBytes());
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate[] cert = new X509Certificate[2];
cert[0] = (X509Certificate) cf.generateCertificate(certArray);
cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
keyTest.setCertificateEntry("alias1", cert[0]);
keyTest.setCertificateEntry("alias2", cert[0]);
keyTest.setCertificateEntry("alias3", cert[1]);
PKIXBuilderParameters p = new PKIXBuilderParameters(keyTest, new X509CertSelector());
assertEquals(5, p.getMaxPathLength());
p.setMaxPathLength(10);
assertEquals(10, p.getMaxPathLength());
}
use of java.security.cert.PKIXBuilderParameters in project robovm by robovm.
the class TrustManagerFactoryTest method test_TrustManagerFactory.
private void test_TrustManagerFactory(TrustManagerFactory tmf) throws Exception {
assertNotNull(tmf);
assertNotNull(tmf.getAlgorithm());
assertNotNull(tmf.getProvider());
// before init
try {
tmf.getTrustManagers();
fail();
} catch (IllegalStateException expected) {
}
// init with null ManagerFactoryParameters
try {
tmf.init((ManagerFactoryParameters) null);
fail();
} catch (InvalidAlgorithmParameterException expected) {
}
// init with useless ManagerFactoryParameters
try {
tmf.init(new UselessManagerFactoryParameters());
fail();
} catch (InvalidAlgorithmParameterException expected) {
}
// init with PKIXParameters ManagerFactoryParameters
try {
PKIXParameters pp = new PKIXParameters(getTestKeyStore().keyStore);
CertPathTrustManagerParameters cptmp = new CertPathTrustManagerParameters(pp);
tmf.init(cptmp);
fail();
} catch (InvalidAlgorithmParameterException expected) {
}
// init with PKIXBuilderParameters ManagerFactoryParameters
X509CertSelector xcs = new X509CertSelector();
PKIXBuilderParameters pbp = new PKIXBuilderParameters(getTestKeyStore().keyStore, xcs);
CertPathTrustManagerParameters cptmp = new CertPathTrustManagerParameters(pbp);
if (supportsManagerFactoryParameters(tmf.getAlgorithm())) {
tmf.init(cptmp);
test_TrustManagerFactory_getTrustManagers(tmf);
} else {
try {
tmf.init(cptmp);
fail();
} catch (InvalidAlgorithmParameterException expected) {
}
}
// init with null for default KeyStore
tmf.init((KeyStore) null);
test_TrustManagerFactory_getTrustManagers(tmf);
// init with specific key store
tmf.init(getTestKeyStore().keyStore);
test_TrustManagerFactory_getTrustManagers(tmf);
}
use of java.security.cert.PKIXBuilderParameters in project jdk8u_jdk by JetBrains.
the class BuildEEBasicConstraints method main.
public static void main(String[] args) throws Exception {
// reset the security property to make sure that the algorithms
// and keys used in this test are not disabled.
Security.setProperty("jdk.certpath.disabledAlgorithms", "MD2");
X509Certificate rootCert = CertUtils.getCertFromFile("anchor.cer");
TrustAnchor anchor = new TrustAnchor(rootCert.getSubjectX500Principal(), rootCert.getPublicKey(), null);
X509CertSelector sel = new X509CertSelector();
sel.setBasicConstraints(-2);
PKIXBuilderParameters params = new PKIXBuilderParameters(Collections.singleton(anchor), sel);
params.setRevocationEnabled(false);
X509Certificate eeCert = CertUtils.getCertFromFile("ee.cer");
X509Certificate caCert = CertUtils.getCertFromFile("ca.cer");
ArrayList<X509Certificate> certs = new ArrayList<X509Certificate>();
certs.add(caCert);
certs.add(eeCert);
CollectionCertStoreParameters ccsp = new CollectionCertStoreParameters(certs);
CertStore cs = CertStore.getInstance("Collection", ccsp);
params.addCertStore(cs);
PKIXCertPathBuilderResult res = CertUtils.build(params);
CertPath cp = res.getCertPath();
// check that first certificate is an EE cert
List<? extends Certificate> certList = cp.getCertificates();
X509Certificate cert = (X509Certificate) certList.get(0);
if (cert.getBasicConstraints() != -1) {
throw new Exception("Target certificate is not an EE certificate");
}
}
Aggregations