Skip to content

Commit

Permalink
Merge pull request #3110 from Particular/hotfix-5.0.9
Browse files Browse the repository at this point in the history
Hotfix 5.0.9
  • Loading branch information
ramonsmits committed Dec 3, 2015
2 parents 707eea4 + 8472e1d commit 0bb48ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,6 @@ public void Should_throw_for_invalid_expired_key()
Assert.AreEqual("The expired key at index 0 has an invalid length of 10 bytes.", exception.Message);
}

[Test]
public void Encrypt_must_set_header()
{
var encryptionKey1 = Encoding.ASCII.GetBytes("gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6");
var service1 = new RijndaelEncryptionServiceWithFakeBus("encryptionKey1", encryptionKey1, new List<byte[]>());

Assert.AreEqual(false, service1.OutgoingKeyIdentifierSet);
service1.Encrypt("string to encrypt");
Assert.AreEqual(true, service1.OutgoingKeyIdentifierSet);
}

[Test]
public void Decrypt_using_key_identifier()
{
Expand Down Expand Up @@ -174,6 +163,24 @@ public void Should_throw_informative_exception_when_decryption_fails_with_key_id
}, "Unable to decrypt property using configured decryption key specified in key identifier header.");
}

[Test]
public void Should_set_header_when_created_and_has_value()
{
var encryptionKey1 = Encoding.ASCII.GetBytes("gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6");
var service1 = new RijndaelEncryptionServiceWithFakeBus("encryptionKey1", encryptionKey1, new List<byte[]>());

Assert.AreEqual(true, service1.OutgoingKeyIdentifierSet);
}

[Test]
public void Should_not_set_header_when_created_and_no_value()
{
var encryptionKey1 = Encoding.ASCII.GetBytes("gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6");
var service1 = new RijndaelEncryptionServiceWithFakeBus(null, encryptionKey1, new List<byte[]>());

Assert.AreEqual(false, service1.OutgoingKeyIdentifierSet);
}

class RijndaelEncryptionServiceWithFakeBus : RijndaelEncryptionService
{
public bool OutgoingKeyIdentifierSet { get; private set; }
Expand Down
14 changes: 5 additions & 9 deletions src/NServiceBus.Core/Encryption/RijndaelEncryptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ IList<byte[]> decryptionKeys
}

VerifyExpiredKeys(decryptionKeys);

if (encryptionKeyIdentifier != null)
AddKeyIdentifierHeader();
}

public string Decrypt(EncryptedValue encryptedValue)
Expand Down Expand Up @@ -144,11 +147,9 @@ public EncryptedValue Encrypt(string value)
{
if (string.IsNullOrEmpty(encryptionKeyIdentifier))
{
throw new InvalidOperationException("It is required to set the rijndael key identifer.");
throw new InvalidOperationException("It is required to set the rijndael key identifier.");
}

AddKeyIdentifierHeader();

using (var rijndael = new RijndaelManaged())
{
rijndael.Key = encryptionKey;
Expand Down Expand Up @@ -208,12 +209,7 @@ static bool IsValidKey(byte[] key)

protected virtual void AddKeyIdentifierHeader()
{
var outgoingHeaders = bus.OutgoingHeaders;

if (!outgoingHeaders.ContainsKey(Headers.RijndaelKeyIdentifier))
{
outgoingHeaders.Add(Headers.RijndaelKeyIdentifier, encryptionKeyIdentifier);
}
bus.OutgoingHeaders[Headers.RijndaelKeyIdentifier] = encryptionKeyIdentifier;
}

protected virtual bool TryGetKeyIdentifierHeader(out string keyIdentifier)
Expand Down

0 comments on commit 0bb48ba

Please sign in to comment.