Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #4041

SSL client program

From Stone <phracek2@gmail.com>
Newsgroups comp.lang.java.programmer
Subject SSL client program
Date 2011-05-13 01:09 -0700
Organization http://groups.google.com
Message-ID <3af63731-b09e-44ff-bf37-1ffebdf80f60@o7g2000vbn.googlegroups.com> (permalink)

Show all headers | View raw


Dear developers,

I am trying to write some client program which will open port 5000 on
the client side and connect to the computer where is run daemon which
listen on the port 5000.
Those port should be secured over SSL.
I have build up the C++ daemon which listen on that port together with
SSL and when I am writing
command:
openssl s_client -ssl3 -connect 192.168.0.120:9000
then in the log of daemon I can see that connection was establish and
working correctly.
Including server certificate, SSL handshake and Secure Renegotiation

I would like to created some client in Java but there I have some
problems.
When I run Java client application the in the daemon I see message:

24741:error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version
number:s3_pkt.c:295:

My Java code is:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ssltest;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.net.ssl.*;
import java.security.cert.*;
/**
 *
 */
public class SSLTest {

  private int port = 5000;
  private SSLSocketFactory sslSocketFactory;
  private SSLSocket connection;
  private SSLContext sslContext;
  private TrustManager[] trustManager;
  private PrintWriter outStream;
  private BufferedReader inStream;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Start");
        SSLTest e = new SSLTest();
    }

    public SSLTest()
    {
        System.out.println("Connecting to 192.168.0.120 to port
5000");
        connectTo();
    }
    private void initializeSSLContext() throws Exception {
        try {
            sslContext = SSLContext.getInstance("SSLv3");
            System.out.println("Contents with TLSv1 was initiated");
            sslContext.init(null, trustManager, new
java.security.SecureRandom());
            System.out.println("Contents with TLSv1 was initiated with
trustManager");
 
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
            HostnameVerifier hv = new HostnameVerifier() {
                public boolean verify(String string, SSLSession ssls)
                {
                    System.out.println("Warning: URL Host: "+string +
" vs. " + ssls.getPeerHost());
                    return true;
                }
            };
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            sslSocketFactory = sslContext.getSocketFactory();
            System.out.println("SSL Socket Factory is done");
        } catch (java.security.NoSuchAlgorithmException e) {
            e.printStackTrace(System.out);
            throw e;
        } catch (java.security.KeyManagementException e) {
            e.printStackTrace(System.out);
            throw e;
        }
    }
    private final void initializeTrustManager() throws Exception {
	// init new TrustManager
        trustManager = new TrustManager[] {
            new X509TrustManager()
            {
                public java.security.cert.X509Certificate[]
getAcceptedIssuers() {
                System.out.println("InitializeTrustManager:
getAcceptedIssuers:");
                return null;
                }

                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] certs,
                        String authType) {
                System.out.println("initializeTrustmanager:
checkClientTrusted:" + certs[0]
                        + " authTyp:" + authType);
                }

                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] certs,
String authType) {
                System.out.println("InitializeTrustManager:
checkServerTrusted:"
                        + certs[0].getIssuerDN() + " authTyp:" +
authType);

                }
                public boolean isClientTrusted(X509Certificate[] arg0)
                {
                    return true;
                }
                public boolean isServerTrusted(X509Certificate[] arg0)
                {
                    return true;
                }
            }
        };
    }
    public void connectTo()
    {
        try
        {
          System.out.println("Initialization of trust Manager");
          initializeTrustManager();
          System.out.println("Initialization of SSL Context");
          initializeSSLContext();
          // open a socket to the server
          connection =
(SSLSocket)sslSocketFactory.createSocket("192.168.0.120", port);
          //connection.setSSLParameters(null)
          //connection.startHandshake();
          //URL u = new URL("https://192.168.0.120:5000/");
          //HttpsURLConnection http = (HttpsURLConnection)
u.openConnection();

          //java.security.cert.Certificate[] serverCerts =
connection.getSession().getPeerCertificates();
          // open streams for reading and writing
          outStream = new PrintWriter(new OutputStreamWriter(
                      connection.getOutputStream()),true);

          inStream = new BufferedReader(new InputStreamReader(
                     connection.getInputStream()));
        }
        catch(Exception e)
        {
        }
    }
}

Those program is run from NetBeans directly

Thank you to all for your help

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar


Thread

SSL client program Stone <phracek2@gmail.com> - 2011-05-13 01:09 -0700
  Re: SSL client program Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-13 18:39 +0200
    Re: SSL client program Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-13 18:57 +0200
      Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-14 01:54 -0700
        Re: SSL client program Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-14 17:34 +0200
          Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-14 09:45 -0700
          Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-14 09:48 -0700
            Re: SSL client program Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-14 21:23 +0200
              Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-14 12:34 -0700
          Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-14 11:35 -0700
        Re: SSL client program Esmond Pitt <esmond.pitt@bigpond.com> - 2011-05-16 16:54 +1000
          Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-16 03:08 -0700
            Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-16 05:22 -0700
              Re: SSL client program Esmond Pitt <esmond.pitt@bigpond.com> - 2011-05-17 09:33 +1000
                Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-19 01:46 -0700
                Re: SSL client program Esmond Pitt <esmond.pitt@bigpond.com> - 2011-05-20 14:15 +1000
                Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-20 01:56 -0700
                Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-20 02:00 -0700
    Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-14 01:49 -0700
  Re: SSL client program Lothar Kimmeringer <news200709@kimmeringer.de> - 2011-05-15 13:52 +0200
    Re: SSL client program Stone <phracek2@gmail.com> - 2011-05-15 11:05 -0700
      Re: SSL client program Lothar Kimmeringer <news200709@kimmeringer.de> - 2011-05-16 08:46 +0200

csiph-web