Skip to content

Commit

Permalink
Potential fix for unbound variable in MacOS (tests)
Browse files Browse the repository at this point in the history
- Use `:+` bash parameter expansion to include pbkdf2 argument to
  openssl only if variable is set
- Simplify variable `pbkdf2_arg` from list to string, since the `[@]`
  referencing doesn't work in all cases for MacOS (at least not
  for the unit tests): an empty list errors with `unbound variable`
  • Loading branch information
jmurty authored and Erotemic committed Jul 3, 2022
1 parent 1ed6cce commit 3dd51a5
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions transcrypt
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,26 @@ _openssl_encrypt() {
# In 3.x openssl disabled output of the salt prefix, which we need for determinism.
# We take control over outputting the the prefix 'Salted__' with the salt
# to ensure it is always included regardless of the OpenSSL version. #133
(
# Always prepend encrypted ciphertext with "Salted__" prefix and binary salt value
printf "Salted__" && printf "%s" "$final_salt" | xxd -r -p &&
# Encrypt file to binary ciphertext
ENC_PASS=$password "$openssl_path" enc -e "-${cipher}" -md "${digest}" -pass env:ENC_PASS -S "$final_salt" "${pbkdf2_args[@]}" -in "$tempfile" |
# Strip "Salted__" prefix and salt value if also added by OpenSSL (version < 3)
LC_ALL=C sed -e "s/^\(Salted__.\{8\}\)\(.*\)/\2/"
) | base64
openssl_major_version=$($openssl_path version | cut -d' ' -f2 | cut -d'.' -f1)
if [ "$openssl_major_version" -ge "3" ]; then
# Encrypt the file to base64, ensuring it includes the prefix 'Salted__' with the salt. #133
(
printf "Salted__" && printf "%s" "$final_salt" | xxd -r -p &&
# Encrypt file to binary ciphertext
ENC_PASS=$password "$openssl_path" enc -e "-${cipher}" -md "${digest}" -pass env:ENC_PASS -S "$final_salt" ${pbkdf2_arg:+"$pbkdf2_arg"} -in "$tempfile"
) |
base64
else
# Encrypt file to base64 ciphertext
ENC_PASS=$password "$openssl_path" enc -e -a "-${cipher}" -md "${digest}" -pass env:ENC_PASS -S "$final_salt" ${pbkdf2_arg:+"$pbkdf2_arg"} -in "$tempfile"
fi
}

_openssl_decrypt() {
# Expects that the following variables are set:
# password, openssl_path, cipher, digest, pbkdf2_args
# password, openssl_path, cipher, digest, pbkdf2_arg
# This works the same across openssl versions
ENC_PASS=$password "$openssl_path" enc -d "-${cipher}" -md "${digest}" -pass env:ENC_PASS -a "$@" "${pbkdf2_args[@]}"
ENC_PASS=$password "$openssl_path" enc -d "-${cipher}" -md "${digest}" -pass env:ENC_PASS -a ${pbkdf2_arg:+"$pbkdf2_arg"} "$@"
}

# compatible openssl list command
Expand Down Expand Up @@ -246,9 +251,7 @@ _load_vars_for_encryption() {
_load_transcrypt_config_vars

if [[ "$kdf" == "1" ]] || [[ "$kdf" == "pbkdf2" ]]; then
pbkdf2_args=('-pbkdf2')
else
pbkdf2_args=()
pbkdf2_arg='-pbkdf2'
fi

if [[ "$salt_method" == "password" ]]; then
Expand Down

0 comments on commit 3dd51a5

Please sign in to comment.