Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

Commit

Permalink
Added check that Associations are created with the appropriate key le…
Browse files Browse the repository at this point in the history
…ngth.
  • Loading branch information
AArnott committed Apr 4, 2008
1 parent ff41dce commit e7c391c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/DotNetOpenId.Test/AssociationTestSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,36 @@ namespace DotNetOpenId.Test
[TestFixture]
public class AssociationTestSuite {
static readonly TimeSpan deltaDateTime = TimeSpan.FromSeconds(2);
byte[] sha1Secret = new byte[CryptUtil.Sha1.HashSize / 8];
byte[] sha1Secret2 = new byte[CryptUtil.Sha1.HashSize / 8];

public AssociationTestSuite() {
// just a little something to make it at all interesting.
sha1Secret[0] = 0x33;
sha1Secret[1] = 0x55;

sha1Secret2[0] = 0x88;
sha1Secret2[1] = 0xcc;
}

[Test]
public void Properties() {
string handle = "somehandle";
byte[] key = new byte[] { 0x33, 0x55 };
TimeSpan lifetime = TimeSpan.FromMinutes(2);
Association assoc = new HmacSha1Association(handle, key, lifetime);
Association assoc = new HmacSha1Association(handle, sha1Secret, lifetime);
Assert.IsFalse(assoc.IsExpired);
Assert.IsTrue(Math.Abs((DateTime.Now - assoc.Issued.ToLocalTime()).TotalSeconds) < deltaDateTime.TotalSeconds);
Assert.IsTrue(Math.Abs((DateTime.Now.ToLocalTime() + lifetime - assoc.Expires.ToLocalTime()).TotalSeconds) < deltaDateTime.TotalSeconds);
Assert.AreEqual(handle, assoc.Handle);
Assert.IsTrue(Math.Abs(lifetime.TotalSeconds - assoc.SecondsTillExpiration) < deltaDateTime.TotalSeconds);
Assert.IsTrue(Util.ArrayEquals(key, assoc.SecretKey));
Assert.IsTrue(Util.ArrayEquals(sha1Secret, assoc.SecretKey));
Assert.AreEqual(0, assoc.Issued.Millisecond, "No milliseconds because this can be cut off in conversions.");
}

[Test]
public void Sign() {
Association assoc1 = new HmacSha1Association("h1", Encoding.ASCII.GetBytes("secret1"), TimeSpan.FromMinutes(2));
Association assoc2 = new HmacSha1Association("h2", Encoding.ASCII.GetBytes("secret2"), TimeSpan.FromMinutes(2));
Association assoc1 = new HmacSha1Association("h1", sha1Secret, TimeSpan.FromMinutes(2));
Association assoc2 = new HmacSha1Association("h2", sha1Secret2, TimeSpan.FromMinutes(2));

var dict = new Dictionary<string, string>();
dict.Add("a", "b");
Expand Down Expand Up @@ -67,7 +77,7 @@ public void Sign() {

[Test]
public void SignSome() {
Association assoc = new HmacSha1Association("h1", Encoding.ASCII.GetBytes("secret1"), TimeSpan.FromMinutes(2));
Association assoc = new HmacSha1Association("h1", sha1Secret, TimeSpan.FromMinutes(2));
const string prefix = "q.";

var dict = new Dictionary<string, string>();
Expand Down
7 changes: 4 additions & 3 deletions src/DotNetOpenId.Test/AssociationsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace DotNetOpenId.Test {
[TestFixture]
public class AssociationsTest {
byte[] sha1Secret = new byte[CryptUtil.Sha1.HashSize / 8];

Associations assocs;
[SetUp]
Expand All @@ -26,7 +27,7 @@ public void RemoveNonexistentHandle() {

[Test]
public void HandleLifecycle() {
Association a = new HmacSha1Association("somehandle", new byte[0], TimeSpan.FromDays(1));
Association a = new HmacSha1Association("somehandle", sha1Secret, TimeSpan.FromDays(1));
assocs.Set(a);
Assert.AreSame(a, assocs.Get(a.Handle));
Assert.IsTrue(assocs.Remove(a.Handle));
Expand All @@ -36,8 +37,8 @@ public void HandleLifecycle() {

[Test]
public void Best() {
Association a = new HmacSha1Association("h1", new byte[0], TimeSpan.FromHours(1));
Association b = new HmacSha1Association("h2", new byte[0], TimeSpan.FromHours(1));
Association a = new HmacSha1Association("h1", sha1Secret, TimeSpan.FromHours(1));
Association b = new HmacSha1Association("h2", sha1Secret, TimeSpan.FromHours(1));

assocs.Set(a);
assocs.Set(b);
Expand Down
2 changes: 2 additions & 0 deletions src/DotNetOpenId/HmacSha1Association.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Diagnostics;

namespace DotNetOpenId {
internal class HmacSha1Association : Association {

public HmacSha1Association(string handle, byte[] secret, TimeSpan totalLifeLength)
: base(handle, secret, totalLifeLength, DateTime.UtcNow) {
Debug.Assert(secret.Length == CryptUtil.Sha1.HashSize / 8);
}

internal override string GetAssociationType(Protocol protocol) {
Expand Down
2 changes: 2 additions & 0 deletions src/DotNetOpenId/HmacSha256Association.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Diagnostics;

namespace DotNetOpenId {
class HmacSha256Association : Association {
public HmacSha256Association(string handle, byte[] secret, TimeSpan totalLifeLength)
: base(handle, secret, totalLifeLength, DateTime.UtcNow) {
Debug.Assert(secret.Length == CryptUtil.Sha256.HashSize / 8);
}

protected override HashAlgorithm CreateHasher() {
Expand Down

0 comments on commit e7c391c

Please sign in to comment.