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