/** * Decodes a ASN.1 length. * @param is * an <code>InputStream</code> * @return * the decoded length. * @throws IOException */ public static final int decodeLength(BERInputStream is) throws IOException { return decodeLength(is, true); }
/** * Decodes a ASN.1 length. * @param is * an {@code InputStream} * @return * the decoded length. * @throws IOException * if the input stream contains an invalid BER encoding or an IO * exception occurred while reading from the stream. */ public static int decodeLength(BERInputStream is) throws IOException { return decodeLength(is, true); }
/** * Decodes a ASN.1 length. * @param is * an {@code InputStream} * @return * the decoded length. * @throws IOException * if the input stream contains an invalid BER encoding or an IO * exception occurred while reading from the stream. */ public static int decodeLength(BERInputStream is) throws IOException { return decodeLength(is, true); }
private Object resolvOpaque(Opaque var) { //If not resolved, we will return the data as an array of bytes Object value = var.getValue(); try { byte[] bytesArray = var.getValue(); ByteBuffer bais = ByteBuffer.wrap(bytesArray); BERInputStream beris = new BERInputStream(bais); byte t1 = bais.get(); byte t2 = bais.get(); int l = BER.decodeLength(beris); if(t1 == TAG1) { if(t2 == TAG_FLOAT && l == 4) value = new Float(bais.getFloat()); else if(t2 == TAG_DOUBLE && l == 8) value = new Double(bais.getDouble()); } } catch (IOException e) { logger.error(var.toString()); } return value; }
private Object resolvOpaque(Opaque var) { // If not resolved, we will return the data as an array of bytes Object value = var.getValue(); try { byte[] bytesArray = var.getValue(); ByteBuffer bais = ByteBuffer.wrap(bytesArray); BERInputStream beris = new BERInputStream(bais); byte t1 = bais.get(); byte t2 = bais.get(); int l = BER.decodeLength(beris); if(t1 == TAG1) { if(t2 == TAG_FLOAT && l == 4) value = new Float(bais.getFloat()); else if(t2 == TAG_DOUBLE && l == 8) value = new Double(bais.getDouble()); } } catch (IOException e) { logger.error(var.toString()); } return value; }
public static void decodeNull(BERInputStream is, MutableByte type) throws IOException { // get the type type.setValue((byte)(is.read() & 0xFF)); if ((type.value != (byte)0x05) && (type.value != (byte)0x80) && (type.value != (byte)0x81) && (type.value != (byte)0x82)) { throw new IOException("Wrong ASN.1 type. Is not null: " + type.value+ getPositionMessage(is)); } int length = decodeLength(is); if (length != 0) { throw new IOException("Invalid Null encoding, length is not zero: "+ length+getPositionMessage(is)); } }
public static void decodeNull(BERInputStream is, MutableByte type) throws IOException { // get the type type.setValue((byte)(is.read() & 0xFF)); if ((type.value != (byte)0x05) && (type.value != (byte)0x80) && (type.value != (byte)0x81) && (type.value != (byte)0x82)) { throw new IOException("Wrong ASN.1 type. Is not null: " + type.value+ getPositionMessage(is)); } int length = decodeLength(is); if (length != 0) { throw new IOException("Invalid Null encoding, length is not zero: "+ length+getPositionMessage(is)); } }
public static final void decodeNull(BERInputStream is, MutableByte type) throws IOException { // get the type type.setValue((byte)(is.read() & 0xFF)); if ((type.value != (byte)0x05) && (type.value != (byte)0x80) && (type.value != (byte)0x81) && (type.value != (byte)0x82)) { throw new IOException("Wrong ASN.1 type. Is not null: " + type.value+ getPositionMessage(is)); } int length = decodeLength(is); if (length != 0) { throw new IOException("Invalid Null encoding, length is not zero: "+ length+getPositionMessage(is)); } }
public static BigInteger decodeBigInteger(BERInputStream is, MutableByte type) throws IOException { int length; type.setValue((byte)is.read()); if (type.value != 0x02) { throw new IOException("Wrong ASN.1 type. Not an INTEGER: "+type.value+ getPositionMessage(is)); } length = decodeLength(is); if (length < 0) { throw new IOException("Length greater than "+Integer.MAX_VALUE+" are not supported "+ " for integers: "+getPositionMessage(is)); } byte[] bytes = new byte[length]; int actualRead = is.read(bytes); if (actualRead != length) { throw new IOException("Length of INTEGER ("+length+") is greater than number of bytes left in BER stream: "+ actualRead); } return new BigInteger(bytes); }
public static BigInteger decodeBigInteger(BERInputStream is, MutableByte type) throws IOException { int length; type.setValue((byte)is.read()); if (type.value != 0x02) { throw new IOException("Wrong ASN.1 type. Not an INTEGER: "+type.value+ getPositionMessage(is)); } length = decodeLength(is); if (length < 0) { throw new IOException("Length greater than "+Integer.MAX_VALUE+" are not supported "+ " for integers: "+getPositionMessage(is)); } byte[] bytes = new byte[length]; int actualRead = is.read(bytes); if (actualRead != length) { throw new IOException("Length of INTEGER ("+length+") is greater than number of bytes left in BER stream: "+ actualRead); } return new BigInteger(bytes); }
public static byte[] decodeString(BERInputStream is, MutableByte type) throws IOException { /* * ASN.1 octet string ::= primstring | cmpdstring * primstring ::= 0x04 asnlength byte {byte}* * cmpdstring ::= 0x24 asnlength string {string}* * ipaddress ::= 0x40 4 byte byte byte byte */ // get the type type.setValue((byte)is.read()); if ((type.value != BER.OCTETSTRING) && (type.value != 0x24) && (type.value != BER.IPADDRESS) && (type.value != BER.OPAQUE) && (type.value != BER.BITSTRING) && (type.value != 0x45)) { throw new IOException("Wrong ASN.1 type. Not a string: "+type.value+getPositionMessage(is)); } int length = decodeLength(is); byte[] value = new byte[length]; if (length > 0) { int read = is.read(value, 0, length); if ((read < 0) || (read < length)) { throw new IOException("Wrong string length " + read + " < " + length); } } return value; }
return decodeLength(is, checkLength);
public static byte[] decodeString(BERInputStream is, MutableByte type) throws IOException { /* * ASN.1 octet string ::= primstring | cmpdstring * primstring ::= 0x04 asnlength byte {byte}* * cmpdstring ::= 0x24 asnlength string {string}* * ipaddress ::= 0x40 4 byte byte byte byte */ // get the type type.setValue((byte)is.read()); if ((type.value != BER.OCTETSTRING) && (type.value != 0x24) && (type.value != BER.IPADDRESS) && (type.value != BER.OPAQUE) && (type.value != BER.BITSTRING) && (type.value != 0x45)) { throw new IOException("Wrong ASN.1 type. Not a string: "+type.value+getPositionMessage(is)); } int length = decodeLength(is); byte[] value = new byte[length]; if (length > 0) { int read = is.read(value, 0, length); if ((read < 0) || (read < length)) { throw new IOException("Wrong string length " + read + " < " + length); } } return value; }
(" at position "+inputStream.getPosition())); length = BER.decodeLength(inputStream); if (length > 4) { LOG.debug("Working around invalid Integer32 likely dealing with a permissive Net-SNMP agent");
public static int decodeInteger(BERInputStream is, MutableByte type) throws IOException { int length; int value = 0; type.setValue((byte)is.read()); if ((type.value != 0x02) && (type.value != 0x43) && (type.value != 0x41)) { throw new IOException("Wrong ASN.1 type. Not an integer: "+type.value+ getPositionMessage(is)); } length = decodeLength(is); if (length > 4) { throw new IOException("Length greater than 32bit are not supported "+ " for integers: "+getPositionMessage(is)); } int b = is.read() & 0xFF; if ((b & 0x80) > 0) { value = -1; /* integer is negative */ } while (length-- > 0) { value = (value << 8) | b; if (length > 0) { b = is.read(); } } return value; }
public static final int decodeInteger(BERInputStream is, MutableByte type) throws IOException { int length; int value = 0; type.setValue((byte)is.read()); if ((type.value != 0x02) && (type.value != 0x43) && (type.value != 0x41)) { throw new IOException("Wrong ASN.1 type. Not an integer: "+type.value+ getPositionMessage(is)); } length = decodeLength(is); if (length > 4) { throw new IOException("Length greater than 32bit are not supported "+ " for integers: "+getPositionMessage(is)); } int b = is.read() & 0xFF; if ((b & 0x80) > 0) { value = -1; /* integer is negative */ } while (length-- > 0) { value = (value << 8) | b; if (length > 0) { b = is.read(); } } return value; }
(" at position "+inputStream.getPosition())); length = BER.decodeLength(inputStream); if (length > 4) { LOG.debug("Working around invalid Integer32 likely dealing with a permissive Net-SNMP agent");
public static int decodeInteger(BERInputStream is, MutableByte type) throws IOException { int length; int value = 0; type.setValue((byte)is.read()); if ((type.value != 0x02) && (type.value != 0x43) && (type.value != 0x41)) { throw new IOException("Wrong ASN.1 type. Not an integer: "+type.value+ getPositionMessage(is)); } length = decodeLength(is); if (length > 4) { throw new IOException("Length greater than 32bit are not supported "+ " for integers: "+getPositionMessage(is)); } int b = is.read() & 0xFF; if ((b & 0x80) > 0) { value = -1; /* integer is negative */ } while (length-- > 0) { value = (value << 8) | b; if (length > 0) { b = is.read(); } } return value; }
getPositionMessage(is)); int length = decodeLength(is); int b = is.read() & 0xFF; if (length > 9) {
getPositionMessage(is)); int length = decodeLength(is); int b = is.read() & 0xFF; if (length > 9) {