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; 23 24import javax.net.ssl.KeyManager; 25import javax.net.ssl.SSLContextSpi; 26import javax.net.ssl.SSLEngine; 27import javax.net.ssl.SSLEngineResult; 28import javax.net.ssl.SSLException; 29import javax.net.ssl.SSLParameters; 30import javax.net.ssl.SSLSession; 31import javax.net.ssl.SSLSessionContext; 32import javax.net.ssl.SSLServerSocketFactory; 33import javax.net.ssl.SSLSocketFactory; 34import javax.net.ssl.TrustManager; 35 36/** 37 * Additional class for verification of SSLContextSpi and SSLContext 38 * functionality 39 * 40 */ 41 42public class MySSLContextSpi extends SSLContextSpi { 43 private boolean init = false; 44 @Override 45 protected void engineInit(KeyManager[] km, TrustManager[] tm, 46 SecureRandom sr) throws KeyManagementException { 47 if (sr == null) { 48 throw new KeyManagementException( 49 "secureRandom is null"); 50 } 51 init = true; 52 } 53 54 @Override 55 protected SSLSocketFactory engineGetSocketFactory() { 56 if (!init) { 57 throw new RuntimeException("Not initialiazed"); 58 }; 59 return null; 60 } 61 62 @Override 63 protected SSLServerSocketFactory engineGetServerSocketFactory() { 64 if (!init) { 65 throw new RuntimeException("Not initialiazed"); 66 } 67 return null; 68 } 69 70 @Override 71 protected SSLSessionContext engineGetServerSessionContext() { 72 if (!init) { 73 throw new RuntimeException("Not initialiazed"); 74 } 75 return null; 76 } 77 78 @Override 79 protected SSLSessionContext engineGetClientSessionContext() { 80 if (!init) { 81 throw new RuntimeException("Not initialiazed"); 82 } 83 return null; 84 } 85 86 /* 87 * FIXME: add these methods 88 */ 89 @Override 90 protected SSLEngine engineCreateSSLEngine(String host, int port) { 91 if (!init) { 92 throw new RuntimeException("Not initialiazed"); 93 } 94 return new tmpSSLEngine(host, port); 95 } 96 97 @Override 98 protected SSLEngine engineCreateSSLEngine() { 99 if (!init) { 100 throw new RuntimeException("Not initialiazed"); 101 } 102 return new tmpSSLEngine(); 103 } 104 105 public class tmpSSLEngine extends SSLEngine { 106 String tmpHost; 107 int tmpPort; 108 public tmpSSLEngine() { 109 tmpHost = null; 110 tmpPort = 0; 111 } 112 public tmpSSLEngine(String host, int port) { 113 tmpHost = host; 114 tmpPort = port; 115 } 116 @Override 117 public String getPeerHost() { 118 return tmpHost; 119 } 120 @Override 121 public int getPeerPort() { 122 return tmpPort; 123 } 124 @Override 125 public void beginHandshake() throws SSLException { } 126 @Override 127 public void closeInbound() throws SSLException { } 128 @Override 129 public void closeOutbound() {} 130 @Override 131 public Runnable getDelegatedTask() { return null; } 132 @Override 133 public String[] getEnabledCipherSuites() { return null; } 134 @Override 135 public String[] getEnabledProtocols() {return null; } 136 @Override 137 public boolean getEnableSessionCreation() { return true; } 138 @Override 139 public SSLEngineResult.HandshakeStatus getHandshakeStatus() { return null; }; 140 @Override 141 public boolean getNeedClientAuth() { return true; } 142 @Override 143 public SSLSession getSession() { return null; } 144 @Override 145 public String[] getSupportedCipherSuites() { return null; } 146 @Override 147 public String[] getSupportedProtocols() { return null; } 148 @Override 149 public boolean getUseClientMode() { return true; } 150 @Override 151 public boolean getWantClientAuth() { return true; } 152 @Override 153 public boolean isInboundDone() { return true; } 154 @Override 155 public boolean isOutboundDone() { return true; } 156 @Override 157 public void setEnabledCipherSuites(String[] suites) { } 158 @Override 159 public void setEnabledProtocols(String[] protocols) { } 160 @Override 161 public void setEnableSessionCreation(boolean flag) { } 162 @Override 163 public void setNeedClientAuth(boolean need) { } 164 @Override 165 public void setUseClientMode(boolean mode) { } 166 @Override 167 public void setWantClientAuth(boolean want) { } 168 @Override 169 public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, 170 int offset, int length) throws SSLException { 171 return null; 172 } 173 @Override 174 public SSLEngineResult wrap(ByteBuffer[] srcs, int offset, 175 int length, ByteBuffer dst) throws SSLException { 176 return null; 177 } 178 179 @Override 180 public SSLParameters getSSLParameters() { 181 // TODO Auto-generated method stub 182 return null; 183 } 184 185 @Override 186 public void setSSLParameters(SSLParameters sslP) { 187 // TODO Auto-generated method stub 188 189 } 190 } 191 192 @Override 193 protected SSLParameters engineGetDefaultSSLParameters() { 194 return new SSLParameters(new String[] { "Default_SSL_Parameters_For_Test1" }, 195 new String[] { "TLSv1" }); 196 } 197 198 @Override 199 protected SSLParameters engineGetSupportedSSLParameters() { 200 return new SSLParameters(new String[] { "Default_SSL_Parameters_For_Test1", 201 "Default_SSL_Parameters_For_Test2" }, new String[] { "TLSv1", "SSLv3" }); 202 } 203}