+ Reply to Thread
Results 1 to 2 of 2

Thread: NoSuchAlgorithException

  1. #1

    NoSuchAlgorithException

    I'm working on a project that I want to add SSL to, so I created a simple client/server test implementation to see if it worked and I get a NoSuchAlgorithmException. The following is my server code which is throwing the exception:
    Code:
    import java.io.*;
    import java.net.*;
    import java.security.*;
    import javax.net.ssl.*;
    
    public class SslServer
    {
        private static final int PORT = 5555;
    
        public static void main(String[] args)
        {
            SecureRandom sr = new SecureRandom();
            sr.nextInt();
    
            try {
                //client.public is the keystore file that holds the client's public key (created with keytool)
                KeyStore clientKeyStore = KeyStore.getInstance("JKS");
                clientKeyStore.load(new FileInputStream("client.public"), "clientpublicpw".toCharArray());
    
                //server.private is the key pair for the server (created with keytool)
                KeyStore serverKeyStore = KeyStore.getInstance("JKS");
                clientKeyStore.load(new FileInputStream("server.private"), "serverprivatepw".toCharArray());
    
                TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
                tmf.init(clientKeyStore);
    
                //This next line is where the exception occurs
                KeyManagerFactory kmf = KeyManagerFactory.getInstance("TLS");
                kmf.init(serverKeyStore, "serverprivatepw".toCharArray());
    
                SSLContext sslContext = SSLContext.getInstance("TLS");
                sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), sr);
    
                SSLServerSocketFactory sf = sslContext.getServerSocketFactory();
                SSLServerSocket ss = (SSLServerSocket)sf.createServerSocket(SslServer.PORT);
                ss.setNeedClientAuth(true);
    
                BufferedReader in = new BufferedReader(new InputStreamReader(ss.accept().getInputStream()));
    
                String line = null;
                while((line = in.readLine()) != null)
                {
                    System.out.println(line);
                }
                in.close();
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (CertificateException e) {
                e.printStackTrace();
            } catch (KeyStoreException e) {
                e.printStackTrace();
            } catch (UnrecoverableKeyException e) {
                e.printStackTrace();
            } catch (KeyManagementException e) {
                e.printStackTrace();
            }
        }
    
    }
    The error I get is:
    Code:
    java.security.NoSuchAlgorithmException: TLS KeyManagerFactory not available
        at sun.security.jca.GetInstance.getInstance(Unknown Source)
        at javax.net.ssl.KeyManagerFactory.getInstance(Unknown Source)
        at SslServer.main(SslServer.java:32)

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    45
    Firstly it is called : TLS , anyway the code should look like that :
    Code:
    KeyStore trustStore = KeyStore.getInstance("JKS");
    InputStream tsis = new FileInputStream("trustedcerts.jks");
    trustStore.load(tsis, "clientpublicpw".toCharArray());
    tsis.close();
    
    KeyStore serverKeyStore = KeyStore.getInstance("JKS");
    InputStream ksis = new FileInputStream("server.jks");
    clientKeyStore.load(ksis.close(), "serverprivatepw".toCharArray());
    ksis.close();
    
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(trustStore);
    
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(serverKeyStore, "serverprivatepw".toCharArray());
    
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    
    SSLServerSocketFactory sf = sslContext.getServerSocketFactory();
    SSLServerSocket ss = (SSLServerSocket)sf.createServerSocket(SslServer.PORT);
    ss.setNeedClientAuth(true);

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts