- c->allow_request = METAKEY;
-
- if(c->protocol_minor >= 2) {
- c->allow_request = ACK;
- char label[25 + strlen(myself->name) + strlen(c->name)];
-
- if(c->outgoing)
- snprintf(label, sizeof label, "tinc TCP key expansion %s %s", myself->name, c->name);
- else
- snprintf(label, sizeof label, "tinc TCP key expansion %s %s", c->name, myself->name);
-
- return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
- } else {
- return send_metakey(c);
- }
-}
-
-bool send_metakey(connection_t *c) {
- if(!read_rsa_public_key(c))
- return false;
-
- if(!cipher_open_blowfish_ofb(&c->outcipher))
- return false;
-
- if(!digest_open_sha1(&c->outdigest, -1))
- return false;
-
- size_t len = rsa_size(&c->rsa);
- char key[len];
- char enckey[len];
- char hexkey[2 * len + 1];
-
- /* Create a random key */
-
- randomize(key, 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.
- */
-
- key[0] &= 0x7F;
-
- cipher_set_key_from_rsa(&c->outcipher, key, len, true);
-
- if(debug_level >= DEBUG_SCARY_THINGS) {
- bin2hex(key, hexkey, len);
- logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
- }
-
- /* 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(&c->rsa, key, len, enckey)) {
- logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
- return false;
- }
-
- /* Convert the encrypted random data to a hexadecimal formatted string */
-
- bin2hex(enckey, hexkey, len);
-
- /* Send the meta key */
-
- bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
- cipher_get_nid(&c->outcipher),
- digest_get_nid(&c->outdigest), c->outmaclength,
- c->outcompression, hexkey);
-
- c->status.encryptout = true;
- return result;
-}
-
-bool metakey_h(connection_t *c, const char *request) {
- char hexkey[MAX_STRING_SIZE];
- int cipher, digest, maclength, compression;
- size_t len = rsa_size(&myself->connection->rsa);
- char enckey[len];
- char key[len];
-
- if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, hexkey) != 5) {
- logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
- return false;
- }
-
- /* Convert the challenge from hexadecimal back to binary */