Beispiel #1
0
 /**
  * Transforms `cipher` from array to CipherParams, if necessary
  */
 public function __construct($options = [])
 {
     parent::__construct($options);
     if (is_array($this->cipher)) {
         $this->cipher = Crypto::getDefaultParams($this->cipher);
     }
 }
Beispiel #2
0
 public static function setUpBeforeClass()
 {
     self::$testApp = new TestApp();
     self::$defaultOptions = self::$testApp->getOptions();
     self::$ably = new AblyRest(array_merge(self::$defaultOptions, ['key' => self::$testApp->getAppKeyDefault()->string]));
     $fixture = self::$testApp->getFixture();
     self::$presenceFixture = $fixture->post_apps->channels[0]->presence;
     $cipherParams = Crypto::getDefaultParams(['key' => $fixture->cipher->key, 'algorithm' => $fixture->cipher->algorithm, 'keyLength' => $fixture->cipher->keylength, 'mode' => $fixture->cipher->mode, 'iv' => $fixture->cipher->iv, 'base64Key' => true, 'base64Iv' => true]);
     $options = ['cipher' => $cipherParams];
     self::$channel = self::$ably->channel('persisted:presence_fixtures', $options);
 }
Beispiel #3
0
 /**
  * Tests if example presence messages match actual presence messages after encryption/decryption and vice versa:
  * decrypt(encrypted_example) == unencrypted_example
  * encrypt(unencrypted_example) == encrypted_example
  *
  * @dataProvider filenameProvider
  */
 public function testPresenceMessageEncryptionAgainstFixture($filename)
 {
     $fixture = json_decode(file_get_contents($filename));
     foreach ($fixture->items as $example) {
         $cipherParams = Crypto::getDefaultParams(['key' => $fixture->key, 'algorithm' => $fixture->algorithm, 'keyLength' => $fixture->keylength, 'mode' => $fixture->mode, 'iv' => $fixture->iv, 'base64Key' => true, 'base64Iv' => true]);
         unset($example->encoded->name);
         // we're reusing fixtures for standard messages, but presence messages do not have a name
         unset($example->encrypted->name);
         $decodedExample = new PresenceMessage();
         $decodedExample->fromJSON($example->encoded);
         $decryptedExample = new PresenceMessage();
         $decryptedExample->setCipherParams($cipherParams);
         $decryptedExample->fromJSON($example->encrypted);
         $this->assertEquals($decodedExample->data, $decryptedExample->data, 'Expected unencrypted and decrypted message\'s contents to match');
         $decodedExample->setCipherParams($cipherParams);
         $encryptedJSON = json_decode($decodedExample->toJSON());
         $this->assertEquals($example->encrypted->data, $encryptedJSON->data, 'Expected encrypted and example encrypted message\'s contents to match');
     }
 }
Beispiel #4
0
 /**
  * Decodes message's data field according to encoding
  * @throws AblyException
  */
 protected function decode()
 {
     $this->originalData = $this->data;
     $this->originalEncoding = $this->encoding;
     if (!empty($this->encoding)) {
         $encodings = explode('/', $this->encoding);
         foreach (array_reverse($encodings) as $encoding) {
             if ($encoding == 'base64') {
                 $this->data = base64_decode($this->data);
                 if ($this->data === false) {
                     throw new AblyException('Could not base64-decode message data');
                 }
                 array_pop($encodings);
             } else {
                 if ($encoding == 'json') {
                     $this->data = json_decode($this->data);
                     if ($this->data === null) {
                         throw new AblyException('Could not JSON-decode message data');
                     }
                     array_pop($encodings);
                 } else {
                     if (strpos($encoding, 'cipher+') === 0) {
                         if (!$this->cipherParams) {
                             Log::e('Could not decrypt message data, no cipherParams provided');
                             break;
                         }
                         $data = Crypto::decrypt($this->data, $this->cipherParams);
                         if ($data === false) {
                             Log::e('Could not decrypt message data');
                             break;
                         }
                         $this->data = $data;
                         array_pop($encodings);
                     }
                 }
             }
         }
         $this->encoding = count($encodings) ? implode('/', $encodings) : null;
     }
 }
Beispiel #5
0
 /**
  * Check if message encodings are correct, including the default encryption
  */
 public function testMessageEncodings()
 {
     $msg = new Message();
     $msg->data = 'This is a UTF-8 string message payload. äôč ビール';
     $this->assertEquals('', $this->getMessageEncoding($msg), 'Expected empty message encoding');
     $msg->data = (object) ['test' => 'This is a JSONObject message payload'];
     $this->assertEquals('json', $this->getMessageEncoding($msg), 'Expected empty message encoding');
     $msg->data = hex2bin('00102030405060708090a0b0c0d0e0f0ff');
     $this->assertEquals('base64', $this->getMessageEncoding($msg), 'Expected empty message encoding');
     $msg->setCipherParams(Crypto::getDefaultParams(['key' => Crypto::generateRandomKey(128)]));
     $msg->data = 'This is a UTF-8 string message payload. äôč ビール';
     $this->assertEquals('utf-8/cipher+aes-128-cbc/base64', $this->getMessageEncoding($msg), 'Expected empty message encoding');
     $msg->data = (object) ['test' => 'This is a JSONObject message payload'];
     $this->assertEquals('json/utf-8/cipher+aes-128-cbc/base64', $this->getMessageEncoding($msg), 'Expected empty message encoding');
     $msg->data = hex2bin('00102030405060708090a0b0c0d0e0f0ff');
     $this->assertEquals('cipher+aes-128-cbc/base64', $this->getMessageEncoding($msg), 'Expected empty message encoding');
 }