18283

Sign file using .NET OpenSSL wrapper

I was trying to copy this line

openssl smime -sign -signer <chain_crt_file> -in <infile> -out <outfile> -inkey <privatekey> -outform der

into C# However it didn't turn out to be as easy as I thought. So far I came only this point

OpenSSL.Core.BIO crtBio = OpenSSL.Core.BIO.File("C:/asl/chain.crt", "r"); OpenSSL.Core.BIO keyBio = OpenSSL.Core.BIO.File("C:/asl/keydec.txt", "r"); OpenSSL.X509.X509Chain crt = new OpenSSL.X509.X509Chain(crtBio); OpenSSL.Crypto.RSA key = OpenSSL.Crypto.RSA.FromPrivateKey(keyBio); String str = "test"; byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);

Where (hopefully) I'm importing chain certificate and decoded private key. Now the thing is how to sign a file and export is as DER. OpenSSL.NET wrapper lacks documentation and examples I found on the internet are 'how to encrypt and decrypt messages using public/private key' which is not a case here.

To get started I tried to sign this "test" string (as file in/out should be pretty straightfoward) but I have no clue where to start.

The thing is that I need to <strong>sign</strong> this string thus I will need both key and certificates chain.

Thanks a lot for your help.

Answer1:

To get started I tried to sign this "test" string (as file in/out should be pretty straightfoward) but I have no clue where to start.

The OpenSSL source is probably a good place to start. OpenSSL provides the source for smime in <openssl dir>/apps/smime.c.

OpenSSL's smime utility just calls PKCS7_sign with the appropriate parameters. From around line 688:

else if (operation & SMIME_SIGNERS) { int i; /* If detached data content we only enable streaming if * S/MIME output format. */ if (operation == SMIME_SIGN) { if (flags & PKCS7_DETACHED) { if (outformat == FORMAT_SMIME) flags |= PKCS7_STREAM; } else if (indef) flags |= PKCS7_STREAM; flags |= PKCS7_PARTIAL; p7 = PKCS7_sign(NULL, NULL, other, in, flags); if (!p7) goto end; } ...

With knowledge of PKCS7_sign, you can visit OpenSSL's docs at PKCS7_sign(3). Or, you can hunt for an example.

I don't know about the wrapper you are using.

Recommend

  • Extract public key from certificate x509
  • How to do asymmetric encryption with X509 certificates and C#?
  • Is there a way to redirect stderr to file in Jupyter?
  • Uploading file with WCF streaming, tiny reads from stream
  • How do I go about listing all of the installed packages and versions on mac os x?
  • Apache Lucene - Improving the results of Spell Checker
  • How to correctly get a WinForms Button control to draw custom text
  • How can I get a specific line from a text file? [duplicate]
  • How to read data from a text file if the file location is not known in c#?
  • Proguard Exception java.io.IOException: Duplicate zip entry
  • parsing numbers from a text file
  • Reading XML into Datatable gives incorrect DateTime when the time has Time Zone info
  • How to save dynamically created textboxes and their values
  • Rodeo UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal
  • Getting NullPointer exception with File.listfiles()
  • pip in virtualenv gets ConnectTimeoutError
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Parse a date string in a specific locale (not timezone!)
  • Installed module is empty
  • copying resource to sdcard gives a damaged file in android
  • Spark fat jar to run multiple versions on YARN
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • AES padding and writing the ciphertext to a disk file
  • How to handle AllServersUnavailable Exception
  • JSON with duplicate key names losing information when parsed
  • How do you troubleshoot character encoding problems?
  • Jquery - Jquery Wysiwyg return html as a string
  • 0x202A in filename: Why?
  • AT Commands to Send SMS not working in Windows 8.1
  • retrieve vertices with no linked edge in arangodb
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Sorting a 2D array using the second column C++
  • Python/Django TangoWithDjango Models and Databases