@@ -85,7 +85,7 @@ convert_blob(struct agent_connection* con, const char *blob, DWORD blen, char **
8585
8686#define REG_KEY_SDDL L"D:P(A;; GA;;; SY)(A;; GA;;; BA)"
8787
88- int
88+ static int
8989process_add_identity (struct sshbuf * request , struct sshbuf * response , struct agent_connection * con ) {
9090 struct sshkey * key = NULL ;
9191 int r = 0 , blob_len , eblob_len , request_invalid = 0 , success = 0 ;
@@ -114,7 +114,7 @@ process_add_identity(struct sshbuf* request, struct sshbuf* response, struct age
114114 convert_blob (con , blob , blob_len , & eblob , & eblob_len , 1 ) != 0 ||
115115 ((thumbprint = sshkey_fingerprint (key , SSH_FP_HASH_DEFAULT , SSH_FP_DEFAULT )) == NULL ) ||
116116 get_user_root (con , & user_root ) != 0 ||
117- RegCreateKeyExW (user_root , SSHD_KEYS_ROOT , 0 , 0 , 0 , KEY_WRITE | KEY_WOW64_64KEY , & sa , & reg , NULL ) != 0 ||
117+ RegCreateKeyExW (user_root , SSH_KEYS_ROOT , 0 , 0 , 0 , KEY_WRITE | KEY_WOW64_64KEY , & sa , & reg , NULL ) != 0 ||
118118 RegCreateKeyExA (reg , thumbprint , 0 , 0 , 0 , KEY_WRITE | KEY_WOW64_64KEY , & sa , & sub , NULL ) != 0 ||
119119 RegSetValueExW (sub , NULL , 0 , REG_BINARY , eblob , eblob_len ) != 0 ||
120120 RegSetValueExW (sub , L"pub" , 0 , REG_BINARY , pubkey_blob , pubkey_blob_len ) != 0 ||
@@ -171,7 +171,7 @@ static int sign_blob(const struct sshkey *pubkey, u_char ** sig, size_t *siglen,
171171
172172 if ((thumbprint = sshkey_fingerprint (pubkey , SSH_FP_HASH_DEFAULT , SSH_FP_DEFAULT )) == NULL ||
173173 get_user_root (con , & user_root ) != 0 ||
174- RegOpenKeyExW (user_root , SSHD_KEYS_ROOT ,
174+ RegOpenKeyExW (user_root , SSH_KEYS_ROOT ,
175175 0 , STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_WOW64_64KEY | KEY_ENUMERATE_SUB_KEYS , & reg ) != 0 ||
176176 RegOpenKeyExA (reg , thumbprint , 0 ,
177177 STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY , & sub ) != 0 ||
@@ -209,7 +209,7 @@ static int sign_blob(const struct sshkey *pubkey, u_char ** sig, size_t *siglen,
209209 return success ? 0 : -1 ;
210210}
211211
212- int
212+ static int
213213process_sign_request (struct sshbuf * request , struct sshbuf * response , struct agent_connection * con ) {
214214 u_char * blob , * data , * signature = NULL ;
215215 size_t blen , dlen , slen = 0 ;
@@ -254,21 +254,48 @@ process_sign_request(struct sshbuf* request, struct sshbuf* response, struct age
254254 return r ;
255255}
256256
257- int
257+ static int
258+ process_remove_all (struct sshbuf * request , struct sshbuf * response , struct agent_connection * con ) {
259+ HKEY user_root = 0 , root = 0 ;
260+ int r = 0 ;
261+
262+ 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 ) {
264+ goto done ;
265+ }
266+
267+ RegDeleteKeyExW (root , SSH_KEYS_KEY , KEY_WOW64_64KEY , 0 );
268+ done :
269+ r = 0 ;
270+ if (sshbuf_put_u8 (response , SSH_AGENT_SUCCESS ) != 0 )
271+ r = -1 ;
272+
273+ if (user_root )
274+ RegCloseKey (user_root );
275+ if (root )
276+ RegCloseKey (root );
277+ return r ;
278+ }
279+
280+ static int
258281process_request_identities (struct sshbuf * request , struct sshbuf * response , struct agent_connection * con ) {
259- int count = 0 , index = 0 , success = 0 ;
282+ int count = 0 , index = 0 , success = 0 , r = 0 ;
260283 HKEY root = NULL , sub = NULL , user_root = 0 ;
261284 char * count_ptr = NULL ;
262285 wchar_t sub_name [MAX_KEY_LENGTH ];
263286 DWORD sub_name_len = MAX_KEY_LENGTH ;
264- char * regdata = NULL ;
265- DWORD regdatalen = 0 , key_count = 0 ;
287+ char * pkblob = NULL , * comment = NULL ;
288+ DWORD regdatalen = 0 , commentlen = 0 , key_count = 0 ;
266289 struct sshbuf * identities ;
267290
268- if ((identities = sshbuf_new ()) == NULL ||
269- get_user_root (con , & user_root ) != 0 ||
270- RegOpenKeyExW (user_root , SSHD_KEYS_ROOT , 0 , STANDARD_RIGHTS_READ | KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY , & root ) != 0 )
291+ if ((identities = sshbuf_new ()) == NULL )
292+ goto done ;
293+
294+ if ( get_user_root (con , & user_root ) != 0 ||
295+ RegOpenKeyExW (user_root , SSH_KEYS_ROOT , 0 , STANDARD_RIGHTS_READ | KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY , & root ) != 0 ) {
296+ success = 1 ;
271297 goto done ;
298+ }
272299
273300 while (1 ) {
274301 sub_name_len = MAX_KEY_LENGTH ;
@@ -277,79 +304,48 @@ process_request_identities(struct sshbuf* request, struct sshbuf* response, stru
277304 sub = NULL ;
278305 }
279306 if (RegEnumKeyExW (root , index ++ , sub_name , & sub_name_len , NULL , NULL , NULL , NULL ) == 0 ) {
280- if (RegOpenKeyExW (root , sub_name , 0 , KEY_QUERY_VALUE , & sub ) == 0 ) {
281- if (RegQueryValueExW (sub , L"pub" , 0 , NULL , NULL , & regdatalen ) == 0 ) {
282-
283-
284- if (r == ERROR_MORE_DATA ) {
285- r = 0 ;
286- if (regdata )
287- free (regdata );
288- if ((regdata = malloc (regdatalen )) == NULL ) {
289- r = ENOMEM ;
290- goto done ;
291- }
292- if ((r = RegQueryValueExW (sub , L"pub" , 0 , NULL , regdata , & regdatalen )) != 0 )
293- goto done ;
294-
295- }
296- else {
297- r = EOTHER ;
298- goto done ;
299- }
300- }
301-
302- if ((r = sshbuf_put_string (identities , regdata , regdatalen )) != 0 )
303- goto done ;
304-
305- if ((r = RegQueryValueExW (sub , L"comment" , 0 , NULL , regdata , & regdatalen )) != 0 ) {
306- if (r == ERROR_MORE_DATA ) {
307- r = 0 ;
308- if (regdata )
309- free (regdata );
310- if ((regdata = malloc (regdatalen )) == NULL ) {
311- r = ENOMEM ;
312- goto done ;
313- }
314- if ((r = RegQueryValueExW (sub , L"comment" , 0 , NULL , regdata , & regdatalen )) != 0 )
315- goto done ;
316-
317- }
318- else {
319- r = EOTHER ;
320- goto done ;
321- }
322- }
323- if ((r = sshbuf_put_string (identities , regdata , regdatalen )) != 0 )
307+ if (RegOpenKeyExW (root , sub_name , 0 , KEY_QUERY_VALUE | KEY_WOW64_64KEY , & sub ) == 0 &&
308+ RegQueryValueExW (sub , L"pub" , 0 , NULL , NULL , & regdatalen ) == 0 &&
309+ RegQueryValueExW (sub , L"comment" , 0 , NULL , NULL , & commentlen ) == 0 ) {
310+ if (pkblob )
311+ free (pkblob );
312+ if (comment )
313+ free (comment );
314+ pkblob = NULL ;
315+ comment = NULL ;
316+
317+ if ((pkblob = malloc (regdatalen )) == NULL ||
318+ (comment = malloc (commentlen )) == NULL ||
319+ RegQueryValueExW (sub , L"pub" , 0 , NULL , pkblob , & regdatalen ) != 0 ||
320+ RegQueryValueExW (sub , L"comment" , 0 , NULL , comment , & commentlen ) != 0 ||
321+ sshbuf_put_string (identities , pkblob , regdatalen ) != 0 ||
322+ sshbuf_put_string (identities , comment , commentlen ) != 0 )
324323 goto done ;
324+
325325 key_count ++ ;
326-
327- }
328- else if (r == ERROR_FILE_NOT_FOUND ) {
329- r = 0 ;
330- continue ;
331326 }
332- else
333- goto done ;
334- }
335- else if (r == ERROR_NO_MORE_ITEMS ) {
336- r = 0 ;
337- break ;
338327 }
339328 else
340- goto done ;
329+ break ;
341330
342331 }
343332
344- if (((r = sshbuf_put_u8 (response , SSH2_AGENT_IDENTITIES_ANSWER )) != 0 )
345- || ((r = sshbuf_put_u32 (response , key_count )) != 0 )
346- || ((r = sshbuf_putb (response , identities )) != 0 ))
347- goto done ;
348-
349-
333+ success = 1 ;
350334done :
351- if (regdata )
352- free (regdata );
335+ r = 0 ;
336+ if (success ) {
337+ if (sshbuf_put_u8 (response , SSH2_AGENT_IDENTITIES_ANSWER ) != 0 ||
338+ sshbuf_put_u32 (response , key_count ) != 0 ||
339+ sshbuf_putb (response , identities ) != 0 )
340+ goto done ;
341+ }
342+ else
343+ r = -1 ;
344+
345+ if (pkblob )
346+ free (pkblob );
347+ if (comment )
348+ free (comment );
353349 if (identities )
354350 sshbuf_free (identities );
355351 if (user_root )
@@ -375,6 +371,10 @@ int process_keyagent_request(struct sshbuf* request, struct sshbuf* response, st
375371 return process_request_identities (request , response , con );
376372 case SSH2_AGENTC_SIGN_REQUEST :
377373 return process_sign_request (request , response , con );
374+ case SSH2_AGENTC_REMOVE_IDENTITY :
375+
376+ case SSH2_AGENTC_REMOVE_ALL_IDENTITIES :
377+ return process_remove_all (request , response , con );
378378 default :
379379 debug ("unknown key agent request %d" , type );
380380 return EINVAL ;
0 commit comments