001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Boris Kuznetsov
020: * @version $Revision$
021: */package org.apache.harmony.xnet.provider.jsse;
022:
023: import java.security.InvalidAlgorithmParameterException;
024: import java.security.KeyStore;
025: import java.security.cert.CertPathValidator;
026: import java.security.cert.CertPathValidatorException;
027: import java.security.cert.CertificateException;
028: import java.security.cert.CertificateFactory;
029: import java.security.cert.PKIXParameters;
030: import java.security.cert.TrustAnchor;
031: import java.security.cert.X509Certificate;
032: import java.util.Arrays;
033: import java.util.Enumeration;
034: import java.util.HashSet;
035: import java.util.Iterator;
036: import java.util.Set;
037:
038: import javax.net.ssl.X509TrustManager;
039:
040: /**
041: *
042: * TrustManager implementation. The implementation is based on CertPathValidator
043: * PKIX and CertificateFactory X509 implementations. This implementations should
044: * be provided by some certification provider.
045: *
046: * @see javax.net.ssl.X509TrustManager
047: */
048: public class TrustManagerImpl implements X509TrustManager {
049:
050: private CertPathValidator validator;
051:
052: private PKIXParameters params;
053:
054: private Exception err = null;
055:
056: private CertificateFactory factory;
057:
058: /**
059: * Creates trust manager implementation
060: *
061: * @param ks
062: */
063: public TrustManagerImpl(KeyStore ks) {
064: try {
065: validator = CertPathValidator.getInstance("PKIX");
066: factory = CertificateFactory.getInstance("X509");
067: String alias;
068: X509Certificate cert;
069: byte[] nameConstrains = null;
070: Set trusted = new HashSet();
071: for (Enumeration en = ks.aliases(); en.hasMoreElements();) {
072: alias = (String) en.nextElement();
073: cert = (X509Certificate) ks.getCertificate(alias);
074: if (cert != null) {
075: trusted.add(new TrustAnchor(cert, nameConstrains));
076: }
077: }
078: params = new PKIXParameters(trusted);
079: params.setRevocationEnabled(false);
080: } catch (Exception e) {
081: err = e;
082: }
083: }
084:
085: /**
086: * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],
087: * String)
088: */
089: public void checkClientTrusted(X509Certificate[] chain,
090: String authType) throws CertificateException {
091: if (chain == null || chain.length == 0 || authType == null
092: || authType.length() == 0) {
093: throw new IllegalArgumentException(
094: "null or zero-length parameter");
095: }
096: if (err != null) {
097: throw new CertificateException(err);
098: }
099: try {
100: validator.validate(factory.generateCertPath(Arrays
101: .asList(chain)), params);
102: } catch (InvalidAlgorithmParameterException e) {
103: throw new CertificateException(e);
104: } catch (CertPathValidatorException e) {
105: throw new CertificateException(e);
106: }
107: }
108:
109: /**
110: * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],
111: * String)
112: */
113: public void checkServerTrusted(X509Certificate[] chain,
114: String authType) throws CertificateException {
115: if (chain == null || chain.length == 0 || authType == null
116: || authType.length() == 0) {
117: throw new IllegalArgumentException(
118: "null or zero-length parameter");
119: }
120: if (err != null) {
121: throw new CertificateException(err);
122: }
123: try {
124: validator.validate(factory.generateCertPath(Arrays
125: .asList(chain)), params);
126: } catch (InvalidAlgorithmParameterException e) {
127: throw new CertificateException(e);
128: } catch (CertPathValidatorException e) {
129: throw new CertificateException(e);
130: }
131: }
132:
133: /**
134: * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
135: */
136: public X509Certificate[] getAcceptedIssuers() {
137: if (params == null) {
138: return new X509Certificate[0];
139: }
140: Set anchors = params.getTrustAnchors();
141: X509Certificate[] certs = new X509Certificate[anchors.size()];
142: int i = 0;
143: for (Iterator it = anchors.iterator(); it.hasNext();) {
144: certs[i++] = ((TrustAnchor) it.next()).getTrustedCert();
145: }
146: return certs;
147: }
148:
149: }
|