Remoting.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Security.Cryptography;
  29. using System.Text;
  30. namespace OpenSim.Framework
  31. {
  32. /// <summary>
  33. /// NEEDS AUDIT.
  34. /// </summary>
  35. /// <remarks>
  36. /// Suggested implementation
  37. /// <para>Store two digests for each foreign host. A local copy of the local hash using the local challenge (when issued), and a local copy of the remote hash using the remote challenge.</para>
  38. /// <para>When sending data to the foreign host - run 'Sign' on the data and affix the returned byte[] to the message.</para>
  39. /// <para>When receiving data from the foreign host - run 'Authenticate' against the data and the attached byte[].</para>
  40. /// <para>Both hosts should be performing these operations for this to be effective.</para>
  41. /// </remarks>
  42. internal class RemoteDigest
  43. {
  44. private byte[] currentHash;
  45. private byte[] secret;
  46. private SHA512Managed SHA512;
  47. /// <summary>
  48. /// Initialises a new RemoteDigest authentication mechanism
  49. /// </summary>
  50. /// <remarks>Needs an audit by a cryptographic professional - was not "roll your own"'d by choice but rather a serious lack of decent authentication mechanisms in .NET remoting</remarks>
  51. /// <param name="sharedSecret">The shared secret between systems (for inter-sim, this is provided in encrypted form during connection, for grid this is input manually in setup)</param>
  52. /// <param name="salt">Binary salt - some common value - to be decided what</param>
  53. /// <param name="challenge">The challenge key provided by the third party</param>
  54. public RemoteDigest(string sharedSecret, byte[] salt, string challenge)
  55. {
  56. SHA512 = new SHA512Managed();
  57. Rfc2898DeriveBytes RFC2898 = new Rfc2898DeriveBytes(sharedSecret, salt);
  58. secret = RFC2898.GetBytes(512);
  59. ASCIIEncoding ASCII = new ASCIIEncoding();
  60. currentHash = SHA512.ComputeHash(AppendArrays(secret, ASCII.GetBytes(challenge)));
  61. }
  62. /// <summary>
  63. /// Authenticates a piece of incoming data against the local digest. Upon successful authentication, digest string is incremented.
  64. /// </summary>
  65. /// <param name="data">The incoming data</param>
  66. /// <param name="digest">The remote digest</param>
  67. /// <returns></returns>
  68. public bool Authenticate(byte[] data, byte[] digest)
  69. {
  70. byte[] newHash = SHA512.ComputeHash(AppendArrays(AppendArrays(currentHash, secret), data));
  71. if (digest == newHash)
  72. {
  73. currentHash = newHash;
  74. return true;
  75. }
  76. else
  77. {
  78. throw new Exception("Hash comparison failed. Key resync required.");
  79. }
  80. }
  81. /// <summary>
  82. /// Signs a new bit of data with the current hash. Returns a byte array which should be affixed to the message.
  83. /// Signing a piece of data will automatically increment the hash - if you sign data and do not send it, the
  84. /// hashes will get out of sync and throw an exception when validation is attempted.
  85. /// </summary>
  86. /// <param name="data">The outgoing data</param>
  87. /// <returns>The local digest</returns>
  88. public byte[] Sign(byte[] data)
  89. {
  90. currentHash = SHA512.ComputeHash(AppendArrays(AppendArrays(currentHash, secret), data));
  91. return currentHash;
  92. }
  93. /// <summary>
  94. /// Generates a new challenge string to be issued to a foreign host. Challenges are 1024-bit (effective strength of less than 512-bits) messages generated using the Crytographic Random Number Generator.
  95. /// </summary>
  96. /// <returns>A 128-character hexadecimal string containing the challenge.</returns>
  97. public static string GenerateChallenge()
  98. {
  99. RNGCryptoServiceProvider RNG = new RNGCryptoServiceProvider();
  100. byte[] bytes = new byte[64];
  101. RNG.GetBytes(bytes);
  102. StringBuilder sb = new StringBuilder(bytes.Length*2);
  103. foreach (byte b in bytes)
  104. {
  105. sb.AppendFormat("{0:x2}", b);
  106. }
  107. return sb.ToString();
  108. }
  109. /// <summary>
  110. /// Helper function, merges two byte arrays
  111. /// </summary>
  112. /// <remarks>Sourced from MSDN Forum</remarks>
  113. /// <param name="a">A</param>
  114. /// <param name="b">B</param>
  115. /// <returns>C</returns>
  116. private byte[] AppendArrays(byte[] a, byte[] b)
  117. {
  118. byte[] c = new byte[a.Length + b.Length];
  119. Buffer.BlockCopy(a, 0, c, 0, a.Length);
  120. Buffer.BlockCopy(b, 0, c, a.Length, b.Length);
  121. return c;
  122. }
  123. }
  124. }