Skip to content

Commit fcc3bdc

Browse files
committed
5-11 C4
1 parent 558419f commit fcc3bdc

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

contrib/win32/win32compat/ssh-agent/keyagent-request.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,56 @@ process_sign_request(struct sshbuf* request, struct sshbuf* response, struct age
254254
return r;
255255
}
256256

257+
static int
258+
process_remove_key(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
259+
HKEY user_root = 0, root = 0;
260+
char *blob, *thumbprint = NULL;
261+
size_t blen;
262+
int r = 0, success = 0, request_invalid = 0;
263+
struct sshkey *key = NULL;
264+
265+
if (sshbuf_get_string_direct(request, &blob, &blen) != 0 ||
266+
sshkey_from_blob(blob, blen, &key) != 0) {
267+
request_invalid = 1;
268+
goto done;
269+
}
270+
271+
if ((thumbprint = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT, SSH_FP_DEFAULT)) == NULL ||
272+
get_user_root(con, &user_root) != 0 ||
273+
RegOpenKeyExW(user_root, SSH_KEYS_ROOT, 0,
274+
DELETE | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE | KEY_WOW64_64KEY, &root) != 0 ||
275+
RegDeleteTreeA(root, thumbprint) != 0)
276+
goto done;
277+
success = 1;
278+
done:
279+
r = 0;
280+
if (request_invalid)
281+
r = -1;
282+
else if (sshbuf_put_u8(response, success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE) != 0)
283+
r = -1;
284+
285+
if (key)
286+
sshkey_free(key);
287+
if (user_root)
288+
RegCloseKey(user_root);
289+
if (root)
290+
RegCloseKey(root);
291+
if (thumbprint)
292+
free(thumbprint);
293+
return r;
294+
}
257295
static int
258296
process_remove_all(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
259297
HKEY user_root = 0, root = 0;
260298
int r = 0;
261299

262300
if (get_user_root(con, &user_root) != 0 ||
263-
RegOpenKeyExW(user_root, SSH_ROOT, 0, STANDARD_RIGHTS_READ | KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY, &root) != 0) {
301+
RegOpenKeyExW(user_root, SSH_ROOT, 0,
302+
DELETE | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE | KEY_WOW64_64KEY, &root) != 0) {
264303
goto done;
265304
}
266305

267-
RegDeleteKeyExW(root, SSH_KEYS_KEY, KEY_WOW64_64KEY, 0);
306+
RegDeleteTreeW(root, SSH_KEYS_KEY);
268307
done:
269308
r = 0;
270309
if (sshbuf_put_u8(response, SSH_AGENT_SUCCESS) != 0)
@@ -372,7 +411,7 @@ int process_keyagent_request(struct sshbuf* request, struct sshbuf* response, st
372411
case SSH2_AGENTC_SIGN_REQUEST:
373412
return process_sign_request(request, response, con);
374413
case SSH2_AGENTC_REMOVE_IDENTITY:
375-
414+
return process_remove_key(request, response, con);
376415
case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
377416
return process_remove_all(request, response, con);
378417
default:

0 commit comments

Comments
 (0)