cut-and-paste from key-exchange messages to general packet transport.
- * $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 $
/*----- Revision history --------------------------------------------------*
*
* $Log: client.c,v $
/*----- 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.
*
* Revision 1.10 2003/05/17 10:59:19 mdw
* Put a newline in the pidfile.
*
close(pfd[0]);
if (logfp)
fclose(logfp);
close(pfd[0]);
if (logfp)
fclose(logfp);
+ if (pidfp)
+ fclose(pidfp);
closelog();
if (f & f_daemon)
u_detach();
closelog();
if (f & f_daemon)
u_detach();
- * $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
*
*
* Key exchange protocol
*
/*----- Revision history --------------------------------------------------*
*
* $Log: keyexch.c,v $
/*----- 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.
* Revision 1.7 2003/05/17 11:01:28 mdw
* Handle flags on challenge timers correctly to prevent confusing the event
* list.
buf_init(&bb, buf_i, sizeof(buf_i));
buf_putmp(&bb, kxc->r);
buf_flip(&bb);
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 --- */
}
/* --- Update the statistics --- */
buf_putmp(&bb, kxc->r);
buf_put(&bb, kxc->hswrq_out, HASHSZ);
buf_flip(&bb);
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'",
break;
case KXS_SWITCH:
T( trace(T_KEYEXCH, "keyexch: sending switch confirmation to `%s'",
buf_init(&bb, buf_i, sizeof(buf_i));
buf_put(&bb, kxc->hswok_out, HASHSZ);
buf_flip(&bb);
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);
/* --- @matchreply@ --- *
*
* Arguments: @keyexch *kx@ = pointer to key exchange context
/* --- @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)
* @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)
* challenge is returned.
*/
* 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;
const octet *hc_out, mp *ck, buf *b)
{
kxchal *kxc;
/* --- Decrypt the rest of the packet --- */
buf_init(&bb, buf_o, sizeof(buf_o));
/* --- 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;
}
a_warn("failed to decrypt reply from `%s'", p_name(kx->p));
goto bad;
}
a_warn("invalid reply packet from `%s'", p_name(kx->p));
goto bad;
}
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));
goto bad;
if (BLEFT(b)) {
a_warn("invalid reply packet from `%s'", p_name(kx->p));
a_warn("invalid switch request from `%s'", p_name(kx->p));
goto bad;
}
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));
goto bad;
if ((hswrq = buf_get(b, HASHSZ)) == 0 || BLEFT(b)) {
a_warn("invalid switch request from `%s'", p_name(kx->p));
}
kxc = kx->r[0];
buf_init(&bb, buf_o, sizeof(buf_o));
}
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;
}
a_warn("failed to decrypt switch confirmation from `%s'", p_name(kx->p));
goto bad;
}
- * $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
*
*
* Handling of symmetric keysets
*
/*----- Revision history --------------------------------------------------*
*
* $Log: keyset.c,v $
/*----- 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
* 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
/* --- Encrypted data format --- *
*
/* --- 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
*
*
* %$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
*
* 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
/* --- @doencrypt@ --- *
*
* Arguments: @keyset *ks@ = pointer to keyset to use
/* --- @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
*
* @buf *b@ = pointer to an input buffer
* @buf *bb@ = pointer to an output buffer
*
* keyset is OK to use.
*/
* 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)
octet *qmac, *qseq, *qiv, *qpk;
uint32 oseq;
size_t osz, nsz;
octet *qmac, *qseq, *qiv, *qpk;
uint32 oseq;
size_t osz, nsz;
int rc = 0;
/* --- Allocate the required buffer space --- */
int rc = 0;
/* --- Allocate the required buffer space --- */
return (0); /* Caution! */
qmac = BCUR(bb); qseq = qmac + MACSZ; qiv = qseq + SEQSZ; qpk = qiv + IVSZ;
BSTEP(bb, MACSZ + SEQSZ + IVSZ + sz);
return (0); /* Caution! */
qmac = BCUR(bb); qseq = qmac + MACSZ; qiv = qseq + SEQSZ; qpk = qiv + IVSZ;
BSTEP(bb, MACSZ + SEQSZ + IVSZ + sz);
/* --- Encrypt the packet --- */
/* --- Encrypt the packet --- */
/* --- Now compute the MAC --- */
h = ks->mout->ops->init(ks->mout);
/* --- 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);
h->ops->hash(h, qseq, SEQSZ + IVSZ + sz);
memcpy(qmac, h->ops->done(h, 0), MACSZ);
h->ops->destroy(h);
/* --- @dodecrypt@ --- *
*
* Arguments: @keyset *ks@ = pointer to keyset to use
/* --- @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
* @buf *b@ = pointer to an input buffer
* @buf *bb@ = pointer to an output buffer
* @uint32 *seq@ = where to store the sequence number
* packet, and the packet's sequence number is stored in @*seq@.
*/
* 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);
{
const octet *pmac, *piv, *pseq, *ppk;
size_t psz = BLEFT(b);
size_t ivsz = c->ops->c->blksz;
octet *mac;
int eq;
size_t ivsz = c->ops->c->blksz;
octet *mac;
int eq;
/* --- Break up the packet into its components --- */
/* --- Break up the packet into its components --- */
}
sz = psz - IVSZ - SEQSZ - MACSZ;
pmac = BCUR(b); pseq = pmac + MACSZ; piv = pseq + SEQSZ; ppk = piv + IVSZ;
}
sz = psz - IVSZ - SEQSZ - MACSZ;
pmac = BCUR(b); pseq = pmac + MACSZ; piv = pseq + SEQSZ; ppk = piv + IVSZ;
/* --- Verify the MAC on the packet --- */
h = ks->min->ops->init(ks->min);
/* --- 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);
h->ops->hash(h, pseq, SEQSZ + IVSZ + sz);
mac = h->ops->done(h, 0);
eq = !memcmp(mac, pmac, MACSZ);
/* --- @ks_encrypt@ --- *
*
* Arguments: @keyset *ks@ = pointer to a keyset
/* --- @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
*
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* used even if it's marked as not for data output.
*/
* 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)
buf_break(bb);
return (0);
}
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
}
/* --- @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
*
* @buf *b@ = pointer to an input buffer
* @buf *bb@ = pointer to an output buffer
*
* marking that it's not for encryption.
*/
* 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)) ||
{
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);
dosequence(ks, seq))
return (-1);
return (0);
/* --- @ksl_encrypt@ --- *
*
* Arguments: @keyset **ksroot@ = pointer to keyset list head
/* --- @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
*
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* Use: Encrypts a packet.
*/
* 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;
{
time_t now = time(0);
keyset *ks = *ksroot;
- return (doencrypt(ks, b, bb));
+ return (doencrypt(ks, ty, b, bb));
}
/* --- @ksl_decrypt@ --- *
*
* Arguments: @keyset **ksroot@ = pointer to keyset list head
}
/* --- @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
*
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* Use: Decrypts a packet.
*/
* 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;
{
time_t now = time(0);
keyset *ks;
for (ks = *ksroot; ks; ks = ks->next) {
if (!KEYOK(ks, now))
continue;
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); )
if (ks->f & KSF_LISTEN) {
T( trace(T_KEYSET, "keyset: implicitly activating keyset %u",
ks->seq); )
- * $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
*
*
* Communication with the peer
*
/*----- Revision history --------------------------------------------------*
*
* $Log: peer.c,v $
/*----- 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.
*
* Revision 1.8 2003/05/16 12:09:03 mdw
* Allow binding to a chosen address.
*
return;
}
buf_init(&bb, buf_o, sizeof(buf_o));
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;
p->st.n_reject++;
a_warn("couldn't decrypt inbound packet from `%s'", p->name);
return;
buf *bb = p_txstart(p, MSG_PACKET);
TIMER;
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++;
kx_start(&p->kx);
if (BOK(bb) && BLEN(bb)) {
p->st.n_ipout++;
- * $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
*
*
* Main header file for TrIPE
*
/*----- Revision history --------------------------------------------------*
*
* $Log: tripe.h,v $
/*----- 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.
*
* Revision 1.15 2003/05/16 12:09:03 mdw
* Allow binding to a chosen address.
*
/* --- @ks_encrypt@ --- *
*
* Arguments: @keyset *ks@ = pointer to a keyset
/* --- @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
*
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* used even if it's marked as not for data output.
*/
* 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
/* --- @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
*
* @buf *b@ = pointer to an input buffer
* @buf *bb@ = pointer to an output buffer
*
* marking that it's not for encryption.
*/
* 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@ --- *
*
/* --- @ksl_free@ --- *
*
/* --- @ksl_encrypt@ --- *
*
* Arguments: @keyset **ksroot@ = pointer to keyset list head
/* --- @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
*
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* Use: Encrypts a packet.
*/
* 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
/* --- @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
*
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* Use: Decrypts a packet.
*/
* 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 ------------------------------------------*/
/*----- Administration interface ------------------------------------------*/