|
| 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 | +package org.apache.tomcat.jni; |
| 18 | + |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +/** |
| 25 | + * Tests for SSL_CONF context and OpenSSL configuration commands. |
| 26 | + */ |
| 27 | +public class TestSSLConf extends BaseTest { |
| 28 | + |
| 29 | + private long pool; |
| 30 | + private long sslCtx; |
| 31 | + private long confCtx; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setup() throws Exception { |
| 35 | + requireLibrary(); |
| 36 | + pool = Pool.create(0); |
| 37 | + sslCtx = SSLContext.make(pool, SSL.SSL_PROTOCOL_ALL, SSL.SSL_MODE_SERVER); |
| 38 | + } |
| 39 | + |
| 40 | + @After |
| 41 | + public void tearDown() { |
| 42 | + if (confCtx != 0) { |
| 43 | + SSLConf.free(confCtx); |
| 44 | + } |
| 45 | + if (sslCtx != 0) { |
| 46 | + SSLContext.free(sslCtx); |
| 47 | + } |
| 48 | + if (pool != 0) { |
| 49 | + Pool.destroy(pool); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testCreateServerSSLConf() throws Exception { |
| 55 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 56 | + Assert.assertNotEquals("SSL_CONF context should be created", 0, confCtx); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testCreateClientSSLConf() throws Exception { |
| 61 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_CLIENT); |
| 62 | + Assert.assertNotEquals("SSL_CONF context should be created", 0, confCtx); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testCreateSSLConfWithMultipleFlags() throws Exception { |
| 67 | + int flags = SSL.SSL_CONF_FLAG_SERVER | SSL.SSL_CONF_FLAG_FILE; |
| 68 | + confCtx = SSLConf.make(pool, flags); |
| 69 | + Assert.assertNotEquals("SSL_CONF context with multiple flags should be created", 0, confCtx); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testAssignSSLContext() throws Exception { |
| 74 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 75 | + |
| 76 | + // Assign SSL context to CONF context - should not throw |
| 77 | + SSLConf.assign(confCtx, sslCtx); |
| 78 | + Assert.assertTrue("Assignment should succeed without exception", true); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testCheckMinProtocol() throws Exception { |
| 83 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 84 | + SSLConf.assign(confCtx, sslCtx); |
| 85 | + |
| 86 | + // Check MinProtocol command |
| 87 | + int result = SSLConf.check(confCtx, "MinProtocol", "TLSv1.2"); |
| 88 | + Assert.assertTrue("MinProtocol check should return valid result", result >= 0); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testCheckCipherString() throws Exception { |
| 93 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 94 | + SSLConf.assign(confCtx, sslCtx); |
| 95 | + |
| 96 | + // Check CipherString command |
| 97 | + int result = SSLConf.check(confCtx, "CipherString", "HIGH:!aNULL"); |
| 98 | + Assert.assertTrue("CipherString check should return valid result", result >= 0); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testApplyMinProtocol() throws Exception { |
| 103 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 104 | + SSLConf.assign(confCtx, sslCtx); |
| 105 | + |
| 106 | + // Apply MinProtocol command |
| 107 | + int result = SSLConf.apply(confCtx, "MinProtocol", "TLSv1.2"); |
| 108 | + Assert.assertTrue("MinProtocol apply should return success", result > 0); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testApplyMaxProtocol() throws Exception { |
| 113 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 114 | + SSLConf.assign(confCtx, sslCtx); |
| 115 | + |
| 116 | + // Apply MaxProtocol command |
| 117 | + int result = SSLConf.apply(confCtx, "MaxProtocol", "TLSv1.3"); |
| 118 | + Assert.assertTrue("MaxProtocol apply should return success", result > 0); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testApplyCipherString() throws Exception { |
| 123 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 124 | + SSLConf.assign(confCtx, sslCtx); |
| 125 | + |
| 126 | + // Apply CipherString command |
| 127 | + int result = SSLConf.apply(confCtx, "CipherString", "HIGH:!aNULL:!MD5"); |
| 128 | + Assert.assertTrue("CipherString apply should return success", result > 0); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testApplyOptions() throws Exception { |
| 133 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 134 | + SSLConf.assign(confCtx, sslCtx); |
| 135 | + |
| 136 | + // Apply Options command |
| 137 | + int result = SSLConf.apply(confCtx, "Options", "ServerPreference"); |
| 138 | + Assert.assertTrue("Options apply should return success", result > 0); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testFinish() throws Exception { |
| 143 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 144 | + SSLConf.assign(confCtx, sslCtx); |
| 145 | + |
| 146 | + // Apply some commands |
| 147 | + SSLConf.apply(confCtx, "MinProtocol", "TLSv1.2"); |
| 148 | + SSLConf.apply(confCtx, "CipherString", "HIGH"); |
| 149 | + |
| 150 | + // Finish should be called after all commands |
| 151 | + int result = SSLConf.finish(confCtx); |
| 152 | + Assert.assertTrue("Finish should return success", result > 0); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testMultipleCommandSequence() throws Exception { |
| 157 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 158 | + SSLConf.assign(confCtx, sslCtx); |
| 159 | + |
| 160 | + // Apply multiple commands in sequence |
| 161 | + int result1 = SSLConf.apply(confCtx, "MinProtocol", "TLSv1.2"); |
| 162 | + Assert.assertTrue("First command should succeed", result1 > 0); |
| 163 | + |
| 164 | + int result2 = SSLConf.apply(confCtx, "MaxProtocol", "TLSv1.3"); |
| 165 | + Assert.assertTrue("Second command should succeed", result2 > 0); |
| 166 | + |
| 167 | + int result3 = SSLConf.apply(confCtx, "CipherString", "HIGH:!aNULL"); |
| 168 | + Assert.assertTrue("Third command should succeed", result3 > 0); |
| 169 | + |
| 170 | + int finishResult = SSLConf.finish(confCtx); |
| 171 | + Assert.assertTrue("Finish should succeed", finishResult > 0); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testCheckBeforeApply() throws Exception { |
| 176 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 177 | + SSLConf.assign(confCtx, sslCtx); |
| 178 | + |
| 179 | + // Check a command first |
| 180 | + int checkResult = SSLConf.check(confCtx, "MinProtocol", "TLSv1.2"); |
| 181 | + Assert.assertTrue("Check should succeed", checkResult >= 0); |
| 182 | + |
| 183 | + // Then apply the same command |
| 184 | + int applyResult = SSLConf.apply(confCtx, "MinProtocol", "TLSv1.2"); |
| 185 | + Assert.assertTrue("Apply should succeed after check", applyResult > 0); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void testInvalidCommandHandling() throws Exception { |
| 190 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 191 | + SSLConf.assign(confCtx, sslCtx); |
| 192 | + |
| 193 | + // Try applying an invalid command - should handle gracefully |
| 194 | + try { |
| 195 | + int result = SSLConf.apply(confCtx, "InvalidCommand", "SomeValue"); |
| 196 | + // Result might be <= 0 for unrecognized commands |
| 197 | + // This is acceptable behavior |
| 198 | + Assert.assertTrue("Invalid command handled", true); |
| 199 | + } catch (Exception e) { |
| 200 | + // Exception is also acceptable for invalid commands |
| 201 | + Assert.assertTrue("Exception on invalid command is acceptable", true); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void testMultipleSSLConfContexts() throws Exception { |
| 207 | + long confCtx1 = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 208 | + long confCtx2 = SSLConf.make(pool, SSL.SSL_CONF_FLAG_CLIENT); |
| 209 | + |
| 210 | + Assert.assertNotEquals("First CONF context should be created", 0, confCtx1); |
| 211 | + Assert.assertNotEquals("Second CONF context should be created", 0, confCtx2); |
| 212 | + Assert.assertNotEquals("CONF contexts should differ", confCtx1, confCtx2); |
| 213 | + |
| 214 | + // Assign same SSL context to both |
| 215 | + SSLConf.assign(confCtx1, sslCtx); |
| 216 | + SSLConf.assign(confCtx2, sslCtx); |
| 217 | + |
| 218 | + // Apply different commands to each |
| 219 | + SSLConf.apply(confCtx1, "MinProtocol", "TLSv1.2"); |
| 220 | + SSLConf.apply(confCtx2, "MinProtocol", "TLSv1.3"); |
| 221 | + |
| 222 | + SSLConf.free(confCtx2); |
| 223 | + SSLConf.free(confCtx1); |
| 224 | + |
| 225 | + // Set confCtx to 0 so tearDown doesn't try to free it again |
| 226 | + confCtx = 0; |
| 227 | + } |
| 228 | + |
| 229 | + @Test |
| 230 | + public void testProtocolConfiguration() throws Exception { |
| 231 | + confCtx = SSLConf.make(pool, SSL.SSL_CONF_FLAG_SERVER); |
| 232 | + SSLConf.assign(confCtx, sslCtx); |
| 233 | + |
| 234 | + // Test different protocol combinations |
| 235 | + String[] protocols = {"TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"}; |
| 236 | + |
| 237 | + for (String protocol : protocols) { |
| 238 | + // Just verify these don't throw - actual protocol support |
| 239 | + // depends on OpenSSL version |
| 240 | + try { |
| 241 | + SSLConf.apply(confCtx, "MinProtocol", protocol); |
| 242 | + } catch (Exception e) { |
| 243 | + // Some protocols might not be supported |
| 244 | + // That's acceptable |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + SSLConf.finish(confCtx); |
| 249 | + } |
| 250 | +} |
0 commit comments