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
|
# --
# Kernel/System/Crypt/PGP.pm - the main crypt module
# Copyright (C) 2001-2005 Martin Edenhofer <martin+code@otrs.org>
# --
# $Id: PGP.pm,v 1.10 2005/10/17 20:41:55 martin Exp $
# --
# 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 http://www.gnu.org/licenses/gpl.txt.
# --
package Kernel::System::Crypt::PGP;
use strict;
use vars qw($VERSION);
$VERSION = '$Revision: 1.10 $';
$VERSION =~ s/^.*:\s(\d+\.\d+)\s.*$/$1/;
=head1 NAME
Kernel::System::Crypt::PGP - pgp crypt backend lib
=head1 SYNOPSIS
This is a sub module of Kernel::System::Crypt and contains all pgp functions.
=head1 PUBLIC INTERFACE
=over 4
=cut
# just for internal
sub Init {
my $Self = shift;
my %Param = @_;
$Self->{GPGBin} = $Self->{ConfigObject}->Get('PGP::Bin') || '/usr/bin/gpg';
$Self->{Options} = $Self->{ConfigObject}->Get('PGP::Options') || '--batch --no-tty --yes';
$Self->{GPGBin} = "$Self->{GPGBin} $Self->{Options} ";
return $Self;
}
=item Check()
check if environment is working
my $Message = $CryptObject->Check();
=cut
sub Check {
my $Self = shift;
my %Param = @_;
my $GPGBin = $Self->{ConfigObject}->Get('PGP::Bin') || '/usr/bin/gpg';
if (! -e $GPGBin) {
return "No such $GPGBin!";
}
elsif (! -x $GPGBin) {
return "$GPGBin not executable!";
}
}
=item Crypt()
crypt a message
my $Message = $CryptObject->Crypt(
Message => $Message,
Key => $PGPPublicKeyID,
);
=cut
sub Crypt {
my $Self = shift;
my %Param = @_;
my $LogMessage = '';
my $UsedKey = '';
# check needed stuff
foreach (qw(Message Key)) {
if (!$Param{$_}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need $_!");
return;
}
}
my ($FH, $Filename) = $Self->{FileTempObject}->TempFile();
my ($FHCrypt, $FilenameCrypt) = $Self->{FileTempObject}->TempFile();
print $FH $Param{Message};
open (CRYPT, "$Self->{GPGBin} --always-trust --yes -e -a -o $FilenameCrypt -r $Param{Key} $Filename |");
while (<CRYPT>) {
$LogMessage .= $_;
}
close (CRYPT);
# error
if ($LogMessage) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Can't crypt with Key $Param{Key}: $LogMessage!");
return;
}
# get crypted content
my $Crypt;
open (TMP, "< $FilenameCrypt") || die "$!";
while (<TMP>) {
$Crypt .= $_;
}
close (TMP);
return $Crypt;
}
=item Decrypt()
decrypt a message and returns a hash (Successful, Message, Data)
my %Message = $CryptObject->Decrypt(
Message => $CryptedMessage,
);
=cut
sub Decrypt {
my $Self = shift;
my %Param = @_;
my $LogMessage = '';
my $Decrypt = '';
# check needed stuff
foreach (qw(Message)) {
if (!defined($Param{$_})) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need $_!");
return;
}
}
my ($FH, $Filename) = $Self->{FileTempObject}->TempFile();
my ($FHDecrypt, $FilenameDecrypt) = $Self->{FileTempObject}->TempFile();
print $FH $Param{Message};
if (!$Param{Password}) {
my $Pw;
my @Keys = $Self->_CryptedWithKey(File => $Filename);
my %Password = %{$Self->{ConfigObject}->Get('PGP::Key::Password')};
foreach (@Keys) {
if (defined ($Password{$_})) {
$Pw = $Password{$_};
}
}
if (defined($Pw)) {
$Param{Password} = $Pw;
}
else {
$Param{Password} = '';
}
}
open (SIGN, "echo ".quotemeta($Param{Password})." | $Self->{GPGBin} --passphrase-fd 0 --always-trust --yes -d -o $FilenameDecrypt $Filename 2>&1 | ");
while (<SIGN>) {
$LogMessage .= $_;
}
close (SIGN);
if ($LogMessage =~ /failed/i) {
$Self->{LogObject}->Log(Priority => 'error', Message => "$LogMessage!");
return (
Successful => 0,
Message => $LogMessage,
);
}
else {
open (TMP, "< $FilenameDecrypt");
while (<TMP>) {
$Decrypt .= $_;
}
close (TMP);
return (
Successful => 1,
Message => "$LogMessage",
Data => $Decrypt,
);
}
}
=item Sign()
sign a message
my $Sign = $CryptObject->Sign(
Message => $Message,
Key => $PGPPrivateKeyID,
Type => 'Detached' # Detached|Inline
);
=cut
sub Sign {
my $Self = shift;
my %Param = @_;
my $LogMessage = '';
my $UsedKey = '';
my $AddParams = '';
# check needed stuff
foreach (qw(Message Key)) {
if (!$Param{$_}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need $_!");
return;
}
}
my $Pw = '';
my %Password = %{$Self->{ConfigObject}->Get('PGP::Key::Password')};
if (defined ($Password{$Param{Key}})) {
$Pw = $Password{$Param{Key}};
}
if ($Param{Type} && $Param{Type} eq 'Detached') {
$AddParams = '-sba';
}
else {
$AddParams = '--clearsign';
}
# create tmp files
my ($FH, $Filename) = $Self->{FileTempObject}->TempFile();
my ($FHSign, $FilenameSign) = $Self->{FileTempObject}->TempFile();
print $FH $Param{Message};
open (SIGN, "echo ".quotemeta($Pw)." | $Self->{GPGBin} --passphrase-fd 0 --default-key $Param{Key} -o $FilenameSign $AddParams $Filename 2>&1 |");
while (<SIGN>) {
$LogMessage .= $_;
}
close (SIGN);
# error
if ($LogMessage) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Can't sign with Key $Param{Key}: $LogMessage!");
return;
}
# get signed content
my $Signed;
open (TMP, "< $FilenameSign");
while (<TMP>) {
$Signed .= $_;
}
close (TMP);
return $Signed;
}
=item Verify()
verify a message signature and returns a hash (Successful, Message, Data)
Inline sign:
my %Data = $CryptObject->Verify(
Message => $Message,
);
Attached sign:
my %Data = $CryptObject->Verify(
Message => $Message,
Sign => $Sign,
);
=cut
sub Verify {
my $Self = shift;
my %Param = @_;
my %Return = ();
my $Message = '';
my $UsedKey = '';
# check needed stuff
if (!$Param{Message}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need Message!");
return;
}
my ($FH, $Filename) = $Self->{FileTempObject}->TempFile();
my ($FHSign, $FilenameSign) = $Self->{FileTempObject}->TempFile();
print $FH $Param{Message};
my $File = $Filename;
if ($Param{Sign}) {
print $FHSign $Param{Sign};
$File = "$FilenameSign $File";
}
open (VERIFY, "$Self->{GPGBin} --verify $File 2>&1 |");
while (<VERIFY>) {
$Message .= $_;
}
close (VERIFY);
if ($Message =~ /(Good signature from ".+?")/i) {
%Return = (
SignatureFound => 1,
Successful => 1,
Message => "gpg: $1",
MessageLong => $Message,
);
}
else {
%Return = (
SignatureFound => 1,
Successful => 0,
Message => $Message,
);
}
return %Return;
}
=item KeySearch()
returns a array with serach result (private and public keys)
my @Keys = $CryptObject->KeySearch(
Search => 'something to search'
);
=cut
sub KeySearch {
my $Self = shift;
my %Param = @_;
my @Result = ();
push (@Result, $Self->PublicKeySearch(%Param));
push (@Result, $Self->PrivateKeySearch(%Param));
return @Result;
}
=item PrivateKeySearch()
returns a array with serach result (private keys)
my @Keys = $CryptObject->PrivateKeySearch(
Search => 'something to search'
);
=cut
sub PrivateKeySearch {
my $Self = shift;
my %Param = @_;
my $Search = $Param{Search} || '';
my @Result = ();
my $InKey = 0;
open (SEARCH, "$Self->{GPGBin} --list-secret-keys --with-fingerprint ".quotemeta($Search)." 2>&1 |");
my %Key = ();
while (my $Line = <SEARCH>) {
if ($Line =~ /^(se.+?)\s(.+?)\/(.+?)\s(.+?)\s(.*)$/) {
if (%Key) {
push (@Result, {%Key});
%Key = ();
}
$InKey = 1;
$Key{Type} .= $1;
$Key{Bit} .= $2;
$Key{Key} .= $3;
$Key{Created} .= $4;
$Key{Identifier} .= $5;
}
if ($InKey && $Line =~ /^uid\s+(.*)/) {
$Key{Identifier} .= $1;
}
if ($InKey && $Line =~ /^(ssb)\s(.+?)\/(.+?)\s(.+?)\s/) {
$Key{Bit} = $2;
$Key{Key} = $3;
$Key{Created} = $4;
}
if ($InKey && $Line =~ /\[expires:\s(.+?)\]/) {
$Key{Expires} = $1;
}
if ($InKey && $Line =~ /Key fingerprint = (.*)/) {
$Key{Fingerprint} = $1;
$Key{FingerprintShort} = $1;
$Key{FingerprintShort} =~ s/ / /g;
$Key{FingerprintShort} =~ s/ //g;
}
}
push (@Result, {%Key}) if (%Key);
close (SEARCH);
return @Result;
}
=item PublicKeySearch()
returns a array with serach result (public keys)
my @Keys = $CryptObject->PublicKeySearch(
Search => 'something to search'
);
=cut
sub PublicKeySearch {
my $Self = shift;
my %Param = @_;
my $Search = $Param{Search} || '';
my @Result = ();
my $InKey = 0;
open (SEARCH, "$Self->{GPGBin} --list-public-keys --with-fingerprint ".quotemeta($Search)." 2>&1 |");
my %Key = ();
while (my $Line = <SEARCH>) {
if ($Line =~ /^(p.+?)\s(.+?)\/(.+?)\s(.+?)\s(.*)$/) {
if (%Key) {
push (@Result, {%Key});
%Key = ();
}
$InKey = 1;
$Key{Type} .= $1;
$Key{Bit} .= $2;
$Key{Key} .= $3;
$Key{Created} .= $4;
$Key{Identifier} .= $5;
}
if ($InKey && $Line =~ /^uid\s+(.*)/) {
$Key{Identifier} .= $1;
}
if ($InKey && $Line =~ /\[expires:\s(.+?)\]/) {
$Key{Expires} = $1;
}
if ($InKey && $Line =~ /Key fingerprint = (.*)/) {
$Key{Fingerprint} = $1;
$Key{FingerprintShort} = $1;
$Key{FingerprintShort} =~ s/ / /g;
$Key{FingerprintShort} =~ s/ //g;
}
}
push (@Result, {%Key}) if (%Key);
close (SEARCH);
return @Result;
}
=item PublicKeyGet()
returns public key in ascii
my $Key = $CryptObject->PublicKeyGet(
Key => $KeyID,
);
=cut
sub PublicKeyGet {
my $Self = shift;
my %Param = @_;
my $Key = $Param{Key} || '';
my $KeyString = '';
my %Result = ();
open (SEARCH, "$Self->{GPGBin} --export -a ".quotemeta($Key)." 2>&1 |");
while (<SEARCH>) {
$KeyString .= $_;
}
close (SEARCH);
return $KeyString;
}
=item SecretKeyGet()
returns secret key in ascii
my $Key = $CryptObject->SecretKeyGet(
Key => $KeyID,
);
=cut
sub SecretKeyGet {
my $Self = shift;
my %Param = @_;
my $Key = $Param{Key} || '';
my $KeyString = '';
my %Result = ();
open (SEARCH, "$Self->{GPGBin} --export-secret-keys -a ".quotemeta($Key)." 2>&1 |");
while (<SEARCH>) {
$KeyString .= $_;
}
close (SEARCH);
return $KeyString;
}
=item PublicKeyDelete()
remove public key from key ring
$CryptObject->PublicKeyDelete(
Key => $KeyID,
);
=cut
sub PublicKeyDelete {
my $Self = shift;
my %Param = @_;
my $Key = $Param{Key} || '';
my $KeyString = '';
my %Result = ();
open (SEARCH, "$Self->{GPGBin} --delete-key ".quotemeta($Key)." 2>&1 |");
while (<SEARCH>) {
$KeyString .= $_;
}
close (SEARCH);
return $KeyString;
}
=item SecretKeyDelete()
remove secret key from key ring
$CryptObject->SecretKeyDelete(
Key => $KeyID,
);
=cut
sub SecretKeyDelete {
my $Self = shift;
my %Param = @_;
my $Key = $Param{Key} || '';
my $KeyString = '';
my %Result = ();
open (SEARCH, "$Self->{GPGBin} --delete-secret-key ".quotemeta($Key)." 2>&1 |");
while (<SEARCH>) {
$KeyString .= $_;
}
close (SEARCH);
return $KeyString;
}
=item KeyAdd()
add key to key ring
my $Message = $CryptObject->KeyAdd(
Key => $KeyString,
);
=cut
sub KeyAdd {
my $Self = shift;
my %Param = @_;
my $Key = $Param{Key} || '';
my $Message = '';
my %Result = ();
my ($FH, $Filename) = $Self->{FileTempObject}->TempFile();
print $FH $Key;
open (OUT, "$Self->{GPGBin} --import $Filename 2>&1 |");
while (<OUT>) {
$Message .= $_;
}
close (OUT);
return $Message;
}
sub _CryptedWithKey {
my $Self = shift;
my %Param = @_;
my $Message = '';
# check needed stuff
if (!$Param{File}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need File!");
return;
}
open (OUT, "$Self->{GPGBin} $Param{File} 2>&1 |");
while (<OUT>) {
$Message .= $_;
}
close (OUT);
if ($Message =~ /encrypted with.+?\sID\s(.+?),\s/i) {
my @Result = $Self->KeySearch(Search => $1);
if (@Result) {
return ($1, $Result[$#Result]->{Key});
}
}
return;
}
1;
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (http://otrs.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 http://www.gnu.org/licenses/gpl.txt.
=cut
=head1 VERSION
$Revision: 1.10 $ $Date: 2005/10/17 20:41:55 $
=cut
|