BaseActivity.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.app.KeyguardManager;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.os.Build;
  26. import android.os.Bundle;
  27. import android.os.Handler;
  28. import android.util.Log;
  29. import android.view.WindowManager;
  30. import android.webkit.SslErrorHandler;
  31. import androidx.annotation.Nullable;
  32. import androidx.annotation.RequiresApi;
  33. import androidx.appcompat.app.AppCompatActivity;
  34. import androidx.biometric.BiometricPrompt;
  35. import autodagger.AutoInjector;
  36. import com.nextcloud.talk.R;
  37. import com.nextcloud.talk.application.NextcloudTalkApplication;
  38. import com.nextcloud.talk.events.CertificateEvent;
  39. import com.nextcloud.talk.utils.HandlerExecutor;
  40. import com.nextcloud.talk.utils.SecurityUtils;
  41. import com.nextcloud.talk.utils.preferences.AppPreferences;
  42. import com.nextcloud.talk.utils.ssl.MagicTrustManager;
  43. import com.yarolegovich.lovelydialog.LovelyStandardDialog;
  44. import org.greenrobot.eventbus.EventBus;
  45. import org.greenrobot.eventbus.Subscribe;
  46. import org.greenrobot.eventbus.ThreadMode;
  47. import javax.inject.Inject;
  48. import java.security.cert.CertificateParsingException;
  49. import java.security.cert.X509Certificate;
  50. import java.text.DateFormat;
  51. import java.util.List;
  52. import java.util.concurrent.Executor;
  53. @AutoInjector(NextcloudTalkApplication.class)
  54. public class BaseActivity extends AppCompatActivity {
  55. private static final String TAG = "BaseActivity";
  56. private static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 112;
  57. @Inject
  58. EventBus eventBus;
  59. @Inject
  60. AppPreferences appPreferences;
  61. @Inject
  62. Context context;
  63. private KeyguardManager keyguardManager;
  64. @Override
  65. protected void onCreate(Bundle savedInstanceState) {
  66. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  67. super.onCreate(savedInstanceState);
  68. if (appPreferences.getIsScreenLocked()) {
  69. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  70. SecurityUtils.createKey(appPreferences.getScreenLockTimeout());
  71. }
  72. }
  73. }
  74. @Override
  75. public void onResume() {
  76. super.onResume();
  77. if (appPreferences.getIsScreenSecured()) {
  78. getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
  79. } else {
  80. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
  81. }
  82. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  83. checkIfWeAreSecure();
  84. }
  85. }
  86. @RequiresApi(api = Build.VERSION_CODES.M)
  87. private void checkIfWeAreSecure() {
  88. keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
  89. if (keyguardManager != null && keyguardManager.isKeyguardSecure() && appPreferences.getIsScreenLocked()) {
  90. if (!SecurityUtils.checkIfWeAreAuthenticated(appPreferences.getScreenLockTimeout())) {
  91. if (SecurityUtils.isFingerprintAvailable(context)) {
  92. showBiometricDialog();
  93. } else {
  94. showAuthenticationScreen();
  95. }
  96. }
  97. }
  98. }
  99. @RequiresApi(api = Build.VERSION_CODES.M)
  100. private void showBiometricDialog() {
  101. final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
  102. .setTitle(String.format(context.getString(R.string.nc_biometric_unlock), context.getString(R.string.nc_app_name)))
  103. .setNegativeButtonText(context.getString(R.string.nc_cancel))
  104. .build();
  105. Executor mainExecutor = new HandlerExecutor(new Handler(getMainLooper()));
  106. final BiometricPrompt biometricPrompt = new BiometricPrompt(this, mainExecutor,
  107. new BiometricPrompt.AuthenticationCallback() {
  108. }
  109. );
  110. biometricPrompt.authenticate(promptInfo, SecurityUtils.getCryptoObject());
  111. }
  112. private void showAuthenticationScreen() {
  113. Intent intent = keyguardManager.createConfirmDeviceCredentialIntent(null, null);
  114. if (intent != null) {
  115. startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
  116. }
  117. }
  118. @Override
  119. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  120. if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
  121. if (resultCode == RESULT_OK) {
  122. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  123. if (SecurityUtils.checkIfWeAreAuthenticated(appPreferences.getScreenLockTimeout())) {
  124. // all went well
  125. }
  126. }
  127. } else {
  128. // we didnt auth
  129. }
  130. }
  131. }
  132. public void showCertificateDialog(X509Certificate cert, MagicTrustManager magicTrustManager,
  133. @Nullable SslErrorHandler sslErrorHandler) {
  134. DateFormat formatter = DateFormat.getDateInstance(DateFormat.LONG);
  135. String validFrom = formatter.format(cert.getNotBefore());
  136. String validUntil = formatter.format(cert.getNotAfter());
  137. String issuedBy = cert.getIssuerDN().toString();
  138. String issuedFor;
  139. try {
  140. if (cert.getSubjectAlternativeNames() != null) {
  141. StringBuilder stringBuilder = new StringBuilder();
  142. for (Object o : cert.getSubjectAlternativeNames()) {
  143. List list = (List) o;
  144. int type = (Integer) list.get(0);
  145. if (type == 2) {
  146. String name = (String) list.get(1);
  147. stringBuilder.append("[").append(type).append("]").append(name).append(" ");
  148. }
  149. }
  150. issuedFor = stringBuilder.toString();
  151. } else {
  152. issuedFor = cert.getSubjectDN().getName();
  153. }
  154. @SuppressLint("StringFormatMatches") String dialogText = String.format(getResources()
  155. .getString(R.string.nc_certificate_dialog_text),
  156. issuedBy, issuedFor, validFrom, validUntil);
  157. new LovelyStandardDialog(this)
  158. .setTopColorRes(R.color.nc_darkRed)
  159. .setNegativeButtonColorRes(R.color.nc_darkRed)
  160. .setPositiveButtonColorRes(R.color.colorPrimaryDark)
  161. .setIcon(R.drawable.ic_security_white_24dp)
  162. .setTitle(R.string.nc_certificate_dialog_title)
  163. .setMessage(dialogText)
  164. .setPositiveButton(R.string.nc_yes, v -> {
  165. magicTrustManager.addCertInTrustStore(cert);
  166. if (sslErrorHandler != null) {
  167. sslErrorHandler.proceed();
  168. }
  169. })
  170. .setNegativeButton(R.string.nc_no, view1 -> {
  171. if (sslErrorHandler != null) {
  172. sslErrorHandler.cancel();
  173. }
  174. })
  175. .show();
  176. } catch (CertificateParsingException e) {
  177. Log.d(TAG, "Failed to parse the certificate");
  178. }
  179. }
  180. @Subscribe(threadMode = ThreadMode.MAIN)
  181. public void onMessageEvent(CertificateEvent event) {
  182. showCertificateDialog(event.getX509Certificate(), event.getMagicTrustManager(), event.getSslErrorHandler());
  183. }
  184. @Override
  185. public void onStart() {
  186. super.onStart();
  187. eventBus.register(this);
  188. }
  189. @Override
  190. public void onStop() {
  191. super.onStop();
  192. eventBus.unregister(this);
  193. }
  194. @Override
  195. public void onPause() {
  196. super.onPause();
  197. if (!disposable.isDisposed()) {
  198. disposable.dispose();
  199. }
  200. }
  201. }