123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- addPlugin('OStatus');
- class DiasporaPlugin extends Plugin
- {
- const PLUGIN_VERSION = '0.2.0';
- const REL_SEED_LOCATION = 'http://joindiaspora.com/seed_location';
- const REL_GUID = 'http://joindiaspora.com/guid';
- const REL_PUBLIC_KEY = 'diaspora-public-key';
- public function onEndAttachPubkeyToUserXRD(Magicsig $magicsig, XML_XRD $xrd, Profile $target)
- {
-
-
- assert($magicsig->publicKey instanceof \phpseclib\Crypt\RSA);
- $xrd->links[] = new XML_XRD_Element_Link(self::REL_PUBLIC_KEY,
- base64_encode($magicsig->exportPublicKey()), 'RSA');
-
-
- $xrd->links[] = new XML_XRD_Element_Link(self::REL_GUID,
- strtolower($magicsig->toFingerprint()));
- }
- public function onMagicsigPublicKeyFromXRD(XML_XRD $xrd, &$pubkey)
- {
-
- $link = $xrd->get(self::REL_PUBLIC_KEY, 'RSA');
- if (!is_null($link)) {
-
- $pkcs1 = base64_decode($link->href);
- $magicsig = new Magicsig(Magicsig::DEFAULT_SIGALG);
- try {
-
- $magicsig->loadPublicKeyPKCS1($pkcs1);
-
-
- $pubkey = 'data:application/magic-public-key,'.$magicsig->toString();
- common_debug('magic-public-key found in diaspora-public-key: '.$pubkey);
- return false;
- } catch (ServerException $e) {
- common_log(LOG_WARNING, $e->getMessage());
- }
- }
- return true;
- }
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = array('name' => 'Diaspora',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Mikael Nordfeldth',
- 'homepage' => GNUSOCIAL_ENGINE_URL,
-
- 'rawdescription' => _m('Follow people across social networks that implement '.
- 'the <a href="https://diasporafoundation.org/">Diaspora</a> federation protocol.'));
- return true;
- }
- public function onStartMagicEnvelopeToXML(MagicEnvelope $magic_env, XMLStringer $xs, $flavour=null, Profile $target=null)
- {
-
-
- if ($flavour !== 'diaspora') {
- return true;
- }
-
-
-
- $xs->elementStart('diaspora', array('xmlns'=>'https://joindiaspora.com/protocol'));
-
- $inner_key = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_CBC);
- $inner_key->setKeyLength(256);
- $inner_key->setKey(common_random_rawstr(32));
- $inner_key->setIV(common_random_rawstr(16));
-
- $decrypted_header = sprintf('<decrypted_header><iv>%1$s</iv><aes_key>%2$s</aes_key><author_id>%3$s</author_id></decrypted_header>',
- base64_encode($inner_key->iv),
- base64_encode($inner_key->key),
- $magic_env->getActor()->getAcctUri());
-
- $outer_key = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_CBC);
- $outer_key->setKeyLength(256);
- $outer_key->setKey(common_random_rawstr(32));
- $outer_key->setIV(common_random_rawstr(16));
-
- $ciphertext = $outer_key->encrypt($decrypted_header, \phpseclib\Crypt\RSA::PADDING_PKCS1);
-
- $outer_bundle = json_encode(array(
- 'iv' => base64_encode($outer_key->iv),
- 'key' => base64_encode($outer_key->key),
- ));
-
- common_debug('Diaspora creating "outer aes key bundle", will require magic-public-key');
- $key_fetcher = new MagicEnvelope();
- $remote_keys = $key_fetcher->getKeyPair($target, true);
- $enc_outer = $remote_keys->publicKey->encrypt($outer_bundle, \phpseclib\Crypt\RSA::PADDING_PKCS1);
-
- $enc_header = json_encode(array(
- 'aes_key' => base64_encode($enc_outer),
- 'ciphertext' => base64_encode($ciphertext),
- ));
-
- $xs->element('encrypted_header', null, base64_encode($enc_header));
-
- $payload = $inner_key->encrypt($magic_env->getData(), \phpseclib\Crypt\RSA::PADDING_PKCS1);
-
-
-
- $magic_env->signMessage(base64_encode($payload), 'application/xml');
-
-
-
- $xs->elementStart('me:env', array('xmlns:me' => MagicEnvelope::NS));
- $xs->element('me:data', array('type' => $magic_env->getDataType()), $magic_env->getData());
- $xs->element('me:encoding', null, $magic_env->getEncoding());
- $xs->element('me:alg', null, $magic_env->getSignatureAlgorithm());
- $xs->element('me:sig', null, $magic_env->getSignature());
- $xs->elementEnd('me:env');
- $xs->elementEnd('entry');
- return false;
- }
- public function onSalmonSlap($endpoint_uri, MagicEnvelope $magic_env, Profile $target=null)
- {
- try {
- $envxml = $magic_env->toXML($target, 'diaspora');
- } catch (Exception $e) {
- common_log(LOG_ERR, sprintf('Could not generate Magic Envelope XML (diaspora flavour) for profile id=='.$target->getID().': '.$e->getMessage()));
- return false;
- }
-
- $headers = array('Content-Type: application/x-www-form-urlencoded');
-
-
-
- try {
- $client = new HTTPClient();
- $client->setBody('xml=' . Magicsig::base64_url_encode($envxml));
- $response = $client->post($endpoint_uri, $headers);
- } catch (Exception $e) {
- common_log(LOG_ERR, "Diaspora-flavoured Salmon post to $endpoint_uri failed: " . $e->getMessage());
- return false;
- }
-
-
- if (!in_array($response->getStatus(), array(200, 202))) {
- common_log(LOG_ERR, sprintf('Salmon (from profile %d) endpoint %s returned status %s: %s',
- $magic_env->getActor()->getID(), $endpoint_uri, $response->getStatus(), $response->getBody()));
- return true;
- }
-
- return false;
- }
- }
|