diff --git a/src/org/ice4j/StackProperties.java b/src/org/ice4j/StackProperties.java index 97ba1ee..7b20563 100644 --- a/src/org/ice4j/StackProperties.java +++ b/src/org/ice4j/StackProperties.java @@ -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 diff --git a/src/org/ice4j/attribute/AttributeDecoder.java b/src/org/ice4j/attribute/AttributeDecoder.java index ee471fa..8f3b400 100644 --- a/src/org/ice4j/attribute/AttributeDecoder.java +++ b/src/org/ice4j/attribute/AttributeDecoder.java @@ -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."); diff --git a/src/org/ice4j/attribute/ErrorCodeAttribute.java b/src/org/ice4j/attribute/ErrorCodeAttribute.java index aff988a..f112729 100644 --- a/src/org/ice4j/attribute/ErrorCodeAttribute.java +++ b/src/org/ice4j/attribute/ErrorCodeAttribute.java @@ -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)); } diff --git a/src/org/ice4j/message/Message.java b/src/org/ice4j/message/Message.java index 5618959..575dcc4 100644 --- a/src/org/ice4j/message/Message.java +++ b/src/org/ice4j/message/Message.java @@ -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; diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index b54b730..5b4a008 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -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( diff --git a/test/org/ice4j/MsgFixture.java b/test/org/ice4j/MsgFixture.java index 05e4b00..5aaaf27 100644 --- a/test/org/ice4j/MsgFixture.java +++ b/test/org/ice4j/MsgFixture.java @@ -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 }; diff --git a/test/org/ice4j/attribute/ErrorCodeAttributeTest.java b/test/org/ice4j/attribute/ErrorCodeAttributeTest.java index b3f6e1e..c992ceb 100644 --- a/test/org/ice4j/attribute/ErrorCodeAttributeTest.java +++ b/test/org/ice4j/attribute/ErrorCodeAttributeTest.java @@ -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); } /**