2 * ownCloud Android client application
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.ui
.adapter
;
23 import java
.security
.cert
.X509Certificate
;
24 import java
.text
.DateFormat
;
25 import java
.util
.Date
;
26 import java
.util
.HashMap
;
29 import javax
.security
.auth
.x500
.X500Principal
;
31 import com
.owncloud
.android
.R
;
32 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
;
34 import android
.view
.View
;
35 import android
.widget
.TextView
;
40 public class X509CertificateViewAdapter
implements SslUntrustedCertDialog
.CertificateViewAdapter
{
42 //private final static String TAG = X509CertificateViewAdapter.class.getSimpleName();
44 private X509Certificate mCertificate
= null
;
46 public X509CertificateViewAdapter(X509Certificate certificate
) {
47 mCertificate
= certificate
;
51 public void updateCertificateView(View dialogView
) {
52 TextView nullCerView
= (TextView
) dialogView
.findViewById(R
.id
.null_cert
);
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
);
62 nullCerView
.setVisibility(View
.VISIBLE
);
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());
73 public String
getHex(final byte [] raw
) {
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))));
84 return hex
.toString();
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
));
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
));
104 if (s
.get("CN") != null
) {
105 cnView
.setText(s
.get("CN"));
106 cnView
.setVisibility(View
.VISIBLE
);
108 cnView
.setVisibility(View
.GONE
);
110 if (s
.get("O") != null
) {
111 oView
.setText(s
.get("O"));
112 oView
.setVisibility(View
.VISIBLE
);
114 oView
.setVisibility(View
.GONE
);
116 if (s
.get("OU") != null
) {
117 ouView
.setText(s
.get("OU"));
118 ouView
.setVisibility(View
.VISIBLE
);
120 ouView
.setVisibility(View
.GONE
);
122 if (s
.get("C") != null
) {
123 cView
.setText(s
.get("C"));
124 cView
.setVisibility(View
.VISIBLE
);
126 cView
.setVisibility(View
.GONE
);
128 if (s
.get("ST") != null
) {
129 stView
.setText(s
.get("ST"));
130 stView
.setVisibility(View
.VISIBLE
);
132 stView
.setVisibility(View
.GONE
);
134 if (s
.get("L") != null
) {
135 lView
.setText(s
.get("L"));
136 lView
.setVisibility(View
.VISIBLE
);
138 lView
.setVisibility(View
.GONE
);
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
));
151 if (s
.get("CN") != null
) {
152 cnView
.setText(s
.get("CN"));
153 cnView
.setVisibility(View
.VISIBLE
);
155 cnView
.setVisibility(View
.GONE
);
157 if (s
.get("O") != null
) {
158 oView
.setText(s
.get("O"));
159 oView
.setVisibility(View
.VISIBLE
);
161 oView
.setVisibility(View
.GONE
);
163 if (s
.get("OU") != null
) {
164 ouView
.setText(s
.get("OU"));
165 ouView
.setVisibility(View
.VISIBLE
);
167 ouView
.setVisibility(View
.GONE
);
169 if (s
.get("C") != null
) {
170 cView
.setText(s
.get("C"));
171 cView
.setVisibility(View
.VISIBLE
);
173 cView
.setVisibility(View
.GONE
);
175 if (s
.get("ST") != null
) {
176 stView
.setText(s
.get("ST"));
177 stView
.setVisibility(View
.VISIBLE
);
179 stView
.setVisibility(View
.GONE
);
181 if (s
.get("L") != null
) {
182 lView
.setText(s
.get("L"));
183 lView
.setVisibility(View
.VISIBLE
);
185 lView
.setVisibility(View
.GONE
);
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));