Debian Codegraph @

tests/basic

raw
1 #!/bin/bash
2
3 # simple set of tests to exercise the msva.
4
5 # these tests currently depend on the user having the following tools
6 # installed locally:
7
8 # monkeysphere (for pem2openpgp)
9 # openssl (for openssl req)
10 # openssh-client (for ssh-keygen)
11 # gpg (for obvious reasons)
12 # bash (yes, this test script isn't posix-compliant)
13
14 # note that this test requires the ability to bind on the loopback
15 # interface, which might not be possible in some build environments.
16
17 # Author: Daniel Kahn Gillmor
18 # Copyright: 2010
19 # License: This is licensed under the GPL v3 or later
20 # (see the top-level COPYING file in this distribution)
21
22 set -e
23
24 srcdir=$(dirname $0)/..
25
26 REPS=5
27
28 CERTTYPES="x509pem x509der opensshpubkey rfc4716 openpgp4fpr"
29
30 printf "testing %d reps of simple/quick true/false:\n" "$REPS"
31 for n in $(seq 1 "$REPS") ; do
32 "${srcdir}"/test-msva msva-perl true
33 printf "+"
34 ! "${srcdir}"/test-msva msva-perl false
35 printf "-"
36 done
37 printf "\ndone\n"
38
39 WORKDIR=$(mktemp -d)
40 mkdir -m 0700 "${WORKDIR}/"{pkc,sec,gnupg}
41 touch "${WORKDIR}/gnupg/gpg.conf"
42 export GNUPGHOME="${WORKDIR}/gnupg"
43
44 if gpg --quick-random --version ; then
45 GPGQR=--quick-random
46 elif gpg --debug-quick-random --version ; then
47 GPGQR=--debug-quick-random
48 else
49 GPGQR=
50 fi
51
52 # make a CA
53 printf "Key-Type: RSA\nKey-Length: 1024\nKey-Usage: sign\nName-Real: MSVA Test Certificate Authority (DO NOT USE!)\n" | gpg --batch --no-tty $GPGQR --gen-key
54
55 # make 3 websites (X, Y, and Z) with self-signed certs:
56 for name in x y z ; do
57 openssl req -x509 -subj "/CN=${name}.example.net/" -nodes -sha256 -newkey rsa:1024 -keyout "${WORKDIR}/sec/${name}.key" -outform DER -out "${WORKDIR}/pkc/${name}.x509der"
58 chmod 0400 "${WORKDIR}/sec/${name}.key"
59 openssl x509 -inform DER -outform PEM < "${WORKDIR}/pkc/${name}.x509der" > "${WORKDIR}/pkc/${name}.x509pem"
60 ssh-keygen -y -P '' -f "${WORKDIR}/sec/${name}.key" > "${WORKDIR}/pkc/${name}.opensshpubkey"
61 ssh-keygen -e -P '' -f "${WORKDIR}/sec/${name}.key" > "${WORKDIR}/pkc/${name}.rfc4716"
62 done
63
64 # make 2 client certs (A and B) with self-signed certs
65 for name in a b ; do
66 openssl req -x509 -subj "/eMail=${name}@example.net/CN=${name}/" -nodes -sha256 -newkey rsa:1024 -keyout "${WORKDIR}/sec/${name}.key" -outform DER -out "${WORKDIR}/pkc/${name}.x509der"
67 chmod 0400 "${WORKDIR}/sec/${name}.key"
68 openssl x509 -inform DER -outform PEM < "${WORKDIR}/pkc/${name}.x509der" > "${WORKDIR}/pkc/${name}.x509pem"
69 ssh-keygen -y -P '' -f "${WORKDIR}/sec/${name}.key" > "${WORKDIR}/pkc/${name}.opensshpubkey"
70 ssh-keygen -e -P '' -f "${WORKDIR}/sec/${name}.key" > "${WORKDIR}/pkc/${name}.rfc4716"
71 done
72
73 # translate X and Y's keys into OpenPGP cert
74 for name in x y; do
75 uid="https://${name}.example.net"
76 PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$uid" < "${WORKDIR}/sec/${name}.key" | gpg --import
77 # export fingerprint for openpgp4fpr
78 gpg --with-colons --fingerprint "=${uid}" | grep '^fpr:' | cut -d: -f10 > "${WORKDIR}/pkc/${name}.openpgp4fpr"
79 done
80 # touch an empty openpgp4fpr file for z, who is not supposed to be in
81 # the monkeysphere at all, and therefore has no openpgp4fpr
82 touch "${WORKDIR}/pkc/z.openpgp4fpr"
83 # and the same for the clients A and B
84 for name in a b; do
85 uid="${name} <${name}@example.net>"
86 # make user keys 'a' and 's' capable
87 PEM2OPENPGP_USAGE_FLAGS=authenticate,sign pem2openpgp "$uid" < "${WORKDIR}/sec/${name}.key" | gpg --import
88 # export fingerprint for openpgp4fpr
89 gpg --with-colons --fingerprint "=${uid}" | grep '^fpr:' | cut -d: -f10 > "${WORKDIR}/pkc/${name}.openpgp4fpr"
90 done
91
92 runtests() {
93 # X should not validate as X or Y or Z:
94 for name in x y z; do
95 for ctype in $CERTTYPES; do
96 ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "${name}.example.net" "${ctype}" < "${WORKDIR}/pkc/x.${ctype}"
97 echo
98 done
99 done
100 # A shouldn't validate as A or B:
101 for name in a b; do
102 for ctype in $CERTTYPES; do
103 ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "${name} <${name}@example.net>" "${ctype}" client < "${WORKDIR}/pkc/a.${ctype}"
104 echo
105 done
106 done
107
108 # certify X and A's OpenPGP cert with CA
109 gpg --batch --yes --sign-key https://x.example.net
110 gpg --batch --yes --sign-key [email protected]
111
112 echo "Testing bad data:"
113 # it should fail if we pass it the wrong kind of data:
114 ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https x.example.net "x509der" < "${WORKDIR}/pkc/x.x509pem"
115 echo
116 ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https x.example.net "x509pem" < "${WORKDIR}/pkc/x.x509der"
117 echo
118 echo "Done testing bad data."
119
120 for ctype in $CERTTYPES; do
121 # X should now validate as X
122 "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https x.example.net "${ctype}" < "${WORKDIR}/pkc/x.${ctype}"
123 echo
124 "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https 'a <[email protected]>' "${ctype}" client < "${WORKDIR}/pkc/a.${ctype}"
125 # also test "e-mail" context
126 "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent e-mail 'a <[email protected]>' "${ctype}" < "${WORKDIR}/pkc/a.${ctype}"
127
128 # but X should not validate as Y or Z:
129 for name in x y z; do
130 ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "${name}.example.net" "${ctype}" < "${WORKDIR}/pkc/x.${ctype}"
131 echo
132 done
133 # and A shouldn't validate as B:
134 ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "b <[email protected]>" "${ctype}" client < "${WORKDIR}/pkc/a.${ctype}"
135 echo
136
137 # neither Y nor Z should validate as any of them:
138 for src in y z; do
139 for targ in x y z; do
140 ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "${targ}.example.net" "${ctype}" < "${WORKDIR}/pkc/${src}.${ctype}"
141 echo
142 done
143 done
144 # B should also still not validate as itself:
145 ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "b <[email protected]>" "${ctype}" client < "${WORKDIR}/pkc/b.${ctype}"
146 echo
147 done
148 }
149
150 set -x
151 MSVA_KEYSERVER_POLICY=never runtests
152 set +x
153 echo "Completed all tests as expected!"
154
155 rm -rf "$WORKDIR"