Page 1 of 3 123 LastLast
Results 1 to 20 of 46
  1. #1
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default [SOLVED] Start SSL socket

    Hello, I am a begginer in programimg and I programmed the chat with SSL sockets. I created the certificate via tutorial on this website: Installing and Configuring SSL Support . I use IDE NetBeans and I would like to know the place/folder where the certificate has to be saved. Could you tell me in detail the procedure in Netbeans.
    But momentarily it doesn't work because of the error: javax.net.ssl.SSLException: No available certificate or key corresponds to the SSL cipher suites which are enabled.
    This is the code of server:
    Java Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.net.ssl.SSLServerSocket;
    import javax.net.ssl.SSLServerSocketFactory;
    import javax.net.ssl.SSLSocket;
    
    /**
     *
     * @author Lolek
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            try {
                int port = 5000;
                SSLServerSocketFactory serverSocketFactory = null;
                SSLServerSocket serverSocket = null;
                SSLSocket socket = null;
    
                serverSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
                serverSocket = (SSLServerSocket) serverSocketFactory.createServerSocket(port);
                
                
                while (true) {
                    socket = (SSLSocket) serverSocket.accept();
                    ChatHandler handler = new ChatHandler(socket);
                    handler.start();
                }
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            
          
          
        }
    }
    class ChatHandler extends Thread
    {
         static ArrayList handlers = new ArrayList(10);
         private SSLSocket socket;
         private BufferedReader read;
         private BufferedWriter write;
         
         public ChatHandler(SSLSocket socket)
         {
            try {
                this.socket = socket;
                this.read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                this.write = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            } catch (IOException ex) {
                Logger.getLogger(ChatHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
         }
         
        
         public void run(){
         String line=null;
    	synchronized(handlers) {
    	    handlers.add(this);
    	}
    	try {
               
    	    while(!(line = read.readLine()).equalsIgnoreCase("/q")) {            
                    for(int i = 0; i < handlers.size(); i++) {	
    			synchronized(handlers) {
    		            ChatHandler handler = (ChatHandler)handlers.get(i);                            
                                handler.write.write(line);
                                handler.write.newLine();
                                handler.write.flush();                            
                        }
    		}
    	    }
                
    	} catch(IOException ioe) {
    	    ioe.printStackTrace();
    	} finally {
    	    try {
    		read.close();
    		write.close();
    		socket.close();
    	    } catch(IOException ioe) {
    	    } finally {
    		synchronized(handlers) {
    		    handlers.remove(this);
    		}
    	    }
    	}
        }
    }

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You need to set up an SSLContext with your keystore, and use that to create get an SSLServerSocketFactory. The keystore goes wherever you can access it from your code.
    Last edited by OrangeDog; 05-18-2009 at 03:40 PM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    And could you show some example how can i set up an SSLContext? I am begginer I don´t know what are you talking about. I need this just to the school. Thank you

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Java Code:
    SSLContext ctx = null;
    // load keystore
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    char[] password = getPassword();
    InputStream in  = ClassLoader.getSystemResourceAsStream(keystore);
    ks.load(in, password);
    in.close();
    // set-up SSLContext
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509", "SunJSSE");
    kmf.init(ks, password);
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX", "SunJSSE");
    tmf.init(ks);
    ctx = SSLContext.getInstance("TLS");
    ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default


    I did how you wrote but something is wrong where is the problem?

  6. #6
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    The getPassword() refered to is an example, you must replace getPassword() with a password OR write a getPassword() method as follows:

    Java Code:
    private static char[] getPassword()
    {
        // this is a bad example but you get the point
        return("My Password".toCharArray());
    }

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Likewise, replace keystore with a path to your keystore
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  8. #8
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    OK where is the problem now?

    Thank you very much.

  9. #9
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    What's the argument type for getSystemResourceAsStream?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  10. #10
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    :) I am sory I am stupid. String "" . Thank you.
    But nullpointer:
    Last edited by Koren3; 05-07-2009 at 06:29 PM.

  11. #11
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    It's a lot easier to not use IDE screenshots.

    The class loader couldn't find that as it's not relative to the classpath. Use some other method to get the InputStream.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  12. #12
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    What other metod can i use?

  13. #13
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    As you have done, but with a correct path
    Using a URL
    Using a File
    Using a Socket
    and a few others
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  14. #14
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    I used this:
    Java Code:
     ObjectInputStream in = new ObjectInputStream(new FileInputStream("C:\\Koren\\Java\\SSLCentralServer\\src\\sslcentralserver\\keystore"));
    But it write this error: java.io.StreamCorruptedException: invalid stream header: FEEDFEED

  15. #15
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Don't use an Object stream.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  16. #16
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    OK but what can I use? If I use
    Java Code:
    InputStream in = (new FileInputStream("C:\\Koren\\Java\\SSLCentralServer\\src\\sslcentralserver\\keystore"));
    This error write: javax.net.ssl.SSLException: No available certificate or key corresponds to the SSL cipher suites which are enabled.

  17. #17
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Then you need to change the values in the getInstance() methods to match the format of your keystore, or change the keystore to match the available SSL cipher suites.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  18. #18
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Default

    Could you show some example? Thank you.

  19. #19
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    No, because I don't know the format of your keystore.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  20. #20
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

Page 1 of 3 123 LastLast

Similar Threads

  1. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 08:02 PM
  2. help about Socket
    By fahien_akim in forum New To Java
    Replies: 0
    Last Post: 04-16-2009, 11:41 AM
  3. Replies: 5
    Last Post: 03-20-2009, 11:04 PM
  4. Socket
    By rob in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 03:24 PM
  5. Socket
    By vortex in forum New To Java
    Replies: 2
    Last Post: 05-25-2008, 07:41 AM

Posting Permissions

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