Wednesday 4 November 2020

.NET CmsSigner, SignedCms and ContentInfo equivalent in Node Js

This question may be vague. But I need to convert .NET code to node js. I am new to node APIs.

Basically I need to create a detached signature of a content buffer using a .pfx file. Function has to return a buffer that can be saved as .sig file and verified using the same classes.

Below is the .NET code

Signing

public byte[] CreateSignature(byte[] sourceData, X509Certificate2 certToUse)
{
    var signer = new CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, certToUse);
    var signedCms = new SignedCms(new ContentInfo(sourceData), true);

    signedCms.ComputeSignature(signer);
    return signedCms.Encode();
}

Verification

public (VerificationStatus, X509Certificate2Collection) VerifySignature(byte[] data,
    byte[] signature)
{
    try
    {
        // verify signature and get certificates
        var signedCms = new SignedCms(
            SubjectIdentifierType.SubjectKeyIdentifier,
            new ContentInfo(data),
            true);

        signedCms.Decode(signature);
        signedCms.CheckSignature(true);
        return (VerificationStatus.VerificationSuccess, signedCms.Certificates);
    }
    catch
    {
        return (VerificationStatus.UnableToVerifySignatureAgainstContent, null);
    }
}

Also, kindly help with System.Security.Cryptography.X509Certificates.X509Chain :)



from .NET CmsSigner, SignedCms and ContentInfo equivalent in Node Js

No comments:

Post a Comment