Skip to content

Commit 87549a7

Browse files
authored
Merge pull request #100 from ConnectSDK/PLAT-38706-anishlg
PLAT-38706: Improve security level for ConnectSDK with CA certificate
2 parents f37a594 + 2719f95 commit 87549a7

File tree

1 file changed

+92
-4
lines changed

1 file changed

+92
-4
lines changed

src/com/connectsdk/service/webos/WebOSTVServiceSocketClient.java

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
import com.connectsdk.service.command.URLServiceSubscription;
5050
import com.connectsdk.service.config.WebOSTVServiceConfig;
5151

52+
import java.security.PublicKey;
53+
import java.security.cert.CertificateExpiredException;
54+
import java.security.cert.CertificateNotYetValidException;
55+
5256
@SuppressLint("DefaultLocale")
5357
public class WebOSTVServiceSocketClient extends WebSocketClient implements ServiceCommandProcessor {
5458

@@ -290,12 +294,18 @@ protected void handleMessage(JSONObject message) {
290294

291295
// Track SSL certificate
292296
// Not the prettiest way to get it, but we don't have direct access to the SSLEngine
293-
((WebOSTVServiceConfig) mService.getServiceConfig()).setServerCertificate(customTrustManager.getLastCheckedCertificate());
294297

295-
handleRegistered();
298+
sendVerification();
299+
if (verification_status) {
300+
((WebOSTVServiceConfig) mService.getServiceConfig()).setServerCertificate(customTrustManager.getLastCheckedCertificate());
301+
handleRegistered();
296302

297-
if (id != null)
298-
requests.remove(id);
303+
if (id != null)
304+
requests.remove(id);
305+
} else {
306+
Log.d(TAG, "Certification Verification Failed");
307+
mListener.onRegistrationFailed(new ServiceCommandError(0, "Certificate Registration failed", null));
308+
}
299309
}
300310
} else if ("error".equals(type)) {
301311
String error = message.optString("error");
@@ -422,6 +432,84 @@ private void helloTV() {
422432
this.sendCommandImmediately(request);
423433
}
424434

435+
protected void sendVerification() {
436+
ResponseListener<Object> listener = new ResponseListener<Object>() {
437+
438+
@Override
439+
public void onError(ServiceCommandError error) {
440+
state = State.INITIAL;
441+
442+
if (mListener != null)
443+
mListener.onRegistrationFailed(error);
444+
}
445+
446+
@Override
447+
public void onSuccess(Object object) {
448+
if (object instanceof JSONObject) {
449+
450+
}
451+
}
452+
};
453+
454+
int dataId = this.nextRequestId++;
455+
456+
ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(this, null, null, listener);
457+
command.setRequestId(dataId);
458+
459+
JSONObject headers = new JSONObject();
460+
JSONObject payload = new JSONObject();
461+
int public_key_value = 0;
462+
int valid_value = 0;
463+
464+
try {
465+
466+
headers.put("type", "verification");
467+
headers.put("id", dataId);
468+
469+
X509Certificate cert = customTrustManager.getLastCheckedCertificate();
470+
PublicKey pk = null;
471+
472+
473+
pk = cert.getPublicKey();
474+
String pubKey = Base64.encodeToString(pk.getEncoded(),Base64.DEFAULT);
475+
476+
if(!(Public_Key == null || Public_Key.isEmpty())) {
477+
boolean verified = pubKey.trim().equalsIgnoreCase(Public_Key.trim());
478+
if (verified) {
479+
payload.put("public-key", 1);
480+
public_key_value = 1;
481+
} else {
482+
payload.put("public-key", -1);
483+
public_key_value = -1;
484+
}
485+
} else {
486+
payload.put("public-key", -1);
487+
public_key_value = -1;
488+
}
489+
490+
491+
try {
492+
((X509Certificate)cert).checkValidity();
493+
payload.put("validity", 1);
494+
valid_value = 1;
495+
}catch (CertificateExpiredException|CertificateNotYetValidException e) {
496+
payload.put("validity", -1);
497+
valid_value = -1;
498+
e.printStackTrace();
499+
}
500+
501+
} catch (JSONException e) {
502+
e.printStackTrace();
503+
}
504+
505+
requests.put(dataId, command);
506+
sendMessage(headers, payload);
507+
508+
if(public_key_value == 1 && valid_value == 1) {
509+
verification_status = true;
510+
}
511+
}
512+
425513
protected void sendRegister() {
426514
ResponseListener<Object> listener = new ResponseListener<Object>() {
427515

0 commit comments

Comments
 (0)