3 # amcrypt-ossl-asym.sh - asymmetric crypto helper using OpenSSL
4 # Usage: amcrypt-ossl-asym.sh [-d]
6 # Copyright © 2006 Ben Slusky <sluskyb@paranoiacs.org>
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # Keys can be generated with the standard OpenSSL commands, e.g.:
25 # $ openssl genrsa -aes128 -out backup-privkey.pem 1024
26 # Generating RSA private key, 1024 bit long modulus
28 # Enter pass phrase for backup-privkey.pem: <ENTER YOUR PASS PHRASE>
29 # Verifying - Enter pass phrase for backup-privkey.pem: <ENTER YOUR PASS PHRASE>
31 # $ openssl rsa -in backup-privkey.pem -pubout -out backup-pubkey.pem
32 # Enter pass phrase for backup-privkey.pem: <ENTER YOUR PASS PHRASE>
36 # change these as needed
37 OPENSSL= # whatever's in $PATH
38 CIPHER=aes-256-cbc # see `openssl help` for more ciphers
39 AMANDA_HOME=~@CLIENT_LOGIN@
40 RANDFILE=$AMANDA_HOME/.rnd
42 PASSPHRASE=$AMANDA_HOME/.am_passphrase # optional
43 PRIVKEY=$AMANDA_HOME/backup-privkey.pem
44 PUBKEY=$AMANDA_HOME/backup-pubkey.pem
46 # where might openssl be?
47 PATH=/bin:/usr/bin:/usr/local/bin:/usr/ssl/bin:/usr/local/ssl/bin
49 MAGIC='AmAnDa+OpEnSsL'
51 WORKDIR="/tmp/.${ME}.$$"
54 if [ -z "${OPENSSL:=`which openssl`}" ]; then
55 echo "${ME}: openssl not found" >&2
57 elif [ ! -x "${OPENSSL}" ]; then
58 echo "${ME}: can't execute openssl (${OPENSSL})" >&2
62 if [ -n "${PASSPHRASE}" ]; then
63 # check the openssl version. if it's too old, we have to handle
64 # the pass phrase differently.
65 OSSL_VERSION=`eval \"${OPENSSL}\" version |cut -d\ -f2`
66 case "${OSSL_VERSION}" in
67 ''|0.[0-8].*|0.9.[0-6]*|0.9.7|0.9.7[a-c]*)
68 echo "${ME}: ${OPENSSL} is version ${OSSL_VERSION}" >&2
69 echo "${ME}: Using pass phrase kluge for OpenSSL version >=0.9.7d" >&2
75 mkdir -m 700 "${WORKDIR}"
77 echo "${ME}: failed to create temp directory" >&2
82 trap "rm -rf \"${WORKDIR}\"" 0 1 3 15
84 # we'll need to pad the datastream to a multiple of the cipher block size
85 # prior to encryption and decryption. 96 bytes (= 768 bits) should be good
88 perl -pe 'BEGIN { $bs = 96; $/ = \8192 } $nbytes = ($nbytes + length) % $bs; END { print "\0" x ($bs - $nbytes) }'
92 # generate a random printable cipher key (on one line)
93 echo `"${OPENSSL}" rand -base64 80` >"${WORKDIR}/pass"
95 # encrypt the cipher key using the RSA public key
96 "${OPENSSL}" rsautl -encrypt -in "${WORKDIR}/pass" -out "${WORKDIR}/pass.ciphertext" -pubin -inkey "${PUBKEY}" -pkcs
97 [ $? -eq 0 ] || return 1
102 # print the encrypted cipher key, preceded by size
103 ls -l "${WORKDIR}/pass.ciphertext" | awk '{ printf("%-10d", $5) }'
104 cat "${WORKDIR}/pass.ciphertext"
106 # encrypt data using the cipher key and print
107 pad | "${OPENSSL}" enc "-${CIPHER}" -nopad -e -pass "file:${WORKDIR}/pass" -nosalt
108 [ $? -eq 0 ] || return 1
113 magicsize=`printf %s "${MAGIC}" | wc -c | sed 's/^ *//'`
114 magic=`dd bs=$magicsize count=1 2>/dev/null`
115 if [ "$magic" != "${MAGIC}" ]; then
116 echo "${ME}: bad magic" >&2
120 # read size of encrypted cipher key
121 n=`dd bs=10 count=1 2>/dev/null`
122 [ $n -gt 0 ] 2>/dev/null
123 if [ $? -ne 0 ]; then
124 echo "${ME}: bad header" >&2
128 # read the encrypted cipher key
129 dd "of=${WORKDIR}/pass.ciphertext" bs=$n count=1 2>/dev/null
131 # decrypt the cipher key using the RSA private key
132 if [ "${PASS_FROM_STDIN}" = yes ]; then
133 "${OPENSSL}" rsautl -decrypt -in "${WORKDIR}/pass.ciphertext" -out "${WORKDIR}/pass" -inkey "${PRIVKEY}" -pkcs < "${PASSPHRASE}"
135 "${OPENSSL}" rsautl -decrypt -in "${WORKDIR}/pass.ciphertext" -out "${WORKDIR}/pass" -inkey "${PRIVKEY}" ${PASSARG} -pkcs 3< "${PASSPHRASE}"
137 [ $? -eq 0 ] || return 1
139 # use the cipher key to decrypt data
140 pad | "${OPENSSL}" enc "-${CIPHER}" -nopad -d -pass "file:${WORKDIR}/pass" -nosalt
142 # N.B.: in the likely event that we're piping to gzip, the above command
143 # may return a spurious error if gzip closes the output stream early.
147 if [ "$1" = -d ]; then
148 if [ -z "${PRIVKEY}" ]; then
149 echo "${ME}: must specify private key for decryption" >&2
151 elif [ ! -r "${PRIVKEY}" ]; then
152 echo "${ME}: can't read private key from ${PRIVKEY}" >&2
156 if [ -n "${PASSPHRASE}" -a -e "${PASSPHRASE}" -a -r "${PASSPHRASE}" ]; then
157 PASSARG='-passin fd:3'
163 if [ $? -ne 0 ]; then
164 echo "${ME}: decryption failed" >&2
168 if [ -z "${PUBKEY}" ]; then
169 echo "${ME}: must specify public key for encryption" >&2
171 elif [ ! -r "${PUBKEY}" ]; then
172 echo "${ME}: can't read public key from ${PUBKEY}" >&2
177 if [ $? -ne 0 ]; then
178 echo "${ME}: encryption failed" >&2