/// <summary> /// The sign atached button click. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void SignAtachedButtonClick(object sender, EventArgs e) { this.InfoListBox.Items.Clear(); var certificateWrapper = (X509Certificate2Wrapper)this.CertificatesComboBox.SelectedItem; var message = File.ReadAllBytes(this.FileForSignNameTextBox.Text); var contentInfo = new ContentInfo(message); var signedCms = new SignedCms(contentInfo, false); var isSingPresent = true; try { signedCms.Decode(message); } catch { isSingPresent = false; } if (isSingPresent) { this.InfoListBox.Items.Add("Документ содержит подписи:"); if (signedCms.Certificates.Count > 0) { foreach (var cer in signedCms.Certificates) { this.InfoListBox.Items.Add(CryptographicWrapperModule.ExtractCertificateName(cer.SubjectName)); } } } else { this.InfoListBox.Items.Add("Подписи в документе отсутствуют"); } /*var gpiCryptographicWrapper = new GpiCryptographicWrapper(); * var certificateList = gpiCryptographicWrapper.GetCertificateList(); * foreach (var certificate in certificateList) * { * var cmsSigner = new CmsSigner(certificate.Certificat); // Определяем подписывающего, объектом CmsSigner. * signedCms.ComputeSignature(cmsSigner, false); // Подписываем CMS/PKCS #7 сообение. * }*/ var cmsSigner = new CmsSigner(certificateWrapper.Certificat); // Определяем подписывающего, объектом CmsSigner. signedCms.ComputeSignature(cmsSigner, false); // Подписываем CMS/PKCS #7 сообение. var signature = signedCms.Encode(); var outFileName = this.FileForSignNameTextBox.Text; if (!this.FileForSignNameTextBox.Text.EndsWith(".sig")) { outFileName = this.FileForSignNameTextBox.Text + ".sig"; } File.WriteAllBytes(outFileName, signature); this.InfoListBox.Items.Add("Документ подписан: " + certificateWrapper.DisplayName); }
/// <summary> /// The assute sign button click. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void AssuteSignButtonClick(object sender, EventArgs e) { this.InfoListBox.Items.Clear(); var certificateWrapper = (X509Certificate2Wrapper)this.CertificatesComboBox.SelectedItem; var message = File.ReadAllBytes(this.FileForSignNameTextBox.Text); var contentInfo = new ContentInfo(message); var signedCms = new SignedCms(contentInfo, false); var isSingPresent = true; try { signedCms.Decode(message); } catch { isSingPresent = false; } if (isSingPresent) { this.InfoListBox.Items.Add("Документ содержит подписи:"); if (signedCms.Certificates.Count > 0) { foreach (var cer in signedCms.Certificates) { this.InfoListBox.Items.Add(CryptographicWrapperModule.ExtractCertificateName(cer.SubjectName)); } } } else { this.InfoListBox.Items.Add("Подписи в документе отсутствуют"); return; } foreach (var sign in signedCms.SignerInfos) { // if sign.Certificate.SubjectName.Name.Contains("Second")) { sign.ComputeCounterSignature(new CmsSigner(certificateWrapper.Certificat)); } } var signature = signedCms.Encode(); var outFileName = this.FileForSignNameTextBox.Text; if (!this.FileForSignNameTextBox.Text.EndsWith(".sig")) { outFileName = this.FileForSignNameTextBox.Text + ".sig"; } File.WriteAllBytes(outFileName, signature); this.InfoListBox.Items.Add("Подписи заверены: " + certificateWrapper.DisplayName); }
/// <summary> /// The gpi document sugner form load. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void GpiDocumentSugnerFormLoad(object sender, EventArgs e) { this.InfoListBox.Items.Clear(); var commandLine = Environment.GetCommandLineArgs(); if (commandLine.Length > 1) { this.FileForSignNameTextBox.Text = commandLine[1]; this.FileForCheckNameTextBox.Text = commandLine[1]; } var cryptographicWrapperModule = new CryptographicWrapperModule(); var certificateList = cryptographicWrapperModule.GetCertificateList(); this.CertificatesComboBox.DataSource = certificateList; this.CertificateListBox.DataSource = certificateList; }
/// <summary> /// The remove certificate button click. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void RemoveCertificateButtonClick(object sender, EventArgs e) { var certificateWrapper = (X509Certificate2Wrapper)this.CertificateListBox.SelectedItem; var cryptographicWrapperModule = new CryptographicWrapperModule(); var result = cryptographicWrapperModule.RemoveCertificateFromUserStore(certificateWrapper.Certificat); if (result) { this.InfoListBox.Items.Add("Certificate installed!"); var certificateList = cryptographicWrapperModule.GetCertificateList(); this.CertificatesComboBox.DataSource = certificateList; this.CertificateListBox.DataSource = certificateList; } else { this.InfoListBox.Items.Add("Error!"); cryptographicWrapperModule.LastError.ForEach(c => this.InfoListBox.Items.Add(c)); } }
/// <summary> /// The create request button click. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void CreateRequestButtonClick(object sender, EventArgs e) { var gpiCryptographicWrapper = new CryptographicWrapperModule(); var request = gpiCryptographicWrapper.CreateActiveDirectoryCertificateRequest("User"); var response = gpiCryptographicWrapper.SendActiveDirectoryCertificateRequest(request); var result = gpiCryptographicWrapper.InstallCertificateResponse(response); this.InfoListBox.Items.Add(result ? "Certificate installed!" : "Error"); if (result) { this.InfoListBox.Items.Add("Certificate installed!"); var certificateList = gpiCryptographicWrapper.GetCertificateList(); this.CertificatesComboBox.DataSource = certificateList; this.CertificateListBox.DataSource = certificateList; } else { this.InfoListBox.Items.Add("Error!"); gpiCryptographicWrapper.LastError.ForEach(c => this.InfoListBox.Items.Add(c)); } }
/// <summary> /// The import certificate button click. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void ImportCertificateButtonClick(object sender, EventArgs e) { try { var openFileDialog = new OpenFileDialog { Filter = @"cer files (*.cer)|*.cer|All files (*.*)|*.*", FilterIndex = 1, RestoreDirectory = true }; if (openFileDialog.ShowDialog() != DialogResult.OK) { return; } var certificate = new X509Certificate2(); certificate.Import(openFileDialog.FileName); var cryptographicWrapperModule = new CryptographicWrapperModule(); var result = cryptographicWrapperModule.InstallCertificateToUserStore(certificate); if (result) { this.InfoListBox.Items.Add("Certificate installed!"); var certificateList = cryptographicWrapperModule.GetCertificateList(); this.CertificatesComboBox.DataSource = certificateList; this.CertificateListBox.DataSource = certificateList; } else { this.InfoListBox.Items.Add("Error!"); cryptographicWrapperModule.LastError.ForEach(c => this.InfoListBox.Items.Add(c)); } } catch (Exception ex) { this.InfoListBox.Items.Add(ex.Message); } }
/// <summary> /// The check atached button click. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void CheckAtachedButtonClick(object sender, EventArgs e) { this.InfoListBox.Items.Clear(); var action = this.VerifySignatureOnlyCheckBox.Checked ? @"Проверка подписи" : @"Проверка подписи и сертификатов"; this.InfoListBox.Items.Add($"{action} {this.FileForCheckNameTextBox.Text}"); try { var signature = File.ReadAllBytes(this.FileForCheckNameTextBox.Text); var contentInfo = new ContentInfo(signature); var signedCms = new SignedCms(contentInfo, false); try { signedCms.Decode(signature); } catch (CryptographicException crex) { this.InfoListBox.Items.Add("Не удалось декодировать информационный блок ЭЦП"); this.InfoListBox.Items.Add("(возможно документ не подписан)"); this.InfoListBox.Items.Add("Ошибка: " + crex.Message); } /*ClientCredentials creds = new ClientCredentials(); * creds.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust; // Configure peer trust. * creds.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.ChainTrust; // Configure chain trust. * creds.*/ var count = 1; if (signedCms.SignerInfos.Count > 0) { this.InfoListBox.Items.Add("Документ содержит подписи:"); foreach (var signerInfos in signedCms.SignerInfos) { this.InfoListBox.Items.Add(string.Format("{0} {1}", count++, CryptographicWrapperModule.ExtractCertificateName(signerInfos.Certificate.SubjectName))); if (signerInfos.CounterSignerInfos.Count > 0) { foreach (var counterSignerInfos in signerInfos.CounterSignerInfos) { this.InfoListBox.Items.Add(string.Format(" (подпись заверена {0})", CryptographicWrapperModule.ExtractCertificateName(counterSignerInfos.Certificate.SubjectName))); } } try { signerInfos.CheckSignature(this.VerifySignatureOnlyCheckBox.Checked); this.InfoListBox.Items.Add("Проверка: Ok!"); signerInfos.Certificate.Verify(); foreach (X509VerificationFlags enumValue in Enum.GetValues(typeof(X509VerificationFlags))) { var chain = new X509Chain { ChainPolicy = { RevocationMode = X509RevocationMode.Offline, VerificationFlags = enumValue } }; chain.Build(signerInfos.Certificate); foreach (var element in chain.ChainElements) { this.InfoListBox.Items.Add($"Element issuer name: {element.Certificate.Issuer}"); this.InfoListBox.Items.Add($"Element certificate valid until: {element.Certificate.NotAfter}"); this.InfoListBox.Items.Add($"Element certificate is valid: {element.Certificate.Verify()}"); this.InfoListBox.Items.Add($"Element error status length: {element.ChainElementStatus.Length}"); this.InfoListBox.Items.Add($"Element information: {element.Information}"); this.InfoListBox.Items.Add($"Number of element extensions: {element.Certificate.Extensions.Count}{Environment.NewLine}"); if (chain.ChainStatus.Length > 0) { for (int index = 0; index < element.ChainElementStatus.Length; index++) { this.InfoListBox.Items.Add(element.ChainElementStatus[index].Status); this.InfoListBox.Items.Add(element.ChainElementStatus[index].StatusInformation); } } } } } catch (CryptographicException crex) { this.InfoListBox.Items.Add(string.Format("Проверка: {0}", crex.Message)); } } } else { this.InfoListBox.Items.Add("Подписи отсутствуют"); } this.InfoListBox.Items.Add("Ok!"); } catch (Exception ex) { this.InfoListBox.Items.Add(ex.Message); } }