From 7ed14135434df136e971a43b1774b827714ef7f9 Mon Sep 17 00:00:00 2001 Message-Id: <7ed14135434df136e971a43b1774b827714ef7f9.1715427753.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sun, 13 Jul 2003 11:19:49 +0000 Subject: [PATCH] Incopatible protocol fix! Include message type code under MAC tag to prevent cut-and-paste from key-exchange messages to general packet transport. Organization: Straylight/Edgeware From: mdw --- client.c | 8 +++++++- keyexch.c | 25 ++++++++++++++++--------- keyset.c | 43 ++++++++++++++++++++++++++++++------------- peer.c | 10 +++++++--- tripe.h | 22 +++++++++++++++++----- 5 files changed, 77 insertions(+), 31 deletions(-) diff --git a/client.c b/client.c index 696e9c77..1014d246 100644 --- a/client.c +++ b/client.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: client.c,v 1.10 2003/05/17 10:59:19 mdw Exp $ + * $Id: client.c,v 1.11 2003/07/13 11:19:49 mdw Exp $ * * Client for TrIPE * @@ -29,6 +29,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: client.c,v $ + * Revision 1.11 2003/07/13 11:19:49 mdw + * Incopatible protocol fix! Include message type code under MAC tag to prevent + * cut-and-paste from key-exchange messages to general packet transport. + * * Revision 1.10 2003/05/17 10:59:19 mdw * Put a newline in the pidfile. * @@ -440,6 +444,8 @@ int main(int argc, char *argv[]) close(pfd[0]); if (logfp) fclose(logfp); + if (pidfp) + fclose(pidfp); closelog(); if (f & f_daemon) u_detach(); diff --git a/keyexch.c b/keyexch.c index f2ea62b1..eb4fc9c8 100644 --- a/keyexch.c +++ b/keyexch.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: keyexch.c,v 1.7 2003/05/17 11:01:28 mdw Exp $ + * $Id: keyexch.c,v 1.8 2003/07/13 11:19:49 mdw Exp $ * * Key exchange protocol * @@ -29,6 +29,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: keyexch.c,v $ + * Revision 1.8 2003/07/13 11:19:49 mdw + * Incopatible protocol fix! Include message type code under MAC tag to prevent + * cut-and-paste from key-exchange messages to general packet transport. + * * Revision 1.7 2003/05/17 11:01:28 mdw * Handle flags on challenge timers correctly to prevent confusing the event * list. @@ -328,7 +332,7 @@ static void kxc_answer(keyexch *kx, kxchal *kxc) buf_init(&bb, buf_i, sizeof(buf_i)); buf_putmp(&bb, kxc->r); buf_flip(&bb); - ks_encrypt(kxc->ks, &bb, b); + ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_REPLY, &bb, b); } /* --- Update the statistics --- */ @@ -615,7 +619,7 @@ static void resend(keyexch *kx) buf_putmp(&bb, kxc->r); buf_put(&bb, kxc->hswrq_out, HASHSZ); buf_flip(&bb); - ks_encrypt(kxc->ks, &bb, b); + ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCH, &bb, b); break; case KXS_SWITCH: T( trace(T_KEYEXCH, "keyexch: sending switch confirmation to `%s'", @@ -625,7 +629,7 @@ static void resend(keyexch *kx) buf_init(&bb, buf_i, sizeof(buf_i)); buf_put(&bb, kxc->hswok_out, HASHSZ); buf_flip(&bb); - ks_encrypt(kxc->ks, &bb, b); + ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCHOK, &bb, b); break; default: abort(); @@ -644,6 +648,7 @@ static void resend(keyexch *kx) /* --- @matchreply@ --- * * * Arguments: @keyexch *kx@ = pointer to key exchange context + * @unsigned ty@ = type of incoming message * @const octet *hc_in@ = a hash of his challenge * @const octet *hc_out@ = a hash of my challenge (cookie) * @mp *ck@ = his expected-reply hash (optional) @@ -657,7 +662,7 @@ static void resend(keyexch *kx) * challenge is returned. */ -static kxchal *matchreply(keyexch *kx, const octet *hc_in, +static kxchal *matchreply(keyexch *kx, unsigned ty, const octet *hc_in, const octet *hc_out, mp *ck, buf *b) { kxchal *kxc; @@ -696,7 +701,7 @@ static kxchal *matchreply(keyexch *kx, const octet *hc_in, /* --- Decrypt the rest of the packet --- */ buf_init(&bb, buf_o, sizeof(buf_o)); - if (ks_decrypt(kxc->ks, b, &bb)) { + if (ks_decrypt(kxc->ks, ty, b, &bb)) { a_warn("failed to decrypt reply from `%s'", p_name(kx->p)); goto bad; } @@ -775,7 +780,8 @@ static int doreply(keyexch *kx, buf *b) a_warn("invalid reply packet from `%s'", p_name(kx->p)); goto bad; } - if ((kxc = matchreply(kx, hc_in, hc_out, ck, b)) == 0) + if ((kxc = matchreply(kx, MSG_KEYEXCH | KX_REPLY, + hc_in, hc_out, ck, b)) == 0) goto bad; if (BLEFT(b)) { a_warn("invalid reply packet from `%s'", p_name(kx->p)); @@ -813,7 +819,8 @@ static int doswitch(keyexch *kx, buf *b) a_warn("invalid switch request from `%s'", p_name(kx->p)); goto bad; } - if ((kxc = matchreply(kx, hc_in, hc_out, 0, b)) == 0) + if ((kxc = matchreply(kx, MSG_KEYEXCH | KX_SWITCH, + hc_in, hc_out, 0, b)) == 0) goto bad; if ((hswrq = buf_get(b, HASHSZ)) == 0 || BLEFT(b)) { a_warn("invalid switch request from `%s'", p_name(kx->p)); @@ -864,7 +871,7 @@ static int doswitchok(keyexch *kx, buf *b) } kxc = kx->r[0]; buf_init(&bb, buf_o, sizeof(buf_o)); - if (ks_decrypt(kxc->ks, b, &bb)) { + if (ks_decrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCHOK, b, &bb)) { a_warn("failed to decrypt switch confirmation from `%s'", p_name(kx->p)); goto bad; } diff --git a/keyset.c b/keyset.c index 12ade4f3..d109714c 100644 --- a/keyset.c +++ b/keyset.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: keyset.c,v 1.7 2003/05/17 11:00:47 mdw Exp $ + * $Id: keyset.c,v 1.8 2003/07/13 11:19:49 mdw Exp $ * * Handling of symmetric keysets * @@ -29,6 +29,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: keyset.c,v $ + * Revision 1.8 2003/07/13 11:19:49 mdw + * Incopatible protocol fix! Include message type code under MAC tag to prevent + * cut-and-paste from key-exchange messages to general packet transport. + * * Revision 1.7 2003/05/17 11:00:47 mdw * Don't make scary messages just because one key didn't work on a message: * only be frightened if they all fail. Set initial keyset refcount @@ -82,13 +86,14 @@ /* --- Encrypted data format --- * * - * Let %$p_i$% be the %$i$%-th plaintext message. We first compute + * Let %$p_i$% be the %$i$%-th plaintext message, with type %$t$%. We first + * compute * * %$c_i = \mathcal{E}\textrm{-CBC}_{K_{\text{E}}}(p_i)$% * * as the CBC-ciphertext of %$p_i$%, and then * - * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(i, c_i)$% + * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(t, i, c_i)$% * * as a MAC on the %%\emph{ciphertext}%%. The message sent is then the pair * %$(\sigma_i, c_i)$%. This construction is provably secure in the NM-CCA @@ -106,6 +111,7 @@ /* --- @doencrypt@ --- * * * Arguments: @keyset *ks@ = pointer to keyset to use + * @unsigned ty@ = type of message this is * @buf *b@ = pointer to an input buffer * @buf *bb@ = pointer to an output buffer * @@ -115,7 +121,7 @@ * keyset is OK to use. */ -static int doencrypt(keyset *ks, buf *b, buf *bb) +static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb) { ghash *h; gcipher *c; @@ -124,6 +130,7 @@ static int doencrypt(keyset *ks, buf *b, buf *bb) octet *qmac, *qseq, *qiv, *qpk; uint32 oseq; size_t osz, nsz; + octet t[4]; int rc = 0; /* --- Allocate the required buffer space --- */ @@ -133,6 +140,7 @@ static int doencrypt(keyset *ks, buf *b, buf *bb) return (0); /* Caution! */ qmac = BCUR(bb); qseq = qmac + MACSZ; qiv = qseq + SEQSZ; qpk = qiv + IVSZ; BSTEP(bb, MACSZ + SEQSZ + IVSZ + sz); + STORE32(t, ty); /* --- Encrypt the packet --- */ @@ -149,6 +157,7 @@ static int doencrypt(keyset *ks, buf *b, buf *bb) /* --- Now compute the MAC --- */ h = ks->mout->ops->init(ks->mout); + h->ops->hash(h, t, sizeof(t)); h->ops->hash(h, qseq, SEQSZ + IVSZ + sz); memcpy(qmac, h->ops->done(h, 0), MACSZ); h->ops->destroy(h); @@ -175,6 +184,7 @@ static int doencrypt(keyset *ks, buf *b, buf *bb) /* --- @dodecrypt@ --- * * * Arguments: @keyset *ks@ = pointer to keyset to use + * @unsigned ty@ = expected type code * @buf *b@ = pointer to an input buffer * @buf *bb@ = pointer to an output buffer * @uint32 *seq@ = where to store the sequence number @@ -189,7 +199,7 @@ static int doencrypt(keyset *ks, buf *b, buf *bb) * packet, and the packet's sequence number is stored in @*seq@. */ -static int dodecrypt(keyset *ks, buf *b, buf *bb, uint32 *seq) +static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq) { const octet *pmac, *piv, *pseq, *ppk; size_t psz = BLEFT(b); @@ -200,6 +210,7 @@ static int dodecrypt(keyset *ks, buf *b, buf *bb, uint32 *seq) size_t ivsz = c->ops->c->blksz; octet *mac; int eq; + octet t[4]; /* --- Break up the packet into its components --- */ @@ -209,10 +220,12 @@ static int dodecrypt(keyset *ks, buf *b, buf *bb, uint32 *seq) } sz = psz - IVSZ - SEQSZ - MACSZ; pmac = BCUR(b); pseq = pmac + MACSZ; piv = pseq + SEQSZ; ppk = piv + IVSZ; + STORE32(t, ty); /* --- Verify the MAC on the packet --- */ h = ks->min->ops->init(ks->min); + h->ops->hash(h, t, sizeof(t)); h->ops->hash(h, pseq, SEQSZ + IVSZ + sz); mac = h->ops->done(h, 0); eq = !memcmp(mac, pmac, MACSZ); @@ -422,6 +435,7 @@ void ks_activate(keyset *ks) /* --- @ks_encrypt@ --- * * * Arguments: @keyset *ks@ = pointer to a keyset + * @unsigned ty@ = message type * @buf *b@ = pointer to input buffer * @buf *bb@ = pointer to output buffer * @@ -435,7 +449,7 @@ void ks_activate(keyset *ks) * used even if it's marked as not for data output. */ -int ks_encrypt(keyset *ks, buf *b, buf *bb) +int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb) { time_t now = time(0); @@ -443,12 +457,13 @@ int ks_encrypt(keyset *ks, buf *b, buf *bb) buf_break(bb); return (0); } - return (doencrypt(ks, b, bb)); + return (doencrypt(ks, ty, b, bb)); } /* --- @ks_decrypt@ --- * * * Arguments: @keyset *ks@ = pointer to a keyset + * @unsigned ty@ = expected type code * @buf *b@ = pointer to an input buffer * @buf *bb@ = pointer to an output buffer * @@ -459,14 +474,14 @@ int ks_encrypt(keyset *ks, buf *b, buf *bb) * marking that it's not for encryption. */ -int ks_decrypt(keyset *ks, buf *b, buf *bb) +int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb) { time_t now = time(0); uint32 seq; if (!KEYOK(ks, now) || buf_ensure(bb, BLEN(b)) || - dodecrypt(ks, b, bb, &seq) || + dodecrypt(ks, ty, b, bb, &seq) || dosequence(ks, seq)) return (-1); return (0); @@ -553,6 +568,7 @@ void ksl_prune(keyset **ksroot) /* --- @ksl_encrypt@ --- * * * Arguments: @keyset **ksroot@ = pointer to keyset list head + * @unsigned ty@ = message type * @buf *b@ = pointer to input buffer * @buf *bb@ = pointer to output buffer * @@ -561,7 +577,7 @@ void ksl_prune(keyset **ksroot) * Use: Encrypts a packet. */ -int ksl_encrypt(keyset **ksroot, buf *b, buf *bb) +int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) { time_t now = time(0); keyset *ks = *ksroot; @@ -577,12 +593,13 @@ int ksl_encrypt(keyset **ksroot, buf *b, buf *bb) ks = ks->next; } - return (doencrypt(ks, b, bb)); + return (doencrypt(ks, ty, b, bb)); } /* --- @ksl_decrypt@ --- * * * Arguments: @keyset **ksroot@ = pointer to keyset list head + * @unsigned ty@ = expected type code * @buf *b@ = pointer to input buffer * @buf *bb@ = pointer to output buffer * @@ -591,7 +608,7 @@ int ksl_encrypt(keyset **ksroot, buf *b, buf *bb) * Use: Decrypts a packet. */ -int ksl_decrypt(keyset **ksroot, buf *b, buf *bb) +int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb) { time_t now = time(0); keyset *ks; @@ -603,7 +620,7 @@ int ksl_decrypt(keyset **ksroot, buf *b, buf *bb) for (ks = *ksroot; ks; ks = ks->next) { if (!KEYOK(ks, now)) continue; - if (!dodecrypt(ks, b, bb, &seq)) { + if (!dodecrypt(ks, ty, b, bb, &seq)) { if (ks->f & KSF_LISTEN) { T( trace(T_KEYSET, "keyset: implicitly activating keyset %u", ks->seq); ) diff --git a/peer.c b/peer.c index 9a2fc591..e9fd280c 100644 --- a/peer.c +++ b/peer.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: peer.c,v 1.8 2003/05/16 12:09:03 mdw Exp $ + * $Id: peer.c,v 1.9 2003/07/13 11:19:49 mdw Exp $ * * Communication with the peer * @@ -29,6 +29,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: peer.c,v $ + * Revision 1.9 2003/07/13 11:19:49 mdw + * Incopatible protocol fix! Include message type code under MAC tag to prevent + * cut-and-paste from key-exchange messages to general packet transport. + * * Revision 1.8 2003/05/16 12:09:03 mdw * Allow binding to a chosen address. * @@ -135,7 +139,7 @@ found: return; } buf_init(&bb, buf_o, sizeof(buf_o)); - if (ksl_decrypt(&p->ks, &b, &bb)) { + if (ksl_decrypt(&p->ks, MSG_PACKET, &b, &bb)) { p->st.n_reject++; a_warn("couldn't decrypt inbound packet from `%s'", p->name); return; @@ -218,7 +222,7 @@ void p_tun(peer *p, buf *b) buf *bb = p_txstart(p, MSG_PACKET); TIMER; - if (ksl_encrypt(&p->ks, b, bb)) + if (ksl_encrypt(&p->ks, MSG_PACKET, b, bb)) kx_start(&p->kx); if (BOK(bb) && BLEN(bb)) { p->st.n_ipout++; diff --git a/tripe.h b/tripe.h index 1f73d6ba..8d70c1f6 100644 --- a/tripe.h +++ b/tripe.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: tripe.h,v 1.15 2003/05/16 12:09:03 mdw Exp $ + * $Id: tripe.h,v 1.16 2003/07/13 11:19:49 mdw Exp $ * * Main header file for TrIPE * @@ -29,6 +29,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: tripe.h,v $ + * Revision 1.16 2003/07/13 11:19:49 mdw + * Incopatible protocol fix! Include message type code under MAC tag to prevent + * cut-and-paste from key-exchange messages to general packet transport. + * * Revision 1.15 2003/05/16 12:09:03 mdw * Allow binding to a chosen address. * @@ -668,6 +672,7 @@ extern void ks_activate(keyset */*ks*/); /* --- @ks_encrypt@ --- * * * Arguments: @keyset *ks@ = pointer to a keyset + * @unsigned ty@ = message type * @buf *b@ = pointer to input buffer * @buf *bb@ = pointer to output buffer * @@ -681,11 +686,13 @@ extern void ks_activate(keyset */*ks*/); * used even if it's marked as not for data output. */ -extern int ks_encrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/); +extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/, + buf */*b*/, buf */*bb*/); /* --- @ks_decrypt@ --- * * * Arguments: @keyset *ks@ = pointer to a keyset + * @unsigned ty@ = expected type code * @buf *b@ = pointer to an input buffer * @buf *bb@ = pointer to an output buffer * @@ -696,7 +703,8 @@ extern int ks_encrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/); * marking that it's not for encryption. */ -extern int ks_decrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/); +extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/, + buf */*b*/, buf */*bb*/); /* --- @ksl_free@ --- * * @@ -737,6 +745,7 @@ extern void ksl_prune(keyset **/*ksroot*/); /* --- @ksl_encrypt@ --- * * * Arguments: @keyset **ksroot@ = pointer to keyset list head + * @unsigned ty@ = message type * @buf *b@ = pointer to input buffer * @buf *bb@ = pointer to output buffer * @@ -745,11 +754,13 @@ extern void ksl_prune(keyset **/*ksroot*/); * Use: Encrypts a packet. */ -extern int ksl_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/); +extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/, + buf */*b*/, buf */*bb*/); /* --- @ksl_decrypt@ --- * * * Arguments: @keyset **ksroot@ = pointer to keyset list head + * @unsigned ty@ = expected type code * @buf *b@ = pointer to input buffer * @buf *bb@ = pointer to output buffer * @@ -758,7 +769,8 @@ extern int ksl_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/); * Use: Decrypts a packet. */ -extern int ksl_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/); +extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/, + buf */*b*/, buf */*bb*/); /*----- Administration interface ------------------------------------------*/ -- [mdw]