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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# 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.
# --
package Kernel::Output::HTML::ArticleCompose::Crypt;
use parent 'Kernel::Output::HTML::Base';
use strict;
use warnings;
use Mail::Address;
use Kernel::Language qw(Translatable);
use Kernel::System::VariableCheck qw(:all);
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::Crypt::PGP',
'Kernel::System::Crypt::SMIME',
'Kernel::Output::HTML::Layout',
);
sub Option {
my ( $Self, %Param ) = @_;
# Get config object.
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
# Check if PGP and SMIME are disabled.
return if !$ConfigObject->Get('PGP') && !$ConfigObject->Get('SMIME');
return ('CryptKeyID');
}
sub Run {
my ( $Self, %Param ) = @_;
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
# Check if PGP and SMIME are disabled.
return if !$ConfigObject->Get('PGP') && !$ConfigObject->Get('SMIME');
my %KeyList = $Self->Data(%Param);
# Recipients with unique public keys won't be displayed in the selection
my $UniqueEncryptKeyIDsToRemove = $Self->_GetUniqueEncryptKeyIDsToRemove(%Param);
my @ExpiredIdentifiers;
my @RevokedIdentifiers;
my $InvalidMessage = '';
my $Class = '';
if ( IsArrayRefWithData($UniqueEncryptKeyIDsToRemove) ) {
UNIQUEKEY:
for my $UniqueEncryptKeyIDToRemove ( @{$UniqueEncryptKeyIDsToRemove} ) {
next UNIQUEKEY if !defined $KeyList{$UniqueEncryptKeyIDToRemove};
if ( $KeyList{$UniqueEncryptKeyIDToRemove} =~ m/WARNING: EXPIRED KEY/ ) {
my ( $Type, $Key, $Identifier ) = split /::/, $UniqueEncryptKeyIDToRemove;
push @ExpiredIdentifiers, $Identifier;
}
if ( $KeyList{$UniqueEncryptKeyIDToRemove} =~ m/WARNING: REVOKED KEY/ ) {
my ( $Type, $Key, $Identifier ) = split /::/, $UniqueEncryptKeyIDToRemove;
push @RevokedIdentifiers, $Identifier;
}
delete $KeyList{$UniqueEncryptKeyIDToRemove};
}
}
# Find recipient list.
my $Recipient = '';
for (qw(To Cc Bcc)) {
if ( $Param{$_} ) {
$Recipient .= ', ' . $Param{$_};
}
}
my @SearchAddress = ();
if ($Recipient) {
@SearchAddress = Mail::Address->parse($Recipient);
}
if (
!IsArrayRefWithData( $Param{CryptKeyID} )
|| ( $Param{ExpandCustomerName} && $Param{ExpandCustomerName} == 3 )
)
{
$Param{CryptKeyID} = $Self->_PickEncryptKeyIDs(%Param);
}
# Check if all recipients have at least a key selected
if (
$Param{StoreNew}
&& $Param{EmailSecurityOptions}
&& $Param{EmailSecurityOptions} =~ m{Encrypt}msxi
)
{
my $CheckSuccess = $Self->_CheckRecipient(%Param);
if ( !$CheckSuccess ) {
if ( IsArrayRefWithData( $Self->{MissingKeys} ) ) {
$InvalidMessage .= $LayoutObject->{LanguageObject}->Translate(
"There are no encryption keys available for the addresses: '%s'. ",
join ', ', @{ $Self->{MissingKeys} }
);
}
if ( IsArrayRefWithData( $Self->{MissingSelectedKey} ) ) {
$InvalidMessage .= $LayoutObject->{LanguageObject}->Translate(
"There are no selected encryption keys for the addresses: '%s'. ",
join ', ', @{ $Self->{MissingSelectedKey} }
);
}
$Self->{Error}->{EncryptMissingKey} = 1;
$Class .= ' ServerError';
}
}
# Check if selected encryption keys are expired.
if (
IsArrayRefWithData( $Param{CryptKeyID} )
&& !IsArrayRefWithData( \@ExpiredIdentifiers )
&& !IsArrayRefWithData( \@RevokedIdentifiers )
)
{
ENCRYPTKEYID:
for my $EncryptKey ( @{ $Param{CryptKeyID} } ) {
my ( $Type, $Key, $Identifier ) = split /::/, $EncryptKey;
next ENCRYPTKEYID if !defined $KeyList{$EncryptKey};
if ( $KeyList{$EncryptKey} =~ m/WARNING: EXPIRED KEY/ ) {
push @ExpiredIdentifiers, $Identifier;
}
if ( $KeyList{$EncryptKey} =~ m/WARNING: REVOKED KEY/ ) {
push @RevokedIdentifiers, $Identifier;
}
}
}
if ( IsArrayRefWithData( \@ExpiredIdentifiers ) ) {
$InvalidMessage .= $LayoutObject->{LanguageObject}->Translate(
"Cannot use expired encryption keys for the addresses: '%s'. ",
join ', ', @ExpiredIdentifiers
);
$Self->{Error}->{ExpiredKey} = 1;
$Class .= ' ServerError';
}
if ( IsArrayRefWithData( \@RevokedIdentifiers ) ) {
$InvalidMessage .= $LayoutObject->{LanguageObject}->Translate(
"Cannot use revoked encryption keys for the addresses: '%s'. ",
join ', ', @RevokedIdentifiers
);
$Self->{Error}->{ExpiredKey} = 1;
$Class .= ' ServerError';
}
# Add encrypt options.
my $List = $LayoutObject->BuildSelection(
Data => \%KeyList,
Name => 'CryptKeyID',
SelectedID => $Param{CryptKeyID} || '',
Class => "$Class Modernize",
Max => 150,
Multiple => 1,
PossibleNone => 1.
);
$LayoutObject->Block(
Name => 'Option',
Data => {
Name => 'CryptKeyID',
Key => Translatable('Encrypt'),
Value => $List,
Invalid => $InvalidMessage,
FieldExplanation => Translatable(
'Keys/certificates will only be shown for recipients with more than one key/certificate. The first found key/certificate will be pre-selected. Please make sure to select the correct one.'
),
},
);
return;
}
sub Data {
my ( $Self, %Param ) = @_;
# Get config object.
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
# Check if PGP or SMIME is disabled
return if !$ConfigObject->Get('PGP') && !$ConfigObject->Get('SMIME');
# Find recipient list.
my $Recipient = '';
for (qw(To Cc Bcc)) {
if ( $Param{$_} ) {
$Recipient .= ', ' . $Param{$_};
}
}
my @SearchAddress = ();
if ($Recipient) {
@SearchAddress = Mail::Address->parse($Recipient);
}
# Generate key list.
my %KeyList;
return %KeyList if !@SearchAddress;
return %KeyList if !$Param{EmailSecurityOptions};
# Get email security options.
my ( $Backend, $Sign, $Encrypt ) = split /::/, $Param{EmailSecurityOptions};
return %KeyList if !$Backend;
return %KeyList if !$Encrypt;
return %KeyList if $Encrypt ne 'Encrypt';
# Check PGP backend.
if ( $Backend eq 'PGP' ) {
my $PGPObject = $Kernel::OM->Get('Kernel::System::Crypt::PGP');
return %KeyList if !$PGPObject;
# Get PGP method (Detached or In-line).
my $PGPMethod = $ConfigObject->Get('PGP::Method') || 'Detached';
# For PGP In-line Rich-text editor needs to be disabled.
if (
$PGPMethod eq 'Detached'
|| ( $PGPMethod eq 'Inline' && !$Kernel::OM->Get('Kernel::Output::HTML::Layout')->{BrowserRichText} )
)
{
for my $SearchAddress (@SearchAddress) {
my @PublicKeys = $PGPObject->PublicKeySearch(
Search => $SearchAddress->address(),
);
for my $DataRef (@PublicKeys) {
my $Expires = '';
if ( $DataRef->{Expires} ) {
$Expires = "[$DataRef->{Expires}]";
}
my $Status = '[' . $DataRef->{Status} . ']';
if ( $DataRef->{Status} eq 'expired' ) {
$Status = '[WARNING: EXPIRED KEY]';
}
elsif ( $DataRef->{Status} eq 'revoked' ) {
$Status = '[WARNING: REVOKED KEY]';
}
$KeyList{"PGP::$DataRef->{Key}::$DataRef->{Identifier}"}
= "PGP: $Status $DataRef->{Key} $Expires $DataRef->{Identifier}";
}
}
}
}
# Check SMIME backend.
elsif ( $Backend eq 'SMIME' ) {
my $SMIMEObject = $Kernel::OM->Get('Kernel::System::Crypt::SMIME');
return %KeyList if !$SMIMEObject;
for my $SearchAddress (@SearchAddress) {
my @PublicKeys = $SMIMEObject->CertificateSearch(
Search => $SearchAddress->address(),
);
for my $DataRef (@PublicKeys) {
my $Expired = '';
my $EndDate = ( defined $DataRef->{EndDate} ) ? "[$DataRef->{EndDate}]" : '';
if ( defined $DataRef->{EndDate} && $SMIMEObject->KeyExpiredCheck( EndDate => $DataRef->{EndDate} ) ) {
$Expired = ' [WARNING: EXPIRED KEY]';
}
$KeyList{"SMIME::$DataRef->{Filename}::$DataRef->{Email}"}
= "SMIME:$Expired $DataRef->{Filename} $EndDate $DataRef->{Email}";
}
}
}
return %KeyList;
}
sub ArticleOption {
my ( $Self, %Param ) = @_;
if ( IsArrayRefWithData( $Param{CryptKeyID} ) ) {
my @Keys;
for my $EncryptKey ( @{ $Param{CryptKeyID} } ) {
my ( $Type, $Key ) = split /::/, $EncryptKey;
push @Keys, $Key;
}
$Param{EmailSecurity}->{EncryptKeys} = \@Keys;
return (
EmailSecurity => $Param{EmailSecurity},
);
}
return;
}
sub GetParamAJAX {
my ( $Self, %Param ) = @_;
# Get config object.
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
# Check if PGP and SMIME are disabled
return if !$ConfigObject->Get('PGP') && !$ConfigObject->Get('SMIME');
return (
CryptKeyID => $Self->_PickEncryptKeyIDs(%Param),
);
}
sub Error {
my ( $Self, %Param ) = @_;
if ( $Self->{Error} ) {
return %{ $Self->{Error} };
}
return;
}
sub _CheckRecipient {
my ( $Self, %Param ) = @_;
# Get email security options.
my ( $Backend, $Sign, $Encrypt ) = split /::/, $Param{EmailSecurityOptions};
# Stop checking if no backed was selected.
return 1 if !$Backend;
return 1 if $Encrypt ne 'Encrypt';
# Get encrypt object.
my $EncryptObject = $Kernel::OM->Get("Kernel::System::Crypt::$Backend");
# Return error if encrypt object could not be created.
return 0 if !$EncryptObject;
# Create a selected encryption keys lookup table
my %SelectedEncryptKeyIDs = map { $_ => 1 } @{ $Param{CryptKeyID} };
# Recipients with unique public keys aren't part of the selection so add them manually
my $UniqueEncryptKeyIDsToRemove = $Self->_GetUniqueEncryptKeyIDsToRemove(%Param);
if ( IsArrayRefWithData($UniqueEncryptKeyIDsToRemove) ) {
for my $UniqueEncryptKeyIDToRemove ( @{$UniqueEncryptKeyIDsToRemove} ) {
$SelectedEncryptKeyIDs{$UniqueEncryptKeyIDToRemove} = 1;
}
}
my $MissingSelectedKeyFlag;
my $MissingKeysFlag;
# Check each recipient type.
RECIPIENTTYPE:
for my $RecipientType (qw(To Cc Bcc)) {
# Get all addresses for each recipient type.
my @SearchAddress;
if ( $Param{$RecipientType} ) {
@SearchAddress = Mail::Address->parse( $Param{$RecipientType} );
}
# Get all certificates/public keys for each address.
ADDRESS:
for my $Address (@SearchAddress) {
my $EmailAddress = $Address->address();
my @PublicKeys;
if ( $Backend eq 'PGP' ) {
@PublicKeys = $EncryptObject->PublicKeySearch(
Search => $EmailAddress,
);
}
else {
@PublicKeys = $EncryptObject->CertificateSearch(
Search => $EmailAddress,
);
}
# Remember addresses with no encryption keys available
if ( !@PublicKeys ) {
push @{ $Self->{MissingKeys} }, $EmailAddress;
$MissingKeysFlag = 1;
next ADDRESS;
}
$MissingSelectedKeyFlag = 1;
PUBLICKEY:
for my $PublicKey (@PublicKeys) {
my $EncryptKeyID;
if ( $Backend eq 'PGP' ) {
$EncryptKeyID = "PGP::$PublicKey->{Key}::$PublicKey->{Identifier}";
}
else {
$EncryptKeyID = "SMIME::$PublicKey->{Filename}::$PublicKey->{Email}";
}
# If this key is selected everything is fine, remove missing key flag and check next
# address.
if ( $SelectedEncryptKeyIDs{$EncryptKeyID} ) {
$MissingSelectedKeyFlag = 0;
next ADDRESS;
}
}
push @{ $Self->{MissingSelectedKey} }, $EmailAddress;
}
}
# Return error if there was no encryption keys available for an email address.
return if $MissingKeysFlag;
# Return error if there was no selected key for an email address.
return if $MissingSelectedKeyFlag;
# Otherwise return success
return 1;
}
sub _PickEncryptKeyIDs {
my ( $Self, %Param ) = @_;
# Get the list of keys for the current backend.
my %KeyList = $Self->Data(%Param);
# Return nothing if there are no possible encrypt keys;
return [] if !%KeyList;
my %SelectedEncryptKeyIDs;
# Check if encryption keys are still valid for the selected backend, otherwise remove them.
ENCRYPTKEYID:
for my $EncryptKeyID ( @{ $Param{CryptKeyID} } ) {
next ENCRYPTKEYID if !$EncryptKeyID;
next ENCRYPTKEYID if !$KeyList{$EncryptKeyID};
next ENCRYPTKEYID if $KeyList{$EncryptKeyID} =~ m/WARNING: EXPIRED KEY/;
next ENCRYPTKEYID if $KeyList{$EncryptKeyID} =~ m/WARNING: REVOKED KEY/;
$SelectedEncryptKeyIDs{$EncryptKeyID} = 1;
}
# Return nothing if there are no security options.
return [] if !defined $Param{EmailSecurityOptions};
# Get email security options.
my ( $Backend, $Sign, $Encrypt ) = split /::/, $Param{EmailSecurityOptions};
# Return nothing if Backend is not present.
return [] if !$Backend;
# Get encrypt object.
my $EncryptObject = $Kernel::OM->Get("Kernel::System::Crypt::$Backend");
# Return nothing if encrypt object was not created
return [] if !$EncryptObject;
# Check each recipient type.
for my $RecipientType (qw(To Cc Bcc)) {
# Get all addresses for each recipient type.
my @SearchAddress;
if ( $Param{$RecipientType} ) {
@SearchAddress = Mail::Address->parse( $Param{$RecipientType} );
}
ADDRESS:
for my $Address (@SearchAddress) {
my @PublicKeys;
if ( $Backend eq 'PGP' ) {
@PublicKeys = $EncryptObject->PublicKeySearch(
Search => $Address->address(),
);
@PublicKeys = sort { $a->{Expires} cmp $b->{Expires} } grep { $_->{Status} eq 'good' } @PublicKeys;
}
else {
@PublicKeys = $EncryptObject->CertificateSearch(
Search => $Address->address(),
Valid => 1,
);
@PublicKeys = sort { $a->{ShortEndDate} cmp $b->{ShortEndDate} } @PublicKeys;
}
# If there are no public keys for this recipient, do nothing and check the next one.
next ADDRESS if !@PublicKeys;
# Check / store key.
my $EncryptKeyID;
if ( $Backend eq 'PGP' ) {
$EncryptKeyID = 'PGP::' . $PublicKeys[-1]->{Key} . '::' . $PublicKeys[-1]->{Identifier};
}
else {
$EncryptKeyID = 'SMIME::' . $PublicKeys[-1]->{Filename} . '::' . $PublicKeys[-1]->{Email};
}
# Do nothing if key is already selected, check next address.
if ( !defined $SelectedEncryptKeyIDs{$EncryptKeyID} ) {
# Select the last key and check the next address.
$SelectedEncryptKeyIDs{$EncryptKeyID} = 1;
}
}
}
my @EncryptKeyIDs = sort keys %SelectedEncryptKeyIDs;
return \@EncryptKeyIDs;
}
sub GetOptionsToRemoveAJAX {
my ( $Self, %Param ) = @_;
# Get config object.
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
# Check if PGP and SMIME are disabled
return if !$ConfigObject->Get('PGP') && !$ConfigObject->Get('SMIME');
my $OptionsToRemove = $Self->_GetUniqueEncryptKeyIDsToRemove(%Param);
return if !IsArrayRefWithData($OptionsToRemove);
return @{$OptionsToRemove};
}
sub _GetUniqueEncryptKeyIDsToRemove {
my ( $Self, %Param ) = @_;
# Get the list of keys for the current backend.
my %KeyList = $Self->Data(%Param);
# Return nothing if there are no possible encrypt keys;
return if !%KeyList;
# Return nothing if there are no security options.
return if !defined $Param{EmailSecurityOptions};
# Get email security options.
my ( $Backend, $Sign, $Encrypt ) = split /::/, $Param{EmailSecurityOptions};
return if !$Backend;
return if !$Encrypt || $Encrypt ne 'Encrypt';
# Get encrypt object.
my $EncryptObject = $Kernel::OM->Get("Kernel::System::Crypt::$Backend");
# Return nothing if encrypt object was not created
return if !$EncryptObject;
my %UniqueEncryptKeyIDsToRemove;
# Check each recipient type.
for my $RecipientType (qw(To Cc Bcc)) {
# Get all addresses for each recipient type.
my @SearchAddress;
if ( $Param{$RecipientType} ) {
@SearchAddress = Mail::Address->parse( $Param{$RecipientType} );
}
ADDRESS:
for my $Address (@SearchAddress) {
my @PublicKeys;
if ( $Backend eq 'PGP' ) {
@PublicKeys = $EncryptObject->PublicKeySearch(
Search => $Address->address(),
);
}
else {
@PublicKeys = $EncryptObject->CertificateSearch(
Search => $Address->address(),
);
}
# Only unique public keys will be removed, so skip to next address if there
# is not exactly one public key
next ADDRESS if @PublicKeys != 1;
my $PublicKey = shift @PublicKeys;
my $EncryptKeyID;
if ( $Backend eq 'PGP' ) {
$EncryptKeyID = "PGP::$PublicKey->{Key}::$PublicKey->{Identifier}";
}
else {
$EncryptKeyID = "SMIME::$PublicKey->{Filename}::$PublicKey->{Email}";
}
$UniqueEncryptKeyIDsToRemove{$EncryptKeyID} = 1;
}
}
my @UniqueEncryptKeyIDsToRemove = sort keys %UniqueEncryptKeyIDsToRemove;
return \@UniqueEncryptKeyIDsToRemove;
}
1;
|