Updates about_licenses, adding the full text of the Apache License 2.0 and the 3...
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / SslCertificateErrorDialog.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.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 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced 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;
38
39 import com.stoutner.privacybrowser.R;
40
41 import java.text.DateFormat;
42 import java.util.Date;
43
44 public class SslCertificateErrorDialog extends AppCompatDialogFragment {
45
46     private int primaryErrorInt;
47     private String urlWithError;
48     private String issuedToCName;
49     private String issuedToOName;
50     private String issuedToUName;
51     private String issuedByCName;
52     private String issuedByOName;
53     private String issuedByUName;
54     private String startDate;
55     private String endDate;
56
57     public static SslCertificateErrorDialog displayDialog(SslError error) {
58         // Get the various components of the SSL error message.
59         int primaryErrorIntForBundle = error.getPrimaryError();
60         String urlWithErrorForBundle = error.getUrl();
61         SslCertificate sslCertificate = error.getCertificate();
62         String issuedToCNameForBundle = sslCertificate.getIssuedTo().getCName();
63         String issuedToONameForBundle = sslCertificate.getIssuedTo().getOName();
64         String issuedToUNameForBundle = sslCertificate.getIssuedTo().getUName();
65         String issuedByCNameForBundle = sslCertificate.getIssuedBy().getCName();
66         String issuedByONameForBundle = sslCertificate.getIssuedBy().getOName();
67         String issuedByUNameForBundle = sslCertificate.getIssuedBy().getUName();
68         Date startDateForBundle = sslCertificate.getValidNotBeforeDate();
69         Date endDateForBundle = sslCertificate.getValidNotAfterDate();
70
71         // Store the SSL error message components in a `Bundle`.
72         Bundle argumentsBundle = new Bundle();
73         argumentsBundle.putInt("PrimaryErrorInt", primaryErrorIntForBundle);
74         argumentsBundle.putString("UrlWithError", urlWithErrorForBundle);
75         argumentsBundle.putString("IssuedToCName", issuedToCNameForBundle);
76         argumentsBundle.putString("IssuedToOName", issuedToONameForBundle);
77         argumentsBundle.putString("IssuedToUName", issuedToUNameForBundle);
78         argumentsBundle.putString("IssuedByCName", issuedByCNameForBundle);
79         argumentsBundle.putString("IssuedByOName", issuedByONameForBundle);
80         argumentsBundle.putString("IssuedByUName", issuedByUNameForBundle);
81         argumentsBundle.putString("StartDate", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDateForBundle));
82         argumentsBundle.putString("EndDate", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDateForBundle));
83
84         // Add `argumentsBundle` to this instance of `SslCertificateErrorDialog`.
85         SslCertificateErrorDialog thisSslCertificateErrorDialog = new SslCertificateErrorDialog();
86         thisSslCertificateErrorDialog.setArguments(argumentsBundle);
87         return thisSslCertificateErrorDialog;
88     }
89
90     @Override
91     public void onCreate(Bundle savedInstanceState) {
92         super.onCreate(savedInstanceState);
93
94         // Save the components of the SSL error message in class variables.
95         primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
96         urlWithError = getArguments().getString("UrlWithError");
97         issuedToCName = getArguments().getString("IssuedToCName");
98         issuedToOName = getArguments().getString("IssuedToOName");
99         issuedToUName = getArguments().getString("IssuedToUName");
100         issuedByCName = getArguments().getString("IssuedByCName");
101         issuedByOName = getArguments().getString("IssuedByOName");
102         issuedByUName = getArguments().getString("IssuedByUName");
103         startDate = getArguments().getString("StartDate");
104         endDate = getArguments().getString("EndDate");
105     }
106
107     // The public interface is used to send information back to the parent activity.
108     public interface SslCertificateErrorListener {
109         void onSslErrorCancel();
110
111         void onSslErrorProceed();
112     }
113
114     // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
115     private SslCertificateErrorListener sslCertificateErrorListener;
116
117     // Check to make sure that the parent activity implements the listener.
118     public void onAttach(Context context) {
119         super.onAttach(context);
120
121         try {
122             sslCertificateErrorListener = (SslCertificateErrorListener) context;
123         } catch(ClassCastException exception) {
124             throw new ClassCastException(context.toString() + " must implement SslCertificateErrorListener");
125         }
126     }
127
128     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
129     @SuppressLint("InflateParams")
130     @SuppressWarnings("deprecation")
131     @Override
132     @NonNull
133     public Dialog onCreateDialog(Bundle savedInstanceState) {
134         // Get the activity's layout inflater.
135         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
136
137         // Use `AlertDialog.Builder` to create the `AlertDialog`.  `R.style.LightAlertDialog` formats the color of the button text.
138         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
139         dialogBuilder.setTitle(R.string.ssl_certificate_error);
140         // The parent view is `null` because it will be assigned by `AlertDialog`.
141         dialogBuilder.setView(layoutInflater.inflate(R.layout.ssl_certificate_error, null));
142
143         // Set an `onClick` listener on the negative button.  `null` doesn't do anything extra when the button is pressed.  The `Dialog` will automatically close.
144         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
145             @Override
146             public void onClick(DialogInterface dialog, int which) {
147                 sslCertificateErrorListener.onSslErrorCancel();
148             }
149         });
150
151         // Set an `onClick` listener on the positive button.
152         dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
153             @Override
154             public void onClick(DialogInterface dialog, int which) {
155                 sslCertificateErrorListener.onSslErrorProceed();
156             }
157         });
158
159
160         // Create an `AlertDialog` from the `AlertDialog.Builder`.
161         AlertDialog alertDialog = dialogBuilder.create();
162
163         // We have to show the `AlertDialog` before we can modify the content.
164         alertDialog.show();
165
166         // Get handles for the `TextViews`
167         TextView primaryErrorTextView = (TextView) alertDialog.findViewById(R.id.primary_error);
168         TextView urlTextView = (TextView) alertDialog.findViewById(R.id.url_error_dialog);
169         TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
170         TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
171         TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
172         TextView issuedByTextView = (TextView) alertDialog.findViewById(R.id.issued_by_textview);
173         TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
174         TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
175         TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
176         TextView validDatesTextView = (TextView) alertDialog.findViewById(R.id.valid_dates_textview);
177         TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date_error_dialog);
178         TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date_error_dialog);
179
180         // Setup the common strings.
181         String urlLabel = getString(R.string.url_label) + "  ";
182         String cNameLabel = getString(R.string.common_name) + "  ";
183         String oNameLabel = getString(R.string.organization) + "  ";
184         String uNameLabel = getString(R.string.organizational_unit) + "  ";
185         String startDateLabel = getString(R.string.start_date) + "  ";
186         String endDateLabel = getString(R.string.end_date) + "  ";
187
188         // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
189         SpannableStringBuilder urlStringBuilder = new SpannableStringBuilder(urlLabel + urlWithError);
190         SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
191         SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
192         SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
193         SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
194         SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
195         SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
196         SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
197         SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
198
199         // Create the `ForegroundColorSpan`.  We have to use the deprecated `getColor` until API >= 23.
200         ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
201         ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
202
203         // Setup the spans to display the certificate information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
204         urlStringBuilder.setSpan(blueColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
205         issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
206         issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
207         issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
208         issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
209         issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
210         issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
211         startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
212         endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
213
214         // Initialize `primaryErrorString`.
215         String primaryErrorString = "";
216
217         // Highlight the primary error in red and store the primary error string in `primaryErrorString`.
218         switch (primaryErrorInt) {
219             case SslError.SSL_IDMISMATCH:
220                 // Change the URL span colors to red.
221                 urlStringBuilder.setSpan(redColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
222                 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
223
224                 // Store the primary error string.
225                 primaryErrorString = getString(R.string.cn_mismatch);
226                 break;
227
228             case SslError.SSL_UNTRUSTED:
229                 // Change the `issuesByTextView` text to red.  We have to use the deprecated `getColor()` until API >= 23.
230                 issuedByTextView.setTextColor(getResources().getColor(R.color.red_a700));
231
232                 // Change the issued by span color to red.
233                 issuedByCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
234                 issuedByONameStringBuilder.setSpan(redColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
235                 issuedByUNameStringBuilder.setSpan(redColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
236
237                 // Store the primary error string.
238                 primaryErrorString = getString(R.string.untrusted);
239                 break;
240
241             case SslError.SSL_DATE_INVALID:
242                 // Change the `validDatesTextView` text to red.  We have to use the deprecated `getColor()` until API >= 23.
243                 validDatesTextView.setTextColor(getResources().getColor(R.color.red_a700));
244
245                 // Change the date span colors to red.
246                 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
247                 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
248
249                 // Store the primary error string.
250                 primaryErrorString = getString(R.string.invalid_date);
251                 break;
252
253             case SslError.SSL_NOTYETVALID:
254                 // Change the start date span color to red.
255                 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
256
257                 // Store the primary error string.
258                 primaryErrorString = getString(R.string.future_certificate);
259                 break;
260
261             case SslError.SSL_EXPIRED:
262                 // Change the end date span color to red.
263                 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
264
265                 // Store the primary error string.
266                 primaryErrorString = getString(R.string.expired_certificate);
267                 break;
268
269             case SslError.SSL_INVALID:
270                 // Store the primary error string.
271                 primaryErrorString = getString(R.string.invalid_certificate);
272                 break;
273         }
274
275
276         // Display the strings.
277         primaryErrorTextView.setText(primaryErrorString);
278         urlTextView.setText(urlStringBuilder);
279         issuedToCNameTextView.setText(issuedToCNameStringBuilder);
280         issuedToONameTextView.setText(issuedToONameStringBuilder);
281         issuedToUNameTextView.setText(issuedToUNameStringBuilder);
282         issuedByCNameTextView.setText(issuedByCNameStringBuilder);
283         issuedByONameTextView.setText(issuedByONameStringBuilder);
284         issuedByUNameTextView.setText(issuedByUNameStringBuilder);
285         startDateTextView.setText(startDateStringBuilder);
286         endDateTextView.setText(endDateStringBuilder);
287
288         // `onCreateDialog` requires the return of an `AlertDialog`.
289         return alertDialog;
290     }
291 }