BaseActivity.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.activities;
  21. import android.annotation.SuppressLint;
  22. import android.os.Bundle;
  23. import android.util.Log;
  24. import android.webkit.SslErrorHandler;
  25. import com.nextcloud.talk.R;
  26. import com.nextcloud.talk.application.NextcloudTalkApplication;
  27. import com.nextcloud.talk.events.CertificateEvent;
  28. import com.nextcloud.talk.utils.ssl.MagicTrustManager;
  29. import com.yarolegovich.lovelydialog.LovelyStandardDialog;
  30. import org.greenrobot.eventbus.EventBus;
  31. import org.greenrobot.eventbus.Subscribe;
  32. import org.greenrobot.eventbus.ThreadMode;
  33. import java.security.cert.CertificateParsingException;
  34. import java.security.cert.X509Certificate;
  35. import java.text.DateFormat;
  36. import java.util.List;
  37. import javax.inject.Inject;
  38. import androidx.annotation.Nullable;
  39. import androidx.appcompat.app.AppCompatActivity;
  40. import autodagger.AutoInjector;
  41. @AutoInjector(NextcloudTalkApplication.class)
  42. public class BaseActivity extends AppCompatActivity {
  43. private static final String TAG = "BaseActivity";
  44. @Inject
  45. EventBus eventBus;
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  50. }
  51. public void showCertificateDialog(X509Certificate cert, MagicTrustManager magicTrustManager,
  52. @Nullable SslErrorHandler sslErrorHandler) {
  53. DateFormat formatter = DateFormat.getDateInstance(DateFormat.LONG);
  54. String validFrom = formatter.format(cert.getNotBefore());
  55. String validUntil = formatter.format(cert.getNotAfter());
  56. String issuedBy = cert.getIssuerDN().toString();
  57. String issuedFor;
  58. try {
  59. if (cert.getSubjectAlternativeNames() != null) {
  60. StringBuilder stringBuilder = new StringBuilder();
  61. for (Object o : cert.getSubjectAlternativeNames()) {
  62. List list = (List) o;
  63. int type = (Integer) list.get(0);
  64. if (type == 2) {
  65. String name = (String) list.get(1);
  66. stringBuilder.append("[").append(type).append("]").append(name).append(" ");
  67. }
  68. }
  69. issuedFor = stringBuilder.toString();
  70. } else {
  71. issuedFor = cert.getSubjectDN().getName();
  72. }
  73. @SuppressLint("StringFormatMatches") String dialogText = String.format(getResources()
  74. .getString(R.string.nc_certificate_dialog_text),
  75. issuedBy, issuedFor, validFrom, validUntil);
  76. new LovelyStandardDialog(this)
  77. .setTopColorRes(R.color.nc_darkRed)
  78. .setNegativeButtonColorRes(R.color.nc_darkRed)
  79. .setPositiveButtonColorRes(R.color.colorPrimaryDark)
  80. .setIcon(R.drawable.ic_security_white_24dp)
  81. .setTitle(R.string.nc_certificate_dialog_title)
  82. .setMessage(dialogText)
  83. .setPositiveButton(R.string.nc_yes, v -> {
  84. magicTrustManager.addCertInTrustStore(cert);
  85. if (sslErrorHandler != null) {
  86. sslErrorHandler.proceed();
  87. }
  88. })
  89. .setNegativeButton(R.string.nc_no, view1 -> {
  90. if (sslErrorHandler != null) {
  91. sslErrorHandler.cancel();
  92. }
  93. })
  94. .show();
  95. } catch (CertificateParsingException e) {
  96. Log.d(TAG, "Failed to parse the certificate");
  97. }
  98. }
  99. @Subscribe(threadMode = ThreadMode.MAIN)
  100. public void onMessageEvent(CertificateEvent event) {
  101. showCertificateDialog(event.getX509Certificate(), event.getMagicTrustManager(), event.getSslErrorHandler());
  102. }
  103. @Override
  104. public void onStart() {
  105. super.onStart();
  106. eventBus.register(this);
  107. }
  108. @Override
  109. public void onStop() {
  110. super.onStop();
  111. eventBus.unregister(this);
  112. }
  113. }