2 * winstore.c: Windows-specific implementation of the interface
3 * defined in storage.h.
11 static char seedpath[2*MAX_PATH+10] = "\0";
13 static char hex[16] = "0123456789ABCDEF";
15 static void mungestr(char *in, char *out) {
19 if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' ||
20 *in == '%' || *in < ' ' || *in > '~' || (*in == '.' && !candot)) {
22 *out++ = hex[((unsigned char)*in) >> 4];
23 *out++ = hex[((unsigned char)*in) & 15];
33 static void unmungestr(char *in, char *out) {
35 if (*in == '%' && in[1] && in[2]) {
38 i = in[1] - '0'; i -= (i > 9 ? 7 : 0);
39 j = in[2] - '0'; j -= (j > 9 ? 7 : 0);
50 void *open_settings_w(char *sessionname);
51 void write_setting_s(void *handle, char *key, char *value);
52 void write_setting_i(void *handle, char *key, int value);
53 void *close_settings_w(void *handle);
55 void *open_settings_r(char *sessionname);
56 char *read_setting_s(void *handle, char *key, char *buffer, int buflen);
57 int read_setting_i(void *handle, char *key, int defvalue);
58 void *close_settings_r(void *handle);
60 static void hostkey_regname(char *buffer, char *hostname, char *keytype) {
61 strcpy(buffer, keytype);
63 mungestr(hostname, buffer + strlen(buffer));
66 int verify_host_key(char *hostname, char *keytype, char *key) {
67 char *otherstr, *regname;
74 len = 1 + strlen(key);
77 * Now read a saved key in from the registry and see what it
80 otherstr = smalloc(len);
81 regname = smalloc(3*(strlen(hostname)+strlen(keytype))+5);
82 if (!otherstr || !regname)
83 fatalbox("Out of memory");
85 hostkey_regname(regname, hostname, keytype);
87 if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
88 &rkey) != ERROR_SUCCESS)
89 return 1; /* key does not exist in registry */
92 ret = RegQueryValueEx(rkey, regname, NULL, &type, otherstr, &readlen);
94 if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
95 !strcmp(keytype, "rsa")) {
97 * Key didn't exist. If the key type is RSA, we'll try
98 * another trick, which is to look up the _old_ key format
99 * under just the hostname and translate that.
101 char *justhost = regname + 1 + strlen(keytype);
102 char *oldstyle = smalloc(len + 10); /* safety margin */
104 ret = RegQueryValueEx(rkey, justhost, NULL, &type,
107 if (ret == ERROR_SUCCESS && type == REG_SZ) {
109 * The old format is two old-style bignums separated by
110 * a slash. An old-style bignum is made of groups of
111 * four hex digits: digits are ordered in sensible
112 * (most to least significant) order within each group,
113 * but groups are ordered in silly (least to most)
114 * order within the bignum. The new format is two
115 * ordinary C-format hex numbers (0xABCDEFG...XYZ, with
116 * A nonzero except in the special case 0x0, which
117 * doesn't appear anyway in RSA keys) separated by a
118 * comma. All hex digits are lowercase in both formats.
124 for (i = 0; i < 2; i++) {
126 *p++ = '0'; *p++ = 'x';
127 ndigits = strcspn(q, "/"); /* find / or end of string */
128 nwords = ndigits / 4;
129 /* now trim ndigits to remove leading zeros */
130 while (q[ (ndigits-1) ^ 3 ] == '0' && ndigits > 1)
132 /* now move digits over to new string */
133 for (j = 0; j < ndigits; j++)
134 p[ndigits-1-j] = q[j^3];
138 q++; /* eat the slash */
139 *p++ = ','; /* add a comma */
141 *p = '\0'; /* terminate the string */
145 * Now _if_ this key matches, we'll enter it in the new
146 * format. If not, we'll assume something odd went
147 * wrong, and hyper-cautiously do nothing.
149 if (!strcmp(otherstr, key))
150 RegSetValueEx(rkey, regname, 0, REG_SZ, otherstr,
157 compare = strcmp(otherstr, key);
162 if (ret == ERROR_MORE_DATA ||
163 (ret == ERROR_SUCCESS && type == REG_SZ && compare))
164 return 2; /* key is different in registry */
165 else if (ret != ERROR_SUCCESS || type != REG_SZ)
166 return 1; /* key does not exist in registry */
168 return 0; /* key matched OK in registry */
171 void store_host_key(char *hostname, char *keytype, char *key) {
175 regname = smalloc(3*(strlen(hostname)+strlen(keytype))+5);
177 fatalbox("Out of memory");
179 hostkey_regname(regname, hostname, keytype);
181 if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
182 &rkey) != ERROR_SUCCESS)
183 return; /* key does not exist in registry */
184 RegSetValueEx(rkey, regname, 0, REG_SZ, key,
190 * Find the random seed file path and store it in `seedpath'.
192 static void get_seedpath(void) {
196 size = sizeof(seedpath);
198 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey)==ERROR_SUCCESS) {
199 int ret = RegQueryValueEx(rkey, "RandSeedFile",
200 0, &type, seedpath, &size);
201 if (ret != ERROR_SUCCESS || type != REG_SZ)
210 len = GetEnvironmentVariable("HOMEDRIVE", seedpath, sizeof(seedpath));
211 ret = GetEnvironmentVariable("HOMEPATH", seedpath+len,
212 sizeof(seedpath)-len);
213 if (ret == 0) { /* probably win95; store in \WINDOWS */
214 GetWindowsDirectory(seedpath, sizeof(seedpath));
215 len = strlen(seedpath);
218 strcpy(seedpath+len, "\\PUTTY.RND");
222 void read_random_seed(noise_consumer_t consumer) {
228 seedf = CreateFile(seedpath, GENERIC_READ,
229 FILE_SHARE_READ | FILE_SHARE_WRITE,
230 NULL, OPEN_EXISTING, 0, NULL);
232 if (seedf != INVALID_HANDLE_VALUE) {
237 if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len)
246 void write_random_seed(void *data, size_t len) {
252 seedf = CreateFile(seedpath, GENERIC_WRITE, 0,
253 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
255 if (seedf != INVALID_HANDLE_VALUE) {
258 WriteFile(seedf, data, len, &lenwritten, NULL);
264 * Recursively delete a registry key and everything under it.
266 static void registry_recursive_remove(HKEY key) {
268 char name[MAX_PATH+1];
272 while (RegEnumKey(key, i, name, sizeof(name)) == ERROR_SUCCESS) {
273 if (RegOpenKey(key, name, &subkey) == ERROR_SUCCESS) {
274 registry_recursive_remove(subkey);
277 RegDeleteKey(key, name);
281 void cleanup_all(void) {
284 char name[MAX_PATH+1];
286 /* ------------------------------------------------------------
287 * Wipe out the random seed file.
293 /* ------------------------------------------------------------
294 * Destroy all registry information associated with PuTTY.
298 * Open the main PuTTY registry key and remove everything in it.
300 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &key) == ERROR_SUCCESS) {
301 registry_recursive_remove(key);
305 * Now open the parent key and remove the PuTTY main key. Once
306 * we've done that, see if the parent key has any other
309 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_PARENT,
310 &key) == ERROR_SUCCESS) {
311 RegDeleteKey(key, PUTTY_REG_PARENT_CHILD);
312 ret = RegEnumKey(key, 0, name, sizeof(name));
315 * If the parent key had no other children, we must delete
316 * it in its turn. That means opening the _grandparent_
319 if (ret != ERROR_SUCCESS) {
320 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_GPARENT,
321 &key) == ERROR_SUCCESS) {
322 RegDeleteKey(key, PUTTY_REG_GPARENT_CHILD);