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