1 /* ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2012-2014 ownCloud Inc.
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.
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.
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/>.
20 package com
.owncloud
.android
.ui
.adapter
;
22 import java
.security
.cert
.X509Certificate
;
23 import java
.text
.DateFormat
;
24 import java
.util
.Date
;
25 import java
.util
.HashMap
;
28 import javax
.security
.auth
.x500
.X500Principal
;
30 import com
.owncloud
.android
.R
;
31 import com
.owncloud
.android
.ui
.dialog
.SslUntrustedCertDialog
;
33 import android
.view
.View
;
34 import android
.widget
.TextView
;
39 public class X509CertificateViewAdapter
implements SslUntrustedCertDialog
.CertificateViewAdapter
{
41 //private final static String TAG = X509CertificateViewAdapter.class.getSimpleName();
43 private X509Certificate mCertificate
= null
;
45 public X509CertificateViewAdapter(X509Certificate certificate
) {
46 mCertificate
= certificate
;
50 public void updateCertificateView(View dialogView
) {
51 TextView nullCerView
= (TextView
) dialogView
.findViewById(R
.id
.null_cert
);
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
);
61 nullCerView
.setVisibility(View
.VISIBLE
);
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());
72 public String
getHex(final byte [] raw
) {
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))));
83 return hex
.toString();
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
));
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
));
103 if (s
.get("CN") != null
) {
104 cnView
.setText(s
.get("CN"));
105 cnView
.setVisibility(View
.VISIBLE
);
107 cnView
.setVisibility(View
.GONE
);
109 if (s
.get("O") != null
) {
110 oView
.setText(s
.get("O"));
111 oView
.setVisibility(View
.VISIBLE
);
113 oView
.setVisibility(View
.GONE
);
115 if (s
.get("OU") != null
) {
116 ouView
.setText(s
.get("OU"));
117 ouView
.setVisibility(View
.VISIBLE
);
119 ouView
.setVisibility(View
.GONE
);
121 if (s
.get("C") != null
) {
122 cView
.setText(s
.get("C"));
123 cView
.setVisibility(View
.VISIBLE
);
125 cView
.setVisibility(View
.GONE
);
127 if (s
.get("ST") != null
) {
128 stView
.setText(s
.get("ST"));
129 stView
.setVisibility(View
.VISIBLE
);
131 stView
.setVisibility(View
.GONE
);
133 if (s
.get("L") != null
) {
134 lView
.setText(s
.get("L"));
135 lView
.setVisibility(View
.VISIBLE
);
137 lView
.setVisibility(View
.GONE
);
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
));
150 if (s
.get("CN") != null
) {
151 cnView
.setText(s
.get("CN"));
152 cnView
.setVisibility(View
.VISIBLE
);
154 cnView
.setVisibility(View
.GONE
);
156 if (s
.get("O") != null
) {
157 oView
.setText(s
.get("O"));
158 oView
.setVisibility(View
.VISIBLE
);
160 oView
.setVisibility(View
.GONE
);
162 if (s
.get("OU") != null
) {
163 ouView
.setText(s
.get("OU"));
164 ouView
.setVisibility(View
.VISIBLE
);
166 ouView
.setVisibility(View
.GONE
);
168 if (s
.get("C") != null
) {
169 cView
.setText(s
.get("C"));
170 cView
.setVisibility(View
.VISIBLE
);
172 cView
.setVisibility(View
.GONE
);
174 if (s
.get("ST") != null
) {
175 stView
.setText(s
.get("ST"));
176 stView
.setVisibility(View
.VISIBLE
);
178 stView
.setVisibility(View
.GONE
);
180 if (s
.get("L") != null
) {
181 lView
.setText(s
.get("L"));
182 lView
.setVisibility(View
.VISIBLE
);
184 lView
.setVisibility(View
.GONE
);
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));