1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
using System;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace Janrain.OpenId
{
public abstract class Association : ICloneable
{
#region Member Variables
protected internal static DateTime UNIX_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
private string handle;
private DateTime issued;
protected TimeSpan expiresIn;
protected byte[] key;
#endregion
#region Properties
public string Handle
{
get { return handle; }
set { handle = value; }
}
public DateTime Issued
{
get { return this.issued; }
set { this.issued = value; }
}
public byte[] Secret
{
get { return key; }
}
public uint IssuedUnix
{
get { return (uint)((this.issued - UNIX_EPOCH).TotalSeconds); }
}
public DateTime Expires
{
get { return this.issued.Add(this.expiresIn); }
}
public bool IsExpired
{
get { return this.Expires < DateTime.UtcNow; }
}
public long ExpiresIn
{
get { return (long)(this.Expires - DateTime.UtcNow).TotalSeconds; }
}
#endregion
#region Abstract Methods
public abstract string AssociationType
{
get;
}
public abstract string SignDict(string[] fields, NameValueCollection data, string prefix);
public abstract byte[] Sign(NameValueCollection l, IList<string> keyOrder);
#endregion
#region ICloneable Members
object ICloneable.Clone()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
#region Methods
public virtual byte[] Serialize()
{
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("version", "2");
dict.Add("handle", this.Handle);
dict.Add("secret", CryptUtil.ToBase64String(this.Secret));
dict.Add("issued", this.IssuedUnix.ToString());
dict.Add("expires_in", Convert.ToInt32(this.expiresIn.TotalSeconds).ToString());
dict.Add("assoc_type", this.AssociationType);
return KVUtil.DictToKV(dict);
}
public virtual Association Deserialize(byte[] data)
{
Dictionary<string, string> kvpairs = (Dictionary<string, string>)KVUtil.KVToDict(data);
string version = kvpairs["version"];
if (version != "2")
throw new NotSupportedException("Unknown version: " + version);
string assoc_type = kvpairs["assoc_type"];
if (assoc_type == "HMAC-SHA1")
return new HMACSHA1Association(kvpairs);
else
throw new NotSupportedException("Unknown association type: " + assoc_type);
}
#endregion
public override string ToString()
{
string returnString = @"Association.Handle= '{0}'
Association.Issued = '{1}'
Association.Secret = '{2}'
Association.IssuedUnix = '{3}'
Association.Expires = '{4}'
Association.IsExpired = '{5}'
Association.ExpiresIn = '{6}' ";
return String.Format(returnString, Handle, Issued.ToString(), Secret.ToString(), IssuedUnix, Expires.ToString(), IsExpired, ExpiresIn);
}
}
// TODO Move this class out to it's own file
public class HMACSHA1Association : Association
{
#region Constructor(s)
public HMACSHA1Association(string handle, byte[] secret, TimeSpan expiresIn)
{
this.Handle = handle;
this.key = secret;
this.Issued = UNIX_EPOCH.Add(new TimeSpan(0, 0, Convert.ToInt32((DateTime.UtcNow - UNIX_EPOCH).TotalSeconds)));
this.expiresIn = expiresIn;
}
public HMACSHA1Association(IDictionary kvpairs)
{
this.Handle = kvpairs["handle"].ToString();
this.key = Convert.FromBase64String(kvpairs["secret"].ToString());
int seconds = Convert.ToInt32(kvpairs["issued"]);
this.Issued = UNIX_EPOCH.Add(new TimeSpan(0, 0, seconds));
seconds = Convert.ToInt32(kvpairs["expires_in"]);
this.expiresIn = new TimeSpan(0, 0, seconds);
}
#endregion
#region Methods
public override string AssociationType
{
get { return "HMAC-SHA1"; }
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj.GetType() == typeof(HMACSHA1Association))
{
HMACSHA1Association a = (HMACSHA1Association)obj;
if (a.Handle != this.Handle)
return false;
if (CryptUtil.ToBase64String(a.Secret) != CryptUtil.ToBase64String(this.Secret))
return false;
if (a.Expires != this.Expires)
return false;
if (a.expiresIn != this.expiresIn)
return false;
return true;
}
return false;
}
public override int GetHashCode()
{
HMACSHA1 hmac = new HMACSHA1(this.Secret);
CryptoStream cs = new CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write);
byte[] hbytes = ASCIIEncoding.ASCII.GetBytes(this.Handle);
cs.Write(hbytes, 0, hbytes.Length);
cs.Close();
byte[] hash = hmac.Hash;
hmac.Clear();
long val = 0;
for (int i = 0; i < hash.Length; i++)
{
val = val ^ (long)hash[i];
}
val = val ^ this.Expires.ToFileTimeUtc();
return (int)val;
}
public override string SignDict(string[] fields, NameValueCollection data, string prefix)
{
NameValueCollection l = new NameValueCollection();
foreach (string field in fields)
{
l.Add(field, data[(prefix + field)]);
}
return CryptUtil.ToBase64String(Sign(l, fields));
}
public override byte[] Sign(NameValueCollection l, IList<string> keyOrder)
{
byte[] data = KVUtil.SeqToKV(l, keyOrder, false);
HMACSHA1 hmac = new HMACSHA1(this.Secret);
byte[] hash = hmac.ComputeHash(data);
hmac.Clear();
return hash;
}
#endregion
}
}
|