Last active
January 15, 2018 23:13
-
-
Save yottatsa/808349520e45d3b91f075791709f3c37 to your computer and use it in GitHub Desktop.
Encrypt a message with s/mime and send key password back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias openssl=/Users/yottatsa/.local/homebrew/Cellar/openssl/1.0.2l/bin/openssl | |
conf () { | |
echo " | |
[ req ] | |
default_bits = 4096 | |
distinguished_name = req_distinguished_name | |
prompt = no | |
[ req_distinguished_name ] | |
CN = $1 | |
emailAddress = $1 | |
" | |
} | |
ca () { | |
echo " | |
[ usr_cert ] | |
basicConstraints = CA:TRUE" | |
} | |
usr_cert () { | |
echo " | |
[ usr_cert ] | |
basicConstraints = CA:FALSE | |
authorityKeyIdentifier = keyid | |
subjectKeyIdentifier = hash | |
keyUsage = critical, digitalSignature, keyEncipherment | |
extendedKeyUsage = critical, emailProtection | |
subjectAltName = email:copy | |
authorityKeyIdentifier = keyid | |
subjectKeyIdentifier = hash" | |
} | |
generatesend () { | |
if ! [ -f $1.crt ] | |
then | |
openssl rand -hex 16 > $1.password | |
openssl req -batch -newkey rsa:4096 -keyout $1.key -out $1.csr -passout file:$1.password -config <(conf $1; usr_cert) | |
openssl req -new -x509 -days 7 -key $1.key -out $1_ca.crt -passin file:$1.password -batch -config <(conf $1; ca) | |
openssl x509 -req -days 365 -in $1.csr -CA $1_ca.crt -CAkey $1.key -set_serial 1 -out $1.crt -setalias "Self Signed S/MIME for $1" -addtrust emailProtection -addreject clientAuth -addreject serverAuth -trustout -passin file:$1.password | |
openssl pkcs12 -export -in $1.crt -certfile $1_ca.crt -inkey $1.key -out $1.p12 -name "Self Signed S/MIME for $1" -passin file:$1.password -passout file:<(cat $1.password) -aes256 | |
(echo Content-Transfer-Encoding: base64; echo "Content-Disposition: attachment;filename=\"$1.p12\""; echo "Content-Type: application/x-pkcs12;name=\"$1.p12\""; echo; base64 -b 64 < $1.p12) > $1_p12.mime | |
(echo "Subject:"; echo "MIME-Version: 1.0"; echo; cat $1.password) |\ | |
openssl smime -sign -signer $1.crt -inkey $1.key -passin file:$1.password |\ | |
openssl smime -encrypt -aes256 -from $1 -to $sender -subject "Password for $1" -out $1_password.eml $cert | |
rm -f $1_ca.crt $1.csr $1.key $1.password $1.p12 | |
fi | |
( | |
echo From: $sender | |
echo To: $1 | |
echo "MIME-Version: 1.0" | |
echo "Content-Type: multipart/mixed;boundary=\"mime\"" | |
echo "Subject: encrypted" | |
echo | |
echo This is encrypted message | |
echo | |
echo "--mime" | |
openssl smime -encrypt -aes256 -in in.eml $1.crt | |
echo "--mime" | |
cat $1_p12.mime | |
echo | |
echo "--mime--" | |
) > $1.eml | |
} | |
sender=$1 | |
shift | |
cert=$1 | |
shift | |
tee > in.eml | |
for email | |
do | |
generatesend $email email.txt | |
done | |
rm -f in.eml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment