]> git.mxchange.org Git - friendica.git/blob - include/salmon.php
Move xml to namespace
[friendica.git] / include / salmon.php
1 <?php
2 /**
3  * @file include/salmon.php
4  */
5 use Friendica\Network\Probe;
6 use Friendica\Util\Xml;
7
8 require_once 'include/crypto.php';
9
10 function get_salmon_key($uri, $keyhash)
11 {
12         $ret = array();
13
14         logger('Fetching salmon key for '.$uri);
15
16         $arr = Probe::lrdd($uri);
17
18         if (is_array($arr)) {
19                 foreach ($arr as $a) {
20                         if ($a['@attributes']['rel'] === 'magic-public-key') {
21                                 $ret[] = $a['@attributes']['href'];
22                         }
23                 }
24         } else {
25                 return '';
26         }
27
28         // We have found at least one key URL
29         // If it's inline, parse it - otherwise get the key
30
31         if (count($ret) > 0) {
32                 for ($x = 0; $x < count($ret); $x ++) {
33                         if (substr($ret[$x], 0, 5) === 'data:') {
34                                 if (strstr($ret[$x], ',')) {
35                                         $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
36                                 } else {
37                                         $ret[$x] = substr($ret[$x], 5);
38                                 }
39                         } elseif (normalise_link($ret[$x]) == 'http://') {
40                                 $ret[$x] = fetch_url($ret[$x]);
41                         }
42                 }
43         }
44
45
46         logger('Key located: ' . print_r($ret, true));
47
48         if (count($ret) == 1) {
49                 // We only found one one key so we don't care if the hash matches.
50                 // If it's the wrong key we'll find out soon enough because
51                 // message verification will fail. This also covers some older
52                 // software which don't supply a keyhash. As long as they only
53                 // have one key we'll be right.
54
55                 return $ret[0];
56         } else {
57                 foreach ($ret as $a) {
58                         $hash = base64url_encode(hash('sha256', $a));
59                         if ($hash == $keyhash) {
60                                 return $a;
61                         }
62                 }
63         }
64
65         return '';
66 }
67
68
69
70 function slapper($owner, $url, $slap)
71 {
72         // does contact have a salmon endpoint?
73
74         if (! strlen($url)) {
75                 return;
76         }
77
78         if (! $owner['sprvkey']) {
79                 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
80                 $owner['username'], $owner['uid']));
81                 return;
82         }
83
84         logger('slapper called for '.$url.'. Data: ' . $slap);
85
86         // create a magic envelope
87
88         $data      = base64url_encode($slap);
89         $data_type = 'application/atom+xml';
90         $encoding  = 'base64url';
91         $algorithm = 'RSA-SHA256';
92         $keyhash   = base64url_encode(hash('sha256', salmon_key($owner['spubkey'])), true);
93
94         $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm);
95
96         // GNU Social format
97         $signature   = base64url_encode(rsa_sign($data . $precomputed, $owner['sprvkey']));
98
99         // Compliant format
100         $signature2  = base64url_encode(rsa_sign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
101
102         // Old Status.net format
103         $signature3  = base64url_encode(rsa_sign($data, $owner['sprvkey']));
104
105         // At first try the non compliant method that works for GNU Social
106         $xmldata = array("me:env" => array("me:data" => $data,
107                         "@attributes" => array("type" => $data_type),
108                         "me:encoding" => $encoding,
109                         "me:alg" => $algorithm,
110                         "me:sig" => $signature,
111                         "@attributes2" => array("key_id" => $keyhash)));
112
113         $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
114
115         $salmon = Xml::from_array($xmldata, $xml, false, $namespaces);
116
117         // slap them
118         post_url($url, $salmon, array(
119                 'Content-type: application/magic-envelope+xml',
120                 'Content-length: ' . strlen($salmon)
121         ));
122
123         $a = get_app();
124         $return_code = $a->get_curl_code();
125
126         // check for success, e.g. 2xx
127
128         if ($return_code > 299) {
129                 logger('GNU Social salmon failed. Falling back to compliant mode');
130
131                 // Now try the compliant mode that normally isn't used for GNU Social
132                 $xmldata = array("me:env" => array("me:data" => $data,
133                                 "@attributes" => array("type" => $data_type),
134                                 "me:encoding" => $encoding,
135                                 "me:alg" => $algorithm,
136                                 "me:sig" => $signature2,
137                                 "@attributes2" => array("key_id" => $keyhash)));
138
139                 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
140
141                 $salmon = Xml::from_array($xmldata, $xml, false, $namespaces);
142
143                 // slap them
144                 post_url($url, $salmon, array(
145                         'Content-type: application/magic-envelope+xml',
146                         'Content-length: ' . strlen($salmon)
147                 ));
148                 $return_code = $a->get_curl_code();
149         }
150
151         if ($return_code > 299) {
152                 logger('compliant salmon failed. Falling back to old status.net');
153
154                 // Last try. This will most likely fail as well.
155                 $xmldata = array("me:env" => array("me:data" => $data,
156                                 "@attributes" => array("type" => $data_type),
157                                 "me:encoding" => $encoding,
158                                 "me:alg" => $algorithm,
159                                 "me:sig" => $signature3,
160                                 "@attributes2" => array("key_id" => $keyhash)));
161
162                 $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
163
164                 $salmon = Xml::from_array($xmldata, $xml, false, $namespaces);
165
166                 // slap them
167                 post_url($url, $salmon, array(
168                         'Content-type: application/magic-envelope+xml',
169                         'Content-length: ' . strlen($salmon)
170                 ));
171                 $return_code = $a->get_curl_code();
172         }
173
174         logger('slapper for '.$url.' returned ' . $return_code);
175
176         if (! $return_code) {
177                 return -1;
178         }
179
180         if (($return_code == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) {
181                 return -1;
182         }
183
184         return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
185 }