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