#!/usr/bin/perl
use strict;
use warnings;
use utf8;

# Core modules
use English;
use FindBin qw( $Bin );

# CPAN modules
use Test::More;
use Test::Deep ':v1';
use Test::Exception;

#use OpenXPKI::Debug; $OpenXPKI::Debug::LEVEL{'OpenXPKI::Crypto::CLI.*'} = 100;

# Project modules
use OpenXPKI::FileUtils;
use lib "$Bin/../lib";
use OpenXPKI::Test;

plan tests => 7;

#
# Setup env
#
my $oxitest = OpenXPKI::Test->new(
    with => "CryptoLayer",
);

my $passwd = "root";
my $rsa    = "-----BEGIN ENCRYPTED PRIVATE KEY-----\nMIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQI92Z4mO7qmCsCAggA\nMBQGCCqGSIb3DQMHBAi2Hforzj6ykwSCBMjLkgjSC4E1XewpNnD5CpnJHRQ3g6OI\nr6moMJOn/7f1sbjhW3kMdDvjvexkxo2tc9ujmEIcQOLK1gH+ljVtNbs4mGj1CQnE\nve242QVKDFQ6B5MZ9hF7sPe/sl8Z4/9yJEA1TlKYe7fSdO4evDC9ztFm43mIE+NR\nn48xuHdrolUb3MVATqG/kD5aGfopLF0WjXFViGYTI+xJsrxn5FWsVpnS2tc3Sh39\nUzyP05wP8Hj6BaEbXvyhBExj45z4gYhmaIWUhyKw83y3t+XdqMK4UEoUaoddpveE\naISbOugxlH59/ghNKj9jUM25SaGKKx5rfoTMUTofULHOOR1rK5i6YyAb23oeKMDi\nb0XZTmKbKzblfarX/IRUqCgDkzCkbANC/otG7fy3XzVASlDpY7oAUK9Q8wm+d//7\nSq3q+PYUETCILrWgWsitw/vfmTezFUY3RtVTIzbWPKnKS2bnC4N1hsvOzfHvkOAO\n4HkXAsUlUnNjcwg8hoHnJdgIr/LY8FkdTmnFtsECLa48v7Gab/Vt1CUIB1sbDErU\n1qS0w2bTZyz5gmk3TC/0uXdyuRqqXaTMkMI3yiqXZxYcJeNuSUhs9MChb5O78YxP\n+8ILoU2jd1VPaz6ZjxNRtoqzLc2+G9PLylpRO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Je2AHvbL0JCN+pdm0lFG7WY0sQN2PLei+qplRWkM8eOjH\nrPMv/C0/Saqly37QapyIqq/7jvxz6jv0F0uBbc5tQmT4Oidl/KNnBZAdNpKx4xoB\n3swjYPMYPysCAwEAAaNgMF4wDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUjNW5\nhSaTjfK6RnG/bYUrvV8tQZUwHwYDVR0jBBgwFoAUjNW5hSaTjfK6RnG/bYUrvV8t\nQZUwCwYDVR0PBAQDAgEGMA0GCSqGSIb3DQEBCwUAA4IBAQCoqYzWPbowgJxmR1Mb\nIMhyyg7VdF44P8e8Sv2h+i2LaWBY6HwUVkDByqCEDysWfvH4RNzjNNiWQ10yoiFp\n59NQcNjXC5GMZZYdLjB/+OkVUAT8KwV+GMeB9LTSju4yMxmvtjj+auVZmadADh1b\n7/atPtRSAH/O9CJTXupoceh/AXeYV9Vx2J7Y4cujHIH3H326QidnR9zeUyha8wqF\n1/nwFOwt5CFZ89PjyAl/D2g0IowNNHRGdm7oLvQAR8HDwFvlS+Qpc2PLYiqGG8eo\nK7jxPAQWCvdQU5b3xFYUehK35E58DTGA0wnKrAxeB47ZrZF11jc9xrMCGg2BLm8R\n+UwW\n-----END CERTIFICATE-----";
my $content = "This is for example a passprase.";

#
# Tests
#
use_ok "OpenXPKI::Crypto::TokenManager";

my $default_token;
lives_and {
    my $mgmt = OpenXPKI::Crypto::TokenManager->new;
    $default_token = $mgmt->get_system_token({ TYPE => "DEFAULT" });
    ok $default_token;
} 'Get default token';

# create signature
my $sig;
lives_and {
    $sig = $default_token->command({
        COMMAND   => "pkcs7_sign",
        CONTENT   => $content,
        CERT      => $cert,
        KEY       => $rsa,
        PASSWD    => $passwd,
    });
    like $sig, qr/^-----BEGIN PKCS7-----/;
} "Create PKCS#7 signature";

## encrypt content
my $enc;
lives_and {
    $enc = $default_token->command({
        COMMAND   => "pkcs7_encrypt",
        CONTENT   => $content,
        CERT      => $cert,
    });
    like $enc, qr/^-----BEGIN PKCS7-----/;
} "Encrypt content";

## decrypt content
lives_and {
    my $plain = $default_token->command({
        COMMAND => "pkcs7_decrypt",
        PKCS7   => $enc,
        CERT    => $cert,
        KEY     => $rsa,
        PASSWD  => $passwd,
    });
    is $plain, $content;
} "Decrypt content";

## verify signature
lives_and {
    my $result = $default_token->command({
        COMMAND => "pkcs7_verify",
        CONTENT => $content,
        PKCS7   => $sig,
        CHAIN   => [ $cacert, $rootcert ],
    });
    ok $result;
} "Decrypt content";

lives_and {
    my $result = $default_token->command({
        COMMAND => "pkcs7_get_chain",
        PKCS7   => $sig,
    });
    cmp_deeply $result, [ $cert ];
} "Extract certificate (chain) from signature";

1;
