Security.cpp 7.9 KB

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