1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.dialog
;
20 import java
.io
.IOException
;
21 import java
.security
.KeyStoreException
;
22 import java
.security
.NoSuchAlgorithmException
;
23 import java
.security
.cert
.CertificateException
;
24 import java
.security
.cert
.X509Certificate
;
25 import java
.util
.Date
;
26 import java
.util
.HashMap
;
29 import javax
.security
.auth
.x500
.X500Principal
;
31 import android
.app
.Dialog
;
32 import android
.content
.Context
;
33 import android
.os
.Bundle
;
34 import android
.util
.Log
;
35 import android
.view
.View
;
36 import android
.view
.Window
;
37 import android
.widget
.Button
;
38 import android
.widget
.TextView
;
40 import com
.owncloud
.android
.Log_OC
;
41 import com
.owncloud
.android
.R
;
42 import com
.owncloud
.android
.network
.CertificateCombinedException
;
43 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
44 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
47 * Dialog to request the user about a certificate that could not be validated with the certificates store in the system.
49 * @author David A. Velasco
51 public class SslValidatorDialog
extends Dialog
{
53 private final static String TAG
= SslValidatorDialog
.class.getSimpleName();
55 private OnSslValidatorListener mListener
;
56 private CertificateCombinedException mException
= null
;
61 * Creates a new SslValidatorDialog to ask the user if an untrusted certificate from a server should
64 * @param context Android context where the dialog will live.
65 * @param result Result of a failed remote operation.
66 * @param listener Object to notice when the server certificate was added to the local certificates store.
67 * @return A new SslValidatorDialog instance. NULL if the operation can not be recovered
68 * by setting the certificate as reliable.
70 public static SslValidatorDialog
newInstance(Context context
, RemoteOperationResult result
, OnSslValidatorListener listener
) {
71 if (result
!= null
&& result
.isSslRecoverableException()) {
72 SslValidatorDialog dialog
= new SslValidatorDialog(context
, listener
);
80 * Private constructor.
82 * Instances have to be created through static {@link SslValidatorDialog#newInstance}.
84 * @param context Android context where the dialog will live
85 * @param e Exception causing the need of prompt the user about the server certificate.
86 * @param listener Object to notice when the server certificate was added to the local certificates store.
88 private SslValidatorDialog(Context context
, OnSslValidatorListener listener
) {
97 protected void onCreate(Bundle savedInstanceState
) {
98 super.onCreate(savedInstanceState
);
99 requestWindowFeature(Window
.FEATURE_NO_TITLE
);
100 mView
= getLayoutInflater().inflate(R
.layout
.ssl_validator_layout
, null
);
101 setContentView(mView
);
103 mView
.findViewById(R
.id
.ok
).setOnClickListener(
104 new View
.OnClickListener() {
106 public void onClick(View v
) {
110 if (mListener
!= null
)
111 mListener
.onSavedCertificate();
113 Log_OC
.d(TAG
, "Nobody there to notify the certificate was saved");
115 } catch (Exception e
) {
117 if (mListener
!= null
)
118 mListener
.onFailedSavingCertificate();
119 Log_OC
.e(TAG
, "Server certificate could not be saved in the known servers trust store ", e
);
124 mView
.findViewById(R
.id
.cancel
).setOnClickListener(
125 new View
.OnClickListener() {
127 public void onClick(View v
) {
132 mView
.findViewById(R
.id
.details_btn
).setOnClickListener(
133 new View
.OnClickListener() {
135 public void onClick(View v
) {
136 View detailsScroll
= findViewById(R
.id
.details_scroll
);
137 if (detailsScroll
.getVisibility() == View
.VISIBLE
) {
138 detailsScroll
.setVisibility(View
.GONE
);
139 ((Button
)v
).setText(R
.string
.ssl_validator_btn_details_see
);
142 detailsScroll
.setVisibility(View
.VISIBLE
);
143 ((Button
)v
).setText(R
.string
.ssl_validator_btn_details_hide
);
150 public void updateResult(RemoteOperationResult result
) {
151 if (result
.isSslRecoverableException()) {
152 mException
= (CertificateCombinedException
) result
.getException();
155 mView
.findViewById(R
.id
.reason_cert_not_trusted
).setVisibility(View
.GONE
);
156 mView
.findViewById(R
.id
.reason_cert_expired
).setVisibility(View
.GONE
);
157 mView
.findViewById(R
.id
.reason_cert_not_yet_valid
).setVisibility(View
.GONE
);
158 mView
.findViewById(R
.id
.reason_hostname_not_verified
).setVisibility(View
.GONE
);
159 mView
.findViewById(R
.id
.details_scroll
).setVisibility(View
.GONE
);
162 if (mException
.getCertPathValidatorException() != null
) {
163 ((TextView
)mView
.findViewById(R
.id
.reason_cert_not_trusted
)).setVisibility(View
.VISIBLE
);
166 if (mException
.getCertificateExpiredException() != null
) {
167 ((TextView
)mView
.findViewById(R
.id
.reason_cert_expired
)).setVisibility(View
.VISIBLE
);
170 if (mException
.getCertificateNotYetValidException() != null
) {
171 ((TextView
)mView
.findViewById(R
.id
.reason_cert_not_yet_valid
)).setVisibility(View
.VISIBLE
);
174 if (mException
.getSslPeerUnverifiedException() != null
) {
175 ((TextView
)mView
.findViewById(R
.id
.reason_hostname_not_verified
)).setVisibility(View
.VISIBLE
);
179 showCertificateData(mException
.getServerCertificate());
184 private void showCertificateData(X509Certificate cert
) {
187 showSubject(cert
.getSubjectX500Principal());
188 showIssuer(cert
.getIssuerX500Principal());
189 showValidity(cert
.getNotBefore(), cert
.getNotAfter());
193 // this should not happen
198 private void showSignature(X509Certificate cert
) {
199 TextView sigView
= ((TextView
)mView
.findViewById(R
.id
.value_signature
));
200 TextView algorithmView
= ((TextView
)mView
.findViewById(R
.id
.value_signature_algorithm
));
201 sigView
.setText(getHex(cert
.getSignature()));
202 algorithmView
.setText(cert
.getSigAlgName());
205 public String
getHex(final byte [] raw
) {
209 final StringBuilder hex
= new StringBuilder(2 * raw
.length
);
210 for (final byte b
: raw
) {
211 final int hiVal
= (b
& 0xF0) >> 4;
212 final int loVal
= b
& 0x0F;
213 hex
.append((char) ('0' + (hiVal
+ (hiVal
/ 10 * 7))));
214 hex
.append((char) ('0' + (loVal
+ (loVal
/ 10 * 7))));
216 return hex
.toString();
219 private void showValidity(Date notBefore
, Date notAfter
) {
220 TextView fromView
= ((TextView
)mView
.findViewById(R
.id
.value_validity_from
));
221 TextView toView
= ((TextView
)mView
.findViewById(R
.id
.value_validity_to
));
222 fromView
.setText(notBefore
.toLocaleString());
223 toView
.setText(notAfter
.toLocaleString());
226 private void showSubject(X500Principal subject
) {
227 Map
<String
, String
> s
= parsePrincipal(subject
);
228 TextView cnView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_CN
));
229 TextView oView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_O
));
230 TextView ouView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_OU
));
231 TextView cView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_C
));
232 TextView stView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_ST
));
233 TextView lView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_L
));
235 if (s
.get("CN") != null
) {
236 cnView
.setText(s
.get("CN"));
237 cnView
.setVisibility(View
.VISIBLE
);
239 cnView
.setVisibility(View
.GONE
);
241 if (s
.get("O") != null
) {
242 oView
.setText(s
.get("O"));
243 oView
.setVisibility(View
.VISIBLE
);
245 oView
.setVisibility(View
.GONE
);
247 if (s
.get("OU") != null
) {
248 ouView
.setText(s
.get("OU"));
249 ouView
.setVisibility(View
.VISIBLE
);
251 ouView
.setVisibility(View
.GONE
);
253 if (s
.get("C") != null
) {
254 cView
.setText(s
.get("C"));
255 cView
.setVisibility(View
.VISIBLE
);
257 cView
.setVisibility(View
.GONE
);
259 if (s
.get("ST") != null
) {
260 stView
.setText(s
.get("ST"));
261 stView
.setVisibility(View
.VISIBLE
);
263 stView
.setVisibility(View
.GONE
);
265 if (s
.get("L") != null
) {
266 lView
.setText(s
.get("L"));
267 lView
.setVisibility(View
.VISIBLE
);
269 lView
.setVisibility(View
.GONE
);
273 private void showIssuer(X500Principal issuer
) {
274 Map
<String
, String
> s
= parsePrincipal(issuer
);
275 TextView cnView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_CN
));
276 TextView oView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_O
));
277 TextView ouView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_OU
));
278 TextView cView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_C
));
279 TextView stView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_ST
));
280 TextView lView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_L
));
282 if (s
.get("CN") != null
) {
283 cnView
.setText(s
.get("CN"));
284 cnView
.setVisibility(View
.VISIBLE
);
286 cnView
.setVisibility(View
.GONE
);
288 if (s
.get("O") != null
) {
289 oView
.setText(s
.get("O"));
290 oView
.setVisibility(View
.VISIBLE
);
292 oView
.setVisibility(View
.GONE
);
294 if (s
.get("OU") != null
) {
295 ouView
.setText(s
.get("OU"));
296 ouView
.setVisibility(View
.VISIBLE
);
298 ouView
.setVisibility(View
.GONE
);
300 if (s
.get("C") != null
) {
301 cView
.setText(s
.get("C"));
302 cView
.setVisibility(View
.VISIBLE
);
304 cView
.setVisibility(View
.GONE
);
306 if (s
.get("ST") != null
) {
307 stView
.setText(s
.get("ST"));
308 stView
.setVisibility(View
.VISIBLE
);
310 stView
.setVisibility(View
.GONE
);
312 if (s
.get("L") != null
) {
313 lView
.setText(s
.get("L"));
314 lView
.setVisibility(View
.VISIBLE
);
316 lView
.setVisibility(View
.GONE
);
321 private Map
<String
, String
> parsePrincipal(X500Principal principal
) {
322 Map
<String
, String
> result
= new HashMap
<String
, String
>();
323 String toParse
= principal
.getName();
324 String
[] pieces
= toParse
.split(",");
325 String
[] tokens
= {"CN", "O", "OU", "C", "ST", "L"};
326 for (int i
=0; i
< pieces
.length
; i
++) {
327 for (int j
=0; j
<tokens
.length
; j
++) {
328 if (pieces
[i
].startsWith(tokens
[j
] + "=")) {
329 result
.put(tokens
[j
], pieces
[i
].substring(tokens
[j
].length()+1));
336 private void saveServerCert() throws KeyStoreException
, NoSuchAlgorithmException
, CertificateException
, IOException
{
337 if (mException
.getServerCertificate() != null
) {
338 // TODO make this asynchronously, it can take some time
339 OwnCloudClientUtils
.addCertToKnownServersStore(mException
.getServerCertificate(), getContext());
344 public interface OnSslValidatorListener
{
345 public void onSavedCertificate();
346 public void onFailedSavingCertificate();