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
|
# --
# 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::ArticleCheck::SMIME;
use strict;
use warnings;
use Kernel::System::EmailParser;
use Kernel::Language qw(Translatable);
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::Crypt::SMIME',
'Kernel::System::Log',
'Kernel::System::Ticket::Article',
'Kernel::Output::HTML::Layout',
);
sub new {
my ( $Type, %Param ) = @_;
my $Self = {};
bless( $Self, $Type );
for my $Needed (qw(UserID ArticleID)) {
if ( $Param{$Needed} ) {
$Self->{$Needed} = $Param{$Needed};
}
else {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $Needed!"
);
}
}
return $Self;
}
sub Check {
my ( $Self, %Param ) = @_;
my %SignCheck;
my @Return;
my $ConfigObject = $Param{ConfigObject} || $Kernel::OM->Get('Kernel::Config');
my $LayoutObject = $Param{LayoutObject} || $Kernel::OM->Get('Kernel::Output::HTML::Layout');
my $UserType = $LayoutObject->{UserType} // '';
my $ChangeUserID = $UserType eq 'Customer' ? $ConfigObject->Get('CustomerPanelUserID') : $Self->{UserID};
# check if smime is enabled
return if !$ConfigObject->Get('SMIME');
# check if article is an email
my $ArticleBackendObject
= $Kernel::OM->Get('Kernel::System::Ticket::Article')->BackendForArticle( %{ $Param{Article} // {} } );
return if $ArticleBackendObject->ChannelNameGet() ne 'Email';
my $SMIMEObject = $Kernel::OM->Get('Kernel::System::Crypt::SMIME');
# check inline smime
if ( $Param{Article}->{Body} && $Param{Article}->{Body} =~ /^-----BEGIN PKCS7-----/ ) {
%SignCheck = $SMIMEObject->Verify( Message => $Param{Article}->{Body} );
if (%SignCheck) {
# remember to result
$Self->{Result} = \%SignCheck;
}
else {
# return with error
push(
@Return,
{
Key => Translatable('Signed'),
Value => Translatable('"S/MIME SIGNED MESSAGE" header found, but invalid!'),
}
);
}
}
# check smime
else {
# get email from fs
my $Message = $ArticleBackendObject->ArticlePlain(
TicketID => $Param{Article}->{TicketID},
ArticleID => $Self->{ArticleID},
UserID => $Self->{UserID},
);
return if !$Message;
my @Email = ();
my @Lines = split( /\n/, $Message );
for my $Line (@Lines) {
push( @Email, $Line . "\n" );
}
my $ParserObject = Kernel::System::EmailParser->new(
Email => \@Email,
);
use MIME::Parser;
my $Parser = MIME::Parser->new();
$Parser->decode_headers(0);
$Parser->extract_nested_messages(0);
$Parser->output_to_core("ALL");
my $Entity = $Parser->parse_data($Message);
my $Head = $Entity->head();
$Head->unfold();
$Head->combine('Content-Type');
my $ContentType = $Head->get('Content-Type');
if (
$ContentType
&& $ContentType =~ /application\/(x-pkcs7|pkcs7)-mime/i
&& $ContentType !~ /signed/i
)
{
# check if article is already decrypted
if ( $Param{Article}->{Body} && $Param{Article}->{Body} ne '- no text message => see attachment -' ) {
push(
@Return,
{
Key => Translatable('Crypted'),
Value => Translatable('Ticket decrypted before'),
Successful => 1,
}
);
}
# check sender (don't decrypt sent emails)
if ( $Param{Article}->{SenderType} && $Param{Article}->{SenderType} =~ /(agent|system)/i ) {
# return info
return (
{
Key => Translatable('Crypted'),
Value => Translatable('Sent message encrypted to recipient!'),
Successful => 1,
}
);
}
# get all email addresses on article
my %EmailsToSearch;
for my $Email (qw(Resent-To Envelope-To To Cc Delivered-To X-Original-To)) {
my @EmailAddressOnField = $ParserObject->SplitAddressLine(
Line => $ParserObject->GetParam( WHAT => $Email ),
);
# filter email addresses avoiding repeated and save on hash to search
for my $EmailAddress (@EmailAddressOnField) {
my $CleanEmailAddress = $ParserObject->GetEmailAddress(
Email => $EmailAddress,
);
$EmailsToSearch{$CleanEmailAddress} = '1';
}
}
# look for private keys for every email address
# extract every resulting cert and put it into an hash of hashes avoiding repeated
my %PrivateKeys;
for my $EmailAddress ( sort keys %EmailsToSearch ) {
my @PrivateKeysResult = $SMIMEObject->PrivateSearch(
Search => $EmailAddress,
);
for my $Cert (@PrivateKeysResult) {
$PrivateKeys{ $Cert->{Filename} } = $Cert;
}
}
# search private cert to decrypt email
if ( !%PrivateKeys ) {
push(
@Return,
{
Key => Translatable('Crypted'),
Value => Translatable('Impossible to decrypt: private key for email was not found!'),
}
);
return @Return;
}
my %Decrypt;
PRIVATESEARCH:
for my $CertResult ( values %PrivateKeys ) {
# decrypt
%Decrypt = $SMIMEObject->Decrypt(
Message => $Message,
SearchingNeededKey => 1,
%{$CertResult},
);
last PRIVATESEARCH if ( $Decrypt{Successful} );
}
# ok, decryption went fine
if ( $Decrypt{Successful} ) {
push(
@Return,
{
Key => Translatable('Crypted'),
Value => $Decrypt{Message} || Translatable('Successful decryption'),
%Decrypt,
}
);
# store decrypted data
my $EmailContent = $Decrypt{Data};
# now check if the data contains a signature too
%SignCheck = $SMIMEObject->Verify(
Message => $Decrypt{Data},
);
if ( $SignCheck{SignatureFound} ) {
# If the signature was verified well, use the stripped content to store the email.
# Now it contains only the email without other SMIME generated data.
$EmailContent = $SignCheck{Content} if $SignCheck{Successful};
push(
@Return,
{
Key => Translatable('Signed'),
Value => $SignCheck{Message},
%SignCheck,
}
);
}
# parse the decrypted email body
my $ParserObject = Kernel::System::EmailParser->new(
Email => $EmailContent
);
my $Body = $ParserObject->GetMessageBody();
# from RFC 3850
# 3. Using Distinguished Names for Internet Mail
#
# End-entity certificates MAY contain ...
#
# ...
#
# Sending agents SHOULD make the address in the From or Sender header
# in a mail message match an Internet mail address in the signer's
# certificate. Receiving agents MUST check that the address in the
# From or Sender header of a mail message matches an Internet mail
# address, if present, in the signer's certificate, if mail addresses
# are present in the certificate. A receiving agent SHOULD provide
# some explicit alternate processing of the message if this comparison
# fails, which may be to display a message that shows the recipient the
# addresses in the certificate or other certificate details.
# as described in bug#5098 and RFC 3850 an alternate mail handling should be
# made if sender and signer addresses does not match
# get original sender from email
my @OrigEmail = map {"$_\n"} split( /\n/, $Message );
my $ParserObjectOrig = Kernel::System::EmailParser->new(
Email => \@OrigEmail,
);
my $OrigFrom = $ParserObjectOrig->GetParam( WHAT => 'From' );
my $OrigSender = $ParserObjectOrig->GetEmailAddress( Email => $OrigFrom );
# compare sender email to signer email
my $SignerSenderMatch = 0;
SIGNER:
for my $Signer ( @{ $SignCheck{Signers} } ) {
if ( $OrigSender =~ m{\A \Q$Signer\E \z}xmsi ) {
$SignerSenderMatch = 1;
last SIGNER;
}
}
# sender email does not match signing certificate!
if ( !$SignerSenderMatch ) {
$SignCheck{Successful} = 0;
$SignCheck{Message} =~ s/successful/failed!/;
$SignCheck{Message} .= " (signed by "
. join( ' | ', @{ $SignCheck{Signers} } )
. ")"
. ", but sender address $OrigSender: does not match certificate address!";
}
# Determine if we have decrypted article and attachments before.
my %Index = $ArticleBackendObject->ArticleAttachmentIndex(
ArticleID => $Self->{ArticleID},
);
if (
!grep { $Index{$_}->{ContentType} =~ m{ application/ (?: x- )? pkcs7-mime }xms } sort keys %Index
)
{
return @Return;
}
# updated article body
$ArticleBackendObject->ArticleUpdate(
TicketID => $Param{Article}->{TicketID},
ArticleID => $Self->{ArticleID},
Key => 'Body',
Value => $Body,
UserID => $ChangeUserID,
);
# delete crypted attachments
$ArticleBackendObject->ArticleDeleteAttachment(
ArticleID => $Self->{ArticleID},
UserID => $ChangeUserID,
);
# write attachments to the storage
for my $Attachment ( $ParserObject->GetAttachments() ) {
$ArticleBackendObject->ArticleWriteAttachment(
%{$Attachment},
ArticleID => $Self->{ArticleID},
UserID => $ChangeUserID,
);
}
return @Return;
}
else {
push(
@Return,
{
Key => Translatable('Crypted'),
Value => "$Decrypt{Message}",
%Decrypt,
}
);
}
}
if (
$ContentType
&& $ContentType =~ /application\/(x-pkcs7|pkcs7)/i
&& $ContentType =~ /signed/i
)
{
# check sign and get clear content
%SignCheck = $SMIMEObject->Verify(
Message => $Message,
);
# parse and update clear content
if ( %SignCheck && $SignCheck{Successful} && $SignCheck{Content} ) {
my @Email = ();
my @Lines = split( /\n/, $SignCheck{Content} );
for (@Lines) {
push( @Email, $_ . "\n" );
}
my $ParserObject = Kernel::System::EmailParser->new(
Email => \@Email,
);
my $Body = $ParserObject->GetMessageBody();
# from RFC 3850
# 3. Using Distinguished Names for Internet Mail
#
# End-entity certificates MAY contain ...
#
# ...
#
# Sending agents SHOULD make the address in the From or Sender header
# in a mail message match an Internet mail address in the signer's
# certificate. Receiving agents MUST check that the address in the
# From or Sender header of a mail message matches an Internet mail
# address, if present, in the signer's certificate, if mail addresses
# are present in the certificate. A receiving agent SHOULD provide
# some explicit alternate processing of the message if this comparison
# fails, which may be to display a message that shows the recipient the
# addresses in the certificate or other certificate details.
# as described in bug#5098 and RFC 3850 an alternate mail handling should be
# made if sender and signer addresses does not match
# get original sender from email
my @OrigEmail = map {"$_\n"} split( /\n/, $Message );
my $ParserObjectOrig = Kernel::System::EmailParser->new(
Email => \@OrigEmail,
);
my $OrigFrom = $ParserObjectOrig->GetParam( WHAT => 'From' );
my $OrigSender = $ParserObjectOrig->GetEmailAddress( Email => $OrigFrom );
# compare sender email to signer email
my $SignerSenderMatch = 0;
SIGNER:
for my $Signer ( @{ $SignCheck{Signers} } ) {
if ( $OrigSender =~ m{\A \Q$Signer\E \z}xmsi ) {
$SignerSenderMatch = 1;
last SIGNER;
}
}
# sender email does not match signing certificate!
if ( !$SignerSenderMatch ) {
$SignCheck{Successful} = 0;
$SignCheck{Message} =~ s/successful/failed!/;
$SignCheck{Message} .= " (signed by "
. join( ' | ', @{ $SignCheck{Signers} } )
. ")"
. ", but sender address $OrigSender: does not match certificate address!";
}
# Determine if we have decrypted article and attachments before.
my %Index = $ArticleBackendObject->ArticleAttachmentIndex(
ArticleID => $Self->{ArticleID},
);
if (
grep { $Index{$_}->{ContentType} =~ m{ application/ (?: x- )? pkcs7 }xms } sort keys %Index
)
{
# Update article body.
$ArticleBackendObject->ArticleUpdate(
TicketID => $Param{Article}->{TicketID},
ArticleID => $Self->{ArticleID},
Key => 'Body',
Value => $Body,
UserID => $ChangeUserID,
);
# Delete crypted attachments.
$ArticleBackendObject->ArticleDeleteAttachment(
ArticleID => $Self->{ArticleID},
UserID => $ChangeUserID,
);
# Write decrypted attachments to the storage.
for my $Attachment ( $ParserObject->GetAttachments() ) {
$ArticleBackendObject->ArticleWriteAttachment(
%{$Attachment},
ArticleID => $Self->{ArticleID},
UserID => $ChangeUserID,
);
}
}
}
# output signature verification errors
elsif (
%SignCheck
&& !$SignCheck{SignatureFound}
&& !$SignCheck{Successful}
&& !$SignCheck{Content}
)
{
# return result
push(
@Return,
{
Key => Translatable('Signed'),
Value => $SignCheck{Message},
%SignCheck,
}
);
}
}
}
if ( $SignCheck{SignatureFound} ) {
# return result
push(
@Return,
{
Key => Translatable('Signed'),
Value => $SignCheck{Message},
%SignCheck,
}
);
}
return @Return;
}
sub Filter {
my ( $Self, %Param ) = @_;
# remove signature if one is found
if ( $Self->{Result}->{SignatureFound} ) {
# remove SMIME begin signed message
$Param{Article}->{Body} =~ s/^-----BEGIN\sPKCS7-----.+?Hash:\s.+?$//sm;
# remove SMIME inline sign
$Param{Article}->{Body} =~ s/^-----END\sPKCS7-----//sm;
}
return 1;
}
1;
|