|
Line 0
Link Here
|
|
|
1 |
package C4::GnuPG; |
| 2 |
|
| 3 |
# Copyright 2012/2016 Mirko Tietgen koha.abunchofthings.net |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
|
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
use C4::Context; |
| 24 |
use Crypt::GPG; |
| 25 |
|
| 26 |
=head1 C4::GnuPG |
| 27 |
|
| 28 |
C4::GnuPG |
| 29 |
|
| 30 |
=head1 Description |
| 31 |
|
| 32 |
Provides methods to interact with GnuPG via Crypt::GPG for the purpose of encrypting emails to patrons |
| 33 |
|
| 34 |
=cut |
| 35 |
|
| 36 |
our $gpg = new Crypt::GPG; |
| 37 |
$gpg->gpgbin('/usr/bin/gpg'); |
| 38 |
$gpg->encryptsafe(0); # we have to allow untrusted keys |
| 39 |
|
| 40 |
=head2 encrypt |
| 41 |
|
| 42 |
Take email content and patron's email address, send back encrypted content. Call like |
| 43 |
|
| 44 |
$content = C4::GnuPG->encrypt($content, $patronemail); |
| 45 |
|
| 46 |
=cut |
| 47 |
|
| 48 |
sub encrypt { |
| 49 |
my ($self, $content, $patronemail) = @_; |
| 50 |
$content = $gpg->encrypt($content, $patronemail); |
| 51 |
return $content; |
| 52 |
} |
| 53 |
|
| 54 |
=head2 does_exist |
| 55 |
|
| 56 |
Check if there is a GnuPG key for the patron's email address. Call like |
| 57 |
|
| 58 |
if ( C4::GnuPG->does_exist($patronemail) = 1 ) { |
| 59 |
# … encrypt … |
| 60 |
} |
| 61 |
|
| 62 |
=cut |
| 63 |
|
| 64 |
sub does_exist { |
| 65 |
# TODO: check if gpg binary is found before encrypting to avoid errors |
| 66 |
my ($self, $patronemail) = @_; |
| 67 |
if ( $gpg->keydb($patronemail) ) { |
| 68 |
return 1; |
| 69 |
} |
| 70 |
else { |
| 71 |
return 0; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
=head2 add_key |
| 76 |
|
| 77 |
Add or replace a patron's key. If there is already a key present, delete. Then add the new key. |
| 78 |
|
| 79 |
=cut |
| 80 |
|
| 81 |
sub add_key { |
| 82 |
my ($self, $key, $patronemail) = @_; |
| 83 |
# check if it is a public key before doing anything |
| 84 |
if ( $key =~ /BEGIN PGP PUBLIC KEY BLOCK/ ) { |
| 85 |
# if we already got a key, delete |
| 86 |
if ( does_exist($patronemail) ) { |
| 87 |
my @oldkey = $gpg->keydb($patronemail); |
| 88 |
if ( $oldkey[0] ) { |
| 89 |
$gpg->delkey($oldkey[0]); |
| 90 |
} |
| 91 |
} |
| 92 |
# … add new key |
| 93 |
$gpg->addkey($key); |
| 94 |
} |
| 95 |
else { |
| 96 |
return 0; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
sub del_key { |
| 101 |
my ($self, $patronemail) = @_; |
| 102 |
my @key = $gpg->keydb($patronemail); |
| 103 |
$gpg->delkey($key[0]); |
| 104 |
} |
| 105 |
|
| 106 |
=head2 get_key |
| 107 |
|
| 108 |
=cut |
| 109 |
|
| 110 |
sub get_key { |
| 111 |
# TODO: check security issue: can i get a secret key with this? |
| 112 |
my ($self, $patronemail) = @_; |
| 113 |
my @key = $gpg->keydb($patronemail); |
| 114 |
my $keystring = $gpg->export($key[0]); |
| 115 |
if ( $keystring =~ /BEGIN PGP PUBLIC KEY BLOCK/) { |
| 116 |
return $keystring; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
1; |