Search in sources :

Example 1 with TLV

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

the class LdapDecoderTest method testDecodeBadTagTransition.

/**
 * Test the decoding of a PDU with a bad primitive Length. The second TLV
 * has a length of 0x02 when the PDU is 0x01 bytes long.
 */
@Test
public void testDecodeBadTagTransition() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x35);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x33, // Length should be 0x01...
    0x02, 0x01, // messageID MessageID
    0x01, 0x2D, // CHOICE { ..., bindRequest BindRequest, ...
    0x2D, // 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
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a BindRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertEquals("ERR_01200_BAD_TRANSITION_FROM_STATE Bad transition from state MESSAGE_ID_STATE, tag 0x2D", 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) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) 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)

Example 2 with TLV

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

the class LdapDecoderTest method testDecodeBadPrimitiveLengthTooBig.

/**
 * Test the decoding of a PDU with a bad primitive Length. The second TLV
 * has a length of 0x02 when the PDU is 0x01 bytes long.
 */
@Test
public void testDecodeBadPrimitiveLengthTooBig() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x35);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x33, // Length should be 0x01...
    0x02, 0x02, // 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 AuthenticationChoice
    0x08, // ...
    'p', 'a', 's', 's', 'w', 'o', 'r' });
    stream.flip();
    // Allocate a LdapMessage Container
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a BindRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertEquals("ERR_01200_BAD_TRANSITION_FROM_STATE Bad transition from state MESSAGE_ID_STATE, tag 0x2E", 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) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) 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)

Example 3 with TLV

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

the class InitReferrals method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<MessageDecorator<? extends Message>> container) throws DecoderException {
    TLV tlv = container.getCurrentTLV();
    // If we hae a Referrals sequence, then it should not be empty
    if (tlv.getLength() == 0) {
        String msg = I18n.err(I18n.ERR_04011);
        LOG.error(msg);
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(msg);
    }
    ResultResponse response = (ResultResponse) container.getMessage();
    LdapResult ldapResult = response.getLdapResult();
    Referral referral = new ReferralImpl();
    ldapResult.setReferral(referral);
    if (IS_DEBUG) {
        LOG.debug("Initialising a referrals list");
    }
}
Also used : ResultResponse(org.apache.directory.api.ldap.model.message.ResultResponse) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapResult(org.apache.directory.api.ldap.model.message.LdapResult) Referral(org.apache.directory.api.ldap.model.message.Referral) ReferralImpl(org.apache.directory.api.ldap.model.message.ReferralImpl) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 4 with TLV

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

the class StoreErrorMessage method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<MessageDecorator<? extends Message>> container) throws DecoderException {
    // Get the Value and store it in the BindResponse
    TLV tlv = container.getCurrentTLV();
    String errorMessage;
    // message
    if (tlv.getLength() == 0) {
        errorMessage = "";
    } else {
        errorMessage = Strings.utf8ToString(tlv.getValue().getData());
    }
    ResultResponse response = (ResultResponse) container.getMessage();
    LdapResult ldapResult = response.getLdapResult();
    ldapResult.setDiagnosticMessage(errorMessage);
    // We can have an END transition
    container.setGrammarEndAllowed(true);
    if (IS_DEBUG) {
        LOG.debug("The error message is : " + errorMessage);
    }
}
Also used : ResultResponse(org.apache.directory.api.ldap.model.message.ResultResponse) LdapResult(org.apache.directory.api.ldap.model.message.LdapResult) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 5 with TLV

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

the class StoreMatchedDN method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<MessageDecorator<? extends Message>> container) throws DecoderException {
    // Get the Value and store it in the BindResponse
    TLV tlv = container.getCurrentTLV();
    Dn matchedDn;
    ResultCodeEnum resultCode;
    ResultResponse response = (ResultResponse) container.getMessage();
    LdapResult ldapResult = response.getLdapResult();
    resultCode = ldapResult.getResultCode();
    // Dn
    if (tlv.getLength() == 0) {
        matchedDn = Dn.EMPTY_DN;
    } else {
        switch(resultCode) {
            case NO_SUCH_OBJECT:
            case ALIAS_PROBLEM:
            case INVALID_DN_SYNTAX:
            case ALIAS_DEREFERENCING_PROBLEM:
                byte[] dnBytes = tlv.getValue().getData();
                String dnStr = Strings.utf8ToString(dnBytes);
                try {
                    matchedDn = new Dn(dnStr);
                } catch (LdapInvalidDnException ine) {
                    // This is for the client side. We will never decode LdapResult on the server
                    String msg = I18n.err(I18n.ERR_04013, dnStr, Strings.dumpBytes(dnBytes), ine.getLocalizedMessage());
                    LOG.error(msg);
                    throw new DecoderException(I18n.err(I18n.ERR_04014, ine.getLocalizedMessage()), ine);
                }
                break;
            default:
                LOG.warn("The matched Dn should not be set when the result code is not one of NoSuchObject," + " AliasProblem, InvalidDNSyntax or AliasDreferencingProblem");
                matchedDn = Dn.EMPTY_DN;
                break;
        }
    }
    if (IS_DEBUG) {
        LOG.debug("The matchedDn is " + matchedDn);
    }
    ldapResult.setMatchedDn(matchedDn);
}
Also used : ResultResponse(org.apache.directory.api.ldap.model.message.ResultResponse) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapResult(org.apache.directory.api.ldap.model.message.LdapResult) Dn(org.apache.directory.api.ldap.model.name.Dn) TLV(org.apache.directory.api.asn1.ber.tlv.TLV) ResultCodeEnum(org.apache.directory.api.ldap.model.message.ResultCodeEnum) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

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