Security.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "Security.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. #define PWALG_SIMPLE_INTERNAL 0x00
  10. #define PWALG_SIMPLE_EXTERNAL 0x01
  11. //---------------------------------------------------------------------------
  12. RawByteString SimpleEncryptChar(unsigned char Ch)
  13. {
  14. Ch = (unsigned char)((~Ch) ^ PWALG_SIMPLE_MAGIC);
  15. return
  16. PWALG_SIMPLE_STRING.SubString(((Ch & 0xF0) >> 4) + 1, 1) +
  17. PWALG_SIMPLE_STRING.SubString(((Ch & 0x0F) >> 0) + 1, 1);
  18. }
  19. //---------------------------------------------------------------------------
  20. unsigned char SimpleDecryptNextChar(RawByteString &Str)
  21. {
  22. if (Str.Length() > 0)
  23. {
  24. unsigned char Result = (unsigned char)
  25. ~((((PWALG_SIMPLE_STRING.Pos(Str.c_str()[0])-1) << 4) +
  26. ((PWALG_SIMPLE_STRING.Pos(Str.c_str()[1])-1) << 0)) ^ PWALG_SIMPLE_MAGIC);
  27. Str.Delete(1, 2);
  28. return Result;
  29. }
  30. else return 0x00;
  31. }
  32. //---------------------------------------------------------------------------
  33. RawByteString EncryptPassword(UnicodeString UnicodePassword, UnicodeString UnicodeKey, Integer /* Algorithm */)
  34. {
  35. UTF8String Password = UnicodePassword;
  36. UTF8String Key = UnicodeKey;
  37. RawByteString Result("");
  38. int Shift, Index;
  39. if (!RandSeed) Randomize();
  40. Password = Key + Password;
  41. Shift = (Password.Length() < PWALG_SIMPLE_MAXLEN) ?
  42. (unsigned char)random(PWALG_SIMPLE_MAXLEN - Password.Length()) : 0;
  43. Result += SimpleEncryptChar((unsigned char)PWALG_SIMPLE_FLAG); // Flag
  44. Result += SimpleEncryptChar((unsigned char)PWALG_SIMPLE_INTERNAL); // Dummy
  45. Result += SimpleEncryptChar((unsigned char)Password.Length());
  46. Result += SimpleEncryptChar((unsigned char)Shift);
  47. for (Index = 0; Index < Shift; Index++)
  48. Result += SimpleEncryptChar((unsigned char)random(256));
  49. for (Index = 0; Index < Password.Length(); Index++)
  50. Result += SimpleEncryptChar(Password.c_str()[Index]);
  51. while (Result.Length() < PWALG_SIMPLE_MAXLEN * 2)
  52. Result += SimpleEncryptChar((unsigned char)random(256));
  53. return Result;
  54. }
  55. //---------------------------------------------------------------------------
  56. UnicodeString DecryptPassword(RawByteString Password, UnicodeString UnicodeKey, Integer /* Algorithm */)
  57. {
  58. UTF8String Key = UnicodeKey;
  59. UTF8String Result("");
  60. Integer Index;
  61. unsigned char Length, Flag;
  62. Flag = SimpleDecryptNextChar(Password);
  63. if (Flag == PWALG_SIMPLE_FLAG)
  64. {
  65. /* Dummy = */ SimpleDecryptNextChar(Password);
  66. Length = SimpleDecryptNextChar(Password);
  67. }
  68. else Length = Flag;
  69. Password.Delete(1, ((Integer)SimpleDecryptNextChar(Password))*2);
  70. for (Index = 0; Index < Length; Index++)
  71. Result += (char)SimpleDecryptNextChar(Password);
  72. if (Flag == PWALG_SIMPLE_FLAG)
  73. {
  74. if (Result.SubString(1, Key.Length()) != Key) Result = "";
  75. else Result.Delete(1, Key.Length());
  76. }
  77. return UnicodeString(Result);
  78. }
  79. //---------------------------------------------------------------------------
  80. RawByteString SetExternalEncryptedPassword(RawByteString Password)
  81. {
  82. RawByteString Result;
  83. Result += SimpleEncryptChar((unsigned char)PWALG_SIMPLE_FLAG);
  84. Result += SimpleEncryptChar((unsigned char)PWALG_SIMPLE_EXTERNAL);
  85. Result += UTF8String(BytesToHex(reinterpret_cast<const unsigned char *>(Password.c_str()), Password.Length()));
  86. return Result;
  87. }
  88. //---------------------------------------------------------------------------
  89. bool GetExternalEncryptedPassword(RawByteString Encrypted, RawByteString & Password)
  90. {
  91. bool Result =
  92. (SimpleDecryptNextChar(Encrypted) == PWALG_SIMPLE_FLAG) &&
  93. (SimpleDecryptNextChar(Encrypted) == PWALG_SIMPLE_EXTERNAL);
  94. if (Result)
  95. {
  96. Password = HexToBytes(UTF8ToString(Encrypted));
  97. }
  98. return Result;
  99. }
  100. //---------------------------------------------------------------------------
  101. bool WindowsValidateCertificate(const unsigned char * Certificate, size_t Len, UnicodeString & Error)
  102. {
  103. bool Result = false;
  104. // Parse the certificate into a context.
  105. const CERT_CONTEXT * CertContext =
  106. CertCreateCertificateContext(
  107. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, Certificate, Len);
  108. if (CertContext != NULL)
  109. {
  110. CERT_CHAIN_PARA ChainPara;
  111. // Retrieve the certificate chain of the certificate
  112. // (a certificate without a valid root does not have a chain).
  113. memset(&ChainPara, 0, sizeof(ChainPara));
  114. ChainPara.cbSize = sizeof(ChainPara);
  115. CERT_CHAIN_ENGINE_CONFIG ChainConfig;
  116. memset(&ChainConfig, 0, sizeof(ChainConfig));
  117. ChainConfig.cbSize = sizeof(CERT_CHAIN_ENGINE_CONFIG);
  118. ChainConfig.hRestrictedRoot = NULL;
  119. ChainConfig.hRestrictedTrust = NULL;
  120. ChainConfig.hRestrictedOther = NULL;
  121. ChainConfig.cAdditionalStore = 0;
  122. ChainConfig.rghAdditionalStore = NULL;
  123. ChainConfig.dwFlags = CERT_CHAIN_CACHE_END_CERT;
  124. ChainConfig.dwUrlRetrievalTimeout = 0;
  125. ChainConfig.MaximumCachedCertificates =0;
  126. ChainConfig.CycleDetectionModulus = 0;
  127. HCERTCHAINENGINE ChainEngine;
  128. if (CertCreateCertificateChainEngine(&ChainConfig, &ChainEngine))
  129. {
  130. const CERT_CHAIN_CONTEXT * ChainContext = NULL;
  131. if (CertGetCertificateChain(ChainEngine, CertContext, NULL, NULL, &ChainPara,
  132. CERT_CHAIN_CACHE_END_CERT |
  133. CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT,
  134. NULL, &ChainContext))
  135. {
  136. CERT_CHAIN_POLICY_PARA PolicyPara;
  137. PolicyPara.cbSize = sizeof(PolicyPara);
  138. PolicyPara.dwFlags = 0;
  139. PolicyPara.pvExtraPolicyPara = NULL;
  140. CERT_CHAIN_POLICY_STATUS PolicyStatus;
  141. PolicyStatus.cbSize = sizeof(PolicyStatus);
  142. if (CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL,
  143. ChainContext, &PolicyPara, &PolicyStatus))
  144. {
  145. // Windows thinks the certificate is valid.
  146. Result = (PolicyStatus.dwError == S_OK);
  147. if (!Result)
  148. {
  149. Error = FORMAT(L"Error: %x, Chain index: %d, Element index: %d", (PolicyStatus.dwError, PolicyStatus.lChainIndex, PolicyStatus.lElementIndex));
  150. }
  151. }
  152. CertFreeCertificateChain(ChainContext);
  153. }
  154. CertFreeCertificateChainEngine(ChainEngine);
  155. }
  156. CertFreeCertificateContext(CertContext);
  157. }
  158. return Result;
  159. }
  160. //---------------------------------------------------------------------------