2 * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.dialogs;
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;
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;
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;
50 import java.text.DateFormat;
51 import java.util.Calendar;
52 import java.util.Date;
54 public class ViewSslCertificateDialog extends DialogFragment {
55 public static ViewSslCertificateDialog displayDialog(long webViewFragmentId) {
56 // Create an arguments bundle.
57 Bundle argumentsBundle = new Bundle();
59 // Store the WebView fragment ID in the bundle.
60 argumentsBundle.putLong("webview_fragment_id", webViewFragmentId);
62 // Create a new instance of the dialog.
63 ViewSslCertificateDialog viewSslCertificateDialog = new ViewSslCertificateDialog();
65 // Add the bundle to the dialog.
66 viewSslCertificateDialog.setArguments(argumentsBundle);
68 // Return the new dialog.
69 return viewSslCertificateDialog;
72 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the alert dialog.
73 @SuppressLint("InflateParams")
76 public Dialog onCreateDialog(Bundle savedInstanceState) {
77 // Get a handle for the activity and the context.
78 Activity activity = getActivity();
79 Context context = getContext();
81 // Remove the incorrect lint warnings below that the activity and context might be null.
82 assert activity != null;
83 assert context != null;
85 // Get the activity's layout inflater.
86 LayoutInflater layoutInflater = activity.getLayoutInflater();
89 Bundle arguments = getArguments();
91 // Remove the incorrect lint warning below that the arguments might be null.
92 assert arguments != null;
94 // Get the current position of this WebView fragment.
95 int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(arguments.getLong("webview_fragment_id"));
97 // Get the WebView tab fragment.
98 WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition);
100 // Get the fragment view.
101 View fragmentView = webViewTabFragment.getView();
103 // Remove the incorrect lint warning below that the fragment view might be null.
104 assert fragmentView != null;
106 // Get a handle for the current WebView.
107 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
110 // Use a builder to create the alert dialog.
111 AlertDialog.Builder dialogBuilder;
113 // Get a handle for the shared preferences.
114 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
116 // Get the screenshot and theme preferences.
117 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
118 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
120 // Set the style according to the theme.
122 dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogDark);
124 dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogLight);
127 // Create a drawable version of the favorite icon.
128 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), nestedScrollWebView.getFavoriteOrDefaultIcon());
131 dialogBuilder.setIcon(favoriteIconDrawable);
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);
136 // Get the SSL certificate.
137 SslCertificate sslCertificate = nestedScrollWebView.getCertificate();
139 // Check to see if the website is encrypted.
140 if (sslCertificate == null) { // The website is not encrypted.
142 dialogBuilder.setTitle(R.string.unencrypted_website);
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));
147 // Create an alert dialog from the alert dialog builder.
148 final AlertDialog alertDialog = dialogBuilder.create();
150 // Disable screenshots if not allowed.
151 if (!allowScreenshots) {
152 // Remove the warning below that `getWindow()` might be null.
153 assert alertDialog.getWindow() != null;
155 // Disable screenshots.
156 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
159 // `onCreateDialog` requires the return of an `AlertDialog`.
162 } else { // Display the SSL certificate information
164 dialogBuilder.setTitle(R.string.ssl_certificate);
166 // Set the layout. The parent view is `null` because it will be assigned by the alert dialog.
167 dialogBuilder.setView(layoutInflater.inflate(R.layout.view_ssl_certificate_dialog, null));
169 // Create an alert dialog from the builder.
170 final AlertDialog alertDialog = dialogBuilder.create();
172 // Disable screenshots if not allowed.
173 if (!allowScreenshots) {
174 // Remove the warning below that `getWindow()` might be null.
175 assert alertDialog.getWindow() != null;
177 // Disable screenshots.
178 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
181 // The alert dialog must be shown before items in the layout can be modified.
184 // Get handles for the text views.
185 TextView domainTextView = alertDialog.findViewById(R.id.domain);
186 TextView ipAddressesTextView = alertDialog.findViewById(R.id.ip_addresses);
187 TextView issuedToCNameTextView = alertDialog.findViewById(R.id.issued_to_cname);
188 TextView issuedToONameTextView = alertDialog.findViewById(R.id.issued_to_oname);
189 TextView issuedToUNameTextView = alertDialog.findViewById(R.id.issued_to_uname);
190 TextView issuedByCNameTextView = alertDialog.findViewById(R.id.issued_by_cname);
191 TextView issuedByONameTextView = alertDialog.findViewById(R.id.issued_by_oname);
192 TextView issuedByUNameTextView = alertDialog.findViewById(R.id.issued_by_uname);
193 TextView startDateTextView = alertDialog.findViewById(R.id.start_date);
194 TextView endDateTextView = alertDialog.findViewById(R.id.end_date);
197 String domainLabel = getString(R.string.domain_label) + " ";
198 String ipAddressesLabel = getString(R.string.ip_addresses) + " ";
199 String cNameLabel = getString(R.string.common_name) + " ";
200 String oNameLabel = getString(R.string.organization) + " ";
201 String uNameLabel = getString(R.string.organizational_unit) + " ";
202 String startDateLabel = getString(R.string.start_date) + " ";
203 String endDateLabel = getString(R.string.end_date) + " ";
205 // Convert the formatted URL string to a URI.
206 Uri uri = Uri.parse(nestedScrollWebView.getUrl());
208 // Extract the domain name from the URI.
209 String domainString = uri.getHost();
211 // Get the strings from the SSL certificate.
212 String issuedToCName = sslCertificate.getIssuedTo().getCName();
213 String issuedToOName = sslCertificate.getIssuedTo().getOName();
214 String issuedToUName = sslCertificate.getIssuedTo().getUName();
215 String issuedByCName = sslCertificate.getIssuedBy().getCName();
216 String issuedByOName = sslCertificate.getIssuedBy().getOName();
217 String issuedByUName = sslCertificate.getIssuedBy().getUName();
218 Date startDate = sslCertificate.getValidNotBeforeDate();
219 Date endDate = sslCertificate.getValidNotAfterDate();
221 // Create spannable string builders for each text view that needs multiple colors of text.
222 SpannableStringBuilder domainStringBuilder = new SpannableStringBuilder(domainLabel + domainString);
223 SpannableStringBuilder ipAddressesStringBuilder = new SpannableStringBuilder(ipAddressesLabel + nestedScrollWebView.getCurrentIpAddresses());
224 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
225 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
226 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
227 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
228 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
229 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
230 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
231 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
233 // Create a red foreground color span. The deprecated `getColor` must be used until the minimum API >= 23.
234 ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
236 // Create a blue foreground color span.
237 ForegroundColorSpan blueColorSpan;
239 // Set the blue color span according to the theme. The deprecated `getColor()` must be used until the minimum API >= 23.
241 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
243 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
246 // Remove the incorrect lint error that `.equals` might produce a NullPointerException.
247 assert domainString != null;
249 // Formet the domain string and issued to CName colors.
250 if (domainString.equals(issuedToCName)) { // `domainString` and `issuedToCName` match.
251 // Set the strings to be blue.
252 domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
253 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
254 } else if(issuedToCName.startsWith("*.")){ // `issuedToCName` begins with a wildcard.
255 // Remove the initial `*.`.
256 String baseCertificateDomain = issuedToCName.substring(2);
258 // Setup a copy of `domainString` to test subdomains.
259 String domainStringSubdomain = domainString;
261 // Initialize `domainNamesMatch`.
262 boolean domainNamesMatch = false;
264 // Check all the subdomains in `domainStringSubdomain` against `baseCertificateDomain`.
265 while (!domainNamesMatch && domainStringSubdomain.contains(".")) { // Stop checking if we know that `domainNamesMatch` is `true` or if we run out of `.`.
266 // Test the `domainStringSubdomain` against `baseCertificateDomain`.
267 if (domainStringSubdomain.equals(baseCertificateDomain)) {
268 domainNamesMatch = true;
271 // Strip out the lowest subdomain of `certificateCommonNameSubdomain`.
272 domainStringSubdomain = domainStringSubdomain.substring(domainStringSubdomain.indexOf(".") + 1);
275 // Format the domain and issued to Common Name according to `domainNamesMatch`.
276 if (domainNamesMatch) { // `domainString` is a subdomain of the wildcard `issuedToCNameString`.
277 // Set the strings to be blue.
278 domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
279 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
280 } else { // `domainString` is not a subdomain of the wildcard `issuedToCNameString`.
281 // Set the string to be red.
282 domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
283 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
285 } else { // The strings do not match and `issuedToCNameString` does not begin with a wildcard.
286 // Set the strings to be red.
287 domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
288 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
291 // 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.
292 ipAddressesStringBuilder.setSpan(blueColorSpan, ipAddressesLabel.length(), ipAddressesStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
293 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
294 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
295 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
296 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
297 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
299 // Get the current date.
300 Date currentDate = Calendar.getInstance().getTime();
302 // Format the start date color. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
303 if (startDate.after(currentDate)) { // The certificate start date is in the future.
304 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
305 } else { // The certificate start date is in the past.
306 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
309 // Format the end date color. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
310 if (endDate.before(currentDate)) { // The certificate end date is in the past.
311 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
312 } else { // The certificate end date is in the future.
313 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
316 // Display the strings.
317 domainTextView.setText(domainStringBuilder);
318 ipAddressesTextView.setText(ipAddressesStringBuilder);
319 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
320 issuedToONameTextView.setText(issuedToONameStringBuilder);
321 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
322 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
323 issuedByONameTextView.setText(issuedByONameStringBuilder);
324 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
325 startDateTextView.setText(startDateStringBuilder);
326 endDateTextView.setText(endDateStringBuilder);
328 // `onCreateDialog` requires the return of an alert dialog.