]> git.mxchange.org Git - friendica.git/blob - include/salmon.php
4dd3717466f37beebab9efb6c220620a1cd71892
[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) > 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         }
54         else {
55                 foreach ($ret as $a) {
56                         $hash = base64url_encode(hash('sha256',$a));
57                         if ($hash == $keyhash) {
58                                 return $a;
59                         }
60                 }
61         }
62
63         return '';
64 }
65
66
67
68 function slapper($owner,$url,$slap) {
69
70         // does contact have a salmon endpoint?
71
72         if (! strlen($url)) {
73                 return;
74         }
75
76
77         if (! $owner['sprvkey']) {
78                 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
79                 $owner['username'],$owner['uid']));
80                 return;
81         }
82
83         logger('slapper called for '.$url.'. Data: ' . $slap);
84
85         // create a magic envelope
86
87         $data      = base64url_encode($slap);
88         $data_type = 'application/atom+xml';
89         $encoding  = 'base64url';
90         $algorithm = 'RSA-SHA256';
91         $keyhash   = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
92
93         // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
94
95         $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
96
97         $signature   = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
98
99         $signature2  = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
100
101         $signature3  = base64url_encode(rsa_sign($data,$owner['sprvkey']));
102
103         $salmon_tpl = get_markup_template('magicsig.tpl');
104
105         $salmon = replace_macros($salmon_tpl,array(
106                 '$data'      => $data,
107                 '$encoding'  => $encoding,
108                 '$algorithm' => $algorithm,
109                 '$keyhash'   => $keyhash,
110                 '$signature' => $signature
111         ));
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('compliant salmon failed. Falling back to status.net hack2');
127
128                 // Entirely likely that their salmon implementation is
129                 // non-compliant. Let's try once more, this time only signing
130                 // the data, without stripping '=' chars
131
132                 $salmon = replace_macros($salmon_tpl,array(
133                         '$data'      => $data,
134                         '$encoding'  => $encoding,
135                         '$algorithm' => $algorithm,
136                         '$keyhash'   => $keyhash,
137                         '$signature' => $signature2
138                 ));
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
150                         logger('compliant salmon failed. Falling back to status.net hack3');
151
152                         // Entirely likely that their salmon implementation is
153                         // non-compliant. Let's try once more, this time only signing
154                         // the data, without the precomputed blob
155
156                         $salmon = replace_macros($salmon_tpl,array(
157                                 '$data'      => $data,
158                                 '$encoding'  => $encoding,
159                                 '$algorithm' => $algorithm,
160                                 '$keyhash'   => $keyhash,
161                                 '$signature' => $signature3
162                         ));
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
180         return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
181 }
182