private void SetSig(X509Certificate2 certSig) { _certSig=certSig; labelSignedBy.Visible=false; textSignedBy.Visible=false; textSignedBy.Text=""; butSig.Visible=false; textFromAddress.ReadOnly=false; if(certSig!=null) { labelSignedBy.Visible=true; textSignedBy.Visible=true; textSignedBy.Text=EmailNameResolver.GetCertSubjectName(certSig); //Show the user that, if the message is signed, then the sender will always look like the address on the certificate, //even if they have a Sender Address setup. Otherwise we would be misrepresenting how the Sender Address feature works. textFromAddress.Text=textSignedBy.Text; textFromAddress.ReadOnly=true; butSig.Visible=true; } }
private void FormEmailDigitalSignature_Load(object sender, EventArgs e) { string signedByAddress = EmailNameResolver.GetCertSubjectName(_certSig); textSignedBy.Text = signedByAddress; textCertificateAuthority.Text = _certSig.IssuerName.Name; textValidFrom.Text = _certSig.NotBefore.ToShortDateString() + " to " + _certSig.NotAfter.ToShortDateString(); textThumbprint.Text = _certSig.Thumbprint; textVersion.Text = _certSig.Version.ToString(); _isTrusted = (EmailMessages.GetReceiverUntrustedCount(signedByAddress) == -1); if (_isTrusted) { butTrust.Visible = false; textTrustStatus.Text = Lan.g(this, "Trusted"); textTrustExplanation.Text = Lan.g(this, "Encrypted email and EHR Direct messaging are currently enabled for the signer."); } else { butTrust.Visible = true; textTrustStatus.Text = Lan.g(this, "Untrusted or invalid"); textTrustExplanation.Text = Lan.g(this, "Encrypted email and EHR Direct messaging will not work until this digital signature is trusted by you.") + " " + Lan.g(this, "Click the Trust button to add trust for this digital signature."); } }
private void butOK_Click(object sender, EventArgs e) { if (textVerificationCode.Text.Trim() == "") { MsgBox.Show(this, "Verification Code is blank."); return; } if (!File.Exists(textCertFilePath.Text)) { MsgBox.Show(this, "Certificate file path is invalid."); return; } string ext = Path.GetExtension(textCertFilePath.Text).ToLower(); if (ext != ".der" && ext != ".cer") { MsgBox.Show(this, "Certificate file path extension must be .der or .cer."); return; } byte[] arrayCertificateBytes = null; try { arrayCertificateBytes = File.ReadAllBytes(textCertFilePath.Text); } catch (Exception ex) { MessageBox.Show(Lan.g(this, "Failed to read the certificate file.") + " " + ex.Message); return; } X509Certificate2 cert = null; try { cert = new X509Certificate2(arrayCertificateBytes); } catch (Exception ex) { MessageBox.Show(Lan.g(this, "Invalid certificate file.") + " " + ex.Message); return; } if (EmailNameResolver.GetCertSubjectName(cert).ToLower() != textEmailAddress.Text.ToLower()) { MessageBox.Show(Lan.g(this, "Email certificates are tied to specific addresses or domains.") + " " + Lan.g(this, "The email address on the certificate is") + " " + EmailNameResolver.GetCertSubjectName(cert) + ", " + Lan.g(this, "but the email address you specified is") + " " + textEmailAddress.Text); return; } if (cert.HasPrivateKey) { MsgBox.Show(this, "The specified certificate contains a private key. For your security, please export your public key and upload that instead."); return; } Cursor = Cursors.WaitCursor; XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); StringBuilder strbuild = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(strbuild, settings)) { writer.WriteStartElement("PostEmailCertificate"); writer.WriteElementString("RegistrationKey", PrefC.GetString(PrefName.RegistrationKey)); writer.WriteElementString("EmailAddress", textEmailAddress.Text); writer.WriteElementString("VerificationCode", textVerificationCode.Text); writer.WriteElementString("CertificateData", Convert.ToBase64String(arrayCertificateBytes)); writer.WriteEndElement(); } #if DEBUG OpenDental.localhost.Service1 updateService = new OpenDental.localhost.Service1(); #else OpenDental.customerUpdates.Service1 updateService = new OpenDental.customerUpdates.Service1(); updateService.Url = PrefC.GetString(PrefName.UpdateServerAddress); #endif if (PrefC.GetString(PrefName.UpdateWebProxyAddress) != "") { IWebProxy proxy = new WebProxy(PrefC.GetString(PrefName.UpdateWebProxyAddress)); ICredentials cred = new NetworkCredential(PrefC.GetString(PrefName.UpdateWebProxyUserName), PrefC.GetString(PrefName.UpdateWebProxyPassword)); proxy.Credentials = cred; updateService.Proxy = proxy; } string xmlResponse = ""; try { xmlResponse = updateService.PostEmailCertificate(strbuild.ToString()); } catch (Exception ex) { Cursor = Cursors.Default; MessageBox.Show("Error: " + ex.Message); return; } XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlResponse); XmlNode node = doc.SelectSingleNode("//Error"); if (node != null) { Cursor = Cursors.Default; MessageBox.Show(Lan.g(this, "Error.") + " " + Lan.g(this, "Email certificate was not registered.") + " " + node.InnerText); return; } Cursor = Cursors.Default; if (doc.InnerText == "Insert") { MessageBox.Show(Lan.g(this, "Done.") + " " + Lan.g(this, "The email certificate has been registered for address") + " " + textEmailAddress.Text); } else //Updated { MessageBox.Show(Lan.g(this, "Done.") + " " + Lan.g(this, "The email certificate has been updated for address") + " " + textEmailAddress.Text); } DialogResult = DialogResult.OK; }