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