Merge branch 'remove_ActionBarSherlock' into navigationDrawer_update
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / X509CertificateViewAdapter.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21 package com.owncloud.android.ui.adapter;
22
23 import java.security.cert.X509Certificate;
24 import java.text.DateFormat;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.security.auth.x500.X500Principal;
30
31 import com.owncloud.android.R;
32 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
33
34 import android.view.View;
35 import android.widget.TextView;
36
37 /**
38 *
39 */
40 public class X509CertificateViewAdapter implements SslUntrustedCertDialog.CertificateViewAdapter {
41
42 //private final static String TAG = X509CertificateViewAdapter.class.getSimpleName();
43
44 private X509Certificate mCertificate = null;
45
46 public X509CertificateViewAdapter(X509Certificate certificate) {
47 mCertificate = certificate;
48 }
49
50 @Override
51 public void updateCertificateView(View dialogView) {
52 TextView nullCerView = (TextView) dialogView.findViewById(R.id.null_cert);
53
54 if (mCertificate != null) {
55 nullCerView.setVisibility(View.GONE);
56 showSubject(mCertificate.getSubjectX500Principal(), dialogView);
57 showIssuer(mCertificate.getIssuerX500Principal(), dialogView);
58 showValidity(mCertificate.getNotBefore(), mCertificate.getNotAfter(), dialogView);
59 showSignature(dialogView);
60
61 } else {
62 nullCerView.setVisibility(View.VISIBLE);
63 }
64 }
65
66 private void showSignature(View dialogView) {
67 TextView sigView = ((TextView)dialogView.findViewById(R.id.value_signature));
68 TextView algorithmView = ((TextView)dialogView.findViewById(R.id.value_signature_algorithm));
69 sigView.setText(getHex(mCertificate.getSignature()));
70 algorithmView.setText(mCertificate.getSigAlgName());
71 }
72
73 public String getHex(final byte [] raw) {
74 if (raw == null) {
75 return null;
76 }
77 final StringBuilder hex = new StringBuilder(2 * raw.length);
78 for (final byte b : raw) {
79 final int hiVal = (b & 0xF0) >> 4;
80 final int loVal = b & 0x0F;
81 hex.append((char) ('0' + (hiVal + (hiVal / 10 * 7))));
82 hex.append((char) ('0' + (loVal + (loVal / 10 * 7))));
83 }
84 return hex.toString();
85 }
86
87 private void showValidity(Date notBefore, Date notAfter, View dialogView) {
88 TextView fromView = ((TextView)dialogView.findViewById(R.id.value_validity_from));
89 TextView toView = ((TextView)dialogView.findViewById(R.id.value_validity_to));
90 DateFormat dateFormat = DateFormat.getDateInstance();
91 fromView.setText(dateFormat.format(notBefore));
92 toView.setText(dateFormat.format(notAfter));
93 }
94
95 private void showSubject(X500Principal subject, View dialogView) {
96 Map<String, String> s = parsePrincipal(subject);
97 TextView cnView = ((TextView)dialogView.findViewById(R.id.value_subject_CN));
98 TextView oView = ((TextView)dialogView.findViewById(R.id.value_subject_O));
99 TextView ouView = ((TextView)dialogView.findViewById(R.id.value_subject_OU));
100 TextView cView = ((TextView)dialogView.findViewById(R.id.value_subject_C));
101 TextView stView = ((TextView)dialogView.findViewById(R.id.value_subject_ST));
102 TextView lView = ((TextView)dialogView.findViewById(R.id.value_subject_L));
103
104 if (s.get("CN") != null) {
105 cnView.setText(s.get("CN"));
106 cnView.setVisibility(View.VISIBLE);
107 } else {
108 cnView.setVisibility(View.GONE);
109 }
110 if (s.get("O") != null) {
111 oView.setText(s.get("O"));
112 oView.setVisibility(View.VISIBLE);
113 } else {
114 oView.setVisibility(View.GONE);
115 }
116 if (s.get("OU") != null) {
117 ouView.setText(s.get("OU"));
118 ouView.setVisibility(View.VISIBLE);
119 } else {
120 ouView.setVisibility(View.GONE);
121 }
122 if (s.get("C") != null) {
123 cView.setText(s.get("C"));
124 cView.setVisibility(View.VISIBLE);
125 } else {
126 cView.setVisibility(View.GONE);
127 }
128 if (s.get("ST") != null) {
129 stView.setText(s.get("ST"));
130 stView.setVisibility(View.VISIBLE);
131 } else {
132 stView.setVisibility(View.GONE);
133 }
134 if (s.get("L") != null) {
135 lView.setText(s.get("L"));
136 lView.setVisibility(View.VISIBLE);
137 } else {
138 lView.setVisibility(View.GONE);
139 }
140 }
141
142 private void showIssuer(X500Principal issuer, View dialogView) {
143 Map<String, String> s = parsePrincipal(issuer);
144 TextView cnView = ((TextView)dialogView.findViewById(R.id.value_issuer_CN));
145 TextView oView = ((TextView)dialogView.findViewById(R.id.value_issuer_O));
146 TextView ouView = ((TextView)dialogView.findViewById(R.id.value_issuer_OU));
147 TextView cView = ((TextView)dialogView.findViewById(R.id.value_issuer_C));
148 TextView stView = ((TextView)dialogView.findViewById(R.id.value_issuer_ST));
149 TextView lView = ((TextView)dialogView.findViewById(R.id.value_issuer_L));
150
151 if (s.get("CN") != null) {
152 cnView.setText(s.get("CN"));
153 cnView.setVisibility(View.VISIBLE);
154 } else {
155 cnView.setVisibility(View.GONE);
156 }
157 if (s.get("O") != null) {
158 oView.setText(s.get("O"));
159 oView.setVisibility(View.VISIBLE);
160 } else {
161 oView.setVisibility(View.GONE);
162 }
163 if (s.get("OU") != null) {
164 ouView.setText(s.get("OU"));
165 ouView.setVisibility(View.VISIBLE);
166 } else {
167 ouView.setVisibility(View.GONE);
168 }
169 if (s.get("C") != null) {
170 cView.setText(s.get("C"));
171 cView.setVisibility(View.VISIBLE);
172 } else {
173 cView.setVisibility(View.GONE);
174 }
175 if (s.get("ST") != null) {
176 stView.setText(s.get("ST"));
177 stView.setVisibility(View.VISIBLE);
178 } else {
179 stView.setVisibility(View.GONE);
180 }
181 if (s.get("L") != null) {
182 lView.setText(s.get("L"));
183 lView.setVisibility(View.VISIBLE);
184 } else {
185 lView.setVisibility(View.GONE);
186 }
187 }
188
189
190 private Map<String, String> parsePrincipal(X500Principal principal) {
191 Map<String, String> result = new HashMap<String, String>();
192 String toParse = principal.getName();
193 String[] pieces = toParse.split(",");
194 String[] tokens = {"CN", "O", "OU", "C", "ST", "L"};
195 for (int i=0; i < pieces.length ; i++) {
196 for (int j=0; j<tokens.length; j++) {
197 if (pieces[i].startsWith(tokens[j] + "=")) {
198 result.put(tokens[j], pieces[i].substring(tokens[j].length()+1));
199 }
200 }
201 }
202 return result;
203 }
204
205 }