Remoting.cs 6.4 KB

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