Skip to content

Commit 1627b8c

Browse files
committed
5-3 C1
1 parent abea018 commit 1627b8c

File tree

7 files changed

+87
-64
lines changed

7 files changed

+87
-64
lines changed

authfd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ ssh_get_authentication_socket(int *fdp)
9595
*fdp = -1;
9696

9797
#ifdef WIN32_FIXME
98-
#define SSH_AGENT_ROOT "SYSTEM\\CurrentControlSet\\Control\\SSH\\agent"
98+
#define SSH_AGENT_ROOT "SOFTWARE\\SSH\\Agent"
9999
HKEY agent_root = 0;
100100
DWORD agent_pid = 0, tmp_size = 4, pipe_server_pid = 0xff;
101101
RegOpenKeyEx(HKEY_LOCAL_MACHINE, SSH_AGENT_ROOT, 0, KEY_QUERY_VALUE, &agent_root);

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

Lines changed: 71 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,27 @@
3535
#define MAX_KEY_LENGTH 255
3636
#define MAX_VALUE_NAME 16383
3737

38+
static int
39+
get_user_root(struct agent_connection* con, HKEY *root){
40+
int r = 0;
41+
if (ImpersonateNamedPipeClient(con->connection) == FALSE)
42+
return ERROR_INTERNAL_ERROR;
43+
44+
r = RegOpenCurrentUser(KEY_ALL_ACCESS, root);
45+
46+
RevertToSelf();
47+
return r;
48+
}
49+
3850
int
39-
process_add_identity(struct sshbuf* request, struct sshbuf* response, HANDLE client) {
51+
process_add_identity(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
4052
struct sshkey* key = NULL;
4153
int r = 0, r1 = 0, blob_len;
4254
size_t comment_len, pubkey_blob_len;
4355
u_char *pubkey_blob = NULL;
44-
char *thumbprint = NULL, *blob, *comment;
45-
HKEY reg = 0, sub = 0;
56+
char *thumbprint = NULL, *comment;
57+
const char *blob;
58+
HKEY reg = 0, sub = 0, user_root = 0;
4659

4760
blob = sshbuf_ptr(request);
4861
if ((r = sshkey_private_deserialize(request, &key)) != 0)
@@ -53,39 +66,29 @@ process_add_identity(struct sshbuf* request, struct sshbuf* response, HANDLE cli
5366
goto done;
5467
}
5568

56-
if ((r = sshbuf_peek_string_direct(request, &comment, &comment_len)) != 0)
57-
goto done;
58-
59-
if ((thumbprint = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT, SSH_FP_DEFAULT)) == NULL)
60-
goto done;
61-
62-
if ((r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SSHD_HOST_KEYS_ROOT,
63-
0, KEY_WRITE, &reg)) != 0)
64-
goto done;
65-
66-
if ((r = RegCreateKeyExA(reg, thumbprint, 0, 0, 0, KEY_WRITE, NULL, &sub, NULL)) != 0)
67-
goto done;
68-
69-
if ((r = RegSetValueEx(sub, NULL, 0, REG_BINARY, blob, blob_len)) != 0)
70-
goto done;
71-
72-
if ((r = RegSetValueEx(sub, L"pub", 0, REG_BINARY, pubkey_blob, pubkey_blob_len)) != 0)
73-
goto done;
74-
75-
if ((r = RegSetValueEx(sub, L"type", 0, REG_DWORD, &key->type, 4)) != 0)
76-
goto done;
77-
78-
if ((r = RegSetValueEx(sub, L"comment", 0, REG_BINARY, comment, comment_len)) != 0)
69+
if (((r = sshbuf_peek_string_direct(request, &comment, &comment_len)) != 0) ||
70+
((thumbprint = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT, SSH_FP_DEFAULT)) == NULL) ||
71+
((r = get_user_root(con, &user_root)) != 0) ||
72+
((r = RegCreateKeyExW(user_root, SSHD_KEYS_ROOT, 0, 0, 0, KEY_WRITE, NULL, &reg, NULL)) != 0) ||
73+
((r = RegCreateKeyExA(reg, thumbprint, 0, 0, 0, KEY_WRITE, NULL, &sub, NULL)) != 0) ||
74+
((r = RegSetValueExW(sub, NULL, 0, REG_BINARY, blob, blob_len)) != 0) ||
75+
((r = RegSetValueExW(sub, L"pub", 0, REG_BINARY, pubkey_blob, pubkey_blob_len)) != 0) ||
76+
((r = RegSetValueExW(sub, L"type", 0, REG_DWORD, (BYTE*)&key->type, 4)) != 0) ||
77+
((r = RegSetValueExW(sub, L"comment", 0, REG_BINARY, comment, comment_len)) != 0) )
7978
goto done;
8079

8180
done:
8281

82+
/* TODO if r failed the delete reg entries*/
83+
8384
r1 = sshbuf_put_u8(response, (r==0) ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
8485

8586
if (key)
8687
sshkey_free(key);
8788
if (thumbprint)
8889
free(thumbprint);
90+
if (user_root)
91+
RegCloseKey(user_root);
8992
if (reg)
9093
RegCloseKey(reg);
9194
if (sub)
@@ -96,8 +99,8 @@ process_add_identity(struct sshbuf* request, struct sshbuf* response, HANDLE cli
9699
}
97100

98101
static int sign_blob(const struct sshkey *pubkey, u_char ** sig, size_t *siglen,
99-
const u_char *blob, size_t blen, u_int flags) {
100-
HKEY reg = 0, sub = 0;
102+
const u_char *blob, size_t blen, u_int flags, struct agent_connection* con) {
103+
HKEY reg = 0, sub = 0, user_root = 0;
101104
int r = 0;
102105
struct sshkey* prikey = NULL;
103106
char *thumbprint = NULL, *regdata = NULL;
@@ -113,15 +116,18 @@ static int sign_blob(const struct sshkey *pubkey, u_char ** sig, size_t *siglen,
113116
if ((thumbprint = sshkey_fingerprint(pubkey, SSH_FP_HASH_DEFAULT, SSH_FP_DEFAULT)) == NULL)
114117
goto done;
115118

116-
if ((r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SSHD_HOST_KEYS_ROOT,
119+
if ((r = get_user_root(con, &user_root)) != 0)
120+
goto done;
121+
122+
if ((r = RegOpenKeyExW(user_root, SSHD_KEYS_ROOT,
117123
0, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &reg)) != 0)
118124
goto done;
119125

120-
if ((r = RegOpenKeyEx(reg, thumbprint, 0, 0, 0, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS,
121-
NULL, &sub, NULL)) != 0)
126+
if ((r = RegOpenKeyExA(reg, thumbprint, 0, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS,
127+
&sub)) != 0)
122128
goto done;
123129

124-
if ((RegQueryValueEx(sub, NULL, 0, NULL, regdata, &regdatalen)) != ERROR_MORE_DATA) {
130+
if ((RegQueryValueExW(sub, NULL, 0, NULL, regdata, &regdatalen)) != ERROR_MORE_DATA) {
125131
r = EOTHER;
126132
goto done;
127133
}
@@ -131,7 +137,7 @@ static int sign_blob(const struct sshkey *pubkey, u_char ** sig, size_t *siglen,
131137
goto done;
132138
}
133139

134-
if ((r = RegQueryValueEx(sub, NULL, 0, NULL, regdata, &regdatalen)) != 0)
140+
if ((r = RegQueryValueExW(sub, NULL, 0, NULL, regdata, &regdatalen)) != 0)
135141
goto done;
136142

137143
if ((tmpbuf = sshbuf_from(regdata, regdatalen)) == NULL) {
@@ -152,6 +158,8 @@ static int sign_blob(const struct sshkey *pubkey, u_char ** sig, size_t *siglen,
152158
sshkey_free(prikey);
153159
if (thumbprint)
154160
free(thumbprint);
161+
if (user_root)
162+
RegCloseKey(user_root);
155163
if (reg)
156164
RegCloseKey(reg);
157165
if (sub)
@@ -161,7 +169,7 @@ static int sign_blob(const struct sshkey *pubkey, u_char ** sig, size_t *siglen,
161169
}
162170

163171
int
164-
process_sign_request(struct sshbuf* request, struct sshbuf* response, HANDLE client) {
172+
process_sign_request(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
165173
u_char *blob, *data, *signature = NULL;
166174
size_t blen, dlen, slen = 0;
167175
u_int flags = 0;
@@ -177,7 +185,7 @@ process_sign_request(struct sshbuf* request, struct sshbuf* response, HANDLE cli
177185

178186
if (((r = sshkey_from_blob(blob, blen, &key)) != 0)
179187
|| ((r = sign_blob(key, &signature, &slen,
180-
data, dlen, 0)) != 0))
188+
data, dlen, 0, con)) != 0))
181189
goto done;
182190

183191
done:
@@ -196,24 +204,29 @@ process_sign_request(struct sshbuf* request, struct sshbuf* response, HANDLE cli
196204
}
197205

198206
int
199-
process_request_identities(struct sshbuf* request, struct sshbuf* response, HANDLE client) {
207+
process_request_identities(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
200208
int r, r1, count = 0, index = 0;
201-
HKEY root = NULL, sub = NULL;
209+
HKEY root = NULL, sub = NULL, user_root = 0;
202210
char* count_ptr = NULL;
203211
wchar_t sub_name[MAX_KEY_LENGTH];
204212
DWORD sub_name_len = MAX_KEY_LENGTH;
205213
char *regdata = NULL;
206214
DWORD regdatalen = 0, key_count = 0;
215+
struct sshbuf* identities;
207216

208217
regdata = malloc(4);
209218
regdatalen = 4;
210219

211-
if ((r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SSHD_HOST_KEYS_ROOT,
212-
0, STANDARD_RIGHTS_READ | KEY_ENUMERATE_SUB_KEYS, &root)) != 0)
220+
identities = sshbuf_new();
221+
222+
if ((identities == NULL) || (regdata == NULL))
213223
goto done;
214224

215-
if (((r = sshbuf_put_u8(response, SSH2_AGENT_IDENTITIES_ANSWER)) != 0)
216-
|| ((r = sshbuf_reserve(response, 4, &count_ptr)) != 0))
225+
if ((r = get_user_root(con, &user_root)) != 0)
226+
goto done;
227+
228+
if ((r = RegOpenKeyExW(user_root, SSHD_KEYS_ROOT,
229+
0, STANDARD_RIGHTS_READ | KEY_ENUMERATE_SUB_KEYS, &root)) != 0)
217230
goto done;
218231

219232
while (1) {
@@ -222,9 +235,9 @@ process_request_identities(struct sshbuf* request, struct sshbuf* response, HAND
222235
RegCloseKey(sub);
223236
sub = NULL;
224237
}
225-
if ((r = RegEnumKeyEx(root, index++, sub_name, &sub_name_len, NULL, NULL, NULL, NULL)) == 0) {
226-
if ((r = RegOpenKeyEx(root, sub_name, 0, KEY_QUERY_VALUE, &sub)) == 0) {
227-
if ((r = RegQueryValueEx(sub, L"pub", 0, NULL, regdata, &regdatalen)) != 0) {
238+
if ((r = RegEnumKeyExW(root, index++, sub_name, &sub_name_len, NULL, NULL, NULL, NULL)) == 0) {
239+
if ((r = RegOpenKeyExW(root, sub_name, 0, KEY_QUERY_VALUE, &sub)) == 0) {
240+
if ((r = RegQueryValueExW(sub, L"pub", 0, NULL, regdata, &regdatalen)) != 0) {
228241
if (r == ERROR_MORE_DATA) {
229242
r = 0;
230243
if (regdata)
@@ -233,7 +246,7 @@ process_request_identities(struct sshbuf* request, struct sshbuf* response, HAND
233246
r = ENOMEM;
234247
goto done;
235248
}
236-
if ((r = RegQueryValueEx(sub, L"pub", 0, NULL, regdata, &regdatalen)) != 0)
249+
if ((r = RegQueryValueExW(sub, L"pub", 0, NULL, regdata, &regdatalen)) != 0)
237250
goto done;
238251

239252
}
@@ -243,10 +256,10 @@ process_request_identities(struct sshbuf* request, struct sshbuf* response, HAND
243256
}
244257
}
245258

246-
if ((r = sshbuf_put_string(response, regdata, regdatalen)) != 0)
259+
if ((r = sshbuf_put_string(identities, regdata, regdatalen)) != 0)
247260
goto done;
248261

249-
if ((r = RegQueryValueEx(sub, L"comment", 0, NULL, regdata, &regdatalen)) != 0) {
262+
if ((r = RegQueryValueExW(sub, L"comment", 0, NULL, regdata, &regdatalen)) != 0) {
250263
if (r == ERROR_MORE_DATA) {
251264
r = 0;
252265
if (regdata)
@@ -255,7 +268,7 @@ process_request_identities(struct sshbuf* request, struct sshbuf* response, HAND
255268
r = ENOMEM;
256269
goto done;
257270
}
258-
if ((r = RegQueryValueEx(sub, L"comment", 0, NULL, regdata, &regdatalen)) != 0)
271+
if ((r = RegQueryValueExW(sub, L"comment", 0, NULL, regdata, &regdatalen)) != 0)
259272
goto done;
260273

261274
}
@@ -264,7 +277,7 @@ process_request_identities(struct sshbuf* request, struct sshbuf* response, HAND
264277
goto done;
265278
}
266279
}
267-
if ((r = sshbuf_put_string(response, regdata, regdatalen)) != 0)
280+
if ((r = sshbuf_put_string(identities, regdata, regdatalen)) != 0)
268281
goto done;
269282
key_count++;
270283

@@ -285,11 +298,19 @@ process_request_identities(struct sshbuf* request, struct sshbuf* response, HAND
285298

286299
}
287300

288-
POKE_U32(count_ptr, key_count);
301+
if (((r = sshbuf_put_u8(response, SSH2_AGENT_IDENTITIES_ANSWER)) != 0)
302+
|| ((r = sshbuf_put_u32(response, key_count)) != 0)
303+
|| ((r = sshbuf_putb(response, identities)) != 0))
304+
goto done;
305+
289306

290307
done:
291308
if (regdata)
292309
free(regdata);
310+
if (identities)
311+
sshbuf_free(identities);
312+
if (user_root)
313+
RegCloseKey(user_root);
293314
if (root)
294315
RegCloseKey(root);
295316
if (sub)

contrib/win32/win32compat/ssh-agent/agent-request.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ typedef unsigned __int64 u_int64_t;
1111
#include "digest.h"
1212

1313

14-
int process_add_identity(struct sshbuf*, struct sshbuf*, HANDLE);
14+
int process_add_identity(struct sshbuf*, struct sshbuf*, struct agent_connection*);
15+
int process_request_identities(struct sshbuf*, struct sshbuf*, struct agent_connection*);
16+
int process_sign_request(struct sshbuf*, struct sshbuf*, struct agent_connection*);

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void agent_sm_process_action_queue() {
6565
memset(&sa, 0, sizeof(sa));
6666
sa.bInheritHandle = FALSE;
6767
sa.lpSecurityDescriptor = NULL;
68-
h = CreateNamedPipe(
68+
h = CreateNamedPipeW(
6969
AGENT_PIPE_ID, // pipe name
7070
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, // read/write access
7171
PIPE_TYPE_BYTE | // message type pipe
@@ -165,9 +165,8 @@ int agent_start() {
165165
QueueUserWorkItem(iocp_work, NULL, 0);
166166

167167
agent_listen();
168-
RegOpenKeyEx(HKEY_LOCAL_MACHINE, SSH_AGENT_ROOT,
169-
0, KEY_SET_VALUE, &agent_root);
170-
RegSetValueEx(agent_root, L"ProcessID", 0, REG_DWORD, &process_id, 4);
168+
RegCreateKeyExW(HKEY_LOCAL_MACHINE, SSH_AGENT_ROOT, 0, 0, 0, KEY_WRITE, 0, &agent_root, 0);
169+
RegSetValueExW(agent_root, L"ProcessID", 0, REG_DWORD, (BYTE*)&process_id, 4);
171170
iocp_work(NULL);
172171
return 1;
173172
}

contrib/win32/win32compat/ssh-agent/agent.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
#include <stdio.h>
33
#define MAX_MESSAGE_SIZE 5 * 1024
44

5-
#define SSH_ROOT L"SYSTEM\\CurrentControlSet\\Control\\SSH"
6-
#define SSH_AGENT_ROOT SSH_ROOT L"\\agent"
7-
#define SSHD_HOST_KEYS_ROOT SSH_ROOT L"\\Host\\Keys"
5+
#define SSH_ROOT L"SOFTWARE\\SSH"
6+
#define SSH_AGENT_ROOT SSH_ROOT L"\\Agent"
7+
#define SSHD_KEYS_ROOT SSH_ROOT L"\\Keys"
88

99
#define HEADER_SIZE 4
1010
struct agent_connection {
1111
OVERLAPPED ol;
1212
HANDLE connection;
13-
HANDLE client_token;
1413
struct {
1514
DWORD num_bytes;
1615
DWORD transferred;

contrib/win32/win32compat/ssh-agent/connection.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ process_request(struct agent_connection* con) {
133133

134134
switch (type) {
135135
case SSH2_AGENTC_ADD_IDENTITY:
136-
r = process_add_identity(request, response, con->client_token);
136+
r = process_add_identity(request, response, con);
137137
break;
138138
case SSH2_AGENTC_REQUEST_IDENTITIES:
139-
r = process_request_identities(request, response, con->client_token);
139+
r = process_request_identities(request, response, con);
140140
break;
141141
case SSH2_AGENTC_SIGN_REQUEST:
142-
r = process_sign_request(request, response, con->client_token);
142+
r = process_sign_request(request, response, con);
143143
break;
144144
default:
145145
r = EINVAL;

sshd.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,9 @@ main(int ac, char **av)
22422242
error("Could not connect to agent \"%s\": %s",
22432243
options.host_key_agent, ssh_err(r));
22442244
}
2245-
2245+
#ifdef WIN32_FIXME
2246+
have_agent = 1;
2247+
#endif
22462248
for (i = 0; i < options.num_host_key_files; i++) {
22472249
if (options.host_key_files[i] == NULL)
22482250
continue;

0 commit comments

Comments
 (0)