-int metakey_h(connection_t *c)
-{
- char buffer[MAX_STRING_SIZE];
- int cipher, digest, maclength, compression;
- int len;
-cp
- if(sscanf(c->buffer, "%*d %d %d %d %d "MAX_STRING, &cipher, &digest, &maclength, &compression, buffer) != 5)
- {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
- return -1;
- }
-cp
-#ifdef USE_OPENSSL
- len = RSA_size(myself->connection->rsa_key);
-#endif
-
- /* Check if the length of the meta key is all right */
-
- if(strlen(buffer) != len*2)
- {
- syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
- return -1;
- }
-
- /* Allocate buffers for the meta key */
-cp
- if(!c->inkey)
- c->inkey = xmalloc(len);
-
-#ifdef USE_OPENSSL
- if(!c->inctx)
- c->inctx = xmalloc(sizeof(*c->inctx));
-#endif
-
- /* Convert the challenge from hexadecimal back to binary */
-cp
- hex2bin(buffer,buffer,len);
-
- /* Decrypt the meta key */
-cp
-#ifdef USE_OPENSSL
- if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
- {
- syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
- return -1;
- }
-#endif
-
- if(debug_lvl >= DEBUG_SCARY_THINGS)
- {
- bin2hex(c->inkey, buffer, len);
- buffer[len*2] = '\0';
- syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
- }
-
- /* All incoming requests will now be encrypted. */
-cp
- /* Check and lookup cipher and digest algorithms */
-
- if(cipher)
- {
-#ifdef USE_OPENSSL
- c->incipher = EVP_get_cipherbynid(cipher);
- if(!c->incipher)
- {
- syslog(LOG_ERR, _("%s (%s) uses unknown cipher!"), c->name, c->hostname);
- return -1;
- }
-
- EVP_DecryptInit(c->inctx, c->incipher,
- c->inkey + len - c->incipher->key_len,
- c->inkey + len - c->incipher->key_len - c->incipher->iv_len);
-
- c->status.decryptin = 1;
-#endif
- }
- else
- {
- c->incipher = NULL;
- }
-
- c->inmaclength = maclength;
-
- if(digest)
- {
-#ifdef USE_OPENSSL
- c->indigest = EVP_get_digestbynid(digest);
- if(!c->indigest)
- {
- syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), c->name, c->hostname);
- return -1;
- }
-
- if(c->inmaclength > c->indigest->md_size || c->inmaclength < 0)
- {
- syslog(LOG_ERR, _("%s (%s) uses bogus MAC length!"), c->name, c->hostname);
- return -1;
- }
-#endif
- }
- else
- {
- c->indigest = NULL;
- }
-
- c->incompression = compression;
-
- c->allow_request = CHALLENGE;
-cp
- return send_challenge(c);
+bool send_metakey(connection_t *c) {
+ bool x;
+
+ int len = RSA_size(c->rsa_key);
+
+ /* Allocate buffers for the meta key */
+
+ char buffer[2 * len + 1];
+
+ c->outkey = xrealloc(c->outkey, len);
+
+ if(!c->outctx) {
+ c->outctx = EVP_CIPHER_CTX_new();
+ if(!c->outctx)
+ abort();
+ }
+
+ /* Copy random data to the buffer */
+
+ if (1 != RAND_bytes((unsigned char *)c->outkey, len)) {
+ int err = ERR_get_error();
+ logger(LOG_ERR, "Failed to generate meta key (%s)", ERR_error_string(err, NULL));
+ return false;
+ }
+
+
+ /* 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;
+
+ ifdebug(SCARY_THINGS) {
+ bin2hex(c->outkey, buffer, len);
+ buffer[len * 2] = '\0';
+ logger(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.
+ */
+
+ if(RSA_public_encrypt(len, (unsigned char *)c->outkey, (unsigned char *)buffer, c->rsa_key, RSA_NO_PADDING) != len) {
+ logger(LOG_ERR, "Error during encryption of meta key for %s (%s): %s",
+ c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
+ return false;
+ }
+
+ /* Convert the encrypted random data to a hexadecimal formatted string */
+
+ bin2hex(buffer, buffer, len);
+ buffer[len * 2] = '\0';
+
+ /* Send the meta key */
+
+ x = send_request(c, "%d %d %d %d %d %s", METAKEY,
+ c->outcipher ? EVP_CIPHER_nid(c->outcipher) : 0,
+ c->outdigest ? EVP_MD_type(c->outdigest) : 0, c->outmaclength,
+ c->outcompression, buffer);
+
+ /* Further outgoing requests are encrypted with the key we just generated */
+
+ if(c->outcipher) {
+ if(!EVP_EncryptInit(c->outctx, c->outcipher,
+ (unsigned char *)c->outkey + len - EVP_CIPHER_key_length(c->outcipher),
+ (unsigned char *)c->outkey + len - EVP_CIPHER_key_length(c->outcipher) -
+ EVP_CIPHER_iv_length(c->outcipher))) {
+ logger(LOG_ERR, "Error during initialisation of cipher for %s (%s): %s",
+ c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
+ return false;
+ }
+
+ c->outbudget = byte_budget(c->outcipher);
+ c->status.encryptout = true;
+ }
+
+ return x;