/* * 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. */ package org.apache.tomcat.util.net; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.security.KeyStore; import java.security.NoSuchAlgorithmException; import java.security.Principal; import java.security.PrivateKey; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Locale; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509ExtendedKeyManager; import javax.net.ssl.X509KeyManager; import javax.net.ssl.X509TrustManager; import javax.security.auth.x500.X500Principal; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.junit.Assert; import org.apache.catalina.Context; import org.apache.catalina.authenticator.SSLAuthenticator; import org.apache.catalina.connector.Connector; import org.apache.catalina.core.AprLifecycleListener; import org.apache.catalina.core.StandardServer; import org.apache.catalina.startup.TesterMapRealm; import org.apache.catalina.startup.Tomcat; import org.apache.tomcat.jni.Library; import org.apache.tomcat.jni.LibraryNotFoundError; import org.apache.tomcat.jni.SSL; import org.apache.tomcat.util.compat.JreCompat; import org.apache.tomcat.util.descriptor.web.LoginConfig; import org.apache.tomcat.util.descriptor.web.SecurityCollection; import org.apache.tomcat.util.descriptor.web.SecurityConstraint; import org.apache.tomcat.util.net.SSLHostConfigCertificate.Type; public final class TesterSupport { public static final String SSL_DIR = "test/org/apache/tomcat/util/net/"; public static final String CA_ALIAS = "ca"; public static final String CA_JKS = SSL_DIR + CA_ALIAS + ".jks"; public static final String CLIENT_ALIAS = "user1"; public static final String CLIENT_JKS = SSL_DIR + CLIENT_ALIAS + ".jks"; public static final String LOCALHOST_EC_JKS = SSL_DIR + "localhost-ec.jks"; public static final String LOCALHOST_RSA_JKS = SSL_DIR + "localhost-rsa.jks"; public static final String LOCALHOST_KEYPASS_JKS = SSL_DIR + "localhost-rsa-copy1.jks"; public static final String JKS_PASS = "changeit"; public static final String JKS_KEY_PASS = "tomcatpass"; public static final String CA_CERT_PEM = SSL_DIR + CA_ALIAS + "-cert.pem"; public static final String LOCALHOST_EC_CERT_PEM = SSL_DIR + "localhost-ec-cert.pem"; public static final String LOCALHOST_EC_KEY_PEM = SSL_DIR + "localhost-ec-key.pem"; public static final String LOCALHOST_RSA_CERT_PEM = SSL_DIR + "localhost-rsa-cert.pem"; public static final String LOCALHOST_RSA_KEY_PEM = SSL_DIR + "localhost-rsa-key.pem"; public static final boolean OPENSSL_AVAILABLE; public static final int OPENSSL_VERSION; public static final boolean TLSV13_AVAILABLE; public static final String ROLE = "testrole"; private static String clientAuthExpectedIssuer; private static String lastUsage = "NONE"; private static Principal[] lastRequestedIssuers = new Principal[0]; static { boolean available = false; int version = 0; try { Library.initialize(null); available = true; version = SSL.version(); Library.terminate(); } catch (Exception | LibraryNotFoundError ex) { // Ignore } OPENSSL_AVAILABLE = available; OPENSSL_VERSION = version; available = false; try { SSLContext.getInstance(Constants.SSL_PROTO_TLSv1_3); available = true; } catch (NoSuchAlgorithmException ex) { } TLSV13_AVAILABLE = available; } public static boolean isOpensslAvailable() { return OPENSSL_AVAILABLE; } public static int getOpensslVersion() { return OPENSSL_VERSION; } public static boolean isTlsv13Available() { return TLSV13_AVAILABLE; } public static void initSsl(Tomcat tomcat) { initSsl(tomcat, LOCALHOST_RSA_JKS, null, null); } protected static void initSsl(Tomcat tomcat, String keystore, String keystorePass, String keyPass) { Connector connector = tomcat.getConnector(); connector.setSecure(true); Assert.assertTrue(connector.setProperty("SSLEnabled", "true")); SSLHostConfig sslHostConfig = new SSLHostConfig(); SSLHostConfigCertificate certificate = new SSLHostConfigCertificate(sslHostConfig, Type.UNDEFINED); sslHostConfig.addCertificate(certificate); connector.addSslHostConfig(sslHostConfig); String protocol = tomcat.getConnector().getProtocolHandlerClassName(); if (!protocol.contains("Apr")) { String sslImplementation = System.getProperty("tomcat.test.sslImplementation"); if (sslImplementation != null && !"${test.sslImplementation}".equals(sslImplementation)) { StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); listener.setSSLRandomSeed("/dev/urandom"); server.addLifecycleListener(listener); connector.setAttribute("sslImplementationName", sslImplementation); } sslHostConfig.setSslProtocol("tls"); certificate.setCertificateKeystoreFile(new File(keystore).getAbsolutePath()); sslHostConfig.setTruststoreFile(new File(CA_JKS).getAbsolutePath()); if (keystorePass != null) { certificate.setCertificateKeystorePassword(keystorePass); } if (keyPass != null) { certificate.setCertificateKeyPassword(keyPass); } } else { certificate.setCertificateFile(new File(LOCALHOST_RSA_CERT_PEM).getAbsolutePath()); certificate.setCertificateKeyFile(new File(LOCALHOST_RSA_KEY_PEM).getAbsolutePath()); sslHostConfig.setCaCertificateFile(new File(CA_CERT_PEM).getAbsolutePath()); } } protected static KeyManager[] getUser1KeyManagers() throws Exception { KeyManagerFactory kmf = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm()); kmf.init(getKeyStore(CLIENT_JKS), JKS_PASS.toCharArray()); KeyManager[] managers = kmf.getKeyManagers(); KeyManager manager; for (int i=0; i < managers.length; i++) { manager = managers[i]; if (manager instanceof X509ExtendedKeyManager) { managers[i] = new TrackingExtendedKeyManager((X509ExtendedKeyManager)manager); } else if (manager instanceof X509KeyManager) { managers[i] = new TrackingKeyManager((X509KeyManager)manager); } } return managers; } protected static TrustManager[] getTrustManagers() throws Exception { TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm()); tmf.init(getKeyStore(CA_JKS)); return tmf.getTrustManagers(); } protected static ClientSSLSocketFactory configureClientSsl() { ClientSSLSocketFactory clientSSLSocketFactory = null; try { SSLContext sc = SSLContext.getInstance(Constants.SSL_PROTO_TLS); sc.init(TesterSupport.getUser1KeyManagers(), TesterSupport.getTrustManagers(), null); clientSSLSocketFactory = new ClientSSLSocketFactory(sc.getSocketFactory()); javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(clientSSLSocketFactory); } catch (Exception e) { e.printStackTrace(); } return clientSSLSocketFactory; } private static KeyStore getKeyStore(String keystore) throws Exception { File keystoreFile = new File(keystore); KeyStore ks = KeyStore.getInstance("JKS"); try (InputStream is = new FileInputStream(keystoreFile)) { ks.load(is, JKS_PASS.toCharArray()); } return ks; } protected static boolean isMacOs() { return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).startsWith("mac os x"); } protected static boolean isRenegotiationSupported(Tomcat tomcat) { String protocol = tomcat.getConnector().getProtocolHandlerClassName(); if (protocol.contains("Apr")) { // Disabled by default in 1.1.20 windows binary (2010-07-27) return false; } return true; } protected static boolean isClientRenegotiationSupported(Tomcat tomcat) { String protocol = tomcat.getConnector().getProtocolHandlerClassName(); if (protocol.contains("Apr")) { // Disabled by default in 1.1.20 windows binary (2010-07-27) return false; } if (protocol.contains("NioProtocol") || (protocol.contains("Nio2Protocol") && isMacOs())) { // Doesn't work on all platforms - see BZ 56448. return false; } String sslImplementation = System.getProperty("tomcat.test.sslImplementation"); if (sslImplementation != null && !"${test.sslImplementation}".equals(sslImplementation)) { // Assume custom SSL is not supporting this return false; } return true; } protected static void configureClientCertContext(Tomcat tomcat) { TesterSupport.initSsl(tomcat); /* When running on Java 11, TLSv1.3 is enabled by default. The JSSE * implementation of TLSv1.3 does not support * certificateVerification="optional", a setting on which these tests * depend. * Java 7 does not enable TLSv1.1 or TLS1.2 by default * * Ensure these tests pass with all JREs from Java 7 onwards. */ if (JreCompat.isJre8Available()) { tomcat.getConnector().findSslHostConfigs()[0].setProtocols(Constants.SSL_PROTO_TLSv1_2); } else { tomcat.getConnector().findSslHostConfigs()[0].setProtocols(Constants.SSL_PROTO_TLSv1); } // Need a web application with a protected and unprotected URL // No file system docBase required Context ctx = tomcat.addContext("", null); Tomcat.addServlet(ctx, "simple", new SimpleServlet()); ctx.addServletMappingDecoded("/unprotected", "simple"); ctx.addServletMappingDecoded("/protected", "simple"); // Security constraints SecurityCollection collection = new SecurityCollection(); collection.addPatternDecoded("/protected"); SecurityConstraint sc = new SecurityConstraint(); sc.addAuthRole(ROLE); sc.addCollection(collection); ctx.addConstraint(sc); // Configure the Realm TesterMapRealm realm = new TesterMapRealm(); // Get the CA subject the server should send us for client cert selection try { KeyStore ks = getKeyStore(CA_JKS); X509Certificate cert = (X509Certificate)ks.getCertificate(CA_ALIAS); clientAuthExpectedIssuer = cert.getSubjectDN().getName(); } catch (Exception ex) { throw new RuntimeException(ex); } String cn = "NOTFOUND"; try { KeyStore ks = getKeyStore(CLIENT_JKS); X509Certificate cert = (X509Certificate)ks.getCertificate(CLIENT_ALIAS); cn = cert.getSubjectDN().getName(); } catch (Exception ex) { throw new RuntimeException(ex); } realm.addUser(cn, "not used"); realm.addUserRole(cn, ROLE); ctx.setRealm(realm); // Configure the authenticator LoginConfig lc = new LoginConfig(); lc.setAuthMethod("CLIENT-CERT"); ctx.setLoginConfig(lc); ctx.getPipeline().addValve(new SSLAuthenticator()); // Clear the tracking data lastUsage = "NONE"; lastRequestedIssuers = new Principal[0]; } protected static String getClientAuthExpectedIssuer() { return clientAuthExpectedIssuer; } protected static void trackTrackingKeyManagers(@SuppressWarnings("unused") KeyManager wrapper, @SuppressWarnings("unused") KeyManager wrapped, String usage, Principal[] issuers) { lastUsage = usage; lastRequestedIssuers = issuers; } protected static String getLastClientAuthKeyManagerUsage() { return lastUsage; } protected static int getLastClientAuthRequestedIssuerCount() { return lastRequestedIssuers == null ? 0 : lastRequestedIssuers.length; } protected static Principal getLastClientAuthRequestedIssuer(int index) { return lastRequestedIssuers[index]; } protected static boolean checkLastClientAuthRequestedIssuers() { if (lastRequestedIssuers == null || lastRequestedIssuers.length != 1) return false; return (new X500Principal(clientAuthExpectedIssuer)).equals( new X500Principal(lastRequestedIssuers[0].getName())); } public static final byte DATA = (byte)33; public static class SimpleServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.getWriter().print("OK"); if (req.isUserInRole(ROLE)) { resp.getWriter().print("-" + ROLE); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Swallow any request body int read = 0; int len = 0; byte[] buffer = new byte[4096]; InputStream is = req.getInputStream(); boolean contentOK = true; while (len > -1) { len = is.read(buffer); read = read + len; for (int i=0; i= 0x1010100f) { return Constants.SSL_PROTO_TLSv1_3; } else { return Constants.SSL_PROTO_TLSv1_2; } } else { // NIO or NIO2. Tests do not use JSSE+OpenSSL so JSSE will be used. // Due to check above, it is known that TLS 1.3 is available return Constants.SSL_PROTO_TLSv1_3; } } public static boolean isDefaultTLSProtocolForTesting13(Connector connector) { return Constants.SSL_PROTO_TLSv1_3.equals( TesterSupport.getDefaultTLSProtocolForTesting(connector)); } }