Skip to content

Commit e2aa50f

Browse files
author
kasemir
committed
Better "CN=..." decoding from principal
1 parent a2e146a commit e2aa50f

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

core/pva/src/main/java/org/epics/pva/common/SecureSockets.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.security.KeyStore;
1717
import java.util.logging.Level;
1818

19+
import javax.naming.ldap.LdapName;
20+
import javax.naming.ldap.Rdn;
1921
import javax.net.ssl.KeyManagerFactory;
2022
import javax.net.ssl.SSLContext;
2123
import javax.net.ssl.SSLServerSocket;
@@ -173,18 +175,16 @@ public static Socket createClientSocket(final InetSocketAddress address, final b
173175
/** Get name from local principal
174176
*
175177
* @param socket {@link SSLSocket} that may have local principal
176-
* @return Name (without "CN=..") or <code>null</code> if socket has certificate to authenticate
178+
* @return Name (without "CN=..") if socket has certificate to authenticate or <code>null</code>
177179
*/
178180
public static String getLocalPrincipalName(final SSLSocket socket)
179181
{
180182
try
181183
{
182-
String name = socket.getSession().getLocalPrincipal().getName();
183-
if (name.startsWith("CN="))
184-
name = name.substring(3);
185-
else
186-
logger.log(Level.WARNING, "Client has principal '" + name + "', expected 'CN=...'");
187-
return name;
184+
final LdapName ldn = new LdapName(socket.getSession().getLocalPrincipal().getName());
185+
for (Rdn rdn : ldn.getRdns())
186+
if (rdn.getType().equals("CN"))
187+
return (String) rdn.getValue();
188188
}
189189
catch (Exception ex)
190190
{ // May not have certificate with name

0 commit comments

Comments
 (0)