Pārlūkot izejas kodu

Unused files

Source commit: f46333704323a46574c636279445bf3a2da4be4f
Martin Prikryl 1 mēnesi atpakaļ
vecāks
revīzija
cb5cde16ab
2 mainītis faili ar 0 papildinājumiem un 468 dzēšanām
  1. 0 393
      source/core/KeyGen.cpp
  2. 0 75
      source/core/KeyGen.h

+ 0 - 393
source/core/KeyGen.cpp

@@ -1,393 +0,0 @@
-//---------------------------------------------------------------------------
-#include <vcl.h>
-#pragma hdrstop
-
-// CompThread.pas must be linked to project
-#include <CompThread.hpp>
-#define THREAD_CLASS TCompThread
-#else
-#include <Classes.hpp>
-#define THREAD_CLASS TThread
-#endif
-
-#include <Common.h>
-#include "TextsCore.h"
-#include <PuttyIntf.h>
-#include "KeyGen.h"
-//---------------------------------------------------------------------------
-#pragma package(smart_init)
-//---------------------------------------------------------------------------
-extern "C" void KeyGenerationProgressUpdate(void * Thread,
-    int Action, int Phase, int IProgress);
-//---------------------------------------------------------------------------
-class TKeyGenerationThread : public THREAD_CLASS
-{
-public:
-  #define PROGRESSRANGE 65535
-  #define MAXPHASE 5
-  struct
-  {
-    int NPhases;
-    struct
-    {
-      bool Exponential;
-      unsigned StartPoint, Total;
-      unsigned Param, Current, N;   /* if exponential */
-      unsigned Mult;                    /* if linear */
-    } Phases[MAXPHASE];
-    unsigned Total, Divisor, Range;
-    unsigned Position;
-    TKeyGenerationComplete Complete;
-  } Progress;
-
-  TKeyGenerator * FGenerator;
-
-  __fastcall TKeyGenerationThread(TKeyGenerator * AGenerator):
-    THREAD_CLASS(true)
-  {
-    FGenerator = AGenerator;
-    Resume();
-  }
-
-  void __fastcall DistributeProgressUpdate()
-  {
-    FGenerator->ProgressUpdate(Progress.Range, Progress.Position, Progress.Complete);
-  }
-
-  void __fastcall ProgressUpdate(int Action, int Phase, unsigned IProgress)
-  {
-    int Position;
-
-    if (Action < PROGFN_READY && Progress.NPhases < Phase)
-        Progress.NPhases = Phase;
-
-    switch (Action)
-    {
-      case PROGFN_INITIALISE:
-        Progress.NPhases = 0;
-        Progress.Complete = kgInProgress;
-        break;
-
-      case PROGFN_LIN_PHASE:
-        Progress.Phases[Phase-1].Exponential = false;
-        Progress.Phases[Phase-1].Mult = Progress.Phases[Phase].Total / IProgress;
-        break;
-
-      case PROGFN_EXP_PHASE:
-        Progress.Phases[Phase-1].Exponential = true;
-        Progress.Phases[Phase-1].Param = 0x10000 + IProgress;
-        Progress.Phases[Phase-1].Current = Progress.Phases[Phase-1].Total;
-        Progress.Phases[Phase-1].N = 0;
-        break;
-
-      case PROGFN_PHASE_EXTENT:
-        Progress.Phases[Phase-1].Total = IProgress;
-        break;
-
-      case PROGFN_READY:
-        {
-          unsigned Total = 0;
-          int i;
-          for (i = 0; i < Progress.NPhases; i++)
-          {
-            Progress.Phases[i].StartPoint = Total;
-            Total += Progress.Phases[i].Total;
-          }
-          Progress.Total = Total;
-          Progress.Divisor = ((Progress.Total + PROGRESSRANGE - 1) / PROGRESSRANGE);
-          Progress.Range = Progress.Total / Progress.Divisor;
-
-          Synchronize(DistributeProgressUpdate);
-        }
-        break;
-
-      case PROGFN_PROGRESS:
-        if (Progress.Phases[Phase-1].Exponential)
-        {
-          while (Progress.Phases[Phase-1].N < IProgress)
-          {
-            Progress.Phases[Phase-1].N++;
-            Progress.Phases[Phase-1].Current *= Progress.Phases[Phase-1].Param;
-            Progress.Phases[Phase-1].Current /= 0x10000;
-          }
-          Position = (Progress.Phases[Phase-1].StartPoint +
-            Progress.Phases[Phase-1].Total - Progress.Phases[Phase-1].Current);
-        }
-        else
-        {
-          Position = (Progress.Phases[Phase-1].StartPoint +
-            IProgress * Progress.Phases[Phase-1].Mult);
-        }
-        Progress.Position = Position / Progress.Divisor;
-        Synchronize(DistributeProgressUpdate);
-        break;
-    }
-  }
-
-  virtual void __fastcall Execute()
-  {
-    try
-    {
-      ProgressUpdate(PROGFN_INITIALISE, 0, 0);
-
-      if (FGenerator->KeyType == ktDSA)
-      {
-        dsa_generate(FGenerator->FDSSKey, FGenerator->KeySize,
-          KeyGenerationProgressUpdate, this);
-      }
-      else
-      {
-        rsa_generate(FGenerator->FRSAKey, FGenerator->KeySize,
-          KeyGenerationProgressUpdate, this);
-      }
-      Progress.Complete = kgSuccess;
-    }
-    catch(...)
-    {
-      Progress.Complete = kgFailure;
-    }
-    Synchronize(DistributeProgressUpdate);
-  }
-#pragma warn +lvc
-};
-//---------------------------------------------------------------------------
-void KeyGenerationProgressUpdate(void * Thread,
-  int Action, int Phase, int IProgress)
-{
-  DebugAssert(Thread);
-  ((TKeyGenerationThread*)Thread)->ProgressUpdate(Action, Phase, IProgress);
-}
-//---------------------------------------------------------------------------
-__fastcall TKeyGenerator::TKeyGenerator():
-  TObject()
-{
-  FSSH2Key = new ssh2_userkey;
-  FRSAKey = new RSAKey;
-  FDSSKey = new dss_key;
-  FEntropy = NULL;
-  FState = kgInitializing;
-  FOnGenerating = NULL;
-  FGenerationRange = -1;
-  KeyType = ktRSA2;
-  KeySize = 1024;
-}
-//---------------------------------------------------------------------------
-__fastcall TKeyGenerator::~TKeyGenerator()
-{
-  DebugAssert(FState != kgGenerating);
-  if (FEntropy) delete FEntropy;
-  delete FSSH2Key;
-  delete FRSAKey;
-  delete FDSSKey;
-}
-//---------------------------------------------------------------------------
-void __fastcall TKeyGenerator::SetKeySize(int value)
-{
-  DebugAssert(FState != kgGenerating);
-  FState = kgInitializing;
-  FKeySize = value;
-  FEntropyRequired = (KeySize / 2) * 2;
-  FEntropyGot = 0;
-  if (FEntropy) delete FEntropy;
-  FEntropy = new TEntropyBit[FEntropyRequired];
-}
-//---------------------------------------------------------------------------
-void __fastcall TKeyGenerator::SetKeyType(TKeyType value)
-{
-  DebugAssert(FState != kgGenerating);
-  FState = kgInitializing;
-  FKeyType = value;
-}
-//---------------------------------------------------------------------------
-void __fastcall TKeyGenerator::AddEntropy(TEntropyBit Entropy)
-{
-  DebugAssert(FState == kgInitializing);
-  DebugAssert(FEntropy);
-  DebugAssert(FEntropyGot < FEntropyRequired);
-  FEntropy[FEntropyGot++] = Entropy;
-  if (FEntropyGot == FEntropyRequired)
-  {
-    FState = kgInitialized;
-    random_add_heavynoise(FEntropy, FEntropyRequired * sizeof(TEntropyBit));
-    delete FEntropy;
-    FEntropy = NULL;
-  }
-}
-//---------------------------------------------------------------------------
-void __fastcall TKeyGenerator::ResetKey()
-{
-  DebugAssert(FSSH2Key);
-  switch (KeyType) {
-    case ktDSA:
-      FSSH2Key->data = FDSSKey;
-      FSSH2Key->alg = &ssh_dss;
-      break;
-
-    case ktRSA2:
-      FSSH2Key->data = FRSAKey;
-      FSSH2Key->alg = &ssh_rsa;
-      break;
-  }
-
-  FFingerprint = "";
-  FPublicKey = "";
-
-  if (Comment.IsEmpty())
-  {
-    Comment = FORMAT("%s-key-%s",
-      ((KeyType == ktDSA ? "dsa" : "rsa"), FormatDateTime("yyyymmdd", Now())));
-  }
-}
-//---------------------------------------------------------------------------
-void __fastcall TKeyGenerator::StartGenerationThread()
-{
-  DebugAssert(FState == kgInitialized);
-  FState = kgGenerating;
-  new TKeyGenerationThread(this);
-}
-//---------------------------------------------------------------------------
-void __fastcall TKeyGenerator::Generate()
-{
-  DebugAssert(FState == kgInitialized);
-  THREAD_CLASS * Thread;
-  FState = kgGenerating;
-  Thread = new TKeyGenerationThread(this);
-  Thread->WaitFor();
-}
-//---------------------------------------------------------------------------
-void __fastcall TKeyGenerator::ProgressUpdate(int Range, int Position,
-  TKeyGenerationComplete Complete)
-{
-  DebugAssert(FState == kgGenerating);
-  if (Complete == kgSuccess)
-  {
-    FState = kgComplete;
-    ResetKey();
-  }
-  FGenerationRange = Range;
-  FGenerationPosition = Position;
-  if (FOnGenerating) FOnGenerating(this, Range, Position, Complete);
-}
-//---------------------------------------------------------------------------
-int __fastcall TKeyGenerator::GetPercentGenerated()
-{
-  switch (FState) {
-    case kgComplete: return 100;
-    case kgGenerating: return (FGenerationPosition * 100) / FGenerationRange;
-    default: return 0;
-  }
-}
-//---------------------------------------------------------------------------
-void __fastcall TKeyGenerator::SetComment(AnsiString value)
-{
-  if (FComment != value)
-  {
-    FComment = value;
-    FPublicKey = "";
-    DebugAssert(FSSH2Key);
-    FSSH2Key->comment = FComment.c_str();
-    FRSAKey->comment = FComment.c_str();
-  }
-}
-//---------------------------------------------------------------------------
-AnsiString __fastcall TKeyGenerator::GetFingerprint()
-{
-  if (FFingerprint.IsEmpty())
-  {
-    if (IsSSH2)
-    {
-      DebugAssert(FSSH2Key);
-      FFingerprint = FSSH2Key->alg->fingerprint(FSSH2Key->data);
-    }
-    else
-    {
-      char Buf[128];
-      rsa_fingerprint(Buf, sizeof(Buf), FRSAKey);
-      FFingerprint = Buf;
-    }
-  }
-  return FFingerprint;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TKeyGenerator::GetIsSSH2()
-{
-  return (KeyType == ktDSA || KeyType == ktRSA2);
-}
-//---------------------------------------------------------------------------
-AnsiString __fastcall TKeyGenerator::GetPublicKey()
-{
-  if (FPublicKey.IsEmpty())
-  {
-    unsigned char *pub_blob;
-    char *buffer, *p;
-    int pub_len;
-    int i;
-
-    DebugAssert(FSSH2Key);
-    pub_blob = FSSH2Key->alg->public_blob(FSSH2Key->data, &pub_len);
-    buffer = new char[4 * ((pub_len + 2) / 3) + 1];
-    p = buffer;
-    i = 0;
-    while (i < pub_len)
-    {
-      int n = (pub_len - i < 3 ? pub_len - i : 3);
-      base64_encode_atom(pub_blob + i, n, p);
-      i += n;
-      p += 4;
-    }
-    *p = '\0';
-    FPublicKey = buffer;
-    sfree(pub_blob);
-    delete [] buffer;
-  }
-  return FPublicKey;
-}
-//---------------------------------------------------------------------------
-AnsiString __fastcall TKeyGenerator::GetAuthorizedKeysLine()
-{
-  DebugAssert(FSSH2Key);
-  return FORMAT("%s %s %s", (FSSH2Key->alg->name, PublicKey, Comment));
-}
-//---------------------------------------------------------------------------
-void __fastcall TKeyGenerator::SaveKey(const AnsiString FileName,
-  const AnsiString Passphrase, TKeyFormat Format)
-{
-  DebugAssert(FSSH2Key);
-  DebugAssert(FState == kgComplete);
-  DebugAssert((Format != kfOpenSSH && Format != kfSSHCom) || IsSSH2);
-
-  int Result;
-
-  if (IsSSH2)
-  {
-    switch (Format)
-    {
-      case kfPutty:
-        Result = ssh2_save_userkey(FileName.c_str(), FSSH2Key,
-          (char*)Passphrase.data());
-        break;
-
-      case kfOpenSSH:
-        Result = export_ssh2(FileName.c_str(), SSH_KEYTYPE_OPENSSH, FSSH2Key,
-          (char*)Passphrase.data());
-        break;
-
-      case kfSSHCom:
-        Result = export_ssh2(FileName.c_str(), SSH_KEYTYPE_SSHCOM, FSSH2Key,
-          (char*)Passphrase.data());
-        break;
-
-      default:
-        DebugFail();
-    }
-  }
-  else
-  {
-    DebugAssert(Format == kfPutty);
-    Result = saversakey(FileName.c_str(), FRSAKey,
-      (char*)Passphrase.data());
-  }
-
-  if (Result <= 0)
-    throw Exception(FMTLOAD(SAVE_KEY_ERROR, (FileName)));
-}

+ 0 - 75
source/core/KeyGen.h

@@ -1,75 +0,0 @@
-//---------------------------------------------------------------------------
-#ifndef KeyGenH
-#define KeyGenH
-//---------------------------------------------------------------------------
-enum TKeyType { ktRSA1, ktRSA2, ktDSA };
-typedef unsigned TEntropyBit;
-class TKeyGenerationThread;
-class TKeyGenerator;
-enum TKeyGeneratorState { kgInitializing, kgInitialized, kgGenerating, kgComplete };
-enum TKeyGenerationComplete { kgInProgress, kgSuccess, kgFailure };
-enum TKeyFormat { kfPutty, kfOpenSSH, kfSSHCom };
-typedef void __fastcall (__closure *TKeyGeneratorGenerating)
-  (TKeyGenerator * Generator, int Range, int Position, TKeyGenerationComplete Complete);
-//---------------------------------------------------------------------------
-struct ssh2_userkey;
-struct RSAKey;
-struct dss_key;
-//---------------------------------------------------------------------------
-class TKeyGenerator : public TObject
-{
-friend class TKeyGenerationThread;
-private:
-  AnsiString FComment;
-  int FKeySize;
-  TKeyType FKeyType;
-  TEntropyBit * FEntropy;
-  int FEntropyGot;
-  int FEntropyRequired;
-  AnsiString FFingerprint;
-  TKeyGeneratorState FState;
-  int FGenerationRange;
-  int FGenerationPosition;
-  TKeyGeneratorGenerating FOnGenerating;
-  AnsiString FPublicKey;
-  TKeyGenerationThread * FThread;
-  AnsiString __fastcall GetAuthorizedKeysLine();
-  AnsiString __fastcall GetFingerprint();
-  bool __fastcall GetIsSSH2();
-  struct ssh2_userkey * FSSH2Key;
-  int __fastcall GetPercentGenerated();
-  AnsiString __fastcall GetPublicKey();
-  void __fastcall SetComment(AnsiString value);
-  void __fastcall SetKeySize(int value);
-  void __fastcall SetKeyType(TKeyType value);
-protected:
-  // both written by TKeyGenerationThread (from different thread)
-  RSAKey * FRSAKey;
-  dss_key * FDSSKey;
-  // called by TKeyGenerationThread (from main VCL thread)
-  void __fastcall ProgressUpdate(int Range, int Position, TKeyGenerationComplete Complete);
-  void __fastcall ResetKey();
-public:
-  __fastcall TKeyGenerator();
-  virtual __fastcall ~TKeyGenerator();
-  void __fastcall AddEntropy(TEntropyBit Entropy);
-  void __fastcall Generate();
-  void __fastcall SaveKey(const AnsiString FileName,
-    const AnsiString Passphrase, TKeyFormat Format);
-  void __fastcall StartGenerationThread();
-  __property AnsiString AuthorizedKeysLine = { read = GetAuthorizedKeysLine };
-  __property AnsiString Comment = { read = FComment, write = SetComment };
-  __property int EntropyGot = { read = FEntropyGot };
-  __property int EntropyRequired = { read = FEntropyRequired };
-  __property AnsiString Fingerprint = { read = GetFingerprint };
-  __property bool IsSSH2 = { read = GetIsSSH2 };
-  __property TKeyGeneratorState State = { read = FState };
-
-  __property int KeySize = { read = FKeySize, write = SetKeySize };
-  __property TKeyType KeyType = { read = FKeyType, write = SetKeyType };
-  __property TKeyGeneratorGenerating OnGenerating = { read = FOnGenerating, write = FOnGenerating };
-  __property int PercentGenerated = { read = GetPercentGenerated };
-  __property AnsiString PublicKey = { read = GetPublicKey };
-};
-//---------------------------------------------------------------------------
-#endif