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