#
# Copyright (c) 2023-2026 Roumen Petrov, Sofia, Bulgaria
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
#
# DESCRIPTION: Test client authentication using keys from pkcs#11 engine:
#  - "IdentityFile" is not used.
#  - "PubkeyAlgorithms" selects either X.509 or plain key.
#  - "AuthorizedKeysFile" contain encoded public key.
# NOTE: Engine requires pkcs#11 module.


# === common PKCS#11 setup:
. ${SCRIPTDIR}../pkcs11-env
. ${SCRIPTDIR}pkcs11-func

SSH_MODULE_LOG="$CWD/.ssh-module.log"
> "$SSH_MODULE_LOG" || exit $?

SSH_ENGINE="$CWD/ssh_engine"
OPENSSL_CONF="$CWD/openssl_engine.cnf"
# remove "stale" configuration, as it may affect setup
rm -f $SSH_ENGINE $OPENSSL_CONF


# ===
#env. vars:
#  SSH_CLIENTKEY
#  type
testPUBKEYautorization () {
  case "$SSH_CLIENTKEY" in
  *rsa*)         key_id='0001';;
  *eccnistp256*) key_id='0002';;
  *eccnistp384*) key_id='0003';;
  *eccnistp521*) key_id='0004';;
  esac

  base_identity_file="$SSH_CLIENTKEY"
  if test ! -r "$base_identity_file" ; then
    error_file_not_readable "$base_identity_file"; return $?
  fi
  identity_file="$base_identity_file-$type"
  if test ! -r "$identity_file" ; then
    error_file_not_readable "$identity_file"; return $?
  fi

  "$TEST_SSH_SSHKEYGEN" -f "$base_identity_file" -y 2>/dev/null > "$AUTHORIZEDKEYSFILE" || return $?

  typemsg="$base_identity_file"
  must_fail=

  SSH_EXTRA_OPTIONS="-G $SSH_ENGINE"

if $load_cert ; then
  case "$SSH_CLIENTKEY" in
  *rsa*)         algs='x509v3-rsa2048-sha256 x509v3-ssh-rsa x509v3-sign-rsa';;
  *eccnistp256*) algs='x509v3-ecdsa-sha2-nistp256';;
  *eccnistp384*) algs='x509v3-ecdsa-sha2-nistp384';;
  *eccnistp521*) algs='x509v3-ecdsa-sha2-nistp521';;
  esac
  for PubkeyAlgorithms in $algs ; do
(
  SSH_EXTRA_OPTIONS="$SSH_EXTRA_OPTIONS -o PubkeyAlgorithms=$PubkeyAlgorithms"

  runTest "$typemsg" "engine:pkcs11:$key_id" "X.509 - $PubkeyAlgorithms" "$must_fail"
) || return $?
  done
fi

  case "$SSH_CLIENTKEY" in
  *rsa*)         algs='rsa-sha2-256 rsa-sha2-512 ssh-rsa';;
  *eccnistp256*) algs='ecdsa-sha2-nistp256';;
  *eccnistp384*) algs='ecdsa-sha2-nistp384';;
  *eccnistp521*) algs='ecdsa-sha2-nistp521';;
  esac
  for PubkeyAlgorithms in $algs ; do
(
  SSH_EXTRA_OPTIONS="$SSH_EXTRA_OPTIONS -o PubkeyAlgorithms=$PubkeyAlgorithms"

  runTest "$typemsg" "engine:pkcs11:$key_id" "plain - $PubkeyAlgorithms" "$must_fail"
) || return $?
  done
}


# ===

test_pkcs11 () {
  echo "* ${extd}pkcs#11 engine${norm}:"

  creTestSSHDcfgFile
  cat >> "$SSHD_CFG" <<EOF
CACertificateFile $SSH_CAROOT/$CACERTFILE
CACertificatePath /path/not/found
CARevocationFile  /crlfile/not/found
CARevocationPath  /crlpath/not/found
EOF

  runSSHdaemon || return $?
  (
    SSH_ASKPASS_REQUIRE=force
    SSH_ASKPASS=$P11_ASKPASS
    cre_ssh_p11_askpass
    export SSH_ASKPASS SSH_ASKPASS_REQUIRE

    export OPENSSL_CONF

    for SSH_CLIENTKEY in $TEST_SSH_CLIENTKEYS; do
      # filter supported by engine
      # note engine functionality is deprecated and will not support new key types
      supported=:
      case $SSH_CLIENTKEY in
      *rsa*);;
      *eccnistp*);;
      *) supported=false;;
      esac
      if $supported ; then :
      else
        echo "  * ${extd}$SSH_CLIENTKEY${norm} - skipped unsupported key type"
        continue
      fi
      testPUBKEYautorization || exit $?
    done
  ); retval=$?
  killSSHdaemon
  return $retval
}


# ===

do_test () {
  if $USE_OPENSSL_ENGINE ; then :
  else
    echo "* ${extd}engine${norm} module is ${warn}disabled${norm}"
    return 0
  fi
  if p11_find_module ; then :
  else
    echo "warning: ${extd}SoftHSM module not found${norm}" >&2
    return 1
  fi
  if p11_find_engine ; then :
  else
    echo "warning: ${extd}PKCS#11 engine not found${norm}" >&2
    return 1
  fi
  if p11_find_SOFTHSM2_TOOL ; then :
  else
    echo "warning: ${extd}SoftHSM tool not found${norm}" >&2
    return 1
  fi
  if p11_find_PKCS11_TOOL ; then :
  else
    echo "warning: ${extd}PKCS#11 tool not found${norm}" >&2
    return 1
  fi

  EDDSA_MECHANISM=false
  p11_setup_token || return $?

  (
    cat > $SSH_ENGINE << EOF
Engine pkcs11
# debug if libp11 <= 0.4.13
#VERBOSE
# debug if libp11 > 0.4.13
#DEBUG_LEVEL = 7
MODULE_PATH = $TEST_SSH_PKCS11
EOF
    cat > $OPENSSL_CONF << EOF
openssl_conf = config

[ config ]
engines = engine_section

[ engine_section ]
engine1 = engine1_section

[engine1_section]
dynamic_path = $TEST_SSH_PKCS11_ENGINE
EOF
  ) || return $?

  # test with only one CA authority
  for type in $SSH_SIGN_TYPES ; do
    break
  done

  test_pkcs11
}
