Cryptographic framework for authenticating and verifying remoting messages. Must be implemented as per the suggested implementation in the comments. Buyer beware - this code has not been peer reviewed by an expert cryptologist and needs to be done so before this security is relied upon for important transactions.
parent
14a4ff3081
commit
379552b0f9
|
@ -0,0 +1,90 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
|
namespace OpenSim.Framework
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// NEEDS AUDIT.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Suggested implementation
|
||||||
|
/// <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>
|
||||||
|
/// <para>When sending data to the foreign host - run 'Sign' on the data and affix the returned byte[] to the message.</para>
|
||||||
|
/// <para>When recieving data from the foreign host - run 'Authenticate' against the data and the attached byte[].</para>
|
||||||
|
/// <para>Both hosts should be performing these operations for this to be effective.</para>
|
||||||
|
/// </remarks>
|
||||||
|
class RemoteDigest
|
||||||
|
{
|
||||||
|
private byte[] currentHash;
|
||||||
|
private byte[] secret;
|
||||||
|
|
||||||
|
private SHA512Managed SHA512;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialises a new RemoteDigest authentication mechanism
|
||||||
|
/// </summary>
|
||||||
|
/// <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>
|
||||||
|
/// <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>
|
||||||
|
/// <param name="salt">Binary salt - some common value - to be decided what</param>
|
||||||
|
/// <param name="challenge">The challenge key provided by the third party</param>
|
||||||
|
public RemoteDigest(string sharedSecret, byte[] salt, string challenge)
|
||||||
|
{
|
||||||
|
SHA512 = new SHA512Managed();
|
||||||
|
Rfc2898DeriveBytes RFC2898 = new Rfc2898DeriveBytes(sharedSecret,salt);
|
||||||
|
secret = RFC2898.GetBytes(512);
|
||||||
|
ASCIIEncoding ASCII = new ASCIIEncoding();
|
||||||
|
|
||||||
|
currentHash = SHA512.ComputeHash(AppendArrays(secret, ASCII.GetBytes(challenge)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Authenticates a piece of incoming data against the local digest. Upon successful authentication, digest string is incremented.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data">The incoming data</param>
|
||||||
|
/// <param name="digest">The remote digest</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool Authenticate(byte[] data, byte[] digest)
|
||||||
|
{
|
||||||
|
byte[] newHash = SHA512.ComputeHash(AppendArrays(AppendArrays(currentHash, secret), data));
|
||||||
|
if (digest == newHash)
|
||||||
|
{
|
||||||
|
currentHash = newHash;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("Hash comparison failed. Key resync required.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Signs a new bit of data with the current hash. Returns a byte array which should be affixed to the message.
|
||||||
|
/// Signing a piece of data will automatically increment the hash - if you sign data and do not send it, the
|
||||||
|
/// hashes will get out of sync and throw an exception when validation is attempted.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data">The outgoing data</param>
|
||||||
|
/// <returns>The local digest</returns>
|
||||||
|
public byte[] Sign(byte[] data)
|
||||||
|
{
|
||||||
|
currentHash = SHA512.ComputeHash(AppendArrays(AppendArrays(currentHash, secret), data));
|
||||||
|
return currentHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Helper function, merges two byte arrays
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="a">A</param>
|
||||||
|
/// <param name="b">B</param>
|
||||||
|
/// <returns>C</returns>
|
||||||
|
private byte[] AppendArrays(byte[] a, byte[] b)
|
||||||
|
{
|
||||||
|
byte[] c = new byte[a.Length + b.Length];
|
||||||
|
Buffer.BlockCopy(a, 0, c, 0, a.Length);
|
||||||
|
Buffer.BlockCopy(b, 0, c, a.Length, b.Length);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue