Ongoing work on automatically generating some error responses
This commit is contained in:
parent
8f528bbafc
commit
1d4945e563
@ -24,30 +24,22 @@ public class AttributeDecoder
|
||||
* @param bytes the binary array that should be decoded.
|
||||
* @param offset the index where the message starts.
|
||||
* @param length the number of bytes that the message is long.
|
||||
* @param messageHeadBytes the binary array that contains the all the bytes
|
||||
* of the message that brought this attribute up until the beginning of the
|
||||
* attribute (at least).
|
||||
* @param messageHeadOffset the start of the container message in the
|
||||
* <tt>messageHeadBytes</tt> array.
|
||||
* @param messageHeadLen the length of the message head in the
|
||||
* <tt>messageHeadBytes</tt> array, up until the beginning of the attribute.
|
||||
*
|
||||
* @return An object representing the attribute encoded in bytes or null if
|
||||
* the attribute was not recognized.
|
||||
*
|
||||
* @throws StunException if bytes does is not a valid STUN attribute.
|
||||
* @throws StunException if bytes is not a valid STUN attribute.
|
||||
*/
|
||||
public static Attribute decode(byte[] bytes,
|
||||
char offset,
|
||||
char length,
|
||||
byte[] messageHeadBytes,
|
||||
char messageHeadOffset,
|
||||
char messageHeadLen)
|
||||
char offset,
|
||||
char length)
|
||||
throws StunException
|
||||
{
|
||||
if(bytes == null || bytes.length < Attribute.HEADER_LENGTH)
|
||||
throw new StunException(StunException.ILLEGAL_ARGUMENT,
|
||||
{
|
||||
throw new StunException( StunException.ILLEGAL_ARGUMENT,
|
||||
"Could not decode the specified binary array.");
|
||||
}
|
||||
|
||||
//Discover attribute type
|
||||
char attributeType = (char)((bytes[offset]<<8)|bytes[offset + 1]);
|
||||
@ -59,9 +51,8 @@ public class AttributeDecoder
|
||||
}
|
||||
|
||||
if(attributeLength > bytes.length - offset )
|
||||
throw new StunException(StunException.ILLEGAL_ARGUMENT,
|
||||
"The indicated attribute length ("+attributeLength+") "
|
||||
+"does not match the length of the passed binary array");
|
||||
throw new StunException( StunException.ILLEGAL_ARGUMENT,
|
||||
"Could not decode the specified binary array.");
|
||||
|
||||
Attribute decodedAttribute = null;
|
||||
|
||||
@ -78,9 +69,7 @@ public class AttributeDecoder
|
||||
decodedAttribute = new ErrorCodeAttribute(); break;
|
||||
case Attribute.MESSAGE_INTEGRITY:
|
||||
decodedAttribute = new MessageIntegrityAttribute(); break;
|
||||
case Attribute.PASSWORD:
|
||||
throw new UnsupportedOperationException(
|
||||
"The PASSWORD Attribute is not yet implemented.");
|
||||
//case Attribute.PASSWORD: //handle as an unknown attribute
|
||||
case Attribute.REFLECTED_FROM:
|
||||
decodedAttribute = new ReflectedFromAttribute(); break;
|
||||
case Attribute.RESPONSE_ADDRESS:
|
||||
@ -128,15 +117,14 @@ public class AttributeDecoder
|
||||
|
||||
//According to rfc3489 we should silently ignore unknown attributes.
|
||||
default: decodedAttribute
|
||||
= new OptionalAttribute(Attribute.UNKNOWN_OPTIONAL_ATTRIBUTE);
|
||||
= new OptionalAttribute( Attribute.UNKNOWN_OPTIONAL_ATTRIBUTE);
|
||||
break;
|
||||
}
|
||||
|
||||
decodedAttribute.setAttributeType(attributeType);
|
||||
|
||||
decodedAttribute.decodeAttributeBody(bytes,
|
||||
(char)(Attribute.HEADER_LENGTH + offset),
|
||||
attributeLength);
|
||||
(char)(Attribute.HEADER_LENGTH + offset), attributeLength);
|
||||
|
||||
return decodedAttribute;
|
||||
}
|
||||
|
||||
@ -250,8 +250,6 @@ public class ConnectivityCheckClient
|
||||
if(pairToCheck != null)
|
||||
transactionID = parentClient.startCheckForPair(pairToCheck);
|
||||
|
||||
|
||||
|
||||
if(pairToCheck == null)
|
||||
{
|
||||
//we are done sending checks for this list. we'll send its
|
||||
|
||||
@ -618,6 +618,7 @@ public abstract class Message
|
||||
* @param offset the index where the message starts.
|
||||
* @param arrayLen the length of the message
|
||||
* @return a Message object constructed from the binMessage array
|
||||
*
|
||||
* @throws StunException ILLEGAL_ARGUMENT if one or more of the arguments
|
||||
* have invalid values.
|
||||
*/
|
||||
@ -626,12 +627,10 @@ public abstract class Message
|
||||
{
|
||||
int originalOffset = offset;
|
||||
arrayLen = (char)Math.min(binMessage.length, arrayLen);
|
||||
char initialOffset = offset;
|
||||
|
||||
|
||||
if(binMessage == null || arrayLen - offset < Message.HEADER_LENGTH)
|
||||
{
|
||||
throw new StunException(StunException.ILLEGAL_ARGUMENT,
|
||||
throw new StunException( StunException.ILLEGAL_ARGUMENT,
|
||||
"The given binary array is not a valid StunMessage");
|
||||
}
|
||||
|
||||
@ -648,7 +647,7 @@ public abstract class Message
|
||||
}
|
||||
else /* indication */
|
||||
{
|
||||
message = new Indication();
|
||||
message = new Indication();
|
||||
}
|
||||
|
||||
message.setMessageType(messageType);
|
||||
@ -662,24 +661,31 @@ public abstract class Message
|
||||
offset+=4;
|
||||
|
||||
if(arrayLen - offset - TRANSACTION_ID_LENGTH < length)
|
||||
throw new StunException(StunException.ILLEGAL_ARGUMENT,
|
||||
"The given binary array does not seem to "
|
||||
+"contain a whole StunMessage");
|
||||
{
|
||||
throw new StunException( StunException.ILLEGAL_ARGUMENT,
|
||||
"The given binary array does not seem to "
|
||||
+ "contain a whole StunMessage");
|
||||
}
|
||||
|
||||
byte tranID[] = new byte[TRANSACTION_ID_LENGTH];
|
||||
System.arraycopy(binMessage, offset, tranID, 0, TRANSACTION_ID_LENGTH);
|
||||
message.setTransactionID(tranID);
|
||||
try
|
||||
{
|
||||
message.setTransactionID(tranID);
|
||||
}
|
||||
catch (StunException exc)
|
||||
{
|
||||
throw new StunException( StunException.ILLEGAL_ARGUMENT,
|
||||
"The given binary array does not seem to "
|
||||
+ "contain a whole StunMessage", exc);
|
||||
}
|
||||
|
||||
offset+=TRANSACTION_ID_LENGTH;
|
||||
|
||||
while(offset - Message.HEADER_LENGTH< length)
|
||||
{
|
||||
Attribute att = AttributeDecoder.decode(
|
||||
binMessage, offset, (char)(length - offset), //attribute
|
||||
binMessage, (char)initialOffset,
|
||||
(char)(offset - initialOffset)); // message head
|
||||
|
||||
performAttributeSpecificActions(att, binMessage, message,
|
||||
originalOffset, offset);
|
||||
binMessage, offset, (char)(length - offset));
|
||||
|
||||
message.addAttribute(att);
|
||||
offset += att.getDataLength() + Attribute.HEADER_LENGTH;
|
||||
@ -688,219 +694,6 @@ public abstract class Message
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes actions related specific attributes like asserting proper
|
||||
* checksums or verifying the validity of user names.
|
||||
*
|
||||
* @param attribute the <tt>Attribute</tt> we'd like to process.
|
||||
* @param binMessage the byte array that the message arrived with.
|
||||
* @param message the <tt>Message</tt> that we're parsing
|
||||
* <tt>binMessage</tt> into.
|
||||
* @param offset the index where data starts in <tt>binMessage</tt>.
|
||||
* @param msgLen the number of message bytes in <tt>binMessage</tt>.
|
||||
*
|
||||
* @throws StunException if there's something in the <tt>attribute</tt>
|
||||
* that caused us to discard the whole message (e.g. an invalid checksum
|
||||
* or username)
|
||||
*/
|
||||
private static void performAttributeSpecificActions(Attribute attribute,
|
||||
byte[] binMessage,
|
||||
Message message,
|
||||
int offset,
|
||||
int msgLen)
|
||||
throws StunException
|
||||
{
|
||||
//assert valid username
|
||||
if (attribute instanceof UsernameAttribute)
|
||||
{
|
||||
if (!validateUsername( (UsernameAttribute)attribute))
|
||||
{
|
||||
throw new StunException(StunException.ILLEGAL_ARGUMENT,
|
||||
"Non-recognized username: "
|
||||
+ new String(((UsernameAttribute)attribute)
|
||||
.getUsername()) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//assert Message Integrity
|
||||
if (attribute instanceof MessageIntegrityAttribute)
|
||||
{
|
||||
if (!message.validateMessageIntegrity(
|
||||
(MessageIntegrityAttribute)attribute,
|
||||
binMessage, offset, msgLen))
|
||||
{
|
||||
throw new StunException(StunException.ILLEGAL_ARGUMENT,
|
||||
"Wrong value in MESSAGE-INTEGRITY");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//check finger print CRC
|
||||
if (attribute instanceof FingerprintAttribute)
|
||||
{
|
||||
if (!validateFingerprint((FingerprintAttribute)attribute,
|
||||
binMessage, offset, msgLen))
|
||||
{
|
||||
throw new StunException(StunException.ILLEGAL_ARGUMENT,
|
||||
"Wrong value in FINGERPRINT");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculates the fingerprint CRC32 checksum of the <tt>message</tt>
|
||||
* array so that we could compare it with the value brought by the
|
||||
* {@link FingerprintAttribute}.
|
||||
*
|
||||
* @param fingerprint the attribute that we need to validate.
|
||||
* @param message the message whose CRC32 checksum we'd need to recalculate.
|
||||
* @param offset the index in <tt>message</tt> where data starts.
|
||||
* @param length the number of bytes in <tt>message</tt> that the CRC32
|
||||
* would need to be calculated over.
|
||||
*
|
||||
* @return <tt>true</tt> if <tt>fingerprint</tt> contains a valid CRC32
|
||||
* value and <tt>false</tt> otherwise.
|
||||
*/
|
||||
private static boolean validateFingerprint(FingerprintAttribute fingerprint,
|
||||
byte[] message,
|
||||
int offset,
|
||||
int length)
|
||||
{
|
||||
|
||||
byte[] incomingCrcBytes = fingerprint.getChecksum();
|
||||
|
||||
//now check whether the CRC really is what it's supposed to be.
|
||||
//re calculate the check sum
|
||||
byte[] realCrcBytes = FingerprintAttribute.calculateXorCRC32(
|
||||
message, offset, length);
|
||||
|
||||
//CRC validation.
|
||||
if ( ! Arrays.equals(incomingCrcBytes, realCrcBytes))
|
||||
{
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.fine(
|
||||
"An incoming message arrived with a wrong FINGERPRINT "
|
||||
+"attribute value. "
|
||||
+"CRC Was:" + Arrays.toString(incomingCrcBytes)
|
||||
+ ". Should have been:" + Arrays.toString(realCrcBytes)
|
||||
+". Will ignore.");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculates the HMAC-SHA1 signature of the <tt>message</tt> array so
|
||||
* that we could compare it with the value brought by the
|
||||
* {@link MessageIntegrityAttribute}.
|
||||
*
|
||||
* @param msgInt the attribute that we need to validate.
|
||||
* @param message the message whose SHA1 checksum we'd need to recalculate.
|
||||
* @param offset the index in <tt>message</tt> where data starts.
|
||||
* @param length the number of bytes in <tt>message</tt> that the SHA1 would
|
||||
* need to be calculated over.
|
||||
*
|
||||
* @return <tt>true</tt> if <tt>msgInt</tt> contains a valid SHA1 value and
|
||||
* <tt>false</tt> otherwise.
|
||||
*/
|
||||
private boolean validateMessageIntegrity(MessageIntegrityAttribute msgInt,
|
||||
byte[] message,
|
||||
int offset,
|
||||
int length)
|
||||
{
|
||||
//first get a password for the username specified with this message.
|
||||
UsernameAttribute unameAttr
|
||||
= (UsernameAttribute)getAttribute(Attribute.USERNAME);
|
||||
|
||||
if (unameAttr == null)
|
||||
{
|
||||
logger.info( "Received a message containing a "
|
||||
+" MESSAGE-INTEGRITY attribute and no USERNAME");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
String username = new String(unameAttr.getUsername());
|
||||
|
||||
int colon = username.indexOf(":");
|
||||
|
||||
if( username.length() < 1
|
||||
|| colon < 1)
|
||||
{
|
||||
if(logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.log(Level.FINE, "Received a message with an improperly "
|
||||
+"formatted username");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
String lfrag = username.substring(0, colon);
|
||||
|
||||
byte[] key = StunStack.getInstance()
|
||||
.getCredentialsManager().getLocalKey(lfrag);
|
||||
|
||||
if(key == null)
|
||||
return false;
|
||||
|
||||
//now check whether the SHA1 matches.
|
||||
byte[] expectedSha1 = MessageIntegrityAttribute
|
||||
.calculateHmacSha1(message, offset, length, key);
|
||||
|
||||
if (!Arrays.equals(expectedSha1, msgInt.getHmacSha1Content()))
|
||||
{
|
||||
if(logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.log(Level.FINE, "Received a message with a wrong "
|
||||
+"MESSAGE-INTEGRITY HMAC-SHA1 signature");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (logger.isLoggable(Level.FINEST))
|
||||
logger.finest("Successfully verified msg integrity");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the validity of the user name we've received in
|
||||
* <tt>unameAttr</tt>.
|
||||
*
|
||||
* @param unameAttr the attribute that we need to validate.
|
||||
*
|
||||
* @return <tt>true</tt> if <tt>unameAttr</tt> contains a valid user name
|
||||
* and <tt>false</tt> otherwise.
|
||||
*/
|
||||
private static boolean validateUsername(UsernameAttribute unameAttr)
|
||||
{
|
||||
String username = new String(unameAttr.getUsername());
|
||||
|
||||
int colon = username.indexOf(":");
|
||||
|
||||
if( username.length() < 1
|
||||
|| colon < 1)
|
||||
{
|
||||
if(logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.log(Level.FINE, "Received a message with an improperly "
|
||||
+"formatted username");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
String lfrag = username.substring(0, colon);
|
||||
|
||||
return StunStack.getInstance()
|
||||
.getCredentialsManager().checkLocalUserName(lfrag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the message has all obligatory attributes and throw an
|
||||
* exception if this is not the case.
|
||||
|
||||
148
src/org/ice4j/message/MessageDecodeException.java
Normal file
148
src/org/ice4j/message/MessageDecodeException.java
Normal file
@ -0,0 +1,148 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.message;
|
||||
|
||||
import org.ice4j.attribute.*;
|
||||
|
||||
/**
|
||||
* The {@link MessageDecodeException} is thrown during decoding in order
|
||||
* to indicate what problems there might have been with a particular incoming
|
||||
* message. The exception is particularly useful when decoding incoming requests
|
||||
* so that the stack could generate an appropriate error response.
|
||||
*
|
||||
* @author Emil Ivov
|
||||
*/
|
||||
public class MessageDecodeException
|
||||
extends Exception
|
||||
{
|
||||
/**
|
||||
* Dummy version UID
|
||||
*/
|
||||
static final long serialVersionUID = -1L;
|
||||
|
||||
/**
|
||||
* The error code to use in case we do not want this exception to cause a
|
||||
* response and would prefer to discard instead.
|
||||
*/
|
||||
public static int NO_RESPONSE = -1;
|
||||
|
||||
/**
|
||||
* A STUN Error Response code to use if the message that caused the
|
||||
* exception was a {@link Request} and we could send an error response.
|
||||
*/
|
||||
private int stunResponseCode = NO_RESPONSE;
|
||||
|
||||
/**
|
||||
* An attribute to include in the error response.
|
||||
*/
|
||||
private Attribute responseAttribute = null;
|
||||
|
||||
/**
|
||||
* The ID of the transaction to use in case the exception allows for
|
||||
* sending an error response.
|
||||
*/
|
||||
private byte[] transactionID = null;
|
||||
|
||||
/**
|
||||
* Creates a new <tt>MessageDecodeException</tt> instance.
|
||||
*
|
||||
* @param message a text message explaining the exception
|
||||
*/
|
||||
public MessageDecodeException(String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <tt>MessageDecodeException</tt> instance.
|
||||
*
|
||||
* @param message a text message explaining the exception
|
||||
* @param cause A reference to a {@link Throwable} instance in case this
|
||||
* exception was caused by another, unexpected exception.
|
||||
*/
|
||||
public MessageDecodeException(String message,
|
||||
Throwable cause)
|
||||
{
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <tt>MessageDecodeException</tt> instance.
|
||||
*
|
||||
* @param message a text message explaining the exception
|
||||
* @param stunResponseCode a STUN Error Response code to use if the message
|
||||
* that caused the exception was a {@link Request} and we could send an
|
||||
* error response, or <tt>-1</tt> in case we should not reply.
|
||||
* @param transactionID the id of the transaction to use in an error
|
||||
* response.
|
||||
*/
|
||||
public MessageDecodeException(String message,
|
||||
int stunResponseCode,
|
||||
byte[] transactionID)
|
||||
{
|
||||
super(message);
|
||||
this.stunResponseCode = stunResponseCode;
|
||||
this.transactionID = transactionID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <tt>MessageDecodeException</tt> instance.
|
||||
*
|
||||
* @param message a text message explaining the exception
|
||||
* @param stunResponseCode a STUN Error Response code to use if the message
|
||||
* that caused the exception was a {@link Request} and we could send an
|
||||
* error response, or <tt>-1</tt> in case we should not reply.
|
||||
* @param transactionID the id of the transaction to use in an error
|
||||
* response.
|
||||
* @param attribute the Attribute an attribute to include in the error
|
||||
* response.
|
||||
*/
|
||||
public MessageDecodeException(String message,
|
||||
int stunResponseCode,
|
||||
byte[] transactionID,
|
||||
Attribute attribute)
|
||||
{
|
||||
super(message);
|
||||
this.stunResponseCode = stunResponseCode;
|
||||
this.transactionID = transactionID;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a STUN Error Response code to use if the message that caused the
|
||||
* exception was a {@link Request} and we could send an error response.
|
||||
*
|
||||
* @return a STUN Error Response code to use if the message that caused the
|
||||
* exception was a {@link Request} and we could send an error response.
|
||||
*/
|
||||
public int getErrorResponseCode()
|
||||
{
|
||||
return stunResponseCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whatever attribute would have to be included in a potential
|
||||
* error response like for example the attribute that cause a 420 error.
|
||||
*
|
||||
* @return whatever attribute would have to be included in a potential
|
||||
* error response like for example the attribute that cause a 420 error.
|
||||
*/
|
||||
public Attribute getResponseAttribute()
|
||||
{
|
||||
return responseAttribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the id of the transaction to use in an error response.
|
||||
*
|
||||
* @return the id of the transaction to use in an error response.
|
||||
*/
|
||||
public byte[] getTransactionID()
|
||||
{
|
||||
return transactionID;
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,7 @@ package org.ice4j.stack;
|
||||
import java.util.logging.*;
|
||||
|
||||
import org.ice4j.*;
|
||||
import org.ice4j.attribute.*;
|
||||
import org.ice4j.message.*;
|
||||
|
||||
/**
|
||||
@ -125,10 +126,11 @@ class MessageProcessor
|
||||
(char) 0,
|
||||
(char) rawMessage.getMessageLength());
|
||||
}
|
||||
catch (StunException ex)
|
||||
catch (MessageDecodeException ex)
|
||||
{
|
||||
errorHandler.handleError("Failed to decode a stun mesage!",
|
||||
ex);
|
||||
|
||||
continue; //let this one go and for better luck next time.
|
||||
}
|
||||
|
||||
|
||||
@ -523,6 +523,8 @@ public class StunStack
|
||||
sTran.start();
|
||||
}
|
||||
|
||||
//validate attributes that need validation.
|
||||
|
||||
eventDispatcher.fireMessageEvent(event);
|
||||
|
||||
}
|
||||
@ -584,4 +586,229 @@ public class StunStack
|
||||
tran.expire();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes actions related specific attributes like asserting proper
|
||||
* checksums or verifying the validity of user names.
|
||||
*
|
||||
* @param attribute the <tt>Attribute</tt> we'd like to process.
|
||||
* @param binMessage the byte array that the message arrived with.
|
||||
* @param message the <tt>Message</tt> that we're parsing
|
||||
* <tt>binMessage</tt> into.
|
||||
* @param offset the index where data starts in <tt>binMessage</tt>.
|
||||
* @param msgLen the number of message bytes in <tt>binMessage</tt>.
|
||||
*
|
||||
* @throws MessageDecodeException if there's something in the
|
||||
* <tt>attribute</tt> that caused us to discard the whole message (e.g. an
|
||||
* invalid checksum
|
||||
* or username)
|
||||
*/
|
||||
private void performAttributeSpecificActions(Attribute attribute,
|
||||
byte[] binMessage,
|
||||
Message message,
|
||||
int offset,
|
||||
int msgLen)
|
||||
throws MessageDecodeException
|
||||
{
|
||||
///HANDLE OPTIONAL ATTRIBUTES
|
||||
byte[] tranID = message.getTransactionID();
|
||||
//assert valid username
|
||||
if (attribute instanceof UsernameAttribute)
|
||||
{
|
||||
if (!validateUsername( (UsernameAttribute)attribute))
|
||||
{
|
||||
throw new MessageDecodeException(
|
||||
"Non-recognized username: "
|
||||
+ new String(((UsernameAttribute)attribute)
|
||||
.getUsername()),
|
||||
ErrorCodeAttribute.UNAUTHORIZED, tranID);
|
||||
}
|
||||
}
|
||||
|
||||
//assert Message Integrity
|
||||
if (attribute instanceof MessageIntegrityAttribute)
|
||||
{
|
||||
//we should complain if we have msg integrity and no username.
|
||||
if (message.getAttribute(Attribute.USERNAME) == null)
|
||||
{
|
||||
throw new MessageDecodeException(
|
||||
"Missing USERNAME in the presence of MESSAGE-INTEGRITY: ",
|
||||
ErrorCodeAttribute.BAD_REQUEST, tranID);
|
||||
}
|
||||
|
||||
if (!validateMessageIntegrity((MessageIntegrityAttribute)attribute,
|
||||
binMessage, offset, msgLen))
|
||||
{
|
||||
throw new MessageDecodeException(
|
||||
"Wrong MESSAGE-INTEGRITY value.",
|
||||
ErrorCodeAttribute.UNAUTHORIZED, tranID);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//check finger print CRC
|
||||
if (attribute instanceof FingerprintAttribute)
|
||||
{
|
||||
if (!validateFingerprint((FingerprintAttribute)attribute,
|
||||
binMessage, offset, msgLen))
|
||||
{
|
||||
//RFC 5389 says that we should ignore bad CRCs rather than
|
||||
//reply with an error response.
|
||||
throw new MessageDecodeException(
|
||||
"Wrong value in FINGERPRINT");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculates the FINGERPRINT CRC32 checksum of the <tt>message</tt>
|
||||
* array so that we could compare it with the value brought by the
|
||||
* {@link FingerprintAttribute}.
|
||||
*
|
||||
* @param fingerprint the attribute that we need to validate.
|
||||
* @param message the message whose CRC32 checksum we'd need to recalculate.
|
||||
* @param offset the index in <tt>message</tt> where data starts.
|
||||
* @param length the number of bytes in <tt>message</tt> that the CRC32
|
||||
* would need to be calculated over.
|
||||
*
|
||||
* @return <tt>true</tt> if <tt>FINGERPRINT</tt> contains a valid CRC32
|
||||
* value and <tt>false</tt> otherwise.
|
||||
*/
|
||||
private static boolean validateFingerprint(FingerprintAttribute fingerprint,
|
||||
byte[] message,
|
||||
int offset,
|
||||
int length)
|
||||
{
|
||||
|
||||
byte[] incomingCrcBytes = fingerprint.getChecksum();
|
||||
|
||||
//now check whether the CRC really is what it's supposed to be.
|
||||
//re calculate the check sum
|
||||
byte[] realCrcBytes = FingerprintAttribute.calculateXorCRC32(
|
||||
message, offset, length);
|
||||
|
||||
//CRC validation.
|
||||
if ( ! Arrays.equals(incomingCrcBytes, realCrcBytes))
|
||||
{
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.fine(
|
||||
"An incoming message arrived with a wrong FINGERPRINT "
|
||||
+"attribute value. "
|
||||
+"CRC Was:" + Arrays.toString(incomingCrcBytes)
|
||||
+ ". Should have been:" + Arrays.toString(realCrcBytes)
|
||||
+". Will ignore.");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculates the HMAC-SHA1 signature of the <tt>message</tt> array so
|
||||
* that we could compare it with the value brought by the
|
||||
* {@link MessageIntegrityAttribute}.
|
||||
*
|
||||
* @param msgInt the attribute that we need to validate.
|
||||
* @param message the message whose SHA1 checksum we'd need to recalculate.
|
||||
* @param offset the index in <tt>message</tt> where data starts.
|
||||
* @param length the number of bytes in <tt>message</tt> that the SHA1 would
|
||||
* need to be calculated over.
|
||||
*
|
||||
* @return <tt>true</tt> if <tt>msgInt</tt> contains a valid SHA1 value and
|
||||
* <tt>false</tt> otherwise.
|
||||
*/
|
||||
private boolean validateMessageIntegrity(MessageIntegrityAttribute msgInt,
|
||||
byte[] message,
|
||||
int offset,
|
||||
int length)
|
||||
{
|
||||
//first get a password for the username specified with this message.
|
||||
UsernameAttribute unameAttr
|
||||
= (UsernameAttribute)getAttribute(Attribute.USERNAME);
|
||||
|
||||
if (unameAttr == null)
|
||||
{
|
||||
logger.info( "Received a message containing a "
|
||||
+" MESSAGE-INTEGRITY attribute and no USERNAME");
|
||||
return false;
|
||||
}
|
||||
|
||||
String username = new String(unameAttr.getUsername());
|
||||
|
||||
int colon = username.indexOf(":");
|
||||
|
||||
if( username.length() < 1
|
||||
|| colon < 1)
|
||||
{
|
||||
if(logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.log(Level.FINE, "Received a message with an improperly "
|
||||
+"formatted username");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
String lfrag = username.substring(0, colon);
|
||||
|
||||
byte[] key = StunStack.getInstance()
|
||||
.getCredentialsManager().getLocalKey(lfrag);
|
||||
|
||||
if(key == null)
|
||||
return false;
|
||||
|
||||
//now check whether the SHA1 matches.
|
||||
byte[] expectedSha1 = MessageIntegrityAttribute
|
||||
.calculateHmacSha1(message, offset, length, key);
|
||||
|
||||
if (!Arrays.equals(expectedSha1, msgInt.getHmacSha1Content()))
|
||||
{
|
||||
if(logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.log(Level.FINE, "Received a message with a wrong "
|
||||
+"MESSAGE-INTEGRITY HMAC-SHA1 signature");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (logger.isLoggable(Level.FINEST))
|
||||
logger.finest("Successfully verified msg integrity");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the validity of the user name we've received in
|
||||
* <tt>unameAttr</tt>.
|
||||
*
|
||||
* @param unameAttr the attribute that we need to validate.
|
||||
*
|
||||
* @return <tt>true</tt> if <tt>unameAttr</tt> contains a valid user name
|
||||
* and <tt>false</tt> otherwise.
|
||||
*/
|
||||
private static boolean validateUsername(UsernameAttribute unameAttr)
|
||||
{
|
||||
String username = new String(unameAttr.getUsername());
|
||||
|
||||
int colon = username.indexOf(":");
|
||||
|
||||
if( username.length() < 1
|
||||
|| colon < 1)
|
||||
{
|
||||
if(logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.log(Level.FINE, "Received a message with an improperly "
|
||||
+"formatted username");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
String lfrag = username.substring(0, colon);
|
||||
|
||||
return StunStack.getInstance()
|
||||
.getCredentialsManager().checkLocalUserName(lfrag);
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,8 +67,7 @@ public class AttributeDecoderTest extends TestCase {
|
||||
Transport.UDP));
|
||||
|
||||
Attribute actualReturn = AttributeDecoder.decode(
|
||||
bytes, offset, length, //attribute
|
||||
bytes, (char)0, offset);//message head
|
||||
bytes, offset, length, null);
|
||||
|
||||
assertEquals(
|
||||
"AttributeDecoder.decode() failed for a MAPPED-ADDRESS attribute",
|
||||
@ -92,8 +91,7 @@ public class AttributeDecoderTest extends TestCase {
|
||||
Transport.UDP));
|
||||
|
||||
Attribute actualReturn = AttributeDecoder.decode(
|
||||
bytes, offset, length, //attribute
|
||||
bytes, (char)0, offset);//message head
|
||||
bytes, offset, length, null);
|
||||
|
||||
assertEquals(
|
||||
"AttributeDecoder.decode() failed for a MAPPED-ADDRESS attribute",
|
||||
@ -101,7 +99,7 @@ public class AttributeDecoderTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testDecodeChangeRequest()
|
||||
throws StunException
|
||||
throws Exception
|
||||
{
|
||||
//
|
||||
byte[] bytes = msgFixture.chngReqTestValue1;
|
||||
@ -114,8 +112,7 @@ public class AttributeDecoderTest extends TestCase {
|
||||
expectedReturn.setChangePortFlag(MsgFixture.CHANGE_PORT_FLAG_1);
|
||||
|
||||
Attribute actualReturn = AttributeDecoder.decode(
|
||||
bytes, offset, length, //attribute
|
||||
bytes, (char)0, offset);//message head
|
||||
bytes, offset, length, null);
|
||||
assertEquals("AttributeDecoder.decode() failed for a CHANGE-REQUEST attribute",
|
||||
expectedReturn, actualReturn);
|
||||
|
||||
@ -123,7 +120,7 @@ public class AttributeDecoderTest extends TestCase {
|
||||
|
||||
|
||||
public void testDecodeErrorCode()
|
||||
throws StunException
|
||||
throws Exception
|
||||
{
|
||||
//
|
||||
byte[] bytes = msgFixture.errCodeTestValue;
|
||||
@ -137,8 +134,7 @@ public class AttributeDecoderTest extends TestCase {
|
||||
expectedReturn.setReasonPhrase(MsgFixture.REASON_PHRASE);
|
||||
|
||||
Attribute actualReturn = AttributeDecoder.decode(
|
||||
bytes, offset, length, //attribute
|
||||
bytes, (char)0, offset);//message head
|
||||
bytes, offset, length, null);
|
||||
assertEquals(
|
||||
"AttributeDecoder.decode() failed for a ERROR-CODE attribute",
|
||||
expectedReturn, actualReturn);
|
||||
@ -146,7 +142,7 @@ public class AttributeDecoderTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testDecodeUnknownAttributes()
|
||||
throws StunException
|
||||
throws Exception
|
||||
{
|
||||
//unknown attributes
|
||||
byte[] bytes = msgFixture.unknownAttsDecodeTestValue;
|
||||
@ -154,14 +150,14 @@ public class AttributeDecoderTest extends TestCase {
|
||||
char length = (char)msgFixture.mappedAddress.length;
|
||||
|
||||
//create the message
|
||||
UnknownAttributesAttribute expectedReturn = new UnknownAttributesAttribute();
|
||||
UnknownAttributesAttribute expectedReturn
|
||||
= new UnknownAttributesAttribute();
|
||||
expectedReturn.addAttributeID(MsgFixture.UNKNOWN_ATTRIBUTES_1ST_ATT);
|
||||
expectedReturn.addAttributeID(MsgFixture.UNKNOWN_ATTRIBUTES_2ND_ATT);
|
||||
expectedReturn.addAttributeID(MsgFixture.UNKNOWN_ATTRIBUTES_3D_ATT);
|
||||
|
||||
Attribute actualReturn = AttributeDecoder.decode(
|
||||
bytes, offset, length, //attribute
|
||||
bytes, (char)0, offset);//message head
|
||||
bytes, offset, length, null);
|
||||
|
||||
assertEquals("AttributeDecoder.decode() failed for a ERROR-CODE attribute",
|
||||
expectedReturn, actualReturn);
|
||||
@ -169,7 +165,7 @@ public class AttributeDecoderTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testDecodeUnknownOptionalAttribute()
|
||||
throws StunException
|
||||
throws Exception
|
||||
{
|
||||
//unknown attributes
|
||||
byte[] bytes = msgFixture.unknownOptionalAttribute;
|
||||
@ -183,8 +179,7 @@ public class AttributeDecoderTest extends TestCase {
|
||||
expectedAttributeValue.length);
|
||||
|
||||
Attribute actualReturn = AttributeDecoder.decode(
|
||||
bytes, offset, length, //attribute
|
||||
bytes, (char)0, offset);//message head
|
||||
bytes, offset, length, null);
|
||||
assertEquals(
|
||||
"AttributeDecoder.decode() failed for a UNKNOWN_OPTIONAL attribute",
|
||||
expectedReturn, actualReturn);
|
||||
|
||||
@ -157,14 +157,14 @@ public class MessageTest extends TestCase {
|
||||
* @throws StunException java.lang.Exception if we fail
|
||||
*/
|
||||
public void testDecode()
|
||||
throws StunException
|
||||
throws Exception
|
||||
{
|
||||
//Binding Request
|
||||
Message expectedReturn = bindingRequest;
|
||||
|
||||
Message actualReturn = Message.decode(msgFixture.bindingRequest,
|
||||
(char)0,
|
||||
(char)msgFixture.bindingRequest.length);
|
||||
(char)0,
|
||||
(char)msgFixture.bindingRequest.length);
|
||||
|
||||
assertEquals("A binding request was not properly decoded",
|
||||
expectedReturn, actualReturn );
|
||||
@ -173,8 +173,8 @@ public class MessageTest extends TestCase {
|
||||
expectedReturn = bindingResponse;
|
||||
|
||||
actualReturn = Message.decode(msgFixture.bindingResponse,
|
||||
(char)0,
|
||||
(char)msgFixture.bindingResponse.length);
|
||||
(char)0,
|
||||
(char)msgFixture.bindingResponse.length);
|
||||
|
||||
assertEquals("A binding response was not properly decoded",
|
||||
expectedReturn, actualReturn );
|
||||
@ -192,36 +192,44 @@ public class MessageTest extends TestCase {
|
||||
Object target = null;
|
||||
boolean expectedReturn = false;
|
||||
boolean actualReturn = bindingRequest.equals(target);
|
||||
assertEquals("Equals failed against a null target", expectedReturn, actualReturn);
|
||||
assertEquals("Equals failed against a null target",
|
||||
expectedReturn, actualReturn);
|
||||
|
||||
actualReturn = bindingResponse.equals(target);
|
||||
assertEquals("Equals failed against a null target", expectedReturn, actualReturn);
|
||||
assertEquals("Equals failed against a null target",
|
||||
expectedReturn, actualReturn);
|
||||
|
||||
//different
|
||||
actualReturn = bindingRequest.equals(bindingResponse);
|
||||
assertEquals("Equals failed against a different target", expectedReturn, actualReturn);
|
||||
assertEquals("Equals failed against a different target",
|
||||
expectedReturn, actualReturn);
|
||||
|
||||
actualReturn = bindingResponse.equals(bindingRequest);
|
||||
assertEquals("Equals failed against a different target", expectedReturn, actualReturn);
|
||||
assertEquals("Equals failed against a different target",
|
||||
expectedReturn, actualReturn);
|
||||
|
||||
//same
|
||||
expectedReturn = true;
|
||||
|
||||
//Create a binding request with the same attributes as this.bindingRequest
|
||||
//Create a binding request with the same attributes as
|
||||
//this.bindingRequest
|
||||
Request binReqTarget = new Request();
|
||||
binReqTarget.setMessageType(Message.BINDING_REQUEST);
|
||||
binReqTarget.addAttribute(changeRequest);
|
||||
actualReturn = bindingRequest.equals(binReqTarget);
|
||||
assertEquals("Equals failed against an equal target", expectedReturn, actualReturn);
|
||||
assertEquals("Equals failed against an equal target",
|
||||
expectedReturn, actualReturn);
|
||||
|
||||
//Create a binding response with the same attributes as this.bindingRequest
|
||||
//Create a binding response with the same attributes as
|
||||
//this.bindingRequest
|
||||
Response binResTarget = new Response();
|
||||
binResTarget.setMessageType(Message.BINDING_RESPONSE);
|
||||
binResTarget.addAttribute(mappedAddress);
|
||||
binResTarget.addAttribute(sourceAddress);
|
||||
binResTarget.addAttribute(changedAddress);
|
||||
actualReturn = bindingResponse.equals(binResTarget);
|
||||
assertEquals("Equals failed against a different target", expectedReturn, actualReturn);
|
||||
assertEquals("Equals failed against a different target",
|
||||
expectedReturn, actualReturn);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -246,12 +254,13 @@ public class MessageTest extends TestCase {
|
||||
bindingRequest.removeAttribute(changeRequest.getAttributeType());
|
||||
|
||||
assertNull("An attribute was still in the request after being removed",
|
||||
bindingRequest.getAttribute(changeRequest.getAttributeType()));
|
||||
bindingRequest.getAttribute(changeRequest.getAttributeType()));
|
||||
|
||||
//test count
|
||||
int expectedReturn = 0;
|
||||
int actualReturn = bindingRequest.getAttributeCount();
|
||||
assertEquals("Attribute count did not change after removing an attribute",
|
||||
expectedReturn, actualReturn);
|
||||
assertEquals(
|
||||
"Attribute count did not change after removing an attribute",
|
||||
expectedReturn, actualReturn);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user