- otherstr = malloc(len);
- mungedhost = malloc(3*strlen(host)+1);
- if (!otherstr || !mungedhost)
- fatalbox("Out of memory");
-
- mungestr(host, mungedhost);
-
- if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
- &rkey) != ERROR_SUCCESS) {
- if (MessageBox(NULL, "PuTTY was unable to open the host key cache\n"
- "in the registry. There is thus no way to tell\n"
- "if the remote host is what you think it is.\n"
- "Connect anyway?", "PuTTY Problem",
- MB_ICONWARNING | MB_YESNO) == IDNO)
- exit(0);
- } else {
- DWORD readlen = len;
- DWORD type;
- int ret;
-
- ret = RegQueryValueEx(rkey, mungedhost, NULL,
- &type, otherstr, &readlen);
-
- if (ret == ERROR_MORE_DATA ||
- (ret == ERROR_SUCCESS && type == REG_SZ &&
- strcmp(otherstr, keystr))) {
- if (MessageBox(NULL,
- "This host's host key is different from the\n"
- "one cached in the registry! Someone may be\n"
- "impersonating this host for malicious reasons;\n"
- "alternatively, the host key may have changed\n"
- "due to sloppy system administration.\n"
- "Replace key in registry and connect?",
- "PuTTY: Security Warning",
- MB_ICONWARNING | MB_YESNO) == IDNO)
- exit(0);
- RegSetValueEx(rkey, mungedhost, 0, REG_SZ, keystr,
- strlen(keystr)+1);
- } else if (ret != ERROR_SUCCESS || type != REG_SZ) {
- if (MessageBox(NULL,
- "This host's host key is not cached in the\n"
- "registry. Do you want to add it to the cache\n"
- "and carry on connecting?",
- "PuTTY: New Host",
- MB_ICONWARNING | MB_YESNO) == IDNO)
- exit(0);
- RegSetValueEx(rkey, mungedhost, 0, REG_SZ, keystr,
- strlen(keystr)+1);
- }
-
- RegCloseKey(rkey);
+ ret = verify_host_key(host, keytype, keystr);
+
+ if (ret == 0) /* success - key matched OK */
+ return;
+ if (ret == 2) { /* key was different */
+ int mbret;
+ sprintf(message, wrongmsg, fingerprint);
+ mbret = MessageBox(NULL, message, mbtitle,
+ MB_ICONWARNING | MB_YESNOCANCEL);
+ if (mbret == IDYES)
+ store_host_key(host, keytype, keystr);
+ if (mbret == IDCANCEL)
+ exit(0);
+ }
+ if (ret == 1) { /* key was absent */
+ int mbret;
+ sprintf(message, absentmsg, fingerprint);
+ mbret = MessageBox(NULL, message, mbtitle,
+ MB_ICONWARNING | MB_YESNO);
+ if (mbret == IDNO)
+ exit(0);
+ store_host_key(host, keytype, keystr);