Skip to content

Commit

Permalink
Use secure port for mouse socket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarl-jinyoung authored and Suresh Arumugam committed Jun 29, 2017
1 parent 2c04b6f commit 48df1d7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local.properties
*.launch
.cproject
.buildpath
.idea/
*.DS_Store
bin/jarlist.cache
test/test-reports/
test/test-reports/
23 changes: 15 additions & 8 deletions src/com/connectsdk/service/webos/WebOSTVMouseSocketConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public interface WebOSTVMouseSocketListener {
WebSocketClient ws;
String socketPath;
WebOSTVMouseSocketListener listener;
WebOSTVTrustManager customTrustManager;

public enum ButtonType {
HOME,
Expand All @@ -50,14 +51,8 @@ public enum ButtonType {
public WebOSTVMouseSocketConnection(String socketPath, WebOSTVMouseSocketListener listener) {
Log.d("PtrAndKeyboardFragment", "got socketPath: " + socketPath);

this.listener = listener;

if (socketPath.startsWith("wss:")) {
this.socketPath = socketPath.replace("wss:", "ws:").replace(":3001/", ":3000/"); // downgrade to plaintext
Log.d("PtrAndKeyboardFragment", "downgraded socketPath: " + this.socketPath);
}
else
this.socketPath = socketPath;
this.listener = listener;
this.socketPath = socketPath;

try {
URI uri = new URI(this.socketPath);
Expand Down Expand Up @@ -96,6 +91,18 @@ public void onClose(int arg0, String arg1, boolean arg2) {
}
};

try {
SSLContext sslContext = SSLContext.getInstance("TLS");
customTrustManager = new WebOSTVTrustManager();
sslContext.init(null, new WebOSTVTrustManager[] {customTrustManager}, null);
WebSocketClient.WebSocketClientFactory fac = new DefaultSSLWebSocketClientFactory(sslContext);
ws.setWebSocketFactory(fac);
} catch (KeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

ws.connect();
}

Expand Down
50 changes: 1 addition & 49 deletions src/com/connectsdk/service/webos/WebOSTVServiceSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public enum State {

WebOSTVServiceSocketClientListener mListener;
WebOSTVService mService;
WebOSTVTrustManager customTrustManager;

int nextRequestId = 1;

TrustManager customTrustManager;
State state = State.INITIAL;

JSONObject manifest;
Expand Down Expand Up @@ -816,54 +816,6 @@ public static boolean isInteger(String s) {
return true;
}

class TrustManager implements X509TrustManager {
X509Certificate expectedCert;
X509Certificate lastCheckedCert;

public void setExpectedCertificate(X509Certificate cert) {
this.expectedCert = cert;
}

public X509Certificate getLastCheckedCertificate () {
return lastCheckedCert;
}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
Log.d(Util.T, "Expecting device cert " + (expectedCert != null ? expectedCert.getSubjectDN() : "(any)"));

if (chain != null && chain.length > 0) {
X509Certificate cert = chain[0];

lastCheckedCert = cert;

if (expectedCert != null) {
byte [] certBytes = cert.getEncoded();
byte [] expectedCertBytes = expectedCert.getEncoded();

Log.d(Util.T, "Device presented cert " + cert.getSubjectDN());

if (!Arrays.equals(certBytes, expectedCertBytes)) {
throw new CertificateException("certificate does not match");
}
}
} else {
lastCheckedCert = null;
throw new CertificateException("no server certificate");
}
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}

public interface WebOSTVServiceSocketClientListener {

public void onConnect();
Expand Down
59 changes: 59 additions & 0 deletions src/com/connectsdk/service/webos/WebOSTVTrustManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.connectsdk.service.webos;

import android.util.Log;

import com.connectsdk.core.Util;

import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import java.util.Arrays;

public class WebOSTVTrustManager implements X509TrustManager {
X509Certificate expectedCert;
X509Certificate lastCheckedCert;

public void setExpectedCertificate(X509Certificate cert) {
this.expectedCert = cert;
}

public X509Certificate getLastCheckedCertificate () {
return lastCheckedCert;
}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
Log.d(Util.T, "Expecting device cert " + (expectedCert != null ? expectedCert.getSubjectDN() : "(any)"));

if (chain != null && chain.length > 0) {
X509Certificate cert = chain[0];

lastCheckedCert = cert;

if (expectedCert != null) {
byte [] certBytes = cert.getEncoded();
byte [] expectedCertBytes = expectedCert.getEncoded();

Log.d(Util.T, "Device presented cert " + cert.getSubjectDN());

if (!Arrays.equals(certBytes, expectedCertBytes)) {
throw new CertificateException("certificate does not match");
}
}
} else {
lastCheckedCert = null;
throw new CertificateException("no server certificate");
}
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}

0 comments on commit 48df1d7

Please sign in to comment.