]> git.mxchange.org Git - friendica.git/blob - include/salmon.php
fix typo
[friendica.git] / include / salmon.php
1 <?php
2
3 require_once('include/crypto.php');
4
5
6
7 function get_salmon_key($uri,$keyhash) {
8         $ret = array();
9
10         logger('Fetching salmon key');
11
12         $arr = lrdd($uri);
13
14         if(is_array($arr)) {
15                 foreach($arr as $a) {
16                         if($a['@attributes']['rel'] === 'magic-public-key') {
17                                 $ret[] = $a['@attributes']['href'];
18                         }
19                 }
20         }
21         else {
22                 return '';
23         }
24
25         // We have found at least one key URL
26         // If it's inline, parse it - otherwise get the key
27
28         if(count($ret)) {
29                 for($x = 0; $x < count($ret); $x ++) {
30                         if(substr($ret[$x],0,5) === 'data:') {
31                                 if(strstr($ret[$x],','))
32                                         $ret[$x] = substr($ret[$x],strpos($ret[$x],',')+1);
33                                 else
34                                         $ret[$x] = substr($ret[$x],5);
35                         }
36                         else
37                                 $ret[$x] = fetch_url($ret[$x]);
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         return '';
63 }
64
65         
66                 
67 function slapper($owner,$url,$slap) {
68
69         logger('slapper called. Data: ' . $slap);
70
71         // does contact have a salmon endpoint? 
72
73         if(! strlen($url))
74                 return;
75
76
77         if(! $owner['sprvkey']) {
78                 logger(sprintf("slapper: user '%s' (%d) does not have a salmon private key. Send failed.",
79                 $owner['username'],$owner['uid']));
80                 return;
81         }
82
83         // add all namespaces to item
84
85 $namespaces = <<< EOT
86 <entry xmlns="http://www.w3.org/2005/Atom"
87       xmlns:thr="http://purl.org/syndication/thread/1.0"
88       xmlns:at="http://purl.org/atompub/tombstones/1.0"
89       xmlns:media="http://purl.org/syndication/atommedia"
90       xmlns:dfrn="http://purl.org/macgirvin/dfrn/1.0" 
91       xmlns:as="http://activitystrea.ms/spec/1.0/"
92       xmlns:georss="http://www.georss.org/georss" 
93       xmlns:poco="http://portablecontacts.net/spec/1.0" 
94       xmlns:ostatus="http://ostatus.org/schema/1.0" 
95           xmlns:statusnet="http://status.net/schema/api/1/" >                                                                                                   >
96 EOT;
97
98         $slap = str_replace('<entry>',$namespaces,$slap);
99         
100         // create a magic envelope
101
102         $data      = base64url_encode($slap);
103         $data_type = 'application/atom+xml';
104         $encoding  = 'base64url';
105         $algorithm = 'RSA-SHA256';
106         $keyhash   = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
107
108         // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
109
110         $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
111
112         $signature   = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),$owner['sprvkey']));
113
114         $signature2  = base64url_encode(rsa_sign($data . $precomputed,$owner['sprvkey']));
115
116         $signature3  = base64url_encode(rsa_sign($data,$owner['sprvkey']));
117
118         $salmon_tpl = get_markup_template('magicsig.tpl');
119
120         $salmon = replace_macros($salmon_tpl,array(
121                 '$data'      => $data,
122                 '$encoding'  => $encoding,
123                 '$algorithm' => $algorithm,
124                 '$keyhash'   => $keyhash,
125                 '$signature' => $signature
126         ));
127
128         // slap them 
129         post_url($url,$salmon, array(
130                 'Content-type: application/magic-envelope+xml',
131                 'Content-length: ' . strlen($salmon)
132         ));
133
134         $a = get_app();
135         $return_code = $a->get_curl_code();
136
137         // check for success, e.g. 2xx
138
139         if($return_code > 299) {
140
141                 logger('slapper: compliant salmon failed. Falling back to status.net hack2');
142
143                 // Entirely likely that their salmon implementation is
144                 // non-compliant. Let's try once more, this time only signing
145                 // the data, without stripping '=' chars
146
147                 $salmon = replace_macros($salmon_tpl,array(
148                         '$data'      => $data,
149                         '$encoding'  => $encoding,
150                         '$algorithm' => $algorithm,
151                         '$keyhash'   => $keyhash,
152                         '$signature' => $signature2
153                 ));
154
155                 // slap them 
156                 post_url($url,$salmon, array(
157                         'Content-type: application/magic-envelope+xml',
158                         'Content-length: ' . strlen($salmon)
159                 ));
160                 $return_code = $a->get_curl_code();
161
162
163                 if($return_code > 299) {
164
165                         logger('slapper: compliant salmon failed. Falling back to status.net hack3');
166
167                         // Entirely likely that their salmon implementation is
168                         // non-compliant. Let's try once more, this time only signing
169                         // the data, without the precomputed blob 
170
171                         $salmon = replace_macros($salmon_tpl,array(
172                                 '$data'      => $data,
173                                 '$encoding'  => $encoding,
174                                 '$algorithm' => $algorithm,
175                                 '$keyhash'   => $keyhash,
176                                 '$signature' => $signature3
177                         ));
178
179                         // slap them 
180                         post_url($url,$salmon, array(
181                                 'Content-type: application/magic-envelope+xml',
182                                 'Content-length: ' . strlen($salmon)
183                         ));
184                         $return_code = $a->get_curl_code();
185                 }
186         }
187         logger('slapper returned ' . $return_code); 
188         if(! $return_code)
189                 return(-1);
190         if(($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))
191                 return(-1);
192
193         return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
194 }
195