Security.cpp 7.9 KB

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