edab1319676a943c92cfc6c8ce00cd69f543d846
[debian/amanda] / common-src / amcrypt-ossl.sh
1 #!@SHELL@
2 #
3 # amcrypt-ossl.sh - crypto helper using OpenSSL
4 # Usage: amcrypt-ossl.sh [-d]
5 #
6
7 prefix="@prefix@"
8 exec_prefix="@exec_prefix@"
9 sbindir="@sbindir@"
10 amlibexecdir="@amlibexecdir@"
11 . "${amlibexecdir}/amanda-sh-lib.sh"
12
13 # change these as needed
14 OPENSSL=                        # whatever's in $PATH
15 CIPHER=aes-256-cbc              # see `openssl help` for more ciphers
16 AMANDA_HOME=~@CLIENT_LOGIN@
17 RANDFILE=$AMANDA_HOME/.rnd
18 export RANDFILE
19 PASSPHRASE=$AMANDA_HOME/.am_passphrase  # required
20
21 # where might openssl be?
22 PATH=/bin:/usr/bin:/usr/local/bin:/usr/ssl/bin:/usr/local/ssl/bin:/opt/csw/bin
23 export PATH
24 ME=`basename "$0"`
25
26 if [ -z "${OPENSSL:=`which openssl`}" ]; then
27         echo `_ '%s: openssl not found' "${ME}"` >&2
28         exit 1
29 elif [ ! -x "${OPENSSL}" ]; then
30         echo `_ "%s: can't execute %s (%s)" "${ME}" "openssl" "${OPENSSL}"` >&2
31         exit 1
32 fi
33
34 # we'll need to pad the datastream to a multiple of the cipher block size prior
35 # to encryption. 96 bytes (= 768 bits) should be good for any cipher.
36 pad() {
37         perl -pe 'BEGIN { $bs = 96; $/ = \8192 } $nbytes = ($nbytes + length) % $bs; END { print "\0" x ($bs - $nbytes) }'
38 }
39
40 if [ "$1" = -d ]; then
41         # decrypt
42         "${OPENSSL}" enc -d "-${CIPHER}" -nopad -salt -pass fd:3 3< "${PASSPHRASE}"
43 else
44         # encrypt
45         pad | "${OPENSSL}" enc -e "-${CIPHER}" -nopad -salt -pass fd:3 3< "${PASSPHRASE}"
46 fi
47