Search in sources :

Example 41 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project undertow by undertow-io.

the class ALPNHackSSLEngine method unwrap.

@Override
public SSLEngineResult unwrap(ByteBuffer dataToUnwrap, ByteBuffer[] byteBuffers, int i, int i1) throws SSLException {
    if (!unwrapHelloSeen) {
        if (!delegate.getUseClientMode() && applicationProtocols != null) {
            try {
                List<String> result = ALPNHackClientHelloExplorer.exploreClientHello(dataToUnwrap.duplicate());
                if (result != null) {
                    for (String protocol : applicationProtocols) {
                        if (result.contains(protocol)) {
                            selectedApplicationProtocol = protocol;
                            break;
                        }
                    }
                }
                unwrapHelloSeen = true;
            } catch (BufferUnderflowException e) {
                return new SSLEngineResult(SSLEngineResult.Status.BUFFER_UNDERFLOW, SSLEngineResult.HandshakeStatus.NEED_UNWRAP, 0, 0);
            }
        } else if (delegate.getUseClientMode() && ALPNHackClientByteArrayOutputStream != null) {
            if (!dataToUnwrap.hasRemaining()) {
                return delegate.unwrap(dataToUnwrap, byteBuffers, i, i1);
            }
            try {
                ByteBuffer dup = dataToUnwrap.duplicate();
                int type = dup.get();
                int major = dup.get();
                int minor = dup.get();
                if (type == 22 && major == 3 && minor == 3) {
                    //we only care about TLS 1.2
                    //split up the records, there may be multiple when doing a fast session resume
                    List<ByteBuffer> records = ALPNHackServerHelloExplorer.extractRecords(dataToUnwrap.duplicate());
                    //this will be the handshake record
                    ByteBuffer firstRecord = records.get(0);
                    final AtomicReference<String> alpnResult = new AtomicReference<>();
                    ByteBuffer dupFirst = firstRecord.duplicate();
                    dupFirst.position(firstRecord.position() + 5);
                    ByteBuffer firstLessFraming = dupFirst.duplicate();
                    byte[] result = ALPNHackServerHelloExplorer.removeAlpnExtensionsFromServerHello(dupFirst, alpnResult);
                    firstLessFraming.limit(dupFirst.position());
                    unwrapHelloSeen = true;
                    if (result != null) {
                        selectedApplicationProtocol = alpnResult.get();
                        int newFirstRecordLength = result.length + dupFirst.remaining();
                        byte[] newFirstRecord = new byte[newFirstRecordLength];
                        System.arraycopy(result, 0, newFirstRecord, 0, result.length);
                        dupFirst.get(newFirstRecord, result.length, dupFirst.remaining());
                        dataToUnwrap.position(dataToUnwrap.limit());
                        byte[] originalFirstRecord = new byte[firstLessFraming.remaining()];
                        firstLessFraming.get(originalFirstRecord);
                        ByteBuffer newData = ALPNHackServerHelloExplorer.createNewOutputRecords(newFirstRecord, records);
                        dataToUnwrap.clear();
                        dataToUnwrap.put(newData);
                        dataToUnwrap.flip();
                        ALPNHackClientByteArrayOutputStream.setReceivedServerHello(originalFirstRecord);
                    }
                }
            } catch (BufferUnderflowException e) {
                return new SSLEngineResult(SSLEngineResult.Status.BUFFER_UNDERFLOW, SSLEngineResult.HandshakeStatus.NEED_UNWRAP, 0, 0);
            }
        }
    }
    SSLEngineResult res = delegate.unwrap(dataToUnwrap, byteBuffers, i, i1);
    if (!delegate.getUseClientMode() && selectedApplicationProtocol != null && alpnHackServerByteArrayOutputStream == null) {
        alpnHackServerByteArrayOutputStream = replaceServerByteOutput(delegate, selectedApplicationProtocol);
    }
    return res;
}
Also used : SSLEngineResult(javax.net.ssl.SSLEngineResult) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 42 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project undertow by undertow-io.

the class ALPNHackClientHelloExplorer method rewriteClientHello.

static byte[] rewriteClientHello(byte[] source, List<String> alpnProtocols) throws SSLException {
    ByteBuffer input = ByteBuffer.wrap(source);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // Do we have a complete header?
    if (input.remaining() < RECORD_HEADER_SIZE) {
        throw new BufferUnderflowException();
    }
    try {
        // Is it a handshake message?
        byte firstByte = input.get();
        byte secondByte = input.get();
        byte thirdByte = input.get();
        out.write(firstByte & 0xFF);
        out.write(secondByte & 0xFF);
        out.write(thirdByte & 0xFF);
        if ((firstByte & 0x80) != 0 && thirdByte == 0x01) {
            // looks like a V2ClientHello, we ignore it.
            return null;
        } else if (firstByte == 22) {
            // 22: handshake record
            if (secondByte == 3 && thirdByte == 3) {
                //TLS1.2 is the only one we care about. Previous versions can't use HTTP/2, newer versions won't be backported to JDK8
                exploreTLSRecord(input, firstByte, secondByte, thirdByte, alpnProtocols, out);
                //we need to adjust the record length;
                int clientHelloLength = out.size() - 9;
                byte[] data = out.toByteArray();
                int newLength = data.length - 5;
                data[3] = (byte) ((newLength >> 8) & 0xFF);
                data[4] = (byte) (newLength & 0xFF);
                //now we need to adjust the handshake frame length
                data[6] = (byte) ((clientHelloLength >> 16) & 0xFF);
                data[7] = (byte) ((clientHelloLength >> 8) & 0xFF);
                data[8] = (byte) (clientHelloLength & 0xFF);
                return data;
            }
            return null;
        } else {
            throw UndertowMessages.MESSAGES.notHandshakeRecord();
        }
    } catch (ALPNPresentException e) {
        return null;
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 43 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project gora by apache.

the class SpecificFixedSerializer method fromByteBuffer.

@Override
public SpecificFixed fromByteBuffer(ByteBuffer byteBuffer) {
    if (byteBuffer == null) {
        return null;
    }
    Object value = null;
    try {
        value = clazz.newInstance();
    } catch (InstantiationException ie) {
        LOG.warn("Instantiation error for class=" + clazz, ie);
        return null;
    } catch (IllegalAccessException iae) {
        LOG.warn("Illegal access error for class=" + clazz, iae);
        return null;
    }
    if (!(value instanceof SpecificFixed)) {
        LOG.warn("Not an instance of SpecificFixed");
        return null;
    }
    SpecificFixed fixed = (SpecificFixed) value;
    byte[] bytes = fixed.bytes();
    try {
        byteBuffer.get(bytes, 0, bytes.length);
    } catch (BufferUnderflowException e) {
        // LOG.info(e.toString() + " : class=" + clazz.getName() + " length=" + bytes.length);
        throw e;
    }
    fixed.bytes(bytes);
    return fixed;
}
Also used : SpecificFixed(org.apache.avro.specific.SpecificFixed) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 44 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project geode by apache.

the class ByteSourceJUnitTest method testGetChar.

@Test
public void testGetChar() {
    ByteBuffer bb = ByteBuffer.allocate(10);
    CharBuffer cb = bb.asCharBuffer();
    cb.put("abcde");
    byte[] bytes = bb.array();
    ByteSource bs = createByteSource(bytes);
    char c = bs.getChar();
    assertEquals('a', c);
    assertEquals(2, bs.position());
    c = bs.getChar();
    assertEquals('b', c);
    assertEquals(4, bs.position());
    bs.position(8);
    c = bs.getChar();
    assertEquals('e', c);
    assertEquals(10, bs.position());
    try {
        bs.getChar();
        fail("expected BufferUnderflowException");
    } catch (BufferUnderflowException expected) {
    }
}
Also used : CharBuffer(java.nio.CharBuffer) ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 45 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project geode by apache.

the class ByteSourceJUnitTest method testGetLong.

@Test
public void testGetLong() {
    ByteBuffer bb = ByteBuffer.allocate(40);
    LongBuffer lb = bb.asLongBuffer();
    lb.put(0x1110223344556677L);
    lb.put(0x2220334455667788L);
    lb.put(0x3330445566778899L);
    lb.put(0x4440556677889900L);
    lb.put(0x55506677889900AAL);
    byte[] bytes = bb.array();
    ByteSource bs = createByteSource(bytes);
    long l = bs.getLong();
    assertEquals(0x1110223344556677L, l);
    assertEquals(8, bs.position());
    l = bs.getLong();
    assertEquals(0x2220334455667788L, l);
    assertEquals(16, bs.position());
    bs.position(4 * 8);
    l = bs.getLong();
    assertEquals(0x55506677889900AAL, l);
    assertEquals(40, bs.position());
    try {
        bs.getLong();
        fail("expected BufferUnderflowException");
    } catch (BufferUnderflowException expected) {
    }
}
Also used : LongBuffer(java.nio.LongBuffer) ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

BufferUnderflowException (java.nio.BufferUnderflowException)123 ByteBuffer (java.nio.ByteBuffer)70 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)22 DirectByteBuffer (java.nio.DirectByteBuffer)15 Test (org.junit.Test)14 CertificateException (java.security.cert.CertificateException)12 X509Certificate (java.security.cert.X509Certificate)11 BigInteger (java.math.BigInteger)10 ByteSource (org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource)9 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)9 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)9 UnitTest (org.apache.geode.test.junit.categories.UnitTest)9 CharBuffer (java.nio.CharBuffer)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 FloatBuffer (java.nio.FloatBuffer)6 CertificateFactory (java.security.cert.CertificateFactory)6 HashMap (java.util.HashMap)6 ArrayMap (android.util.ArrayMap)5 HSIconFileElement (com.android.anqp.HSIconFileElement)5