|
| 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.catalina.tribes.group.interceptors; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.junit.runners.Parameterized; |
| 27 | +import org.junit.runners.Parameterized.Parameter; |
| 28 | +import org.junit.runners.Parameterized.Parameters; |
| 29 | + |
| 30 | +import org.apache.catalina.tribes.Channel; |
| 31 | + |
| 32 | +@RunWith(Parameterized.class) |
| 33 | +public class TestEncryptInterceptorAlgorithms extends EncryptionInterceptorBaseTest { |
| 34 | + |
| 35 | + @Parameters(name = "{index} {0}/{1}/{2}") |
| 36 | + public static Collection<Object[]> inputs() { |
| 37 | + |
| 38 | + List<Object[]> result = new ArrayList<>(); |
| 39 | + // Covers all cipher algorithm modes currently listed in Java Standard Names |
| 40 | + |
| 41 | + // Not supported - Insecure |
| 42 | + result.add(new Object[] { "AES", "NONE", "NoPadding", Boolean.FALSE}); |
| 43 | + // Not supported - Insecure - Padding makes no sense if there is no encryption |
| 44 | + result.add(new Object[] { "AES", "NONE", "PKCS5Padding", Boolean.FALSE}); |
| 45 | + |
| 46 | + // Not supported - NoPadding requires fixed block size and cluster messages are variable length |
| 47 | + result.add(new Object[] { "AES", "CBC", "NoPadding", Boolean.FALSE}); |
| 48 | + // Supported but not recommended - possible security issues in some configurations - backwards compatibility |
| 49 | + result.add(new Object[] { "AES", "CBC", "PKCS5Padding", Boolean.TRUE}); |
| 50 | + |
| 51 | + // Not supported - JCA provider doesn't included it |
| 52 | + result.add(new Object[] { "AES", "CCM", "NoPadding", Boolean.FALSE}); |
| 53 | + // Not supported - JCA provider doesn't included it - CCM doesn't need (support?) padding |
| 54 | + result.add(new Object[] { "AES", "CCM", "PKCS5Padding", Boolean.FALSE}); |
| 55 | + |
| 56 | + // Not supported - NoPadding requires fixed block size and cluster messages are variable length |
| 57 | + result.add(new Object[] { "AES", "CFB", "NoPadding", Boolean.FALSE}); |
| 58 | + // Supported but not recommended - possible security issues in some configurations - backwards compatibility |
| 59 | + result.add(new Object[] { "AES", "CFB", "PKCS5Padding", Boolean.TRUE}); |
| 60 | + |
| 61 | + // Not supported - Insecure and/or slow |
| 62 | + result.add(new Object[] { "AES", "CFB8", "NoPadding", Boolean.FALSE}); |
| 63 | + result.add(new Object[] { "AES", "CFB8", "PKCS5Padding", Boolean.FALSE}); |
| 64 | + result.add(new Object[] { "AES", "CFB16", "NoPadding", Boolean.FALSE}); |
| 65 | + result.add(new Object[] { "AES", "CFB16", "PKCS5Padding", Boolean.FALSE}); |
| 66 | + // large block sizes not tested but will be rejected as well |
| 67 | + |
| 68 | + // Not supported - Insecure |
| 69 | + result.add(new Object[] { "AES", "CTR", "NoPadding", Boolean.FALSE}); |
| 70 | + // Not supported - Configuration not recommended |
| 71 | + result.add(new Object[] { "AES", "CTR", "PKCS5Padding", Boolean.FALSE}); |
| 72 | + |
| 73 | + // Not supported - has minimum length |
| 74 | + result.add(new Object[] { "AES", "CTS", "NoPadding", Boolean.FALSE}); |
| 75 | + result.add(new Object[] { "AES", "CTS", "PKCS5Padding", Boolean.FALSE}); |
| 76 | + |
| 77 | + // Not supported - Insecure |
| 78 | + result.add(new Object[] { "AES", "ECB", "NoPadding", Boolean.FALSE}); |
| 79 | + result.add(new Object[] { "AES", "ECB", "PKCS5Padding", Boolean.FALSE}); |
| 80 | + |
| 81 | + // Default for Tomcat 12 onwards |
| 82 | + result.add(new Object[] { "AES", "GCM", "NoPadding", Boolean.TRUE}); |
| 83 | + // Not supported - GCM doesn't need (support?) padding |
| 84 | + result.add(new Object[] { "AES", "GCM", "PKCS5Padding", Boolean.FALSE}); |
| 85 | + |
| 86 | + // Not supported - KW not appropriate for encrypting cluster messages |
| 87 | + result.add(new Object[] { "AES", "KW", "NoPadding", Boolean.FALSE}); |
| 88 | + result.add(new Object[] { "AES", "KW", "PKCS5Padding", Boolean.FALSE}); |
| 89 | + |
| 90 | + // Not supported - KWP not appropriate for encrypting cluster messages |
| 91 | + result.add(new Object[] { "AES", "KWP", "NoPadding", Boolean.FALSE}); |
| 92 | + result.add(new Object[] { "AES", "KWP", "PKCS5Padding", Boolean.FALSE}); |
| 93 | + |
| 94 | + // Not supported - NoPadding requires fixed block size and cluster messages are variable length |
| 95 | + result.add(new Object[] { "AES", "OFB", "NoPadding", Boolean.FALSE}); |
| 96 | + |
| 97 | + // Supported but not recommended - possible security issues in some configurations - backwards compatibility |
| 98 | + result.add(new Object[] { "AES", "OFB", "PKCS5Padding", Boolean.TRUE}); |
| 99 | + |
| 100 | + // Not supported - Insecure and/or slow |
| 101 | + result.add(new Object[] { "AES", "OFB8", "NoPadding", Boolean.FALSE}); |
| 102 | + result.add(new Object[] { "AES", "OFB8", "PKCS5Padding", Boolean.FALSE}); |
| 103 | + result.add(new Object[] { "AES", "OFB16", "NoPadding", Boolean.FALSE}); |
| 104 | + result.add(new Object[] { "AES", "OFB16", "PKCS5Padding", Boolean.FALSE}); |
| 105 | + // large block sizes not tested but will be rejected as well |
| 106 | + |
| 107 | + // Not supported - Insecure |
| 108 | + result.add(new Object[] { "AES", "PCBC", "NoPadding", Boolean.FALSE}); |
| 109 | + result.add(new Object[] { "AES", "PCBC", "PKCS5Padding", Boolean.FALSE}); |
| 110 | + |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + @Parameter(0) |
| 115 | + public String algorithm; |
| 116 | + |
| 117 | + @Parameter(1) |
| 118 | + public String mode; |
| 119 | + |
| 120 | + @Parameter(2) |
| 121 | + public String padding; |
| 122 | + |
| 123 | + @Parameter(3) |
| 124 | + public boolean shouldSucceed; |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testAlgorithm() throws Exception { |
| 128 | + if (shouldSucceed) { |
| 129 | + doTestShouldSucceed(); |
| 130 | + } else { |
| 131 | + doTestShouldNotSucceed(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private void doTestShouldSucceed() throws Exception { |
| 136 | + String transformation = String.format("%s/%s/%s", algorithm, mode, padding); |
| 137 | + |
| 138 | + src.setEncryptionAlgorithm(transformation); |
| 139 | + src.start(Channel.SND_TX_SEQ); |
| 140 | + dest.setEncryptionAlgorithm(transformation); |
| 141 | + dest.start(Channel.SND_TX_SEQ); |
| 142 | + |
| 143 | + String testInput = "The quick brown fox jumps over the lazy dog."; |
| 144 | + |
| 145 | + Assert.assertEquals("Failed in " + transformation + " mode", |
| 146 | + testInput, |
| 147 | + roundTrip(testInput, src, dest)); |
| 148 | + } |
| 149 | + |
| 150 | + private void doTestShouldNotSucceed() throws Exception { |
| 151 | + try { |
| 152 | + String transformation = String.format("%s/%s/%s", algorithm, mode, padding); |
| 153 | + src.setEncryptionAlgorithm(transformation); |
| 154 | + src.start(Channel.SND_TX_SEQ); |
| 155 | + |
| 156 | + // start() should trigger IllegalArgumentException |
| 157 | + Assert.fail(transformation + " mode is not being refused"); |
| 158 | + } catch (IllegalArgumentException iae) { |
| 159 | + // Expected |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments