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