Search code examples
javaandroidssl-certificatebouncycastlekeystore

Illegal key size


I have already installed jce to allow bigger keys but both KeytoolUIU and Portecle are giving errors like java.IO.Exception: Error initialising store of key store: java.security.InvalidKeyException: Illegal Key Size. The key is only 1024 so I don't know why it's complaining.

Here's my current code for loading the key files and accessing the secure website.

package com.g4apps.secure.android.sslclient;

import java.io.InputStream;
import java.security.KeyStore;
import java.security.Security;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import android.content.Context;

/**
 * This example demonstrates how to create secure connections with a custom SSL
 * context.
 */
public class SSLclient {


    public final static String authenticate(Context context) throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        String output=null;

        Security.addProvider(new BouncyCastleProvider());
        try {
            KeyStore trustStore  = KeyStore.getInstance("BKS");
            InputStream instream = context.getResources().getAssets().open("my.truststore");
            try {
                trustStore.load(instream, "dysan100".toCharArray());
            } finally {
                try { instream.close(); } catch (Exception ignore) {}
            }
            KeyStore keystore = KeyStore.getInstance("BKS");
            InputStream keystream = context.getResources().getAssets().open("my.keystore.bks");
            try {
                keystore.load(keystream, "dysan100".toCharArray());
            } finally {
                try { keystream.close(); } catch (Exception ignore) {}
            }

            SSLSocketFactory socketFactory = new SSLSocketFactory(keystore,"dysan100",trustStore);
            socketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
            Scheme sch = new Scheme("https", socketFactory, 443);
            httpclient.getConnectionManager().getSchemeRegistry().register(sch);

            HttpGet httpget = new HttpGet("https://192.168.1.123/test.php");

            System.out.println("executing request" + httpget.getRequestLine());

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
                output=EntityUtils.toString(entity);
                System.out.println(output);
                return output;

            }


        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }

        return null;
    }

}

Currently the keystores are made up like so

my.truststore.bks has my CA certificate
my.keystore.bks is suppose to have my Server Certificate, Client Certificate and Client's Private Key.

That's the same way I had it setup in my pc version of the program (using JKS store instead though).

Since it's not letting me setup my stores like this is there another way that might work for me?


Solution

  • I am not sure why I am unable to create the bks keystore but I was able to get it to work with the PKCS12 keystore. So I have an alternative.

            KeyStore keystore = KeyStore.getInstance("PKCS12");
            InputStream keystream = context.getResources().getAssets().open("client.p12");
            try {
                keystore.load(keystream, "dysan100".toCharArray());
            } finally {
                try { keystream.close(); } catch (Exception ignore) {}
            }