#!/bin/sh
# Copyright (c) 2020 Mastercard

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# wrapper script for Utimaco tokens
# see with_pkcs11_common for usage and alternate invocations
#
# Utimaco-specific notes:
#   - supports both R2 and R3 client libraries (auto-detected)
#   - CS_PKCS11_R2_CFG / CS_PKCS11_R3_CFG point to the Utimaco config file

PKCS11PASSWORD=${PKCS11PASSWORD:-changeit}

_p11_vendor=utimaco

# vendor-specific settings
# Utimaco: since there is no standard install, we try a few common locations
PKCS11_LIBPATHS="/opt/utimaco/lib $HOME/utimaco/pkcs11/lib $HOME/utimaco/lib"
case $(uname -s) in
    Darwin) PKCS11_LIBNAMES="libcs_pkcs11_R3.dylib libcs_pkcs11_R2.dylib" ;;
    *)      PKCS11_LIBNAMES="libcs_pkcs11_R3.so libcs_pkcs11_R2.so"       ;;
esac
PKCS11_CFGNAMES="cs_pkcs11_R3.cfg cs_pkcs11_R2.cfg"
PKCS11_CFGPATHS="/opt/utimaco /usr/local/etc /etc $HOME/utimaco/pkcs11 $HOME/utimaco"

LIB_ENVVARS="CS_PKCS11_R2_CFG CS_PKCS11_R3_CFG"

# Utimaco requires a config file - locate it if not already set
_find_utimaco_cfg()
{
    for path in $PKCS11_CFGPATHS; do
        for file in $PKCS11_CFGNAMES; do
            if [ -e "$path/$file" ]; then
                echo "$path/$file"
                return 0
            fi
        done
    done
    echo "NOT_FOUND"
    return 1
}

# Resolve and validate the config file *after* .pkcs11rc has been sourced, so a
# user can point CS_PKCS11_R3_CFG/CS_PKCS11_R2_CFG at it from .pkcs11rc.
# with_pkcs11_common calls this hook once the rc file has been loaded.
p11_post_rc()
{
    CS_PKCS11_R3_CFG=${CS_PKCS11_R3_CFG:-$(_find_utimaco_cfg)}
    CS_PKCS11_R2_CFG=${CS_PKCS11_R2_CFG:-$(_find_utimaco_cfg)}

    if [ "$CS_PKCS11_R2_CFG" = "NOT_FOUND" ] && [ "$CS_PKCS11_R3_CFG" = "NOT_FOUND" ]; then
        echo "***Error: Utimaco configuration file not found, please set CS_PKCS11_R3_CFG/CS_PKCS11_R2_CFG accordingly." >&2
        exit 1
    fi

    # Never export the NOT_FOUND sentinel to the library.
    [ "$CS_PKCS11_R3_CFG" = "NOT_FOUND" ] && unset CS_PKCS11_R3_CFG
    [ "$CS_PKCS11_R2_CFG" = "NOT_FOUND" ] && unset CS_PKCS11_R2_CFG
}

# Utimaco has multiple possible library names; override find_p11_lib.
# Names are space-separated (PKCS11_LIBNAMES) while build_p11_libpaths emits
# one path per line (paths may contain spaces).  Iterate names first using the
# default IFS so they split on spaces, and only switch IFS to newline for the
# inner path loop.  Doing it the other way round makes the inner name loop run
# with IFS=newline, so PKCS11_LIBNAMES is never split and nothing is found.
# Name-major order also means the preferred R3 library wins over R2 even when
# R2 sits in an earlier search directory.
find_p11_lib()
{
    _old_ifs=$IFS
    for lib in $PKCS11_LIBNAMES; do
        IFS='
'
        for path in $(build_p11_libpaths); do
            if [ -e "$path/$lib" ]; then
                echo "$path/$lib"
                IFS=$_old_ifs
                return 0
            fi
        done
        IFS=$_old_ifs
    done
    echo "NOT_FOUND"
    return 1
}

# load common logic
. "$(dirname "$0")/with_pkcs11_common" "$@"
