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