|
49 | 49 | import com.connectsdk.service.command.URLServiceSubscription; |
50 | 50 | import com.connectsdk.service.config.WebOSTVServiceConfig; |
51 | 51 |
|
| 52 | +import java.security.PublicKey; |
| 53 | +import java.security.cert.CertificateExpiredException; |
| 54 | +import java.security.cert.CertificateNotYetValidException; |
| 55 | + |
52 | 56 | @SuppressLint("DefaultLocale") |
53 | 57 | public class WebOSTVServiceSocketClient extends WebSocketClient implements ServiceCommandProcessor { |
54 | 58 |
|
@@ -290,12 +294,18 @@ protected void handleMessage(JSONObject message) { |
290 | 294 |
|
291 | 295 | // Track SSL certificate |
292 | 296 | // Not the prettiest way to get it, but we don't have direct access to the SSLEngine |
293 | | - ((WebOSTVServiceConfig) mService.getServiceConfig()).setServerCertificate(customTrustManager.getLastCheckedCertificate()); |
294 | 297 |
|
295 | | - handleRegistered(); |
| 298 | + sendVerification(); |
| 299 | + if (verification_status) { |
| 300 | + ((WebOSTVServiceConfig) mService.getServiceConfig()).setServerCertificate(customTrustManager.getLastCheckedCertificate()); |
| 301 | + handleRegistered(); |
296 | 302 |
|
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 | + } |
299 | 309 | } |
300 | 310 | } else if ("error".equals(type)) { |
301 | 311 | String error = message.optString("error"); |
@@ -422,6 +432,84 @@ private void helloTV() { |
422 | 432 | this.sendCommandImmediately(request); |
423 | 433 | } |
424 | 434 |
|
| 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 | + |
425 | 513 | protected void sendRegister() { |
426 | 514 | ResponseListener<Object> listener = new ResponseListener<Object>() { |
427 | 515 |
|
|
0 commit comments