--- gvpe/src/connection.C 2013/07/17 04:36:03 1.99 +++ gvpe/src/connection.C 2014/09/12 10:40:43 1.112 @@ -35,11 +35,19 @@ #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) require (exp) +#endif + #include "conf.h" #include "slog.h" #include "device.h" @@ -121,14 +129,13 @@ 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 (auth2.rsa.ikm, sizeof (auth2.rsa.ikm)); 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); - require (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0)); + require101 (HMAC_Init_ex (&hctx, mac_key, MAC_KEYSIZE, MAC_DIGEST (), 0)); } { @@ -136,7 +143,6 @@ 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 (auth2.rsa.ikm, sizeof (auth2.rsa.ikm)); kdf.extract (auth1.rsa.cipher_key, sizeof (auth1.rsa.cipher_key)); kdf.extract (s, sizeof (s)); kdf.extract_done (HKDF_PRF_HASH ()); @@ -160,6 +166,8 @@ (unsigned char *)&auth.rsa, (unsigned char *)&encr.rsa, key, RSA_PKCS1_OAEP_PADDING) < 0) fatal ("RSA_public_encrypt error"); + + memcpy (&encr.ecdh, &auth.ecdh, sizeof (encr.ecdh)); } static inline bool @@ -173,20 +181,18 @@ return 0; memcpy (&auth.rsa, rsa_decrypt, sizeof (auth.rsa)); + memcpy (&auth.ecdh, &encr.ecdh, sizeof (auth.ecdh)); return 1; } static void -auth_hash (const auth_data &auth, auth_mac &mac) +auth_hash (const auth_data &auth, const ecdh_key &b, auth_mac &mac) { - HMAC_CTX ctx; - - HMAC_CTX_init (&ctx); - require (HMAC_Init_ex (&ctx, auth.rsa.auth_key, sizeof (auth.rsa.auth_key), AUTH_DIGEST (), 0)); - require (HMAC_Update (&ctx, (const unsigned char *)&auth, sizeof auth)); - require (HMAC_Final (&ctx, (unsigned char *)&mac, 0)); - HMAC_CTX_cleanup (&ctx); + 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 @@ -195,9 +201,8 @@ if (auth_expire < ev_now ()) { // request data - RAND_bytes ((unsigned char *)&snd_auth.rsa, sizeof snd_auth.rsa); + rand_fill (snd_auth.rsa); curve25519_generate (snd_ecdh_a, snd_auth.ecdh); - auth_hash (snd_auth, snd_auth_mac); // eventual response data curve25519_generate (rcv_ecdh_a, rcv_ecdh_b); @@ -363,35 +368,30 @@ ///////////////////////////////////////////////////////////////////////////// -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; - require (HMAC_Init_ex (hctx, 0, 0, 0, 0)); - require (HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), - len - sizeof (hmac_packet))); - require (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); - - return !memcmp (hmac, hmac_digest, HMACLENGTH); + unsigned char hmac_digest[EVP_MAX_MD_SIZE]; + hmac_gen (ctx, hmac_digest); + return slow_memeq (hmac, hmac_digest, HMACLENGTH); } void @@ -460,7 +460,9 @@ datahdr.seqno = ntohl (seqno); #if RAND_SIZE - RAND_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, @@ -501,9 +503,15 @@ d = cdata; else #endif - d = &(*p)[6 + 6 - DATAHDR]; + d = &(*p)[6 + 6] - DATAHDR; - /* this overwrites part of the src mac, but we fix that later */ + // 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 can overwrite the len/dst/src fields require (EVP_DecryptUpdate (cctx, d, &outl2, (unsigned char *)&data, len - data_hdr_size ())); @@ -544,12 +552,13 @@ struct config_packet : vpn_packet { + 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 () { @@ -576,6 +585,8 @@ flags = 0; features = get_features (); + 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 ())); @@ -585,20 +596,33 @@ } 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); + 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, _("cipher algo mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), 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, _("mac algo mismatch (remote %x <=> local %x)"), ntohl (mac_nid), 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, _("auth algo mismatch (remote %x <=> local %x)"), ntohl (auth_nid), 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; } @@ -622,13 +646,13 @@ } }; -struct auth_res_packet : config_packet // UNPROTECTED +struct auth_res_packet : vpn_packet // UNPROTECTED { 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); } @@ -678,7 +702,7 @@ slog (L_INFO, _("%s(%s): connection established (%s), protocol version %d.%d."), conf->nodename, (const char *)rsi, - is_direct ? "direct" : "forwarded", + vpn->can_direct (THISNODE, conf) ? "direct" : "forwarded", PROTOCOL_MAJOR, prot_minor); if (::conf.script_node_up) @@ -694,41 +718,30 @@ delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1); oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; - if (ictx && octx) - { - // 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); + oiv.reset (); - keepalive.start (::conf.keepalive); + // 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); - // send queued packets - if (ictx && octx) - { - while (tap_packet *p = (tap_packet *)data_queue.get ()) - { - if (p->len) send_data_packet (p); - delete p; - } + hmac_error = 0.; - while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) - { - if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); - delete p; - } - } + keepalive.start (::conf.keepalive); - vpn->connection_established (this); + // send queued packets + while (tap_packet *p = (tap_packet *)data_queue.get ()) + { + if (p->len) send_data_packet (p); + delete p; } -#if 0 - 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; } -#endif + + vpn->connection_established (this); } void @@ -743,8 +756,6 @@ } si.set (conf, protocol); - - is_direct = si.valid (); } // ensure sockinfo is valid, forward if necessary @@ -773,7 +784,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 @@ -822,8 +833,8 @@ { auth_res_packet *pkt = new auth_res_packet (conf->id); - auth_hash (rcv_auth, pkt->response.mac); - memcpy (pkt->response.ecdh, rcv_ecdh_b, sizeof (rcv_ecdh_b)); + 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 @@ -859,7 +870,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; } @@ -871,7 +882,7 @@ reset_si (); - bool slow = si.prot & PROT_SLOW; + bool slow = (si.prot & PROT_SLOW) || (conf->low_power || THISNODE->low_power); if (si.prot && !si.host && vpn->can_direct (THISNODE, conf)) { @@ -891,14 +902,17 @@ if (dsi.valid () && auth_rate_limiter.can (dsi)) { - if (retry_cnt < 4) + // use ping after the first few retries + // TODO: on rekeys, the other node might not interpret ping correctly, + // TODO: as it will still have a valid connection + if (retry_cnt < 4 && (!conf->low_power || THISNODE->low_power)) send_auth_request (dsi, true); else send_ping (dsi, 0); } } - retry_int *= slow ? 8. : 0.9; + retry_int *= slow ? 4. : 0.9; if (retry_int < conf->max_retry) retry_cnt++; @@ -910,12 +924,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) { @@ -949,14 +963,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 (); } @@ -983,7 +997,7 @@ connection::post_inject_queue () { // force a connection every now and when when packets are sent (max 1/s) - if (ev_now () - last_establish_attempt >= 0.95) // arbitrary + if (ev_now () - last_establish_attempt >= (conf->low_power || THISNODE->low_power ? 2.95 : 0.95)) // arbitrary establish_connection.stop (); establish_connection (); @@ -1053,20 +1067,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: @@ -1078,7 +1090,12 @@ conf->nodename, p->initiate ? "initiate" : "reply", p->protocols, p->features); - if (p->chk_config () && !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."), @@ -1086,7 +1103,12 @@ 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"); + } auth_data auth; @@ -1097,7 +1119,7 @@ } else { - bool chg = !have_rcv_auth || memcmp (&rcv_auth, &auth, sizeof auth); + bool chg = !have_rcv_auth || !slow_memeq (&rcv_auth, &auth, sizeof auth); rcv_auth = auth; have_rcv_auth = true; @@ -1115,9 +1137,6 @@ break; } - else - slog (L_WARN, _("%s(%s): protocol mismatch."), - conf->nodename, (const char *)rsi); send_reset (rsi); } @@ -1130,32 +1149,22 @@ slog (L_TRACE, "%s >> PT_AUTH_RES", conf->nodename); - if (p->chk_config ()) + auth_mac local_mac; + auth_hash (snd_auth, p->response.ecdh, local_mac); + + if (!slow_memeq (&p->response.mac, local_mac, sizeof local_mac)) { - if (memcmp (&p->response.mac, snd_auth_mac, sizeof (snd_auth_mac))) - { - slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), - conf->nodename, (const char *)rsi); - } - else if (!have_snd_auth) - { - 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); - - prot_minor = p->prot_minor; - memcpy (snd_ecdh_b, p->response.ecdh, sizeof (snd_ecdh_b)); - - have_snd_auth = true; - connection_established (rsi); - } + 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); - break; + have_snd_auth = true; + connection_established (rsi); } } - - send_reset (rsi); break; case vpn_packet::PT_DATA_COMPRESSED: @@ -1171,15 +1180,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); @@ -1315,7 +1343,7 @@ w.start (when); else if (when < -15) { - reset_connection (); + reset_connection ("keepalive overdue"); establish_connection (); } else if (conf->connectmode != conf_node::C_ONDEMAND @@ -1329,7 +1357,7 @@ // should delete octx, though, or something like that ;) w.start (when + 10); else - reset_connection (); + reset_connection ("keepalive timeout"); } void @@ -1439,7 +1467,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 ()