]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - pageant.c
Remove the list of key algorithms in pageant.c.
[PuTTY.git] / pageant.c
1 /*
2  * pageant.c: cross-platform code to implement Pageant.
3  */
4
5 #include <stddef.h>
6 #include <stdlib.h>
7 #include <assert.h>
8
9 #include "putty.h"
10 #include "ssh.h"
11 #include "pageant.h"
12
13 /*
14  * We need this to link with the RSA code, because rsaencrypt()
15  * pads its data with random bytes. Since we only use rsadecrypt()
16  * and the signing functions, which are deterministic, this should
17  * never be called.
18  *
19  * If it _is_ called, there is a _serious_ problem, because it
20  * won't generate true random numbers. So we must scream, panic,
21  * and exit immediately if that should happen.
22  */
23 int random_byte(void)
24 {
25     modalfatalbox("Internal error: attempt to use random numbers in Pageant");
26     exit(0);
27     return 0;                 /* unreachable, but placate optimiser */
28 }
29
30 /*
31  * rsakeys stores SSH-1 RSA keys. ssh2keys stores all SSH-2 keys.
32  */
33 static tree234 *rsakeys, *ssh2keys;
34
35 /*
36  * Blob structure for passing to the asymmetric SSH-2 key compare
37  * function, prototyped here.
38  */
39 struct blob {
40     const unsigned char *blob;
41     int len;
42 };
43 static int cmpkeys_ssh2_asymm(void *av, void *bv);
44
45 /*
46  * Key comparison function for the 2-3-4 tree of RSA keys.
47  */
48 static int cmpkeys_rsa(void *av, void *bv)
49 {
50     struct RSAKey *a = (struct RSAKey *) av;
51     struct RSAKey *b = (struct RSAKey *) bv;
52     Bignum am, bm;
53     int alen, blen;
54
55     am = a->modulus;
56     bm = b->modulus;
57     /*
58      * Compare by length of moduli.
59      */
60     alen = bignum_bitcount(am);
61     blen = bignum_bitcount(bm);
62     if (alen > blen)
63         return +1;
64     else if (alen < blen)
65         return -1;
66     /*
67      * Now compare by moduli themselves.
68      */
69     alen = (alen + 7) / 8;             /* byte count */
70     while (alen-- > 0) {
71         int abyte, bbyte;
72         abyte = bignum_byte(am, alen);
73         bbyte = bignum_byte(bm, alen);
74         if (abyte > bbyte)
75             return +1;
76         else if (abyte < bbyte)
77             return -1;
78     }
79     /*
80      * Give up.
81      */
82     return 0;
83 }
84
85 /*
86  * Key comparison function for the 2-3-4 tree of SSH-2 keys.
87  */
88 static int cmpkeys_ssh2(void *av, void *bv)
89 {
90     struct ssh2_userkey *a = (struct ssh2_userkey *) av;
91     struct ssh2_userkey *b = (struct ssh2_userkey *) bv;
92     int i;
93     int alen, blen;
94     unsigned char *ablob, *bblob;
95     int c;
96
97     /*
98      * Compare purely by public blob.
99      */
100     ablob = a->alg->public_blob(a->data, &alen);
101     bblob = b->alg->public_blob(b->data, &blen);
102
103     c = 0;
104     for (i = 0; i < alen && i < blen; i++) {
105         if (ablob[i] < bblob[i]) {
106             c = -1;
107             break;
108         } else if (ablob[i] > bblob[i]) {
109             c = +1;
110             break;
111         }
112     }
113     if (c == 0 && i < alen)
114         c = +1;                        /* a is longer */
115     if (c == 0 && i < blen)
116         c = -1;                        /* a is longer */
117
118     sfree(ablob);
119     sfree(bblob);
120
121     return c;
122 }
123
124 /*
125  * Key comparison function for looking up a blob in the 2-3-4 tree
126  * of SSH-2 keys.
127  */
128 static int cmpkeys_ssh2_asymm(void *av, void *bv)
129 {
130     struct blob *a = (struct blob *) av;
131     struct ssh2_userkey *b = (struct ssh2_userkey *) bv;
132     int i;
133     int alen, blen;
134     const unsigned char *ablob;
135     unsigned char *bblob;
136     int c;
137
138     /*
139      * Compare purely by public blob.
140      */
141     ablob = a->blob;
142     alen = a->len;
143     bblob = b->alg->public_blob(b->data, &blen);
144
145     c = 0;
146     for (i = 0; i < alen && i < blen; i++) {
147         if (ablob[i] < bblob[i]) {
148             c = -1;
149             break;
150         } else if (ablob[i] > bblob[i]) {
151             c = +1;
152             break;
153         }
154     }
155     if (c == 0 && i < alen)
156         c = +1;                        /* a is longer */
157     if (c == 0 && i < blen)
158         c = -1;                        /* a is longer */
159
160     sfree(bblob);
161
162     return c;
163 }
164
165 /*
166  * Create an SSH-1 key list in a malloc'ed buffer; return its
167  * length.
168  */
169 void *pageant_make_keylist1(int *length)
170 {
171     int i, nkeys, len;
172     struct RSAKey *key;
173     unsigned char *blob, *p, *ret;
174     int bloblen;
175
176     /*
177      * Count up the number and length of keys we hold.
178      */
179     len = 4;
180     nkeys = 0;
181     for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
182         nkeys++;
183         blob = rsa_public_blob(key, &bloblen);
184         len += bloblen;
185         sfree(blob);
186         len += 4 + strlen(key->comment);
187     }
188
189     /* Allocate the buffer. */
190     p = ret = snewn(len, unsigned char);
191     if (length) *length = len;
192
193     PUT_32BIT(p, nkeys);
194     p += 4;
195     for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
196         blob = rsa_public_blob(key, &bloblen);
197         memcpy(p, blob, bloblen);
198         p += bloblen;
199         sfree(blob);
200         PUT_32BIT(p, strlen(key->comment));
201         memcpy(p + 4, key->comment, strlen(key->comment));
202         p += 4 + strlen(key->comment);
203     }
204
205     assert(p - ret == len);
206     return ret;
207 }
208
209 /*
210  * Create an SSH-2 key list in a malloc'ed buffer; return its
211  * length.
212  */
213 void *pageant_make_keylist2(int *length)
214 {
215     struct ssh2_userkey *key;
216     int i, len, nkeys;
217     unsigned char *blob, *p, *ret;
218     int bloblen;
219
220     /*
221      * Count up the number and length of keys we hold.
222      */
223     len = 4;
224     nkeys = 0;
225     for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
226         nkeys++;
227         len += 4;              /* length field */
228         blob = key->alg->public_blob(key->data, &bloblen);
229         len += bloblen;
230         sfree(blob);
231         len += 4 + strlen(key->comment);
232     }
233
234     /* Allocate the buffer. */
235     p = ret = snewn(len, unsigned char);
236     if (length) *length = len;
237
238     /*
239      * Packet header is the obvious five bytes, plus four
240      * bytes for the key count.
241      */
242     PUT_32BIT(p, nkeys);
243     p += 4;
244     for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
245         blob = key->alg->public_blob(key->data, &bloblen);
246         PUT_32BIT(p, bloblen);
247         p += 4;
248         memcpy(p, blob, bloblen);
249         p += bloblen;
250         sfree(blob);
251         PUT_32BIT(p, strlen(key->comment));
252         memcpy(p + 4, key->comment, strlen(key->comment));
253         p += 4 + strlen(key->comment);
254     }
255
256     assert(p - ret == len);
257     return ret;
258 }
259
260 char *fingerprint_ssh2_blob(const void *blob, int bloblen)
261 {
262     unsigned char digest[16];
263     char fingerprint_str[16*3];
264     unsigned stringlen;
265     int i;
266
267     MD5Simple(blob, bloblen, digest);
268     for (i = 0; i < 16; i++)
269         sprintf(fingerprint_str + i*3, "%02x%s", digest[i], i==15 ? "" : ":");
270
271     stringlen = GET_32BIT((const unsigned char *)blob);
272     if (stringlen < bloblen-4)
273         return dupprintf("%.*s %s", (int)stringlen, (const char *)blob + 4,
274                          fingerprint_str);
275     else
276         return dupstr(fingerprint_str);        
277 }
278
279 static void plog(void *logctx, pageant_logfn_t logfn, const char *fmt, ...)
280 #ifdef __GNUC__
281 __attribute__ ((format (printf, 3, 4)))
282 #endif
283     ;
284
285 static void plog(void *logctx, pageant_logfn_t logfn, const char *fmt, ...)
286 {
287     /*
288      * This is the wrapper that takes a variadic argument list and
289      * turns it into the va_list that the log function really expects.
290      * It's safe to call this with logfn==NULL, because we
291      * double-check that below; but if you're going to do lots of work
292      * before getting here (such as looping, or hashing things) then
293      * you should probably check logfn manually before doing that.
294      */
295     if (logfn) {
296         va_list ap;
297         va_start(ap, fmt);
298         logfn(logctx, fmt, ap);
299         va_end(ap);
300     }
301 }
302
303 void *pageant_handle_msg(const void *msg, int msglen, int *outlen,
304                          void *logctx, pageant_logfn_t logfn)
305 {
306     const unsigned char *p = msg;
307     const unsigned char *msgend;
308     unsigned char *ret = snewn(AGENT_MAX_MSGLEN, unsigned char);
309     int type;
310     const char *fail_reason;
311
312     msgend = p + msglen;
313
314     /*
315      * Get the message type.
316      */
317     if (msgend < p+1) {
318         fail_reason = "message contained no type code";
319         goto failure;
320     }
321     type = *p++;
322
323     switch (type) {
324       case SSH1_AGENTC_REQUEST_RSA_IDENTITIES:
325         /*
326          * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
327          */
328         {
329             int len;
330             void *keylist;
331
332             plog(logctx, logfn, "request: SSH1_AGENTC_REQUEST_RSA_IDENTITIES");
333
334             ret[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER;
335             keylist = pageant_make_keylist1(&len);
336             if (len + 5 > AGENT_MAX_MSGLEN) {
337                 sfree(keylist);
338                 fail_reason = "output would exceed max msglen";
339                 goto failure;
340             }
341             PUT_32BIT(ret, len + 1);
342             memcpy(ret + 5, keylist, len);
343
344             plog(logctx, logfn, "reply: SSH1_AGENT_RSA_IDENTITIES_ANSWER");
345             if (logfn) {               /* skip this loop if not logging */
346                 int i;
347                 struct RSAKey *rkey;
348                 for (i = 0; NULL != (rkey = pageant_nth_ssh1_key(i)); i++) {
349                     char fingerprint[128];
350                     rsa_fingerprint(fingerprint, sizeof(fingerprint), rkey);
351                     plog(logctx, logfn, "returned key: %s", fingerprint);
352                 }
353             }
354             sfree(keylist);
355         }
356         break;
357       case SSH2_AGENTC_REQUEST_IDENTITIES:
358         /*
359          * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
360          */
361         {
362             int len;
363             void *keylist;
364
365             plog(logctx, logfn, "request: SSH2_AGENTC_REQUEST_IDENTITIES");
366
367             ret[4] = SSH2_AGENT_IDENTITIES_ANSWER;
368             keylist = pageant_make_keylist2(&len);
369             if (len + 5 > AGENT_MAX_MSGLEN) {
370                 sfree(keylist);
371                 fail_reason = "output would exceed max msglen";
372                 goto failure;
373             }
374             PUT_32BIT(ret, len + 1);
375             memcpy(ret + 5, keylist, len);
376
377             plog(logctx, logfn, "reply: SSH2_AGENT_IDENTITIES_ANSWER");
378             if (logfn) {               /* skip this loop if not logging */
379                 int i;
380                 struct ssh2_userkey *skey;
381                 for (i = 0; NULL != (skey = pageant_nth_ssh2_key(i)); i++) {
382                     char *fingerprint = skey->alg->fingerprint(skey->data);
383                     plog(logctx, logfn, "returned key: %s %s",
384                          fingerprint, skey->comment);
385                     sfree(fingerprint);
386                 }
387             }
388
389             sfree(keylist);
390         }
391         break;
392       case SSH1_AGENTC_RSA_CHALLENGE:
393         /*
394          * Reply with either SSH1_AGENT_RSA_RESPONSE or
395          * SSH_AGENT_FAILURE, depending on whether we have that key
396          * or not.
397          */
398         {
399             struct RSAKey reqkey, *key;
400             Bignum challenge, response;
401             unsigned char response_source[48], response_md5[16];
402             struct MD5Context md5c;
403             int i, len;
404
405             plog(logctx, logfn, "request: SSH1_AGENTC_RSA_CHALLENGE");
406
407             p += 4;
408             i = ssh1_read_bignum(p, msgend - p, &reqkey.exponent);
409             if (i < 0) {
410                 fail_reason = "request truncated before key exponent";
411                 goto failure;
412             }
413             p += i;
414             i = ssh1_read_bignum(p, msgend - p, &reqkey.modulus);
415             if (i < 0) {
416                 freebn(reqkey.exponent);
417                 fail_reason = "request truncated before key modulus";
418                 goto failure;
419             }
420             p += i;
421             i = ssh1_read_bignum(p, msgend - p, &challenge);
422             if (i < 0) {
423                 freebn(reqkey.exponent);
424                 freebn(reqkey.modulus);
425                 fail_reason = "request truncated before challenge";
426                 goto failure;
427             }
428             p += i;
429             if (msgend < p+16) {
430                 freebn(reqkey.exponent);
431                 freebn(reqkey.modulus);
432                 freebn(challenge);
433                 fail_reason = "request truncated before session id";
434                 goto failure;
435             }
436             memcpy(response_source + 32, p, 16);
437             p += 16;
438             if (msgend < p+4) {
439                 freebn(reqkey.exponent);
440                 freebn(reqkey.modulus);
441                 freebn(challenge);
442                 fail_reason = "request truncated before response type";
443                 goto failure;
444             }
445             if (GET_32BIT(p) != 1) {
446                 freebn(reqkey.exponent);
447                 freebn(reqkey.modulus);
448                 freebn(challenge);
449                 fail_reason = "response type other than 1 not supported";
450                 goto failure;
451             }
452             if (logfn) {
453                 char fingerprint[128];
454                 reqkey.comment = NULL;
455                 rsa_fingerprint(fingerprint, sizeof(fingerprint), &reqkey);
456                 plog(logctx, logfn, "requested key: %s", fingerprint);
457             }
458             if ((key = find234(rsakeys, &reqkey, NULL)) == NULL) {
459                 freebn(reqkey.exponent);
460                 freebn(reqkey.modulus);
461                 freebn(challenge);
462                 fail_reason = "key not found";
463                 goto failure;
464             }
465             response = rsadecrypt(challenge, key);
466             for (i = 0; i < 32; i++)
467                 response_source[i] = bignum_byte(response, 31 - i);
468
469             MD5Init(&md5c);
470             MD5Update(&md5c, response_source, 48);
471             MD5Final(response_md5, &md5c);
472             smemclr(response_source, 48);       /* burn the evidence */
473             freebn(response);          /* and that evidence */
474             freebn(challenge);         /* yes, and that evidence */
475             freebn(reqkey.exponent);   /* and free some memory ... */
476             freebn(reqkey.modulus);    /* ... while we're at it. */
477
478             /*
479              * Packet is the obvious five byte header, plus sixteen
480              * bytes of MD5.
481              */
482             len = 5 + 16;
483             PUT_32BIT(ret, len - 4);
484             ret[4] = SSH1_AGENT_RSA_RESPONSE;
485             memcpy(ret + 5, response_md5, 16);
486
487             plog(logctx, logfn, "reply: SSH1_AGENT_RSA_RESPONSE");
488         }
489         break;
490       case SSH2_AGENTC_SIGN_REQUEST:
491         /*
492          * Reply with either SSH2_AGENT_SIGN_RESPONSE or
493          * SSH_AGENT_FAILURE, depending on whether we have that key
494          * or not.
495          */
496         {
497             struct ssh2_userkey *key;
498             struct blob b;
499             const unsigned char *data;
500             unsigned char *signature;
501             int datalen, siglen, len;
502
503             plog(logctx, logfn, "request: SSH2_AGENTC_SIGN_REQUEST");
504
505             if (msgend < p+4) {
506                 fail_reason = "request truncated before public key";
507                 goto failure;
508             }
509             b.len = toint(GET_32BIT(p));
510             if (b.len < 0 || b.len > msgend - (p+4)) {
511                 fail_reason = "request truncated before public key";
512                 goto failure;
513             }
514             p += 4;
515             b.blob = p;
516             p += b.len;
517             if (msgend < p+4) {
518                 fail_reason = "request truncated before string to sign";
519                 goto failure;
520             }
521             datalen = toint(GET_32BIT(p));
522             p += 4;
523             if (datalen < 0 || datalen > msgend - p) {
524                 fail_reason = "request truncated before string to sign";
525                 goto failure;
526             }
527             data = p;
528             if (logfn) {
529                 char *fingerprint = fingerprint_ssh2_blob(b.blob, b.len);
530                 plog(logctx, logfn, "requested key: %s", fingerprint);
531                 sfree(fingerprint);
532             }
533             key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
534             if (!key) {
535                 fail_reason = "key not found";
536                 goto failure;
537             }
538             signature = key->alg->sign(key->data, (const char *)data,
539                                        datalen, &siglen);
540             len = 5 + 4 + siglen;
541             PUT_32BIT(ret, len - 4);
542             ret[4] = SSH2_AGENT_SIGN_RESPONSE;
543             PUT_32BIT(ret + 5, siglen);
544             memcpy(ret + 5 + 4, signature, siglen);
545             sfree(signature);
546
547             plog(logctx, logfn, "reply: SSH2_AGENT_SIGN_RESPONSE");
548         }
549         break;
550       case SSH1_AGENTC_ADD_RSA_IDENTITY:
551         /*
552          * Add to the list and return SSH_AGENT_SUCCESS, or
553          * SSH_AGENT_FAILURE if the key was malformed.
554          */
555         {
556             struct RSAKey *key;
557             char *comment;
558             int n, commentlen;
559
560             plog(logctx, logfn, "request: SSH1_AGENTC_ADD_RSA_IDENTITY");
561
562             key = snew(struct RSAKey);
563             memset(key, 0, sizeof(struct RSAKey));
564
565             n = makekey(p, msgend - p, key, NULL, 1);
566             if (n < 0) {
567                 freersakey(key);
568                 sfree(key);
569                 fail_reason = "request truncated before public key";
570                 goto failure;
571             }
572             p += n;
573
574             n = makeprivate(p, msgend - p, key);
575             if (n < 0) {
576                 freersakey(key);
577                 sfree(key);
578                 fail_reason = "request truncated before private key";
579                 goto failure;
580             }
581             p += n;
582
583             /* SSH-1 names p and q the other way round, i.e. we have
584              * the inverse of p mod q and not of q mod p. We swap the
585              * names, because our internal RSA wants iqmp. */
586
587             n = ssh1_read_bignum(p, msgend - p, &key->iqmp);  /* p^-1 mod q */
588             if (n < 0) {
589                 freersakey(key);
590                 sfree(key);
591                 fail_reason = "request truncated before iqmp";
592                 goto failure;
593             }
594             p += n;
595
596             n = ssh1_read_bignum(p, msgend - p, &key->q);  /* p */
597             if (n < 0) {
598                 freersakey(key);
599                 sfree(key);
600                 fail_reason = "request truncated before p";
601                 goto failure;
602             }
603             p += n;
604
605             n = ssh1_read_bignum(p, msgend - p, &key->p);  /* q */
606             if (n < 0) {
607                 freersakey(key);
608                 sfree(key);
609                 fail_reason = "request truncated before q";
610                 goto failure;
611             }
612             p += n;
613
614             if (msgend < p+4) {
615                 freersakey(key);
616                 sfree(key);
617                 fail_reason = "request truncated before key comment";
618                 goto failure;
619             }
620             commentlen = toint(GET_32BIT(p));
621
622             if (commentlen < 0 || commentlen > msgend - p) {
623                 freersakey(key);
624                 sfree(key);
625                 fail_reason = "request truncated before key comment";
626                 goto failure;
627             }
628
629             comment = snewn(commentlen+1, char);
630             if (comment) {
631                 memcpy(comment, p + 4, commentlen);
632                 comment[commentlen] = '\0';
633                 key->comment = comment;
634             }
635
636             if (logfn) {
637                 char fingerprint[128];
638                 rsa_fingerprint(fingerprint, sizeof(fingerprint), key);
639                 plog(logctx, logfn, "submitted key: %s", fingerprint);
640             }
641
642             if (add234(rsakeys, key) == key) {
643                 keylist_update();
644                 PUT_32BIT(ret, 1);
645                 ret[4] = SSH_AGENT_SUCCESS;
646
647                 plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
648             } else {
649                 freersakey(key);
650                 sfree(key);
651
652                 fail_reason = "key already present";
653                 goto failure;
654             }
655         }
656         break;
657       case SSH2_AGENTC_ADD_IDENTITY:
658         /*
659          * Add to the list and return SSH_AGENT_SUCCESS, or
660          * SSH_AGENT_FAILURE if the key was malformed.
661          */
662         {
663             struct ssh2_userkey *key;
664             char *comment;
665             const char *alg;
666             int alglen, commlen;
667             int bloblen;
668
669             plog(logctx, logfn, "request: SSH2_AGENTC_ADD_IDENTITY");
670
671             if (msgend < p+4) {
672                 fail_reason = "request truncated before key algorithm";
673                 goto failure;
674             }
675             alglen = toint(GET_32BIT(p));
676             p += 4;
677             if (alglen < 0 || alglen > msgend - p) {
678                 fail_reason = "request truncated before key algorithm";
679                 goto failure;
680             }
681             alg = (const char *)p;
682             p += alglen;
683
684             key = snew(struct ssh2_userkey);
685             key->alg = find_pubkey_alg_len(alglen, alg);
686             if (!key->alg) {
687                 sfree(key);
688                 fail_reason = "algorithm unknown";
689                 goto failure;
690             }
691
692             bloblen = msgend - p;
693             key->data = key->alg->openssh_createkey(&p, &bloblen);
694             if (!key->data) {
695                 sfree(key);
696                 fail_reason = "key setup failed";
697                 goto failure;
698             }
699
700             /*
701              * p has been advanced by openssh_createkey, but
702              * certainly not _beyond_ the end of the buffer.
703              */
704             assert(p <= msgend);
705
706             if (msgend < p+4) {
707                 key->alg->freekey(key->data);
708                 sfree(key);
709                 fail_reason = "request truncated before key comment";
710                 goto failure;
711             }
712             commlen = toint(GET_32BIT(p));
713             p += 4;
714
715             if (commlen < 0 || commlen > msgend - p) {
716                 key->alg->freekey(key->data);
717                 sfree(key);
718                 fail_reason = "request truncated before key comment";
719                 goto failure;
720             }
721             comment = snewn(commlen + 1, char);
722             if (comment) {
723                 memcpy(comment, p, commlen);
724                 comment[commlen] = '\0';
725             }
726             key->comment = comment;
727
728             if (logfn) {
729                 char *fingerprint = key->alg->fingerprint(key->data);
730                 plog(logctx, logfn, "submitted key: %s %s",
731                      fingerprint, key->comment);
732                 sfree(fingerprint);
733             }
734
735             if (add234(ssh2keys, key) == key) {
736                 keylist_update();
737                 PUT_32BIT(ret, 1);
738                 ret[4] = SSH_AGENT_SUCCESS;
739
740                 plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
741             } else {
742                 key->alg->freekey(key->data);
743                 sfree(key->comment);
744                 sfree(key);
745
746                 fail_reason = "key already present";
747                 goto failure;
748             }
749         }
750         break;
751       case SSH1_AGENTC_REMOVE_RSA_IDENTITY:
752         /*
753          * Remove from the list and return SSH_AGENT_SUCCESS, or
754          * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
755          * start with.
756          */
757         {
758             struct RSAKey reqkey, *key;
759             int n;
760
761             plog(logctx, logfn, "request: SSH1_AGENTC_REMOVE_RSA_IDENTITY");
762
763             n = makekey(p, msgend - p, &reqkey, NULL, 0);
764             if (n < 0) {
765                 fail_reason = "request truncated before public key";
766                 goto failure;
767             }
768
769             if (logfn) {
770                 char fingerprint[128];
771                 reqkey.comment = NULL;
772                 rsa_fingerprint(fingerprint, sizeof(fingerprint), &reqkey);
773                 plog(logctx, logfn, "unwanted key: %s", fingerprint);
774             }
775
776             key = find234(rsakeys, &reqkey, NULL);
777             freebn(reqkey.exponent);
778             freebn(reqkey.modulus);
779             PUT_32BIT(ret, 1);
780             if (key) {
781                 plog(logctx, logfn, "found with comment: %s", key->comment);
782
783                 del234(rsakeys, key);
784                 keylist_update();
785                 freersakey(key);
786                 sfree(key);
787                 ret[4] = SSH_AGENT_SUCCESS;
788
789                 plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
790             } else {
791                 fail_reason = "key not found";
792                 goto failure;
793             }
794         }
795         break;
796       case SSH2_AGENTC_REMOVE_IDENTITY:
797         /*
798          * Remove from the list and return SSH_AGENT_SUCCESS, or
799          * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
800          * start with.
801          */
802         {
803             struct ssh2_userkey *key;
804             struct blob b;
805
806             plog(logctx, logfn, "request: SSH2_AGENTC_REMOVE_IDENTITY");
807
808             if (msgend < p+4) {
809                 fail_reason = "request truncated before public key";
810                 goto failure;
811             }
812             b.len = toint(GET_32BIT(p));
813             p += 4;
814
815             if (b.len < 0 || b.len > msgend - p) {
816                 fail_reason = "request truncated before public key";
817                 goto failure;
818             }
819             b.blob = p;
820             p += b.len;
821
822             if (logfn) {
823                 char *fingerprint = fingerprint_ssh2_blob(b.blob, b.len);
824                 plog(logctx, logfn, "unwanted key: %s", fingerprint);
825                 sfree(fingerprint);
826             }
827
828             key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
829             if (!key) {
830                 fail_reason = "key not found";
831                 goto failure;
832             }
833
834             plog(logctx, logfn, "found with comment: %s", key->comment);
835
836             del234(ssh2keys, key);
837             keylist_update();
838             key->alg->freekey(key->data);
839             sfree(key);
840             PUT_32BIT(ret, 1);
841             ret[4] = SSH_AGENT_SUCCESS;
842
843             plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
844         }
845         break;
846       case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
847         /*
848          * Remove all SSH-1 keys. Always returns success.
849          */
850         {
851             struct RSAKey *rkey;
852
853             plog(logctx, logfn, "request:"
854                 " SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES");
855
856             while ((rkey = index234(rsakeys, 0)) != NULL) {
857                 del234(rsakeys, rkey);
858                 freersakey(rkey);
859                 sfree(rkey);
860             }
861             keylist_update();
862
863             PUT_32BIT(ret, 1);
864             ret[4] = SSH_AGENT_SUCCESS;
865
866             plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
867         }
868         break;
869       case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
870         /*
871          * Remove all SSH-2 keys. Always returns success.
872          */
873         {
874             struct ssh2_userkey *skey;
875
876             plog(logctx, logfn, "request: SSH2_AGENTC_REMOVE_ALL_IDENTITIES");
877
878             while ((skey = index234(ssh2keys, 0)) != NULL) {
879                 del234(ssh2keys, skey);
880                 skey->alg->freekey(skey->data);
881                 sfree(skey);
882             }
883             keylist_update();
884
885             PUT_32BIT(ret, 1);
886             ret[4] = SSH_AGENT_SUCCESS;
887
888             plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
889         }
890         break;
891       default:
892         plog(logctx, logfn, "request: unknown message type %d", type);
893
894         fail_reason = "unrecognised message";
895         /* fall through */
896       failure:
897         /*
898          * Unrecognised message. Return SSH_AGENT_FAILURE.
899          */
900         PUT_32BIT(ret, 1);
901         ret[4] = SSH_AGENT_FAILURE;
902         plog(logctx, logfn, "reply: SSH_AGENT_FAILURE (%s)", fail_reason);
903         break;
904     }
905
906     *outlen = 4 + GET_32BIT(ret);
907     return ret;
908 }
909
910 void *pageant_failure_msg(int *outlen)
911 {
912     unsigned char *ret = snewn(5, unsigned char);
913     PUT_32BIT(ret, 1);
914     ret[4] = SSH_AGENT_FAILURE;
915     *outlen = 5;
916     return ret;
917 }
918
919 void pageant_init(void)
920 {
921     rsakeys = newtree234(cmpkeys_rsa);
922     ssh2keys = newtree234(cmpkeys_ssh2);
923 }
924
925 struct RSAKey *pageant_nth_ssh1_key(int i)
926 {
927     return index234(rsakeys, i);
928 }
929
930 struct ssh2_userkey *pageant_nth_ssh2_key(int i)
931 {
932     return index234(ssh2keys, i);
933 }
934
935 int pageant_count_ssh1_keys(void)
936 {
937     return count234(rsakeys);
938 }
939
940 int pageant_count_ssh2_keys(void)
941 {
942     return count234(ssh2keys);
943 }
944
945 int pageant_add_ssh1_key(struct RSAKey *rkey)
946 {
947     return add234(rsakeys, rkey) == rkey;
948 }
949
950 int pageant_add_ssh2_key(struct ssh2_userkey *skey)
951 {
952     return add234(ssh2keys, skey) == skey;
953 }
954
955 int pageant_delete_ssh1_key(struct RSAKey *rkey)
956 {
957     struct RSAKey *deleted = del234(rsakeys, rkey);
958     if (!deleted)
959         return FALSE;
960     assert(deleted == rkey);
961     return TRUE;
962 }
963
964 int pageant_delete_ssh2_key(struct ssh2_userkey *skey)
965 {
966     struct ssh2_userkey *deleted = del234(ssh2keys, skey);
967     if (!deleted)
968         return FALSE;
969     assert(deleted == skey);
970     return TRUE;
971 }
972
973 /* ----------------------------------------------------------------------
974  * The agent plug.
975  */
976
977 /*
978  * Coroutine macros similar to, but simplified from, those in ssh.c.
979  */
980 #define crBegin(v)      { int *crLine = &v; switch(v) { case 0:;
981 #define crFinish(z)     } *crLine = 0; return (z); }
982 #define crGetChar(c) do                                         \
983     {                                                           \
984         while (len == 0) {                                      \
985             *crLine =__LINE__; return 1; case __LINE__:;        \
986         }                                                       \
987         len--;                                                  \
988         (c) = (unsigned char)*data++;                           \
989     } while (0)
990
991 struct pageant_conn_state {
992     const struct plug_function_table *fn;
993     /* the above variable absolutely *must* be the first in this structure */
994
995     Socket connsock;
996     void *logctx;
997     pageant_logfn_t logfn;
998     unsigned char lenbuf[4], pktbuf[AGENT_MAX_MSGLEN];
999     unsigned len, got;
1000     int real_packet;
1001     int crLine;            /* for coroutine in pageant_conn_receive */
1002 };
1003
1004 static int pageant_conn_closing(Plug plug, const char *error_msg,
1005                                 int error_code, int calling_back)
1006 {
1007     struct pageant_conn_state *pc = (struct pageant_conn_state *)plug;
1008     if (error_msg)
1009         plog(pc->logctx, pc->logfn, "%p: error: %s", pc, error_msg);
1010     else
1011         plog(pc->logctx, pc->logfn, "%p: connection closed", pc);
1012     sk_close(pc->connsock);
1013     sfree(pc);
1014     return 1;
1015 }
1016
1017 static void pageant_conn_sent(Plug plug, int bufsize)
1018 {
1019     /* struct pageant_conn_state *pc = (struct pageant_conn_state *)plug; */
1020
1021     /*
1022      * We do nothing here, because we expect that there won't be a
1023      * need to throttle and unthrottle the connection to an agent -
1024      * clients will typically not send many requests, and will wait
1025      * until they receive each reply before sending a new request.
1026      */
1027 }
1028
1029 static void pageant_conn_log(void *logctx, const char *fmt, va_list ap)
1030 {
1031     /* Wrapper on pc->logfn that prefixes the connection identifier */
1032     struct pageant_conn_state *pc = (struct pageant_conn_state *)logctx;
1033     char *formatted = dupvprintf(fmt, ap);
1034     plog(pc->logctx, pc->logfn, "%p: %s", pc, formatted);
1035     sfree(formatted);
1036 }
1037
1038 static int pageant_conn_receive(Plug plug, int urgent, char *data, int len)
1039 {
1040     struct pageant_conn_state *pc = (struct pageant_conn_state *)plug;
1041     char c;
1042
1043     crBegin(pc->crLine);
1044
1045     while (len > 0) {
1046         pc->got = 0;
1047         while (pc->got < 4) {
1048             crGetChar(c);
1049             pc->lenbuf[pc->got++] = c;
1050         }
1051
1052         pc->len = GET_32BIT(pc->lenbuf);
1053         pc->got = 0;
1054         pc->real_packet = (pc->len < AGENT_MAX_MSGLEN-4);
1055
1056         while (pc->got < pc->len) {
1057             crGetChar(c);
1058             if (pc->real_packet)
1059                 pc->pktbuf[pc->got] = c;
1060             pc->got++;
1061         }
1062
1063         {
1064             void *reply;
1065             int replylen;
1066
1067             if (pc->real_packet) {
1068                 reply = pageant_handle_msg(pc->pktbuf, pc->len, &replylen, pc,
1069                                            pc->logfn?pageant_conn_log:NULL);
1070             } else {
1071                 plog(pc->logctx, pc->logfn, "%p: overlong message (%u)",
1072                      pc, pc->len);
1073                 plog(pc->logctx, pc->logfn, "%p: reply: SSH_AGENT_FAILURE "
1074                      "(message too long)", pc);
1075                 reply = pageant_failure_msg(&replylen);
1076             }
1077             sk_write(pc->connsock, reply, replylen);
1078             smemclr(reply, replylen);
1079         }
1080     }
1081
1082     crFinish(1);
1083 }
1084
1085 struct pageant_listen_state {
1086     const struct plug_function_table *fn;
1087     /* the above variable absolutely *must* be the first in this structure */
1088
1089     Socket listensock;
1090     void *logctx;
1091     pageant_logfn_t logfn;
1092 };
1093
1094 static int pageant_listen_closing(Plug plug, const char *error_msg,
1095                                   int error_code, int calling_back)
1096 {
1097     struct pageant_listen_state *pl = (struct pageant_listen_state *)plug;
1098     if (error_msg)
1099         plog(pl->logctx, pl->logfn, "listening socket: error: %s", error_msg);
1100     sk_close(pl->listensock);
1101     pl->listensock = NULL;
1102     return 1;
1103 }
1104
1105 static int pageant_listen_accepting(Plug plug,
1106                                     accept_fn_t constructor, accept_ctx_t ctx)
1107 {
1108     static const struct plug_function_table connection_fn_table = {
1109         NULL, /* no log function, because that's for outgoing connections */
1110         pageant_conn_closing,
1111         pageant_conn_receive,
1112         pageant_conn_sent,
1113         NULL /* no accepting function, because we've already done it */
1114     };
1115     struct pageant_listen_state *pl = (struct pageant_listen_state *)plug;
1116     struct pageant_conn_state *pc;
1117     const char *err;
1118
1119     pc = snew(struct pageant_conn_state);
1120     pc->fn = &connection_fn_table;
1121     pc->logfn = pl->logfn;
1122     pc->logctx = pl->logctx;
1123     pc->crLine = 0;
1124
1125     pc->connsock = constructor(ctx, (Plug) pc);
1126     if ((err = sk_socket_error(pc->connsock)) != NULL) {
1127         sk_close(pc->connsock);
1128         sfree(pc);
1129         return TRUE;
1130     }
1131
1132     sk_set_frozen(pc->connsock, 0);
1133
1134     /* FIXME: can we get any useful peer id info? */
1135     plog(pl->logctx, pl->logfn, "%p: new connection", pc);
1136
1137     return 0;
1138 }
1139
1140 struct pageant_listen_state *pageant_listener_new(void)
1141 {
1142     static const struct plug_function_table listener_fn_table = {
1143         NULL, /* no log function, because that's for outgoing connections */
1144         pageant_listen_closing,
1145         NULL, /* no receive function on a listening socket */
1146         NULL, /* no sent function on a listening socket */
1147         pageant_listen_accepting
1148     };
1149
1150     struct pageant_listen_state *pl = snew(struct pageant_listen_state);
1151     pl->fn = &listener_fn_table;
1152     pl->logctx = NULL;
1153     pl->logfn = NULL;
1154     pl->listensock = NULL;
1155     return pl;
1156 }
1157
1158 void pageant_listener_got_socket(struct pageant_listen_state *pl, Socket sock)
1159 {
1160     pl->listensock = sock;
1161 }
1162
1163 void pageant_listener_set_logfn(struct pageant_listen_state *pl,
1164                                 void *logctx, pageant_logfn_t logfn)
1165 {
1166     pl->logctx = logctx;
1167     pl->logfn = logfn;
1168 }
1169
1170 void pageant_listener_free(struct pageant_listen_state *pl)
1171 {
1172     if (pl->listensock)
1173         sk_close(pl->listensock);
1174     sfree(pl);
1175 }