1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.xnet.tests.support;
19
20import java.nio.ByteBuffer;
21import java.security.KeyManagementException;
22import java.security.SecureRandom;
23import javax.net.ssl.KeyManager;
24import javax.net.ssl.SSLContextSpi;
25import javax.net.ssl.SSLEngine;
26import javax.net.ssl.SSLEngineResult;
27import javax.net.ssl.SSLException;
28import javax.net.ssl.SSLParameters;
29import javax.net.ssl.SSLServerSocketFactory;
30import javax.net.ssl.SSLSession;
31import javax.net.ssl.SSLSessionContext;
32import javax.net.ssl.SSLSocketFactory;
33import javax.net.ssl.TrustManager;
34
35/**
36 * Additional class for verification of SSLContextSpi and SSLContext
37 * functionality
38 *
39 */
40
41public class MySSLContextSpi extends SSLContextSpi {
42    private boolean init = false;
43    protected void engineInit(KeyManager[] km, TrustManager[] tm,
44            SecureRandom sr) throws KeyManagementException {
45        if (sr == null) {
46            throw new KeyManagementException(
47                    "secureRandom is null");
48        }
49        init = true;
50    }
51
52    protected SSLSocketFactory engineGetSocketFactory() {
53        if (!init) {
54            throw new RuntimeException("Not initialiazed");
55        }
56        return null;
57    }
58
59    protected SSLServerSocketFactory engineGetServerSocketFactory() {
60        if (!init) {
61            throw new RuntimeException("Not initialiazed");
62        }
63        return null;
64    }
65
66    protected SSLSessionContext engineGetServerSessionContext() {
67        if (!init) {
68            throw new RuntimeException("Not initialiazed");
69        }
70        return null;
71    }
72
73    protected SSLSessionContext engineGetClientSessionContext() {
74        if (!init) {
75            throw new RuntimeException("Not initialiazed");
76        }
77        return null;
78    }
79
80    protected SSLParameters engineGetDefaultSSLParameters() {
81        engineGetSocketFactory();
82        return null;
83    }
84
85    protected SSLParameters engineGetSupportedSSLParameters() {
86        engineGetSocketFactory();
87        return null;
88    }
89
90    /*
91     * FIXME: add these methods
92     */
93    protected SSLEngine engineCreateSSLEngine(String host, int port) {
94        if (!init) {
95            throw new RuntimeException("Not initialiazed");
96        }
97        return new tmpSSLEngine(host, port);
98    }
99
100    protected SSLEngine engineCreateSSLEngine() {
101        if (!init) {
102            throw new RuntimeException("Not initialiazed");
103        }
104        return new tmpSSLEngine();
105    }
106
107    public class tmpSSLEngine extends SSLEngine {
108        String tmpHost;
109        int tmpPort;
110        public tmpSSLEngine() {
111            tmpHost = null;
112            tmpPort = 0;
113        }
114        public tmpSSLEngine(String host, int port) {
115            tmpHost = host;
116            tmpPort = port;
117        }
118        public String getPeerHost() {
119            return tmpHost;
120        }
121        public int getPeerPort() {
122            return tmpPort;
123        }
124        public void beginHandshake() throws SSLException { }
125        public void closeInbound() throws SSLException { }
126        public void closeOutbound() {}
127        public Runnable getDelegatedTask() { return null; }
128        public String[] getEnabledCipherSuites() { return null; }
129        public String[] getEnabledProtocols() {return null; }
130        public boolean getEnableSessionCreation() { return true; }
131        public SSLEngineResult.HandshakeStatus getHandshakeStatus() { return null; }
132        public boolean getNeedClientAuth() { return true; }
133        public SSLSession getSession() { return null; }
134        public String[] getSupportedCipherSuites()  { return null; }
135        public String[] getSupportedProtocols()  { return null; }
136        public boolean getUseClientMode()  { return true; }
137        public boolean getWantClientAuth()  { return true; }
138        public boolean isInboundDone()  { return true; }
139        public boolean isOutboundDone()  { return true; }
140        public void setEnabledCipherSuites(String[] suites) { }
141        public void setEnabledProtocols(String[] protocols) { }
142        public void setEnableSessionCreation(boolean flag) { }
143        public void setNeedClientAuth(boolean need) { }
144        public void setUseClientMode(boolean mode) { }
145        public void setWantClientAuth(boolean want) { }
146        public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts,
147                int offset, int length) throws SSLException {
148            return null;
149        }
150        public SSLEngineResult wrap(ByteBuffer[] srcs, int offset,
151                int length, ByteBuffer dst) throws SSLException {
152            return null;
153        }
154    }
155}
156