#!/usr/bin/bash

##
#   run_smime TESTS
##

#-----------------------------------------------------------#
red() {
     echo -en "\\0033[1;31m"
     echo "$1"
     echo -en "\\0033[0;39m"
}

green() {
     echo -en "\\0033[1;32m"
     echo "$1"
     echo -en "\\0033[0;39m"

}

orange() {
     echo -en "\\0033[1;33m"
     echo "$1"
     echo -en "\\0033[0;39m"

}
#-----------------------------------------------------------#

/bin/mkdir -p clsmime
echo "Preparing clsmime for canned password"

VER=$(/bin/clsmime -version)
green  "Found clsmime version ${VER}"
orange "You need at least clsmime version 1.2 for this test"

if test -x /lib/cryptlib/examples/print_test_password ; then
     echo "FOUND /lib/cryptlib/examples/print_test_password" 
     cp /bin/clsmime clsmime/smime; /bin/sed -i '103s/^/ASKPASS = "..\/print_test_password"/' clsmime/smime
else
     if test -r /lib/cryptlib/examples/print_test_password.c ; then
          echo "COMPILE /lib/cryptlib/examples/print_test_password.c" 
          gcc -o print_test_password print_test_password.c
     else
          cp /bin/clsmime clsmime/smime; /bin/sed -i '103s/^/ASKPASS = "echo deadbeefdead"/' clsmime/smime
          echo "Use 'echo deadbeefdead'"
          red "This does not work with claes version 1.3"
     fi	  
fi

cd clsmime
cp ../fixed/bsd .
rm -f err log


DESC="\nENcrypt a file for James: bsd ... "
cp ../fixed/James.cert .
rm -f bsd.smime
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./smime -debug encrypt bsd James.cert >> log 2>> err
echo "--> : $?"  
if ls -l bsd.smime ; then green "OK"; else red "FAILED"; fi


DESC="\nDEcrypt a small message made for James :  bsd.smime ... "
cp ../fixed/James.p15 .
rm -f bsd
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./smime -debug decrypt bsd.smime James >> log 2>> err
echo "--> : $?"  
if ls -l bsd ; then green "OK"; else red "FAILED"; fi

### clsmime does not handle DSA based certificates (Paul.cert)


DESC="\nSign a file with James' RSA certificate: bsd ... "
cp ../fixed/bsd .
cp ../fixed/testkey3.p15.KEYID ./James.p15.KEYID
rm -f bsd.sig
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./smime -debug sign bsd James >> log 2>> err
echo "--> : $?"  
cp bsd.sig bsd.clearsign
if ls -l bsd.clearsign ; then green "OK"; else red "FAILED"; fi


DESC="\nVerify the signature made by James :  bsd.clearsign ... "
cp ../fixed/CAkey1.cert.pem .
rm -f bsd.clearsign.verified
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./smime -debug verify bsd.clearsign CAkey1.cert.pem >> log 2>> err
echo "--> : $?"  
if diff  bsd bsd.clearsign.verified ; then green "OK"; else red "FAILED"; fi


DESC="\nSign a message by James as detached signature :  bsd ... "
rm -f bsd.sig*
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./smime -debug -detach sign bsd James >> log 2>> err
echo "--> : $?"  
cp bsd.sig bsd.detached
if ls -l bsd.detached ; then green "OK"; else red "FAILED"; fi


DESC="\nVerify the detached signature made by James :  bsd.detached ... "
rm -f bsd.detached.verified
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./smime -debug verify bsd.detached CAkey1.cert.pem >> log 2>> err
echo "--> : $?"  
if diff  bsd bsd.detached.verified ; then green "OK"; else red "FAILED"; fi


### exchange between OpenSSL and clSMIME

echo
orange  "exchange between OpenSSL and clSMIME"

cp bsd bsd.original
cp ../fixed/*SMIME .
DESC="\nENcrypt with OpenSSL and DEcrypt with clsmime (James) ... "
cp ../fixed/James.cert .
rm -f bsd.smime
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./encryptSMIME bsd James.cert >> log 2>> err
echo "--> : $?"  
if ls -l bsd.smime ; then green "OpenSSL encryption OK"; else red "OpenSSL encryption FAILED"; fi
rm -f bsd
./smime -debug decrypt bsd.smime James >> log 2>> err
echo "--> : $?"  
ls -l bsd
if diff bsd.original bsd ; then green "clsmime decryption OK"; else red "clsmime decryption FAILED"; fi


DESC="\nENcrypt with clsmime and DEcrypt with OpenSSL (Harry) ... "
cp ../fixed/Harry.cert .
cp ../fixed/Harry.key .
rm -f bsd.smime
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./smime -debug encrypt bsd Harry.cert >> log 2>> err
echo "--> : $?"  
if ls -l bsd.smime ; then green "clsmime encryption OK"; else red "clsmime encryption FAILED"; fi
rm -f bsd.smime.clear
./decryptSMIME bsd.smime Harry.cert Harry.key >> log 2>> err
echo "--> : $?"  
ls -l bsd.smime.clear
if diff bsd.original bsd.smime.clear ; then green "OpenSSL decryption OK"; else red "OpenSSL decryption FAILED"; fi


cp bsd bsd.original
DESC="\nSign with OpenSSL and Verify with clsmime (Harry) ... "
cp ../fixed/James.cert .
cp ../fixed/testCC.cert .
rm -f bsd.sig
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./signSMIME bsd Harry.cert Harry.key  >> log 2>> err
echo "--> : $?"  
if ls -l bsd.sig ; then green "OpenSSL signature OK"; else red "OpenSSL signature FAILED"; fi
tail -4 bsd.sig
rm -f bsd.sig.verified
./smime -debug  verify bsd.sig testCC.cert >> log 2>> err
echo "--> : $?"  
ls -l bsd.sig.verified
if diff bsd.original bsd.sig.verified ; then green "clsmime verification OK"; else red "clsmime verification FAILED"; fi


DESC="\nSign with clsmime and Verify with OpenSSL (James) ... "
rm -f bsd.sig
cp ../fixed/CAkey1.cert.pem .
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./smime -debug sign -binary bsd James >> log 2>> err
echo "--> : $?"  
if ls -l bsd.sig ; then green "clsmime signature OK"; else red "clsmime signature FAILED"; fi
rm -f bsd.sig.verified
./verifySMIME bsd.sig James.cert CAkey1.cert.pem >> log 2>> err
echo "--> : $?"  
ls -l bsd.sig.verified
if diff bsd.original bsd.sig.verified ; then green "OpenSSL verification OK"; else red "OpenSSL verification FAILED"; fi

### Evolution tests
echo
orange  "Running Evolution tests ..."

DESC="\nDEcrypt a Thunderbird encrypted message for JOE"
rm -f Joe.clear
cp ../files/EVO-enc-Joe.eml .
cp ../certs/Joe.p15 .
rm -f bsd.smime
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
echo Joe.clear | ./smime -debug  decrypt EVO-enc-Joe.eml Joe >> log 2>> err
echo "--> : $?"
if ls -l Joe.clear; then green "OK"; else red "FAILED"; fi


DESC="\nDEcrypt a Thunderbird encrypted message for JOE signed by Tom"
rm -f Joe.sigclear Joe.sigclear.verified
cp ../files/EVO-sig-enc-Joe.eml .
cp ../certs/Tom.cert .
rm -f bsd.smime
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
echo Joe.sigclear | ./smime -debug  decrypt EVO-sig-enc-Joe.eml Joe >> log 2>> err
echo "--> : $?"
if ls -l Joe.sigclear; then green "decryption OK"; else red "decryption FAILED"; fi
./smime -debug  verify Joe.sigclear testCC.cert >> log 2>> err
echo "--> : $?"
if ls -l Joe.sigclear.verified; then green "verification OK"; else red "verification FAILED"; fi



DESC="\nVerify a Evolution message signed by Tom ... "
rm -f Tom.sig.eml.verified
cp ../files/EVO-signed-Tom.eml ./Tom.sig.eml
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./smime -debug verify Tom.sig.eml testCC.cert >> log 2>> err
echo "--> : $?"
if ls -l Tom.sig.eml.verified ; then green "OK"; else red "FAILED"; fi


### run tests on Peters smime test files

echo
orange "run verification tests on Peter's smime test files"
find ../test/smime -name "signed*" > list.good
mkdir -p peter
cp $(cat list.good) peter
rm -f peter/signed2*
rm -f peter/signed5.p7s
rm -f peter/*.verified*
for EML in $(find peter -type f)
do 
     echo
     echo "#####################################################" >> log
     echo "####    $EML   ####" >> log
     echo "#####################################################" >> err
     echo "####    $EML   ####" >> err
     echo "./smime -debug verify $EML -certchain"  
     echo y | ./smime -debug  verify $EML -certchain >> log 2>> err 
     RET=$?
     echo "--> EXIT CODE is: $RET" >> log 
     echo "--> EXIT CODE is: $RET" >> err
     cat $EML.verified >> ${TEST}.err 2>/dev/null
     echo "--> EXIT CODE is: $RET" 

     if (( $RET == 0 )) >> log
     then
          green "OK : $EML"
     else
          red "FAILED : $EML"
     fi
     echo
done



echo
orange "clsmime tests finished."
echo

ls -l
