I need to send an encrypted email with a binary attachment from bash. I've read the RFC, and the openssl docs as well as a couple additional posts here in SF to no avail.
So far the process I understand goes like this:
- Create a MIME message
- use openssl smime to encrypt it generating additional headers for the envelope. This should be signed with my own private key but encrypted with the recipient's public key.
- pipe this output to sendmail
- The receiver should be able to decrypt the whole thing in outlook.
However what I'm seeing is a bit of garbled text. If anyone can shine some light where I'm messing up, I'd be thankful.
What follows are the nitty gritty details:
1. MIME Message
From: <FROM>
To: <TO>
Subject: <SUBJECT>
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="<BOUNDARY>"
--<BOUNDARY>
Content-Type: text/plain; charset=utf-8
<TEXT>
--<BOUNDARY>
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename= "<FILENAME>"
<BASE64_DATA>
--<BOUNDARY>
2. The signing & Encrypting:
SIGNED=$(openssl smime -sign -in mime.txt -signer MyPublic.cer -inkey MyPrivate.key)
ENCRYPTED=$(openssl smime -encrypt -subject "Work damn you" RecipientPublic.cer <<< $SIGNED)
3. The Sending
echo "$ENCRYPTED" | sendmail recipient@hush-hush.com