]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/ViewSslCertificateDialog.java
Color code the issued to Common Name in the view SSL certificate dialog. Implements...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ViewSslCertificateDialog.java
1 /*
2  * Copyright © 2016-2017 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.widget.TextView;
36
37 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
38 import com.stoutner.privacybrowser.R;
39
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         // Create a drawable version of the favorite icon.
52         Drawable favoriteIconDrawable = new BitmapDrawable(getResources(), MainWebViewActivity.favoriteIconBitmap);
53
54         // Use `AlertDialog.Builder` to create the `AlertDialog`.
55         AlertDialog.Builder dialogBuilder;
56
57         // Set the style according to the theme.
58         if (MainWebViewActivity.darkTheme) {
59             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
60         } else {
61             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
62         }
63
64         // Set the icon.
65         dialogBuilder.setIcon(favoriteIconDrawable);
66
67         // Set an `onClick` listener on the negative button.  Using `null` 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 `AlertDialog` from the `AlertDialog.Builder`
79             final AlertDialog alertDialog = dialogBuilder.create();
80
81             // Show `alertDialog`.
82             alertDialog.show();
83
84             // `onCreateDialog` requires the return of an `AlertDialog`.
85             return alertDialog;
86
87         } else {  // Display the SSL certificate information
88             // Set the title.
89             dialogBuilder.setTitle(R.string.ssl_certificate);
90
91             // Set the layout.  The parent view is `null` because it will be assigned by `AlertDialog`.
92             dialogBuilder.setView(layoutInflater.inflate(R.layout.view_ssl_certificate, null));
93
94             // Create an `AlertDialog` from the `AlertDialog.Builder`
95             final AlertDialog alertDialog = dialogBuilder.create();
96
97             // The `AlertDialog` must be shown before items in the layout can be modified.
98             alertDialog.show();
99
100             // Get handles for the `TextViews`.
101             TextView domainTextView = (TextView) alertDialog.findViewById(R.id.domain);
102             TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname);
103             TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname);
104             TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname);
105             TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname);
106             TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname);
107             TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname);
108             TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date);
109             TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date);
110
111             // Setup the labels.
112             String domainLabel = getString(R.string.domain_label) + "  ";
113             String cNameLabel = getString(R.string.common_name) + "  ";
114             String oNameLabel = getString(R.string.organization) + "  ";
115             String uNameLabel = getString(R.string.organizational_unit) + "  ";
116             String startDateLabel = getString(R.string.start_date) + "  ";
117             String endDateLabel = getString(R.string.end_date) + "  ";
118
119             // Parse `formattedUrlString` to a `URI`.
120             Uri uri = Uri.parse(MainWebViewActivity.formattedUrlString);
121
122             // Extract the domain name from `uri`.
123             String domainString = uri.getHost();
124
125             // Get the SSL certificate.
126             SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
127
128             // Get the strings from the SSL certificate.
129             String issuedToCNameString = sslCertificate.getIssuedTo().getCName();
130             String issuedToONameString = sslCertificate.getIssuedTo().getOName();
131             String issuedToUNameString = sslCertificate.getIssuedTo().getUName();
132             String issuedByCNameString = sslCertificate.getIssuedBy().getCName();
133             String issuedByONameString = sslCertificate.getIssuedBy().getOName();
134             String issuedByUNameString = sslCertificate.getIssuedBy().getUName();
135             Date startDate = sslCertificate.getValidNotBeforeDate();
136             Date endDate = sslCertificate.getValidNotAfterDate();
137
138             // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
139             SpannableStringBuilder domainStringBuilder = new SpannableStringBuilder(domainLabel + domainString);
140             SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCNameString);
141             SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToONameString);
142             SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUNameString);
143             SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCNameString);
144             SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByONameString);
145             SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUNameString);
146             SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDate));
147             SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDate));
148
149             // Create a red `ForegroundColorSpan`.  We have to use the deprecated `getColor` until API >= 23.
150             @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
151
152             // Create a blue `ForegroundColorSpan`.
153             ForegroundColorSpan blueColorSpan;
154
155             // Set `blueColorSpan` according to the theme.  We have to use the deprecated `getColor()` until API >= 23.
156             if (MainWebViewActivity.darkTheme) {
157                 //noinspection deprecation
158                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
159             } else {
160                 //noinspection deprecation
161                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
162             }
163
164             // Formet the `domainString` and `issuedToCName` colors.
165             if (domainString.equals(issuedToCNameString)) {  // `domainString` and `issuedToCNameString` match.
166                 // Set the strings to be blue.
167                 domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
168                 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
169             } else if(issuedToCNameString.startsWith("*.")){  // `issuedToCNameString` begins with a wildcard.
170                 // Remove the initial `*.`.
171                 String baseCertificateDomain = issuedToCNameString.substring(2);
172
173                 // Setup a copy of `domainString` to test subdomains.
174                 String domainStringSubdomain = domainString;
175
176                 // Initialize `domainNamesMatch`.
177                 boolean domainNamesMatch = false;
178
179                 // Check all the subdomains in `domainStringSubdomain` against `baseCertificateDomain`.
180                 while (!domainNamesMatch && domainStringSubdomain.contains(".")) {  // Stop checking if we know that `domainNamesMatch` is `true` or if we run out of  `.`.
181                     // Test the `domainStringSubdomain` against `baseCertificateDomain`.
182                     if (domainStringSubdomain.equals(baseCertificateDomain)) {
183                         domainNamesMatch = true;
184                     }
185
186                     // Strip out the lowest subdomain of `certificateCommonNameSubdomain`.
187                     domainStringSubdomain = domainStringSubdomain.substring(domainStringSubdomain.indexOf(".") + 1);
188                 }
189
190                 // Format the domain and issued to Common Name according to `domainNamesMatch`.
191                 if (domainNamesMatch) {  // `domainString` is a subdomain of the wildcard `issuedToCNameString`.
192                     // Set the strings to be blue.
193                     domainStringBuilder.setSpan(blueColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
194                     issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
195                 } else {  // `domainString` is not a subdomain of the wildcard `issuedToCNameString`.
196                     // Set the string to be red.
197                     domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
198                     issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
199                 }
200             } else {  // The strings do not match and `issuedToCNameString` does not begin with a wildcard.
201                 // Set the strings to be red.
202                 domainStringBuilder.setSpan(redColorSpan, domainLabel.length(), domainStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
203                 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
204             }
205
206             // 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.
207             issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
208             issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
209             issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
210             issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
211             issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
212
213             Date currentDate = Calendar.getInstance().getTime();
214
215             //  Format the start date color.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
216             if (startDate.after(currentDate)) {  // The certificate start date is in the future.
217                 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
218             } else {  // The certificate start date is in the past.
219                 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
220             }
221
222             // Format the end date color.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
223             if (endDate.before(currentDate)) {  // The certificate end date is in the past.
224                 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
225             } else {  // The certificate end date is in the future.
226                 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
227             }
228
229             // Display the strings.
230             domainTextView.setText(domainStringBuilder);
231             issuedToCNameTextView.setText(issuedToCNameStringBuilder);
232             issuedToONameTextView.setText(issuedToONameStringBuilder);
233             issuedToUNameTextView.setText(issuedToUNameStringBuilder);
234             issuedByCNameTextView.setText(issuedByCNameStringBuilder);
235             issuedByONameTextView.setText(issuedByONameStringBuilder);
236             issuedByUNameTextView.setText(issuedByUNameStringBuilder);
237             startDateTextView.setText(startDateStringBuilder);
238             endDateTextView.setText(endDateStringBuilder);
239
240             // `onCreateDialog` requires the return of an `AlertDialog`.
241             return alertDialog;
242         }
243     }
244 }