+
+ /* Forward it if necessary */
+
+ if(to != myself) {
+ return send_request(to->nexthop->connection, "%s", c->buffer);
+ }
+
+ /* Update our copy of the origin's packet key */
+
+ if(from->key)
+ free(from->key);
+
+ from->key = xstrdup(key);
+ from->keylength = strlen(key) / 2;
+ hex2bin(from->key, from->key, from->keylength);
+ from->key[from->keylength] = '\0';
+
+ from->status.validkey = 1;
+ from->status.waitingforkey = 0;
+ from->sent_seqno = 0;
+
+ /* Check and lookup cipher and digest algorithms */
+
+ if(cipher) {
+ from->cipher = EVP_get_cipherbynid(cipher);
+
+ if(!from->cipher) {
+ logger(DEBUG_ALWAYS, LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
+ from->hostname);
+ return -1;
+ }
+
+ if(from->keylength != from->cipher->key_len + from->cipher->iv_len) {
+ logger(DEBUG_ALWAYS, LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
+ from->hostname);
+ return -1;
+ }
+ } else {
+ from->cipher = NULL;
+ }
+
+ from->maclength = maclength;
+
+ if(digest) {
+ from->digest = EVP_get_digestbynid(digest);
+
+ if(!from->digest) {
+ logger(DEBUG_ALWAYS, LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
+ from->hostname);
+ return -1;
+ }
+
+ if(from->maclength > from->digest->md_size || from->maclength < 0) {
+ logger(DEBUG_ALWAYS, LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
+ from->name, from->hostname);
+ return -1;
+ }
+ } else {
+ from->digest = NULL;
+ }
+
+ if(compression < 0 || compression > 11) {
+ logger(DEBUG_ALWAYS, LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
+ return -1;
+ }
+
+ from->compression = compression;
+
+ EVP_EncryptInit_ex(&from->packet_ctx, from->cipher, NULL, from->key, from->key + from->cipher->key_len);
+
+ flush_queue(from);
+
+ return 0;