- }
-cp
-}
-
-/* Connection protocol:
-
- Client Server
- send_id(*)
- send_challenge
- send_chal_reply(*)
- send_id
- send_challenge
- send_chal_reply
- send_ack
- send_ack
-
- (*) Unencrypted.
-*/
-
-int send_id(conn_list_t *cl)
-{
-cp
- return send_request(cl, "%d %s %d-%d %s", ID, myself->id, myself->min_version, myself->max_version, opt2str(myself->options));
-}
-
-int id_h(conn_list_t *cl)
-{
- conn_list_t *old;
- char *options;
-cp
- if(sscanf(cl->buffer, "%*d %as %d-%d %as", &cl->id, &cl->min_version, &cl->max_version, &options) != 4)
- {
- syslog(LOG_ERR, _("Got bad ID from %s"), cl->hostname);
- return -1;
- }
-
- /* Check if version ranges overlap */
-
- if((cl->min_version > myself->max_version) || (cl->max_version < myself_min_version) || (cl->min_version > cl->max_version))
- {
- syslog(LOG_ERR, _("Peer %s uses incompatible version (%d-%d)"), cl->hostname, cl->min_version, cl->max_version);
- return -1;
- }
-
- /* Check if option string is valid */
-
- if(str2opt(options) == -1)
- {
- syslog(LOG_ERR, _("Peer %s uses invalid option string"), cl->hostname);
- return -1;
- }
-
- /* Check if identity is a valid name */
-
- if(!check_id(cl->id))
- {
- syslog(LOG_ERR, _("Peer %s uses invalid identity name"), cl->hostname);
- return -1;
- }
-
- /* Load information about peer */
-
- if(!read_id(cl))
- {
- syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), cl->hostname, cl->id);
- return -1;
- }
-
-
- /* First check if the host we connected to is already in our
- connection list. If so, we are probably making a loop, which
- is not desirable.
- */
-
- if(cl->status.outgoing)
- {
- if((old=lookup_id(cl->id)))
- {
- if(debug_lvl > DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Uplink %s (%s) is already in our connection list"), cl->id, cl->hostname);
- cl->status.outgoing = 0;
- old->status.outgoing = 1;
- terminate_connection(cl);
- return 0;
- }
- }
-
- /* Since we know the identity now, we can encrypt the meta channel */
-
- cl->status.encryptout = 1;
-
- /* Send a challenge to verify the identity */
-
- cl->allow_request = CHAL_REPLY;
-cp
- return send_challenge(cl);
-}
-
-int send_challenge(conn_list_t *cl)
-{
- char *buffer;
- int keylength;
- int x;
-cp
- if(cl->chal_hash)
- free(cl->chal_hash);
-
- /* Allocate buffers for the challenge and the hash */
-
- cl->chal_hash = xmalloc(SHA_DIGEST_LEN);
- keylength = BN_num_bytes(cl->metakey.n);
- buffer = xmalloc(keylength*2);
-
- /* Copy random data and the public key to the buffer */
-
- RAND_bytes(buffer, keylength);
- BN_bn2bin(cl->metakey.n, buffer+keylength);
-
- /* Calculate the hash from that */
-
- SHA1(buffer, keylength*2, cl->chal_hash);
-
- /* Convert the random data to a hexadecimal formatted string */
-
- bin2hex(buffer,buffer,keylength);
- buffer[keylength*2] = '\0';
-
- /* Send the challenge */
-
- cl->allow_request = CHAL_REPLY;
- x = send_request(cl, "%d %s", CHALLENGE, buffer);
- free(buffer);
-cp
- return x;
-}
-
-int challenge_h(conn_list_t *cl)
-{
- char *challenge;
-cp
- if(sscanf(cl->buffer, "%*d %as", &cl->id, &challenge) != 1)
- {
- syslog(LOG_ERR, _("Got bad CHALLENGE from %s (%s)"), cl->id, cl->hostname);
- return -1;
- }
-
- /* Rest is done by send_chal_reply() */
-
- x = send_chal_reply(cl, challenge);
- free(challenge);
-cp
- return x;
-}
-
-int send_chal_reply(conn_list_t *cl, char *challenge)
-{
- char *buffer;
- int keylength;
- char *hash;
- int x;
-cp
- keylength = BN_num_bytes(myself->meyakey.n);
-
- /* Check if the length of the challenge is all right */
-
- if(strlen(challenge) != keylength*2)
- {
- syslog(LOG_ERROR, _("Intruder: wrong challenge length from %s (%s)"), cl->id, cl->hostname);
- return -1;
- }
-
- /* Allocate buffers for the challenge and the hash */
-
- buffer = xmalloc(keylength*2);
- hash = xmalloc(SHA_DIGEST_LEN*2+1);
-
- /* Copy the incoming random data and our public key to the buffer */
-
- hex2bin(challenge, buffer, keylength);
- BN_bn2bin(myself->metakey.n, buffer+keylength);
-
- /* Calculate the hash from that */
-
- SHA1(buffer, keylength*2, hash);
- free(buffer);
-
- /* Convert the hash to a hexadecimal formatted string */
-
- bin2hex(hash,hash,SHA_DIGEST_LEN);
- hash[SHA_DIGEST_LEN*2] = '\0';
-
- /* Send the reply */
-
- if(cl->status.outgoing)
- cl->allow_resuest = ID;
- else
- cl->allow_request = ACK;
-
- x = send_request(cl, "%d %s", CHAL_REPLY, hash);
- free(hash);
-cp
- return x;
-}
-
-int chal_reply_h(conn_list_t *cl)
-{
- char *hash;
-cp
- if(sscanf(cl->buffer, "%*d %as", &cl->id, &hash) != 2)
- {
- syslog(LOG_ERR, _("Got bad CHAL_REPLY from %s (%s)"), cl->id, cl->hostname);
- return -1;
- }
-
- /* Check if the length of the hash is all right */
-
- if(strlen(hash) != SHA_DIGEST_LEN*2)
- {
- syslog(LOG_ERROR, _("Intruder: wrong challenge reply length from %s (%s)"), cl->id, cl->hostname);
- return -1;
- }
-
- /* Convert the hash to binary format */
-
- hex2bin(hash, hash, SHA_DIGEST_LEN);