Blame tests/test-signed-commit.sh

rpm-build 0fba15
#!/bin/bash
rpm-build 0fba15
#
rpm-build 0fba15
# Copyright (C) 2019 Collabora Ltd.
rpm-build 0fba15
#
rpm-build 0fba15
# SPDX-License-Identifier: LGPL-2.0+
rpm-build 0fba15
#
rpm-build 0fba15
# This library is free software; you can redistribute it and/or
rpm-build 0fba15
# modify it under the terms of the GNU Lesser General Public
rpm-build 0fba15
# License as published by the Free Software Foundation; either
rpm-build 0fba15
# version 2 of the License, or (at your option) any later version.
rpm-build 0fba15
#
rpm-build 0fba15
# This library is distributed in the hope that it will be useful,
rpm-build 0fba15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0fba15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0fba15
# Lesser General Public License for more details.
rpm-build 0fba15
#
rpm-build 0fba15
# You should have received a copy of the GNU Lesser General Public
rpm-build 0fba15
# License along with this library; if not, write to the
rpm-build 0fba15
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rpm-build 0fba15
# Boston, MA 02111-1307, USA.
rpm-build 0fba15
rpm-build 0fba15
set -euo pipefail
rpm-build 0fba15
rpm-build 0fba15
. $(dirname $0)/libtest.sh
rpm-build 0fba15
rpm-build 0fba15
echo "1..11"
rpm-build 0fba15
rpm-build 0fba15
# This is explicitly opt in for testing
rpm-build 0fba15
export OSTREE_DUMMY_SIGN_ENABLED=1
rpm-build 0fba15
rpm-build 0fba15
mkdir ${test_tmpdir}/repo
rpm-build 0fba15
ostree_repo_init repo --mode="archive"
rpm-build 0fba15
rpm-build 0fba15
echo "Unsigned commit" > file.txt
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo commit -b main -s 'Unsigned commit'
rpm-build 0fba15
COMMIT="$(ostree --repo=${test_tmpdir}/repo rev-parse main)"
rpm-build 0fba15
rpm-build 0fba15
# Test `ostree sign` with dummy module first
rpm-build 0fba15
DUMMYSIGN="dummysign"
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --sign-type=dummy ${COMMIT} ${DUMMYSIGN}
rpm-build 0fba15
rpm-build 0fba15
# Ensure that detached metadata really contain expected string
rpm-build 0fba15
EXPECTEDSIGN="$(echo $DUMMYSIGN | hexdump -n 9 -e '8/1 "0x%.2x, " 1/1 " 0x%.2x"')"
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=repo show ${COMMIT} --print-detached-metadata-key=ostree.sign.dummy | grep -q -e "${EXPECTEDSIGN}"
rpm-build 0fba15
echo "ok Detached dummy signature added"
rpm-build 0fba15
rpm-build 0fba15
# Verify vith sign mechanism
rpm-build 0fba15
${CMD_PREFIX} ostree  --repo=${test_tmpdir}/repo sign --sign-type=dummy --verify ${COMMIT} ${DUMMYSIGN}
rpm-build 0fba15
echo "ok dummy signature verified"
rpm-build 0fba15
rpm-build 0fba15
echo "Signed commit with dummy key: ${DUMMYSIGN}" >> file.txt
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo commit -b main -s 'Signed with dummy module' --sign=${DUMMYSIGN} --sign-type=dummy 
rpm-build 0fba15
COMMIT="$(ostree --repo=${test_tmpdir}/repo rev-parse main)"
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --sign-type=dummy --verify ${COMMIT} ${DUMMYSIGN}
rpm-build 0fba15
echo "ok commit with dummy signing"
rpm-build 0fba15
rpm-build 0fba15
if ${CMD_PREFIX} env -u OSTREE_DUMMY_SIGN_ENABLED ostree --repo=${test_tmpdir}/repo sign --sign-type=dummy --verify ${COMMIT} ${DUMMYSIGN} 2>err.txt; then
rpm-build 0fba15
    fatal "verified dummy signature without env"
rpm-build 0fba15
fi
rpm-build 0fba15
# FIXME the error message here is broken
rpm-build 0fba15
#assert_file_has_content_literal err.txt 'dummy signature type is only for ostree testing'
rpm-build 0fba15
assert_file_has_content_literal err.txt ' No valid signatures found'
rpm-build 0fba15
echo "ok dummy sig requires env"
rpm-build 0fba15
rpm-build 0fba15
# tests below require libsodium support
rpm-build 0fba15
if ! has_sign_ed25519; then
rpm-build 0fba15
    echo "ok Detached ed25519 signature # SKIP due libsodium unavailability"
rpm-build 0fba15
    echo "ok ed25519 signature verified # SKIP due libsodium unavailability"
rpm-build 0fba15
    echo "ok multiple signing # SKIP due libsodium unavailability"
rpm-build 0fba15
    echo "ok verify ed25519 keys file # SKIP due libsodium unavailability"
rpm-build 0fba15
    echo "ok sign with ed25519 keys file # SKIP due libsodium unavailability"
rpm-build 0fba15
    echo "ok verify ed25519 system-wide configuration # SKIP due libsodium unavailability"
rpm-build 0fba15
    echo "ok verify ed25519 revoking keys mechanism # SKIP due libsodium unavailability"
rpm-build 0fba15
    exit 0
rpm-build 0fba15
fi
rpm-build 0fba15
rpm-build 0fba15
# Test ostree sign with 'ed25519' module
rpm-build 0fba15
gen_ed25519_keys
rpm-build 0fba15
PUBLIC=${ED25519PUBLIC}
rpm-build 0fba15
SEED=${ED25519SEED}
rpm-build 0fba15
SECRET=${ED25519SECRET}
rpm-build 0fba15
rpm-build 0fba15
WRONG_PUBLIC="$(gen_ed25519_random_public)"
rpm-build 0fba15
rpm-build 0fba15
echo "SEED = $SEED"
rpm-build 0fba15
echo "PUBLIC = $PUBLIC"
rpm-build 0fba15
rpm-build 0fba15
echo "Signed commit with ed25519: ${SECRET}" >> file.txt
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo commit -b main -s "Signed with ed25519 module" --sign="${SECRET}" --sign-type=ed25519
rpm-build 0fba15
COMMIT="$(ostree --repo=${test_tmpdir}/repo rev-parse main)"
rpm-build 0fba15
rpm-build 0fba15
# Ensure that detached metadata contain signature
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=repo show ${COMMIT} --print-detached-metadata-key=ostree.sign.ed25519 &>/dev/null
rpm-build 0fba15
echo "ok Detached ed25519 signature added"
rpm-build 0fba15
rpm-build 0fba15
# Verify vith sign mechanism
rpm-build 0fba15
if ${CMD_PREFIX} ostree  --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 ${COMMIT} ${WRONG_PUBLIC}; then
rpm-build 0fba15
    exit 1
rpm-build 0fba15
fi
rpm-build 0fba15
${CMD_PREFIX} ostree  --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 ${COMMIT} ${PUBLIC}
rpm-build 0fba15
${CMD_PREFIX} ostree  --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 ${COMMIT} ${PUBLIC} ${PUBLIC}
rpm-build 0fba15
${CMD_PREFIX} ostree  --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 ${COMMIT} $(gen_ed25519_random_public) ${PUBLIC}
rpm-build 0fba15
${CMD_PREFIX} ostree  --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 ${COMMIT} $(gen_ed25519_random_public) $(gen_ed25519_random_public) ${PUBLIC}
rpm-build 0fba15
${CMD_PREFIX} ostree  --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 ${COMMIT} ${PUBLIC} $(gen_ed25519_random_public) $(gen_ed25519_random_public)
rpm-build 0fba15
${CMD_PREFIX} ostree  --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 ${COMMIT} $(gen_ed25519_random_public) $(gen_ed25519_random_public) ${PUBLIC} $(gen_ed25519_random_public) $(gen_ed25519_random_public)
rpm-build 0fba15
echo "ok ed25519 signature verified"
rpm-build 0fba15
rpm-build 0fba15
# Check if we able to use all available modules to sign the same commit
rpm-build 0fba15
echo "Unsigned commit for multi-sign" >> file.txt
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo commit -b main -s 'Unsigned commit'
rpm-build 0fba15
COMMIT="$(ostree --repo=${test_tmpdir}/repo rev-parse main)"
rpm-build 0fba15
# Check if we have no signatures
rpm-build 0fba15
for mod in "dummy" "ed25519"; do
rpm-build 0fba15
    if ostree --repo=repo show ${COMMIT} --print-detached-metadata-key=ostree.sign.${mod}; then
rpm-build 0fba15
        echo "Unexpected signature for ${mod} found"
rpm-build 0fba15
        exit 1
rpm-build 0fba15
    fi
rpm-build 0fba15
done
rpm-build 0fba15
rpm-build 0fba15
# Sign with all available modules
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --sign-type=dummy ${COMMIT} ${DUMMYSIGN}
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --sign-type=ed25519 ${COMMIT} ${SECRET}
rpm-build 0fba15
# and verify
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 ${COMMIT} ${PUBLIC} >out.txt
rpm-build 0fba15
assert_file_has_content out.txt "ed25519: Signature verified successfully with key"
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --sign-type=dummy --verify ${COMMIT} ${DUMMYSIGN} >out.txt
rpm-build 0fba15
assert_file_has_content out.txt "dummy: Signature verified"
rpm-build 0fba15
echo "ok multiple signing "
rpm-build 0fba15
rpm-build 0fba15
# Prepare files with public ed25519 signatures
rpm-build 0fba15
PUBKEYS="$(mktemp -p ${test_tmpdir} ed25519_XXXXXX.ed25519)"
rpm-build 0fba15
rpm-build 0fba15
# Test if file contain no keys
rpm-build 0fba15
if ${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-file=${PUBKEYS} ${COMMIT}; then
rpm-build 0fba15
    exit 1
rpm-build 0fba15
fi
rpm-build 0fba15
rpm-build 0fba15
# Test if have a problem with file object
rpm-build 0fba15
if ${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-file=${test_tmpdir} ${COMMIT}; then
rpm-build 0fba15
    exit 1
rpm-build 0fba15
fi
rpm-build 0fba15
rpm-build 0fba15
# Test with single key in list
rpm-build 0fba15
echo ${PUBLIC} > ${PUBKEYS}
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-file=${PUBKEYS} ${COMMIT} >out.txt
rpm-build 0fba15
assert_file_has_content out.txt 'ed25519: Signature verified successfully'
rpm-build 0fba15
rpm-build 0fba15
# Test the file with multiple keys without a valid public key
rpm-build 0fba15
for((i=0;i<100;i++)); do
rpm-build 0fba15
    # Generate a list with some public signatures
rpm-build 0fba15
    gen_ed25519_random_public
rpm-build 0fba15
done > ${PUBKEYS}
rpm-build 0fba15
# Check if file contain no valid signatures
rpm-build 0fba15
if ${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-file=${PUBKEYS} ${COMMIT} 2>err.txt; then
rpm-build 0fba15
    fatal "validated with no signatures"
rpm-build 0fba15
fi
rpm-build 0fba15
assert_file_has_content err.txt 'error:.* ed25519: Signature couldn.t be verified; tried 100 keys'
rpm-build 0fba15
# Check if no valid signatures provided via args&file
rpm-build 0fba15
if ${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-file=${PUBKEYS} ${COMMIT} ${WRONG_PUBLIC}; then
rpm-build 0fba15
    exit 1
rpm-build 0fba15
fi
rpm-build 0fba15
rpm-build 0fba15
#Test keys file and public key
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-file=${PUBKEYS} ${COMMIT} ${PUBLIC}
rpm-build 0fba15
rpm-build 0fba15
# Add correct key into the list
rpm-build 0fba15
echo ${PUBLIC} >> ${PUBKEYS}
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-file=${PUBKEYS} ${COMMIT}
rpm-build 0fba15
rpm-build 0fba15
echo "ok verify ed25519 keys file"
rpm-build 0fba15
rpm-build 0fba15
# Check ed25519 signing with secret file
rpm-build 0fba15
echo "Unsigned commit for secret file usage" >> file.txt
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo commit -b main -s 'Unsigned commit'
rpm-build 0fba15
COMMIT="$(ostree --repo=${test_tmpdir}/repo rev-parse main)"
rpm-build 0fba15
rpm-build 0fba15
KEYFILE="$(mktemp -p ${test_tmpdir} secret_XXXXXX.ed25519)"
rpm-build 0fba15
echo "${SECRET}" > ${KEYFILE}
rpm-build 0fba15
# Sign
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --sign-type=ed25519 --keys-file=${KEYFILE} ${COMMIT}
rpm-build 0fba15
# Verify
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-file=${PUBKEYS} ${COMMIT}
rpm-build 0fba15
echo "ok sign with ed25519 keys file"
rpm-build 0fba15
rpm-build 0fba15
# Check the well-known places mechanism
rpm-build 0fba15
mkdir -p ${test_tmpdir}/{trusted,revoked}.ed25519.d
rpm-build 0fba15
for((i=0;i<100;i++)); do
rpm-build 0fba15
    # Generate some key files with random public signatures
rpm-build 0fba15
    gen_ed25519_random_public > ${test_tmpdir}/trusted.ed25519.d/signature_$i
rpm-build 0fba15
done
rpm-build 0fba15
# Check no valid public keys are available
rpm-build 0fba15
if ${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-dir=${test_tmpdir} ${COMMIT}; then
rpm-build 0fba15
    exit 1
rpm-build 0fba15
fi
rpm-build 0fba15
echo ${PUBLIC} > ${test_tmpdir}/trusted.ed25519.d/correct
rpm-build 0fba15
# Verify with correct key
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-dir=${test_tmpdir} ${COMMIT}
rpm-build 0fba15
rpm-build 0fba15
echo "ok verify ed25519 system-wide configuration"
rpm-build 0fba15
rpm-build 0fba15
# Add the public key into revoked list
rpm-build 0fba15
echo ${PUBLIC} > ${test_tmpdir}/revoked.ed25519.d/correct
rpm-build 0fba15
# Check if public key is not valid anymore
rpm-build 0fba15
if ${CMD_PREFIX} ostree --repo=${test_tmpdir}/repo sign --verify --sign-type=ed25519 --keys-dir=${test_tmpdir} ${COMMIT}; then
rpm-build 0fba15
    exit 1
rpm-build 0fba15
fi
rpm-build 0fba15
rm -rf ${test_tmpdir}/{trusted,revoked}.ed25519.d
rpm-build 0fba15
echo "ok verify ed25519 revoking keys mechanism"