use of java.nio.ReadOnlyBufferException in project XobotOS by xamarin.
the class SSLEngineImpl method unwrap.
/**
* Decodes one complete SSL/TLS record provided in the source buffer.
* If decoded record contained application data, this data will
* be placed in the destination buffers.
* For more information about TLS record fragmentation see
* TLS v 1 specification (http://www.ietf.org/rfc/rfc2246.txt) p 6.2.
* @param src source buffer containing SSL/TLS record.
* @param dsts destination buffers to place received application data.
* @see javax.net.ssl.SSLEngine#unwrap(ByteBuffer,ByteBuffer[],int,int)
* method documentation for more information
*/
@Override
public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length) throws SSLException {
if (engine_was_shutteddown) {
return new SSLEngineResult(SSLEngineResult.Status.CLOSED, SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING, 0, 0);
}
if ((src == null) || (dsts == null)) {
throw new IllegalStateException("Some of the input parameters are null");
}
if (!handshake_started) {
beginHandshake();
}
SSLEngineResult.HandshakeStatus handshakeStatus = getHandshakeStatus();
// check if this call was made in spite of handshake status
if ((session == null || engine_was_closed) && (handshakeStatus.equals(SSLEngineResult.HandshakeStatus.NEED_WRAP) || handshakeStatus.equals(SSLEngineResult.HandshakeStatus.NEED_TASK))) {
return new SSLEngineResult(getEngineStatus(), handshakeStatus, 0, 0);
}
if (src.remaining() < recordProtocol.getMinRecordSize()) {
return new SSLEngineResult(SSLEngineResult.Status.BUFFER_UNDERFLOW, getHandshakeStatus(), 0, 0);
}
try {
src.mark();
// check the destination buffers and count their capacity
int capacity = 0;
for (int i = offset; i < offset + length; i++) {
if (dsts[i] == null) {
throw new IllegalStateException("Some of the input parameters are null");
}
if (dsts[i].isReadOnly()) {
throw new ReadOnlyBufferException();
}
capacity += dsts[i].remaining();
}
if (capacity < recordProtocol.getDataSize(src.remaining())) {
return new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, getHandshakeStatus(), 0, 0);
}
recProtIS.setSourceBuffer(src);
// unwrap the record contained in source buffer, pass it
// to appropriate client protocol (alert, handshake, or app)
// and retrieve the type of unwrapped data
int type = recordProtocol.unwrap();
// process the data and return the result
switch(type) {
case ContentType.HANDSHAKE:
case ContentType.CHANGE_CIPHER_SPEC:
if (handshakeProtocol.getStatus().equals(SSLEngineResult.HandshakeStatus.FINISHED)) {
session = recordProtocol.getSession();
}
break;
case ContentType.APPLICATION_DATA:
break;
case ContentType.ALERT:
if (alertProtocol.isFatalAlert()) {
alertProtocol.setProcessed();
if (session != null) {
session.invalidate();
}
String description = "Fatal alert received " + alertProtocol.getAlertDescription();
shutdown();
throw new SSLException(description);
} else {
if (logger != null) {
logger.println("Warning allert has been received: " + alertProtocol.getAlertDescription());
}
switch(alertProtocol.getDescriptionCode()) {
case AlertProtocol.CLOSE_NOTIFY:
alertProtocol.setProcessed();
close_notify_was_received = true;
if (!close_notify_was_sent) {
closeOutbound();
closeInbound();
} else {
closeInbound();
shutdown();
}
break;
case AlertProtocol.NO_RENEGOTIATION:
alertProtocol.setProcessed();
if (session == null) {
// handshake
throw new AlertException(AlertProtocol.HANDSHAKE_FAILURE, new SSLHandshakeException("Received no_renegotiation " + "during the initial handshake"));
} else {
// just stop the handshake
handshakeProtocol.stop();
}
break;
default:
alertProtocol.setProcessed();
}
}
break;
}
return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), recProtIS.consumed(), // and get the number of produced bytes:
appData.placeTo(dsts, offset, length));
} catch (BufferUnderflowException e) {
// there was not enought data ource buffer to make complete packet
src.reset();
return new SSLEngineResult(SSLEngineResult.Status.BUFFER_UNDERFLOW, getHandshakeStatus(), 0, 0);
} catch (AlertException e) {
// fatal alert occured
alertProtocol.alert(AlertProtocol.FATAL, e.getDescriptionCode());
engine_was_closed = true;
src.reset();
if (session != null) {
session.invalidate();
}
// to another peer (by wrap method)
throw e.getReason();
} catch (SSLException e) {
throw e;
} catch (IOException e) {
alertProtocol.alert(AlertProtocol.FATAL, AlertProtocol.INTERNAL_ERROR);
engine_was_closed = true;
// to another peer (by wrap method)
throw new SSLException(e.getMessage());
}
}
use of java.nio.ReadOnlyBufferException in project robovm by robovm.
the class CipherTest method test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer.
public void test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer() throws Exception {
byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
ByteBuffer bInput = ByteBuffer.allocate(64);
ByteBuffer bOutput = ByteBuffer.allocate(64);
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
bInput.put(b, 0, 10);
try {
c.doFinal(bInput, bOutput);
fail();
} catch (IllegalBlockSizeException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
try {
c.doFinal(bInput, bOutput);
fail();
} catch (IllegalStateException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
bInput = ByteBuffer.allocate(16);
bInput.put(b, 0, 16);
int len = c.doFinal(bInput, bOutput);
assertEquals(0, len);
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
bInput = ByteBuffer.allocate(64);
try {
c.doFinal(bOutput, bInput);
fail();
} catch (BadPaddingException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
bInput.put(b, 0, 16);
try {
c.doFinal(bInput, bInput);
fail();
} catch (IllegalArgumentException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
bInput.put(b, 0, 16);
try {
c.doFinal(bInput, bOutput.asReadOnlyBuffer());
fail();
} catch (ReadOnlyBufferException expected) {
}
bInput.rewind();
bInput.put(b, 0, 16);
bOutput = ByteBuffer.allocate(8);
c = Cipher.getInstance("DESede");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
try {
c.doFinal(bInput, bOutput);
fail();
} catch (ShortBufferException expected) {
}
}
use of java.nio.ReadOnlyBufferException in project robovm by robovm.
the class SSLEngineTest method test_wrap_ByteBuffer_ByteBuffer_02.
/**
* javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst)
* ReadOnlyBufferException should be thrown.
*/
public void test_wrap_ByteBuffer_ByteBuffer_02() {
String host = "new host";
int port = 8080;
ByteBuffer bbs = ByteBuffer.allocate(10);
ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer();
SSLEngine sse = getEngine(host, port);
sse.setUseClientMode(true);
try {
sse.wrap(bbs, bbd);
fail("ReadOnlyBufferException wasn't thrown");
} catch (ReadOnlyBufferException iobe) {
//expected
} catch (Exception e) {
fail(e + " was thrown instead of ReadOnlyBufferException");
}
}
use of java.nio.ReadOnlyBufferException in project robovm by robovm.
the class SSLEngineTest method test_wrap_03.
/**
* javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
* int length, ByteBuffer dst)
* Exception case: ReadOnlyBufferException should be thrown.
*/
public void test_wrap_03() throws SSLException {
String host = "new host";
int port = 8080;
ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer();
ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) };
SSLEngine sse = getEngine(host, port);
sse.setUseClientMode(true);
try {
sse.wrap(bbA, 0, bbA.length, bb);
fail("ReadOnlyBufferException wasn't thrown");
} catch (ReadOnlyBufferException iobe) {
//expected
}
}
use of java.nio.ReadOnlyBufferException in project robovm by robovm.
the class SSLEngineTest method test_wrap_ByteBuffer$ByteBuffer_02.
/**
* javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
* ReadOnlyBufferException should be thrown.
*/
public void test_wrap_ByteBuffer$ByteBuffer_02() {
String host = "new host";
int port = 8080;
ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer();
ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) };
SSLEngine sse = getEngine(host, port);
sse.setUseClientMode(true);
try {
sse.wrap(bbA, bb);
fail("ReadOnlyBufferException wasn't thrown");
} catch (ReadOnlyBufferException iobe) {
//expected
} catch (Exception e) {
fail(e + " was thrown instead of ReadOnlyBufferException");
}
}
Aggregations