* Fix a bug when the TURN ChannelData data is padded, it results of the ChannelData to be discarded;

* Allow remote candidates to be injected while ICE processing is running;
 * Support different remote ufrag/password for streams.
This commit is contained in:
seb 2010-12-06 16:41:58 +00:00
parent fc3e411657
commit 15b6780ccf
12 changed files with 230 additions and 89 deletions

View File

@ -105,6 +105,11 @@ public class MessageIntegrityAttribute
*/
private String username;
/**
* The media name if we use short-term authentication.
*/
private String media;
/**
* Creates a <tt>MessageIntegrityAttribute</tt>.
*/
@ -127,6 +132,17 @@ public class MessageIntegrityAttribute
this.username = username;
}
/**
* Sets the media name that we should use to get the corresponding remote
* key (short-term authentication only).
*
* @param media name
*/
public void setMedia(String media)
{
this.media = media;
}
/**
* Returns the HMAC-SHA1 value stored in this attribute.
*
@ -261,7 +277,8 @@ public class MessageIntegrityAttribute
if(Message.isRequestType(msgType))
{
/* attribute part of a request, use the remote key */
key = stunStack.getCredentialsManager().getRemoteKey(username);
key = stunStack.getCredentialsManager().getRemoteKey(username,
media);
}
else if(Message.isSuccessResponseType(msgType) ||
Message.isErrorResponseType(msgType))

View File

@ -141,16 +141,6 @@ public class Agent
*/
private final String password;
/**
* The user fragment that we received from the remote party.
*/
private String remoteUfrag;
/**
* The password that we received from the remote party.
*/
private String remotePassword;
/**
* The tie-breaker number is used in connectivity checks to detect and
* repair the case where both agents believe to have the controlling or the
@ -435,7 +425,8 @@ public class Agent
*/
public boolean isStarted()
{
return state != IceProcessingState.WAITING;
return state != IceProcessingState.WAITING &&
state != IceProcessingState.TERMINATED;
}
/**
@ -600,40 +591,6 @@ public class Agent
return password;
}
/**
* Returns the user name that we received from the remote peer or
* <tt>null</tt> if we haven't received a user name from them yet.
*
* @return the user name that we received from the remote peer or
* <tt>null</tt> if we haven't received a user name from them yet.
*/
public String getRemoteUfrag()
{
return remoteUfrag;
}
/**
* Returns the password that we received from the remote peer or
* <tt>null</tt> if we haven't received a password from them yet.
*
* @return the password that we received from the remote peer or
* <tt>null</tt> if we haven't received a password from them yet.
*/
public String getRemotePassword()
{
return remotePassword;
}
/**
* Specifies the user name that we received from the remote peer.
*
* @param remoteUfrag the user name that we received from the remote peer.
*/
public void setRemoteUfrag(String remoteUfrag)
{
this.remoteUfrag = remoteUfrag;
}
/**
* Returns the user name that this <tt>Agent</tt> should use in connectivity
* checks for outgoing Binding Requests. According to RFC 5245, a Binding
@ -650,12 +607,20 @@ public class Agent
* password of RPASS. A connectivity check from R to L (and its response)
* utilize the username LFRAG:RFRAG and a password of LPASS.
*
* @param media media name that we want to generate local username for.
* @return a user name that this <tt>Agent</tt> can use in connectivity
* check for outgoing Binding Requests.
*/
public String generateLocalUserName()
public String generateLocalUserName(String media)
{
return getRemoteUfrag() + ":" + getLocalUfrag();
IceMediaStream stream = getStream(media);
if(stream == null || stream.getRemoteUfrag() == null)
{
return null;
}
return stream.getRemoteUfrag() + ":" + getLocalUfrag();
}
/**
@ -675,23 +640,20 @@ public class Agent
* password of RPASS. A connectivity check from R to L (and its response)
* utilize the username LFRAG:RFRAG and a password of LPASS.
*
* @param media media name that we want to generate local username for.
* @return a user name that a peer <tt>Agent</tt> would use in connectivity
* check for outgoing Binding Requests.
*/
public String generateRemoteUserName()
public String generateRemoteUserName(String media)
{
return getLocalUfrag() + ":" + getRemoteUfrag();
}
IceMediaStream stream = getStream(media);
/**
* Specifies the password that we received from the remote peer.
*
* @param remotePassword the user name that we received from the remote
* peer.
*/
public void setRemotePassword(String remotePassword)
{
this.remotePassword = remotePassword;
if(stream == null)
{
return null;
}
return getLocalUfrag() + ":" + stream.getRemoteUfrag();
}
/**

View File

@ -22,6 +22,7 @@ import org.ice4j.*;
*
* @author Emil Ivov
* @author Namal Senarathne
* @author Sebastien Vincent
*/
public class Component
{
@ -75,6 +76,13 @@ public class Component
*/
private List<Candidate> remoteCandidates = new LinkedList<Candidate>();
/**
* The list of candidates that the peer agent sent for this stream after
* connectivity establishment.
*/
private List<Candidate> remoteUpdateCandidates =
new LinkedList<Candidate>();
/**
* A <tt>Comparator</tt> that we use for sorting <tt>Candidate</tt>s by
* their priority.
@ -204,6 +212,71 @@ public class Component
}
}
/**
* Update the media-stream <tt>Component</tt> with the specified
* <tt>Candidate</tt>.
*
* @param candidate new <tt>Candidate</tt> to add.
*/
public void addUpdateRemoteCandidate(Candidate candidate)
{
synchronized(remoteUpdateCandidates)
{
remoteUpdateCandidates.add(candidate);
}
}
/**
* Update ICE processing with the new <tt>Candidate</tt>s.
*/
public void updateRemoteCandidate()
{
List<CandidatePair> checkList = null;
synchronized(remoteUpdateCandidates)
{
if(remoteUpdateCandidates.size() == 0)
return;
List<LocalCandidate> localCnds = getLocalCandidates();
checkList = new Vector<CandidatePair>();
for(LocalCandidate localCnd : localCnds)
{
for(Candidate remoteCnd : remoteUpdateCandidates)
{
if(localCnd.canReach(remoteCnd))
{
CandidatePair pair = new CandidatePair(localCnd, remoteCnd);
checkList.add(pair);
}
}
}
remoteUpdateCandidates.clear();
}
/* sort and prune update checklist */
Collections.sort(checkList, CandidatePair.comparator);
parentStream.pruneCheckList(checkList);
if(parentStream.getCheckList().getState().equals(
CheckListState.RUNNING))
{
/* add the update CandidatePair list to the currently running
* checklist
*/
CheckList streamCheckList = parentStream.getCheckList();
synchronized(streamCheckList)
{
for(CandidatePair pair : checkList)
{
streamCheckList.add(pair);
}
}
}
}
/**
* Returns a copy of the list containing all remote candidates currently
* registered in this component.
@ -747,7 +820,7 @@ public class Component
* with this component.
*
* @return "RTP" if the component ID is 1, "RTCP" if the component id is 2
* and the compoent id itself otherwise.
* and the component id itself otherwise.
*/
public String getName()
{

View File

@ -147,7 +147,9 @@ class ConnectivityCheckClient
}
//credentials
String localUserName = parentAgent.generateLocalUserName();
String localUserName = parentAgent.generateLocalUserName(
candidatePair.getParentComponent().getParentStream().getName());
UsernameAttribute unameAttr = AttributeFactory.createUsernameAttribute(
localUserName);
@ -157,6 +159,12 @@ class ConnectivityCheckClient
MessageIntegrityAttribute msgIntegrity = AttributeFactory
.createMessageIntegrityAttribute(localUserName);
// when we will encode the MESSAGE-INTEGRITY attribute (thus generate
// the HMAC-SHA1 authentication), we need to know the remote key of the
// current stream, that why we pass the media name.
msgIntegrity.setMedia(
candidatePair.getParentComponent().getParentStream().getName());
request.addAttribute(msgIntegrity);
TransactionID tran = TransactionID.createNewTransactionID();

View File

@ -125,12 +125,13 @@ class ConnectivityCheckServer
request, evt.getRemoteAddress());
/* add USERNAME and MESSAGE-INTEGRITY attribute in the response */
Attribute usernameAttribute = AttributeFactory.createUsernameAttribute(
parentAgent.generateLocalUserName());
/* The responses utilize the same usernames and passwords as the
* requests
*/
Attribute usernameAttribute =
AttributeFactory.createUsernameAttribute(uname.getUsername());
Attribute messageIntegrityAttribute =
AttributeFactory.createMessageIntegrityAttribute(
new String(uname.getUsername()));
@ -362,29 +363,36 @@ class ConnectivityCheckServer
* are expected to use when querying the remote peer.
*
* @param username the remote ufrag that we should return a password for.
* @param media the media name that we want to get remote key.
*
* @return this handler's parent agent remote password if <tt>username</tt>
* equals the remote ufrag and <tt>null</tt> otherwise.
*/
public byte[] getRemoteKey(String username)
public byte[] getRemoteKey(String username, String media)
{
//support both the case where username is the local fragment or the
//entire user name.
int colon = username.indexOf(":");
IceMediaStream stream = parentAgent.getStream(media);
if(stream == null)
{
return null;
}
if (colon < 0)
{
//caller gave us a ufrag
if (username.equals(parentAgent.getRemoteUfrag()))
return parentAgent.getRemotePassword().getBytes();
if (username.equals(stream.getRemoteUfrag()))
return stream.getRemotePassword().getBytes();
}
else
{
//caller gave us the entire username.
if (username.equals(parentAgent.generateLocalUserName()))
if (username.equals(parentAgent.generateLocalUserName(media)))
{
if(parentAgent.getRemotePassword() != null)
return parentAgent.getRemotePassword().getBytes();
if(stream.getRemotePassword() != null)
return stream.getRemotePassword().getBytes();
}
}
return null;

View File

@ -106,6 +106,16 @@ public class IceMediaStream
*/
private int maxCheckListSize = Agent.DEFAULT_MAX_CHECK_LIST_SIZE;
/**
* The user fragment that we received from the remote party.
*/
private String remoteUfrag = null;
/**
* The password that we received from the remote party.
*/
private String remotePassword = null;
/**
* Initializes a new <tt>IceMediaStream</tt> object.
*
@ -309,7 +319,7 @@ public class IceMediaStream
createCheckList(checkList);
orderCheckList();
pruneCheckList();
pruneCheckList(checkList);
logger.finest("Checklist initialized.");
}
}
@ -377,8 +387,10 @@ public class IceMediaStream
* In addition, in order to limit the attacks described in Section 18.5.2
* of the ICE spec, we limit the total number of pairs and hence
* (connectivity checks) to a specific value, (a total of 100 by default).
*
* @param checkList the checklist to prune
*/
private void pruneCheckList()
protected void pruneCheckList(List<CandidatePair> checkList)
{
//a list that we only use for storing pairs that we've already gone
//through. The list is destroyed at the end of this method.
@ -803,4 +815,49 @@ public class IceMediaStream
return null;
}
/**
* Specifies the user name that we received from the remote peer.
*
* @param remoteUfrag the user name that we received from the remote peer.
*/
public void setRemoteUfrag(String remoteUfrag)
{
this.remoteUfrag = remoteUfrag;
}
/**
* Returns the user name that we received from the remote peer or
* <tt>null</tt> if we haven't received a user name from them yet.
*
* @return the user name that we received from the remote peer or
* <tt>null</tt> if we haven't received a user name from them yet.
*/
public String getRemoteUfrag()
{
return remoteUfrag;
}
/**
* Specifies the password that we received from the remote peer.
*
* @param remotePassword the user name that we received from the remote
* peer.
*/
public void setRemotePassword(String remotePassword)
{
this.remotePassword = remotePassword;
}
/**
* Returns the password that we received from the remote peer or
* <tt>null</tt> if we haven't received a password from them yet.
*
* @return the password that we received from the remote peer or
* <tt>null</tt> if we haven't received a password from them yet.
*/
public String getRemotePassword()
{
return remotePassword;
}
}

View File

@ -39,13 +39,14 @@ public interface CredentialsAuthority
*
* @param username the remote user name or user frag whose credentials we'd
* like to obtain.
* @param media the media name that we want to get remote key.
*
* @return the key (password) that corresponds to the specified remote
* username or user frag, an empty array if there was no password for that
* username or <tt>null</tt> if the username is not a remote user name
* recognized by this <tt>CredentialsAuthority</tt>.
*/
public byte[] getRemoteKey(String username);
public byte[] getRemoteKey(String username, String media);
/**
* Verifies whether <tt>username</tt> is currently known to this authority

View File

@ -67,17 +67,18 @@ public class CredentialsManager
*
* @param username a remote user name or user frag whose credentials we'd
* like to obtain.
* @param media the media name that we want to get remote key.
*
* @return <tt>null</tt> if username was not a recognized remote user name
* for none of the currently registered <tt>CredentialsAuthority</tt>s or
* a <tt>byte</tt> array containing the first non-<tt>null</tt> password
* that one of them returned.
*/
public byte[] getRemoteKey(String username)
public byte[] getRemoteKey(String username, String media)
{
for (CredentialsAuthority auth : authorities)
{
byte[] passwd = auth.getRemoteKey(username);
byte[] passwd = auth.getRemoteKey(username, media);
if (passwd != null)
{

View File

@ -195,13 +195,14 @@ public class LongTermCredentialSession
*
* @param username the remote user name or user frag whose credentials
* we'd like to obtain
* @param media not used
* @return the key (password) that corresponds to the specified remote
* username or user frag, an empty array if there was no password for
* that username or <tt>null</tt> if the username is not a remote user
* name recognized by this <tt>CredentialsAuthority</tt>
* @see CredentialsAuthority#getRemoteKey(String)
*/
public byte[] getRemoteKey(String username)
public byte[] getRemoteKey(String username, String media)
{
// The password is the same on the local and the remote sides.
return getLocalKey(username);

View File

@ -67,7 +67,7 @@ public class RelayedCandidateDatagramSocket
/**
* The minimum channel number which is valid for TURN ChannelBind
* <tt>Request</tt>s.
* <tt>Request</tt>s.
*/
private static final char MIN_CHANNEL_NUMBER = 0x4000;
@ -79,7 +79,7 @@ public class RelayedCandidateDatagramSocket
/**
* The time in milliseconds before a TURN permission expires that a
* <tt>RelayedCandidateDatagramSocket</tt> is to try to reinstall it.
* <tt>RelayedCandidateDatagramSocket</tt> is to try to reinstall it.
*/
private static final long PERMISSION_LIFETIME_LEEWAY
= 60 /* seconds */ * 1000L;
@ -242,7 +242,8 @@ public class RelayedCandidateDatagramSocket
private boolean channelDataSocketAccept(DatagramPacket p)
{
// Is it from our TURN server?
if (turnCandidateHarvest.harvester.stunServer.equals(p.getSocketAddress()))
if (turnCandidateHarvest.harvester.stunServer.equals(
p.getSocketAddress()))
{
int pLength = p.getLength();
@ -278,13 +279,16 @@ public class RelayedCandidateDatagramSocket
((pData[pOffset++] << 8)
| (pData[pOffset++] & 0xFF));
int padding = ((length % 4) > 0) ? 4 - (length % 4) : 0;
/*
* The Length field specifies the length in bytes of the
* Application Data field. The Length field does not include
* the padding that is sometimes present in the data of the
* DatagramPacket.
*/
return ((CHANNELDATA_LENGTH_LENGTH + length) >= pLength);
return (length ==
(pLength - padding - CHANNELDATA_LENGTH_LENGTH));
}
}
}
@ -547,7 +551,7 @@ public class RelayedCandidateDatagramSocket
/*
* RFC 5766: When the client receives a Data indication, it checks that
* the Data indication contains both an XOR-PEER-ADDRESS and a DATA
* attribute, and discards the indication if it does not.
* attribute, and discards the indication if it does not.
*/
XorPeerAddressAttribute peerAddressAttribute
= (XorPeerAddressAttribute)
@ -1312,7 +1316,7 @@ public class RelayedCandidateDatagramSocket
/**
* Gets the indication which determines whether this instance is
* currently considered bound/installed.
*
*
* @return <tt>true</tt> if this instance is currently considered
* bound/installed; otherwise, <tt>false</tt>
*/
@ -1502,7 +1506,7 @@ public class RelayedCandidateDatagramSocket
*
* @param channelNumberIsConfirmed <tt>true</tt> if allocation of the
* channel number has been confirmed by a success <tt>Response</tt> to
* our ChannelBind <tt>Request</tt>
* our ChannelBind <tt>Request</tt>
* @param channelNumberIsConfirmedTransactionID an array of
* <tt>byte</tt>s which represents the ID of the transaction with which
* the confirmation about the allocation of the channel number has

View File

@ -63,14 +63,20 @@ public class Ice
long endTime = System.currentTimeMillis();
transferRemoteCandidates(localAgent, remotePeer);
localAgent.setRemoteUfrag(remotePeer.getLocalUfrag());
localAgent.setRemotePassword(remotePeer.getLocalPassword());
for(IceMediaStream stream : localAgent.getStreams())
{
stream.setRemoteUfrag(remotePeer.getLocalUfrag());
stream.setRemotePassword(remotePeer.getLocalPassword());
}
if (START_CONNECTIVITY_ESTABLISHMENT_OF_REMOTE_PEER)
transferRemoteCandidates(remotePeer, localAgent);
remotePeer.setRemoteUfrag(localAgent.getLocalUfrag());
remotePeer.setRemotePassword(localAgent.getLocalPassword());
for(IceMediaStream stream : remotePeer.getStreams())
{
stream.setRemoteUfrag(localAgent.getLocalUfrag());
stream.setRemotePassword(localAgent.getLocalPassword());
}
System.out.println("Total candidate gathering time: "
+ (endTime - startTime) + "ms");

View File

@ -119,8 +119,11 @@ public class SdpUtils
SdpFactory factory = SdpFactory.getInstance();
SessionDescription sdess = factory.createSessionDescription(sdp);
localAgent.setRemotePassword(sdess.getAttribute("ice-pwd"));
localAgent.setRemoteUfrag(sdess.getAttribute("ice-ufrag"));
for(IceMediaStream stream : localAgent.getStreams())
{
stream.setRemotePassword(sdess.getAttribute("ice-pwd"));
stream.setRemoteUfrag(sdess.getAttribute("ice-ufrag"));
}
Connection globalConn = sdess.getConnection();
String globalConnAddr = null;