Преобразовать отдельную проверку подписи .NET в bouncycastle - PullRequest
0 голосов
/ 25 октября 2019

Я пытаюсь преобразовать следующий код (который использует встроенные библиотеки криптографии .NET) в BouncyCastle. К сожалению, из-за отсутствия документации, путаницы со всеми вопросами по Java BC и необычности работы с отсоединенными подписями я не могу понять, как это сделать.

.NET code:

1) Код, который мне нужно преобразовать:

var data = Encoding.ASCII.GetBytes("Hi!");
var signature = File.ReadAllText("Signature.p7s");
byte[] hashedDocument = HashAlgorithm.Create("SHA256").ComputeHash(data);
var cms = new SignedCms(new ContentInfo(hashedDocument), true);
cms.Decode(Convert.FromBase64String(signature));
cms.CheckSignature(true);

2) Код для генерации p7s:

var data = File.ReadAllBytes(fileToSign);
var hash = SHA256.Create().ComputeHash(data);
ContentInfo ci = new ContentInfo(hash);
SignedCms cms = new SignedCms(ci, true);
CmsSigner signer = new CmsSigner(new X509Certificate2(cert, pw));
signer.IncludeOption = X509IncludeOption.WholeChain;
signer.DigestAlgorithm = new Oid("sha256RSA");
signer.SignedAttributes.Add(new Pkcs9DocumentName(cert.SubjectName.Name));
signer.SignedAttributes.Add(new Pkcs9SigningTime());
cms.ComputeSignature(signer);
var enc = Convert.ToBase64String(cms.Encode());
File.WriteAllText(sigFile, enc);

Я пробовал BouncyCastle's RsaDigestSigner, CmsSignedData,CmsSignedDataParser и SignerUtilities.GetSigner("SHA-256withRSA") (который просто возвращает RsaDigestSigner), но ни один из них, похоже, не имеет способа понять файл p7s и фактически убедиться, что он применяется к указанным данным.

1 Ответ

1 голос
/ 28 октября 2019

Итак, учитывая, что вы не предоставили пример того, как вы генерируете отдельную подпись, мне пришлось сделать некоторые предположения.

Код ниже был запущен в .net Core 3.0.

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;

namespace Whatever
{
    class Program
    {
        static string cert = "MIIJmQIBA.... // This here is base64 encoded PFX for the purposes of the demo ONLY";
        static string pwd = "password";

        static void Main(string[] args)
        {
            var data = Encoding.ASCII.GetBytes("Hi!");

            var hash = HashAlgorithm.Create("SHA256").ComputeHash(data);

            var certificate = new X509Certificate2(Convert.FromBase64String(cert), pwd);

            // So since I've no idea how you got p7s, i improvised here:
            var cms = new SignedCms(new ContentInfo(hash), true); // true -> Detached
            var signer = new CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, certificate);
            cms.ComputeSignature(signer);
            var data2 = cms.Encode();

            // assuming this was in p7s file
            var xx = Convert.ToBase64String(data2);

            // this passes, this is the .Net validation from OP
            var cms2 = new SignedCms(new ContentInfo(hash), true);
            cms2.Decode(Convert.FromBase64String(xx));
            cms2.CheckSignature(true);

            // Same in bouncy castle:
            BCUtil.Validate(certificate, hash, xx);
        }
    }
}

Вот наш BCUtil класс:

using System;
using Org.BouncyCastle.Cms;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Security;
using System.IO;
using System.Linq;

namespace Whatever
{
    public class BCUtil
    {
        public static void Validate(System.Security.Cryptography.X509Certificates.X509Certificate2 cert, byte[] hash, string whatToValidate)
        {
            // My understanding is that you always need a cert to validate a signature with BC, but you only need a PUBLIC key
            var certificate = DotNetUtilities.FromX509Certificate(cert);

            // hash here
            var processable = new CmsProcessableByteArray(hash);
            // and signature here, for full .Net convert to old using() {} syntax
            using var str = new MemoryStream(Convert.FromBase64String(whatToValidate));
            var cms = new CmsSignedData(processable, str);
            var signers = cms.GetSignerInfos();
            var signersCollection = signers.GetSigners();

            foreach(var signer in signersCollection.Cast<SignerInformation>())
            {
                if (signer.Verify(certificate.GetPublicKey()))
                {
                    Console.WriteLine("yes banana"); // pass
                }
                else
                {
                    throw new Exception("no banana"); // fail
                }
            }
        }
    }
}
...