/
Pgp.php
974 lines (878 loc) · 32.1 KB
/
Pgp.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
<?php
/**
* Copyright 2002-2017 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 2002-2017 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
/**
* Contains code related to handling PGP data within IMP.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2002-2017 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Pgp
{
/* Name of PGP public key field in addressbook. */
const PUBKEY_FIELD = 'pgpPublicKey';
/* Encryption type constants. */
const ENCRYPT = 'pgp_encrypt';
const SIGN = 'pgp_sign';
const SIGNENC = 'pgp_signenc';
const SYM_ENCRYPT = 'pgp_sym_enc';
const SYM_SIGNENC = 'pgp_syn_sign';
/**
* Pgp object.
*
* @var Horde_Crypt_Pgp
*/
protected $_pgp;
/**
* Return whether PGP support is current enabled in IMP.
*
* @return boolean True if PGP support is enabled.
*/
public static function enabled()
{
global $conf, $prefs;
return (!empty($conf['gnupg']['path']) && $prefs->getValue('use_pgp'));
}
/**
* Constructor.
*
* @param Horde_Crypt_Pgp $pgp PGP object.
*/
public function __construct(Horde_Crypt_Pgp $pgp)
{
$this->_pgp = $pgp;
}
/**
* Return the list of available encryption options for composing.
*
* @return array Keys are encryption type constants, values are gettext
* strings describing the encryption type.
*/
public function encryptList()
{
$ret = array(
self::ENCRYPT => _("PGP Encrypt Message")
);
if ($this->getPersonalPrivateKey()) {
$ret += array(
self::SIGN => _("PGP Sign Message"),
self::SIGNENC => _("PGP Sign/Encrypt Message")
);
}
return $ret + array(
self::SYM_ENCRYPT => _("PGP Encrypt Message with passphrase"),
self::SYM_SIGNENC => _("PGP Sign/Encrypt Message with passphrase")
);
}
/**
* Generate the personal Public/Private keypair and store in prefs.
*
* @param string $name See Horde_Crypt_Pgp::.
* @param string $email See Horde_Crypt_Pgp::.
* @param string $passphrase See Horde_Crypt_Pgp::.
* @param string $comment See Horde_Crypt_Pgp::.
* @param string $keylength See Horde_Crypt_Pgp::.
* @param integer $expire See Horde_Crypt_Pgp::.
*
* @throws Horde_Crypt_Exception
*/
public function generatePersonalKeys($name, $email, $passphrase,
$comment = '', $keylength = 1024,
$expire = null)
{
$keys = $this->_pgp->generateKey(
$name,
$email,
$passphrase,
$comment,
$keylength,
$expire
);
/* Store the keys in the user's preferences. */
$this->addPersonalPublicKey($keys['public']);
$this->addPersonalPrivateKey($keys['private']);
}
/**
* Add the personal public key to the prefs.
*
* @param mixed $public_key The public key to add (either string or
* array).
*/
public function addPersonalPublicKey($public_key)
{
$GLOBALS['prefs']->setValue('pgp_public_key', trim($public_key));
}
/**
* Add the personal private key to the prefs.
*
* @param mixed $private_key The private key to add (either string or
* array).
*/
public function addPersonalPrivateKey($private_key)
{
$GLOBALS['prefs']->setValue('pgp_private_key', trim($private_key));
}
/**
* Get the personal public key from the prefs.
*
* @return string The personal PGP public key.
*/
public function getPersonalPublicKey()
{
return $GLOBALS['prefs']->getValue('pgp_public_key');
}
/**
* Get the personal private key from the prefs.
*
* @return string The personal PGP private key.
*/
public function getPersonalPrivateKey()
{
return $GLOBALS['prefs']->getValue('pgp_private_key');
}
/**
* Deletes the specified personal keys from the prefs.
*/
public function deletePersonalKeys()
{
$GLOBALS['prefs']->setValue('pgp_public_key', '');
$GLOBALS['prefs']->setValue('pgp_private_key', '');
$this->unsetPassphrase('personal');
}
/**
* Add a public key to an address book.
*
* @param string $public_key An PGP public key.
*
* @return array See Horde_Crypt_Pgp::pgpPacketInformationMultiple()
* @throws Horde_Crypt_Exception
* @throws Horde_Exception
*/
public function addPublicKey($public_key)
{
$valid = false;
$keys = array();
foreach ($this->_pgp->pgpPacketInformationMultiple($public_key) as $key_info) {
/* Make sure the key is valid. */
if (!isset($key_info['signature'])) {
continue;
}
$valid = true;
/* Remove the '_SIGNATURE' entry. */
unset($key_info['signature']['_SIGNATURE']);
/* Store all signatures that appear in the key. */
foreach ($key_info['signature'] as $id => $sig) {
if (!isset($sig['email'])) {
continue;
}
if (!isset($sig['name'])) {
$key_info['signature'][$id]['name'] = $sig['name'] = $sig['email'];
}
/* Check to make sure the key does not already exist in ANY
* address book and remove the id from the key_info for a
* correct output. */
try {
$result = $this->getPublicKey(
$sig['email'],
array(
'nocache' => true,
'nohooks' => true,
'noserver' => true
)
);
if (!empty($result)) {
unset($key_info['signature'][$id]);
continue;
}
} catch (Horde_Crypt_Exception $e) {
}
/* Add key to the user's address book. */
$GLOBALS['registry']->call(
'contacts/addField',
array(
$sig['email'],
$sig['name'],
self::PUBKEY_FIELD,
$public_key,
$GLOBALS['prefs']->getValue('add_source')
)
);
}
$keys[] = $key_info;
}
if (!$valid) {
throw new Horde_Crypt_Exception(_("No valid public key found."));
}
return $keys;
}
/**
* Retrieves a public key by e-mail.
*
* First, the key will be attempted to be retrieved from a user's address
* book(s).
* Second, if unsuccessful, the key is attempted to be retrieved via a
* public PGP keyserver.
*
* @param string $address The e-mail address to search by.
* @param array $options Additional options:
* - keyid: (string) The key ID of the user's key.
* DEFAULT: key ID not used
* - nocache: (boolean) Don't retrieve from cache?
* DEFAULT: false
* - nohooks: (boolean) Don't trigger hook when retrieving public key?
* DEFAULT: false
* - noserver: (boolean) Whether to check the public key servers for the
* key.
* DEFAULT: false
*
* @return string The PGP public key requested.
* @throws Horde_Crypt_Exception
*/
public function getPublicKey($address, $options = array())
{
global $injector, $registry;
$keyid = empty($options['keyid'])
? ''
: $options['keyid'];
/* If there is a cache driver configured, try to get the public key
* from the cache. */
if (empty($options['nocache']) &&
($cache = $injector->getInstance('Horde_Cache'))) {
$result = $cache->get("PGPpublicKey_" . $address . $keyid, 3600);
if ($result) {
Horde::log('PGPpublicKey: ' . serialize($result), 'DEBUG');
return $result;
}
}
if (empty($options['nohooks'])) {
try {
$key = $injector->getInstance('Horde_Core_Hooks')->callHook(
'pgp_key',
'imp',
array($address, $keyid)
);
if ($key) {
return $key;
}
} catch (Horde_Exception_HookNotSet $e) {}
}
/* Try retrieving by e-mail only first. */
$result = null;
try {
$result = $registry->call(
'contacts/getField',
array(
$address,
self::PUBKEY_FIELD,
$injector->getInstance('IMP_Contacts')->sources,
true,
true
)
);
} catch (Horde_Exception $e) {}
if (is_null($result)) {
/* TODO: Retrieve by ID. */
/* See if the address points to the user's public key. */
$identity = $injector->getInstance('IMP_Identity');
$personal_pubkey = $this->getPersonalPublicKey();
if (!empty($personal_pubkey) && $identity->hasAddress($address)) {
$result = $personal_pubkey;
} elseif (empty($options['noserver'])) {
$result = null;
try {
foreach ($this->_keyserverList() as $val) {
try {
$result = $val->get(
empty($keyid) ? $val->getKeyId($address) : $keyid
);
break;
} catch (Exception $e) {}
}
if (is_null($result)) {
throw $e;
}
/* If there is a cache driver configured and a cache
* object exists, store the retrieved public key in the
* cache. */
if (is_object($cache)) {
$cache->set('PGPpublicKey_' . $address . $keyid, $result, 3600);
}
} catch (Horde_Crypt_Exception $e) {
/* Return now, if no public key found at all. */
Horde::log('PGPpublicKey: ' . $e->getMessage(), 'DEBUG');
throw new Horde_Crypt_Exception(sprintf(_("Could not retrieve public key for %s."), $address));
}
} else {
$result = '';
}
}
/* If more than one public key is returned, just return the first in
* the array. There is no way of knowing which is the "preferred" key,
* if the keys are different. */
if (is_array($result)) {
$result = reset($result);
}
return $result;
}
/**
* Retrieves all public keys from a user's address book(s).
*
* @return array All PGP public keys available.
* @throws Horde_Crypt_Exception
*/
public function listPublicKeys()
{
$sources = $GLOBALS['injector']->getInstance('IMP_Contacts')->sources;
return empty($sources)
? array()
: $GLOBALS['registry']->call('contacts/getAllAttributeValues', array(self::PUBKEY_FIELD, $sources));
}
/**
* Deletes a public key from a user's address book(s) by e-mail.
*
* @param string $email The e-mail address to delete.
*
* @throws Horde_Crypt_Exception
*/
public function deletePublicKey($email)
{
return $GLOBALS['registry']->call(
'contacts/deleteField',
array(
$email,
self::PUBKEY_FIELD,
$GLOBALS['injector']->getInstance('IMP_Contacts')->sources
)
);
}
/**
* Send a public key to a public PGP keyserver.
*
* @param string $pubkey The PGP public key.
*
* @throws Horde_Crypt_Exception
*/
public function sendToPublicKeyserver($pubkey)
{
$servers = $this->_keyserverList();
$servers[0]->put($pubkey);
}
/**
* Verifies a signed message with a given public key.
*
* @param string $text The text to verify.
* @param string $address E-mail address of public key.
* @param string $signature A PGP signature block.
* @param string $charset Charset to use.
*
* @return stdClass See Horde_Crypt_Pgp::decrypt().
* @throws Horde_Crypt_Exception
*/
public function verifySignature($text, $address, $signature = '',
$charset = null)
{
if (!empty($signature)) {
$packet_info = $this->_pgp->pgpPacketInformation($signature);
if (isset($packet_info['keyid'])) {
$keyid = $packet_info['keyid'];
}
}
if (!isset($keyid)) {
$keyid = $this->_pgp->getSignersKeyID($text);
}
/* Get key ID of key. */
$public_key = $this->getPublicKey($address, array('keyid' => $keyid));
if (empty($signature)) {
$options = array('type' => 'signature');
} else {
$options = array('type' => 'detached-signature', 'signature' => $signature);
}
$options['pubkey'] = $public_key;
if (!empty($charset)) {
$options['charset'] = $charset;
}
return $this->_pgp->decrypt($text, $options);
}
/**
* Decrypt a message with user's public/private keypair or a passphrase.
*
* @param string $text The text to decrypt.
* @param string $type Either 'literal', 'personal', or 'symmetric'.
* @param array $opts Additional options:
* - passphrase: (boolean) If $type is 'personal' or 'symmetrical', the
* passphrase to use.
* - sender: (string) The sender of the message (used to check signature
* if message is both encrypted & signed).
*
* @return stdClass See Horde_Crypt_Pgp::decrypt().
* @throws Horde_Crypt_Exception
*/
public function decryptMessage($text, $type, array $opts = array())
{
$opts = array_merge(array(
'passphrase' => null
), $opts);
$pubkey = $this->getPersonalPublicKey();
if (isset($opts['sender'])) {
try {
$pubkey .= "\n" . $this->getPublicKey($opts['sender']);
} catch (Horde_Crypt_Exception $e) {}
}
switch ($type) {
case 'literal':
return $this->_pgp->decrypt($text, array(
'no_passphrase' => true,
'pubkey' => $pubkey,
'type' => 'message'
));
break;
case 'symmetric':
return $this->_pgp->decrypt($text, array(
'passphrase' => $opts['passphrase'],
'pubkey' => $pubkey,
'type' => 'message'
));
break;
case 'personal':
return $this->_pgp->decrypt($text, array(
'passphrase' => $opts['passphrase'],
'privkey' => $this->getPersonalPrivateKey(),
'pubkey' => $pubkey,
'type' => 'message'
));
}
}
/**
* Gets a passphrase from the session cache.
*
* @param integer $type The type of passphrase. Either 'personal' or
* 'symmetric'.
* @param string $id If $type is 'symmetric', the ID of the stored
* passphrase.
*
* @return mixed The passphrase, if set, or null.
*/
public function getPassphrase($type, $id = null)
{
if ($type == 'personal') {
$id = 'personal';
}
return (($cache = $GLOBALS['session']->get('imp', 'pgp')) && isset($cache[$type][$id]))
? $cache[$type][$id]
: null;
}
/**
* Store's the user's passphrase in the session cache.
*
* @param integer $type The type of passphrase. Either 'personal' or
* 'symmetric'.
* @param string $passphrase The user's passphrase.
* @param string $id If $type is 'symmetric', the ID of the
* stored passphrase.
*
* @return boolean Returns true if correct passphrase, false if incorrect.
*/
public function storePassphrase($type, $passphrase, $id = null)
{
global $session;
if ($type == 'personal') {
if ($this->_pgp->verifyPassphrase($this->getPersonalPublicKey(), $this->getPersonalPrivateKey(), $passphrase) === false) {
return false;
}
$id = 'personal';
}
$cache = $session->get('imp', 'pgp', Horde_Session::TYPE_ARRAY);
$cache[$type][$id] = $passphrase;
$session->set('imp', 'pgp', $cache, $session::ENCRYPT);
return true;
}
/**
* Clear the passphrase from the session cache.
*
* @param integer $type The type of passphrase. Either 'personal' or
* 'symmetric'.
* @param string $id If $type is 'symmetric', the ID of the
* stored passphrase. Else, all passphrases
* are deleted.
*/
public function unsetPassphrase($type, $id = null)
{
if ($cache = $GLOBALS['session']->get('imp', 'pgp')) {
if (($type == 'symmetric') && !is_null($id)) {
unset($cache['symmetric'][$id]);
} else {
unset($cache[$type]);
}
$GLOBALS['session']->set('imp', 'pgp', $cache);
}
}
/**
* Generates a cache ID for symmetric message data.
*
* @param string $mailbox The mailbox of the message.
* @param integer $uid The UID of the message.
* @param string $id The MIME ID of the message.
*
* @return string A unique symmetric cache ID.
*/
public function getSymmetricId($mailbox, $uid, $id)
{
return implode('|', array($mailbox, $uid, $id));
}
/**
* Provide the list of parameters needed for signing a message.
*
* @return array The list of parameters needed by encrypt().
*/
protected function _signParameters()
{
return array(
'pubkey' => $this->getPersonalPublicKey(),
'privkey' => $this->getPersonalPrivateKey(),
'passphrase' => $this->getPassphrase('personal')
);
}
/**
* Provide the list of parameters needed for encrypting a message.
*
* @param Horde_Mail_Rfc822_List $addresses The e-mail address of the
* keys to use for encryption.
* @param string $symmetric If true, the symmetric
* password to use for
* encrypting. If null, uses the
* personal key.
*
* @return array The list of parameters needed by encrypt().
* @throws Horde_Crypt_Exception
*/
protected function _encryptParameters(Horde_Mail_Rfc822_List $addresses,
$symmetric)
{
if (!is_null($symmetric)) {
return array(
'symmetric' => true,
'passphrase' => $symmetric
);
}
$addr_list = array();
foreach ($addresses as $val) {
/* Get the public key for the address. */
$bare_addr = $val->bare_address;
$addr_list[$bare_addr] = $this->getPublicKey($bare_addr);
}
return array('recips' => $addr_list);
}
/**
* Sign a Horde_Mime_Part using PGP using IMP default parameters.
*
* @param Horde_Mime_Part $mime_part The object to sign.
*
* @return Horde_Mime_Part See Horde_Crypt_Pgp::signMIMEPart().
* @throws Horde_Crypt_Exception
*/
public function signMimePart($mime_part)
{
return $this->_pgp->signMimePart($mime_part, $this->_signParameters());
}
/**
* Encrypt a Horde_Mime_Part using PGP using IMP default parameters.
*
* @param Horde_Mime_Part $mime_part The object to encrypt.
* @param Horde_Mail_Rfc822_List $addresses The e-mail address of the
* keys to use for encryption.
* @param string $symmetric If true, the symmetric
* password to use for
* encrypting. If null, uses the
* personal key.
*
* @return Horde_Mime_Part See Horde_Crypt_Pgp::encryptMimePart().
* @throws Horde_Crypt_Exception
*/
public function encryptMimePart($mime_part,
Horde_Mail_Rfc822_List $addresses,
$symmetric = null)
{
return $this->_pgp->encryptMimePart(
$mime_part,
$this->_encryptParameters($addresses, $symmetric)
);
}
/**
* Sign and Encrypt a Horde_Mime_Part using PGP using IMP default
* parameters.
*
* @param Horde_Mime_Part $mime_part The object to sign and
* encrypt.
* @param Horde_Mail_Rfc822_List $addresses The e-mail address of the
* keys to use for encryption.
* @param string $symmetric If true, the symmetric
* password to use for
* encrypting. If null, uses the
* personal key.
*
* @return Horde_Mime_Part See Horde_Crypt_Pgp::signAndencryptMimePart().
* @throws Horde_Crypt_Exception
*/
public function signAndEncryptMimePart($mime_part,
Horde_Mail_Rfc822_List $addresses,
$symmetric = null)
{
return $this->_pgp->signAndEncryptMimePart(
$mime_part,
$this->_signParameters(),
$this->_encryptParameters($addresses, $symmetric)
);
}
/**
* Generate a Horde_Mime_Part object, in accordance with RFC 2015/3156,
* that contains the user's public key.
*
* @return Horde_Mime_Part See Horde_Crypt_Pgp::publicKeyMimePart().
*/
public function publicKeyMimePart()
{
return $this->_pgp->publicKeyMimePart($this->getPersonalPublicKey());
}
/**
* Extracts public/private keys from armor data.
*
* @param string $data Armor text.
*
* @return array Array with these keys:
* - public: (array) Array of public keys.
* - private: (array) Array of private keys.
*/
public function getKeys($data)
{
global $injector;
$out = array(
'public' => array(),
'private' => array()
);
foreach ($injector->getInstance('Horde_Crypt_Pgp_Parse')->parse($data) as $val) {
switch ($val['type']) {
case Horde_Crypt_Pgp::ARMOR_PUBLIC_KEY:
case Horde_Crypt_Pgp::ARMOR_PRIVATE_KEY:
$key = implode("\n", $val['data']);
if ($key_info = $this->_pgp->pgpPacketInformation($key)) {
if (($val['type'] == Horde_Crypt_Pgp::ARMOR_PUBLIC_KEY) &&
!empty($key_info['public_key'])) {
$out['public'][] = $key;
} elseif (($val['type'] == Horde_Crypt_Pgp::ARMOR_PRIVATE_KEY) &&
!empty($key_info['secret_key'])) {
$out['private'][] = $key;
}
}
break;
}
}
if (!empty($out['private']) &&
empty($out['public']) &&
$res = $this->_pgp->getPublicKeyFromPrivateKey(reset($out['private']))) {
$out['public'][] = $res;
}
return $out;
}
/**
* Returns human readable information on a PGP key.
*
* @param string $pgpdata The PGP data block.
*
* @return string Tabular information on the PGP key.
* @throws Horde_Pgp_Exception
*/
public function prettyKey($pgpdata)
{
$msg = '';
$info = $this->_pgp->pgpPacketInformation($pgpdata);
if (empty($info['signature'])) {
return $msg;
}
$fingerprints = $this->_pgp->getFingerprintsFromKey($pgpdata);
/* Making the property names the same width for all localizations .*/
$leftrow = array(
_("Name"),
_("Key Type"),
_("Key Creation"),
_("Expiration Date"),
_("Key Length"),
_("Comment"),
_("E-Mail"),
_("Hash-Algorithm"),
_("Key ID"),
_("Key Fingerprint")
);
array_walk(
$leftrow,
function (&$s, $k, $m) {
$s .= ':' . str_repeat(' ', $m - Horde_String::length($s));
},
max(array_map('strlen', $leftrow)) + 2
);
foreach ($info['signature'] as $uid_idx => $val) {
if ($uid_idx == '_SIGNATURE') {
continue;
}
$key = $this->_pgp->pgpPacketSignatureByUidIndex(
$pgpdata,
$uid_idx
);
$keyid = empty($key['keyid'])
? null
: $this->getKeyIdString($key['keyid']);
$fingerprint = isset($fingerprints[$keyid])
? $fingerprints[$keyid]
: null;
$sig_key = 'sig_' . $key['keyid'];
$msg .= $leftrow[0] . (isset($key['name']) ? stripcslashes($key['name']) : '') . "\n"
. $leftrow[1] . (($key['key_type'] == 'public_key') ? _("Public Key") : _("Private Key")) . "\n"
. $leftrow[2] . strftime("%D", $val[$sig_key]['created']) . "\n"
. $leftrow[3] . (empty($val[$sig_key]['expires']) ? '[' . _("Never") . ']' : strftime("%D", $val[$sig_key]['expires'])) . "\n"
. $leftrow[4] . $key['key_size'] . " Bytes\n"
. $leftrow[5] . (empty($key['comment']) ? '[' . _("None") . ']' : $key['comment']) . "\n"
. $leftrow[6] . (empty($key['email']) ? '[' . _("None") . ']' : $key['email']) . "\n"
. $leftrow[7] . (empty($key['micalg']) ? '[' . _("Unknown") . ']' : $key['micalg']) . "\n"
. $leftrow[8] . (empty($keyid) ? '[' . _("Unknown") . ']' : $keyid) . "\n"
. $leftrow[9] . (empty($fingerprint) ? '[' . _("Unknown") . ']' : $fingerprint) . "\n\n";
}
return $msg;
}
public function getKeyIdString($keyid)
{
/* Get the 8 character key ID string. */
if (strpos($keyid, '0x') === 0) {
$keyid = substr($keyid, 2);
}
if (strlen($keyid) > 8) {
$keyid = substr($keyid, -8);
}
return '0x' . $keyid;
}
/**
* Returns whether a text has been encrypted symmetrically.
*
* @todo Return null, instead of exception, if tools are not available to
* determine whether data was encrypted symmetrically.
*
* @param string $text The PGP encrypted text.
*
* @return boolean True if the text is symmetrically encrypted.
* @throws Horde_Crypt_Exception
*/
public function encryptedSymmetrically($text)
{
return $this->_pgp->encryptedSymmetrically($text);
}
/**
* Returns information on a PGP data block.
*
* @param string $pgpdata The PGP data block.
*
* @return array An array with information on the PGP data block. If an
* element is not present in the data block, it will
* likewise not be set in the array.
* <pre>
* Array Format:
* -------------
* [public_key]/[secret_key] => Array
* (
* [created] => Key creation - UNIX timestamp
* [expires] => Key expiration - UNIX timestamp (0 = never expires)
* [size] => Size of the key in bits
* )
*
* [keyid] => Key ID of the PGP data (if available)
* 16-bit hex value
*
* [signature] => Array (
* [id{n}/'_SIGNATURE'] => Array (
* [name] => Full Name
* [comment] => Comment
* [email] => E-mail Address
* [keyid] => 16-bit hex value
* [created] => Signature creation - UNIX timestamp
* [expires] => Signature expiration - UNIX timestamp
* [micalg] => The hash used to create the signature
* [sig_{hex}] => Array [details of a sig verifying the ID] (
* [created] => Signature creation - UNIX timestamp
* [expires] => Signature expiration - UNIX timestamp
* [keyid] => 16-bit hex value
* [micalg] => The hash used to create the signature
* )
* )
* )
* </pre>
*
* Each user ID will be stored in the array 'signature' and have data
* associated with it, including an array for information on each
* signature that has signed that UID. Signatures not associated with a
* UID (e.g. revocation signatures and sub keys) will be stored under the
* special keyword '_SIGNATURE'.
*/
public function pgpPacketInformation($pgpdata)
{
return $this->_pgp->pgpPacketInformation($pgpdata);
}
/**
* Returns only information on the first ID that matches the email address
* input.
*
* @param string $pgpdata The PGP data block.
* @param string $email An e-mail address.
*
* @return array An array with information on the PGP data block. If an
* element is not present in the data block, it will
* likewise not be set in the array. Array elements:
* - comment: Comment
* - created: Signature creation (UNIX timestamp)
* - email: E-mail Address
* - key_created: Key creation (UNIX timestamp)
* - key_expires: Key expiration (UNIX timestamp; 0 = never expires)
* - key_size: Size of the key in bits
* - key_type: The key type (public_key or secret_key)
* - keyid: 16-bit hex value
* - micalg: The hash used to create the signature
* - name: Full Name
*/
public function pgpPacketSignature($pgpdata, $email)
{
return $this->_pgp->pgpPacketSignature($pgpdata, $email);
}
/**
* Return list of keyserver objects.
*
* @return array List of Horde_Crypt_Pgp_Keyserver objects.
* @throws Horde_Crypt_Exception
*/
protected function _keyserverList()
{
global $conf, $injector;
if (empty($conf['gnupg']['keyserver'])) {
throw new Horde_Crypt_Exception(_("Public PGP keyserver support has been disabled."));
}
$http = $injector->getInstance('Horde_Core_Factory_HttpClient')->create();
if (!empty($conf['gnupg']['timeout'])) {
$http->{'request.timeout'} = $conf['gnupg']['timeout'];
}
$out = array();
foreach ($conf['gnupg']['keyserver'] as $server) {
$out[] = new Horde_Crypt_Pgp_Keyserver($this, array(
'http' => $http,
'keyserver' => 'http://' . $server
));
}
return $out;
}
}