Fixes problems related to the processing of padding in incoming stun messages
This commit is contained in:
parent
8c446d6f24
commit
8a17ce9e25
@ -98,6 +98,13 @@ public class StackProperties
|
||||
public static final String ALWAYS_SIGN
|
||||
= "org.ice4j.ALWAYS_SIGN";
|
||||
|
||||
/**
|
||||
* Tells the stack whether to reject all incoming requests that do not
|
||||
* carry a MESSAGE-INTEGRITY header.
|
||||
*/
|
||||
public static final String REQUIRE_MESSAGE_INTEGRITY
|
||||
= "org.ice4j.REQUIRE_MESSAGE_INTEGRITY";
|
||||
|
||||
/**
|
||||
* Returns the String value of the specified property (minus all
|
||||
* encompassing whitespaces)and null in case no property value was mapped
|
||||
|
||||
@ -45,11 +45,6 @@ public class AttributeDecoder
|
||||
char attributeType = (char)((bytes[offset]<<8)|bytes[offset + 1]);
|
||||
char attributeLength = (char)((bytes[offset + 2]<<8)|bytes[offset + 3]);
|
||||
|
||||
if((attributeLength % 4) > 0)
|
||||
{
|
||||
attributeLength += (4 - (attributeLength % 4));
|
||||
}
|
||||
|
||||
if(attributeLength > bytes.length - offset )
|
||||
throw new StunException( StunException.ILLEGAL_ARGUMENT,
|
||||
"Could not decode the specified binary array.");
|
||||
|
||||
@ -6,6 +6,8 @@
|
||||
*/
|
||||
package org.ice4j.attribute;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.ice4j.*;
|
||||
|
||||
/**
|
||||
@ -350,10 +352,8 @@ public class ErrorCodeAttribute extends Attribute
|
||||
* offset is equal to the index of the first byte after
|
||||
* length)
|
||||
* @param length the length of the binary array.
|
||||
* @throws StunException if attrubteValue contains invalid data.
|
||||
*/
|
||||
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws
|
||||
StunException
|
||||
void decodeAttributeBody(byte[] attributeValue, char offset, char length)
|
||||
{
|
||||
|
||||
offset += 2; //skip the 0s
|
||||
@ -363,13 +363,11 @@ public class ErrorCodeAttribute extends Attribute
|
||||
setErrorNumber(attributeValue[offset++]);
|
||||
|
||||
//Reason Phrase
|
||||
char reasonPhrase[] = new char[(length-4)/2];
|
||||
byte[] reasonBytes = new byte[length - 4];
|
||||
|
||||
for (int i = 0; i < reasonPhrase.length; i++, offset+=2) {
|
||||
reasonPhrase[i] =
|
||||
(char)(attributeValue[offset] | attributeValue[offset+1]);
|
||||
}
|
||||
setReasonPhrase(new String(reasonPhrase).trim());
|
||||
System.arraycopy(attributeValue, offset, reasonBytes,
|
||||
0, reasonBytes.length);
|
||||
setReasonPhrase(new String(reasonBytes));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -702,6 +702,13 @@ public abstract class Message
|
||||
|
||||
message.addAttribute(att);
|
||||
offset += att.getDataLength() + Attribute.HEADER_LENGTH;
|
||||
|
||||
//now also skip any potential padding that might have come with
|
||||
//this attribute.
|
||||
if((att.getDataLength() % 4) > 0)
|
||||
{
|
||||
offset += (4 - (att.getDataLength() % 4));
|
||||
}
|
||||
}
|
||||
|
||||
return message;
|
||||
|
||||
@ -679,7 +679,7 @@ public class StunStack
|
||||
"Wrong MESSAGE-INTEGRITY value.");
|
||||
}
|
||||
}
|
||||
else
|
||||
else if(Boolean.getBoolean(StackProperties.REQUIRE_MESSAGE_INTEGRITY))
|
||||
{
|
||||
// no message integrity
|
||||
Response error = MessageFactory.createBindingErrorResponse(
|
||||
|
||||
@ -107,21 +107,15 @@ public class MsgFixture
|
||||
public static final String REASON_PHRASE = "Test error reason phrase.";//odd length!
|
||||
|
||||
public byte[] errCodeTestValue = new byte[]
|
||||
{0x00, 0x09, 0x00, 0x38,
|
||||
{0x00, 0x09, 0x00, 0x1D,
|
||||
0x00, 0x00, 0x04, 0x14,
|
||||
0x00, 0x54, 0x00, 0x65,
|
||||
0x00, 0x73, 0x00, 0x74,
|
||||
0x00, 0x20, 0x00, 0x65,
|
||||
0x00, 0x72, 0x00, 0x72,
|
||||
0x00, 0x6F, 0x00, 0x72,
|
||||
0x00, 0x20, 0x00, 0x72,
|
||||
0x00, 0x65, 0x00, 0x61,
|
||||
0x00, 0x73, 0x00, 0x6F,
|
||||
0x00, 0x6E, 0x00, 0x20,
|
||||
0x00, 0x70, 0x00, 0x68,
|
||||
0x00, 0x72, 0x00, 0x61,
|
||||
0x00, 0x73, 0x00, 0x65,
|
||||
0x00, 0x2E, 0x00, 0x20
|
||||
0x54, 0x65, 0x73, 0x74,
|
||||
0x20, 0x65, 0x72, 0x72,
|
||||
0x6F, 0x72, 0x20, 0x72,
|
||||
0x65, 0x61, 0x73, 0x6F,
|
||||
0x6E, 0x20, 0x70, 0x68,
|
||||
0x72, 0x61, 0x73, 0x65,
|
||||
0x2E, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -97,6 +97,7 @@ public class ErrorCodeAttributeTest extends TestCase {
|
||||
errorCodeAttribute.setReasonPhrase(msgFixture.REASON_PHRASE);
|
||||
|
||||
byte[] actualReturn = errorCodeAttribute.encode();
|
||||
|
||||
assertTrue("encode() did not return the expected binary array.",
|
||||
Arrays.equals( expectedReturn, actualReturn));
|
||||
}
|
||||
@ -158,16 +159,15 @@ public class ErrorCodeAttributeTest extends TestCase {
|
||||
public void testGetDataLength()
|
||||
throws StunException
|
||||
{
|
||||
char expectedReturn =
|
||||
(char)(msgFixture.errCodeTestValue.length
|
||||
- Attribute.HEADER_LENGTH);
|
||||
int expectedReturn = msgFixture.REASON_PHRASE.getBytes().length
|
||||
+ 4; //error code specific header
|
||||
|
||||
errorCodeAttribute.setErrorClass(msgFixture.ERROR_CLASS);
|
||||
errorCodeAttribute.setErrorNumber(msgFixture.ERROR_NUMBER);
|
||||
errorCodeAttribute.setReasonPhrase(msgFixture.REASON_PHRASE);
|
||||
|
||||
char actualReturn = errorCodeAttribute.getDataLength();
|
||||
assertEquals("return value", expectedReturn, actualReturn);
|
||||
assertEquals("data length1", (int)expectedReturn, (int)actualReturn);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user