# --
# Copyright (C) 2006-2024 KIX Service Software GmbH, https://www.kixdesk.com
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file LICENSE-AGPL for license information (AGPL). If you
# did not receive this file, see https://www.gnu.org/licenses/agpl.txt.
# --

use strict;
use warnings;
use utf8;

use vars (qw($Self));

use MIME::Base64;

# get helper object
my $Helper = $Kernel::OM->Get('UnitTest::Helper');

# check supported methods
for my $Method (
    qw(
        CertificateCreate CertificateExists CertificateGet
        CertificateDelete CertificateToFS Encrypt Decrypt
        Verify Sign
    )
) {
    $Self->True(
        $Kernel::OM->Get('Certificate')->can($Method),
        'Certificate can "' . $Method . q{"}
    );
}

# begin transaction on database
$Helper->BeginWork();

my $HomeDir = $Kernel::OM->Get('Config')->Get('Home');
my @Certificates = _ReadCertificates();

my @NegativTests = (
    # CERTIFICATECREATE
    {
        Function => 'CertificateCreate',
        Data     => {},
        Expected => undef,
        Name     => 'Certificate: Create / Missing Paramters'
    },
    {
        Function => 'CertificateCreate',
        Data     => {
            Type       => 'Cert',
            CType      => 'SMIME'
        },
        Expected => undef,
        Name     => 'Certificate: Create / No File given'
    },
    {
        Function => 'CertificateCreate',
        Data     => {
            File => {
                %{$Certificates[0]},
                Content => undef
            },
            Type       => 'Cert',
            CType      => 'SMIME'
        },
        Expected => undef,
        Name     => 'Certificate: Create / No Content given'
    },
    {
        Function => 'CertificateCreate',
        Data     => {
            File  => $Certificates[0],
            CType => 'SMIME'
        },
        Expected => undef,
        Name     => 'Certificate: Create / No Type given'
    },
    {
        Function => 'CertificateCreate',
        Data     => {
            File  => $Certificates[0],
            Type  => 'Private',
            CType => 'SMIME'
        },
        Expected => undef,
        Name     => 'Certificate: Create / No Passphrase when type Private given'
    },
    # EO CERTIFICATECREATE
    # CERTIFICATEEXISTS
    {
        Function => 'CertificateExists',
        Data     => {},
        Expected => undef,
        Name     => 'Certificate: Exists / No Filename'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            HasCertificate => 1
        },
        Expected => undef,
        Name     => 'Certificate: Exists / No Type / HasCertificate'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            HasCertificate => 1,
            Type           => 'Private'
        },
        Expected => undef,
        Name     => 'Certificate: Exists / No Attributes'
    },
    # EO CERTIFICATEEXISTS
    # CERTIFICATEGET
    {
        Function => 'CertificateGet',
        Data     => {
        },
        Expected => undef,
        Name     => 'Certificate: Get / Missing Parameters'
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID => undef
        },
        Expected => undef,
        Name     => 'Certificate: Get / No ID'
    },
    # EO CERTIFICATEGET
    # CERTIFICATEDELETE
    {
        Function => 'CertificateDelete',
        Data     => {},
        Expected => undef,
        Name     => 'Certificate: Delete / Missing Parameters'
    },
    {
        Function => 'CertificateDelete',
        Data     => {
            ID => undef
        },
        Expected => undef,
        Name     => 'Certificate: Delete / No ID'
    },
    # EO CERTIFICATEDELETE
);

for my $Test ( @NegativTests ) {
    my $Function = $Test->{Function};
    my $Result   = $Kernel::OM->Get('Certificate')->$Function(
        %{$Test->{Data}},
        Silent => 1
    );

    $Self->Is(
        $Result,
        $Test->{Expected},
        $Test->{Name}
    );
}

# Certificate: Create / Get / Exists
my @CertificateIDs;
my @Tests = (
    {
        Function => 'CertificateCreate',
        Data     => {
            File       => $Certificates[1],
            Type       => 'Private',
            Passphrase => 'start123',
            CType      => 'SMIME',
            Silent     => 1
        },
        Expected => undef,
        Name     => 'Certificate: Create / Type .PEM | application/x-x509-ca-cert / Private Key / No Certificate exists'
    },
    {
        Function => 'CertificateCreate',
        Data     => {
            File  => $Certificates[0],
            Type  => 'Cert',
            CType => 'SMIME'
        },
        Expected => 1,
        Name     => 'Certificate: Create / Type .PEM | application/x-x509-ca-cert / Certificate'
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID => '###ID###'
        },
        Index    => 0,
        Expected => {
            'CType'       => 'SMIME',
            'Email'       => 'example@unittest.org',
            'EndDate'     => '2034-04-23 08:40:48',
            'FileID'      => '###ID###',
            'Filename'    => 'KIX_Cert_###ID###',
            'Fingerprint' => '18:0D:E0:E2:A1:AC:C9:46:92:5A:C7:A1:72:28:33:78:0E:68:4E:07',
            'Hash'        => '2930f735',
            'Issuer'      => 'C =  DE, ST =  Saxony, L =  Example, O =  Example, OU =  Example, CN =  Example, emailAddress =  example@unittest.org',
            'Modulus'     => 'C1EB549A1D0AE1C1A7179A5E5C87AAC2482B448E6491CF335BB93A941EE5AA503F458557FE39C1AA6603FAD9C4BA8277042C9C260983E1820EDCA2379218BE946B09092B06B0DEBF91C13E47314443EE5267D3BF40B925009A4D246A5C003916BF2CF4ED350289EBBD63087638A5ED1A6D3B3A7B0DC5BF739ECB25FCFFF004BFB4FE0B4B5767C5FCEED8AFF8A28810AC30298F414AE70AA4B87A4ED5364B7BC6A82CFC880B356FC225C354ECB775F58E8E626FDA9CD79B09676F22F6025B1CB4BBAE818C3971020CD500F9B0AF4060DA6BA99D58EEAAC34442B4CF21A350DEA3D1394B45DB720AB3232A6CB3C17BA4D89440CDF9294C3BA60F2269C08DF0845B',
            'Serial'      => '5A01B5766B5C040B20CFF7E09E998A9B580BA0F4',
            'StartDate'   => '2024-04-25 08:40:48',
            'Subject'     => 'C =  DE, ST =  Saxony, L =  Example, O =  Example, OU =  Example, CN =  Example, emailAddress =  example@unittest.org',
            'Type'        => 'Cert'
        },
        Name     => 'Certificate: Get / Type .PEM | application/x-x509-ca-cert / Certificate'
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID      => '###ID###',
            Include => 'Content'
        },
        Index    => 0,
        Expected => {
            'CType'       => 'SMIME',
            'Email'       => 'example@unittest.org',
            'EndDate'     => '2034-04-23 08:40:48',
            'FileID'      => '###ID###',
            'Filename'    => 'KIX_Cert_###ID###',
            'Fingerprint' => '18:0D:E0:E2:A1:AC:C9:46:92:5A:C7:A1:72:28:33:78:0E:68:4E:07',
            'Hash'        => '2930f735',
            'Issuer'      => 'C =  DE, ST =  Saxony, L =  Example, O =  Example, OU =  Example, CN =  Example, emailAddress =  example@unittest.org',
            'Modulus'     => 'C1EB549A1D0AE1C1A7179A5E5C87AAC2482B448E6491CF335BB93A941EE5AA503F458557FE39C1AA6603FAD9C4BA8277042C9C260983E1820EDCA2379218BE946B09092B06B0DEBF91C13E47314443EE5267D3BF40B925009A4D246A5C003916BF2CF4ED350289EBBD63087638A5ED1A6D3B3A7B0DC5BF739ECB25FCFFF004BFB4FE0B4B5767C5FCEED8AFF8A28810AC30298F414AE70AA4B87A4ED5364B7BC6A82CFC880B356FC225C354ECB775F58E8E626FDA9CD79B09676F22F6025B1CB4BBAE818C3971020CD500F9B0AF4060DA6BA99D58EEAAC34442B4CF21A350DEA3D1394B45DB720AB3232A6CB3C17BA4D89440CDF9294C3BA60F2269C08DF0845B',
            'Serial'      => '5A01B5766B5C040B20CFF7E09E998A9B580BA0F4',
            'StartDate'   => '2024-04-25 08:40:48',
            'Subject'     => 'C =  DE, ST =  Saxony, L =  Example, O =  Example, OU =  Example, CN =  Example, emailAddress =  example@unittest.org',
            'Type'        => 'Cert',
            'Content'     => <<'END'
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQrVENDQXVHZ0F3SUJBZ0lVV2dHMWRtdGNC
QXNnei9mZ25wbUttMWdMb1BRd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2dZc3hDekFKQmdOVkJBWVRB
a1JGTVE4d0RRWURWUVFJREFaVFlYaHZibmt4RURBT0JnTlZCQWNNQjBWNApZVzF3YkdVeEVEQU9C
Z05WQkFvTUIwVjRZVzF3YkdVeEVEQU9CZ05WQkFzTUIwVjRZVzF3YkdVeEVEQU9CZ05WCkJBTU1C
MFY0WVcxd2JHVXhJekFoQmdrcWhraUc5dzBCQ1FFV0ZHVjRZVzF3YkdWQWRXNXBkSFJsYzNRdWIz
Sm4KTUI0WERUSTBNRFF5TlRBNE5EQTBPRm9YRFRNME1EUXlNekE0TkRBME9Gb3dnWXN4Q3pBSkJn
TlZCQVlUQWtSRgpNUTh3RFFZRFZRUU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dkRrSmx2RzRYVWgvdlMzWEdLRFdIZFVVb0ZTUGp5QVRMOC9qWGdYVXpXdXN6a2EwOEhZejhEMEE0
CkRsZWJUNkk4SExMZmdLdGcwTUZKY0ZhL2Rib0dXSVlNcHNYYnpPOE1nQWo5enhyci93TlRnbzNP
U3VLTjFGbFYKR2JFa2Yrbk5vVEx0amdlQnU1OVg5NUhKclhURHN1bG9XbWN0aHQ4SFQvaEtqSGhZ
ODN5c3ZVdXFSWlQ2aER0eApsUmdRbE9ic2EzSFZPMnZ3MGczaVJ4dnNRVGl3WURxc2NmSHljZG93
VHVrV0FlQWRyWm9PY2xyR1hjbVJEc3JuCjRHSkRIU2hSNEJSL2grQWhVQT09Ci0tLS0tRU5EIENF
UlRJRklDQVRFLS0tLS0K
END
        },
        Name     => 'Certificate: Get / Type .PEM | application/x-x509-ca-cert / Certificate / Include Content'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Filename => 'Certificate/SMIME/Cert/###Fingerprint###',
            Silent   => 1
        },
        Index    => 0,
        Expected => 1,
        Name     => 'Certificate: Exists / Type .PEM | application/x-x509-ca-cert / Certificate'
    },
    {
        Function => 'CertificateCreate',
        Data     => {
            File       => $Certificates[1],
            Type       => 'Private',
            Passphrase => 'start123',
            CType      => 'SMIME'
        },
        Expected => 1,
        Name     => 'Certificate: Create / Type .PEM | application/x-x509-ca-cert / Private Key '
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID => '###ID###'
        },
        Index    => 1,
        Expected => {
            'CType'       => 'SMIME',
            'Email'       => 'example@unittest.org',
            'EndDate'     => '2034-04-23 08:40:48',
            'FileID'      => '###ID###',
            'Filename'    => 'KIX_Private_###ID###',
            'Fingerprint' => '18:0D:E0:E2:A1:AC:C9:46:92:5A:C7:A1:72:28:33:78:0E:68:4E:07',
            'Hash'        => '2930f735',
            'Issuer'      => 'C =  DE, ST =  Saxony, L =  Example, O =  Example, OU =  Example, CN =  Example, emailAddress =  example@unittest.org',
            'Modulus'     => 'C1EB549A1D0AE1C1A7179A5E5C87AAC2482B448E6491CF335BB93A941EE5AA503F458557FE39C1AA6603FAD9C4BA8277042C9C260983E1820EDCA2379218BE946B09092B06B0DEBF91C13E47314443EE5267D3BF40B925009A4D246A5C003916BF2CF4ED350289EBBD63087638A5ED1A6D3B3A7B0DC5BF739ECB25FCFFF004BFB4FE0B4B5767C5FCEED8AFF8A28810AC30298F414AE70AA4B87A4ED5364B7BC6A82CFC880B356FC225C354ECB775F58E8E626FDA9CD79B09676F22F6025B1CB4BBAE818C3971020CD500F9B0AF4060DA6BA99D58EEAAC34442B4CF21A350DEA3D1394B45DB720AB3232A6CB3C17BA4D89440CDF9294C3BA60F2269C08DF0845B',
            'Serial'      => '5A01B5766B5C040B20CFF7E09E998A9B580BA0F4',
            'StartDate'   => '2024-04-25 08:40:48',
            'Subject'     => 'C =  DE, ST =  Saxony, L =  Example, O =  Example, OU =  Example, CN =  Example, emailAddress =  example@unittest.org',
            'Type'        => 'Private'
        },
        Name     => 'Certificate: Get / Type .PEM | application/x-x509-ca-cert / Private Key'
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID      => '###ID###',
            Include => 'Content'
        },
        Index    => 1,
        Expected => {
            'CType'       => 'SMIME',
            'Email'       => 'example@unittest.org',
            'EndDate'     => '2034-04-23 08:40:48',
            'FileID'      => '###ID###',
            'Filename'    => 'KIX_Private_###ID###',
            'Fingerprint' => '18:0D:E0:E2:A1:AC:C9:46:92:5A:C7:A1:72:28:33:78:0E:68:4E:07',
            'Hash'        => '2930f735',
            'Issuer'      => 'C =  DE, ST =  Saxony, L =  Example, O =  Example, OU =  Example, CN =  Example, emailAddress =  example@unittest.org',
            'Modulus'     => 'C1EB549A1D0AE1C1A7179A5E5C87AAC2482B448E6491CF335BB93A941EE5AA503F458557FE39C1AA6603FAD9C4BA8277042C9C260983E1820EDCA2379218BE946B09092B06B0DEBF91C13E47314443EE5267D3BF40B925009A4D246A5C003916BF2CF4ED350289EBBD63087638A5ED1A6D3B3A7B0DC5BF739ECB25FCFFF004BFB4FE0B4B5767C5FCEED8AFF8A28810AC30298F414AE70AA4B87A4ED5364B7BC6A82CFC880B356FC225C354ECB775F58E8E626FDA9CD79B09676F22F6025B1CB4BBAE818C3971020CD500F9B0AF4060DA6BA99D58EEAAC34442B4CF21A350DEA3D1394B45DB720AB3232A6CB3C17BA4D89440CDF9294C3BA60F2269C08DF0845B',
            'Serial'      => '5A01B5766B5C040B20CFF7E09E998A9B580BA0F4',
            'StartDate'   => '2024-04-25 08:40:48',
            'Subject'     => 'C =  DE, ST =  Saxony, L =  Example, O =  Example, OU =  Example, CN =  Example, emailAddress =  example@unittest.org',
            'Type'        => 'Private',
            'Content'     => <<'END'
LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUV2QUlCQURBTkJna3Foa2lHOXcwQkFRRUZB
QVNDQktZd2dnU2lBZ0VBQW9JQkFRREI2MVNhSFFyaHdhY1gKbWw1Y2g2ckNTQ3RFam1TUnp6TmJ1
VHFVSHVXcVVEOUZoVmYrT2NHcVpnUDYyY1M2Z25jRUxKd21DWVBoZ2c3YwpvamVTR0w2VWF3a0pL
d2F3M3IrUndUNUhNVVJEN2xKbjA3OUF1U1VBbWswa2Fsd0FPUmEvTFBUdE5RS0o2NzFqCkNIWTRw
ZTBhYlRzNmV3M0Z2M09leXlYOC8vQUV2N1QrQzB0WFo4WDg3dGl2K0tLSUVLd3dLWTlCU3VjS3BM
aDYKVHRVMlMzdkdxQ3o4aUFzMWI4SWx3MVRzdDNYMWpvNWliOXFjMTVzSloyOGk5Z0piSExTN3Jv
R01PWEVDRE5VQQorYkN2UUdEYWE2bWRXTzZxdzBSQ3RNOGhvMURlbzlFNVMwWGJjZ3F6SXlwc3M4
RjdwTmlVUU0zNUtVdzdwZzhpCmFjQ044SVJiQWdNQkFBRUNnZ0VBTXAyTVBaV3JDM1lTZVJTdjRK
TUF2U2s5TWUzQXpsWTQzNDRmZmgzNmNGUDEKejkyWU5DRTdMWkRuSlF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ZaSGJlR0ZmM3VPTkd2SUUrcGQwY1ZyZ1EzUGZmQnlVdWZycWFoSFAv
L2poaFo3eUpBb0dBVmxsaQpBajJZWlZFQnE0MkxUTnBOSG1uNzRuT0JFK3M1WXFUS2w1dzBQRmJk
MVZhbmdsK0pZSjRVTnBSajhRRDMyWUJ1Ckg5VkIzOUN2d0U3RFBkSHl1NFVlYjlmRFNocW42WXVB
alg2c1g1NHNTbEdOUkxCMmtTZWJ6UnB2amhOQVF5WGgKMFoyVGdCREkwcGhYaksxV0tETDFENDND
RkpYV3VHYjBjRFVocmRjQ2dZQjFNems5TXdLNzlBRGw2V2pDVFRFOQppbkJQbmlsU2N6UDdHSzdS
VENpdVZiVnVYQ1J2eVJHaHZMazNjamFHSkNSUmducWVaS0tpQ09TZ3g0YnBndlBVCnJ6ZmU1UWVF
U0VxQTAwbGZmL08vMVlvTVU0YTY4NVkwOGt2WTBSTkl0dlBacEMyVm1ZTEpqL25IUUlDZWVRSEoK
TUVWd0FIYUlhWXh3dE9CUXBNZFFuZz09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K
END
        },
        Name     => 'Certificate: Get / Type .PEM | application/x-x509-ca-cert / Private Key / Include Content'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Modulus         => '###Modulus###',
            Type            => 'Private',
            HasCertificate  => 1,
            CType           => 'SMIME',
            Silent          => 1
        },
        Index    => 1,
        Expected => 1,
        Name     => 'Certificate: Exists / Type .PEM | application/x-x509-ca-cert / Private Key / Exists Certificate'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Filename => 'Certificate/SMIME/Private/###Fingerprint###',
            Silent   => 1
        },
        Index    => 1,
        Expected => 1,
        Name     => 'Certificate: Exists / Type .PEM | application/x-x509-ca-cert / Private Key'
    },
    {
        Function => 'CertificateCreate',
        Data     => {
            File  => $Certificates[2],
            Type  => 'Cert',
            CType => 'SMIME'
        },
        Expected => 1,
        Name     => 'Certificate: Create / Type .CRT | application/pkix-cert / Certificate'
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID => '###ID###'
        },
        Index    => 2,
        Expected => {
            'CType'       => 'SMIME',
            'Email'       => 'example@unittest.org',
            'EndDate'     => '2029-04-24 08:44:04',
            'FileID'      => '###ID###',
            'Filename'    => 'KIX_Cert_###ID###',
            'Fingerprint' => 'CE:9A:76:C0:B4:8A:C4:B6:C7:3D:CF:F4:A9:A5:CC:60:3D:E9:7D:47',
            'Hash'        => 'a292bbe5',
            'Issuer'      => 'C =  DE, L =  Example, O =  Example, CN =  Unit Test, emailAddress =  example@unittest.org',
            'Modulus'     => 'C3914528F589E7AAC8F55DECD9E2AF9F2FAF0667E8B5E63522A80748E6A1F96E7BA5EEC024DEBDB94A70FC2679EB5ECE77B26F9CFBAC96C065753A008FA8D888116C3DFAAA43DE313356D83FD794031DB70F01BF3007F12185A763F0B55A10EAA306492B2504323AD1F7904263F775E5AE47750F7AA7A6F367614F7F6519F8E56438A0F279931CD1955DC4F6368CFED754CA3EE1295A0C8EFB64272042901445272D9E573027754B2FE8DA92B9C8948B53EBCDDE62BFF8FBCCEDBC46A3FC843B52DBCDEDE084913B6CA23FB95B90C9CE1427DF30DEAC6359FBE9EC501A9C2F368387D22DAACCD726DF3F66D9CA26C7BCBEBD643C066A566CD15A14EDED0EEFCF',
            'Serial'      => '02E3486D597C5309C1CAA42F83021DB43A6DDE9D',
            'StartDate'   => '2024-04-25 08:44:04',
            'Subject'     => 'C =  DE, L =  Example, O =  Example, CN =  Unit Test, emailAddress =  example@unittest.org',
            'Type'        => 'Cert'
        },
        Name     => 'Certificate: Get / Type .CRT | application/pkix-cert / Certificate'
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID      => '###ID###',
            Include => 'Content'
        },
        Index    => 2,
        Expected => {
            'CType'       => 'SMIME',
            'Email'       => 'example@unittest.org',
            'EndDate'     => '2029-04-24 08:44:04',
            'FileID'      => '###ID###',
            'Filename'    => 'KIX_Cert_###ID###',
            'Fingerprint' => 'CE:9A:76:C0:B4:8A:C4:B6:C7:3D:CF:F4:A9:A5:CC:60:3D:E9:7D:47',
            'Hash'        => 'a292bbe5',
            'Issuer'      => 'C =  DE, L =  Example, O =  Example, CN =  Unit Test, emailAddress =  example@unittest.org',
            'Modulus'     => 'C3914528F589E7AAC8F55DECD9E2AF9F2FAF0667E8B5E63522A80748E6A1F96E7BA5EEC024DEBDB94A70FC2679EB5ECE77B26F9CFBAC96C065753A008FA8D888116C3DFAAA43DE313356D83FD794031DB70F01BF3007F12185A763F0B55A10EAA306492B2504323AD1F7904263F775E5AE47750F7AA7A6F367614F7F6519F8E56438A0F279931CD1955DC4F6368CFED754CA3EE1295A0C8EFB64272042901445272D9E573027754B2FE8DA92B9C8948B53EBCDDE62BFF8FBCCEDBC46A3FC843B52DBCDEDE084913B6CA23FB95B90C9CE1427DF30DEAC6359FBE9EC501A9C2F368387D22DAACCD726DF3F66D9CA26C7BCBEBD643C066A566CD15A14EDED0EEFCF',
            'Serial'      => '02E3486D597C5309C1CAA42F83021DB43A6DDE9D',
            'StartDate'   => '2024-04-25 08:44:04',
            'Subject'     => 'C =  DE, L =  Example, O =  Example, CN =  Unit Test, emailAddress =  example@unittest.org',
            'Type'        => 'Cert',
            'Content'     => <<'END'
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURtekNDQW9PZ0F3SUJBZ0lVQXVOSWJWbDhV
d25CeXFRdmd3SWR0RHB0M3Awd0RRWUpLb1pJaHZjTkFRRUwKQlFBd2FqRUxNQWtHQTFVRUJoTUNS
RVV4RURBT0JnTlZCQWNNQjBWNFlXMXdiR1V4RURBT0JnTlZCQW9NQjBWNApZVzF3YkdVeEVqQVFC
Z05WQkFNTUNWVnVhWFFnVkdWemRERWpNQ0VHQ1NxR1NJYjNEUUVKQVJZVVpYaGhiWEJzClpVQjFi
bWwwZEdWemRDNXZjbWN3SGhjTk1qUXdOREkxTURnME5EQTBXaGNOTWprd05ESTBNRGcwTkRBMFdq
QnEKTVFzd0NRWUR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UUwKUHFQWktnalJvZjRHcFFX
eG1mWWhEazIzNWNlYkpkZXZGczIwT3V4Y3pZanJDVm1aQkxlVTk1MjYrNEJZT2dlawpDMmp2czM0
NVNtc3FTN1htVnZ5WjNINTFRdjRwMmR3OFh3a0pOWm54U3lOUWl4R1E3M3ZIcDUxTDIvTHd0WGhZ
CjJmMVhHZ3pybkxzYlBmWDQ1TFhLSTJLbGhUR1RtQUtZckxydFd4MlpDTEs1WmUxaEhHUmoxdURU
Zy9oa3ZMQjQKanorZkFnOEx0dWovMW9kK09zWWIKLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
END
        },
        Name     => 'Certificate: Get / Type .CRT | application/pkix-cert / Certificate / Include Content'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Filename => 'Certificate/SMIME/Cert/###Fingerprint###',
            Silent   => 1
        },
        Index    => 2,
        Expected => 1,
        Name     => 'Certificate: Exists / Type .CRT | application/pkix-cert / Certificate'
    },
    {
        Function => 'CertificateCreate',
        Data     => {
            File  => $Certificates[3],
            Type  => 'Cert',
            CType => 'SMIME'
        },
        Expected => 1,
        Name     => 'Certificate: Create / Type .CSR | application/pkcs10 / Certificate'
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID => '###ID###'
        },
        Index    => 3,
        Expected => {
            'CType'       => 'SMIME',
            'FileID'      => '###ID###',
            'Filename'    => 'KIX_Cert_###ID###',
            'Fingerprint' => 'da:39:a3:ee:5e:6b:4b:0d:32:55:bf:ef:95:60:18:90:af:d8:07:09',
            'Modulus'     => 'C3914528F589E7AAC8F55DECD9E2AF9F2FAF0667E8B5E63522A80748E6A1F96E7BA5EEC024DEBDB94A70FC2679EB5ECE77B26F9CFBAC96C065753A008FA8D888116C3DFAAA43DE313356D83FD794031DB70F01BF3007F12185A763F0B55A10EAA306492B2504323AD1F7904263F775E5AE47750F7AA7A6F367614F7F6519F8E56438A0F279931CD1955DC4F6368CFED754CA3EE1295A0C8EFB64272042901445272D9E573027754B2FE8DA92B9C8948B53EBCDDE62BFF8FBCCEDBC46A3FC843B52DBCDEDE084913B6CA23FB95B90C9CE1427DF30DEAC6359FBE9EC501A9C2F368387D22DAACCD726DF3F66D9CA26C7BCBEBD643C066A566CD15A14EDED0EEFCF',
            'Subject'     => 'C =  DE, L =  Example, O =  Example, CN =  Unit Test, emailAddress =  example@unittest.org',
            'Type'        => 'Cert',
            'Verify'      => 'OK'
        },
        Name     => 'Certificate: Get / Type .CSR | application/pkcs10 / Certificate'
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID      => '###ID###',
            Include => 'Content'
        },
        Index    => 3,
        Expected => {
            'CType'       => 'SMIME',
            'FileID'      => '###ID###',
            'Filename'    => 'KIX_Cert_###ID###',
            'Fingerprint' => 'da:39:a3:ee:5e:6b:4b:0d:32:55:bf:ef:95:60:18:90:af:d8:07:09',
            'Modulus'     => 'C3914528F589E7AAC8F55DECD9E2AF9F2FAF0667E8B5E63522A80748E6A1F96E7BA5EEC024DEBDB94A70FC2679EB5ECE77B26F9CFBAC96C065753A008FA8D888116C3DFAAA43DE313356D83FD794031DB70F01BF3007F12185A763F0B55A10EAA306492B2504323AD1F7904263F775E5AE47750F7AA7A6F367614F7F6519F8E56438A0F279931CD1955DC4F6368CFED754CA3EE1295A0C8EFB64272042901445272D9E573027754B2FE8DA92B9C8948B53EBCDDE62BFF8FBCCEDBC46A3FC843B52DBCDEDE084913B6CA23FB95B90C9CE1427DF30DEAC6359FBE9EC501A9C2F368387D22DAACCD726DF3F66D9CA26C7BCBEBD643C066A566CD15A14EDED0EEFCF',
            'Subject'     => 'C =  DE, L =  Example, O =  Example, CN =  Unit Test, emailAddress =  example@unittest.org',
            'Type'        => 'Cert',
            'Verify'      => 'OK',
            'Content'     => <<'END'
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURSBSRVFVRVNULS0tLS0KTUlJQ3J6Q0NBWmNDQVFBd2FqRUxN
QWtHQTFVRUJoTUNSRVV4RURBT0JnTlZCQWNNQjBWNFlXMXd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b2RjaHpRYjFVWGhMY2NsWHdjTgpuY3pMNjUzV0Q1czFGcGRSWUNoOCtYTStJaUNid1p2
b1M0TmNycTgxUDQzMndqZjlwdExiRURMYWJsakJMZkZXCjdLUEhYNGtJSWpPUDhGUXhIZ3Y5Y0I2
SVVBPT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUgUkVRVUVTVC0tLS0tCg==
END
        },
        Name     => 'Certificate: Get / Type .CSR | application/pkcs10 / Certificate / Include Content'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Filename => 'Certificate/SMIME/Cert/###Fingerprint###',
            Silent   => 1
        },
        Index    => 3,
        Expected => 1,
        Name     => 'Certificate: Exists / Type .CSR | application/pkcs10 / Certificate'
    },
    {
        Function => 'CertificateCreate',
        Data     => {
            File       => $Certificates[4],
            Type       => 'Private',
            Passphrase => 'start123',
            CType      => 'SMIME'
        },
        Expected => 1,
        Name     => 'Certificate: Create / Type .KEY | application/x-iwork-keynote-sffkey / Private Key '
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID => '###ID###'
        },

        Index    => 4,
        Expected => {
            'CType'       => 'SMIME',
            'EndDate'     => '2029-04-24 08:44:04',
            'Email'       => 'example@unittest.org',
            'FileID'      => '###ID###',
            'Filename'    => 'KIX_Private_###ID###',
            'Fingerprint' => 'CE:9A:76:C0:B4:8A:C4:B6:C7:3D:CF:F4:A9:A5:CC:60:3D:E9:7D:47',
            'Hash'        => 'a292bbe5',
            'Issuer'      => 'C =  DE, L =  Example, O =  Example, CN =  Unit Test, emailAddress =  example@unittest.org',
            'Modulus'     => 'C3914528F589E7AAC8F55DECD9E2AF9F2FAF0667E8B5E63522A80748E6A1F96E7BA5EEC024DEBDB94A70FC2679EB5ECE77B26F9CFBAC96C065753A008FA8D888116C3DFAAA43DE313356D83FD794031DB70F01BF3007F12185A763F0B55A10EAA306492B2504323AD1F7904263F775E5AE47750F7AA7A6F367614F7F6519F8E56438A0F279931CD1955DC4F6368CFED754CA3EE1295A0C8EFB64272042901445272D9E573027754B2FE8DA92B9C8948B53EBCDDE62BFF8FBCCEDBC46A3FC843B52DBCDEDE084913B6CA23FB95B90C9CE1427DF30DEAC6359FBE9EC501A9C2F368387D22DAACCD726DF3F66D9CA26C7BCBEBD643C066A566CD15A14EDED0EEFCF',
            'Serial'      => '02E3486D597C5309C1CAA42F83021DB43A6DDE9D',
            'StartDate'   => '2024-04-25 08:44:04',
            'Subject'     => 'C =  DE, L =  Example, O =  Example, CN =  Unit Test, emailAddress =  example@unittest.org',
            'Type'        => 'Private'
        },
        Name     => 'Certificate: Get / Type .KEY | application/x-iwork-keynote-sffkey / Private Key'
    },
    {
        Function => 'CertificateGet',
        Data     => {
            ID      => '###ID###',
            Include => 'Content'
        },

        Index    => 4,
        Expected => {
            'CType'       => 'SMIME',
            'EndDate'     => '2029-04-24 08:44:04',
            'Email'       => 'example@unittest.org',
            'FileID'      => '###ID###',
            'Filename'    => 'KIX_Private_###ID###',
            'Fingerprint' => 'CE:9A:76:C0:B4:8A:C4:B6:C7:3D:CF:F4:A9:A5:CC:60:3D:E9:7D:47',
            'Hash'        => 'a292bbe5',
            'Issuer'      => 'C =  DE, L =  Example, O =  Example, CN =  Unit Test, emailAddress =  example@unittest.org',
            'Modulus'     => 'C3914528F589E7AAC8F55DECD9E2AF9F2FAF0667E8B5E63522A80748E6A1F96E7BA5EEC024DEBDB94A70FC2679EB5ECE77B26F9CFBAC96C065753A008FA8D888116C3DFAAA43DE313356D83FD794031DB70F01BF3007F12185A763F0B55A10EAA306492B2504323AD1F7904263F775E5AE47750F7AA7A6F367614F7F6519F8E56438A0F279931CD1955DC4F6368CFED754CA3EE1295A0C8EFB64272042901445272D9E573027754B2FE8DA92B9C8948B53EBCDDE62BFF8FBCCEDBC46A3FC843B52DBCDEDE084913B6CA23FB95B90C9CE1427DF30DEAC6359FBE9EC501A9C2F368387D22DAACCD726DF3F66D9CA26C7BCBEBD643C066A566CD15A14EDED0EEFCF',
            'Serial'      => '02E3486D597C5309C1CAA42F83021DB43A6DDE9D',
            'StartDate'   => '2024-04-25 08:44:04',
            'Subject'     => 'C =  DE, L =  Example, O =  Example, CN =  Unit Test, emailAddress =  example@unittest.org',
            'Type'        => 'Private',
            'Content'     => <<'END'
LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpQcm9jLVR5cGU6IDQsRU5DUllQVEVECkRF
Sy1JbmZvOiBERVMtRURFMy1DQkMsNzlFOUY1N0NENzgwQ0NGOAoKOG92aHhYbmR0ZUFSczVYZkky
OGtiUTNDb0IvYkJoU0t4TVlHZk91eVBpOTVMZHFCYzlPVFAzblhlVUxFK2h2bAorQzVhNzhvbUk1
MVlWVWRwR2ZraUpwOWZrR2FXZ2RqU1RRSU9YWTZDSU5tNWpXZ1BadXlLbS9kU1JzTFg0M2w0Ci94
elRObm5GdVJ5UUI2RTB6bWFyMmlZMEZ5OXEvTnY5cjN5NS9QSmU0TVJTK3F4OWNEMEdyajd5TW5N
c3U3ZUcKc2pMYy9XVDQzeTBuaFZxSmlNbmlLaXN4OE4yVURMZmtUTGtpY3k5MjI2eW9Pek4zT3Y0
cWFFWWhzeFEvYUxxRwovcmVpZ2RoMjBLZTl6QTF2MzlzSGZkRzgyZVJhS2hUVmYxUGdZdVRwdGVk
Y0lkR2FYSGVIOE1CbWlwSEttTGlTClA3a2M2aUplcXp2Y2txVEZQRTBzSWJtQ1JsUDFXVVdLMTRn
Q0FROEN2R0g0WlN6WWlrMDNmODlNZE1hSVRFbGEKeUxMN2VQSmdPRWh6N3RyUVlZejZQTzNRUzlZ
ZGZSRDRRSXk2T1MyRFJSMmx3QUZ0cmJ6NW5zTEZOZHllTTBOVQoyM2JwWEdxWml6VHI0Vkhod090
T09TaWJxTU5SU1VYbTZXMW4xNmlnVElKU054em1WK0MzaFl3OFNjdzlFWDZiCnVOMDhqTWdDVkRB
WnRqZ2k0K1d1SzR3c3h4YUpjMElsS0ZZa1dEcTA3Y09ZWDkzbXl6SmxpZS9Eb29IeGNidHYKW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FFDTFJZekJtaGRHRW1z
aHFVTm5SdjFXS2t3ZXRlKzVROU4rbitzNDBxUUpvcW1tbVMwcjA3VkwxcQpPd0RpU0pMUDhuRVh1
dzdBa01sNDdnMHFWZnVmZTVFUG1RTjg5ajJjSVNJdzkzRkxHeUZtWVp3WGhoOVVoVDhPClFxTHVj
YkJXVWcveDU2Q2xuRk8zZDZlOXNtcWtxblVlVDZRNHVZZmZhV1ZBTXBEVFVEVXNKT3hQUFB1RnNC
b2gKUjVkU0lWVy9wQWh4bUJ4eE0vUGRiaGZOTU5zWFF2dWx6eEpMRUZpaFFBUlNURGZBV2tENExF
SUNDc2poL1lPLwotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=
END
        },
        Name     => 'Certificate: Get / Type .KEY | application/x-iwork-keynote-sffkey / Private Key / Include Content'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Modulus         => '###Modulus###',
            Type            => 'Private',
            HasCertificate  => 1,
            CType           => 'SMIME',
            Silent          => 1
        },
        Index    => 4,
        Expected => 1,
        Name     => 'Certificate: Exists / Type .KEY | application/x-iwork-keynote-sffkey / Private Key / Exists Certificate'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Filename => 'Certificate/SMIME/Private/###Fingerprint###',
            Silent   => 1
        },
        Index    => 4,
        Expected => 1,
        Name     => 'Certificate: Exists / Type .KEY | application/x-iwork-keynote-sffkey / Private Key'
    },
);

$Kernel::OM->ObjectsDiscard(
    Objects => ['ObjectSearch'],
);

for my $Test ( @Tests ) {
    my $Function = $Test->{Function};

    my $Data = $Test;
    if ( defined $Test->{Index} ) {
        my $Tmp = $Kernel::OM->Get('JSON')->Encode(
            Data => $Data
        );
        for my $Key ( keys %{$CertificateIDs[$Test->{Index}]} ) {
            my $Pattern = "###$Key###";
            my $Replace = $CertificateIDs[$Test->{Index}]->{$Key};

            $Tmp =~ s/$Pattern/$Replace/g;
        }

        $Data = $Kernel::OM->Get('JSON')->Decode(
            Data => $Tmp
        );
    }

    if (
        $Function eq 'CertificateGet'
        && scalar(@CertificateIDs)
    ) {
        $Data->{Data}->{ID} = $CertificateIDs[$Data->{Index}]->{ID};
    }

    my $Result   = $Kernel::OM->Get('Certificate')->$Function(
        %{$Data->{Data}}
    );

    if ( $Function eq 'CertificateGet' ) {
        $Self->IsDeeply(
            $Result,
            $Data->{Expected},
            $Data->{Name}
        );
    }
    else {
        if ( $Data->{Expected} ) {
            $Self->True(
                $Result,
                $Data->{Name}
            );
        }
        else {
            $Self->False(
                $Result,
                $Data->{Name}
            );
        }
    }

    if (
        $Function eq 'CertificateCreate'
        && $Result
    ) {
        push(
            @CertificateIDs,
            {
                ID => $Result
            }
        );
    }

    if (
        $Function eq 'CertificateGet'
        && $Result
    ) {
        %{$CertificateIDs[$Data->{Index}]} = (
            %{$CertificateIDs[$Data->{Index}]},
            %{$Result}
        );
    }

    $Kernel::OM->ObjectsDiscard(
        Objects => ['Certificate'],
    );
}

# Removing only the certificats and keys in the directory
_RemoveFiles();

# Certificate: Export / Export Certificates/Private Keys to FS
my $Result = $Kernel::OM->Get('Certificate')->CertificateToFS();

$Self->Is(
    $Result,
    1,
    'Certificate: Export / Export Certificates/Private Keys to FS'
);

_ExistsFilesInFS();

# Certificate: Delete
my @DeleteTests = (
    {
        Function => 'CertificateDelete',
        Data     => {
            ID  => $CertificateIDs[0]->{ID}
        },
        Expected => 1,
        Name     => 'Certificate: Delete / Type .PEM | application/x-x509-ca-cert / Certificate'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Filename => 'Certificate/SMIME/Cert/' . $CertificateIDs[0]->{Fingerprint},
        },
        Expected => undef,
        Name     => 'Certificate: Exists / Type .PEM | application/x-x509-ca-cert / Certificate'
    },
    {
        Function => 'CertificateDelete',
        Data     => {
            ID  => $CertificateIDs[1]->{ID}
        },
        Expected => 1,
        Name     => 'Certificate: Delete / Type .PEM | application/x-x509-ca-cert / Private Key '
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Filename => 'Certificate/SMIME/Cert/' . $CertificateIDs[1]->{Fingerprint},
        },
        Expected => undef,
        Name     => 'Certificate: Exists / Type .PEM | application/x-x509-ca-cert / Private Key'
    },
    {
        Function => 'CertificateDelete',
        Data     => {
            ID  => $CertificateIDs[2]->{ID}
        },
        Expected => 1,
        Name     => 'Certificate: Delete / Type .CRT | application/pkix-cert / Certificate'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Filename => 'Certificate/SMIME/Cert/' . $CertificateIDs[2]->{Fingerprint},
        },
        Expected => undef,
        Name     => 'Certificate: Exists / Type .CRT | application/pkix-cert / Certificate'
    },
    {
        Function => 'CertificateDelete',
        Data     => {
            ID  => $CertificateIDs[3]->{ID}
        },
        Expected => 1,
        Name     => 'Certificate: Delete / Type .CSR | application/pkcs10 / Certificate'
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Filename => 'Certificate/SMIME/Cert/' . $CertificateIDs[3]->{Fingerprint},
        },
        Expected => undef,
        Name     => 'Certificate: Exists / Type .CSR | application/pkcs10 / Certificate'
    },
    {
        Function => 'CertificateDelete',
        Data     => {
            ID  => $CertificateIDs[4]->{ID}
        },
        Expected => 1,
        Name     => 'Certificate: Delete / Type .KEY | application/x-iwork-keynote-sffkey / Private Key '
    },
    {
        Function => 'CertificateExists',
        Data     => {
            Filename => 'Certificate/SMIME/Cert/' . $CertificateIDs[4]->{Fingerprint},
        },
        Expected => undef,
        Name     => 'Certificate: Exists / Type .KEY | application/x-iwork-keynote-sffkey / Private Key'
    },
);

$Kernel::OM->ObjectsDiscard(
    Objects => ['ObjectSearch'],
);

for my $Test ( @DeleteTests ) {
    my $Function = $Test->{Function};

    my $Result   = $Kernel::OM->Get('Certificate')->$Function(
        %{$Test->{Data}}
    );

    $Self->Is(
        $Result,
        $Test->{Expected},
        $Test->{Name}
    );
}


sub _RemoveFiles {
    my ( %Param ) = @_;

    for my $Cert ( @CertificateIDs ) {
        my $Pre = $Cert->{Type} eq 'Cert' ? 'certs' : 'private';
        my $Success = $Kernel::OM->Get('Main')->FileDelete(
            Location        => $HomeDir . '/var/ssl/'. $Pre . '/KIX_' . $Cert->{Type} . '_' . $Cert->{FileID}
        );
    }

    return 1;
}

sub _ExistsFilesInFS {
    my ( %Param ) = @_;
    my %Counts = (
        Cert    => 0,
        Private => 0
    );

    for my $Cert ( @CertificateIDs ) {
        my $Pre = $Cert->{Type} eq 'Cert' ? 'certs' : 'private';
        my $Cnt = $Kernel::OM->Get('Main')->FileRead(
            Location        => $HomeDir . '/var/ssl/'. $Pre . '/KIX_' . $Cert->{Type} . '_' . $Cert->{FileID},
            Result          => 'SCALAR',
            DisableWarnings => 1
        );
        if ( $Cnt ) {
            $Counts{$Cert->{Type}}++;
        }
    }


    $Self->IsDeeply(
        \%Counts,
        {
            Cert    => 3,
            Private => 2
        },
        'Certificate: Export / Check / Count: Certificates 3 | Private Key 2'
    );

    return 1;
}

sub _ReadCertificates {
    my ( %Param ) = @_;

    my @List;
    my @Files = (
        {
            Filename    => 'ExampleCA.pem',
            Filesize    => 2_800,
            ContentType => 'application/x-x509-ca-cert',
            Name        => 'Certificate: Read / Type .PEM | application/x-x509-ca-cert / Certificate'
        },
        {
            Filename    => 'ExampleKey.pem',
            Filesize    => 1_704,
            ContentType => 'application/x-x509-ca-cert',
            Name        => 'Certificate: Read / Type .PEM | application/x-x509-ca-cert / Private Key'
        },
        {
            Filename    => 'Example.crt',
            Filesize    => 1_310,
            ContentType => 'application/pkix-cert',
            Name        => 'Certificate: Read / Type .CRT | application/pkix-cert / Certificate'
        },
        {
            Filename    => 'Example.csr',
            Filesize    => 1_009,
            ContentType => 'application/pkcs10',
            Name        => 'Certificate: Read / Type .CSR | application/pkcs10 / Certificate'
        },
        {
            Filename    => 'Example.key',
            Filesize    => 1_751,
            ContentType => 'application/x-iwork-keynote-sffkey',
            Name        => 'Certificate: Read / Type .KEY | application/x-iwork-keynote-sffkey / Private Key'
        }
    );

    for my $File ( @Files ) {
        my $Content = $Kernel::OM->Get('Main')->FileRead(
            Directory => $HomeDir . '/scripts/test/system/sample/Certificate',
            Filename  => $File->{Filename},
            Mode      => 'binmode'
        );

        $Self->True(
            $Content,
            $File->{Name} . ' / Exists'
        );

        push (
            @List,
            {
                Filename    => $File->{Filename},
                Filesize    => $File->{Filesize},
                ContentType => $File->{ContentType},
                Content     => MIME::Base64::encode_base64( ${$Content} )
            }
        )
    }

    return @List;
}

# rollback transaction on database
$Helper->Rollback();

1;

=back

=head1 TERMS AND CONDITIONS

This software is part of the KIX project
(L<https://www.kixdesk.com/>).

This software comes with ABSOLUTELY NO WARRANTY. For details, see the enclosed file
LICENSE-AGPL for license information (AGPL). If you did not receive this file, see

<https://www.gnu.org/licenses/agpl.txt>.

=cut
