Click here to Skip to main content
11,413,961 members (68,892 online)
Click here to Skip to main content
Add your own
alternative version

An Asynchronous Socket Server and Client

, 29 Apr 2009 CPOL
An asynchronous socket server and client with encryption and compression.
asyncsocketserverandclient.zip
Code
Demos
CertificateCreation
ca.crt
ca.key
cert.crt
cert.csr
cert.key
cert.p12
Chat
ChatClient
ChatClient.csproj.user
Properties
ChatConsoleServer
ChatConsoleServer.csproj.user
Properties
ChatCryptService
ChatCryptService.csproj.user
Properties
ChatServiceServer
ChatServiceServer.csproj.user
Properties
ChatSocketService
ChatSocketService.csproj.user
Properties
Echo
EchoConsoleClient
EchoConsoleClient.csproj.user
Properties
EchoConsoleServer
EchoConsoleServer.csproj.user
Properties
Service References
EchoCryptService
EchoCryptService.csproj.user
Properties
EchoForm
EchoFormTemplate.csproj.user
Properties
EchoFormClient
EchoFormClient.csproj.user
Properties
Settings.settings
EchoFormClientSynch
EchoFormClientSynch.csproj.user
Properties
EchoFormServer
EchoFormServer.csproj.user
frmEchoServer.cs~RFc68903.TMP
frmEchoServer.Designer.cs~RFc68932.TMP
frmEchoServer.Designer.cs~RFc68a3b.TMP
frmEchoServer.resx~RFc6898f.TMP
Properties
Settings.settings
EchoSocketService
EchoSocketService.csproj.user
Properties
EchoWindowsServiceServer
EchoWindowsServiceServer.csproj.user
Properties
Source
ALAZ.SystemEx
ALAZ.SystemEx.csproj.user
bin
Debug
Release
ALAZ.SystemEx.dll
ALAZ.SystemEx.pdb
Properties
RuntTimeEx
InteropServicesEx
ThreadingEx
ALAZ.SystemEx.NetEx
ALAZ.SystemEx.NetEx.csproj.user
ALAZLibSN.snk
bin
Debug
Release
ALAZ.SystemEx.dll
ALAZ.SystemEx.NetEx.dll
ALAZ.SystemEx.NetEx.pdb
ALAZ.SystemEx.pdb
Properties
SocketsEx
using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;

using ALAZ.SystemEx;
using ALAZ.SystemEx.NetEx.SocketsEx;

namespace EchoCryptService
{
    
    public class EchoCryptService : BaseCryptoService
    {

        #region OnSymmetricAuthenticate

        public override void OnSymmetricAuthenticate(ISocketConnection connection, out RSACryptoServiceProvider serverKey)
        {
            
            /*
             * A RSACryptoServiceProvider is needed to encrypt and send session key.
             * In server side you need public and private key to decrypt session key.
             * In client side tou need only public key to encrypt session key.
             * 
             * You can create a RSACryptoServiceProvider from 
             *      - a string (file, registry)
             *      - a certificate (from file)
             *      - a certificate (from certificate store)
             *      
             * The following certificate and instructions is in CertificateCreation folder.
            */

            /*
            //----- Using string!
            serverKey = new RSACryptoServiceProvider();

            if (connection.Host.HostType == HostType.htClient)
            {
                serverKey.FromXmlString("<RSAKeyValue><Modulus>z2ksxSTLHSBjY4+IEz7TZU5EclOql5pphA9+xyNQ6c1rYW6VPAmXmiXZKmsza8N++YVLAGnzR95iYyr4oL+mBz8lbhjDH2iqyQL7utbW1s87WaDC2o+82dLnLvwEqBhWpnz4tC0v0kCKayH6Jj+30l3xLdgDwReWF7YEvp6yq6nGxHOeSiioPpTtJzNhWjKGnK6oSZqthfWHewlRl2hVIrewD+JbP5JYTp/7iYptOiCwNAUZEBxODR2743D56J1AeHNc8VpZNvE3ZozIoRFhnxZw0ZpvMbgPliKPyjPeOvOFeqZUJ2zkQ7sH+gnqt67QzkOzznfuFPmTpBo0tMheyw==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");
            }
            else
            {
                serverKey.FromXmlString("<RSAKeyValue><Modulus>z2ksxSTLHSBjY4+IEz7TZU5EclOql5pphA9+xyNQ6c1rYW6VPAmXmiXZKmsza8N++YVLAGnzR95iYyr4oL+mBz8lbhjDH2iqyQL7utbW1s87WaDC2o+82dLnLvwEqBhWpnz4tC0v0kCKayH6Jj+30l3xLdgDwReWF7YEvp6yq6nGxHOeSiioPpTtJzNhWjKGnK6oSZqthfWHewlRl2hVIrewD+JbP5JYTp/7iYptOiCwNAUZEBxODR2743D56J1AeHNc8VpZNvE3ZozIoRFhnxZw0ZpvMbgPliKPyjPeOvOF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wZw3yHml4HqTijgn2kl+pV4Ainjbkc0zOqT+FRJPvUM/sIwEtkuSevcqt7NT73ozp9roswv0QHBrclCVIN0uiCqPEsfTaLeVEpg48dOh8as6l1XDlgnDGTFjkj2AgFfD27POPE3n4pJSaYJc5zNijbwrjyz8qa1nr+xBQ+yvteNDOg/1LAczP1xrypDgsl/bRHmkljYhPj40SXwK2jwyicgfgCbE3wi6O9t52D8koacQ==</D></RSAKeyValue>");
            }
            */

            /*
            //----- Using Certificate Store!
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2 certificate = store.Certificates.Find(X509FindType.FindBySubjectName, "TESTCERT", true)[0];
            store.Close();

            serverKey = new RSACryptoServiceProvider();

            if (connection.Host.HostType == HostType.htClient)
            {
                serverKey = (RSACryptoServiceProvider)certificate.PublicKey.Key;
            }
            else
            {

                //----- Need ProviderType=24 for SHA256 sign!
                RSACryptoServiceProvider certKey = (RSACryptoServiceProvider)certificate.PrivateKey;

                CspParameters csp = new CspParameters();
                csp.ProviderName = null;
                csp.ProviderType = 24;
                csp.KeyContainerName = certKey.CspKeyContainerInfo.KeyContainerName;

                serverKey = new RSACryptoServiceProvider(csp);

            }
            */

            //----- Using pfx/p12 certificate!
            X509Certificate2 certificate;

            if (connection.Host.HostType == HostType.htClient)
            {

                certificate = new X509Certificate2(@"..\..\..\..\CertificateCreation\cert.crt");

                serverKey = new RSACryptoServiceProvider();
                serverKey = (RSACryptoServiceProvider)certificate.PublicKey.Key;

            }
            else
            {

                certificate = new X509Certificate2(@"..\..\..\..\CertificateCreation\cert.p12", "12345");

                //----- Need ProviderType=24 for SHA256 sign!
                RSACryptoServiceProvider certKey = (RSACryptoServiceProvider)certificate.PrivateKey;

                CspParameters csp = new CspParameters();
                csp.ProviderName = null;
                csp.ProviderType = 24;
                csp.KeyContainerName = certKey.CspKeyContainerInfo.KeyContainerName;

                serverKey = new RSACryptoServiceProvider(csp);

                certKey.Clear();

            }

        }

        #endregion

        #region OnSSLServerAuthenticate
        
        public override void OnSSLServerAuthenticate(ISocketConnection connection, out X509Certificate2 certificate, out bool clientAuthenticate, ref bool checkRevocation)
        {

            //----- Set server certificate, client authentication and certificate revocation!
            //----- Look at the CertificateCreation folder for instructions

            /*
            //----- Using Certificate Store!
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);

            certificate = store.Certificates.Find(X509FindType.FindBySubjectName, "TESTCERT", false)[0];
            
            store.Close();
            */

            //----- Using pfx/p12 file!
            certificate = new X509Certificate2(@"..\..\..\..\CertificateCreation\cert.p12", "12345");

            clientAuthenticate = false;
            checkRevocation = false;

        }

        #endregion

        #region OnSSLClientAuthenticate

        public override void OnSSLClientAuthenticate(ISocketConnection connection, out string serverName, ref X509Certificate2Collection certs, ref bool checkRevocation)
        {

            /*
            //----- Using client certificate!
            //----- The following certificate and instructions is in CertificateCreation folder.

            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);

            certs = store.Certificates.Find(X509FindType.FindBySubjectName, serverName, false);
             
            store.Close();
            */

            //----- Check server certificate!
            serverName = "TESTCERT";
            checkRevocation = false;

        }

        #endregion

        #region OnSSLClientValidateServerCertificate

        public override void OnSSLClientValidateServerCertificate(X509Certificate serverCertificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, out bool acceptCertificate)
        {

            foreach (X509ChainElement element in chain.ChainElements)
            {
            
                Console.WriteLine("Element issuer name: {0}", element.Certificate.Issuer);
                Console.WriteLine("Element certificate valid until: {0}", element.Certificate.NotAfter);
                Console.WriteLine("Element certificate is valid: {0}", element.Certificate.Verify());
                Console.WriteLine("Element error status length: {0}", element.ChainElementStatus.Length);
                Console.WriteLine("Element information: {0}", element.Information);
                Console.WriteLine("Number of element extensions: {0}{1}", element.Certificate.Extensions.Count, Environment.NewLine);

                if (chain.ChainStatus.Length > 1)
                {
                    
                    for (int index = 0; index < element.ChainElementStatus.Length; index++)
                    {
                        Console.WriteLine(element.ChainElementStatus[index].Status);
                        Console.WriteLine(element.ChainElementStatus[index].StatusInformation);
                    }
                    
                }
                
            }

            acceptCertificate = true;
            
        }

        #endregion

    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Andre Azevedo
Software Developer (Senior)
Brazil Brazil
- Living in São Paulo, Brazil
- Developing since 1994 with
* Clipper (Summer '87 and 5.02)
* FoxPro (DOS, 2.6), Visual Foxpro (6, 7, 8, 9)
* Delphi (1, 2, 5, 7, 2007)
* C# (2.0, 4.0)

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.2 | Last Updated 29 Apr 2009
Article Copyright 2006 by Andre Azevedo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid