| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | +-------------------------------------------------------------------------+ |
|---|
| 4 | | Key class for the Enigma Plugin | |
|---|
| 5 | | | |
|---|
| 6 | | This program is free software; you can redistribute it and/or modify | |
|---|
| 7 | | it under the terms of the GNU General Public License version 2 | |
|---|
| 8 | | as published by the Free Software Foundation. | |
|---|
| 9 | | | |
|---|
| 10 | | This program is distributed in the hope that it will be useful, | |
|---|
| 11 | | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|---|
| 12 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|---|
| 13 | | GNU General Public License for more details. | |
|---|
| 14 | | | |
|---|
| 15 | | You should have received a copy of the GNU General Public License along | |
|---|
| 16 | | with this program; if not, write to the Free Software Foundation, Inc., | |
|---|
| 17 | | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
|---|
| 18 | | | |
|---|
| 19 | +-------------------------------------------------------------------------+ |
|---|
| 20 | | Author: Aleksander Machniak <alec@alec.pl> | |
|---|
| 21 | +-------------------------------------------------------------------------+ |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | class enigma_key |
|---|
| 25 | { |
|---|
| 26 | public $id; |
|---|
| 27 | public $name; |
|---|
| 28 | public $users = array(); |
|---|
| 29 | public $subkeys = array(); |
|---|
| 30 | |
|---|
| 31 | const TYPE_UNKNOWN = 0; |
|---|
| 32 | const TYPE_KEYPAIR = 1; |
|---|
| 33 | const TYPE_PUBLIC = 2; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Keys list sorting callback for usort() |
|---|
| 37 | */ |
|---|
| 38 | static function cmp($a, $b) |
|---|
| 39 | { |
|---|
| 40 | return strcmp($a->name, $b->name); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Returns key type |
|---|
| 45 | */ |
|---|
| 46 | function get_type() |
|---|
| 47 | { |
|---|
| 48 | if ($this->subkeys[0]->has_private) |
|---|
| 49 | return enigma_key::TYPE_KEYPAIR; |
|---|
| 50 | else if (!empty($this->subkeys[0])) |
|---|
| 51 | return enigma_key::TYPE_PUBLIC; |
|---|
| 52 | |
|---|
| 53 | return enigma_key::TYPE_UNKNOWN; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Returns true if all user IDs are revoked |
|---|
| 58 | */ |
|---|
| 59 | function is_revoked() |
|---|
| 60 | { |
|---|
| 61 | foreach ($this->subkeys as $subkey) |
|---|
| 62 | if (!$subkey->revoked) |
|---|
| 63 | return false; |
|---|
| 64 | |
|---|
| 65 | return true; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Returns true if any user ID is valid |
|---|
| 70 | */ |
|---|
| 71 | function is_valid() |
|---|
| 72 | { |
|---|
| 73 | foreach ($this->users as $user) |
|---|
| 74 | if ($user->valid) |
|---|
| 75 | return true; |
|---|
| 76 | |
|---|
| 77 | return false; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * Returns true if any of subkeys is not expired |
|---|
| 82 | */ |
|---|
| 83 | function is_expired() |
|---|
| 84 | { |
|---|
| 85 | $now = time(); |
|---|
| 86 | |
|---|
| 87 | foreach ($this->subkeys as $subkey) |
|---|
| 88 | if (!$subkey->expires || $subkey->expires > $now) |
|---|
| 89 | return true; |
|---|
| 90 | |
|---|
| 91 | return false; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Converts long ID or Fingerprint to short ID |
|---|
| 96 | * Crypt_GPG uses internal, but e.g. Thunderbird's Enigmail displays short ID |
|---|
| 97 | * |
|---|
| 98 | * @param string Key ID or fingerprint |
|---|
| 99 | * @return string Key short ID |
|---|
| 100 | */ |
|---|
| 101 | static function format_id($id) |
|---|
| 102 | { |
|---|
| 103 | // E.g. 04622F2089E037A5 => 89E037A5 |
|---|
| 104 | |
|---|
| 105 | return substr($id, -8); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * Formats fingerprint string |
|---|
| 110 | * |
|---|
| 111 | * @param string Key fingerprint |
|---|
| 112 | * |
|---|
| 113 | * @return string Formatted fingerprint (with spaces) |
|---|
| 114 | */ |
|---|
| 115 | static function format_fingerprint($fingerprint) |
|---|
| 116 | { |
|---|
| 117 | if (!$fingerprint) |
|---|
| 118 | return ''; |
|---|
| 119 | |
|---|
| 120 | $result = ''; |
|---|
| 121 | for ($i=0; $i<40; $i++) { |
|---|
| 122 | if ($i % 4 == 0) |
|---|
| 123 | $result .= ' '; |
|---|
| 124 | $result .= $fingerprint[$i]; |
|---|
| 125 | } |
|---|
| 126 | return $result; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | } |
|---|