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_driver_phpssl.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. +-------------------------------------------------------------------------+
  4. | S/MIME driver 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_driver_phpssl extends enigma_driver
  17. {
  18. private $rc;
  19. private $homedir;
  20. private $user;
  21. function __construct($user)
  22. {
  23. $rcmail = rcmail::get_instance();
  24. $this->rc = $rcmail;
  25. $this->user = $user;
  26. }
  27. /**
  28. * Driver initialization and environment checking.
  29. * Should only return critical errors.
  30. *
  31. * @return mixed NULL on success, enigma_error on failure
  32. */
  33. function init()
  34. {
  35. $homedir = $this->rc->config->get('enigma_smime_homedir', INSTALL_PATH . '/plugins/enigma/home');
  36. if (!$homedir)
  37. return new enigma_error(enigma_error::INTERNAL,
  38. "Option 'enigma_smime_homedir' not specified");
  39. // check if homedir exists (create it if not) and is readable
  40. if (!file_exists($homedir))
  41. return new enigma_error(enigma_error::INTERNAL,
  42. "Keys directory doesn't exists: $homedir");
  43. if (!is_writable($homedir))
  44. return new enigma_error(enigma_error::INTERNAL,
  45. "Keys directory isn't writeable: $homedir");
  46. $homedir = $homedir . '/' . $this->user;
  47. // check if user's homedir exists (create it if not) and is readable
  48. if (!file_exists($homedir))
  49. mkdir($homedir, 0700);
  50. if (!file_exists($homedir))
  51. return new enigma_error(enigma_error::INTERNAL,
  52. "Unable to create keys directory: $homedir");
  53. if (!is_writable($homedir))
  54. return new enigma_error(enigma_error::INTERNAL,
  55. "Unable to write to keys directory: $homedir");
  56. $this->homedir = $homedir;
  57. }
  58. function encrypt($text, $keys)
  59. {
  60. }
  61. function decrypt($text, $keys = array())
  62. {
  63. }
  64. function sign($text, $key, $passwd, $mode = null)
  65. {
  66. }
  67. function verify($struct, $message)
  68. {
  69. // use common temp dir
  70. $temp_dir = $this->rc->config->get('temp_dir');
  71. $msg_file = tempnam($temp_dir, 'rcmMsg');
  72. $cert_file = tempnam($temp_dir, 'rcmCert');
  73. $fh = fopen($msg_file, "w");
  74. if ($struct->mime_id) {
  75. $message->get_part_body($struct->mime_id, false, 0, $fh);
  76. }
  77. else {
  78. $this->rc->storage->get_raw_body($message->uid, $fh);
  79. }
  80. fclose($fh);
  81. // @TODO: use stored certificates
  82. // try with certificate verification
  83. $sig = openssl_pkcs7_verify($msg_file, 0, $cert_file);
  84. $validity = true;
  85. if ($sig !== true) {
  86. // try without certificate verification
  87. $sig = openssl_pkcs7_verify($msg_file, PKCS7_NOVERIFY, $cert_file);
  88. $validity = enigma_error::UNVERIFIED;
  89. }
  90. if ($sig === true) {
  91. $sig = $this->parse_sig_cert($cert_file, $validity);
  92. }
  93. else {
  94. $errorstr = $this->get_openssl_error();
  95. $sig = new enigma_error(enigma_error::INTERNAL, $errorstr);
  96. }
  97. // remove temp files
  98. @unlink($msg_file);
  99. @unlink($cert_file);
  100. return $sig;
  101. }
  102. public function import($content, $isfile=false)
  103. {
  104. }
  105. public function export($key, $with_private = false)
  106. {
  107. }
  108. public function list_keys($pattern='')
  109. {
  110. }
  111. public function get_key($keyid)
  112. {
  113. }
  114. public function gen_key($data)
  115. {
  116. }
  117. public function delete_key($keyid)
  118. {
  119. }
  120. public function delete_privkey($keyid)
  121. {
  122. }
  123. public function delete_pubkey($keyid)
  124. {
  125. }
  126. /**
  127. * Converts Crypt_GPG_Key object into Enigma's key object
  128. *
  129. * @param Crypt_GPG_Key Key object
  130. *
  131. * @return enigma_key Key object
  132. */
  133. private function parse_key($key)
  134. {
  135. /*
  136. $ekey = new enigma_key();
  137. foreach ($key->getUserIds() as $idx => $user) {
  138. $id = new enigma_userid();
  139. $id->name = $user->getName();
  140. $id->comment = $user->getComment();
  141. $id->email = $user->getEmail();
  142. $id->valid = $user->isValid();
  143. $id->revoked = $user->isRevoked();
  144. $ekey->users[$idx] = $id;
  145. }
  146. $ekey->name = trim($ekey->users[0]->name . ' <' . $ekey->users[0]->email . '>');
  147. foreach ($key->getSubKeys() as $idx => $subkey) {
  148. $skey = new enigma_subkey();
  149. $skey->id = $subkey->getId();
  150. $skey->revoked = $subkey->isRevoked();
  151. $skey->created = $subkey->getCreationDate();
  152. $skey->expires = $subkey->getExpirationDate();
  153. $skey->fingerprint = $subkey->getFingerprint();
  154. $skey->has_private = $subkey->hasPrivate();
  155. $ekey->subkeys[$idx] = $skey;
  156. };
  157. $ekey->id = $ekey->subkeys[0]->id;
  158. return $ekey;
  159. */
  160. }
  161. private function get_openssl_error()
  162. {
  163. $tmp = array();
  164. while ($errorstr = openssl_error_string()) {
  165. $tmp[] = $errorstr;
  166. }
  167. return join("\n", array_values($tmp));
  168. }
  169. private function parse_sig_cert($file, $validity)
  170. {
  171. $cert = openssl_x509_parse(file_get_contents($file));
  172. if (empty($cert) || empty($cert['subject'])) {
  173. $errorstr = $this->get_openssl_error();
  174. return new enigma_error(enigma_error::INTERNAL, $errorstr);
  175. }
  176. $data = new enigma_signature();
  177. $data->id = $cert['hash']; //?
  178. $data->valid = $validity;
  179. $data->fingerprint = $cert['serialNumber'];
  180. $data->created = $cert['validFrom_time_t'];
  181. $data->expires = $cert['validTo_time_t'];
  182. $data->name = $cert['subject']['CN'];
  183. // $data->comment = '';
  184. $data->email = $cert['subject']['emailAddress'];
  185. return $data;
  186. }
  187. }