]> git.mxchange.org Git - friendica.git/blob - include/salmon.php
Merge pull request #3112 from Quix0r/rewrites/coding-convention
[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         }
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
76         if (! $owner['sprvkey']) {
77                 logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
78                 $owner['username'],$owner['uid']));
79                 return;
80         }
81
82         logger('slapper called for '.$url.'. Data: ' . $slap);
83
84         // create a magic envelope
85
86         $data      = base64url_encode($slap);
87         $data_type = 'application/atom+xml';
88         $encoding  = 'base64url';
89         $algorithm = 'RSA-SHA256';
90         $keyhash   = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
91
92         // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
93
94         $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
95
96         $signature   = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
97
98         $signature2  = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
99
100         $signature3  = base64url_encode(rsa_sign($data,$owner['sprvkey']));
101
102         $salmon_tpl = get_markup_template('magicsig.tpl');
103
104         $salmon = replace_macros($salmon_tpl,array(
105                 '$data'      => $data,
106                 '$encoding'  => $encoding,
107                 '$algorithm' => $algorithm,
108                 '$keyhash'   => $keyhash,
109                 '$signature' => $signature
110         ));
111
112         // slap them
113         post_url($url,$salmon, array(
114                 'Content-type: application/magic-envelope+xml',
115                 'Content-length: ' . strlen($salmon)
116         ));
117
118         $a = get_app();
119         $return_code = $a->get_curl_code();
120
121         // check for success, e.g. 2xx
122
123         if ($return_code > 299) {
124
125                 logger('compliant salmon failed. Falling back to status.net hack2');
126
127                 // Entirely likely that their salmon implementation is
128                 // non-compliant. Let's try once more, this time only signing
129                 // the data, without stripping '=' chars
130
131                 $salmon = replace_macros($salmon_tpl,array(
132                         '$data'      => $data,
133                         '$encoding'  => $encoding,
134                         '$algorithm' => $algorithm,
135                         '$keyhash'   => $keyhash,
136                         '$signature' => $signature2
137                 ));
138
139                 // slap them
140                 post_url($url,$salmon, array(
141                         'Content-type: application/magic-envelope+xml',
142                         'Content-length: ' . strlen($salmon)
143                 ));
144                 $return_code = $a->get_curl_code();
145
146
147                 if ($return_code > 299) {
148
149                         logger('compliant salmon failed. Falling back to status.net hack3');
150
151                         // Entirely likely that their salmon implementation is
152                         // non-compliant. Let's try once more, this time only signing
153                         // the data, without the precomputed blob
154
155                         $salmon = replace_macros($salmon_tpl,array(
156                                 '$data'      => $data,
157                                 '$encoding'  => $encoding,
158                                 '$algorithm' => $algorithm,
159                                 '$keyhash'   => $keyhash,
160                                 '$signature' => $signature3
161                         ));
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
179         return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
180 }
181