Search in sources :

Example 66 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project android_frameworks_base by crdroidandroid.

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 67 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project android_frameworks_base by crdroidandroid.

the class ApkSignatureSchemeV2Verifier 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}.
     */
private 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 : DirectByteBuffer(java.nio.DirectByteBuffer) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 68 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project opennms by OpenNMS.

the class TrivialTimeMonitor method pollTimeTcp.

/**
 * <p>pollTimeTcp</p>
 *
 * @param svc a {@link org.opennms.netmgt.poller.MonitoredService} object.
 * @param parameters a {@link java.util.Map} object.
 * @param serviceStatus a {@link org.opennms.netmgt.poller.PollStatus} object.
 * @param tracker a {@link org.opennms.core.utils.TimeoutTracker} object.
 * @param ipv4Addr a {@link java.net.InetAddress} object.
 * @param port a int.
 * @param allowedSkew a int.
 * @param persistSkew a boolean.
 * @return a {@link org.opennms.netmgt.poller.PollStatus} object.
 */
public PollStatus pollTimeTcp(MonitoredService svc, Map<String, Object> parameters, PollStatus serviceStatus, TimeoutTracker tracker, InetAddress ipv4Addr, int port, int allowedSkew, boolean persistSkew) {
    int localTime = 0;
    int remoteTime = 0;
    boolean gotTime = false;
    for (tracker.reset(); tracker.shouldRetry() && !gotTime; tracker.nextAttempt()) {
        Socket socket = null;
        try {
            tracker.startAttempt();
            socket = new Socket();
            socket.connect(new InetSocketAddress(ipv4Addr, port), tracker.getConnectionTimeout());
            socket.setSoTimeout(tracker.getSoTimeout());
            LOG.debug("Connected to host: {} on TCP port: {}", ipv4Addr, port);
            // 
            // Try to read from the socket
            // 
            byte[] timeBytes = new byte[4];
            ByteBuffer timeByteBuffer = ByteBuffer.wrap(timeBytes);
            int bytesRead = socket.getInputStream().read(timeBytes);
            if (bytesRead != 4)
                continue;
            LOG.debug("pollTimeTcp: bytes read = {}", bytesRead);
            try {
                remoteTime = timeByteBuffer.getInt();
            } catch (BufferUnderflowException bue) {
                LOG.error("Encountered buffer underflow while reading time from remote socket.");
                remoteTime = 0;
                serviceStatus = PollStatus.unavailable("Failed to read a valid time from remote host.");
                // to next iteration of for() loop
                continue;
            }
            localTime = (int) (System.currentTimeMillis() / 1000) - EPOCH_ADJ_FACTOR;
            gotTime = true;
            serviceStatus = qualifyTime(remoteTime, localTime, allowedSkew, serviceStatus, tracker.elapsedTimeInMillis(), persistSkew);
        } catch (NoRouteToHostException e) {
            String reason = "No route to host exception for address " + InetAddressUtils.str(ipv4Addr);
            LOG.debug(reason, e);
            serviceStatus = PollStatus.unavailable(reason);
        } catch (InterruptedIOException e) {
            String reason = "did not connect to host with " + tracker;
            LOG.debug(reason);
            serviceStatus = PollStatus.unavailable(reason);
        } catch (ConnectException e) {
            String reason = "Connection exception for address: " + ipv4Addr;
            LOG.debug(reason, e);
            serviceStatus = PollStatus.unavailable(reason);
        } catch (IOException e) {
            String reason = "IOException while polling address: " + ipv4Addr;
            LOG.debug(reason, e);
            serviceStatus = PollStatus.unavailable(reason);
        } finally {
            try {
                // Close the socket
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.fillInStackTrace();
                LOG.debug("pollTimeTcp: Error closing socket.", e);
            }
        }
    }
    return serviceStatus;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ByteBuffer(java.nio.ByteBuffer) NoRouteToHostException(java.net.NoRouteToHostException) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) BufferUnderflowException(java.nio.BufferUnderflowException) ConnectException(java.net.ConnectException)

Example 69 with BufferUnderflowException

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

the class AbstractCommand method getFirstLine.

protected String getFirstLine() {
    CharBuffer buffer = firstLineBuffer.get();
    StringBuilder builder = new StringBuilder();
    try {
        char c = buffer.get();
        for (; ; ) {
            builder.append(c);
            if (c == N) {
                break;
            }
            c = buffer.get();
        }
    } catch (BufferUnderflowException e) {
        throw new ClientError("error reading command:" + builder.toString());
    }
    String firstLine = builder.toString();
    if (getLogger().fineEnabled()) {
        getLogger().fine("gemcached command:" + firstLine);
    }
    return firstLine;
}
Also used : CharBuffer(java.nio.CharBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 70 with BufferUnderflowException

use of java.nio.BufferUnderflowException in project jdk8u_jdk by JetBrains.

the class Type1Font method verifyPFB.

private void verifyPFB(ByteBuffer bb) throws FontFormatException {
    int pos = 0;
    while (true) {
        try {
            int segType = bb.getShort(pos) & 0xffff;
            if (segType == 0x8001 || segType == 0x8002) {
                bb.order(ByteOrder.LITTLE_ENDIAN);
                int segLen = bb.getInt(pos + 2);
                bb.order(ByteOrder.BIG_ENDIAN);
                if (segLen <= 0) {
                    throw new FontFormatException("bad segment length");
                }
                pos += segLen + 6;
            } else if (segType == 0x8003) {
                return;
            } else {
                throw new FontFormatException("bad pfb file");
            }
        } catch (BufferUnderflowException bue) {
            throw new FontFormatException(bue.toString());
        } catch (Exception e) {
            throw new FontFormatException(e.toString());
        }
    }
}
Also used : FontFormatException(java.awt.FontFormatException) BufferUnderflowException(java.nio.BufferUnderflowException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BufferUnderflowException(java.nio.BufferUnderflowException) FontFormatException(java.awt.FontFormatException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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