3 * Request a key over UDP, or respond to such a request
5 * (c) 2012 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of udpkey.
12 * The udpkey program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * The udpkey program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with udpkey; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
36 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <arpa/inet.h>
45 #include <netinet/in.h>
48 #include <mLib/alloc.h>
50 #include <mLib/daemonize.h>
51 #include <mLib/dstr.h>
52 #include <mLib/fdflags.h>
53 #include <mLib/fwatch.h>
55 #include <mLib/mdwopt.h>
56 #include <mLib/quis.h>
57 #include <mLib/report.h>
61 #include <catacomb/buf.h>
62 #include <catacomb/ct.h>
63 #include <catacomb/dh.h>
64 #include <catacomb/ec.h>
65 #include <catacomb/ec-keys.h>
66 #include <catacomb/gcipher.h>
67 #include <catacomb/gmac.h>
68 #include <catacomb/group.h>
69 #include <catacomb/key.h>
70 #include <catacomb/mp.h>
71 #include <catacomb/mprand.h>
72 #include <catacomb/noise.h>
73 #include <catacomb/rand.h>
75 #include <catacomb/rijndael-counter.h>
76 #include <catacomb/sha256.h>
84 /*---- Static variables ---------------------------------------------------*/
86 static unsigned flags = 0;
93 static unsigned char ibuf[BUFSZ], obuf[BUFSZ];
96 static const char *kfname = "keyring";
97 static const char *pidfile;
98 static fwatch kfwatch;
101 /*----- Miscellaneous utilities -------------------------------------------*/
103 /* Resolve NAME, storing the address in *ADDR. Exit on error. */
104 static void resolve(const char *name, struct in_addr *addr)
108 if ((h = gethostbyname(name)) == 0)
109 die(1, "failed to resolve `%s': %s", name, hstrerror(h_errno));
110 if (h->h_addrtype != AF_INET)
111 die(1, "unexpected address type %d", h->h_addrtype);
112 memcpy(addr, h->h_addr, sizeof(struct in_addr));
115 /* Convert PORT to a port number (in host byte order). Exit on error. */
116 static unsigned short getport(const char *port)
123 if (!isdigit(*port) ||
124 (i = strtoul(port, &q, 0)) == 0 ||
125 i >= 65536 || *q || errno)
126 die(1, "invalid port number `%s'", port);
128 return ((unsigned short)i);
131 /* Read the file named by NAME into a buffer -- or at least an initial
132 * portion of it; set *P to the start and *SZ to the length. Return -1 if it
133 * didn't work. The buffer doesn't need to be freed: the data is stashed in
136 static int snarf(const char *name, void **p, size_t *sz)
141 if ((fd = open(name, O_RDONLY)) < 0) return (-1);
142 n = read(fd, ibuf, sizeof(ibuf));
144 if (n < 0) return (-1);
149 /* Complain about something. If f_syslog is set then complain to that;
150 * otherwise write to stderr. Don't use `%m' because that won't work when
153 static void PRINTF_LIKE(2, 3) complain(int sev, const char *msg, ...)
158 if (flags & f_syslog)
159 vsyslog(sev, msg, ap);
161 fprintf(stderr, "%s: ", QUIS);
162 vfprintf(stderr, msg, ap);
167 /*----- Reading key data --------------------------------------------------*/
174 const gcmac *mc; size_t tagsz;
178 /* Clear a kinfo structure so it can be freed without trouble. */
179 static void k_init(struct kinfo *k) { k->g = 0; k->x = 0; k->X = 0; }
181 /* Free a kinfo structure. This is safe on any initialized kinfo
184 static void k_free(struct kinfo *k)
186 if (k->X) { G_DESTROY(k->g, k->X); k->X = 0; }
187 if (k->x) { MP_DROP(k->x); k->x = 0; }
188 if (k->g) { G_DESTROYGROUP(k->g); k->g = 0; }
191 /* Empty macro arguments are forbidden. But arguments are expended during
192 * replacement, not while the call is being processed, so this hack is OK.
193 * Unfortunately, if a potentially empty argument is passed on to another
194 * macro then it needs to be guarded with a use of EMPTY too...
198 /* Table of key types. Entries have the form
200 * _(name, NAME, SETGROUP, SETPRIV, SETPUB)
202 * The name and NAME are lower- and uppercase names for the type used for
203 * constructing various type name constant names. The code fragment SETGROUP
204 * initializes k->g given the name_{pub,priv} structure in p; SETPRIV and
205 * SETPUB set up k->x and k->X respectively. (In this last case, k->X will
206 * have been created as a group element already.)
208 #define KEYTYPES(_) \
211 { k->g = group_prime(&p.dp); }, \
212 { k->x = MP_COPY(p.x); }, \
213 { if (G_FROMINT(k->g, k->X, p.y)) { \
214 complain(LOG_ERR, "bad public key in `%s'", t->buf); \
220 { ec_info ei; const char *e; \
221 if ((e = ec_getinfo(&ei, p.cstr)) != 0) { \
222 complain(LOG_ERR, "bad elliptic curve in `%s': %s", t->buf, e); \
225 k->g = group_ec(&ei); \
227 { k->x = MP_COPY(p.x); }, \
228 { if (G_FROMEC(k->g, k->X, &p.p)) { \
229 complain(LOG_ERR, "bad public point in `%s'", t->buf); \
234 /* Define load_tywhich, where which is `pub' or `priv', to load a public or
235 * private key. Other parameters are as for the KEYTYPES list above.
237 #define KLOAD(ty, TY, which, WHICH, setgroup, setpriv, setpub) \
238 static int load_##ty##which(key_data *kd, struct kinfo *k, dstr *t) \
240 key_packstruct kps[TY##_##WHICH##FETCHSZ]; \
245 /* Extract the key data from the keydata. */ \
246 kp = key_fetchinit(ty##_##which##fetch, kps, &p); \
247 if ((rc = key_unpack(kp, kd, t)) != 0) { \
248 complain(LOG_ERR, "failed to unpack key `%s': %s", \
249 t->buf, key_strerror(rc)); \
253 /* Extract the components as abstract group elements. */ \
256 k->X = G_CREATE(k->g); \
259 /* Dispose of stuff we don't need. */ \
263 /* Tidy up after mishaps. */ \
270 /* Map over the KEYTYPES to declare the load_tywhich functions using KLOAD
273 #define KEYTYPE_KLOAD(ty, TY, setgroup, setpriv, setpub) \
274 KLOAD(ty, TY, priv, PRIV, setgroup, setpriv, \
275 { G_EXP(k->g, k->X, k->g->g, k->x); }) \
276 KLOAD(ty, TY, pub, PUB, setgroup, { }, setpub)
277 KEYTYPES(KEYTYPE_KLOAD)
279 /* Define a table of group key-loading operations. */
282 int (*loadpriv)(key_data *, struct kinfo *, dstr *);
283 int (*loadpub)(key_data *, struct kinfo *, dstr *);
286 static const struct kload_ops kload_ops[] = {
287 #define KEYTYPE_OPS(ty, TY, setgroup, setpriv, setpub) \
288 { #ty, load_##ty##priv, load_##ty##pub },
289 KEYTYPES(KEYTYPE_OPS)
293 /* Load a private or public (indicated by PRIVP) key named TAG into a kinfo
294 * structure K. Also fill in the cipher suite selections extracted from the
297 static int loadkey(const char *tag, struct kinfo *k, int privp)
299 const struct kload_ops *ops;
300 dstr d = DSTR_INIT, dd = DSTR_INIT;
308 /* Find the key data. */
309 if (key_qtag(kf, tag, &d, &ky, &kd)) {
310 complain(LOG_ERR, "unknown key tag `%s'", tag);
314 /* Find the key's group type and locate the group operations. */
315 ty = key_getattr(kf, ky, "group");
316 if (!ty && strncmp(ky->type, "udpkey-", 7) == 0) ty = ky->type + 7;
318 complain(LOG_ERR, "no group type for key %s", d.buf);
321 for (ops = kload_ops; ops->name; ops++) {
322 if (strcmp(ty, ops->name) == 0)
325 complain(LOG_ERR, "unknown group type `%s' in key %s", ty, d.buf);
329 /* Extract the key data into an appropriately abstract form. */
330 k->g = 0; k->x = 0; k->X = 0;
331 if ((rc = (privp ? ops->loadpriv : ops->loadpub)(*kd, k, &d)) != 0)
334 /* Extract the chosen symmetric cipher. */
335 if ((p = key_getattr(kf, ky, "cipher")) == 0)
336 k->cc = &rijndael_counter;
337 else if ((k->cc = gcipher_byname(p)) == 0) {
338 complain(LOG_ERR, "unknown cipher `%s' in key %s", p, d.buf);
342 /* And the chosen hash function. */
343 if ((p = key_getattr(kf, ky, "hash")) == 0)
345 else if ((k->hc = ghash_byname(p)) == 0) {
346 complain(LOG_ERR, "unknown hash `%s' in key %s", p, d.buf);
350 /* And finally a MAC. This is more fiddly because we must handle (a)
351 * truncation and (b) defaulting based on the hash.
353 if ((p = key_getattr(kf, ky, "mac")) == 0)
354 dstr_putf(&dd, "%s-hmac", k->hc->name);
357 if ((q = strchr(dd.buf, '/')) != 0) *q++ = 0;
359 if ((k->mc = gmac_byname(dd.buf)) == 0) {
360 complain(LOG_ERR, "unknown mac `%s' in key %s", dd.buf, d.buf);
364 k->tagsz = k->mc->hashsz/2;
367 if (tsz <= 0 || tsz%8 || tsz/8 > k->mc->hashsz) {
368 complain(LOG_ERR, "bad tag size `%s' for mac `%s' in key %s",
369 q, k->mc->name, d.buf);
387 static void keymoan(const char *file, int line, const char *err, void *p)
388 { complain(LOG_ERR, "%s:%d: %s", file, line, err); }
390 /* Update the keyring `kf' if the file has been changed since we last looked.
392 static void kfupdate(void)
396 if (!fwatch_update(&kfwatch, kfname)) return;
397 kfnew = CREATE(key_file);
398 if (key_open(kfnew, kfname, KOPEN_READ, keymoan, 0)) {
407 /*----- Low-level crypto operations ---------------------------------------*/
409 /* Derive a key, writing its address to *KK and size to *N. The size is
410 * compatible with the keysz rules KSZ. It is generated for the purpose of
411 * keying a WHAT (used for key separation and in error messages), and NAME is
412 * the name of the specific instance (e.g., `twofish-counter') from the class
413 * name. The kinfo structure K tells us which algorithms to use for the
414 * derivation. The group elements U and Z are the cryptographic inputs
415 * for the derivation.
417 * Basically all we do is compute H(what || U || Z).
419 static int derive(struct kinfo *k, ge *U, ge *Z,
420 const char *what, const char *name, const octet *ksz,
421 octet **kk, size_t *n)
427 /* Find a suitable key size. */
428 if ((*n = keysz(k->hc->hashsz, ksz)) == 0) {
430 "failed to find suitable key size for %s `%s' and hash `%s'",
431 what, name, k->hc->name);
435 /* Build the hash preimage. */
436 buf_init(&b, obuf, sizeof(obuf));
437 buf_put(&b, "udpkey-", 7);
438 buf_putstrz(&b, what);
439 G_TORAW(k->g, &b, U);
440 G_TORAW(k->g, &b, Z);
442 complain(LOG_ERR, "overflow while deriving key (prepare preimage)!");
446 /* Derive the output key. */
448 GH_HASH(h, BBASE(&b), BLEN(&b));
449 buf_init(&b, obuf, sizeof(obuf));
450 if ((p = buf_get(&b, h->ops->c->hashsz)) == 0) {
451 complain(LOG_ERR, "overflow while deriving key (output hash)!");
462 static void debug_mp(const char *what, mp *x)
463 { fprintf(stderr, "%s: *** ", QUIS); MP_EPRINT(what, x); }
464 static void debug_ge(const char *what, group *g, ge *X)
466 fprintf(stderr, "%s: *** %s = ", QUIS, what);
467 group_writefile(g, X, stderr);
472 /*----- Listening for requests --------------------------------------------*/
474 /* Rate limiting parameters.
476 * There's a probabilistic rate-limiting mechanism. A counter starts at 0.
477 * Every time we oricess a request, we increment the counter. The counter
478 * drops by RATE_REFILL every second. If the counter is below RATE_CREDIT
479 * then the request is processed; otherwise it is processed with probability
480 * 1/(counter - RATE_CREDIT).
482 #define RATE_REFILL 10 /* Credits per second. */
483 #define RATE_CREDIT 1000 /* Initial credit. */
485 static int dolisten(int argc, char *argv[])
494 struct sockaddr_in sin;
499 dstr d = DSTR_INIT, dd = DSTR_INIT;
503 mp *r = MP_NEW, *v = MP_NEW;
504 ge *R = 0, *U = 0, *V = 0, *W = 0, *Y = 0, *Z = 0;
511 unsigned bucket = 0, toks;
512 time_t last = 0, now;
514 /* Set up the socket address. */
515 sin.sin_family = AF_INET;
516 aspec = xstrdup(argv[0]);
517 if ((p = strchr(aspec, ':')) == 0) {
519 sin.sin_addr.s_addr = INADDR_ANY;
522 resolve(aspec, &sin.sin_addr);
524 sin.sin_port = htons(getport(p));
526 /* Create and set up the socket itself. */
527 if ((sk = socket(PF_INET, SOCK_DGRAM, 0)) < 0 ||
528 fdflags(sk, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC) ||
529 bind(sk, (struct sockaddr *)&sin, sizeof(sin)))
530 die(1, "failed to create socket: %s", strerror(errno));
532 /* That's enough initialization. If we should fork, then do that. */
533 if (flags & f_daemon) {
534 if (pidfile && (fp = fopen(pidfile, "w")) == 0)
535 die(1, "failed to open pidfile `%s': %s", pidfile, strerror(errno));
536 openlog(QUIS, LOG_PID, LOG_DAEMON);
538 die(1, "failed to become background process: %s", strerror(errno));
539 if (pidfile) { fprintf(fp, "%ld\n", (long)getpid()); fclose(fp); }
545 /* Clear out the key state. */
548 /* Wait for something to happen. */
551 if (select(sk + 1, &fdin, 0, 0, 0) < 0)
552 die(1, "select failed: %s", strerror(errno));
553 noise_timer(RAND_GLOBAL);
555 /* Fetch a packet. */
557 n = recvfrom(sk, ibuf, sizeof(ibuf), 0, (struct sockaddr *)&sin, &len);
559 if (errno != EAGAIN && errno != EINTR)
560 complain(LOG_ERR, "unexpected receive error: %s", strerror(errno));
564 /* Refill the bucket, and see whether we should reject this packet. */
566 if (bucket && now != last) {
567 toks = (now - last)*RATE_REFILL;
568 bucket = bucket < toks ? 0 : bucket - toks;
571 if (bucket > RATE_CREDIT &&
572 grand_range(&rand_global, bucket - RATE_CREDIT))
576 /* Set up the input buffer for parsing the request. */
577 buf_init(&bin, ibuf, n);
579 /* Extract the key tag name. */
580 if ((p = buf_getmemz(&bin, &sz)) == 0) {
581 complain(LOG_WARNING, "invalid key tag from %s:%d",
582 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
588 if (key_qtag(kf, p, &d, &ky, &kkd)) {
589 complain(LOG_WARNING, "unknown key tag `%s' from %s:%d",
590 p, inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
594 /* And make sure that it has the right shape. */
595 if ((ky->k->e & KF_ENCMASK) != KENC_BINARY) {
596 complain(LOG_ERR, "key %s is not plain binary data", d.buf);
600 /* Find the list of clients, and look up the caller's address in the
601 * list. Entries have the form ADDRESS[/LEN][=TAG] and are separated by
604 if ((pp = key_getattr(kf, ky, "clients")) == 0) {
605 complain(LOG_WARNING,
606 "key %s requested from %s:%d has no `clients' attribute",
607 d.buf, inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
614 while (isdigit((unsigned char)*q) || *q == '.') q++;
616 if (!inet_aton(p, &in)) goto skip;
621 while (isdigit((unsigned char)*q)) q++;
625 if (((sin.sin_addr.s_addr ^ in.s_addr) &
626 (0xffffffff << (32 - mlen))) == 0)
631 while (*p && *p != ';') p++;
634 complain(LOG_WARNING, "access to key %s denied to %s:%d",
635 d.buf, inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
639 /* Build a tag name for the caller's KEM key, either from the client
640 * match or the source address.
644 dstr_puts(&dd, "client-");
645 dstr_puts(&dd, inet_ntoa(sin.sin_addr));
649 while (*q && *q != ';') q++;
650 if (*q == ';') *q++ = 0;
653 /* Report the match. */
654 complain(LOG_NOTICE, "client %s:%d (`%s') requests key %s",
655 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), p, d.buf);
657 /* Load the KEM key. */
658 if (loadkey(p, &k, 0)) goto again;
659 D( debug_ge("X", k.g, k.X); )
661 /* Read the caller's ephemeral key. */
662 R = G_CREATE(k.g); W = G_CREATE(k.g);
663 U = G_CREATE(k.g); V = G_CREATE(k.g);
664 Y = G_CREATE(k.g); Z = G_CREATE(k.g);
665 if (G_FROMBUF(k.g, &bin, U)) {
666 complain(LOG_WARNING, "failed to read ephemeral vector from %s:%d",
667 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
670 D( debug_ge("U", k.g, U); )
672 complain(LOG_WARNING, "trailing junk in request from %s:%d",
673 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
677 /* Ephemeral Diffie--Hellman. Choose v in GF(q) at random; compute
678 * V = v P and -Y = (-v) U.
680 v = mprand_range(v, k.g->r, &rand_global, 0);
681 G_EXP(k.g, V, k.g->g, v);
682 D( debug_mp("v", v); debug_ge("V", k.g, V); )
683 v = mp_sub(v, k.g->r, v);
685 D( debug_ge("-Y", k.g, Y); )
687 /* DLIES. Choose r in GF(q) at random; compute R = r P and Z = r X.
688 * Mask the clue R as W = R - Y. (Doing the subtraction here makes life
689 * easier at the other end, since we can determine -Y by negating v
690 * whereas the recipient must subtract vectors which may be less
693 r = mprand_range(r, k.g->r, &rand_global, 0);
694 G_EXP(k.g, R, k.g->g, r);
695 D( debug_mp("r", r); debug_ge("R", k.g, R); )
696 G_EXP(k.g, Z, k.X, r);
698 D( debug_ge("Z", k.g, Z); debug_ge("W", k.g, W); )
700 /* Derive encryption and integrity keys. */
701 derive(&k, R, Z, "cipher", k.cc->name, k.cc->keysz, &kk, &ksz);
702 c = GC_INIT(k.cc, kk, ksz);
703 derive(&k, R, Z, "mac", k.mc->name, k.mc->keysz, &kk, &ksz);
704 m = GM_KEY(k.mc, kk, ksz);
706 /* Build the ciphertext and compute a MAC tag over it. */
707 buf_init(&bout, obuf, sizeof(obuf));
708 if (G_TOBUF(k.g, &bout, V) ||
709 G_TOBUF(k.g, &bout, W))
711 if ((t = buf_get(&bout, k.tagsz)) == 0) goto bad;
713 if (BENSURE(&bout, sz)) goto bad;
714 GC_ENCRYPT(c, ky->k->u.k.k, BCUR(&bout), sz);
716 GH_HASH(h, BCUR(&bout), sz);
717 tt = GH_DONE(h, 0); memcpy(t, tt, k.tagsz);
720 /* Send the reply packet back to the caller. */
721 if (sendto(sk, BBASE(&bout), BLEN(&bout), 0,
722 (struct sockaddr *)&sin, len) < 0) {
723 complain(LOG_ERR, "failed to send response to %s:%d: %s",
724 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port),
732 /* Report a problem building the reply. */
733 complain(LOG_ERR, "failed to construct response to %s:%d",
734 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
737 /* Free stuff for the next iteration. */
738 DRESET(&d); DRESET(&dd);
739 if (R) { G_DESTROY(k.g, R); R = 0; }
740 if (U) { G_DESTROY(k.g, U); U = 0; }
741 if (V) { G_DESTROY(k.g, V); V = 0; }
742 if (W) { G_DESTROY(k.g, W); W = 0; }
743 if (Y) { G_DESTROY(k.g, Y); Y = 0; }
744 if (Z) { G_DESTROY(k.g, Z); Z = 0; }
745 if (c) { GC_DESTROY(c); c = 0; }
746 if (m) { GM_DESTROY(m); m = 0; }
747 if (h) { GH_DESTROY(h); h = 0; }
754 /*----- Sending requests and processing responses -------------------------*/
765 struct sockaddr_in sin;
772 /* Record a successful fetch of key material for a query Q. The data starts
773 * at K and is SZ bytes long. The data is copied: it's safe to overwrite it.
775 static void donequery(struct query *q, const void *k, size_t sz)
776 { q->k = xmalloc(sz); memcpy(q->k, k, sz); q->sz = sz; nq--; }
778 /* Initialize a query to a remote server. */
779 static struct query *qinit_net(const char *tag, const char *spec)
782 struct server *s, **stail;
783 dstr d = DSTR_INIT, dd = DSTR_INIT;
787 /* Allocate the query block. */
788 q = CREATE(struct query);
791 /* Put the spec somewhere we can hack at it. */
795 /* Parse the query spec. Entries have the form ADDRESS:PORT[=TAG][#HASH]
796 * and are separated by `;'.
800 /* Allocate a new server node. */
801 s = CREATE(struct server);
802 s->sin.sin_family = AF_INET;
804 /* Extract the server address. */
805 if ((pp = strchr(p, ':')) == 0)
806 die(1, "invalid syntax: missing `:PORT'");
808 resolve(p, &s->sin.sin_addr);
810 /* Extract the port number. */
812 while (isdigit((unsigned char)*pp)) pp++;
814 s->sin.sin_port = htons(getport(p));
816 /* If there's a key tag then extract that; otherwise use a default. */
821 pp += strcspn(pp, ";#");
824 if (loadkey(p, &s->k, 1)) exit(1);
825 D( debug_mp("x", s->k.x); debug_ge("X", s->k.g, s->k.X); )
827 /* Choose an ephemeral private key u. Let x be our private key. We
828 * compute U = u P and transmit this.
830 s->u = mprand_range(MP_NEW, s->k.g->r, &rand_global, 0);
831 s->U = G_CREATE(s->k.g);
832 G_EXP(s->k.g, s->U, s->k.g->g, s->u);
833 D( debug_mp("u", s->u); debug_ge("U", s->k.g, s->U); )
835 /* Link the server on. */
836 *stail = s; stail = &s->next;
838 /* If there's a trailing hash then extract it. */
843 while (*pp == '-' || isxdigit((unsigned char)*pp)) pp++;
846 hex_decode(&hc, p, pp - p, &dd);
847 if (dd.len != s->k.hc->hashsz) die(1, "incorrect hash length");
848 s->h = xmalloc(dd.len);
849 memcpy(s->h, dd.buf, dd.len);
853 /* If there are more servers, then continue parsing. */
855 else if (ch != ';') die(1, "invalid syntax: expected `;'");
859 /* Terminate the server list and return. */
867 /* Handle a `query' to a local file. */
868 static struct query *qinit_file(const char *tag, const char *file)
874 /* Snarf the file. */
875 q = CREATE(struct query);
876 if (snarf(file, &k, &sz))
877 die(1, "failed to read `%s': %s", file, strerror(errno));
883 /* Reransmission and timeout parameters. */
884 #define TO_NEXT(t) (((t) + 2)*4/3) /* Timeout growth function */
885 #define TO_MAX 30 /* When to give up */
887 static int doquery(int argc, char *argv[])
889 struct query *q = 0, *qq, **qtail = &qq;
890 struct server *s = 0;
891 const char *tag = argv[0];
896 struct timeval now, when, tv;
897 struct sockaddr_in sin;
898 ge *R, *V = 0, *W = 0, *Y = 0, *Z = 0;
909 /* Create a socket. We just use the one socket for everything. We don't
910 * care which port we get allocated.
912 if ((sk = socket(PF_INET, SOCK_DGRAM, 0)) < 0 ||
913 fdflags(sk, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC))
914 die(1, "failed to create socket: %s", strerror(errno));
916 /* Parse the query target specifications. The adjustments of `nq' aren't
917 * in the right order but that doesn't matter.
919 for (i = 1; i < argc; i++) {
920 if (*argv[i] == '.' || *argv[i] == '/') q = qinit_file(tag, argv[i]);
921 else if (strchr(argv[i], ':')) q = qinit_net(tag, argv[i]);
922 else die(1, "unrecognized query target `%s'", argv[i]);
923 *qtail = q; qtail = &q->next; nq++;
927 /* Find the current time so we can compute retransmission times properly.
929 gettimeofday(&now, 0);
932 /* Continue retransmitting until we have all the answers. */
935 /* Work out when we next want to wake up. */
936 if (TV_CMP(&now, >=, &when)) {
938 if (next >= TO_MAX) die(1, "no responses: giving up");
939 next = TO_NEXT(next);
940 TV_ADDL(&when, &when, next, 0);
941 } while (TV_CMP(&when, <=, &now));
942 for (q = qq; q; q = q->next) {
944 for (s = q->s; s; s = s->next) {
945 buf_init(&bout, obuf, sizeof(obuf));
946 buf_putstrz(&bout, tag);
947 G_TOBUF(s->k.g, &bout, s->U);
949 moan("overflow while constructing request!");
952 sendto(sk, BBASE(&bout), BLEN(&bout), 0,
953 (struct sockaddr *)&s->sin, sizeof(s->sin));
958 /* Wait until something interesting happens. */
961 TV_SUB(&tv, &when, &now);
962 if (select(sk + 1, &fdin, 0, 0, &tv) < 0)
963 die(1, "select failed: %s", strerror(errno));
964 gettimeofday(&now, 0);
966 /* If we have an input event, process incoming packets. */
967 if (FD_ISSET(sk, &fdin)) {
970 /* Read a packet and capture its address. */
972 nn = recvfrom(sk, ibuf, sizeof(ibuf), 0,
973 (struct sockaddr *)&sin, &len);
975 if (errno == EAGAIN) break;
976 else if (errno == EINTR) continue;
978 moan("error receiving reply: %s", strerror(errno));
983 /* Wee whether this corresponds to any of our servers. Don't just
984 * check the active servers, since this may be late replies caused by
985 * retransmissions or similar.
987 for (q = qq; q; q = q->next) {
988 for (s = q->s; s; s = s->next) {
989 if (s->sin.sin_addr.s_addr == sin.sin_addr.s_addr &&
990 s->sin.sin_port == sin.sin_port)
994 moan("received reply from unexpected source %s:%d",
995 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
999 /* If the query we found has now been satisfied, ignore this packet.
1001 if (q->k) goto again;
1003 /* Start parsing the reply. */
1004 buf_init(&bin, ibuf, nn);
1005 R = G_CREATE(s->k.g);
1006 V = G_CREATE(s->k.g); W = G_CREATE(s->k.g);
1007 Y = G_CREATE(s->k.g); Z = G_CREATE(s->k.g);
1008 if (G_FROMBUF(s->k.g, &bin, V)) {
1009 moan("invalid Diffie--Hellman vector from %s:%d",
1010 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
1013 if (G_FROMBUF(s->k.g, &bin, W)) {
1014 moan("invalid clue vector from %s:%d",
1015 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
1018 D( debug_ge("V", s->k.g, V); debug_ge("W", s->k.g, W); )
1020 /* We have V and W from the server; determine Y = u V, R = W + Y and
1021 * Z = x R, and then derive the symmetric keys.
1023 G_EXP(s->k.g, Y, V, s->u);
1024 G_MUL(s->k.g, R, W, Y);
1025 G_EXP(s->k.g, Z, R, s->k.x);
1026 D( debug_ge("R", s->k.g, R);
1027 debug_ge("Y", s->k.g, Y);
1028 debug_ge("Z", s->k.g, Z); )
1029 derive(&s->k, R, Z, "cipher", s->k.cc->name, s->k.cc->keysz,
1031 c = GC_INIT(s->k.cc, kk, ksz);
1032 derive(&s->k, R, Z, "mac", s->k.cc->name, s->k.cc->keysz,
1034 m = GM_KEY(s->k.mc, kk, ksz);
1036 /* Find where the MAC tag is. */
1037 if ((t = buf_get(&bin, s->k.tagsz)) == 0) {
1038 moan("missing tag from %s:%d",
1039 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
1043 /* Check the integrity of the ciphertext against the tag. */
1044 p = BCUR(&bin); n = BLEFT(&bin);
1048 if (!ct_memeq(t, tt, s->k.tagsz)) {
1049 moan("incorrect tag from %s:%d",
1050 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
1054 /* Decrypt the result and declare this server done. */
1055 GC_DECRYPT(c, p, p, n);
1058 h = GH_INIT(s->k.hc);
1061 if (memcmp(tt, s->h, h->ops->c->hashsz) != 0) {
1062 moan("response from %s:%d doesn't match hash",
1063 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
1070 /* Tidy things up for the next run through. */
1071 if (R) { G_DESTROY(s->k.g, R); R = 0; }
1072 if (V) { G_DESTROY(s->k.g, V); V = 0; }
1073 if (W) { G_DESTROY(s->k.g, W); W = 0; }
1074 if (Y) { G_DESTROY(s->k.g, Y); Y = 0; }
1075 if (Z) { G_DESTROY(s->k.g, Z); Z = 0; }
1076 if (c) { GC_DESTROY(c); c = 0; }
1077 if (m) { GM_DESTROY(m); m = 0; }
1078 if (h) { GH_DESTROY(h); h = 0; }
1083 /* Check that all of the responses match up and XOR them together. */
1085 if (n > BUFSZ) die(1, "response too large");
1087 for (q = qq; q; q = q->next) {
1088 if (!q->k) die(1, "INTERNAL: query not complete");
1089 if (q->sz != n) die(1, "inconsistent response sizes");
1090 for (j = 0; j < n; j++) obuf[j] ^= q->k[j];
1093 /* Write out the completed answer. */
1096 if ((nn = write(STDOUT_FILENO, p, n)) < 0)
1097 die(1, "error writing response: %s", strerror(errno));
1103 /*----- Main program ------------------------------------------------------*/
1105 static void usage(FILE *fp)
1107 pquis(fp, "Usage: \n\
1108 $ [-OPTS] LABEL {ADDR:PORT | FILE} ...\n\
1109 $ [-OPTS] -l [ADDR:]PORT\n\
1113 static void version(FILE *fp)
1114 { pquis(fp, "$, version " VERSION "\n"); }
1116 static void help(FILE *fp)
1124 -d, --daemon Run in the background while listening.\n\
1125 -k, --keyring=FILE Read keys from FILE. [default = `keyring']\n\
1126 -l, --listen Listen for incoming requests and serve keys.\n\
1127 -p, --pidfile=FILE Write process id to FILE if in daemon mode.\n\
1128 -r, --random=FILE Key random number generator with contents of FILE.\n\
1132 int main(int argc, char *argv[])
1140 static const struct option opts[] = {
1141 { "help", 0, 0, 'h' },
1142 { "version", 0, 0, 'v' },
1143 { "usage", 0, 0, 'u' },
1144 { "daemon", 0, 0, 'd' },
1145 { "keyfile", OPTF_ARGREQ, 0, 'k' },
1146 { "listen", 0, 0, 'l' },
1147 { "pidfile", OPTF_ARGREQ, 0, 'p' },
1148 { "random", OPTF_ARGREQ, 0, 'r' },
1152 int i = mdwopt(argc, argv, "hvu" "dk:lp:r:", opts, 0, 0, 0);
1156 case 'h': help(stdout); exit(0);
1157 case 'v': version(stdout); exit(0);
1158 case 'u': usage(stdout); exit(0);
1160 case 'd': flags |= f_daemon; break;
1161 case 'k': kfname = optarg; break;
1162 case 'l': flags |= f_listen; break;
1163 case 'p': pidfile = optarg; break;
1165 if (snarf(optarg, &k, &sz))
1166 die(1, "failed to read `%s': %s", optarg, strerror(errno));
1167 rand_key(RAND_GLOBAL, k, sz);
1170 default: flags |= f_bogus; break;
1174 argv += optind; argc -= optind;
1175 if (flags & f_listen) argmin = argmax = 1;
1176 else argmin = 2, argmax = -1;
1177 if ((flags & f_bogus) || argc < argmin || (argmax >= 0 && argc > argmax))
1178 { usage(stderr); exit(1); }
1180 fwatch_init(&kfwatch, kfname);
1181 kf = CREATE(key_file);
1182 if (key_open(kf, kfname, KOPEN_READ, keymoan, 0))
1183 die(1, "failed to open keyring file `%s'", kfname);
1185 rand_noisesrc(RAND_GLOBAL, &noise_source);
1186 rand_seed(RAND_GLOBAL, 512);
1188 if (flags & f_listen) return dolisten(argc, argv);
1189 else return doquery(argc, argv);
1192 /*----- That's all, folks -------------------------------------------------*/