@@ -138,10 +138,10 @@ generate_user_token(wchar_t* user) {
138138}
139139
140140#define AUTH_REQUEST "keyauthenticate"
141- #define MAX_USER_NAME_LEN 255 + 255
141+ #define MAX_USER_NAME_LEN 256
142142
143143int process_authagent_request (struct sshbuf * request , struct sshbuf * response , struct agent_connection * con ) {
144- int r = 0 ;
144+ int r = -1 ;
145145 char * opn , * key_blob , * user , * sig , * blob ;
146146 size_t opn_len , key_blob_len , user_len , sig_len , blob_len ;
147147 struct sshkey * key = NULL ;
@@ -151,36 +151,44 @@ int process_authagent_request(struct sshbuf* request, struct sshbuf* response, s
151151 ULONG client_pid ;
152152
153153 user = NULL ;
154- if ((r = sshbuf_get_string_direct (request , & opn , & opn_len )) != 0 ||
155- (r = sshbuf_get_string_direct (request , & key_blob , & key_blob_len )) != 0 ||
156- (r = sshbuf_get_cstring (request , & user , & user_len )) != 0 ||
157- (r = sshbuf_get_string_direct (request , & sig , & sig_len )) != 0 ||
158- (r = sshbuf_get_string_direct (request , & blob , & blob_len )) != 0 ||
159- (r = sshkey_from_blob (key_blob , key_blob_len , & key )) != 0 )
154+ if (sshbuf_get_string_direct (request , & opn , & opn_len ) != 0 ||
155+ sshbuf_get_string_direct (request , & key_blob , & key_blob_len ) != 0 ||
156+ sshbuf_get_cstring (request , & user , & user_len ) != 0 ||
157+ sshbuf_get_string_direct (request , & sig , & sig_len ) != 0 ||
158+ sshbuf_get_string_direct (request , & blob , & blob_len ) != 0 ||
159+ sshkey_from_blob (key_blob , key_blob_len , & key ) != 0 ||
160+ opn_len != strlen (AUTH_REQUEST ) ||
161+ memcmp (opn , AUTH_REQUEST , opn_len ) != 0 ) {
162+ debug ("auth agent invalid request" );
160163 goto done ;
164+ }
161165
162- if ((opn_len != strlen (AUTH_REQUEST )) || (memcmp (opn , AUTH_REQUEST , opn_len ) != 0 )) {
163- r = EINVAL ;
166+ if (MultiByteToWideChar (CP_UTF8 , 0 , user , user_len + 1 , wuser , MAX_USER_NAME_LEN ) == 0 ||
167+ (token = generate_user_token (wuser )) == 0 ) {
168+ debug ("unable to generate user token" );
164169 goto done ;
165170 }
166171
167- if (0 == MultiByteToWideChar (CP_UTF8 , 0 , user , user_len + 1 , wuser , MAX_USER_NAME_LEN )) {
168- r = GetLastError ();
172+ if (SHGetKnownFolderPath (& FOLDERID_Profile , 0 , token , & wuser_home ) != S_OK ||
173+ pubkey_allowed (key , wuser , wuser_home ) != 1 ) {
174+ debug ("given public key is not mapped to user %ls" , wuser );
169175 goto done ;
170176 }
171177
172- if (key_verify (key , sig , sig_len , blob , blob_len ) != 1 ||
173- (token = generate_user_token (wuser )) == 0 ||
174- SHGetKnownFolderPath (& FOLDERID_Profile , 0 , token , & wuser_home ) != S_OK ||
175- pubkey_allowed (key , wuser , wuser_home ) != 1 ||
176- (FALSE == GetNamedPipeClientProcessId (con -> connection , & client_pid )) ||
178+ if (key_verify (key , sig , sig_len , blob , blob_len ) != 1 ) {
179+ debug ("signature verification failed" );
180+ goto done ;
181+ }
182+
183+ if ((FALSE == GetNamedPipeClientProcessId (con -> connection , & client_pid )) ||
177184 ( (client_proc = OpenProcess (PROCESS_DUP_HANDLE , FALSE, client_pid )) == NULL ) ||
178185 (FALSE == DuplicateHandle (GetCurrentProcess (), token , client_proc , & dup_token , TOKEN_QUERY | TOKEN_IMPERSONATE , FALSE, DUPLICATE_SAME_ACCESS )) ||
179186 (sshbuf_put_u32 (response , dup_token ) != 0 ) ) {
180- r = EINVAL ;
187+ debug ( "failed to authorize user" ) ;
181188 goto done ;
182189 }
183190
191+ r = 0 ;
184192done :
185193 if (user )
186194 free (user );
0 commit comments