-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
- len = RSA_size(myself->connection->rsa_key);
-
- /* 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);
-
- if(!c->inctx)
- c->inctx = xmalloc(sizeof(*c->inctx));
-
- /* Convert the challenge from hexadecimal back to binary */
-cp
- hex2bin(buffer,buffer,len);
-
- /* Decrypt the meta key */
-cp
- 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;
- }
-
- 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)
- {
- 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;
- }
- else
- {
- c->incipher = NULL;
- }
-
- c->inmaclength = maclength;
-
- if(digest)
- {
- 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;
- }
- }
- else
- {
- c->indigest = NULL;
- }
-
- c->incompression = compression;
-
- c->allow_request = CHALLENGE;
-cp
- return send_challenge(c);
+bool metakey_h(connection_t *c) {
+ char buffer[MAX_STRING_SIZE];
+ int cipher, digest, maclength, compression;
+ int len;
+
+ if(sscanf(c->buffer, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, buffer) != 5) {
+ logger(LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name,
+ c->hostname);
+ return false;
+ }
+
+ len = RSA_size(myself->connection->rsa_key);
+
+ /* Check if the length of the meta key is all right */
+
+ if(strlen(buffer) != len * 2) {
+ logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
+ return false;
+ }
+
+ /* Allocate buffers for the meta key */
+
+ c->inkey = xrealloc(c->inkey, len);
+
+ if(!c->inctx) {
+ c->inctx = EVP_CIPHER_CTX_new();
+
+ if(!c->inctx) {
+ abort();
+ }
+ }
+
+ /* Convert the challenge from hexadecimal back to binary */
+
+ if(!hex2bin(buffer, buffer, len)) {
+ logger(LOG_ERR, "Got bad %s from %s(%s): %s", "METAKEY", c->name, c->hostname, "invalid key");
+ return false;
+ }
+
+ /* Decrypt the meta key */
+
+ if(RSA_private_decrypt(len, (unsigned char *)buffer, (unsigned char *)c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) { /* See challenge() */
+ logger(LOG_ERR, "Error during decryption of meta key for %s (%s): %s",
+ c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
+ return false;
+ }
+
+ ifdebug(SCARY_THINGS) {
+ bin2hex(c->inkey, buffer, len);
+ buffer[len * 2] = '\0';
+ logger(LOG_DEBUG, "Received random meta key (unencrypted): %s", buffer);
+ }
+
+ /* All incoming requests will now be encrypted. */
+
+ /* Check and lookup cipher and digest algorithms */
+
+ if(cipher) {
+ c->incipher = EVP_get_cipherbynid(cipher);
+
+ if(!c->incipher) {
+ logger(LOG_ERR, "%s (%s) uses unknown cipher!", c->name, c->hostname);
+ return false;
+ }
+
+ if(!EVP_DecryptInit(c->inctx, c->incipher,
+ (unsigned char *)c->inkey + len - EVP_CIPHER_key_length(c->incipher),
+ (unsigned char *)c->inkey + len - EVP_CIPHER_key_length(c->incipher) -
+ EVP_CIPHER_iv_length(c->incipher))) {
+ logger(LOG_ERR, "Error during initialisation of cipher from %s (%s): %s",
+ c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
+ return false;
+ }
+
+ c->inbudget = byte_budget(c->incipher);
+ c->status.decryptin = true;
+ } else {
+ c->incipher = NULL;
+ }
+
+ c->inmaclength = maclength;
+
+ if(digest) {
+ c->indigest = EVP_get_digestbynid(digest);
+
+ if(!c->indigest) {
+ logger(LOG_ERR, "Node %s (%s) uses unknown digest!", c->name, c->hostname);
+ return false;
+ }
+
+ if(c->inmaclength > EVP_MD_size(c->indigest) || c->inmaclength < 0) {
+ logger(LOG_ERR, "%s (%s) uses bogus MAC length!", c->name, c->hostname);
+ return false;
+ }
+ } else {
+ c->indigest = NULL;
+ }
+
+ c->incompression = compression;
+
+ c->allow_request = CHALLENGE;
+
+ return send_challenge(c);