]> git.mxchange.org Git - friendica.git/blob - include/salmon.php
breaking up boot file (part of zot refactor)
[friendica.git] / include / salmon.php
1 <?php
2
3 require_once('library/asn1.php');
4
5 function salmon_key($pubkey) {
6         $lines = explode("\n",$pubkey);
7         unset($lines[0]);
8         unset($lines[count($lines)]);
9         $x = base64_decode(implode('',$lines));
10
11         $r = ASN_BASE::parseASNString($x);
12
13         $m = $r[0]->asnData[1]->asnData[0]->asnData[0]->asnData;
14         $e = $r[0]->asnData[1]->asnData[0]->asnData[1]->asnData;
15
16
17         return 'RSA' . '.' . $m . '.' . $e ;
18 }
19
20
21 function get_salmon_key($uri,$keyhash) {
22         $ret = array();
23
24         logger('Fetching salmon key');
25
26         $arr = lrdd($uri);
27
28         if(is_array($arr)) {
29                 foreach($arr as $a) {
30                         if($a['@attributes']['rel'] === 'magic-public-key') {
31                                 $ret[] = $a['@attributes']['href'];
32                         }
33                 }
34         }
35         else {
36                 return '';
37         }
38
39         // We have found at least one key URL
40         // If it's inline, parse it - otherwise get the key
41
42         if(count($ret)) {
43                 for($x = 0; $x < count($ret); $x ++) {
44                         if(substr($ret[$x],0,5) === 'data:') {
45                                 if(strstr($ret[$x],','))
46                                         $ret[$x] = substr($ret[$x],strpos($ret[$x],',')+1);
47                                 else
48                                         $ret[$x] = substr($ret[$x],5);
49                         }
50                         else
51                                 $ret[$x] = fetch_url($ret[$x]);
52                 }
53         }
54
55
56         logger('Key located: ' . print_r($ret,true));
57
58         if(count($ret) == 1) {
59
60                 // We only found one one key so we don't care if the hash matches.
61                 // If it's the wrong key we'll find out soon enough because 
62                 // message verification will fail. This also covers some older 
63                 // software which don't supply a keyhash. As long as they only
64                 // have one key we'll be right. 
65
66                 return $ret[0];
67         }
68         else {
69                 foreach($ret as $a) {
70                         $hash = base64url_encode(hash('sha256',$a));
71                         if($hash == $keyhash)
72                                 return $a;
73                 }
74         }
75
76         return '';
77 }
78
79         
80                 
81 function slapper($owner,$url,$slap) {
82
83         logger('slapper called. Data: ' . $slap);
84
85         // does contact have a salmon endpoint? 
86
87         if(! strlen($url))
88                 return;
89
90         // add all namespaces to item
91
92 $namespaces = <<< EOT
93 <entry xmlns="http://www.w3.org/2005/Atom"
94       xmlns:thr="http://purl.org/syndication/thread/1.0"
95       xmlns:at="http://purl.org/atompub/tombstones/1.0"
96       xmlns:media="http://purl.org/syndication/atommedia"
97       xmlns:dfrn="http://purl.org/macgirvin/dfrn/1.0" 
98       xmlns:as="http://activitystrea.ms/spec/1.0/"
99       xmlns:georss="http://www.georss.org/georss" 
100       xmlns:poco="http://portablecontacts.net/spec/1.0" 
101       xmlns:ostatus="http://ostatus.org/schema/1.0" 
102           xmlns:statusnet="http://status.net/schema/api/1/" >                                                                                                   >
103 EOT;
104
105         $slap = str_replace('<entry>',$namespaces,$slap);
106         
107         // create a magic envelope
108
109         $data      = base64url_encode($slap);
110         $data_type = 'application/atom+xml';
111         $encoding  = 'base64url';
112         $algorithm = 'RSA-SHA256';
113         $keyhash   = base64url_encode(hash('sha256',salmon_key($owner['spubkey'])));
114
115         // Setup RSA stuff to PKCS#1 sign the data
116
117         set_include_path(get_include_path() . PATH_SEPARATOR . 'library/phpsec');
118
119         require_once('library/phpsec/Crypt/RSA.php');
120
121     $rsa = new CRYPT_RSA();
122     $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
123     $rsa->setHash('sha256');
124         $rsa->loadKey($owner['sprvkey']);
125
126         // precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
127
128         $precomputed = '.YXBwbGljYXRpb24vYXRvbSt4bWw=.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
129
130         $signature  = base64url_encode($rsa->sign($data . $precomputed));
131
132         $signature2  = base64url_encode($rsa->sign($data));
133
134         $salmon_tpl = get_markup_template('magicsig.tpl');
135         $salmon = replace_macros($salmon_tpl,array(
136                 '$data'      => $data,
137                 '$encoding'  => $encoding,
138                 '$algorithm' => $algorithm,
139                 '$keyhash'   => $keyhash,
140                 '$signature' => $signature
141         ));
142
143         // slap them 
144         post_url($url,$salmon, array(
145                 'Content-type: application/magic-envelope+xml',
146                 'Content-length: ' . strlen($salmon)
147         ));
148
149         $a = get_app();
150         $return_code = $a->get_curl_code();
151
152         // check for success, e.g. 2xx
153
154         if($return_code > 299) {
155
156                 logger('slapper: compliant salmon failed. Falling back to status.net hack');
157
158                 // Entirely likely that their salmon implementation is
159                 // non-compliant. Let's try once more, this time only signing
160                 // the data, without the precomputed blob 
161
162                 $salmon = replace_macros($salmon_tpl,array(
163                         '$data'      => $data,
164                         '$encoding'  => $encoding,
165                         '$algorithm' => $algorithm,
166                         '$keyhash'   => $keyhash,
167                         '$signature' => $signature2
168                 ));
169
170                 // slap them 
171                 post_url($url,$salmon, array(
172                         'Content-type: application/magic-envelope+xml',
173                         'Content-length: ' . strlen($salmon)
174                 ));
175                 $return_code = $a->get_curl_code();
176
177         }
178         logger('slapper returned ' . $return_code); 
179         if(! $return_code)
180                 return(-1);
181         return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
182 }
183