]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/CheckPinnedMismatchHelper.kt
Update Guide > User Agent. https://redmine.stoutner.com/issues/896
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / CheckPinnedMismatchHelper.kt
1 /*
2  * Copyright 2018-2019,2021-2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android 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 Android 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 Android.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.helpers
21
22 import androidx.fragment.app.DialogFragment
23 import androidx.fragment.app.FragmentManager
24
25 import com.stoutner.privacybrowser.activities.MainWebViewActivity
26 import com.stoutner.privacybrowser.dataclasses.PendingDialogDataClass
27 import com.stoutner.privacybrowser.dialogs.PinnedMismatchDialog.Companion.displayDialog
28 import com.stoutner.privacybrowser.views.NestedScrollWebView
29
30 import java.lang.Exception
31
32 import java.util.Date
33
34 object CheckPinnedMismatchHelper {
35     @JvmStatic
36     fun checkPinnedMismatch(nestedScrollWebView: NestedScrollWebView, supportFragmentManager: FragmentManager, pinnedMismatchString: String) {
37         // Initialize the current SSL certificate variables.
38         var currentWebsiteIssuedToCName = ""
39         var currentWebsiteIssuedToOName = ""
40         var currentWebsiteIssuedToUName = ""
41         var currentWebsiteIssuedByCName = ""
42         var currentWebsiteIssuedByOName = ""
43         var currentWebsiteIssuedByUName = ""
44         var currentWebsiteSslStartDate: Date? = null
45         var currentWebsiteSslEndDate: Date? = null
46
47         // Initialize the pinned SSL certificate variables.
48         var pinnedSslIssuedToCName = ""
49         var pinnedSslIssuedToOName = ""
50         var pinnedSslIssuedToUName = ""
51         var pinnedSslIssuedByCName = ""
52         var pinnedSslIssuedByOName = ""
53         var pinnedSslIssuedByUName = ""
54         var pinnedSslStartDate: Date? = null
55         var pinnedSslEndDate: Date? = null
56
57         // Get the current website SSL certificate.
58         val currentWebsiteSslCertificate = nestedScrollWebView.certificate
59
60         // Extract the individual pieces of information from the current website SSL certificate if it is not null.
61         if (currentWebsiteSslCertificate != null) {
62             currentWebsiteIssuedToCName = currentWebsiteSslCertificate.issuedTo.cName
63             currentWebsiteIssuedToOName = currentWebsiteSslCertificate.issuedTo.oName
64             currentWebsiteIssuedToUName = currentWebsiteSslCertificate.issuedTo.uName
65             currentWebsiteIssuedByCName = currentWebsiteSslCertificate.issuedBy.cName
66             currentWebsiteIssuedByOName = currentWebsiteSslCertificate.issuedBy.oName
67             currentWebsiteIssuedByUName = currentWebsiteSslCertificate.issuedBy.uName
68             currentWebsiteSslStartDate = currentWebsiteSslCertificate.validNotBeforeDate
69             currentWebsiteSslEndDate = currentWebsiteSslCertificate.validNotAfterDate
70         }
71
72         // Get the pinned SSL certificate information if it exists.
73         if (nestedScrollWebView.hasPinnedSslCertificate()) {
74             // Get the pinned SSL certificate.
75             val pinnedSslCertificatePair = nestedScrollWebView.getPinnedSslCertificate()
76
77             // Extract the arrays from the array list.
78             val pinnedSslCertificateStringArray = pinnedSslCertificatePair.first
79             val pinnedSslCertificateDateArray = pinnedSslCertificatePair.second
80
81             // Populate the pinned SSL certificate string variables.
82             pinnedSslIssuedToCName = pinnedSslCertificateStringArray[0]
83             pinnedSslIssuedToOName = pinnedSslCertificateStringArray[1]
84             pinnedSslIssuedToUName = pinnedSslCertificateStringArray[2]
85             pinnedSslIssuedByCName = pinnedSslCertificateStringArray[3]
86             pinnedSslIssuedByOName = pinnedSslCertificateStringArray[4]
87             pinnedSslIssuedByUName = pinnedSslCertificateStringArray[5]
88
89             // Populate the pinned SSL certificate date variables.
90             pinnedSslStartDate = pinnedSslCertificateDateArray[0]
91             pinnedSslEndDate = pinnedSslCertificateDateArray[1]
92         }
93
94         // Initialize string variables to store the SSL certificate dates.  Strings are needed to compare the values below, which doesn't work with dates if the first one is null.
95         var currentWebsiteSslStartDateString = ""
96         var currentWebsiteSslEndDateString = ""
97         var pinnedSslStartDateString = ""
98         var pinnedSslEndDateString = ""
99
100         // Convert the dates to strings if they are not null.
101         if (currentWebsiteSslStartDate != null) {
102             currentWebsiteSslStartDateString = currentWebsiteSslStartDate.toString()
103         }
104         if (currentWebsiteSslEndDate != null) {
105             currentWebsiteSslEndDateString = currentWebsiteSslEndDate.toString()
106         }
107         if (pinnedSslStartDate != null) {
108             pinnedSslStartDateString = pinnedSslStartDate.toString()
109         }
110         if (pinnedSslEndDate != null) {
111             pinnedSslEndDateString = pinnedSslEndDate.toString()
112         }
113
114         // Check to see if the pinned information matches the current information.
115         if (((nestedScrollWebView.pinnedIpAddresses.isNotEmpty()) && (nestedScrollWebView.currentIpAddresses != nestedScrollWebView.pinnedIpAddresses)) ||
116              (nestedScrollWebView.hasPinnedSslCertificate() && ((currentWebsiteIssuedToCName != pinnedSslIssuedToCName) ||
117              (currentWebsiteIssuedToOName != pinnedSslIssuedToOName) || (currentWebsiteIssuedToUName != pinnedSslIssuedToUName) ||
118              (currentWebsiteIssuedByCName != pinnedSslIssuedByCName) || (currentWebsiteIssuedByOName != pinnedSslIssuedByOName) ||
119              (currentWebsiteIssuedByUName != pinnedSslIssuedByUName) || (currentWebsiteSslStartDateString != pinnedSslStartDateString) ||
120              (currentWebsiteSslEndDateString != pinnedSslEndDateString)))) {
121
122             // Get a handle for the pinned mismatch alert dialog.
123             val pinnedMismatchDialogFragment: DialogFragment = displayDialog(nestedScrollWebView.webViewFragmentId)
124
125             // Try to show the dialog.  Sometimes the window is not active.
126             try {
127                 // Show the pinned mismatch alert dialog.
128                 pinnedMismatchDialogFragment.show(supportFragmentManager, pinnedMismatchString)
129             } catch (exception: Exception) {
130                 // Add the dialog to the pending dialog array list.  It will be displayed in `onStart()`.
131                 MainWebViewActivity.pendingDialogsArrayList.add(PendingDialogDataClass(pinnedMismatchDialogFragment, pinnedMismatchString))
132             }
133         }
134     }
135 }