BaseMtpValidationMode

Enum

Specifies the validation mode for MTP (Message Transfer Protocol) certificates.

Members

Default

The default validation mode, which uses the system's default certificate validation policy.

Full

Full validation is performed, including checking the certificate chain, revocation status, and validity period.

None

No certificate validation is performed. This is insecure and should only be used in development or testing scenarios.

Example Usage

The following C# code demonstrates how to use the BaseMtpValidationMode enum to configure certificate validation for an MTP connection.


using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public class MtpClient
{
    public static void Connect(string serverName, X509Certificate2 clientCertificate)
    {
        // Configure the SslStream for MTP validation
        // For demonstration purposes, we'll use 'Full' validation.
        // In a real application, choose the mode appropriate for your security needs.
        SslProtocols protocols = SslProtocols.Tls12; // Or other appropriate protocols
        LocalCertificateSelectionCallback certificateSelectionCallback = (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) =>
        {
            return clientCertificate;
        };

        // Create a RemoteCertificateValidationCallback to handle certificate validation
        RemoteCertificateValidationCallback validationCallback = (sender, certificate, chain, sslPolicyErrors) =>
        {
            // Use the BaseMtpValidationMode to determine validation behavior
            BaseMtpValidationMode validationMode = BaseMtpValidationMode.Full; // Example setting

            if (validationMode == BaseMtpValidationMode.Full)
            {
                if (sslPolicyErrors != SslPolicyErrors.None)
                {
                    Console.WriteLine($"SSL Policy Errors: {sslPolicyErrors}");
                    // Further detailed validation can be performed here
                    return false; // Reject if there are policy errors
                }
                if (certificate == null)
                {
                    Console.WriteLine("Remote certificate is null.");
                    return false;
                }
                try
                {
                    X509Chain ch = new X509Chain();
                    ch.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
                    ch.ChainPolicy.RevocationMode = X509RevocationMode.Online;
                    ch.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
                    ch.ChainPolicy.ExtraStore.Add(clientCertificate); // Add client cert if needed for chain building

                    if (!ch.Build((X509Certificate2)certificate))
                    {
                        Console.WriteLine("Certificate chain build failed.");
                        foreach (X509ChainStatus status in ch.ChainStatus)
                        {
                            Console.WriteLine($"Status: {status.Status} - {status.StatusInformation}");
                        }
                        return false;
                    }

                    foreach (X509ChainStatus status in ch.ChainStatus)
                    {
                        if (status.Status != X509ChainStatusFlags.NoError)
                        {
                            Console.WriteLine($"Certificate validation error: {status.Status} - {status.StatusInformation}");
                            return false;
                        }
                    }
                    Console.WriteLine("Certificate validation succeeded.");
                    return true; // Certificate is valid
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error during certificate validation: {ex.Message}");
                    return false;
                }
            }
            else if (validationMode == BaseMtpValidationMode.None)
            {
                Console.WriteLine("Certificate validation is disabled.");
                return true; // Bypass validation
            }
            else // Default mode
            {
                // For default, we can rely on the system's default behavior or implement custom logic
                if (sslPolicyErrors == SslPolicyErrors.None)
                {
                    return true;
                }
                Console.WriteLine($"SSL Policy Errors (Default): {sslPolicyErrors}");
                return false;
            }
        };

        // Use the callbacks when creating an SslStream or establishing a connection
        // Example: using (var client = new TcpClient(serverName, port))
        //          using (var sslStream = new SslStream(client.GetStream(), false, validationCallback, certificateSelectionCallback))
        //          {
        //              sslStream.AuthenticateAsClient(serverName);
        //              // ... use sslStream ...
        //          }
        Console.WriteLine($"MTP client configured for {validationMode}.");
    }

    public static void Main(string[] args)
    {
        // Dummy client certificate for example
        X509Certificate2 dummyCert = new X509Certificate2("path/to/your/certificate.pfx", "password");
        Connect("example.com", dummyCert);
    }
}