Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract public Certificate from SMIME Message (pkcs7-signature) with OpenSSL

Tags:

How can i extract the public certificate from an smime message (pkcs7-signature) with OpenSSL?

like image 271
x2on Avatar asked Apr 15 '11 14:04

x2on


People also ask

What is OpenSSL CMS?

CMS is used as the key cryptographic component of many other cryptographic standards, such as S/MIME, PKCS #12 and the RFC 3161 digital timestamping protocol. OpenSSL is open source software that can encrypt, decrypt, sign and verify, compress and uncompress CMS documents.


2 Answers

With the command-line tool, assuming the S/MIME message itself is in file message:

openssl smime -verify -in message -noverify -signer cert.pem -out textdata 

This writes the signer certificate (as embedded in the signature blob) into cert.pem, and the message text data in the textdata file.

Alternatively, you can save the signature blob as an independent file (it is just a kind of attachment, so any mailer application or library should be able to do that. Then, assuming that the said blob is in a file named smime.p7s, use:

openssl pkcs7 -in smime.p7s -inform DER -print_certs 

which will print out all certificates which are embedded in the PKCS#7 signature. Note that there can be several: the signer's certificate itself, and any extra certificates that the signer found fit to include (e.g. intermediate CA certificates which may help in validating his certificate).

like image 63
Thomas Pornin Avatar answered Sep 18 '22 09:09

Thomas Pornin


Or just:

cat message.eml | openssl smime -pk7out | openssl pkcs7 -print_certs > senders-cert.pem 
like image 35
Franta Avatar answered Sep 21 '22 09:09

Franta