#!/bin/bash # vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # Author: Karel Srot # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # Copyright (c) 2022 Red Hat, Inc. # # This copyrighted material is made available to anyone wishing # to use, modify, copy, or redistribute it subject to the terms # and conditions of the GNU General Public License version 2. # # This program is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public # License along with this program; if not, write to the Free # Software Foundation, Inc., 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Include Beaker environment . /usr/share/beakerlib/beakerlib.sh || exit 1 PACKAGE="opencryptoki" [ -n "$SLOT" ] || SLOT=sw USER="user$RANDOM" rlJournalStart rlPhaseStartSetup rlAssertRpm $PACKAGE rlRun "rlImport opencryptoki/token-manipulation" || rlDie "Could not import opencryptoki/token-manipulation library" rlRun "TmpDir=\$(mktemp -d)" 0 "Creating tmp directory" rlRun "pushd $TmpDir" rlRun "useradd -m $USER -G pkcs11" rlRun -s "su $USER -c 'id'" rlFileBackup /etc/opencryptoki/opencryptoki.conf rlServiceStop pkcsslotd rlRun "pkcsResetTokens" rlServiceStart pkcsslotd sleep 1 rlRun "pkcsconf -t | grep 'Soft'" SLOT=$( pkcsGetTokenSlot $SLOT ) rlRun "pkcsInitToken $SLOT" rlRun "pkcsconf -t" rlRun "pkcsstats -R" rlPhaseEnd rlPhaseStartTest "Generate aes key as root and verify stats" rlRun -s "pkcsstats -s $SLOT -U root" rlAssertGrep "[no mechanisms were used]" $rlRun_LOG rlRun "p11sak generate-key aes 128 --slot $SLOT --pin $pkcsUSER_PIN --label aes_key --attr ED" rlRun -s "p11sak list-key aes --slot $SLOT --pin $pkcsUSER_PIN" rlAssertGrep 'AES 128 | "?aes_key"?' $rlRun_LOG -E rlRun -s "p11sak gen-key ec prime256v1 --slot $SLOT --pin $pkcsUSER_PIN --label ec_key" rlRun -s "p11sak ls secret --long --slot $SLOT --pin $pkcsUSER_PIN" rlRun -s "pkcsstats -s $SLOT -U root" rlAssertGrep "CKM_AES_KEY_GEN *\| *0 *0 *1 *0 *0" $rlRun_LOG -E rlAssertGrep "CKM_EC_KEY_PAIR_GEN *\| *0 *0 *1 *0 *0" $rlRun_LOG -E rlPhaseEnd rlPhaseStartTest "Generate aes key as $USER and verify stats" rlRun -s "pkcsstats -s $SLOT -U $USER" 0,1 rlAssertGrep "(no mechanisms were used|No statistics are available for user '$USER')" $rlRun_LOG -E rlRun "su - $USER -c 'sleep 3 && p11sak generate-key aes 128 --slot $SLOT --pin $pkcsUSER_PIN --label aes_key --attr ED'" rlRun -s "p11sak list-key aes --slot $SLOT --pin $pkcsUSER_PIN" rlAssertGrep 'AES 128 | "?aes_key"?' $rlRun_LOG -E rlRun -s "pkcsstats -s $SLOT -U $USER" rlAssertGrep "CKM_AES_KEY_GEN *\| *0 *0 *1 *0 *0" $rlRun_LOG -E rlPhaseEnd rlPhaseStartTest "Print acummulated stats for all users" rlRun -s "pkcsstats -s $SLOT -S" rlAssertGrep "CKM_AES_KEY_GEN *\| *0 *0 *2 *0 *0" $rlRun_LOG -E rlPhaseEnd rlPhaseStartTest "Print --all-mechs stats for $USER" rlRun -s "pkcsstats -s $SLOT --all-mechs -U $USER" rlAssertGrep "CKM_AES_KEY_GEN *\| *0 *0 *1 *0 *0" $rlRun_LOG -E rlAssertGrep "CKM_EC_KEY_PAIR_GEN *\| *0 *0 *0 *0 *0" $rlRun_LOG -E rlPhaseEnd rlPhaseStartTest "Reset stats for $USER" rlRun -s "pkcsstats -r -U $USER" rlRun -s "pkcsstats -s $SLOT -U $USER" rlAssertGrep "no mechanisms were used" $rlRun_LOG rlRun -s "pkcsstats -s $SLOT -U $USER -a" rlAssertGrep "CKM_AES_KEY_GEN *\| *0 *0 *0 *0 *0" $rlRun_LOG -E rlPhaseEnd rlPhaseStartTest "Delete stats for $USER" rlRun -s "pkcsstats -d -U $USER" rlRun -s "pkcsstats -s $SLOT -U $USER -a" 1 rlAssertGrep "No statistics are available for user '$USER'" $rlRun_LOG -E rlPhaseEnd rlPhaseStartTest "Print user stats in JSON" rlRun -s "pkcsstats -s $SLOT -j" rlAssertGrep '"user": "root",' $rlRun_LOG rlAssertGrep '"mechanism": "CKM_AES_KEY_GEN",' $rlRun_LOG rlAssertGrep '"strength-128": 1,' $rlRun_LOG rlPhaseEnd rlPhaseStartTest "Print help" rlRun -s "pkcsstats -h" rlAssertGrep "Usage: pkcsstats \[OPTIONS\]" $rlRun_LOG rlPhaseEnd rlPhaseStartCleanup rlServiceStop pkcsslotd rlRun "pkcsRestoreTokens" rlFileRestore rlServiceRestore pkcsslotd rlRun "popd" rlRun "rm -r $TmpDir" 0 "Removing tmp directory" rlLog "Waiting 12 for systemd --user process to exit..." sleep 15 rlRun "userdel -r $USER" || ps -ef rlPhaseEnd rlJournalPrintText rlJournalEnd