From 75231a7e3eca10ff90304f7ff44668136daf1969 Mon Sep 17 00:00:00 2001 From: emcho Date: Tue, 27 Apr 2010 23:32:18 +0000 Subject: [PATCH] Delays necessity of a password for outgoing checks so that we could handle the OFFER check ANSWER case. --- .../attribute/MessageIntegrityAttribute.java | 2 +- .../ice4j/ice/ConnectivityCheckServer.java | 40 +++++++++++++-- src/org/ice4j/message/Message.java | 4 +- .../ice4j/security/CredentialsAuthority.java | 39 ++++++++++---- .../ice4j/security/CredentialsManager.java | 51 +++++++++++++++---- 5 files changed, 107 insertions(+), 29 deletions(-) diff --git a/src/org/ice4j/attribute/MessageIntegrityAttribute.java b/src/org/ice4j/attribute/MessageIntegrityAttribute.java index 5c5fbfe..799d9df 100644 --- a/src/org/ice4j/attribute/MessageIntegrityAttribute.java +++ b/src/org/ice4j/attribute/MessageIntegrityAttribute.java @@ -255,7 +255,7 @@ public class MessageIntegrityAttribute binValue[3] = (byte)(getDataLength()&0x00FF); byte[] key = StunStack.getInstance().getCredentialsManager() - .getKey(username); + .getLocalKey(username); //now calculate the HMAC-SHA1 this.hmacSha1Content = calculateHmacSha1(content, offset, length, key); diff --git a/src/org/ice4j/ice/ConnectivityCheckServer.java b/src/org/ice4j/ice/ConnectivityCheckServer.java index 429b0db..dd23d92 100644 --- a/src/org/ice4j/ice/ConnectivityCheckServer.java +++ b/src/org/ice4j/ice/ConnectivityCheckServer.java @@ -63,7 +63,7 @@ public class ConnectivityCheckServer .getAttribute(Attribute.USERNAME); if(uname == null - || !checkUserName(new String(uname.getUsername()))) + || !checkLocalUserName(new String(uname.getUsername()))) { return; } @@ -92,7 +92,7 @@ public class ConnectivityCheckServer * @return true if username is known to this * ConnectivityCheckServer and false otherwise. */ - public boolean checkUserName(String username) + public boolean checkLocalUserName(String username) { boolean accept = false; int colon = username.indexOf(":"); @@ -112,7 +112,7 @@ public class ConnectivityCheckServer } /** - * Implements the {@link CredentialsAuthority#getKey(String)} method in a + * Implements the {@link CredentialsAuthority#getLocalKey(String)} method in a * way that would return this handler's parent agent password if * username is either the local ufrag or the username that the * agent's remote peer was expected to use. @@ -122,7 +122,7 @@ public class ConnectivityCheckServer * @return this handler's parent agent local password if username * equals the local ufrag and null otherwise. */ - public byte[] getKey(String username) + public byte[] getLocalKey(String username) { //support both the case where username is the local fragment or the //entire user name. @@ -142,4 +142,36 @@ public class ConnectivityCheckServer return null; } + + /** + * Implements the {@link CredentialsAuthority#getRemoteKey(String)} method + * in a way that would return this handler's parent agent remote password if + * username is either the remote ufrag or the username that we + * are expected to use when querying the remote peer. + * + * @param username the remote ufrag that we should return a password for. + * + * @return this handler's parent agent remote password if username + * equals the remote ufrag and null otherwise. + */ + public byte[] getRemoteKey(String username) + { + //support both the case where username is the local fragment or the + //entire user name. + int colon = username.indexOf(":"); + if( colon < 0) + { + //caller gave us a ufrag + if (username.equals(parentAgent.getRemoteUfrag())) + return parentAgent.getRemotePassword().getBytes(); + } + else + { + //caller gave us the entire username. + if (username.equals(parentAgent.generateLocalUserName())) + return parentAgent.getRemotePassword().getBytes(); + } + + return null; + } } diff --git a/src/org/ice4j/message/Message.java b/src/org/ice4j/message/Message.java index dac72b1..8d40dbc 100644 --- a/src/org/ice4j/message/Message.java +++ b/src/org/ice4j/message/Message.java @@ -844,7 +844,7 @@ public abstract class Message String lfrag = username.substring(0, colon); byte[] key = StunStack.getInstance() - .getCredentialsManager().getKey(lfrag); + .getCredentialsManager().getLocalKey(lfrag); if(key == null) return false; @@ -898,7 +898,7 @@ public abstract class Message String lfrag = username.substring(0, colon); return StunStack.getInstance() - .getCredentialsManager().checkUserName(lfrag); + .getCredentialsManager().checkLocalUserName(lfrag); } /** diff --git a/src/org/ice4j/security/CredentialsAuthority.java b/src/org/ice4j/security/CredentialsAuthority.java index 7ce4b23..2bc0dd5 100644 --- a/src/org/ice4j/security/CredentialsAuthority.java +++ b/src/org/ice4j/security/CredentialsAuthority.java @@ -16,19 +16,36 @@ package org.ice4j.security; public interface CredentialsAuthority { /** - * Returns the key (password) that corresponds to the specified username, - * an empty array if there was no password for that username or - * null if the username is not known to this - * CredentialsAuthority. + * Returns the key (password) that corresponds to the specified local + * username or user frag, an empty array if there was no password for that + * username or null if the username is not a local user name + * recognized by this CredentialsAuthority. * - * @param username the user name whose credentials we'd like to obtain. + * @param username the local user name or user frag whose credentials we'd + * like to obtain. * - * @return the key (password) that corresponds to the specified username, - * an empty array if there was no password for that username or - * null if the username is not known to this - * CredentialsAuthority. + * @return the key (password) that corresponds to the specified local + * username or user frag, an empty array if there was no password for that + * username or null if the username is not a local user name + * recognized by this CredentialsAuthority. */ - public byte[] getKey(String username); + public byte[] getLocalKey(String username); + + /** + * Returns 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 null if the username is not a remote user name + * recognized by this CredentialsAuthority. + * + * @param username the remote user name or user frag whose credentials we'd + * like to obtain. + * + * @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 null if the username is not a remote user name + * recognized by this CredentialsAuthority. + */ + public byte[] getRemoteKey(String username); /** * Verifies whether username is currently known to this authority @@ -39,5 +56,5 @@ public interface CredentialsAuthority * @return true if username is known to this * CredentialsAuthority and false otherwise. */ - public boolean checkUserName(String username); + public boolean checkLocalUserName(String username); } diff --git a/src/org/ice4j/security/CredentialsManager.java b/src/org/ice4j/security/CredentialsManager.java index c41ade8..e666b86 100644 --- a/src/org/ice4j/security/CredentialsManager.java +++ b/src/org/ice4j/security/CredentialsManager.java @@ -35,21 +35,50 @@ public class CredentialsManager /** * Queries all currently registered {@link CredentialsAuthority}s for a - * password corresponding to username and returns the first - * non-null one. + * password corresponding to the specified local username or user + * frag and returns the first non-null one. * - * @param username the user name whose credentials we'd like to obtain. + * @param username a local user name or user frag whose credentials we'd + * like to obtain. * - * @return null if username was not known to neither of the - * currently registered CredentialsAuthoritys or a byte - * array containing the first non-null password that one of them - * returned. + * @return null if username was not a recognized local user name + * for none of the currently registered CredentialsAuthoritys or + * a byte array containing the first non-null password + * that one of them returned. */ - public byte[] getKey(String username) + public byte[] getLocalKey(String username) { for (CredentialsAuthority auth : authorities) { - byte[] passwd = auth.getKey(username); + byte[] passwd = auth.getLocalKey(username); + + if (passwd != null) + { + return passwd; + } + } + + return null; + } + + /** + * Queries all currently registered {@link CredentialsAuthority}s for a + * password corresponding to the specified remote username or user + * frag and returns the first non-null one. + * + * @param username a remote user name or user frag whose credentials we'd + * like to obtain. + * + * @return null if username was not a recognized remote user name + * for none of the currently registered CredentialsAuthoritys or + * a byte array containing the first non-null password + * that one of them returned. + */ + public byte[] getRemoteKey(String username) + { + for (CredentialsAuthority auth : authorities) + { + byte[] passwd = auth.getRemoteKey(username); if (passwd != null) { @@ -72,11 +101,11 @@ public class CredentialsManager * CredentialsAuthoritys registered here and false * otherwise. */ - public boolean checkUserName(String username) + public boolean checkLocalUserName(String username) { for (CredentialsAuthority auth : authorities) { - if( auth.checkUserName(username)) + if( auth.checkLocalUserName(username)) return true; }