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 Vera Y. Petrashkova
020: * @version $Revision$
021: */package javax.net.ssl;
022:
023: import java.security.KeyManagementException;
024: import java.security.NoSuchAlgorithmException;
025: import java.security.NoSuchProviderException;
026: import java.security.Provider;
027: import java.security.SecureRandom;
028: import java.security.Security;
029:
030: import org.apache.harmony.security.fortress.Engine;
031:
032: /**
033: * @com.intel.drl.spec_ref
034: *
035: */
036:
037: public class SSLContext {
038: // StoreSSLContext service name
039: private static final String SERVICE = "SSLContext";
040:
041: // Used to access common engine functionality
042: private static Engine engine = new Engine(SERVICE);
043:
044: // Storeused provider
045: private final Provider provider;
046:
047: // Storeused SSLContextSpi implementation
048: private final SSLContextSpi spiImpl;
049:
050: // Storeused protocol
051: private final String protocol;
052:
053: /*
054: * @com.intel.drl.spec_ref
055: *
056: */
057: protected SSLContext(SSLContextSpi contextSpi, Provider provider,
058: String protocol) {
059: this .provider = provider;
060: this .protocol = protocol;
061: this .spiImpl = contextSpi;
062: }
063:
064: /**
065: * @com.intel.drl.spec_ref
066: *
067: * throws NullPointerException if protocol is null (instead of
068: * NoSuchAlgorithmException as in 1.4 release)
069: */
070: public static SSLContext getInstance(String protocol)
071: throws NoSuchAlgorithmException {
072: if (protocol == null) {
073: throw new NullPointerException("protocol is null");
074: }
075: synchronized (engine) {
076: engine.getInstance(protocol, null);
077: return new SSLContext((SSLContextSpi) engine.spi,
078: engine.provider, protocol);
079: }
080: }
081:
082: /**
083: * @com.intel.drl.spec_ref
084: *
085: * throws NullPointerException if protocol is null (instead of
086: * NoSuchAlgorithmException as in 1.4 release)
087: */
088: public static SSLContext getInstance(String protocol,
089: String provider) throws NoSuchAlgorithmException,
090: NoSuchProviderException {
091: if (provider == null) {
092: throw new IllegalArgumentException("Provider is null");
093: }
094: if (provider.length() == 0) {
095: throw new IllegalArgumentException("Provider is empty");
096: }
097: Provider impProvider = Security.getProvider(provider);
098: if (impProvider == null) {
099: throw new NoSuchProviderException(provider);
100: }
101: return getInstance(protocol, impProvider);
102: }
103:
104: /**
105: * @com.intel.drl.spec_ref
106: *
107: * throws NullPointerException if protocol is null (instead of
108: * NoSuchAlgorithmException as in 1.4 release)
109: */
110: public static SSLContext getInstance(String protocol,
111: Provider provider) throws NoSuchAlgorithmException {
112: if (provider == null) {
113: throw new IllegalArgumentException("provider is null");
114: }
115: if (protocol == null) {
116: throw new NullPointerException("protocol is null");
117: }
118: synchronized (engine) {
119: engine.getInstance(protocol, provider, null);
120: return new SSLContext((SSLContextSpi) engine.spi, provider,
121: protocol);
122: }
123: }
124:
125: /**
126: * @com.intel.drl.spec_ref
127: *
128: */
129: public final String getProtocol() {
130: return protocol;
131: }
132:
133: /**
134: * @com.intel.drl.spec_ref
135: *
136: */
137: public final Provider getProvider() {
138: return provider;
139: }
140:
141: /**
142: * @com.intel.drl.spec_ref
143: *
144: * FIXME: check what exception will be thrown when parameters are null
145: */
146: public final void init(KeyManager[] km, TrustManager[] tm,
147: SecureRandom sr) throws KeyManagementException {
148: spiImpl.engineInit(km, tm, sr);
149: }
150:
151: /**
152: * @com.intel.drl.spec_ref
153: *
154: */
155: public final SSLSocketFactory getSocketFactory() {
156: return spiImpl.engineGetSocketFactory();
157: }
158:
159: /**
160: * @com.intel.drl.spec_ref
161: *
162: */
163: public final SSLServerSocketFactory getServerSocketFactory() {
164: return spiImpl.engineGetServerSocketFactory();
165: }
166:
167: /**
168: * @com.intel.drl.spec_ref
169: *
170: */
171: public final SSLEngine createSSLEngine() {
172: return spiImpl.engineCreateSSLEngine();
173: }
174:
175: /**
176: * @com.intel.drl.spec_ref
177: *
178: */
179: public final SSLEngine createSSLEngine(String peerHost, int peerPort) {
180: return spiImpl.engineCreateSSLEngine(peerHost, peerPort);
181: }
182:
183: /**
184: * @com.intel.drl.spec_ref
185: *
186: */
187: public final SSLSessionContext getServerSessionContext() {
188: return spiImpl.engineGetServerSessionContext();
189: }
190:
191: /**
192: * @com.intel.drl.spec_ref
193: *
194: */
195: public final SSLSessionContext getClientSessionContext() {
196: return spiImpl.engineGetClientSessionContext();
197: }
198: }
|