Search in sources :

Example 91 with TLV

use of org.apache.directory.api.asn1.ber.tlv.TLV in project directory-ldap-api by apache.

the class Asn1Decoder method treatTagStartState.

/**
 * Treat the start of a TLV. It reads the tag and get its value.
 *
 * @param stream The ByteBuffer containing the PDU to decode
 * @param container The container that stores the current state,
 * the result and other informations.
 * @return <code>true</code> if there are more bytes to read, <code>false
 * </code> otherwise
 */
private boolean treatTagStartState(ByteBuffer stream, Asn1Container container) {
    if (stream.hasRemaining()) {
        byte octet = stream.get();
        TLV tlv = new TLV(container.getNewTlvId());
        tlv.setTag(octet);
        // Store the current TLV in the container.
        container.setCurrentTLV(tlv);
        // Create a link between the current TLV with its parent
        tlv.setParent(container.getParentTLV());
        // Switch to the next state, which is the Length decoding
        container.setState(TLVStateEnum.LENGTH_STATE_START);
        if (IS_DEBUG) {
            byte tag = container.getCurrentTLV().getTag();
            LOG.debug(I18n.msg(I18n.MSG_01000_TAG_DECODED, Asn1StringUtils.dumpByte(tag)));
        }
        return MORE;
    } else {
        // The stream has been exhausted
        return END;
    }
}
Also used : TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 92 with TLV

use of org.apache.directory.api.asn1.ber.tlv.TLV in project directory-ldap-api by apache.

the class Asn1Decoder method treatLengthStartState.

/**
 * Treat the Length start. The tag has been decoded, so we have to deal with
 * the LENGTH, which can be multi-bytes.
 *
 * @param stream  The ByteBuffer containing the PDU to decode
 * @param container The container that stores the current state,
 * the result and other informations.
 * @return <code>true</code> if there are more bytes to read, <code>false
 * </code> otherwise
 * @throws DecoderException Thrown if anything went wrong
 */
private boolean treatLengthStartState(ByteBuffer stream, Asn1Container container) throws DecoderException {
    if (stream.hasRemaining()) {
        byte octet = stream.get();
        TLV tlv = container.getCurrentTLV();
        if ((octet & TLV.LENGTH_LONG_FORM) == 0) {
            // We don't have a long form. The Length of the Value part is
            // given by this byte.
            tlv.setLength(octet);
            tlv.setLengthNbBytes(1);
            container.setState(TLVStateEnum.LENGTH_STATE_END);
        } else if ((octet & TLV.LENGTH_EXTENSION_RESERVED) != TLV.LENGTH_EXTENSION_RESERVED) {
            int expectedLength = octet & TLV.LENGTH_SHORT_MASK;
            if (expectedLength > 4) {
                String msg = I18n.err(I18n.ERR_01000_LENGTH_OVERFLOW);
                LOG.error(msg);
                throw new DecoderException(msg);
            }
            tlv.setLength(0);
            tlv.setLengthNbBytes(1 + expectedLength);
            tlv.setLengthBytesRead(1);
            container.setState(TLVStateEnum.LENGTH_STATE_PENDING);
        } else {
            String msg = I18n.err(I18n.ERR_01001_LENGTH_EXTENSION_RESERVED);
            LOG.error(msg);
            throw new DecoderException(msg);
        }
        return MORE;
    } else {
        return END;
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 93 with TLV

use of org.apache.directory.api.asn1.ber.tlv.TLV in project directory-ldap-api by apache.

the class AbstractReadOctetString method action.

/**
 * {@inheritDoc}
 */
@Override
public final void action(C container) throws DecoderException {
    TLV tlv = container.getCurrentTLV();
    // The Length should not be null
    if ((tlv.getLength() == 0) && (!canBeNull)) {
        String msg = I18n.err(I18n.ERR_01101_NULL_LENGTH);
        LOG.error(msg);
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(msg);
    }
    BerValue value = tlv.getValue();
    // The data should not be null
    if ((value.getData() == null) && (!canBeNull)) {
        String msg = I18n.err(I18n.ERR_01101_NULL_LENGTH);
        LOG.error(msg);
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(msg);
    }
    setOctetString(value.getData(), container);
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 94 with TLV

use of org.apache.directory.api.asn1.ber.tlv.TLV in project directory-ldap-api by apache.

the class AbstractContainer method updateParent.

/**
 * {@inheritDoc}
 */
@Override
public void updateParent() {
    TLV parentTlv = tlv.getParent();
    while ((parentTlv != null) && (parentTlv.getExpectedLength() == 0)) {
        parentTlv = parentTlv.getParent();
    }
    this.parentTLV = parentTlv;
}
Also used : TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 95 with TLV

use of org.apache.directory.api.asn1.ber.tlv.TLV in project directory-ldap-api by apache.

the class LdapDecoderTest method testDecodeBadLengthTooSmall.

/**
 * Test the decoding of a PDU with a bad Length. The first TLV has a length
 * of 0x32 when the PDU is 0x33 bytes long.
 */
@Test
public void testDecodeBadLengthTooSmall() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x35);
    stream.put(new byte[] { // Length should be 0x33...
    0x30, // LDAPMessage ::=SEQUENCE {
    0x32, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x2E, // BindRequest ::= APPLICATION[0] SEQUENCE {
    0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, // name LDAPDN,
    0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', (byte) 0x80, // authentication
    0x08, // ...
    'p', 'a', 's', 's', 'w', 'o', 'r', 'd' });
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<MessageDecorator<? extends Message>> ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a BindRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertEquals("ERR_01003_VALUE_LENGTH_ABOVE_EXPECTED_LENGTH The current Value length 48 is above the expected length 47", de.getMessage());
        return;
    }
    fail("Should never reach this point..");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

TLV (org.apache.directory.api.asn1.ber.tlv.TLV)92 DecoderException (org.apache.directory.api.asn1.DecoderException)54 BerValue (org.apache.directory.api.asn1.ber.tlv.BerValue)19 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)17 ResponseCarryingException (org.apache.directory.api.ldap.codec.api.ResponseCarryingException)12 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)10 Dn (org.apache.directory.api.ldap.model.name.Dn)10 IntegerDecoderException (org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException)9 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)7 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)6 BooleanDecoderException (org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException)5 Filter (org.apache.directory.api.ldap.codec.search.Filter)5 SubstringFilter (org.apache.directory.api.ldap.codec.search.SubstringFilter)5 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)5 LdapResult (org.apache.directory.api.ldap.model.message.LdapResult)5 ResultResponse (org.apache.directory.api.ldap.model.message.ResultResponse)5 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)4 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)4 AddRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator)4 ModifyRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator)4