+ if(!rsa_private_decrypt(myself->connection->legacy->rsa, enckey, len, key)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
+ return false;
+ }
+
+ if(debug_level >= DEBUG_SCARY_THINGS) {
+ bin2hex(key, hexkey, len);
+ logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
+ // Hopefully the user knew what he was doing leaking session keys into logs. We'll do the right thing here anyway.
+ memzero(hexkey, HEX_SIZE(len));
+ }
+
+ /* Check and lookup cipher and digest algorithms */
+
+ if(!init_crypto_by_nid(&c->legacy->in, cipher, digest)) {
+ memzero(key, len);
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher or digest from %s (%s)", c->name, c->hostname);
+ return false;
+ }
+
+ bool key_set = cipher_set_key_from_rsa(&c->legacy->in.cipher, key, len, false);
+ memzero(key, len);
+
+ if(!key_set) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error setting RSA key for %s (%s)", c->name, c->hostname);
+ return false;
+ }
+
+ c->status.decryptin = true;
+
+ c->allow_request = CHALLENGE;
+
+ return send_challenge(c);
+}
+
+bool send_challenge(connection_t *c) {
+ const size_t len = rsa_size(c->legacy->rsa);
+ char *buffer = alloca(len * 2 + 1);
+
+ c->hischallenge = xrealloc(c->hischallenge, len);
+
+ /* Copy random data to the buffer */
+
+ randomize(c->hischallenge, len);
+
+ /* Convert to hex */
+
+ bin2hex(c->hischallenge, buffer, len);
+
+ /* Send the challenge */
+
+ return send_request(c, "%d %s", CHALLENGE, buffer);
+}
+
+bool challenge_h(connection_t *c, const char *request) {
+ if(!myself->connection->legacy) {
+ return false;
+ }
+
+ char buffer[MAX_STRING_SIZE];
+ const size_t len = rsa_size(myself->connection->legacy->rsa);
+
+ if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
+ return false;
+ }
+
+ /* Check if the length of the challenge is all right */
+
+ if(strlen(buffer) != (size_t)len * 2) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
+ return false;
+ }
+
+ c->mychallenge = xrealloc(c->mychallenge, len);
+
+ /* Convert the challenge from hexadecimal back to binary */
+
+ hex2bin(buffer, c->mychallenge, len);
+
+ /* The rest is done by send_chal_reply() */
+
+ c->allow_request = CHAL_REPLY;
+
+ if(c->outgoing) {
+ return send_chal_reply(c);
+ } else {
+ return true;
+ }
+}
+
+bool send_chal_reply(connection_t *c) {
+ const size_t len = rsa_size(myself->connection->legacy->rsa);
+ size_t digestlen = digest_length(&c->legacy->in.digest);
+ char *digest = alloca(digestlen * 2 + 1);
+
+ /* Calculate the hash from the challenge we received */
+
+ if(!digest_create(&c->legacy->in.digest, c->mychallenge, len, digest)) {
+ return false;
+ }
+
+ free(c->mychallenge);
+ c->mychallenge = NULL;
+
+ /* Convert the hash to a hexadecimal formatted string */
+
+ bin2hex(digest, digest, digestlen);
+
+ /* Send the reply */
+
+ return send_request(c, "%d %s", CHAL_REPLY, digest);
+}
+
+bool chal_reply_h(connection_t *c, const char *request) {
+ char hishash[MAX_STRING_SIZE];
+
+ if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
+ c->hostname);
+ return false;
+ }
+
+ /* Convert the hash to binary format */
+
+ size_t inlen = hex2bin(hishash, hishash, sizeof(hishash));
+
+ /* Check if the length of the hash is all right */
+
+ if(inlen != digest_length(&c->legacy->out.digest)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
+ return false;
+ }
+
+
+ /* Verify the hash */
+
+ if(!digest_verify(&c->legacy->out.digest, c->hischallenge, rsa_size(c->legacy->rsa), hishash)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
+ return false;
+ }
+
+ /* Identity has now been positively verified.
+ Send an acknowledgement with the rest of the information needed.
+ */
+
+ free(c->hischallenge);
+ c->hischallenge = NULL;
+ c->allow_request = ACK;
+
+ if(!c->outgoing) {
+ send_chal_reply(c);
+ }
+
+ return send_ack(c);
+}
+
+static bool send_upgrade(connection_t *c) {
+ /* Special case when protocol_minor is 1: the other end is Ed25519 capable,
+ * but doesn't know our key yet. So send it now. */
+
+ char *pubkey = ecdsa_get_base64_public_key(myself->connection->ecdsa);
+
+ if(!pubkey) {
+ return false;
+ }
+
+ bool result = send_request(c, "%d %s", ACK, pubkey);
+ free(pubkey);
+ return result;
+}
+#else
+bool send_metakey(connection_t *c) {
+ (void)c;
+ return false;
+}
+
+bool metakey_h(connection_t *c, const char *request) {
+ (void)c;
+ (void)request;
+ return false;
+}
+
+bool send_challenge(connection_t *c) {
+ (void)c;
+ return false;
+}
+
+bool challenge_h(connection_t *c, const char *request) {
+ (void)c;
+ (void)request;
+ return false;
+}
+
+bool send_chal_reply(connection_t *c) {
+ (void)c;
+ return false;
+}
+
+bool chal_reply_h(connection_t *c, const char *request) {
+ (void)c;
+ (void)request;
+ return false;