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.

enigma_key.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. +-------------------------------------------------------------------------+
  4. | Key class for the Enigma Plugin |
  5. | |
  6. | Copyright (C) 2010-2015 The Roundcube Dev Team |
  7. | |
  8. | Licensed under the GNU General Public License version 3 or |
  9. | any later version with exceptions for skins & plugins. |
  10. | See the README file for a full license statement. |
  11. | |
  12. +-------------------------------------------------------------------------+
  13. | Author: Aleksander Machniak <alec@alec.pl> |
  14. +-------------------------------------------------------------------------+
  15. */
  16. class enigma_key
  17. {
  18. public $id;
  19. public $name;
  20. public $users = array();
  21. public $subkeys = array();
  22. public $reference;
  23. public $password;
  24. const TYPE_UNKNOWN = 0;
  25. const TYPE_KEYPAIR = 1;
  26. const TYPE_PUBLIC = 2;
  27. const CAN_ENCRYPT = 1;
  28. const CAN_SIGN = 2;
  29. const CAN_CERTIFY = 4;
  30. const CAN_AUTHENTICATE = 8;
  31. /**
  32. * Keys list sorting callback for usort()
  33. */
  34. static function cmp($a, $b)
  35. {
  36. return strcmp($a->name, $b->name);
  37. }
  38. /**
  39. * Returns key type
  40. */
  41. function get_type()
  42. {
  43. if ($this->subkeys[0]->has_private)
  44. return enigma_key::TYPE_KEYPAIR;
  45. else if (!empty($this->subkeys[0]))
  46. return enigma_key::TYPE_PUBLIC;
  47. return enigma_key::TYPE_UNKNOWN;
  48. }
  49. /**
  50. * Returns true if all user IDs are revoked
  51. */
  52. function is_revoked()
  53. {
  54. foreach ($this->subkeys as $subkey)
  55. if (!$subkey->revoked)
  56. return false;
  57. return true;
  58. }
  59. /**
  60. * Returns true if any user ID is valid
  61. */
  62. function is_valid()
  63. {
  64. foreach ($this->users as $user)
  65. if ($user->valid)
  66. return true;
  67. return false;
  68. }
  69. /**
  70. * Returns true if any of subkeys is not expired
  71. */
  72. function is_expired()
  73. {
  74. $now = time();
  75. foreach ($this->subkeys as $subkey)
  76. if (!$subkey->expires || $subkey->expires > $now)
  77. return true;
  78. return false;
  79. }
  80. /**
  81. * Returns true if any of subkeys is a private key
  82. */
  83. function is_private()
  84. {
  85. $now = time();
  86. foreach ($this->subkeys as $subkey)
  87. if ($subkey->has_private)
  88. return true;
  89. return false;
  90. }
  91. /**
  92. * Get key ID by user email
  93. */
  94. function find_subkey($email, $mode)
  95. {
  96. $now = time();
  97. foreach ($this->users as $user) {
  98. if (strcasecmp($user->email, $email) === 0 && $user->valid && !$user->revoked) {
  99. foreach ($this->subkeys as $subkey) {
  100. if (!$subkey->revoked && (!$subkey->expires || $subkey->expires > $now)) {
  101. if ($subkey->usage & $mode) {
  102. return $subkey;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Converts long ID or Fingerprint to short ID
  111. * Crypt_GPG uses internal, but e.g. Thunderbird's Enigmail displays short ID
  112. *
  113. * @param string Key ID or fingerprint
  114. * @return string Key short ID
  115. */
  116. static function format_id($id)
  117. {
  118. // E.g. 04622F2089E037A5 => 89E037A5
  119. return substr($id, -8);
  120. }
  121. /**
  122. * Formats fingerprint string
  123. *
  124. * @param string Key fingerprint
  125. *
  126. * @return string Formatted fingerprint (with spaces)
  127. */
  128. static function format_fingerprint($fingerprint)
  129. {
  130. if (!$fingerprint) {
  131. return '';
  132. }
  133. $result = '';
  134. for ($i=0; $i<40; $i++) {
  135. if ($i % 4 == 0) {
  136. $result .= ' ';
  137. }
  138. $result .= $fingerprint[$i];
  139. }
  140. return $result;
  141. }
  142. }