]> git.mxchange.org Git - friendica.git/blob - include/salmon.php
Merge pull request #2849 from annando/1610-mastodon
[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         }
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)) {
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                         } elseif (normalise_link($ret[$x]) == 'http://')
35                                 $ret[$x] = fetch_url($ret[$x]);
36                 }
37         }
38
39
40         logger('Key located: ' . print_r($ret,true));
41
42         if(count($ret) == 1) {
43
44                 // We only found one one key so we don't care if the hash matches.
45                 // If it's the wrong key we'll find out soon enough because
46                 // message verification will fail. This also covers some older
47                 // software which don't supply a keyhash. As long as they only
48                 // have one key we'll be right.
49
50                 return $ret[0];
51         }
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         return '';
61 }
62
63
64
65 function slapper($owner,$url,$slap) {
66
67         // does contact have a salmon endpoint?
68
69         if(! strlen($url))
70                 return;
71
72
73         if(! $owner['sprvkey']) {
74                 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
75                 $owner['username'],$owner['uid']));
76                 return;
77         }
78
79         logger('slapper called for '.$url.'. Data: ' . $slap);
80
81         // create a magic envelope
82
83         $data      = base64url_encode($slap);
84         $data_type = 'application/atom+xml';
85         $encoding  = 'base64url';
86         $algorithm = 'RSA-SHA256';
87         $keyhash   = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
88
89         // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
90
91         $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
92
93         $signature   = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
94
95         $signature2  = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
96
97         $signature3  = base64url_encode(rsa_sign($data,$owner['sprvkey']));
98
99         $salmon_tpl = get_markup_template('magicsig.tpl');
100
101         $salmon = replace_macros($salmon_tpl,array(
102                 '$data'      => $data,
103                 '$encoding'  => $encoding,
104                 '$algorithm' => $algorithm,
105                 '$keyhash'   => $keyhash,
106                 '$signature' => $signature
107         ));
108
109         // slap them
110         post_url($url,$salmon, array(
111                 'Content-type: application/magic-envelope+xml',
112                 'Content-length: ' . strlen($salmon)
113         ));
114
115         $a = get_app();
116         $return_code = $a->get_curl_code();
117
118         // check for success, e.g. 2xx
119
120         if($return_code > 299) {
121
122                 logger('compliant salmon failed. Falling back to status.net hack2');
123
124                 // Entirely likely that their salmon implementation is
125                 // non-compliant. Let's try once more, this time only signing
126                 // the data, without stripping '=' chars
127
128                 $salmon = replace_macros($salmon_tpl,array(
129                         '$data'      => $data,
130                         '$encoding'  => $encoding,
131                         '$algorithm' => $algorithm,
132                         '$keyhash'   => $keyhash,
133                         '$signature' => $signature2
134                 ));
135
136                 // slap them
137                 post_url($url,$salmon, array(
138                         'Content-type: application/magic-envelope+xml',
139                         'Content-length: ' . strlen($salmon)
140                 ));
141                 $return_code = $a->get_curl_code();
142
143
144                 if($return_code > 299) {
145
146                         logger('compliant salmon failed. Falling back to status.net hack3');
147
148                         // Entirely likely that their salmon implementation is
149                         // non-compliant. Let's try once more, this time only signing
150                         // the data, without the precomputed blob
151
152                         $salmon = replace_macros($salmon_tpl,array(
153                                 '$data'      => $data,
154                                 '$encoding'  => $encoding,
155                                 '$algorithm' => $algorithm,
156                                 '$keyhash'   => $keyhash,
157                                 '$signature' => $signature3
158                         ));
159
160                         // slap them
161                         post_url($url,$salmon, array(
162                                 'Content-type: application/magic-envelope+xml',
163                                 'Content-length: ' . strlen($salmon)
164                         ));
165                         $return_code = $a->get_curl_code();
166                 }
167         }
168         logger('slapper for '.$url.' returned ' . $return_code);
169         if(! $return_code)
170                 return(-1);
171         if(($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))
172                 return(-1);
173
174         return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
175 }
176