|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.tomcat.util.net; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.security.cert.X509Certificate; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import javax.net.ssl.X509KeyManager; |
| 25 | + |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import org.apache.tomcat.util.net.SSLHostConfigCertificate.Type; |
| 30 | +import org.apache.tomcat.util.net.TesterKeystoreGenerator.CertificateExtensionsCustomizer; |
| 31 | +import org.apache.tomcat.util.net.TesterKeystoreGenerator.KeystoreEntry; |
| 32 | +import org.apache.tomcat.util.net.jsse.JSSEUtil; |
| 33 | +import org.bouncycastle.asn1.x509.ExtendedKeyUsage; |
| 34 | +import org.bouncycastle.asn1.x509.Extension; |
| 35 | +import org.bouncycastle.asn1.x509.KeyPurposeId; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests selection of a TLS server authentication certificate when a key store holds several certificates that share a |
| 39 | + * Subject. |
| 40 | + */ |
| 41 | +public class TestSSLUtilBaseKeyUsageSelection { |
| 42 | + |
| 43 | + private static final String HOST = "localhost"; |
| 44 | + |
| 45 | + @SuppressWarnings("unused") |
| 46 | + private static final CertificateExtensionsCustomizer SERVER_AUTH = (keyPair, builder) -> builder |
| 47 | + .addExtension(Extension.extendedKeyUsage, true, new ExtendedKeyUsage(KeyPurposeId.id_kp_serverAuth)); |
| 48 | + @SuppressWarnings("unused") |
| 49 | + private static final CertificateExtensionsCustomizer CLIENT_AUTH = (keyPair, builder) -> builder |
| 50 | + .addExtension(Extension.extendedKeyUsage, true, new ExtendedKeyUsage(KeyPurposeId.id_kp_clientAuth)); |
| 51 | + @SuppressWarnings("unused") |
| 52 | + private static final CertificateExtensionsCustomizer ANY_AUTH = (keyPair, builder) -> builder |
| 53 | + .addExtension(Extension.extendedKeyUsage, true, new ExtendedKeyUsage(KeyPurposeId.anyExtendedKeyUsage)); |
| 54 | + |
| 55 | + |
| 56 | + private static KeystoreEntry key(String alias, CertificateExtensionsCustomizer eku) { |
| 57 | + return KeystoreEntry.key(alias, HOST, eku); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + /* |
| 62 | + * Builds a key store from the given entries, configures a JSSE certificate pointed at alias (null for none), and |
| 63 | + * returns the leaf certificate the key manager actually presents. |
| 64 | + */ |
| 65 | + private static X509Certificate present(String alias, KeystoreEntry... entries) throws Exception { |
| 66 | + File keystore = TesterKeystoreGenerator.generateKeystore(List.of(entries)); |
| 67 | + |
| 68 | + SSLHostConfig hostConfig = new SSLHostConfig(); |
| 69 | + SSLHostConfigCertificate certificate = new SSLHostConfigCertificate(hostConfig, Type.UNDEFINED); |
| 70 | + certificate.setCertificateKeystoreFile(keystore.getAbsolutePath()); |
| 71 | + certificate.setCertificateKeystorePassword(TesterSupport.JKS_PASS); |
| 72 | + if (alias != null) { |
| 73 | + certificate.setCertificateKeyAlias(alias); |
| 74 | + } |
| 75 | + |
| 76 | + X509KeyManager keyManager = (X509KeyManager) new JSSEUtil(certificate).getKeyManagers()[0]; |
| 77 | + X509Certificate[] chain = keyManager.getCertificateChain(keyManager.chooseServerAlias("RSA", null, null)); |
| 78 | + Assert.assertNotNull("Key manager returned no certificate chain", chain); |
| 79 | + return chain[0]; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + private static void assertEku(X509Certificate certificate, String oid) throws Exception { |
| 84 | + List<String> extendedKeyUsage = certificate.getExtendedKeyUsage(); |
| 85 | + Assert.assertNotNull("Presented certificate has no Extended Key Usage", extendedKeyUsage); |
| 86 | + Assert.assertTrue("Presented certificate must be valid for Extended Key Usage " + oid, |
| 87 | + extendedKeyUsage.contains(oid)); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testSelectsServerAuthCertificateOverConfiguredClientAuthAlias() throws Exception { |
| 93 | + X509Certificate presented = present(HOST, |
| 94 | + key(HOST, CLIENT_AUTH), |
| 95 | + key("localhost-server", SERVER_AUTH)); |
| 96 | + assertEku(presented, Constants.SERVER_AUTH_EXTENDED_KEY_USAGE_OID); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testKeepsConfiguredAliasWhenItIsAlreadyServerAuth() throws Exception { |
| 102 | + X509Certificate presented = present(HOST, |
| 103 | + key(HOST, SERVER_AUTH), |
| 104 | + key("localhost-client", CLIENT_AUTH)); |
| 105 | + assertEku(presented, Constants.SERVER_AUTH_EXTENDED_KEY_USAGE_OID); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testSelectsCertificateWithAnyExtendedKeyUsage() throws Exception { |
| 111 | + X509Certificate presented = present(HOST, |
| 112 | + key(HOST, CLIENT_AUTH), |
| 113 | + key("localhost-any", ANY_AUTH)); |
| 114 | + assertEku(presented, Constants.ANY_EXTENDED_KEY_USAGE_OID); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testSelectsCertificateWithoutExtendedKeyUsage() throws Exception { |
| 120 | + X509Certificate presented = present(HOST, |
| 121 | + key(HOST, CLIENT_AUTH), |
| 122 | + key("localhost-none", null)); |
| 123 | + Assert.assertNull("A certificate with no Extended Key Usage is unrestricted and must be selectable", |
| 124 | + presented.getExtendedKeyUsage()); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testSelectsServerAuthCertificateWhenNoAliasConfigured() throws Exception { |
| 130 | + X509Certificate presented = present(null, |
| 131 | + key("first", CLIENT_AUTH), |
| 132 | + key("second", SERVER_AUTH)); |
| 133 | + assertEku(presented, Constants.SERVER_AUTH_EXTENDED_KEY_USAGE_OID); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testSelectsServerAuthCertificateWhenMultipleMatch() throws Exception { |
| 139 | + // Two server-auth certificates share the Subject. Which one wins depends on key-store enumeration order, |
| 140 | + // which JKS does not guarantee, so this only asserts that the ambiguity resolves to a server-auth |
| 141 | + // certificate rather than the client-auth one - not which of the two is chosen. |
| 142 | + X509Certificate presented = present(HOST, |
| 143 | + key(HOST, CLIENT_AUTH), |
| 144 | + key("localhost-s1", SERVER_AUTH), |
| 145 | + key("localhost-s2", SERVER_AUTH)); |
| 146 | + assertEku(presented, Constants.SERVER_AUTH_EXTENDED_KEY_USAGE_OID); |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testFallsBackToConfiguredAliasWhenNoServerAuthCertificate() throws Exception { |
| 152 | + X509Certificate presented = present(HOST, |
| 153 | + key(HOST, CLIENT_AUTH), |
| 154 | + key("localhost-2", CLIENT_AUTH)); |
| 155 | + assertEku(presented, Constants.CLIENT_AUTH_EXTENDED_KEY_USAGE_OID); |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + @Test |
| 160 | + public void testIgnoresServerAuthCertificateWithDifferentSubject() throws Exception { |
| 161 | + X509Certificate presented = present(HOST, |
| 162 | + key(HOST, CLIENT_AUTH), |
| 163 | + KeystoreEntry.key("other-server", "other.example", SERVER_AUTH)); |
| 164 | + assertEku(presented, Constants.CLIENT_AUTH_EXTENDED_KEY_USAGE_OID); |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testIgnoresTrustedCertificateEntries() throws Exception { |
| 170 | + // The only server-auth-capable certificate is a trusted (keyless) entry, so it cannot be used as a server key. |
| 171 | + // Selection must skip it and fall back to the configured key entry. If trusted entries were NOT skipped, the |
| 172 | + // keyless "ca" would be chosen and the resulting key manager would have no chain for it. |
| 173 | + X509Certificate presented = present(HOST, |
| 174 | + KeystoreEntry.trusted("ca", HOST, SERVER_AUTH), |
| 175 | + key(HOST, CLIENT_AUTH)); |
| 176 | + assertEku(presented, Constants.CLIENT_AUTH_EXTENDED_KEY_USAGE_OID); |
| 177 | + } |
| 178 | +} |
0 commit comments