Exemplo n.º 1
0
    public emsSSLSampleClient(string[] args)
    {
        if (topicName == null)
        {
            System.Console.WriteLine("Error: must specify topic name");
        }

        System.Console.WriteLine("Global SSL parameters sample with Microsoft Cerrtificate Store.");
        try
        {
            // System Store Info object to be used while setting the store type for a connection factory via the
            // ConnectionFactory.SetCertificateStoreType. The store info consists of the store location, store name,
            // the certificate name (to look for in the specified store name at the specified store location).
            // The default store location is StoreLocation.CurrentUser and the default store name is 'my' store as defined by the .NET framework.
            // The search criteria to find the certificate in the store name at the store location is X509FindType.FindBySubjectDistinguishedName.
            EMSSSLSystemStoreInfo storeInfo = new EMSSSLSystemStoreInfo();

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

            // Set the target host name.
            // This is a required parameter for all.NET SSL connections.Because System.Net.Security.SslStream
            // requires a target host, this value is required.
            //
            // The name of the server as defined in the server's certificate. Usually the server's HostName
            // is specified as the CN in the server's certificate. This value must match the name on the
            // server's certificate server name.
            if (ssl_target_hostname != null)
            {
                EMSSSL.SetTargetHostName(ssl_target_hostname);
            }

            // Set location of the certificate store
            // The certificate store location indicates where to lookup the certificate by name. If no store name is specified,
            // then the default store name is "My" store name within this store location.
            // storeLocation	Location in which to lookup certificate by name. For example, "CurrentUser" or "LocalMachine."
            if (ssl_cert_store_location != null)
            {
                if (ssl_cert_store_location.Equals("currentuser"))
                {
                    storeInfo.SetCertificateStoreLocation(StoreLocation.CurrentUser);
                }
                else if (ssl_cert_store_location.Equals("localmachine"))
                {
                    storeInfo.SetCertificateStoreLocation(StoreLocation.LocalMachine);
                }
            }

            // Set the certificate store name
            // This is the name of the store in which certificates are stored. During the SSL handshake,
            // this is where the client library looks for the certificates.
            if (ssl_cert_store_name != null)
            {
                storeInfo.SetCertificateStoreName(ssl_cert_store_name);
            }

            // Set the name of the certificate as a full subject DN
            // This method sets the name of the certificate. The certificate name is the subject distinguished name of the certificate.
            // During the SSL handshake, the server searches for the named certificate in the store specified by SetCertificateStoreName
            // at the location specified by SetCertificateStoreLocation. The search criteria to find the certificate in the store name
            // at the store location is X509FindType.FindBySubjectDistinguishedName.
            // A subject DN sample
            //      [email protected], CN=client, OU=client Unit, O=Test Company, L=us-english, S=California, C=US
            if (ssl_cert_name != null)
            {
                storeInfo.SetCertificateNameAsFullSubjectDN(ssl_cert_name);
            }

            // Set the store type for ALL the connection factories
            // If the store type is EMSSSL_STORE_TYPE_SYSTEM, then storeInfo must be an EMSSSLSystemStoreInfo object. If the store type is
            // EMSSSL_STORE_TYPE_FILE, then storeInfo must be an EMSSSLFileStoreInfo object.
            // The type of certificate store. Can be either EMSSSL_STORE_TYPE_SYSTEM or EMSSSL_STORE_TYPE_FILE. See EMSSSLStoreType details.
            EMSSSL.SetCertificateStoreType(EMSSSLStoreType.EMSSSL_STORE_TYPE_SYSTEM, 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);
                }
            }
        }

        try
        {
            ConnectionFactory factory = new ConnectionFactory(serverUrl);

            //How can I wrap this to ignore self signed certs mistrust
            // this does not work for our issues....
            ServicePointManager.ServerCertificateValidationCallback = delegate(
                object obj, X509Certificate certificate, X509Chain chain,
                System.Net.Security.SslPolicyErrors errors)
            {
                return(true);
            };

            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);
            }
        }
    }
Exemplo n.º 2
0
    public emsSSLSampleClient(string[] args)
    {
        if (topicName == null)
        {
            System.Console.WriteLine("Error: must specify topic name");
            usage();
        }

        System.Console.WriteLine("Global SSL parameters sample with Microsoft Certificate Store.");

        try
        {
            EMSSSLSystemStoreInfo storeInfo = new EMSSSLSystemStoreInfo();
            //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 target host name in the sertificate if specified
            if (ssl_target_hostname != null)
            {
                EMSSSL.SetTargetHostName(ssl_target_hostname);
            }

            if (ssl_cert_store_location != null)
            {
                if (ssl_cert_store_location.Equals("currentuser"))
                {
                    storeInfo.SetCertificateStoreLocation(StoreLocation.CurrentUser);
                }
                else if (ssl_cert_store_location.Equals("localmachine"))
                {
                    storeInfo.SetCertificateStoreLocation(StoreLocation.LocalMachine);
                }
            }

            if (ssl_cert_store_name != null)
            {
                storeInfo.SetCertificateStoreName(ssl_cert_store_name);
            }
            if (ssl_cert_name != null)
            {
                storeInfo.SetCertificateNameAsFullSubjectDN(ssl_cert_name);
            }

            EMSSSL.SetCertificateStoreType(EMSSSLStoreType.EMSSSL_STORE_TYPE_SYSTEM, 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);
            //factory.SetSSLTrace(true);
            factory.SetConnAttemptCount(1);
            factory.SetConnAttemptDelay(1000);
            factory.SetConnAttemptTimeout(1000);

            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 Connect:" + 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);
        }
    }