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.AlertDialog;
24 import android.app.Dialog;
25 import android.graphics.drawable.BitmapDrawable;
26 import android.graphics.drawable.Drawable;
27 import android.net.Uri;
28 import android.net.http.SslCertificate;
29 import android.os.Bundle;
30 import android.text.SpannableStringBuilder;
31 import android.text.Spanned;
32 import android.text.style.ForegroundColorSpan;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.TextView;
38 import androidx.annotation.NonNull;
39 import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22.
41 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
42 import com.stoutner.privacybrowser.R;
43 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
44 import com.stoutner.privacybrowser.views.NestedScrollWebView;
46 import java.text.DateFormat;
47 import java.util.Calendar;
48 import java.util.Date;
50 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
51 @SuppressLint("InflateParams")
52 public class ViewSslCertificateDialog extends DialogFragment {
53 public static ViewSslCertificateDialog displayDialog(long webViewFragmentId) {
54 // Create an arguments bundle.
55 Bundle argumentsBundle = new Bundle();
57 // Store the WebView fragment ID in the bundle.
58 argumentsBundle.putLong("webview_fragment_id", webViewFragmentId);
60 // Create a new instance of the dialog.
61 ViewSslCertificateDialog viewSslCertificateDialog = new ViewSslCertificateDialog();
63 // Add the bundle to the dialog.
64 viewSslCertificateDialog.setArguments(argumentsBundle);
66 // Return the new dialog.
67 return viewSslCertificateDialog;
71 public Dialog onCreateDialog(Bundle savedInstanceState) {
72 // Remove the incorrect lint warning below that the activity might be null.
73 assert getActivity() != null;
75 // Get the activity's layout inflater.
76 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
78 // Remove the incorrect lint warning below that `getArguments().getLong()` might be null.
79 assert getArguments() != null;
81 // Get the current position of this WebView fragment.
82 int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(getArguments().getLong("webview_fragment_id"));
84 // Get the WebView tab fragment.
85 WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition);
87 // Get the fragment view.
88 View fragmentView = webViewTabFragment.getView();
90 // Remove the incorrect lint warning below that the fragment view might be null.
91 assert fragmentView != null;
93 // Get a handle for the current WebView.
94 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
96 // Use a builder to create the alert dialog.
97 AlertDialog.Builder dialogBuilder;
99 // Set the style according to the theme.
100 if (MainWebViewActivity.darkTheme) {
101 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
103 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
106 // Create a drawable version of the favorite icon.
107 Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
110 dialogBuilder.setIcon(favoriteIconDrawable);
112 // Set a listener on the negative button. Using `null` as the listener closes the dialog without doing anything else.
113 dialogBuilder.setNegativeButton(R.string.close, null);
115 // Get the SSL certificate.
116 SslCertificate sslCertificate = nestedScrollWebView.getCertificate();
118 // Check to see if the website is encrypted.
119 if (sslCertificate == null) { // The website is not encrypted.
121 dialogBuilder.setTitle(R.string.unencrypted_website);
123 // Set the Layout. The parent view is `null` because it will be assigned by `AlertDialog`.
124 dialogBuilder.setView(layoutInflater.inflate(R.layout.unencrypted_website, null));
126 // Create an alert dialog from the alert dialog builder.
127 final AlertDialog alertDialog = dialogBuilder.create();
129 // Disable screenshots if not allowed.
130 if (!MainWebViewActivity.allowScreenshots) {
131 // Remove the warning below that `getWindow()` might be null.
132 assert alertDialog.getWindow() != null;
134 // Disable screenshots.
135 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
138 // `onCreateDialog` requires the return of an `AlertDialog`.
141 } else { // Display the SSL certificate information
143 dialogBuilder.setTitle(R.string.ssl_certificate);
145 // Set the layout. The parent view is `null` because it will be assigned by `AlertDialog`.
146 dialogBuilder.setView(layoutInflater.inflate(R.layout.view_ssl_certificate, null));
148 // Create an alert dialog from the builder.
149 final AlertDialog alertDialog = dialogBuilder.create();
151 // Disable screenshots if not allowed.
152 if (!MainWebViewActivity.allowScreenshots) {
153 // Remove the warning below that `getWindow()` might be null.
154 assert alertDialog.getWindow() != null;
156 // Disable screenshots.
157 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
160 // The alert dialog must be shown before items in the layout can be modified.
163 // Get handles for the text views.
164 TextView domainTextView = alertDialog.findViewById(R.id.domain);
165 TextView ipAddressesTextView = alertDialog.findViewById(R.id.ip_addresses);
166 TextView issuedToCNameTextView = alertDialog.findViewById(R.id.issued_to_cname);
167 TextView issuedToONameTextView = alertDialog.findViewById(R.id.issued_to_oname);
168 TextView issuedToUNameTextView = alertDialog.findViewById(R.id.issued_to_uname);
169 TextView issuedByCNameTextView = alertDialog.findViewById(R.id.issued_by_cname);
170 TextView issuedByONameTextView = alertDialog.findViewById(R.id.issued_by_oname);
171 TextView issuedByUNameTextView = alertDialog.findViewById(R.id.issued_by_uname);
172 TextView startDateTextView = alertDialog.findViewById(R.id.start_date);
173 TextView endDateTextView = alertDialog.findViewById(R.id.end_date);
176 String domainLabel = getString(R.string.domain_label) + " ";
177 String ipAddressesLabel = getString(R.string.ip_addresses) + " ";
178 String cNameLabel = getString(R.string.common_name) + " ";
179 String oNameLabel = getString(R.string.organization) + " ";
180 String uNameLabel = getString(R.string.organizational_unit) + " ";
181 String startDateLabel = getString(R.string.start_date) + " ";
182 String endDateLabel = getString(R.string.end_date) + " ";
184 // Convert the formatted URL string to a URI.
185 Uri uri = Uri.parse(MainWebViewActivity.formattedUrlString);
187 // Extract the domain name from the URI.
188 String domainString = uri.getHost();
190 // Get the strings from the SSL certificate.
191 String issuedToCName = sslCertificate.getIssuedTo().getCName();
192 String issuedToOName = sslCertificate.getIssuedTo().getOName();
193 String issuedToUName = sslCertificate.getIssuedTo().getUName();
194 String issuedByCName = sslCertificate.getIssuedBy().getCName();
195 String issuedByOName = sslCertificate.getIssuedBy().getOName();
196 String issuedByUName = sslCertificate.getIssuedBy().getUName();
197 Date startDate = sslCertificate.getValidNotBeforeDate();
198 Date endDate = sslCertificate.getValidNotAfterDate();
200 // Create spannable string builders for each text view that needs multiple colors of text.
201 SpannableStringBuilder domainStringBuilder = new SpannableStringBuilder(domainLabel + domainString);
202 SpannableStringBuilder ipAddressesStringBuilder = new SpannableStringBuilder(ipAddressesLabel + nestedScrollWebView.getCurrentIpAddresses());
203 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
204 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
205 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
206 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
207 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
208 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
209 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
210 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
212 // Create a red foreground color span. The deprecated `getColor` must be used until the minimum API >= 23.
213 @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
215 // Create a blue foreground color span.
216 ForegroundColorSpan blueColorSpan;
218 // Set the blue color span according to the theme. The deprecated `getColor()` must be used until the minimum API >= 23.
219 if (MainWebViewActivity.darkTheme) {
220 //noinspection deprecation
221 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
223 //noinspection deprecation
224 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
227 // Remove the incorrect lint error that `.equals` might produce a NullPointerException.
228 assert domainString != null;
230 // Formet the domain string and issued to CName colors.
231 if (domainString.equals(issuedToCName)) { // `domainString` and `issuedToCName` match.
232 // Set the strings to be blue.
233 domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
234 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
235 } else if(issuedToCName.startsWith("*.")){ // `issuedToCName` begins with a wildcard.
236 // Remove the initial `*.`.
237 String baseCertificateDomain = issuedToCName.substring(2);
239 // Setup a copy of `domainString` to test subdomains.
240 String domainStringSubdomain = domainString;
242 // Initialize `domainNamesMatch`.
243 boolean domainNamesMatch = false;
245 // Check all the subdomains in `domainStringSubdomain` against `baseCertificateDomain`.
246 while (!domainNamesMatch && domainStringSubdomain.contains(".")) { // Stop checking if we know that `domainNamesMatch` is `true` or if we run out of `.`.
247 // Test the `domainStringSubdomain` against `baseCertificateDomain`.
248 if (domainStringSubdomain.equals(baseCertificateDomain)) {
249 domainNamesMatch = true;
252 // Strip out the lowest subdomain of `certificateCommonNameSubdomain`.
253 domainStringSubdomain = domainStringSubdomain.substring(domainStringSubdomain.indexOf(".") + 1);
256 // Format the domain and issued to Common Name according to `domainNamesMatch`.
257 if (domainNamesMatch) { // `domainString` is a subdomain of the wildcard `issuedToCNameString`.
258 // Set the strings to be blue.
259 domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
260 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
261 } else { // `domainString` is not a subdomain of the wildcard `issuedToCNameString`.
262 // Set the string to be red.
263 domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
264 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
266 } else { // The strings do not match and `issuedToCNameString` does not begin with a wildcard.
267 // Set the strings to be red.
268 domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
269 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
272 // 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.
273 ipAddressesStringBuilder.setSpan(blueColorSpan, ipAddressesLabel.length(), ipAddressesStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
274 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
275 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
276 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
277 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
278 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
280 // Get the current date.
281 Date currentDate = Calendar.getInstance().getTime();
283 // Format the start date color. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
284 if (startDate.after(currentDate)) { // The certificate start date is in the future.
285 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
286 } else { // The certificate start date is in the past.
287 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
290 // Format the end date color. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
291 if (endDate.before(currentDate)) { // The certificate end date is in the past.
292 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
293 } else { // The certificate end date is in the future.
294 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
297 // Display the strings.
298 domainTextView.setText(domainStringBuilder);
299 ipAddressesTextView.setText(ipAddressesStringBuilder);
300 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
301 issuedToONameTextView.setText(issuedToONameStringBuilder);
302 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
303 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
304 issuedByONameTextView.setText(issuedByONameStringBuilder);
305 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
306 startDateTextView.setText(startDateStringBuilder);
307 endDateTextView.setText(endDateStringBuilder);
309 // `onCreateDialog` requires the return of an alert dialog.