private byte[] decodeUncheckedStringAsBytes() throws ASN1Exception { int length = readLength(); byte[] result = new byte[length]; if ((length != 0) && (bi.drain(result, 0, length) != length)) { throw log.asnUnexpectedEndOfInput(); } return result; }
private void endConstructedElement(long nextElementIndex) throws ASN1Exception { long pos = bi.getIndex(); if (pos < nextElementIndex) { // Any elements in this constructed element that have not yet been read will be discarded int i; for (i = 0; i < (nextElementIndex - pos) && bi.hasNext(); i++) { bi.next(); } if (i != (nextElementIndex - pos)) { throw log.asnUnexpectedEndOfInput(); } } else if (pos > nextElementIndex) { // Shouldn't happen throw new IllegalStateException(); } }
@Override public byte[] decodeOctetString() throws ASN1Exception { readTag(OCTET_STRING_TYPE); int length = readLength(); byte[] result = new byte[length]; if ((length != 0) && (bi.drain(result, 0, length) != length)) { throw log.asnUnexpectedEndOfInput(); } return result; }
@Override public byte[] drainElementValue() throws ASN1Exception { if (implicitTag != -1) { implicitTag = -1; } readTag(); int length = readLength(); byte[] value = new byte[length]; if ((length != 0) && (bi.drain(value) != length)) { throw log.asnUnexpectedEndOfInput(); } return value; }
@Override public String decodeOctetStringAsString(String charSet) throws ASN1Exception { readTag(OCTET_STRING_TYPE); int length = readLength(); byte[] octets = new byte[length]; if ((length != 0) && (bi.drain(octets, 0, length) != length)) { throw log.asnUnexpectedEndOfInput(); } try { return new String(octets, charSet); } catch (UnsupportedEncodingException e) { throw new ASN1Exception(e); } }
@Override public void skipElement() throws ASN1Exception { readTag(); int length = readLength(); int i; for (i = 0; i < length && bi.hasNext(); i++) { bi.next(); } if (i != length) { throw log.asnUnexpectedEndOfInput(); } }
private int readLength() throws ASN1Exception { try { int length = bi.next(); if (length > 127) { // Long form int numOctets = length & 0x7f; if (numOctets > 4) { throw log.asnLengthEncodingExceeds4bytes(); } length = 0; int nextOctet; for (int i = 0; i < numOctets; i++) { nextOctet = bi.next(); length = (length << 8) + nextOctet; } } return length; } catch (NoSuchElementException e) { throw log.asnUnexpectedEndOfInput(); } }
@Override public boolean decodeBoolean() throws ASN1Exception { readTag(BOOLEAN_TYPE); int length = readLength(); if (length != 1) { throw log.asnInvalidLengthForBooleanTypeTag(); } if (! bi.hasNext()) { throw log.asnUnexpectedEndOfInput(); } return bi.next() != BOOLEAN_FALSE; }
@Override public byte[] decodePrintableStringAsBytes() throws ASN1Exception { readTag(PRINTABLE_STRING_TYPE); final int length = readLength(); int c = 0; byte[] result = new byte[length]; while (bi.hasNext() && c < length) { final int b = bi.next(); validatePrintableByte(b); result[c++] = (byte) b; } if (c < length) { throw log.asnUnexpectedEndOfInput(); } return result; }
private int readTag() throws ASN1Exception { try { int tag = bi.next(); int tagClass = tag & CLASS_MASK; int constructed = tag & CONSTRUCTED_MASK; int tagNumber = tag & TAG_NUMBER_MASK; if (tagNumber == 0x1f) { // High-tag-number form tagNumber = 0; int octet = bi.next(); if ((octet & 0x7f) == 0) { // Bits 7 to 1 of the first subsequent octet cannot be 0 throw log.asnInvalidHighTagNumberForm(); } while ((octet >= 0) && ((octet & 0x80) != 0)) { tagNumber |= (octet & 0x7f); tagNumber <<= 7; octet = bi.next(); } tagNumber |= (octet & 0x7f); } return (tagClass | constructed | tagNumber); } catch (NoSuchElementException e) { throw log.asnUnexpectedEndOfInput(); } }
@Override public byte[] drainElement() throws ASN1Exception { if (implicitTag != -1) { implicitTag = -1; } long currOffset = bi.getIndex(); readTag(); int valueLength = readLength(); int length = (int) ((bi.getIndex() - currOffset) + valueLength); while ((bi.getIndex() != currOffset) && bi.hasPrevious()) { bi.previous(); } byte[] element = new byte[length]; if ((length != 0) && (bi.drain(element) != length)) { throw log.asnUnexpectedEndOfInput(); } return element; }