|
| 1 | +/* |
| 2 | + * Copyright 2015-2024 Ritense BV, the Netherlands. |
| 3 | + * |
| 4 | + * Licensed under EUPL, Version 1.2 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" basis, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.ritense.valtimo.security.x509 |
| 18 | + |
| 19 | +import org.assertj.core.api.Assertions.assertThat |
| 20 | +import org.junit.jupiter.api.Assertions.fail |
| 21 | +import org.junit.jupiter.api.DisplayName |
| 22 | +import org.junit.jupiter.api.Test |
| 23 | +import org.junit.jupiter.params.ParameterizedTest |
| 24 | +import org.junit.jupiter.params.provider.Arguments |
| 25 | +import org.junit.jupiter.params.provider.MethodSource |
| 26 | +import org.springframework.security.web.authentication.preauth.x509.SubjectDnX509PrincipalExtractor |
| 27 | +import java.util.stream.Stream |
| 28 | + |
| 29 | +class SubjectDnX509PrincipalExtractorVulnerabilityTest { |
| 30 | + |
| 31 | + private val extractor = SubjectDnX509PrincipalExtractor() |
| 32 | + |
| 33 | + @Test |
| 34 | + @DisplayName("Legitimate certificate extracts correct CN") |
| 35 | + fun `legitimate certificate extracts correct CN`() { |
| 36 | + val cert = X509TestCertificateGenerator.createCertificate("CN=legitimate-user") |
| 37 | + |
| 38 | + val principal = extractor.extractPrincipal(cert) as String |
| 39 | + |
| 40 | + assertThat(principal).isEqualTo("legitimate-user") |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + @DisplayName("Certificate with multiple RDNs extracts first CN") |
| 45 | + fun `certificate with multiple RDNs extracts first CN`() { |
| 46 | + val cert = X509TestCertificateGenerator.createCertificate( |
| 47 | + "CN=actual-user,OU=Department,O=Organization,C=NL" |
| 48 | + ) |
| 49 | + |
| 50 | + val principal = extractor.extractPrincipal(cert) as String |
| 51 | + |
| 52 | + assertThat(principal).isEqualTo("actual-user") |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @DisplayName("Multi-valued RDN extracts CN from first RDN (expected RFC 2253 behavior)") |
| 57 | + fun `multi-valued RDN extracts CN from first RDN`() { |
| 58 | + val cert = X509TestCertificateGenerator.createCertificate( |
| 59 | + "CN=admin+OU=fake,CN=real-user" |
| 60 | + ) |
| 61 | + |
| 62 | + val principal = extractor.extractPrincipal(cert) as String |
| 63 | + |
| 64 | + assertThat(principal).isEqualTo("admin") |
| 65 | + } |
| 66 | + |
| 67 | + @ParameterizedTest(name = "[{index}] DN: {0}") |
| 68 | + @MethodSource("malformedDNsWithExpectedResults") |
| 69 | + @DisplayName("Malformed DN should not allow impersonation") |
| 70 | + fun `malformed DN should not allow impersonation`( |
| 71 | + subjectDN: String, |
| 72 | + forbiddenPrincipal: String, |
| 73 | + description: String |
| 74 | + ) { |
| 75 | + val cert = try { |
| 76 | + X509TestCertificateGenerator.createCertificate(subjectDN) |
| 77 | + } catch (e: Exception) { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + val extractedPrincipal = extractor.extractPrincipal(cert) as String |
| 82 | + |
| 83 | + if (forbiddenPrincipal == extractedPrincipal) { |
| 84 | + fail<Unit>( |
| 85 | + """ |
| 86 | + VULNERABILITY DETECTED! $description |
| 87 | + Subject DN: $subjectDN |
| 88 | + Extracted principal: $extractedPrincipal |
| 89 | + This should NOT have extracted '$forbiddenPrincipal' - potential impersonation attack! |
| 90 | + """.trimIndent() |
| 91 | + ) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + companion object { |
| 96 | + @JvmStatic |
| 97 | + fun malformedDNsWithExpectedResults(): Stream<Arguments> = Stream.of( |
| 98 | + Arguments.of( |
| 99 | + "OU=CN\\=admin,CN=attacker", |
| 100 | + "admin", |
| 101 | + "CN embedded in OU value with escaped equals" |
| 102 | + ), |
| 103 | + Arguments.of( |
| 104 | + "CN=admin\\,CN=victim", |
| 105 | + "admin", |
| 106 | + "Escaped comma attempting to create fake second CN" |
| 107 | + ), |
| 108 | + Arguments.of( |
| 109 | + "CN=admin\\\\,CN=real-user", |
| 110 | + "admin", |
| 111 | + "Double backslash before comma" |
| 112 | + ), |
| 113 | + Arguments.of( |
| 114 | + "O=CN\\=admin,CN=legitimate", |
| 115 | + "admin", |
| 116 | + "CN pattern embedded in Organization" |
| 117 | + ), |
| 118 | + Arguments.of( |
| 119 | + "CN=first,CN=second,O=Org", |
| 120 | + "second", |
| 121 | + "Multiple CN attributes - should pick first, not second" |
| 122 | + ), |
| 123 | + Arguments.of( |
| 124 | + "CN=\\ admin\\ ,O=Org", |
| 125 | + "admin", |
| 126 | + "CN with leading/trailing spaces should include spaces" |
| 127 | + ), |
| 128 | + Arguments.of( |
| 129 | + "CN=,CN=real-user", |
| 130 | + "real-user", |
| 131 | + "Empty CN followed by populated CN" |
| 132 | + ), |
| 133 | + Arguments.of( |
| 134 | + "CN=#61646D696E,O=Org", |
| 135 | + "admin", |
| 136 | + "Hex-encoded 'admin' in CN - verify proper decoding" |
| 137 | + ), |
| 138 | + Arguments.of( |
| 139 | + "2.5.4.3=#61646D696E,CN=real-user", |
| 140 | + "admin", |
| 141 | + "OID 2.5.4.3 (CN) with hex value before string CN" |
| 142 | + ) |
| 143 | + ) |
| 144 | + } |
| 145 | +} |
0 commit comments