You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Key.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Contains a class representing GPG keys
  5. *
  6. * PHP version 5
  7. *
  8. * LICENSE:
  9. *
  10. * This library is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as
  12. * published by the Free Software Foundation; either version 2.1 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, see
  22. * <http://www.gnu.org/licenses/>
  23. *
  24. * @category Encryption
  25. * @package Crypt_GPG
  26. * @author Michael Gauthier <mike@silverorange.com>
  27. * @copyright 2008-2010 silverorange
  28. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  29. * @link http://pear.php.net/package/Crypt_GPG
  30. */
  31. /**
  32. * Sub-key class definition
  33. */
  34. require_once 'Crypt/GPG/SubKey.php';
  35. /**
  36. * User id class definition
  37. */
  38. require_once 'Crypt/GPG/UserId.php';
  39. // {{{ class Crypt_GPG_Key
  40. /**
  41. * A data class for GPG key information
  42. *
  43. * This class is used to store the results of the {@link Crypt_GPG::getKeys()}
  44. * method.
  45. *
  46. * @category Encryption
  47. * @package Crypt_GPG
  48. * @author Michael Gauthier <mike@silverorange.com>
  49. * @copyright 2008-2010 silverorange
  50. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  51. * @link http://pear.php.net/package/Crypt_GPG
  52. * @see Crypt_GPG::getKeys()
  53. */
  54. class Crypt_GPG_Key
  55. {
  56. // {{{ class properties
  57. /**
  58. * The user ids associated with this key
  59. *
  60. * This is an array of {@link Crypt_GPG_UserId} objects.
  61. *
  62. * @var array
  63. *
  64. * @see Crypt_GPG_Key::addUserId()
  65. * @see Crypt_GPG_Key::getUserIds()
  66. */
  67. private $_userIds = array();
  68. /**
  69. * The subkeys of this key
  70. *
  71. * This is an array of {@link Crypt_GPG_SubKey} objects.
  72. *
  73. * @var array
  74. *
  75. * @see Crypt_GPG_Key::addSubKey()
  76. * @see Crypt_GPG_Key::getSubKeys()
  77. */
  78. private $_subKeys = array();
  79. // }}}
  80. // {{{ getSubKeys()
  81. /**
  82. * Gets the sub-keys of this key
  83. *
  84. * @return array the sub-keys of this key.
  85. *
  86. * @see Crypt_GPG_Key::addSubKey()
  87. */
  88. public function getSubKeys()
  89. {
  90. return $this->_subKeys;
  91. }
  92. // }}}
  93. // {{{ getUserIds()
  94. /**
  95. * Gets the user ids of this key
  96. *
  97. * @return array the user ids of this key.
  98. *
  99. * @see Crypt_GPG_Key::addUserId()
  100. */
  101. public function getUserIds()
  102. {
  103. return $this->_userIds;
  104. }
  105. // }}}
  106. // {{{ getPrimaryKey()
  107. /**
  108. * Gets the primary sub-key of this key
  109. *
  110. * The primary key is the first added sub-key.
  111. *
  112. * @return Crypt_GPG_SubKey the primary sub-key of this key.
  113. */
  114. public function getPrimaryKey()
  115. {
  116. $primary_key = null;
  117. if (count($this->_subKeys) > 0) {
  118. $primary_key = $this->_subKeys[0];
  119. }
  120. return $primary_key;
  121. }
  122. // }}}
  123. // {{{ canSign()
  124. /**
  125. * Gets whether or not this key can sign data
  126. *
  127. * This key can sign data if any sub-key of this key can sign data.
  128. *
  129. * @return boolean true if this key can sign data and false if this key
  130. * cannot sign data.
  131. */
  132. public function canSign()
  133. {
  134. $canSign = false;
  135. foreach ($this->_subKeys as $subKey) {
  136. if ($subKey->canSign()) {
  137. $canSign = true;
  138. break;
  139. }
  140. }
  141. return $canSign;
  142. }
  143. // }}}
  144. // {{{ canEncrypt()
  145. /**
  146. * Gets whether or not this key can encrypt data
  147. *
  148. * This key can encrypt data if any sub-key of this key can encrypt data.
  149. *
  150. * @return boolean true if this key can encrypt data and false if this
  151. * key cannot encrypt data.
  152. */
  153. public function canEncrypt()
  154. {
  155. $canEncrypt = false;
  156. foreach ($this->_subKeys as $subKey) {
  157. if ($subKey->canEncrypt()) {
  158. $canEncrypt = true;
  159. break;
  160. }
  161. }
  162. return $canEncrypt;
  163. }
  164. // }}}
  165. // {{{ addSubKey()
  166. /**
  167. * Adds a sub-key to this key
  168. *
  169. * The first added sub-key will be the primary key of this key.
  170. *
  171. * @param Crypt_GPG_SubKey $subKey the sub-key to add.
  172. *
  173. * @return Crypt_GPG_Key the current object, for fluent interface.
  174. */
  175. public function addSubKey(Crypt_GPG_SubKey $subKey)
  176. {
  177. $this->_subKeys[] = $subKey;
  178. return $this;
  179. }
  180. // }}}
  181. // {{{ addUserId()
  182. /**
  183. * Adds a user id to this key
  184. *
  185. * @param Crypt_GPG_UserId $userId the user id to add.
  186. *
  187. * @return Crypt_GPG_Key the current object, for fluent interface.
  188. */
  189. public function addUserId(Crypt_GPG_UserId $userId)
  190. {
  191. $this->_userIds[] = $userId;
  192. return $this;
  193. }
  194. // }}}
  195. // {{{ __toString()
  196. /**
  197. * String representation of the key
  198. *
  199. * @return string The key ID.
  200. */
  201. public function __toString()
  202. {
  203. foreach ($this->_subKeys as $subKey) {
  204. if ($id = $subKey->getId()) {
  205. return $id;
  206. }
  207. }
  208. return '';
  209. }
  210. // }}}
  211. }
  212. // }}}
  213. ?>