/
Smime.php
461 lines (400 loc) · 15.6 KB
/
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
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
<?php
/**
* Copyright 2002-2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2000-2015 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
/**
* Renderer for viewing/decrypting of S/MIME v3.2 messages (RFC 5751).
*
* This class handles the following MIME types:
* application/pkcs7-mime
* application/x-pkcs7-mime
* application/pkcs7-signature (in multipart/signed part)
* application/x-pkcs7-signature (in multipart/signed part)
*
* This class may add the following parameters to the URL:
* 'smime_verify_msg' - (boolean) Do verification of S/MIME message.
* 'view_smime_key' - (boolean) Display the S/MIME Key.
*
* @author Mike Cochrane <mike@graftonhall.co.nz>
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2000-2015 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Mime_Viewer_Smime extends Horde_Mime_Viewer_Base
{
/**
* This driver's display capabilities.
*
* @var array
*/
protected $_capability = array(
'full' => false,
'info' => false,
'inline' => true,
'raw' => false
);
/**
* Metadata for the current viewer/data.
*
* @var array
*/
protected $_metadata = array(
'compressed' => false,
'embedded' => true,
'forceinline' => true
);
/**
* IMP_Crypt_Smime object.
*
* @var IMP_Crypt_Smime
*/
protected $_impsmime = null;
/**
* Init the S/MIME Horde_Crypt object.
*/
protected function _initSmime()
{
if (is_null($this->_impsmime) &&
$GLOBALS['prefs']->getValue('use_smime')) {
try {
$this->_impsmime = $GLOBALS['injector']->getInstance('IMP_Crypt_Smime');
$this->_impsmime->checkForOpenSSL();
} catch (Horde_Exception $e) {
$this->_impsmime = null;
}
}
}
/**
* Return the rendered inline version of the Horde_Mime_Part object.
*
* @return array See parent::render().
*/
protected function _renderInline()
{
/* Check to see if S/MIME support is available. */
$this->_initSmime();
if ($GLOBALS['injector']->getInstance('Horde_Variables')->view_smime_key) {
return $this->_outputSmimeKey();
}
$id = $this->_mimepart->getMimeId();
switch ($this->_mimepart->getType()) {
case 'multipart/signed':
if (!in_array($this->_mimepart->getContentTypeParameter('protocol'), array('application/pkcs7-signature', 'application/x-pkcs7-signature'))) {
return array();
}
$this->_parseSignedData(true);
// Fall-through
case 'application/pkcs7-mime':
case 'application/x-pkcs7-mime':
$cache = $this->getConfigParam('imp_contents')->getViewCache();
if (isset($cache->smime[$id])) {
$ret = array(
$id => array(
'data' => null,
'status' => $cache->smime[$id]['status'],
'type' => 'text/plain; charset=' . $this->getConfigParam('charset'),
'wrap' => $cache->smime[$id]['wrap']
)
);
if (isset($cache->smime[$id]['sig'])) {
$ret[$cache->smime[$id]['sig']] = null;
}
return $ret;
}
// Fall-through
default:
return array();
}
}
/**
* If this MIME part can contain embedded MIME parts, and those embedded
* MIME parts exist, return an altered version of the Horde_Mime_Part that
* contains the embedded MIME part information.
*
* @return mixed A Horde_Mime_Part with the embedded MIME part information
* or null if no embedded MIME parts exist.
*/
protected function _getEmbeddedMimeParts()
{
if (!in_array($this->_mimepart->getType(), array('application/pkcs7-mime', 'application/x-pkcs7-mime'))) {
return null;
}
switch ($this->_getSmimeType($this->_mimepart)) {
case 'signed-data':
return $this->_parseSignedData();
case 'enveloped-data':
return $this->_parseEnvelopedData();
}
}
/**
* Parse enveloped (encrypted) data.
*
* @return mixed See self::_getEmbeddedMimeParts().
*/
protected function _parseEnvelopedData()
{
$base_id = $this->_mimepart->getMimeId();
/* Initialize inline data. */
$status = new IMP_Mime_Status(
$this->_mimepart,
_("The data in this part has been encrypted via S/MIME.")
);
$status->icon('mime/encryption.png', 'S/MIME');
$cache = $this->getConfigParam('imp_contents')->getViewCache();
$cache->smime[$base_id] = array(
'status' => $status,
'wrap' => ''
);
/* Is PGP active? */
$this->_initSmime();
if (empty($this->_impsmime)) {
$status->addText(_("S/MIME support is not currently enabled so the data is unable to be decrypted."));
return null;
}
if (!$this->_impsmime->getPersonalPrivateKey()) {
$status->addText(_("No personal private key exists so the data is unable to be decrypted."));
return null;
}
/* Make sure we have a passphrase. */
$passphrase = $this->_impsmime->getPassphrase();
if ($passphrase === false) {
$imple = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Imple')->create('IMP_Ajax_Imple_PassphraseDialog', array(
'type' => 'smimePersonal'
));
$status->addText(Horde::link('#', '', '', '', '', '', '', array('id' => $imple->getDomId())) . _("You must enter the passphrase for your S/MIME private key to view this data.") . '</a>');
return null;
}
$raw_text = $this->_getPartStream($this->_mimepart->getMimeId());
try {
$decrypted_data = $this->_impsmime->decryptMessage($this->_mimepart->replaceEOL($raw_text, Horde_Mime_Part::RFC_EOL));
} catch (Horde_Exception $e) {
$status->addText($e->getMessage());
return null;
}
$cache->smime[$base_id]['wrap'] = 'mimePartWrapValid';
$new_part = Horde_Mime_Part::parseMessage($decrypted_data, array(
'forcemime' => true
));
switch ($new_part->getType()) {
case 'application/pkcs7-mime':
case 'application/x-pkcs7-mime':
$signed_data = ($this->_getSmimeType($new_part) === 'signed-data');
break;
case 'multipart/signed':
$signed_data = true;
break;
default:
$signed_data = false;
break;
}
if ($signed_data) {
$hdrs = $this->getConfigParam('imp_contents')->getHeader();
$data = new Horde_Stream_Temp();
$data->add(
'From:' . $hdrs['From'] . "\n" .
$decrypted_data
);
$new_part->setMetadata('imp-smime-decrypt', $data);
$new_part->setContents($decrypted_data, array(
'encoding' => 'binary'
));
}
return $new_part;
}
/**
* Parse signed data.
*
* @param boolean $sig_only Only do signature checking?
*
* @return mixed See self::_getEmbeddedMimeParts().
*/
protected function _parseSignedData($sig_only = false)
{
$partlist = array_keys($this->_mimepart->contentTypeMap());
$base_id = reset($partlist);
$data_id = next($partlist);
$id_ob = new Horde_Mime_Id($data_id);
$sig_id = $id_ob->idArithmetic($id_ob::ID_NEXT);
/* Initialize inline data. */
$status = new IMP_Mime_Status(
$this->_mimepart,
_("The data in this part has been digitally signed via S/MIME.")
);
$status->icon('mime/encryption.png', 'S/MIME');
$cache = $this->getConfigParam('imp_contents')->getViewCache();
$cache->smime[$base_id] = array(
'sig' => $sig_id,
'status' => $status,
'wrap' => 'mimePartWrap'
);
if (!$GLOBALS['prefs']->getValue('use_smime')) {
$status->addText(_("S/MIME support is not enabled so the digital signature is unable to be verified."));
return null;
}
/* Sanity checking to make sure MIME structure is correct. */
if (!in_array($sig_id, $partlist)) {
$status->action(IMP_Mime_Status::ERROR);
$cache->smime[$base_id]['wrap'] = 'mimePartWrapInvalid';
$status->addText(_("Invalid S/MIME data."));
/* This will suppress displaying the invalid part. */
$cache->smime[$base_id]['sig'] = $data_id;
return null;
}
$imp_contents = $this->getConfigParam('imp_contents');
$stream = $imp_contents->isEmbedded($base_id)
? $this->_mimepart->getMetadata('imp-smime-decrypt')->stream
: $this->_getPartStream($base_id);
$raw_text = $this->_mimepart->replaceEOL($stream, Horde_Mime_Part::RFC_EOL);
$this->_initSmime();
$sig_result = null;
if ($GLOBALS['prefs']->getValue('smime_verify') ||
$GLOBALS['injector']->getInstance('Horde_Variables')->smime_verify_msg) {
try {
$sig_result = $this->_impsmime->verifySignature($raw_text);
if ($sig_result->verify) {
$status->action(IMP_Mime_Status::SUCCESS);
} else {
$status->action(IMP_Mime_Status::WARNING);
}
$cache->smime[$base_id]['wrap'] = 'mimePartWrapValid';
$email = is_array($sig_result->email)
? implode(', ', $sig_result->email)
: $sig_result->email;
$status->addText($sig_result->msg);
if (!empty($sig_result->cert)) {
$cert = $this->_impsmime->parseCert($sig_result->cert);
if (isset($cert['certificate']['subject']['CommonName']) &&
(strcasecmp($email, $cert['certificate']['subject']['CommonName']) !== 0)) {
$email = $cert['certificate']['subject']['CommonName'] . ' (' . trim($email) . ')';
}
}
if (!empty($sig_result->cert) &&
isset($sig_result->email) &&
$GLOBALS['registry']->hasMethod('contacts/addField') &&
$GLOBALS['prefs']->getValue('add_source')) {
$status->addText(sprintf(_("Sender: %s"), $imp_contents->linkViewJS($this->_mimepart, 'view_attach', htmlspecialchars(strlen($email) ? $email : $sig_result->email), array(
'jstext' => _("View certificate details"),
'params' => array(
'mode' => IMP_Contents::RENDER_INLINE,
'view_smime_key' => 1
)
))));
try {
$this->_impsmime->getPublicKey($sig_result->email);
} catch (Horde_Exception $e) {
$imple = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Imple')->create('IMP_Ajax_Imple_ImportEncryptKey', array(
'mime_id' => $base_id,
'muid' => strval($imp_contents->getIndicesOb()),
'type' => 'smime'
));
$status->addText(Horde::link('#', '', '', '', '', '', '', array('id' => $imple->getDomId())) . _("Save the certificate to your Address Book.") . '</a>');
}
} elseif (strlen($email)) {
$status->addText(sprintf(_("Sender: %s"), htmlspecialchars($email)));
}
} catch (Horde_Exception $e) {
$status->action(IMP_Mime_Status::ERROR);
$cache->smime[$base_id]['wrap'] = 'mimePartWrapInvalid';
$status->addText($e->getMessage());
}
} else {
$status->addMimeAction(
'smimeVerifyMsg',
_("Click to verify the data.")
);
}
if ($sig_only) {
return;
}
if (!($subpart = $imp_contents->getMimePart($sig_id))) {
try {
$msg_data = $this->_impsmime->extractSignedContents($raw_text);
$subpart = Horde_Mime_Part::parseMessage($msg_data, array('forcemime' => true));
} catch (Horde_Exception $e) {
$status->addText($e->getMessage());
return null;
}
}
return $subpart;
}
/**
* Generates HTML output for the S/MIME key.
*
* @return string The HTML output.
*/
protected function _outputSmimeKey()
{
if (empty($this->_impsmime)) {
return array();
}
$raw_text = $this->_getPartStream($this->_mimepart->getMimeId());
try {
$sig_result = $this->_impsmime->verifySignature($this->_mimepart->replaceEOL($raw_text, Horde_Mime_Part::RFC_EOL));
} catch (Horde_Exception $e) {
return array();
}
return array(
$this->_mimepart->getMimeId() => array(
'data' => $this->_impsmime->certToHTML($sig_result->cert),
'type' => 'text/html; charset=' . $this->getConfigParam('charset')
)
);
}
/**
*/
protected function _getPartStream($id)
{
return $id
? $this->getConfigParam('imp_contents')->getBodyPart($id, array('mimeheaders' => true, 'stream' => true))->data
: $this->getConfigParam('imp_contents')->fullMessageText();
}
/**
* Determines the S/MIME type of a part. Uses the smime-type content
* parameter (if it exists), and falls back to ASN.1 parsing of data if
* it doesn't exist.
*
* @param Horde_Mime_Part $part MIME part with S/MIME data.
*
* @return string 'signed-data', 'enveloped-data', or null.
*/
protected function _getSmimeType(Horde_Mime_Part $part)
{
if ($type = $part->getContentTypeParameter('smime-type')) {
return strtolower($type);
}
if (!class_exists('File_ASN1')) {
return null;
}
$asn1 = new File_ASN1();
$decoded = $asn1->decodeBER($part->getContents());
foreach ($decoded as $val) {
if ($val['type'] == FILE_ASN1_TYPE_SEQUENCE) {
foreach ($val['content'] as $val2) {
if ($val2['type'] == FILE_ASN1_TYPE_OBJECT_IDENTIFIER) {
/* ASN.1 values from STD 70/RFC 5652 - CMS syntax */
switch ($val2['content']) {
case '1.2.840.113549.1.7.2':
return 'signed-data';
case '1.2.840.113549.1.7.3':
return 'enveloped-data';
default:
// Other types not supported as of now.
return null;
}
}
}
}
}
return null;
}
}