-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrc_smime.php
365 lines (314 loc) · 12.2 KB
/
rc_smime.php
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
<?php
class rc_smime extends rcube_plugin
{
public $task = 'mail|settings';
private $rc;
private $uname = '';
private $homedir;
private $css_loaded;
private $dir_checked;
private $signatures = array();
private $signed_parts = array();
function init()
{
$this->rc = rcube::get_instance();
$this->uname = $this->rc->user->get_username();
$homedir = $this->rc->config->get('rc_smime_homedir', $this->home . '/users');
if (!$this->dir_checked) {
$this->check_dir($homedir);
$this->dir_checked = true;
}
if ($this->rc->task == 'mail') {
// include localization (if wasn't included before)
$this->add_texts('localization/', true);
$this->load_css();
// load javascript
$this->include_script('rc_smime.js');
// message parse/display hooks
$this->add_hook('message_body_prefix', array($this, 'message_body_prefix_hook'));
$this->add_hook('message_part_structure', array($this, 'message_part_structure_hook'));
} elseif ($this->rc->task == 'settings') {
}
}
function message_body_prefix_hook($args)
{
$part_id = $args['part']->mime_id;
// skip: not a message part
if ($args['part'] instanceof rcube_message) {
return $args;
}
// Signature verification status
if (isset($this->signed_parts[$part_id]) && ($sig = $this->signatures[$this->signed_parts[$part_id]])) {
$attrib['id'] = 'smime-message';
$smime_sender = '';
if (is_array($sig['email'])) {
foreach ($sig['email'] as $email) {
if ($smime_sender != '') {
$smime_sender .= ' ' . $this->gettext('alias') . ' ';
}
$smime_sender .= ($sig['name'] ? $sig['name'] . ' ' : '') . '<' . $email . '>';
}
} else {
$smime_sender = ($sig['name'] ? $sig['name'] . ' ' : '') . '<' . $sig['email'] . '>';
}
switch ($sig['valid']) {
case "valid":
$attrib['class'] = 'smime-notice';
$smime_msg = rcube::Q($this->gettext(array(
'name' => 'sigvalid',
'vars' => array(
'sender' => $smime_sender,
'issuer' => $sig['issuer'],
),
)));
break;
case "unverified":
$attrib['class'] = 'smime-warning';
$smime_msg = rcube::Q($this->gettext(array(
'name' => 'sigunverified',
'vars' => array(
'sender' => $smime_sender,
'issuer' => $sig['issuer'],
),
)));
break;
default:
$attrib['class'] = 'smime-error';
$smime_msg = rcube::Q($this->gettext('siginvalid'));
}
$args['prefix'] .= html::div($attrib, $smime_msg);
// Display each signature message only once
unset($this->signatures[$this->signed_parts[$part_id]]);
}
return $args;
}
function message_part_structure_hook($args)
{
if ($args['mimetype'] == 'multipart/signed') {
$this->parse_signed($args);
}
return $args;
}
private function parse_signed(&$args)
{
$struct = $args['structure'];
// S/MIME
if ($first_part = $struct->parts[1]) {
$mime_type = $first_part->mimetype;
if ($mime_type == 'application/pkcs7-signature' || $mime_type == 'application/x-pkcs7-signature') {
$this->parse_smime_signed($args);
}
}
}
private function parse_smime_signed(&$args)
{
$struct = $args['structure'];
$msg = $args['object'];
// Verify signature
if ($this->rc->action == 'show' || $this->rc->action == 'preview') {
$msg_part = $struct->parts[0];
$full_file = tempnam($this->homedir, 'rcube_mail_full_');
$cert_file = tempnam($this->homedir, 'rcube_mail_cert_');
$part_file = tempnam($this->homedir, 'rcube_mail_part_');
$full_handle = fopen($full_file, "w");
$fullbody = $this->rc->storage->get_raw_body($msg->uid, $full_handle);
fclose($full_handle);
$out = array(
'error' => array(),
);
$sig = openssl_pkcs7_verify($full_file, 0, $cert_file);
$errorstr = $this->get_openssl_error();
if (strlen($errorstr) > 0) {
$out['error']['verify1'] = $errorstr;
}
if ($sig === true) {
$out = $this->get_user_info_from_cert($cert_file, 'valid');
} else {
$sig = openssl_pkcs7_verify($full_file, PKCS7_NOVERIFY, $cert_file);
$errorstr = $this->get_openssl_error();
if (strlen($errorstr) > 0) {
$out['error']['verify2'] = $errorstr;
}
if ($sig === true) {
$out = $this->get_user_info_from_cert($cert_file, 'unverified');
} elseif ($sig === false) {
$out['valid'] = 'invalid';
} else {
$part_handle = fopen($part_file, "w");
$mimes = $this->rc->storage->conn->fetchMIMEHeaders($msg->folder, $msg->uid, $struct->mime_id, true);
foreach (array_values($mimes) as $mime) {
fwrite($part_handle, $mime);
}
fwrite($part_handle, "\n");
$part_out = $this->rc->storage->conn->handlePartBody($msg->folder, $msg->uid, true, $struct->mime_id, null, null, $part_handle);
fclose($part_handle);
$sig = openssl_pkcs7_verify($part_file, 0, $cert_file);
$errorstr = $this->get_openssl_error();
if (strlen($errorstr) > 0) {
$out['error']['verify3'] = $errorstr;
}
if ($sig === true) {
$out = $this->get_user_info_from_cert($cert_file, 'valid');
} else {
$sig = openssl_pkcs7_verify($part_file, PKCS7_NOVERIFY, $cert_file);
$errorstr = $this->get_openssl_error();
if (strlen($errorstr) > 0) {
$out['error']['verify4'] = $errorstr;
}
if ($sig === true) {
$out = $this->get_user_info_from_cert($cert_file, 'unverified');
} elseif ($sig === false) {
$out['valid'] = 'invalid';
} else {
$out['valid'] = 'error';
}
}
}
}
unlink($full_file);
unlink($cert_file);
unlink($part_file);
$this->signatures[$struct->mime_id] = $out;
// Message can be multipart (assign signature to each subpart)
$this->set_signed_parts($msg_part, $struct->mime_id);
}
return $args;
}
private function get_user_info_from_cert($file, $valid)
{
$cert = openssl_x509_parse(file_get_contents($file));
$errorstr = $this->get_openssl_error();
$sub = $cert['subject'];
$ret = array(
'valid' => $valid,
);
if (strlen($errorstr) > 0) {
$ret['error'] = $errorstr;
}
if (array_key_exists('emailAddress', $sub)) {
$ret['email'] = $sub['emailAddress'];
}
if (array_key_exists('CN', $sub)) {
$ret['name'] = $sub['CN'];
}
if (array_key_exists('issuer', $cert)) {
$issuer = $cert['issuer'];
if (array_key_exists('O', $issuer)) {
$ret['issuer'] = $issuer['O'];
}
}
// Scan subAltName for email addresses
if (array_key_exists('extensions', $cert) && array_key_exists('subjectAltName', $cert['extensions'])) {
$emailAddresses = array();
foreach (explode(', ', $cert['extensions']['subjectAltName']) as $altName) {
$parts = explode(':', $altName);
if ($parts[0] == 'email') {
array_push ($emailAddresses, $parts[1]);
}
}
if (count($emailAddresses) > 0) {
$ret['email'] = $emailAddresses;
}
}
return $ret;
}
private function get_openssl_error()
{
$tmp = array();
while ($errorstr = openssl_error_string()) {
$tmp[] = $errorstr;
}
$ret = join("\n", array_values($tmp));
return $ret;
}
private function set_signed_parts($msg_part, $id)
{
if (!empty($msg_part->parts)) {
foreach ($msg_part->parts as $part) {
$this->signed_parts[$part->mime_id] = $id;
if (!empty($part->parts)) {
$this->set_signed_parts($part, $id);
}
}
} else {
$this->signed_parts[$msg_part->mime_id] = $id;
}
}
/**
* Checks if specified message part contains body data.
* If body is not set it will be fetched from IMAP server.
*
* @param rcube_message_part Message part object
* @param integer Message UID
*/
private function set_part_body($part, $uid)
{
if (!isset($part->body)) {
$part->body = $this->rc->storage->get_message_part($uid, $part->mime_id, $part);
}
}
private function load_css()
{
if ($this->css_loaded) {
return;
} else {
$this->include_stylesheet("skins/rc_smime.css");
$this->css_loaded = true;
}
}
private function check_dir($dir)
{
// check if homedir exists (create it if not) and is readable
if (!$dir) {
return $this->rc->raise_error(array(
'code' => 999,
'type' => 'php',
'file' => __FILE__,
'line' => __LINE__,
'message' => "Option 'rc_smime_homedir' not specified",
), true, false);
}
if (!file_exists($dir)) {
return $this->rc->raise_error(array(
'code' => 999,
'type' => 'php',
'file' => __FILE__,
'line' => __LINE__,
'message' => "Keys directory doesn't exists: $dir",
), true, false);
}
if (!is_writable($dir)) {
return $this->rc->raise_error(array(
'code' => 999,
'type' => 'php',
'file' => __FILE__,
'line' => __LINE__,
'message' => "Keys directory isn't writeable: $dir",
), true, false);
}
$dir = $dir . '/' . $this->uname;
// check if user's homedir exists (create it if not) and is readable
if (!file_exists($dir)) {
mkdir($dir, 0700);
}
if (!file_exists($dir)) {
return $this->rc->raise_error(array(
'code' => 999,
'type' => 'php',
'file' => __FILE__,
'line' => __LINE__,
'message' => "Unable to create keys directory: $dir",
), true, false);
}
if (!is_writable($dir)) {
return $this->rc->raise_error(array(
'code' => 999,
'type' => 'php',
'file' => __FILE__,
'line' => __LINE__,
'message' => "Unable to write to keys directory: $dir",
), true, false);
}
$this->homedir = $dir;
}
}