SSL client authentication

by telematics » Thu, 12 Mar 2009 05:40:52 GMT


Sponsored Links
  am new on SSL programming. The Android app I am developing needs to
open an SSL socket to a secure server which requires the client
authentication. When running on the Emulator and trying to talk to a
Server running on the host PC, the following Android code snippet
always gives me a SocketException at the line, SSLSocket c =
(SSLSocket) f.createSocket(hostName, 8888) :

private void openSslClient(String hostName)
{
try
{
KeyStore keyStore = KeyStore.getInstance
(KeyStore.getDefaultType());
InputStream fis = this.getAssets().open("client.bks");
keyStore.load(fis, "clientjks".toCharArray());

KeyManagerFactory kmf = KeyManagerFactory.getInstance
("X509");

kmf.init(keyStore, "clientkey".toCharArray());

TrustManagerFactory tmf = TrustManagerFactory.getInstance
("X509");

tmf.init(keyStore);

SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(),
new SecureRandom());

SSLSocketFactory f = sc.getSocketFactory();
SSLSocket c = (SSLSocket) f.createSocket(hostName, 8888);
c.startHandshake();
w = new BufferedWriter(new OutputStreamWriter
(c.getOutputStream()));
r = new BufferedReader(new InputStreamReader
(c.getInputStream()));
} catch (Exception e)
{
Log.e(LOG_TAG, "openSslClient failed", e);
}
}

The stack trace looks like below:
03-11 21:24:00.585: ERROR/OSNetworkSystem(1093): unknown socket error
-1
03-11 21:24:07.156: ERROR/PatientDataUploader(1093): openSslClient
failed
03-11 21:24:07.156: ERROR/PatientDataUploader(1093):
java.net.SocketException: unknown error
03-11 21:24:07.156: ERROR/PatientDataUploader(1093): at
org.apache.harmony.luni.platform.OSNetworkSystem.createSocketImpl
(Native Method)
03-11 21:24:07.156: ERROR/PatientDataUploader(1093): at
org.apache.harmony.luni.platform.OSNetworkSystem.createSocket
(OSNetworkSystem.java:79)
03-11 21:24:07.156: ERROR/PatientDataUploader(1093): at
org.apache.harmony.luni.net.PlainSocketImpl2.create
(PlainSocketImpl2.java:59)
03-11 21:24:07.156: ERROR/PatientDataUploader(1093): at
java.net.Socket.startupSocket(Socket.java:668)
03-11 21:24:07.156: ERROR/PatientDataUploader(1093): at
java.net.Socket.<init>(Socket.java:142)
03-11 21:24:07.156: ERROR/PatientDataUploader(1093): at
javax.net.ssl.SSLSocket.<init>(SSLSocket.java:42)
03-11 21:24:07.156: ERROR/PatientDataUploader(1093): at
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.<init>
(OpenSSLSocketImpl.java:179)
03-11 21:24:07.156: ERROR/PatientDataUploader(1093): at
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl.createSocket
(OpenSSLSocketFactoryImpl.java:65)


The server test program I am using is a modified version of
SslReverseEchoer available on Dr. Herong Yang's website. It looks like
below:

public static void main(String[] args) {
if (args.length < 3) {
System.out.println("Usage:");
System.out
.println(" java
SslReverseEchoerRevised ksName ksPass ctPass
ipAddr");



SSL client authentication

by telematics » Thu, 12 Mar 2009 13:59:14 GMT


 ever mind. I figured it out by my own - add the following to the
manifest.

<uses-permission android:name="android.permission.INTERNET" />

On Mar 11, 4:40pm, telematics <hai.huan...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. constructor AssetManager() is not visible???

hi,

AssetManager assetManager = new AssetManager();

the constructor of this AssetManager(); is not visible in SDK 1.0...
can anyone show me how to create AssetManager with the path I
defined???


wesley.
--~--~---------~--~----~------------~-------~--~----~

2. How to pass custom attributes via XML Resource.

Hi
First of all,
I followed this steps

1) I made an activity class and a customized button class which
inherits Button class
2) I made an attrs.xml(in res/values) I declare the custom xml
parameters for my customButton
Here is a code :
================================
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="MyImageButton">

</declare-styleable>
        <attr name="Focused"/>
        <attr name="NotFocused"/>
</resources>
================================
3) in customized-Button class I implement a constructor which inflate
the view based on the xml parameters and retrieve the custom
parameter,
4) and in layout I use the custom parameter as
"app:<custom_parameter>="..."
Here is a code
================================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
                 xmlns:app="http://schemas.android.com/apk/res/
com.android.demo.componentdemo"
<com.android.demo.componentdemo.MyImageButton
android:id="@+id/image_button"

android:src="@drawable/help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
app:NotFocused="@drawable/help"          <===Here!!!!!!!!!!!
app:Focused="@drawable/help_focused"
/>
</LinearLayout>
================================
And I checked R.java file out and there was "attr" and  "styleable" as
I expected.

5) In the customized-Button class I made init method for getting
AttributeSet (of course I got attributeset from constructor)

Here is init code
================================
private void init(Context context, AttributeSet attrs) {
                TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MyImageButton);


=== If you see ApiDemo there is a LabelView there is a this code
 CharSequence s = a.getString(R.styleable.LabelView_text);

=============================================
But I can't find anything like R.styleable.MyImageButton_focused

just there is a R.styleable.MyImageButton

did i miss some code in Xml or somewhere else ?






--~--~---------~--~----~------------~-------~--~----~

3. OpenGL ES documentation/tutorial suggestions

4. Dutch developer community.

5. Google Map Usage

6. Poll : .apk MIME Type

7. How to enable getChildStaticTransformation in 1.0_r1