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.

SignatureCreationInfo.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Part of Crypt_GPG
  4. *
  5. * PHP version 5
  6. *
  7. * @category Encryption
  8. * @package Crypt_GPG
  9. * @author Christian Weiske <cweiske@php.net>
  10. * @copyright 2015 PEAR
  11. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  12. * @link http://pear.php.net/package/Crypt_GPG
  13. * @link http://pear.php.net/manual/en/package.encryption.crypt-gpg.php
  14. * @link http://www.gnupg.org/
  15. */
  16. /**
  17. * Information about a recently created signature.
  18. *
  19. * @category Encryption
  20. * @package Crypt_GPG
  21. * @author Christian Weiske <cweiske@php.net>
  22. * @copyright 2015 PEAR
  23. * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  24. * @link http://pear.php.net/package/Crypt_GPG
  25. * @link http://pear.php.net/manual/en/package.encryption.crypt-gpg.php
  26. * @link http://www.gnupg.org/
  27. */
  28. class Crypt_GPG_SignatureCreationInfo
  29. {
  30. /**
  31. * One of the three signature types:
  32. * - {@link Crypt_GPG::SIGN_MODE_NORMAL}
  33. * - {@link Crypt_GPG::SIGN_MODE_CLEAR}
  34. * - {@link Crypt_GPG::SIGN_MODE_DETACHED}
  35. *
  36. * @var integer
  37. */
  38. protected $mode;
  39. /**
  40. * Public Key algorithm
  41. *
  42. * @var integer
  43. */
  44. protected $pkAlgorithm;
  45. /**
  46. * Algorithm to hash the data
  47. *
  48. * @see RFC 2440 / 9.4. Hash Algorithm
  49. * @var integer
  50. */
  51. protected $hashAlgorithm;
  52. /**
  53. * OpenPGP signature class
  54. *
  55. * @var mixed
  56. */
  57. protected $class;
  58. /**
  59. * Unix timestamp when the signature was created
  60. *
  61. * @var integer
  62. */
  63. protected $timestamp;
  64. /**
  65. * Key fingerprint
  66. *
  67. * @var string
  68. */
  69. protected $keyFingerprint;
  70. /**
  71. * If the line given to the constructor was valid
  72. *
  73. * @var boolean
  74. */
  75. protected $valid;
  76. /**
  77. * Names for the hash algorithm IDs.
  78. *
  79. * Names taken from RFC 3156, without the leading "pgp-".
  80. *
  81. * @see RFC 2440 / 9.4. Hash Algorithm
  82. * @see RFC 3156 / 5. OpenPGP signed data
  83. * @var array
  84. */
  85. protected static $hashAlgorithmNames = array(
  86. 1 => 'md5',
  87. 2 => 'sha1',
  88. 3 => 'ripemd160',
  89. 5 => 'md2',
  90. 6 => 'tiger192',
  91. 7 => 'haval-5-160',
  92. 8 => 'sha256',
  93. 9 => 'sha384',
  94. 10 => 'sha512',
  95. 11 => 'sha224',
  96. );
  97. /**
  98. * Parse a SIG_CREATED line from gnupg
  99. *
  100. * @param string $sigCreatedLine Line beginning with "SIG_CREATED "
  101. */
  102. public function __construct($sigCreatedLine = null)
  103. {
  104. if ($sigCreatedLine === null) {
  105. $this->valid = false;
  106. return;
  107. }
  108. $parts = explode(' ', $sigCreatedLine);
  109. if (count($parts) !== 7) {
  110. $this->valid = false;
  111. return;
  112. }
  113. list(
  114. $title, $mode, $pkAlgorithm, $hashAlgorithm,
  115. $class, $timestamp, $keyFingerprint
  116. ) = $parts;
  117. switch (strtoupper($mode[0])) {
  118. case 'D':
  119. $this->mode = Crypt_GPG::SIGN_MODE_DETACHED;
  120. break;
  121. case 'C':
  122. $this->mode = Crypt_GPG::SIGN_MODE_CLEAR;
  123. break;
  124. case 'S':
  125. $this->mode = Crypt_GPG::SIGN_MODE_NORMAL;
  126. break;
  127. }
  128. $this->pkAlgorithm = (int) $pkAlgorithm;
  129. $this->hashAlgorithm = (int) $hashAlgorithm;
  130. $this->class = $class;
  131. if (is_numeric($timestamp)) {
  132. $this->timestamp = (int) $timestamp;
  133. } else {
  134. $this->timestamp = strtotime($timestamp);
  135. }
  136. $this->keyFingerprint = $keyFingerprint;
  137. $this->valid = true;
  138. }
  139. /**
  140. * Get the signature type
  141. * - {@link Crypt_GPG::SIGN_MODE_NORMAL}
  142. * - {@link Crypt_GPG::SIGN_MODE_CLEAR}
  143. * - {@link Crypt_GPG::SIGN_MODE_DETACHED}
  144. *
  145. * @return integer
  146. */
  147. public function getMode()
  148. {
  149. return $this->mode;
  150. }
  151. /**
  152. * Return the public key algorithm used.
  153. *
  154. * @return integer
  155. */
  156. public function getPkAlgorithm()
  157. {
  158. return $this->pkAlgorithm;
  159. }
  160. /**
  161. * Return the hash algorithm used to hash the data to sign.
  162. *
  163. * @return integer
  164. */
  165. public function getHashAlgorithm()
  166. {
  167. return $this->hashAlgorithm;
  168. }
  169. /**
  170. * Get a name for the used hashing algorithm.
  171. *
  172. * @return string|null
  173. */
  174. public function getHashAlgorithmName()
  175. {
  176. if (!isset(self::$hashAlgorithmNames[$this->hashAlgorithm])) {
  177. return null;
  178. }
  179. return self::$hashAlgorithmNames[$this->hashAlgorithm];
  180. }
  181. /**
  182. * Return the timestamp at which the signature was created
  183. *
  184. * @return integer
  185. */
  186. public function getTimestamp()
  187. {
  188. return $this->timestamp;
  189. }
  190. /**
  191. * Return the key's fingerprint
  192. *
  193. * @return string
  194. */
  195. public function getKeyFingerprint()
  196. {
  197. return $this->keyFingerprint;
  198. }
  199. /**
  200. * Tell if the fingerprint line given to the constructor was valid
  201. *
  202. * @return boolean
  203. */
  204. public function isValid()
  205. {
  206. return $this->valid;
  207. }
  208. }
  209. ?>