Skip to content

Commit 7aa8b89

Browse files
authored
Merge pull request #103 from ConnectSDK/PLAT-38706-anishtd
Support SSL certificate for backward compatibility
2 parents b2a7be5 + 87a7a90 commit 7aa8b89

File tree

1 file changed

+55
-47
lines changed

1 file changed

+55
-47
lines changed

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

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

52+
import java.security.NoSuchProviderException;
53+
import java.security.InvalidKeyException;
54+
import java.security.SignatureException;
5255
import java.security.PublicKey;
5356
import java.security.cert.CertificateExpiredException;
5457
import java.security.cert.CertificateNotYetValidException;
@@ -278,7 +281,7 @@ protected void handleMessage(JSONObject message) {
278281
// Log.d(Util.T, "Found requests need to handle response");
279282
if (payload != null) {
280283
Util.postSuccess(request.getResponseListener(), payload);
281-
}
284+
}
282285
else {
283286
Util.postError(request.getResponseListener(), new ServiceCommandError(-1, "JSON parse error", null));
284287
}
@@ -310,7 +313,7 @@ protected void handleMessage(JSONObject message) {
310313
if (id != null)
311314
requests.remove(id);
312315
} else {
313-
Log.d(TAG, "Certification Verification Failed");
316+
Log.d(Util.T, "Certification Verification Failed");
314317
mListener.onRegistrationFailed(new ServiceCommandError(0, "Certificate Registration failed", null));
315318
}
316319
}
@@ -340,7 +343,7 @@ protected void handleMessage(JSONObject message) {
340343
if (request != null) {
341344
Util.postError(request.getResponseListener(), new ServiceCommandError(errorCode, errorDesc, payload));
342345

343-
if (!(request instanceof URLServiceSubscription))
346+
if (!(request instanceof URLServiceSubscription))
344347
requests.remove(id);
345348

346349
}
@@ -396,7 +399,7 @@ private void helloTV() {
396399
@SuppressWarnings("deprecation")
397400
int height = display.getHeight(); // deprecated, but still needed for supporting API levels 10-12
398401

399-
String screenResolution = String.format("%dx%d", width, height);
402+
String screenResolution = String.format("%dx%d", width, height);
400403

401404
// app Name
402405
ApplicationInfo applicationInfo;
@@ -476,45 +479,51 @@ public void onSuccess(Object object) {
476479
X509Certificate cert = customTrustManager.getLastCheckedCertificate();
477480
PublicKey pk = null;
478481

482+
pk = cert.getPublicKey();
483+
String pubKey = Base64.encodeToString(pk.getEncoded(),Base64.DEFAULT);
479484

480-
pk = cert.getPublicKey();
481-
String pubKey = Base64.encodeToString(pk.getEncoded(),Base64.DEFAULT);
482-
483-
if(!(Public_Key == null || Public_Key.isEmpty())) {
484-
boolean verified = pubKey.trim().equalsIgnoreCase(Public_Key.trim());
485-
if (verified) {
486-
payload.put("public-key", 1);
487-
public_key_value = 1;
488-
} else {
489-
payload.put("public-key", -1);
490-
public_key_value = -1;
491-
}
485+
try {
486+
cert.verify(pk);
487+
verification_status = true;
488+
} catch (CertificateException|SignatureException e) {
489+
if (!(Public_Key == null || Public_Key.isEmpty())) {
490+
boolean verified = pubKey.trim().equalsIgnoreCase(Public_Key.trim());
491+
if (verified) {
492+
payload.put("public-key", 1);
493+
public_key_value = 1;
492494
} else {
493495
payload.put("public-key", -1);
494496
public_key_value = -1;
495497
}
498+
} else {
499+
payload.put("public-key", 1);
500+
public_key_value = 1;
501+
}
496502

503+
try {
504+
((X509Certificate) cert).checkValidity();
505+
payload.put("validity", 1);
506+
valid_value = 1;
507+
} catch (CertificateExpiredException | CertificateNotYetValidException error) {
508+
payload.put("validity", -1);
509+
valid_value = -1;
510+
error.printStackTrace();
511+
}
497512

498-
try {
499-
((X509Certificate)cert).checkValidity();
500-
payload.put("validity", 1);
501-
valid_value = 1;
502-
}catch (CertificateExpiredException|CertificateNotYetValidException e) {
503-
payload.put("validity", -1);
504-
valid_value = -1;
513+
if (public_key_value == 1 && valid_value == 1) {
514+
verification_status = true;
515+
}
516+
517+
requests.put(dataId, command);
518+
sendMessage(headers, payload);
519+
520+
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException e) {
505521
e.printStackTrace();
506522
}
507523

508524
} catch (JSONException e) {
509525
e.printStackTrace();
510526
}
511-
512-
requests.put(dataId, command);
513-
sendMessage(headers, payload);
514-
515-
if(public_key_value == 1 && valid_value == 1) {
516-
verification_status = true;
517-
}
518527
}
519528

520529
protected void sendRegister() {
@@ -523,7 +532,7 @@ protected void sendRegister() {
523532
@Override
524533
public void onError(ServiceCommandError error) {
525534
state = State.INITIAL;
526-
535+
527536
if (mListener != null)
528537
mListener.onRegistrationFailed(error);
529538
}
@@ -610,37 +619,37 @@ public void sendPairingKey(String pairingKey) {
610619
@Override
611620
public void onError(ServiceCommandError error) {
612621
state = State.INITIAL;
613-
622+
614623
if (mListener != null)
615624
mListener.onFailWithError(error);
616625
}
617626

618627
@Override
619628
public void onSuccess(Object object) { }
620629
};
621-
630+
622631
String uri = "ssap://pairing/setPin";
623632

624633
int dataId = this.nextRequestId++;
625-
634+
626635
ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(this, null, null, listener);
627636
command.setRequestId(dataId);
628-
637+
629638
JSONObject headers = new JSONObject();
630639
JSONObject payload = new JSONObject();
631-
640+
632641
try {
633642
headers.put("type", "request");
634643
headers.put("id", dataId);
635644
headers.put("uri", uri);
636-
645+
637646
payload.put("pin", pairingKey);
638647
} catch (JSONException e) {
639648
e.printStackTrace();
640649
}
641650

642651
requests.put(dataId, command);
643-
652+
644653
sendMessage(headers, payload);
645654
}
646655

@@ -663,9 +672,9 @@ protected void handleRegistered() {
663672
// ConnectableDevice storedDevice = connectableDeviceStore.getDevice(mService.getServiceConfig().getServiceUUID());
664673
// if (storedDevice == null) {
665674
// storedDevice = new ConnectableDevice(
666-
// mService.getServiceDescription().getIpAddress(),
667-
// mService.getServiceDescription().getFriendlyName(),
668-
// mService.getServiceDescription().getModelName(),
675+
// mService.getServiceDescription().getIpAddress(),
676+
// mService.getServiceDescription().getFriendlyName(),
677+
// mService.getServiceDescription().getModelName(),
669678
// mService.getServiceDescription().getModelNumber());
670679
// }
671680
// storedDevice.addService(WebOSTVService.this);
@@ -750,7 +759,7 @@ protected void sendCommandImmediately(ServiceCommand<?> command) {
750759
}
751760

752761
this.sendMessage(headers, null);
753-
}
762+
}
754763
else if (payloadType.equals("hello")) {
755764
this.send(payload.toString());
756765
}
@@ -909,10 +918,10 @@ private X509Certificate loadCertificateFromPEM(String pemString) {
909918
}
910919

911920
public static boolean isInteger(String s) {
912-
try {
913-
Integer.parseInt(s);
914-
} catch(NumberFormatException e) {
915-
return false;
921+
try {
922+
Integer.parseInt(s);
923+
} catch(NumberFormatException e) {
924+
return false;
916925
}
917926
// only got here if we didn't return false
918927
return true;
@@ -929,5 +938,4 @@ public interface WebOSTVServiceSocketClientListener {
929938
public Boolean onReceiveMessage(JSONObject message);
930939

931940
}
932-
933941
}

0 commit comments

Comments
 (0)