]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - privacybrowser/dialogs/ViewSslCertificateDialog.java
Add an option to delete all domain settings at once. https://redmine.stoutner.com...
[PrivacyBrowserAndroid.git] / privacybrowser / dialogs / ViewSslCertificateDialog.java
1 /*
2  * Copyright © 2016-2020 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs;
21
22 import android.annotation.SuppressLint;
23 import android.app.Activity;
24 import android.app.AlertDialog;
25 import android.app.Dialog;
26 import android.content.Context;
27 import android.content.SharedPreferences;
28 import android.graphics.drawable.BitmapDrawable;
29 import android.graphics.drawable.Drawable;
30 import android.net.Uri;
31 import android.net.http.SslCertificate;
32 import android.os.Bundle;
33 import android.text.SpannableStringBuilder;
34 import android.text.Spanned;
35 import android.text.style.ForegroundColorSpan;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.view.WindowManager;
39 import android.widget.TextView;
40
41 import androidx.annotation.NonNull;
42 import androidx.fragment.app.DialogFragment;  // The AndroidX dialog fragment must be used or an error is produced on API <=22.
43 import androidx.preference.PreferenceManager;
44
45 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
46 import com.stoutner.privacybrowser.R;
47 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
48 import com.stoutner.privacybrowser.views.NestedScrollWebView;
49
50 import java.text.DateFormat;
51 import java.util.Calendar;
52 import java.util.Date;
53
54 public class ViewSslCertificateDialog extends DialogFragment {
55     public static ViewSslCertificateDialog displayDialog(long webViewFragmentId) {
56         // Create an arguments bundle.
57         Bundle argumentsBundle = new Bundle();
58
59         // Store the WebView fragment ID in the bundle.
60         argumentsBundle.putLong("webview_fragment_id", webViewFragmentId);
61
62         // Create a new instance of the dialog.
63         ViewSslCertificateDialog viewSslCertificateDialog = new ViewSslCertificateDialog();
64
65         // Add the bundle to the dialog.
66         viewSslCertificateDialog.setArguments(argumentsBundle);
67
68         // Return the new dialog.
69         return viewSslCertificateDialog;
70     }
71
72     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the alert dialog.
73     @SuppressLint("InflateParams")
74     @Override
75     @NonNull
76     public Dialog onCreateDialog(Bundle savedInstanceState) {
77         // Get a handle for the activity and the context.
78         Activity activity = getActivity();
79         Context context = getContext();
80
81         // Remove the incorrect lint warnings below that the activity and context might be null.
82         assert activity != null;
83         assert context != null;
84
85         // Get the activity's layout inflater.
86         LayoutInflater layoutInflater = activity.getLayoutInflater();
87
88         // Get the arguments.
89         Bundle arguments = getArguments();
90
91         // Remove the incorrect lint warning below that the arguments might be null.
92         assert arguments != null;
93
94         // Get the current position of this WebView fragment.
95         int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(arguments.getLong("webview_fragment_id"));
96
97         // Get the WebView tab fragment.
98         WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition);
99
100         // Get the fragment view.
101         View fragmentView = webViewTabFragment.getView();
102
103         // Remove the incorrect lint warning below that the fragment view might be null.
104         assert fragmentView != null;
105
106         // Get a handle for the current WebView.
107         NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
108
109
110         // Use a builder to create the alert dialog.
111         AlertDialog.Builder dialogBuilder;
112
113         // Get a handle for the shared preferences.
114         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
115
116         // Get the screenshot and theme preferences.
117         boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
118         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
119
120         // Set the style according to the theme.
121         if (darkTheme) {
122             dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogDark);
123         } else {
124             dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogLight);
125         }
126
127         // Create a drawable version of the favorite icon.
128         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), nestedScrollWebView.getFavoriteOrDefaultIcon());
129
130         // Set the icon.
131         dialogBuilder.setIcon(favoriteIconDrawable);
132
133         // Set the close button listener.  Using `null` as the listener closes the dialog without doing anything else.
134         dialogBuilder.setNegativeButton(R.string.close, null);
135
136         // Get the SSL certificate.
137         SslCertificate sslCertificate = nestedScrollWebView.getCertificate();
138
139         // Check to see if the website is encrypted.
140         if (sslCertificate == null) {  // The website is not encrypted.
141             // Set the title.
142             dialogBuilder.setTitle(R.string.unencrypted_website);
143
144             // Set the Layout.  The parent view is `null` because it will be assigned by the alert dialog.
145             dialogBuilder.setView(layoutInflater.inflate(R.layout.unencrypted_website_dialog, null));
146
147             // Create an alert dialog from the alert dialog builder.
148             final AlertDialog alertDialog = dialogBuilder.create();
149
150             // Disable screenshots if not allowed.
151             if (!allowScreenshots) {
152                 // Remove the warning below that `getWindow()` might be null.
153                 assert alertDialog.getWindow() != null;
154
155                 // Disable screenshots.
156                 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
157             }
158
159             // `onCreateDialog` requires the return of an `AlertDialog`.
160             return alertDialog;
161         } else {  // Display the SSL certificate information
162             // Set the title.
163             dialogBuilder.setTitle(R.string.ssl_certificate);
164
165             // Set the layout.  The parent view is `null` because it will be assigned by the alert dialog.
166             dialogBuilder.setView(layoutInflater.inflate(R.layout.view_ssl_certificate_dialog, null));
167
168             // Create an alert dialog from the builder.
169             final AlertDialog alertDialog = dialogBuilder.create();
170
171             // Disable screenshots if not allowed.
172             if (!allowScreenshots) {
173                 // Remove the warning below that `getWindow()` might be null.
174                 assert alertDialog.getWindow() != null;
175
176                 // Disable screenshots.
177                 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
178             }
179
180             // The alert dialog must be shown before items in the layout can be modified.
181             alertDialog.show();
182
183             // Get handles for the text views.
184             TextView domainTextView = alertDialog.findViewById(R.id.domain);
185             TextView ipAddressesTextView = alertDialog.findViewById(R.id.ip_addresses);
186             TextView issuedToCNameTextView = alertDialog.findViewById(R.id.issued_to_cname);
187             TextView issuedToONameTextView = alertDialog.findViewById(R.id.issued_to_oname);
188             TextView issuedToUNameTextView = alertDialog.findViewById(R.id.issued_to_uname);
189             TextView issuedByCNameTextView = alertDialog.findViewById(R.id.issued_by_cname);
190             TextView issuedByONameTextView = alertDialog.findViewById(R.id.issued_by_oname);
191             TextView issuedByUNameTextView = alertDialog.findViewById(R.id.issued_by_uname);
192             TextView startDateTextView = alertDialog.findViewById(R.id.start_date);
193             TextView endDateTextView = alertDialog.findViewById(R.id.end_date);
194
195             // Setup the labels.
196             String domainLabel = getString(R.string.domain_label) + "  ";
197             String ipAddressesLabel = getString(R.string.ip_addresses) + "  ";
198             String cNameLabel = getString(R.string.common_name) + "  ";
199             String oNameLabel = getString(R.string.organization) + "  ";
200             String uNameLabel = getString(R.string.organizational_unit) + "  ";
201             String startDateLabel = getString(R.string.start_date) + "  ";
202             String endDateLabel = getString(R.string.end_date) + "  ";
203
204             // Convert the formatted URL string to a URI.
205             Uri uri = Uri.parse(nestedScrollWebView.getUrl());
206
207             // Extract the domain name from the URI.
208             String domainString = uri.getHost();
209
210             // Get the strings from the SSL certificate.
211             String issuedToCName = sslCertificate.getIssuedTo().getCName();
212             String issuedToOName = sslCertificate.getIssuedTo().getOName();
213             String issuedToUName = sslCertificate.getIssuedTo().getUName();
214             String issuedByCName = sslCertificate.getIssuedBy().getCName();
215             String issuedByOName = sslCertificate.getIssuedBy().getOName();
216             String issuedByUName = sslCertificate.getIssuedBy().getUName();
217             Date startDate = sslCertificate.getValidNotBeforeDate();
218             Date endDate = sslCertificate.getValidNotAfterDate();
219
220             // Create spannable string builders for each text view that needs multiple colors of text.
221             SpannableStringBuilder domainStringBuilder = new SpannableStringBuilder(domainLabel + domainString);
222             SpannableStringBuilder ipAddressesStringBuilder = new SpannableStringBuilder(ipAddressesLabel + nestedScrollWebView.getCurrentIpAddresses());
223             SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
224             SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
225             SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
226             SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
227             SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
228             SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
229             SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
230             SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
231
232             // Create a red foreground color span.  The deprecated `getColor` must be used until the minimum API >= 23.
233             ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
234
235             // Create a blue foreground color span.
236             ForegroundColorSpan blueColorSpan;
237
238             // Set the blue color span according to the theme.  The deprecated `getColor()` must be used until the minimum API >= 23.
239             if (darkTheme) {
240                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
241             } else {
242                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
243             }
244
245             // Remove the incorrect lint error that `.equals` might produce a NullPointerException.
246             assert domainString != null;
247
248             // Formet the domain string and issued to CName colors.
249             if (domainString.equals(issuedToCName)) {  // `domainString` and `issuedToCName` match.
250                 // Set the strings to be blue.
251                 domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
252                 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
253             } else if(issuedToCName.startsWith("*.")){  // `issuedToCName` begins with a wildcard.
254                 // Remove the initial `*.`.
255                 String baseCertificateDomain = issuedToCName.substring(2);
256
257                 // Setup a copy of `domainString` to test subdomains.
258                 String domainStringSubdomain = domainString;
259
260                 // Initialize `domainNamesMatch`.
261                 boolean domainNamesMatch = false;
262
263                 // Check all the subdomains in `domainStringSubdomain` against `baseCertificateDomain`.
264                 while (!domainNamesMatch && domainStringSubdomain.contains(".")) {  // Stop checking if we know that `domainNamesMatch` is `true` or if we run out of  `.`.
265                     // Test the `domainStringSubdomain` against `baseCertificateDomain`.
266                     if (domainStringSubdomain.equals(baseCertificateDomain)) {
267                         domainNamesMatch = true;
268                     }
269
270                     // Strip out the lowest subdomain of `certificateCommonNameSubdomain`.
271                     domainStringSubdomain = domainStringSubdomain.substring(domainStringSubdomain.indexOf(".") + 1);
272                 }
273
274                 // Format the domain and issued to Common Name according to `domainNamesMatch`.
275                 if (domainNamesMatch) {  // `domainString` is a subdomain of the wildcard `issuedToCNameString`.
276                     // Set the strings to be blue.
277                     domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
278                     issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
279                 } else {  // `domainString` is not a subdomain of the wildcard `issuedToCNameString`.
280                     // Set the string to be red.
281                     domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
282                     issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
283                 }
284             } else {  // The strings do not match and `issuedToCNameString` does not begin with a wildcard.
285                 // Set the strings to be red.
286                 domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
287                 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
288             }
289
290             // Set the IP addresses, issued to, and issued by spans to display the certificate information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
291             ipAddressesStringBuilder.setSpan(blueColorSpan, ipAddressesLabel.length(), ipAddressesStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
292             issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
293             issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
294             issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
295             issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
296             issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
297
298             // Get the current date.
299             Date currentDate = Calendar.getInstance().getTime();
300
301             //  Format the start date color.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
302             if (startDate.after(currentDate)) {  // The certificate start date is in the future.
303                 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
304             } else {  // The certificate start date is in the past.
305                 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
306             }
307
308             // Format the end date color.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
309             if (endDate.before(currentDate)) {  // The certificate end date is in the past.
310                 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
311             } else {  // The certificate end date is in the future.
312                 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
313             }
314
315             // Display the strings.
316             domainTextView.setText(domainStringBuilder);
317             ipAddressesTextView.setText(ipAddressesStringBuilder);
318             issuedToCNameTextView.setText(issuedToCNameStringBuilder);
319             issuedToONameTextView.setText(issuedToONameStringBuilder);
320             issuedToUNameTextView.setText(issuedToUNameStringBuilder);
321             issuedByCNameTextView.setText(issuedByCNameStringBuilder);
322             issuedByONameTextView.setText(issuedByONameStringBuilder);
323             issuedByUNameTextView.setText(issuedByUNameStringBuilder);
324             startDateTextView.setText(startDateStringBuilder);
325             endDateTextView.setText(endDateStringBuilder);
326
327             // `onCreateDialog` requires the return of an alert dialog.
328             return alertDialog;
329         }
330     }
331 }