Advtools.AdvInterceptor.CertificatesManager.GetCertificate C# (CSharp) Method

GetCertificate() private method

private GetCertificate ( string name ) : X509Certificate2
name string
return System.Security.Cryptography.X509Certificates.X509Certificate2
        internal MSX509.X509Certificate2 GetCertificate(string name)
        {
            List<X509Extension> extensions = new List<X509Extension>();

            BasicConstraintsExtension constraints = new BasicConstraintsExtension();
            constraints.CertificateAuthority = false;
            constraints.Critical = true;
            extensions.Add(constraints);

            KeyUsageExtension keyUsage = new KeyUsageExtension();
            keyUsage.KeyUsage = KeyUsages.digitalSignature | KeyUsages.nonRepudiation | KeyUsages.keyEncipherment;
            extensions.Add(keyUsage);

            ExtendedKeyUsageExtension extendedUsage = new ExtendedKeyUsageExtension();
            extendedUsage.KeyPurpose.Add("1.3.6.1.5.5.7.3.1");
            extendedUsage.KeyPurpose.Add("1.3.6.1.5.5.7.3.2");
            extensions.Add(extendedUsage);

            return CreateCertificate(name, extensions, GetRootCertificate(), state_.Config.X509.AuthorityName, MSX509.StoreName.My, state_.Config.X509.RootValidity);
        }

Usage Example

Example #1
0
        public void Start()
        {
            //HttpServer.Logging.LogFactory.Assign(new HttpServer.Logging.ConsoleLogFactory(null));

            // TODO: more than one Interception can be configured with the same port and IP

            foreach (var interception in state_.Config.Interceptions)
            {
                IPAddress ip = GetIp(interception.IPv4);
                if (ip == null)
                {
                    state_.Logger.Error("Invalid IPv4 address: {0}", interception.IPv4);
                    continue;
                }

                state_.Logger.Information("Intercept {0} {2} {3}:{1}", interception.Protocol, interception.Port, interception.Name, ip);

                try
                {
                    HttpListener listener = interception.Protocol == Protocol.Https ?
                                            HttpListener.Create(ip, interception.Port, certificatesMgr_.GetCertificate(interception.Name)) :
                                            HttpListener.Create(ip, interception.Port);
                    listener.RequestReceived += OnRequest;
                    listener.Start(state_.Config.Web.WebBacklog);
                    listeners_.Add(listener);
                }
                catch (System.Net.Sockets.SocketException e)
                {
                    state_.Logger.Exception(e, "Error setting up listener on port {0}", interception.Port);
                }
            }
        }