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.

179 lines
4.3 KiB

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