OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / pluto / preshared.c
1 /* mechanisms for preshared keys (public, private, and preshared secrets)
2  * Copyright (C) 1998-2001  D. Hugh Redelmeier.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * RCSID $Id: preshared.c,v 1.60 2002/03/22 23:38:28 dhr Exp $
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <time.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <resolv.h>
29 #include <arpa/nameser.h>       /* missing from <resolv.h> on old systems */
30 #include <netdb.h>
31
32 #include <glob.h>
33 #ifndef GLOB_ABORTED
34 # define GLOB_ABORTED    GLOB_ABEND     /* fix for old versions */
35 #endif
36
37 #include <freeswan.h>
38
39 #include "constants.h"
40 #include "defs.h"
41 #include "id.h"
42 #include "x509.h"
43 #include "connections.h"        /* needs id.h */
44 #include "state.h"
45 #include "preshared.h"
46 #include "adns.h"       /* needs <resolv.h> */
47 #include "dnskey.h"     /* needs preshared.h and adns.h */
48 #include "log.h"
49 #include "whack.h"      /* for RC_LOG_SERIOUS */
50 #include "pkcs.h"
51
52 /* Maximum length of filename and passphrase buffer */
53
54 #define BUF_LEN         256
55
56 #ifdef NAT_TRAVERSAL
57 #define PB_STREAM_UNDEFINED
58 #include "nat_traversal.h"
59 #endif
60
61 struct fld {
62     const char *name;
63     size_t offset;
64 };
65
66 static const struct fld RSA_private_field[] =
67 {
68     { "Modulus", offsetof(struct RSA_private_key, pub.n) },
69     { "PublicExponent", offsetof(struct RSA_private_key, pub.e) },
70
71     { "PrivateExponent", offsetof(struct RSA_private_key, d) },
72     { "Prime1", offsetof(struct RSA_private_key, p) },
73     { "Prime2", offsetof(struct RSA_private_key, q) },
74     { "Exponent1", offsetof(struct RSA_private_key, dP) },
75     { "Exponent2", offsetof(struct RSA_private_key, dQ) },
76     { "Coefficient", offsetof(struct RSA_private_key, qInv) },
77 };
78
79 #ifdef DEBUG
80 static void
81 RSA_show_key_fields(struct RSA_private_key *k, int fieldcnt)
82 {
83     const struct fld *p;
84
85     DBG_log(" keyid: *%s", k->pub.keyid);
86
87     for (p = RSA_private_field; p < &RSA_private_field[fieldcnt]; p++)
88     {
89         MP_INT *n = (MP_INT *) ((char *)k + p->offset);
90         size_t sz = mpz_sizeinbase(n, 16);
91         char buf[2048/4 + 2];   /* ought to be big enough */
92
93         passert(sz <= sizeof(buf));
94         mpz_get_str(buf, 16, n);
95
96         DBG_log(" %s: %s", p->name, buf);
97     }
98 }
99
100 /* debugging info that compromises security! */
101 static void
102 RSA_show_private_key(struct RSA_private_key *k)
103 {
104     RSA_show_key_fields(k, elemsof(RSA_private_field));
105 }
106
107 static void
108 RSA_show_public_key(struct RSA_public_key *k)
109 {
110     /* Kludge: pretend that it is a private key, but only display the
111      * first two fields (which are the public key).
112      */
113     passert(offsetof(struct RSA_private_key, pub) == 0);
114     RSA_show_key_fields((struct RSA_private_key *)k, 2);
115 }
116 #endif
117
118 static const char *
119 RSA_private_key_sanity(struct RSA_private_key *k)
120 {
121     /* note that the *last* error found is reported */
122     err_t ugh = NULL;
123     mpz_t t, u, q1;
124
125 #ifdef DEBUG    /* debugging info that compromises security */
126     DBG(DBG_PRIVATE, RSA_show_private_key(k));
127 #endif
128
129     /* PKCS#1 1.5 section 6 requires modulus to have at least 12 octets.
130      * We actually require more (for security).
131      */
132     if (k->pub.k < RSA_MIN_OCTETS)
133         return RSA_MIN_OCTETS_UGH;
134
135     /* we picked a max modulus size to simplify buffer allocation */
136     if (k->pub.k > RSA_MAX_OCTETS)
137         return RSA_MAX_OCTETS_UGH;
138
139     mpz_init(t);
140     mpz_init(u);
141     mpz_init(q1);
142
143     /* check that n == p * q */
144     mpz_mul(u, &k->p, &k->q);
145     if (mpz_cmp(u, &k->pub.n) != 0)
146         ugh = "n != p * q";
147
148     /* check that e divides neither p-1 nor q-1 */
149     mpz_sub_ui(t, &k->p, 1);
150     mpz_mod(t, t, &k->pub.e);
151     if (mpz_cmp_ui(t, 0) == 0)
152         ugh = "e divides p-1";
153
154     mpz_sub_ui(t, &k->q, 1);
155     mpz_mod(t, t, &k->pub.e);
156     if (mpz_cmp_ui(t, 0) == 0)
157         ugh = "e divides q-1";
158
159     /* check that d is e^-1 (mod lcm(p-1, q-1)) */
160     /* see PKCS#1v2, aka RFC 2437, for the "lcm" */
161     mpz_sub_ui(q1, &k->q, 1);
162     mpz_sub_ui(u, &k->p, 1);
163     mpz_gcd(t, u, q1);          /* t := gcd(p-1, q-1) */
164     mpz_mul(u, u, q1);          /* u := (p-1) * (q-1) */
165     mpz_divexact(u, u, t);      /* u := lcm(p-1, q-1) */
166
167     mpz_mul(t, &k->d, &k->pub.e);
168     mpz_mod(t, t, u);
169     if (mpz_cmp_ui(t, 1) != 0)
170         ugh = "(d * e) mod (lcm(p-1, q-1)) != 1";
171
172     /* check that dP is d mod (p-1) */
173     mpz_sub_ui(u, &k->p, 1);
174     mpz_mod(t, &k->d, u);
175     if (mpz_cmp(t, &k->dP) != 0)
176         ugh = "dP is not congruent to d mod (p-1)";
177
178     /* check that dQ is d mod (q-1) */
179     mpz_sub_ui(u, &k->q, 1);
180     mpz_mod(t, &k->d, u);
181     if (mpz_cmp(t, &k->dQ) != 0)
182         ugh = "dQ is not congruent to d mod (q-1)";
183
184     /* check that qInv is (q^-1) mod p */
185     mpz_mul(t, &k->qInv, &k->q);
186     mpz_mod(t, t, &k->p);
187     if (mpz_cmp_ui(t, 1) != 0)
188         ugh = "qInv is not conguent ot (q^-1) mod p";
189
190     mpz_clear(t);
191     mpz_clear(u);
192     mpz_clear(q1);
193     return ugh;
194 }
195
196 const char *shared_secrets_file = SHARED_SECRETS_FILE;
197
198 struct id_list {
199     struct id id;
200     struct id_list *next;
201 };
202
203 struct secret {
204     struct id_list *ids;
205     enum PrivateKeyKind kind;
206     union {
207         chunk_t preshared_secret;
208         struct RSA_private_key RSA_private_key;
209     } u;
210     struct secret *next;
211 };
212
213 static struct pubkeyrec*
214 allocate_RSA_public_key(const x509cert_t *cert)
215 {
216     struct pubkeyrec *p = alloc_thing(struct pubkeyrec, "pubkeyrec");
217     chunk_t e = cert->publicExponent;
218     chunk_t n = cert->modulus;
219
220     /* eliminate leading zero byte in modulus from ASN.1 coding */
221     if (*n.ptr == 0x00)
222     {
223         n.ptr++;  n.len--;
224     }
225     n_to_mpz(&p->u.rsa.e, e.ptr, e.len);
226     n_to_mpz(&p->u.rsa.n, n.ptr, n.len);
227
228     /* form keyid */
229     p->u.rsa.keyid[0] = '\0';   /* in case of splitkeytoid failure */
230     splitkeytoid(e.ptr, e.len, n.ptr, n.len, p->u.rsa.keyid, sizeof(p->u.rsa.keyid));
231
232 #ifdef DEBUG
233     DBG(DBG_PRIVATE, RSA_show_public_key(&p->u.rsa));
234 #endif
235
236     p->u.rsa.k = mpz_sizeinbase(&p->u.rsa.n, 2);        /* size in bits, for a start */
237     p->u.rsa.k = (p->u.rsa.k + BITS_PER_BYTE - 1) / BITS_PER_BYTE;      /* now octets */
238
239     p->alg = PUBKEY_ALG_RSA;
240     p->id  = empty_id;
241
242     return p;
243 }
244
245 struct secret *secrets = NULL;
246
247 /* find the struct secret associated with the combination of
248  * me and the peer.  We match the Id (if none, the IP address).
249  * Failure is indicated by a NULL.
250  */
251 static const struct secret *
252 get_id_secret(enum PrivateKeyKind kind
253 , bool asym
254 , struct id *my_id
255 , struct id *his_id
256 , struct pubkeyrec *my_public_key)
257 {
258     enum {
259         match_default = 0x01,
260         match_him =     0x02,
261         match_me =      0x04,
262         match_pubkey =  0x08
263     };
264
265     unsigned int best_match = 0;
266     struct secret *best = NULL;
267     struct secret *s;
268
269     for (s = secrets; s != NULL; s = s->next)
270     {
271         if (s->kind == kind)
272         {
273             unsigned int match = 0;
274
275             if (s->ids == NULL)
276             {
277                 /* a default (signified by lack of ids):
278                  * accept if no more specific match found
279                  */
280                 match = match_default;
281             }
282             else
283             {
284                 /* check if both ends match ids */
285                 struct id_list *i;
286
287                 for (i = s->ids; i != NULL; i = i->next)
288                 {
289                     if (same_id(my_id, &i->id))
290                         match |= match_me;
291
292                     if (same_id(his_id, &i->id))
293                         match |= match_him;
294                 }
295
296                 /* If our end matched the only id in the list,
297                  * default to matching any peer.
298                  * A more specific match will trump this.
299                  */
300                 if (match == match_me
301                 && s->ids->next == NULL)
302                     match |= match_default;
303             }
304
305             if (my_public_key != NULL &&
306                 same_RSA_public_key(&s->u.RSA_private_key.pub, &my_public_key->u.rsa))
307             {
308                 match = match_pubkey;
309             }
310
311             if (match == match_pubkey)
312             {
313                 best = s;
314                 break; /* we have found the private key - no sense in searching further */
315             }
316
317             switch (match)
318             {
319             case match_me:
320                 /* if this is an asymmetric (eg. public key) system,
321                  * allow this-side-only match to count, even if
322                  * there are other ids in the list.
323                  */
324                 if(!asym)
325                     break;
326                 /* FALLTHROUGH */
327             case match_default: /* default all */
328             case match_me | match_default:      /* default peer */
329             case match_me | match_him:  /* explicit */
330                 if (match == best_match)
331                 {
332                     /* two good matches are equally good:
333                      * do they agree?
334                      */
335                     bool same;
336
337                     switch (kind)
338                     {
339                     case PPK_PSK:
340                         same = s->u.preshared_secret.len == best->u.preshared_secret.len
341                             && memcmp(s->u.preshared_secret.ptr, best->u.preshared_secret.ptr, s->u.preshared_secret.len) == 0;
342                         break;
343                     case PPK_RSA:
344                         /* Dirty trick: since we have code to compare
345                          * RSA public keys, but not private keys, we
346                          * make the assumption that equal public keys
347                          * mean equal private keys.  This ought to work.
348                          */
349                         same = same_RSA_public_key(&s->u.RSA_private_key.pub
350                             , &best->u.RSA_private_key.pub);
351                         break;
352                     default:
353                         impossible();
354                     }
355                     if (!same)
356                     {
357                         loglog(RC_LOG_SERIOUS, "multiple ipsec.secrets entries with distinct secrets match endpoints:"
358                             " first secret used");
359                         best = s;       /* list is backwards: take latest in list */
360                     }
361                 }
362                 else if (match > best_match)
363                 {
364                     /* this is the best match so far */
365                     best_match = match;
366                     best = s;
367                 }
368             }
369         }
370     }
371     return best;
372 }
373
374 static const struct secret *
375 get_secret(struct connection *c, enum PrivateKeyKind kind, bool asym)
376 {
377     const struct secret *best = NULL;
378     struct id *my_id = &c->this.id
379         , rw_id, my_rw_id
380         , *his_id = &c->that.id;
381
382     struct pubkeyrec *my_public_key = NULL;
383
384     /* is there a certificate assigned to this connection? */
385     if (kind == PPK_RSA && c->this.cert != NULL)
386     {
387         my_public_key = allocate_RSA_public_key(c->this.cert);
388     }
389
390     if (his_id_was_instantiated(c))
391     {
392         /* roadwarrior: replace him with ID_NONE */
393         rw_id.kind = ID_NONE;
394         his_id = &rw_id;
395     }
396 #ifdef NAT_TRAVERSAL
397     else if ((nat_traversal_enabled) && (c->policy & POLICY_PSK) &&
398         (kind == PPK_PSK) && (
399             ((c->kind == CK_TEMPLATE) && (c->that.id.kind == ID_NONE)) ||
400             ((c->kind == CK_INSTANCE) && (id_is_ipaddr(&c->that.id)))))
401     {
402         /* roadwarrior: replace him with ID_NONE */
403         rw_id.kind = ID_NONE;
404         his_id = &rw_id;
405     }
406 #endif
407
408     best = get_id_secret(kind, asym, my_id, his_id, my_public_key);
409     if (best == NULL) {
410         /* replace me with ID_NONE and try again */
411         my_rw_id.kind = ID_NONE;
412         my_id = &my_rw_id;
413         best = get_id_secret(kind, asym, my_id, his_id, my_public_key);
414     }
415
416     if (my_public_key != NULL)
417     {
418         free_public_key(my_public_key);
419     }
420     return best;
421 }
422
423 /* find the appropriate preshared key (see get_secret).
424  * Failure is indicated by a NULL pointer.
425  * Note: the result is not to be freed by the caller.
426  */
427 const chunk_t *
428 get_preshared_secret(struct connection *c)
429 {
430     const struct secret *s = get_secret(c, PPK_PSK, FALSE);
431
432 #ifdef DEBUG
433     DBG(DBG_PRIVATE,
434         if (s == NULL)
435             DBG_log("no Preshared Key Found");
436         else
437             DBG_dump_chunk("Preshared Key", s->u.preshared_secret);
438         );
439 #endif
440     return s == NULL? NULL : &s->u.preshared_secret;
441 }
442
443 /* find the appropriate RSA private key (see get_secret).
444  * Failure is indicated by a NULL pointer.
445  */
446 const struct RSA_private_key *
447 get_RSA_private_key(struct connection *c)
448 {
449     const struct secret *s = get_secret(c, PPK_RSA, TRUE);
450
451     return s == NULL? NULL : &s->u.RSA_private_key;
452 }
453
454 /* digest a secrets file
455  *
456  * The file is a sequence of records.  A record is a maximal sequence of
457  * tokens such that the first, and only the first, is in the first column
458  * of a line.
459  *
460  * Tokens are generally separated by whitespace and are key words, ids,
461  * strings, or data suitable for ttodata(3).  As a nod to convention,
462  * a trailing ":" on what would otherwise be a token is taken as a
463  * separate token.  If preceded by whitespace, a "#" is taken as starting
464  * a comment: it and the rest of the line are ignored.
465  *
466  * One kind of record is an include directive.  It starts with "include".
467  * The filename is the only other token in the record.
468  * If the filename does not start with /, it is taken to
469  * be relative to the directory containing the current file.
470  *
471  * The other kind of record describes a key.  It starts with a
472  * sequence of ids and ends with key information.  Each id
473  * is an IP address, a Fully Qualified Domain Name (which will immediately
474  * be resolved), or @FQDN which will be left as a name.
475  *
476  * The key part can be in several forms.
477  *
478  * The old form of the key is still supported: a simple
479  * quoted strings (with no escapes) is taken as a preshred key.
480  *
481  * The new form starts the key part with a ":".
482  *
483  * For Preshared Key, use the "PSK" keyword, and follow it by a string
484  * or a data token suitable for ttodata(3).
485  *
486  * For RSA Private Key, use the "RSA" keyword, followed by a
487  * brace-enclosed list of key field keywords and data values.
488  * The data values are large integers to be decoded by ttodata(3).
489  * The fields are a subset of those used by BIND 8.2 and have the
490  * same names.
491  */
492
493 struct secrets_file_position
494 {
495     int depth;  /* how deeply we are nested */
496     char *filename;
497     FILE *fp;
498     enum { B_none, B_record, B_file } bdry;     /* current boundary */
499     int lino;   /* line number in file */
500     char buffer[2049];    /* note: one extra char for our use (jamming '"') */
501     char *cur;  /* cursor */
502     char under; /* except in shift(): character orignally at *cur */
503     struct secrets_file_position *previous;
504 };
505
506 static struct secrets_file_position *sfp = NULL;
507
508 /* Token decoding: shift() loads the next token into tok.
509  * Iff a token starts at the left margin, it is considered
510  * to be the first in a record.  We create a special condition,
511  * Record Boundary (analogous to EOF), just before such a token.
512  * We are unwilling to shift through a record boundary:
513  * it must be overridden first.
514  * Returns FALSE iff Record Boundary or EOF (i.e. no token);
515  * tok will then be NULL.
516  */
517
518 static void process_secrets_file(const char *file_pat);
519
520 static char *tok;
521 #define tokeq(s) (streq(tok, (s)))
522 #define tokeqword(s) (strcasecmp(tok, (s)) == 0)
523
524 static bool
525 shift(void)
526 {
527     char *p = sfp->cur;
528     char *sor = NULL;   /* start of record for any new lines */
529
530     passert(sfp->bdry == B_none);
531
532     *p = sfp->under;
533     sfp->under = '\0';
534
535     for (;;)
536     {
537         switch (*p)
538         {
539         case '\0':      /* end of line */
540         case '#':       /* comment to end of line: treat as end of line */
541             /* get the next line */
542             if (fgets(sfp->buffer, sizeof(sfp->buffer)-1, sfp->fp) == NULL)
543             {
544                 sfp->bdry = B_file;
545                 tok = sfp->cur = NULL;
546                 return FALSE;
547             }
548             else
549             {
550                 /* strip trailing whitespace, including \n */
551
552                 for (p = sfp->buffer+strlen(sfp->buffer)-1; p>sfp->buffer; p--)
553                     if (!isspace(p[-1]))
554                         break;
555                 *p = '\0';
556
557                 sfp->lino++;
558                 sor = p = sfp->buffer;
559             }
560             break;      /* try again for a token */
561
562         case ' ':       /* whitespace */
563         case '\t':
564             p++;
565             break;      /* try again for a token */
566
567         case '"':       /* quoted token */
568         case '\'':
569             if (p != sor)
570             {
571                 /* we have a quoted token: note and advance to its end */
572                 tok = p;
573                 p = strchr(p+1, *p);
574                 if (p == NULL)
575                 {
576                     loglog(RC_LOG_SERIOUS, "\"%s\" line %d: unterminated string"
577                         , sfp->filename, sfp->lino);
578                     p = tok + strlen(tok);
579                 }
580                 else
581                 {
582                     p++;        /* include delimiter in token */
583                 }
584
585                 /* remember token delimiter and replace with '\0' */
586                 sfp->under = *p;
587                 *p = '\0';
588                 sfp->cur = p;
589                 return TRUE;
590             }
591             /* FALL THROUGH */
592         default:
593             if (p != sor)
594             {
595                 /* we seem to have a token: note and advance to its end */
596                 tok = p;
597
598                 if (p[0] == '0' && p[1] == 't')
599                 {
600                     /* 0t... token goes to end of line */
601                     p += strlen(p);
602                 }
603                 else
604                 {
605                     /* "ordinary" token: up to whitespace or end of line */
606                     do {
607                         p++;
608                     } while (*p != '\0' && !isspace(*p))
609                         ;
610
611                     /* fudge to separate ':' from a preceding adjacent token */
612                     if (p-1 > tok && p[-1] == ':')
613                         p--;
614                 }
615
616                 /* remember token delimiter and replace with '\0' */
617                 sfp->under = *p;
618                 *p = '\0';
619                 sfp->cur = p;
620                 return TRUE;
621             }
622
623             /* we have a start-of-record: return it, deferring "real" token */
624             sfp->bdry = B_record;
625             tok = NULL;
626             sfp->under = *p;
627             sfp->cur = p;
628             return FALSE;
629         }
630     }
631 }
632
633 /* ensures we are at a Record (or File) boundary, optionally warning if not */
634
635 static bool
636 flushline(const char *m)
637 {
638     if (sfp->bdry != B_none)
639     {
640         return TRUE;
641     }
642     else
643     {
644         if (m != NULL)
645             loglog(RC_LOG_SERIOUS, "\"%s\" line %d: %s", sfp->filename, sfp->lino, m);
646         do ; while (shift());
647         return FALSE;
648     }
649 }
650
651 /* parse PSK from file */
652 static err_t
653 process_psk_secret(chunk_t *psk)
654 {
655     err_t ugh = NULL;
656
657     if (*tok == '"' || *tok == '\'')
658     {
659         clonetochunk(*psk, tok+1, sfp->cur - tok  - 2, "PSK");
660         (void) shift();
661     }
662     else
663     {
664         char buf[2048]; /* limit on size of binary representation of key */
665         size_t sz;
666
667         ugh = ttodatav(tok, sfp->cur - tok, 0, buf, sizeof(buf), &sz
668             , diag_space, sizeof(diag_space));
669         if (ugh != NULL)
670         {
671             /* ttodata didn't like PSK data */
672             ugh = builddiag("PSK data malformed (%s): %s", ugh, tok);
673         }
674         else
675         {
676             clonetochunk(*psk, buf, sz, "PSK");
677             (void) shift();
678         }
679     }
680     return ugh;
681 }
682
683 /* Parse fields of RSA private key.
684  * A braced list of keyword and value pairs.
685  * At the moment, each field is required, in order.
686  * The fields come from BIND 8.2's representation
687  */
688 static err_t
689 process_rsa_secret(struct RSA_private_key *rsak)
690 {
691     char buf[2048];     /* limit on size of binary representation of key */
692     const struct fld *p;
693
694     /* save bytes of Modulus and PublicExponent for keyid calculation */
695     unsigned char ebytes[sizeof(buf)];
696     unsigned char *eb_next = ebytes;
697     chunk_t pub_bytes[2];
698     chunk_t *pb_next = &pub_bytes[0];
699
700     for (p = RSA_private_field; p < &RSA_private_field[elemsof(RSA_private_field)]; p++)
701     {
702         size_t sz;
703         err_t ugh;
704
705         if (!shift())
706         {
707             return "premature end of RSA key";
708         }
709         else if (!tokeqword(p->name))
710         {
711             return builddiag("%s keyword not found where expected in RSA key"
712                 , p->name);
713         }
714         else if (!(shift()
715         && (!tokeq(":") || shift())))   /* ignore optional ":" */
716         {
717             return "premature end of RSA key";
718         }
719         else if (NULL != (ugh = ttodatav(tok, sfp->cur - tok
720         , 0, buf, sizeof(buf), &sz, diag_space, sizeof(diag_space))))
721         {
722             /* in RSA key, ttodata didn't like */
723             return builddiag("RSA data malformed (%s): %s", ugh, tok);
724         }
725         else
726         {
727             MP_INT *n = (MP_INT *) ((char *)rsak + p->offset);
728
729             n_to_mpz(n, buf, sz);
730             if (pb_next < &pub_bytes[elemsof(pub_bytes)])
731             {
732                 if (eb_next - ebytes + sz > sizeof(ebytes))
733                     return "public key takes too many bytes";
734
735                 setchunk(*pb_next, eb_next, sz);
736                 memcpy(eb_next, buf, sz);
737                 eb_next += sz;
738                 pb_next++;
739             }
740 #if 0   /* debugging info that compromises security */
741             {
742                 size_t sz = mpz_sizeinbase(n, 16);
743                 char buf[2048/4 + 2];   /* ought to be big enough */
744
745                 passert(sz <= sizeof(buf));
746                 mpz_get_str(buf, 16, n);
747
748                 loglog(RC_LOG_SERIOUS, "%s: %s", p->name, buf);
749             }
750 #endif
751         }
752     }
753
754     /* We require an (indented) '}' and the end of the record.
755      * We break down the test so that the diagnostic will be
756      * more helpful.  Some people don't seem to wish to indent
757      * the brace!
758      */
759     if (!shift() || !tokeq("}"))
760     {
761         return "malformed end of RSA private key -- indented '}' required";
762     }
763     else if (shift())
764     {
765         return "malformed end of RSA private key -- unexpected token after '}'";
766     }
767     else
768     {
769         unsigned bits = mpz_sizeinbase(&rsak->pub.n, 2);
770
771         rsak->pub.k = (bits + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
772         rsak->pub.keyid[0] = '\0';      /* in case of splitkeytoid failure */
773         splitkeytoid(pub_bytes[1].ptr, pub_bytes[1].len
774             , pub_bytes[0].ptr, pub_bytes[0].len
775             , rsak->pub.keyid, sizeof(rsak->pub.keyid));
776         return RSA_private_key_sanity(rsak);
777     }
778 }
779
780 static void
781 process_secret(struct secret *s)
782 {
783     err_t ugh = NULL;
784
785     s->kind = PPK_PSK;  /* default */
786     if (*tok == '"' || *tok == '\'')
787     {
788         /* old PSK format: just a string */
789         ugh = process_psk_secret(&s->u.preshared_secret);
790     }
791     else if (tokeqword("psk"))
792     {
793         /* preshared key: quoted string or ttodata format */
794         ugh = !shift()? "unexpected end of record in PSK"
795             : process_psk_secret(&s->u.preshared_secret);
796     }
797     else if (tokeqword("rsa"))
798     {
799         /* RSA key: the fun begins.
800          * A braced list of keyword and value pairs.
801          */
802         s->kind = PPK_RSA;
803         if (!shift())
804         {
805             ugh = "bad RSA key syntax";
806         }
807         else if (tokeq("{"))
808         {
809             ugh = process_rsa_secret(&s->u.RSA_private_key);
810         }
811         else
812         {
813             /* we expect the filename of a PKCS#1 private key file */
814             char filename[BUF_LEN];
815             char passphrase[BUF_LEN];
816             pkcs1privkey_t *key = NULL;
817
818             memset(filename,   '\0', BUF_LEN);
819             memset(passphrase, '\0', BUF_LEN);
820
821             if (*tok == '"' || *tok == '\'')  /* quoted filename */
822                 memcpy(filename, tok+1, sfp->cur - tok - 2);
823             else
824                 memcpy(filename, tok, sfp->cur - tok);
825
826             if (shift())
827             {
828                 /* we expect an appended passphrase */
829                 if (*tok == '"' || *tok == '\'') /* quoted passphrase */
830                    memcpy(passphrase, tok+1, sfp->cur - tok - 2);
831                 else
832                    memcpy(passphrase, tok, sfp->cur - tok);
833
834                 if (shift())
835                 {
836                     ugh = "RSA private key file -- unexpected token after passphrase";
837                 }
838             }
839
840             key = load_pkcs1_private_key(filename, passphrase);
841
842             if (key == NULL)
843                 ugh = "error loading RSA private key file";
844             else
845             {
846                 u_int i;
847
848                 for (i = 0; ugh == NULL && i < elemsof(RSA_private_field); i++)
849                 {
850                     MP_INT *n = (MP_INT *) ((char *)&s->u.RSA_private_key +
851                                                           RSA_private_field[i].offset);
852                     n_to_mpz(n, key->field[i].ptr, key->field[i].len);
853                 }
854                 {
855                     unsigned bits = mpz_sizeinbase(&s->u.RSA_private_key.pub.n, 2);
856                     chunk_t n = key->field[0]; /* public modulus n */
857                     chunk_t e = key->field[1]; /* public exponent e */
858
859                     /* eliminate leading zero byte in modulus from ASN.1 coding */
860                     if (*n.ptr == 0x00)
861                     {
862                         n.ptr++;  n.len--;
863                     }
864
865                     s->u.RSA_private_key.pub.k = (bits + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
866
867                     /* compute keyid */
868                     s->u.RSA_private_key.pub.keyid[0] = '\0';   /* in case of splitkeytoid failure */
869                     splitkeytoid(e.ptr, e.len, n.ptr, n.len
870                         , s->u.RSA_private_key.pub.keyid, sizeof(s->u.RSA_private_key.pub.keyid));
871
872                     ugh = RSA_private_key_sanity(&s->u.RSA_private_key);
873                 }
874                 pfree(key->pkcs1object.ptr);
875                 pfree(key);
876             }
877         }
878     }
879     else
880     {
881         ugh = builddiag("unrecognized key format: %s", tok);
882     }
883
884     if (ugh != NULL)
885     {
886         loglog(RC_LOG_SERIOUS, "\"%s\" line %d: %s"
887             , sfp->filename, sfp->lino, ugh);
888     }
889     else if (flushline("expected record boundary in key"))
890     {
891         /* gauntlet has been run: install new secret */
892         s->next = secrets;
893         secrets = s;
894     }
895 }
896
897 static void
898 process_secret_records(void)
899 {
900     struct hostent *h;
901     const char *cp;
902     size_t n;
903     char **h_addr_list_element;
904     int h_addr_list_size = 0;
905     size_t strlength = 0;
906     int count;
907     int multiple_ips = FALSE;
908     /* read records from ipsec.secrets and load them into our table */
909     for (;;)
910     {
911         (void)flushline(NULL);  /* silently ditch leftovers, if any */
912         if (sfp->bdry == B_file)
913             break;
914
915         sfp->bdry = B_none;     /* eat the Record Boundary */
916         (void)shift();  /* get real first token */
917
918         if (tokeqword("include"))
919         {
920             /* an include directive */
921             char fn[2048];      /* space for filename (I hope) */
922             char *p = fn;
923             char *end_prefix = strrchr(sfp->filename, '/');
924
925             if (!shift())
926             {
927                 loglog(RC_LOG_SERIOUS, "\"%s\" line %d: unexpected end of include directive"
928                     , sfp->filename, sfp->lino);
929                 continue;   /* abandon this record */
930             }
931
932             /* if path is relative and including file's pathname has
933              * a non-empty dirname, prefix this path with that dirname.
934              */
935             if (tok[0] != '/' && end_prefix != NULL)
936             {
937                 size_t pl = end_prefix - sfp->filename + 1;
938
939                 /* "clamp" length to prevent problems now;
940                  * will be rediscovered and reported later.
941                  */
942                 if (pl > sizeof(fn))
943                     pl = sizeof(fn);
944                 memcpy(fn, sfp->filename, pl);
945                 p += pl;
946             }
947             if (sfp->cur - tok >= &fn[sizeof(fn)] - p)
948             {
949                 loglog(RC_LOG_SERIOUS, "\"%s\" line %d: include pathname too long"
950                     , sfp->filename, sfp->lino);
951                 continue;   /* abandon this record */
952             }
953             strcpy(p, tok);
954             (void) shift();     /* move to Record Boundary, we hope */
955             if (flushline("ignoring malformed INCLUDE -- expected Record Boundary after filename"))
956             {
957                 process_secrets_file(fn);
958                 tok = NULL;     /* correct, but probably redundant */
959             }
960         }
961         else
962         {
963             /* expecting a list of indices and then the key info */
964             struct secret *s = alloc_thing(struct secret, "secret");
965
966             s->ids = NULL;
967             s->kind = PPK_PSK;  /* default */
968             setchunk(s->u.preshared_secret, NULL, 0);
969             s->next = NULL;
970
971             for (;;)
972             {
973                 if (tok[0] == '"' || tok[0] == '\'')
974                 {
975                     /* found key part */
976                     process_secret(s);
977                     break;
978                 }
979                 else if (tokeq(":"))
980                 {
981                     /* found key part */
982                     shift();    /* discard explicit separator */
983                     process_secret(s);
984                     break;
985                 }
986                 else
987                 {
988                     /* an id
989                      * See RFC2407 IPsec Domain of Interpretation 4.6.2
990                      */
991                     struct id id;
992                     err_t ugh;
993
994                     if (tokeq("%any"))
995                     {
996                         id = empty_id;
997                         id.kind = ID_IPV4_ADDR;
998                         ugh = anyaddr(AF_INET, &id.ip_addr);
999                     }
1000                     else if (tokeq("%any6"))
1001                     {
1002                         id = empty_id;
1003                         id.kind = ID_IPV6_ADDR;
1004                         ugh = anyaddr(AF_INET6, &id.ip_addr);
1005                     }
1006                     else
1007                     {
1008                         ugh = atoid(tok, &id);  
1009                     }
1010                     if (ugh != NULL)
1011                     {
1012                         loglog(RC_LOG_SERIOUS, "\"%s\" line %d: %s \"%s\""
1013                             , sfp->filename, sfp->lino, ugh, tok);
1014                     }
1015                     else
1016                     {
1017                         struct id_list *i = alloc_thing(struct id_list
1018                             , "id_list");
1019
1020                         i->id = id;
1021                         unshare_id_content(&i->id);
1022                         i->next = s->ids;
1023                         s->ids = i;
1024                         /* DBG_log("id type %d: %s %.*s", i->kind, ip_str(&i->ip_addr), (int)i->name.len, i->name.ptr); */
1025                     }
1026                     if (!shift())
1027                     {
1028                         /* unexpected Record Boundary or EOF */
1029                         loglog(RC_LOG_SERIOUS, "\"%s\" line %d: unexpected end of id list"
1030                             , sfp->filename, sfp->lino);
1031                         break;
1032                     }
1033                 }
1034             }
1035         }
1036     }
1037 }
1038
1039 static int
1040 globugh(const char *epath, int eerrno)
1041 {
1042     log_errno_routine(eerrno, "problem with secrets file \"%s\"", epath);
1043     return 1;   /* stop glob */
1044 }
1045
1046 static void
1047 process_secrets_file(const char *file_pat)
1048 {
1049     struct secrets_file_position pos;
1050     char **fnp;
1051     glob_t globbuf;
1052
1053     pos.depth = sfp == NULL? 0 : sfp->depth + 1;
1054
1055     if (pos.depth > 10)
1056     {
1057         loglog(RC_LOG_SERIOUS, "preshared secrets file \"%s\" nested too deeply", file_pat);
1058         return;
1059     }
1060
1061     /* do globbing */
1062     {
1063         int r = glob(file_pat, GLOB_ERR, globugh, &globbuf);
1064
1065         if (r != 0)
1066         {
1067             switch (r)
1068             {
1069             case GLOB_NOSPACE:
1070                 loglog(RC_LOG_SERIOUS, "out of space processing secrets filename \"%s\"", file_pat);
1071                 break;
1072             case GLOB_ABORTED:
1073                 break;  /* already logged */
1074             case GLOB_NOMATCH:
1075                 loglog(RC_LOG_SERIOUS, "no secrets filename matched \"%s\"", file_pat);
1076                 break;
1077             default:
1078                 loglog(RC_LOG_SERIOUS, "unknown glob error %d", r);
1079                 break;
1080             }
1081             globfree(&globbuf);
1082             return;
1083         }
1084     }
1085
1086     pos.previous = sfp;
1087     sfp = &pos;
1088
1089     /* for each file... */
1090     for (fnp = globbuf.gl_pathv; *fnp != NULL; fnp++)
1091     {
1092 #if defined(CONFIG_SNAPGEAR) || defined(CONFIG_SECUREEDGE)
1093         char *cmd;
1094         int len;
1095
1096         len = strlen(*fnp) + sizeof("ipsec showfile %s");
1097         cmd = (char *) malloc(len);
1098         if (!cmd) {
1099             log_errno((e, "fail to allocate memory"));
1100             continue;   /* try the next one */
1101         }
1102         snprintf(cmd, len, "ipsec showfile %s", *fnp);
1103         pos.filename = *fnp;
1104         pos.fp = popen(cmd, "r");
1105         if (pos.fp == NULL)
1106         {
1107             log_errno((e, "could not open \"%s\"", cmd));
1108                 free(cmd);
1109             continue;   /* try the next one */
1110         }
1111         free(cmd);
1112 #else
1113         pos.filename = *fnp;
1114         pos.fp = fopen(pos.filename, "r");
1115         if (pos.fp == NULL)
1116         {
1117             log_errno((e, "could not open \"%s\"", pos.filename));
1118             continue;   /* try the next one */
1119         }
1120 #endif
1121
1122         log("loading secrets from \"%s\"", pos.filename);
1123
1124         pos.lino = 0;
1125         pos.bdry = B_none;
1126
1127         pos.cur = pos.buffer;   /* nothing loaded yet */
1128         pos.under = *pos.cur = '\0';
1129
1130         (void) shift(); /* prime tok */
1131         (void) flushline("file starts with indentation (continuation notation)");
1132         process_secret_records();
1133 #if defined(CONFIG_SNAPGEAR) || defined(CONFIG_SECUREEDGE)
1134         pclose(pos.fp);
1135 #else
1136         fclose(pos.fp);
1137 #endif
1138     }
1139
1140     sfp = pos.previous; /* restore old state */
1141 }
1142
1143 void
1144 free_preshared_secrets(void)
1145 {
1146     if (secrets != NULL)
1147     {
1148         struct secret *s, *ns;
1149
1150         log("forgetting secrets");
1151
1152         for (s = secrets; s != NULL; s = ns)
1153         {
1154             struct id_list *i, *ni;
1155
1156             ns = s->next;       /* grab before freeing s */
1157             for (i = s->ids; i != NULL; i = ni)
1158             {
1159                 ni = i->next;   /* grab before freeing i */
1160                 free_id_content(&i->id);
1161                 pfree(i);
1162             }
1163             switch (s->kind)
1164             {
1165             case PPK_PSK:
1166                 pfree(s->u.preshared_secret.ptr);
1167                 break;
1168             case PPK_RSA:
1169                 free_RSA_public_content(&s->u.RSA_private_key.pub);
1170                 mpz_clear(&s->u.RSA_private_key.d);
1171                 mpz_clear(&s->u.RSA_private_key.p);
1172                 mpz_clear(&s->u.RSA_private_key.q);
1173                 mpz_clear(&s->u.RSA_private_key.dP);
1174                 mpz_clear(&s->u.RSA_private_key.dQ);
1175                 mpz_clear(&s->u.RSA_private_key.qInv);
1176                 break;
1177             default:
1178                 impossible();
1179             }
1180             pfree(s);
1181         }
1182         secrets = NULL;
1183     }
1184 }
1185
1186 void
1187 load_preshared_secrets(void)
1188 {
1189     free_preshared_secrets();
1190     (void) process_secrets_file(shared_secrets_file);
1191 }
1192
1193 /* public key machinery */
1194
1195 struct pubkeyrec *
1196 public_key_from_rsa(const struct RSA_public_key *k)
1197 {
1198     struct pubkeyrec *p = alloc_thing(struct pubkeyrec, "pubkeyrec");
1199
1200     p->id = empty_id;   /* don't know, doesn't matter */
1201
1202     p->alg = PUBKEY_ALG_RSA;
1203
1204     p->u.rsa.k = k->k;
1205     mpz_init_set(&p->u.rsa.e, &k->e);
1206     mpz_init_set(&p->u.rsa.n, &k->n);
1207
1208     p->next = NULL;
1209     return p;
1210 }
1211
1212 void free_RSA_public_content(struct RSA_public_key *rsa)
1213 {
1214     mpz_clear(&rsa->n);
1215     mpz_clear(&rsa->e);
1216 }
1217
1218 /* Free a public key record.
1219  * As a convenience, this returns a pointer to next.
1220  */
1221 struct pubkeyrec *
1222 free_public_key(struct pubkeyrec *p)
1223 {
1224     struct pubkeyrec *nxt = p->next;
1225
1226     free_id_content(&p->id);
1227
1228     /* algorithm-specific freeing */
1229     switch (p->alg)
1230     {
1231     case PUBKEY_ALG_RSA:
1232         free_RSA_public_content(&p->u.rsa);
1233         break;
1234     default:
1235         impossible();
1236     }
1237
1238     pfree(p);
1239     return nxt;
1240 }
1241
1242 void
1243 free_public_keys(struct pubkeyrec **keys)
1244 {
1245     while (*keys != NULL)
1246         *keys = free_public_key(*keys);
1247 }
1248
1249 /* root of chained public key list */
1250
1251 struct pubkeyrec *pubkeys = NULL;       /* keys from ipsec.conf */
1252
1253 void
1254 free_remembered_public_keys(void)
1255 {
1256     free_public_keys(&pubkeys);
1257 }
1258
1259 /* transfer public keys from *keys list to front of pubkeys list */
1260 void
1261 remember_public_keys(struct pubkeyrec **keys)
1262 {
1263     struct pubkeyrec **pp = keys;
1264
1265     while (*pp != NULL)
1266         pp = &(*pp)->next;
1267     *pp = pubkeys;
1268     pubkeys = *keys;
1269     *keys = NULL;
1270 }
1271
1272 /* decode of RSA pubkey chunk
1273  * - format specified in RFC 2537 RSA/MD5 Keys and SIGs in the DNS
1274  * - exponent length in bytes (1 or 3 octets)
1275  *   + 1 byte if in [1, 255]
1276  *   + otherwise 0x00 followed by 2 bytes of length
1277  * - exponent
1278  * - modulus
1279  */
1280 err_t
1281 unpack_RSA_public_key(struct RSA_public_key *rsa, chunk_t *pubkey)
1282 {
1283     chunk_t exp;
1284     chunk_t mod;
1285
1286     rsa->keyid[0] = '\0';       /* in case of keybolbtoid failure */
1287
1288     if (pubkey->len < 3)
1289         return "RSA public key blob way to short";      /* not even room for length! */
1290
1291     if (pubkey->ptr[0] != 0x00)
1292     {
1293         setchunk(exp, pubkey->ptr + 1, pubkey->ptr[0]);
1294     }
1295     else
1296     {
1297         setchunk(exp, pubkey->ptr + 3
1298             , (pubkey->ptr[1] << BITS_PER_BYTE) + pubkey->ptr[2]);
1299     }
1300
1301     if (pubkey->len - (exp.ptr - pubkey->ptr) < exp.len + RSA_MIN_OCTETS_RFC)
1302         return "RSA public key blob too short";
1303
1304     mod.ptr = exp.ptr + exp.len;
1305     mod.len = &pubkey->ptr[pubkey->len] - mod.ptr;
1306
1307     if (mod.len < RSA_MIN_OCTETS)
1308         return RSA_MIN_OCTETS_UGH;
1309
1310     if (mod.len > RSA_MAX_OCTETS)
1311         return RSA_MAX_OCTETS_UGH;
1312
1313     n_to_mpz(&rsa->e, exp.ptr, exp.len);
1314     n_to_mpz(&rsa->n, mod.ptr, mod.len);
1315
1316     keyblobtoid(pubkey->ptr, pubkey->len, rsa->keyid, sizeof(rsa->keyid));
1317
1318 #ifdef DEBUG
1319     DBG(DBG_PRIVATE, RSA_show_public_key(rsa));
1320 #endif
1321
1322
1323     rsa->k = mpz_sizeinbase(&rsa->n, 2);        /* size in bits, for a start */
1324     rsa->k = (rsa->k + BITS_PER_BYTE - 1) / BITS_PER_BYTE;      /* now octets */
1325
1326     if (rsa->k != mod.len)
1327     {
1328         mpz_clear(&rsa->e);
1329         mpz_clear(&rsa->n);
1330         return "RSA modulus shorter than specified";
1331     }
1332
1333     return NULL;
1334 }
1335
1336 bool
1337 same_RSA_public_key(const struct RSA_public_key *a
1338     , const struct RSA_public_key *b)
1339 {
1340     return a == b
1341     || (a->k == b->k && mpz_cmp(&a->n, &b->n) == 0 && mpz_cmp(&a->e, &b->e) == 0);
1342 }
1343
1344
1345 static void
1346 install_public_key(struct pubkeyrec *p, struct pubkeyrec **head)
1347 {
1348     unshare_id_content(&p->id);
1349
1350     /* store the time the public key was installed */
1351     time(&p->installed);
1352
1353     /* install new key at front */
1354     p->next = *head;
1355     *head = p;
1356 }
1357
1358
1359 void
1360 delete_public_keys(const struct id *id, enum pubkey_alg alg)
1361 {
1362     struct pubkeyrec **pp, *p;
1363
1364     for (pp = &pubkeys; (p = *pp) != NULL; )
1365     {
1366         if (same_id(id, &p->id) && p->alg == alg)
1367             *pp = free_public_key(p);
1368         else
1369             pp = &p->next;
1370     }
1371 }
1372
1373 err_t
1374 add_public_key(const struct id *id
1375 , enum dns_auth_level dns_auth_level
1376 , enum pubkey_alg alg
1377 , chunk_t *key
1378 , struct pubkeyrec **head)
1379 {
1380     struct pubkeyrec *p = alloc_thing(struct pubkeyrec, "pubkeyrec");
1381
1382     /* first: algorithm-specific decoding of key chunk */
1383     switch (alg)
1384     {
1385     case PUBKEY_ALG_RSA:
1386         {
1387             err_t ugh = unpack_RSA_public_key(&p->u.rsa, key);
1388
1389             if (ugh != NULL)
1390             {
1391                 pfree(p);
1392                 return ugh;
1393             }
1394         }
1395         break;
1396     default:
1397         impossible();
1398     }
1399
1400     p->id = *id;
1401     p->dns_auth_level = dns_auth_level;
1402     p->alg = alg;
1403     p->until = UNDEFINED_TIME;
1404     install_public_key(p, head);
1405     return NULL;
1406 }
1407
1408 /*  extract id and public key from x.509 certificate and insert it
1409  *  into a pubkeyrec
1410  */
1411 void
1412 add_x509_public_key(const x509cert_t *cert , enum dns_auth_level dns_auth_level)
1413 {
1414     generalName_t *gn;
1415     struct pubkeyrec *p;
1416
1417     /* we support RSA only */
1418     if (cert->subjectPublicKeyAlgorithm != PUBKEY_ALG_RSA) return;
1419
1420     /* ID type: ID_DER_ASN1_DN  (X.509 subject field) */
1421     p = allocate_RSA_public_key(cert);
1422     p->id.kind = ID_DER_ASN1_DN;
1423     p->id.name = cert->subject;
1424     p->dns_auth_level = dns_auth_level;
1425     p->until = cert->notAfter;
1426     delete_public_keys(&p->id, p->alg);
1427     install_public_key(p, &pubkeys);
1428
1429     gn = cert->subjectAltName;
1430
1431     while (gn != NULL) /* insert all subjectAltNames */
1432     {
1433         struct id id = empty_id;
1434
1435         gntoid(&id, gn);
1436         if (id.kind != ID_NONE)
1437         {
1438             p = allocate_RSA_public_key(cert);
1439             p->id = id;
1440             p->dns_auth_level = dns_auth_level;
1441             p->until = cert->notAfter;
1442             delete_public_keys(&p->id, p->alg);
1443             install_public_key(p, &pubkeys);
1444         }
1445         gn = gn->next;
1446     }
1447 }
1448
1449 /*  when a X.509 certificate gets revoked, all instances of
1450  *  the corresponding public key must be removed
1451  */
1452 void
1453 remove_x509_public_key(const x509cert_t *cert)
1454 {
1455     struct pubkeyrec *p, **pp, *revoked_p;
1456
1457     revoked_p = allocate_RSA_public_key(cert);
1458     p         = pubkeys;
1459     pp        = &pubkeys;
1460
1461     while(p != NULL)
1462    {
1463         if (same_RSA_public_key(&p->u.rsa, &revoked_p->u.rsa))
1464         {
1465             /* remove p from list and free memory */
1466             *pp = free_public_key(p);
1467             loglog(RC_LOG_SERIOUS,
1468                 "revoked RSA public key deleted");
1469         }
1470         else
1471         {
1472             pp = &p->next;
1473         }
1474         p =*pp;
1475     }
1476     free_public_key(revoked_p);
1477 }
1478
1479 /*
1480  *  list all public keys in the chained list
1481  */
1482 void list_public_keys(bool utc)
1483 {
1484     struct pubkeyrec *p = pubkeys;
1485
1486     whack_log(RC_COMMENT, " ");
1487     whack_log(RC_COMMENT, "List of Public Keys:");
1488     whack_log(RC_COMMENT, " ");
1489
1490     while (p != NULL)
1491     {
1492         if (p->alg == PUBKEY_ALG_RSA)
1493         {
1494             char id_buf[IDTOA_BUF];
1495             char expires_buf[TIMETOA_BUF];
1496
1497             idtoa(&p->id, id_buf, IDTOA_BUF);
1498             strcpy(expires_buf, timetoa(&p->until, utc));
1499
1500             whack_log(RC_COMMENT, "%s, %4d RSA Key %s, until %s %s",
1501                 timetoa(&p->installed, utc), 8*p->u.rsa.k, p->u.rsa.keyid,
1502                 expires_buf,
1503                 check_expiry(p->until, PUBKEY_WARNING_INTERVAL, TRUE));
1504             whack_log(RC_COMMENT,"       %s '%s'",
1505                 enum_show(&ident_names, p->id.kind), id_buf);
1506         }
1507         p = p->next;
1508     }
1509 }