Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marshalling byte[] based on JDK class. #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>

<artifactId>ice4j</artifactId>
<version>2.0-SNAPSHOT</version>
<version>2.0-mami-SNAPSHOT</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accidentally renamed?

<packaging>bundle</packaging>

<name>ice4j</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package org.ice4j.attribute;

import org.ice4j.stack.*;

/**
* <tt>ContentDependentAttribute</tt>s have a value that depend on the content
* of the message. The {@link MessageIntegrityAttribute} and {@link
Expand All @@ -35,7 +33,7 @@ public interface ContentDependentAttribute
/**
* Returns a binary representation of this attribute.
*
* @param stunStack the <tt>StunStack</tt> in the context of which the
* @param context the context of which the
* request to encode this <tt>ContentDependentAttribute</tt> is being made
* @param content the content of the message that this attribute will be
* transported in
Expand All @@ -47,6 +45,6 @@ public interface ContentDependentAttribute
* with the specified <tt>content</tt>.
*/
public byte[] encode(
StunStack stunStack,
KeysDependentAttributeContext context,
byte[] content, int offset, int length);
}
5 changes: 2 additions & 3 deletions src/main/java/org/ice4j/attribute/FingerprintAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.zip.*;

import org.ice4j.*;
import org.ice4j.stack.*;

/**
* The FINGERPRINT attribute is used to distinguish STUN packets from packets
Expand Down Expand Up @@ -157,7 +156,7 @@ public byte[] encode()
/**
* Returns a binary representation of this attribute.
*
* @param stunStack the <tt>StunStack</tt> in the context of which the
* @param context the context of which the
* request to encode this <tt>ContentDependentAttribute</tt> is being made
* @param content the content of the message that this attribute will be
* transported in
Expand All @@ -169,7 +168,7 @@ public byte[] encode()
* with the specified <tt>content</tt>.
*/
public byte[] encode(
StunStack stunStack,
KeysDependentAttributeContext context,
byte[] content, int offset, int length)
{
char type = getAttributeType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.ice4j.attribute;

public interface KeysDependentAttributeContext {

byte[] getRemoteKey(String username, String media);

byte[] getLocalKey(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import javax.crypto.spec.*;

import org.ice4j.message.*;
import org.ice4j.stack.*;

/**
* The MESSAGE-INTEGRITY attribute contains an HMAC-SHA1 [RFC2104] of
Expand Down Expand Up @@ -253,7 +252,7 @@ public byte[] encode()
/**
* Returns a binary representation of this attribute.
*
* @param stunStack the <tt>StunStack</tt> in the context of which the
* @param context the context of which the
* request to encode this <tt>ContentDependentAttribute</tt> is being made
* @param content the content of the message that this attribute will be
* transported in
Expand All @@ -265,7 +264,7 @@ public byte[] encode()
* with the specified <tt>content</tt>.
*/
public byte[] encode(
StunStack stunStack,
KeysDependentAttributeContext context,
byte[] content, int offset, int length)
{
char type = getAttributeType();
Expand All @@ -286,14 +285,14 @@ public byte[] encode(
if(Message.isRequestType(msgType))
{
/* attribute part of a request, use the remote key */
key = stunStack.getCredentialsManager().getRemoteKey(username,
key = context.getRemoteKey(username,
media);
}
else if(Message.isSuccessResponseType(msgType) ||
Message.isErrorResponseType(msgType))
{
/* attribute part of a response, use the local key */
key = stunStack.getCredentialsManager().getLocalKey(username);
key = context.getLocalKey(username);
}

//now calculate the HMAC-SHA1
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/org/ice4j/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -889,17 +889,34 @@ public boolean equals(Object obj)
return true;
}

@Deprecated
public byte[] encode(final StunStack stunStack)
throws IllegalStateException
{
return encode(new KeysDependentAttributeContext() {
@Override
public byte[] getRemoteKey(String username, String media) {
return stunStack.getCredentialsManager().getRemoteKey(username, media);
}

@Override
public byte[] getLocalKey(String username) {
return stunStack.getCredentialsManager().getLocalKey(username);
}
});
}

/**
* Returns a binary representation of this message.
*
* @param stunStack the <tt>StunStack</tt> in the context of which the
* @param keysContext the context of which the
* request to encode this <tt>Message</tt> is being made
* @return a binary representation of this message.
*
* @throws IllegalStateException if the message does not have all
* required attributes.
*/
public byte[] encode(StunStack stunStack)
public byte[] encode(KeysDependentAttributeContext keysContext)
throws IllegalStateException
{
prepareForEncoding();
Expand Down Expand Up @@ -977,7 +994,7 @@ public byte[] encode(StunStack stunStack)
= (byte)(dataLengthForContentDependentAttribute & 0xFF);
binAtt
= ((ContentDependentAttribute)attribute)
.encode(stunStack, binMsg, 0, offset);
.encode(keysContext, binMsg, 0, offset);
}
else
{
Expand Down
24 changes: 2 additions & 22 deletions src/main/java/org/ice4j/stack/StunStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.logging.*;

import javax.crypto.*;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;

import org.ice4j.*;
import org.ice4j.attribute.*;
Expand Down Expand Up @@ -1362,28 +1363,7 @@ public boolean validateMessageIntegrity(
*/
private static String toHexString(byte[] bytes)
{
if (bytes == null)
return null;
else
{
StringBuilder hexStringBuilder
= new StringBuilder(2 * bytes.length);
char[] hexes
= new char[]
{
'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'
};

for (int i = 0; i < bytes.length; i++)
{
byte b = bytes[i];

hexStringBuilder.append(hexes[(b & 0xF0) >> 4]);
hexStringBuilder.append(hexes[b & 0x0F]);
}
return hexStringBuilder.toString();
}
return new HexBinaryAdapter().marshal(bytes);
}

/**
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/ice4j/message/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@

import org.ice4j.*;
import org.ice4j.attribute.*;
import org.ice4j.security.LongTermCredential;
import org.ice4j.security.LongTermCredentialSession;
import org.ice4j.stack.*;

import javax.xml.bind.annotation.adapters.HexBinaryAdapter;

public class MessageTest extends TestCase
{
private Message bindingRequest = null;
Expand Down Expand Up @@ -281,4 +285,45 @@ public void testRemoveAttribute()
"Attribute count did not change after removing an attribute",
expectedReturn, actualReturn);
}

private static byte[] unmarshal(String hex) {
return new HexBinaryAdapter().unmarshal(hex);
}

private static String marshal(byte[] bytes) {
return new HexBinaryAdapter().marshal(bytes);
}


public void testEncodeWithKeys() throws StunException {
Request request = MessageFactory.createAllocateRequest();
request.setTransactionID(unmarshal("7766497a70656b5a357a4530"));
request.putAttribute(AttributeFactory.createRequestedTransportAttribute((byte)0x11));
String username = "mamiusername";
String password = "mamipassword";
byte[] realm = unmarshal("6c69766562616e6b2e6c6f63616c");
request.putAttribute(AttributeFactory.createUsernameAttribute(username));
request.putAttribute(AttributeFactory.createRealmAttribute(realm));
request.putAttribute(AttributeFactory.createNonceAttribute(unmarshal("3239336133663862346462643461626662643037313766393934303035396363")));
request.putAttribute(AttributeFactory.createMessageIntegrityAttribute(username));

final LongTermCredentialSession longTerm = new LongTermCredentialSession(new LongTermCredential(username, password), realm);

byte[] requestHex = request.encode(new KeysDependentAttributeContext() {

@Override
public byte[] getRemoteKey(String username, String media) {
return longTerm.getRemoteKey(username, media);
}

@Override
public byte[] getLocalKey(String username) {
return longTerm.getLocalKey(username);
}
});
String resultHex = marshal(requestHex);
String expectedHex ="000300682112A4427766497A70656B5A357A453000190004110000000006000C6D616D69757365726E616D650014000E6C69766562616E6B2E6C6F63616C00000015002032393361336638623464626434616266626430373137663939343030353963630008001433E767B5433E897A4A9EF38807153D81AF47D262";
assertEquals(expectedHex, resultHex);
//System.out.println("requestHexString: "+requestHexString);
}
}