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