]> git.mxchange.org Git - friendica.git/blob - include/salmon.php
Merge branch 'pull'
[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         // add all namespaces to item
77
78 $namespaces = <<< EOT
79 <entry xmlns="http://www.w3.org/2005/Atom"
80       xmlns:thr="http://purl.org/syndication/thread/1.0"
81       xmlns:at="http://purl.org/atompub/tombstones/1.0"
82       xmlns:media="http://purl.org/syndication/atommedia"
83       xmlns:dfrn="http://purl.org/macgirvin/dfrn/1.0" 
84       xmlns:as="http://activitystrea.ms/spec/1.0/"
85       xmlns:georss="http://www.georss.org/georss" 
86       xmlns:poco="http://portablecontacts.net/spec/1.0" 
87       xmlns:ostatus="http://ostatus.org/schema/1.0" 
88           xmlns:statusnet="http://status.net/schema/api/1/" >                                                                                                   >
89 EOT;
90
91         $slap = str_replace('<entry>',$namespaces,$slap);
92         
93         // create a magic envelope
94
95         $data      = base64url_encode($slap);
96         $data_type = 'application/atom+xml';
97         $encoding  = 'base64url';
98         $algorithm = 'RSA-SHA256';
99         $keyhash   = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])),true);
100
101         // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
102
103         $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
104
105         $signature   = base64url_encode(rsa_sign(str_replace('=','',$data . $precomputed),true),$owner['sprvkey']);
106
107         $signature2  = base64url_encode(rsa_sign($data . $precomputed),$owner['sprvkey']);
108
109         $signature3  = base64url_encode(rsa_sign($data),$owner['sprvkey']);
110
111         $salmon_tpl = get_markup_template('magicsig.tpl');
112
113         $salmon = replace_macros($salmon_tpl,array(
114                 '$data'      => $data,
115                 '$encoding'  => $encoding,
116                 '$algorithm' => $algorithm,
117                 '$keyhash'   => $keyhash,
118                 '$signature' => $signature
119         ));
120
121         // slap them 
122         post_url($url,$salmon, array(
123                 'Content-type: application/magic-envelope+xml',
124                 'Content-length: ' . strlen($salmon)
125         ));
126
127         $a = get_app();
128         $return_code = $a->get_curl_code();
129
130         // check for success, e.g. 2xx
131
132         if($return_code > 299) {
133
134                 logger('slapper: compliant salmon failed. Falling back to status.net hack2');
135
136                 // Entirely likely that their salmon implementation is
137                 // non-compliant. Let's try once more, this time only signing
138                 // the data, without stripping '=' chars
139
140                 $salmon = replace_macros($salmon_tpl,array(
141                         '$data'      => $data,
142                         '$encoding'  => $encoding,
143                         '$algorithm' => $algorithm,
144                         '$keyhash'   => $keyhash,
145                         '$signature' => $signature2
146                 ));
147
148                 // slap them 
149                 post_url($url,$salmon, array(
150                         'Content-type: application/magic-envelope+xml',
151                         'Content-length: ' . strlen($salmon)
152                 ));
153                 $return_code = $a->get_curl_code();
154
155
156                 if($return_code > 299) {
157
158                         logger('slapper: compliant salmon failed. Falling back to status.net hack3');
159
160                         // Entirely likely that their salmon implementation is
161                         // non-compliant. Let's try once more, this time only signing
162                         // the data, without the precomputed blob 
163
164                         $salmon = replace_macros($salmon_tpl,array(
165                                 '$data'      => $data,
166                                 '$encoding'  => $encoding,
167                                 '$algorithm' => $algorithm,
168                                 '$keyhash'   => $keyhash,
169                                 '$signature' => $signature3
170                         ));
171
172                         // slap them 
173                         post_url($url,$salmon, array(
174                                 'Content-type: application/magic-envelope+xml',
175                                 'Content-length: ' . strlen($salmon)
176                         ));
177                         $return_code = $a->get_curl_code();
178                 }
179         }
180         logger('slapper returned ' . $return_code); 
181         if(! $return_code)
182                 return(-1);
183         return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
184 }
185