-
Notifications
You must be signed in to change notification settings - Fork 1
/
poudriere-key.sh
executable file
·156 lines (134 loc) · 3.94 KB
/
poudriere-key.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/sh
#
# Copyright (c) 2022 Konrad Witaszczyk
#
# This software was developed by the University of Cambridge Department of
# Computer Science and Technology with support from Innovate UK project 105694,
# "Digital Security by Design (DSbD) Technology Platform Prototype".
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
KEY_PREFIX="pkg.cheribsd.org"
KEY_DIR="${HOME}/keys/pkg.cheribsd.org"
KEY_LATEST="${KEY_DIR}/${KEY_PREFIX}.latest"
KEY_PRIVATE="${KEY_DIR}/${KEY_PREFIX}.latest.priv"
KEY_PUBLIC="${KEY_DIR}/${KEY_PREFIX}.latest.pub"
die() {
echo "${*}" >&2
exit 1
}
usage() {
echo "usage: ${0} generate|sign"
exit 1
}
key_generate() {
local _date _keydir _keyname _keypath
_date="$(date "+%Y%m%d")"
if [ $? -ne 0 ]; then
die "Unable to calculate a date."
fi
_keyname="${KEY_PREFIX}.${_date}"
_keydir="${KEY_DIR}/${_keyname}"
_keypath="${_keydir}/${_keyname}"
if [ -d "${_keydir}" ]; then
die "Directory ${_keydir} already exists. Remove it and try again."
fi
mkdir -p "${_keydir}"
if [ $? -ne 0 ]; then
die "Unable to create a key directory."
fi
openssl genrsa -out "${_keypath}.priv" 2048
if [ $? -ne 0 ]; then
die "Unable to generate a key."
fi
chmod 0400 "${_keypath}.priv"
if [ $? -ne 0 ]; then
die "Unable to change permissions."
fi
openssl rsa -in "${_keypath}.priv" -out "${_keypath}.pub" -pubout
if [ $? -ne 0 ]; then
die "Unable to generate a public key."
fi
_fingerprint="$(sha256 -q "${_keypath}.pub")"
if [ $? -ne 0 ]; then
die "Unable to calculate a fingerprint."
fi
cat << EOF >"${_keypath}"
function: "sha256"
fingerprint: "${_fingerprint}"
EOF
if [ $? -ne 0 ]; then
die "Unable to store a fingerprint."
fi
ln -sf "${_keyname}/${_keyname}" "${KEY_LATEST}"
if [ $? -ne 0 ]; then
die "Unable to create a symlink to the latest key."
fi
ln -sf "${_keyname}/${_keyname}.pub" "${KEY_PUBLIC}"
if [ $? -ne 0 ]; then
die "Unable to create a symlink to the latest public key."
fi
ln -sf "${_keyname}/${_keyname}.priv" "${KEY_PRIVATE}"
if [ $? -ne 0 ]; then
die "Unable to create a symlink to the latest private key."
fi
echo
echo "You can find the new key in: ${_keydir}."
echo
echo "Remember to add ${_keypath} to share/keys/pkg/trusted/ in CheriBSD."
}
key_sign() {
local _checksum
read -t 2 _checksum
if [ -z "${_checksum}" ]; then
die "Checksum cannot be empty."
fi
echo "SIGNATURE"
echo -n "${_checksum}" |
openssl dgst -sign "${KEY_PRIVATE}" -sha256 -binary
if [ $? -ne 0 ]; then
die "Unable to generate a signature."
fi
echo
echo "CERT"
cat "${KEY_PUBLIC}"
if [ $? -ne 0 ]; then
die "Unable to read the latest public key."
fi
echo "END"
}
main() {
local _cmd
_cmd="${1}"
case "${_cmd}" in
generate)
key_generate
;;
sign)
key_sign
;;
*)
usage
;;
esac
}
main "${@}"