Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract PKCS#1 signature from PKCS#7

I know that PKCS#7 = Certificate + Optional raw data + Signature in PKCS#1 format I need to extract PKCS#1 from a PKCS#7 signature how can I do this in C#. Can I use the bouncy castle to do this, Here is my implementation ie. to convert PKCS#7 to ASN.1 and to take the last sequence as it is PKCS#1

Asn1InputStream asn1 = new Asn1InputStream(pkcs7Stream);
Asn1Sequence sequence = (Asn1Sequence)asn1.ReadObject().ToAsn1Object();
var sequenceString = sequence.ToString();
var lastCommaIndex = sequenceString.LastIndexOf(",");
var pkcs1HexStr = sequenceString.Substring(lastCommaIndex + 3).Replace("]", string.Empty);

Is there any other eligant way to obtain PKCS#1

like image 651
Prashanth Avatar asked Oct 22 '25 03:10

Prashanth


1 Answers

The SignedCms class can do this for you, .NET Core 2.1+ or .NET Framework 4.7.2+:

SignedCms cms = new SignedCms();
cms.Decode(message);
return cms.SignerInfos[0].GetSignature();

Assuming you want the signature from the first signer, of course. (The GetSignature method is what requires net472+)

Other signers or countersigners would also be available, just through different aspects of the object model.

like image 52
bartonjs Avatar answered Oct 23 '25 16:10

bartonjs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!