2 * pageant.c: cross-platform code to implement Pageant.
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
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.
25 modalfatalbox("Internal error: attempt to use random numbers in Pageant");
27 return 0; /* unreachable, but placate optimiser */
30 static int pageant_local = FALSE;
33 * rsakeys stores SSH-1 RSA keys. ssh2keys stores all SSH-2 keys.
35 static tree234 *rsakeys, *ssh2keys;
38 * Blob structure for passing to the asymmetric SSH-2 key compare
39 * function, prototyped here.
42 const unsigned char *blob;
45 static int cmpkeys_ssh2_asymm(void *av, void *bv);
48 * Key comparison function for the 2-3-4 tree of RSA keys.
50 static int cmpkeys_rsa(void *av, void *bv)
52 struct RSAKey *a = (struct RSAKey *) av;
53 struct RSAKey *b = (struct RSAKey *) bv;
60 * Compare by length of moduli.
62 alen = bignum_bitcount(am);
63 blen = bignum_bitcount(bm);
69 * Now compare by moduli themselves.
71 alen = (alen + 7) / 8; /* byte count */
74 abyte = bignum_byte(am, alen);
75 bbyte = bignum_byte(bm, alen);
78 else if (abyte < bbyte)
88 * Key comparison function for the 2-3-4 tree of SSH-2 keys.
90 static int cmpkeys_ssh2(void *av, void *bv)
92 struct ssh2_userkey *a = (struct ssh2_userkey *) av;
93 struct ssh2_userkey *b = (struct ssh2_userkey *) bv;
96 unsigned char *ablob, *bblob;
100 * Compare purely by public blob.
102 ablob = a->alg->public_blob(a->data, &alen);
103 bblob = b->alg->public_blob(b->data, &blen);
106 for (i = 0; i < alen && i < blen; i++) {
107 if (ablob[i] < bblob[i]) {
110 } else if (ablob[i] > bblob[i]) {
115 if (c == 0 && i < alen)
116 c = +1; /* a is longer */
117 if (c == 0 && i < blen)
118 c = -1; /* a is longer */
127 * Key comparison function for looking up a blob in the 2-3-4 tree
130 static int cmpkeys_ssh2_asymm(void *av, void *bv)
132 struct blob *a = (struct blob *) av;
133 struct ssh2_userkey *b = (struct ssh2_userkey *) bv;
136 const unsigned char *ablob;
137 unsigned char *bblob;
141 * Compare purely by public blob.
145 bblob = b->alg->public_blob(b->data, &blen);
148 for (i = 0; i < alen && i < blen; i++) {
149 if (ablob[i] < bblob[i]) {
152 } else if (ablob[i] > bblob[i]) {
157 if (c == 0 && i < alen)
158 c = +1; /* a is longer */
159 if (c == 0 && i < blen)
160 c = -1; /* a is longer */
168 * Create an SSH-1 key list in a malloc'ed buffer; return its
171 void *pageant_make_keylist1(int *length)
175 unsigned char *blob, *p, *ret;
179 * Count up the number and length of keys we hold.
183 for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
185 blob = rsa_public_blob(key, &bloblen);
188 len += 4 + strlen(key->comment);
191 /* Allocate the buffer. */
192 p = ret = snewn(len, unsigned char);
193 if (length) *length = len;
197 for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
198 blob = rsa_public_blob(key, &bloblen);
199 memcpy(p, blob, bloblen);
202 PUT_32BIT(p, strlen(key->comment));
203 memcpy(p + 4, key->comment, strlen(key->comment));
204 p += 4 + strlen(key->comment);
207 assert(p - ret == len);
212 * Create an SSH-2 key list in a malloc'ed buffer; return its
215 void *pageant_make_keylist2(int *length)
217 struct ssh2_userkey *key;
219 unsigned char *blob, *p, *ret;
223 * Count up the number and length of keys we hold.
227 for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
229 len += 4; /* length field */
230 blob = key->alg->public_blob(key->data, &bloblen);
233 len += 4 + strlen(key->comment);
236 /* Allocate the buffer. */
237 p = ret = snewn(len, unsigned char);
238 if (length) *length = len;
241 * Packet header is the obvious five bytes, plus four
242 * bytes for the key count.
246 for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
247 blob = key->alg->public_blob(key->data, &bloblen);
248 PUT_32BIT(p, bloblen);
250 memcpy(p, blob, bloblen);
253 PUT_32BIT(p, strlen(key->comment));
254 memcpy(p + 4, key->comment, strlen(key->comment));
255 p += 4 + strlen(key->comment);
258 assert(p - ret == len);
262 static void plog(void *logctx, pageant_logfn_t logfn, const char *fmt, ...)
264 __attribute__ ((format (printf, 3, 4)))
268 static void plog(void *logctx, pageant_logfn_t logfn, const char *fmt, ...)
271 * This is the wrapper that takes a variadic argument list and
272 * turns it into the va_list that the log function really expects.
273 * It's safe to call this with logfn==NULL, because we
274 * double-check that below; but if you're going to do lots of work
275 * before getting here (such as looping, or hashing things) then
276 * you should probably check logfn manually before doing that.
281 logfn(logctx, fmt, ap);
286 void *pageant_handle_msg(const void *msg, int msglen, int *outlen,
287 void *logctx, pageant_logfn_t logfn)
289 const unsigned char *p = msg;
290 const unsigned char *msgend;
291 unsigned char *ret = snewn(AGENT_MAX_MSGLEN, unsigned char);
293 const char *fail_reason;
298 * Get the message type.
301 fail_reason = "message contained no type code";
307 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES:
309 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
315 plog(logctx, logfn, "request: SSH1_AGENTC_REQUEST_RSA_IDENTITIES");
317 ret[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER;
318 keylist = pageant_make_keylist1(&len);
319 if (len + 5 > AGENT_MAX_MSGLEN) {
321 fail_reason = "output would exceed max msglen";
324 PUT_32BIT(ret, len + 1);
325 memcpy(ret + 5, keylist, len);
327 plog(logctx, logfn, "reply: SSH1_AGENT_RSA_IDENTITIES_ANSWER");
328 if (logfn) { /* skip this loop if not logging */
331 for (i = 0; NULL != (rkey = pageant_nth_ssh1_key(i)); i++) {
332 char fingerprint[128];
333 rsa_fingerprint(fingerprint, sizeof(fingerprint), rkey);
334 plog(logctx, logfn, "returned key: %s", fingerprint);
340 case SSH2_AGENTC_REQUEST_IDENTITIES:
342 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
348 plog(logctx, logfn, "request: SSH2_AGENTC_REQUEST_IDENTITIES");
350 ret[4] = SSH2_AGENT_IDENTITIES_ANSWER;
351 keylist = pageant_make_keylist2(&len);
352 if (len + 5 > AGENT_MAX_MSGLEN) {
354 fail_reason = "output would exceed max msglen";
357 PUT_32BIT(ret, len + 1);
358 memcpy(ret + 5, keylist, len);
360 plog(logctx, logfn, "reply: SSH2_AGENT_IDENTITIES_ANSWER");
361 if (logfn) { /* skip this loop if not logging */
363 struct ssh2_userkey *skey;
364 for (i = 0; NULL != (skey = pageant_nth_ssh2_key(i)); i++) {
365 char *fingerprint = ssh2_fingerprint(skey->alg,
367 plog(logctx, logfn, "returned key: %s %s",
368 fingerprint, skey->comment);
376 case SSH1_AGENTC_RSA_CHALLENGE:
378 * Reply with either SSH1_AGENT_RSA_RESPONSE or
379 * SSH_AGENT_FAILURE, depending on whether we have that key
383 struct RSAKey reqkey, *key;
384 Bignum challenge, response;
385 unsigned char response_source[48], response_md5[16];
386 struct MD5Context md5c;
389 plog(logctx, logfn, "request: SSH1_AGENTC_RSA_CHALLENGE");
392 i = ssh1_read_bignum(p, msgend - p, &reqkey.exponent);
394 fail_reason = "request truncated before key exponent";
398 i = ssh1_read_bignum(p, msgend - p, &reqkey.modulus);
400 freebn(reqkey.exponent);
401 fail_reason = "request truncated before key modulus";
405 i = ssh1_read_bignum(p, msgend - p, &challenge);
407 freebn(reqkey.exponent);
408 freebn(reqkey.modulus);
409 fail_reason = "request truncated before challenge";
414 freebn(reqkey.exponent);
415 freebn(reqkey.modulus);
417 fail_reason = "request truncated before session id";
420 memcpy(response_source + 32, p, 16);
423 freebn(reqkey.exponent);
424 freebn(reqkey.modulus);
426 fail_reason = "request truncated before response type";
429 if (GET_32BIT(p) != 1) {
430 freebn(reqkey.exponent);
431 freebn(reqkey.modulus);
433 fail_reason = "response type other than 1 not supported";
437 char fingerprint[128];
438 reqkey.comment = NULL;
439 rsa_fingerprint(fingerprint, sizeof(fingerprint), &reqkey);
440 plog(logctx, logfn, "requested key: %s", fingerprint);
442 if ((key = find234(rsakeys, &reqkey, NULL)) == NULL) {
443 freebn(reqkey.exponent);
444 freebn(reqkey.modulus);
446 fail_reason = "key not found";
449 response = rsadecrypt(challenge, key);
450 for (i = 0; i < 32; i++)
451 response_source[i] = bignum_byte(response, 31 - i);
454 MD5Update(&md5c, response_source, 48);
455 MD5Final(response_md5, &md5c);
456 smemclr(response_source, 48); /* burn the evidence */
457 freebn(response); /* and that evidence */
458 freebn(challenge); /* yes, and that evidence */
459 freebn(reqkey.exponent); /* and free some memory ... */
460 freebn(reqkey.modulus); /* ... while we're at it. */
463 * Packet is the obvious five byte header, plus sixteen
467 PUT_32BIT(ret, len - 4);
468 ret[4] = SSH1_AGENT_RSA_RESPONSE;
469 memcpy(ret + 5, response_md5, 16);
471 plog(logctx, logfn, "reply: SSH1_AGENT_RSA_RESPONSE");
474 case SSH2_AGENTC_SIGN_REQUEST:
476 * Reply with either SSH2_AGENT_SIGN_RESPONSE or
477 * SSH_AGENT_FAILURE, depending on whether we have that key
481 struct ssh2_userkey *key;
483 const unsigned char *data;
484 unsigned char *signature;
485 int datalen, siglen, len;
487 plog(logctx, logfn, "request: SSH2_AGENTC_SIGN_REQUEST");
490 fail_reason = "request truncated before public key";
493 b.len = toint(GET_32BIT(p));
494 if (b.len < 0 || b.len > msgend - (p+4)) {
495 fail_reason = "request truncated before public key";
502 fail_reason = "request truncated before string to sign";
505 datalen = toint(GET_32BIT(p));
507 if (datalen < 0 || datalen > msgend - p) {
508 fail_reason = "request truncated before string to sign";
513 char *fingerprint = ssh2_fingerprint_blob(b.blob, b.len);
514 plog(logctx, logfn, "requested key: %s", fingerprint);
517 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
519 fail_reason = "key not found";
522 signature = key->alg->sign(key->data, (const char *)data,
524 len = 5 + 4 + siglen;
525 PUT_32BIT(ret, len - 4);
526 ret[4] = SSH2_AGENT_SIGN_RESPONSE;
527 PUT_32BIT(ret + 5, siglen);
528 memcpy(ret + 5 + 4, signature, siglen);
531 plog(logctx, logfn, "reply: SSH2_AGENT_SIGN_RESPONSE");
534 case SSH1_AGENTC_ADD_RSA_IDENTITY:
536 * Add to the list and return SSH_AGENT_SUCCESS, or
537 * SSH_AGENT_FAILURE if the key was malformed.
544 plog(logctx, logfn, "request: SSH1_AGENTC_ADD_RSA_IDENTITY");
546 key = snew(struct RSAKey);
547 memset(key, 0, sizeof(struct RSAKey));
549 n = makekey(p, msgend - p, key, NULL, 1);
553 fail_reason = "request truncated before public key";
558 n = makeprivate(p, msgend - p, key);
562 fail_reason = "request truncated before private key";
567 /* SSH-1 names p and q the other way round, i.e. we have
568 * the inverse of p mod q and not of q mod p. We swap the
569 * names, because our internal RSA wants iqmp. */
571 n = ssh1_read_bignum(p, msgend - p, &key->iqmp); /* p^-1 mod q */
575 fail_reason = "request truncated before iqmp";
580 n = ssh1_read_bignum(p, msgend - p, &key->q); /* p */
584 fail_reason = "request truncated before p";
589 n = ssh1_read_bignum(p, msgend - p, &key->p); /* q */
593 fail_reason = "request truncated before q";
601 fail_reason = "request truncated before key comment";
604 commentlen = toint(GET_32BIT(p));
606 if (commentlen < 0 || commentlen > msgend - p) {
609 fail_reason = "request truncated before key comment";
613 comment = snewn(commentlen+1, char);
615 memcpy(comment, p + 4, commentlen);
616 comment[commentlen] = '\0';
617 key->comment = comment;
621 char fingerprint[128];
622 rsa_fingerprint(fingerprint, sizeof(fingerprint), key);
623 plog(logctx, logfn, "submitted key: %s", fingerprint);
626 if (add234(rsakeys, key) == key) {
629 ret[4] = SSH_AGENT_SUCCESS;
631 plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
636 fail_reason = "key already present";
641 case SSH2_AGENTC_ADD_IDENTITY:
643 * Add to the list and return SSH_AGENT_SUCCESS, or
644 * SSH_AGENT_FAILURE if the key was malformed.
647 struct ssh2_userkey *key;
653 plog(logctx, logfn, "request: SSH2_AGENTC_ADD_IDENTITY");
656 fail_reason = "request truncated before key algorithm";
659 alglen = toint(GET_32BIT(p));
661 if (alglen < 0 || alglen > msgend - p) {
662 fail_reason = "request truncated before key algorithm";
665 alg = (const char *)p;
668 key = snew(struct ssh2_userkey);
669 key->alg = find_pubkey_alg_len(alglen, alg);
672 fail_reason = "algorithm unknown";
676 bloblen = msgend - p;
677 key->data = key->alg->openssh_createkey(&p, &bloblen);
680 fail_reason = "key setup failed";
685 * p has been advanced by openssh_createkey, but
686 * certainly not _beyond_ the end of the buffer.
691 key->alg->freekey(key->data);
693 fail_reason = "request truncated before key comment";
696 commlen = toint(GET_32BIT(p));
699 if (commlen < 0 || commlen > msgend - p) {
700 key->alg->freekey(key->data);
702 fail_reason = "request truncated before key comment";
705 comment = snewn(commlen + 1, char);
707 memcpy(comment, p, commlen);
708 comment[commlen] = '\0';
710 key->comment = comment;
713 char *fingerprint = ssh2_fingerprint(key->alg, key->data);
714 plog(logctx, logfn, "submitted key: %s %s",
715 fingerprint, key->comment);
719 if (add234(ssh2keys, key) == key) {
722 ret[4] = SSH_AGENT_SUCCESS;
724 plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
726 key->alg->freekey(key->data);
730 fail_reason = "key already present";
735 case SSH1_AGENTC_REMOVE_RSA_IDENTITY:
737 * Remove from the list and return SSH_AGENT_SUCCESS, or
738 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
742 struct RSAKey reqkey, *key;
745 plog(logctx, logfn, "request: SSH1_AGENTC_REMOVE_RSA_IDENTITY");
747 n = makekey(p, msgend - p, &reqkey, NULL, 0);
749 fail_reason = "request truncated before public key";
754 char fingerprint[128];
755 reqkey.comment = NULL;
756 rsa_fingerprint(fingerprint, sizeof(fingerprint), &reqkey);
757 plog(logctx, logfn, "unwanted key: %s", fingerprint);
760 key = find234(rsakeys, &reqkey, NULL);
761 freebn(reqkey.exponent);
762 freebn(reqkey.modulus);
765 plog(logctx, logfn, "found with comment: %s", key->comment);
767 del234(rsakeys, key);
771 ret[4] = SSH_AGENT_SUCCESS;
773 plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
775 fail_reason = "key not found";
780 case SSH2_AGENTC_REMOVE_IDENTITY:
782 * Remove from the list and return SSH_AGENT_SUCCESS, or
783 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
787 struct ssh2_userkey *key;
790 plog(logctx, logfn, "request: SSH2_AGENTC_REMOVE_IDENTITY");
793 fail_reason = "request truncated before public key";
796 b.len = toint(GET_32BIT(p));
799 if (b.len < 0 || b.len > msgend - p) {
800 fail_reason = "request truncated before public key";
807 char *fingerprint = ssh2_fingerprint_blob(b.blob, b.len);
808 plog(logctx, logfn, "unwanted key: %s", fingerprint);
812 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
814 fail_reason = "key not found";
818 plog(logctx, logfn, "found with comment: %s", key->comment);
820 del234(ssh2keys, key);
822 key->alg->freekey(key->data);
825 ret[4] = SSH_AGENT_SUCCESS;
827 plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
830 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
832 * Remove all SSH-1 keys. Always returns success.
837 plog(logctx, logfn, "request:"
838 " SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES");
840 while ((rkey = index234(rsakeys, 0)) != NULL) {
841 del234(rsakeys, rkey);
848 ret[4] = SSH_AGENT_SUCCESS;
850 plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
853 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
855 * Remove all SSH-2 keys. Always returns success.
858 struct ssh2_userkey *skey;
860 plog(logctx, logfn, "request: SSH2_AGENTC_REMOVE_ALL_IDENTITIES");
862 while ((skey = index234(ssh2keys, 0)) != NULL) {
863 del234(ssh2keys, skey);
864 skey->alg->freekey(skey->data);
870 ret[4] = SSH_AGENT_SUCCESS;
872 plog(logctx, logfn, "reply: SSH_AGENT_SUCCESS");
876 plog(logctx, logfn, "request: unknown message type %d", type);
878 fail_reason = "unrecognised message";
882 * Unrecognised message. Return SSH_AGENT_FAILURE.
885 ret[4] = SSH_AGENT_FAILURE;
886 plog(logctx, logfn, "reply: SSH_AGENT_FAILURE (%s)", fail_reason);
890 *outlen = 4 + GET_32BIT(ret);
894 void *pageant_failure_msg(int *outlen)
896 unsigned char *ret = snewn(5, unsigned char);
898 ret[4] = SSH_AGENT_FAILURE;
903 void pageant_init(void)
905 pageant_local = TRUE;
906 rsakeys = newtree234(cmpkeys_rsa);
907 ssh2keys = newtree234(cmpkeys_ssh2);
910 struct RSAKey *pageant_nth_ssh1_key(int i)
912 return index234(rsakeys, i);
915 struct ssh2_userkey *pageant_nth_ssh2_key(int i)
917 return index234(ssh2keys, i);
920 int pageant_count_ssh1_keys(void)
922 return count234(rsakeys);
925 int pageant_count_ssh2_keys(void)
927 return count234(ssh2keys);
930 int pageant_add_ssh1_key(struct RSAKey *rkey)
932 return add234(rsakeys, rkey) == rkey;
935 int pageant_add_ssh2_key(struct ssh2_userkey *skey)
937 return add234(ssh2keys, skey) == skey;
940 int pageant_delete_ssh1_key(struct RSAKey *rkey)
942 struct RSAKey *deleted = del234(rsakeys, rkey);
945 assert(deleted == rkey);
949 int pageant_delete_ssh2_key(struct ssh2_userkey *skey)
951 struct ssh2_userkey *deleted = del234(ssh2keys, skey);
954 assert(deleted == skey);
958 /* ----------------------------------------------------------------------
963 * Coroutine macros similar to, but simplified from, those in ssh.c.
965 #define crBegin(v) { int *crLine = &v; switch(v) { case 0:;
966 #define crFinish(z) } *crLine = 0; return (z); }
967 #define crGetChar(c) do \
970 *crLine =__LINE__; return 1; case __LINE__:; \
973 (c) = (unsigned char)*data++; \
976 struct pageant_conn_state {
977 const struct plug_function_table *fn;
978 /* the above variable absolutely *must* be the first in this structure */
982 pageant_logfn_t logfn;
983 unsigned char lenbuf[4], pktbuf[AGENT_MAX_MSGLEN];
986 int crLine; /* for coroutine in pageant_conn_receive */
989 static int pageant_conn_closing(Plug plug, const char *error_msg,
990 int error_code, int calling_back)
992 struct pageant_conn_state *pc = (struct pageant_conn_state *)plug;
994 plog(pc->logctx, pc->logfn, "%p: error: %s", pc, error_msg);
996 plog(pc->logctx, pc->logfn, "%p: connection closed", pc);
997 sk_close(pc->connsock);
1002 static void pageant_conn_sent(Plug plug, int bufsize)
1004 /* struct pageant_conn_state *pc = (struct pageant_conn_state *)plug; */
1007 * We do nothing here, because we expect that there won't be a
1008 * need to throttle and unthrottle the connection to an agent -
1009 * clients will typically not send many requests, and will wait
1010 * until they receive each reply before sending a new request.
1014 static void pageant_conn_log(void *logctx, const char *fmt, va_list ap)
1016 /* Wrapper on pc->logfn that prefixes the connection identifier */
1017 struct pageant_conn_state *pc = (struct pageant_conn_state *)logctx;
1018 char *formatted = dupvprintf(fmt, ap);
1019 plog(pc->logctx, pc->logfn, "%p: %s", pc, formatted);
1023 static int pageant_conn_receive(Plug plug, int urgent, char *data, int len)
1025 struct pageant_conn_state *pc = (struct pageant_conn_state *)plug;
1028 crBegin(pc->crLine);
1032 while (pc->got < 4) {
1034 pc->lenbuf[pc->got++] = c;
1037 pc->len = GET_32BIT(pc->lenbuf);
1039 pc->real_packet = (pc->len < AGENT_MAX_MSGLEN-4);
1041 while (pc->got < pc->len) {
1043 if (pc->real_packet)
1044 pc->pktbuf[pc->got] = c;
1052 if (pc->real_packet) {
1053 reply = pageant_handle_msg(pc->pktbuf, pc->len, &replylen, pc,
1054 pc->logfn?pageant_conn_log:NULL);
1056 plog(pc->logctx, pc->logfn, "%p: overlong message (%u)",
1058 plog(pc->logctx, pc->logfn, "%p: reply: SSH_AGENT_FAILURE "
1059 "(message too long)", pc);
1060 reply = pageant_failure_msg(&replylen);
1062 sk_write(pc->connsock, reply, replylen);
1063 smemclr(reply, replylen);
1070 struct pageant_listen_state {
1071 const struct plug_function_table *fn;
1072 /* the above variable absolutely *must* be the first in this structure */
1076 pageant_logfn_t logfn;
1079 static int pageant_listen_closing(Plug plug, const char *error_msg,
1080 int error_code, int calling_back)
1082 struct pageant_listen_state *pl = (struct pageant_listen_state *)plug;
1084 plog(pl->logctx, pl->logfn, "listening socket: error: %s", error_msg);
1085 sk_close(pl->listensock);
1086 pl->listensock = NULL;
1090 static int pageant_listen_accepting(Plug plug,
1091 accept_fn_t constructor, accept_ctx_t ctx)
1093 static const struct plug_function_table connection_fn_table = {
1094 NULL, /* no log function, because that's for outgoing connections */
1095 pageant_conn_closing,
1096 pageant_conn_receive,
1098 NULL /* no accepting function, because we've already done it */
1100 struct pageant_listen_state *pl = (struct pageant_listen_state *)plug;
1101 struct pageant_conn_state *pc;
1104 pc = snew(struct pageant_conn_state);
1105 pc->fn = &connection_fn_table;
1106 pc->logfn = pl->logfn;
1107 pc->logctx = pl->logctx;
1110 pc->connsock = constructor(ctx, (Plug) pc);
1111 if ((err = sk_socket_error(pc->connsock)) != NULL) {
1112 sk_close(pc->connsock);
1117 sk_set_frozen(pc->connsock, 0);
1119 /* FIXME: can we get any useful peer id info? */
1120 plog(pl->logctx, pl->logfn, "%p: new connection", pc);
1125 struct pageant_listen_state *pageant_listener_new(void)
1127 static const struct plug_function_table listener_fn_table = {
1128 NULL, /* no log function, because that's for outgoing connections */
1129 pageant_listen_closing,
1130 NULL, /* no receive function on a listening socket */
1131 NULL, /* no sent function on a listening socket */
1132 pageant_listen_accepting
1135 struct pageant_listen_state *pl = snew(struct pageant_listen_state);
1136 pl->fn = &listener_fn_table;
1139 pl->listensock = NULL;
1143 void pageant_listener_got_socket(struct pageant_listen_state *pl, Socket sock)
1145 pl->listensock = sock;
1148 void pageant_listener_set_logfn(struct pageant_listen_state *pl,
1149 void *logctx, pageant_logfn_t logfn)
1151 pl->logctx = logctx;
1155 void pageant_listener_free(struct pageant_listen_state *pl)
1158 sk_close(pl->listensock);
1162 /* ----------------------------------------------------------------------
1163 * Code to perform agent operations either as a client, or within the
1164 * same process as the running agent.
1167 static tree234 *passphrases = NULL;
1170 * After processing a list of filenames, we want to forget the
1173 void pageant_forget_passphrases(void)
1175 while (count234(passphrases) > 0) {
1176 char *pp = index234(passphrases, 0);
1177 smemclr(pp, strlen(pp));
1178 delpos234(passphrases, 0);
1183 void *pageant_get_keylist1(int *length)
1187 if (!pageant_local) {
1188 unsigned char request[5], *response;
1190 int resplen, retval;
1191 request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES;
1192 PUT_32BIT(request, 1);
1194 retval = agent_query(request, 5, &vresponse, &resplen, NULL, NULL);
1195 assert(retval == 1);
1196 response = vresponse;
1197 if (resplen < 5 || response[4] != SSH1_AGENT_RSA_IDENTITIES_ANSWER) {
1202 ret = snewn(resplen-5, unsigned char);
1203 memcpy(ret, response+5, resplen-5);
1207 *length = resplen-5;
1209 ret = pageant_make_keylist1(length);
1214 void *pageant_get_keylist2(int *length)
1218 if (!pageant_local) {
1219 unsigned char request[5], *response;
1221 int resplen, retval;
1223 request[4] = SSH2_AGENTC_REQUEST_IDENTITIES;
1224 PUT_32BIT(request, 1);
1226 retval = agent_query(request, 5, &vresponse, &resplen, NULL, NULL);
1227 assert(retval == 1);
1228 response = vresponse;
1229 if (resplen < 5 || response[4] != SSH2_AGENT_IDENTITIES_ANSWER) {
1234 ret = snewn(resplen-5, unsigned char);
1235 memcpy(ret, response+5, resplen-5);
1239 *length = resplen-5;
1241 ret = pageant_make_keylist2(length);
1246 int pageant_add_keyfile(Filename *filename, const char *passphrase,
1249 struct RSAKey *rkey = NULL;
1250 struct ssh2_userkey *skey = NULL;
1255 const char *this_passphrase;
1256 const char *error = NULL;
1260 passphrases = newtree234(NULL);
1265 type = key_type(filename);
1266 if (type != SSH_KEYTYPE_SSH1 && type != SSH_KEYTYPE_SSH2) {
1267 *retstr = dupprintf("Couldn't load this key (%s)",
1268 key_type_to_str(type));
1269 return PAGEANT_ACTION_FAILURE;
1273 * See if the key is already loaded (in the primary Pageant,
1274 * which may or may not be us).
1278 unsigned char *keylist, *p;
1279 int i, nkeys, bloblen, keylistlen;
1281 if (type == SSH_KEYTYPE_SSH1) {
1282 if (!rsakey_pubblob(filename, &blob, &bloblen, NULL, &error)) {
1283 *retstr = dupprintf("Couldn't load private key (%s)", error);
1284 return PAGEANT_ACTION_FAILURE;
1286 keylist = pageant_get_keylist1(&keylistlen);
1288 unsigned char *blob2;
1289 blob = ssh2_userkey_loadpub(filename, NULL, &bloblen,
1292 *retstr = dupprintf("Couldn't load private key (%s)", error);
1293 return PAGEANT_ACTION_FAILURE;
1295 /* For our purposes we want the blob prefixed with its length */
1296 blob2 = snewn(bloblen+4, unsigned char);
1297 PUT_32BIT(blob2, bloblen);
1298 memcpy(blob2 + 4, blob, bloblen);
1302 keylist = pageant_get_keylist2(&keylistlen);
1305 if (keylistlen < 4) {
1306 *retstr = dupstr("Received broken key list from agent");
1307 return PAGEANT_ACTION_FAILURE;
1309 nkeys = toint(GET_32BIT(keylist));
1311 *retstr = dupstr("Received broken key list from agent");
1312 return PAGEANT_ACTION_FAILURE;
1317 for (i = 0; i < nkeys; i++) {
1318 if (!memcmp(blob, p, bloblen)) {
1319 /* Key is already present; we can now leave. */
1322 return PAGEANT_ACTION_OK;
1324 /* Now skip over public blob */
1325 if (type == SSH_KEYTYPE_SSH1) {
1326 int n = rsa_public_blob_len(p, keylistlen);
1328 *retstr = dupstr("Received broken key list from agent");
1329 return PAGEANT_ACTION_FAILURE;
1335 if (keylistlen < 4) {
1336 *retstr = dupstr("Received broken key list from agent");
1337 return PAGEANT_ACTION_FAILURE;
1339 n = toint(4 + GET_32BIT(p));
1340 if (n < 0 || keylistlen < n) {
1341 *retstr = dupstr("Received broken key list from agent");
1342 return PAGEANT_ACTION_FAILURE;
1347 /* Now skip over comment field */
1350 if (keylistlen < 4) {
1351 *retstr = dupstr("Received broken key list from agent");
1352 return PAGEANT_ACTION_FAILURE;
1354 n = toint(4 + GET_32BIT(p));
1355 if (n < 0 || keylistlen < n) {
1356 *retstr = dupstr("Received broken key list from agent");
1357 return PAGEANT_ACTION_FAILURE;
1371 if (type == SSH_KEYTYPE_SSH1)
1372 needs_pass = rsakey_encrypted(filename, &comment);
1374 needs_pass = ssh2_userkey_encrypted(filename, &comment);
1376 if (type == SSH_KEYTYPE_SSH1)
1377 rkey = snew(struct RSAKey);
1380 * Loop round repeatedly trying to load the key, until we either
1381 * succeed, fail for some serious reason, or run out of
1382 * passphrases to try.
1388 * If we've been given a passphrase on input, try using
1389 * it. Otherwise, try one from our tree234 of previously
1390 * useful passphrases.
1393 this_passphrase = (attempts == 0 ? passphrase : NULL);
1395 this_passphrase = (const char *)index234(passphrases, attempts);
1398 if (!this_passphrase) {
1400 * Run out of passphrases to try.
1403 return PAGEANT_ACTION_NEED_PP;
1406 this_passphrase = "";
1408 if (type == SSH_KEYTYPE_SSH1)
1409 ret = loadrsakey(filename, rkey, this_passphrase, &error);
1411 skey = ssh2_load_userkey(filename, this_passphrase, &error);
1412 if (skey == SSH2_WRONG_PASSPHRASE)
1422 * Failed to load the key file, for some reason other than
1425 *retstr = dupstr(error);
1426 return PAGEANT_ACTION_FAILURE;
1427 } else if (ret == 1) {
1429 * Successfully loaded the key file.
1434 * Passphrase wasn't right; go round again.
1441 * If we get here, we've succesfully loaded the key into
1442 * rkey/skey, but not yet added it to the agent.
1446 * If the key was successfully decrypted, save the passphrase for
1447 * use with other keys we try to load.
1450 char *pp_copy = dupstr(this_passphrase);
1451 if (addpos234(passphrases, pp_copy, 0) != pp_copy) {
1452 /* No need; it was already there. */
1453 smemclr(pp_copy, strlen(pp_copy));
1461 if (type == SSH_KEYTYPE_SSH1) {
1462 if (!pageant_local) {
1463 unsigned char *request, *response;
1465 int reqlen, clen, resplen, ret;
1467 clen = strlen(rkey->comment);
1469 reqlen = 4 + 1 + /* length, message type */
1471 ssh1_bignum_length(rkey->modulus) +
1472 ssh1_bignum_length(rkey->exponent) +
1473 ssh1_bignum_length(rkey->private_exponent) +
1474 ssh1_bignum_length(rkey->iqmp) +
1475 ssh1_bignum_length(rkey->p) +
1476 ssh1_bignum_length(rkey->q) + 4 + clen /* comment */
1479 request = snewn(reqlen, unsigned char);
1481 request[4] = SSH1_AGENTC_ADD_RSA_IDENTITY;
1483 PUT_32BIT(request + reqlen, bignum_bitcount(rkey->modulus));
1485 reqlen += ssh1_write_bignum(request + reqlen, rkey->modulus);
1486 reqlen += ssh1_write_bignum(request + reqlen, rkey->exponent);
1488 ssh1_write_bignum(request + reqlen,
1489 rkey->private_exponent);
1490 reqlen += ssh1_write_bignum(request + reqlen, rkey->iqmp);
1491 reqlen += ssh1_write_bignum(request + reqlen, rkey->p);
1492 reqlen += ssh1_write_bignum(request + reqlen, rkey->q);
1493 PUT_32BIT(request + reqlen, clen);
1494 memcpy(request + reqlen + 4, rkey->comment, clen);
1496 PUT_32BIT(request, reqlen - 4);
1498 ret = agent_query(request, reqlen, &vresponse, &resplen,
1501 response = vresponse;
1502 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS) {
1503 *retstr = dupstr("The already running Pageant "
1504 "refused to add the key.");
1505 return PAGEANT_ACTION_FAILURE;
1510 if (!pageant_add_ssh1_key(rkey)) {
1511 sfree(rkey); /* already present, don't waste RAM */
1515 if (!pageant_local) {
1516 unsigned char *request, *response;
1518 int reqlen, alglen, clen, keybloblen, resplen, ret;
1519 alglen = strlen(skey->alg->name);
1520 clen = strlen(skey->comment);
1522 keybloblen = skey->alg->openssh_fmtkey(skey->data, NULL, 0);
1524 reqlen = 4 + 1 + /* length, message type */
1525 4 + alglen + /* algorithm name */
1526 keybloblen + /* key data */
1527 4 + clen /* comment */
1530 request = snewn(reqlen, unsigned char);
1532 request[4] = SSH2_AGENTC_ADD_IDENTITY;
1534 PUT_32BIT(request + reqlen, alglen);
1536 memcpy(request + reqlen, skey->alg->name, alglen);
1538 reqlen += skey->alg->openssh_fmtkey(skey->data,
1541 PUT_32BIT(request + reqlen, clen);
1542 memcpy(request + reqlen + 4, skey->comment, clen);
1544 PUT_32BIT(request, reqlen - 4);
1546 ret = agent_query(request, reqlen, &vresponse, &resplen,
1549 response = vresponse;
1550 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS) {
1551 *retstr = dupstr("The already running Pageant "
1552 "refused to add the key.");
1553 return PAGEANT_ACTION_FAILURE;
1559 if (!pageant_add_ssh2_key(skey)) {
1560 skey->alg->freekey(skey->data);
1561 sfree(skey); /* already present, don't waste RAM */
1565 return PAGEANT_ACTION_OK;
1568 int pageant_enum_keys(pageant_key_enum_fn_t callback, void *callback_ctx,
1571 unsigned char *keylist, *p;
1572 int i, nkeys, keylistlen;
1574 struct pageant_pubkey cbkey;
1576 keylist = pageant_get_keylist1(&keylistlen);
1577 if (keylistlen < 4) {
1578 *retstr = dupstr("Received broken SSH-1 key list from agent");
1580 return PAGEANT_ACTION_FAILURE;
1582 nkeys = toint(GET_32BIT(keylist));
1584 *retstr = dupstr("Received broken SSH-1 key list from agent");
1586 return PAGEANT_ACTION_FAILURE;
1591 for (i = 0; i < nkeys; i++) {
1593 char fingerprint[128];
1596 /* public blob and fingerprint */
1597 memset(&rkey, 0, sizeof(rkey));
1598 n = makekey(p, keylistlen, &rkey, NULL, 0);
1599 if (n < 0 || n > keylistlen) {
1601 *retstr = dupstr("Received broken SSH-1 key list from agent");
1603 return PAGEANT_ACTION_FAILURE;
1605 p += n, keylistlen -= n;
1606 rsa_fingerprint(fingerprint, sizeof(fingerprint), &rkey);
1609 if (keylistlen < 4) {
1610 *retstr = dupstr("Received broken SSH-1 key list from agent");
1613 return PAGEANT_ACTION_FAILURE;
1615 n = toint(GET_32BIT(p));
1616 p += 4, keylistlen -= 4;
1617 if (n < 0 || keylistlen < n) {
1618 *retstr = dupstr("Received broken SSH-1 key list from agent");
1621 return PAGEANT_ACTION_FAILURE;
1623 comment = dupprintf("%.*s", (int)n, (const char *)p);
1624 p += n, keylistlen -= n;
1626 cbkey.blob = rsa_public_blob(&rkey, &cbkey.bloblen);
1627 cbkey.comment = comment;
1628 cbkey.ssh_version = 1;
1629 callback(callback_ctx, fingerprint, comment, &cbkey);
1637 if (keylistlen != 0) {
1638 *retstr = dupstr("Received broken SSH-1 key list from agent");
1639 return PAGEANT_ACTION_FAILURE;
1642 keylist = pageant_get_keylist2(&keylistlen);
1643 if (keylistlen < 4) {
1644 *retstr = dupstr("Received broken SSH-2 key list from agent");
1646 return PAGEANT_ACTION_FAILURE;
1648 nkeys = toint(GET_32BIT(keylist));
1650 *retstr = dupstr("Received broken SSH-2 key list from agent");
1652 return PAGEANT_ACTION_FAILURE;
1657 for (i = 0; i < nkeys; i++) {
1662 if (keylistlen < 4) {
1663 *retstr = dupstr("Received broken SSH-2 key list from agent");
1665 return PAGEANT_ACTION_FAILURE;
1667 n = toint(GET_32BIT(p));
1668 p += 4, keylistlen -= 4;
1669 if (n < 0 || keylistlen < n) {
1670 *retstr = dupstr("Received broken SSH-2 key list from agent");
1672 return PAGEANT_ACTION_FAILURE;
1674 fingerprint = ssh2_fingerprint_blob(p, n);
1677 p += n, keylistlen -= n;
1680 if (keylistlen < 4) {
1681 *retstr = dupstr("Received broken SSH-2 key list from agent");
1684 return PAGEANT_ACTION_FAILURE;
1686 n = toint(GET_32BIT(p));
1687 p += 4, keylistlen -= 4;
1688 if (n < 0 || keylistlen < n) {
1689 *retstr = dupstr("Received broken SSH-2 key list from agent");
1692 return PAGEANT_ACTION_FAILURE;
1694 comment = dupprintf("%.*s", (int)n, (const char *)p);
1695 p += n, keylistlen -= n;
1697 cbkey.ssh_version = 2;
1698 cbkey.comment = comment;
1699 callback(callback_ctx, fingerprint, comment, &cbkey);
1706 if (keylistlen != 0) {
1707 *retstr = dupstr("Received broken SSH-1 key list from agent");
1708 return PAGEANT_ACTION_FAILURE;
1711 return PAGEANT_ACTION_OK;
1714 int pageant_delete_key(struct pageant_pubkey *key, char **retstr)
1716 unsigned char *request, *response;
1717 int reqlen, resplen, ret;
1720 if (key->ssh_version == 1) {
1721 reqlen = 5 + key->bloblen;
1722 request = snewn(reqlen, unsigned char);
1723 PUT_32BIT(request, reqlen - 4);
1724 request[4] = SSH1_AGENTC_REMOVE_RSA_IDENTITY;
1725 memcpy(request + 5, key->blob, key->bloblen);
1727 reqlen = 9 + key->bloblen;
1728 request = snewn(reqlen, unsigned char);
1729 PUT_32BIT(request, reqlen - 4);
1730 request[4] = SSH2_AGENTC_REMOVE_IDENTITY;
1731 PUT_32BIT(request + 5, key->bloblen);
1732 memcpy(request + 9, key->blob, key->bloblen);
1735 ret = agent_query(request, reqlen, &vresponse, &resplen, NULL, NULL);
1737 response = vresponse;
1738 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS) {
1739 *retstr = dupstr("Agent failed to delete key");
1740 ret = PAGEANT_ACTION_FAILURE;
1743 ret = PAGEANT_ACTION_OK;
1750 int pageant_delete_all_keys(char **retstr)
1752 unsigned char request[5], *response;
1753 int reqlen, resplen, success, ret;
1756 PUT_32BIT(request, 1);
1757 request[4] = SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
1759 ret = agent_query(request, reqlen, &vresponse, &resplen, NULL, NULL);
1761 response = vresponse;
1762 success = (resplen >= 4 && response[4] == SSH_AGENT_SUCCESS);
1765 *retstr = dupstr("Agent failed to delete SSH-2 keys");
1766 return PAGEANT_ACTION_FAILURE;
1769 PUT_32BIT(request, 1);
1770 request[4] = SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES;
1772 ret = agent_query(request, reqlen, &vresponse, &resplen, NULL, NULL);
1774 response = vresponse;
1775 success = (resplen >= 4 && response[4] == SSH_AGENT_SUCCESS);
1778 *retstr = dupstr("Agent failed to delete SSH-1 keys");
1779 return PAGEANT_ACTION_FAILURE;
1783 return PAGEANT_ACTION_OK;
1786 struct pageant_pubkey *pageant_pubkey_copy(struct pageant_pubkey *key)
1788 struct pageant_pubkey *ret = snew(struct pageant_pubkey);
1789 ret->blob = snewn(key->bloblen, unsigned char);
1790 memcpy(ret->blob, key->blob, key->bloblen);
1791 ret->bloblen = key->bloblen;
1792 ret->comment = key->comment ? dupstr(key->comment) : NULL;
1793 ret->ssh_version = key->ssh_version;
1797 void pageant_pubkey_free(struct pageant_pubkey *key)
1799 sfree(key->comment);