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