Certificate/Source/ServerCertificates/Get-SMTPCertificate.cs
using System;
using System.Management.Automation; using System.IO; using System.Net.Security; using System.Net.Sockets; using System.Security.Authentication; namespace CPolydorou.Exchange.ServerCertificates { [Cmdlet("Get","SMTPCertificate")] public class GetSMPTCertificate : PSCmdlet { //region Variables // Private variables for parameters private string server; private int port; private bool export = false; private string path; private bool showerrors = false; //endregion //region Parameters [Parameter( Mandatory = true, Position = 0, HelpMessage = "The name of the server.", ParameterSetName = "Basic" )] [Parameter( Mandatory = true, Position = 0, HelpMessage = "The name of the server.", ParameterSetName = "Export" )] [Parameter( Mandatory = true, Position = 0, HelpMessage = "The name of the server.", ParameterSetName = "ShowErrors" )] public string Server { get { return this.server; } set { this.server = value; } } [Parameter( Mandatory = true, Position = 1, HelpMessage = "The port of the server.", ParameterSetName = "Basic" )] [Parameter( Mandatory = true, Position = 1, HelpMessage = "The port of the server.", ParameterSetName = "Export" )] [Parameter( Mandatory = true, Position = 1, HelpMessage = "The port of the server.", ParameterSetName = "ShowErrors" )] public int Port { get { return this.port; } set { this.port = value; } } [Parameter( Mandatory = true, HelpMessage = "Export the certificate.", ParameterSetName = "Export" )] public SwitchParameter Export { get { return this.export; } set { this.export = value; } } [Parameter( Mandatory = true, HelpMessage = "The path to the exported file.", ParameterSetName = "Export" )] public string Path { get { return this.path; } set { this.path = value; } } [Parameter( Mandatory = true, HelpMessage = "Show the certificate erros", ParameterSetName = "ShowErrors" )] public SwitchParameter ShowCertificateErrors { get { return this.showerrors; } set { this.showerrors = value; } } //endregion //region Overrides protected override void ProcessRecord() { System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback); using (System.Net.Mail.SmtpClient S = new System.Net.Mail.SmtpClient(this.server, this.port)) { S.EnableSsl = true; using (System.Net.Mail.MailMessage M = new System.Net.Mail.MailMessage("someone@example.com", "someone@example.com", "Test", "Test")) { try { S.Send(M); } catch (Exception) { return; } } } } //endregion //region Helper functions private bool RemoteServerCertificateValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { // if the ShowErrors parameter has been given if (this.showerrors == true) { // check if there are policy errors if (sslPolicyErrors != SslPolicyErrors.None) { CertificateError err = new CertificateError(); err.Server = this.server; err.Port = this.port; err.Error = sslPolicyErrors.ToString(); WriteObject(err); } // return based on the policy errors if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None) return true; else return false; } // if the Export parameter has not been given if (this.export == false) { Certificate cert = new CPolydorou.Exchange.ServerCertificates.Certificate(); cert.Subject = certificate.Subject; cert.Issuer = certificate.Issuer; cert.ExpirationDate = certificate.GetExpirationDateString(); cert.EffectiveDate = certificate.GetEffectiveDateString(); cert.Hash = certificate.GetCertHashString(); cert.PublicKey = certificate.GetPublicKeyString(); cert.Format = certificate.GetFormat(); cert.KeyAlgorithm = certificate.GetKeyAlgorithm(); cert.SerialNumber = certificate.GetSerialNumberString(); cert.Type = certificate.GetType(); cert.Server = this.server; cert.port = this.port; WriteObject(cert); } else { // Export the certificate to file // Resolve the path SessionState ss = new SessionState(); Directory.SetCurrentDirectory(ss.Path.CurrentFileSystemLocation.Path); // Export the certificate string fullPath = System.IO.Path.GetFullPath(path); byte[] cert = certificate.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Cert); System.IO.File.WriteAllBytes(this.path, cert); } // Return based on the policy errors if (sslPolicyErrors == SslPolicyErrors.None) return true; else return false; } //endregion } } |