Skip to content

Commit a7725ad

Browse files
committed
Added a fix for ASN1 PDU with negative length; JIRA DIRAPI-401
1 parent 4d47eb4 commit a7725ad

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

asn1/ber/src/main/java/org/apache/directory/api/asn1/ber/Asn1Decoder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,9 @@ else if ( ( octet & TLV.LENGTH_EXTENSION_RESERVED ) != TLV.LENGTH_EXTENSION_RESE
246246
* the result and other informations.
247247
* @return <code>true</code> if there are more bytes to read, <code>false
248248
* </code> otherwise
249+
* @throws DecoderException Thrown if anything went wrong
249250
*/
250-
private static boolean treatLengthPendingState( ByteBuffer stream, Asn1Container container )
251+
private static boolean treatLengthPendingState( ByteBuffer stream, Asn1Container container ) throws DecoderException
251252
{
252253
if ( stream.hasRemaining() )
253254
{
@@ -265,6 +266,13 @@ private static boolean treatLengthPendingState( ByteBuffer stream, Asn1Container
265266

266267
tlv.incLengthBytesRead();
267268
length = ( length << 8 ) | ( octet & 0x00FF );
269+
270+
if ( length < 0 )
271+
{
272+
String msg = I18n.err( I18n.ERR_01002_TLV_NULL );
273+
LOG.error( msg );
274+
throw new DecoderException( msg );
275+
}
268276

269277
if ( !stream.hasRemaining() )
270278
{

i18n/src/main/java/org/apache/directory/api/i18n/I18n.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public enum I18n
5555
ERR_01308_ZERO_LENGTH_TLV( "ERR_01308_ZERO_LENGTH_TLV" ),
5656
ERR_01309_EMPTY_TLV( "ERR_01309_EMPTY_TLV" ),
5757
ERR_01310_INTEGER_DECODING_ERROR( "ERR_01310_INTEGER_DECODING_ERROR" ),
58+
ERR_01311_NEGATIVE_LENGTH( "ERR_01311_NEGATIVE_LENGTH" ),
5859

5960
// actions 1100 - 1199
6061
ERR_01100_INCORRECT_LENGTH( "ERR_01100_INCORRECT_LENGTH" ),

i18n/src/main/resources/org/apache/directory/api/i18n/errors.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ERR_01307_0_BYTES_LONG_LONG=The value is 0 byte long. This is not allowed for a
5959
ERR_01308_ZERO_LENGTH_TLV=The TLV has a zero length. This is not allowed
6060
ERR_01309_EMPTY_TLV=The LdapMessage should not be empty
6161
ERR_01310_INTEGER_DECODING_ERROR=The integer cannot be decoded: {0}
62-
62+
ERR_01311_NEGATIVE_LENGTH=The length should not be negative
6363

6464
# asn1-codec
6565
ERR_01001=Encoded result is not a ByteBuffer: {0}

ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/LdapMessageTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.directory.api.ldap.codec.api.LdapEncoder;
3737
import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
3838
import org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest;
39+
import org.apache.directory.api.ldap.model.exception.LdapURLEncodingException;
3940
import org.apache.directory.api.ldap.model.message.Message;
4041
import org.apache.directory.api.ldap.model.message.UnbindRequest;
4142
import org.junit.jupiter.api.Test;
@@ -270,4 +271,28 @@ public void testDecodeUnBindRequestNoControls() throws DecoderException, Encoder
270271

271272
assertArrayEquals( stream.array(), result.array() );
272273
}
274+
275+
276+
/**
277+
* test a negative length
278+
*/
279+
@Test
280+
public void testNegativeLength() throws LdapURLEncodingException
281+
{
282+
String base64Bytes = String.join("", "CoT/gwr/Jg==");
283+
284+
byte[] input = java.util.Base64.getDecoder().decode(base64Bytes);
285+
286+
ByteBuffer stream = ByteBuffer.allocate(input.length);
287+
stream.put(input);
288+
stream.flip();
289+
290+
org.apache.directory.api.ldap.codec.api.LdapApiService codec = new org.apache.directory.api.ldap.codec.osgi.DefaultLdapCodecService();
291+
LdapMessageContainer<Message> container = new LdapMessageContainer<>(codec);
292+
293+
assertThrows( DecoderException.class, ( ) ->
294+
{
295+
Asn1Decoder.decode(stream, container);
296+
} );
297+
}
273298
}

0 commit comments

Comments
 (0)