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