#!/bin/sh

test_description="Check certification"
. $SHARNESS_TEST_SRCDIR/sharness.sh

echo "gost3410-512C gost3410-256A
ed25519-blake2b ed25519-blake2b
slh-dsa-shake-256s slh-dsa-shake-256s" | while read caAlgo eeAlgo ; do

sub="-sub CN=CA -sub C=RU"
test_expect_success "$caAlgo: CA load generation" "cmkeytool \
    -algo $caAlgo -ku sig $sub \
    5>ca.$caAlgo.pub 9>ca.$caAlgo.prv"
test_expect_success "$caAlgo: CA generation" "cmkeytool \
    4<ca.$caAlgo.pub \
    8<ca.$caAlgo.prv \
    <ca.$caAlgo.pub \
    5>ca.$caAlgo.pub.certified"
mv ca.$caAlgo.pub.certified ca.$caAlgo.pub
test_expect_success "$caAlgo: CA regeneration" "cmkeytool \
    4<ca.$caAlgo.pub \
    8<ca.$caAlgo.prv \
    <ca.$caAlgo.pub \
    5>ca.$caAlgo.pub.certified"
mv ca.$caAlgo.pub.certified ca.$caAlgo.pub
test_expect_success "$caAlgo: CA self-signature" "
    cmkeytool -verify 4<ca.$caAlgo.pub <ca.$caAlgo.pub"

sub="-sub CN=SubCA -sub C=RU"
test_expect_success "$eeAlgo: SubCA load generation" "cmkeytool \
    -algo $eeAlgo -ku sig $sub \
    5>subca.$eeAlgo.pub 9>subca.$eeAlgo.prv"
test_expect_success "$eeAlgo: SubCA generation" "cmkeytool \
    4<ca.$caAlgo.pub \
    8<ca.$caAlgo.prv \
    <subca.$eeAlgo.pub \
    5>subca.$eeAlgo.pub.certified"
mv subca.$eeAlgo.pub.certified subca.$eeAlgo.pub
test_expect_success "$eeAlgo: SubCA signature" "
    cmkeytool -verify 4<ca.$caAlgo.pub <subca.$eeAlgo.pub"

sub="-sub CN=EE -sub C=RU"
test_expect_success "$eeAlgo: EE load generation" "cmkeytool \
    -algo $eeAlgo $sub \
    5>ee.$eeAlgo.pub 9>ee.$eeAlgo.prv"
test_expect_success "$eeAlgo: EE generation" "cmkeytool \
    4<subca.$eeAlgo.pub \
    8<subca.$eeAlgo.prv \
    <ee.$eeAlgo.pub \
    5>ee.$eeAlgo.pub.certified"
mv ee.$eeAlgo.pub.certified ee.$eeAlgo.pub
test_expect_success "$eeAlgo: EE chain" "
    cat ca.$caAlgo.pub subca.$eeAlgo.pub |
    cmkeytool -verify 4<&0 <ee.$eeAlgo.pub"

done

test_done
