Mercurial > hg > rc1
comparison plugins/enigma/lib/enigma_key.php @ 0:1e000243b222
vanilla 1.3.3 distro, I hope
| author | Charlie Root |
|---|---|
| date | Thu, 04 Jan 2018 15:50:29 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:1e000243b222 |
|---|---|
| 1 <?php | |
| 2 | |
| 3 /** | |
| 4 +-------------------------------------------------------------------------+ | |
| 5 | Key class for the Enigma Plugin | | |
| 6 | | | |
| 7 | Copyright (C) 2010-2015 The Roundcube Dev Team | | |
| 8 | | | |
| 9 | Licensed under the GNU General Public License version 3 or | | |
| 10 | any later version with exceptions for skins & plugins. | | |
| 11 | See the README file for a full license statement. | | |
| 12 | | | |
| 13 +-------------------------------------------------------------------------+ | |
| 14 | Author: Aleksander Machniak <alec@alec.pl> | | |
| 15 +-------------------------------------------------------------------------+ | |
| 16 */ | |
| 17 | |
| 18 class enigma_key | |
| 19 { | |
| 20 public $id; | |
| 21 public $name; | |
| 22 public $users = array(); | |
| 23 public $subkeys = array(); | |
| 24 public $reference; | |
| 25 public $password; | |
| 26 | |
| 27 const TYPE_UNKNOWN = 0; | |
| 28 const TYPE_KEYPAIR = 1; | |
| 29 const TYPE_PUBLIC = 2; | |
| 30 | |
| 31 const CAN_ENCRYPT = 1; | |
| 32 const CAN_SIGN = 2; | |
| 33 const CAN_CERTIFY = 4; | |
| 34 const CAN_AUTHENTICATE = 8; | |
| 35 | |
| 36 | |
| 37 /** | |
| 38 * Keys list sorting callback for usort() | |
| 39 */ | |
| 40 static function cmp($a, $b) | |
| 41 { | |
| 42 return strcmp($a->name, $b->name); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Returns key type | |
| 47 */ | |
| 48 function get_type() | |
| 49 { | |
| 50 if ($this->subkeys[0]->has_private) | |
| 51 return enigma_key::TYPE_KEYPAIR; | |
| 52 else if (!empty($this->subkeys[0])) | |
| 53 return enigma_key::TYPE_PUBLIC; | |
| 54 | |
| 55 return enigma_key::TYPE_UNKNOWN; | |
| 56 } | |
| 57 | |
| 58 /** | |
| 59 * Returns true if all user IDs are revoked | |
| 60 */ | |
| 61 function is_revoked() | |
| 62 { | |
| 63 foreach ($this->subkeys as $subkey) | |
| 64 if (!$subkey->revoked) | |
| 65 return false; | |
| 66 | |
| 67 return true; | |
| 68 } | |
| 69 | |
| 70 /** | |
| 71 * Returns true if any user ID is valid | |
| 72 */ | |
| 73 function is_valid() | |
| 74 { | |
| 75 foreach ($this->users as $user) | |
| 76 if ($user->valid) | |
| 77 return true; | |
| 78 | |
| 79 return false; | |
| 80 } | |
| 81 | |
| 82 /** | |
| 83 * Returns true if any of subkeys is not expired | |
| 84 */ | |
| 85 function is_expired() | |
| 86 { | |
| 87 $now = time(); | |
| 88 | |
| 89 foreach ($this->subkeys as $subkey) | |
| 90 if (!$subkey->expires || $subkey->expires > $now) | |
| 91 return true; | |
| 92 | |
| 93 return false; | |
| 94 } | |
| 95 | |
| 96 /** | |
| 97 * Returns true if any of subkeys is a private key | |
| 98 */ | |
| 99 function is_private() | |
| 100 { | |
| 101 $now = time(); | |
| 102 | |
| 103 foreach ($this->subkeys as $subkey) | |
| 104 if ($subkey->has_private) | |
| 105 return true; | |
| 106 | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 /** | |
| 111 * Get key ID by user email | |
| 112 */ | |
| 113 function find_subkey($email, $mode) | |
| 114 { | |
| 115 $now = time(); | |
| 116 | |
| 117 foreach ($this->users as $user) { | |
| 118 if (strcasecmp($user->email, $email) === 0 && $user->valid && !$user->revoked) { | |
| 119 foreach ($this->subkeys as $subkey) { | |
| 120 if (!$subkey->revoked && (!$subkey->expires || $subkey->expires > $now)) { | |
| 121 if ($subkey->usage & $mode) { | |
| 122 return $subkey; | |
| 123 } | |
| 124 } | |
| 125 } | |
| 126 } | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 /** | |
| 131 * Converts long ID or Fingerprint to short ID | |
| 132 * Crypt_GPG uses internal, but e.g. Thunderbird's Enigmail displays short ID | |
| 133 * | |
| 134 * @param string Key ID or fingerprint | |
| 135 * @return string Key short ID | |
| 136 */ | |
| 137 static function format_id($id) | |
| 138 { | |
| 139 // E.g. 04622F2089E037A5 => 89E037A5 | |
| 140 | |
| 141 return substr($id, -8); | |
| 142 } | |
| 143 | |
| 144 /** | |
| 145 * Formats fingerprint string | |
| 146 * | |
| 147 * @param string Key fingerprint | |
| 148 * | |
| 149 * @return string Formatted fingerprint (with spaces) | |
| 150 */ | |
| 151 static function format_fingerprint($fingerprint) | |
| 152 { | |
| 153 if (!$fingerprint) { | |
| 154 return ''; | |
| 155 } | |
| 156 | |
| 157 $result = ''; | |
| 158 for ($i=0; $i<40; $i++) { | |
| 159 if ($i % 4 == 0) { | |
| 160 $result .= ' '; | |
| 161 } | |
| 162 $result .= $fingerprint[$i]; | |
| 163 } | |
| 164 | |
| 165 return $result; | |
| 166 } | |
| 167 } |
