Ejemplo n.º 1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static void main(String[] paramArrayOfString) throws Exception
        public static void Main(string[] paramArrayOfString)
        {
            bool @bool;

            char[] arrayOfChar;
            char   c;
            string str1;

            if (paramArrayOfString.Length == 1 || paramArrayOfString.Length == 2)
            {
                string[] arrayOfString = paramArrayOfString[0].Split(":", true);
                str1 = arrayOfString[0];
                c    = (arrayOfString.Length == 1) ? (char)443 : (char)int.Parse(arrayOfString[1]);
                string str = (paramArrayOfString.Length == 1) ? "changeit" : paramArrayOfString[1];
                arrayOfChar = str.ToCharArray();
            }
            else
            {
                Console.WriteLine("Usage: java InstallCert [:port] [passphrase]");
                return;
            }
            File file = new File("jssecacerts");

            if (!file.File)
            {
                char c1    = Path.DirectorySeparatorChar;
                File file1 = new File(System.getProperty("java.home") + c1 + "lib" + c1 + "security");
                file = new File(file1, "jssecacerts");
                if (!file.File)
                {
                    file = new File(file1, "cacerts");
                }
            }
            Console.WriteLine("Loading KeyStore " + file + "...");
            FileStream fileInputStream = new FileStream(file, FileMode.Open, FileAccess.Read);
            KeyStore   keyStore        = KeyStore.getInstance(KeyStore.DefaultType);

            keyStore.load(fileInputStream, arrayOfChar);
            fileInputStream.Close();
            SSLContext          sSLContext          = SSLContext.getInstance("TLS");
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.DefaultAlgorithm);

            trustManagerFactory.init(keyStore);
            X509TrustManager   x509TrustManager   = (X509TrustManager)trustManagerFactory.TrustManagers[0];
            SavingTrustManager savingTrustManager = new SavingTrustManager(x509TrustManager);

            sSLContext.init(null, new TrustManager[] { savingTrustManager }, null);
            SSLSocketFactory sSLSocketFactory = sSLContext.SocketFactory;

            Console.WriteLine("Opening connection to " + str1 + ":" + c + "...");
            SSLSocket sSLSocket = (SSLSocket)sSLSocketFactory.createSocket(str1, c);

            sSLSocket.SoTimeout = 10000;
            try
            {
                Console.WriteLine("Starting SSL handshake...");
                sSLSocket.startHandshake();
                sSLSocket.close();
                Console.WriteLine();
                Console.WriteLine("No errors, certificate is already trusted");
            }
            catch (SSLException sSLException)
            {
                Console.WriteLine();
                sSLException.printStackTrace(System.out);
            }
            X509Certificate[] arrayOfX509Certificate = savingTrustManager.chain;
            if (arrayOfX509Certificate == null)
            {
                Console.WriteLine("Could not obtain server certificate chain");
                return;
            }
            StreamReader bufferedReader = new StreamReader(System.in);

            Console.WriteLine();
            Console.WriteLine("Server sent " + arrayOfX509Certificate.Length + " certificate(s):");
            Console.WriteLine();
            MessageDigest messageDigest1;
            MessageDigest messageDigest2 = (messageDigest1 = MessageDigest.getInstance("SHA1")).getInstance("MD5");

            for (sbyte b = 0; b < arrayOfX509Certificate.Length; b++)
            {
                X509Certificate x509Certificate1 = arrayOfX509Certificate[b];
                Console.WriteLine(" " + (b + true) + " Subject " + x509Certificate1.SubjectDN);
                Console.WriteLine("   Issuer  " + x509Certificate1.IssuerDN);
                messageDigest1.update(x509Certificate1.Encoded);
                Console.WriteLine("   sha1    " + toHexString(messageDigest1.digest()));
                messageDigest2.update(x509Certificate1.Encoded);
                Console.WriteLine("   md5     " + toHexString(messageDigest2.digest()));
                Console.WriteLine();
            }
            Console.WriteLine("Enter certificate to add to trusted keystore or 'q' to quit: [1]");
            string str2 = bufferedReader.ReadLine().Trim();

            try
            {
                @bool = (str2.Length == 0) ? 0 : (int.Parse(str2) - 1);
            }
            catch (System.FormatException)
            {
                Console.WriteLine("KeyStore not changed");
                return;
            }
            X509Certificate x509Certificate = arrayOfX509Certificate[@bool];
            string          str3            = str1 + "-" + (@bool + true);

            keyStore.setCertificateEntry(str3, x509Certificate);
            FileStream fileOutputStream = new FileStream("jssecacerts", FileMode.Create, FileAccess.Write);

            keyStore.store(fileOutputStream, arrayOfChar);
            fileOutputStream.Close();
            Console.WriteLine();
            Console.WriteLine(x509Certificate);
            Console.WriteLine();
            Console.WriteLine("Added certificate to keystore 'jssecacerts' using alias '" + str3 + "'");
        }
Ejemplo n.º 2
0
        public GHSocket ChangeToSSL(EndPoint remote_end)
        {
            if (jSocket == null)
            {
                throw new InvalidOperationException("The underlying socket is null");
            }

            if (!jSocketChannel.isBlocking())
            {
                throw new NotImplementedException("The SSL Socket for non-blocking mode is not supported");
            }

            SSLSocketFactory factory = getSSLSocketFactory();

            if (factory == null)
            {
                throw new ApplicationException("Can't get SSL Socket Factory");
            }

            int err;

            // The problem with local address, when I closed the socket and try to create the new one
            // bounded to the given local address, I receive exception "Address already in use"
            IPEndPoint localEndPoint = null;
//			IPEndPoint localEndPoint = (IPEndPoint) LocalEndPoint_internal(out err);
//			if (err != 0)
//				localEndPoint = null;

            IPEndPoint remoteEndPoint = remote_end as IPEndPoint;

            if (remoteEndPoint == null)
            {
                remoteEndPoint = (IPEndPoint)RemoteEndPoint_internal(out err);
                if (err != 0)
                {
                    remoteEndPoint = null;
                }
            }

            java.net.Socket sslSocket = null;
            try
            {
                if (remoteEndPoint != null)
                {
                    if (localEndPoint != null)
                    {
                        sslSocket = factory.createSocket(
                            java.net.InetAddress.getByName(remoteEndPoint.Address.ToString()),
                            remoteEndPoint.Port,
                            java.net.InetAddress.getByName(localEndPoint.Address.ToString()),
                            localEndPoint.Port);
                    }
                    else
                    {
                        sslSocket = factory.createSocket(
                            jSocket,
                            remoteEndPoint.Address.ToString(),
                            remoteEndPoint.Port,
                            false);
                    }

                    if (sslSocket != null)
                    {
                        String[] protocols = { "TLSv1", "SSLv3" };
                        ((SSLSocket)sslSocket).setUseClientMode(true);
                        ((SSLSocket)sslSocket).startHandshake();
                    }
                }
                else
                {
                    sslSocket = factory.createSocket();
                }
            }
            catch (Exception e)
            {
                sslSocket = null;
#if DEBUG
                Console.WriteLine("Can't create SSL Socket, the exception is {0}, {1}", e.GetType(), e.Message);
#endif
            }

            if (sslSocket == null)
            {
//				throw new ApplicationException("Can't create SSL Socket");
                // it is important to the Socket class to distinguish if the underlying
                // handle (GHSocket) is still valid and can be used as non-SSL, or it is already
                // closed by this function and can't be used any more.
                return(null);
            }

/*
 *                      string[] arr = ((SSLSocket)sslSocket).getEnabledProtocols();
 *                      if (arr != null)
 *                      {
 *                              foreach (string s in arr)
 *                                      Console.WriteLine("s:"+s);
 *                      }
 *                      string [] arr1 = ((SSLSocket)sslSocket).getEnabledCipherSuites();
 *                      if (arr1 != null)
 *                      {
 *                              foreach (string s in arr1)
 *                                      Console.WriteLine("s:"+s);
 *                      }
 */

            return(new GHStreamSocketSSL(sslSocket));
        }