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()][]);
}
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);
}
}
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;
}
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;
}
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());
}
}
}
Aggregations