1
0

Remoting.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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. */
  28. using System;
  29. using System.Security.Cryptography;
  30. using System.Text;
  31. namespace OpenSim.Framework
  32. {
  33. /// <summary>
  34. /// NEEDS AUDIT.
  35. /// </summary>
  36. /// <remarks>
  37. /// Suggested implementation
  38. /// <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>
  39. /// <para>When sending data to the foreign host - run 'Sign' on the data and affix the returned byte[] to the message.</para>
  40. /// <para>When recieving data from the foreign host - run 'Authenticate' against the data and the attached byte[].</para>
  41. /// <para>Both hosts should be performing these operations for this to be effective.</para>
  42. /// </remarks>
  43. class RemoteDigest
  44. {
  45. private byte[] currentHash;
  46. private byte[] secret;
  47. private SHA512Managed SHA512;
  48. /// <summary>
  49. /// Initialises a new RemoteDigest authentication mechanism
  50. /// </summary>
  51. /// <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>
  52. /// <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>
  53. /// <param name="salt">Binary salt - some common value - to be decided what</param>
  54. /// <param name="challenge">The challenge key provided by the third party</param>
  55. public RemoteDigest(string sharedSecret, byte[] salt, string challenge)
  56. {
  57. SHA512 = new SHA512Managed();
  58. Rfc2898DeriveBytes RFC2898 = new Rfc2898DeriveBytes(sharedSecret,salt);
  59. secret = RFC2898.GetBytes(512);
  60. ASCIIEncoding ASCII = new ASCIIEncoding();
  61. currentHash = SHA512.ComputeHash(AppendArrays(secret, ASCII.GetBytes(challenge)));
  62. }
  63. /// <summary>
  64. /// Authenticates a piece of incoming data against the local digest. Upon successful authentication, digest string is incremented.
  65. /// </summary>
  66. /// <param name="data">The incoming data</param>
  67. /// <param name="digest">The remote digest</param>
  68. /// <returns></returns>
  69. public bool Authenticate(byte[] data, byte[] digest)
  70. {
  71. byte[] newHash = SHA512.ComputeHash(AppendArrays(AppendArrays(currentHash, secret), data));
  72. if (digest == newHash)
  73. {
  74. currentHash = newHash;
  75. return true;
  76. }
  77. else
  78. {
  79. throw new Exception("Hash comparison failed. Key resync required.");
  80. }
  81. }
  82. /// <summary>
  83. /// Signs a new bit of data with the current hash. Returns a byte array which should be affixed to the message.
  84. /// Signing a piece of data will automatically increment the hash - if you sign data and do not send it, the
  85. /// hashes will get out of sync and throw an exception when validation is attempted.
  86. /// </summary>
  87. /// <param name="data">The outgoing data</param>
  88. /// <returns>The local digest</returns>
  89. public byte[] Sign(byte[] data)
  90. {
  91. currentHash = SHA512.ComputeHash(AppendArrays(AppendArrays(currentHash, secret), data));
  92. return currentHash;
  93. }
  94. /// <summary>
  95. /// 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.
  96. /// </summary>
  97. /// <returns>A 128-character hexadecimal string containing the challenge.</returns>
  98. public static string GenerateChallenge()
  99. {
  100. RNGCryptoServiceProvider RNG = new RNGCryptoServiceProvider();
  101. byte[] bytes = new byte[64];
  102. RNG.GetBytes(bytes);
  103. StringBuilder sb = new StringBuilder(bytes.Length * 2);
  104. foreach (byte b in bytes)
  105. {
  106. sb.AppendFormat("{0:x2}", b);
  107. }
  108. return sb.ToString();
  109. }
  110. /// <summary>
  111. /// Helper function, merges two byte arrays
  112. /// </summary>
  113. /// <remarks>Sourced from MSDN Forum</remarks>
  114. /// <param name="a">A</param>
  115. /// <param name="b">B</param>
  116. /// <returns>C</returns>
  117. private byte[] AppendArrays(byte[] a, byte[] b)
  118. {
  119. byte[] c = new byte[a.Length + b.Length];
  120. Buffer.BlockCopy(a, 0, c, 0, a.Length);
  121. Buffer.BlockCopy(b, 0, c, a.Length, b.Length);
  122. return c;
  123. }
  124. }
  125. }