Fixes problems related to the processing of padding in incoming stun messages

This commit is contained in:
emcho 2010-05-01 21:37:30 +00:00
parent 8c446d6f24
commit 8a17ce9e25
7 changed files with 34 additions and 33 deletions

View File

@ -98,6 +98,13 @@ public class StackProperties
public static final String ALWAYS_SIGN public static final String ALWAYS_SIGN
= "org.ice4j.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 * Returns the String value of the specified property (minus all
* encompassing whitespaces)and null in case no property value was mapped * encompassing whitespaces)and null in case no property value was mapped

View File

@ -45,11 +45,6 @@ public class AttributeDecoder
char attributeType = (char)((bytes[offset]<<8)|bytes[offset + 1]); char attributeType = (char)((bytes[offset]<<8)|bytes[offset + 1]);
char attributeLength = (char)((bytes[offset + 2]<<8)|bytes[offset + 3]); char attributeLength = (char)((bytes[offset + 2]<<8)|bytes[offset + 3]);
if((attributeLength % 4) > 0)
{
attributeLength += (4 - (attributeLength % 4));
}
if(attributeLength > bytes.length - offset ) if(attributeLength > bytes.length - offset )
throw new StunException( StunException.ILLEGAL_ARGUMENT, throw new StunException( StunException.ILLEGAL_ARGUMENT,
"Could not decode the specified binary array."); "Could not decode the specified binary array.");

View File

@ -6,6 +6,8 @@
*/ */
package org.ice4j.attribute; package org.ice4j.attribute;
import java.util.*;
import org.ice4j.*; import org.ice4j.*;
/** /**
@ -350,10 +352,8 @@ public class ErrorCodeAttribute extends Attribute
* offset is equal to the index of the first byte after * offset is equal to the index of the first byte after
* length) * length)
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attrubteValue contains invalid data.
*/ */
void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws void decodeAttributeBody(byte[] attributeValue, char offset, char length)
StunException
{ {
offset += 2; //skip the 0s offset += 2; //skip the 0s
@ -363,13 +363,11 @@ public class ErrorCodeAttribute extends Attribute
setErrorNumber(attributeValue[offset++]); setErrorNumber(attributeValue[offset++]);
//Reason Phrase //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) { System.arraycopy(attributeValue, offset, reasonBytes,
reasonPhrase[i] = 0, reasonBytes.length);
(char)(attributeValue[offset] | attributeValue[offset+1]); setReasonPhrase(new String(reasonBytes));
}
setReasonPhrase(new String(reasonPhrase).trim());
} }

View File

@ -702,6 +702,13 @@ public abstract class Message
message.addAttribute(att); message.addAttribute(att);
offset += att.getDataLength() + Attribute.HEADER_LENGTH; 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; return message;

View File

@ -679,7 +679,7 @@ public class StunStack
"Wrong MESSAGE-INTEGRITY value."); "Wrong MESSAGE-INTEGRITY value.");
} }
} }
else else if(Boolean.getBoolean(StackProperties.REQUIRE_MESSAGE_INTEGRITY))
{ {
// no message integrity // no message integrity
Response error = MessageFactory.createBindingErrorResponse( Response error = MessageFactory.createBindingErrorResponse(

View File

@ -107,21 +107,15 @@ public class MsgFixture
public static final String REASON_PHRASE = "Test error reason phrase.";//odd length! public static final String REASON_PHRASE = "Test error reason phrase.";//odd length!
public byte[] errCodeTestValue = new byte[] public byte[] errCodeTestValue = new byte[]
{0x00, 0x09, 0x00, 0x38, {0x00, 0x09, 0x00, 0x1D,
0x00, 0x00, 0x04, 0x14, 0x00, 0x00, 0x04, 0x14,
0x00, 0x54, 0x00, 0x65, 0x54, 0x65, 0x73, 0x74,
0x00, 0x73, 0x00, 0x74, 0x20, 0x65, 0x72, 0x72,
0x00, 0x20, 0x00, 0x65, 0x6F, 0x72, 0x20, 0x72,
0x00, 0x72, 0x00, 0x72, 0x65, 0x61, 0x73, 0x6F,
0x00, 0x6F, 0x00, 0x72, 0x6E, 0x20, 0x70, 0x68,
0x00, 0x20, 0x00, 0x72, 0x72, 0x61, 0x73, 0x65,
0x00, 0x65, 0x00, 0x61, 0x2E, 0x00, 0x00, 0x00
0x00, 0x73, 0x00, 0x6F,
0x00, 0x6E, 0x00, 0x20,
0x00, 0x70, 0x00, 0x68,
0x00, 0x72, 0x00, 0x61,
0x00, 0x73, 0x00, 0x65,
0x00, 0x2E, 0x00, 0x20
}; };

View File

@ -97,6 +97,7 @@ public class ErrorCodeAttributeTest extends TestCase {
errorCodeAttribute.setReasonPhrase(msgFixture.REASON_PHRASE); errorCodeAttribute.setReasonPhrase(msgFixture.REASON_PHRASE);
byte[] actualReturn = errorCodeAttribute.encode(); byte[] actualReturn = errorCodeAttribute.encode();
assertTrue("encode() did not return the expected binary array.", assertTrue("encode() did not return the expected binary array.",
Arrays.equals( expectedReturn, actualReturn)); Arrays.equals( expectedReturn, actualReturn));
} }
@ -158,16 +159,15 @@ public class ErrorCodeAttributeTest extends TestCase {
public void testGetDataLength() public void testGetDataLength()
throws StunException throws StunException
{ {
char expectedReturn = int expectedReturn = msgFixture.REASON_PHRASE.getBytes().length
(char)(msgFixture.errCodeTestValue.length + 4; //error code specific header
- Attribute.HEADER_LENGTH);
errorCodeAttribute.setErrorClass(msgFixture.ERROR_CLASS); errorCodeAttribute.setErrorClass(msgFixture.ERROR_CLASS);
errorCodeAttribute.setErrorNumber(msgFixture.ERROR_NUMBER); errorCodeAttribute.setErrorNumber(msgFixture.ERROR_NUMBER);
errorCodeAttribute.setReasonPhrase(msgFixture.REASON_PHRASE); errorCodeAttribute.setReasonPhrase(msgFixture.REASON_PHRASE);
char actualReturn = errorCodeAttribute.getDataLength(); char actualReturn = errorCodeAttribute.getDataLength();
assertEquals("return value", expectedReturn, actualReturn); assertEquals("data length1", (int)expectedReturn, (int)actualReturn);
} }
/** /**