-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility to use particular certificate for client during 2 way…
… ssl connection
- Loading branch information
Simca
committed
Dec 14, 2021
1 parent
7eb1b8e
commit f795626
Showing
8 changed files
with
187 additions
and
23 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
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
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
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
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
87 changes: 87 additions & 0 deletions
87
src/main/java/io/cdap/plugin/http/source/common/http/X509KeyManagerAliasWrapper.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,87 @@ | ||
/* | ||
* Copyright © 2021 Cask Data, Inc. | ||
* | ||
* Licensed 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. | ||
*/ | ||
package io.cdap.plugin.http.source.common.http; | ||
|
||
import java.net.Socket; | ||
import java.security.Principal; | ||
import java.security.PrivateKey; | ||
import java.security.cert.X509Certificate; | ||
|
||
import javax.net.ssl.KeyManager; | ||
import javax.net.ssl.KeyManagerFactory; | ||
import javax.net.ssl.X509KeyManager; | ||
|
||
|
||
/** | ||
* This is just wrapper over SunX509KeyManagerImpl with possibility to provide specific alias | ||
* | ||
* Usage example: | ||
* X509KeyManagerAliasWrapper.getKeyManagers(keyManagerFactory, CERT_ALIAS); | ||
*/ | ||
public class X509KeyManagerAliasWrapper implements X509KeyManager { | ||
|
||
private final X509KeyManager originalKeyManager; | ||
private final String certAlias; | ||
|
||
public X509KeyManagerAliasWrapper(X509KeyManager originalKeyManager, String certAlias) { | ||
this.originalKeyManager = originalKeyManager; | ||
this.certAlias = certAlias; | ||
} | ||
|
||
public static KeyManager[] getKeyManagers(KeyManagerFactory keyManagerFactory, String certAlias) { | ||
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers(); | ||
|
||
// Current implementation only support X509 Certificates | ||
if (keyManagers.length != 1) { | ||
return keyManagers; | ||
} | ||
if (!(keyManagers[0] instanceof X509KeyManager)) { | ||
return keyManagers; | ||
} | ||
|
||
return new KeyManager[]{ new X509KeyManagerAliasWrapper((X509KeyManager) keyManagers[0], certAlias) }; | ||
}; | ||
|
||
@Override | ||
public String chooseClientAlias(String[] strings, Principal[] principals, Socket socket) { | ||
return certAlias; | ||
} | ||
|
||
@Override | ||
public String[] getClientAliases(String s, Principal[] principals) { | ||
return originalKeyManager.getClientAliases(s, principals); | ||
} | ||
|
||
@Override | ||
public String[] getServerAliases(String s, Principal[] principals) { | ||
return originalKeyManager.getServerAliases(s, principals); | ||
} | ||
|
||
@Override | ||
public String chooseServerAlias(String s, Principal[] principals, Socket socket) { | ||
return originalKeyManager.chooseServerAlias(s, principals, socket); | ||
} | ||
|
||
@Override | ||
public X509Certificate[] getCertificateChain(String s) { | ||
return originalKeyManager.getCertificateChain(s); | ||
} | ||
|
||
@Override | ||
public PrivateKey getPrivateKey(String s) { | ||
return originalKeyManager.getPrivateKey(s); | ||
} | ||
} |
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
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