-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added TLS support (backported from master)
- Loading branch information
Showing
10 changed files
with
388 additions
and
8 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
64 changes: 64 additions & 0 deletions
64
src/main/java/com/github/shyiko/mysql/binlog/network/DefaultSSLSocketFactory.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,64 @@ | ||
/* | ||
* Copyright 2016 Stanley Shyiko | ||
* | ||
* 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 com.github.shyiko.mysql.binlog.network; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSocket; | ||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.net.SocketException; | ||
import java.security.GeneralSecurityException; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Stanley Shyiko</a> | ||
*/ | ||
public class DefaultSSLSocketFactory implements SSLSocketFactory { | ||
|
||
private final String protocol; | ||
|
||
public DefaultSSLSocketFactory() { | ||
this("TLSv1"); | ||
} | ||
|
||
/** | ||
* @param protocol TLSv1, TLSv1.1 or TLSv1.2 (the last two require JDK 7+) | ||
*/ | ||
public DefaultSSLSocketFactory(String protocol) { | ||
this.protocol = protocol; | ||
} | ||
|
||
@Override | ||
public SSLSocket createSocket(Socket socket) throws SocketException { | ||
SSLContext sc; | ||
try { | ||
sc = SSLContext.getInstance(this.protocol); | ||
initSSLContext(sc); | ||
} catch (GeneralSecurityException e) { | ||
throw new SocketException(e.getMessage()); | ||
} | ||
try { | ||
return (SSLSocket) sc.getSocketFactory() | ||
.createSocket(socket, socket.getInetAddress().getHostName(), socket.getPort(), true); | ||
} catch (IOException e) { | ||
throw new SocketException(e.getMessage()); | ||
} | ||
} | ||
|
||
protected void initSSLContext(SSLContext sc) throws GeneralSecurityException { | ||
sc.init(null, null, null); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/github/shyiko/mysql/binlog/network/IdentityVerificationException.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,29 @@ | ||
/* | ||
* Copyright 2016 Stanley Shyiko | ||
* | ||
* 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 com.github.shyiko.mysql.binlog.network; | ||
|
||
import javax.net.ssl.SSLException; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Stanley Shyiko</a> | ||
*/ | ||
public class IdentityVerificationException extends SSLException { | ||
|
||
public IdentityVerificationException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/github/shyiko/mysql/binlog/network/SSLMode.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,50 @@ | ||
/* | ||
* Copyright 2016 Stanley Shyiko | ||
* | ||
* 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 com.github.shyiko.mysql.binlog.network; | ||
|
||
/** | ||
* @see <a href="https://dev.mysql.com/doc/refman/5.7/en/secure-connection-options.html#option_general_ssl-mode> | ||
* ssl-mode</a> for the original documentation. | ||
* @author <a href="mailto:[email protected]">Stanley Shyiko</a> | ||
*/ | ||
public enum SSLMode { | ||
|
||
/** | ||
* Establish a secure (encrypted) connection if the server supports secure connections. | ||
* Fall back to an unencrypted connection otherwise. | ||
*/ | ||
PREFERRED, | ||
/** | ||
* Establish an unencrypted connection. | ||
*/ | ||
DISABLED, | ||
/** | ||
* Establish a secure connection if the server supports secure connections. | ||
* The connection attempt fails if a secure connection cannot be established. | ||
*/ | ||
REQUIRED, | ||
/** | ||
* Like REQUIRED, but additionally verify the server TLS certificate against the configured Certificate Authority | ||
* (CA) certificates. The connection attempt fails if no valid matching CA certificates are found. | ||
*/ | ||
VERIFY_CA, | ||
/** | ||
* Like VERIFY_CA, but additionally verify that the server certificate matches the host to which the connection is | ||
* attempted. | ||
*/ | ||
VERIFY_IDENTITY | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/github/shyiko/mysql/binlog/network/SSLSocketFactory.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,28 @@ | ||
/* | ||
* Copyright 2016 Stanley Shyiko | ||
* | ||
* 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 com.github.shyiko.mysql.binlog.network; | ||
|
||
import javax.net.ssl.SSLSocket; | ||
import java.net.Socket; | ||
import java.net.SocketException; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Stanley Shyiko</a> | ||
*/ | ||
public interface SSLSocketFactory { | ||
|
||
SSLSocket createSocket(Socket socket) throws SocketException; | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/github/shyiko/mysql/binlog/network/TLSHostnameVerifier.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,49 @@ | ||
/* | ||
* Copyright 2016 Stanley Shyiko | ||
* | ||
* 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 com.github.shyiko.mysql.binlog.network; | ||
|
||
import sun.security.util.HostnameChecker; | ||
|
||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.SSLPeerUnverifiedException; | ||
import javax.net.ssl.SSLSession; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.X509Certificate; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Stanley Shyiko</a> | ||
*/ | ||
public class TLSHostnameVerifier implements HostnameVerifier { | ||
|
||
public boolean verify(String hostname, SSLSession session) { | ||
HostnameChecker checker = HostnameChecker.getInstance(HostnameChecker.TYPE_TLS); | ||
try { | ||
Certificate[] peerCertificates = session.getPeerCertificates(); | ||
if (peerCertificates.length > 0 && peerCertificates[0] instanceof X509Certificate) { | ||
X509Certificate peerCertificate = (X509Certificate) peerCertificates[0]; | ||
try { | ||
checker.match(hostname, peerCertificate); | ||
return true; | ||
} catch (CertificateException ignored) { | ||
} | ||
} | ||
} catch (SSLPeerUnverifiedException ignored) { | ||
} | ||
return false; | ||
} | ||
|
||
} |
Oops, something went wrong.