SymmetricCrypt.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <string>
  19. #include <kopano/stringutil.h>
  20. #include <kopano/charset/convert.h>
  21. #include <cassert>
  22. #include "SymmetricCrypt.h"
  23. namespace KC {
  24. /**
  25. * Check if the provided password is crypted.
  26. *
  27. * Crypted passwords have the format "{N}:<crypted password>, with N being the encryption algorithm.
  28. * Currently only algorithm number 1 and 2 are supported:
  29. * 1: base64-of-XOR-A5 of windows-1252 encoded data
  30. * 2: base64-of-XOR-A5 of UTF-8 encoded data
  31. *
  32. * @param[in] strCrypted
  33. * The string to test.
  34. *
  35. * @return boolean
  36. * @retval true The provided string was encrypted.
  37. * @retval false The provided string was not encrypted.
  38. */
  39. bool SymmetricIsCrypted(const char *c)
  40. {
  41. return strncmp(c, "{1}:", 4) == 0 || strncmp(c, "{2}:", 4) == 0;
  42. }
  43. /**
  44. * Check if the provided password is crypted.
  45. *
  46. * Crypted passwords have the format "{N}:<crypted password>, with N being the encryption algorithm.
  47. * Currently only algorithm number 1 and 2 are supported:
  48. * 1: base64-of-XOR-A5 of windows-1252 encoded data
  49. * 2: base64-of-XOR-A5 of UTF-8 encoded data
  50. *
  51. * @param[in] strCrypted
  52. * The wide character string to test.
  53. *
  54. * @return boolean
  55. * @retval true The provided string was encrypted.
  56. * @retval false The provided string was not encrypted.
  57. */
  58. bool SymmetricIsCrypted(const wchar_t *c)
  59. {
  60. return wcsncmp(c, L"{1}:", 4) == 0 || wcsncmp(c, L"{2}:", 4) == 0;
  61. }
  62. /**
  63. * Decrypt the crypt data.
  64. *
  65. * Depending on the N value, the password is decrypted using algorithm 1 or 2.
  66. *
  67. * @param[in] ulAlg
  68. * The number selecting the algorithm. (1 or 2)
  69. * @param[in] strXORed
  70. * The binary data to decrypt.
  71. *
  72. * @return The decrypted password encoded in UTF-8.
  73. */
  74. static std::string SymmetricDecryptBlob(unsigned int ulAlg, const std::string &strXORed)
  75. {
  76. std::string strRaw = strXORed;
  77. size_t z = strRaw.size();
  78. assert(ulAlg == 1 || ulAlg == 2);
  79. for (unsigned int i = 0; i < z; ++i)
  80. strRaw[i] ^= 0xA5;
  81. // Check the encoding algorithm. If it equals 1, the raw data is windows-1252.
  82. // Otherwise, it must be 2, which means it is already UTF-8.
  83. if (ulAlg == 1)
  84. strRaw = convert_to<std::string>("UTF-8", strRaw, rawsize(strRaw), "WINDOWS-1252");
  85. return strRaw;
  86. }
  87. /**
  88. * Decrypt an encrypted password.
  89. *
  90. * Depending on the N value, the password is decrypted using algorithm 1 or 2.
  91. *
  92. * @param[in] strCrypted
  93. * The UTF-8 encoded encrypted password to decrypt.
  94. *
  95. * @return THe decrypted password encoded in UTF-8.
  96. */
  97. std::string SymmetricDecrypt(const char *strCrypted)
  98. {
  99. if (!SymmetricIsCrypted(strCrypted))
  100. return "";
  101. // Length has been guaranteed to be >=4.
  102. return SymmetricDecryptBlob(strCrypted[1] - '0',
  103. base64_decode(convert_to<std::string>(strCrypted + 4)));
  104. }
  105. /**
  106. * Decrypt an encrypted password.
  107. *
  108. * Depending on the N value, the password is decrypted using algorithm 1 or 2.
  109. *
  110. * @param[in] strCrypted
  111. * The wide character encrypted password to decrypt.
  112. *
  113. * @return THe decrypted password encoded in UTF-8.
  114. */
  115. std::string SymmetricDecrypt(const wchar_t *wstrCrypted)
  116. {
  117. if (!SymmetricIsCrypted(wstrCrypted))
  118. return "";
  119. // Length has been guaranteed to be >=4.
  120. return SymmetricDecryptBlob(wstrCrypted[1] - '0',
  121. base64_decode(convert_to<std::string>(wstrCrypted + 4)));
  122. }
  123. } /* namespace */