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.

79 lines
2.0 KiB

  1. <?php
  2. namespace App;
  3. class MessageOmemoHeader
  4. {
  5. public $sid;
  6. private $keys = [];
  7. private $iv;
  8. private $payload;
  9. public function import($omemo)
  10. {
  11. $this->sid = $omemo->sid;
  12. $this->keys = $omemo->keys;
  13. $this->iv = $omemo->iv;
  14. $this->payload = $omemo->payload;
  15. }
  16. public function set($stanza)
  17. {
  18. $this->sid = (int)$stanza->encrypted->header->attributes()->sid;
  19. $this->iv = (string)$stanza->encrypted->header->iv;
  20. $this->payload = (string)$stanza->encrypted->payload;
  21. $keys = [];
  22. foreach ($stanza->encrypted->header->key as $key) {
  23. $keys[(string)$key->attributes()->rid] = [
  24. 'payload' => (string)$key,
  25. 'prekey' => (bool)$key->attributes()->prekey
  26. ];
  27. }
  28. $this->keys = $keys;
  29. }
  30. public function getDom()
  31. {
  32. $dom = new \DOMDocument('1.0', 'UTF-8');
  33. $encrypted = $dom->createElement('encrypted');
  34. $encrypted->setAttribute('xmlns', 'eu.siacs.conversations.axolotl');
  35. $dom->appendChild($encrypted);
  36. $header = $dom->createElement('header');
  37. $header->setAttribute('sid', $this->sid);
  38. $encrypted->appendChild($header);
  39. foreach ($this->keys as $rid => $value ) {
  40. $key = $dom->createElement('key', $value->payload);
  41. $key->setAttribute('rid', $rid);
  42. if ($value->prekey) {
  43. $key->setAttribute('prekey', 'true');
  44. }
  45. $header->appendChild($key);
  46. }
  47. $iv = $dom->createElement('iv', $this->iv);
  48. $header->appendChild($iv);
  49. $payload = $dom->createElement('payload', $this->payload);
  50. $encrypted->appendChild($payload);
  51. return $dom->documentElement;
  52. }
  53. public function __toString()
  54. {
  55. return serialize([
  56. 'sid' => $this->sid,
  57. 'iv' => $this->iv,
  58. 'keys' => $this->keys,
  59. 'payload' => $this->payload,
  60. ]);
  61. }
  62. }