-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements GostSignedCms.RemoveCertificate()
#49 v2.0.10
- Loading branch information
Showing
9 changed files
with
304 additions
and
5 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
Source/GostCryptography.Tests/Pkcs/SignedCmsSignAndExcludeCertificates.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System.Security.Cryptography.Pkcs; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Text; | ||
|
||
using GostCryptography.Pkcs; | ||
|
||
using NUnit.Framework; | ||
|
||
namespace GostCryptography.Tests.Pkcs | ||
{ | ||
/// <summary> | ||
/// Подпись и проверка подписи сообщения CMS/PKCS#7. | ||
/// </summary> | ||
/// <remarks> | ||
/// Тест создает сообщение, формирует подписанное сообщение в формате CMS/PKCS#7, | ||
/// исключая информацию о сертификате подписчика с целью минимизации размера сообщения, | ||
/// а затем проверяет подпись полученную цифровую подпись. | ||
/// </remarks> | ||
[TestFixture(Description = "Подпись и проверка подписи сообщения CMS/PKCS#7")] | ||
public class SignedCmsSignAndExcludeCertificates | ||
{ | ||
[Test] | ||
[TestCaseSource(typeof(TestConfig), nameof(TestConfig.Gost_R3410_Certificates))] | ||
public void ShouldSign(TestCertificateInfo testCase) | ||
{ | ||
// Given | ||
var certificate = testCase.Certificate; | ||
var message = CreateMessage(); | ||
|
||
// When | ||
var signedMessage = SignMessage(certificate, message); | ||
var isValidSignedMessage = VerifyMessage(certificate, signedMessage); | ||
|
||
// Then | ||
Assert.IsTrue(isValidSignedMessage); | ||
} | ||
|
||
private static byte[] CreateMessage() | ||
{ | ||
// Некоторое сообщение для подписи | ||
|
||
return Encoding.UTF8.GetBytes("Some message to sign..."); | ||
} | ||
|
||
private static byte[] SignMessage(X509Certificate2 certificate, byte[] message) | ||
{ | ||
// Создание объекта для подписи сообщения | ||
var signedCms = new GostSignedCms(new ContentInfo(message)); | ||
|
||
// Создание объект с информацией о подписчике | ||
var signer = new CmsSigner(certificate); | ||
|
||
// Включение информации только о конечном сертификате (только для теста) | ||
signer.IncludeOption = X509IncludeOption.EndCertOnly; | ||
|
||
// Создание подписи для сообщения CMS/PKCS#7 | ||
signedCms.ComputeSignature(signer); | ||
|
||
// Исключение сертификатов для уменьшения размера сообщения | ||
signedCms.RemoveCertificates(); | ||
|
||
// Создание сообщения CMS/PKCS#7 | ||
return signedCms.Encode(); | ||
} | ||
|
||
private static bool VerifyMessage(X509Certificate2 certificate, byte[] signedMessage) | ||
{ | ||
// Создание объекта для проверки подписи сообщения | ||
var signedCms = new GostSignedCms(); | ||
|
||
// Чтение сообщения CMS/PKCS#7 | ||
signedCms.Decode(signedMessage); | ||
|
||
// Список сертификатов подписчика | ||
var signerCerts = new X509Certificate2Collection(certificate); | ||
|
||
try | ||
{ | ||
// Проверка подписи сообщения CMS/PKCS#7 | ||
signedCms.CheckSignature(signerCerts, true); | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.