--- gvpe/src/connection.C 2013/07/18 13:35:16 1.102 +++ gvpe/src/connection.C 2016/06/30 11:43:38 1.114 @@ -1,6 +1,6 @@ /* connection.C -- manage a single connection - Copyright (C) 2003-2008,2010,2011,2013 Marc Lehmann + Copyright (C) 2003-2008,2010,2011,2013,2016 Marc Lehmann This file is part of GVPE. @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -42,6 +43,7 @@ #include "conf.h" #include "slog.h" +#include "crypto.h" #include "device.h" #include "vpn.h" #include "connection.h" @@ -50,7 +52,6 @@ #include "netcompat.h" #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 @@ -105,7 +106,7 @@ struct crypto_ctx { EVP_CIPHER_CTX cctx; - HMAC_CTX hctx; + hmac hctx; crypto_ctx (const auth_data &auth1, const auth_data &auth2, const ecdh_key &a, const ecdh_key &b, int enc); ~crypto_ctx (); @@ -127,8 +128,7 @@ 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)); + hctx.init (mac_key, MAC_KEYSIZE, MAC_DIGEST ()); } { @@ -149,7 +149,6 @@ crypto_ctx::~crypto_ctx () { require (EVP_CIPHER_CTX_cleanup (&cctx)); - HMAC_CTX_cleanup (&hctx); } static inline void @@ -180,15 +179,12 @@ } 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 @@ -199,7 +195,6 @@ // request data 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); @@ -365,35 +360,28 @@ ///////////////////////////////////////////////////////////////////////////// -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)); + ctx->hctx.init (); + ctx->hctx.add (((unsigned char *) this) + sizeof (hmac_packet), len - sizeof (hmac_packet)); + ctx->hctx.digest (hmac_digest); } 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 @@ -409,11 +397,11 @@ } #define MAXVPNDATA (MAX_MTU - 6 - 6) -#define DATAHDR (sizeof (u32) + RAND_SIZE) struct vpndata_packet : vpn_packet { - u8 data[MAXVPNDATA + DATAHDR]; // seqno + u32 ctr; // seqno + u8 data[MAXVPNDATA]; void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); tap_packet *unpack (connection *conn, u32 &seqno); @@ -421,10 +409,33 @@ private: const u32 data_hdr_size () const { - return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; + // the distance from beginning of packet to data member + return data - at (0); } }; +// expands packet counter (unlike seqno, in network byte order) to counter mode IV +static unsigned char * +expand_iv (u32 ctr) +{ + static u32 iv[IV_SIZE (CIPHER) / 4]; + + require (sizeof (iv) == 4 * 4); + require (IV_SIZE (CIPHER) % 4 == 0); + + iv[0] = + iv[1] = + iv[2] = ctr; + + // I would reuse ctr here to to avoid potential endianness issues, + // but it seems openssl wraps around. While this would be still ok, + // and I don't even know if its true, let's play safe and initialise + // to 0. + iv[3] = 0; + + return (unsigned char *)iv; +} + void vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) { @@ -451,36 +462,20 @@ } #endif - require (EVP_CipherInit_ex (cctx, 0, 0, 0, 0, 1)); - - struct { -#if RAND_SIZE - u8 rnd[RAND_SIZE]; -#endif - u32 seqno; - } datahdr; - - datahdr.seqno = ntohl (seqno); -#if 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 + ctr = htonl (seqno); - require (EVP_EncryptUpdate (cctx, - (unsigned char *) data + outl, &outl2, - (unsigned char *) &datahdr, DATAHDR)); - outl += outl2; + require (EVP_EncryptInit_ex (cctx, 0, 0, 0, expand_iv (ctr))); require (EVP_EncryptUpdate (cctx, - (unsigned char *) data + outl, &outl2, - (unsigned char *) d, l)); + (unsigned char *)data + outl, &outl2, + (unsigned char *)d, l)); outl += outl2; - require (EVP_EncryptFinal_ex (cctx, (unsigned char *) data + outl, &outl2)); + // it seems this is a nop for us, but we do it anyways + require (EVP_EncryptFinal_ex (cctx, (unsigned char *)data + outl, &outl2)); outl += outl2; - len = outl + data_hdr_size (); + len = data_hdr_size () + outl; set_hdr (type, dst); @@ -494,9 +489,10 @@ int outl = 0, outl2; tap_packet *p = new tap_packet; u8 *d; - u32 l = len - data_hdr_size (); - require (EVP_CipherInit_ex (cctx, 0, 0, 0, 0, 0)); + seqno = ntohl (ctr); + + require (EVP_DecryptInit_ex (cctx, 0, 0, 0, expand_iv (ctr))); #if ENABLE_COMPRESSION u8 cdata[MAX_MTU]; @@ -505,13 +501,7 @@ d = cdata; else #endif - 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]); + d = &(*p)[6 + 6]; // this can overwrite the len/dst/src fields require (EVP_DecryptUpdate (cctx, @@ -519,25 +509,24 @@ (unsigned char *)&data, len - data_hdr_size ())); outl += outl2; + // it seems this is a nop for us, but we do it anyways require (EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2)); outl += outl2; - seqno = ntohl (*(u32 *)(d + RAND_SIZE)); - id2mac (dst () ? dst() : THISNODE->id, p->dst); id2mac (src (), p->src); #if ENABLE_COMPRESSION if (type == PT_DATA_COMPRESSED) { - u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; + u32 cl = (d[0] << 8) | d[1]; - p->len = lzf_decompress (d + DATAHDR + 2, cl < MAX_MTU ? cl : 0, + p->len = lzf_decompress (d + 2, cl < MAX_MTU - 2 ? cl : 0, &(*p)[6 + 6], MAX_MTU) + 6 + 6; } else - p->len = outl + (6 + 6 - DATAHDR); + p->len = outl + (6 + 6); #endif return p; @@ -554,12 +543,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 () { @@ -582,10 +572,11 @@ { prot_major = PROTOCOL_MAJOR; prot_minor = PROTOCOL_MINOR; - randsize = RAND_SIZE; 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 ())); @@ -595,20 +586,30 @@ } 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); - else if (randsize != RAND_SIZE) - slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); + slog (L_WARN, _("%s(%s): major version mismatch (remote %d <=> local %d)"), + conf->nodename, (const char *)rsi, prot_major, PROTOCOL_MAJOR); 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; } @@ -632,13 +633,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); } @@ -688,7 +689,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) @@ -704,28 +705,25 @@ delete octx; octx = new crypto_ctx (snd_auth, rcv_auth, snd_ecdh_a, snd_ecdh_b , 1); oseqno = ntohl (snd_auth.rsa.seqno) & 0x7fffffff; - oiv.reset (); - // 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 - if (ictx && octx) + while (tap_packet *p = (tap_packet *)data_queue.get ()) { - while (tap_packet *p = (tap_packet *)data_queue.get ()) - { - if (p->len) send_data_packet (p); - delete p; - } + if (p->len) send_data_packet (p); + delete p; + } - while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) - { - if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); - delete p; - } + while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) + { + if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); + delete p; } vpn->connection_established (this); @@ -743,8 +741,6 @@ } si.set (conf, protocol); - - is_direct = si.valid (); } // ensure sockinfo is valid, forward if necessary @@ -773,7 +769,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 +818,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 +855,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 +867,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 +887,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 +909,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 +948,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 +982,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 +1052,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 +1075,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 +1088,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 +1104,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 +1122,6 @@ break; } - else - slog (L_WARN, _("%s(%s): protocol mismatch."), - conf->nodename, (const char *)rsi); send_reset (rsi); } @@ -1130,32 +1134,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)) + { + slog (L_ERR, _("%s(%s): unrequested or outdated auth response, ignoring."), + conf->nodename, (const char *)rsi); + } + else if (!have_snd_auth) { - 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); - } + 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 +1165,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 +1328,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 +1342,7 @@ // should delete octx, though, or something like that ;) w.start (when + 10); else - reset_connection (); + reset_connection ("keepalive timeout"); } void @@ -1439,7 +1452,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 ()