Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Arjan Tijms <arjan.tijms@gmail.com>
  • Loading branch information
arjantijms committed Nov 11, 2022
1 parent 5ef5956 commit 6ab752c
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -31,16 +32,12 @@
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509KeyManager;

import org.glassfish.hk2.api.ServiceLocator;
import org.apache.catalina.net.ServerSocketFactory;
import org.glassfish.internal.api.Globals;
import org.glassfish.security.common.SharedSecureRandomImpl;

//V3:Commented import com.sun.enterprise.ServerConfiguration;
//V3:Commented import com.sun.web.server.*;
//V3:Commented import com.sun.enterprise.server.J2EEServer;
import com.sun.enterprise.security.ssl.J2EEKeyManager;
import com.sun.enterprise.security.ssl.SSLUtils;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.logging.LogDomains;

/**
Expand All @@ -50,15 +47,12 @@
* @author Vivek Nagar
* @author Harpreet Singh
*/
// TODO: this should become a HK2 component
public class SSLSocketFactory implements org.apache.catalina.net.ServerSocketFactory {
public class SSLSocketFactory implements ServerSocketFactory {

static Logger _logger = LogDomains.getLogger(SSLSocketFactory.class, LogDomains.WEB_LOGGER);

private static final boolean clientAuth = false;

private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(SSLSocketFactory.class);

private SSLContext context;
private javax.net.ssl.SSLServerSocketFactory factory;
private String cipherSuites[];
Expand All @@ -68,7 +62,7 @@ public class SSLSocketFactory implements org.apache.catalina.net.ServerSocketFac

// XXX initStoresAtStartup may call more than once, should clean up later
// copied from SSLUtils : V3 to break dependency of this SSLUtils on this Class.
private static boolean initialized = false;
private static boolean initialized;

/**
* Create the SSL socket factory. Initialize the key managers and trust managers which are passed to the SSL context.
Expand Down Expand Up @@ -104,18 +98,8 @@ public SSLSocketFactory() {
public ServerSocket createSocket(int port) throws IOException {
SSLServerSocket socket = (SSLServerSocket) factory.createServerSocket(port);
init(socket);
return socket;
}

/**
* Specify whether the server will require client authentication.
*
* @param socket the SSL server socket.
*/
private void init(SSLServerSocket socket) {
// Some initialization goes here.....
// socket.setEnabledCipherSuites(cipherSuites);
socket.setNeedClientAuth(clientAuth);
return socket;
}

/**
Expand All @@ -128,6 +112,7 @@ private void init(SSLServerSocket socket) {
public ServerSocket createSocket(int port, int backlog) throws IOException {
SSLServerSocket socket = (SSLServerSocket) factory.createServerSocket(port, backlog);
init(socket);

return socket;
}

Expand All @@ -141,6 +126,7 @@ public ServerSocket createSocket(int port, int backlog) throws IOException {
public ServerSocket createSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
SSLServerSocket socket = (SSLServerSocket) factory.createServerSocket(port, backlog, ifAddress);
init(socket);

return socket;
}

Expand All @@ -155,8 +141,7 @@ public static synchronized void initStoresAtStartup() throws Exception {
return;
}

ServiceLocator habitat = Globals.getDefaultHabitat();
SSLUtils sslUtils = habitat.getService(SSLUtils.class);
SSLUtils sslUtils = Globals.getDefaultHabitat().getService(SSLUtils.class);

keyManagers = sslUtils.getKeyManagers();
trustManagers = sslUtils.getTrustManagers();
Expand All @@ -171,8 +156,18 @@ public static synchronized void initStoresAtStartup() throws Exception {
keyManagers[i] = new J2EEKeyManager((X509KeyManager) keyManagers[i], keyAlias);
}
}

sslContext.init(keyManagers, sslUtils.getTrustManagers(), null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
initialized = true;
}

/**
* Specify whether the server will require client authentication.
*
* @param socket the SSL server socket.
*/
private void init(SSLServerSocket socket) {
socket.setNeedClientAuth(clientAuth);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,7 +17,7 @@

package com.sun.enterprise.security.auth.login;

import java.util.logging.Level;
import static java.util.logging.Level.FINE;

import javax.security.auth.login.LoginException;

Expand Down Expand Up @@ -48,17 +49,16 @@ protected void authenticate() throws LoginException {
}
FileRealm fileRealm = (FileRealm) _currentRealm;

String[] grpList = fileRealm.authenticate(_username, getPasswordChar());
String[] groups = fileRealm.authenticate(_username, getPasswordChar());

if (grpList == null) { // JAAS behavior
String msg = sm.getString("filelm.faillogin", _username);
throw new LoginException(msg);
if (groups == null) { // JAAS behavior
throw new LoginException(sm.getString("filelm.faillogin", _username));
}

if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "File login succeeded for: " + _username);
if (_logger.isLoggable(FINE)) {
_logger.log(FINE, "File login succeeded for: " + _username);
}

commitAuthentication(_username, getPasswordChar(), _currentRealm, grpList);
commitAuthentication(_username, getPasswordChar(), _currentRealm, groups);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,6 +17,8 @@

package com.sun.enterprise.security.auth.login;

import static com.sun.enterprise.util.Utility.isEmpty;

import javax.security.auth.login.LoginException;

import com.sun.enterprise.security.auth.realm.ldap.LDAPRealm;
Expand Down Expand Up @@ -61,21 +64,20 @@ protected void authenticate() throws LoginException {
}
_ldapRealm = (LDAPRealm) _currentRealm;

// enforce that password cannot be empty.
// Enforce that password cannot be empty.
// ldap may grant login on empty password!
if (getPasswordChar() == null || getPasswordChar().length == 0) {
String msg = sm.getString("ldaplm.emptypassword", _username);
throw new LoginException(msg);
if (isEmpty(getPasswordChar())) {
throw new LoginException(sm.getString("ldaplm.emptypassword", _username));
}

String mode = _currentRealm.getProperty(LDAPRealm.PARAM_MODE);

if (LDAPRealm.MODE_FIND_BIND.equals(mode)) {
String[] grpList = _ldapRealm.findAndBind(_username, getPasswordChar());
commitAuthentication(_username, getPasswordChar(), _currentRealm, grpList);
} else {
String msg = sm.getString("ldaplm.badmode", mode);
throw new LoginException(msg);
if (!LDAPRealm.MODE_FIND_BIND.equals(mode)) {
throw new LoginException(sm.getString("ldaplm.badmode", mode));
}

String[] groups = _ldapRealm.findAndBind(_username, getPasswordChar());

commitAuthentication(_username, getPasswordChar(), _currentRealm, groups);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -26,7 +27,6 @@
* deprecation.
*
*/
@Deprecated
public abstract class PasswordLoginModule extends BasePasswordLoginModule {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,12 +17,14 @@

package com.sun.enterprise.security.auth.realm.ldap;

import static com.sun.enterprise.security.SecurityLoggerInfo.securityExceptionError;
import static java.util.logging.Level.WARNING;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Comparator;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.net.SocketFactory;
Expand All @@ -44,55 +47,46 @@
*
*/
public class CustomSocketFactory extends SocketFactory implements Comparator<SocketFactory> {
private SocketFactory socketFactory;

public static final String SSL = "SSL";

protected static final Logger _logger = SecurityLoggerInfo.getLogger();
protected static final StringManager sm = StringManager.getManager(CustomSocketFactory.class);

private static final CustomSocketFactory customSocketFactory = new CustomSocketFactory();

private SocketFactory socketFactory;

public CustomSocketFactory() {
SSLUtils sslUtils = Globals.getDefaultHabitat().getService(SSLUtils.class);
SSLContext sc = null;
SSLUtils sslUtils = Globals.get(SSLUtils.class);

try {
sc = SSLContext.getInstance(SSL);
sc.init(sslUtils.getKeyManagers(), sslUtils.getTrustManagers(), SharedSecureRandom.get());
socketFactory = sc.getSocketFactory();
SSLContext sslContext = SSLContext.getInstance(SSL);
sslContext.init(sslUtils.getKeyManagers(), sslUtils.getTrustManagers(), SharedSecureRandom.get());
socketFactory = sslContext.getSocketFactory();
} catch (Exception ex) {
_logger.log(Level.WARNING, SecurityLoggerInfo.securityExceptionError, ex);
_logger.log(WARNING, securityExceptionError, ex);
}
}

/**
* @see javax.net.SocketFactory#createSocket(java.lang.String, int)
*/
@Override
public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException {
return socketFactory.createSocket(arg0, arg1);
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return socketFactory.createSocket(host, port);
}

/**
* @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int)
*/
@Override
public Socket createSocket(InetAddress arg0, int arg1) throws IOException {
return socketFactory.createSocket(arg0, arg1);
public Socket createSocket(InetAddress host, int port) throws IOException {
return socketFactory.createSocket(host, port);
}

/**
* @see javax.net.SocketFactory#createSocket(java.lang.String, int, java.net.InetAddress, int)
*/
@Override
public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException {
return socketFactory.createSocket(arg0, arg1, arg2, arg3);
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return socketFactory.createSocket(host, port, localHost, localPort);
}

/**
* @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int, java.net.InetAddress, int)
*/
@Override
public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException {
return socketFactory.createSocket(arg0, arg1, arg2, arg3);
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return socketFactory.createSocket(address, port, localAddress, localPort);
}

@Override
Expand Down

0 comments on commit 6ab752c

Please sign in to comment.