-bool send_metakey(connection_t *c) {
- if(!read_rsa_public_key(c))
- return false;
-
- if(!cipher_open_blowfish_ofb(&c->outcipher))
- return false;
-
- if(!digest_open_sha1(&c->outdigest, -1))
- return false;
-
- size_t len = rsa_size(&c->rsa);
- char key[len];
- char enckey[len];
- char hexkey[2 * len + 1];
-
- /* Create a random key */
-
- randomize(key, len);
-
- /* The message we send must be smaller than the modulus of the RSA key.
- By definition, for a key of k bits, the following formula holds:
-
- 2^(k-1) <= modulus < 2^(k)
-
- Where ^ means "to the power of", not "xor".
- This means that to be sure, we must choose our message < 2^(k-1).
- This can be done by setting the most significant bit to zero.
- */
-
- key[0] &= 0x7F;
-
- cipher_set_key_from_rsa(&c->outcipher, key, len, true);
-
- if(debug_level >= DEBUG_SCARY_THINGS) {
- bin2hex(key, hexkey, len);
- logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
- }
-
- /* Encrypt the random data
-
- We do not use one of the PKCS padding schemes here.
- This is allowed, because we encrypt a totally random string
- with a length equal to that of the modulus of the RSA key.
- */
-
- if(!rsa_public_encrypt(&c->rsa, key, len, enckey)) {
- logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
- return false;
- }
-
- /* Convert the encrypted random data to a hexadecimal formatted string */