2 * Copyright © 2016-2017 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.content.Context;
26 import android.content.DialogInterface;
27 import android.net.http.SslCertificate;
28 import android.net.http.SslError;
29 import android.os.Bundle;
30 import android.support.annotation.NonNull;
31 // `AppCompatDialogFragment` is used instead of `DialogFragment` to avoid an error on API <=22.
32 import android.support.v7.app.AppCompatDialogFragment;
33 import android.text.SpannableStringBuilder;
34 import android.text.Spanned;
35 import android.text.style.ForegroundColorSpan;
36 import android.view.LayoutInflater;
37 import android.widget.TextView;
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
42 import java.text.DateFormat;
43 import java.util.Date;
45 public class SslCertificateErrorDialog extends AppCompatDialogFragment {
47 // The private variables are used in `onCreate()` and `onCreateDialog()`.
48 private int primaryErrorInt;
49 private String urlWithError;
50 private String issuedToCName;
51 private String issuedToOName;
52 private String issuedToUName;
53 private String issuedByCName;
54 private String issuedByOName;
55 private String issuedByUName;
56 private String startDate;
57 private String endDate;
59 public static SslCertificateErrorDialog displayDialog(SslError error) {
60 // Get the various components of the SSL error message.
61 int primaryErrorIntForBundle = error.getPrimaryError();
62 String urlWithErrorForBundle = error.getUrl();
63 SslCertificate sslCertificate = error.getCertificate();
64 String issuedToCNameForBundle = sslCertificate.getIssuedTo().getCName();
65 String issuedToONameForBundle = sslCertificate.getIssuedTo().getOName();
66 String issuedToUNameForBundle = sslCertificate.getIssuedTo().getUName();
67 String issuedByCNameForBundle = sslCertificate.getIssuedBy().getCName();
68 String issuedByONameForBundle = sslCertificate.getIssuedBy().getOName();
69 String issuedByUNameForBundle = sslCertificate.getIssuedBy().getUName();
70 Date startDateForBundle = sslCertificate.getValidNotBeforeDate();
71 Date endDateForBundle = sslCertificate.getValidNotAfterDate();
73 // Store the SSL error message components in a `Bundle`.
74 Bundle argumentsBundle = new Bundle();
75 argumentsBundle.putInt("PrimaryErrorInt", primaryErrorIntForBundle);
76 argumentsBundle.putString("UrlWithError", urlWithErrorForBundle);
77 argumentsBundle.putString("IssuedToCName", issuedToCNameForBundle);
78 argumentsBundle.putString("IssuedToOName", issuedToONameForBundle);
79 argumentsBundle.putString("IssuedToUName", issuedToUNameForBundle);
80 argumentsBundle.putString("IssuedByCName", issuedByCNameForBundle);
81 argumentsBundle.putString("IssuedByOName", issuedByONameForBundle);
82 argumentsBundle.putString("IssuedByUName", issuedByUNameForBundle);
83 argumentsBundle.putString("StartDate", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDateForBundle));
84 argumentsBundle.putString("EndDate", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDateForBundle));
86 // Add `argumentsBundle` to this instance of `SslCertificateErrorDialog`.
87 SslCertificateErrorDialog thisSslCertificateErrorDialog = new SslCertificateErrorDialog();
88 thisSslCertificateErrorDialog.setArguments(argumentsBundle);
89 return thisSslCertificateErrorDialog;
93 public void onCreate(Bundle savedInstanceState) {
94 super.onCreate(savedInstanceState);
96 // Save the components of the SSL error message in class variables.
97 primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
98 urlWithError = getArguments().getString("UrlWithError");
99 issuedToCName = getArguments().getString("IssuedToCName");
100 issuedToOName = getArguments().getString("IssuedToOName");
101 issuedToUName = getArguments().getString("IssuedToUName");
102 issuedByCName = getArguments().getString("IssuedByCName");
103 issuedByOName = getArguments().getString("IssuedByOName");
104 issuedByUName = getArguments().getString("IssuedByUName");
105 startDate = getArguments().getString("StartDate");
106 endDate = getArguments().getString("EndDate");
109 // The public interface is used to send information back to the parent activity.
110 public interface SslCertificateErrorListener {
111 void onSslErrorCancel();
113 void onSslErrorProceed();
116 // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
117 private SslCertificateErrorListener sslCertificateErrorListener;
119 // Check to make sure that the parent activity implements the listener.
120 public void onAttach(Context context) {
121 super.onAttach(context);
124 sslCertificateErrorListener = (SslCertificateErrorListener) context;
125 } catch(ClassCastException exception) {
126 throw new ClassCastException(context.toString() + " must implement SslCertificateErrorListener");
130 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
131 @SuppressLint("InflateParams")
132 @SuppressWarnings("deprecation")
135 public Dialog onCreateDialog(Bundle savedInstanceState) {
136 // Get the activity's layout inflater.
137 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
139 // Use `AlertDialog.Builder` to create the `AlertDialog`.
140 AlertDialog.Builder dialogBuilder;
142 // Set the style and icon according to the theme.
143 if (MainWebViewActivity.darkTheme) {
145 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
148 dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_dark);
151 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
154 dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_light);
158 dialogBuilder.setTitle(R.string.ssl_certificate_error);
160 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
161 dialogBuilder.setView(layoutInflater.inflate(R.layout.ssl_certificate_error, null));
163 // Set an `onClick` listener on the negative button. `null` doesn't do anything extra when the button is pressed. The `Dialog` will automatically close.
164 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
166 public void onClick(DialogInterface dialog, int which) {
167 sslCertificateErrorListener.onSslErrorCancel();
171 // Set an `onClick` listener on the positive button.
172 dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
174 public void onClick(DialogInterface dialog, int which) {
175 sslCertificateErrorListener.onSslErrorProceed();
180 // Create an `AlertDialog` from the `AlertDialog.Builder`.
181 AlertDialog alertDialog = dialogBuilder.create();
183 // We have to show the `AlertDialog` before we can modify the content.
186 // Get handles for the `TextViews`
187 TextView primaryErrorTextView = (TextView) alertDialog.findViewById(R.id.primary_error);
188 TextView urlTextView = (TextView) alertDialog.findViewById(R.id.url_error_dialog);
189 TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
190 TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
191 TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
192 TextView issuedByTextView = (TextView) alertDialog.findViewById(R.id.issued_by_textview);
193 TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
194 TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
195 TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
196 TextView validDatesTextView = (TextView) alertDialog.findViewById(R.id.valid_dates_textview);
197 TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date_error_dialog);
198 TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date_error_dialog);
200 // Setup the common strings.
201 String urlLabel = getString(R.string.url_label) + " ";
202 String cNameLabel = getString(R.string.common_name) + " ";
203 String oNameLabel = getString(R.string.organization) + " ";
204 String uNameLabel = getString(R.string.organizational_unit) + " ";
205 String startDateLabel = getString(R.string.start_date) + " ";
206 String endDateLabel = getString(R.string.end_date) + " ";
208 // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
209 SpannableStringBuilder urlStringBuilder = new SpannableStringBuilder(urlLabel + urlWithError);
210 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
211 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
212 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
213 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
214 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
215 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
216 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
217 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
219 // Create a red `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
220 @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
222 // Create a blue `ForegroundColorSpan`.
223 ForegroundColorSpan blueColorSpan;
225 // Set `blueColorSpan` according to the theme. We have to use the deprecated `getColor()` until API >= 23.
226 if (MainWebViewActivity.darkTheme) {
227 //noinspection deprecation
228 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
230 //noinspection deprecation
231 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
234 // Setup the spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
235 urlStringBuilder.setSpan(blueColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
236 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
237 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
238 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
239 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
240 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
241 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
242 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
243 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
245 // Initialize `primaryErrorString`.
246 String primaryErrorString = "";
248 // Highlight the primary error in red and store the primary error string in `primaryErrorString`.
249 switch (primaryErrorInt) {
250 case SslError.SSL_IDMISMATCH:
251 // Change the URL span colors to red.
252 urlStringBuilder.setSpan(redColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
253 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
255 // Store the primary error string.
256 primaryErrorString = getString(R.string.cn_mismatch);
259 case SslError.SSL_UNTRUSTED:
260 // Change the `issuesByTextView` text to red. We have to use the deprecated `getColor()` until API >= 23.
261 issuedByTextView.setTextColor(getResources().getColor(R.color.red_a700));
263 // Change the issued by span color to red.
264 issuedByCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
265 issuedByONameStringBuilder.setSpan(redColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
266 issuedByUNameStringBuilder.setSpan(redColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
268 // Store the primary error string.
269 primaryErrorString = getString(R.string.untrusted);
272 case SslError.SSL_DATE_INVALID:
273 // Change the `validDatesTextView` text to red. We have to use the deprecated `getColor()` until API >= 23.
274 validDatesTextView.setTextColor(getResources().getColor(R.color.red_a700));
276 // Change the date span colors to red.
277 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
278 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
280 // Store the primary error string.
281 primaryErrorString = getString(R.string.invalid_date);
284 case SslError.SSL_NOTYETVALID:
285 // Change the start date span color to red.
286 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
288 // Store the primary error string.
289 primaryErrorString = getString(R.string.future_certificate);
292 case SslError.SSL_EXPIRED:
293 // Change the end date span color to red.
294 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
296 // Store the primary error string.
297 primaryErrorString = getString(R.string.expired_certificate);
300 case SslError.SSL_INVALID:
301 // Store the primary error string.
302 primaryErrorString = getString(R.string.invalid_certificate);
307 // Display the strings.
308 primaryErrorTextView.setText(primaryErrorString);
309 urlTextView.setText(urlStringBuilder);
310 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
311 issuedToONameTextView.setText(issuedToONameStringBuilder);
312 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
313 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
314 issuedByONameTextView.setText(issuedByONameStringBuilder);
315 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
316 startDateTextView.setText(startDateStringBuilder);
317 endDateTextView.setText(endDateStringBuilder);
319 // `onCreateDialog` requires the return of an `AlertDialog`.