You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CSecurityDH.cxx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2022 Dinglan Peng
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #ifndef HAVE_NETTLE
  23. #error "This header should not be compiled without HAVE_NETTLE defined"
  24. #endif
  25. #include <stdlib.h>
  26. #ifndef WIN32
  27. #include <unistd.h>
  28. #endif
  29. #include <assert.h>
  30. #include <nettle/aes.h>
  31. #include <nettle/md5.h>
  32. #include <nettle/bignum.h>
  33. #include <rfb/CSecurityDH.h>
  34. #include <rfb/CConnection.h>
  35. #include <rdr/InStream.h>
  36. #include <rdr/OutStream.h>
  37. #include <rdr/RandomStream.h>
  38. #include <rfb/Exception.h>
  39. #include <os/os.h>
  40. using namespace rfb;
  41. const int MinKeyLength = 128;
  42. const int MaxKeyLength = 1024;
  43. CSecurityDH::CSecurityDH(CConnection* cc)
  44. : CSecurity(cc), keyLength(0)
  45. {
  46. mpz_init(g);
  47. mpz_init(p);
  48. mpz_init(A);
  49. mpz_init(b);
  50. mpz_init(B);
  51. mpz_init(k);
  52. }
  53. CSecurityDH::~CSecurityDH()
  54. {
  55. mpz_clear(g);
  56. mpz_clear(p);
  57. mpz_clear(A);
  58. mpz_clear(b);
  59. mpz_clear(B);
  60. mpz_clear(k);
  61. }
  62. bool CSecurityDH::processMsg()
  63. {
  64. if (readKey()) {
  65. writeCredentials();
  66. return true;
  67. }
  68. return false;
  69. }
  70. bool CSecurityDH::readKey()
  71. {
  72. rdr::InStream* is = cc->getInStream();
  73. if (!is->hasData(4))
  74. return false;
  75. is->setRestorePoint();
  76. uint16_t gen = is->readU16();
  77. keyLength = is->readU16();
  78. if (keyLength < MinKeyLength)
  79. throw AuthFailureException("DH key is too short");
  80. if (keyLength > MaxKeyLength)
  81. throw AuthFailureException("DH key is too long");
  82. if (!is->hasDataOrRestore(keyLength * 2))
  83. return false;
  84. is->clearRestorePoint();
  85. mpz_set_ui(g, gen);
  86. std::vector<uint8_t> pBytes(keyLength);
  87. std::vector<uint8_t> ABytes(keyLength);
  88. is->readBytes(pBytes.data(), pBytes.size());
  89. is->readBytes(ABytes.data(), ABytes.size());
  90. nettle_mpz_set_str_256_u(p, pBytes.size(), pBytes.data());
  91. nettle_mpz_set_str_256_u(A, ABytes.size(), ABytes.data());
  92. return true;
  93. }
  94. void CSecurityDH::writeCredentials()
  95. {
  96. std::string username;
  97. std::string password;
  98. rdr::RandomStream rs;
  99. (CSecurity::upg)->getUserPasswd(isSecure(), &username, &password);
  100. std::vector<uint8_t> bBytes(keyLength);
  101. if (!rs.hasData(keyLength))
  102. throw ConnFailedException("failed to generate DH private key");
  103. rs.readBytes(bBytes.data(), bBytes.size());
  104. nettle_mpz_set_str_256_u(b, bBytes.size(), bBytes.data());
  105. mpz_powm(k, A, b, p);
  106. mpz_powm(B, g, b, p);
  107. std::vector<uint8_t> sharedSecret(keyLength);
  108. std::vector<uint8_t> BBytes(keyLength);
  109. nettle_mpz_get_str_256(sharedSecret.size(), sharedSecret.data(), k);
  110. nettle_mpz_get_str_256(BBytes.size(), BBytes.data(), B);
  111. uint8_t key[16];
  112. struct md5_ctx md5Ctx;
  113. md5_init(&md5Ctx);
  114. md5_update(&md5Ctx, sharedSecret.size(), sharedSecret.data());
  115. md5_digest(&md5Ctx, 16, key);
  116. struct aes128_ctx aesCtx;
  117. aes128_set_encrypt_key(&aesCtx, key);
  118. uint8_t buf[128];
  119. if (!rs.hasData(128))
  120. throw ConnFailedException("failed to generate random padding");
  121. rs.readBytes(buf, 128);
  122. if (username.size() >= 64)
  123. throw AuthFailureException("username is too long");
  124. memcpy(buf, username.c_str(), username.size() + 1);
  125. if (password.size() >= 64)
  126. throw AuthFailureException("password is too long");
  127. memcpy(buf + 64, password.c_str(), password.size() + 1);
  128. aes128_encrypt(&aesCtx, 128, buf, buf);
  129. rdr::OutStream* os = cc->getOutStream();
  130. os->writeBytes(buf, 128);
  131. os->writeBytes(BBytes.data(), BBytes.size());
  132. os->flush();
  133. }