xref: /openbmc/qemu/tests/qemu-iotests/common.tls (revision 5242ef88)
1#!/usr/bin/env bash
2#
3# Helpers for TLS related config
4#
5# Copyright (C) 2018 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21tls_dir="${TEST_DIR}/tls"
22
23tls_x509_cleanup()
24{
25    rm -f "${tls_dir}"/*.pem
26    rm -f "${tls_dir}"/*/*.pem
27    rm -f "${tls_dir}"/*/*.psk
28    rmdir "${tls_dir}"/*
29    rmdir "${tls_dir}"
30}
31
32
33tls_certtool()
34{
35    certtool "$@" 1>"${tls_dir}"/certtool.log 2>&1
36    if test "$?" = 0; then
37      head -1 "${tls_dir}"/certtool.log
38    else
39      cat "${tls_dir}"/certtool.log
40    fi
41    rm -f "${tls_dir}"/certtool.log
42}
43
44tls_psktool()
45{
46    psktool "$@" 1>"${tls_dir}"/psktool.log 2>&1
47    if test "$?" = 0; then
48      head -1 "${tls_dir}"/psktool.log
49    else
50      cat "${tls_dir}"/psktool.log
51    fi
52    rm -f "${tls_dir}"/psktool.log
53}
54
55
56tls_x509_init()
57{
58    (certtool --help) >/dev/null 2>&1 || \
59	_notrun "certtool utility not found, skipping test"
60
61    mkdir -p "${tls_dir}"
62
63    # use a fixed key so we don't waste system entropy on
64    # each test run
65    cat > "${tls_dir}/key.pem" <<EOF
66-----BEGIN RSA PRIVATE KEY-----
67MIIG5AIBAAKCAYEAyjWyLSNm5PZvYUKUcDWGqbLX10b2ood+YaFjWSnJrqx/q3qh
68rVGBJglD25AJENJsmZF3zPP1oMhfIxsXu63Hdkb6Rdlc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Fj+WCHYY/UmPr2/Kf6t7Cxk2K
1023cZ4tqWsiTmBT8Bknmah7L5DrhS+ZBJliDeFAA8fZHdMH0Xjr4UBp9kF90EMTdW1
103Xr5uz7ZrMsYpYQI7mmyqV9SSjUg4iBXwVSoag1iDJ1K8Qg/L7Semgg==
104-----END RSA PRIVATE KEY-----
105EOF
106}
107
108
109tls_x509_create_root_ca()
110{
111    name=${1:-ca-cert}
112
113    cat > "${tls_dir}/ca.info" <<EOF
114cn = Cthulhu Dark Lord Enterprises $name
115ca
116cert_signing_key
117EOF
118
119    tls_certtool \
120        --generate-self-signed \
121        --load-privkey "${tls_dir}/key.pem" \
122        --template "${tls_dir}/ca.info" \
123        --outfile "${tls_dir}/$name-cert.pem"
124
125    rm -f "${tls_dir}/ca.info"
126}
127
128
129tls_x509_create_server()
130{
131    caname=$1
132    name=$2
133
134    # We don't include 'localhost' in the cert, as
135    # we want to keep it unlisted to let tests
136    # validate hostname override
137    mkdir -p "${tls_dir}/$name"
138    cat > "${tls_dir}/cert.info" <<EOF
139organization = Cthulhu Dark Lord Enterprises $name
140cn = iotests.qemu.org
141ip_address = 127.0.0.1
142ip_address = ::1
143tls_www_server
144encryption_key
145signing_key
146EOF
147
148    tls_certtool \
149        --generate-certificate \
150        --load-ca-privkey "${tls_dir}/key.pem" \
151        --load-ca-certificate "${tls_dir}/$caname-cert.pem" \
152        --load-privkey "${tls_dir}/key.pem" \
153        --template "${tls_dir}/cert.info" \
154        --outfile "${tls_dir}/$name/server-cert.pem"
155
156    ln -s "${tls_dir}/$caname-cert.pem" "${tls_dir}/$name/ca-cert.pem"
157    ln -s "${tls_dir}/key.pem" "${tls_dir}/$name/server-key.pem"
158
159    rm -f "${tls_dir}/cert.info"
160}
161
162
163tls_x509_create_client()
164{
165    caname=$1
166    name=$2
167
168    mkdir -p "${tls_dir}/$name"
169    cat > "${tls_dir}/cert.info" <<EOF
170country = South Pacific
171locality =  R'lyeh
172organization = Cthulhu Dark Lord Enterprises $name
173cn = localhost
174tls_www_client
175encryption_key
176signing_key
177EOF
178
179    tls_certtool \
180        --generate-certificate \
181        --load-ca-privkey "${tls_dir}/key.pem" \
182        --load-ca-certificate "${tls_dir}/$caname-cert.pem" \
183        --load-privkey "${tls_dir}/key.pem" \
184        --template "${tls_dir}/cert.info" \
185        --outfile "${tls_dir}/$name/client-cert.pem"
186
187    ln -s "${tls_dir}/$caname-cert.pem" "${tls_dir}/$name/ca-cert.pem"
188    ln -s "${tls_dir}/key.pem" "${tls_dir}/$name/client-key.pem"
189
190    rm -f "${tls_dir}/cert.info"
191}
192
193tls_psk_create_creds()
194{
195    name=$1
196
197    mkdir -p "${tls_dir}/$name"
198
199    tls_psktool \
200	--pskfile "${tls_dir}/$name/keys.psk" \
201	--username "$name"
202}
203