File indexing completed on 2024-06-16 05:01:53

0001 #!/bin/sh
0002 
0003 # Copyright (C) 2015 Stephan Platz <trojita@paalsteek.de>
0004 #
0005 # This file is part of the Trojita Qt IMAP e-mail client,
0006 # http://trojita.flaska.net/
0007 #
0008 # This program is free software; you can redistribute it and/or
0009 # modify it under the terms of the GNU General Public License as
0010 # published by the Free Software Foundation; either version 2 of
0011 # the License or (at your option) version 3 or any later version
0012 # accepted by the membership of KDE e.V. (or its successor approved
0013 # by the membership of KDE e.V.), which shall act as a proxy
0014 # defined in Section 14 of version 3 of the license.
0015 #
0016 # This program is distributed in the hope that it will be useful,
0017 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0018 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0019 # GNU General Public License for more details.
0020 #
0021 # You should have received a copy of the GNU General Public License
0022 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
0023 
0024 set -e
0025 
0026 GPG=$(gpgconf --check-programs | grep "^gpg:" | cut -d : -f 3)
0027 
0028 SRCDIR=$1
0029 
0030 if [[ ! -f ${SRCDIR}/CMakeLists.txt ]] ; then
0031     echo "Usage: ${0} path/to/trojita/sources"
0032     exit 1
0033 fi
0034 
0035 KEYDIR="./keys"
0036 SED_QUOTE='s/^\(.*\)$/\t"\1\\r\\n"/g'
0037 
0038 # params: receiver, plaintext
0039 # output: quoted encrypted messages
0040 _encrypt() {
0041         ENC=$(echo -en "$2" | ${GPG} -e --armor -r $1)
0042         # quote each line and add newlines
0043         echo "${ENC}" | sed -e ${SED_QUOTE}
0044 }
0045 
0046 # params: signer, plaintext
0047 gpg_sign() {
0048         CYP=$(echo -en "${2}" | ${GPG} --sign --detach-sig --armor --local-user $1)
0049         echo "${CYP}" | sed -e ${SED_QUOTE}
0050 }
0051 
0052 # params: signer, recipient, plaintext
0053 gpg_sign_encrypt() {
0054         ENC=$(echo -en "${3}" | ${GPG} --sign --encrypt --detach-sig --armor --local-user $1 --recipient $2)
0055         echo "${ENC}" | sed -e ${SED_QUOTE}
0056 }
0057 
0058 [ -d "$KEYDIR" ] && rm -r "$KEYDIR"
0059 mkdir -m700 "$KEYDIR"
0060 
0061 export GNUPGHOME="$KEYDIR"
0062 
0063 # generate keys
0064 LD_PRELOAD=./libfake-dev-random.so ${GPG} --batch --quiet --gen-key ${SRCDIR}/tests/Cryptography/batch-keygen
0065 
0066 HEAD="// This file is autogenerated by tests/Cryptography/keygen.sh
0067 // Do not edit manually.
0068 
0069 #ifndef CRYPTOGRAPHY_DATA_H
0070 #define CRYPTOGRAPHY_DATA_H"
0071 TAIL="#endif // CRYPTOGRAPHY_DATA_H"
0072 
0073 # generate messages
0074 # simple valid message encrypted to valid@
0075 VALID=$(_encrypt valid@test.trojita.flaska.net plaintext)
0076 
0077 # for an invalid message replace some characters. one of vwxyz should occur in every case
0078 INVALID=$(echo "$VALID" | tr vwxyz lmnop)
0079 
0080 # for an expired message we use a key that will be expired after the creation of the messages
0081 EXPIRED=$(_encrypt expired@test.trojita.flaska.net plaintext)
0082 
0083 # for a message with missing key we use a key that will be deleted from the keyring after message generation
0084 UNKNOWN=$(_encrypt unknown@test.trojita.flaska.net plaintext)
0085 
0086 # valid signature
0087 PLAINTEXT_FOR_SIGNING="Content-Type: text/plain\r\n\r\nplaintext\r\n"
0088 SIGNATURE_ME=$(gpg_sign valid@test.trojita.flaska.net "${PLAINTEXT_FOR_SIGNING}")
0089 
0090 echo "$HEAD
0091 
0092 const QByteArray encValid(
0093 $VALID
0094 );
0095 
0096 const QByteArray encInvalid(
0097 $INVALID
0098 );
0099 
0100 const QByteArray encExpired(
0101 $EXPIRED
0102 );
0103 
0104 const QByteArray encUnknown(
0105 $UNKNOWN
0106 );
0107 
0108 const QByteArray sigFromMe(
0109 $SIGNATURE_ME
0110 );
0111 
0112 $TAIL" > crypto_test_data.h
0113 
0114 # expire key
0115 echo -n "key *\nexpire\nseconds=1\nsave\n" | ${GPG} --no-tty --quiet --command-fd 0 --edit-key "expired@test.trojita.flaska.net"
0116 
0117 # extract fingerprint of key to be deleted
0118 FINGERPRINT="$(${GPG} --quiet --no-tty --list-keys --with-colons --fingerprint unknown@test.trojita.flaska.net | grep fpr | head -n 1 | cut -d : -f 10)"
0119 
0120 # delete key
0121 yes | DISPLAY="" ${GPG} --quiet --batch --no-tty --delete-secret-and-public-key --yes --command-fd 0 --passphrase-fd 0 "${FINGERPRINT}"
0122 
0123 #TODO: cleanup: we only want the secring and the keyring in test_keys
0124 if [[ -e "$KEYDIR"/S.gpg-agent ]]; then
0125         rm "$KEYDIR"/S.gpg-agent
0126 fi