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