Slight update to the Privacy Policy wording.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / SslCertificateError.java
1 /**
2  * Copyright 2016 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;
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 import android.support.v7.app.AppCompatDialogFragment;
32 import android.text.SpannableStringBuilder;
33 import android.text.Spanned;
34 import android.text.style.ForegroundColorSpan;
35 import android.view.LayoutInflater;
36 import android.widget.TextView;
37
38 import java.util.Date;
39
40 public class SslCertificateError extends AppCompatDialogFragment {
41
42     private String primaryError;
43     private String urlWithError;
44     private String issuedToCName;
45     private String issuedToOName;
46     private String issuedToUName;
47     private String issuedByCName;
48     private String issuedByOName;
49     private String issuedByUName;
50     private String startDate;
51     private String endDate;
52
53     public static SslCertificateError displayDialog(SslError error) {
54         // Get the various components of the SSL error message.
55         int primaryErrorIntForBundle = error.getPrimaryError();
56         String urlWithErrorForBundle = error.getUrl();
57         SslCertificate sslCertificate = error.getCertificate();
58         String issuedToCNameForBundle = sslCertificate.getIssuedTo().getCName();
59         String issuedToONameForBundle = sslCertificate.getIssuedTo().getOName();
60         String issuedToUNameForBundle = sslCertificate.getIssuedTo().getUName();
61         String issuedByCNameForBundle = sslCertificate.getIssuedBy().getCName();
62         String issuedByONameForBundle = sslCertificate.getIssuedBy().getOName();
63         String issuedByUNameForBundle = sslCertificate.getIssuedBy().getUName();
64         Date startDateForBundle = sslCertificate.getValidNotBeforeDate();
65         Date endDateForBundle = sslCertificate.getValidNotAfterDate();
66
67         // Store the SSL error message components in a `Bundle`.
68         Bundle argumentsBundle = new Bundle();
69         argumentsBundle.putInt("PrimaryErrorInt", primaryErrorIntForBundle);
70         argumentsBundle.putString("UrlWithError", urlWithErrorForBundle);
71         argumentsBundle.putString("IssuedToCName", issuedToCNameForBundle);
72         argumentsBundle.putString("IssuedToOName", issuedToONameForBundle);
73         argumentsBundle.putString("IssuedToUName", issuedToUNameForBundle);
74         argumentsBundle.putString("IssuedByCName", issuedByCNameForBundle);
75         argumentsBundle.putString("IssuedByOName", issuedByONameForBundle);
76         argumentsBundle.putString("IssuedByUName", issuedByUNameForBundle);
77         argumentsBundle.putString("StartDate", startDateForBundle.toString());
78         argumentsBundle.putString("EndDate", endDateForBundle.toString());
79
80         // Add `argumentsBundle` to this instance of `SslCertificateError`.
81         SslCertificateError thisSslCertificateErrorDialog = new SslCertificateError();
82         thisSslCertificateErrorDialog.setArguments(argumentsBundle);
83         return thisSslCertificateErrorDialog;
84     }
85
86     @Override
87     public void onCreate(Bundle savedInstanceState) {
88         super.onCreate(savedInstanceState);
89
90         // Save the components of the SSL error message in class variables.
91         urlWithError = getArguments().getString("UrlWithError");
92         issuedToCName = getArguments().getString("IssuedToCName");
93         issuedToOName = getArguments().getString("IssuedToOName");
94         issuedToUName = getArguments().getString("IssuedToUName");
95         issuedByCName = getArguments().getString("IssuedByCName");
96         issuedByOName = getArguments().getString("IssuedByOName");
97         issuedByUName = getArguments().getString("IssuedByUName");
98         startDate = getArguments().getString("StartDate");
99         endDate = getArguments().getString("EndDate");
100
101         // Get the appropriate string for `primaryError.
102         int primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
103         switch (primaryErrorInt) {
104             case SslError.SSL_NOTYETVALID:
105                 primaryError = getString(R.string.future_certificate);
106                 break;
107
108             case SslError.SSL_EXPIRED:
109                 primaryError = getString(R.string.expired_certificate);
110                 break;
111
112             case SslError.SSL_IDMISMATCH:
113                 primaryError = getString(R.string.cn_mismatch);
114                 break;
115
116             case SslError.SSL_UNTRUSTED:
117                 primaryError = getString(R.string.untrusted);
118                 break;
119
120             case SslError.SSL_DATE_INVALID:
121                 primaryError = getString(R.string.invalid_date);
122                 break;
123
124             case SslError.SSL_INVALID:
125                 primaryError = getString(R.string.invalid_certificate);
126                 break;
127         }
128     }
129
130     // The public interface is used to send information back to the parent activity.
131     public interface SslCertificateErrorListener {
132         void onSslErrorCancel();
133
134         void onSslErrorProceed();
135     }
136
137     // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
138     private SslCertificateErrorListener sslCertificateErrorListener;
139
140     // Check to make sure that the parent activity implements the listener.
141     public void onAttach(Context context) {
142         super.onAttach(context);
143
144         try {
145             sslCertificateErrorListener = (SslCertificateErrorListener) context;
146         } catch(ClassCastException exception) {
147             throw new ClassCastException(context.toString() + " must implement SslCertificateErrorListener");
148         }
149     }
150
151     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
152     @SuppressLint("InflateParams")
153     @Override
154     @NonNull
155     public Dialog onCreateDialog(Bundle savedInstanceState) {
156         // Get the activity's layout inflater.
157         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
158
159         // Use `AlertDialog.Builder` to create the `AlertDialog`.  `R.style.LightAlertDialog` formats the color of the button text.
160         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
161         dialogBuilder.setTitle(R.string.ssl_certificate_error);
162         // The parent view is `null` because it will be assigned by `AlertDialog`.
163         dialogBuilder.setView(layoutInflater.inflate(R.layout.ssl_certificate_error, null));
164
165         // Set an `onClick` listener on the negative button.  `null` doesn't do anything extra when the button is pressed.  The `Dialog` will automatically close.
166         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
167             @Override
168             public void onClick(DialogInterface dialog, int which) {
169                 sslCertificateErrorListener.onSslErrorCancel();
170             }
171         });
172
173         // Set an `onClick` listener on the positive button.
174         dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
175             @Override
176             public void onClick(DialogInterface dialog, int which) {
177                 sslCertificateErrorListener.onSslErrorProceed();
178             }
179         });
180
181
182         // Create an `AlertDialog` from the `AlertDialog.Builder`.
183         AlertDialog alertDialog = dialogBuilder.create();
184
185         // We have to show the `AlertDialog` before we can modify the content.
186         alertDialog.show();
187
188         // Get handles for the `TextViews`
189         TextView primaryErrorTextView = (TextView) alertDialog.findViewById(R.id.primary_error);
190         TextView urlTextView = (TextView) alertDialog.findViewById(R.id.url_error_dialog);
191         TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
192         TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
193         TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
194         TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
195         TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
196         TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
197         TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date_error_dialog);
198         TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date_error_dialog);
199
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) + "  ";
207
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));
218
219         // Create a blue `ForegroundColorSpan`.  We have to use the deprecated `getColor` until API >= 23.
220         @SuppressWarnings("deprecation") ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
221
222         // Setup the spans to display the certificate information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
223         urlStringBuilder.setSpan(blueColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
224         issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
225         issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
226         issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
227         issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
228         issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
229         issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
230         startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
231         endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
232
233
234         // Display the strings.
235         primaryErrorTextView.setText(primaryError);
236         urlTextView.setText(urlStringBuilder);
237         issuedToCNameTextView.setText(issuedToCNameStringBuilder);
238         issuedToONameTextView.setText(issuedToONameStringBuilder);
239         issuedToUNameTextView.setText(issuedToUNameStringBuilder);
240         issuedByCNameTextView.setText(issuedByCNameStringBuilder);
241         issuedByONameTextView.setText(issuedByONameStringBuilder);
242         issuedByUNameTextView.setText(issuedByUNameStringBuilder);
243         startDateTextView.setText(startDateStringBuilder);
244         endDateTextView.setText(endDateStringBuilder);
245
246         // `onCreateDialog` requires the return of an `AlertDialog`.
247         return alertDialog;
248     }
249 }