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.

Exceptions.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Various exception handling classes for Crypt_GPG
  5. *
  6. * Crypt_GPG provides an object oriented interface to GNU Privacy
  7. * Guard (GPG). It requires the GPG executable to be on the system.
  8. *
  9. * This file contains various exception classes used by the Crypt_GPG package.
  10. *
  11. * PHP version 5
  12. *
  13. * LICENSE:
  14. *
  15. * This library is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU Lesser General Public License as
  17. * published by the Free Software Foundation; either version 2.1 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This library is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public
  26. * License along with this library; if not, see
  27. * <http://www.gnu.org/licenses/>
  28. *
  29. * @category Encryption
  30. * @package Crypt_GPG
  31. * @author Nathan Fredrickson <nathan@silverorange.com>
  32. * @author Michael Gauthier <mike@silverorange.com>
  33. * @copyright 2005-2011 silverorange
  34. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  35. * @link http://pear.php.net/package/Crypt_GPG
  36. */
  37. /**
  38. * PEAR Exception handler and base class
  39. */
  40. require_once 'PEAR/Exception.php';
  41. // {{{ class Crypt_GPG_Exception
  42. /**
  43. * An exception thrown by the Crypt_GPG package
  44. *
  45. * @category Encryption
  46. * @package Crypt_GPG
  47. * @author Michael Gauthier <mike@silverorange.com>
  48. * @copyright 2005 silverorange
  49. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  50. * @link http://pear.php.net/package/Crypt_GPG
  51. */
  52. class Crypt_GPG_Exception extends PEAR_Exception
  53. {
  54. }
  55. // }}}
  56. // {{{ class Crypt_GPG_FileException
  57. /**
  58. * An exception thrown when a file is used in ways it cannot be used
  59. *
  60. * For example, if an output file is specified and the file is not writeable, or
  61. * if an input file is specified and the file is not readable, this exception
  62. * is thrown.
  63. *
  64. * @category Encryption
  65. * @package Crypt_GPG
  66. * @author Michael Gauthier <mike@silverorange.com>
  67. * @copyright 2007-2008 silverorange
  68. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  69. * @link http://pear.php.net/package/Crypt_GPG
  70. */
  71. class Crypt_GPG_FileException extends Crypt_GPG_Exception
  72. {
  73. // {{{ private class properties
  74. /**
  75. * The name of the file that caused this exception
  76. *
  77. * @var string
  78. */
  79. private $_filename = '';
  80. // }}}
  81. // {{{ __construct()
  82. /**
  83. * Creates a new Crypt_GPG_FileException
  84. *
  85. * @param string $message an error message.
  86. * @param integer $code a user defined error code.
  87. * @param string $filename the name of the file that caused this exception.
  88. */
  89. public function __construct($message, $code = 0, $filename = '')
  90. {
  91. $this->_filename = $filename;
  92. parent::__construct($message, $code);
  93. }
  94. // }}}
  95. // {{{ getFilename()
  96. /**
  97. * Returns the filename of the file that caused this exception
  98. *
  99. * @return string the filename of the file that caused this exception.
  100. *
  101. * @see Crypt_GPG_FileException::$_filename
  102. */
  103. public function getFilename()
  104. {
  105. return $this->_filename;
  106. }
  107. // }}}
  108. }
  109. // }}}
  110. // {{{ class Crypt_GPG_OpenSubprocessException
  111. /**
  112. * An exception thrown when the GPG subprocess cannot be opened
  113. *
  114. * This exception is thrown when the {@link Crypt_GPG_Engine} tries to open a
  115. * new subprocess and fails.
  116. *
  117. * @category Encryption
  118. * @package Crypt_GPG
  119. * @author Michael Gauthier <mike@silverorange.com>
  120. * @copyright 2005 silverorange
  121. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  122. * @link http://pear.php.net/package/Crypt_GPG
  123. */
  124. class Crypt_GPG_OpenSubprocessException extends Crypt_GPG_Exception
  125. {
  126. // {{{ private class properties
  127. /**
  128. * The command used to try to open the subprocess
  129. *
  130. * @var string
  131. */
  132. private $_command = '';
  133. // }}}
  134. // {{{ __construct()
  135. /**
  136. * Creates a new Crypt_GPG_OpenSubprocessException
  137. *
  138. * @param string $message an error message.
  139. * @param integer $code a user defined error code.
  140. * @param string $command the command that was called to open the
  141. * new subprocess.
  142. *
  143. * @see Crypt_GPG::_openSubprocess()
  144. */
  145. public function __construct($message, $code = 0, $command = '')
  146. {
  147. $this->_command = $command;
  148. parent::__construct($message, $code);
  149. }
  150. // }}}
  151. // {{{ getCommand()
  152. /**
  153. * Returns the contents of the internal _command property
  154. *
  155. * @return string the command used to open the subprocess.
  156. *
  157. * @see Crypt_GPG_OpenSubprocessException::$_command
  158. */
  159. public function getCommand()
  160. {
  161. return $this->_command;
  162. }
  163. // }}}
  164. }
  165. // }}}
  166. // {{{ class Crypt_GPG_InvalidOperationException
  167. /**
  168. * An exception thrown when an invalid GPG operation is attempted
  169. *
  170. * @category Encryption
  171. * @package Crypt_GPG
  172. * @author Michael Gauthier <mike@silverorange.com>
  173. * @copyright 2008 silverorange
  174. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  175. * @link http://pear.php.net/package/Crypt_GPG
  176. */
  177. class Crypt_GPG_InvalidOperationException extends Crypt_GPG_Exception
  178. {
  179. // {{{ private class properties
  180. /**
  181. * The attempted operation
  182. *
  183. * @var string
  184. */
  185. private $_operation = '';
  186. // }}}
  187. // {{{ __construct()
  188. /**
  189. * Creates a new Crypt_GPG_OpenSubprocessException
  190. *
  191. * @param string $message an error message.
  192. * @param integer $code a user defined error code.
  193. * @param string $operation the operation.
  194. */
  195. public function __construct($message, $code = 0, $operation = '')
  196. {
  197. $this->_operation = $operation;
  198. parent::__construct($message, $code);
  199. }
  200. // }}}
  201. // {{{ getOperation()
  202. /**
  203. * Returns the contents of the internal _operation property
  204. *
  205. * @return string the attempted operation.
  206. *
  207. * @see Crypt_GPG_InvalidOperationException::$_operation
  208. */
  209. public function getOperation()
  210. {
  211. return $this->_operation;
  212. }
  213. // }}}
  214. }
  215. // }}}
  216. // {{{ class Crypt_GPG_KeyNotFoundException
  217. /**
  218. * An exception thrown when Crypt_GPG fails to find the key for various
  219. * operations
  220. *
  221. * @category Encryption
  222. * @package Crypt_GPG
  223. * @author Michael Gauthier <mike@silverorange.com>
  224. * @copyright 2005 silverorange
  225. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  226. * @link http://pear.php.net/package/Crypt_GPG
  227. */
  228. class Crypt_GPG_KeyNotFoundException extends Crypt_GPG_Exception
  229. {
  230. // {{{ private class properties
  231. /**
  232. * The key identifier that was searched for
  233. *
  234. * @var string
  235. */
  236. private $_keyId = '';
  237. // }}}
  238. // {{{ __construct()
  239. /**
  240. * Creates a new Crypt_GPG_KeyNotFoundException
  241. *
  242. * @param string $message an error message.
  243. * @param integer $code a user defined error code.
  244. * @param string $keyId the key identifier of the key.
  245. */
  246. public function __construct($message, $code = 0, $keyId= '')
  247. {
  248. $this->_keyId = $keyId;
  249. parent::__construct($message, $code);
  250. }
  251. // }}}
  252. // {{{ getKeyId()
  253. /**
  254. * Gets the key identifier of the key that was not found
  255. *
  256. * @return string the key identifier of the key that was not found.
  257. */
  258. public function getKeyId()
  259. {
  260. return $this->_keyId;
  261. }
  262. // }}}
  263. }
  264. // }}}
  265. // {{{ class Crypt_GPG_NoDataException
  266. /**
  267. * An exception thrown when Crypt_GPG cannot find valid data for various
  268. * operations
  269. *
  270. * @category Encryption
  271. * @package Crypt_GPG
  272. * @author Michael Gauthier <mike@silverorange.com>
  273. * @copyright 2006 silverorange
  274. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  275. * @link http://pear.php.net/package/Crypt_GPG
  276. */
  277. class Crypt_GPG_NoDataException extends Crypt_GPG_Exception
  278. {
  279. }
  280. // }}}
  281. // {{{ class Crypt_GPG_BadPassphraseException
  282. /**
  283. * An exception thrown when a required passphrase is incorrect or missing
  284. *
  285. * @category Encryption
  286. * @package Crypt_GPG
  287. * @author Michael Gauthier <mike@silverorange.com>
  288. * @copyright 2006-2008 silverorange
  289. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  290. * @link http://pear.php.net/package/Crypt_GPG
  291. */
  292. class Crypt_GPG_BadPassphraseException extends Crypt_GPG_Exception
  293. {
  294. // {{{ private class properties
  295. /**
  296. * Keys for which the passhprase is missing
  297. *
  298. * This contains primary user ids indexed by sub-key id.
  299. *
  300. * @var array
  301. */
  302. private $_missingPassphrases = array();
  303. /**
  304. * Keys for which the passhprase is incorrect
  305. *
  306. * This contains primary user ids indexed by sub-key id.
  307. *
  308. * @var array
  309. */
  310. private $_badPassphrases = array();
  311. // }}}
  312. // {{{ __construct()
  313. /**
  314. * Creates a new Crypt_GPG_BadPassphraseException
  315. *
  316. * @param string $message an error message.
  317. * @param integer $code a user defined error code.
  318. * @param array $badPassphrases an array containing user ids of keys
  319. * for which the passphrase is incorrect.
  320. * @param array $missingPassphrases an array containing user ids of keys
  321. * for which the passphrase is missing.
  322. */
  323. public function __construct($message, $code = 0,
  324. array $badPassphrases = array(), array $missingPassphrases = array()
  325. ) {
  326. $this->_badPassphrases = (array) $badPassphrases;
  327. $this->_missingPassphrases = (array) $missingPassphrases;
  328. parent::__construct($message, $code);
  329. }
  330. // }}}
  331. // {{{ getBadPassphrases()
  332. /**
  333. * Gets keys for which the passhprase is incorrect
  334. *
  335. * @return array an array of keys for which the passphrase is incorrect.
  336. * The array contains primary user ids indexed by the sub-key
  337. * id.
  338. */
  339. public function getBadPassphrases()
  340. {
  341. return $this->_badPassphrases;
  342. }
  343. // }}}
  344. // {{{ getMissingPassphrases()
  345. /**
  346. * Gets keys for which the passhprase is missing
  347. *
  348. * @return array an array of keys for which the passphrase is missing.
  349. * The array contains primary user ids indexed by the sub-key
  350. * id.
  351. */
  352. public function getMissingPassphrases()
  353. {
  354. return $this->_missingPassphrases;
  355. }
  356. // }}}
  357. }
  358. // }}}
  359. // {{{ class Crypt_GPG_DeletePrivateKeyException
  360. /**
  361. * An exception thrown when an attempt is made to delete public key that has an
  362. * associated private key on the keyring
  363. *
  364. * @category Encryption
  365. * @package Crypt_GPG
  366. * @author Michael Gauthier <mike@silverorange.com>
  367. * @copyright 2008 silverorange
  368. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  369. * @link http://pear.php.net/package/Crypt_GPG
  370. */
  371. class Crypt_GPG_DeletePrivateKeyException extends Crypt_GPG_Exception
  372. {
  373. // {{{ private class properties
  374. /**
  375. * The key identifier the deletion attempt was made upon
  376. *
  377. * @var string
  378. */
  379. private $_keyId = '';
  380. // }}}
  381. // {{{ __construct()
  382. /**
  383. * Creates a new Crypt_GPG_DeletePrivateKeyException
  384. *
  385. * @param string $message an error message.
  386. * @param integer $code a user defined error code.
  387. * @param string $keyId the key identifier of the public key that was
  388. * attempted to delete.
  389. *
  390. * @see Crypt_GPG::deletePublicKey()
  391. */
  392. public function __construct($message, $code = 0, $keyId = '')
  393. {
  394. $this->_keyId = $keyId;
  395. parent::__construct($message, $code);
  396. }
  397. // }}}
  398. // {{{ getKeyId()
  399. /**
  400. * Gets the key identifier of the key that was not found
  401. *
  402. * @return string the key identifier of the key that was not found.
  403. */
  404. public function getKeyId()
  405. {
  406. return $this->_keyId;
  407. }
  408. // }}}
  409. }
  410. // }}}
  411. // {{{ class Crypt_GPG_KeyNotCreatedException
  412. /**
  413. * An exception thrown when an attempt is made to generate a key and the
  414. * attempt fails
  415. *
  416. * @category Encryption
  417. * @package Crypt_GPG
  418. * @author Michael Gauthier <mike@silverorange.com>
  419. * @copyright 2011 silverorange
  420. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  421. * @link http://pear.php.net/package/Crypt_GPG
  422. */
  423. class Crypt_GPG_KeyNotCreatedException extends Crypt_GPG_Exception
  424. {
  425. }
  426. // }}}
  427. // {{{ class Crypt_GPG_InvalidKeyParamsException
  428. /**
  429. * An exception thrown when an attempt is made to generate a key and the
  430. * key parameters set on the key generator are invalid
  431. *
  432. * @category Encryption
  433. * @package Crypt_GPG
  434. * @author Michael Gauthier <mike@silverorange.com>
  435. * @copyright 2011 silverorange
  436. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  437. * @link http://pear.php.net/package/Crypt_GPG
  438. */
  439. class Crypt_GPG_InvalidKeyParamsException extends Crypt_GPG_Exception
  440. {
  441. // {{{ private class properties
  442. /**
  443. * The key algorithm
  444. *
  445. * @var integer
  446. */
  447. private $_algorithm = 0;
  448. /**
  449. * The key size
  450. *
  451. * @var integer
  452. */
  453. private $_size = 0;
  454. /**
  455. * The key usage
  456. *
  457. * @var integer
  458. */
  459. private $_usage = 0;
  460. // }}}
  461. // {{{ __construct()
  462. /**
  463. * Creates a new Crypt_GPG_InvalidKeyParamsException
  464. *
  465. * @param string $message an error message.
  466. * @param integer $code a user defined error code.
  467. * @param string $algorithm the key algorithm.
  468. * @param string $size the key size.
  469. * @param string $usage the key usage.
  470. */
  471. public function __construct(
  472. $message,
  473. $code = 0,
  474. $algorithm = 0,
  475. $size = 0,
  476. $usage = 0
  477. ) {
  478. parent::__construct($message, $code);
  479. $this->_algorithm = $algorithm;
  480. $this->_size = $size;
  481. $this->_usage = $usage;
  482. }
  483. // }}}
  484. // {{{ getAlgorithm()
  485. /**
  486. * Gets the key algorithm
  487. *
  488. * @return integer the key algorithm.
  489. */
  490. public function getAlgorithm()
  491. {
  492. return $this->_algorithm;
  493. }
  494. // }}}
  495. // {{{ getSize()
  496. /**
  497. * Gets the key size
  498. *
  499. * @return integer the key size.
  500. */
  501. public function getSize()
  502. {
  503. return $this->_size;
  504. }
  505. // }}}
  506. // {{{ getUsage()
  507. /**
  508. * Gets the key usage
  509. *
  510. * @return integer the key usage.
  511. */
  512. public function getUsage()
  513. {
  514. return $this->_usage;
  515. }
  516. // }}}
  517. }
  518. // }}}
  519. ?>