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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. const TYPE_UNKNOWN = 0;
  23. const TYPE_KEYPAIR = 1;
  24. const TYPE_PUBLIC = 2;
  25. const CAN_ENCRYPT = 1;
  26. const CAN_SIGN = 2;
  27. const CAN_CERTIFY = 4;
  28. const CAN_AUTHENTICATE = 8;
  29. /**
  30. * Keys list sorting callback for usort()
  31. */
  32. static function cmp($a, $b)
  33. {
  34. return strcmp($a->name, $b->name);
  35. }
  36. /**
  37. * Returns key type
  38. */
  39. function get_type()
  40. {
  41. if ($this->subkeys[0]->has_private)
  42. return enigma_key::TYPE_KEYPAIR;
  43. else if (!empty($this->subkeys[0]))
  44. return enigma_key::TYPE_PUBLIC;
  45. return enigma_key::TYPE_UNKNOWN;
  46. }
  47. /**
  48. * Returns true if all user IDs are revoked
  49. */
  50. function is_revoked()
  51. {
  52. foreach ($this->subkeys as $subkey)
  53. if (!$subkey->revoked)
  54. return false;
  55. return true;
  56. }
  57. /**
  58. * Returns true if any user ID is valid
  59. */
  60. function is_valid()
  61. {
  62. foreach ($this->users as $user)
  63. if ($user->valid)
  64. return true;
  65. return false;
  66. }
  67. /**
  68. * Returns true if any of subkeys is not expired
  69. */
  70. function is_expired()
  71. {
  72. $now = time();
  73. foreach ($this->subkeys as $subkey)
  74. if (!$subkey->expires || $subkey->expires > $now)
  75. return true;
  76. return false;
  77. }
  78. /**
  79. * Returns true if any of subkeys is a private key
  80. */
  81. function is_private()
  82. {
  83. $now = time();
  84. foreach ($this->subkeys as $subkey)
  85. if ($subkey->has_private)
  86. return true;
  87. return false;
  88. }
  89. /**
  90. * Get key ID by user email
  91. */
  92. function find_subkey($email, $mode)
  93. {
  94. $now = time();
  95. foreach ($this->users as $user) {
  96. if (strcasecmp($user->email, $email) === 0 && $user->valid && !$user->revoked) {
  97. foreach ($this->subkeys as $subkey) {
  98. if (!$subkey->revoked && (!$subkey->expires || $subkey->expires > $now)) {
  99. if ($subkey->usage & $mode) {
  100. return $subkey;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * Converts long ID or Fingerprint to short ID
  109. * Crypt_GPG uses internal, but e.g. Thunderbird's Enigmail displays short ID
  110. *
  111. * @param string Key ID or fingerprint
  112. * @return string Key short ID
  113. */
  114. static function format_id($id)
  115. {
  116. // E.g. 04622F2089E037A5 => 89E037A5
  117. return substr($id, -8);
  118. }
  119. /**
  120. * Formats fingerprint string
  121. *
  122. * @param string Key fingerprint
  123. *
  124. * @return string Formatted fingerprint (with spaces)
  125. */
  126. static function format_fingerprint($fingerprint)
  127. {
  128. if (!$fingerprint) {
  129. return '';
  130. }
  131. $result = '';
  132. for ($i=0; $i<40; $i++) {
  133. if ($i % 4 == 0) {
  134. $result .= ' ';
  135. }
  136. $result .= $fingerprint[$i];
  137. }
  138. return $result;
  139. }
  140. }