コード例 #1
0
        public void init()
        {
            EMSSSLFileStoreInfo storeInfo = new EMSSSLFileStoreInfo();

            if (ssl_trace)
            {
                EMSSSL.SetClientTracer(new System.IO.StreamWriter(System.Console.OpenStandardOutput()));
            }

            if (ssl_target_hostname != null)
            {
                EMSSSL.SetTargetHostName(ssl_target_hostname);
            }

            if (ssl_custom)
            {
                HostVerifier v = new HostVerifier();
                EMSSSL.SetHostNameVerifier(new EMSSSLHostNameVerifier(v.verifyHost));
            }

            if (ssl_trusted != null)
            {
                for (int i = 0; i < ssl_trusted.Count; i++)
                {
                    String certfile = (String)ssl_trusted[i];
                    storeInfo.SetSSLTrustedCertificate(certfile);
                }
            }

            if (ssl_identity != null)
            {
                storeInfo.SetSSLClientIdentity(ssl_identity);
                storeInfo.SetSSLPassword(ssl_password.ToCharArray());
            }

            EMSSSL.SetCertificateStoreType(EMSSSLStoreType.EMSSSL_STORE_TYPE_FILE, storeInfo);
        }
コード例 #2
0
    public csLookupSSL(String[] args)
    {
        parseArgs(args);
        if (providerUrl == null)
        {
            providerUrl = defaultProviderURL;
        }

        // Print parameters
        Console.WriteLine("\n------------------------------------------------------------------------");
        Console.WriteLine("csLookupSSL SAMPLE");
        Console.WriteLine("------------------------------------------------------------------------");
        Console.WriteLine("Server....................... " + providerUrl);
        Console.WriteLine("------------------------------------------------------------------------\n");

        try
        {
            Hashtable env = new Hashtable();

            env.Add(LookupContext.PROVIDER_URL, providerUrl);
            if (userName != null)
            {
                env.Add(LookupContext.SECURITY_PRINCIPAL, userName);
                if (password != null)
                {
                    env.Add(LookupContext.SECURITY_CREDENTIALS, password);
                }
            }

            // Creating the SSL related file store info object.
            EMSSSLFileStoreInfo info = new EMSSSLFileStoreInfo();
            if (ssl_identity != null)
            {
                info.SetSSLClientIdentity(ssl_identity);
            }

            if (ssl_password != null)
            {
                string _sslpassword = ssl_password;
                info.SetSSLPassword(_sslpassword.ToCharArray());
            }

            if (ssl_target_hostname == null)
            {
                Console.WriteLine("ssl_target_hostname is required parameter");
                usage();
            }

            // Setting up the SSL properties for the lookup context.
            env.Add(LookupContext.SSL_STORE_INFO, info);
            env.Add(LookupContext.SSL_STORE_TYPE, EMSSSLStoreType.EMSSSL_STORE_TYPE_FILE);
            env.Add(LookupContext.SECURITY_PROTOCOL, "ssl");
            env.Add(LookupContext.SSL_TARGET_HOST_NAME, ssl_target_hostname);

            LookupContext lcxt = new LookupContext(env);

            // Lookup connection factory which must exist in the factories config file.

            ConnectionFactory factory    = null;
            ConnectionFactory sslFactory = null;

            try
            {
                factory = (ConnectionFactory)lcxt.Lookup("ConnectionFactory");
                Console.WriteLine("OK - successfully did lookup ConnectionFactory");

                // Lookup SSL connection factory which must exist in the
                // factories config file.
                sslFactory = (ConnectionFactory)lcxt.Lookup("SSLConnectionFactory");
                Console.WriteLine("OK - successfully did lookup SSLConnectionFactory");

                // Setting additional parameters for this SSL factory.
                sslFactory.SetTargetHostName(ssl_target_hostname);

                // Also, if users want to set any other SSL parameters,
                // i.e EMSSSLFileStoreInfo parameters, they can call GetCertificateStore
                // on the connection factory object.
                EMSSSLFileStoreInfo fileStoreInfo = (EMSSSLFileStoreInfo)sslFactory.GetCertificateStore();
                fileStoreInfo.SetSSLClientIdentity(ssl_identity);
                string _sslpassword = ssl_password;
                fileStoreInfo.SetSSLPassword(_sslpassword.ToCharArray());
            }
            catch (EMSException e)
            {
                Console.Error.WriteLine("**EMSException occurred while doing a lookup");
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine(e.StackTrace);
                Environment.Exit(0); // Can't lookup anything anyway
            }

            // Let's create a connection to the server and a session to verify
            // that the server is running.
            Connection sslConnection = null;
            Session    sslSession    = null;

            try
            {
                sslConnection = sslFactory.CreateConnection(userName, password);
                sslSession    = sslConnection.CreateSession(false, Session.AUTO_ACKNOWLEDGE);
                sslConnection.Close();
            }
            catch (EMSException e)
            {
                Console.Error.WriteLine("**EMSException occurred while creating SSL connection");
                Console.Error.WriteLine("  Most likely the server is not running, the exception trace follows:");
                Console.Error.WriteLine(e.StackTrace);
                Environment.Exit(0); // Can't lookup anything anyway
            }

            // Let's create a connection to the server and a session to verify
            // that the server is running so we can continue with our lookup operations.
            Connection connection = null;
            Session    session    = null;

            try
            {
                connection = factory.CreateConnection(userName, password);
                session    = connection.CreateSession(false, Session.AUTO_ACKNOWLEDGE);
            }
            catch (EMSException e)
            {
                Console.Error.WriteLine("**EMSException occurred while creating connection");
                Console.Error.WriteLine("  Most likely the server is not running, the exception trace follows:");
                Console.Error.WriteLine(e.StackTrace);
                Environment.Exit(0); // Can't lookup anything anyway
            }

            // Lookup topic 'topic.sample' which must exist in the topics
            // configuration file; if not successfull then most likely such
            // topic does not exist in the config file.
            Topic sampleTopic = null;
            try
            {
                sampleTopic = (Topic)lcxt.Lookup("topic.sample");
                Console.WriteLine("OK - successfully did lookup topic 'topic.sample'");
            }
            catch (NamingException ne)
            {
                Console.Error.WriteLine(ne.StackTrace);
                Console.Error.WriteLine("**NamingException occurred while lookup the topic topic.sample");
                Console.Error.WriteLine("  Most likely such topic does not exist in your configuration.");
                Console.Error.WriteLine("  Exception message follows:");
                Console.Error.WriteLine(ne.Message);
                Environment.Exit(0);
            }

            // Lookup queue 'queue.sample' which must exist in the queues
            // configuration file; if not successfull then it does not exist.
            TIBCO.EMS.Queue sampleQueue = null;
            try
            {
                sampleQueue = (TIBCO.EMS.Queue)lcxt.Lookup("queue.sample");
                Console.WriteLine("OK - successfully did lookup queue 'queue.sample'");
            }
            catch (NamingException ne)
            {
                Console.Error.WriteLine("**NamingException occurred while lookup the queue queue.sample");
                Console.Error.WriteLine("  Most likely such topic does not exist in your configuration.");
                Console.Error.WriteLine("  Exception message follows:");
                Console.Error.WriteLine(ne.Message);
                Environment.Exit(0);
            }

            // Try to lookup a topic when a topic *and* a queue with the
            // same name exist in the configuration. The sample configuration
            // has both topic and queue with the name 'sample' used here. If
            // everything is OK, we will get an exception, then try to lookup
            // those objects using fully-qualified names.
            try
            {
                Object object_Renamed = lcxt.Lookup("sample");

                // If the sample config was not altered, we should not get to this
                // line because lookup() should throw an exception.
                Console.WriteLine("Object named 'sample' has been looked up as: " + object_Renamed);
            }
            catch (NamingException ne)
            {
                Console.Error.WriteLine("OK - NamingException occurred while lookup the object named 'sample'");
                Console.Error.WriteLine("     This is the CORRECT BEHAVIOUR if the exception message below");
                Console.Error.WriteLine("     specifies name conflict situation. Exception message follows:");
                Console.Error.WriteLine("     " + ne.Message);

                // Let's look them up using name qualifiers.
                try
                {
                    Topic t = (Topic)lcxt.Lookup("$topics.sample");
                    Console.WriteLine("OK - successfully did lookup topic '$topics.sample'");
                    TIBCO.EMS.Queue q = (TIBCO.EMS.Queue)lcxt.Lookup("$queues.sample");
                    Console.WriteLine("OK - successfully did lookup queue '$queues.sample'");
                }
                catch (NamingException nex)
                {
                    // These may not be configured: Print a warning and continue.
                    Console.Error.WriteLine("**NamingException occurred while lookup the topic or queue 'sample'");
                    Console.Error.WriteLine("  Exception message follows:");
                    Console.Error.WriteLine("  " + nex.Message);
                }
            }

            // Now let's try to lookup a topic which definitely does not
            // exist. A lookup() operation will throw an exception and then we
            // will try to create such a topic explicitly with the createTopic()
            // method.
            String dynamicName  = "topic.does.not.exist." + (DateTime.Now.Ticks - 621355968000000000) / 10000;
            Topic  dynamicTopic = null;
            try
            {
                dynamicTopic = (Topic)lcxt.Lookup(dynamicName);

                // If the sample config was not altered, we should not get to
                // this line because lookup() should throw an exception.
                Console.Error.WriteLine("**Error: dynamic topic " + dynamicName + " found by lookup");
            }
            catch (NamingException)
            {
                // This is OK.
                Console.Error.WriteLine("OK - could not lookup dynamic topic " + dynamicName);
            }

            // Try to create it.
            try
            {
                dynamicTopic = session.CreateTopic(dynamicName);
                Console.WriteLine("OK - successfully created dynamic topic " + dynamicName);
            }
            catch (EMSException e)
            {
                // A few reasons are possible here. Maybe there is no > entry
                // in the topics configuration or the server security is
                // turned on. If the original sample configuration is used, this
                // should work.
                Console.Error.WriteLine("**EMSException occurred while creating topic " + dynamicName);
                Console.Error.WriteLine("  Please verify your topics configuration and security settings.");
                Console.Error.WriteLine(e.StackTrace);
                Environment.Exit(0);
            }

            connection.Close();

            Console.WriteLine("OK - Done.");
        }
        catch (EMSException e)
        {
            Console.Error.WriteLine(e.StackTrace);
            Environment.Exit(0);
        }
    }
コード例 #3
0
    public emsSSLGlobal(String[] args)
    {
        parseArgs(args);

        if (topicName == null)
        {
            System.Console.WriteLine("Error: must specify topic name");
            usage();
        }

        System.Console.WriteLine("Global SSL parameters sample.");

        try {
            EMSSSLFileStoreInfo storeInfo = new EMSSSLFileStoreInfo();

            // set trace for client-side operations, loading of certificates
            // and other
            if (ssl_trace)
            {
                EMSSSL.SetClientTracer(new System.IO.StreamWriter(System.Console.OpenStandardError()));
            }


            // set trusted certificates if specified
            int s = ssl_trusted.Count;
            for (int i = 0; i < s; i++)
            {
                String cert = (String)ssl_trusted[i];
                storeInfo.SetSSLTrustedCertificate(cert);
            }

            // set target host name in the sertificate if specified
            if (ssl_target_hostname != null)
            {
                EMSSSL.SetTargetHostName(ssl_target_hostname);
            }

            // only pkcs12 or pfx files are supported.
            if (ssl_identity != null)
            {
                if (ssl_password == null)
                {
                    System.Console.WriteLine("Error: must specify -ssl_password if identity is set");
                    System.Environment.Exit(-1);
                }
                storeInfo.SetSSLClientIdentity(ssl_identity);
                storeInfo.SetSSLPassword(ssl_password.ToCharArray());
            }

            EMSSSL.SetCertificateStoreType(EMSSSLStoreType.EMSSSL_STORE_TYPE_FILE, storeInfo);
        }
        catch (Exception e)
        {
            System.Console.WriteLine(e.StackTrace);

            if (e is EMSException)
            {
                EMSException je = (EMSException)e;

                if (je.LinkedException != null)
                {
                    System.Console.WriteLine("##### Linked Exception:");
                    System.Console.WriteLine(je.LinkedException.StackTrace);
                }
            }
            System.Environment.Exit(-1);
        }

        try
        {
            ConnectionFactory factory = new ConnectionFactory(serverUrl);

            Connection connection = factory.CreateConnection(userName, password);

            Session session = connection.CreateSession(false, TIBCO.EMS.SessionMode.AutoAcknowledge);

            Topic topic = session.CreateTopic(topicName);

            MessageProducer publisher  = session.CreateProducer(topic);
            MessageConsumer subscriber = session.CreateConsumer(topic);

            connection.Start();

            MapMessage message = session.CreateMapMessage();
            message.SetStringProperty("field", "SSL message");

            for (int i = 0; i < 3; i++)
            {
                publisher.Send(message);
                System.Console.WriteLine("\nPublished message: " + message);

                /* read same message back */
                message = (MapMessage)subscriber.Receive();
                if (message == null)
                {
                    System.Console.WriteLine("\nCould not receive message");
                }
                else
                {
                    System.Console.WriteLine("\nReceived message: " + message);
                }

                try {
                    System.Threading.Thread.Sleep(1000);
                }
                catch (Exception) {}
            }

            connection.Close();
        }
        catch (EMSException e)
        {
            System.Console.WriteLine("##### Exception:" + e.Message);
            System.Console.WriteLine(e.StackTrace);

            if (e.LinkedException != null)
            {
                System.Console.WriteLine("##### Linked Exception error msg:" + e.LinkedException.Message);
                System.Console.WriteLine("##### Linked Exception:");
                System.Console.WriteLine(e.LinkedException.StackTrace);
            }
            System.Environment.Exit(-1);
        }
    }
コード例 #4
0
ファイル: Program.cs プロジェクト: zyonet/tibcoems-tutorials
    string ssl_password        = null;          // password to decrypt client identity or key file {password}


    public tibemsSSL(string[] args)
    {
        ssl_trusted.Add("C:/tibco/ems/8.3/samples/certs/server_root.cert.pem");
        ssl_target_hostname = "localhost";
        ssl_identity        = "C:/tibco/ems/8.3/samples/certs/client_identity.p12";
        ssl_password        = "******";

        if (topicName == null)
        {
            System.Console.WriteLine("Error: must specify topic name");
            //usage();
        }


        if (topicName == null)
        {
            System.Console.WriteLine("Error: must specify topic name");
        }
        System.Console.WriteLine("SSL sample.");


        // initialize SSL environment
        Hashtable environment = new Hashtable();

        try
        {
            EMSSSLFileStoreInfo storeInfo = new EMSSSLFileStoreInfo();

            // set trace for client-side operations, loading of certificates
            // and other
            if (ssl_trace)
            {
                environment.Add(EMSSSL.TRACE, true);
            }

            // set trusted certificates if specified
            int s = ssl_trusted.Count;
            for (int i = 0; i < s; i++)
            {
                String cert = (String)ssl_trusted[i];
                storeInfo.SetSSLTrustedCertificate(cert);
            }

            // set expected host name in the certificate if specified
            if (ssl_target_hostname != null)
            {
                environment.Add(EMSSSL.TARGET_HOST_NAME, ssl_target_hostname);
            }

            // only pkcs12 or pfx files are supported.
            if (ssl_identity != null)
            {
                if (ssl_password == null)
                {
                    System.Console.WriteLine("Error: must specify -ssl_password if identity is set");
                    System.Environment.Exit(-1);
                }
                storeInfo.SetSSLClientIdentity(ssl_identity);
                storeInfo.SetSSLPassword(ssl_password.ToCharArray());
            }
            environment.Add(EMSSSL.STORE_INFO, storeInfo);
            environment.Add(EMSSSL.STORE_TYPE, EMSSSLStoreType.EMSSSL_STORE_TYPE_FILE);
        }
        catch (Exception e)
        {
            System.Console.WriteLine("ERROR MSG: " + e.Message);
            System.Console.WriteLine(e.StackTrace);

            if (e is EMSException)
            {
                EMSException je = (EMSException)e;
                if (je.LinkedException != null)
                {
                    System.Console.WriteLine("Linked Exception Error Msg: " + e.Message);
                    System.Console.WriteLine("##### Linked Exception:");
                    System.Console.WriteLine(je.LinkedException.StackTrace);
                }
            }
            //System.Environment.Exit(-1);
        }

        try
        {
            ConnectionFactory factory = new ConnectionFactory(serverUrl, null, environment);

            Connection connection = factory.CreateConnection(userName, password);

            Session session = connection.CreateSession(false, TIBCO.EMS.SessionMode.AutoAcknowledge);

            Topic topic = session.CreateTopic(topicName);

            MessageProducer publisher  = session.CreateProducer(topic);
            MessageConsumer subscriber = session.CreateConsumer(topic);

            connection.Start();

            MapMessage message = session.CreateMapMessage();
            message.SetStringProperty("field", "SSL message");

            for (int i = 0; i < 3; i++)
            {
                publisher.Send(message);
                System.Console.WriteLine("\nPublished message: " + message);

                /* read same message back */

                message = (MapMessage)subscriber.Receive();
                if (message == null)
                {
                    System.Console.WriteLine("\nCould not receive message");
                }
                else
                {
                    System.Console.WriteLine("\nReceived message: " + message);
                }

                try
                {
                    System.Threading.Thread.Sleep(1000);
                }
                catch (Exception) { }
            }

            connection.Close();
        }
        catch (EMSException e)
        {
            System.Console.WriteLine("ERROR MSG: " + e.Message);
            System.Console.WriteLine(e.StackTrace);

            if (e.LinkedException != null)
            {
                System.Console.WriteLine("Linked Exception Error Msg: " + e.Message);
                System.Console.WriteLine("##### Linked Exception:");
                System.Console.WriteLine(e.LinkedException.StackTrace);
            }
            //System.Environment.Exit(-1);
        }
    }