--- gvpe/src/connection.C 2011/03/06 19:40:28 1.94 +++ gvpe/src/connection.C 2013/10/11 04:07:24 1.108 @@ -1,6 +1,6 @@ /* connection.C -- manage a single connection - Copyright (C) 2003-2008,2010,2011 Marc Lehmann + Copyright (C) 2003-2008,2010,2011,2013 Marc Lehmann This file is part of GVPE. @@ -35,25 +35,30 @@ #include #include +#include #include #include #include #include +// openssl 0.9.8 compatibility +#if OPENSSL_VERSION_NUMBER < 0x10100000 + #define require101(exp) exp +#else + #define require101(exp) equire (exp) +#endif + #include "conf.h" #include "slog.h" #include "device.h" #include "vpn.h" #include "connection.h" +#include "hkdf.h" #include "netcompat.h" -#if !HAVE_RAND_PSEUDO_BYTES -# define RAND_pseudo_bytes RAND_bytes -#endif - -#define MAGIC_OLD "vped\xbd\xc6\xdb\x82" // 8 bytes of magic (still used in the protocol) -#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic (understood but not generated) +#define MAGIC "gvpe\xbd\xc6\xdb\x82" // 8 bytes of magic +#define MAGIC "HUHN\xbd\xc6\xdb\x82" // 8 bytes of magic//D #define ULTRA_FAST 1 #define HLOG 15 @@ -110,16 +115,43 @@ EVP_CIPHER_CTX cctx; HMAC_CTX hctx; - crypto_ctx (const rsachallenge &challenge, int enc); + crypto_ctx (const auth_data &auth1, const auth_data &auth2, const ecdh_key &a, const ecdh_key &b, int enc); ~crypto_ctx (); }; -crypto_ctx::crypto_ctx (const rsachallenge &challenge, int enc) +crypto_ctx::crypto_ctx (const auth_data &auth1, const auth_data &auth2, const ecdh_key &a, const ecdh_key &b, int enc) { - EVP_CIPHER_CTX_init (&cctx); - require (EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc)); - HMAC_CTX_init (&hctx); - HMAC_Init_ex (&hctx, &challenge[CHG_HMAC_KEY], HMAC_KEYLEN, DIGEST, 0); + ecdh_key s; + + curve25519_combine (a, b, s); + + { + u8 mac_key[MAC_KEYSIZE]; + static const unsigned char mac_info[] = "gvpe mac key"; + + hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ()); + kdf.extract (auth1.rsa.mac_key, sizeof (auth1.rsa.mac_key)); + kdf.extract (s, sizeof (s)); + kdf.extract_done (HKDF_PRF_HASH ()); + kdf.expand (mac_key, sizeof (mac_key), mac_info, sizeof (mac_info)); + + HMAC_CTX_init (&hctx); + require101 (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0)); + } + + { + u8 cipher_key[CIPHER_KEYSIZE]; + static const unsigned char cipher_info[] = "gvpe cipher key"; + + hkdf kdf (auth2.rsa.hkdf_salt, sizeof (auth2.rsa.hkdf_salt), HKDF_XTR_HASH ()); + kdf.extract (auth1.rsa.cipher_key, sizeof (auth1.rsa.cipher_key)); + kdf.extract (s, sizeof (s)); + kdf.extract_done (HKDF_PRF_HASH ()); + kdf.expand (cipher_key, sizeof (cipher_key), cipher_info, sizeof (cipher_info)); + + EVP_CIPHER_CTX_init (&cctx); + require (EVP_CipherInit_ex (&cctx, CIPHER (), 0, cipher_key, 0, enc)); + } } crypto_ctx::~crypto_ctx () @@ -128,87 +160,57 @@ HMAC_CTX_cleanup (&hctx); } -static void -rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) +static inline void +auth_encrypt (RSA *key, const auth_data &auth, auth_encr &encr) { - EVP_MD_CTX ctx; + if (RSA_public_encrypt (sizeof (auth.rsa), + (unsigned char *)&auth.rsa, (unsigned char *)&encr.rsa, + key, RSA_PKCS1_OAEP_PADDING) < 0) + fatal ("RSA_public_encrypt error"); - EVP_MD_CTX_init (&ctx); - require (EVP_DigestInit (&ctx, RSA_HASH)); - require (EVP_DigestUpdate(&ctx, &chg, sizeof chg)); - require (EVP_DigestUpdate(&ctx, &id, sizeof id)); - require (EVP_DigestFinal (&ctx, (unsigned char *)&h, 0)); - EVP_MD_CTX_cleanup (&ctx); + memcpy (&encr.ecdh, &auth.ecdh, sizeof (encr.ecdh)); } -struct rsa_entry +static inline bool +auth_decrypt (RSA *key, const auth_encr &encr, auth_data &auth) { - tstamp expire; - rsaid id; - rsachallenge chg; -}; - -struct rsa_cache : list -{ - inline void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; - - bool find (const rsaid &id, rsachallenge &chg) - { - for (iterator i = begin (); i != end (); ++i) - { - if (!memcmp (&id, &i->id, sizeof id) && i->expire > ev_now ()) - { - memcpy (&chg, &i->chg, sizeof chg); - - erase (i); - return true; - } - } - - if (!cleaner.is_active ()) - cleaner.again (); - - return false; - } - - void gen (rsaid &id, rsachallenge &chg) - { - rsa_entry e; - - RAND_bytes ((unsigned char *)&id, sizeof id); - RAND_bytes ((unsigned char *)&chg, sizeof chg); + u8 rsa_decrypt[RSA_KEYLEN]; - e.expire = ev_now () + RSA_TTL; - e.id = id; - memcpy (&e.chg, &chg, sizeof chg); - - push_back (e); + if (RSA_private_decrypt (sizeof (encr.rsa), + (const unsigned char *)&encr.rsa, (unsigned char *)rsa_decrypt, + key, RSA_PKCS1_OAEP_PADDING) != sizeof (auth.rsa)) + return 0; - if (!cleaner.is_active ()) - cleaner.again (); - } + memcpy (&auth.rsa, rsa_decrypt, sizeof (auth.rsa)); + memcpy (&auth.ecdh, &encr.ecdh, sizeof (auth.ecdh)); - rsa_cache () - { - cleaner.set (this); - cleaner.set (RSA_TTL, RSA_TTL); - } + return 1; +} -} rsa_cache; +static void +auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac) +{ + hkdf kdf (b, sizeof b, AUTH_DIGEST ()); // use response ecdh b as salt + kdf.extract (&auth.rsa, sizeof (auth.rsa)); + kdf.extract_done (); + kdf.expand (mac, sizeof mac, auth.ecdh, sizeof (auth.ecdh)); // use challenge ecdh b as info +} void -rsa_cache::cleaner_cb (ev::timer &w, int revents) +connection::generate_auth_data () { - if (empty ()) - w.stop (); - else + if (auth_expire < ev_now ()) { - for (iterator i = begin (); i != end (); ) - if (i->expire <= ev_now ()) - i = erase (i); - else - ++i; + // request data + rand_fill (snd_auth.rsa); + curve25519_generate (snd_ecdh_a, snd_auth.ecdh); + + // eventual response data + curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); } + + // every use prolongs the expiry + auth_expire = ev_now () + AUTH_TTL; } ////////////////////////////////////////////////////////////////////////////// @@ -367,34 +369,29 @@ ///////////////////////////////////////////////////////////////////////////// -unsigned char hmac_packet::hmac_digest[EVP_MAX_MD_SIZE]; - void -hmac_packet::hmac_gen (crypto_ctx *ctx) +hmac_packet::hmac_gen (crypto_ctx *ctx, u8 *hmac_digest) { - unsigned int xlen; - HMAC_CTX *hctx = &ctx->hctx; - HMAC_Init_ex (hctx, 0, 0, 0, 0); - HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), - len - sizeof (hmac_packet)); - HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen); + require101 (HMAC_Init_ex (hctx, 0, 0, 0, 0)); + require101 (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet))); + require101 (HMAC_Final (hctx, hmac_digest, 0)); } void hmac_packet::hmac_set (crypto_ctx *ctx) { - hmac_gen (ctx); - + unsigned char hmac_digest[EVP_MAX_MD_SIZE]; + hmac_gen (ctx, hmac_digest); memcpy (hmac, hmac_digest, HMACLENGTH); } bool hmac_packet::hmac_chk (crypto_ctx *ctx) { - hmac_gen (ctx); - + unsigned char hmac_digest[EVP_MAX_MD_SIZE]; + hmac_gen (ctx, hmac_digest); return !memcmp (hmac, hmac_digest, HMACLENGTH); } @@ -453,7 +450,7 @@ } #endif - require (EVP_EncryptInit_ex (cctx, 0, 0, 0, 0)); + require (EVP_CipherInit_ex (cctx, 0, 0, 0, 0, 1)); struct { #if RAND_SIZE @@ -464,7 +461,9 @@ datahdr.seqno = ntohl (seqno); #if RAND_SIZE - RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); + // NB: a constant (per session) random prefix + // is likely enough, but we don't take any chances. + conn->oiv.get (datahdr.rnd, RAND_SIZE); #endif require (EVP_EncryptUpdate (cctx, @@ -496,7 +495,7 @@ u8 *d; u32 l = len - data_hdr_size (); - require (EVP_DecryptInit_ex (cctx, 0, 0, 0, 0)); + require (EVP_CipherInit_ex (cctx, 0, 0, 0, 0, 0)); #if ENABLE_COMPRESSION u8 cdata[MAX_MTU]; @@ -505,9 +504,15 @@ d = cdata; else #endif - d = &(*p)[6 + 6 - DATAHDR]; + d = &(*p)[6 + 6] - DATAHDR; + + // we play do evil games with the struct layout atm. + // pending better solutions, we at least do some verification. + // this is fine, as we left ISO territory long ago. + require (DATAHDR <= 16); + require ((u8 *)(&p->len + 1) == &(*p)[0]); - /* this overwrites part of the src mac, but we fix that later */ + // this can overwrite the len/dst/src fields require (EVP_DecryptUpdate (cctx, d, &outl2, (unsigned char *)&data, len - data_hdr_size ())); @@ -548,15 +553,13 @@ struct config_packet : vpn_packet { - // actually, hmaclen cannot be checked because the hmac - // field comes before this data, so peers with other - // hmacs simply will not work. - u8 prot_major, prot_minor, randsize, hmaclen; - u8 flags, challengelen, features, pad3; - u32 cipher_nid, digest_nid, hmac_nid; + u8 serial[SERIAL_SIZE]; + u8 prot_major, prot_minor, randsize; + u8 flags, features, pad6, pad7, pad8; + u32 cipher_nid, mac_nid, auth_nid; void setup (ptype type, int dst); - bool chk_config () const; + bool chk_config (const conf_node *conf, const sockinfo &rsi) const; static u8 get_features () { @@ -580,55 +583,63 @@ prot_major = PROTOCOL_MAJOR; prot_minor = PROTOCOL_MINOR; randsize = RAND_SIZE; - hmaclen = HMACLENGTH; flags = 0; - challengelen = sizeof (rsachallenge); features = get_features (); - cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); - digest_nid = htonl (EVP_MD_type (RSA_HASH)); - hmac_nid = htonl (EVP_MD_type (DIGEST)); + strncpy ((char *)serial, conf.serial, sizeof (serial)); + + cipher_nid = htonl (EVP_CIPHER_nid (CIPHER ())); + mac_nid = htonl (EVP_MD_type (MAC_DIGEST ())); + auth_nid = htonl (EVP_MD_type (AUTH_DIGEST ())); len = sizeof (*this) - sizeof (net_packet); set_hdr (type, dst); } bool -config_packet::chk_config () const +config_packet::chk_config (const conf_node *conf, const sockinfo &rsi) const { if (prot_major != PROTOCOL_MAJOR) - slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); + slog (L_WARN, _("%s(%s): major version mismatch (remote %d <=> local %d)"), + conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR); else if (randsize != RAND_SIZE) - slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); - else if (hmaclen != HMACLENGTH) - slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); - else if (challengelen != sizeof (rsachallenge)) - slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); - else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) - slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); - else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) - slog (L_WARN, _("digest mismatch (remote %x <=> local %x)"), ntohl (digest_nid), EVP_MD_type (RSA_HASH)); - else if (hmac_nid != htonl (EVP_MD_type (DIGEST))) - slog (L_WARN, _("hmac mismatch (remote %x <=> local %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST)); + slog (L_WARN, _("%s(%s): rand size mismatch (remote %d <=> local %d)"), + conf->nodename, (const char *)rsi, randsize, RAND_SIZE); + else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER ()))) + slog (L_WARN, _("%s(%s): cipher algo mismatch (remote %x <=> local %x)"), + conf->nodename, (const char *)rsi, ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER ())); + else if (mac_nid != htonl (EVP_MD_type (MAC_DIGEST ()))) + slog (L_WARN, _("%s(%s): mac algo mismatch (remote %x <=> local %x)"), + conf->nodename, (const char *)rsi, ntohl (mac_nid), EVP_MD_type (MAC_DIGEST ())); + else if (auth_nid != htonl (EVP_MD_type (AUTH_DIGEST ()))) + slog (L_WARN, _("%s(%s): auth algo mismatch (remote %x <=> local %x)"), + conf->nodename, (const char *)rsi, ntohl (auth_nid), EVP_MD_type (AUTH_DIGEST ())); else - return true; + { + int cmp = memcmp (serial, ::conf.serial, sizeof (serial)); + + if (cmp > 0) + slog (L_WARN, _("%s(%s): remote serial newer than local serial - outdated config?"), + conf->nodename, (const char *)rsi); + else if (cmp == 0) + return true; + } return false; } -struct auth_req_packet : config_packet +struct auth_req_packet : config_packet // UNPROTECTED { char magic[8]; u8 initiate; // false if this is just an automatic reply u8 protocols; // supported protocols (will be patched on forward) u8 pad2, pad3; - rsaid id; - rsaencrdata encr; + auth_encr encr; auth_req_packet (int dst, bool initiate_, u8 protocols_) { config_packet::setup (PT_AUTH_REQ, dst); - strncpy (magic, MAGIC_OLD, 8); + memcpy (magic, MAGIC, 8); initiate = !!initiate_; protocols = protocols_; @@ -636,16 +647,13 @@ } }; -struct auth_res_packet : config_packet +struct auth_res_packet : vpn_packet // UNPROTECTED { - rsaid id; - u8 pad1, pad2, pad3; - u8 response_len; // encrypted length - rsaresponse response; + auth_response response; auth_res_packet (int dst) { - config_packet::setup (PT_AUTH_RES, dst); + set_hdr (PT_AUTH_RES, dst); len = sizeof (*this) - sizeof (net_packet); } @@ -685,42 +693,56 @@ ///////////////////////////////////////////////////////////////////////////// void -connection::connection_established () +connection::connection_established (const sockinfo &rsi) { - slog (L_NOISE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); + if (!have_snd_auth || !have_rcv_auth) + return; - if (ictx && octx) + si = rsi; + protocol = rsi.prot; + + slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), + conf->nodename, (const char *)rsi, + vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded", + PROTOCOL_MAJOR, prot_minor); + + if (::conf.script_node_up) { - // make sure rekeying timeouts are slightly asymmetric - ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); - rekey.start (rekey_interval, rekey_interval); - keepalive.start (::conf.keepalive); + run_script_cb *cb = new run_script_cb; + cb->set (this); + run_script_queued (cb, _("node-up command execution failed, continuing.")); + } + + delete ictx; ictx = new crypto_ctx (rcv_auth, snd_auth, rcv_ecdh_a, rcv_auth.ecdh, 0); + iseqno.reset (ntohl (rcv_auth.rsa.seqno) & 0x7fffffff); - // send queued packets - if (ictx && octx) - { - while (tap_packet *p = (tap_packet *)data_queue.get ()) - { - if (p->len) send_data_packet (p); - delete p; - } + delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1); + oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; - while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) - { - if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); - delete p; - } - } + oiv.reset (); - vpn->connection_established (this); + // make sure rekeying timeouts are slightly asymmetric + ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); + rekey.start (rekey_interval, rekey_interval); + + hmac_error = 0.; + + keepalive.start (::conf.keepalive); + + // send queued packets + while (tap_packet *p = (tap_packet *)data_queue.get ()) + { + if (p->len) send_data_packet (p); + delete p; } - else + + while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) { - retry_cnt = 0; - establish_connection.start (5); - keepalive.stop (); - rekey.stop (); + if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); + delete p; } + + vpn->connection_established (this); } void @@ -735,8 +757,6 @@ } si.set (conf, protocol); - - is_direct = si.valid (); } // ensure sockinfo is valid, forward if necessary @@ -765,7 +785,7 @@ connection::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) { if (!vpn->send_vpn_packet (pkt, si, tos)) - reset_connection (); + reset_connection ("packet send error"); } void @@ -776,7 +796,6 @@ pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); slog (L_TRACE, "%s << %s [%s]", conf->nodename, pong ? "PT_PONG" : "PT_PING", (const char *)si); - send_vpn_packet (pkt, si, IPTOS_LOWDELAY); delete pkt; @@ -801,30 +820,24 @@ { auth_req_packet *pkt = new auth_req_packet (conf->id, initiate, THISNODE->protocols); - rsachallenge chg; - rsa_cache.gen (pkt->id, chg); - rsa_encrypt (conf->rsa_key, chg, pkt->encr); + generate_auth_data (); + auth_encrypt (conf->rsa_key, snd_auth, pkt->encr); slog (L_TRACE, "%s << PT_AUTH_REQ [%s]", conf->nodename, (const char *)si); - send_vpn_packet (pkt, si, IPTOS_RELIABILITY | IPTOS_LOWDELAY); // rsa is very very costly delete pkt; } void -connection::send_auth_response (const sockinfo &si, const rsaid &id, const rsachallenge &chg) +connection::send_auth_response (const sockinfo &si) { auth_res_packet *pkt = new auth_res_packet (conf->id); - pkt->id = id; - - rsa_hash (id, chg, pkt->response); - - pkt->hmac_set (octx); + memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof rcv_ecdh_b); + auth_hash (rcv_auth, rcv_ecdh_b, pkt->response.mac); slog (L_TRACE, "%s << PT_AUTH_RES [%s]", conf->nodename, (const char *)si); - send_vpn_packet (pkt, si, IPTOS_RELIABILITY); // rsa is very very costly delete pkt; @@ -858,7 +871,7 @@ // and stop trying. should probably be handled by a per-connection expire handler. if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) { - reset_connection (); + reset_connection ("no demand"); return; } @@ -909,12 +922,12 @@ } void -connection::reset_connection () +connection::reset_connection (const char *reason) { if (ictx && octx) { - slog (L_INFO, _("%s(%s): connection lost"), - conf->nodename, (const char *)si); + slog (L_INFO, _("%s(%s): connection lost (%s)"), + conf->nodename, (const char *)si, reason); if (::conf.script_node_down) { @@ -929,6 +942,10 @@ si.host = 0; + have_snd_auth = false; + have_rcv_auth = false; + auth_expire = 0.; + last_activity = 0.; //last_si_change = 0.; retry_cnt = 0; @@ -944,14 +961,14 @@ if (ictx && octx) send_reset (si); - reset_connection (); + reset_connection ("shutdown"); } // poor-man's rekeying inline void connection::rekey_cb (ev::timer &w, int revents) { - reset_connection (); + reset_connection ("rekeying"); establish_connection (); } @@ -1048,20 +1065,18 @@ break; case vpn_packet::PT_RESET: - { - reset_connection (); + slog (L_TRACE, "%s >> PT_RESET", conf->nodename); - config_packet *p = (config_packet *) pkt; + if (ictx && octx) + { + reset_connection ("remote reset"); + + config_packet *p = (config_packet *) pkt; + + if (p->chk_config (conf, rsi) && connectmode == conf_node::C_ALWAYS) + establish_connection (); + } - if (!p->chk_config ()) - { - slog (L_WARN, _("%s(%s): protocol mismatch, disabling node."), - conf->nodename, (const char *)rsi); - connectmode = conf_node::C_DISABLED; - } - else if (connectmode == conf_node::C_ALWAYS) - establish_connection (); - } break; case vpn_packet::PT_AUTH_REQ: @@ -1073,8 +1088,12 @@ conf->nodename, p->initiate ? "initiate" : "reply", p->protocols, p->features); - if (p->chk_config () - && (!memcmp (p->magic, MAGIC_OLD, 8) || !memcmp (p->magic, MAGIC, 8))) + if (memcmp (p->magic, MAGIC, 8)) + { + slog (L_WARN, _("%s(%s): protocol magic mismatch - stray packet?"), + conf->nodename, (const char *)rsi); + } + else if (p->chk_config (conf, rsi)) { if (p->prot_minor != PROTOCOL_MINOR) slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), @@ -1082,36 +1101,40 @@ PROTOCOL_MINOR, conf->nodename, p->prot_minor); if (p->initiate) - send_auth_request (rsi, false); + { + send_auth_request (rsi, false); + + if (ictx && octx) + reset_connection ("reconnect"); + } - rsachallenge k; + auth_data auth; - if (!rsa_decrypt (::conf.rsa_key, p->encr, k)) + if (!auth_decrypt (::conf.rsa_key, p->encr, auth)) { slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); - break; } else { - delete octx; + bool chg = !have_rcv_auth || memcmp (&rcv_auth, &auth, sizeof auth); - octx = new crypto_ctx (k, 1); - oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; + rcv_auth = auth; + have_rcv_auth = true; - conf->protocols = p->protocols; - features = p->features & config_packet::get_features (); + send_auth_response (rsi); - send_auth_response (rsi, p->id, k); - - connection_established (); + if (chg) + { + conf->protocols = p->protocols; + features = p->features & config_packet::get_features (); - break; + connection_established (rsi); + } } + + break; } - else - slog (L_WARN, _("%s(%s): protocol mismatch."), - conf->nodename, (const char *)rsi); send_reset (rsi); } @@ -1124,76 +1147,22 @@ slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); - if (p->chk_config ()) - { - if (p->prot_minor != PROTOCOL_MINOR) - slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), - conf->nodename, (const char *)rsi, - PROTOCOL_MINOR, conf->nodename, p->prot_minor); - - rsachallenge chg; - - if (!rsa_cache.find (p->id, chg)) - { - slog (L_ERR, _("%s(%s): unrequested auth response, ignoring."), - conf->nodename, (const char *)rsi); - break; - } - else - { - crypto_ctx *cctx = new crypto_ctx (chg, 0); + auth_mac local_mac; + auth_hash (snd_auth, p->response.ecdh, local_mac); - if (!p->hmac_chk (cctx)) - { - slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" - "could be an attack, or just corruption or a synchronization error."), - conf->nodename, (const char *)rsi); - break; - } - else - { - rsaresponse h; - - rsa_hash (p->id, chg, h); - - if (!memcmp ((u8 *)&h, (u8 *)p->response, sizeof h)) - { - prot_minor = p->prot_minor; - - delete ictx; ictx = cctx; - - iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid - - si = rsi; - protocol = rsi.prot; - - slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), - conf->nodename, (const char *)rsi, - is_direct ? "direct" : "forwarded", - p->prot_major, p->prot_minor); - - connection_established (); - - if (::conf.script_node_up) - { - run_script_cb *cb = new run_script_cb; - cb->set (this); - run_script_queued (cb, _("node-up command execution failed, continuing.")); - } - - break; - } - else - slog (L_ERR, _("%s(%s): sent and received challenge do not match."), - conf->nodename, (const char *)rsi); - } + if (memcmp (&p->response.mac, local_mac, sizeof local_mac)) + { + slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), + conf->nodename, (const char *)rsi); + } + else if (!have_snd_auth) + { + memcpy (snd_ecdh_b, p->response.ecdh, sizeof snd_ecdh_b); - delete cctx; - } + have_snd_auth = true; + connection_established (rsi); } } - - send_reset (rsi); break; case vpn_packet::PT_DATA_COMPRESSED: @@ -1209,15 +1178,34 @@ vpndata_packet *p = (vpndata_packet *)pkt; if (!p->hmac_chk (ictx)) - slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" - "could be an attack, or just corruption or a synchronization error."), - conf->nodename, (const char *)rsi); + { + // rekeying often creates temporary hmac auth floods + // we assume they don't take longer than a few seconds normally, + // and suppress messages and resets during that time. + //TODO: should be done per source address + if (!hmac_error) + { + hmac_error = ev_now () + 3; + break; + } + else if (hmac_error >= ev_now ()) + break; // silently suppress + else + { + slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" + "could be an attack, or just corruption or a synchronization error."), + conf->nodename, (const char *)rsi); + // reset + } + } else { u32 seqno; tap_packet *d = p->unpack (this, seqno); int seqclass = iseqno.seqno_classify (seqno); + hmac_error = 0; + if (seqclass == 0) // ok { vpn->tap->send (d); @@ -1312,17 +1300,23 @@ protocol = best_protocol (c->conf->protocols & THISNODE->protocols & p->si.supported_protocols (c->conf)); p->si.upgrade_protocol (protocol, c->conf); - slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s,p%02x) [%d]", + slog (L_TRACE, "%s >> PT_CONNECT_INFO(%s,%s,protocols=%02x,protocol=%02x,upgradable=%02x) [%d]", conf->nodename, vpn->conns[p->id - 1]->conf->nodename, (const char *)p->si, p->protocols, + protocol, + p->si.supported_protocols (c->conf), !c->ictx && !c->octx); const sockinfo &dsi = forward_si (p->si); if (dsi.valid ()) c->send_auth_request (dsi, true); + else + slog (L_INFO, "connect info for %s received (%s), but still unable to contact.", + vpn->conns[p->id - 1]->conf->nodename, + (const char *)p->si); } else slog (L_WARN, @@ -1341,25 +1335,27 @@ inline void connection::keepalive_cb (ev::timer &w, int revents) { - if (ev_now () >= last_activity + ::conf.keepalive + 15) + ev_tstamp when = last_activity + ::conf.keepalive - ev::now (); + + if (when >= 0) + w.start (when); + else if (when < -15) { - reset_connection (); + reset_connection ("keepalive overdue"); establish_connection (); } - else if (ev_now () < last_activity + ::conf.keepalive) - w.start (last_activity + ::conf.keepalive - ev::now ()); else if (conf->connectmode != conf_node::C_ONDEMAND || THISNODE->connectmode != conf_node::C_ONDEMAND) { - send_ping (si); w.start (3); + send_ping (si); } - else if (ev_now () < last_activity + ::conf.keepalive + 10) + else if (when >= -10) // hold ondemand connections implicitly a few seconds longer // should delete octx, though, or something like that ;) - w.start (last_activity + ::conf.keepalive + 10 - ev::now ()); + w.start (when + 10); else - reset_connection (); + reset_connection ("keepalive timeout"); } void @@ -1469,7 +1465,7 @@ if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) vpn_queue.put (new net_packet); - reset_connection (); + reset_connection ("startup"); } connection::~connection ()