| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
package org.apache.qpid.proton.engine.impl; |
| 23 |
|
| 24 |
import static org.apache.qpid.proton.engine.impl.ByteBufferUtils.newWriteableBuffer; |
| 25 |
import static org.apache.qpid.proton.engine.impl.ByteBufferUtils.pourAll; |
| 26 |
import static org.apache.qpid.proton.engine.impl.ByteBufferUtils.pourBufferToArray; |
| 27 |
|
| 28 |
import java.nio.ByteBuffer; |
| 29 |
import java.nio.charset.StandardCharsets; |
| 30 |
import java.util.logging.Level; |
| 31 |
import java.util.logging.Logger; |
| 32 |
|
| 33 |
import org.apache.qpid.proton.amqp.Binary; |
| 34 |
import org.apache.qpid.proton.amqp.Symbol; |
| 35 |
import org.apache.qpid.proton.amqp.security.SaslChallenge; |
| 36 |
import org.apache.qpid.proton.amqp.security.SaslCode; |
| 37 |
import org.apache.qpid.proton.amqp.security.SaslFrameBody; |
| 38 |
import org.apache.qpid.proton.amqp.security.SaslInit; |
| 39 |
import org.apache.qpid.proton.amqp.security.SaslMechanisms; |
| 40 |
import org.apache.qpid.proton.amqp.security.SaslResponse; |
| 41 |
import org.apache.qpid.proton.codec.AMQPDefinedTypes; |
| 42 |
import org.apache.qpid.proton.codec.DecoderImpl; |
| 43 |
import org.apache.qpid.proton.codec.EncoderImpl; |
| 44 |
import org.apache.qpid.proton.engine.Sasl; |
| 45 |
import org.apache.qpid.proton.engine.SaslListener; |
| 46 |
import org.apache.qpid.proton.engine.Transport; |
| 47 |
import org.apache.qpid.proton.engine.TransportException; |
| 48 |
|
| 49 |
public class SaslImpl implements Sasl, SaslFrameBody.SaslFrameBodyHandler<Void>, SaslFrameHandler, TransportLayer |
| 50 |
{ |
| 51 |
private static final Logger _logger = Logger.getLogger(SaslImpl.class.getName()); |
| 52 |
|
| 53 |
public static final byte SASL_FRAME_TYPE = (byte) 1; |
| 54 |
private static final String HEADER_DESCRIPTION = "SASL"; |
| 55 |
|
| 56 |
private final DecoderImpl _decoder = new DecoderImpl(); |
| 57 |
private final EncoderImpl _encoder = new EncoderImpl(_decoder); |
| 58 |
|
| 59 |
private final TransportImpl _transport; |
| 60 |
|
| 61 |
private boolean _tail_closed = false; |
| 62 |
private boolean _head_closed = false; |
| 63 |
private final int _maxFrameSize; |
| 64 |
private final FrameWriter _frameWriter; |
| 65 |
|
| 66 |
private ByteBuffer _pending; |
| 67 |
|
| 68 |
private boolean _headerWritten; |
| 69 |
private Binary _challengeResponse; |
| 70 |
private SaslFrameParser _frameParser; |
| 71 |
private boolean _initReceived; |
| 72 |
private boolean _mechanismsSent; |
| 73 |
private boolean _initSent; |
| 74 |
|
| 75 |
enum Role { CLIENT, SERVER } |
| 76 |
|
| 77 |
private SaslOutcome _outcome = SaslOutcome.PN_SASL_NONE; |
| 78 |
private SaslState _state = SaslState.PN_SASL_IDLE; |
| 79 |
|
| 80 |
private String _hostname; |
| 81 |
private boolean _done; |
| 82 |
private Symbol[] _mechanisms; |
| 83 |
|
| 84 |
private Symbol _chosenMechanism; |
| 85 |
|
| 86 |
private Role _role; |
| 87 |
private boolean _allowSkip = true; |
| 88 |
|
| 89 |
private SaslListener _saslListener; |
| 90 |
|
| 91 |
/** |
| 92 |
* @param maxFrameSize the size of the input and output buffers |
| 93 |
* {@link SaslTransportWrapper#_inputBuffer} and |
| 94 |
* {@link SaslTransportWrapper#_outputBuffer}. |
| 95 |
*/ |
| 96 |
SaslImpl(TransportImpl transport, int maxFrameSize) |
| 97 |
{ |
| 98 |
_transport = transport; |
| 99 |
_maxFrameSize = maxFrameSize; |
| 100 |
|
| 101 |
AMQPDefinedTypes.registerAllTypes(_decoder,_encoder); |
| 102 |
_frameParser = new SaslFrameParser(this, _decoder, maxFrameSize, _transport); |
| 103 |
_frameWriter = new FrameWriter(_encoder, maxFrameSize, FrameWriter.SASL_FRAME_TYPE, _transport); |
| 104 |
} |
| 105 |
|
| 106 |
void fail() { |
| 107 |
if (_role == null || _role == Role.CLIENT) { |
| 108 |
_role = Role.CLIENT; |
| 109 |
_initSent = true; |
| 110 |
} else { |
| 111 |
_initReceived = true; |
| 112 |
|
| 113 |
} |
| 114 |
_done = true; |
| 115 |
_outcome = SaslOutcome.PN_SASL_SYS; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public boolean isDone() |
| 120 |
{ |
| 121 |
return _done && (_role==Role.CLIENT || _initReceived); |
| 122 |
} |
| 123 |
|
| 124 |
private void process() |
| 125 |
{ |
| 126 |
processHeader(); |
| 127 |
|
| 128 |
if(_role == Role.SERVER) |
| 129 |
{ |
| 130 |
if(!_mechanismsSent && _mechanisms != null) |
| 131 |
{ |
| 132 |
SaslMechanisms mechanisms = new SaslMechanisms(); |
| 133 |
|
| 134 |
mechanisms.setSaslServerMechanisms(_mechanisms); |
| 135 |
writeFrame(mechanisms); |
| 136 |
_mechanismsSent = true; |
| 137 |
_state = SaslState.PN_SASL_STEP; |
| 138 |
} |
| 139 |
|
| 140 |
if(getState() == SaslState.PN_SASL_STEP && getChallengeResponse() != null) |
| 141 |
{ |
| 142 |
SaslChallenge challenge = new SaslChallenge(); |
| 143 |
challenge.setChallenge(getChallengeResponse()); |
| 144 |
writeFrame(challenge); |
| 145 |
setChallengeResponse(null); |
| 146 |
} |
| 147 |
|
| 148 |
if(_done) |
| 149 |
{ |
| 150 |
org.apache.qpid.proton.amqp.security.SaslOutcome outcome = |
| 151 |
new org.apache.qpid.proton.amqp.security.SaslOutcome(); |
| 152 |
outcome.setCode(SaslCode.values()[_outcome.getCode()]); |
| 153 |
if (_outcome == PN_SASL_OK) |
| 154 |
{ |
| 155 |
outcome.setAdditionalData(getChallengeResponse()); |
| 156 |
} |
| 157 |
writeFrame(outcome); |
| 158 |
setChallengeResponse(null); |
| 159 |
} |
| 160 |
} |
| 161 |
else if(_role == Role.CLIENT) |
| 162 |
{ |
| 163 |
if(getState() == SaslState.PN_SASL_IDLE && _chosenMechanism != null) |
| 164 |
{ |
| 165 |
processInit(); |
| 166 |
_state = SaslState.PN_SASL_STEP; |
| 167 |
|
| 168 |
//HACK: if we received an outcome before |
| 169 |
//we sent our init, change the state now |
| 170 |
if(_outcome != SaslOutcome.PN_SASL_NONE) |
| 171 |
{ |
| 172 |
_state = classifyStateFromOutcome(_outcome); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if(getState() == SaslState.PN_SASL_STEP && getChallengeResponse() != null) |
| 177 |
{ |
| 178 |
processResponse(); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
private void writeFrame(SaslFrameBody frameBody) |
| 184 |
{ |
| 185 |
_frameWriter.writeFrame(frameBody); |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
final public int recv(byte[] bytes, int offset, int size) |
| 190 |
{ |
| 191 |
if(_pending == null) |
| 192 |
{ |
| 193 |
return -1; |
| 194 |
} |
| 195 |
final int written = pourBufferToArray(_pending, bytes, offset, size); |
| 196 |
if(!_pending.hasRemaining()) |
| 197 |
{ |
| 198 |
_pending = null; |
| 199 |
} |
| 200 |
return written; |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
final public int send(byte[] bytes, int offset, int size) |
| 205 |
{ |
| 206 |
byte[] data = new byte[size]; |
| 207 |
System.arraycopy(bytes, offset, data, 0, size); |
| 208 |
setChallengeResponse(new Binary(data)); |
| 209 |
return size; |
| 210 |
} |
| 211 |
|
| 212 |
final int processHeader() |
| 213 |
{ |
| 214 |
if(!_headerWritten) |
| 215 |
{ |
| 216 |
logHeader(); |
| 217 |
|
| 218 |
_frameWriter.writeHeader(AmqpHeader.SASL_HEADER); |
| 219 |
_headerWritten = true; |
| 220 |
return AmqpHeader.SASL_HEADER.length; |
| 221 |
} |
| 222 |
else |
| 223 |
{ |
| 224 |
return 0; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
private void logHeader() |
| 229 |
{ |
| 230 |
if (_transport.isFrameTracingEnabled()) |
| 231 |
{ |
| 232 |
_transport.log(TransportImpl.OUTGOING, HEADER_DESCRIPTION); |
| 233 |
|
| 234 |
ProtocolTracer tracer = _transport.getProtocolTracer(); |
| 235 |
if (tracer != null) |
| 236 |
{ |
| 237 |
tracer.sentHeader(HEADER_DESCRIPTION); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public int pending() |
| 244 |
{ |
| 245 |
return _pending == null ? 0 : _pending.remaining(); |
| 246 |
} |
| 247 |
|
| 248 |
void setPending(ByteBuffer pending) |
| 249 |
{ |
| 250 |
_pending = pending; |
| 251 |
} |
| 252 |
|
| 253 |
@Override |
| 254 |
public SaslState getState() |
| 255 |
{ |
| 256 |
return _state; |
| 257 |
} |
| 258 |
|
| 259 |
final Binary getChallengeResponse() |
| 260 |
{ |
| 261 |
return _challengeResponse; |
| 262 |
} |
| 263 |
|
| 264 |
final void setChallengeResponse(Binary challengeResponse) |
| 265 |
{ |
| 266 |
_challengeResponse = challengeResponse; |
| 267 |
} |
| 268 |
|
| 269 |
@Override |
| 270 |
public void setMechanisms(String... mechanisms) |
| 271 |
{ |
| 272 |
if(mechanisms != null) |
| 273 |
{ |
| 274 |
_mechanisms = new Symbol[mechanisms.length]; |
| 275 |
for(int i = 0; i < mechanisms.length; i++) |
| 276 |
{ |
| 277 |
_mechanisms[i] = Symbol.valueOf(mechanisms[i]); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
if(_role == Role.CLIENT) |
| 282 |
{ |
| 283 |
assert mechanisms != null; |
| 284 |
assert mechanisms.length == 1; |
| 285 |
|
| 286 |
_chosenMechanism = Symbol.valueOf(mechanisms[0]); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
public String[] getRemoteMechanisms() |
| 292 |
{ |
| 293 |
if(_role == Role.SERVER) |
| 294 |
{ |
| 295 |
return _chosenMechanism == null ? new String[0] : new String[] { _chosenMechanism.toString() }; |
| 296 |
} |
| 297 |
else if(_role == Role.CLIENT) |
| 298 |
{ |
| 299 |
if(_mechanisms == null) |
| 300 |
{ |
| 301 |
return new String[0]; |
| 302 |
} |
| 303 |
else |
| 304 |
{ |
| 305 |
String[] remoteMechanisms = new String[_mechanisms.length]; |
| 306 |
for(int i = 0; i < _mechanisms.length; i++) |
| 307 |
{ |
| 308 |
remoteMechanisms[i] = _mechanisms[i].toString(); |
| 309 |
} |
| 310 |
return remoteMechanisms; |
| 311 |
} |
| 312 |
} |
| 313 |
else |
| 314 |
{ |
| 315 |
throw new IllegalStateException(); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
public void setMechanism(Symbol mechanism) |
| 320 |
{ |
| 321 |
_chosenMechanism = mechanism; |
| 322 |
} |
| 323 |
|
| 324 |
public Symbol getChosenMechanism() |
| 325 |
{ |
| 326 |
return _chosenMechanism; |
| 327 |
} |
| 328 |
|
| 329 |
public void setResponse(Binary initialResponse) |
| 330 |
{ |
| 331 |
setPending(initialResponse.asByteBuffer()); |
| 332 |
} |
| 333 |
|
| 334 |
@Override |
| 335 |
public void handle(SaslFrameBody frameBody, Binary payload) |
| 336 |
{ |
| 337 |
_transport.log(TransportImpl.INCOMING, frameBody); |
| 338 |
|
| 339 |
ProtocolTracer tracer = _transport.getProtocolTracer(); |
| 340 |
if( tracer != null ) |
| 341 |
{ |
| 342 |
tracer.receivedSaslBody(frameBody); |
| 343 |
} |
| 344 |
|
| 345 |
frameBody.invoke(this, payload, null); |
| 346 |
} |
| 347 |
|
| 348 |
@Override |
| 349 |
public void handleInit(SaslInit saslInit, Binary payload, Void context) |
| 350 |
{ |
| 351 |
if(_role == null) |
| 352 |
{ |
| 353 |
server(); |
| 354 |
} |
| 355 |
checkRole(Role.SERVER); |
| 356 |
_hostname = saslInit.getHostname(); |
| 357 |
_chosenMechanism = saslInit.getMechanism(); |
| 358 |
_initReceived = true; |
| 359 |
if(saslInit.getInitialResponse() != null) |
| 360 |
{ |
| 361 |
setPending(saslInit.getInitialResponse().asByteBuffer()); |
| 362 |
} |
| 363 |
|
| 364 |
if(_saslListener != null) { |
| 365 |
_saslListener.onSaslInit(this, _transport); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
@Override |
| 370 |
public void handleResponse(SaslResponse saslResponse, Binary payload, Void context) |
| 371 |
{ |
| 372 |
checkRole(Role.SERVER); |
| 373 |
setPending(saslResponse.getResponse() == null ? null : saslResponse.getResponse().asByteBuffer()); |
| 374 |
|
| 375 |
if(_saslListener != null) { |
| 376 |
_saslListener.onSaslResponse(this, _transport); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
@Override |
| 381 |
public void done(SaslOutcome outcome) |
| 382 |
{ |
| 383 |
checkRole(Role.SERVER); |
| 384 |
_outcome = outcome; |
| 385 |
_done = true; |
| 386 |
_state = classifyStateFromOutcome(outcome); |
| 387 |
_logger.fine("SASL negotiation done: " + this); |
| 388 |
} |
| 389 |
|
| 390 |
private void checkRole(Role role) |
| 391 |
{ |
| 392 |
if(role != _role) |
| 393 |
{ |
| 394 |
throw new IllegalStateException("Role is " + _role + " but should be " + role); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
@Override |
| 399 |
public void handleMechanisms(SaslMechanisms saslMechanisms, Binary payload, Void context) |
| 400 |
{ |
| 401 |
if(_role == null) |
| 402 |
{ |
| 403 |
client(); |
| 404 |
} |
| 405 |
checkRole(Role.CLIENT); |
| 406 |
_mechanisms = saslMechanisms.getSaslServerMechanisms(); |
| 407 |
|
| 408 |
if(_saslListener != null) { |
| 409 |
_saslListener.onSaslMechanisms(this, _transport); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
@Override |
| 414 |
public void handleChallenge(SaslChallenge saslChallenge, Binary payload, Void context) |
| 415 |
{ |
| 416 |
checkRole(Role.CLIENT); |
| 417 |
setPending(saslChallenge.getChallenge() == null ? null : saslChallenge.getChallenge().asByteBuffer()); |
| 418 |
|
| 419 |
if(_saslListener != null) { |
| 420 |
_saslListener.onSaslChallenge(this, _transport); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
@Override |
| 425 |
public void handleOutcome(org.apache.qpid.proton.amqp.security.SaslOutcome saslOutcome, |
| 426 |
Binary payload, |
| 427 |
Void context) |
| 428 |
{ |
| 429 |
checkRole(Role.CLIENT); |
| 430 |
for(SaslOutcome outcome : SaslOutcome.values()) |
| 431 |
{ |
| 432 |
setPending(saslOutcome.getAdditionalData() == null ? null : saslOutcome.getAdditionalData().asByteBuffer()); |
| 433 |
if(outcome.getCode() == saslOutcome.getCode().ordinal()) |
| 434 |
{ |
| 435 |
_outcome = outcome; |
| 436 |
if (_state != SaslState.PN_SASL_IDLE) |
| 437 |
{ |
| 438 |
_state = classifyStateFromOutcome(outcome); |
| 439 |
} |
| 440 |
break; |
| 441 |
} |
| 442 |
} |
| 443 |
_done = true; |
| 444 |
|
| 445 |
if(_logger.isLoggable(Level.FINE)) |
| 446 |
{ |
| 447 |
_logger.fine("Handled outcome: " + this); |
| 448 |
} |
| 449 |
|
| 450 |
if(_saslListener != null) { |
| 451 |
_saslListener.onSaslOutcome(this, _transport); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
private SaslState classifyStateFromOutcome(SaslOutcome outcome) |
| 456 |
{ |
| 457 |
return outcome == SaslOutcome.PN_SASL_OK ? SaslState.PN_SASL_PASS : SaslState.PN_SASL_FAIL; |
| 458 |
} |
| 459 |
|
| 460 |
private void processResponse() |
| 461 |
{ |
| 462 |
SaslResponse response = new SaslResponse(); |
| 463 |
response.setResponse(getChallengeResponse()); |
| 464 |
setChallengeResponse(null); |
| 465 |
writeFrame(response); |
| 466 |
} |
| 467 |
|
| 468 |
private void processInit() |
| 469 |
{ |
| 470 |
SaslInit init = new SaslInit(); |
| 471 |
init.setHostname(_hostname); |
| 472 |
init.setMechanism(_chosenMechanism); |
| 473 |
if(getChallengeResponse() != null) |
| 474 |
{ |
| 475 |
init.setInitialResponse(getChallengeResponse()); |
| 476 |
setChallengeResponse(null); |
| 477 |
} |
| 478 |
_initSent = true; |
| 479 |
writeFrame(init); |
| 480 |
} |
| 481 |
|
| 482 |
@Override |
| 483 |
public void plain(String username, String password) |
| 484 |
{ |
| 485 |
client(); |
| 486 |
_chosenMechanism = Symbol.valueOf("PLAIN"); |
| 487 |
byte[] usernameBytes = username.getBytes(StandardCharsets.UTF_8); |
| 488 |
byte[] passwordBytes = password.getBytes(StandardCharsets.UTF_8); |
| 489 |
byte[] data = new byte[usernameBytes.length+passwordBytes.length+2]; |
| 490 |
System.arraycopy(usernameBytes, 0, data, 1, usernameBytes.length); |
| 491 |
System.arraycopy(passwordBytes, 0, data, 2+usernameBytes.length, passwordBytes.length); |
| 492 |
|
| 493 |
setChallengeResponse(new Binary(data)); |
| 494 |
} |
| 495 |
|
| 496 |
@Override |
| 497 |
public SaslOutcome getOutcome() |
| 498 |
{ |
| 499 |
return _outcome; |
| 500 |
} |
| 501 |
|
| 502 |
@Override |
| 503 |
public void client() |
| 504 |
{ |
| 505 |
_role = Role.CLIENT; |
| 506 |
if(_mechanisms != null) |
| 507 |
{ |
| 508 |
assert _mechanisms.length == 1; |
| 509 |
|
| 510 |
_chosenMechanism = _mechanisms[0]; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
@Override |
| 515 |
public void server() |
| 516 |
{ |
| 517 |
_role = Role.SERVER; |
| 518 |
} |
| 519 |
|
| 520 |
@Override |
| 521 |
public void allowSkip(boolean allowSkip) |
| 522 |
{ |
| 523 |
_allowSkip = allowSkip; |
| 524 |
} |
| 525 |
|
| 526 |
@Override |
| 527 |
public TransportWrapper wrap(final TransportInput input, final TransportOutput output) |
| 528 |
{ |
| 529 |
return new SaslSniffer(new SwitchingSaslTransportWrapper(input, output), |
| 530 |
new PlainTransportWrapper(output, input)) { |
| 531 |
@Override |
| 532 |
protected boolean isDeterminationMade() { |
| 533 |
if (_role == Role.SERVER && _allowSkip) { |
| 534 |
return super.isDeterminationMade(); |
| 535 |
} else { |
| 536 |
_selectedTransportWrapper = _wrapper1; |
| 537 |
return true; |
| 538 |
} |
| 539 |
} |
| 540 |
}; |
| 541 |
} |
| 542 |
|
| 543 |
@Override |
| 544 |
public String toString() |
| 545 |
{ |
| 546 |
StringBuilder builder = new StringBuilder(); |
| 547 |
builder |
| 548 |
.append("SaslImpl [_outcome=").append(_outcome) |
| 549 |
.append(", state=").append(_state) |
| 550 |
.append(", done=").append(_done) |
| 551 |
.append(", role=").append(_role) |
| 552 |
.append("]"); |
| 553 |
return builder.toString(); |
| 554 |
} |
| 555 |
|
| 556 |
private class SaslTransportWrapper implements TransportWrapper |
| 557 |
{ |
| 558 |
private final TransportInput _underlyingInput; |
| 559 |
private final TransportOutput _underlyingOutput; |
| 560 |
private boolean _outputComplete; |
| 561 |
|
| 562 |
private final ByteBuffer _outputBuffer; |
| 563 |
private final ByteBuffer _inputBuffer; |
| 564 |
private final ByteBuffer _head; |
| 565 |
|
| 566 |
private final SwitchingSaslTransportWrapper _parent; |
| 567 |
|
| 568 |
private SaslTransportWrapper(SwitchingSaslTransportWrapper parent, TransportInput input, TransportOutput output) |
| 569 |
{ |
| 570 |
_underlyingInput = input; |
| 571 |
_underlyingOutput = output; |
| 572 |
|
| 573 |
_inputBuffer = newWriteableBuffer(_maxFrameSize); |
| 574 |
_outputBuffer = newWriteableBuffer(_maxFrameSize); |
| 575 |
|
| 576 |
_parent = parent; |
| 577 |
|
| 578 |
if (_transport.isUseReadOnlyOutputBuffer()) { |
| 579 |
_head = _outputBuffer.asReadOnlyBuffer(); |
| 580 |
} else { |
| 581 |
_head = _outputBuffer.duplicate(); |
| 582 |
} |
| 583 |
|
| 584 |
_head.limit(0); |
| 585 |
} |
| 586 |
|
| 587 |
private void fillOutputBuffer() |
| 588 |
{ |
| 589 |
if(isOutputInSaslMode()) |
| 590 |
{ |
| 591 |
writeSaslOutput(); |
| 592 |
if(_done) |
| 593 |
{ |
| 594 |
_outputComplete = true; |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* TODO rationalise this method with respect to the other similar checks of _role/_initReceived etc |
| 601 |
* @see SaslImpl#isDone() |
| 602 |
*/ |
| 603 |
private boolean isInputInSaslMode() |
| 604 |
{ |
| 605 |
return _role == null || (_role == Role.CLIENT && !_done) || (_role == Role.SERVER && (!_initReceived || !_done)); |
| 606 |
} |
| 607 |
|
| 608 |
private boolean isOutputInSaslMode() |
| 609 |
{ |
| 610 |
return _role == null || (_role == Role.CLIENT && (!_done || !_initSent)) || (_role == Role.SERVER && !_outputComplete); |
| 611 |
} |
| 612 |
|
| 613 |
@Override |
| 614 |
public int capacity() |
| 615 |
{ |
| 616 |
if (_tail_closed) return Transport.END_OF_STREAM; |
| 617 |
if (isInputInSaslMode()) |
| 618 |
{ |
| 619 |
return _inputBuffer.remaining(); |
| 620 |
} |
| 621 |
else |
| 622 |
{ |
| 623 |
return _underlyingInput.capacity(); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
@Override |
| 628 |
public int position() |
| 629 |
{ |
| 630 |
if (_tail_closed) return Transport.END_OF_STREAM; |
| 631 |
if (isInputInSaslMode()) |
| 632 |
{ |
| 633 |
return _inputBuffer.position(); |
| 634 |
} |
| 635 |
else |
| 636 |
{ |
| 637 |
return _underlyingInput.position(); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
@Override |
| 642 |
public ByteBuffer tail() |
| 643 |
{ |
| 644 |
if (!isInputInSaslMode()) |
| 645 |
{ |
| 646 |
return _underlyingInput.tail(); |
| 647 |
} |
| 648 |
|
| 649 |
return _inputBuffer; |
| 650 |
} |
| 651 |
|
| 652 |
@Override |
| 653 |
public void process() throws TransportException |
| 654 |
{ |
| 655 |
_inputBuffer.flip(); |
| 656 |
|
| 657 |
try |
| 658 |
{ |
| 659 |
reallyProcessInput(); |
| 660 |
} |
| 661 |
finally |
| 662 |
{ |
| 663 |
_inputBuffer.compact(); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
@Override |
| 668 |
public void close_tail() |
| 669 |
{ |
| 670 |
_tail_closed = true; |
| 671 |
if (isInputInSaslMode()) { |
| 672 |
_head_closed = true; |
| 673 |
_underlyingInput.close_tail(); |
| 674 |
} else { |
| 675 |
_underlyingInput.close_tail(); |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
private void reallyProcessInput() throws TransportException |
| 680 |
{ |
| 681 |
if(isInputInSaslMode()) |
| 682 |
{ |
| 683 |
if(_logger.isLoggable(Level.FINER)) |
| 684 |
{ |
| 685 |
_logger.log(Level.FINER, SaslImpl.this + " about to call input."); |
| 686 |
} |
| 687 |
|
| 688 |
_frameParser.input(_inputBuffer); |
| 689 |
} |
| 690 |
|
| 691 |
if(!isInputInSaslMode()) |
| 692 |
{ |
| 693 |
if(_logger.isLoggable(Level.FINER)) |
| 694 |
{ |
| 695 |
_logger.log(Level.FINER, SaslImpl.this + " about to call plain input"); |
| 696 |
} |
| 697 |
|
| 698 |
if (_inputBuffer.hasRemaining()) |
| 699 |
{ |
| 700 |
int bytes = pourAll(_inputBuffer, _underlyingInput); |
| 701 |
if (bytes == Transport.END_OF_STREAM) |
| 702 |
{ |
| 703 |
_tail_closed = true; |
| 704 |
} |
| 705 |
|
| 706 |
if (!_inputBuffer.hasRemaining()) |
| 707 |
{ |
| 708 |
_parent.switchToNextInput(); |
| 709 |
} |
| 710 |
} |
| 711 |
else |
| 712 |
{ |
| 713 |
_parent.switchToNextInput(); |
| 714 |
} |
| 715 |
|
| 716 |
_underlyingInput.process(); |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
@Override |
| 721 |
public int pending() |
| 722 |
{ |
| 723 |
if (isOutputInSaslMode() || _outputBuffer.position() != 0) |
| 724 |
{ |
| 725 |
fillOutputBuffer(); |
| 726 |
_head.limit(_outputBuffer.position()); |
| 727 |
|
| 728 |
if (_head_closed && _outputBuffer.position() == 0) |
| 729 |
{ |
| 730 |
return Transport.END_OF_STREAM; |
| 731 |
} |
| 732 |
else |
| 733 |
{ |
| 734 |
return _outputBuffer.position(); |
| 735 |
} |
| 736 |
} |
| 737 |
else |
| 738 |
{ |
| 739 |
_parent.switchToNextOutput(); |
| 740 |
return _underlyingOutput.pending(); |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
@Override |
| 745 |
public ByteBuffer head() |
| 746 |
{ |
| 747 |
if (isOutputInSaslMode() || _outputBuffer.position() != 0) |
| 748 |
{ |
| 749 |
pending(); |
| 750 |
return _head; |
| 751 |
} |
| 752 |
else |
| 753 |
{ |
| 754 |
_parent.switchToNextOutput(); |
| 755 |
return _underlyingOutput.head(); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
@Override |
| 760 |
public void pop(int bytes) |
| 761 |
{ |
| 762 |
if (isOutputInSaslMode() || _outputBuffer.position() != 0) |
| 763 |
{ |
| 764 |
_outputBuffer.flip(); |
| 765 |
_outputBuffer.position(bytes); |
| 766 |
_outputBuffer.compact(); |
| 767 |
_head.position(0); |
| 768 |
_head.limit(_outputBuffer.position()); |
| 769 |
} |
| 770 |
else |
| 771 |
{ |
| 772 |
_parent.switchToNextOutput(); |
| 773 |
_underlyingOutput.pop(bytes); |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
@Override |
| 778 |
public void close_head() |
| 779 |
{ |
| 780 |
_parent.switchToNextOutput(); |
| 781 |
_underlyingOutput.close_head(); |
| 782 |
} |
| 783 |
|
| 784 |
private void writeSaslOutput() |
| 785 |
{ |
| 786 |
SaslImpl.this.process(); |
| 787 |
_frameWriter.readBytes(_outputBuffer); |
| 788 |
|
| 789 |
if(_logger.isLoggable(Level.FINER)) |
| 790 |
{ |
| 791 |
_logger.log(Level.FINER, "Finished writing SASL output. Output Buffer : " + _outputBuffer); |
| 792 |
} |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
private class SwitchingSaslTransportWrapper implements TransportWrapper { |
| 797 |
|
| 798 |
private final TransportInput _underlyingInput; |
| 799 |
private final TransportOutput _underlyingOutput; |
| 800 |
|
| 801 |
private TransportInput currentInput; |
| 802 |
private TransportOutput currentOutput; |
| 803 |
|
| 804 |
private SwitchingSaslTransportWrapper(TransportInput input, TransportOutput output) { |
| 805 |
_underlyingInput = input; |
| 806 |
_underlyingOutput = output; |
| 807 |
|
| 808 |
// The wrapper can be GC'd after both current's are switched to next. |
| 809 |
SaslTransportWrapper saslProcessor = new SaslTransportWrapper(this, input, output); |
| 810 |
|
| 811 |
currentInput = saslProcessor; |
| 812 |
currentOutput = saslProcessor; |
| 813 |
} |
| 814 |
|
| 815 |
@Override |
| 816 |
public int capacity() { |
| 817 |
return currentInput.capacity(); |
| 818 |
} |
| 819 |
|
| 820 |
@Override |
| 821 |
public int position() { |
| 822 |
return currentInput.position(); |
| 823 |
} |
| 824 |
|
| 825 |
@Override |
| 826 |
public ByteBuffer tail() throws TransportException { |
| 827 |
return currentInput.tail(); |
| 828 |
} |
| 829 |
|
| 830 |
@Override |
| 831 |
public void process() throws TransportException { |
| 832 |
currentInput.process(); |
| 833 |
} |
| 834 |
|
| 835 |
@Override |
| 836 |
public void close_tail() { |
| 837 |
currentInput.close_tail(); |
| 838 |
} |
| 839 |
|
| 840 |
@Override |
| 841 |
public int pending() { |
| 842 |
return currentOutput.pending(); |
| 843 |
} |
| 844 |
|
| 845 |
@Override |
| 846 |
public ByteBuffer head() { |
| 847 |
return currentOutput.head(); |
| 848 |
} |
| 849 |
|
| 850 |
@Override |
| 851 |
public void pop(int bytes) { |
| 852 |
currentOutput.pop(bytes); |
| 853 |
} |
| 854 |
|
| 855 |
@Override |
| 856 |
public void close_head() { |
| 857 |
currentOutput.close_head(); |
| 858 |
} |
| 859 |
|
| 860 |
void switchToNextInput() { |
| 861 |
currentInput = _underlyingInput; |
| 862 |
} |
| 863 |
|
| 864 |
void switchToNextOutput() { |
| 865 |
currentOutput = _underlyingOutput; |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
@Override |
| 870 |
public String getHostname() |
| 871 |
{ |
| 872 |
if(_role != null) |
| 873 |
{ |
| 874 |
checkRole(Role.SERVER); |
| 875 |
} |
| 876 |
|
| 877 |
return _hostname; |
| 878 |
} |
| 879 |
|
| 880 |
@Override |
| 881 |
public void setRemoteHostname(String hostname) |
| 882 |
{ |
| 883 |
if(_role != null) |
| 884 |
{ |
| 885 |
checkRole(Role.CLIENT); |
| 886 |
} |
| 887 |
|
| 888 |
_hostname = hostname; |
| 889 |
} |
| 890 |
|
| 891 |
@Override |
| 892 |
public void setListener(SaslListener saslListener) { |
| 893 |
_saslListener = saslListener; |
| 894 |
} |
| 895 |
} |