runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Creating a PKCS7 signature from the result of RSACng.SignHash(..) with dotnet tools

Open DavidFlamme opened this issue 5 years ago • 11 comments

Hello,

I want to construct a PKCS7 signature by adding all the parts needed, while doing the sign before that.

The closest guess is looking at SignedCms, which can load PKCS7 signatures, verify them, even create them by using ComputeSignature with a CmsSigner. But it is not possible to take all the parts (hash, signature, certificates, additional infos) and create a SignedCms from that.

Did I miss something, or is this a too exotic approach for the framework.

Best regards David

DavidFlamme avatar Nov 09 '21 09:11 DavidFlamme

Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @GrabYourPitchForks See info in area-owners.md if you want to be subscribed.

Issue Details

Hello,

I want to construct a PKCS7 signature by adding all the parts needed, while doing the sign before that.

The closest guess is looking at SignedCms, which can load PKCS7 signatures, verify them, even create them by using ComputeSignature with a CmsSigner. But it is not possible to take all the parts (hash, signature, certificates, additional infos) and create a SignedCms from that.

Did I miss something, or is this a too exotic approach for the framework.

Best regards David

Author: DavidFlamme
Assignees: -
Labels:

area-System.Security, untriaged

Milestone: -

ghost avatar Nov 09 '21 09:11 ghost

Background information: We want to return a PKCS#7 formatted signature, however, we already signed the hashed data on our own (reasons for signing on our own are: we need the signature on its own + SignedCms does only support PKCS#1 as of now and we need to support RSA-PSS and ECDSA as well).

drauch avatar Nov 09 '21 11:11 drauch

But it is not possible to take all the parts (hash, signature, certificates, additional infos) and create a SignedCms from that.

Correct, we don't have a "stitch things together" API for it at a high level. It shouldn't be too difficult with System.Formats.Asn1.AsnWriter:

AsnWriter writer = new AsnWriter(AsnEncodingRules.DER);

using (writer.PushSequence())
{
    writer.WriteObjectIdentifier("1.2.840.113549.1.7.2");

    using (writer.PushSequence(new Asn1Tag(something.ContextSpecific, 0))
    {
        using (writer.PushSequence())
        {
            writer.WriteInteger(DetermineVersionNumber_5_1());

            using (writer.PushSet())
            {
                using (writer.PushSequence())
                {
                    writer.WriteObjectIdentifier(digestAlgorithmOid);
                }
            }
            
            using (writer.PushSequence())
            {
                writer.WriteObjectIdentifier("1.2.840.113549.1.7.1");

                // Assuming you're doing a detached signature, nothing else goes here.
            }
            
            ...

One thing to note is that if you're doing it this way you can't use signed attributes for the signer. The presence of signed attributes drastically changes what the input is to the signature, so a precomputed hash+signature don't fit this scheme.

bartonjs avatar Nov 09 '21 17:11 bartonjs

SignedCms does only support PKCS#1 as of now and we need to support RSA-PSS and ECDSA as well

I believe that SignedCms has always supported ECDSA on .NET Core (now just ".NET"). I don't remember if it does or doesn't work on .NET Framework.

RSA-PSS support just got added, so it'll be available in .NET 7.

bartonjs avatar Nov 09 '21 17:11 bartonjs

RSA-PSS support just got added, so it'll be available in .NET 7.

Actually, since System.Security.Cryptography.Pkcs is a NuGet package; it'll be available once .NET 7 prerelease packages first start being uploaded. I don't know what our plans are for this release... last release the first preview packages went up mid-February.

bartonjs avatar Nov 09 '21 17:11 bartonjs

One thing to note is that if you're doing it this way you can't use signed attributes for the signer. The presence of signed attributes drastically changes what the input is to the signature, so a precomputed hash+signature don't fit this scheme.

As we're still using .NET Framework 4.8 we cannot yet use AsnWriter. We've now settled with a BouncyCastle-based solution.

@DavidFlamme : You know the details, do we have any signed attributes when using our BouncyCastle-based solution? If so, this might indicate that there is still something fishy with it.

RSA-PSS support just got added, so it'll be available in .NET 7.

Thanks, I noticed @vcsjones pull request this morning, that's great news.

All the best, D.R.

drauch avatar Nov 09 '21 17:11 drauch

As we're still using .NET Framework 4.8 we cannot yet use AsnWriter. We've now settled with a BouncyCastle-based solution.

The System.Formats.Asn1 package is available for .NET Framework as well: https://www.nuget.org/packages/System.Formats.Asn1/. It targets netstandard2.0 and .NET Framework 4.6.1+.

RSA-PSS support just got added, so it'll be available in .NET 7. Thanks, I noticed @vcsjones pull request this morning, that's great news.

Indeed. Just to clarify, SignedCms has been able to verify RSA-PSS signatures for a while now (I want to say .NET Core 3...). Producing them is new in .NET 7.

vcsjones avatar Nov 09 '21 17:11 vcsjones

One thing to note is that if you're doing it this way you can't use signed attributes for the signer. The presence of signed attributes drastically changes what the input is to the signature, so a precomputed hash+signature don't fit this scheme.

This helped me a lot to get my head around this.

If you want to use CMS signatures to wrap a signature, you want to use the signed attributes scheme, where you can't have a precomputed signature. If you just want to use CMS format to carry a signature, you can create a signature in advance and add it as the ContentInfo part. The standard supports both: https://datatracker.ietf.org/doc/html/rfc5652

Thanks for the help!

DavidFlamme avatar Nov 25 '21 09:11 DavidFlamme

@bartonjs: Do you know by chance how to create a "HashedData" content from a byte[] hash we already have so we can pass it on to SignedCms? Up until now we passed in the actual hash as content and just told ContentInfo that this is hashed data, but we think that's not true. It looks like SignedCms doesn't care at all and validates this, but we are pretty sure (due to the RFC) that the resulting CMS is invalid because the content it contains does not match the RFC's definition of HashedData. Are we right about this?

To illustrate, the following code creates a SignedCms:

const string hashedData = "1.2.840.113549.1.7.5"; // same OID as digestedData
var contentType = new Oid(hashedData);
var content = hash;
var contentInfo = new ContentInfo(contentType, content);

var signedCms = new SignedCms(contentInfo, detached: true);

If we sign this CMS with a CmsSigner and then call CheckSignature it succeeds, however, this is still not a valid CMS, is it?

Best regards, D.R.

drauch avatar Nov 25 '21 10:11 drauch

DigestedData is really something parallel to SignedData, so SignedCms doesn't know anything about it. For the most part, the ContentInfo OID is just for passing data from the signer to the recipient. We don't have any support for it in System.Security.Cryptography.Pkcs.

So, to create one, you'd need to use AsnWriter and just build out the structure from https://datatracker.ietf.org/doc/html/rfc5652#section-7:

      DigestedData ::= SEQUENCE {
        version CMSVersion,
        digestAlgorithm DigestAlgorithmIdentifier,
        encapContentInfo EncapsulatedContentInfo,
        digest Digest }

      Digest ::= OCTET STRING

If we sign this CMS with a CmsSigner and then call CheckSignature it succeeds, however, this is still not a valid CMS, is it?

SignedCms considers the content opaque. The only thing it uses the content type for is the Encode routine, when it bumps the SignedCms version to 3 if the content type isn't id-data (it's never used in the verification routine). So the SignedCms portion is structurally valid, but the value in a larger context is not valid (aka a context where someone understands that the content id is id-digestedData and therefore the content should decode as a DigestedData BER value).

The way to think of it is that all CheckSignature does is make sure that the content (and associated signed attributes) hasn't changed since the SignedCms was created. It has no idea (and no concern for) if the content is "valid", even if the content type was id-signedData, meaning it was a nested SignedData (which it would clearly have a structural validator for).

bartonjs avatar Nov 29 '21 20:11 bartonjs

OK, thanks for the clarification, helps us out a lot!

Best regards, D.R.

drauch avatar Nov 29 '21 20:11 drauch