Advertisement
Guest User

Untitled

a guest
Mar 26th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. UnknownHostException
  2.  
  3. package com.example.sslcient;
  4.  
  5. import java.io.IOException;
  6. import java.net.InetAddress;
  7. import java.net.UnknownHostException;
  8. import java.security.KeyManagementException;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.security.SecureRandom;
  11. import java.security.cert.Certificate;
  12. import java.security.cert.CertificateException;
  13. import java.security.cert.X509Certificate;
  14.  
  15. import javax.net.ssl.SSLContext;
  16. import javax.net.ssl.SSLPeerUnverifiedException;
  17. import javax.net.ssl.SSLSession;
  18. import javax.net.ssl.SSLSocket;
  19. import javax.net.ssl.SSLSocketFactory;
  20. import javax.net.ssl.TrustManager;
  21. import javax.net.ssl.X509TrustManager;
  22.  
  23. import android.app.Activity;
  24. import android.graphics.Color;
  25. import android.os.Bundle;
  26. import android.os.StrictMode;
  27. import android.support.v7.appcompat.R.color;
  28. import android.view.View;
  29. import android.widget.EditText;
  30. import android.widget.TextView;
  31.  
  32. public class SSLClientActivity extends Activity {
  33.  
  34. private EditText web_address_et;
  35. private EditText port_number_et;
  36. private TextView connection_information_tv;
  37. private Certificate[] certs;
  38.  
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.activity_sslclient);
  43.  
  44. //Allowing to access the network
  45. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  46. StrictMode.setThreadPolicy(policy);
  47.  
  48. //Getting the GUI user input information
  49. web_address_et = (EditText)findViewById(R.id.editText1);
  50. port_number_et = (EditText)findViewById(R.id.editText2);
  51. connection_information_tv = (TextView)findViewById(R.id.textView3);
  52.  
  53. findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
  54. @Override
  55. public void onClick(View v) {
  56. findViewById(R.id.button1).setBackgroundColor(Color.CYAN);
  57. findViewById(R.id.button2).setBackgroundColor(color.button_material_dark);
  58. if (web_address_et.getText().toString().matches("")
  59. || port_number_et.getText().toString().matches("")) {
  60. connection_information_tv.setText("Please fill the URL and Port# fields!");
  61. }
  62.  
  63. else {
  64. try {
  65. InetAddress host = InetAddress.getByName(web_address_et.getText().toString());
  66.  
  67. //Android default behaviour (do not accept untrusted certificate)
  68. SSLSocketFactory socketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
  69. SSLSocket socket = (SSLSocket)socketFactory.createSocket(host,
  70. Integer.parseInt(port_number_et.getText().toString()));
  71.  
  72. socket.startHandshake();
  73. printSSLSessionInfo(socket, socket.getSession());
  74. socket.close();
  75.  
  76. } catch (UnknownHostException e) {
  77. connection_information_tv.setText(e.toString());
  78. } catch (SSLPeerUnverifiedException e) {
  79. connection_information_tv.setText(e.toString());
  80. } catch (IOException e) {
  81. connection_information_tv.setText(e.toString());
  82. }
  83. }
  84. }
  85. });
  86.  
  87. findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
  88. @Override
  89. public void onClick(View v) {
  90. findViewById(R.id.button1).setBackgroundColor(color.button_material_dark);
  91. findViewById(R.id.button2).setBackgroundColor(Color.CYAN);
  92. if (web_address_et.getText().toString().matches("")
  93. || port_number_et.getText().toString().matches("")) {
  94. connection_information_tv.setText("Please fill the URL and Port# fields!");
  95. }
  96.  
  97. else {
  98. try {
  99. InetAddress host = InetAddress.getByName(web_address_et.getText().toString());
  100.  
  101. //Naive custom TrustManager (empty checkServerTrusted)
  102. SSLContext sslContext = SSLContext.getInstance("SSL");
  103. TrustManager trustManagerNaive = new X509TrustManager(){
  104. @Override
  105. public void checkClientTrusted(
  106. X509Certificate[] chain,
  107. String authType)
  108. throws CertificateException {
  109. // TODO Auto-generated method stub
  110. }
  111.  
  112. @Override
  113. public X509Certificate[] getAcceptedIssuers() {
  114. // TODO Auto-generated method stub
  115. return null;
  116. }
  117.  
  118. @Override
  119. public void checkServerTrusted(
  120. X509Certificate[] chain,
  121. String authType)
  122. throws CertificateException {
  123. // TODO Auto-generated method stub
  124. }
  125. };
  126.  
  127. sslContext.init(null, new TrustManager[]{trustManagerNaive}, new SecureRandom());
  128.  
  129. SSLSocketFactory socketFactory = (SSLSocketFactory)sslContext.getSocketFactory();
  130. SSLSocket socket = (SSLSocket)socketFactory.createSocket(host,
  131. Integer.parseInt(port_number_et.getText().toString()));
  132.  
  133. socket.startHandshake();
  134. printSSLSessionInfo(socket, socket.getSession());
  135. socket.close();
  136.  
  137. } catch (UnknownHostException e) {
  138. connection_information_tv.setText(e.toString());
  139. } catch (NoSuchAlgorithmException e) {
  140. connection_information_tv.setText(e.toString());
  141. } catch (KeyManagementException e) {
  142. connection_information_tv.setText(e.toString());
  143. } catch (SSLPeerUnverifiedException e) {
  144. connection_information_tv.setText(e.toString());
  145. } catch (IOException e) {
  146. connection_information_tv.setText(e.toString());
  147. }
  148. }
  149. }
  150. });
  151. }
  152.  
  153. private void printSSLSessionInfo(SSLSocket socket, SSLSession sslSession) throws SSLPeerUnverifiedException {
  154.  
  155. String certIssuerDN = "";
  156. certs = sslSession.getPeerCertificates();
  157.  
  158. for (Certificate cert : certs) {
  159. System.out.println("Certificate is: " + cert);
  160. if(cert instanceof X509Certificate) {
  161. X509Certificate x = (X509Certificate ) cert;
  162. certIssuerDN = certIssuerDN + x.getIssuerDN() + "n";
  163. }
  164. }
  165.  
  166. connection_information_tv.setText(
  167. "SSL session id: " + sslSession.getId() +
  168. " | Valid session? " + sslSession.isValid() +
  169. "nPeer host/port: " + sslSession.getPeerHost() + "/" + sslSession.getPeerPort() +
  170. "nRequire client authentificartion: " + socket.getNeedClientAuth() +
  171. "nProtocol: " + sslSession.getProtocol() +
  172. "nCipher suite: " + sslSession.getCipherSuite() +
  173. "nnCertificates retrieved: " + certs.length +
  174. "n" + certIssuerDN
  175. );
  176. }
  177. }
  178.  
  179. <?xml version="1.0" encoding="utf-8"?>
  180. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  181. package="com.example.sslcient"
  182. android:versionCode="1"
  183. android:versionName="1.0" >
  184.  
  185. <uses-permission
  186. android:name="android.permission.INTERNET" />
  187.  
  188. <uses-sdk
  189. android:minSdkVersion="9"
  190. android:targetSdkVersion="18" />
  191.  
  192. <application
  193. android:allowBackup="true"
  194. android:icon="@drawable/ic_launcher"
  195. android:label="@string/app_name"
  196. android:theme="@style/AppTheme" >
  197. <activity
  198. android:name=".SSLClientActivity"
  199. android:label="@string/app_name" >
  200. <intent-filter>
  201. <action android:name="android.intent.action.MAIN" />
  202.  
  203. <category android:name="android.intent.category.LAUNCHER" />
  204. </intent-filter>
  205. </activity>
  206. </application>
  207.  
  208. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement