LDAP Password Modify Extended Operation - Data Decoding Error

I'm trying to use LDAPs Password Modify Extended Operation, but I'm having difficulty. I'm hoping someone here can assist. I'm not very familiar with ASN.1 BER encoding, I'm struggling with it. I'm using the JNDI booster pack to handle ASN.1 BER encoding / decoding

http://docs.sun.com/source/816-5618-10/netscape/ldap/ber/stream/package-summary.html

I basically have to send the following request to LDAP:

ExtendedRequest ::= [APPLICATION 23] SEQUENCE{

requestName[0] LDAPOID,

requestValue[1] OCTET STRING OPTIONAL}

The requestName is a dotted-decimal representation of the OBJECT IDENTIFIER corresponding to the request. The requestValue is information in a form defined by that request, encapsulated inside an OCTET STRING.

passwdModifyOID OBJECT IDENTIFIER ::= 1.3.6.1.4.1.4203.1.11.1

The request value is:

PasswdModifyRequestValue ::= SEQUENCE{

userIdentity[0] OCTET STRING OPTIONAL

oldPasswd[1] OCTET STRING OPTIONAL

newPasswd[2] OCTET STRING OPTIONAL}

encapsulated inside an OCTET STRING

This is the code that doesn't work..

publicbyte[] getEncodedValue ()

{

final BERSequence vSeq =new BERSequence();

vSeq.addElement(new BERTag( BERTag.CONTEXT | 0,new BEROctetString( username ),true ) );

vSeq.addElement(new BERTag( BERTag.CONTEXT | 1,new BEROctetString( oldPassword ),true ) );

vSeq.addElement(new BERTag( BERTag.CONTEXT | 2,new BEROctetString( newPassword ),true ) );

final BERSequence seq =new BERSequence();

seq.addElement(new BERTag( BERTag.CONTEXT | 0,new BERObjectId( OID ),true ) );

seq.addElement(new BERTag( BERTag.CONTEXT | 1,new BEROctetString( vSeq.toString() ),true ) );

// ExtendedRequest ::== [APPLICATION 23] SEQUENCE

BERTag extendedRequest =new BERTag( BERTag.APPLICATION | BERTag.CONSTRUCTED | 23, seq,true );

return extendedRequest.toString().getBytes();

}

Any help appreciated,

Thanks

Tony

[3020 byte] By [tony_murphya] at [2007-10-3 2:51:12]
# 1

Problem fixed, solution below

/**

* Retrieves the ASN.1 BER encoded value of the LDAP extended operation request.

*

* @return encoded value of password modify extended request

*/

public byte[] getEncodedValue ()

{

LOG.info( "ASN.1 BER encoding password modify request to be sent to LDAP. " );

final BERSequence vSeq = new BERSequence();

vSeq.addElement( new BERTag( BERTag.CONTEXT | 0, new BEROctetString( "uid=" + username + ",ou=people,dc=example,dc=com" ), true ) );

vSeq.addElement( new BERTag( BERTag.CONTEXT | 1, new BEROctetString( oldPassword ), true ) );

vSeq.addElement( new BERTag( BERTag.CONTEXT | 2, new BEROctetString( newPassword ), true ) );

return flattenBER( vSeq );

}

/**

* Create a "flattened" BER encoding from a BER,

* and return it as a byte array.

* @param ber a BER encoded sequence

* @return the byte array of encoded data.

*/

private byte[] flattenBER( BERSequence ber ) {

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

try {

ber.write( outStream );

} catch ( IOException e ) {

return null;

}

return outStream.toByteArray();

}

}

SuperHoopsa at 2007-7-14 20:40:04 > top of Java-index,Core,Core APIs...