1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 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/>.
17 package com
.owncloud
.android
.ui
.dialog
;
19 import java
.io
.IOException
;
20 import java
.security
.GeneralSecurityException
;
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 com
.actionbarsherlock
.app
.SherlockActivity
;
32 import com
.owncloud
.android
.R
;
33 import com
.owncloud
.android
.lib
.common
.network
.CertificateCombinedException
;
34 import com
.owncloud
.android
.lib
.common
.network
.NetworkUtils
;
35 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
36 import com
.owncloud
.android
.utils
.Log_OC
;
38 import android
.app
.Activity
;
39 import android
.app
.Dialog
;
40 import android
.net
.http
.SslError
;
41 import android
.os
.Bundle
;
42 import android
.view
.LayoutInflater
;
43 import android
.view
.View
;
44 import android
.view
.View
.OnClickListener
;
45 import android
.view
.ViewGroup
;
46 import android
.view
.Window
;
47 import android
.widget
.Button
;
48 import android
.widget
.TextView
;
51 * Dialog to show an Untrusted Certificate
54 * @author David A. Velasco
57 public class SslUntrustedCertDialog
extends SslUntrustedCertDialogABSTRACT
{
59 private final static String TAG
= SslUntrustedCertDialog
.class.getSimpleName();
61 private X509Certificate mCertificate
;
63 private OnSslUntrustedCertListener mListener
;
64 private SslError mError
;
65 private CertificateCombinedException mException
= null
;
67 public SslUntrustedCertDialog() {
70 public SslUntrustedCertDialog(X509Certificate cert
, SslError error
) {
76 * Private constructor.
78 * Instances have to be created through static {@link SslUntrustedCertDialog#newInstance}.
80 * @param context Android context where the dialog will live
81 * @param e Exception causing the need of prompt the user about the server certificate.
82 * @param listener Object to notice when the server certificate was added to the local certificates store.
84 private SslUntrustedCertDialog(RemoteOperationResult result
, OnSslUntrustedCertListener listener
) {
86 if (result
.isSslRecoverableException()) {
87 mException
= (CertificateCombinedException
) result
.getException();
88 mCertificate
= mException
.getServerCertificate();
93 public static SslUntrustedCertDialog
newInstance(X509Certificate cert
, SslError error
) {
95 SslUntrustedCertDialog dialog
= new SslUntrustedCertDialog(cert
, error
);
97 } else { // TODO Review this case
98 SslUntrustedCertDialog dialog
= new SslUntrustedCertDialog();
106 * Creates a new SslUntrustedCertDialog to ask the user if an untrusted certificate from a server should
109 * @param context Android context where the dialog will live.
110 * @param result Result of a failed remote operation.
111 * @param listener Object to notice when the server certificate was added to the local certificates store.
112 * @return A new SslUntrustedCertDialog instance. NULL if the operation can not be recovered
113 * by setting the certificate as reliable.
115 public static SslUntrustedCertDialog
newInstance(RemoteOperationResult result
, OnSslUntrustedCertListener listener
) {
116 if (result
!= null
&& result
.isSslRecoverableException()) {
117 SslUntrustedCertDialog dialog
= new SslUntrustedCertDialog(result
, listener
);
125 public void onCreate(Bundle savedInstanceState
) {
126 super.onCreate(savedInstanceState
);
127 setRetainInstance(true
);
132 public void onAttach(Activity activity
) {
133 super.onAttach(activity
);
134 if (activity
instanceof SherlockActivity
) {
135 mListener
= (OnSslUntrustedCertListener
) activity
;
140 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
141 // Create a view by inflating desired layout
142 mView
= inflater
.inflate(R
.layout
.ssl_untrusted_cert_layout
, container
, false
);
144 updateException(mException
);
146 Button ok
= (Button
) mView
.findViewById(R
.id
.ok
);
147 ok
.setOnClickListener(new OnClickListener() {
150 public void onClick(View v
) {
154 if (mListener
!= null
) {
155 mListener
.onSavedCertificate();
158 Log_OC
.d(TAG
, "Nobody there to notify the certificate was saved");
160 } catch (GeneralSecurityException e
) {
162 if (mListener
!= null
) {
163 mListener
.onFailedSavingCertificate();
165 Log_OC
.e(TAG
, "Server certificate could not be saved in the known servers trust store ", e
);
167 } catch (IOException e
) {
169 if (mListener
!= null
) {
170 mListener
.onFailedSavingCertificate();
172 Log_OC
.e(TAG
, "Server certificate could not be saved in the known servers trust store ", e
);
178 Button cancel
= (Button
) mView
.findViewById(R
.id
.cancel
);
179 cancel
.setOnClickListener(new OnClickListener() {
182 public void onClick(View v
) {
183 getDialog().cancel();
184 mListener
.onCancelCertificate();
188 Button details
= (Button
) mView
.findViewById(R
.id
.details_btn
);
189 details
.setOnClickListener(new OnClickListener() {
192 public void onClick(View v
) {
193 View detailsScroll
= mView
.findViewById(R
.id
.details_scroll
);
194 if (detailsScroll
.getVisibility() == View
.VISIBLE
) {
195 detailsScroll
.setVisibility(View
.GONE
);
196 ((Button
) v
).setText(R
.string
.ssl_validator_btn_details_see
);
199 detailsScroll
.setVisibility(View
.VISIBLE
);
200 ((Button
) v
).setText(R
.string
.ssl_validator_btn_details_hide
);
202 showCertificateData(mCertificate
);
212 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
213 final Dialog dialog
= super.onCreateDialog(savedInstanceState
);
214 dialog
.requestWindowFeature(Window
.FEATURE_NO_TITLE
);
220 public void onDestroyView() {
221 if (getDialog() != null
&& getRetainInstance())
222 getDialog().setDismissMessage(null
);
223 super.onDestroyView();
227 private void updateException(CertificateCombinedException exception
) {
230 mView
.findViewById(R
.id
.reason_cert_not_trusted
).setVisibility(View
.GONE
);
231 mView
.findViewById(R
.id
.reason_cert_expired
).setVisibility(View
.GONE
);
232 mView
.findViewById(R
.id
.reason_cert_not_yet_valid
).setVisibility(View
.GONE
);
233 mView
.findViewById(R
.id
.reason_hostname_not_verified
).setVisibility(View
.GONE
);
234 mView
.findViewById(R
.id
.details_scroll
).setVisibility(View
.GONE
);
237 if (mException
!= null
) {
240 if (mException
.getCertPathValidatorException() != null
) {
241 ((TextView
)mView
.findViewById(R
.id
.reason_cert_not_trusted
)).setVisibility(View
.VISIBLE
);
244 if (mException
.getCertificateExpiredException() != null
) {
245 ((TextView
)mView
.findViewById(R
.id
.reason_cert_expired
)).setVisibility(View
.VISIBLE
);
248 if (mException
.getCertificateNotYetValidException() != null
) {
249 ((TextView
)mView
.findViewById(R
.id
.reason_cert_not_yet_valid
)).setVisibility(View
.VISIBLE
);
252 if (mException
.getSslPeerUnverifiedException() != null
) {
253 ((TextView
)mView
.findViewById(R
.id
.reason_hostname_not_verified
)).setVisibility(View
.VISIBLE
);
260 private void showCertificateData(X509Certificate cert
) {
262 TextView nullCerView
= (TextView
) mView
.findViewById(R
.id
.null_cert
);
265 nullCerView
.setVisibility(View
.GONE
);
266 showSubject(cert
.getSubjectX500Principal());
267 showIssuer(cert
.getIssuerX500Principal());
268 showValidity(cert
.getNotBefore(), cert
.getNotAfter());
272 nullCerView
.setVisibility(View
.VISIBLE
);
276 private void showSignature(X509Certificate cert
) {
277 TextView sigView
= ((TextView
)mView
.findViewById(R
.id
.value_signature
));
278 TextView algorithmView
= ((TextView
)mView
.findViewById(R
.id
.value_signature_algorithm
));
279 sigView
.setText(getHex(cert
.getSignature()));
280 algorithmView
.setText(cert
.getSigAlgName());
283 public String
getHex(final byte [] raw
) {
287 final StringBuilder hex
= new StringBuilder(2 * raw
.length
);
288 for (final byte b
: raw
) {
289 final int hiVal
= (b
& 0xF0) >> 4;
290 final int loVal
= b
& 0x0F;
291 hex
.append((char) ('0' + (hiVal
+ (hiVal
/ 10 * 7))));
292 hex
.append((char) ('0' + (loVal
+ (loVal
/ 10 * 7))));
294 return hex
.toString();
297 @SuppressWarnings("deprecation")
298 private void showValidity(Date notBefore
, Date notAfter
) {
299 TextView fromView
= ((TextView
)mView
.findViewById(R
.id
.value_validity_from
));
300 TextView toView
= ((TextView
)mView
.findViewById(R
.id
.value_validity_to
));
301 fromView
.setText(notBefore
.toLocaleString());
302 toView
.setText(notAfter
.toLocaleString());
305 private void showSubject(X500Principal subject
) {
306 Map
<String
, String
> s
= parsePrincipal(subject
);
307 TextView cnView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_CN
));
308 TextView oView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_O
));
309 TextView ouView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_OU
));
310 TextView cView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_C
));
311 TextView stView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_ST
));
312 TextView lView
= ((TextView
)mView
.findViewById(R
.id
.value_subject_L
));
314 if (s
.get("CN") != null
) {
315 cnView
.setText(s
.get("CN"));
316 cnView
.setVisibility(View
.VISIBLE
);
318 cnView
.setVisibility(View
.GONE
);
320 if (s
.get("O") != null
) {
321 oView
.setText(s
.get("O"));
322 oView
.setVisibility(View
.VISIBLE
);
324 oView
.setVisibility(View
.GONE
);
326 if (s
.get("OU") != null
) {
327 ouView
.setText(s
.get("OU"));
328 ouView
.setVisibility(View
.VISIBLE
);
330 ouView
.setVisibility(View
.GONE
);
332 if (s
.get("C") != null
) {
333 cView
.setText(s
.get("C"));
334 cView
.setVisibility(View
.VISIBLE
);
336 cView
.setVisibility(View
.GONE
);
338 if (s
.get("ST") != null
) {
339 stView
.setText(s
.get("ST"));
340 stView
.setVisibility(View
.VISIBLE
);
342 stView
.setVisibility(View
.GONE
);
344 if (s
.get("L") != null
) {
345 lView
.setText(s
.get("L"));
346 lView
.setVisibility(View
.VISIBLE
);
348 lView
.setVisibility(View
.GONE
);
352 private void showIssuer(X500Principal issuer
) {
353 Map
<String
, String
> s
= parsePrincipal(issuer
);
354 TextView cnView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_CN
));
355 TextView oView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_O
));
356 TextView ouView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_OU
));
357 TextView cView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_C
));
358 TextView stView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_ST
));
359 TextView lView
= ((TextView
)mView
.findViewById(R
.id
.value_issuer_L
));
361 if (s
.get("CN") != null
) {
362 cnView
.setText(s
.get("CN"));
363 cnView
.setVisibility(View
.VISIBLE
);
365 cnView
.setVisibility(View
.GONE
);
367 if (s
.get("O") != null
) {
368 oView
.setText(s
.get("O"));
369 oView
.setVisibility(View
.VISIBLE
);
371 oView
.setVisibility(View
.GONE
);
373 if (s
.get("OU") != null
) {
374 ouView
.setText(s
.get("OU"));
375 ouView
.setVisibility(View
.VISIBLE
);
377 ouView
.setVisibility(View
.GONE
);
379 if (s
.get("C") != null
) {
380 cView
.setText(s
.get("C"));
381 cView
.setVisibility(View
.VISIBLE
);
383 cView
.setVisibility(View
.GONE
);
385 if (s
.get("ST") != null
) {
386 stView
.setText(s
.get("ST"));
387 stView
.setVisibility(View
.VISIBLE
);
389 stView
.setVisibility(View
.GONE
);
391 if (s
.get("L") != null
) {
392 lView
.setText(s
.get("L"));
393 lView
.setVisibility(View
.VISIBLE
);
395 lView
.setVisibility(View
.GONE
);
400 private Map
<String
, String
> parsePrincipal(X500Principal principal
) {
401 Map
<String
, String
> result
= new HashMap
<String
, String
>();
402 String toParse
= principal
.getName();
403 String
[] pieces
= toParse
.split(",");
404 String
[] tokens
= {"CN", "O", "OU", "C", "ST", "L"};
405 for (int i
=0; i
< pieces
.length
; i
++) {
406 for (int j
=0; j
<tokens
.length
; j
++) {
407 if (pieces
[i
].startsWith(tokens
[j
] + "=")) {
408 result
.put(tokens
[j
], pieces
[i
].substring(tokens
[j
].length()+1));
415 private void saveServerCert() throws KeyStoreException
, NoSuchAlgorithmException
, CertificateException
, IOException
{
416 if (mCertificate
!= null
) {
417 // TODO make this asynchronously, it can take some time
418 NetworkUtils
.addCertToKnownServersStore(mCertificate
, getSherlockActivity());
422 public interface OnSslUntrustedCertListener
{
423 public void onSavedCertificate();
424 public void onCancelCertificate();
425 public void onFailedSavingCertificate();