| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * A class representing GPG signatures |
|---|
| 7 | * |
|---|
| 8 | * This file contains a data class representing a GPG signature. |
|---|
| 9 | * |
|---|
| 10 | * PHP version 5 |
|---|
| 11 | * |
|---|
| 12 | * LICENSE: |
|---|
| 13 | * |
|---|
| 14 | * This library is free software; you can redistribute it and/or modify |
|---|
| 15 | * it under the terms of the GNU Lesser General Public License as |
|---|
| 16 | * published by the Free Software Foundation; either version 2.1 of the |
|---|
| 17 | * License, or (at your option) any later version. |
|---|
| 18 | * |
|---|
| 19 | * This library is distributed in the hope that it will be useful, |
|---|
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 22 | * Lesser General Public License for more details. |
|---|
| 23 | * |
|---|
| 24 | * You should have received a copy of the GNU Lesser General Public |
|---|
| 25 | * License along with this library; if not, write to the Free Software |
|---|
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 27 | * |
|---|
| 28 | * @category Encryption |
|---|
| 29 | * @package Crypt_GPG |
|---|
| 30 | * @author Nathan Fredrickson <nathan@silverorange.com> |
|---|
| 31 | * @copyright 2005-2010 silverorange |
|---|
| 32 | * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
|---|
| 33 | * @version CVS: $Id: Signature.php 302773 2010-08-25 14:16:28Z gauthierm $ |
|---|
| 34 | * @link http://pear.php.net/package/Crypt_GPG |
|---|
| 35 | */ |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * User id class definition |
|---|
| 39 | */ |
|---|
| 40 | require_once 'Crypt/GPG/UserId.php'; |
|---|
| 41 | |
|---|
| 42 | // {{{ class Crypt_GPG_Signature |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * A class for GPG signature information |
|---|
| 46 | * |
|---|
| 47 | * This class is used to store the results of the Crypt_GPG::verify() method. |
|---|
| 48 | * |
|---|
| 49 | * @category Encryption |
|---|
| 50 | * @package Crypt_GPG |
|---|
| 51 | * @author Nathan Fredrickson <nathan@silverorange.com> |
|---|
| 52 | * @author Michael Gauthier <mike@silverorange.com> |
|---|
| 53 | * @copyright 2005-2010 silverorange |
|---|
| 54 | * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
|---|
| 55 | * @link http://pear.php.net/package/Crypt_GPG |
|---|
| 56 | * @see Crypt_GPG::verify() |
|---|
| 57 | */ |
|---|
| 58 | class Crypt_GPG_Signature |
|---|
| 59 | { |
|---|
| 60 | // {{{ class properties |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * A base64-encoded string containing a unique id for this signature if |
|---|
| 64 | * this signature has been verified as ok |
|---|
| 65 | * |
|---|
| 66 | * This id is used to prevent replay attacks and is not present for all |
|---|
| 67 | * types of signatures. |
|---|
| 68 | * |
|---|
| 69 | * @var string |
|---|
| 70 | */ |
|---|
| 71 | private $_id = ''; |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * The fingerprint of the key used to create the signature |
|---|
| 75 | * |
|---|
| 76 | * @var string |
|---|
| 77 | */ |
|---|
| 78 | private $_keyFingerprint = ''; |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * The id of the key used to create the signature |
|---|
| 82 | * |
|---|
| 83 | * @var string |
|---|
| 84 | */ |
|---|
| 85 | private $_keyId = ''; |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * The creation date of this signature |
|---|
| 89 | * |
|---|
| 90 | * This is a Unix timestamp. |
|---|
| 91 | * |
|---|
| 92 | * @var integer |
|---|
| 93 | */ |
|---|
| 94 | private $_creationDate = 0; |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * The expiration date of the signature |
|---|
| 98 | * |
|---|
| 99 | * This is a Unix timestamp. If this signature does not expire, this will |
|---|
| 100 | * be zero. |
|---|
| 101 | * |
|---|
| 102 | * @var integer |
|---|
| 103 | */ |
|---|
| 104 | private $_expirationDate = 0; |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * The user id associated with this signature |
|---|
| 108 | * |
|---|
| 109 | * @var Crypt_GPG_UserId |
|---|
| 110 | */ |
|---|
| 111 | private $_userId = null; |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Whether or not this signature is valid |
|---|
| 115 | * |
|---|
| 116 | * @var boolean |
|---|
| 117 | */ |
|---|
| 118 | private $_isValid = false; |
|---|
| 119 | |
|---|
| 120 | // }}} |
|---|
| 121 | // {{{ __construct() |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Creates a new signature |
|---|
| 125 | * |
|---|
| 126 | * Signatures can be initialized from an array of named values. Available |
|---|
| 127 | * names are: |
|---|
| 128 | * |
|---|
| 129 | * - <kbd>string id</kbd> - the unique id of this signature. |
|---|
| 130 | * - <kbd>string fingerprint</kbd> - the fingerprint of the key used to |
|---|
| 131 | * create the signature. The fingerprint |
|---|
| 132 | * should not contain formatting |
|---|
| 133 | * characters. |
|---|
| 134 | * - <kbd>string keyId</kbd> - the id of the key used to create the |
|---|
| 135 | * the signature. |
|---|
| 136 | * - <kbd>integer creation</kbd> - the date the signature was created. |
|---|
| 137 | * This is a UNIX timestamp. |
|---|
| 138 | * - <kbd>integer expiration</kbd> - the date the signature expired. This |
|---|
| 139 | * is a UNIX timestamp. If the signature |
|---|
| 140 | * does not expire, use 0. |
|---|
| 141 | * - <kbd>boolean valid</kbd> - whether or not the signature is valid. |
|---|
| 142 | * - <kbd>string userId</kbd> - the user id associated with the |
|---|
| 143 | * signature. This may also be a |
|---|
| 144 | * {@link Crypt_GPG_UserId} object. |
|---|
| 145 | * |
|---|
| 146 | * @param Crypt_GPG_Signature|array $signature optional. Either an existing |
|---|
| 147 | * signature object, which is copied; or an array of initial values. |
|---|
| 148 | */ |
|---|
| 149 | public function __construct($signature = null) |
|---|
| 150 | { |
|---|
| 151 | // copy from object |
|---|
| 152 | if ($signature instanceof Crypt_GPG_Signature) { |
|---|
| 153 | $this->_id = $signature->_id; |
|---|
| 154 | $this->_keyFingerprint = $signature->_keyFingerprint; |
|---|
| 155 | $this->_keyId = $signature->_keyId; |
|---|
| 156 | $this->_creationDate = $signature->_creationDate; |
|---|
| 157 | $this->_expirationDate = $signature->_expirationDate; |
|---|
| 158 | $this->_isValid = $signature->_isValid; |
|---|
| 159 | |
|---|
| 160 | if ($signature->_userId instanceof Crypt_GPG_UserId) { |
|---|
| 161 | $this->_userId = clone $signature->_userId; |
|---|
| 162 | } else { |
|---|
| 163 | $this->_userId = $signature->_userId; |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | // initialize from array |
|---|
| 168 | if (is_array($signature)) { |
|---|
| 169 | if (array_key_exists('id', $signature)) { |
|---|
| 170 | $this->setId($signature['id']); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | if (array_key_exists('fingerprint', $signature)) { |
|---|
| 174 | $this->setKeyFingerprint($signature['fingerprint']); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | if (array_key_exists('keyId', $signature)) { |
|---|
| 178 | $this->setKeyId($signature['keyId']); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | if (array_key_exists('creation', $signature)) { |
|---|
| 182 | $this->setCreationDate($signature['creation']); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | if (array_key_exists('expiration', $signature)) { |
|---|
| 186 | $this->setExpirationDate($signature['expiration']); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | if (array_key_exists('valid', $signature)) { |
|---|
| 190 | $this->setValid($signature['valid']); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | if (array_key_exists('userId', $signature)) { |
|---|
| 194 | $userId = new Crypt_GPG_UserId($signature['userId']); |
|---|
| 195 | $this->setUserId($userId); |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | // }}} |
|---|
| 201 | // {{{ getId() |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | * Gets the id of this signature |
|---|
| 205 | * |
|---|
| 206 | * @return string a base64-encoded string containing a unique id for this |
|---|
| 207 | * signature. This id is used to prevent replay attacks and |
|---|
| 208 | * is not present for all types of signatures. |
|---|
| 209 | */ |
|---|
| 210 | public function getId() |
|---|
| 211 | { |
|---|
| 212 | return $this->_id; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | // }}} |
|---|
| 216 | // {{{ getKeyFingerprint() |
|---|
| 217 | |
|---|
| 218 | /** |
|---|
| 219 | * Gets the fingerprint of the key used to create this signature |
|---|
| 220 | * |
|---|
| 221 | * @return string the fingerprint of the key used to create this signature. |
|---|
| 222 | */ |
|---|
| 223 | public function getKeyFingerprint() |
|---|
| 224 | { |
|---|
| 225 | return $this->_keyFingerprint; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | // }}} |
|---|
| 229 | // {{{ getKeyId() |
|---|
| 230 | |
|---|
| 231 | /** |
|---|
| 232 | * Gets the id of the key used to create this signature |
|---|
| 233 | * |
|---|
| 234 | * Whereas the fingerprint of the signing key may not always be available |
|---|
| 235 | * (for example if the signature is bad), the id should always be |
|---|
| 236 | * available. |
|---|
| 237 | * |
|---|
| 238 | * @return string the id of the key used to create this signature. |
|---|
| 239 | */ |
|---|
| 240 | public function getKeyId() |
|---|
| 241 | { |
|---|
| 242 | return $this->_keyId; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | // }}} |
|---|
| 246 | // {{{ getCreationDate() |
|---|
| 247 | |
|---|
| 248 | /** |
|---|
| 249 | * Gets the creation date of this signature |
|---|
| 250 | * |
|---|
| 251 | * @return integer the creation date of this signature. This is a Unix |
|---|
| 252 | * timestamp. |
|---|
| 253 | */ |
|---|
| 254 | public function getCreationDate() |
|---|
| 255 | { |
|---|
| 256 | return $this->_creationDate; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | // }}} |
|---|
| 260 | // {{{ getExpirationDate() |
|---|
| 261 | |
|---|
| 262 | /** |
|---|
| 263 | * Gets the expiration date of the signature |
|---|
| 264 | * |
|---|
| 265 | * @return integer the expiration date of this signature. This is a Unix |
|---|
| 266 | * timestamp. If this signature does not expire, this will |
|---|
| 267 | * be zero. |
|---|
| 268 | */ |
|---|
| 269 | public function getExpirationDate() |
|---|
| 270 | { |
|---|
| 271 | return $this->_expirationDate; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | // }}} |
|---|
| 275 | // {{{ getUserId() |
|---|
| 276 | |
|---|
| 277 | /** |
|---|
| 278 | * Gets the user id associated with this signature |
|---|
| 279 | * |
|---|
| 280 | * @return Crypt_GPG_UserId the user id associated with this signature. |
|---|
| 281 | */ |
|---|
| 282 | public function getUserId() |
|---|
| 283 | { |
|---|
| 284 | return $this->_userId; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | // }}} |
|---|
| 288 | // {{{ isValid() |
|---|
| 289 | |
|---|
| 290 | /** |
|---|
| 291 | * Gets whether or no this signature is valid |
|---|
| 292 | * |
|---|
| 293 | * @return boolean true if this signature is valid and false if it is not. |
|---|
| 294 | */ |
|---|
| 295 | public function isValid() |
|---|
| 296 | { |
|---|
| 297 | return $this->_isValid; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | // }}} |
|---|
| 301 | // {{{ setId() |
|---|
| 302 | |
|---|
| 303 | /** |
|---|
| 304 | * Sets the id of this signature |
|---|
| 305 | * |
|---|
| 306 | * @param string $id a base64-encoded string containing a unique id for |
|---|
| 307 | * this signature. |
|---|
| 308 | * |
|---|
| 309 | * @return Crypt_GPG_Signature the current object, for fluent interface. |
|---|
| 310 | * |
|---|
| 311 | * @see Crypt_GPG_Signature::getId() |
|---|
| 312 | */ |
|---|
| 313 | public function setId($id) |
|---|
| 314 | { |
|---|
| 315 | $this->_id = strval($id); |
|---|
| 316 | return $this; |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | // }}} |
|---|
| 320 | // {{{ setKeyFingerprint() |
|---|
| 321 | |
|---|
| 322 | /** |
|---|
| 323 | * Sets the key fingerprint of this signature |
|---|
| 324 | * |
|---|
| 325 | * @param string $fingerprint the key fingerprint of this signature. This |
|---|
| 326 | * is the fingerprint of the primary key used to |
|---|
| 327 | * create this signature. |
|---|
| 328 | * |
|---|
| 329 | * @return Crypt_GPG_Signature the current object, for fluent interface. |
|---|
| 330 | */ |
|---|
| 331 | public function setKeyFingerprint($fingerprint) |
|---|
| 332 | { |
|---|
| 333 | $this->_keyFingerprint = strval($fingerprint); |
|---|
| 334 | return $this; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | // }}} |
|---|
| 338 | // {{{ setKeyId() |
|---|
| 339 | |
|---|
| 340 | /** |
|---|
| 341 | * Sets the key id of this signature |
|---|
| 342 | * |
|---|
| 343 | * @param string $id the key id of this signature. This is the id of the |
|---|
| 344 | * primary key used to create this signature. |
|---|
| 345 | * |
|---|
| 346 | * @return Crypt_GPG_Signature the current object, for fluent interface. |
|---|
| 347 | */ |
|---|
| 348 | public function setKeyId($id) |
|---|
| 349 | { |
|---|
| 350 | $this->_keyId = strval($id); |
|---|
| 351 | return $this; |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | // }}} |
|---|
| 355 | // {{{ setCreationDate() |
|---|
| 356 | |
|---|
| 357 | /** |
|---|
| 358 | * Sets the creation date of this signature |
|---|
| 359 | * |
|---|
| 360 | * @param integer $creationDate the creation date of this signature. This |
|---|
| 361 | * is a Unix timestamp. |
|---|
| 362 | * |
|---|
| 363 | * @return Crypt_GPG_Signature the current object, for fluent interface. |
|---|
| 364 | */ |
|---|
| 365 | public function setCreationDate($creationDate) |
|---|
| 366 | { |
|---|
| 367 | $this->_creationDate = intval($creationDate); |
|---|
| 368 | return $this; |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | // }}} |
|---|
| 372 | // {{{ setExpirationDate() |
|---|
| 373 | |
|---|
| 374 | /** |
|---|
| 375 | * Sets the expiration date of this signature |
|---|
| 376 | * |
|---|
| 377 | * @param integer $expirationDate the expiration date of this signature. |
|---|
| 378 | * This is a Unix timestamp. Specify zero if |
|---|
| 379 | * this signature does not expire. |
|---|
| 380 | * |
|---|
| 381 | * @return Crypt_GPG_Signature the current object, for fluent interface. |
|---|
| 382 | */ |
|---|
| 383 | public function setExpirationDate($expirationDate) |
|---|
| 384 | { |
|---|
| 385 | $this->_expirationDate = intval($expirationDate); |
|---|
| 386 | return $this; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | // }}} |
|---|
| 390 | // {{{ setUserId() |
|---|
| 391 | |
|---|
| 392 | /** |
|---|
| 393 | * Sets the user id associated with this signature |
|---|
| 394 | * |
|---|
| 395 | * @param Crypt_GPG_UserId $userId the user id associated with this |
|---|
| 396 | * signature. |
|---|
| 397 | * |
|---|
| 398 | * @return Crypt_GPG_Signature the current object, for fluent interface. |
|---|
| 399 | */ |
|---|
| 400 | public function setUserId(Crypt_GPG_UserId $userId) |
|---|
| 401 | { |
|---|
| 402 | $this->_userId = $userId; |
|---|
| 403 | return $this; |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | // }}} |
|---|
| 407 | // {{{ setValid() |
|---|
| 408 | |
|---|
| 409 | /** |
|---|
| 410 | * Sets whether or not this signature is valid |
|---|
| 411 | * |
|---|
| 412 | * @param boolean $isValid true if this signature is valid and false if it |
|---|
| 413 | * is not. |
|---|
| 414 | * |
|---|
| 415 | * @return Crypt_GPG_Signature the current object, for fluent interface. |
|---|
| 416 | */ |
|---|
| 417 | public function setValid($isValid) |
|---|
| 418 | { |
|---|
| 419 | $this->_isValid = ($isValid) ? true : false; |
|---|
| 420 | return $this; |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | // }}} |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | // }}} |
|---|
| 427 | |
|---|
| 428 | ?> |
|---|