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