diff options
author | Mario Vavti <mario@mariovavti.com> | 2018-08-16 08:50:52 +0200 |
---|---|---|
committer | Mario Vavti <mario@mariovavti.com> | 2018-08-16 08:50:52 +0200 |
commit | e25db4d1b57116353ad4e4909eb93824a673ffb3 (patch) | |
tree | 8ad9f1cb5f1934ab7593fb8bb291c6d398153a1f | |
parent | 2becc3273aa377574d566c052036f425105b2389 (diff) | |
parent | 32acb0ff01b8fde3801fafbd7953aa36b5933f96 (diff) | |
download | volse-hubzilla-e25db4d1b57116353ad4e4909eb93824a673ffb3.tar.gz volse-hubzilla-e25db4d1b57116353ad4e4909eb93824a673ffb3.tar.bz2 volse-hubzilla-e25db4d1b57116353ad4e4909eb93824a673ffb3.zip |
Merge remote-tracking branch 'mike/master' into dev
-rw-r--r-- | Zotlabs/Lib/ThreadStream.php | 1 | ||||
-rw-r--r-- | Zotlabs/Module/Zot_probe.php | 47 | ||||
-rw-r--r-- | Zotlabs/Widget/Wiki_list.php | 6 | ||||
-rw-r--r-- | Zotlabs/Zot6/HTTPSig.php | 507 | ||||
-rwxr-xr-x | include/items.php | 40 | ||||
-rw-r--r-- | view/pdl/mod_wiki.pdl | 1 |
6 files changed, 587 insertions, 15 deletions
diff --git a/Zotlabs/Lib/ThreadStream.php b/Zotlabs/Lib/ThreadStream.php index d0c964149..020e8729b 100644 --- a/Zotlabs/Lib/ThreadStream.php +++ b/Zotlabs/Lib/ThreadStream.php @@ -196,7 +196,6 @@ class ThreadStream { $item->set_commentable(false); } - require_once('include/channel.php'); $item->set_conversation($this); $this->threads[] = $item; diff --git a/Zotlabs/Module/Zot_probe.php b/Zotlabs/Module/Zot_probe.php new file mode 100644 index 000000000..d0c7e688f --- /dev/null +++ b/Zotlabs/Module/Zot_probe.php @@ -0,0 +1,47 @@ +<?php + +namespace Zotlabs\Module; + +use Zotlabs\Lib\Zotfinger; +use Zotlabs\Zot6\HTTPSig; + +class Zot_probe extends \Zotlabs\Web\Controller { + + function get() { + + $o .= '<h3>Zot6 Probe Diagnostic</h3>'; + + $o .= '<form action="zot_probe" method="get">'; + $o .= 'Lookup URI: <input type="text" style="width: 250px;" name="addr" value="' . $_GET['addr'] .'" /><br>'; + $o .= '<input type="submit" name="submit" value="Submit" /></form>'; + + $o .= '<br /><br />'; + + if(x($_GET,'addr')) { + $addr = $_GET['addr']; + + + $x = Zotfinger::exec($addr); + + $o .= '<pre>' . htmlspecialchars(print_array($x)) . '</pre>'; + + $headers = 'Accept: application/x-zot+json, application/jrd+json, application/json'; + + $redirects = 0; + $x = z_fetch_url($addr,true,$redirects, [ 'headers' => [ $headers ]]); + + if($x['success']) { + + $o .= '<pre>' . htmlspecialchars($x['header']) . '</pre>' . EOL; + + $o .= 'verify returns: ' . str_replace("\n",EOL,print_r(HTTPSig::verify($x),true)) . EOL; + + $o .= '<pre>' . htmlspecialchars(json_encode(json_decode($x['body']),JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)) . '</pre>' . EOL; + + } + + } + return $o; + } + +} diff --git a/Zotlabs/Widget/Wiki_list.php b/Zotlabs/Widget/Wiki_list.php index 62f32dbf0..c8d83cbe8 100644 --- a/Zotlabs/Widget/Wiki_list.php +++ b/Zotlabs/Widget/Wiki_list.php @@ -6,13 +6,17 @@ class Wiki_list { function widget($arr) { + if(argc() < 3) { + return; + } + $channel = channelx_by_n(\App::$profile_uid); $wikis = \Zotlabs\Lib\NativeWiki::listwikis($channel,get_observer_hash()); if($wikis) { return replace_macros(get_markup_template('wikilist_widget.tpl'), array( - '$header' => t('Wiki List'), + '$header' => t('Wikis'), '$channel' => $channel['channel_address'], '$wikis' => $wikis['wikis'] )); diff --git a/Zotlabs/Zot6/HTTPSig.php b/Zotlabs/Zot6/HTTPSig.php new file mode 100644 index 000000000..a0f0d3500 --- /dev/null +++ b/Zotlabs/Zot6/HTTPSig.php @@ -0,0 +1,507 @@ +<?php + +namespace Zotlabs\Zot6; + +use Zotlabs\Lib\ActivityStreams; +use Zotlabs\Lib\Webfinger; +use Zotlabs\Web\HTTPHeaders; + +/** + * @brief Implements HTTP Signatures per draft-cavage-http-signatures-10. + * + * @see https://tools.ietf.org/html/draft-cavage-http-signatures-10 + */ + +class HTTPSig { + + /** + * @brief RFC5843 + * + * @see https://tools.ietf.org/html/rfc5843 + * + * @param string $body The value to create the digest for + * @param string $alg hash algorithm (one of 'sha256','sha512') + * @return string The generated digest header string for $body + */ + + static function generate_digest_header($body,$alg = 'sha256') { + + $digest = base64_encode(hash($alg, $body, true)); + switch($alg) { + case 'sha512': + return 'SHA-512=' . $digest; + case 'sha256': + default: + return 'SHA-256=' . $digest; + break; + } + } + + static function find_headers($data,&$body) { + + // decide if $data arrived via controller submission or curl + + if(is_array($data) && $data['header']) { + if(! $data['success']) + return []; + + $h = new HTTPHeaders($data['header']); + $headers = $h->fetcharr(); + $body = $data['body']; + } + + else { + $headers = []; + $headers['(request-target)'] = strtolower($_SERVER['REQUEST_METHOD']) . ' ' . $_SERVER['REQUEST_URI']; + $headers['content-type'] = $_SERVER['CONTENT_TYPE']; + + foreach($_SERVER as $k => $v) { + if(strpos($k,'HTTP_') === 0) { + $field = str_replace('_','-',strtolower(substr($k,5))); + $headers[$field] = $v; + } + } + } + + //logger('SERVER: ' . print_r($_SERVER,true), LOGGER_ALL); + + //logger('headers: ' . print_r($headers,true), LOGGER_ALL); + + return $headers; + } + + + // See draft-cavage-http-signatures-10 + + static function verify($data,$key = '') { + + $body = $data; + $headers = null; + + $result = [ + 'signer' => '', + 'portable_id' => '', + 'header_signed' => false, + 'header_valid' => false, + 'content_signed' => false, + 'content_valid' => false + ]; + + + $headers = self::find_headers($data,$body); + + if(! $headers) + return $result; + + $sig_block = null; + + if(array_key_exists('signature',$headers)) { + $sig_block = self::parse_sigheader($headers['signature']); + } + elseif(array_key_exists('authorization',$headers)) { + $sig_block = self::parse_sigheader($headers['authorization']); + } + + if(! $sig_block) { + logger('no signature provided.', LOGGER_DEBUG); + return $result; + } + + // Warning: This log statement includes binary data + // logger('sig_block: ' . print_r($sig_block,true), LOGGER_DATA); + + $result['header_signed'] = true; + + $signed_headers = $sig_block['headers']; + if(! $signed_headers) + $signed_headers = [ 'date' ]; + + $signed_data = ''; + foreach($signed_headers as $h) { + if(array_key_exists($h,$headers)) { + $signed_data .= $h . ': ' . $headers[$h] . "\n"; + } + } + $signed_data = rtrim($signed_data,"\n"); + + $algorithm = null; + if($sig_block['algorithm'] === 'rsa-sha256') { + $algorithm = 'sha256'; + } + if($sig_block['algorithm'] === 'rsa-sha512') { + $algorithm = 'sha512'; + } + + if(! array_key_exists('keyId',$sig_block)) + return $result; + + $result['signer'] = $sig_block['keyId']; + + $key = self::get_key($key,$result['signer']); + + if(! ($key && $key['public_key'])) { + return $result; + } + + $x = rsa_verify($signed_data,$sig_block['signature'],$key['public_key'],$algorithm); + + logger('verified: ' . $x, LOGGER_DEBUG); + + if(! $x) + return $result; + + $result['portable_id'] = $key['portable_id']; + $result['header_valid'] = true; + + if(in_array('digest',$signed_headers)) { + $result['content_signed'] = true; + $digest = explode('=', $headers['digest'], 2); + if($digest[0] === 'SHA-256') + $hashalg = 'sha256'; + if($digest[0] === 'SHA-512') + $hashalg = 'sha512'; + + if(base64_encode(hash($hashalg,$body,true)) === $digest[1]) { + $result['content_valid'] = true; + } + + logger('Content_Valid: ' . (($result['content_valid']) ? 'true' : 'false')); + } + + return $result; + } + + static function get_key($key,$id) { + + if($key) { + if(function_exists($key)) { + return $key($id); + } + return [ 'public_key' => $key ]; + } + + $key = self::get_webfinger_key($id); + + if(! $key) { + $key = self::get_activitystreams_key($id); + } + + return $key; + + } + + + function convertKey($key) { + + if(strstr($key,'RSA ')) { + return rsatopem($key); + } + elseif(substr($key,0,5) === 'data:') { + return convert_salmon_key($key); + } + else { + return $key; + } + + } + + + /** + * @brief + * + * @param string $id + * @return boolean|string + * false if no pub key found, otherwise return the pub key + */ + + function get_activitystreams_key($id) { + + $x = q("select * from xchan left join hubloc on xchan_hash = hubloc_hash where hubloc_addr = '%s' or hubloc_id_url = '%s' limit 1", + dbesc(str_replace('acct:','',$id)), + dbesc($id) + ); + + if($x && $x[0]['xchan_pubkey']) { + return [ 'portable_id' => $x[0]['xchan_hash'], 'public_key' => $x[0]['xchan_pubkey'] , 'hubloc' => $x[0] ]; + } + + $r = ActivityStreams::fetch_property($id); + + if($r) { + if(array_key_exists('publicKey',$j) && array_key_exists('publicKeyPem',$j['publicKey']) && array_key_exists('id',$j['publicKey'])) { + if($j['publicKey']['id'] === $id || $j['id'] === $id) { + return [ 'public_key' => self::convertKey($j['publicKey']['publicKeyPem']), 'portable_id' => '', 'hubloc' => [] ]; + } + } + } + + return false; + } + + + function get_webfinger_key($id) { + + $x = q("select * from xchan left join hubloc on xchan_hash = hubloc_hash where hubloc_addr = '%s' or hubloc_id_url = '%s' limit 1", + dbesc(str_replace('acct:','',$id)), + dbesc($id) + ); + + if($x && $x[0]['xchan_pubkey']) { + return [ 'portable_id' => $x[0]['xchan_hash'], 'public_key' => $x[0]['xchan_pubkey'] , 'hubloc' => $x[0] ]; + } + + $wf = Webfinger::exec($id); + $key = [ 'portable_id' => '', 'public_key' => '', 'hubloc' => [] ]; + + if($wf) { + if(array_key_exists('properties',$wf) && array_key_exists('https://w3id.org/security/v1#publicKeyPem',$wf['properties'])) { + $key['public_key'] = self::convertKey($wf['properties']['https://w3id.org/security/v1#publicKeyPem']); + } + if(array_key_exists('links', $wf) && is_array($wf['links'])) { + foreach($wf['links'] as $l) { + if(! (is_array($l) && array_key_exists('rel',$l))) { + continue; + } + if($l['rel'] === 'magic-public-key' && array_key_exists('href',$l) && $key['public_key'] === EMPTY_STR) { + $key['public_key'] = self::convertKey($l['href']); + } + } + } + } + + return (($key['public_key']) ? $key : false); + } + + + function get_zotfinger_key($id) { + + $x = q("select * from xchan left join hubloc on xchan_hash = hubloc_hash where hubloc_addr = '%s' or hubloc_id_url = '%s' limit 1", + dbesc(str_replace('acct:','',$id)), + dbesc($id) + ); + if($x && $x[0]['xchan_pubkey']) { + return [ 'portable_id' => $x[0]['xchan_hash'], 'public_key' => $x[0]['xchan_pubkey'] , 'hubloc' => $x[0] ]; + } + + $wf = Webfinger::exec($id); + $key = [ 'portable_id' => '', 'public_key' => '', 'hubloc' => [] ]; + + if($wf) { + if(array_key_exists('properties',$wf) && array_key_exists('https://w3id.org/security/v1#publicKeyPem',$wf['properties'])) { + $key['public_key'] = self::convertKey($wf['properties']['https://w3id.org/security/v1#publicKeyPem']); + } + if(array_key_exists('links', $wf) && is_array($wf['links'])) { + foreach($wf['links'] as $l) { + if(! (is_array($l) && array_key_exists('rel',$l))) { + continue; + } + if($l['rel'] === 'http://purl.org/zot/protocol/6.0' && array_key_exists('href',$l) && $l['href'] !== EMPTY_STR) { + $z = \Zotlabs\Lib\Zotfinger::exec($l['href']); + if($z) { + $i = Zotlabs\Lib\Libzot::import_xchan($z['data']); + if($i['success']) { + $key['portable_id'] = $i['hash']; + + $x = q("select * from xchan left join hubloc on xchan_hash = hubloc_hash where hubloc_id_url = '%s' limit 1", + dbesc($l['href']) + ); + if($x) { + $key['hubloc'] = $x[0]; + } + } + } + } + if($l['rel'] === 'magic-public-key' && array_key_exists('href',$l) && $key['public_key'] === EMPTY_STR) { + $key['public_key'] = self::convertKey($l['href']); + } + } + } + } + + return (($key['public_key']) ? $key : false); + } + + + /** + * @brief + * + * @param array $head + * @param string $prvkey + * @param string $keyid (optional, default '') + * @param boolean $auth (optional, default false) + * @param string $alg (optional, default 'sha256') + * @param array $encryption [ 'key', 'algorithm' ] or false + * @return array + */ + static function create_sig($head, $prvkey, $keyid = EMPTY_STR, $auth = false, $alg = 'sha256', $encryption = false ) { + + $return_headers = []; + + if($alg === 'sha256') { + $algorithm = 'rsa-sha256'; + } + if($alg === 'sha512') { + $algorithm = 'rsa-sha512'; + } + + $x = self::sign($head,$prvkey,$alg); + + $headerval = 'keyId="' . $keyid . '",algorithm="' . $algorithm . '",headers="' . $x['headers'] . '",signature="' . $x['signature'] . '"'; + + if($encryption) { + $x = crypto_encapsulate($headerval,$encryption['key'],$encryption['algorithm']); + if(is_array($x)) { + $headerval = 'iv="' . $x['iv'] . '",key="' . $x['key'] . '",alg="' . $x['alg'] . '",data="' . $x['data'] . '"'; + } + } + + if($auth) { + $sighead = 'Authorization: Signature ' . $headerval; + } + else { + $sighead = 'Signature: ' . $headerval; + } + + if($head) { + foreach($head as $k => $v) { + // strip the request-target virtual header from the output headers + if($k === '(request-target)') { + continue; + } + $return_headers[] = $k . ': ' . $v; + } + } + $return_headers[] = $sighead; + + return $return_headers; + } + + /** + * @brief set headers + * + * @param array $headers + * @return void + */ + + + static function set_headers($headers) { + if($headers && is_array($headers)) { + foreach($headers as $h) { + header($h); + } + } + } + + + /** + * @brief + * + * @param array $head + * @param string $prvkey + * @param string $alg (optional) default 'sha256' + * @return array + */ + + static function sign($head, $prvkey, $alg = 'sha256') { + + $ret = []; + + $headers = ''; + $fields = ''; + + if($head) { + foreach($head as $k => $v) { + $headers .= strtolower($k) . ': ' . trim($v) . "\n"; + if($fields) + $fields .= ' '; + + $fields .= strtolower($k); + } + // strip the trailing linefeed + $headers = rtrim($headers,"\n"); + } + + $sig = base64_encode(rsa_sign($headers,$prvkey,$alg)); + + $ret['headers'] = $fields; + $ret['signature'] = $sig; + + return $ret; + } + + /** + * @brief + * + * @param string $header + * @return array associate array with + * - \e string \b keyID + * - \e string \b algorithm + * - \e array \b headers + * - \e string \b signature + */ + + static function parse_sigheader($header) { + + $ret = []; + $matches = []; + + // if the header is encrypted, decrypt with (default) site private key and continue + + if(preg_match('/iv="(.*?)"/ism',$header,$matches)) + $header = self::decrypt_sigheader($header); + + if(preg_match('/keyId="(.*?)"/ism',$header,$matches)) + $ret['keyId'] = $matches[1]; + if(preg_match('/algorithm="(.*?)"/ism',$header,$matches)) + $ret['algorithm'] = $matches[1]; + if(preg_match('/headers="(.*?)"/ism',$header,$matches)) + $ret['headers'] = explode(' ', $matches[1]); + if(preg_match('/signature="(.*?)"/ism',$header,$matches)) + $ret['signature'] = base64_decode(preg_replace('/\s+/','',$matches[1])); + + if(($ret['signature']) && ($ret['algorithm']) && (! $ret['headers'])) + $ret['headers'] = [ 'date' ]; + + return $ret; + } + + + /** + * @brief + * + * @param string $header + * @param string $prvkey (optional), if not set use site private key + * @return array|string associative array, empty string if failue + * - \e string \b iv + * - \e string \b key + * - \e string \b alg + * - \e string \b data + */ + + static function decrypt_sigheader($header, $prvkey = null) { + + $iv = $key = $alg = $data = null; + + if(! $prvkey) { + $prvkey = get_config('system', 'prvkey'); + } + + $matches = []; + + if(preg_match('/iv="(.*?)"/ism',$header,$matches)) + $iv = $matches[1]; + if(preg_match('/key="(.*?)"/ism',$header,$matches)) + $key = $matches[1]; + if(preg_match('/alg="(.*?)"/ism',$header,$matches)) + $alg = $matches[1]; + if(preg_match('/data="(.*?)"/ism',$header,$matches)) + $data = $matches[1]; + + if($iv && $key && $alg && $data) { + return crypto_unencapsulate([ 'encrypted' => true, 'iv' => $iv, 'key' => $key, 'alg' => $alg, 'data' => $data ] , $prvkey); + } + + return ''; + } + +} diff --git a/include/items.php b/include/items.php index 9dd5d005b..ee7bfd5bc 100755 --- a/include/items.php +++ b/include/items.php @@ -234,10 +234,11 @@ function can_comment_on_post($observer_xchan, $item) { // logger('Comment_policy: ' . $item['comment_policy'], LOGGER_DEBUG); $x = [ - 'observer_hash' => $observer_xchan, - 'item' => $item, - 'allowed' => 'unset' + 'observer_hash' => $observer_xchan, + 'item' => $item, + 'allowed' => 'unset' ]; + /** * @hooks can_comment_on_post * Called when deciding whether or not to present a comment box for a post. @@ -245,26 +246,34 @@ function can_comment_on_post($observer_xchan, $item) { * * \e array \b item * * \e boolean \b allowed - return value */ + call_hooks('can_comment_on_post', $x); - if($x['allowed'] !== 'unset') + + if($x['allowed'] !== 'unset') { return $x['allowed']; + } - if(! $observer_xchan) + if(! $observer_xchan) { return false; + } - if($item['comment_policy'] === 'none') + if($item['comment_policy'] === 'none') { return false; + } - if(comments_are_now_closed($item)) + if(comments_are_now_closed($item)) { return false; + } - if($observer_xchan === $item['author_xchan'] || $observer_xchan === $item['owner_xchan']) + if($observer_xchan === $item['author_xchan'] || $observer_xchan === $item['owner_xchan']) { return true; + } switch($item['comment_policy']) { case 'self': - if($observer_xchan === $item['author_xchan'] || $observer_xchan === $item['owner_xchan']) + if($observer_xchan === $item['author_xchan'] || $observer_xchan === $item['owner_xchan']) { return true; + } break; case 'public': case 'authenticated': @@ -276,17 +285,22 @@ function can_comment_on_post($observer_xchan, $item) { case 'any connections': case 'contacts': case '': - if(array_key_exists('owner',$item) && get_abconfig($item['uid'],$item['owner']['abook_xchan'],'their_perms','post_comments')) { - return true; + if(local_channel() && get_abconfig(local_channel(),$item['owner_xchan'],'their_perms','post_comments')) { + return true; + } + if(intval($item['item_wall']) && perm_is_allowed($item['uid'],$observer_xchan,'post_comments')) { + return true; } break; default: break; } - if(strstr($item['comment_policy'],'network:') && strstr($item['comment_policy'],'red')) + if(strstr($item['comment_policy'],'network:') && strstr($item['comment_policy'],'red')) { return true; - if(strstr($item['comment_policy'],'site:') && strstr($item['comment_policy'],App::get_hostname())) + } + if(strstr($item['comment_policy'],'site:') && strstr($item['comment_policy'],App::get_hostname())) { return true; + } return false; } diff --git a/view/pdl/mod_wiki.pdl b/view/pdl/mod_wiki.pdl index be86be7e3..2e99ea36b 100644 --- a/view/pdl/mod_wiki.pdl +++ b/view/pdl/mod_wiki.pdl @@ -1,5 +1,6 @@ [region=aside] [widget=vcard][/widget] +[widget=wiki_list][/widget] [widget=wiki_pages][/widget] [/region] [region=right_aside] |