Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.
 
 
 
 
 
 

196 lines
4.9 KiB

  1. <?php
  2. require_once('include/crypto.php');
  3. function get_salmon_key($uri,$keyhash) {
  4. $ret = array();
  5. logger('Fetching salmon key');
  6. $arr = lrdd($uri);
  7. if(is_array($arr)) {
  8. foreach($arr as $a) {
  9. if($a['@attributes']['rel'] === 'magic-public-key') {
  10. $ret[] = $a['@attributes']['href'];
  11. }
  12. }
  13. }
  14. else {
  15. return '';
  16. }
  17. // We have found at least one key URL
  18. // If it's inline, parse it - otherwise get the key
  19. if(count($ret)) {
  20. for($x = 0; $x < count($ret); $x ++) {
  21. if(substr($ret[$x],0,5) === 'data:') {
  22. if(strstr($ret[$x],','))
  23. $ret[$x] = substr($ret[$x],strpos($ret[$x],',')+1);
  24. else
  25. $ret[$x] = substr($ret[$x],5);
  26. }
  27. else
  28. $ret[$x] = fetch_url($ret[$x]);
  29. }
  30. }
  31. logger('Key located: ' . print_r($ret,true));
  32. if(count($ret) == 1) {
  33. // We only found one one key so we don't care if the hash matches.
  34. // If it's the wrong key we'll find out soon enough because
  35. // message verification will fail. This also covers some older
  36. // software which don't supply a keyhash. As long as they only
  37. // have one key we'll be right.
  38. return $ret[0];
  39. }
  40. else {
  41. foreach($ret as $a) {
  42. $hash = base64url_encode(hash('sha256',$a));
  43. if($hash == $keyhash)
  44. return $a;
  45. }
  46. }
  47. return '';
  48. }
  49. function slapper($owner,$url,$slap) {
  50. logger('slapper called for '.$owner['url'].' to '.$url.' . Data: ' . $slap);
  51. // does contact have a salmon endpoint?
  52. if(! strlen($url))
  53. return;
  54. if(! $owner['sprvkey']) {
  55. logger(sprintf("slapper: user '%s' (%d) does not have a salmon private key. Send failed.",
  56. $owner['username'],$owner['uid']));
  57. return;
  58. }
  59. // add all namespaces to item
  60. $namespaces = <<< EOT
  61. <entry xmlns="http://www.w3.org/2005/Atom"
  62. xmlns:thr="http://purl.org/syndication/thread/1.0"
  63. xmlns:at="http://purl.org/atompub/tombstones/1.0"
  64. xmlns:media="http://purl.org/syndication/atommedia"
  65. xmlns:dfrn="http://purl.org/macgirvin/dfrn/1.0"
  66. xmlns:as="http://activitystrea.ms/spec/1.0/"
  67. xmlns:georss="http://www.georss.org/georss"
  68. xmlns:poco="http://portablecontacts.net/spec/1.0"
  69. xmlns:ostatus="http://ostatus.org/schema/1.0"
  70. xmlns:statusnet="http://status.net/schema/api/1/" > >
  71. EOT;
  72. $slap = str_replace('<entry>',$namespaces,$slap);
  73. // create a magic envelope
  74. $data = base64url_encode($slap);
  75. $data_type = 'application/atom+xml';
  76. $encoding = 'base64url';
  77. $algorithm = 'RSA-SHA256';
  78. $keyhash = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
  79. // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
  80. $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
  81. $signature = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
  82. $signature2 = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
  83. $signature3 = base64url_encode(rsa_sign($data,$owner['sprvkey']));
  84. $salmon_tpl = get_markup_template('magicsig.tpl');
  85. $salmon = replace_macros($salmon_tpl,array(
  86. '$data' => $data,
  87. '$encoding' => $encoding,
  88. '$algorithm' => $algorithm,
  89. '$keyhash' => $keyhash,
  90. '$signature' => $signature
  91. ));
  92. // slap them
  93. post_url($url,$salmon, array(
  94. 'Content-type: application/magic-envelope+xml',
  95. 'Content-length: ' . strlen($salmon)
  96. ));
  97. $a = get_app();
  98. $return_code = $a->get_curl_code();
  99. // check for success, e.g. 2xx
  100. if($return_code > 299) {
  101. logger('slapper: compliant salmon failed. Falling back to status.net hack2');
  102. // Entirely likely that their salmon implementation is
  103. // non-compliant. Let's try once more, this time only signing
  104. // the data, without stripping '=' chars
  105. $salmon = replace_macros($salmon_tpl,array(
  106. '$data' => $data,
  107. '$encoding' => $encoding,
  108. '$algorithm' => $algorithm,
  109. '$keyhash' => $keyhash,
  110. '$signature' => $signature2
  111. ));
  112. // slap them
  113. post_url($url,$salmon, array(
  114. 'Content-type: application/magic-envelope+xml',
  115. 'Content-length: ' . strlen($salmon)
  116. ));
  117. $return_code = $a->get_curl_code();
  118. if($return_code > 299) {
  119. logger('slapper: compliant salmon failed. Falling back to status.net hack3');
  120. // Entirely likely that their salmon implementation is
  121. // non-compliant. Let's try once more, this time only signing
  122. // the data, without the precomputed blob
  123. $salmon = replace_macros($salmon_tpl,array(
  124. '$data' => $data,
  125. '$encoding' => $encoding,
  126. '$algorithm' => $algorithm,
  127. '$keyhash' => $keyhash,
  128. '$signature' => $signature3
  129. ));
  130. // slap them
  131. post_url($url,$salmon, array(
  132. 'Content-type: application/magic-envelope+xml',
  133. 'Content-length: ' . strlen($salmon)
  134. ));
  135. $return_code = $a->get_curl_code();
  136. }
  137. }
  138. logger('slapper returned ' . $return_code);
  139. if(! $return_code)
  140. return(-1);
  141. if(($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))
  142. return(-1);
  143. return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
  144. }