3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * A class representing GPG signatures
8 * This file contains a data class representing a GPG signature.
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.
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.
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
28 * @category Encryption
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
38 * User id class definition
40 require_once 'Crypt/GPG/UserId.php';
42 // {{{ class Crypt_GPG_Signature
45 * A class for GPG signature information
47 * This class is used to store the results of the Crypt_GPG::verify() method.
49 * @category Encryption
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()
58 class Crypt_GPG_Signature
60 // {{{ class properties
63 * A base64-encoded string containing a unique id for this signature if
64 * this signature has been verified as ok
66 * This id is used to prevent replay attacks and is not present for all
67 * types of signatures.
74 * The fingerprint of the key used to create the signature
78 private $_keyFingerprint = '';
81 * The id of the key used to create the signature
88 * The creation date of this signature
90 * This is a Unix timestamp.
94 private $_creationDate = 0;
97 * The expiration date of the signature
99 * This is a Unix timestamp. If this signature does not expire, this will
104 private $_expirationDate = 0;
107 * The user id associated with this signature
109 * @var Crypt_GPG_UserId
111 private $_userId = null;
114 * Whether or not this signature is valid
118 private $_isValid = false;
124 * Creates a new signature
126 * Signatures can be initialized from an array of named values. Available
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
134 * - <kbd>string keyId</kbd> - the id of the key used to create the
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.
146 * @param Crypt_GPG_Signature|array $signature optional. Either an existing
147 * signature object, which is copied; or an array of initial values.
149 public function __construct($signature = null)
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;
160 if ($signature->_userId instanceof Crypt_GPG_UserId) {
161 $this->_userId = clone $signature->_userId;
163 $this->_userId = $signature->_userId;
167 // initialize from array
168 if (is_array($signature)) {
169 if (array_key_exists('id', $signature)) {
170 $this->setId($signature['id']);
173 if (array_key_exists('fingerprint', $signature)) {
174 $this->setKeyFingerprint($signature['fingerprint']);
177 if (array_key_exists('keyId', $signature)) {
178 $this->setKeyId($signature['keyId']);
181 if (array_key_exists('creation', $signature)) {
182 $this->setCreationDate($signature['creation']);
185 if (array_key_exists('expiration', $signature)) {
186 $this->setExpirationDate($signature['expiration']);
189 if (array_key_exists('valid', $signature)) {
190 $this->setValid($signature['valid']);
193 if (array_key_exists('userId', $signature)) {
194 $userId = new Crypt_GPG_UserId($signature['userId']);
195 $this->setUserId($userId);
204 * Gets the id of this signature
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.
210 public function getId()
216 // {{{ getKeyFingerprint()
219 * Gets the fingerprint of the key used to create this signature
221 * @return string the fingerprint of the key used to create this signature.
223 public function getKeyFingerprint()
225 return $this->_keyFingerprint;
232 * Gets the id of the key used to create this signature
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
238 * @return string the id of the key used to create this signature.
240 public function getKeyId()
242 return $this->_keyId;
246 // {{{ getCreationDate()
249 * Gets the creation date of this signature
251 * @return integer the creation date of this signature. This is a Unix
254 public function getCreationDate()
256 return $this->_creationDate;
260 // {{{ getExpirationDate()
263 * Gets the expiration date of the signature
265 * @return integer the expiration date of this signature. This is a Unix
266 * timestamp. If this signature does not expire, this will
269 public function getExpirationDate()
271 return $this->_expirationDate;
278 * Gets the user id associated with this signature
280 * @return Crypt_GPG_UserId the user id associated with this signature.
282 public function getUserId()
284 return $this->_userId;
291 * Gets whether or no this signature is valid
293 * @return boolean true if this signature is valid and false if it is not.
295 public function isValid()
297 return $this->_isValid;
304 * Sets the id of this signature
306 * @param string $id a base64-encoded string containing a unique id for
309 * @return Crypt_GPG_Signature the current object, for fluent interface.
311 * @see Crypt_GPG_Signature::getId()
313 public function setId($id)
315 $this->_id = strval($id);
320 // {{{ setKeyFingerprint()
323 * Sets the key fingerprint of this signature
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.
329 * @return Crypt_GPG_Signature the current object, for fluent interface.
331 public function setKeyFingerprint($fingerprint)
333 $this->_keyFingerprint = strval($fingerprint);
341 * Sets the key id of this signature
343 * @param string $id the key id of this signature. This is the id of the
344 * primary key used to create this signature.
346 * @return Crypt_GPG_Signature the current object, for fluent interface.
348 public function setKeyId($id)
350 $this->_keyId = strval($id);
355 // {{{ setCreationDate()
358 * Sets the creation date of this signature
360 * @param integer $creationDate the creation date of this signature. This
361 * is a Unix timestamp.
363 * @return Crypt_GPG_Signature the current object, for fluent interface.
365 public function setCreationDate($creationDate)
367 $this->_creationDate = intval($creationDate);
372 // {{{ setExpirationDate()
375 * Sets the expiration date of this signature
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.
381 * @return Crypt_GPG_Signature the current object, for fluent interface.
383 public function setExpirationDate($expirationDate)
385 $this->_expirationDate = intval($expirationDate);
393 * Sets the user id associated with this signature
395 * @param Crypt_GPG_UserId $userId the user id associated with this
398 * @return Crypt_GPG_Signature the current object, for fluent interface.
400 public function setUserId(Crypt_GPG_UserId $userId)
402 $this->_userId = $userId;
410 * Sets whether or not this signature is valid
412 * @param boolean $isValid true if this signature is valid and false if it
415 * @return Crypt_GPG_Signature the current object, for fluent interface.
417 public function setValid($isValid)
419 $this->_isValid = ($isValid) ? true : false;