]> git.mxchange.org Git - friendica.git/blob - include/salmon.php
magic-envelope verification, status.net appears to do it wrong.
[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         return strtr(base64_encode($s),'+/','-_');
23 }
24
25 function base64url_decode($s) {
26         return base64_decode(strtr($s,'-_','+/'));
27 }
28
29 function get_salmon_key($uri,$keyhash) {
30         $ret = array();
31
32         $debugging = get_config('system','debugging');
33         if($debugging)          
34                 file_put_contents('salmon.out', "\n" . 'Fetch key' . "\n", FILE_APPEND);
35
36         if(strstr($uri,'@')) {  
37                 $arr = webfinger($uri);
38                 if($debugging)
39                         file_put_contents('salmon.out', "\n" . 'Fetch key from webfinger' . "\n", FILE_APPEND);
40         }
41         else {
42                 $html = fetch_url($uri);
43                 $a = get_app();
44                 $h = $a->get_curl_headers();
45                 if($debugging)
46                         file_put_contents('salmon.out', "\n" . 'Fetch key via HTML header: ' . $h . "\n", FILE_APPEND);
47
48                 $l = explode("\n",$h);
49                 if(count($l)) {
50                         foreach($l as $line) {
51                                 
52                                 if($debugging)
53                                         file_put_contents('salmon.out', "\n" . $line . "\n", FILE_APPEND);
54                                 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
55                                         $link = $matches[1];
56                                         if($debugging)
57                                                 file_put_contents('salmon.out', "\n" . 'Fetch key via Link from header: ' . $link . "\n", FILE_APPEND);
58                                         break;
59                                 }
60                         }
61                 }
62         }
63
64         if(! isset($link)) {
65                 require_once('library/HTML5/Parser.php');
66                 $dom = HTML5_Parser::parse($html);
67
68                 if(! $dom)
69                         return '';
70
71                 $items = $dom->getElementsByTagName('link');
72
73                 foreach($items as $item) {
74                         $x = $item->getAttribute('rel');
75                         if($x == "lrdd") {
76                                 $link = $item->getAttribute('href');
77                                 if($debugging)
78                                         file_put_contents('salmon.out', "\n" . 'Fetch key via HTML body' . $link . "\n", FILE_APPEND);
79                                 break;
80                         }
81                 }
82         }
83
84         if(! isset($link))
85                 return '';
86
87         $arr = fetch_xrd_links($link);
88
89         if($arr) {
90                 foreach($arr as $a) {
91                         if($a['@attributes']['rel'] === 'magic-public-key') {
92                                 $ret[] = $a['@attributes']['href'];
93                         }
94                 }
95         }
96         if(count($ret)) {
97                 for($x = 0; $x < count($ret); $x ++) {
98                         if(substr($ret[$x],0,5) === 'data:') {
99                                 if(strstr($ret[$x],','))
100                                         $ret[$x] = substr($ret[$x],strpos($ret[$x],',')+1);
101                                 else
102                                         $ret[$x] = substr($ret[$x],5);
103                         }
104                         else
105                                 $ret[$x] = fetch_url($ret[$x]);
106                 }
107         }
108         if($debugging)
109                 file_put_contents('salmon.out', "\n" . 'Key located: ' . print_r($ret,true) . "\n", FILE_APPEND);
110
111         if(count($ret) == 1) {
112                 return $ret[0];
113         }
114         else {
115                 foreach($ret as $a) {
116                         $hash = base64url_encode(hash('sha256',$a));
117                         if($hash == $keyhash)
118                                 return $a;
119                 }
120         }
121
122         return '';
123 }
124
125         
126                 
127