Răsfoiți Sursa

Warning when selecting too new key file + Fixing selecting PPK files and error message when selecting SSH-1 keys, both broken by 5dfcf1eb

(cherry picked from commit 2a355b95b18e5897754788273585743821df5b22)

Source commit: 49dc88d07bf774f9b2eee826d1f0f1617f8a3e31
Martin Prikryl 4 ani în urmă
părinte
comite
7a9a31484f
3 a modificat fișierele cu 62 adăugiri și 18 ștergeri
  1. 33 4
      source/core/PuttyIntf.cpp
  2. 1 0
      source/core/PuttyTools.h
  3. 28 14
      source/windows/Tools.cpp

+ 33 - 4
source/core/PuttyIntf.cpp

@@ -652,7 +652,7 @@ bool IsKeyEncrypted(TKeyType KeyType, const UnicodeString & FileName, UnicodeStr
   return Result;
 }
 //---------------------------------------------------------------------------
-TPrivateKey * LoadKey(TKeyType KeyType, const UnicodeString & FileName, const UnicodeString & Passphrase)
+TPrivateKey * LoadKey(TKeyType KeyType, const UnicodeString & FileName, const UnicodeString & Passphrase, UnicodeString & Error)
 {
   UTF8String UtfFileName = UTF8String(FileName);
   Filename * KeyFile = filename_from_str(UtfFileName.c_str());
@@ -686,21 +686,50 @@ TPrivateKey * LoadKey(TKeyType KeyType, const UnicodeString & FileName, const Un
 
   if (Ssh2Key == NULL)
   {
-    UnicodeString Error = AnsiString(ErrorStr);
     // While theoretically we may get "unable to open key file" and
     // so we should check system error code,
     // we actully never get here unless we call KeyType previously
     // and handle ktUnopenable accordingly.
-    throw Exception(Error);
+    Error = AnsiString(ErrorStr);
   }
   else if (Ssh2Key == SSH2_WRONG_PASSPHRASE)
   {
-    throw Exception(LoadStr(AUTH_TRANSL_WRONG_PASSPHRASE));
+    Error = EmptyStr;
+    Ssh2Key = NULL;
   }
 
   return reinterpret_cast<TPrivateKey *>(Ssh2Key);
 }
 //---------------------------------------------------------------------------
+TPrivateKey * LoadKey(TKeyType KeyType, const UnicodeString & FileName, const UnicodeString & Passphrase)
+{
+  UnicodeString Error;
+  TPrivateKey * Result = LoadKey(KeyType, FileName, Passphrase, Error);
+  if (Result == NULL)
+  {
+    if (!Error.IsEmpty())
+    {
+      throw Exception(Error);
+    }
+    else
+    {
+      throw Exception(LoadStr(AUTH_TRANSL_WRONG_PASSPHRASE));
+    }
+  }
+  return Result;
+}
+//---------------------------------------------------------------------------
+UnicodeString TestKey(TKeyType KeyType, const UnicodeString & FileName)
+{
+  UnicodeString Result;
+  TPrivateKey * Key = LoadKey(KeyType, FileName, EmptyStr, Result);
+  if (Key != NULL)
+  {
+    FreeKey(Key);
+  }
+  return Result;
+}
+//---------------------------------------------------------------------------
 void ChangeKeyComment(TPrivateKey * PrivateKey, const UnicodeString & Comment)
 {
   AnsiString AnsiComment(Comment);

+ 1 - 0
source/core/PuttyTools.h

@@ -13,6 +13,7 @@ TKeyType KeyType(UnicodeString FileName);
 bool IsKeyEncrypted(TKeyType KeyType, const UnicodeString & FileName, UnicodeString & Comment);
 struct TPrivateKey;
 TPrivateKey * LoadKey(TKeyType KeyType, const UnicodeString & FileName, const UnicodeString & Passphrase);
+UnicodeString TestKey(TKeyType KeyType, const UnicodeString & FileName);
 void ChangeKeyComment(TPrivateKey * PrivateKey, const UnicodeString & Comment);
 void SaveKey(TKeyType KeyType, const UnicodeString & FileName,
   const UnicodeString & Passphrase, TPrivateKey * PrivateKey);

+ 28 - 14
source/windows/Tools.cpp

@@ -1281,7 +1281,7 @@ static void __fastcall ConvertKey(UnicodeString & FileName, TKeyType Type)
   }
 }
 //---------------------------------------------------------------------------
-static void __fastcall DoVerifyKey(UnicodeString & FileName, bool Convert, bool CanIgnore)
+void DoVerifyKey(UnicodeString & FileName, bool Convert, UnicodeString & Message, TStrings *& MoreMessages, UnicodeString & HelpKeyword)
 {
   if (!FileName.Trim().IsEmpty())
   {
@@ -1289,10 +1289,9 @@ static void __fastcall DoVerifyKey(UnicodeString & FileName, bool Convert, bool
     TKeyType Type = KeyType(FileName);
     // reason _wfopen failed
     int Error = errno;
-    UnicodeString Message;
-    UnicodeString HelpKeyword = HELP_LOGIN_KEY_TYPE;
+    HelpKeyword = HELP_LOGIN_KEY_TYPE;
     UnicodeString PuttygenPath;
-    std::unique_ptr<TStrings> MoreMessages;
+    std::unique_ptr<TStrings> AMoreMessages;
     switch (Type)
     {
       case ktOpenSSHPEM:
@@ -1306,7 +1305,7 @@ static void __fastcall DoVerifyKey(UnicodeString & FileName, bool Convert, bool
           {
             Configuration->Usage->Inc(L"PrivateKeyConvertSuggestionsNative");
             UnicodeString ConvertMessage = FMTLOAD(KEY_TYPE_CONVERT3, (TypeName, RemoveMainInstructionsTag(Message)));
-            Message = UnicodeString();
+            Message = EmptyStr;
             if (MoreMessageDialog(ConvertMessage, NULL, qtConfirmation, qaOK | qaCancel, HelpKeyword) == qaOK)
             {
               ConvertKey(FileName, Type);
@@ -1325,7 +1324,11 @@ static void __fastcall DoVerifyKey(UnicodeString & FileName, bool Convert, bool
         break;
 
       case ktSSH1:
-        Message = MainInstructions(LoadStr(KEY_TYPE_SSH1));
+        Message = MainInstructions(FMTLOAD(KEY_TYPE_SSH1, (FileName)));
+        break;
+
+      case ktSSH2:
+        Message = TestKey(Type, FileName);
         break;
 
       case ktSSH1Public:
@@ -1338,7 +1341,7 @@ static void __fastcall DoVerifyKey(UnicodeString & FileName, bool Convert, bool
         Message = MainInstructions(FMTLOAD(KEY_TYPE_UNOPENABLE, (FileName)));
         if (Error != ERROR_SUCCESS)
         {
-          MoreMessages.reset(TextToStringList(SysErrorMessageForError(Error)));
+          AMoreMessages.reset(TextToStringList(SysErrorMessageForError(Error)));
         }
         break;
 
@@ -1350,14 +1353,25 @@ static void __fastcall DoVerifyKey(UnicodeString & FileName, bool Convert, bool
         break;
     }
 
-    if (!Message.IsEmpty())
+    MoreMessages = AMoreMessages.release();
+  }
+}
+
+//---------------------------------------------------------------------------
+static void __fastcall DoVerifyKey(UnicodeString & FileName, bool Convert, bool CanIgnore)
+{
+  TStrings * AMoreMessages;
+  UnicodeString Message;
+  UnicodeString HelpKeyword;
+  DoVerifyKey(FileName, Convert, Message, AMoreMessages, HelpKeyword);
+  std::unique_ptr<TStrings> MoreMessages(AMoreMessages);
+  if (!Message.IsEmpty())
+  {
+    Configuration->Usage->Inc(L"PrivateKeySelectErrors");
+    unsigned int Answers = (CanIgnore ? (qaIgnore | qaAbort) : qaOK);
+    if (MoreMessageDialog(Message, MoreMessages.get(), qtWarning, Answers, HelpKeyword) != qaIgnore)
     {
-      Configuration->Usage->Inc(L"PrivateKeySelectErrors");
-      unsigned int Answers = (CanIgnore ? (qaIgnore | qaAbort) : qaOK);
-      if (MoreMessageDialog(Message, MoreMessages.get(), qtWarning, Answers, HelpKeyword) != qaIgnore)
-      {
-        Abort();
-      }
+      Abort();
     }
   }
 }