Skip to content

Commit

Permalink
Merge branch 'hotfix-4.0.7' into support-4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
John Simons committed Aug 19, 2014
2 parents f030d22 + a42ad45 commit 28c580d
Show file tree
Hide file tree
Showing 46 changed files with 69 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,5 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
<Import Project="Fody.targets" />
</Project>
13 changes: 13 additions & 0 deletions src/NServiceBus.Core.Tests/Encryption/EncryptionServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,18 @@ public void Should_throw_when_decrypt_with_wrong_key()
}
}

[Test]
public void Should_throw_when_encrypt_and_decrypt_keys_are_too_similar()
{
var key = Encoding.ASCII.GetBytes("gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6");
var service = new EncryptionService
{
Key = key,
ExpiredKeys = new List<byte[]>{ key } //note that we use the same key to get the code to throw
};
var exception = Assert.Throws<Exception>(service.VerifyKeysAreNotTooSimilar);

Assert.AreEqual("The new Encryption Key is too similar to the Expired Key at index 0. This can cause issues when decrypting data. To fix this issue please ensure the new encryption key is not too similar to the existing Expired Keys.", exception.Message);
}
}
}
1 change: 0 additions & 1 deletion src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,4 @@
<Folder Include="Queuing\SqlServer\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
12 changes: 9 additions & 3 deletions src/NServiceBus.Core/ConfigureRijndaelEncryptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace NServiceBus
using System.Linq;
using System.Text;
using Config;
using Encryption;
using Encryption.Rijndael;
using Logging;

Expand All @@ -25,7 +26,7 @@ public static Configure RijndaelEncryptionService(this Configure config)
if (section == null)
Logger.Warn("Could not find configuration section for Rijndael Encryption Service.");

var encryptConfig = config.Configurer.ConfigureComponent<EncryptionService>(DependencyLifecycle.SingleInstance);
var encryptionService = new EncryptionService();

if (section != null)
{
Expand All @@ -34,10 +35,15 @@ public static Configure RijndaelEncryptionService(this Configure config)
throw new Exception("The RijndaelEncryptionServiceConfig has an empty 'Key' attribute.");
}
var expiredKeys = ExtractExpiredKeysFromConfigSection(section);
encryptConfig.ConfigureProperty(s => s.Key, Encoding.ASCII.GetBytes(section.Key));
encryptConfig.ConfigureProperty(s => s.ExpiredKeys, expiredKeys.Select(x=>Encoding.ASCII.GetBytes(x)).ToList());

encryptionService.Key = Encoding.ASCII.GetBytes(section.Key);
encryptionService.ExpiredKeys = expiredKeys.Select(x => Encoding.ASCII.GetBytes(x)).ToList();
}

encryptionService.VerifyKeysAreNotTooSimilar();

config.Configurer.RegisterSingleton<IEncryptionService>(encryptionService);

return config;
}

Expand Down
73 changes: 47 additions & 26 deletions src/NServiceBus.Core/Encryption/Rijndael/EncryptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,48 @@ string IEncryptionService.Decrypt(EncryptedValue encryptedValue)
return encryptedValue.EncryptedBase64Value;
}

var decryptionKeys = new List<byte[]>{Key};
var decryptionKeys = new List<byte[]> { Key };
if (ExpiredKeys != null)
{
decryptionKeys.AddRange(ExpiredKeys);
}
var encrypted = Convert.FromBase64String(encryptedValue.EncryptedBase64Value);
var cryptographicExceptions = new List<CryptographicException>();
using (var rijndael = new RijndaelManaged())
{
rijndael.IV = Convert.FromBase64String(encryptedValue.Base64Iv);
rijndael.Mode = CipherMode.CBC;

foreach (var key in decryptionKeys)
foreach (var key in decryptionKeys)
{
try
{
rijndael.Key = key;
try
{
return Decrypt(rijndael, encrypted);
}
catch (CryptographicException exception)
{
cryptographicExceptions.Add(exception);
}
return Decrypt(encryptedValue, key);
}
catch (CryptographicException exception)
{
cryptographicExceptions.Add(exception);
}
}
var message = string.Format("Could not decrypt message. Tried {0} keys.", decryptionKeys.Count);
throw new AggregateException(message, cryptographicExceptions);
}
static string Decrypt(RijndaelManaged rijndael, byte[] encrypted)

static string Decrypt(EncryptedValue encryptedValue, byte[] key)
{
using (var decryptor = rijndael.CreateDecryptor())
using (var memoryStream = new MemoryStream(encrypted))
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
using (var reader = new StreamReader(cryptoStream))
using (var rijndael = new RijndaelManaged())
{
return reader.ReadToEnd();
var encrypted = Convert.FromBase64String(encryptedValue.EncryptedBase64Value);
rijndael.IV = Convert.FromBase64String(encryptedValue.Base64Iv);
rijndael.Mode = CipherMode.CBC;
rijndael.Key = key;
using (var decryptor = rijndael.CreateDecryptor())
using (var memoryStream = new MemoryStream(encrypted))
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
using (var reader = new StreamReader(cryptoStream))
{
return reader.ReadToEnd();
}
}
}

EncryptedValue IEncryptionService.Encrypt(string value)
{
if (Key == null)
throw new InvalidOperationException("Cannot encrypt because a Key was not configured. Please specify 'RijndaelEncryptionServiceConfig' in your application's configuration file.");

using (var rijndael = new RijndaelManaged())
{
rijndael.Key = Key;
Expand All @@ -99,6 +97,29 @@ EncryptedValue IEncryptionService.Encrypt(string value)
}
}

private static readonly ILog Logger = LogManager.GetLogger(typeof (EncryptionService));
internal void VerifyKeysAreNotTooSimilar()
{
for (var index = 0; index < ExpiredKeys.Count; index++)
{
var decryption = ExpiredKeys[index];
CryptographicException exception = null;
var encryptedValue = ((IEncryptionService) this).Encrypt("a");
try
{
Decrypt(encryptedValue, decryption);
}
catch (CryptographicException cryptographicException)
{
exception = cryptographicException;
}
if (exception == null)
{
var message = string.Format("The new Encryption Key is too similar to the Expired Key at index {0}. This can cause issues when decrypting data. To fix this issue please ensure the new encryption key is not too similar to the existing Expired Keys.", index);
throw new Exception(message);
}
}
}

static readonly ILog Logger = LogManager.GetLogger(typeof(EncryptionService));
}
}
1 change: 0 additions & 1 deletion src/NServiceBus.Core/NServiceBus.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,5 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
<Import Project="Fody.targets" />
</Project>
1 change: 0 additions & 1 deletion src/NServiceBus/NServiceBus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,5 @@
<PropertyGroup>
<notused>copy /Y "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\..\build\"</notused>
</PropertyGroup>
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
<Import Project="Fody.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,4 @@
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,5 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
<Import Project="Fody.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
1 change: 0 additions & 1 deletion src/azure/NServiceBus.Azure/NServiceBus.Azure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,5 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
<Import Project="Fody.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,5 @@
<PropertyGroup>
<notused>xcopy /Y "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\..\..\build\host\"</notused>
</PropertyGroup>
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
<Import Project="Fody.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,4 @@
<notused>xcopy /Y "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\..\..\..\build\containers\"</notused>
<notused>xcopy /Y "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\..\..\..\build\containers\"</notused>
</PropertyGroup>
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,4 @@
<PropertyGroup>
<notused>xcopy /Y "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\..\..\..\build\containers\"</notused>
</PropertyGroup>
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,4 @@
<PropertyGroup>
<notused>copy /Y "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\..\..\..\build\"</notused>
</PropertyGroup>
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@
<PropertyGroup>
<notused>copy /Y "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\..\..\..\build\"</notused>
</PropertyGroup>
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,4 @@
<PropertyGroup>
<notused>xcopy /Y "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\..\..\..\build\containers\"</notused>
</PropertyGroup>
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,4 @@
<PropertyGroup>
<notused>xcopy /Y "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\..\..\..\build\containers\"</notused>
</PropertyGroup>
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,4 @@
<PropertyGroup>
<notused>xcopy /Y "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\..\..\..\build\XsdGenerator\"</notused>
</PropertyGroup>
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,5 @@
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
<Import Project="Fody.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,4 @@
<Target Name="AfterBuild">
</Target>
-->
<Import Project="$(SolutionDir)Tools\Pepita\PepitaGet.targets" />
</Project>
Loading

0 comments on commit 28c580d

Please sign in to comment.