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