Skip to content

Commit

Permalink
Push IPs address to SSLEngine session
Browse files Browse the repository at this point in the history
**SSLEngine** is by design unaware of the underlying communication channel.
In tomcat the communication channel is started by the classes
`NioEndpoint` and it is maintained in `SecureNioChannel` which will
create the buffer used with the SSLEngine in order to wrap and unwrap
the messages.

To allow the audit of TLS messages to include IP addresses of the client
and server, the above classed have been extended in order to store the
IPs in the SSLEngine session after its creation.

Replace the tomcatJSS PR#73
(dogtagpki/tomcatjss#73)
  • Loading branch information
fmarco76 committed Aug 28, 2023
1 parent 4742271 commit 8791361
Show file tree
Hide file tree
Showing 3 changed files with 527 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
/* BEGIN COPYRIGHT BLOCK
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Copyright (C) 2017 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK */
package org.dogtagpki.jss.tomcat;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.coyote.http11.AbstractHttp11JsseProtocol;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.net.NioChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Http11NioProtocol extends org.apache.coyote.http11.Http11NioProtocol {
public class Http11NioProtocol extends AbstractHttp11JsseProtocol<NioChannel> {

public static Logger logger = LoggerFactory.getLogger(Http11NioProtocol.class);
private static final Log log = LogFactory.getLog(Http11NioProtocol.class);

TomcatJSS tomcatjss = TomcatJSS.getInstance();

public Http11NioProtocol() {
super(new JSSNioEndpoint());
}

public String getCertdbDir() {
return tomcatjss.getCertdbDir();
}
Expand Down Expand Up @@ -123,4 +150,34 @@ public void setTruststorePassFile(String truststorePassFile) {
throw new RuntimeException(e);
}
}

@Override
protected Log getLog() {
return log;
}

@Override
protected String getNamePrefix() {
if (isSSLEnabled()) {
return "https-" + getSslImplementationShortName()+ "-jss-nio";
}
return "http-jss-nio";
}

// These methods are temporarly present to replicate the default behaviour provided by tomcat
public void setSelectorTimeout(long timeout) {
((JSSNioEndpoint)getEndpoint()).setSelectorTimeout(timeout);
}

public long getSelectorTimeout() {
return ((JSSNioEndpoint)getEndpoint()).getSelectorTimeout();
}

public void setPollerThreadPriority(int threadPriority) {
((JSSNioEndpoint)getEndpoint()).setPollerThreadPriority(threadPriority);
}

public int getPollerThreadPriority() {
return ((JSSNioEndpoint)getEndpoint()).getPollerThreadPriority();
}
}
133 changes: 133 additions & 0 deletions tomcat/src/main/java/org/dogtagpki/jss/tomcat/JSSNioEndpoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* BEGIN COPYRIGHT BLOCK
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Copyright (C) 2017 Red Hat, Inc.
* All rights reserved.
*
*
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Apache Tomcat
* Copyright 1999-2023 The Apache Software Foundation
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. *
* END COPYRIGHT BLOCK */

package org.dogtagpki.jss.tomcat;


import java.nio.channels.SocketChannel;
import java.util.List;

import javax.net.ssl.SSLEngine;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.net.NioChannel;
import org.apache.tomcat.util.net.NioEndpoint;
import org.apache.tomcat.util.net.SocketBufferHandler;
import org.apache.tomcat.util.net.openssl.ciphers.Cipher;

public class JSSNioEndpoint extends NioEndpoint {

private static final Log log = LogFactory.getLog(NioEndpoint.class);
/**
* Code in the following method is almost identical of that available in the base
* class {@link org.apache.tomcat.util.net.NioEndpoint#setSocketOptions(SocketChannel) from tomcat
* git repository for the version 9.0.78..
* <p>
* The only difference is the instantiation of the JSSSecureNioChannel class instead of the tomcat
* provided SecureNioChannel class. This is needed because the channel class is hard-coded in the
* base class method.
*
* @see org.apache.tomcat.util.net.NioEndpoint#setSocketOptions(SocketChannel socket)
*/

@Override
protected boolean setSocketOptions(SocketChannel socket) {
NioSocketWrapper socketWrapper = null;
try {
// Allocate channel and wrapper
NioChannel channel = null;
if (getNioChannels() != null) {
channel = getNioChannels().pop();
}
if (channel == null) {
SocketBufferHandler bufhandler = new SocketBufferHandler(
socketProperties.getAppReadBufSize(),
socketProperties.getAppWriteBufSize(),
socketProperties.getDirectBuffer());
if (isSSLEnabled()) {
// This is the change from the code in the base class
channel = new JSSSecureNioChannel(bufhandler, this);
// End of difference
} else {
channel = new NioChannel(bufhandler);
}
}
NioSocketWrapper newWrapper = new NioSocketWrapper(channel, this);
channel.reset(socket, newWrapper);
connections.put(socket, newWrapper);
socketWrapper = newWrapper;

// Set socket properties
// Disable blocking, polling will be used
socket.configureBlocking(false);
if (getUnixDomainSocketPath() == null) {
socketProperties.setProperties(socket.socket());
}

socketWrapper.setReadTimeout(getConnectionTimeout());
socketWrapper.setWriteTimeout(getConnectionTimeout());
socketWrapper.setKeepAliveLeft(JSSNioEndpoint.this.getMaxKeepAliveRequests());
getPoller().register(socketWrapper);
return true;
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
try {
log.error(sm.getString("endpoint.socketOptionsError"), t);
} catch (Throwable tt) {
ExceptionUtils.handleThrowable(tt);
}
if (socketWrapper == null) {
destroySocket(socket);
}
}
// Tell to close the socket if needed
return false;

}
@Override
protected SSLEngine createSSLEngine(String arg0, List<Cipher> arg1, List<String> arg2) {
return super.createSSLEngine(arg0, arg1, arg2);
}

}
Loading

0 comments on commit 8791361

Please sign in to comment.