- {
- bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
- hishash[SHA_DIGEST_LENGTH*2] = '\0';
- syslog(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
- }
- return -1;
- }
-
- /* Identity has now been positively verified.
- ack_h() handles the rest from now on.
- */
-cp
- return ack_h(cl);
-}
-
-int send_metakey(connection_t *cl)
-{
- char *buffer;
- int len, x;
-cp
- len = RSA_size(cl->rsa_key);
-
- /* Allocate buffers for the meta key */
-
- buffer = xmalloc(len*2+1);
-
- if(!cl->cipher_outkey)
- cl->cipher_outkey = xmalloc(len);
-
- if(!cl->cipher_outctx)
- cl->cipher_outctx = xmalloc(sizeof(*cl->cipher_outctx));
-cp
- /* Copy random data to the buffer */
-
- RAND_bytes(cl->cipher_outkey, len);
-
- /* 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.
- */
-
- cl->cipher_outkey[0] &= 0x7F;
-
- if(debug_lvl >= DEBUG_SCARY_THINGS)
- {
- bin2hex(cl->cipher_outkey, buffer, len);
- buffer[len*2] = '\0';
- syslog(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, cl->cipher_outkey, buffer, cl->rsa_key, RSA_NO_PADDING) != len)
- {
- syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), cl->name, cl->hostname);
- free(buffer);
- return -1;
- }
-cp
- /* 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(cl, "%d %s", METAKEY, buffer);
- free(buffer);
-
- /* Further outgoing requests are encrypted with the key we just generated */
-
- EVP_EncryptInit(cl->cipher_outctx, EVP_bf_cfb(),
- cl->cipher_outkey + len - EVP_bf_cfb()->key_len,
- cl->cipher_outkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
-
- cl->status.encryptout = 1;
-cp
- return x;
-}
-
-int metakey_h(connection_t *cl)
-{
- char buffer[MAX_STRING_SIZE];
- int len;
-cp
- if(sscanf(cl->buffer, "%*d "MAX_STRING, buffer) != 1)
- {
- syslog(LOG_ERR, _("Got bad METAKEY from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- len = RSA_size(myself->rsa_key);
-
- /* Check if the length of the meta key is all right */
-
- if(strlen(buffer) != len*2)
- {
- syslog(LOG_ERR, _("Intruder: wrong meta key length from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Allocate buffers for the meta key */
-
- if(!cl->cipher_inkey)
- cl->cipher_inkey = xmalloc(len);
-
- if(!cl->cipher_inctx)
- cl->cipher_inctx = xmalloc(sizeof(*cl->cipher_inctx));
-
- /* Convert the challenge from hexadecimal back to binary */
-
- hex2bin(buffer,buffer,len);
-
- /* Decrypt the meta key */
-
- if(RSA_private_decrypt(len, buffer, cl->cipher_inkey, myself->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
- {
- syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- if(debug_lvl >= DEBUG_SCARY_THINGS)
- {
- bin2hex(cl->cipher_inkey, buffer, len);
- buffer[len*2] = '\0';
- syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
- }
-
- /* All incoming requests will now be encrypted. */
-
- EVP_DecryptInit(cl->cipher_inctx, EVP_bf_cfb(),
- cl->cipher_inkey + len - EVP_bf_cfb()->key_len,
- cl->cipher_inkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
-
- cl->status.decryptin = 1;
-
- cl->allow_request = CHALLENGE;
-cp
- return send_challenge(cl);
-}
-
-/* Address and subnet information exchange */
-
-int send_add_subnet(connection_t *cl, subnet_t *subnet)
-{
- int x;
- char *netstr;
- char *owner;
-cp
- if((cl->options | myself->options | subnet->owner->options) & OPTION_INDIRECT)
- owner = myself->name;
- else
- owner = subnet->owner->name;
-
- x = send_request(cl, "%d %s %s", ADD_SUBNET,
- owner, netstr = net2str(subnet));
- free(netstr);
-cp
- return x;
-}
-
-int add_subnet_h(connection_t *cl)
-{
- char subnetstr[MAX_STRING_SIZE];
- char name[MAX_STRING_SIZE];
- connection_t *owner, *p;
- subnet_t *subnet;
- avl_node_t *node;
-cp
- if(sscanf(cl->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 2)
- {
- syslog(LOG_ERR, _("Got bad ADD_SUBNET from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if owner name is a valid */
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Got bad ADD_SUBNET from %s (%s): invalid identity name"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if subnet string is valid */
-
- if(!(subnet = str2net(subnetstr)))
- {
- syslog(LOG_ERR, _("Got bad ADD_SUBNET from %s (%s): invalid subnet string"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if somebody tries to add a subnet of ourself */
-
- if(!strcmp(name, myself->name))
- {
- syslog(LOG_ERR, _("Warning: got ADD_SUBNET from %s (%s) for ourself, restarting"),
- cl->name, cl->hostname);
- sighup = 1;
- return 0;
- }
-
- /* Check if the owner of the new subnet is in the connection list */
-
- if(!(owner = lookup_id(name)))
- {
- syslog(LOG_ERR, _("Got ADD_SUBNET for %s from %s (%s) which is not in our connection list"),
- name, cl->name, cl->hostname);
- return -1;
- }
-
- /* If everything is correct, add the subnet to the list of the owner */
-
- subnet_add(owner, subnet);
-
- /* Tell the rest */
-
- for(node = connection_tree->head; node; node = node->next)
- {
- p = (connection_t *)node->data;
- if(p->status.active && p!= cl)
- send_add_subnet(p, subnet);
- }
-cp
- return 0;
-}
-
-int send_del_subnet(connection_t *cl, subnet_t *subnet)
-{
- int x;
- char *netstr;
- char *owner;
-cp
- if(cl->options & OPTION_INDIRECT)
- owner = myself->name;
- else
- owner = subnet->owner->name;
-
- x = send_request(cl, "%d %s %s", DEL_SUBNET, owner, netstr = net2str(subnet));
- free(netstr);
-cp
- return x;
-}
-
-int del_subnet_h(connection_t *cl)
-{
- char subnetstr[MAX_STRING_SIZE];
- char name[MAX_STRING_SIZE];
- connection_t *owner, *p;
- subnet_t *subnet;
- avl_node_t *node;
-cp
- if(sscanf(cl->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 3)
- {
- syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s)"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if owner name is a valid */
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s): invalid identity name"), cl->name, cl->hostname);
- return -1;
- }
-
- /* Check if subnet string is valid */
-
- if(!(subnet = str2net(subnetstr)))
- {
- syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s): invalid subnet string"), cl->name, cl->hostname);
- return -1;
- }
-
- free(subnetstr);
-
- /* Check if somebody tries to add a subnet of ourself */
-
- if(!strcmp(name, myself->name))
- {
- syslog(LOG_ERR, _("Warning: got DEL_SUBNET from %s (%s) for ourself, restarting"),
- cl->name, cl->hostname);
- sighup = 1;
- return 0;
- }
-
- /* Check if the owner of the new subnet is in the connection list */
-
- if(!(owner = lookup_id(name)))
- {
- syslog(LOG_ERR, _("Got DEL_SUBNET for %s from %s (%s) which is not in our connection list"),
- name, cl->name, cl->hostname);
- return -1;
- }
-
- /* If everything is correct, delete the subnet from the list of the owner */
-
- subnet_del(subnet);
-
- /* Tell the rest */
-
- for(node = connection_tree->head; node; node = node->next)
- {
- p = (connection_t *)node->data;
- if(p->status.active && p!= cl)
- send_del_subnet(p, subnet);