2 * Windows implementation of SSH connection-sharing IPC setup.
8 #define DEFINE_PLUG_METHOD_MACROS
15 #if !defined NO_SECURITY
19 #define CONNSHARE_PIPE_PREFIX "\\\\.\\pipe\\putty-connshare"
20 #define CONNSHARE_MUTEX_PREFIX "Local\\putty-connshare-mutex"
22 static char *obfuscate_name(const char *realname)
25 * Windows's named pipes all live in the same namespace, so one
26 * user can see what pipes another user has open. This is an
27 * undesirable privacy leak and in particular permits one user to
28 * know what username@host another user is SSHing to, so we
29 * protect that information by using CryptProtectMemory (which
30 * uses a key built in to each user's account).
35 unsigned char lenbuf[4];
36 unsigned char digest[32];
40 cryptlen = strlen(realname) + 1;
41 cryptlen += CRYPTPROTECTMEMORY_BLOCK_SIZE - 1;
42 cryptlen /= CRYPTPROTECTMEMORY_BLOCK_SIZE;
43 cryptlen *= CRYPTPROTECTMEMORY_BLOCK_SIZE;
45 cryptdata = snewn(cryptlen, char);
46 memset(cryptdata, 0, cryptlen);
47 strcpy(cryptdata, realname);
50 * CRYPTPROTECTMEMORY_CROSS_PROCESS causes CryptProtectMemory to
51 * use the same key in all processes with this user id, meaning
52 * that the next PuTTY process calling this function with the same
53 * input will get the same data.
55 * (Contrast with CryptProtectData, which invents a new session
56 * key every time since its API permits returning more data than
57 * was input, so calling _that_ and hashing the output would not
60 * We don't worry too much if this doesn't work for some reason.
61 * Omitting this step still has _some_ privacy value (in that
62 * another user can test-hash things to confirm guesses as to
63 * where you might be connecting to, but cannot invert SHA-256 in
64 * the absence of any plausible guess). So we don't abort if we
65 * can't call CryptProtectMemory at all, or if it fails.
68 p_CryptProtectMemory(cryptdata, cryptlen,
69 CRYPTPROTECTMEMORY_CROSS_PROCESS);
72 * We don't want to give away the length of the hostname either,
73 * so having got it back out of CryptProtectMemory we now hash it.
76 PUT_32BIT_MSB_FIRST(lenbuf, cryptlen);
77 SHA256_Bytes(&sha, lenbuf, 4);
78 SHA256_Bytes(&sha, cryptdata, cryptlen);
79 SHA256_Final(&sha, digest);
84 * Finally, make printable.
86 for (i = 0; i < 32; i++) {
87 sprintf(retbuf + 2*i, "%02x", digest[i]);
88 /* the last of those will also write the trailing NUL */
91 return dupstr(retbuf);
94 static char *make_name(const char *prefix, const char *name)
96 char *username, *retname;
98 username = get_username();
99 retname = dupprintf("%s.%s.%s", prefix, username, name);
105 Socket new_named_pipe_client(const char *pipename, Plug plug);
106 Socket new_named_pipe_listener(const char *pipename, Plug plug);
108 int platform_ssh_share(const char *pi_name, Conf *conf,
109 Plug downplug, Plug upplug, Socket *sock,
110 char **logtext, char **ds_err, char **us_err,
111 int can_upstream, int can_downstream)
113 char *name, *mutexname, *pipename;
116 PSECURITY_DESCRIPTOR psd;
121 * Transform the platform-independent version of the connection
122 * identifier into the obfuscated version we'll use for our
123 * Windows named pipe and mutex. A side effect of doing this is
124 * that it also eliminates any characters illegal in Windows pipe
127 name = obfuscate_name(pi_name);
129 *logtext = dupprintf("Unable to call CryptProtectMemory: %s",
130 win_strerror(GetLastError()));
135 * Make a mutex name out of the connection identifier, and lock it
136 * while we decide whether to be upstream or downstream.
139 SECURITY_ATTRIBUTES sa;
141 mutexname = make_name(CONNSHARE_MUTEX_PREFIX, name);
142 if (!make_private_security_descriptor(MUTEX_ALL_ACCESS,
149 memset(&sa, 0, sizeof(sa));
150 sa.nLength = sizeof(sa);
151 sa.lpSecurityDescriptor = psd;
152 sa.bInheritHandle = FALSE;
154 mutex = CreateMutex(&sa, FALSE, mutexname);
157 *logtext = dupprintf("CreateMutex(\"%s\") failed: %s",
158 mutexname, win_strerror(GetLastError()));
161 LocalFree(networksid);
168 LocalFree(networksid);
171 WaitForSingleObject(mutex, INFINITE);
174 pipename = make_name(CONNSHARE_PIPE_PREFIX, name);
178 if (can_downstream) {
179 retsock = new_named_pipe_client(pipename, downplug);
180 if (sk_socket_error(retsock) == NULL) {
187 return SHARE_DOWNSTREAM;
190 *ds_err = dupprintf("%s: %s", pipename, sk_socket_error(retsock));
195 retsock = new_named_pipe_listener(pipename, upplug);
196 if (sk_socket_error(retsock) == NULL) {
203 return SHARE_UPSTREAM;
206 *us_err = dupprintf("%s: %s", pipename, sk_socket_error(retsock));
210 /* One of the above clauses ought to have happened. */
211 assert(*logtext || *ds_err || *us_err);
220 void platform_ssh_share_cleanup(const char *name)
224 #else /* !defined NO_SECURITY */
228 #endif /* !defined NO_SECURITY */