]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/ViewSslCertificateDialog.java
73ad2ce877d861ece7b265d0aa81e84539aafd2e
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ViewSslCertificateDialog.java
1 /*
2  * Copyright © 2016-2019 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.AlertDialog;
24 import android.app.Dialog;
25 import android.app.DialogFragment;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.graphics.drawable.Drawable;
28 import android.net.Uri;
29 import android.net.http.SslCertificate;
30 import android.os.Bundle;
31 import android.text.SpannableStringBuilder;
32 import android.text.Spanned;
33 import android.text.style.ForegroundColorSpan;
34 import android.view.LayoutInflater;
35 import android.view.WindowManager;
36 import android.widget.TextView;
37
38 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39 import com.stoutner.privacybrowser.R;
40 import java.text.DateFormat;
41 import java.util.Calendar;
42 import java.util.Date;
43
44 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
45 @SuppressLint("InflateParams")
46 public class ViewSslCertificateDialog extends DialogFragment {
47     public Dialog onCreateDialog(Bundle savedInstanceState) {
48         // Get the activity's layout inflater.
49         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
50
51         // Use a builder to create the alert dialog.
52         AlertDialog.Builder dialogBuilder;
53
54         // Set the style according to the theme.
55         if (MainWebViewActivity.darkTheme) {
56             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
57         } else {
58             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
59         }
60
61         // Create a drawable version of the favorite icon.
62         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
63
64         // Set the icon.
65         dialogBuilder.setIcon(favoriteIconDrawable);
66
67         // Set a listener on the negative button.  Using `null` as the listener closes the dialog without doing anything else.
68         dialogBuilder.setNegativeButton(R.string.close, null);
69
70         // Check to see if the website is encrypted.
71         if (MainWebViewActivity.sslCertificate == null) {  // The website is not encrypted.
72             // Set the title.
73             dialogBuilder.setTitle(R.string.unencrypted_website);
74
75             // Set the Layout.  The parent view is `null` because it will be assigned by `AlertDialog`.
76             dialogBuilder.setView(layoutInflater.inflate(R.layout.unencrypted_website, null));
77
78             // Create an alert dialog from the alert dialog builder.
79             final AlertDialog alertDialog = dialogBuilder.create();
80
81             // Disable screenshots if not allowed.
82             if (!MainWebViewActivity.allowScreenshots) {
83                 // Remove the warning below that `getWindow()` might be null.
84                 assert alertDialog.getWindow() != null;
85
86                 // Disable screenshots.
87                 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
88             }
89
90             // `onCreateDialog` requires the return of an `AlertDialog`.
91             return alertDialog;
92
93         } else {  // Display the SSL certificate information
94             // Set the title.
95             dialogBuilder.setTitle(R.string.ssl_certificate);
96
97             // Set the layout.  The parent view is `null` because it will be assigned by `AlertDialog`.
98             dialogBuilder.setView(layoutInflater.inflate(R.layout.view_ssl_certificate, null));
99
100             // Create an alert dialog from the builder.
101             final AlertDialog alertDialog = dialogBuilder.create();
102
103             // Disable screenshots if not allowed.
104             if (!MainWebViewActivity.allowScreenshots) {
105                 // Remove the warning below that `getWindow()` might be null.
106                 assert alertDialog.getWindow() != null;
107
108                 // Disable screenshots.
109                 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
110             }
111
112             // The alert dialog must be shown before items in the layout can be modified.
113             alertDialog.show();
114
115             // Get handles for the text views.
116             TextView domainTextView = alertDialog.findViewById(R.id.domain);
117             TextView ipAddressesTextView = alertDialog.findViewById(R.id.ip_addresses);
118             TextView issuedToCNameTextView = alertDialog.findViewById(R.id.issued_to_cname);
119             TextView issuedToONameTextView = alertDialog.findViewById(R.id.issued_to_oname);
120             TextView issuedToUNameTextView = alertDialog.findViewById(R.id.issued_to_uname);
121             TextView issuedByCNameTextView = alertDialog.findViewById(R.id.issued_by_cname);
122             TextView issuedByONameTextView = alertDialog.findViewById(R.id.issued_by_oname);
123             TextView issuedByUNameTextView = alertDialog.findViewById(R.id.issued_by_uname);
124             TextView startDateTextView = alertDialog.findViewById(R.id.start_date);
125             TextView endDateTextView = alertDialog.findViewById(R.id.end_date);
126
127             // Setup the labels.
128             String domainLabel = getString(R.string.domain_label) + "  ";
129             String ipAddressesLabel = getString(R.string.ip_addresses) + "  ";
130             String cNameLabel = getString(R.string.common_name) + "  ";
131             String oNameLabel = getString(R.string.organization) + "  ";
132             String uNameLabel = getString(R.string.organizational_unit) + "  ";
133             String startDateLabel = getString(R.string.start_date) + "  ";
134             String endDateLabel = getString(R.string.end_date) + "  ";
135
136             // Convert the formatted URL string to a URI.
137             Uri uri = Uri.parse(MainWebViewActivity.formattedUrlString);
138
139             // Extract the domain name from the URI.
140             String domainString = uri.getHost();
141
142             // Get the SSL certificate.
143             SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
144
145             // Get the strings from the SSL certificate.
146             String issuedToCName = sslCertificate.getIssuedTo().getCName();
147             String issuedToOName = sslCertificate.getIssuedTo().getOName();
148             String issuedToUName = sslCertificate.getIssuedTo().getUName();
149             String issuedByCName = sslCertificate.getIssuedBy().getCName();
150             String issuedByOName = sslCertificate.getIssuedBy().getOName();
151             String issuedByUName = sslCertificate.getIssuedBy().getUName();
152             Date startDate = sslCertificate.getValidNotBeforeDate();
153             Date endDate = sslCertificate.getValidNotAfterDate();
154
155             // Create spannable string builders for each text view that needs multiple colors of text.
156             SpannableStringBuilder domainStringBuilder = new SpannableStringBuilder(domainLabel + domainString);
157             SpannableStringBuilder ipAddressesStringBuilder = new SpannableStringBuilder(ipAddressesLabel + MainWebViewActivity.currentHostIpAddresses);
158             SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
159             SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
160             SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
161             SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
162             SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
163             SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
164             SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
165             SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
166
167             // Create a red foreground color span.  The deprecated `getColor` must be used until the minimum API >= 23.
168             @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
169
170             // Create a blue foreground color span.
171             ForegroundColorSpan blueColorSpan;
172
173             // Set the blue color span according to the theme.  The deprecated `getColor()` must be used until the minimum API >= 23.
174             if (MainWebViewActivity.darkTheme) {
175                 //noinspection deprecation
176                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
177             } else {
178                 //noinspection deprecation
179                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
180             }
181
182             // Remove the incorrect lint error that `.equals` might produce a NullPointerException.
183             assert domainString != null;
184
185             // Formet the domain string and issued to CName colors.
186             if (domainString.equals(issuedToCName)) {  // `domainString` and `issuedToCName` match.
187                 // Set the strings to be blue.
188                 domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
189                 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
190             } else if(issuedToCName.startsWith("*.")){  // `issuedToCName` begins with a wildcard.
191                 // Remove the initial `*.`.
192                 String baseCertificateDomain = issuedToCName.substring(2);
193
194                 // Setup a copy of `domainString` to test subdomains.
195                 String domainStringSubdomain = domainString;
196
197                 // Initialize `domainNamesMatch`.
198                 boolean domainNamesMatch = false;
199
200                 // Check all the subdomains in `domainStringSubdomain` against `baseCertificateDomain`.
201                 while (!domainNamesMatch && domainStringSubdomain.contains(".")) {  // Stop checking if we know that `domainNamesMatch` is `true` or if we run out of  `.`.
202                     // Test the `domainStringSubdomain` against `baseCertificateDomain`.
203                     if (domainStringSubdomain.equals(baseCertificateDomain)) {
204                         domainNamesMatch = true;
205                     }
206
207                     // Strip out the lowest subdomain of `certificateCommonNameSubdomain`.
208                     domainStringSubdomain = domainStringSubdomain.substring(domainStringSubdomain.indexOf(".") + 1);
209                 }
210
211                 // Format the domain and issued to Common Name according to `domainNamesMatch`.
212                 if (domainNamesMatch) {  // `domainString` is a subdomain of the wildcard `issuedToCNameString`.
213                     // Set the strings to be blue.
214                     domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
215                     issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
216                 } else {  // `domainString` is not a subdomain of the wildcard `issuedToCNameString`.
217                     // Set the string to be red.
218                     domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
219                     issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
220                 }
221             } else {  // The strings do not match and `issuedToCNameString` does not begin with a wildcard.
222                 // Set the strings to be red.
223                 domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
224                 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
225             }
226
227             // 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.
228             ipAddressesStringBuilder.setSpan(blueColorSpan, ipAddressesLabel.length(), ipAddressesStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
229             issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
230             issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
231             issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
232             issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
233             issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
234
235             // Get the current date.
236             Date currentDate = Calendar.getInstance().getTime();
237
238             //  Format the start date color.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
239             if (startDate.after(currentDate)) {  // The certificate start date is in the future.
240                 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
241             } else {  // The certificate start date is in the past.
242                 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
243             }
244
245             // Format the end date color.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
246             if (endDate.before(currentDate)) {  // The certificate end date is in the past.
247                 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
248             } else {  // The certificate end date is in the future.
249                 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
250             }
251
252             // Display the strings.
253             domainTextView.setText(domainStringBuilder);
254             ipAddressesTextView.setText(ipAddressesStringBuilder);
255             issuedToCNameTextView.setText(issuedToCNameStringBuilder);
256             issuedToONameTextView.setText(issuedToONameStringBuilder);
257             issuedToUNameTextView.setText(issuedToUNameStringBuilder);
258             issuedByCNameTextView.setText(issuedByCNameStringBuilder);
259             issuedByONameTextView.setText(issuedByONameStringBuilder);
260             issuedByUNameTextView.setText(issuedByUNameStringBuilder);
261             startDateTextView.setText(startDateStringBuilder);
262             endDateTextView.setText(endDateStringBuilder);
263
264             // `onCreateDialog` requires the return of an alert dialog.
265             return alertDialog;
266         }
267     }
268 }