Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have a code written in C# to sign and envelop a string to be sent to a web service, this code receives a certificate in the format .pfx and the certificate password.

C#
string myString = "Hello World";
X509Certificate2 cert = new X509Certificate2(@"<MY PFX FILE>", "<MY PASS>");
CmsSigner signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, cert);
signer.DigestAlgorithm = new Oid("<MY OID>");

ContentInfo content = new ContentInfo(new Oid("<MY OID>"), new System.Text.UTF8Encoding().GetBytes(myString));
SignedCms signedCms = new SignedCms(content, false);

signedCms.ComputeSignature(signer, false);

var asig = signedCms.Encode();


Now I need to write a code that does exactly the same thing, but in C, after performing several searches on google, I found some libs. that work with this type of information, and in this search I found OpenSSL.

But I did not find how I could sign and envelop the message, just like I am doing in C#. Thanks in advance if anyone can help me, how can I do the same thing I do in C# in C.

What I have tried:

I did some tests with lib "ChilKat", C RSA Sign Using Private Key from .pfx/.p12 to Base64 Signature[^], when I encrypted the same information with my C# program and with this C++ program, the output is complete different, and, I did not managed how to use the OID digest algorithm...

With OpenSSL, I didn't find how I can sign a message use PKCS#7 certification.
Posted
Updated 15-Mar-17 11:09am
Comments
Garth J Lancaster 3-Mar-17 20:06pm    
I don't envy your task - there are such a lot of parameters/variables that have to be 'just so' on each side - it's bad enough using a 'common library' yet alone c# vs OpenSSL (for example)

I've done 'similar', but then I cheated - I found a library in c++, I wrote a high level wrapper for the functionals I required of it, in c++, exported that wrapper as a dll, and then used a P/Invoke Interop layer from c# - it meant that I was using apples and apples on each side.

This comment likely doesn't help you at all - and I've turned my solution 'the other way around' basing it on the c++ side rather than the c# .. I have no doubt it is doable in OpenSSL and C#, but it will likely involve a large amount of research and experimentation

1 solution

There are quite a few steps that need to be done when using OpenSSL:
1. First read the pfx data (encoded in DER format) from a source (e.g. file)
2. Parse it to get its contents (private key, cert and CAs)
3. Sign data using the private key

OpenSSL provides a set of functions e.g. d2i_PKCS12_fp() to import DER data to its internal data structure. d2i_PKCS12_fp reads PKCS12 data in DER format from a file (specified by file pointer).

Use PKCS12_parse() to get private key.
See https://www.openssl.org/docs/man1.1.0/crypto/PKCS12_parse.html

You may use EVP_Sign (or its variants) to sign your data. However, this doesn;t create a PKCS#7 signature.

Use PKCS7_sign() to create a PKCS#7 signature.
See https://www.openssl.org/docs/man1.1.0/crypto/PKCS7_sign.html

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900