/** * @param AppStoreInterface $appStore * @param string $blob * @return array * Decoded data. */ public static function decode($appStore, $blob) { $parts = explode(Constants::PROTOCOL_DELIM, $blob, 5); if (count($parts) != 5) { throw new InvalidMessageException('Invalid message: insufficient parameters'); } list($wireProt, $wireAppId, $rsaCiphertextB64, $signature, $body) = $parts; if ($wireProt !== self::NAME) { throw new InvalidMessageException('Invalid message: wrong protocol name'); } $appPrivKey = $appStore->getPrivateKey($wireAppId); if (!$appPrivKey) { throw new InvalidMessageException('Received message intended for unknown app.'); } $rsaCiphertext = base64_decode($rsaCiphertextB64); if (strlen($rsaCiphertext) !== Constants::RSA_MSG_BYTES) { throw new InvalidMessageException("RSA ciphertext has incorrect length"); } $secret = UserError::adapt('Civi\\Cxn\\Rpc\\Exception\\InvalidMessageException', function () use($rsaCiphertext, $appPrivKey) { return RegistrationMessage::getRsa($appPrivKey, 'private')->decrypt($rsaCiphertext); }); if (empty($secret)) { throw new InvalidMessageException("Invalid message: decryption produced empty secret"); } $plaintext = AesHelper::authenticateThenDecrypt($secret, $body, $signature); return json_decode($plaintext, TRUE); }
/** * @param CxnStoreInterface $cxnStore * A repository that contains shared secrets. * @param string $message * Ciphertext. * @return static * @throws InvalidMessageException */ public static function decode($cxnStore, $message) { list($parsedProt, $parsedCxnId, $parsedHmac, $parsedBody) = explode(Constants::PROTOCOL_DELIM, $message, 4); if ($parsedProt != self::NAME) { throw new InvalidMessageException('Incorrect coding. Expected: ' . self::NAME); } $cxn = $cxnStore->getByCxnId($parsedCxnId); if (empty($cxn)) { throw new InvalidMessageException('Unknown connection ID'); } $jsonPlaintext = AesHelper::authenticateThenDecrypt($cxn['secret'], $parsedBody, $parsedHmac); return new StdMessage($parsedCxnId, $cxn['secret'], json_decode($jsonPlaintext, TRUE)); }