-int send_metakey(connection_t *c)
-{
- char buffer[MAX_STRING_SIZE];
- int len, x;
-cp
-#ifdef USE_OPENSSL
- len = RSA_size(c->rsa_key);
-
- /* Allocate buffers for the meta key */
-
- if(!c->outkey)
- c->outkey = xmalloc(len);
-
- if(!c->outctx)
- c->outctx = xmalloc(sizeof(*c->outctx));
-cp
- /* Copy random data to the buffer */
-
- RAND_bytes(c->outkey, len);
-#endif
-
-#ifdef USE_GCRYPT
- len = 123; /* FIXME: RSA key length */
- c->outkey = gcry_random_bytes(len, GCRY_WEAK_RANDOM);
-#endif
-
- /* 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.
- */
-
- c->outkey[0] &= 0x7F;
-
- if(debug_lvl >= DEBUG_SCARY_THINGS)
- {
- bin2hex(c->outkey, buffer, len);
- buffer[len*2] = '\0';
- syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), buffer);
- }
-
- /* 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.
- */
-
-#ifdef USE_OPENSSL
- if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len)
- {
- syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
- return -1;
- }
-#endif
-cp
- /* Convert the encrypted random data to a hexadecimal formatted string */
-
-#ifdef USE_OPENSSL
- bin2hex(buffer, buffer, len);
-#endif
- buffer[len*2] = '\0';
-
- /* Send the meta key */
-
-#ifdef USE_OPENSSL
- x = send_request(c, "%d %d %d %d %d %s", METAKEY,
- c->outcipher?c->outcipher->nid:0, c->outdigest?c->outdigest->type:0,
- c->outmaclength, c->outcompression, buffer);
-#endif
-
- /* Further outgoing requests are encrypted with the key we just generated */
-
-#ifdef USE_OPENSSL
- if(c->outcipher)
- {
- EVP_EncryptInit(c->outctx, c->outcipher,
- c->outkey + len - c->outcipher->key_len,
- c->outkey + len - c->outcipher->key_len - c->outcipher->iv_len);
-
- c->status.encryptout = 1;
- }
-#endif
-cp
- return x;
+bool send_metakey(connection_t *c) {
+ size_t len = rsa_size(&c->rsa);
+ char key[len];
+ char enckey[len];
+ char hexkey[2 * len + 1];
+
+ cp();
+
+ if(!cipher_open_blowfish_ofb(&c->outcipher))
+ return false;
+
+ if(!digest_open_sha1(&c->outdigest))
+ return false;
+
+ /* 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);
+
+ ifdebug(SCARY_THINGS) {
+ bin2hex(key, hexkey, len);
+ hexkey[len * 2] = '\0';
+ logger(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(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 */
+
+ bin2hex(enckey, hexkey, len);
+ hexkey[len * 2] = '\0';
+
+ /* Send the meta key */
+
+ bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
+ cipher_get_nid(&c->outcipher),
+ digest_get_nid(&c->outdigest), c->outmaclength,
+ c->outcompression, hexkey);
+
+ c->status.encryptout = true;
+ return result;