]> git.mxchange.org Git - friendica.git/blob - include/salmon.php
Merge pull request #2916 from tobiasd/20160925-changes
[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         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         if(($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))
175                 return(-1);
176
177         return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
178 }
179