Search in sources :

Example 11 with ReadOnlyBufferException

use of java.nio.ReadOnlyBufferException in project robovm by robovm.

the class SSLEngineTest method test_unwrap_ByteBuffer$ByteBuffer_02.

/**
     * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
     * ReadOnlyBufferException should be thrown.
     */
@KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
public void test_unwrap_ByteBuffer$ByteBuffer_02() {
    String host = "new host";
    int port = 8080;
    ByteBuffer bbs = ByteBuffer.allocate(10);
    ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer();
    ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
    SSLEngine sse = getEngine(host, port);
    sse.setUseClientMode(true);
    try {
        sse.unwrap(bbs, bbA);
        fail("ReadOnlyBufferException wasn't thrown");
    } catch (ReadOnlyBufferException iobe) {
    //expected
    } catch (Exception e) {
        fail(e + " was thrown instead of ReadOnlyBufferException");
    }
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) SSLEngine(javax.net.ssl.SSLEngine) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) SSLException(javax.net.ssl.SSLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ReadOnlyBufferException(java.nio.ReadOnlyBufferException) KnownFailure(dalvik.annotation.KnownFailure)

Example 12 with ReadOnlyBufferException

use of java.nio.ReadOnlyBufferException in project tomcat by apache.

the class OpenSSLEngine method wrap.

@Override
public synchronized SSLEngineResult wrap(final ByteBuffer[] srcs, final int offset, final int length, final ByteBuffer dst) throws SSLException {
    // Check to make sure the engine has not been closed
    if (destroyed) {
        return new SSLEngineResult(SSLEngineResult.Status.CLOSED, SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING, 0, 0);
    }
    // Throw required runtime exceptions
    if (srcs == null || dst == null) {
        throw new IllegalArgumentException(sm.getString("engine.nullBuffer"));
    }
    if (offset >= srcs.length || offset + length > srcs.length) {
        throw new IndexOutOfBoundsException(sm.getString("engine.invalidBufferArray", Integer.toString(offset), Integer.toString(length), Integer.toString(srcs.length)));
    }
    if (dst.isReadOnly()) {
        throw new ReadOnlyBufferException();
    }
    // Prepare OpenSSL to work in server mode and receive handshake
    if (accepted == 0) {
        beginHandshakeImplicitly();
    }
    // In handshake or close_notify stages, check if call to wrap was made
    // without regard to the handshake status.
    SSLEngineResult.HandshakeStatus handshakeStatus = getHandshakeStatus();
    if ((!handshakeFinished || engineClosed) && handshakeStatus == SSLEngineResult.HandshakeStatus.NEED_UNWRAP) {
        return new SSLEngineResult(getEngineStatus(), SSLEngineResult.HandshakeStatus.NEED_UNWRAP, 0, 0);
    }
    int bytesProduced = 0;
    int pendingNet;
    // Check for pending data in the network BIO
    pendingNet = SSL.pendingWrittenBytesInBIO(networkBIO);
    if (pendingNet > 0) {
        // Do we have enough room in destination to write encrypted data?
        int capacity = dst.remaining();
        if (capacity < pendingNet) {
            return new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, handshakeStatus, 0, 0);
        }
        // Write the pending data from the network BIO into the dst buffer
        try {
            bytesProduced = readEncryptedData(dst, pendingNet);
        } catch (Exception e) {
            throw new SSLException(e);
        }
        // for the receipt the peer's close_notify message -- shutdown.
        if (isOutboundDone) {
            shutdown();
        }
        return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), 0, bytesProduced);
    }
    // There was no pending data in the network BIO -- encrypt any application data
    int bytesConsumed = 0;
    int endOffset = offset + length;
    for (int i = offset; i < endOffset; ++i) {
        final ByteBuffer src = srcs[i];
        if (src == null) {
            throw new IllegalArgumentException(sm.getString("engine.nullBufferInArray"));
        }
        while (src.hasRemaining()) {
            // Write plain text application data to the SSL engine
            try {
                bytesConsumed += writePlaintextData(src);
            } catch (Exception e) {
                throw new SSLException(e);
            }
            // Check to see if the engine wrote data into the network BIO
            pendingNet = SSL.pendingWrittenBytesInBIO(networkBIO);
            if (pendingNet > 0) {
                // Do we have enough room in dst to write encrypted data?
                int capacity = dst.remaining();
                if (capacity < pendingNet) {
                    return new SSLEngineResult(SSLEngineResult.Status.BUFFER_OVERFLOW, getHandshakeStatus(), bytesConsumed, bytesProduced);
                }
                // Write the pending data from the network BIO into the dst buffer
                try {
                    bytesProduced += readEncryptedData(dst, pendingNet);
                } catch (Exception e) {
                    throw new SSLException(e);
                }
                return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), bytesConsumed, bytesProduced);
            }
        }
    }
    return new SSLEngineResult(getEngineStatus(), getHandshakeStatus(), bytesConsumed, bytesProduced);
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) SSLEngineResult(javax.net.ssl.SSLEngineResult) SSLException(javax.net.ssl.SSLException) ByteBuffer(java.nio.ByteBuffer) SSLException(javax.net.ssl.SSLException) CertificateException(javax.security.cert.CertificateException) ReadOnlyBufferException(java.nio.ReadOnlyBufferException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException)

Example 13 with ReadOnlyBufferException

use of java.nio.ReadOnlyBufferException in project buck by facebook.

the class ObjectFileScrubbers method createDateUidGidScrubber.

public static FileContentsScrubber createDateUidGidScrubber(final PaddingStyle paddingStyle) {
    return new FileContentsScrubber() {

        /**
       * Efficiently modifies the archive backed by the given buffer to remove any non-deterministic
       * meta-data such as timestamps, UIDs, and GIDs.
       */
        @SuppressWarnings("PMD.AvoidUsingOctalValues")
        @Override
        public void scrubFile(FileChannel file) throws IOException, ScrubException {
            try {
                ByteBuffer header = ByteBuffer.allocate(GLOBAL_HEADER_SIZE);
                file.read(header);
                // Grab the global header chunk and verify it's accurate.
                header.position(0);
                byte[] globalHeader = getBytes(header, GLOBAL_HEADER_SIZE);
                boolean thin = checkHeader(globalHeader);
                // Iterate over all the file meta-data entries, injecting zero's for timestamp,
                // UID, and GID.
                final int entrySize = 16 + /* fileName */
                12 + /* file modification time */
                6 + /* owner ID */
                6 + /* group ID */
                8 + /* file mode */
                10 + /* file size */
                2;
                long start = GLOBAL_HEADER_SIZE;
                ByteBuffer buffer = ByteBuffer.allocate(entrySize);
                while (start < file.size()) {
                    checkArchive(file.size() - start >= entrySize, "Invalid entry metadata format");
                    buffer.clear();
                    file.position(start);
                    int read = file.read(buffer);
                    checkArchive(read == entrySize, "Not all bytes have been read");
                    // position points just past the last byte read, so need to reset
                    buffer.position(0);
                    String fileName = new String(getBytes(buffer, 16), Charsets.US_ASCII).trim();
                    // Inject 0's for the non-deterministic meta-data entries.
                    /* File modification timestamp */
                    putIntAsDecimalString(buffer, 12, ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP, paddingStyle);
                    /* Owner ID */
                    putIntAsDecimalString(buffer, 6, 0, paddingStyle);
                    /* Group ID */
                    putIntAsDecimalString(buffer, 6, 0, paddingStyle);
                    /* File mode */
                    putIntAsOctalString(buffer, 8, 0100644, paddingStyle);
                    long fileSize = getDecimalStringAsLong(buffer, 10);
                    // Lastly, grab the file magic entry and verify it's accurate.
                    byte[] fileMagic = getBytes(buffer, 2);
                    checkArchive(Arrays.equals(END_OF_FILE_HEADER_MARKER, fileMagic), "invalid file magic");
                    // write the changes
                    // position points just past the last byte accessed, need to reset
                    buffer.position(0);
                    file.position(start);
                    int written = file.write(buffer);
                    checkArchive(written == entrySize, "Not all bytes have been written");
                    // Skip the file data.
                    start += entrySize;
                    if (!thin || SPECIAL_ENTRIES.contains(fileName)) {
                        start += fileSize + fileSize % 2;
                    }
                }
            // Convert any low-level exceptions to `ArchiveExceptions`s.
            } catch (BufferUnderflowException | ReadOnlyBufferException e) {
                throw new ScrubException(e.getMessage());
            }
        }
    };
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) FileContentsScrubber(com.facebook.buck.io.FileContentsScrubber) FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 14 with ReadOnlyBufferException

use of java.nio.ReadOnlyBufferException in project j2objc by google.

the class BufferTest method testPutByteBuffer.

// http://code.google.com/p/android/issues/detail?id=16184
public void testPutByteBuffer() throws Exception {
    ByteBuffer dst = ByteBuffer.allocate(10).asReadOnlyBuffer();
    // Can't put into a read-only buffer.
    try {
        dst.put(ByteBuffer.allocate(5));
        fail();
    } catch (ReadOnlyBufferException expected) {
    }
    // Can't put a buffer into itself.
    dst = ByteBuffer.allocate(10);
    try {
        dst.put(dst);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    // Can't put the null ByteBuffer.
    try {
        dst.put((ByteBuffer) null);
        fail();
    } catch (NullPointerException expected) {
    }
    // Can't put a larger source into a smaller destination.
    try {
        dst.put(ByteBuffer.allocate(dst.capacity() + 1));
        fail();
    } catch (BufferOverflowException expected) {
    }
    assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocate(8), false);
    assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocateDirect(8), false);
    assertPutByteBuffer(ByteBuffer.allocate(10), allocateMapped(8), false);
    assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocate(8), true);
    assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocateDirect(8), true);
    assertPutByteBuffer(ByteBuffer.allocate(10), allocateMapped(8), true);
    assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocate(8), false);
    assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocateDirect(8), false);
    assertPutByteBuffer(ByteBuffer.allocateDirect(10), allocateMapped(8), false);
    assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocate(8), true);
    assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocateDirect(8), true);
    assertPutByteBuffer(ByteBuffer.allocateDirect(10), allocateMapped(8), true);
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 15 with ReadOnlyBufferException

use of java.nio.ReadOnlyBufferException in project netty by netty.

the class AbstractByteBufTest method testGetReadOnlyDst.

private void testGetReadOnlyDst(boolean direct) {
    byte[] bytes = { 'a', 'b', 'c', 'd' };
    ByteBuf buffer = newBuffer(bytes.length);
    buffer.writeBytes(bytes);
    ByteBuffer dst = direct ? ByteBuffer.allocateDirect(bytes.length) : ByteBuffer.allocate(bytes.length);
    ByteBuffer readOnlyDst = dst.asReadOnlyBuffer();
    try {
        buffer.getBytes(0, readOnlyDst);
        fail();
    } catch (ReadOnlyBufferException e) {
    // expected
    }
    assertEquals(0, readOnlyDst.position());
    buffer.release();
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ReadOnlyBufferException (java.nio.ReadOnlyBufferException)35 ByteBuffer (java.nio.ByteBuffer)22 SSLException (javax.net.ssl.SSLException)10 IOException (java.io.IOException)7 SSLEngineResult (javax.net.ssl.SSLEngineResult)7 SSLEngine (javax.net.ssl.SSLEngine)6 BufferOverflowException (java.nio.BufferOverflowException)5 KeyManagementException (java.security.KeyManagementException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 CharBuffer (java.nio.CharBuffer)4 KnownFailure (dalvik.annotation.KnownFailure)3 BufferUnderflowException (java.nio.BufferUnderflowException)3 ByteBuf (io.netty.buffer.ByteBuf)2 MappedByteBuffer (java.nio.MappedByteBuffer)2 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)2 Cipher (javax.crypto.Cipher)2 ShortBufferException (javax.crypto.ShortBufferException)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)2