Security.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. const size_t ChainConfigSize =
  118. reinterpret_cast<const char *>(&ChainConfig.CycleDetectionModulus) + sizeof(ChainConfig.CycleDetectionModulus) -
  119. reinterpret_cast<const char *>(&ChainConfig);
  120. // The hExclusiveRoot and hExclusiveTrustedPeople were added in Windows 7.
  121. // The CertGetCertificateChain fails with E_INVALIDARG when we include them to ChainConfig.cbSize.
  122. DebugAssert(ChainConfigSize == 40);
  123. DebugAssert(ChainConfigSize == sizeof(CERT_CHAIN_ENGINE_CONFIG) - sizeof(ChainConfig.hExclusiveRoot) - sizeof(ChainConfig.hExclusiveTrustedPeople));
  124. ChainConfig.cbSize = ChainConfigSize;
  125. ChainConfig.hRestrictedRoot = NULL;
  126. ChainConfig.hRestrictedTrust = NULL;
  127. ChainConfig.hRestrictedOther = NULL;
  128. ChainConfig.cAdditionalStore = 0;
  129. ChainConfig.rghAdditionalStore = NULL;
  130. ChainConfig.dwFlags = CERT_CHAIN_CACHE_END_CERT;
  131. ChainConfig.dwUrlRetrievalTimeout = 0;
  132. ChainConfig.MaximumCachedCertificates =0;
  133. ChainConfig.CycleDetectionModulus = 0;
  134. HCERTCHAINENGINE ChainEngine;
  135. bool ChainEngineResult = CertCreateCertificateChainEngine(&ChainConfig, &ChainEngine);
  136. if (ChainEngineResult)
  137. {
  138. const CERT_CHAIN_CONTEXT * ChainContext = NULL;
  139. if (CertGetCertificateChain(ChainEngine, CertContext, NULL, NULL, &ChainPara,
  140. CERT_CHAIN_CACHE_END_CERT |
  141. CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT,
  142. NULL, &ChainContext))
  143. {
  144. CERT_CHAIN_POLICY_PARA PolicyPara;
  145. PolicyPara.cbSize = sizeof(PolicyPara);
  146. PolicyPara.dwFlags = 0;
  147. PolicyPara.pvExtraPolicyPara = NULL;
  148. CERT_CHAIN_POLICY_STATUS PolicyStatus;
  149. PolicyStatus.cbSize = sizeof(PolicyStatus);
  150. if (CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL,
  151. ChainContext, &PolicyPara, &PolicyStatus))
  152. {
  153. // Windows thinks the certificate is valid.
  154. Result = (PolicyStatus.dwError == S_OK);
  155. if (!Result)
  156. {
  157. Error = FORMAT(L"Error: %x, Chain index: %d, Element index: %d", (PolicyStatus.dwError, PolicyStatus.lChainIndex, PolicyStatus.lElementIndex));
  158. }
  159. }
  160. CertFreeCertificateChain(ChainContext);
  161. }
  162. CertFreeCertificateChainEngine(ChainEngine);
  163. }
  164. CertFreeCertificateContext(CertContext);
  165. }
  166. return Result;
  167. }
  168. //---------------------------------------------------------------------------