- /**
- * @brief Creates the envelope for a public message
- *
- * @param string $msg The message that is to be transmitted
- * @param array $user The record of the sender
- * @param array $contact Target of the communication
- * @param string $prvkey The private key of the sender
- * @param string $pubkey The public key of the receiver
- *
- * @return string The envelope
- */
- private static function build_public_message($msg, $user, $contact, $prvkey, $pubkey) {
-
- logger("Message: ".$msg, LOGGER_DATA);
-
- $handle = self::my_handle($user);
-
- $b64url_data = base64url_encode($msg);
-
- $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
-
- $type = "application/xml";
- $encoding = "base64url";
- $alg = "RSA-SHA256";
-
- $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
-
- $signature = rsa_sign($signable_data,$prvkey);
- $sig = base64url_encode($signature);
-
- $xmldata = array("diaspora" => array("header" => array("author_id" => $handle),
- "me:env" => array("me:encoding" => $encoding,
- "me:alg" => $alg,
- "me:data" => $data,
- "@attributes" => array("type" => $type),
- "me:sig" => $sig)));
-
- $namespaces = array("" => "https://joindiaspora.com/protocol",
- "me" => "http://salmon-protocol.org/ns/magic-env");
-
- $magic_env = xml::from_array($xmldata, $xml, false, $namespaces);
-
- logger("magic_env: ".$magic_env, LOGGER_DATA);
- return $magic_env;
- }
-
- /**
- * @brief Creates the envelope for a private message
- *
- * @param string $msg The message that is to be transmitted
- * @param array $user The record of the sender
- * @param array $contact Target of the communication
- * @param string $prvkey The private key of the sender
- * @param string $pubkey The public key of the receiver
- *
- * @return string The envelope
- */
- private static function build_private_message($msg, $user, $contact, $prvkey, $pubkey) {
-
- logger("Message: ".$msg, LOGGER_DATA);
-
- // without a public key nothing will work
-
- if (!$pubkey) {
- logger("pubkey missing: contact id: ".$contact["id"]);
- return false;
- }
-
- $inner_aes_key = openssl_random_pseudo_bytes(32);
- $b_inner_aes_key = base64_encode($inner_aes_key);
- $inner_iv = openssl_random_pseudo_bytes(16);
- $b_inner_iv = base64_encode($inner_iv);
-
- $outer_aes_key = openssl_random_pseudo_bytes(32);
- $b_outer_aes_key = base64_encode($outer_aes_key);
- $outer_iv = openssl_random_pseudo_bytes(16);
- $b_outer_iv = base64_encode($outer_iv);
-
- $handle = self::my_handle($user);
-
- $inner_encrypted = self::aes_encrypt($inner_aes_key, $inner_iv, $msg);
-
- $b64_data = base64_encode($inner_encrypted);
-
-
- $b64url_data = base64url_encode($b64_data);
- $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
-
- $type = "application/xml";
- $encoding = "base64url";
- $alg = "RSA-SHA256";
-
- $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
-
- $signature = rsa_sign($signable_data,$prvkey);
- $sig = base64url_encode($signature);
-
- $xmldata = array("decrypted_header" => array("iv" => $b_inner_iv,
- "aes_key" => $b_inner_aes_key,
- "author_id" => $handle));
-
- $decrypted_header = xml::from_array($xmldata, $xml, true);
-
- $ciphertext = self::aes_encrypt($outer_aes_key, $outer_iv, $decrypted_header);
-
- $outer_json = json_encode(array("iv" => $b_outer_iv, "key" => $b_outer_aes_key));
-
- $encrypted_outer_key_bundle = "";
- openssl_public_encrypt($outer_json, $encrypted_outer_key_bundle, $pubkey);
-
- $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
-
- logger("outer_bundle: ".$b64_encrypted_outer_key_bundle." key: ".$pubkey, LOGGER_DATA);
-
- $encrypted_header_json_object = json_encode(array("aes_key" => base64_encode($encrypted_outer_key_bundle),
- "ciphertext" => base64_encode($ciphertext)));
- $cipher_json = base64_encode($encrypted_header_json_object);
-
- $xmldata = array("diaspora" => array("encrypted_header" => $cipher_json,
- "me:env" => array("me:encoding" => $encoding,
- "me:alg" => $alg,
- "me:data" => $data,
- "@attributes" => array("type" => $type),
- "me:sig" => $sig)));
-
- $namespaces = array("" => "https://joindiaspora.com/protocol",
- "me" => "http://salmon-protocol.org/ns/magic-env");
-
- $magic_env = xml::from_array($xmldata, $xml, false, $namespaces);
-
- logger("magic_env: ".$magic_env, LOGGER_DATA);
- return $magic_env;
- }
-