-
Notifications
You must be signed in to change notification settings - Fork 972
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTPCLIENT-2337: Add sanitizeX500Principal method to escape control c…
…haracters in X500Principal. Escapes ISO control characters in X500Principal using hexadecimal representation.
- Loading branch information
1 parent
aad0e9a
commit 9acd96c
Showing
2 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
httpclient5/src/test/java/org/apache/hc/client5/http/ssl/AbstractClientTlsStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* ==================================================================== | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* ==================================================================== | ||
* | ||
* This software consists of voluntary contributions made by many | ||
* individuals on behalf of the Apache Software Foundation. For more | ||
* information on the Apache Software Foundation, please see | ||
* <http://www.apache.org/>. | ||
* | ||
*/ | ||
|
||
package org.apache.hc.client5.http.ssl; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.security.cert.X509Certificate; | ||
|
||
import javax.net.ssl.SSLEngine; | ||
import javax.net.ssl.SSLParameters; | ||
import javax.net.ssl.SSLSession; | ||
import javax.security.auth.x500.X500Principal; | ||
|
||
import org.apache.hc.core5.reactor.ssl.SSLBufferMode; | ||
import org.apache.hc.core5.reactor.ssl.TlsDetails; | ||
import org.apache.hc.core5.ssl.SSLContexts; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class AbstractClientTlsStrategyTest { | ||
|
||
@Test | ||
public void testToEscapedString_withControlCharacters() { | ||
// Create a X500Principal with control characters | ||
final X500Principal principal = new X500Principal("CN=Test\b\bName\n,O=TestOrg"); | ||
|
||
// Create a mock subclass of AbstractClientTlsStrategy | ||
final AbstractClientTlsStrategy tlsStrategy = new AbstractClientTlsStrategy( | ||
SSLContexts.createDefault(), | ||
null, null, SSLBufferMode.STATIC, | ||
HostnameVerificationPolicy.BUILTIN, | ||
HttpsSupport.getDefaultHostnameVerifier()) { | ||
@Override | ||
void applyParameters(final SSLEngine sslEngine, final SSLParameters sslParameters, final String[] appProtocols) { | ||
// No-op for test | ||
} | ||
|
||
@Override | ||
TlsDetails createTlsDetails(final SSLEngine sslEngine) { | ||
return null; // No-op for test | ||
} | ||
}; | ||
|
||
// Call the toEscapedString method | ||
final String escaped = tlsStrategy.toEscapedString(principal); | ||
|
||
// Assert that control characters are properly escaped | ||
assertEquals("CN=Test\\x08\\x08Name\\x0a,O=TestOrg", escaped); | ||
} | ||
|
||
@Test | ||
public void testVerifySession_escapedPeerAndIssuer() throws Exception { | ||
// Mock SSLSession and X509Certificate | ||
final SSLSession mockSession = mock(SSLSession.class); | ||
final X509Certificate mockCert = mock(X509Certificate.class); | ||
|
||
// Create a mock X500Principal with control characters | ||
final X500Principal peerPrincipal = new X500Principal("CN=Peer\bName,O=PeerOrg"); | ||
final X500Principal issuerPrincipal = new X500Principal("CN=Issuer\bName,O=IssuerOrg"); | ||
|
||
when(mockSession.getPeerCertificates()).thenReturn(new X509Certificate[]{mockCert}); | ||
when(mockCert.getSubjectX500Principal()).thenReturn(peerPrincipal); | ||
when(mockCert.getIssuerX500Principal()).thenReturn(issuerPrincipal); | ||
|
||
// Create a mock subclass of AbstractClientTlsStrategy | ||
final AbstractClientTlsStrategy tlsStrategy = new AbstractClientTlsStrategy( | ||
SSLContexts.createDefault(), | ||
null, null, SSLBufferMode.STATIC, | ||
HostnameVerificationPolicy.BUILTIN, | ||
HttpsSupport.getDefaultHostnameVerifier()) { | ||
@Override | ||
void applyParameters(final SSLEngine sslEngine, final SSLParameters sslParameters, final String[] appProtocols) { | ||
// No-op for test | ||
} | ||
|
||
@Override | ||
TlsDetails createTlsDetails(final SSLEngine sslEngine) { | ||
return null; // No-op for test | ||
} | ||
}; | ||
|
||
// Test the verifySession method | ||
tlsStrategy.verifySession("localhost", mockSession, null); | ||
|
||
} | ||
|
||
|
||
} |