3 require_once('library/asn1.php');
5 function salmon_key($pubkey) {
6 $lines = explode("\n",$pubkey);
8 unset($lines[count($lines)]);
9 $x = base64_decode(implode('',$lines));
11 $r = ASN_BASE::parseASNString($x);
13 $m = $r[0]->asnData[1]->asnData[0]->asnData[0]->asnData;
14 $e = $r[0]->asnData[1]->asnData[0]->asnData[1]->asnData;
17 return 'RSA' . '.' . $m . '.' . $e ;
21 function base64url_encode($s) {
22 return strtr(base64_encode($s),'+/','-_');
25 function base64url_decode($s) {
26 return base64_decode(strtr($s,'-_','+/'));
29 function get_salmon_key($uri,$keyhash) {
32 $debugging = get_config('system','debugging');
34 file_put_contents('salmon.out', "\n" . 'Fetch key' . "\n", FILE_APPEND);
36 if(strstr($uri,'@')) {
37 $arr = webfinger($uri);
39 file_put_contents('salmon.out', "\n" . 'Fetch key from webfinger' . "\n", FILE_APPEND);
42 $html = fetch_url($uri);
44 $h = $a->get_curl_headers();
46 file_put_contents('salmon.out', "\n" . 'Fetch key via HTML header: ' . $h . "\n", FILE_APPEND);
48 $l = explode("\n",$h);
50 foreach($l as $line) {
53 file_put_contents('salmon.out', "\n" . $line . "\n", FILE_APPEND);
54 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
57 file_put_contents('salmon.out', "\n" . 'Fetch key via Link from header: ' . $link . "\n", FILE_APPEND);
65 require_once('library/HTML5/Parser.php');
66 $dom = HTML5_Parser::parse($html);
71 $items = $dom->getElementsByTagName('link');
73 foreach($items as $item) {
74 $x = $item->getAttribute('rel');
76 $link = $item->getAttribute('href');
78 file_put_contents('salmon.out', "\n" . 'Fetch key via HTML body' . $link . "\n", FILE_APPEND);
87 $arr = fetch_xrd_links($link);
91 if($a['@attributes']['rel'] === 'magic-public-key') {
92 $ret[] = $a['@attributes']['href'];
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);
102 $ret[$x] = substr($ret[$x],5);
105 $ret[$x] = fetch_url($ret[$x]);
109 file_put_contents('salmon.out', "\n" . 'Key located: ' . print_r($ret,true) . "\n", FILE_APPEND);
111 if(count($ret) == 1) {
115 foreach($ret as $a) {
116 $hash = base64url_encode(hash('sha256',$a));
117 if($hash == $keyhash)