| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.gsi.gssapi.net; |
| 16 |
|
| 17 |
import java.net.Socket; |
| 18 |
import java.io.IOException; |
| 19 |
import java.io.InputStream; |
| 20 |
import java.io.OutputStream; |
| 21 |
|
| 22 |
import org.globus.common.ChainedIOException; |
| 23 |
import org.globus.net.WrappedSocket; |
| 24 |
import org.globus.net.SocketFactory; |
| 25 |
import org.globus.gsi.gssapi.auth.Authorization; |
| 26 |
import org.globus.gsi.gssapi.auth.SelfAuthorization; |
| 27 |
|
| 28 |
import org.ietf.jgss.GSSContext; |
| 29 |
import org.ietf.jgss.GSSException; |
| 30 |
|
| 31 |
import org.apache.commons.logging.Log; |
| 32 |
import org.apache.commons.logging.LogFactory; |
| 33 |
|
| 34 |
public abstract class GssSocket extends WrappedSocket { |
| 35 |
|
| 36 |
private static Log logger = |
| 37 |
LogFactory.getLog(GssSocket.class.getName()); |
| 38 |
|
| 39 |
protected GSSContext context; |
| 40 |
protected boolean clientMode; |
| 41 |
|
| 42 |
protected InputStream in; |
| 43 |
protected OutputStream out; |
| 44 |
|
| 45 |
protected Authorization authorization = |
| 46 |
SelfAuthorization.getInstance(); |
| 47 |
|
| 48 |
public static final int SSL_MODE = 1; |
| 49 |
public static final int GSI_MODE = 2; |
| 50 |
|
| 51 |
protected int mode = -1; |
| 52 |
|
| 53 |
public GssSocket(String host, int port, GSSContext context) |
| 54 |
throws IOException { |
| 55 |
this(SocketFactory.getDefault().createSocket(host, port), context); |
| 56 |
} |
| 57 |
|
| 58 |
public GssSocket(Socket socket, GSSContext context) { |
| 59 |
super(socket); |
| 60 |
this.context = context; |
| 61 |
this.clientMode = true; |
| 62 |
} |
| 63 |
|
| 64 |
public void setAuthorization(Authorization auth) { |
| 65 |
this.authorization = auth; |
| 66 |
} |
| 67 |
|
| 68 |
public Authorization getAuthorization() { |
| 69 |
return this.authorization; |
| 70 |
} |
| 71 |
|
| 72 |
public void setUseClientMode(boolean clientMode) { |
| 73 |
this.clientMode = clientMode; |
| 74 |
} |
| 75 |
|
| 76 |
public boolean getClientMode() { |
| 77 |
return this.clientMode; |
| 78 |
} |
| 79 |
|
| 80 |
public void setWrapMode(int mode) { |
| 81 |
this.mode = mode; |
| 82 |
} |
| 83 |
|
| 84 |
public int getWrapMode() { |
| 85 |
return this.mode; |
| 86 |
} |
| 87 |
|
| 88 |
public GSSContext getContext() { |
| 89 |
return this.context; |
| 90 |
} |
| 91 |
|
| 92 |
abstract protected void writeToken(byte [] token) |
| 93 |
throws IOException; |
| 94 |
|
| 95 |
abstract protected byte[] readToken() |
| 96 |
throws IOException; |
| 97 |
|
| 98 |
protected synchronized void authenticateClient() |
| 99 |
throws IOException, GSSException { |
| 100 |
|
| 101 |
byte [] outToken = null; |
| 102 |
byte [] inToken = new byte[0]; |
| 103 |
|
| 104 |
while (!this.context.isEstablished()) { |
| 105 |
|
| 106 |
outToken = |
| 107 |
this.context.initSecContext(inToken, 0, inToken.length); |
| 108 |
|
| 109 |
if (outToken != null) { |
| 110 |
writeToken(outToken); |
| 111 |
} |
| 112 |
|
| 113 |
if (!this.context.isEstablished()) { |
| 114 |
inToken = readToken(); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
protected synchronized void authenticateServer() |
| 120 |
throws IOException, GSSException { |
| 121 |
|
| 122 |
byte [] outToken = null; |
| 123 |
byte [] inToken = null; |
| 124 |
|
| 125 |
while (!this.context.isEstablished()) { |
| 126 |
inToken = readToken(); |
| 127 |
|
| 128 |
outToken = |
| 129 |
this.context.acceptSecContext(inToken, 0, inToken.length); |
| 130 |
|
| 131 |
if (outToken != null) { |
| 132 |
writeToken(outToken); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
public synchronized void startHandshake() |
| 138 |
throws IOException { |
| 139 |
if (this.context.isEstablished()) return; |
| 140 |
logger.debug("Handshake start"); |
| 141 |
|
| 142 |
try { |
| 143 |
if (this.clientMode) { |
| 144 |
authenticateClient(); |
| 145 |
} else { |
| 146 |
authenticateServer(); |
| 147 |
} |
| 148 |
} catch (GSSException e) { |
| 149 |
throw new ChainedIOException("Authentication failed", e); |
| 150 |
} |
| 151 |
|
| 152 |
logger.debug("Handshake end"); |
| 153 |
if (this.authorization != null) { |
| 154 |
logger.debug("Performing authorization."); |
| 155 |
this.authorization.authorize(this.context, |
| 156 |
getInetAddress().getHostAddress()); |
| 157 |
} else { |
| 158 |
logger.debug("Authorization not set"); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public synchronized OutputStream getOutputStream() |
| 163 |
throws IOException { |
| 164 |
try { |
| 165 |
startHandshake(); |
| 166 |
return this.out; |
| 167 |
} catch (IOException e) { |
| 168 |
try { close(); } catch (IOException ioe) {} |
| 169 |
throw e; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public synchronized InputStream getInputStream() |
| 174 |
throws IOException { |
| 175 |
try { |
| 176 |
startHandshake(); |
| 177 |
return this.in; |
| 178 |
} catch (IOException e) { |
| 179 |
try { close(); } catch (IOException ioe) {} |
| 180 |
throw e; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Disposes of the context and closes the connection |
| 186 |
*/ |
| 187 |
public void close() |
| 188 |
throws IOException { |
| 189 |
try { |
| 190 |
this.context.dispose(); |
| 191 |
} catch (GSSException e) { |
| 192 |
throw new ChainedIOException("dispose failed.", e); |
| 193 |
} finally { |
| 194 |
this.socket.close(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
} |