X509CertificateViewAdapter.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. import java.security.cert.X509Certificate;
  22. import java.text.DateFormat;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import javax.security.auth.x500.X500Principal;
  27. import com.owncloud.android.R;
  28. import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
  29. import android.view.View;
  30. import android.widget.TextView;
  31. /**
  32. *
  33. */
  34. public class X509CertificateViewAdapter implements SslUntrustedCertDialog.CertificateViewAdapter {
  35. //private final static String TAG = X509CertificateViewAdapter.class.getSimpleName();
  36. private X509Certificate mCertificate = null;
  37. public X509CertificateViewAdapter(X509Certificate certificate) {
  38. mCertificate = certificate;
  39. }
  40. @Override
  41. public void updateCertificateView(View dialogView) {
  42. TextView nullCerView = (TextView) dialogView.findViewById(R.id.null_cert);
  43. if (mCertificate != null) {
  44. nullCerView.setVisibility(View.GONE);
  45. showSubject(mCertificate.getSubjectX500Principal(), dialogView);
  46. showIssuer(mCertificate.getIssuerX500Principal(), dialogView);
  47. showValidity(mCertificate.getNotBefore(), mCertificate.getNotAfter(), dialogView);
  48. showSignature(dialogView);
  49. } else {
  50. nullCerView.setVisibility(View.VISIBLE);
  51. }
  52. }
  53. private void showSignature(View dialogView) {
  54. TextView sigView = ((TextView)dialogView.findViewById(R.id.value_signature));
  55. TextView algorithmView = ((TextView)dialogView.findViewById(R.id.value_signature_algorithm));
  56. sigView.setText(getHex(mCertificate.getSignature()));
  57. algorithmView.setText(mCertificate.getSigAlgName());
  58. }
  59. public String getHex(final byte [] raw) {
  60. if (raw == null) {
  61. return null;
  62. }
  63. final StringBuilder hex = new StringBuilder(2 * raw.length);
  64. for (final byte b : raw) {
  65. final int hiVal = (b & 0xF0) >> 4;
  66. final int loVal = b & 0x0F;
  67. hex.append((char) ('0' + (hiVal + (hiVal / 10 * 7))));
  68. hex.append((char) ('0' + (loVal + (loVal / 10 * 7))));
  69. }
  70. return hex.toString();
  71. }
  72. private void showValidity(Date notBefore, Date notAfter, View dialogView) {
  73. TextView fromView = ((TextView)dialogView.findViewById(R.id.value_validity_from));
  74. TextView toView = ((TextView)dialogView.findViewById(R.id.value_validity_to));
  75. DateFormat dateFormat = DateFormat.getDateInstance();
  76. fromView.setText(dateFormat.format(notBefore));
  77. toView.setText(dateFormat.format(notAfter));
  78. }
  79. private void showSubject(X500Principal subject, View dialogView) {
  80. Map<String, String> s = parsePrincipal(subject);
  81. TextView cnView = ((TextView)dialogView.findViewById(R.id.value_subject_CN));
  82. TextView oView = ((TextView)dialogView.findViewById(R.id.value_subject_O));
  83. TextView ouView = ((TextView)dialogView.findViewById(R.id.value_subject_OU));
  84. TextView cView = ((TextView)dialogView.findViewById(R.id.value_subject_C));
  85. TextView stView = ((TextView)dialogView.findViewById(R.id.value_subject_ST));
  86. TextView lView = ((TextView)dialogView.findViewById(R.id.value_subject_L));
  87. if (s.get("CN") != null) {
  88. cnView.setText(s.get("CN"));
  89. cnView.setVisibility(View.VISIBLE);
  90. } else {
  91. cnView.setVisibility(View.GONE);
  92. }
  93. if (s.get("O") != null) {
  94. oView.setText(s.get("O"));
  95. oView.setVisibility(View.VISIBLE);
  96. } else {
  97. oView.setVisibility(View.GONE);
  98. }
  99. if (s.get("OU") != null) {
  100. ouView.setText(s.get("OU"));
  101. ouView.setVisibility(View.VISIBLE);
  102. } else {
  103. ouView.setVisibility(View.GONE);
  104. }
  105. if (s.get("C") != null) {
  106. cView.setText(s.get("C"));
  107. cView.setVisibility(View.VISIBLE);
  108. } else {
  109. cView.setVisibility(View.GONE);
  110. }
  111. if (s.get("ST") != null) {
  112. stView.setText(s.get("ST"));
  113. stView.setVisibility(View.VISIBLE);
  114. } else {
  115. stView.setVisibility(View.GONE);
  116. }
  117. if (s.get("L") != null) {
  118. lView.setText(s.get("L"));
  119. lView.setVisibility(View.VISIBLE);
  120. } else {
  121. lView.setVisibility(View.GONE);
  122. }
  123. }
  124. private void showIssuer(X500Principal issuer, View dialogView) {
  125. Map<String, String> s = parsePrincipal(issuer);
  126. TextView cnView = ((TextView)dialogView.findViewById(R.id.value_issuer_CN));
  127. TextView oView = ((TextView)dialogView.findViewById(R.id.value_issuer_O));
  128. TextView ouView = ((TextView)dialogView.findViewById(R.id.value_issuer_OU));
  129. TextView cView = ((TextView)dialogView.findViewById(R.id.value_issuer_C));
  130. TextView stView = ((TextView)dialogView.findViewById(R.id.value_issuer_ST));
  131. TextView lView = ((TextView)dialogView.findViewById(R.id.value_issuer_L));
  132. if (s.get("CN") != null) {
  133. cnView.setText(s.get("CN"));
  134. cnView.setVisibility(View.VISIBLE);
  135. } else {
  136. cnView.setVisibility(View.GONE);
  137. }
  138. if (s.get("O") != null) {
  139. oView.setText(s.get("O"));
  140. oView.setVisibility(View.VISIBLE);
  141. } else {
  142. oView.setVisibility(View.GONE);
  143. }
  144. if (s.get("OU") != null) {
  145. ouView.setText(s.get("OU"));
  146. ouView.setVisibility(View.VISIBLE);
  147. } else {
  148. ouView.setVisibility(View.GONE);
  149. }
  150. if (s.get("C") != null) {
  151. cView.setText(s.get("C"));
  152. cView.setVisibility(View.VISIBLE);
  153. } else {
  154. cView.setVisibility(View.GONE);
  155. }
  156. if (s.get("ST") != null) {
  157. stView.setText(s.get("ST"));
  158. stView.setVisibility(View.VISIBLE);
  159. } else {
  160. stView.setVisibility(View.GONE);
  161. }
  162. if (s.get("L") != null) {
  163. lView.setText(s.get("L"));
  164. lView.setVisibility(View.VISIBLE);
  165. } else {
  166. lView.setVisibility(View.GONE);
  167. }
  168. }
  169. private Map<String, String> parsePrincipal(X500Principal principal) {
  170. Map<String, String> result = new HashMap<String, String>();
  171. String toParse = principal.getName();
  172. String[] pieces = toParse.split(",");
  173. String[] tokens = {"CN", "O", "OU", "C", "ST", "L"};
  174. for (int i=0; i < pieces.length ; i++) {
  175. for (int j=0; j<tokens.length; j++) {
  176. if (pieces[i].startsWith(tokens[j] + "=")) {
  177. result.put(tokens[j], pieces[i].substring(tokens[j].length()+1));
  178. }
  179. }
  180. }
  181. return result;
  182. }
  183. }