forked from niklasfemerstrand/rc_openpgpjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc_openpgpjs.php
More file actions
279 lines (251 loc) · 10.6 KB
/
rc_openpgpjs.php
File metadata and controls
279 lines (251 loc) · 10.6 KB
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
<?php
/*
+-------------------------------------------------------------------------+
| OpenPGP.js implemented in Roundcube |
| |
| Copyright (C) 2013 Niklas Femerstrand <nik@qnrq.se> |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License version 2 |
| as published by the Free Software Foundation. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License along |
| with this program; if not, write to the Free Software Foundation, Inc., |
| 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| |
+-------------------------------------------------------------------------+
*/
class rc_openpgpjs extends rcube_plugin
{
public $task = 'mail|settings';
public $rc;
/**
* Plugin initialization.
*/
function init()
{
$this->rc = rcube::get_instance();
$this->rm = rcmail::get_instance();
$this->add_hook('user_create', array($this, 'user_create'));
$this->register_action('plugin.pks_search', array($this, 'hkp_search'));
$this->register_action('plugin.hkp_add', array($this, 'hkp_add'));
if ($this->rc->task == 'mail') {
$this->add_hook('render_page', array($this, 'render_page'));
// make localization available on the client
$this->add_texts('localization/', true);
// load js
$this->include_script('js/openpgp.min.js');
$this->include_script('js/rc_openpgpjs.crypto.js');
$this->include_script('js/rc_openpgpjs.js');
// load css
$this->include_stylesheet($this->local_skin_path() . '/rc_openpgpjs.css');
if ($this->api->output->type == 'html') {
// add key manager item to message menu
$opts = array("command" => "open-key-manager",
"label" => "rc_openpgpjs.key_manager",
"type" => "link",
"classact" => "icon active",
"class" => "icon",
"innerclass" => "icon key_manager");
$this->api->add_content(html::tag('li', null, $this->api->output->button($opts)), "messagemenu");
if ($this->rc->action == 'compose') {
// add key manager button to compose toolbar
$opts = array("command" => "open-key-manager",
"label" => "rc_openpgpjs.key_manager",
"type" => "link",
"classact" => "button active key_manager",
"class" => "button key_manager");
$this->api->add_content($this->api->output->button($opts), "toolbar");
// add encrypt and sign checkboxes to composeoptions
$encrypt_opts = array('id' => 'openpgpjs_encrypt',
'type' => 'checkbox');
if($this->rc->config->get('encrypt', true)) {
$encrypt_opts['checked'] = 'checked';
}
$encrypt = new html_inputfield($encrypt_opts);
$this->api->add_content(
html::span('composeoption', html::label(null, $encrypt->show() . $this->gettext('encrypt'))),
"composeoptions"
);
$sign_opts = array('id' => 'openpgpjs_sign',
'type' => 'checkbox');
if($this->rc->config->get('sign', true)) {
$sign_opts['checked'] = 'checked';
}
$sign = new html_inputfield($sign_opts);
$this->api->add_content(
html::span('composeoption', html::label(null, $sign->show() . $this->gettext('sign'))),
"composeoptions"
);
}
}
} elseif ($this->rc->task == 'settings') {
// load localization
$this->add_texts('localization/', false);
// add hooks for OpenPGP settings
$this->add_hook('preferences_list', array($this, 'preferences_list'));
$this->add_hook('preferences_save', array($this, 'preferences_save'));
}
}
/**
* Add key manager and key selector to html output
*
* @param array Original parameters
* @return array Modified parameters
*/
function render_page($params) {
$template_path = $this->home . '/'. $this->local_skin_path();
$this->rc->output->add_footer($this->rc->output->just_parse(
file_get_contents($template_path . '/templates/key_manager.html') .
file_get_contents($template_path . '/templates/key_search.html') .
file_get_contents($template_path . '/templates/key_select.html')));
$this->rc->output->add_footer(html::div(array('style' => "visibility: hidden;",
'id' => "openpgpjs_identities"),
json_encode($this->rm->user->list_identities())));
return $params;
}
/**
* Create default identity, required as pubkey metadata
*/
function user_create($params)
{
$params['user_name'] = preg_replace("/@.*$/", "", $params['user']);
$params['user_email'] = $params['user'];
return $params;
}
/**
* This Public Key Server proxy is written to circumvent Access-Control-Allow-Origin
* limitations. It also provides a layer of security as HKP normally doesn't
* support HTTPS; essentially preventing MITM if the Roundcube installation
* is configured to use HTTPS.
*
* For more details see the following:
* http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00
* http://sks-keyservers.net/
*/
function hkp_search() {
if(!isset($_POST['op']) || !isset($_POST['search'])) {
return $this->rc->output->command(
'plugin.pks_search',
array('message' => "ERR: Missing param",
'op' => htmlspecialchars($_POST['op'])));
$op = "";
$search = "";
} else {
$op = $_POST["op"];
$search = $_POST["search"];
}
if($op != "get" &&
$op != "index" &&
$op != "vindex")
return $this->rc->output->command(
'plugin.pks_search',
array('message' => "ERR: Invalid operation",
'op' => htmlspecialchars($op)));
if($op == "index") {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "http://pgp.mit.edu:11371/pks/lookup?op=index&search={$search}");
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($status == 200) {
// TODO Fix search regex to match 32/64-bit str
preg_match_all("/\/pks\/lookup\?op=vindex&search=(.*)\">(.*)<\/a>/", $result, $m);
if(count($m) > 0) {
$found = array();
for($i = 0; $i < count($m[0]); $i++)
$found[] = array($m[1][$i], $m[2][$i]);
return $this->rc->output->command(
'plugin.pks_search',
array('message' => json_encode($found),
'op' => htmlspecialchars($op)));
}
} else {
preg_match("/Error handling request: (.*)<\/body>/", $result, $m);
return $this->rc->output->command(
'plugin.pks_search',
array('message' => "ERR: " . htmlspecialchars($m[1]),
'op' => htmlspecialchars($op)));
}
} elseif($op == "get") {
if(preg_match("/^0x[0-9A-F]{8}$/i", $search)) {
define("32_BIT_KEY", true);
define("64_BIT_KEY", false);
} elseif(preg_match("/^0x[0-9A-F]{16}$/i", $search)) {
define("32_BIT_KEY", false);
define("64_BIT_KEY", true);
} else {
return $this->rc->output->command(
'plugin.pks_search',
array('message' => "ERR: Incorrect search format for this operation",
'op' => htmlspecialchars($op)));
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "http://pgp.mit.edu:11371/pks/lookup?op=get&search={$search}");
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($status == 200) {
preg_match_all("/-----BEGIN PGP PUBLIC KEY BLOCK-----(.*)-----END PGP PUBLIC KEY BLOCK-----/s", $result, $m);
return $this->rc->output->command(
'plugin.pks_search',
array('message' => json_encode($m),
'op' => htmlspecialchars($op)));
}
}
}
// TODO: Store pubkeys in rc storage
// Don't sync upstream, it hurts decentralization
function hkp_add() {
header("HTTP/1.1 501 Not Implemented");
die();
}
/**
* Handler for preferences_list hook.
* Adds options blocks into Compose settings sections in Preferences.
*
* @param array Original parameters
* @return array Modified parameters
*/
function preferences_list($p)
{
if ($p['section'] == 'compose') {
$p['blocks']['openpgp']['name'] = $this->gettext('openpgp_options');
$field_id = 'rcmfd_encrypt';
$encrypt = new html_checkbox(array('name' => '_encrypt', 'id' => $field_id, 'value' => 1));
$p['blocks']['openpgp']['options']['encrypt'] = array(
'title' => html::label($field_id, Q($this->gettext('always_encrypt'))),
'content' => $encrypt->show($this->rc->config->get('encrypt', true)?1:0),
);
$field_id = 'rcmfd_sign';
$sign = new html_checkbox(array('name' => '_sign', 'id' => $field_id, 'value' => 1));
$p['blocks']['openpgp']['options']['sign'] = array(
'title' => html::label($field_id, Q($this->gettext('always_sign'))),
'content' => $sign->show($this->rc->config->get('sign', true)?1:0),
);
}
return $p;
}
/**
* Handler for preferences_save hook.
* Executed on Compose settings form submit.
*
* @param array Original parameters
* @return array Modified parameters
*/
function preferences_save($p)
{
if ($p['section'] == 'compose') {
$p['prefs']['encrypt'] = get_input_value('_encrypt', RCUBE_INPUT_POST) ? true : false;
$p['prefs']['sign'] = get_input_value('_sign', RCUBE_INPUT_POST) ? true : false;
}
return $p;
}
}