Magic signature discovery and envelope changes

This commit is contained in:
Mikael Nordfeldth 2015-10-04 17:26:35 +02:00
parent 6afa091dca
commit af1b0915f4
3 changed files with 52 additions and 13 deletions

View File

@ -56,6 +56,29 @@ class DiasporaPlugin extends Plugin
strtolower($magicsig->toFingerprint())); strtolower($magicsig->toFingerprint()));
} }
public function onMagicsigPublicKeyFromXRD(XML_XRD $xrd, &$pubkey)
{
// See if we have a Diaspora public key in the XRD response
$link = $xrd->get(self::REL_PUBLIC_KEY, 'RSA');
if (!is_null($link)) {
// If we do, decode it so we have the PKCS1 format (starts with -----BEGIN PUBLIC KEY-----)
$pkcs1 = base64_decode($link->href);
$magicsig = new Magicsig(Magicsig::DEFAULT_SIGALG); // Diaspora uses RSA-SHA256 (we do too)
try {
// Try to load the public key so we can get it in the standard Magic signature format
$magicsig->loadPublicKeyPKCS1($pkcs1);
// We found it and will now store it in $pubkey in a proper format!
// This is how it would be found in a well implemented XRD according to the standard.
$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) public function onPluginVersion(array &$versions)
{ {
$versions[] = array('name' => 'Diaspora', $versions[] = array('name' => 'Diaspora',
@ -144,6 +167,7 @@ class DiasporaPlugin extends Plugin
* Encrypt the “outer aes key bundle” with Bobs RSA public key. * Encrypt the “outer aes key bundle” with Bobs RSA public key.
* I shall refer to this as the “encrypted outer aes key bundle”. * I shall refer to this as the “encrypted outer aes key bundle”.
*/ */
common_debug('Diaspora creating "outer aes key bundle", will require magic-public-key');
$key_fetcher = new MagicEnvelope(); $key_fetcher = new MagicEnvelope();
$remote_keys = $key_fetcher->getKeyPair($target, true); // actually just gets the public key $remote_keys = $key_fetcher->getKeyPair($target, true); // actually just gets the public key
$enc_outer = $remote_keys->publicKey->encrypt($outer_bundle); $enc_outer = $remote_keys->publicKey->encrypt($outer_bundle);
@ -216,7 +240,7 @@ class DiasporaPlugin extends Plugin
// 202 Accepted is what we get from Diaspora for example // 202 Accepted is what we get from Diaspora for example
if (!in_array($response->getStatus(), array(200, 202))) { if (!in_array($response->getStatus(), array(200, 202))) {
common_log(LOG_ERR, sprintf('Salmon (from profile %d) endpoint %s returned status %s: %s', common_log(LOG_ERR, sprintf('Salmon (from profile %d) endpoint %s returned status %s: %s',
$user->id, $endpoint_uri, $response->getStatus(), $response->getBody())); $magic_env->getActor()->getID(), $endpoint_uri, $response->getStatus(), $response->getBody()));
return true; return true;
} }

View File

@ -1381,7 +1381,7 @@ class OStatusPlugin extends Plugin
return false; return false;
} }
if ($response->getStatus() === 422) { if ($response->getStatus() === 422) {
common_debug(sprintf('Salmon (from profile %d) endpoint %s returned status %s. We assume it is a Diaspora seed, will adapt and try again if that plugin is enabled!')); common_debug(sprintf('Salmon (from profile %d) endpoint %s returned status %s. We assume it is a Diaspora seed; will adapt and try again if that plugin is enabled!', $magic_env->getActor()->getID(), $endpoint_uri, $response->getStatus()));
return true; return true;
} }
@ -1389,7 +1389,7 @@ class OStatusPlugin extends Plugin
// 202 Accepted is what we get from Diaspora for example // 202 Accepted is what we get from Diaspora for example
if (!in_array($response->getStatus(), array(200, 202))) { if (!in_array($response->getStatus(), array(200, 202))) {
common_log(LOG_ERR, sprintf('Salmon (from profile %d) endpoint %s returned status %s: %s', common_log(LOG_ERR, sprintf('Salmon (from profile %d) endpoint %s returned status %s: %s',
$user->id, $endpoint_uri, $response->getStatus(), $response->getBody())); $magic_env->getActor()->getID(), $endpoint_uri, $response->getStatus(), $response->getBody()));
return true; return true;
} }

View File

@ -78,14 +78,16 @@ class MagicEnvelope
* @param boolean $discovery Network discovery if no local cache? * @param boolean $discovery Network discovery if no local cache?
*/ */
public function getKeyPair(Profile $profile, $discovery=false) { public function getKeyPair(Profile $profile, $discovery=false) {
$magicsig = Magicsig::getKV('user_id', $profile->id); if (!$profile->isLocal()) common_debug('Getting magic-public-key for non-local profile id=='.$profile->getID());
$magicsig = Magicsig::getKV('user_id', $profile->getID());
if ($discovery && !$magicsig instanceof Magicsig) { if ($discovery && !$magicsig instanceof Magicsig) {
if (!$profile->isLocal()) common_debug('magic-public-key not found, will do discovery for profile id=='.$profile->getID());
// Throws exception on failure, but does not try to _load_ the keypair string. // Throws exception on failure, but does not try to _load_ the keypair string.
$keypair = $this->discoverKeyPair($profile); $keypair = $this->discoverKeyPair($profile);
$magicsig = new Magicsig(); $magicsig = new Magicsig();
$magicsig->user_id = $profile->id; $magicsig->user_id = $profile->getID();
$magicsig->importKeys($keypair); $magicsig->importKeys($keypair);
// save the public key for this profile in our database. // save the public key for this profile in our database.
// TODO: If the profile generates a new key remotely, we must be able to replace // TODO: If the profile generates a new key remotely, we must be able to replace
@ -113,28 +115,41 @@ class MagicEnvelope
{ {
$signer_uri = $profile->getUri(); $signer_uri = $profile->getUri();
if (empty($signer_uri)) { if (empty($signer_uri)) {
throw new ServerException(sprintf('Profile missing URI (id==%d)', $profile->id)); throw new ServerException(sprintf('Profile missing URI (id==%d)', $profile->getID()));
} }
$disco = new Discovery(); $disco = new Discovery();
// Throws exception on lookup problems // Throws exception on lookup problems
try {
$xrd = $disco->lookup($signer_uri); $xrd = $disco->lookup($signer_uri);
} catch (Exception $e) {
// Diaspora seems to require us to request the acct: uri
$xrd = $disco->lookup($profile->getAcctUri());
}
common_debug('Will try to find magic-public-key from XRD of profile id=='.$profile->getID());
$pubkey = null;
if (Event::handle('MagicsigPublicKeyFromXRD', array($xrd, &$pubkey))) {
$link = $xrd->get(Magicsig::PUBLICKEYREL); $link = $xrd->get(Magicsig::PUBLICKEYREL);
if (is_null($link)) { if (is_null($link)) {
// TRANS: Exception. // TRANS: Exception.
throw new Exception(_m('Unable to locate signer public key.')); throw new Exception(_m('Unable to locate signer public key.'));
} }
$pubkey = $link->href;
}
if (empty($pubkey)) {
throw new ServerException('Empty Magicsig public key. A bug?');
}
// We have a public key element, let's hope it has proper key data. // We have a public key element, let's hope it has proper key data.
$keypair = false; $keypair = false;
$parts = explode(',', $link->href); $parts = explode(',', $pubkey);
if (count($parts) == 2) { if (count($parts) == 2) {
$keypair = $parts[1]; $keypair = $parts[1];
} else { } else {
// Backwards compatibility check for separator bug in 0.9.0 // Backwards compatibility check for separator bug in 0.9.0
$parts = explode(';', $link->href); $parts = explode(';', $pubkey);
if (count($parts) == 2) { if (count($parts) == 2) {
$keypair = $parts[1]; $keypair = $parts[1];
} }