浏览代码

When converting private keys to .ppk format, matching certificate is auto-detected and added to the converted private key file

Source commit: 568782e618dad65d96737b24914c9c7f064ae3fb
Martin Prikryl 2 年之前
父节点
当前提交
6ce6141232
共有 3 个文件被更改,包括 25 次插入1 次删除
  1. 1 0
      source/resource/TextsWin.h
  2. 1 0
      source/resource/TextsWin1.rc
  3. 23 1
      source/windows/Tools.cpp

+ 1 - 0
source/resource/TextsWin.h

@@ -672,6 +672,7 @@
 #define KEX_NAME_RSA            6078
 #define KEX_NAME_ECDH           6079
 #define LOGIN_KEY_WITH_CERTIFICATE 6080
+#define CERTIFICATE_ADDED       6081
 
 // 2xxx is reserved for TextsFileZilla.h
 

+ 1 - 0
source/resource/TextsWin1.rc

@@ -677,6 +677,7 @@ BEGIN
         KEX_NAME_RSA, "RSA-based key exchange"
         KEX_NAME_ECDH, "ECDH key exchange"
         LOGIN_KEY_WITH_CERTIFICATE, "**This key contains an OpenSSH certificate.**\nIt is not supposed to be added to OpenSSH authorized_keys file."
+        CERTIFICATE_ADDED, "Matching certificate was detected in '%s' and added to the converted key file."
 
         WIN_VARIABLE_STRINGS, "WIN_VARIABLE"
         WINSCP_COPYRIGHT, "Copyright © 2000–2023 Martin Prikryl"

+ 23 - 1
source/windows/Tools.cpp

@@ -1275,6 +1275,24 @@ static void __fastcall ConvertKey(UnicodeString & FileName, TKeyType Type)
 
   try
   {
+    AppLogFmt(L"Loaded key from \"%s\".", (FileName));
+
+    UnicodeString CertificateMessage;
+    UnicodeString CertificateFileName = FileName + L"-cert.pub";
+    if (FileExists(CertificateFileName))
+    {
+      try
+      {
+        AddCertificateToKey(PrivateKey, CertificateFileName);
+        AppLogFmt(L"Added certificate from auto-detected \"%s\".", (CertificateFileName));
+        CertificateMessage = L"\n" + FMTLOAD(CERTIFICATE_ADDED, (CertificateFileName));
+      }
+      catch (Exception & E)
+      {
+        AppLogFmt(L"Cannot add certificate from auto-detected \"%s\": %s", (CertificateFileName, E.Message));
+      }
+    }
+
     FileName = GetConvertedKeyFileName(FileName);
 
     if (!SaveDialog(LoadStr(CONVERTKEY_SAVE_TITLE), LoadStr(CONVERTKEY_SAVE_FILTER), PuttyKeyExt, FileName))
@@ -1283,8 +1301,12 @@ static void __fastcall ConvertKey(UnicodeString & FileName, TKeyType Type)
     }
 
     SaveKey(ktSSH2, FileName, Passphrase, PrivateKey);
+    AppLogFmt(L"Saved converted key to \"%s\".", (FileName));
 
-    MessageDialog(MainInstructions(FMTLOAD(CONVERTKEY_SAVED, (FileName))), qtInformation, qaOK);
+    UnicodeString Message =
+      MainInstructions(FMTLOAD(CONVERTKEY_SAVED, (FileName))) +
+      CertificateMessage;
+    MessageDialog(Message, qtInformation, qaOK);
   }
   __finally
   {