1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
use strict;
use warnings;
use utf8;
use vars (qw($Self));
use File::Path qw(mkpath rmtree);
use Devel::Peek;
use Kernel::System::Crypt::SMIME;
# get needed objects
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
# get helper object
$Kernel::OM->ObjectParamAdd(
'Kernel::System::UnitTest::Helper' => {
RestoreDatabase => 1,
},
);
my $HelperObject = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
my $HomeDir = $ConfigObject->Get('Home');
# create directory for certificates and private keys
my $CertPath = $ConfigObject->Get('Home') . "/var/tmp/certs";
my $PrivatePath = $ConfigObject->Get('Home') . "/var/tmp/private";
mkpath( [$CertPath], 0, 0770 ); ## no critic
mkpath( [$PrivatePath], 0, 0770 ); ## no critic
# set SMIME paths
$ConfigObject->Set(
Key => 'SMIME::CertPath',
Value => $CertPath,
);
$ConfigObject->Set(
Key => 'SMIME::PrivatePath',
Value => $PrivatePath,
);
my $OpenSSLBin = $ConfigObject->Get('SMIME::Bin');
# set config
$ConfigObject->Set(
Key => 'SMIME',
Value => 1,
);
$ConfigObject->Set(
Key => 'SMIME::FetchFromCustomer',
Value => 1,
);
# check if openssl is located there
if ( !-e $ConfigObject->Get('SMIME::Bin') ) {
# maybe it's a mac with macport
if ( -e '/opt/local/bin/openssl' ) {
$ConfigObject->Set(
Key => 'SMIME::Bin',
Value => '/opt/local/bin/openssl',
);
}
}
my $SMIMEObject = $Kernel::OM->Get('Kernel::System::Crypt::SMIME');
if ( !$SMIMEObject ) {
print STDERR "NOTICE: No SMIME support!\n";
if ( !-e $OpenSSLBin ) {
$Self->False(
1,
"No such $OpenSSLBin!",
);
}
elsif ( !-x $OpenSSLBin ) {
$Self->False(
1,
"$OpenSSLBin not executable!",
);
}
elsif ( !-e $CertPath ) {
$Self->False(
1,
"No such $CertPath!",
);
}
elsif ( !-d $CertPath ) {
$Self->False(
1,
"No such $CertPath directory!",
);
}
elsif ( !-w $CertPath ) {
$Self->False(
1,
"$CertPath not writable!",
);
}
elsif ( !-e $PrivatePath ) {
$Self->False(
1,
"No such $PrivatePath!",
);
}
elsif ( !-d $Self->{PrivatePath} ) {
$Self->False(
1,
"No such $PrivatePath directory!",
);
}
elsif ( !-w $PrivatePath ) {
$Self->False(
1,
"$PrivatePath not writable!",
);
}
return 1;
}
=item $CertificateSearch->()
searching for unittest-added certificates
returns found certificates with attributes
my $Result = $CertificateSearch->();
Returns:
same return as $Kernel::OM->Get('Kernel::System::Crypt::SMIME')->CertificateSearch(
Search => 'SearchString'
);
=cut
my $CertificateSearch = sub {
my ( $Self, %Param ) = @_;
my @Result;
my $SMIMEObject = $Kernel::OM->Get('Kernel::System::Crypt::SMIME');
my %Search = (
1 => 'unittest@example.org',
2 => 'unittest2@example.org',
3 => 'unittest3@example.org',
4 => 'unittest4@example.org',
5 => 'unittest5@example.org',
);
for my $SearchString ( values %Search, 'john.doe@example.com' ) {
my @Certificat = $SMIMEObject->CertificateSearch( Search => $SearchString );
if ( defined $Certificat[0]->{Filename} && !grep { $_->{Filename} eq $Certificat[0]->{Filename} } @Result ) {
push @Result, @Certificat;
}
}
return @Result;
};
# delete all before running
my @PreRunSearchResult = $CertificateSearch->();
for my $Cert (@PreRunSearchResult) {
$SMIMEObject->CertificateRemove(
Filename => $Cert->{Filename},
);
}
# All certificates for testing
my @Certificates = (
{
CertificateName => 'Check1',
CertificateFileName1 => 'SMIMECertificate-1.asc',
CertificateFileName2 => 'SMIMECertificate-1.p7b',
CertificateFileName3 => 'SMIMECertificate-1.der',
CertificateFileName4 => 'SMIMECertificate-1.pfx',
CertificatePassFile => 'SMIMEPrivateKeyPass-1.asc',
Success => 1
},
{
CertificateName => 'Check2',
CertificateFileName1 => 'SMIMECertificate-2.asc',
CertificateFileName2 => 'SMIMECertificate-2.p7b',
CertificateFileName3 => 'SMIMECertificate-2.der',
CertificateFileName4 => 'SMIMECertificate-2.pfx',
CertificatePassFile => 'SMIMEPrivateKeyPass-2.asc',
Success => 1
},
{
CertificateName => 'Check3',
CertificateFileName1 => 'SMIMECertificate-3.asc',
CertificateFileName2 => 'SMIMECertificate-3.p7b',
CertificateFileName3 => 'SMIMECertificate-3.der',
CertificateFileName4 => 'SMIMECertificate-3.pfx',
CertificatePassFile => 'SMIMEPrivateKeyPass-3.asc',
Success => 1
},
{
CertificateName => 'OTRSUserCert',
CertificateFileName1 => 'SMIMECertificate-smimeuser1.crt',
CertificateFileName2 => 'SMIMECertificate-smimeuser1.p7b',
CertificateFileName3 => 'SMIMECertificate-smimeuser1.der',
CertificateFileName4 => 'SMIMECertificate-smimeuser1.pfx',
CertificatePassFile => 'SMIMEPrivateKeyPass-smimeuser1.crt',
Success => 0 # Test with passfile will fail (wrong password)
},
);
=item $CertificationConversionTest->()
do the testing
- read
- convert
- search-empty
- add
- search-filled
- remove
returns Certificate String converted to PEM-format
my $Result = $CertificationConversionTest->(
$CertificateString, # filename
'PEM', # format # PEM, PKCS#7/P7B, DER, PFX
'PemCertificateString', # CheckString # (optional), FilereadString if empty
'Path/to/PassFile' # pass-filename # (optional) only needed for PFX
);
Returns:
$Result =
"-----BEGIN CERTIFICATE-----
MIIEXjCCA0agAwIBAgIJAPIBQyBe/HbpMA0GCSqGSIb3DQEBBQUAMHwxCzAJBgNV
...
nj2wbQO4KjM12YLUuvahk5se
-----END CERTIFICATE-----
";
=cut
my $CertificationConversionTest = sub {
my ( $Success, $CertificateFileName, $Format, $CheckString, $CertificatePassFile ) = @_;
return if !defined $Success;
return if !$CertificateFileName;
return if !$Format;
# create objects
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
my $SMIMEObject = $Kernel::OM->Get('Kernel::System::Crypt::SMIME');
# read
my $CertString = $MainObject->FileRead(
Directory => $ConfigObject->Get('Home') . "/scripts/test/sample/SMIME/",
Filename => $CertificateFileName,
);
$CheckString ||= ${$CertString};
my $FormatedCertificate;
# convert
# no passfile
if ( !$CertificatePassFile ) {
$FormatedCertificate = $SMIMEObject->ConvertCertFormat( String => ${$CertString} );
# Remove any not needed information for easy compare.
if ( $FormatedCertificate && $FormatedCertificate !~ m{\A-----BEGIN} ) {
$FormatedCertificate = substr( $FormatedCertificate, index( $FormatedCertificate, '-----BEGIN' ), -1 );
}
$Self->Is(
$FormatedCertificate,
$CheckString,
"#$CertificateFileName ConvertCertFormat() was successful for $Format-format",
);
}
# passfile needed
else {
my $Pass = $MainObject->FileRead(
Directory => $ConfigObject->Get('Home') . "/scripts/test/sample/SMIME/",
Filename => $CertificatePassFile,
);
$FormatedCertificate = $SMIMEObject->ConvertCertFormat(
String => ${$CertString},
Passphrase => ${$Pass}
);
# Remove any not needed information for easy compare.
if ( $FormatedCertificate && $FormatedCertificate !~ m{\A-----BEGIN} ) {
$FormatedCertificate
= substr( $FormatedCertificate, index( $FormatedCertificate, '-----BEGIN' ), -1 ) . "\n";
}
if ($Success) {
$Self->Is(
$FormatedCertificate,
$CheckString,
"#$CertificateFileName ConvertCertFormat() was successful for $Format-format",
);
}
else {
$Self->Is(
$FormatedCertificate,
undef,
"#$CertificateFileName ConvertCertFormat() was UNsuccessful for $Format-format (invalid password?)",
);
}
}
# search before add
my @SearchResult = $CertificateSearch->();
$Self->False(
$SearchResult[0] || '',
"#$CertificateFileName CertificateSearch()-before was empty",
);
# add
if ($FormatedCertificate) {
my %AddResult = $SMIMEObject->CertificateAdd( Certificate => $FormatedCertificate );
$Self->True(
$AddResult{Successful} || '',
"#$CertificateFileName CertificateAdd() - $AddResult{Message}",
);
# search after add
@SearchResult = $CertificateSearch->();
$Self->True(
$SearchResult[0] || '',
"#$CertificateFileName CertificateSearch()-after was NOT empty",
);
}
# remove
for my $Cert (@SearchResult) {
my %Result = $SMIMEObject->CertificateRemove(
Filename => $Cert->{Filename},
);
$Self->Is(
$Result{Successful},
1,
"#$CertificateFileName CertificateRemove() - $Result{Message}",
);
}
return $FormatedCertificate;
};
# check certificates
for my $Certificate (@Certificates) {
#
# PEM check
#
my $PemCertificate = $CertificationConversionTest->(
$Certificate->{Success}, # Success
$Certificate->{CertificateFileName1}, # Filename
'PEM' # format
);
#
# P7B check
#
$CertificationConversionTest->(
$Certificate->{Success}, # Success
$Certificate->{CertificateFileName2}, # filename
'PKCS#7/P7B', # format
$PemCertificate # checkstring
);
#
# DER check
#
$CertificationConversionTest->(
$Certificate->{Success}, # Success
$Certificate->{CertificateFileName3}, # filename
'DER', # format
$PemCertificate # checkstring
);
#
# PFX check
#
$CertificationConversionTest->(
$Certificate->{Success}, # Success
$Certificate->{CertificateFileName4}, # filename
'PFX', # format
$PemCertificate, # checkstring
$Certificate->{CertificatePassFile} # pass-filename
);
}
# delete needed test directories
for my $Directory ( $CertPath, $PrivatePath ) {
my $Success = rmtree( [$Directory] );
$Self->True(
$Success,
"Directory deleted - '$Directory'",
);
}
# cleanup is done by RestoreDatabase.
1;
|