use of java.security.cert.TrustAnchor in project robovm by robovm.
the class PKIXCertPathBuilderResultTest method testPKIXCertPathBuilderResult02.
/**
* Test #2 for <code>PKIXCertPathBuilderResult(CertPath, TrustAnchor,
* PolicyNode, PublicKey)</code> constructor<br>
* Assertion: policy tree parameter may be <code>null</code>
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
public final void testPKIXCertPathBuilderResult02() throws InvalidKeySpecException, NoSuchAlgorithmException {
TrustAnchor ta = TestUtils.getTrustAnchor();
if (ta == null) {
fail(getName() + ": not performed (could not create test TrustAnchor)");
}
CertPathBuilderResult r = new PKIXCertPathBuilderResult(new MyCertPath(testEncoding), ta, null, testPublicKey);
assertTrue(r instanceof PKIXCertPathBuilderResult);
}
use of java.security.cert.TrustAnchor in project robovm by robovm.
the class PKIXCertPathBuilderResultTest method testPKIXCertPathBuilderResult03.
/**
* Test #3 for <code>PKIXCertPathBuilderResult(CertPath, TrustAnchor,
* PolicyNode, PublicKey)</code> constructor<br>
* Assertion: <code>NullPointerException</code>
* if certPath is <code>null</code>
*/
public final void testPKIXCertPathBuilderResult03() {
TrustAnchor ta = TestUtils.getTrustAnchor();
if (ta == null) {
fail(getName() + ": not performed (could not create test TrustAnchor)");
}
try {
// pass null
new PKIXCertPathBuilderResult(null, ta, TestUtils.getPolicyTree(), testPublicKey);
fail("NPE expected");
} catch (NullPointerException e) {
}
}
use of java.security.cert.TrustAnchor in project robovm by robovm.
the class TestUtils method getTrustAnchor.
/**
* Creates <code>TrustAnchor</code> instance
* constructed using self signed test certificate
*
* @return <code>TrustAnchor</code> instance
*/
public static TrustAnchor getTrustAnchor() {
CertificateFactory cf = null;
try {
cf = CertificateFactory.getInstance(certType);
} catch (CertificateException e) {
// that were searched
throw new RuntimeException(e);
}
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new ByteArrayInputStream(getEncodedX509Certificate()));
X509Certificate c1 = (X509Certificate) cf.generateCertificate(bis);
return new TrustAnchor(c1, null);
} catch (Exception e) {
// all failures are fatal
throw new RuntimeException(e);
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException ign) {
}
}
}
}
use of java.security.cert.TrustAnchor in project robovm by robovm.
the class TestUtils method getCertPathParameters.
public static CertPathParameters getCertPathParameters() throws InvalidAlgorithmParameterException {
if ((rootCertificateSS == null) || (theCertSelector == null) || (builder == null)) {
throw new RuntimeException("Call initCertPathSSCertChain prior to buildCertPath");
}
PKIXBuilderParameters buildParams = new PKIXBuilderParameters(Collections.singleton(new TrustAnchor(rootCertificateSS, null)), theCertSelector);
buildParams.addCertStore(store);
buildParams.setRevocationEnabled(false);
return buildParams;
}
use of java.security.cert.TrustAnchor in project XobotOS by xamarin.
the class CertPathValidatorUtilities method findTrustAnchor.
/**
* Search the given Set of TrustAnchor's for one that is the
* issuer of the given X509 certificate. Uses the specified
* provider for signature verification, or the default provider
* if null.
*
* @param cert the X509 certificate
* @param trustAnchors a Set of TrustAnchor's
* @param sigProvider the provider to use for signature verification
*
* @return the <code>TrustAnchor</code> object if found or
* <code>null</code> if not.
*
* @exception AnnotatedException
* if a TrustAnchor was found but the signature verification
* on the given certificate has thrown an exception.
*/
protected static TrustAnchor findTrustAnchor(X509Certificate cert, Set trustAnchors, String sigProvider) throws AnnotatedException {
TrustAnchor trust = null;
PublicKey trustPublicKey = null;
Exception invalidKeyEx = null;
X509CertSelector certSelectX509 = new X509CertSelector();
X500Principal certIssuer = getEncodedIssuerPrincipal(cert);
try {
certSelectX509.setSubject(certIssuer.getEncoded());
} catch (IOException ex) {
throw new AnnotatedException("Cannot set subject search criteria for trust anchor.", ex);
}
Iterator iter = trustAnchors.iterator();
while (iter.hasNext() && trust == null) {
trust = (TrustAnchor) iter.next();
if (trust.getTrustedCert() != null) {
if (certSelectX509.match(trust.getTrustedCert())) {
trustPublicKey = trust.getTrustedCert().getPublicKey();
} else {
trust = null;
}
} else if (trust.getCAName() != null && trust.getCAPublicKey() != null) {
try {
X500Principal caName = new X500Principal(trust.getCAName());
if (certIssuer.equals(caName)) {
trustPublicKey = trust.getCAPublicKey();
} else {
trust = null;
}
} catch (IllegalArgumentException ex) {
trust = null;
}
} else {
trust = null;
}
if (trustPublicKey != null) {
try {
verifyX509Certificate(cert, trustPublicKey, sigProvider);
} catch (Exception ex) {
invalidKeyEx = ex;
trust = null;
}
}
}
if (trust == null && invalidKeyEx != null) {
throw new AnnotatedException("TrustAnchor found but certificate validation failed.", invalidKeyEx);
}
return trust;
}
Aggregations