This is an automated email from the ASF dual-hosted git repository.

rmaucher pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 887a9c8ad1f6b9081e1eb22477a00f6398171292
Author: opencode <[email protected]>
AuthorDate: Thu Oct 15 15:36:41 2026 +0200

    Support AES-128-CBC and AES-192-CBC encrypted traditional PEM keys
    
    The switch on the DEK-Info algorithm of a traditional PEM encrypted
    private key only handled DES-CBC, DES-EDE3-CBC and AES-256-CBC. Any
    other algorithm, including the AES-128-CBC and AES-192-CBC that OpenSSL
    readily produces with openssl rsa -aes128 and -aes192, fell into a
    default branch that used the OpenSSL algorithm name directly as the JCE
    transformation name. That always failed with a message like Cannot find
    any provider supporting AES-128-CBC, which does not say that the
    algorithm is simply unsupported.
    
    Add the two missing algorithms, using the same key derivation and the
    same warning about the OpenSSL password based key derivation that the
    AES-256-CBC case uses, and report any other algorithm as unsupported
    with the name of the PEM file and the algorithm instead of attempting to
    use the OpenSSL name as a JCE transformation.
    
    Tests: new fixtures key-encrypted-pkcs1-aes128.pem and
    key-encrypted-pkcs1-aes192.pem, generated with OpenSSL from the existing
    key-pkcs1.pem, cover the new algorithms. A new test checks that an
    algorithm outside the supported set is reported as a
    NoSuchAlgorithmException that names the algorithm.
---
 .../tomcat/util/net/jsse/LocalStrings.properties   |  1 +
 java/org/apache/tomcat/util/net/jsse/PEMFile.java  | 21 +++++++++----
 .../apache/tomcat/util/net/jsse/TestPEMFile.java   | 35 ++++++++++++++++++++++
 .../util/net/jsse/key-encrypted-pkcs1-aes128.pem   | 18 +++++++++++
 .../util/net/jsse/key-encrypted-pkcs1-aes192.pem   | 18 +++++++++++
 5 files changed, 88 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/jsse/LocalStrings.properties 
b/java/org/apache/tomcat/util/net/jsse/LocalStrings.properties
index 2d3ff4ffeb..85fc27deca 100644
--- a/java/org/apache/tomcat/util/net/jsse/LocalStrings.properties
+++ b/java/org/apache/tomcat/util/net/jsse/LocalStrings.properties
@@ -36,6 +36,7 @@ pemFile.notValidRFC5915=The provided key file does not 
conform to RFC 5915
 pemFile.parseError=Unable to parse the key from [{0}]
 pemFile.parseError.algorithm=Unable to parse the key using algorithm [{0}]
 pemFile.startWithoutEnd=The PEM file [{0}] contains the part of type [{1}] 
which is not closed by an end boundary
+pemFile.unknownDekInfoAlgorithm=The PEM file [{0}] is using the DEK-Info 
encryption algorithm [{1}] which is not a recognised PEM encryption algorithm
 pemFile.unknownEncryptedFormat=The format [{0}] is not a recognised encrypted 
PEM file format
 pemFile.unknownEncryptionAlgorithm=The encryption algorithm with DER encoded 
OID of [{0}] was not recognised
 pemFile.unknownPkcs8Algorithm=The PKCS#8 encryption algorithm with DER encoded 
OID of [{0}] was not recognised
diff --git a/java/org/apache/tomcat/util/net/jsse/PEMFile.java 
b/java/org/apache/tomcat/util/net/jsse/PEMFile.java
index 5e413b4d31..0d6149090a 100644
--- a/java/org/apache/tomcat/util/net/jsse/PEMFile.java
+++ b/java/org/apache/tomcat/util/net/jsse/PEMFile.java
@@ -438,6 +438,20 @@ public class PEMFile {
                             
log.warn(sm.getString("pemFile.encryption.insecure", filename, algorithm));
                             break;
                         }
+                        case "AES-128-CBC": {
+                            secretKeyAlgorithm = "AES";
+                            cipherTransformation = "AES/CBC/PKCS5Padding";
+                            keyLength = 16;
+                            
log.warn(sm.getString("pemFile.encryption.insecure", filename, algorithm));
+                            break;
+                        }
+                        case "AES-192-CBC": {
+                            secretKeyAlgorithm = "AES";
+                            cipherTransformation = "AES/CBC/PKCS5Padding";
+                            keyLength = 24;
+                            
log.warn(sm.getString("pemFile.encryption.insecure", filename, algorithm));
+                            break;
+                        }
                         case "AES-256-CBC": {
                             secretKeyAlgorithm = "AES";
                             cipherTransformation = "AES/CBC/PKCS5Padding";
@@ -446,11 +460,8 @@ public class PEMFile {
                             break;
                         }
                         default:
-                            // This will almost certainly trigger errors
-                            secretKeyAlgorithm = algorithm;
-                            cipherTransformation = algorithm;
-                            keyLength = 8;
-                            break;
+                            throw new NoSuchAlgorithmException(
+                                    
sm.getString("pemFile.unknownDekInfoAlgorithm", filename, algorithm));
                     }
 
                     byte[] iv = fromHex(ivHex);
diff --git a/test/org/apache/tomcat/util/net/jsse/TestPEMFile.java 
b/test/org/apache/tomcat/util/net/jsse/TestPEMFile.java
index da286a8b36..92afbf89d0 100644
--- a/test/org/apache/tomcat/util/net/jsse/TestPEMFile.java
+++ b/test/org/apache/tomcat/util/net/jsse/TestPEMFile.java
@@ -21,6 +21,7 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.security.NoSuchAlgorithmException;
 import java.security.PrivateKey;
 import java.security.cert.X509Certificate;
 import java.util.ArrayList;
@@ -42,6 +43,8 @@ public class TestPEMFile {
     private static final String KEY_PKCS1 = "key-pkcs1.pem";
     private static final String KEY_ENCRYPTED_PKCS1_DES_CBC = 
"key-encrypted-pkcs1-des-cbc.pem";
     private static final String KEY_ENCRYPTED_PKCS1_DES_EDE3_CBC = 
"key-encrypted-pkcs1-des-ede3-cbc.pem";
+    private static final String KEY_ENCRYPTED_PKCS1_AES128 = 
"key-encrypted-pkcs1-aes128.pem";
+    private static final String KEY_ENCRYPTED_PKCS1_AES192 = 
"key-encrypted-pkcs1-aes192.pem";
     private static final String KEY_ENCRYPTED_PKCS1_AES256 = 
"key-encrypted-pkcs1-aes256.pem";
     private static final String 
KEY_ENCRYPTED_PKCS8_HMACSHA1DEFAULT_DES_EDE3_CBC = 
"key-encrypted-pkcs8-hmacsha1default-des-ede3-cbc.pem";
     private static final String KEY_ENCRYPTED_PKCS8_HMACSHA256_AES_128_CBC = 
"key-encrypted-pkcs8-hmacsha256-aes-128-cbc.pem";
@@ -100,12 +103,44 @@ public class TestPEMFile {
     }
 
 
+    @Test
+    public void testKeyEncryptedPkcs1Aes128() throws Exception {
+        testKeyEncrypted(KEY_ENCRYPTED_PKCS1_AES128);
+    }
+
+
+    @Test
+    public void testKeyEncryptedPkcs1Aes192() throws Exception {
+        testKeyEncrypted(KEY_ENCRYPTED_PKCS1_AES192);
+    }
+
+
     @Test
     public void testKeyEncryptedPkcs1Aes256() throws Exception {
         testKeyEncrypted(KEY_ENCRYPTED_PKCS1_AES256);
     }
 
 
+    @Test
+    public void testKeyEncryptedPkcs1UnsupportedAlgorithm() throws Exception {
+        // The name of the algorithm is not a JCE transformation name, but the
+        // algorithm should still be rejected as unsupported rather than by
+        // the failure of an attempt to use the name as a transformation. The
+        // algorithm is rejected before the content is decrypted, so the rest
+        // of the content does not matter.
+        String pem = "-----BEGIN RSA PRIVATE KEY-----\n" + "Proc-Type: 
4,ENCRYPTED\n"
+                + "DEK-Info: AES-128-CTR,0123456789ABCDEF0123456789ABCDEF\n" + 
"QUJDRA==\n"
+                + "-----END RSA PRIVATE KEY-----\n";
+        try {
+            new PEMFile("unsupported", new 
ByteArrayInputStream(pem.getBytes(StandardCharsets.US_ASCII)), KEY_PASSWORD,
+                    null);
+            Assert.fail("Expected the unsupported encryption algorithm to be 
rejected");
+        } catch (NoSuchAlgorithmException nsae) {
+            // Expected
+        }
+    }
+
+
     @Test
     public void testKeyEncryptedPkcs8HmacSha1DefaultDesEde3Cbc() throws 
Exception {
         testKeyEncrypted(KEY_ENCRYPTED_PKCS8_HMACSHA1DEFAULT_DES_EDE3_CBC);
diff --git 
a/test/org/apache/tomcat/util/net/jsse/key-encrypted-pkcs1-aes128.pem 
b/test/org/apache/tomcat/util/net/jsse/key-encrypted-pkcs1-aes128.pem
new file mode 100644
index 0000000000..d8c09dcbc5
--- /dev/null
+++ b/test/org/apache/tomcat/util/net/jsse/key-encrypted-pkcs1-aes128.pem
@@ -0,0 +1,18 @@
+-----BEGIN RSA PRIVATE KEY-----
+Proc-Type: 4,ENCRYPTED
+DEK-Info: AES-128-CBC,4DCB0290D2C234A782B7C1724A7AB2EE
+
+4hLNoDWz8XP4SV9c7Tk4AuNxdT4jXDNxXwRZAumk4aNe03GjZjV43HA++fNn6VQ8
+JOwIE2s4/RN4brzIyFHCQ1lq1vsGLLa/gmfabHZdv11h2rMAfyCrOwvPRh7N/x75
+Y2yTavRoqqT8+K7/IrpWX5IvVLA8dBXMiB11CcKyiCCKK6qaIap7HP2FpjdSq5Jd
+Irnuvvq5rXUkHz5HLk0g+p2iKbwoZS3aaQmSK+AnmH5WOHF6XRz12Z5Ell5vOAGG
+F8OEV5Z+pxlP6Jf4zAqsYvIXMGv3gOocy6RISB+db7/x9cvVnqQgDHOdvLSDiNH2
+r2eo1SFS1qsJAGuKmvU/05yk/XX22Bwuh1P4D7lV+swTBoYXnRrkrG0X8IjUQ04b
+rOQzq+RIdz+iJNd7sjsHEcYjHCZblvffHd1B3kR9Lmpp7qjVVpk+j/itpG87NVFh
+aOUBm9A7eWSx6D1YJlEv+sbumlf9V91L6vB/wC++vOFqUDv05gGy1HjOFqw7Mg92
+lF+F64I62wpBM9qbdE9az4qhuibklFQW7rPh6QLzf2gFL1ziOUzybn9xSYDvvqyZ
+fCJX9YNDVu3C4Su9BxGv4S1tTdxtGq1G+pL45/vZM8xsYE1vrxrQttromANKq3Xu
+tLvAeVN8HELjMt45qPRUfnsRBRBbDxgEkw2g38gAcUV3o9cd3mfqzZ0BHWRzaKdz
+NjSbNHqeHp3YAuAVM8Bu5pwhASBpR1YIYeWltZjFXJsSNxs+Lw3nGlC6FspNOkFj
+k17aKDez9G/3IqrzLcyl8LD9MN5PNanikxRoNThQaRRAxI152RxQXyAAa5aXcgFI
+-----END RSA PRIVATE KEY-----
diff --git 
a/test/org/apache/tomcat/util/net/jsse/key-encrypted-pkcs1-aes192.pem 
b/test/org/apache/tomcat/util/net/jsse/key-encrypted-pkcs1-aes192.pem
new file mode 100644
index 0000000000..c570bbe493
--- /dev/null
+++ b/test/org/apache/tomcat/util/net/jsse/key-encrypted-pkcs1-aes192.pem
@@ -0,0 +1,18 @@
+-----BEGIN RSA PRIVATE KEY-----
+Proc-Type: 4,ENCRYPTED
+DEK-Info: AES-192-CBC,F20C2EFDA5AD3F0F9F5750197F6FDE71
+
+xFk1lqug3zdJIvgjcDjHjhZVl4znx0oVQfA0bDqmiQDmzRlKmpDoD9y0qySs/Vp8
+TwgAHbbNX4VBahBJUyXOsnw+AoldGoRmHiaHuTW94TJhfEcJnripMNSJdeN19nVv
+OAlKMXpvektnkU3s6DtcP70GmmTTcXBHzxpGHfwFjpJ6C6QhU2TFz/cuGjXZRp43
+FKW5YdJnQ+XgUjj6GOwUwCf/l+xA0bI6/RPvHdOjFRxBGNxrpQr9PDOAJ3p9t9AF
+kdP7wIlNND+Siy4HAE3kzRXNeY5E3/Ts7uTJQ0/D3EZqCxhBuvh0ySqmv8Mqidn6
+VZzN0QHuYkxtaDxYXV19C5TVgNq/yYz2Tfl1uKgA1XJqi0tyXlwWOx1UafmELHlN
+SqAmZXgbP8n2cOWQ9sZ678NtQLsMGNc+w37BkZbZRi0Lu8E7ElCe9/u4GZtVFXUt
+E3tedjPgDaAUa2yVhnGKH45Rh7pVjQLU71oMhiTnqwm44sJ4l5h5/g21ta9NM+/8
+mtIbIYt+N1v0GXyL1vMV2aofH4cKI7rBPd4LVB/8uTRFCCdjuvtiQLJMSsAkEbJW
+1rtr/dIZcHO+HF8Mcb/jWK9RSJrBif1u2mHQwVNQlW4pdq0xLC6zamXV8wPkxvHZ
+LAHhdwOjPZDc7P1959FE9X8/lHk52FGGL/xpa7bbztYXlJVSVEbIgF1CdEFC/t3W
+2f4e9QM6HhBrS3ly6crYUjVNObv44E7Cg+OrggPY2dA0OQrjQY0szMNRJl9RRsp5
+nuhzXkbE4iht931uQi3oqjSetCnQkWcXH5gVzEzT1/pr/BtOK+eyzYnMwDQTmJfs
+-----END RSA PRIVATE KEY-----


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to