+ ret = snewn(resplen-5, unsigned char);
+ memcpy(ret, response+5, resplen-5);
+ sfree(response);
+
+ if (length)
+ *length = resplen-5;
+ } else {
+ ret = pageant_make_keylist1(length);
+ }
+ return ret;
+}
+
+void *pageant_get_keylist2(int *length)
+{
+ void *ret;
+
+ if (!pageant_local) {
+ unsigned char request[5], *response;
+ void *vresponse;
+ int resplen, retval;
+
+ request[4] = SSH2_AGENTC_REQUEST_IDENTITIES;
+ PUT_32BIT(request, 1);
+
+ retval = agent_query(request, 5, &vresponse, &resplen, NULL, NULL);
+ assert(retval == 1);
+ response = vresponse;
+ if (resplen < 5 || response[4] != SSH2_AGENT_IDENTITIES_ANSWER) {
+ sfree(response);
+ return NULL;
+ }
+
+ ret = snewn(resplen-5, unsigned char);
+ memcpy(ret, response+5, resplen-5);
+ sfree(response);
+
+ if (length)
+ *length = resplen-5;
+ } else {
+ ret = pageant_make_keylist2(length);
+ }
+ return ret;
+}
+
+int pageant_add_keyfile(Filename *filename, const char *passphrase,
+ char **retstr)
+{
+ struct RSAKey *rkey = NULL;
+ struct ssh2_userkey *skey = NULL;
+ int needs_pass;
+ int ret;
+ int attempts;
+ char *comment;
+ const char *this_passphrase;
+ const char *error = NULL;
+ int type;
+
+ if (!passphrases) {
+ passphrases = newtree234(NULL);
+ }
+
+ *retstr = NULL;
+
+ type = key_type(filename);
+ if (type != SSH_KEYTYPE_SSH1 && type != SSH_KEYTYPE_SSH2) {
+ *retstr = dupprintf("Couldn't load this key (%s)",
+ key_type_to_str(type));
+ return PAGEANT_ACTION_FAILURE;
+ }
+
+ /*
+ * See if the key is already loaded (in the primary Pageant,
+ * which may or may not be us).
+ */
+ {
+ void *blob;
+ unsigned char *keylist, *p;
+ int i, nkeys, bloblen, keylistlen;
+
+ if (type == SSH_KEYTYPE_SSH1) {
+ if (!rsakey_pubblob(filename, &blob, &bloblen, NULL, &error)) {
+ *retstr = dupprintf("Couldn't load private key (%s)", error);
+ return PAGEANT_ACTION_FAILURE;
+ }
+ keylist = pageant_get_keylist1(&keylistlen);
+ } else {
+ unsigned char *blob2;
+ blob = ssh2_userkey_loadpub(filename, NULL, &bloblen,
+ NULL, &error);
+ if (!blob) {
+ *retstr = dupprintf("Couldn't load private key (%s)", error);
+ return PAGEANT_ACTION_FAILURE;
+ }
+ /* For our purposes we want the blob prefixed with its length */
+ blob2 = snewn(bloblen+4, unsigned char);
+ PUT_32BIT(blob2, bloblen);
+ memcpy(blob2 + 4, blob, bloblen);
+ sfree(blob);
+ blob = blob2;
+
+ keylist = pageant_get_keylist2(&keylistlen);