ASN1InputStreamReader.java revision 6870993d12bf8a2b9d5cd103dc5ccabc42f9bf5d
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2008 Sun Microsystems, Inc.
* Portions copyright 2012-2014 ForgeRock AS.
*/
/**
* An ASN1Reader that reads from an input stream.
*/
final class ASN1InputStreamReader extends AbstractASN1Reader {
private byte peekType = 0;
private int peekLength = -1;
private int lengthBytesNeeded = 0;
private final int maxElementSize;
private InputStream in;
private byte[] buffer;
/**
* Creates a new ASN1 reader whose source is the provided input stream and
* having a user defined maximum BER element size.
*
* @param stream
* The input stream to be read.
* @param maxElementSize
* The maximum BER element size, or <code>0</code> to indicate
* that there is no limit.
*/
this.buffer = new byte[512];
this.maxElementSize = maxElementSize;
}
/** {@inheritDoc} */
public void close() throws IOException {
// Calling close of SizeLimitInputStream should close the parent
// stream.
streamStack.clear();
}
/** {@inheritDoc} */
public boolean elementAvailable() throws IOException {
return false;
}
&& !needFirstLengthByteState(false, false)) {
return false;
}
&& !needAdditionalLengthBytesState(false, false)) {
return false;
}
}
/** {@inheritDoc} */
public boolean hasNextElement() throws IOException {
if (!streamStack.isEmpty()) {
// We are reading a sub sequence. Return true as long as we
// haven't exhausted the size limit for the sub sequence sub input
// stream.
}
}
/** {@inheritDoc} */
public int peekLength() throws IOException {
peekType();
switch (state) {
needFirstLengthByteState(true, true);
break;
needAdditionalLengthBytesState(true, true);
break;
default: // ASN1.ELEMENT_READ_STATE_NEED_VALUE_BYTES
break;
}
return peekLength;
}
/** {@inheritDoc} */
public byte peekType() throws IOException {
needTypeState(true, true);
}
return peekType;
}
/** {@inheritDoc} */
public boolean readBoolean() throws IOException {
// Read the header if haven't done so already
peekLength();
if (peekLength != 1) {
}
if (readByte == -1) {
}
logger.trace("READ ASN.1 BOOLEAN(type=0x%x, length=%d, value=%s)", peekType, peekLength, readByte != 0x00);
return readByte != 0x00;
}
/** {@inheritDoc} */
public void readEndSequence() throws IOException {
if (streamStack.isEmpty()) {
}
// Ignore all unused trailing components.
}
// Reset the state
}
/** {@inheritDoc} */
}
/** {@inheritDoc} */
public void readEndSet() throws IOException {
// From an implementation point of view, a set is equivalent to a
// sequence.
}
/** {@inheritDoc} */
public int readEnumerated() throws IOException {
// Read the header if haven't done so already
peekLength();
}
// From an implementation point of view, an enumerated value is
// equivalent to an integer.
return (int) readInteger();
}
/** {@inheritDoc} */
public long readInteger() throws IOException {
// Read the header if haven't done so already
peekLength();
}
if (peekLength > 4) {
long longValue = 0;
for (int i = 0; i < peekLength; i++) {
if (readByte == -1) {
final LocalizableMessage message =
}
longValue = 0xFFFFFFFFFFFFFFFFL;
}
}
return longValue;
} else {
int intValue = 0;
for (int i = 0; i < peekLength; i++) {
if (readByte == -1) {
final LocalizableMessage message =
}
intValue = 0xFFFFFFFF;
}
}
return intValue;
}
}
/** {@inheritDoc} */
public void readNull() throws IOException {
// Read the header if haven't done so already
peekLength();
// Make sure that the decoded length is exactly zero byte.
if (peekLength != 0) {
}
}
/** {@inheritDoc} */
// Read the header if haven't done so already
peekLength();
if (peekLength == 0) {
return ByteString.empty();
}
// Copy the value and construct the element to return.
final byte[] value = new byte[peekLength];
int bytesNeeded = peekLength;
int bytesRead;
while (bytesNeeded > 0) {
if (bytesRead < 0) {
final LocalizableMessage message =
}
bytesNeeded -= bytesRead;
}
}
/** {@inheritDoc} */
// Read the header if haven't done so already
peekLength();
if (peekLength == 0) {
return builder;
}
// Copy the value and construct the element to return.
int bytesNeeded = peekLength;
int bytesRead;
while (bytesNeeded > 0) {
if (bytesRead < 0) {
final LocalizableMessage message =
}
bytesNeeded -= bytesRead;
}
return builder;
}
/** {@inheritDoc} */
// Read the header if haven't done so already
peekLength();
if (peekLength == 0) {
return "";
}
// Resize the temp buffer if needed
buffer = new byte[peekLength];
}
int bytesNeeded = peekLength;
int bytesRead;
while (bytesNeeded > 0) {
if (bytesRead < 0) {
final LocalizableMessage message =
}
bytesNeeded -= bytesRead;
}
try {
} catch (final UnsupportedEncodingException e) {
// TODO: I18N
throw new RuntimeException("Unable to decode ASN.1 OCTETSTRING bytes as UTF-8 string ", e);
}
return str;
}
/** {@inheritDoc} */
public void readStartSequence() throws IOException {
// Read the header if haven't done so already
peekLength();
// Reset the state
}
/** {@inheritDoc} */
}
/** {@inheritDoc} */
public void readStartSet() throws IOException {
// From an implementation point of view, a set is equivalent to a
// sequence.
}
/** {@inheritDoc} */
// Read the header if haven't done so already
peekLength();
if (bytesSkipped != peekLength) {
}
return this;
}
/**
* Internal helper method reading the additional ASN.1 length bytes and
* transition to the next state if successful.
*
* @param isBlocking
* <code>true</code> to block if the type byte is not available
* or <code>false</code> to check for availability first.
* @param throwEofException
* <code>true</code> to throw an exception when an EOF is
* encountered or <code>false</code> to return false.
* @return <code>true</code> if the length bytes was successfully read.
* @throws IOException
* If an error occurs while reading from the stream.
*/
private boolean needAdditionalLengthBytesState(final boolean isBlocking,
final boolean throwEofException) throws IOException {
return false;
}
int readByte;
while (lengthBytesNeeded > 0) {
if (readByte == -1) {
if (throwEofException) {
final LocalizableMessage message =
}
return false;
}
}
// Make sure that the element is not larger than the maximum allowed
// message size.
final LocalizableMessage message =
}
return true;
}
/**
* Internal helper method reading the first length bytes and transition to
* the next state if successful.
*
* @param isBlocking
* <code>true</code> to block if the type byte is not available
* or <code>false</code> to check for availability first.
* @param throwEofException
* <code>true</code> to throw an exception when an EOF is
* encountered or <code>false</code> to return false.
* @return <code>true</code> if the length bytes was successfully read
* @throws IOException
* If an error occurs while reading from the stream.
*/
private boolean needFirstLengthByteState(final boolean isBlocking,
final boolean throwEofException) throws IOException {
return false;
}
if (readByte == -1) {
if (throwEofException) {
}
return false;
}
if (peekLength != readByte) {
if (lengthBytesNeeded > 4) {
final LocalizableMessage message =
}
peekLength = 0x00;
return false;
}
while (lengthBytesNeeded > 0) {
if (readByte == -1) {
if (throwEofException) {
final LocalizableMessage message =
}
return false;
}
}
}
// Make sure that the element is not larger than the maximum allowed
// message size.
final LocalizableMessage message =
}
return true;
}
/**
* Internal helper method reading the ASN.1 type byte and transition to the
* next state if successful.
*
* @param isBlocking
* <code>true</code> to block if the type byte is not available
* or <code>false</code> to check for availability first.
* @param throwEofException
* <code>true</code> to throw an exception when an EOF is
* encountered or <code>false</code> to return false.
* @return <code>true</code> if the type byte was successfully read
* @throws IOException
* If an error occurs while reading from the stream.
*/
throws IOException {
// Read just the type.
return false;
}
if (type == -1) {
if (throwEofException) {
}
return false;
}
return true;
}
}