Hi devs. I am trying to establish SSL connection between android and java 
server running on windows using SSLSockets. But i am not able to get the 
connection into working.
I am nowhere good in SSL connection and lost in establishing connection. So 
any pointers on how it should be done between android and java server ?
I have attached the code that i have tried. Can anyone find out where i am 
going wrong ?

Here is the stacktrace.

04-09 16:33:36.516: WARN/System.err(278): java.io.IOException: Wrong 
version of key store.
04-09 16:33:36.545: WARN/System.err(278):     at 
org.bouncycastle.jce.provider.JDKKeyStore.engineLoad(JDKKeyStore.java:839)
04-09 16:33:36.545: WARN/System.err(278):     at 
java.security.KeyStore.load(KeyStore.java:676)
04-09 16:33:36.545: WARN/System.err(278):     at 
com.pkg.sslmsg.SSLMsgActivity$1.onClick(SSLMsgActivity.java:68)
04-09 16:33:36.545: WARN/System.err(278):     at 
android.view.View.performClick(View.java:2408)
04-09 16:33:36.545: WARN/System.err(278):     at 
android.view.View$PerformClick.run(View.java:8816)
04-09 16:33:36.545: WARN/System.err(278):     at 
android.os.Handler.handleCallback(Handler.java:587)
04-09 16:33:36.545: WARN/System.err(278):     at 
android.os.Handler.dispatchMessage(Handler.java:92)
04-09 16:33:36.545: WARN/System.err(278):     at 
android.os.Looper.loop(Looper.java:123)
04-09 16:33:36.545: WARN/System.err(278):     at 
android.app.ActivityThread.main(ActivityThread.java:4627)
04-09 16:33:36.545: WARN/System.err(278):     at 
java.lang.reflect.Method.invokeNative(Native Method)
04-09 16:33:36.545: WARN/System.err(278):     at 
java.lang.reflect.Method.invoke(Method.java:521)
04-09 16:33:36.545: WARN/System.err(278):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-09 16:33:36.545: WARN/System.err(278):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-09 16:33:36.545: WARN/System.err(278):     at 
dalvik.system.NativeStart.main(Native Method)

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
package com.pkg.sslmsg;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SSLMsgActivity extends Activity {
	
	private EditText ipadd,port,msgout;
	private Button send,conn;
	private TextView msgin;
	private SSLSocket socket;
	private DataOutputStream dataOut;
	private DataInputStream dataIn;
	private Context cont;
	private static final String TAG = "SSLMsgActivity";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cont = getApplicationContext();
        ipadd = (EditText) findViewById(R.id.ipadd);
        port = (EditText) findViewById(R.id.port);
        msgin = (TextView) findViewById(R.id.msgrcv);
        msgout = (EditText) findViewById(R.id.msgsnd);
        send = (Button) findViewById(R.id.btsnd);
        conn = (Button) findViewById(R.id.btconn);
        msgin.setText("I'm here");
        conn.setOnClickListener(onConnClick);
        send.setOnClickListener(onSendClick);       
    }
    
    OnClickListener onConnClick = new OnClickListener() {
		
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			try {
				Log.d(TAG, "Starting..");
				
	            // Setup truststore
				Log.d("SSLClient","TrustStore beginning");
	            KeyStore trustStore = KeyStore.getInstance("BKS");
	            Log.d("SSLClinet","..here1..");
	            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
	            Log.d("SSLClinet","..here2..");
	            InputStream trustStoreStream = cont.getResources().openRawResource(R.raw.mysrvtruststore);
	            Log.d("SSLClinet","..here3..");
	            trustStore.load(trustStoreStream, "server".toCharArray());
	            Log.d("SSLClinet","..here4..");
	            trustManagerFactory.init(trustStore);
	            Log.d("SSLClient","TrustStore done");

	            // Setup keystore
	            Log.d("SSLClient","Keystore beginning");
	            KeyStore keyStore = KeyStore.getInstance("BKS");
	            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
	            InputStream keyStoreStream = cont.getResources().openRawResource(R.raw.clientkeystore);
				keyStore.load(keyStoreStream, "client".toCharArray());
	            keyManagerFactory.init(keyStore, "client".toCharArray());
	            Log.d("SSLClient","Keystore done");

	            // Setup the SSL context to use the truststore and keystore
	            SSLContext ssl_context = SSLContext.getInstance("TLS");
	            ssl_context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
	            Log.d("SSLClinet","..done..");
				
				SSLSocketFactory socketFactory = (SSLSocketFactory) SSLClient.ssl_context.getSocketFactory();
				Log.d(TAG, "Socket factory created");
				socket = (SSLSocket) socketFactory.createSocket(ipadd.getText().toString().trim(), Integer.parseInt(port.getText().toString().trim()));
				Log.d(TAG, "Socket created");
				dataOut = new DataOutputStream(socket.getOutputStream());
				dataIn = new DataInputStream(socket.getInputStream());
				msgin.setText("Connected");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				msgin.setText("Not connected");
				System.out.println("Error");
				e.printStackTrace();
				Log.d(TAG,"Error !!");
			}
		}
	};
	
	OnClickListener onSendClick = new OnClickListener() {
		
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			try {
				if(socket != null && dataOut != null)
					dataOut.writeUTF(msgout.getText().toString());
				else
					msgin.setText("Connection required");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				msgin.setText("Error !!");
				e.printStackTrace();
			}
		}
	};
	
	protected void onDestroy() {
		if(socket != null)
			try {
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(dataIn != null)
			try {
				dataIn.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(dataOut != null)
			try {
				dataOut.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	};
}
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;


public class SSLServer {
	public static void main(String args[]){
		SSLServerSocketFactory mySSLServerFac = null;
		SSLServerSocket mySSLServerSocket = null;
		SSLSocket mySSLSocket = null;
		String msg = null;
		System.out.print("I'm here\n");
		try {
			mySSLServerFac = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
			mySSLServerSocket = (SSLServerSocket) mySSLServerFac.createServerSocket(9989);
			System.out.println("Listening on 9999\n");
			mySSLSocket = (SSLSocket) mySSLServerSocket.accept();
			
			DataInputStream input = new DataInputStream(mySSLSocket.getInputStream());
			DataOutputStream output = new DataOutputStream(mySSLSocket.getOutputStream());		
			do{
				System.out.println("Remote IP Address : " + mySSLSocket.getInetAddress());
				java.util.Scanner sc = new java.util.Scanner(System.in);
				output.writeUTF(sc.nextLine());
				msg = input.readUTF().toString();
				System.out.println(msg);
			}while(msg != "exit");
			System.out.println(msg);				
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if(mySSLServerSocket != null){
				try { mySSLServerSocket.close(); }
				catch (Exception e){}
			}
			if(mySSLSocket != null){
				try { mySSLSocket.close(); } 
				catch (Exception e){}
			}
		}
	}
}

Reply via email to