aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Common-Source/OpenSim.Framework/Remoting.cs
diff options
context:
space:
mode:
authorMW2007-05-24 12:16:50 +0000
committerMW2007-05-24 12:16:50 +0000
commit3376b82501000692d6dac24b051af738cdaf2737 (patch)
tree90ed0a5d4955236f011fa63fce9d555186b0d179 /Common-Source/OpenSim.Framework/Remoting.cs
parentAdded "terrain save grdmap <filename> <gradientmap>" function to console. Gra... (diff)
downloadopensim-SC_OLD-3376b82501000692d6dac24b051af738cdaf2737.zip
opensim-SC_OLD-3376b82501000692d6dac24b051af738cdaf2737.tar.gz
opensim-SC_OLD-3376b82501000692d6dac24b051af738cdaf2737.tar.bz2
opensim-SC_OLD-3376b82501000692d6dac24b051af738cdaf2737.tar.xz
Some more code refactoring, plus a restructuring of the directories so that the Grid servers can be a separate solution to the region server.
Diffstat (limited to 'Common-Source/OpenSim.Framework/Remoting.cs')
-rw-r--r--Common-Source/OpenSim.Framework/Remoting.cs109
1 files changed, 109 insertions, 0 deletions
diff --git a/Common-Source/OpenSim.Framework/Remoting.cs b/Common-Source/OpenSim.Framework/Remoting.cs
new file mode 100644
index 0000000..1212ee5
--- /dev/null
+++ b/Common-Source/OpenSim.Framework/Remoting.cs
@@ -0,0 +1,109 @@
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Security.Cryptography;
5
6namespace OpenSim.Framework
7{
8 /// <summary>
9 /// NEEDS AUDIT.
10 /// </summary>
11 /// <remarks>
12 /// Suggested implementation
13 /// <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>
14 /// <para>When sending data to the foreign host - run 'Sign' on the data and affix the returned byte[] to the message.</para>
15 /// <para>When recieving data from the foreign host - run 'Authenticate' against the data and the attached byte[].</para>
16 /// <para>Both hosts should be performing these operations for this to be effective.</para>
17 /// </remarks>
18 class RemoteDigest
19 {
20 private byte[] currentHash;
21 private byte[] secret;
22
23 private SHA512Managed SHA512;
24
25 /// <summary>
26 /// Initialises a new RemoteDigest authentication mechanism
27 /// </summary>
28 /// <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>
29 /// <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>
30 /// <param name="salt">Binary salt - some common value - to be decided what</param>
31 /// <param name="challenge">The challenge key provided by the third party</param>
32 public RemoteDigest(string sharedSecret, byte[] salt, string challenge)
33 {
34 SHA512 = new SHA512Managed();
35 Rfc2898DeriveBytes RFC2898 = new Rfc2898DeriveBytes(sharedSecret,salt);
36 secret = RFC2898.GetBytes(512);
37 ASCIIEncoding ASCII = new ASCIIEncoding();
38
39 currentHash = SHA512.ComputeHash(AppendArrays(secret, ASCII.GetBytes(challenge)));
40 }
41
42 /// <summary>
43 /// Authenticates a piece of incoming data against the local digest. Upon successful authentication, digest string is incremented.
44 /// </summary>
45 /// <param name="data">The incoming data</param>
46 /// <param name="digest">The remote digest</param>
47 /// <returns></returns>
48 public bool Authenticate(byte[] data, byte[] digest)
49 {
50 byte[] newHash = SHA512.ComputeHash(AppendArrays(AppendArrays(currentHash, secret), data));
51 if (digest == newHash)
52 {
53 currentHash = newHash;
54 return true;
55 }
56 else
57 {
58 throw new Exception("Hash comparison failed. Key resync required.");
59 }
60 }
61
62 /// <summary>
63 /// Signs a new bit of data with the current hash. Returns a byte array which should be affixed to the message.
64 /// Signing a piece of data will automatically increment the hash - if you sign data and do not send it, the
65 /// hashes will get out of sync and throw an exception when validation is attempted.
66 /// </summary>
67 /// <param name="data">The outgoing data</param>
68 /// <returns>The local digest</returns>
69 public byte[] Sign(byte[] data)
70 {
71 currentHash = SHA512.ComputeHash(AppendArrays(AppendArrays(currentHash, secret), data));
72 return currentHash;
73 }
74
75 /// <summary>
76 /// 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.
77 /// </summary>
78 /// <returns>A 128-character hexadecimal string containing the challenge.</returns>
79 public static string GenerateChallenge()
80 {
81 RNGCryptoServiceProvider RNG = new RNGCryptoServiceProvider();
82 byte[] bytes = new byte[64];
83 RNG.GetBytes(bytes);
84
85 StringBuilder sb = new StringBuilder(bytes.Length * 2);
86 foreach (byte b in bytes)
87 {
88 sb.AppendFormat("{0:x2}", b);
89 }
90 return sb.ToString();
91 }
92
93 /// <summary>
94 /// Helper function, merges two byte arrays
95 /// </summary>
96 /// <remarks>Sourced from MSDN Forum</remarks>
97 /// <param name="a">A</param>
98 /// <param name="b">B</param>
99 /// <returns>C</returns>
100 private byte[] AppendArrays(byte[] a, byte[] b)
101 {
102 byte[] c = new byte[a.Length + b.Length];
103 Buffer.BlockCopy(a, 0, c, 0, a.Length);
104 Buffer.BlockCopy(b, 0, c, a.Length, b.Length);
105 return c;
106 }
107
108 }
109}