Search in sources :

Example 91 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project walle by Meituan-Dianping.

the class V2SchemeVerifier method getByteBuffer.

/**
     * Relative <em>get</em> method for reading {@code size} number of bytes from the current
     * position of this buffer.
     *
     * <p>This method reads the next {@code size} bytes at this buffer's current position,
     * returning them as a {@code ByteBuffer} with start set to 0, limit and capacity set to
     * {@code size}, byte order set to this buffer's byte order; and then increments the position by
     * {@code size}.
     */
public static ByteBuffer getByteBuffer(ByteBuffer source, int size) throws BufferUnderflowException {
    if (size < 0) {
        throw new IllegalArgumentException("size: " + size);
    }
    int originalLimit = source.limit();
    int position = source.position();
    int limit = position + size;
    if ((limit < position) || (limit > originalLimit)) {
        throw new BufferUnderflowException();
    }
    source.limit(limit);
    try {
        ByteBuffer result = source.slice();
        result.order(source.order());
        source.position(limit);
        return result;
    } finally {
        source.limit(originalLimit);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 92 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project walle by Meituan-Dianping.

the class ApkUtil method getByteBuffer.

/**
     * Relative <em>get</em> method for reading {@code size} number of bytes from the current
     * position of this buffer.
     * <p>
     * <p>This method reads the next {@code size} bytes at this buffer's current position,
     * returning them as a {@code ByteBuffer} with start set to 0, limit and capacity set to
     * {@code size}, byte order set to this buffer's byte order; and then increments the position by
     * {@code size}.
     */
private static ByteBuffer getByteBuffer(final ByteBuffer source, final int size) throws BufferUnderflowException {
    if (size < 0) {
        throw new IllegalArgumentException("size: " + size);
    }
    final int originalLimit = source.limit();
    final int position = source.position();
    final int limit = position + size;
    if ((limit < position) || (limit > originalLimit)) {
        throw new BufferUnderflowException();
    }
    source.limit(limit);
    try {
        final ByteBuffer result = source.slice();
        result.order(source.order());
        source.position(limit);
        return result;
    } finally {
        source.limit(originalLimit);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 93 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project platform_frameworks_base by android.

the class ApkSignatureSchemeV2Verifier method verify.

/**
     * Verifies the contents of the provided APK file against the provided APK Signature Scheme v2
     * Block.
     *
     * @param signatureInfo APK Signature Scheme v2 Block and information relevant for verifying it
     *        against the APK file.
     */
private static X509Certificate[][] verify(FileDescriptor apkFileDescriptor, SignatureInfo signatureInfo) throws SecurityException {
    int signerCount = 0;
    Map<Integer, byte[]> contentDigests = new ArrayMap<>();
    List<X509Certificate[]> signerCerts = new ArrayList<>();
    CertificateFactory certFactory;
    try {
        certFactory = CertificateFactory.getInstance("X.509");
    } catch (CertificateException e) {
        throw new RuntimeException("Failed to obtain X.509 CertificateFactory", e);
    }
    ByteBuffer signers;
    try {
        signers = getLengthPrefixedSlice(signatureInfo.signatureBlock);
    } catch (IOException e) {
        throw new SecurityException("Failed to read list of signers", e);
    }
    while (signers.hasRemaining()) {
        signerCount++;
        try {
            ByteBuffer signer = getLengthPrefixedSlice(signers);
            X509Certificate[] certs = verifySigner(signer, contentDigests, certFactory);
            signerCerts.add(certs);
        } catch (IOException | BufferUnderflowException | SecurityException e) {
            throw new SecurityException("Failed to parse/verify signer #" + signerCount + " block", e);
        }
    }
    if (signerCount < 1) {
        throw new SecurityException("No signers found");
    }
    if (contentDigests.isEmpty()) {
        throw new SecurityException("No content digests found");
    }
    verifyIntegrity(contentDigests, apkFileDescriptor, signatureInfo.apkSigningBlockOffset, signatureInfo.centralDirOffset, signatureInfo.eocdOffset, signatureInfo.eocd);
    return signerCerts.toArray(new X509Certificate[signerCerts.size()][]);
}
Also used : ArrayList(java.util.ArrayList) ArrayMap(android.util.ArrayMap) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) DirectByteBuffer(java.nio.DirectByteBuffer) ByteBuffer(java.nio.ByteBuffer) X509Certificate(java.security.cert.X509Certificate) BigInteger(java.math.BigInteger) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 94 with BufferUnderflowException

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

the class ALPNHackClientHelloExplorer method exploreClientHello.

/**
     *
     *
     */
static List<String> exploreClientHello(ByteBuffer source) throws SSLException {
    ByteBuffer input = source.duplicate();
    // Do we have a complete header?
    if (input.remaining() < RECORD_HEADER_SIZE) {
        throw new BufferUnderflowException();
    }
    List<String> alpnProtocols = new ArrayList<>();
    // Is it a handshake message?
    byte firstByte = input.get();
    byte secondByte = input.get();
    byte thirdByte = input.get();
    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 >= 1 && thirdByte <= 3) {
            exploreTLSRecord(input, firstByte, secondByte, thirdByte, alpnProtocols, null);
            return alpnProtocols;
        }
        return null;
    } else {
        throw UndertowMessages.MESSAGES.notHandshakeRecord();
    }
}
Also used : ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 95 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project mongo-java-driver by mongodb.

the class ObjectId method createMachineIdentifier.

private static int createMachineIdentifier() {
    // build a 2-byte machine piece based on NICs info
    int machinePiece;
    try {
        StringBuilder sb = new StringBuilder();
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface ni = e.nextElement();
            sb.append(ni.toString());
            byte[] mac = ni.getHardwareAddress();
            if (mac != null) {
                ByteBuffer bb = ByteBuffer.wrap(mac);
                try {
                    sb.append(bb.getChar());
                    sb.append(bb.getChar());
                    sb.append(bb.getChar());
                } catch (BufferUnderflowException shortHardwareAddressException) {
                //NOPMD
                // mac with less than 6 bytes. continue
                }
            }
        }
        machinePiece = sb.toString().hashCode();
    } catch (Throwable t) {
        // exception sometimes happens with IBM JVM, use random
        machinePiece = (new SecureRandom().nextInt());
        LOGGER.log(Level.WARNING, "Failed to get machine identifier from network interface, using random number instead", t);
    }
    machinePiece = machinePiece & LOW_ORDER_THREE_BYTES;
    return machinePiece;
}
Also used : NetworkInterface(java.net.NetworkInterface) SecureRandom(java.security.SecureRandom) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

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