2017-08-29 02:06:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Zotlabs\Lib;
|
|
|
|
|
|
|
|
require_once('library/jsonld/jsonld.php');
|
|
|
|
|
|
|
|
class LDSignatures {
|
|
|
|
|
|
|
|
|
|
|
|
static function verify($data,$pubkey) {
|
|
|
|
|
|
|
|
$ohash = self::hash(self::signable_options($data['signature']));
|
2017-08-29 02:12:35 +00:00
|
|
|
$dhash = self::hash(self::signable_data($data));
|
2017-08-29 02:06:01 +00:00
|
|
|
|
2018-12-14 01:16:01 +00:00
|
|
|
$x = Crypto::verify($ohash . $dhash,base64_decode($data['signature']['signatureValue']), $pubkey);
|
2017-09-21 04:20:00 +00:00
|
|
|
logger('LD-verify: ' . intval($x));
|
|
|
|
|
|
|
|
return $x;
|
2017-08-29 02:06:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 01:46:00 +00:00
|
|
|
static function dopplesign(&$data,$channel) {
|
2017-09-13 03:24:57 +00:00
|
|
|
// remove for the time being - performance issues
|
|
|
|
// $data['magicEnv'] = self::salmon_sign($data,$channel);
|
2017-08-30 01:46:00 +00:00
|
|
|
return self::sign($data,$channel);
|
|
|
|
}
|
2017-08-29 02:06:01 +00:00
|
|
|
|
|
|
|
static function sign($data,$channel) {
|
2017-09-12 01:56:17 +00:00
|
|
|
|
2017-08-29 02:06:01 +00:00
|
|
|
$options = [
|
2019-07-19 03:38:36 +00:00
|
|
|
'type' => 'RsaSignature2017',
|
|
|
|
'nonce' => random_string(64),
|
|
|
|
'creator' => channel_url($channel),
|
2019-11-08 20:16:29 +00:00
|
|
|
'created' => datetime_convert('UTC','UTC', 'now', 'Y-m-d\TH:i:s\Z')
|
2017-08-29 02:06:01 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$ohash = self::hash(self::signable_options($options));
|
|
|
|
$dhash = self::hash(self::signable_data($data));
|
2018-12-14 01:16:01 +00:00
|
|
|
$options['signatureValue'] = base64_encode(Crypto::sign($ohash . $dhash,$channel['channel_prvkey']));
|
2017-08-29 02:06:01 +00:00
|
|
|
|
|
|
|
$signed = array_merge([
|
2017-09-20 04:40:38 +00:00
|
|
|
'@context' => [
|
|
|
|
ACTIVITYSTREAMS_JSONLD_REV,
|
2019-05-13 05:29:16 +00:00
|
|
|
'https://w3id.org/security/v1',
|
|
|
|
z_root() . ZOT_APSCHEMA_REV
|
|
|
|
],
|
|
|
|
],$options);
|
2017-08-29 02:06:01 +00:00
|
|
|
|
|
|
|
return $signed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static function signable_data($data) {
|
|
|
|
|
|
|
|
$newdata = [];
|
|
|
|
if($data) {
|
|
|
|
foreach($data as $k => $v) {
|
|
|
|
if(! in_array($k,[ 'signature' ])) {
|
2017-09-12 01:56:17 +00:00
|
|
|
$newdata[$k] = $v;
|
2017-08-29 02:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json_encode($newdata,JSON_UNESCAPED_SLASHES);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static function signable_options($options) {
|
|
|
|
|
|
|
|
$newopts = [ '@context' => 'https://w3id.org/identity/v1' ];
|
|
|
|
if($options) {
|
|
|
|
foreach($options as $k => $v) {
|
|
|
|
if(! in_array($k,[ 'type','id','signatureValue' ])) {
|
|
|
|
$newopts[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json_encode($newopts,JSON_UNESCAPED_SLASHES);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function hash($obj) {
|
2017-09-12 01:56:17 +00:00
|
|
|
|
2017-08-29 02:06:01 +00:00
|
|
|
return hash('sha256',self::normalise($obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static function normalise($data) {
|
|
|
|
if(is_string($data)) {
|
|
|
|
$data = json_decode($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(! is_object($data))
|
|
|
|
return '';
|
|
|
|
|
2017-09-20 02:15:15 +00:00
|
|
|
jsonld_set_document_loader('jsonld_document_loader');
|
2017-09-25 03:18:36 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$d = jsonld_normalize($data,[ 'algorithm' => 'URDNA2015', 'format' => 'application/nquads' ]);
|
|
|
|
}
|
|
|
|
catch (\Exception $e) {
|
2020-05-06 04:34:31 +00:00
|
|
|
// Don't log the exception - this can exhaust memory
|
|
|
|
// logger('normalise error:' . print_r($e,true));
|
2017-09-25 03:18:36 +00:00
|
|
|
logger('normalise error: ' . print_r($data,true));
|
|
|
|
}
|
2017-09-20 02:15:15 +00:00
|
|
|
|
2017-09-25 03:18:36 +00:00
|
|
|
return $d;
|
2017-08-29 02:06:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 01:46:00 +00:00
|
|
|
static function salmon_sign($data,$channel) {
|
|
|
|
|
2017-08-30 05:08:37 +00:00
|
|
|
$arr = $data;
|
2017-08-30 01:46:00 +00:00
|
|
|
$data = json_encode($data,JSON_UNESCAPED_SLASHES);
|
|
|
|
$data = base64url_encode($data, false); // do not strip padding
|
|
|
|
$data_type = 'application/activity+json';
|
|
|
|
$encoding = 'base64url';
|
|
|
|
$algorithm = 'RSA-SHA256';
|
2019-07-19 03:38:36 +00:00
|
|
|
$keyhash = base64url_encode(channel_url($channel));
|
2017-08-30 01:46:00 +00:00
|
|
|
|
|
|
|
$data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$data);
|
|
|
|
|
|
|
|
// precomputed base64url encoding of data_type, encoding, algorithm concatenated with periods
|
|
|
|
|
|
|
|
$precomputed = '.' . base64url_encode($data_type,false) . '.YmFzZTY0dXJs.UlNBLVNIQTI1Ng==';
|
|
|
|
|
2018-12-14 01:16:01 +00:00
|
|
|
$signature = base64url_encode(Crypto::sign($data . $precomputed,$channel['channel_prvkey']));
|
2017-08-30 01:46:00 +00:00
|
|
|
|
|
|
|
return ([
|
2019-07-19 03:38:36 +00:00
|
|
|
'id' => $arr['id'],
|
|
|
|
'meData' => $data,
|
|
|
|
'meDataType' => $data_type,
|
|
|
|
'meEncoding' => $encoding,
|
|
|
|
'meAlgorithm' => $algorithm,
|
|
|
|
'meCreator' => channel_url($channel),
|
2017-08-30 01:46:00 +00:00
|
|
|
'meSignatureValue' => $signature
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-29 02:06:01 +00:00
|
|
|
}
|