+/*
+ * Create an SSH1 key list in a malloc'ed buffer; return its
+ * length.
+ */
+static void *make_keylist1(int *length)
+{
+ int i, nkeys, len;
+ struct RSAKey *key;
+ unsigned char *blob, *p, *ret;
+ int bloblen;
+
+ /*
+ * Count up the number and length of keys we hold.
+ */
+ len = 4;
+ nkeys = 0;
+ for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
+ nkeys++;
+ blob = rsa_public_blob(key, &bloblen);
+ len += bloblen;
+ sfree(blob);
+ len += 4 + strlen(key->comment);
+ }
+
+ /* Allocate the buffer. */
+ p = ret = smalloc(len);
+ if (length) *length = len;
+
+ PUT_32BIT(p, nkeys);
+ p += 4;
+ for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
+ blob = rsa_public_blob(key, &bloblen);
+ memcpy(p, blob, bloblen);
+ p += bloblen;
+ sfree(blob);
+ PUT_32BIT(p, strlen(key->comment));
+ memcpy(p + 4, key->comment, strlen(key->comment));
+ p += 4 + strlen(key->comment);
+ }
+
+ assert(p - ret == len);
+ return ret;
+}
+
+/*
+ * Create an SSH2 key list in a malloc'ed buffer; return its
+ * length.
+ */
+static void *make_keylist2(int *length)
+{
+ struct ssh2_userkey *key;
+ int i, len, nkeys;
+ unsigned char *blob, *p, *ret;
+ int bloblen;
+
+ /*
+ * Count up the number and length of keys we hold.
+ */
+ len = 4;
+ nkeys = 0;
+ for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
+ nkeys++;
+ len += 4; /* length field */
+ blob = key->alg->public_blob(key->data, &bloblen);
+ len += bloblen;
+ sfree(blob);
+ len += 4 + strlen(key->comment);
+ }
+
+ /* Allocate the buffer. */
+ p = ret = smalloc(len);
+ if (length) *length = len;
+
+ /*
+ * Packet header is the obvious five bytes, plus four
+ * bytes for the key count.
+ */
+ PUT_32BIT(p, nkeys);
+ p += 4;
+ for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
+ blob = key->alg->public_blob(key->data, &bloblen);
+ PUT_32BIT(p, bloblen);
+ p += 4;
+ memcpy(p, blob, bloblen);
+ p += bloblen;
+ sfree(blob);
+ PUT_32BIT(p, strlen(key->comment));
+ memcpy(p + 4, key->comment, strlen(key->comment));
+ p += 4 + strlen(key->comment);
+ }
+
+ assert(p - ret == len);
+ return ret;
+}
+
+/*
+ * Acquire a keylist1 from the primary Pageant; this means either
+ * calling make_keylist1 (if that's us) or sending a message to the
+ * primary Pageant (if it's not).
+ */
+static void *get_keylist1(void)
+{
+ void *ret;
+
+ if (already_running) {
+ unsigned char request[5], *response;
+ void *vresponse;
+ int resplen;
+ request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES;
+ PUT_32BIT(request, 4);
+
+ agent_query(request, 5, &vresponse, &resplen);
+ response = vresponse;
+ if (resplen < 5 || response[4] != SSH1_AGENT_RSA_IDENTITIES_ANSWER)
+ return NULL;
+
+ ret = smalloc(resplen-5);
+ memcpy(ret, response+5, resplen-5);
+ sfree(response);
+ } else {
+ ret = make_keylist1(NULL);
+ }
+ return ret;
+}
+
+/*
+ * Acquire a keylist2 from the primary Pageant; this means either
+ * calling make_keylist2 (if that's us) or sending a message to the
+ * primary Pageant (if it's not).
+ */
+static void *get_keylist2(void)
+{
+ void *ret;
+
+ if (already_running) {
+ unsigned char request[5], *response;
+ void *vresponse;
+ int resplen;
+
+ request[4] = SSH2_AGENTC_REQUEST_IDENTITIES;
+ PUT_32BIT(request, 4);
+
+ agent_query(request, 5, &vresponse, &resplen);
+ response = vresponse;
+ if (resplen < 5 || response[4] != SSH2_AGENT_IDENTITIES_ANSWER)
+ return NULL;
+
+ ret = smalloc(resplen-5);
+ memcpy(ret, response+5, resplen-5);
+ sfree(response);
+ } else {
+ ret = make_keylist2(NULL);
+ }
+ return ret;
+}
+