TrustManagerImplpublic class TrustManagerImpl extends Object implements X509TrustManagerTrustManager implementation. The implementation is based on CertPathValidator
PKIX and CertificateFactory X509 implementations. This implementations should
be provided by some certification provider. |
Fields Summary |
---|
private CertPathValidator | validator | private PKIXParameters | params | private Exception | err | private CertificateFactory | factory |
Constructors Summary |
---|
public TrustManagerImpl(KeyStore ks)Creates trust manager implementation
try {
validator = CertPathValidator.getInstance("PKIX");
factory = CertificateFactory.getInstance("X509");
String alias;
X509Certificate cert;
byte[] nameConstrains = null;
Set trusted = new HashSet();
for (Enumeration en = ks.aliases(); en.hasMoreElements();) {
alias = (String) en.nextElement();
cert = (X509Certificate) ks.getCertificate(alias);
if (cert != null) {
trusted.add(new TrustAnchor(cert, nameConstrains));
}
}
params = new PKIXParameters(trusted);
params.setRevocationEnabled(false);
} catch (Exception e) {
err = e;
}
|
Methods Summary |
---|
public void | checkClientTrusted(java.security.cert.X509Certificate[] chain, java.lang.String authType)
if (chain == null || chain.length == 0 || authType == null
|| authType.length() == 0) {
throw new IllegalArgumentException("null or zero-length parameter");
}
if (err != null) {
throw new CertificateException(err);
}
// BEGIN android-added
// Cater for degenerate special case where we can't
// establish an actual certificate chain the usual way,
// but have the peer certificate in our trust store.
if (isDirectlyTrustedCert(chain)) {
return;
}
// END android-added
try {
// BEGIN android-changed
CertPath certPath = factory.generateCertPath(Arrays.asList(chain));
if (!Arrays.equals(chain[0].getEncoded(),
((X509Certificate)certPath.getCertificates().get(0))
.getEncoded())) {
// sanity check failed (shouldn't ever happen, but we are using pretty remote code)
throw new CertificateException("Certificate chain error");
}
validator.validate(certPath, params);
// END android-changed
} catch (InvalidAlgorithmParameterException e) {
throw new CertificateException(e);
} catch (CertPathValidatorException e) {
throw new CertificateException(e);
}
| public void | checkServerTrusted(java.security.cert.X509Certificate[] chain, java.lang.String authType)
if (chain == null || chain.length == 0 || authType == null
|| authType.length() == 0) {
throw new IllegalArgumentException("null or zero-length parameter");
}
if (err != null) {
throw new CertificateException(err);
}
// BEGIN android-added
// Cater for degenerate special case where we can't
// establish an actual certificate chain the usual way,
// but have the peer certificate in our trust store.
if (isDirectlyTrustedCert(chain)) {
return;
}
// END android-added
try {
// BEGIN android-changed
CertPath certPath = factory.generateCertPath(Arrays.asList(chain));
if (!Arrays.equals(chain[0].getEncoded(),
((X509Certificate)certPath.getCertificates().get(0))
.getEncoded())) {
// sanity check failed (shouldn't ever happen, but we are using pretty remote code)
throw new CertificateException("Certificate chain error");
}
validator.validate(certPath, params);
// END android-changed
} catch (InvalidAlgorithmParameterException e) {
throw new CertificateException(e);
} catch (CertPathValidatorException e) {
throw new CertificateException(e);
}
| public java.security.cert.X509Certificate[] | getAcceptedIssuers()
if (params == null) {
return new X509Certificate[0];
}
Set anchors = params.getTrustAnchors();
X509Certificate[] certs = new X509Certificate[anchors.size()];
int i = 0;
for (Iterator it = anchors.iterator(); it.hasNext();) {
certs[i++] = ((TrustAnchor) it.next()).getTrustedCert();
}
return certs;
| private boolean | isDirectlyTrustedCert(java.security.cert.X509Certificate[] chain)Checks whether the given chain is just a certificate
that we have in our trust store.
byte[] questionable;
if (chain.length == 1) {
try {
questionable = chain[0].getEncoded();
Set<TrustAnchor> anchors = params.getTrustAnchors();
for (TrustAnchor trustAnchor : anchors) {
byte[] trusted = trustAnchor.getTrustedCert().getEncoded();
if (Arrays.equals(questionable, trusted)) {
return true;
}
}
} catch (CertificateEncodingException e) {
// Ignore.
}
}
return false;
|
|